blob: d0a5fde61a07b532a87b3ea3bb788100da2fe21b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <net/bluetooth/bluetooth.h>
37#include <net/bluetooth/hci_core.h>
38#include <net/bluetooth/l2cap.h>
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070039
Marcel Holtmannac4b7232013-10-10 14:54:16 -070040#include "smp.h"
Marcel Holtmann70247282013-10-10 14:54:15 -070041#include "a2mp.h"
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070042#include "amp.h"
Jukka Rissanen18722c22013-12-11 17:05:37 +020043#include "6lowpan.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;
Marcel Holtmannd40bffb2013-10-12 08:18:18 -070050static u8 l2cap_fixed_chan[8] = { L2CAP_FC_L2CAP | L2CAP_FC_CONNLESS, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Johannes Bergb5ad8b72011-06-01 08:54:45 +020052static LIST_HEAD(chan_list);
53static DEFINE_RWLOCK(chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Johan Hedbergf15b8ec2013-12-03 15:08:25 +020055static u16 le_max_credits = L2CAP_LE_MAX_CREDITS;
56static u16 le_default_mps = L2CAP_LE_DEFAULT_MPS;
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +010059 u8 code, u8 ident, u16 dlen, void *data);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -030060static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
Gustavo Padovan2d792812012-10-06 10:07:01 +010061 void *data);
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -030062static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +020063static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Gustavo Padovand6603662012-05-21 13:58:22 -030065static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
Gustavo Padovan2d792812012-10-06 10:07:01 +010066 struct sk_buff_head *skbs, u8 event);
Mat Martineau608bcc62012-05-17 20:53:32 -070067
Marcel Holtmann4f1654e2013-10-13 08:50:41 -070068static inline __u8 bdaddr_type(struct hci_conn *hcon, __u8 type)
69{
70 if (hcon->type == LE_LINK) {
71 if (type == ADDR_LE_DEV_PUBLIC)
72 return BDADDR_LE_PUBLIC;
73 else
74 return BDADDR_LE_RANDOM;
75 }
76
77 return BDADDR_BREDR;
78}
79
Marcel Holtmann01394182006-07-03 10:02:46 +020080/* ---- L2CAP channels ---- */
Gustavo F. Padovan71ba0e52011-05-17 14:34:52 -030081
Gustavo Padovan2d792812012-10-06 10:07:01 +010082static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
83 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +020084{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020085 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -030086
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020087 list_for_each_entry(c, &conn->chan_l, list) {
88 if (c->dcid == cid)
89 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +020090 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020091 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +020092}
93
Gustavo Padovan2d792812012-10-06 10:07:01 +010094static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn,
95 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +020096{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020097 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -030098
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020099 list_for_each_entry(c, &conn->chan_l, list) {
100 if (c->scid == cid)
101 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +0200102 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200103 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200104}
105
106/* Find channel with given SCID.
Mat Martineauef191ad2012-05-02 09:42:00 -0700107 * Returns locked channel. */
Gustavo Padovan2d792812012-10-06 10:07:01 +0100108static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn,
109 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +0200110{
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300111 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300112
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200113 mutex_lock(&conn->chan_lock);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300114 c = __l2cap_get_chan_by_scid(conn, cid);
Mat Martineauef191ad2012-05-02 09:42:00 -0700115 if (c)
116 l2cap_chan_lock(c);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200117 mutex_unlock(&conn->chan_lock);
118
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300119 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +0200120}
121
Mat Martineaub1a130b2012-10-23 15:24:09 -0700122/* Find channel with given DCID.
123 * Returns locked channel.
124 */
125static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
126 u16 cid)
127{
128 struct l2cap_chan *c;
129
130 mutex_lock(&conn->chan_lock);
131 c = __l2cap_get_chan_by_dcid(conn, cid);
132 if (c)
133 l2cap_chan_lock(c);
134 mutex_unlock(&conn->chan_lock);
135
136 return c;
137}
138
Gustavo Padovan2d792812012-10-06 10:07:01 +0100139static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn,
140 u8 ident)
Marcel Holtmann01394182006-07-03 10:02:46 +0200141{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200142 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300143
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200144 list_for_each_entry(c, &conn->chan_l, list) {
145 if (c->ident == ident)
146 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +0200147 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200148 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200149}
150
Mat Martineau5b155ef2012-10-23 15:24:14 -0700151static struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn,
152 u8 ident)
153{
154 struct l2cap_chan *c;
155
156 mutex_lock(&conn->chan_lock);
157 c = __l2cap_get_chan_by_ident(conn, ident);
158 if (c)
159 l2cap_chan_lock(c);
160 mutex_unlock(&conn->chan_lock);
161
162 return c;
163}
164
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300165static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300166{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300167 struct l2cap_chan *c;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300168
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300169 list_for_each_entry(c, &chan_list, global_l) {
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700170 if (c->sport == psm && !bacmp(&c->src, src))
Szymon Janc250938c2011-11-16 09:32:22 +0100171 return c;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300172 }
Szymon Janc250938c2011-11-16 09:32:22 +0100173 return NULL;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300174}
175
176int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm)
177{
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300178 int err;
179
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200180 write_lock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300181
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300182 if (psm && __l2cap_global_chan_by_addr(psm, src)) {
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300183 err = -EADDRINUSE;
184 goto done;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300185 }
186
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300187 if (psm) {
188 chan->psm = psm;
189 chan->sport = psm;
190 err = 0;
191 } else {
192 u16 p;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300193
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300194 err = -EINVAL;
195 for (p = 0x1001; p < 0x1100; p += 2)
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300196 if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300197 chan->psm = cpu_to_le16(p);
198 chan->sport = cpu_to_le16(p);
199 err = 0;
200 break;
201 }
202 }
203
204done:
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200205 write_unlock(&chan_list_lock);
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300206 return err;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300207}
208
209int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid)
210{
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200211 write_lock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300212
213 chan->scid = scid;
214
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200215 write_unlock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300216
217 return 0;
218}
219
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300220static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
Marcel Holtmann01394182006-07-03 10:02:46 +0200221{
Johan Hedberge77af752013-10-08 10:31:00 +0200222 u16 cid, dyn_end;
Marcel Holtmann01394182006-07-03 10:02:46 +0200223
Johan Hedberge77af752013-10-08 10:31:00 +0200224 if (conn->hcon->type == LE_LINK)
225 dyn_end = L2CAP_CID_LE_DYN_END;
226 else
227 dyn_end = L2CAP_CID_DYN_END;
228
229 for (cid = L2CAP_CID_DYN_START; cid < dyn_end; cid++) {
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300230 if (!__l2cap_get_chan_by_scid(conn, cid))
Marcel Holtmann01394182006-07-03 10:02:46 +0200231 return cid;
232 }
233
234 return 0;
235}
236
Gustavo Padovanf93fa272013-10-21 14:21:40 -0200237static void l2cap_state_change(struct l2cap_chan *chan, int state)
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300238{
Andrei Emeltchenko42d2d872012-02-17 11:40:57 +0200239 BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state),
Gustavo Padovan2d792812012-10-06 10:07:01 +0100240 state_to_string(state));
Gustavo F. Padovanbadaaa02011-11-23 20:11:46 -0200241
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300242 chan->state = state;
Gustavo Padovan53f52122013-10-15 19:24:45 -0300243 chan->ops->state_change(chan, state, 0);
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300244}
245
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300246static inline void l2cap_state_change_and_error(struct l2cap_chan *chan,
247 int state, int err)
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200248{
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300249 chan->state = state;
Gustavo Padovan53f52122013-10-15 19:24:45 -0300250 chan->ops->state_change(chan, chan->state, err);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200251}
252
253static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err)
254{
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300255 chan->ops->state_change(chan, chan->state, err);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200256}
257
Mat Martineau4239d162012-05-17 20:53:49 -0700258static void __set_retrans_timer(struct l2cap_chan *chan)
259{
260 if (!delayed_work_pending(&chan->monitor_timer) &&
261 chan->retrans_timeout) {
262 l2cap_set_timer(chan, &chan->retrans_timer,
263 msecs_to_jiffies(chan->retrans_timeout));
264 }
265}
266
267static void __set_monitor_timer(struct l2cap_chan *chan)
268{
269 __clear_retrans_timer(chan);
270 if (chan->monitor_timeout) {
271 l2cap_set_timer(chan, &chan->monitor_timer,
272 msecs_to_jiffies(chan->monitor_timeout));
273 }
274}
275
Mat Martineau608bcc62012-05-17 20:53:32 -0700276static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head,
277 u16 seq)
278{
279 struct sk_buff *skb;
280
281 skb_queue_walk(head, skb) {
282 if (bt_cb(skb)->control.txseq == seq)
283 return skb;
284 }
285
286 return NULL;
287}
288
Mat Martineau3c588192012-04-11 10:48:42 -0700289/* ---- L2CAP sequence number lists ---- */
290
291/* For ERTM, ordered lists of sequence numbers must be tracked for
292 * SREJ requests that are received and for frames that are to be
293 * retransmitted. These seq_list functions implement a singly-linked
294 * list in an array, where membership in the list can also be checked
295 * in constant time. Items can also be added to the tail of the list
296 * and removed from the head in constant time, without further memory
297 * allocs or frees.
298 */
299
300static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size)
301{
302 size_t alloc_size, i;
303
304 /* Allocated size is a power of 2 to map sequence numbers
305 * (which may be up to 14 bits) in to a smaller array that is
306 * sized for the negotiated ERTM transmit windows.
307 */
308 alloc_size = roundup_pow_of_two(size);
309
310 seq_list->list = kmalloc(sizeof(u16) * alloc_size, GFP_KERNEL);
311 if (!seq_list->list)
312 return -ENOMEM;
313
314 seq_list->mask = alloc_size - 1;
315 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
316 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
317 for (i = 0; i < alloc_size; i++)
318 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
319
320 return 0;
321}
322
323static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list)
324{
325 kfree(seq_list->list);
326}
327
328static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list,
329 u16 seq)
330{
331 /* Constant-time check for list membership */
332 return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR;
333}
334
Mat Martineau3c588192012-04-11 10:48:42 -0700335static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list)
336{
Johan Hedberg03a0c5d2014-01-18 21:32:59 +0200337 u16 seq = seq_list->head;
338 u16 mask = seq_list->mask;
339
340 seq_list->head = seq_list->list[seq & mask];
341 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR;
342
343 if (seq_list->head == L2CAP_SEQ_LIST_TAIL) {
344 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
345 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
346 }
347
348 return seq;
Mat Martineau3c588192012-04-11 10:48:42 -0700349}
350
351static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list)
352{
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300353 u16 i;
Mat Martineau3c588192012-04-11 10:48:42 -0700354
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300355 if (seq_list->head == L2CAP_SEQ_LIST_CLEAR)
356 return;
357
358 for (i = 0; i <= seq_list->mask; i++)
359 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
360
361 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
362 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
Mat Martineau3c588192012-04-11 10:48:42 -0700363}
364
365static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq)
366{
367 u16 mask = seq_list->mask;
368
369 /* All appends happen in constant time */
370
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300371 if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR)
372 return;
Mat Martineau3c588192012-04-11 10:48:42 -0700373
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300374 if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR)
375 seq_list->head = seq;
376 else
377 seq_list->list[seq_list->tail & mask] = seq;
378
379 seq_list->tail = seq;
380 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL;
Mat Martineau3c588192012-04-11 10:48:42 -0700381}
382
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300383static void l2cap_chan_timeout(struct work_struct *work)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300384{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300385 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100386 chan_timer.work);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200387 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300388 int reason;
389
Andrei Emeltchenkoe05dcc32012-02-17 11:40:56 +0200390 BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300391
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200392 mutex_lock(&conn->chan_lock);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200393 l2cap_chan_lock(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300394
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300395 if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300396 reason = ECONNREFUSED;
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300397 else if (chan->state == BT_CONNECT &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100398 chan->sec_level != BT_SECURITY_SDP)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300399 reason = ECONNREFUSED;
400 else
401 reason = ETIMEDOUT;
402
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300403 l2cap_chan_close(chan, reason);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300404
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200405 l2cap_chan_unlock(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300406
Gustavo Padovan80b98022012-05-27 22:27:51 -0300407 chan->ops->close(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200408 mutex_unlock(&conn->chan_lock);
409
Ulisses Furquim371fd832011-12-21 20:02:36 -0200410 l2cap_chan_put(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300411}
412
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -0300413struct l2cap_chan *l2cap_chan_create(void)
Marcel Holtmann01394182006-07-03 10:02:46 +0200414{
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300415 struct l2cap_chan *chan;
Marcel Holtmann01394182006-07-03 10:02:46 +0200416
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300417 chan = kzalloc(sizeof(*chan), GFP_ATOMIC);
418 if (!chan)
419 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200420
Andrei Emeltchenkoc03b3552012-02-21 12:54:56 +0200421 mutex_init(&chan->lock);
422
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200423 write_lock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300424 list_add(&chan->global_l, &chan_list);
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200425 write_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300426
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300427 INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300428
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300429 chan->state = BT_OPEN;
430
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530431 kref_init(&chan->kref);
Gustavo F. Padovan71ba0e52011-05-17 14:34:52 -0300432
Mat Martineau28270112012-05-17 21:14:09 -0700433 /* This flag is cleared in l2cap_chan_ready() */
434 set_bit(CONF_NOT_COMPLETE, &chan->conf_state);
435
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -0300436 BT_DBG("chan %p", chan);
Szymon Jancabc545b2011-11-03 16:05:44 +0100437
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300438 return chan;
Marcel Holtmann01394182006-07-03 10:02:46 +0200439}
440
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530441static void l2cap_chan_destroy(struct kref *kref)
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300442{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530443 struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref);
444
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530445 BT_DBG("chan %p", chan);
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_del(&chan->global_l);
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200449 write_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300450
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530451 kfree(chan);
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300452}
453
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530454void l2cap_chan_hold(struct l2cap_chan *c)
455{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530456 BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount));
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530457
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530458 kref_get(&c->kref);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530459}
460
461void l2cap_chan_put(struct l2cap_chan *c)
462{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530463 BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount));
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530464
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530465 kref_put(&c->kref, l2cap_chan_destroy);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530466}
467
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300468void l2cap_chan_set_defaults(struct l2cap_chan *chan)
469{
470 chan->fcs = L2CAP_FCS_CRC16;
471 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
472 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
473 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
Jukka Rissanen6a5e8162014-05-28 14:43:04 +0300474 chan->remote_max_tx = chan->max_tx;
475 chan->remote_tx_win = chan->tx_win;
Mat Martineauc20f8e32012-07-10 05:47:07 -0700476 chan->ack_win = L2CAP_DEFAULT_TX_WINDOW;
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300477 chan->sec_level = BT_SECURITY_LOW;
Jukka Rissanen6a5e8162014-05-28 14:43:04 +0300478 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
479 chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
480 chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
481 chan->conf_state = 0;
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300482
483 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
484}
485
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200486static void l2cap_le_flowctl_init(struct l2cap_chan *chan)
Johan Hedberg38319712013-05-17 12:49:23 +0300487{
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200488 chan->sdu = NULL;
489 chan->sdu_last_frag = NULL;
490 chan->sdu_len = 0;
Johan Hedberg0cd75f72013-05-17 13:09:05 +0300491 chan->tx_credits = 0;
Johan Hedbergf15b8ec2013-12-03 15:08:25 +0200492 chan->rx_credits = le_max_credits;
Johan Hedbergd1d79412014-01-27 15:11:33 -0800493 chan->mps = min_t(u16, chan->imtu, le_default_mps);
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200494
495 skb_queue_head_init(&chan->tx_q);
Johan Hedberg38319712013-05-17 12:49:23 +0300496}
497
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +0300498void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
Marcel Holtmann01394182006-07-03 10:02:46 +0200499{
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -0300500 BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
Andrei Emeltchenko097db762012-03-09 14:16:17 +0200501 __le16_to_cpu(chan->psm), chan->dcid);
Marcel Holtmann01394182006-07-03 10:02:46 +0200502
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +0200503 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100504
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300505 chan->conn = conn;
Marcel Holtmann01394182006-07-03 10:02:46 +0200506
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200507 switch (chan->chan_type) {
508 case L2CAP_CHAN_CONN_ORIENTED:
Johan Hedberg21626e62014-01-24 10:35:41 +0200509 /* Alloc CID for connection-oriented socket */
510 chan->scid = l2cap_alloc_cid(conn);
511 if (conn->hcon->type == ACL_LINK)
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300512 chan->omtu = L2CAP_DEFAULT_MTU;
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200513 break;
514
515 case L2CAP_CHAN_CONN_LESS:
Marcel Holtmann01394182006-07-03 10:02:46 +0200516 /* Connectionless socket */
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300517 chan->scid = L2CAP_CID_CONN_LESS;
518 chan->dcid = L2CAP_CID_CONN_LESS;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300519 chan->omtu = L2CAP_DEFAULT_MTU;
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200520 break;
521
Johan Hedberg2338a7e2014-01-24 10:35:40 +0200522 case L2CAP_CHAN_FIXED:
523 /* Caller will set CID and CID specific MTU values */
Andrei Emeltchenko416fa752012-05-29 13:59:16 +0300524 break;
525
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200526 default:
Marcel Holtmann01394182006-07-03 10:02:46 +0200527 /* Raw socket can send/recv signalling messages only */
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300528 chan->scid = L2CAP_CID_SIGNALING;
529 chan->dcid = L2CAP_CID_SIGNALING;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300530 chan->omtu = L2CAP_DEFAULT_MTU;
Marcel Holtmann01394182006-07-03 10:02:46 +0200531 }
532
Andrei Emeltchenko8f7975b2011-10-13 16:18:54 +0300533 chan->local_id = L2CAP_BESTEFFORT_ID;
534 chan->local_stype = L2CAP_SERV_BESTEFFORT;
535 chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE;
536 chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME;
537 chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT;
Andrei Emeltchenko8936fa62012-10-08 11:14:41 +0300538 chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO;
Andrei Emeltchenko8f7975b2011-10-13 16:18:54 +0300539
Ulisses Furquim371fd832011-12-21 20:02:36 -0200540 l2cap_chan_hold(chan);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300541
Johan Hedberg5ee98912013-04-29 19:35:43 +0300542 hci_conn_hold(conn->hcon);
543
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200544 list_add(&chan->list, &conn->chan_l);
Andrei Emeltchenko643162a2012-02-22 17:11:55 +0200545}
546
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300547void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
Andrei Emeltchenko643162a2012-02-22 17:11:55 +0200548{
549 mutex_lock(&conn->chan_lock);
550 __l2cap_chan_add(conn, chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200551 mutex_unlock(&conn->chan_lock);
Marcel Holtmann01394182006-07-03 10:02:46 +0200552}
553
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300554void l2cap_chan_del(struct l2cap_chan *chan, int err)
Marcel Holtmann01394182006-07-03 10:02:46 +0200555{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300556 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann01394182006-07-03 10:02:46 +0200557
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -0300558 __clear_chan_timer(chan);
Marcel Holtmann01394182006-07-03 10:02:46 +0200559
Gustavo F. Padovan49208c92011-04-04 15:59:54 -0300560 BT_DBG("chan %p, conn %p, err %d", chan, conn, err);
Marcel Holtmann01394182006-07-03 10:02:46 +0200561
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900562 if (conn) {
Andrei Emeltchenko56f60982012-10-15 11:58:44 +0300563 struct amp_mgr *mgr = conn->hcon->amp_mgr;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300564 /* Delete from channel list */
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200565 list_del(&chan->list);
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -0200566
Ulisses Furquim371fd832011-12-21 20:02:36 -0200567 l2cap_chan_put(chan);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300568
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300569 chan->conn = NULL;
Andrei Emeltchenko3cabbfd2012-05-31 11:01:37 +0300570
Johan Hedberg2338a7e2014-01-24 10:35:40 +0200571 if (chan->scid != L2CAP_CID_A2MP)
David Herrmann76a68ba2013-04-06 20:28:37 +0200572 hci_conn_drop(conn->hcon);
Andrei Emeltchenko56f60982012-10-15 11:58:44 +0300573
574 if (mgr && mgr->bredr_chan == chan)
575 mgr->bredr_chan = NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200576 }
577
Andrei Emeltchenko419e08c2012-10-31 15:46:34 +0200578 if (chan->hs_hchan) {
579 struct hci_chan *hs_hchan = chan->hs_hchan;
580
581 BT_DBG("chan %p disconnect hs_hchan %p", chan, hs_hchan);
582 amp_disconnect_logical_link(hs_hchan);
583 }
584
Gustavo Padovanb699ec02012-10-06 11:51:54 +0100585 chan->ops->teardown(chan, err);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200586
Mat Martineau28270112012-05-17 21:14:09 -0700587 if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state))
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300588 return;
Gustavo F. Padovan2ead70b2011-04-01 15:13:36 -0300589
Gustavo Padovanee556f62012-05-18 20:22:38 -0300590 switch(chan->mode) {
591 case L2CAP_MODE_BASIC:
592 break;
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300593
Johan Hedberg38319712013-05-17 12:49:23 +0300594 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedberg177f8f22013-05-31 17:54:51 +0300595 skb_queue_purge(&chan->tx_q);
Johan Hedberg38319712013-05-17 12:49:23 +0300596 break;
597
Gustavo Padovanee556f62012-05-18 20:22:38 -0300598 case L2CAP_MODE_ERTM:
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -0300599 __clear_retrans_timer(chan);
600 __clear_monitor_timer(chan);
601 __clear_ack_timer(chan);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300602
Gustavo F. Padovanf1c67752011-03-25 20:36:10 -0300603 skb_queue_purge(&chan->srej_q);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300604
Mat Martineau3c588192012-04-11 10:48:42 -0700605 l2cap_seq_list_free(&chan->srej_list);
606 l2cap_seq_list_free(&chan->retrans_list);
Gustavo Padovanee556f62012-05-18 20:22:38 -0300607
608 /* fall through */
609
610 case L2CAP_MODE_STREAMING:
611 skb_queue_purge(&chan->tx_q);
612 break;
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300613 }
Gustavo Padovanee556f62012-05-18 20:22:38 -0300614
615 return;
Marcel Holtmann01394182006-07-03 10:02:46 +0200616}
617
Johan Hedberg387a33e2014-02-18 21:41:33 +0200618void l2cap_conn_update_id_addr(struct hci_conn *hcon)
619{
620 struct l2cap_conn *conn = hcon->l2cap_data;
621 struct l2cap_chan *chan;
622
623 mutex_lock(&conn->chan_lock);
624
625 list_for_each_entry(chan, &conn->chan_l, list) {
626 l2cap_chan_lock(chan);
627 bacpy(&chan->dst, &hcon->dst);
628 chan->dst_type = bdaddr_type(hcon, hcon->dst_type);
629 l2cap_chan_unlock(chan);
630 }
631
632 mutex_unlock(&conn->chan_lock);
633}
634
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300635static void l2cap_chan_le_connect_reject(struct l2cap_chan *chan)
636{
637 struct l2cap_conn *conn = chan->conn;
638 struct l2cap_le_conn_rsp rsp;
639 u16 result;
640
641 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
642 result = L2CAP_CR_AUTHORIZATION;
643 else
644 result = L2CAP_CR_BAD_PSM;
645
646 l2cap_state_change(chan, BT_DISCONN);
647
648 rsp.dcid = cpu_to_le16(chan->scid);
649 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +0200650 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +0300651 rsp.credits = cpu_to_le16(chan->rx_credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300652 rsp.result = cpu_to_le16(result);
653
654 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
655 &rsp);
656}
657
Johan Hedberg791d60f2013-05-14 22:24:44 +0300658static void l2cap_chan_connect_reject(struct l2cap_chan *chan)
659{
660 struct l2cap_conn *conn = chan->conn;
661 struct l2cap_conn_rsp rsp;
662 u16 result;
663
664 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
665 result = L2CAP_CR_SEC_BLOCK;
666 else
667 result = L2CAP_CR_BAD_PSM;
668
669 l2cap_state_change(chan, BT_DISCONN);
670
671 rsp.scid = cpu_to_le16(chan->dcid);
672 rsp.dcid = cpu_to_le16(chan->scid);
673 rsp.result = cpu_to_le16(result);
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700674 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Johan Hedberg791d60f2013-05-14 22:24:44 +0300675
676 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
677}
678
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300679void l2cap_chan_close(struct l2cap_chan *chan, int reason)
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300680{
681 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300682
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700683 BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300684
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300685 switch (chan->state) {
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300686 case BT_LISTEN:
Gustavo Padovanb699ec02012-10-06 11:51:54 +0100687 chan->ops->teardown(chan, 0);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300688 break;
689
690 case BT_CONNECTED:
691 case BT_CONFIG:
Johan Hedberg7b25c9b2014-01-28 15:28:04 -0800692 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
Gustavo Padovan8d836d72013-10-15 19:24:47 -0300693 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +0200694 l2cap_send_disconn_req(chan, reason);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300695 } else
696 l2cap_chan_del(chan, reason);
697 break;
698
699 case BT_CONNECT2:
Johan Hedberg791d60f2013-05-14 22:24:44 +0300700 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
701 if (conn->hcon->type == ACL_LINK)
702 l2cap_chan_connect_reject(chan);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300703 else if (conn->hcon->type == LE_LINK)
704 l2cap_chan_le_connect_reject(chan);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300705 }
706
707 l2cap_chan_del(chan, reason);
708 break;
709
710 case BT_CONNECT:
711 case BT_DISCONN:
712 l2cap_chan_del(chan, reason);
713 break;
714
715 default:
Gustavo Padovanb699ec02012-10-06 11:51:54 +0100716 chan->ops->teardown(chan, 0);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300717 break;
718 }
719}
720
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300721static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
Johan Hedberg8556edd32011-01-19 12:06:50 +0530722{
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700723 switch (chan->chan_type) {
724 case L2CAP_CHAN_RAW:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300725 switch (chan->sec_level) {
Johan Hedberg8556edd32011-01-19 12:06:50 +0530726 case BT_SECURITY_HIGH:
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800727 case BT_SECURITY_FIPS:
Johan Hedberg8556edd32011-01-19 12:06:50 +0530728 return HCI_AT_DEDICATED_BONDING_MITM;
729 case BT_SECURITY_MEDIUM:
730 return HCI_AT_DEDICATED_BONDING;
731 default:
732 return HCI_AT_NO_BONDING;
733 }
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700734 break;
Marcel Holtmann3124b842013-10-12 07:19:32 -0700735 case L2CAP_CHAN_CONN_LESS:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700736 if (chan->psm == cpu_to_le16(L2CAP_PSM_3DSP)) {
Marcel Holtmann3124b842013-10-12 07:19:32 -0700737 if (chan->sec_level == BT_SECURITY_LOW)
738 chan->sec_level = BT_SECURITY_SDP;
739 }
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800740 if (chan->sec_level == BT_SECURITY_HIGH ||
741 chan->sec_level == BT_SECURITY_FIPS)
Marcel Holtmann3124b842013-10-12 07:19:32 -0700742 return HCI_AT_NO_BONDING_MITM;
743 else
744 return HCI_AT_NO_BONDING;
745 break;
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700746 case L2CAP_CHAN_CONN_ORIENTED:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700747 if (chan->psm == cpu_to_le16(L2CAP_PSM_SDP)) {
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700748 if (chan->sec_level == BT_SECURITY_LOW)
749 chan->sec_level = BT_SECURITY_SDP;
Johan Hedberg8556edd32011-01-19 12:06:50 +0530750
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800751 if (chan->sec_level == BT_SECURITY_HIGH ||
752 chan->sec_level == BT_SECURITY_FIPS)
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700753 return HCI_AT_NO_BONDING_MITM;
754 else
755 return HCI_AT_NO_BONDING;
756 }
757 /* fall through */
758 default:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300759 switch (chan->sec_level) {
Johan Hedberg8556edd32011-01-19 12:06:50 +0530760 case BT_SECURITY_HIGH:
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800761 case BT_SECURITY_FIPS:
Johan Hedberg8556edd32011-01-19 12:06:50 +0530762 return HCI_AT_GENERAL_BONDING_MITM;
763 case BT_SECURITY_MEDIUM:
764 return HCI_AT_GENERAL_BONDING;
765 default:
766 return HCI_AT_NO_BONDING;
767 }
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700768 break;
Johan Hedberg8556edd32011-01-19 12:06:50 +0530769 }
770}
771
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200772/* Service level security */
Gustavo F. Padovand45fc422011-11-05 19:54:24 -0200773int l2cap_chan_check_security(struct l2cap_chan *chan)
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200774{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300775 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100776 __u8 auth_type;
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200777
Johan Hedberga17de2f2013-05-14 13:25:37 +0300778 if (conn->hcon->type == LE_LINK)
779 return smp_conn_security(conn->hcon, chan->sec_level);
780
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300781 auth_type = l2cap_get_auth_type(chan);
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100782
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300783 return hci_conn_security(conn->hcon, chan->sec_level, auth_type);
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200784}
785
Johannes Bergb5ad8b72011-06-01 08:54:45 +0200786static u8 l2cap_get_ident(struct l2cap_conn *conn)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200787{
788 u8 id;
789
790 /* Get next available identificator.
791 * 1 - 128 are used by kernel.
792 * 129 - 199 are reserved.
793 * 200 - 254 are used by utilities like l2ping, etc.
794 */
795
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200796 spin_lock(&conn->lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200797
798 if (++conn->tx_ident > 128)
799 conn->tx_ident = 1;
800
801 id = conn->tx_ident;
802
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200803 spin_unlock(&conn->lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200804
805 return id;
806}
807
Gustavo Padovan2d792812012-10-06 10:07:01 +0100808static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
809 void *data)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200810{
811 struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +0200812 u8 flags;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200813
814 BT_DBG("code 0x%2.2x", code);
815
816 if (!skb)
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -0300817 return;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200818
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +0200819 if (lmp_no_flush_capable(conn->hcon->hdev))
820 flags = ACL_START_NO_FLUSH;
821 else
822 flags = ACL_START;
823
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700824 bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +0200825 skb->priority = HCI_PRIO_MAX;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700826
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200827 hci_send_acl(conn->hchan, skb, flags);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200828}
829
Mat Martineau02b0fbb2012-10-23 15:24:10 -0700830static bool __chan_is_moving(struct l2cap_chan *chan)
831{
832 return chan->move_state != L2CAP_MOVE_STABLE &&
833 chan->move_state != L2CAP_MOVE_WAIT_PREPARE;
834}
835
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200836static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
837{
838 struct hci_conn *hcon = chan->conn->hcon;
839 u16 flags;
840
841 BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100842 skb->priority);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200843
Mat Martineaud5f8a752012-10-23 15:24:18 -0700844 if (chan->hs_hcon && !__chan_is_moving(chan)) {
845 if (chan->hs_hchan)
846 hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE);
847 else
848 kfree_skb(skb);
849
850 return;
851 }
852
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200853 if (!test_bit(FLAG_FLUSHABLE, &chan->flags) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100854 lmp_no_flush_capable(hcon->hdev))
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200855 flags = ACL_START_NO_FLUSH;
856 else
857 flags = ACL_START;
858
859 bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
860 hci_send_acl(chan->conn->hchan, skb, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700863static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control)
864{
865 control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT;
866 control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT;
867
868 if (enh & L2CAP_CTRL_FRAME_TYPE) {
869 /* S-Frame */
870 control->sframe = 1;
871 control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT;
872 control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT;
873
874 control->sar = 0;
875 control->txseq = 0;
876 } else {
877 /* I-Frame */
878 control->sframe = 0;
879 control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT;
880 control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT;
881
882 control->poll = 0;
883 control->super = 0;
884 }
885}
886
887static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control)
888{
889 control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT;
890 control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT;
891
892 if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) {
893 /* S-Frame */
894 control->sframe = 1;
895 control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT;
896 control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT;
897
898 control->sar = 0;
899 control->txseq = 0;
900 } else {
901 /* I-Frame */
902 control->sframe = 0;
903 control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT;
904 control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT;
905
906 control->poll = 0;
907 control->super = 0;
908 }
909}
910
911static inline void __unpack_control(struct l2cap_chan *chan,
912 struct sk_buff *skb)
913{
914 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
915 __unpack_extended_control(get_unaligned_le32(skb->data),
916 &bt_cb(skb)->control);
Mat Martineaucec8ab6e2012-05-17 20:53:36 -0700917 skb_pull(skb, L2CAP_EXT_CTRL_SIZE);
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700918 } else {
919 __unpack_enhanced_control(get_unaligned_le16(skb->data),
920 &bt_cb(skb)->control);
Mat Martineaucec8ab6e2012-05-17 20:53:36 -0700921 skb_pull(skb, L2CAP_ENH_CTRL_SIZE);
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700922 }
923}
924
925static u32 __pack_extended_control(struct l2cap_ctrl *control)
926{
927 u32 packed;
928
929 packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT;
930 packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT;
931
932 if (control->sframe) {
933 packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT;
934 packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT;
935 packed |= L2CAP_EXT_CTRL_FRAME_TYPE;
936 } else {
937 packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT;
938 packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT;
939 }
940
941 return packed;
942}
943
944static u16 __pack_enhanced_control(struct l2cap_ctrl *control)
945{
946 u16 packed;
947
948 packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT;
949 packed |= control->final << L2CAP_CTRL_FINAL_SHIFT;
950
951 if (control->sframe) {
952 packed |= control->poll << L2CAP_CTRL_POLL_SHIFT;
953 packed |= control->super << L2CAP_CTRL_SUPER_SHIFT;
954 packed |= L2CAP_CTRL_FRAME_TYPE;
955 } else {
956 packed |= control->sar << L2CAP_CTRL_SAR_SHIFT;
957 packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT;
958 }
959
960 return packed;
961}
962
963static inline void __pack_control(struct l2cap_chan *chan,
964 struct l2cap_ctrl *control,
965 struct sk_buff *skb)
966{
967 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
968 put_unaligned_le32(__pack_extended_control(control),
969 skb->data + L2CAP_HDR_SIZE);
970 } else {
971 put_unaligned_le16(__pack_enhanced_control(control),
972 skb->data + L2CAP_HDR_SIZE);
973 }
974}
975
Gustavo Padovanba7aa642012-05-29 13:29:16 -0300976static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan)
977{
978 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
979 return L2CAP_EXT_HDR_SIZE;
980 else
981 return L2CAP_ENH_HDR_SIZE;
982}
983
Mat Martineaua67d7f62012-05-17 20:53:35 -0700984static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan,
985 u32 control)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300986{
987 struct sk_buff *skb;
988 struct l2cap_hdr *lh;
Gustavo Padovanba7aa642012-05-29 13:29:16 -0300989 int hlen = __ertm_hdr_size(chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300990
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300991 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +0300992 hlen += L2CAP_FCS_SIZE;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300993
Mat Martineaua67d7f62012-05-17 20:53:35 -0700994 skb = bt_skb_alloc(hlen, GFP_KERNEL);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300995
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300996 if (!skb)
Mat Martineaua67d7f62012-05-17 20:53:35 -0700997 return ERR_PTR(-ENOMEM);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300998
999 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001000 lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001001 lh->cid = cpu_to_le16(chan->dcid);
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03001002
Mat Martineaua67d7f62012-05-17 20:53:35 -07001003 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1004 put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
1005 else
1006 put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001007
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001008 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineaua67d7f62012-05-17 20:53:35 -07001009 u16 fcs = crc16(0, (u8 *)skb->data, skb->len);
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03001010 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001011 }
1012
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001013 skb->priority = HCI_PRIO_MAX;
Mat Martineaua67d7f62012-05-17 20:53:35 -07001014 return skb;
1015}
1016
1017static void l2cap_send_sframe(struct l2cap_chan *chan,
1018 struct l2cap_ctrl *control)
1019{
1020 struct sk_buff *skb;
1021 u32 control_field;
1022
1023 BT_DBG("chan %p, control %p", chan, control);
1024
1025 if (!control->sframe)
1026 return;
1027
Mat Martineaub99e13a2012-10-23 15:24:19 -07001028 if (__chan_is_moving(chan))
1029 return;
1030
Mat Martineaua67d7f62012-05-17 20:53:35 -07001031 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) &&
1032 !control->poll)
1033 control->final = 1;
1034
1035 if (control->super == L2CAP_SUPER_RR)
1036 clear_bit(CONN_RNR_SENT, &chan->conn_state);
1037 else if (control->super == L2CAP_SUPER_RNR)
1038 set_bit(CONN_RNR_SENT, &chan->conn_state);
1039
1040 if (control->super != L2CAP_SUPER_SREJ) {
1041 chan->last_acked_seq = control->reqseq;
1042 __clear_ack_timer(chan);
1043 }
1044
1045 BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq,
1046 control->final, control->poll, control->super);
1047
1048 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1049 control_field = __pack_extended_control(control);
1050 else
1051 control_field = __pack_enhanced_control(control);
1052
1053 skb = l2cap_create_sframe_pdu(chan, control_field);
1054 if (!IS_ERR(skb))
1055 l2cap_do_send(chan, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001056}
1057
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001058static void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, bool poll)
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001059{
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001060 struct l2cap_ctrl control;
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001061
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001062 BT_DBG("chan %p, poll %d", chan, poll);
1063
1064 memset(&control, 0, sizeof(control));
1065 control.sframe = 1;
1066 control.poll = poll;
1067
1068 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
1069 control.super = L2CAP_SUPER_RNR;
1070 else
1071 control.super = L2CAP_SUPER_RR;
1072
1073 control.reqseq = chan->buffer_seq;
1074 l2cap_send_sframe(chan, &control);
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001075}
1076
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001077static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
Andrei Emeltchenkoe501d052010-07-08 12:14:41 +03001078{
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001079 return !test_bit(CONF_CONNECT_PEND, &chan->conf_state);
Andrei Emeltchenkoe501d052010-07-08 12:14:41 +03001080}
1081
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001082static bool __amp_capable(struct l2cap_chan *chan)
1083{
1084 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann1df7b172013-10-05 11:47:49 -07001085 struct hci_dev *hdev;
1086 bool amp_available = false;
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001087
Marcel Holtmann1df7b172013-10-05 11:47:49 -07001088 if (!conn->hs_enabled)
1089 return false;
1090
1091 if (!(conn->fixed_chan_mask & L2CAP_FC_A2MP))
1092 return false;
1093
1094 read_lock(&hci_dev_list_lock);
1095 list_for_each_entry(hdev, &hci_dev_list, list) {
1096 if (hdev->amp_type != AMP_TYPE_BREDR &&
1097 test_bit(HCI_UP, &hdev->flags)) {
1098 amp_available = true;
1099 break;
1100 }
1101 }
1102 read_unlock(&hci_dev_list_lock);
1103
1104 if (chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED)
1105 return amp_available;
Marcel Holtmann848566b2013-10-01 22:59:22 -07001106
1107 return false;
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001108}
1109
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +02001110static bool l2cap_check_efs(struct l2cap_chan *chan)
1111{
1112 /* Check EFS parameters */
1113 return true;
1114}
1115
Andrei Emeltchenko2766be42012-09-27 17:26:21 +03001116void l2cap_send_conn_req(struct l2cap_chan *chan)
Andrei Emeltchenko9b27f352012-02-24 16:00:00 +02001117{
1118 struct l2cap_conn *conn = chan->conn;
1119 struct l2cap_conn_req req;
1120
1121 req.scid = cpu_to_le16(chan->scid);
1122 req.psm = chan->psm;
1123
1124 chan->ident = l2cap_get_ident(conn);
1125
1126 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
1127
1128 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req);
1129}
1130
Mat Martineau8eb200b2012-10-23 15:24:17 -07001131static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id)
1132{
1133 struct l2cap_create_chan_req req;
1134 req.scid = cpu_to_le16(chan->scid);
1135 req.psm = chan->psm;
1136 req.amp_id = amp_id;
1137
1138 chan->ident = l2cap_get_ident(chan->conn);
1139
1140 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ,
1141 sizeof(req), &req);
1142}
1143
Mat Martineau02b0fbb2012-10-23 15:24:10 -07001144static void l2cap_move_setup(struct l2cap_chan *chan)
1145{
1146 struct sk_buff *skb;
1147
1148 BT_DBG("chan %p", chan);
1149
1150 if (chan->mode != L2CAP_MODE_ERTM)
1151 return;
1152
1153 __clear_retrans_timer(chan);
1154 __clear_monitor_timer(chan);
1155 __clear_ack_timer(chan);
1156
1157 chan->retry_count = 0;
1158 skb_queue_walk(&chan->tx_q, skb) {
1159 if (bt_cb(skb)->control.retries)
1160 bt_cb(skb)->control.retries = 1;
1161 else
1162 break;
1163 }
1164
1165 chan->expected_tx_seq = chan->buffer_seq;
1166
1167 clear_bit(CONN_REJ_ACT, &chan->conn_state);
1168 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
1169 l2cap_seq_list_clear(&chan->retrans_list);
1170 l2cap_seq_list_clear(&chan->srej_list);
1171 skb_queue_purge(&chan->srej_q);
1172
1173 chan->tx_state = L2CAP_TX_STATE_XMIT;
1174 chan->rx_state = L2CAP_RX_STATE_MOVE;
1175
1176 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
1177}
1178
Mat Martineau5f3847a2012-10-23 15:24:12 -07001179static void l2cap_move_done(struct l2cap_chan *chan)
1180{
1181 u8 move_role = chan->move_role;
1182 BT_DBG("chan %p", chan);
1183
1184 chan->move_state = L2CAP_MOVE_STABLE;
1185 chan->move_role = L2CAP_MOVE_ROLE_NONE;
1186
1187 if (chan->mode != L2CAP_MODE_ERTM)
1188 return;
1189
1190 switch (move_role) {
1191 case L2CAP_MOVE_ROLE_INITIATOR:
1192 l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL);
1193 chan->rx_state = L2CAP_RX_STATE_WAIT_F;
1194 break;
1195 case L2CAP_MOVE_ROLE_RESPONDER:
1196 chan->rx_state = L2CAP_RX_STATE_WAIT_P;
1197 break;
1198 }
1199}
1200
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001201static void l2cap_chan_ready(struct l2cap_chan *chan)
1202{
Mat Martineau28270112012-05-17 21:14:09 -07001203 /* This clears all conf flags, including CONF_NOT_COMPLETE */
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001204 chan->conf_state = 0;
1205 __clear_chan_timer(chan);
1206
Johan Hedberg0ce43ce2013-12-05 14:55:33 +02001207 if (chan->mode == L2CAP_MODE_LE_FLOWCTL && !chan->tx_credits)
1208 chan->ops->suspend(chan);
Johan Hedberg177f8f22013-05-31 17:54:51 +03001209
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001210 chan->state = BT_CONNECTED;
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001211
Andrei Emeltchenkofd83e2c2012-05-30 09:55:32 +03001212 chan->ops->ready(chan);
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001213}
1214
Johan Hedbergf1496de2013-05-13 14:15:56 +03001215static void l2cap_le_connect(struct l2cap_chan *chan)
1216{
1217 struct l2cap_conn *conn = chan->conn;
1218 struct l2cap_le_conn_req req;
1219
Johan Hedberg595177f2013-12-02 22:12:22 +02001220 if (test_and_set_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags))
1221 return;
1222
Johan Hedbergf1496de2013-05-13 14:15:56 +03001223 req.psm = chan->psm;
1224 req.scid = cpu_to_le16(chan->scid);
1225 req.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02001226 req.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03001227 req.credits = cpu_to_le16(chan->rx_credits);
Johan Hedbergf1496de2013-05-13 14:15:56 +03001228
1229 chan->ident = l2cap_get_ident(conn);
1230
1231 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_REQ,
1232 sizeof(req), &req);
1233}
1234
1235static void l2cap_le_start(struct l2cap_chan *chan)
1236{
1237 struct l2cap_conn *conn = chan->conn;
1238
1239 if (!smp_conn_security(conn->hcon, chan->sec_level))
1240 return;
1241
1242 if (!chan->psm) {
1243 l2cap_chan_ready(chan);
1244 return;
1245 }
1246
1247 if (chan->state == BT_CONNECT)
1248 l2cap_le_connect(chan);
1249}
1250
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001251static void l2cap_start_connection(struct l2cap_chan *chan)
1252{
1253 if (__amp_capable(chan)) {
1254 BT_DBG("chan %p AMP capable: discover AMPs", chan);
1255 a2mp_discover_amp(chan);
Johan Hedbergf1496de2013-05-13 14:15:56 +03001256 } else if (chan->conn->hcon->type == LE_LINK) {
1257 l2cap_le_start(chan);
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001258 } else {
1259 l2cap_send_conn_req(chan);
1260 }
1261}
1262
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001263static void l2cap_do_start(struct l2cap_chan *chan)
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001264{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001265 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001266
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001267 if (conn->hcon->type == LE_LINK) {
Johan Hedberg96ac34f2013-05-13 11:15:07 +03001268 l2cap_le_start(chan);
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001269 return;
1270 }
1271
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001272 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
Marcel Holtmann984947d2009-02-06 23:35:19 +01001273 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
1274 return;
1275
Gustavo F. Padovand45fc422011-11-05 19:54:24 -02001276 if (l2cap_chan_check_security(chan) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01001277 __l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001278 l2cap_start_connection(chan);
1279 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001280 } else {
1281 struct l2cap_info_req req;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001282 req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001283
1284 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
1285 conn->info_ident = l2cap_get_ident(conn);
1286
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08001287 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001288
Gustavo Padovan2d792812012-10-06 10:07:01 +01001289 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
1290 sizeof(req), &req);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001291 }
1292}
1293
Gustavo F. Padovancf6c2c02010-06-07 20:54:45 -03001294static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
1295{
1296 u32 local_feat_mask = l2cap_feat_mask;
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03001297 if (!disable_ertm)
Gustavo F. Padovancf6c2c02010-06-07 20:54:45 -03001298 local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
1299
1300 switch (mode) {
1301 case L2CAP_MODE_ERTM:
1302 return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
1303 case L2CAP_MODE_STREAMING:
1304 return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
1305 default:
1306 return 0x00;
1307 }
1308}
1309
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001310static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err)
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001311{
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001312 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001313 struct l2cap_disconn_req req;
1314
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001315 if (!conn)
1316 return;
1317
Andrei Emeltchenkoaad3d0e2012-09-06 15:05:42 +03001318 if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) {
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -03001319 __clear_retrans_timer(chan);
1320 __clear_monitor_timer(chan);
1321 __clear_ack_timer(chan);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001322 }
1323
Johan Hedberg2338a7e2014-01-24 10:35:40 +02001324 if (chan->scid == L2CAP_CID_A2MP) {
Gustavo Padovand1177732012-10-06 11:47:38 +01001325 l2cap_state_change(chan, BT_DISCONN);
Andrei Emeltchenko416fa752012-05-29 13:59:16 +03001326 return;
1327 }
1328
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001329 req.dcid = cpu_to_le16(chan->dcid);
1330 req.scid = cpu_to_le16(chan->scid);
Gustavo Padovan2d792812012-10-06 10:07:01 +01001331 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ,
1332 sizeof(req), &req);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001333
Gustavo Padovanf8e73012013-10-15 19:24:46 -03001334 l2cap_state_change_and_error(chan, BT_DISCONN, err);
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001335}
1336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337/* ---- L2CAP connections ---- */
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001338static void l2cap_conn_start(struct l2cap_conn *conn)
1339{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001340 struct l2cap_chan *chan, *tmp;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001341
1342 BT_DBG("conn %p", conn);
1343
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001344 mutex_lock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001345
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001346 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001347 l2cap_chan_lock(chan);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001348
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001349 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001350 l2cap_chan_unlock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001351 continue;
1352 }
1353
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001354 if (chan->state == BT_CONNECT) {
Gustavo F. Padovand45fc422011-11-05 19:54:24 -02001355 if (!l2cap_chan_check_security(chan) ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01001356 !__l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001357 l2cap_chan_unlock(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001358 continue;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02001359 }
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001360
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001361 if (!l2cap_mode_supported(chan->mode, conn->feat_mask)
Gustavo Padovan2d792812012-10-06 10:07:01 +01001362 && test_bit(CONF_STATE2_DEVICE,
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001363 &chan->conf_state)) {
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001364 l2cap_chan_close(chan, ECONNRESET);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001365 l2cap_chan_unlock(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001366 continue;
1367 }
1368
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001369 l2cap_start_connection(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001370
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001371 } else if (chan->state == BT_CONNECT2) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001372 struct l2cap_conn_rsp rsp;
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001373 char buf[128];
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001374 rsp.scid = cpu_to_le16(chan->dcid);
1375 rsp.dcid = cpu_to_le16(chan->scid);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001376
Gustavo F. Padovand45fc422011-11-05 19:54:24 -02001377 if (l2cap_chan_check_security(chan)) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07001378 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001379 rsp.result = cpu_to_le16(L2CAP_CR_PEND);
1380 rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08001381 chan->ops->defer(chan);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001382
1383 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02001384 l2cap_state_change(chan, BT_CONFIG);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001385 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
1386 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001387 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001388 } else {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001389 rsp.result = cpu_to_le16(L2CAP_CR_PEND);
1390 rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001391 }
1392
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001393 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001394 sizeof(rsp), &rsp);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001395
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001396 if (test_bit(CONF_REQ_SENT, &chan->conf_state) ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01001397 rsp.result != L2CAP_CR_SUCCESS) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001398 l2cap_chan_unlock(chan);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001399 continue;
1400 }
1401
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001402 set_bit(CONF_REQ_SENT, &chan->conf_state);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001403 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001404 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03001405 chan->num_conf_req++;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001406 }
1407
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001408 l2cap_chan_unlock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001409 }
1410
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001411 mutex_unlock(&conn->chan_lock);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001412}
1413
Ido Yarivc2287682012-04-20 15:46:07 -03001414/* Find socket with cid and source/destination bdaddr.
Ville Tervob62f3282011-02-10 22:38:50 -03001415 * Returns closest match, locked.
1416 */
Andrei Emeltchenkod9b88702012-03-12 12:13:08 +02001417static struct l2cap_chan *l2cap_global_chan_by_scid(int state, u16 cid,
Ido Yarivc2287682012-04-20 15:46:07 -03001418 bdaddr_t *src,
1419 bdaddr_t *dst)
Ville Tervob62f3282011-02-10 22:38:50 -03001420{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001421 struct l2cap_chan *c, *c1 = NULL;
Ville Tervob62f3282011-02-10 22:38:50 -03001422
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001423 read_lock(&chan_list_lock);
Ville Tervob62f3282011-02-10 22:38:50 -03001424
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001425 list_for_each_entry(c, &chan_list, global_l) {
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001426 if (state && c->state != state)
Ville Tervob62f3282011-02-10 22:38:50 -03001427 continue;
1428
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001429 if (c->scid == cid) {
Ido Yarivc2287682012-04-20 15:46:07 -03001430 int src_match, dst_match;
1431 int src_any, dst_any;
1432
Ville Tervob62f3282011-02-10 22:38:50 -03001433 /* Exact match. */
Marcel Holtmann7eafc592013-10-13 08:12:47 -07001434 src_match = !bacmp(&c->src, src);
1435 dst_match = !bacmp(&c->dst, dst);
Ido Yarivc2287682012-04-20 15:46:07 -03001436 if (src_match && dst_match) {
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001437 read_unlock(&chan_list_lock);
1438 return c;
1439 }
Ville Tervob62f3282011-02-10 22:38:50 -03001440
1441 /* Closest match */
Marcel Holtmann7eafc592013-10-13 08:12:47 -07001442 src_any = !bacmp(&c->src, BDADDR_ANY);
1443 dst_any = !bacmp(&c->dst, BDADDR_ANY);
Ido Yarivc2287682012-04-20 15:46:07 -03001444 if ((src_match && dst_any) || (src_any && dst_match) ||
1445 (src_any && dst_any))
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001446 c1 = c;
Ville Tervob62f3282011-02-10 22:38:50 -03001447 }
1448 }
Gustavo F. Padovan280f2942011-04-13 19:01:22 -03001449
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001450 read_unlock(&chan_list_lock);
Ville Tervob62f3282011-02-10 22:38:50 -03001451
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001452 return c1;
Ville Tervob62f3282011-02-10 22:38:50 -03001453}
1454
1455static void l2cap_le_conn_ready(struct l2cap_conn *conn)
1456{
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07001457 struct hci_conn *hcon = conn->hcon;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001458 struct l2cap_chan *chan, *pchan;
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07001459 u8 dst_type;
Ville Tervob62f3282011-02-10 22:38:50 -03001460
1461 BT_DBG("");
1462
Jukka Rissanen18722c22013-12-11 17:05:37 +02001463 bt_6lowpan_add_conn(conn);
1464
Ville Tervob62f3282011-02-10 22:38:50 -03001465 /* Check if we have socket listening on cid */
Johan Hedberg073d1cf2013-04-29 19:35:35 +03001466 pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_ATT,
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07001467 &hcon->src, &hcon->dst);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001468 if (!pchan)
Ville Tervob62f3282011-02-10 22:38:50 -03001469 return;
1470
Johan Hedberg44f3b0f2013-04-29 19:35:45 +03001471 /* Client ATT sockets should override the server one */
1472 if (__l2cap_get_chan_by_dcid(conn, L2CAP_CID_ATT))
1473 return;
1474
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07001475 dst_type = bdaddr_type(hcon, hcon->dst_type);
1476
1477 /* If device is blocked, do not create a channel for it */
1478 if (hci_blacklist_lookup(hcon->hdev, &hcon->dst, dst_type))
1479 return;
1480
Gustavo Padovan8ffb9292013-10-21 14:21:41 -02001481 l2cap_chan_lock(pchan);
Gustavo F. Padovan62f3a2c2011-04-14 18:34:34 -03001482
Gustavo Padovan80b98022012-05-27 22:27:51 -03001483 chan = pchan->ops->new_connection(pchan);
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001484 if (!chan)
Ville Tervob62f3282011-02-10 22:38:50 -03001485 goto clean;
1486
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07001487 bacpy(&chan->src, &hcon->src);
1488 bacpy(&chan->dst, &hcon->dst);
1489 chan->src_type = bdaddr_type(hcon, hcon->src_type);
1490 chan->dst_type = dst_type;
Ville Tervob62f3282011-02-10 22:38:50 -03001491
Johan Hedberg44f3b0f2013-04-29 19:35:45 +03001492 __l2cap_chan_add(conn, chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001493
Ville Tervob62f3282011-02-10 22:38:50 -03001494clean:
Gustavo Padovan8ffb9292013-10-21 14:21:41 -02001495 l2cap_chan_unlock(pchan);
Ville Tervob62f3282011-02-10 22:38:50 -03001496}
1497
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001498static void l2cap_conn_ready(struct l2cap_conn *conn)
1499{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001500 struct l2cap_chan *chan;
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001501 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001502
1503 BT_DBG("conn %p", conn);
1504
Johan Hedbergd8729922013-04-29 19:35:39 +03001505 /* For outgoing pairing which doesn't necessarily have an
1506 * associated socket (e.g. mgmt_pair_device).
1507 */
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001508 if (hcon->out && hcon->type == LE_LINK)
1509 smp_conn_security(hcon, hcon->pending_sec_level);
Vinicius Costa Gomes160dc6a2011-08-19 21:06:55 -03001510
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001511 mutex_lock(&conn->chan_lock);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001512
Johan Hedberg44f3b0f2013-04-29 19:35:45 +03001513 if (hcon->type == LE_LINK)
1514 l2cap_le_conn_ready(conn);
1515
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001516 list_for_each_entry(chan, &conn->chan_l, list) {
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03001517
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001518 l2cap_chan_lock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001519
Johan Hedberg2338a7e2014-01-24 10:35:40 +02001520 if (chan->scid == L2CAP_CID_A2MP) {
Andrei Emeltchenko416fa752012-05-29 13:59:16 +03001521 l2cap_chan_unlock(chan);
1522 continue;
1523 }
1524
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001525 if (hcon->type == LE_LINK) {
Johan Hedbergf1496de2013-05-13 14:15:56 +03001526 l2cap_le_start(chan);
Vinicius Costa Gomes63128452011-06-17 22:46:26 -03001527 } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
Gustavo Padovan74e75742013-10-15 19:24:51 -03001528 l2cap_chan_ready(chan);
Anderson Brigliab501d6a2011-06-07 18:46:31 -03001529
Gustavo Padovan1c244f72012-12-07 03:29:10 -02001530 } else if (chan->state == BT_CONNECT) {
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001531 l2cap_do_start(chan);
Gustavo Padovan1c244f72012-12-07 03:29:10 -02001532 }
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001533
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001534 l2cap_chan_unlock(chan);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001535 }
1536
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001537 mutex_unlock(&conn->chan_lock);
Johan Hedberg61a939c2014-01-17 20:45:11 +02001538
1539 queue_work(hcon->hdev->workqueue, &conn->pending_rx_work);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001540}
1541
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001542/* Notify sockets that we cannot guaranty reliability anymore */
1543static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
1544{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001545 struct l2cap_chan *chan;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001546
1547 BT_DBG("conn %p", conn);
1548
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001549 mutex_lock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001550
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001551 list_for_each_entry(chan, &conn->chan_l, list) {
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +03001552 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
Gustavo Padovan1d8b1fd2012-10-06 11:34:52 +01001553 l2cap_chan_set_err(chan, err);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001554 }
1555
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001556 mutex_unlock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001557}
1558
Gustavo F. Padovanf878fca2011-12-15 01:16:14 -02001559static void l2cap_info_timeout(struct work_struct *work)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001560{
Gustavo F. Padovanf878fca2011-12-15 01:16:14 -02001561 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001562 info_timer.work);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001563
Marcel Holtmann984947d2009-02-06 23:35:19 +01001564 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01001565 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01001566
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001567 l2cap_conn_start(conn);
1568}
1569
David Herrmann2c8e1412013-04-06 20:28:45 +02001570/*
1571 * l2cap_user
1572 * External modules can register l2cap_user objects on l2cap_conn. The ->probe
1573 * callback is called during registration. The ->remove callback is called
1574 * during unregistration.
1575 * An l2cap_user object can either be explicitly unregistered or when the
1576 * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon,
1577 * l2cap->hchan, .. are valid as long as the remove callback hasn't been called.
1578 * External modules must own a reference to the l2cap_conn object if they intend
1579 * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at
1580 * any time if they don't.
1581 */
1582
1583int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user)
1584{
1585 struct hci_dev *hdev = conn->hcon->hdev;
1586 int ret;
1587
1588 /* We need to check whether l2cap_conn is registered. If it is not, we
1589 * must not register the l2cap_user. l2cap_conn_del() is unregisters
1590 * l2cap_conn objects, but doesn't provide its own locking. Instead, it
1591 * relies on the parent hci_conn object to be locked. This itself relies
1592 * on the hci_dev object to be locked. So we must lock the hci device
1593 * here, too. */
1594
1595 hci_dev_lock(hdev);
1596
1597 if (user->list.next || user->list.prev) {
1598 ret = -EINVAL;
1599 goto out_unlock;
1600 }
1601
1602 /* conn->hchan is NULL after l2cap_conn_del() was called */
1603 if (!conn->hchan) {
1604 ret = -ENODEV;
1605 goto out_unlock;
1606 }
1607
1608 ret = user->probe(conn, user);
1609 if (ret)
1610 goto out_unlock;
1611
1612 list_add(&user->list, &conn->users);
1613 ret = 0;
1614
1615out_unlock:
1616 hci_dev_unlock(hdev);
1617 return ret;
1618}
1619EXPORT_SYMBOL(l2cap_register_user);
1620
1621void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user)
1622{
1623 struct hci_dev *hdev = conn->hcon->hdev;
1624
1625 hci_dev_lock(hdev);
1626
1627 if (!user->list.next || !user->list.prev)
1628 goto out_unlock;
1629
1630 list_del(&user->list);
1631 user->list.next = NULL;
1632 user->list.prev = NULL;
1633 user->remove(conn, user);
1634
1635out_unlock:
1636 hci_dev_unlock(hdev);
1637}
1638EXPORT_SYMBOL(l2cap_unregister_user);
1639
1640static void l2cap_unregister_all_users(struct l2cap_conn *conn)
1641{
1642 struct l2cap_user *user;
1643
1644 while (!list_empty(&conn->users)) {
1645 user = list_first_entry(&conn->users, struct l2cap_user, list);
1646 list_del(&user->list);
1647 user->list.next = NULL;
1648 user->list.prev = NULL;
1649 user->remove(conn, user);
1650 }
1651}
1652
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001653static void l2cap_conn_del(struct hci_conn *hcon, int err)
1654{
1655 struct l2cap_conn *conn = hcon->l2cap_data;
1656 struct l2cap_chan *chan, *l;
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001657
1658 if (!conn)
1659 return;
1660
1661 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
1662
1663 kfree_skb(conn->rx_skb);
1664
Johan Hedberg61a939c2014-01-17 20:45:11 +02001665 skb_queue_purge(&conn->pending_rx);
Jukka Taimisto7ab56c32014-06-12 10:15:13 +00001666
1667 /* We can not call flush_work(&conn->pending_rx_work) here since we
1668 * might block if we are running on a worker from the same workqueue
1669 * pending_rx_work is waiting on.
1670 */
1671 if (work_pending(&conn->pending_rx_work))
1672 cancel_work_sync(&conn->pending_rx_work);
Johan Hedberg61a939c2014-01-17 20:45:11 +02001673
David Herrmann2c8e1412013-04-06 20:28:45 +02001674 l2cap_unregister_all_users(conn);
1675
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001676 mutex_lock(&conn->chan_lock);
1677
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001678 /* Kill channels */
1679 list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
Mat Martineau61d6ef32012-04-27 16:50:50 -07001680 l2cap_chan_hold(chan);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001681 l2cap_chan_lock(chan);
1682
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001683 l2cap_chan_del(chan, err);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001684
1685 l2cap_chan_unlock(chan);
1686
Gustavo Padovan80b98022012-05-27 22:27:51 -03001687 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07001688 l2cap_chan_put(chan);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001689 }
1690
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001691 mutex_unlock(&conn->chan_lock);
1692
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001693 hci_chan_del(conn->hchan);
1694
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001695 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
Ulisses Furquim127074b2012-01-30 18:26:29 -02001696 cancel_delayed_work_sync(&conn->info_timer);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001697
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001698 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) {
Ulisses Furquim127074b2012-01-30 18:26:29 -02001699 cancel_delayed_work_sync(&conn->security_timer);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001700 smp_chan_destroy(conn);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001701 }
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001702
1703 hcon->l2cap_data = NULL;
David Herrmann9c903e32013-04-06 20:28:44 +02001704 conn->hchan = NULL;
1705 l2cap_conn_put(conn);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001706}
1707
Gustavo F. Padovan6c9d42a2011-12-20 10:57:27 -02001708static void security_timeout(struct work_struct *work)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001709{
Gustavo F. Padovan6c9d42a2011-12-20 10:57:27 -02001710 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001711 security_timer.work);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001712
Johan Hedbergd06cc412012-06-06 18:44:11 +08001713 BT_DBG("conn %p", conn);
1714
1715 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) {
1716 smp_chan_destroy(conn);
1717 l2cap_conn_del(conn->hcon, ETIMEDOUT);
1718 }
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001719}
1720
David Herrmann9c903e32013-04-06 20:28:44 +02001721static void l2cap_conn_free(struct kref *ref)
1722{
1723 struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref);
1724
1725 hci_conn_put(conn->hcon);
1726 kfree(conn);
1727}
1728
1729void l2cap_conn_get(struct l2cap_conn *conn)
1730{
1731 kref_get(&conn->ref);
1732}
1733EXPORT_SYMBOL(l2cap_conn_get);
1734
1735void l2cap_conn_put(struct l2cap_conn *conn)
1736{
1737 kref_put(&conn->ref, l2cap_conn_free);
1738}
1739EXPORT_SYMBOL(l2cap_conn_put);
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741/* ---- Socket interface ---- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Ido Yarivc2287682012-04-20 15:46:07 -03001743/* Find socket with psm and source / destination bdaddr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 * Returns closest match.
1745 */
Ido Yarivc2287682012-04-20 15:46:07 -03001746static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm,
1747 bdaddr_t *src,
Johan Hedbergbf20fd42013-05-14 13:23:13 +03001748 bdaddr_t *dst,
1749 u8 link_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001751 struct l2cap_chan *c, *c1 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001753 read_lock(&chan_list_lock);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00001754
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001755 list_for_each_entry(c, &chan_list, global_l) {
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001756 if (state && c->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 continue;
1758
Johan Hedbergbf20fd42013-05-14 13:23:13 +03001759 if (link_type == ACL_LINK && c->src_type != BDADDR_BREDR)
1760 continue;
1761
1762 if (link_type == LE_LINK && c->src_type == BDADDR_BREDR)
1763 continue;
1764
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001765 if (c->psm == psm) {
Ido Yarivc2287682012-04-20 15:46:07 -03001766 int src_match, dst_match;
1767 int src_any, dst_any;
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /* Exact match. */
Marcel Holtmann7eafc592013-10-13 08:12:47 -07001770 src_match = !bacmp(&c->src, src);
1771 dst_match = !bacmp(&c->dst, dst);
Ido Yarivc2287682012-04-20 15:46:07 -03001772 if (src_match && dst_match) {
Johannes Berga7567b22011-06-01 08:29:54 +02001773 read_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001774 return c;
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 /* Closest match */
Marcel Holtmann7eafc592013-10-13 08:12:47 -07001778 src_any = !bacmp(&c->src, BDADDR_ANY);
1779 dst_any = !bacmp(&c->dst, BDADDR_ANY);
Ido Yarivc2287682012-04-20 15:46:07 -03001780 if ((src_match && dst_any) || (src_any && dst_match) ||
1781 (src_any && dst_any))
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001782 c1 = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001786 read_unlock(&chan_list_lock);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00001787
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001788 return c1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001791static void l2cap_monitor_timeout(struct work_struct *work)
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001792{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001793 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau4239d162012-05-17 20:53:49 -07001794 monitor_timer.work);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001795
Gustavo F. Padovan525cd182011-03-25 19:43:39 -03001796 BT_DBG("chan %p", chan);
Gustavo F. Padovan0e989582010-04-19 14:45:38 -03001797
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001798 l2cap_chan_lock(chan);
1799
Mat Martineau80909e02012-05-17 20:53:50 -07001800 if (!chan->conn) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001801 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001802 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001803 return;
1804 }
1805
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03001806 l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001807
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001808 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001809 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001810}
1811
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001812static void l2cap_retrans_timeout(struct work_struct *work)
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001813{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001814 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau4239d162012-05-17 20:53:49 -07001815 retrans_timer.work);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001816
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03001817 BT_DBG("chan %p", chan);
Gustavo F. Padovan0e989582010-04-19 14:45:38 -03001818
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001819 l2cap_chan_lock(chan);
1820
Mat Martineau80909e02012-05-17 20:53:50 -07001821 if (!chan->conn) {
1822 l2cap_chan_unlock(chan);
1823 l2cap_chan_put(chan);
1824 return;
1825 }
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001826
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03001827 l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001828 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001829 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001830}
1831
Gustavo Padovand6603662012-05-21 13:58:22 -03001832static void l2cap_streaming_send(struct l2cap_chan *chan,
1833 struct sk_buff_head *skbs)
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001834{
Gustavo F. Padovanccbb84a2010-08-30 18:44:44 -03001835 struct sk_buff *skb;
Mat Martineau37339372012-05-17 20:53:33 -07001836 struct l2cap_ctrl *control;
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001837
Mat Martineau37339372012-05-17 20:53:33 -07001838 BT_DBG("chan %p, skbs %p", chan, skbs);
1839
Mat Martineaub99e13a2012-10-23 15:24:19 -07001840 if (__chan_is_moving(chan))
1841 return;
1842
Mat Martineau37339372012-05-17 20:53:33 -07001843 skb_queue_splice_tail_init(skbs, &chan->tx_q);
1844
1845 while (!skb_queue_empty(&chan->tx_q)) {
1846
1847 skb = skb_dequeue(&chan->tx_q);
1848
1849 bt_cb(skb)->control.retries = 1;
1850 control = &bt_cb(skb)->control;
1851
1852 control->reqseq = 0;
1853 control->txseq = chan->next_tx_seq;
1854
1855 __pack_control(chan, control, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001856
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001857 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineau37339372012-05-17 20:53:33 -07001858 u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
1859 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001860 }
1861
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001862 l2cap_do_send(chan, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001863
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001864 BT_DBG("Sent txseq %u", control->txseq);
Mat Martineau37339372012-05-17 20:53:33 -07001865
Andrei Emeltchenko836be932011-10-17 12:19:57 +03001866 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
Mat Martineau37339372012-05-17 20:53:33 -07001867 chan->frames_sent++;
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001868 }
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001869}
1870
Szymon Janc67c9e842011-07-28 16:24:33 +02001871static int l2cap_ertm_send(struct l2cap_chan *chan)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001872{
1873 struct sk_buff *skb, *tx_skb;
Mat Martineau18a48e72012-05-17 20:53:34 -07001874 struct l2cap_ctrl *control;
1875 int sent = 0;
1876
1877 BT_DBG("chan %p", chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001878
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001879 if (chan->state != BT_CONNECTED)
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001880 return -ENOTCONN;
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001881
Mat Martineau94122bb2012-05-02 09:42:02 -07001882 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
1883 return 0;
1884
Mat Martineaub99e13a2012-10-23 15:24:19 -07001885 if (__chan_is_moving(chan))
1886 return 0;
1887
Mat Martineau18a48e72012-05-17 20:53:34 -07001888 while (chan->tx_send_head &&
1889 chan->unacked_frames < chan->remote_tx_win &&
1890 chan->tx_state == L2CAP_TX_STATE_XMIT) {
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001891
Mat Martineau18a48e72012-05-17 20:53:34 -07001892 skb = chan->tx_send_head;
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001893
Mat Martineau18a48e72012-05-17 20:53:34 -07001894 bt_cb(skb)->control.retries = 1;
1895 control = &bt_cb(skb)->control;
Gustavo F. Padovan95ffa972010-06-18 20:37:33 -03001896
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03001897 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
Mat Martineau18a48e72012-05-17 20:53:34 -07001898 control->final = 1;
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03001899
Mat Martineau18a48e72012-05-17 20:53:34 -07001900 control->reqseq = chan->buffer_seq;
1901 chan->last_acked_seq = chan->buffer_seq;
1902 control->txseq = chan->next_tx_seq;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001903
Mat Martineau18a48e72012-05-17 20:53:34 -07001904 __pack_control(chan, control, skb);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001905
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001906 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineau18a48e72012-05-17 20:53:34 -07001907 u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
1908 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001909 }
1910
Mat Martineau18a48e72012-05-17 20:53:34 -07001911 /* Clone after data has been modified. Data is assumed to be
1912 read-only (for locking purposes) on cloned sk_buffs.
1913 */
1914 tx_skb = skb_clone(skb, GFP_KERNEL);
1915
1916 if (!tx_skb)
1917 break;
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -03001918
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -03001919 __set_retrans_timer(chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001920
Andrei Emeltchenko836be932011-10-17 12:19:57 +03001921 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
Mat Martineau18a48e72012-05-17 20:53:34 -07001922 chan->unacked_frames++;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03001923 chan->frames_sent++;
Mat Martineau18a48e72012-05-17 20:53:34 -07001924 sent++;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001925
Gustavo F. Padovan58d35f82011-04-04 16:16:44 -03001926 if (skb_queue_is_last(&chan->tx_q, skb))
1927 chan->tx_send_head = NULL;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001928 else
Gustavo F. Padovan58d35f82011-04-04 16:16:44 -03001929 chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
Mat Martineau18a48e72012-05-17 20:53:34 -07001930
1931 l2cap_do_send(chan, tx_skb);
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001932 BT_DBG("Sent txseq %u", control->txseq);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001933 }
1934
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001935 BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent,
1936 chan->unacked_frames, skb_queue_len(&chan->tx_q));
Mat Martineau18a48e72012-05-17 20:53:34 -07001937
1938 return sent;
Gustavo F. Padovan9e917af2010-05-01 16:15:37 -03001939}
1940
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001941static void l2cap_ertm_resend(struct l2cap_chan *chan)
1942{
1943 struct l2cap_ctrl control;
1944 struct sk_buff *skb;
1945 struct sk_buff *tx_skb;
1946 u16 seq;
1947
1948 BT_DBG("chan %p", chan);
1949
1950 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
1951 return;
1952
Mat Martineaub99e13a2012-10-23 15:24:19 -07001953 if (__chan_is_moving(chan))
1954 return;
1955
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001956 while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) {
1957 seq = l2cap_seq_list_pop(&chan->retrans_list);
1958
1959 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq);
1960 if (!skb) {
1961 BT_DBG("Error: Can't retransmit seq %d, frame missing",
Gustavo Padovan2d792812012-10-06 10:07:01 +01001962 seq);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001963 continue;
1964 }
1965
1966 bt_cb(skb)->control.retries++;
1967 control = bt_cb(skb)->control;
1968
1969 if (chan->max_tx != 0 &&
1970 bt_cb(skb)->control.retries > chan->max_tx) {
1971 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001972 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001973 l2cap_seq_list_clear(&chan->retrans_list);
1974 break;
1975 }
1976
1977 control.reqseq = chan->buffer_seq;
1978 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
1979 control.final = 1;
1980 else
1981 control.final = 0;
1982
1983 if (skb_cloned(skb)) {
1984 /* Cloned sk_buffs are read-only, so we need a
1985 * writeable copy
1986 */
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03001987 tx_skb = skb_copy(skb, GFP_KERNEL);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001988 } else {
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03001989 tx_skb = skb_clone(skb, GFP_KERNEL);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001990 }
1991
1992 if (!tx_skb) {
1993 l2cap_seq_list_clear(&chan->retrans_list);
1994 break;
1995 }
1996
1997 /* Update skb contents */
1998 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
1999 put_unaligned_le32(__pack_extended_control(&control),
2000 tx_skb->data + L2CAP_HDR_SIZE);
2001 } else {
2002 put_unaligned_le16(__pack_enhanced_control(&control),
2003 tx_skb->data + L2CAP_HDR_SIZE);
2004 }
2005
2006 if (chan->fcs == L2CAP_FCS_CRC16) {
2007 u16 fcs = crc16(0, (u8 *) tx_skb->data, tx_skb->len);
2008 put_unaligned_le16(fcs, skb_put(tx_skb,
2009 L2CAP_FCS_SIZE));
2010 }
2011
2012 l2cap_do_send(chan, tx_skb);
2013
2014 BT_DBG("Resent txseq %d", control.txseq);
2015
2016 chan->last_acked_seq = chan->buffer_seq;
2017 }
2018}
2019
Mat Martineauf80842a2012-05-17 20:53:46 -07002020static void l2cap_retransmit(struct l2cap_chan *chan,
2021 struct l2cap_ctrl *control)
2022{
2023 BT_DBG("chan %p, control %p", chan, control);
2024
2025 l2cap_seq_list_append(&chan->retrans_list, control->reqseq);
2026 l2cap_ertm_resend(chan);
2027}
2028
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002029static void l2cap_retransmit_all(struct l2cap_chan *chan,
2030 struct l2cap_ctrl *control)
2031{
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002032 struct sk_buff *skb;
2033
2034 BT_DBG("chan %p, control %p", chan, control);
2035
2036 if (control->poll)
2037 set_bit(CONN_SEND_FBIT, &chan->conn_state);
2038
2039 l2cap_seq_list_clear(&chan->retrans_list);
2040
2041 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
2042 return;
2043
2044 if (chan->unacked_frames) {
2045 skb_queue_walk(&chan->tx_q, skb) {
2046 if (bt_cb(skb)->control.txseq == control->reqseq ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01002047 skb == chan->tx_send_head)
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002048 break;
2049 }
2050
2051 skb_queue_walk_from(&chan->tx_q, skb) {
2052 if (skb == chan->tx_send_head)
2053 break;
2054
2055 l2cap_seq_list_append(&chan->retrans_list,
2056 bt_cb(skb)->control.txseq);
2057 }
2058
2059 l2cap_ertm_resend(chan);
2060 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002061}
2062
Szymon Jancb17e73b2012-01-11 10:59:47 +01002063static void l2cap_send_ack(struct l2cap_chan *chan)
2064{
Mat Martineau0a0aba42012-05-17 20:53:39 -07002065 struct l2cap_ctrl control;
2066 u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
2067 chan->last_acked_seq);
2068 int threshold;
2069
2070 BT_DBG("chan %p last_acked_seq %d buffer_seq %d",
2071 chan, chan->last_acked_seq, chan->buffer_seq);
2072
2073 memset(&control, 0, sizeof(control));
2074 control.sframe = 1;
2075
2076 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
2077 chan->rx_state == L2CAP_RX_STATE_RECV) {
2078 __clear_ack_timer(chan);
2079 control.super = L2CAP_SUPER_RNR;
2080 control.reqseq = chan->buffer_seq;
2081 l2cap_send_sframe(chan, &control);
2082 } else {
2083 if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) {
2084 l2cap_ertm_send(chan);
2085 /* If any i-frames were sent, they included an ack */
2086 if (chan->buffer_seq == chan->last_acked_seq)
2087 frames_to_ack = 0;
2088 }
2089
Mat Martineauc20f8e32012-07-10 05:47:07 -07002090 /* Ack now if the window is 3/4ths full.
Mat Martineau0a0aba42012-05-17 20:53:39 -07002091 * Calculate without mul or div
2092 */
Mat Martineauc20f8e32012-07-10 05:47:07 -07002093 threshold = chan->ack_win;
Mat Martineau0a0aba42012-05-17 20:53:39 -07002094 threshold += threshold << 1;
2095 threshold >>= 2;
2096
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002097 BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack,
Mat Martineau0a0aba42012-05-17 20:53:39 -07002098 threshold);
2099
2100 if (frames_to_ack >= threshold) {
2101 __clear_ack_timer(chan);
2102 control.super = L2CAP_SUPER_RR;
2103 control.reqseq = chan->buffer_seq;
2104 l2cap_send_sframe(chan, &control);
2105 frames_to_ack = 0;
2106 }
2107
2108 if (frames_to_ack)
2109 __set_ack_timer(chan);
2110 }
Szymon Jancb17e73b2012-01-11 10:59:47 +01002111}
2112
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002113static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan,
2114 struct msghdr *msg, int len,
2115 int count, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002117 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002118 struct sk_buff **frag;
Gustavo Padovan90338942012-04-06 20:15:47 -03002119 int sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Jukka Rissanen04988782014-06-18 16:37:07 +03002121 if (chan->ops->memcpy_fromiovec(chan, skb_put(skb, count),
2122 msg->msg_iov, count))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002123 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 sent += count;
2126 len -= count;
2127
2128 /* Continuation fragments (no L2CAP header) */
2129 frag = &skb_shinfo(skb)->frag_list;
2130 while (len) {
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002131 struct sk_buff *tmp;
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 count = min_t(unsigned int, conn->mtu, len);
2134
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002135 tmp = chan->ops->alloc_skb(chan, 0, count,
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002136 msg->msg_flags & MSG_DONTWAIT);
2137 if (IS_ERR(tmp))
2138 return PTR_ERR(tmp);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002139
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002140 *frag = tmp;
2141
Jukka Rissanen04988782014-06-18 16:37:07 +03002142 if (chan->ops->memcpy_fromiovec(chan, skb_put(*frag, count),
2143 msg->msg_iov, count))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002144 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 sent += count;
2147 len -= count;
2148
Gustavo Padovan2d0ed3d2012-05-11 13:16:12 -03002149 skb->len += (*frag)->len;
2150 skb->data_len += (*frag)->len;
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 frag = &(*frag)->next;
2153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 return sent;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002156}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002158static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan,
Marcel Holtmann8d463212014-06-05 15:22:51 +02002159 struct msghdr *msg, size_t len)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002160{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002161 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002162 struct sk_buff *skb;
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002163 int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002164 struct l2cap_hdr *lh;
2165
Marcel Holtmann8d463212014-06-05 15:22:51 +02002166 BT_DBG("chan %p psm 0x%2.2x len %zu", chan,
2167 __le16_to_cpu(chan->psm), len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002168
2169 count = min_t(unsigned int, (conn->mtu - hlen), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002170
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002171 skb = chan->ops->alloc_skb(chan, hlen, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002172 msg->msg_flags & MSG_DONTWAIT);
2173 if (IS_ERR(skb))
2174 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002175
2176 /* Create L2CAP header */
2177 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002178 lh->cid = cpu_to_le16(chan->dcid);
Andrei Emeltchenkodaf6a78c2012-05-03 10:55:52 +03002179 lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE);
Marcel Holtmann43b1b8d2013-10-12 06:01:26 -07002180 put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002181
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002182 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002183 if (unlikely(err < 0)) {
2184 kfree_skb(skb);
2185 return ERR_PTR(err);
2186 }
2187 return skb;
2188}
2189
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002190static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan,
Marcel Holtmann8d463212014-06-05 15:22:51 +02002191 struct msghdr *msg, size_t len)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002192{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002193 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002194 struct sk_buff *skb;
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002195 int err, count;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002196 struct l2cap_hdr *lh;
2197
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002198 BT_DBG("chan %p len %zu", chan, len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002199
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002200 count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002201
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002202 skb = chan->ops->alloc_skb(chan, L2CAP_HDR_SIZE, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002203 msg->msg_flags & MSG_DONTWAIT);
2204 if (IS_ERR(skb))
2205 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002206
2207 /* Create L2CAP header */
2208 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002209 lh->cid = cpu_to_le16(chan->dcid);
Gustavo Padovan6ff9b5e2012-05-02 11:56:17 -03002210 lh->len = cpu_to_le16(len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002211
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002212 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002213 if (unlikely(err < 0)) {
2214 kfree_skb(skb);
2215 return ERR_PTR(err);
2216 }
2217 return skb;
2218}
2219
Luiz Augusto von Dentzab0ff762011-09-12 20:00:50 +03002220static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan,
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002221 struct msghdr *msg, size_t len,
2222 u16 sdulen)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002223{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002224 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002225 struct sk_buff *skb;
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03002226 int err, count, hlen;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002227 struct l2cap_hdr *lh;
2228
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002229 BT_DBG("chan %p len %zu", chan, len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002230
Gustavo F. Padovan0ee0d202010-05-01 16:15:41 -03002231 if (!conn)
2232 return ERR_PTR(-ENOTCONN);
2233
Gustavo Padovanba7aa642012-05-29 13:29:16 -03002234 hlen = __ertm_hdr_size(chan);
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03002235
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002236 if (sdulen)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002237 hlen += L2CAP_SDULEN_SIZE;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002238
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03002239 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002240 hlen += L2CAP_FCS_SIZE;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03002241
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002242 count = min_t(unsigned int, (conn->mtu - hlen), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002243
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002244 skb = chan->ops->alloc_skb(chan, hlen, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002245 msg->msg_flags & MSG_DONTWAIT);
2246 if (IS_ERR(skb))
2247 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002248
2249 /* Create L2CAP header */
2250 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002251 lh->cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002252 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03002253
Mat Martineau18a48e72012-05-17 20:53:34 -07002254 /* Control header is populated later */
2255 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
2256 put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
2257 else
2258 put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03002259
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002260 if (sdulen)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002261 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002262
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002263 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002264 if (unlikely(err < 0)) {
2265 kfree_skb(skb);
2266 return ERR_PTR(err);
2267 }
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03002268
Mat Martineau18a48e72012-05-17 20:53:34 -07002269 bt_cb(skb)->control.fcs = chan->fcs;
Mat Martineau3ce35142012-04-25 16:36:14 -07002270 bt_cb(skb)->control.retries = 0;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002271 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
2273
Mat Martineau94122bb2012-05-02 09:42:02 -07002274static int l2cap_segment_sdu(struct l2cap_chan *chan,
2275 struct sk_buff_head *seg_queue,
2276 struct msghdr *msg, size_t len)
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002277{
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002278 struct sk_buff *skb;
Mat Martineau94122bb2012-05-02 09:42:02 -07002279 u16 sdu_len;
2280 size_t pdu_len;
Mat Martineau94122bb2012-05-02 09:42:02 -07002281 u8 sar;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002282
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002283 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002284
Mat Martineau94122bb2012-05-02 09:42:02 -07002285 /* It is critical that ERTM PDUs fit in a single HCI fragment,
2286 * so fragmented skbs are not used. The HCI layer's handling
2287 * of fragmented skbs is not compatible with ERTM's queueing.
2288 */
2289
2290 /* PDU size is derived from the HCI MTU */
2291 pdu_len = chan->conn->mtu;
2292
Mat Martineaua5495742012-10-23 15:24:21 -07002293 /* Constrain PDU size for BR/EDR connections */
2294 if (!chan->hs_hcon)
2295 pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD);
Mat Martineau94122bb2012-05-02 09:42:02 -07002296
2297 /* Adjust for largest possible L2CAP overhead. */
Gustavo Padovan35d401d2012-05-25 18:57:05 -03002298 if (chan->fcs)
2299 pdu_len -= L2CAP_FCS_SIZE;
2300
Gustavo Padovanba7aa642012-05-29 13:29:16 -03002301 pdu_len -= __ertm_hdr_size(chan);
Mat Martineau94122bb2012-05-02 09:42:02 -07002302
2303 /* Remote device may have requested smaller PDUs */
2304 pdu_len = min_t(size_t, pdu_len, chan->remote_mps);
2305
2306 if (len <= pdu_len) {
2307 sar = L2CAP_SAR_UNSEGMENTED;
2308 sdu_len = 0;
2309 pdu_len = len;
2310 } else {
2311 sar = L2CAP_SAR_START;
2312 sdu_len = len;
2313 pdu_len -= L2CAP_SDULEN_SIZE;
2314 }
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002315
2316 while (len > 0) {
Mat Martineau94122bb2012-05-02 09:42:02 -07002317 skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002318
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002319 if (IS_ERR(skb)) {
Mat Martineau94122bb2012-05-02 09:42:02 -07002320 __skb_queue_purge(seg_queue);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002321 return PTR_ERR(skb);
2322 }
2323
Mat Martineau94122bb2012-05-02 09:42:02 -07002324 bt_cb(skb)->control.sar = sar;
2325 __skb_queue_tail(seg_queue, skb);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002326
Mat Martineau94122bb2012-05-02 09:42:02 -07002327 len -= pdu_len;
2328 if (sdu_len) {
2329 sdu_len = 0;
2330 pdu_len += L2CAP_SDULEN_SIZE;
2331 }
2332
2333 if (len <= pdu_len) {
2334 sar = L2CAP_SAR_END;
2335 pdu_len = len;
2336 } else {
2337 sar = L2CAP_SAR_CONTINUE;
2338 }
2339 }
2340
Gustavo Padovanf0f62792012-05-29 13:29:17 -03002341 return 0;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002342}
2343
Johan Hedberg177f8f22013-05-31 17:54:51 +03002344static struct sk_buff *l2cap_create_le_flowctl_pdu(struct l2cap_chan *chan,
2345 struct msghdr *msg,
2346 size_t len, u16 sdulen)
2347{
2348 struct l2cap_conn *conn = chan->conn;
2349 struct sk_buff *skb;
2350 int err, count, hlen;
2351 struct l2cap_hdr *lh;
2352
2353 BT_DBG("chan %p len %zu", chan, len);
2354
2355 if (!conn)
2356 return ERR_PTR(-ENOTCONN);
2357
2358 hlen = L2CAP_HDR_SIZE;
2359
2360 if (sdulen)
2361 hlen += L2CAP_SDULEN_SIZE;
2362
2363 count = min_t(unsigned int, (conn->mtu - hlen), len);
2364
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002365 skb = chan->ops->alloc_skb(chan, hlen, count,
Johan Hedberg177f8f22013-05-31 17:54:51 +03002366 msg->msg_flags & MSG_DONTWAIT);
2367 if (IS_ERR(skb))
2368 return skb;
2369
2370 /* Create L2CAP header */
2371 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
2372 lh->cid = cpu_to_le16(chan->dcid);
2373 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
2374
2375 if (sdulen)
2376 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
2377
2378 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
2379 if (unlikely(err < 0)) {
2380 kfree_skb(skb);
2381 return ERR_PTR(err);
2382 }
2383
2384 return skb;
2385}
2386
2387static int l2cap_segment_le_sdu(struct l2cap_chan *chan,
2388 struct sk_buff_head *seg_queue,
2389 struct msghdr *msg, size_t len)
2390{
2391 struct sk_buff *skb;
2392 size_t pdu_len;
2393 u16 sdu_len;
2394
2395 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
2396
2397 pdu_len = chan->conn->mtu - L2CAP_HDR_SIZE;
2398
2399 pdu_len = min_t(size_t, pdu_len, chan->remote_mps);
2400
2401 sdu_len = len;
2402 pdu_len -= L2CAP_SDULEN_SIZE;
2403
2404 while (len > 0) {
2405 if (len <= pdu_len)
2406 pdu_len = len;
2407
2408 skb = l2cap_create_le_flowctl_pdu(chan, msg, pdu_len, sdu_len);
2409 if (IS_ERR(skb)) {
2410 __skb_queue_purge(seg_queue);
2411 return PTR_ERR(skb);
2412 }
2413
2414 __skb_queue_tail(seg_queue, skb);
2415
2416 len -= pdu_len;
2417
2418 if (sdu_len) {
2419 sdu_len = 0;
2420 pdu_len += L2CAP_SDULEN_SIZE;
2421 }
2422 }
2423
2424 return 0;
2425}
2426
Marcel Holtmann8d463212014-06-05 15:22:51 +02002427int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002428{
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002429 struct sk_buff *skb;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002430 int err;
Mat Martineau94122bb2012-05-02 09:42:02 -07002431 struct sk_buff_head seg_queue;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002432
Seung-Woo Kim31e8ce82013-11-05 18:46:33 +09002433 if (!chan->conn)
2434 return -ENOTCONN;
2435
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002436 /* Connectionless channel */
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03002437 if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
Marcel Holtmann8d463212014-06-05 15:22:51 +02002438 skb = l2cap_create_connless_pdu(chan, msg, len);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002439 if (IS_ERR(skb))
2440 return PTR_ERR(skb);
2441
Andrzej Kaczmarekede81a22014-02-25 17:16:22 +01002442 /* Channel lock is released before requesting new skb and then
2443 * reacquired thus we need to recheck channel state.
2444 */
2445 if (chan->state != BT_CONNECTED) {
2446 kfree_skb(skb);
2447 return -ENOTCONN;
2448 }
2449
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002450 l2cap_do_send(chan, skb);
2451 return len;
2452 }
2453
2454 switch (chan->mode) {
Johan Hedberg38319712013-05-17 12:49:23 +03002455 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedberg177f8f22013-05-31 17:54:51 +03002456 /* Check outgoing MTU */
2457 if (len > chan->omtu)
2458 return -EMSGSIZE;
2459
Johan Hedbergfad5fc82013-12-05 09:45:01 +02002460 if (!chan->tx_credits)
2461 return -EAGAIN;
2462
Johan Hedberg177f8f22013-05-31 17:54:51 +03002463 __skb_queue_head_init(&seg_queue);
2464
2465 err = l2cap_segment_le_sdu(chan, &seg_queue, msg, len);
2466
2467 if (chan->state != BT_CONNECTED) {
2468 __skb_queue_purge(&seg_queue);
2469 err = -ENOTCONN;
2470 }
2471
2472 if (err)
2473 return err;
2474
2475 skb_queue_splice_tail_init(&seg_queue, &chan->tx_q);
2476
2477 while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) {
2478 l2cap_do_send(chan, skb_dequeue(&chan->tx_q));
2479 chan->tx_credits--;
2480 }
2481
2482 if (!chan->tx_credits)
2483 chan->ops->suspend(chan);
2484
2485 err = len;
2486
2487 break;
2488
Johan Hedbergfad5fc82013-12-05 09:45:01 +02002489 case L2CAP_MODE_BASIC:
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002490 /* Check outgoing MTU */
2491 if (len > chan->omtu)
2492 return -EMSGSIZE;
2493
2494 /* Create a basic PDU */
Marcel Holtmann8d463212014-06-05 15:22:51 +02002495 skb = l2cap_create_basic_pdu(chan, msg, len);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002496 if (IS_ERR(skb))
2497 return PTR_ERR(skb);
2498
Andrzej Kaczmarekede81a22014-02-25 17:16:22 +01002499 /* Channel lock is released before requesting new skb and then
2500 * reacquired thus we need to recheck channel state.
2501 */
2502 if (chan->state != BT_CONNECTED) {
2503 kfree_skb(skb);
2504 return -ENOTCONN;
2505 }
2506
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002507 l2cap_do_send(chan, skb);
2508 err = len;
2509 break;
2510
2511 case L2CAP_MODE_ERTM:
2512 case L2CAP_MODE_STREAMING:
Mat Martineau94122bb2012-05-02 09:42:02 -07002513 /* Check outgoing MTU */
2514 if (len > chan->omtu) {
2515 err = -EMSGSIZE;
2516 break;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002517 }
2518
Mat Martineau94122bb2012-05-02 09:42:02 -07002519 __skb_queue_head_init(&seg_queue);
2520
2521 /* Do segmentation before calling in to the state machine,
2522 * since it's possible to block while waiting for memory
2523 * allocation.
2524 */
2525 err = l2cap_segment_sdu(chan, &seg_queue, msg, len);
2526
2527 /* The channel could have been closed while segmenting,
2528 * check that it is still connected.
2529 */
2530 if (chan->state != BT_CONNECTED) {
2531 __skb_queue_purge(&seg_queue);
2532 err = -ENOTCONN;
2533 }
2534
2535 if (err)
2536 break;
2537
Mat Martineau37339372012-05-17 20:53:33 -07002538 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo Padovand6603662012-05-21 13:58:22 -03002539 l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST);
Mat Martineau37339372012-05-17 20:53:33 -07002540 else
Gustavo Padovand6603662012-05-21 13:58:22 -03002541 l2cap_streaming_send(chan, &seg_queue);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002542
Gustavo Padovand6603662012-05-21 13:58:22 -03002543 err = len;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002544
Mat Martineau94122bb2012-05-02 09:42:02 -07002545 /* If the skbs were not queued for sending, they'll still be in
2546 * seg_queue and need to be purged.
2547 */
2548 __skb_queue_purge(&seg_queue);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002549 break;
2550
2551 default:
2552 BT_DBG("bad state %1.1x", chan->mode);
2553 err = -EBADFD;
2554 }
2555
2556 return err;
2557}
2558
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002559static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq)
2560{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002561 struct l2cap_ctrl control;
2562 u16 seq;
2563
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002564 BT_DBG("chan %p, txseq %u", chan, txseq);
Mat Martineaubed68bd2012-05-17 20:53:44 -07002565
2566 memset(&control, 0, sizeof(control));
2567 control.sframe = 1;
2568 control.super = L2CAP_SUPER_SREJ;
2569
2570 for (seq = chan->expected_tx_seq; seq != txseq;
2571 seq = __next_seq(chan, seq)) {
2572 if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) {
2573 control.reqseq = seq;
2574 l2cap_send_sframe(chan, &control);
2575 l2cap_seq_list_append(&chan->srej_list, seq);
2576 }
2577 }
2578
2579 chan->expected_tx_seq = __next_seq(chan, txseq);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002580}
2581
2582static void l2cap_send_srej_tail(struct l2cap_chan *chan)
2583{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002584 struct l2cap_ctrl control;
2585
2586 BT_DBG("chan %p", chan);
2587
2588 if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR)
2589 return;
2590
2591 memset(&control, 0, sizeof(control));
2592 control.sframe = 1;
2593 control.super = L2CAP_SUPER_SREJ;
2594 control.reqseq = chan->srej_list.tail;
2595 l2cap_send_sframe(chan, &control);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002596}
2597
2598static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq)
2599{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002600 struct l2cap_ctrl control;
2601 u16 initial_head;
2602 u16 seq;
2603
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002604 BT_DBG("chan %p, txseq %u", chan, txseq);
Mat Martineaubed68bd2012-05-17 20:53:44 -07002605
2606 memset(&control, 0, sizeof(control));
2607 control.sframe = 1;
2608 control.super = L2CAP_SUPER_SREJ;
2609
2610 /* Capture initial list head to allow only one pass through the list. */
2611 initial_head = chan->srej_list.head;
2612
2613 do {
2614 seq = l2cap_seq_list_pop(&chan->srej_list);
2615 if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR)
2616 break;
2617
2618 control.reqseq = seq;
2619 l2cap_send_sframe(chan, &control);
2620 l2cap_seq_list_append(&chan->srej_list, seq);
2621 } while (chan->srej_list.head != initial_head);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002622}
2623
Mat Martineau608bcc62012-05-17 20:53:32 -07002624static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq)
2625{
2626 struct sk_buff *acked_skb;
2627 u16 ackseq;
2628
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002629 BT_DBG("chan %p, reqseq %u", chan, reqseq);
Mat Martineau608bcc62012-05-17 20:53:32 -07002630
2631 if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq)
2632 return;
2633
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002634 BT_DBG("expected_ack_seq %u, unacked_frames %u",
Mat Martineau608bcc62012-05-17 20:53:32 -07002635 chan->expected_ack_seq, chan->unacked_frames);
2636
2637 for (ackseq = chan->expected_ack_seq; ackseq != reqseq;
2638 ackseq = __next_seq(chan, ackseq)) {
2639
2640 acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq);
2641 if (acked_skb) {
2642 skb_unlink(acked_skb, &chan->tx_q);
2643 kfree_skb(acked_skb);
2644 chan->unacked_frames--;
2645 }
2646 }
2647
2648 chan->expected_ack_seq = reqseq;
2649
2650 if (chan->unacked_frames == 0)
2651 __clear_retrans_timer(chan);
2652
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002653 BT_DBG("unacked_frames %u", chan->unacked_frames);
Mat Martineau608bcc62012-05-17 20:53:32 -07002654}
2655
2656static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan)
2657{
2658 BT_DBG("chan %p", chan);
2659
2660 chan->expected_tx_seq = chan->buffer_seq;
2661 l2cap_seq_list_clear(&chan->srej_list);
2662 skb_queue_purge(&chan->srej_q);
2663 chan->rx_state = L2CAP_RX_STATE_RECV;
2664}
2665
Gustavo Padovand6603662012-05-21 13:58:22 -03002666static void l2cap_tx_state_xmit(struct l2cap_chan *chan,
2667 struct l2cap_ctrl *control,
2668 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002669{
Mat Martineau608bcc62012-05-17 20:53:32 -07002670 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2671 event);
2672
2673 switch (event) {
2674 case L2CAP_EV_DATA_REQUEST:
2675 if (chan->tx_send_head == NULL)
2676 chan->tx_send_head = skb_peek(skbs);
2677
2678 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2679 l2cap_ertm_send(chan);
2680 break;
2681 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2682 BT_DBG("Enter LOCAL_BUSY");
2683 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2684
2685 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2686 /* The SREJ_SENT state must be aborted if we are to
2687 * enter the LOCAL_BUSY state.
2688 */
2689 l2cap_abort_rx_srej_sent(chan);
2690 }
2691
2692 l2cap_send_ack(chan);
2693
2694 break;
2695 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2696 BT_DBG("Exit LOCAL_BUSY");
2697 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2698
2699 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2700 struct l2cap_ctrl local_control;
2701
2702 memset(&local_control, 0, sizeof(local_control));
2703 local_control.sframe = 1;
2704 local_control.super = L2CAP_SUPER_RR;
2705 local_control.poll = 1;
2706 local_control.reqseq = chan->buffer_seq;
Mat Martineaua67d7f62012-05-17 20:53:35 -07002707 l2cap_send_sframe(chan, &local_control);
Mat Martineau608bcc62012-05-17 20:53:32 -07002708
2709 chan->retry_count = 1;
2710 __set_monitor_timer(chan);
2711 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2712 }
2713 break;
2714 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2715 l2cap_process_reqseq(chan, control->reqseq);
2716 break;
2717 case L2CAP_EV_EXPLICIT_POLL:
2718 l2cap_send_rr_or_rnr(chan, 1);
2719 chan->retry_count = 1;
2720 __set_monitor_timer(chan);
2721 __clear_ack_timer(chan);
2722 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2723 break;
2724 case L2CAP_EV_RETRANS_TO:
2725 l2cap_send_rr_or_rnr(chan, 1);
2726 chan->retry_count = 1;
2727 __set_monitor_timer(chan);
2728 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2729 break;
2730 case L2CAP_EV_RECV_FBIT:
2731 /* Nothing to process */
2732 break;
2733 default:
2734 break;
2735 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002736}
2737
Gustavo Padovand6603662012-05-21 13:58:22 -03002738static void l2cap_tx_state_wait_f(struct l2cap_chan *chan,
2739 struct l2cap_ctrl *control,
2740 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002741{
Mat Martineau608bcc62012-05-17 20:53:32 -07002742 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2743 event);
2744
2745 switch (event) {
2746 case L2CAP_EV_DATA_REQUEST:
2747 if (chan->tx_send_head == NULL)
2748 chan->tx_send_head = skb_peek(skbs);
2749 /* Queue data, but don't send. */
2750 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2751 break;
2752 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2753 BT_DBG("Enter LOCAL_BUSY");
2754 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2755
2756 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2757 /* The SREJ_SENT state must be aborted if we are to
2758 * enter the LOCAL_BUSY state.
2759 */
2760 l2cap_abort_rx_srej_sent(chan);
2761 }
2762
2763 l2cap_send_ack(chan);
2764
2765 break;
2766 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2767 BT_DBG("Exit LOCAL_BUSY");
2768 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2769
2770 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2771 struct l2cap_ctrl local_control;
2772 memset(&local_control, 0, sizeof(local_control));
2773 local_control.sframe = 1;
2774 local_control.super = L2CAP_SUPER_RR;
2775 local_control.poll = 1;
2776 local_control.reqseq = chan->buffer_seq;
Mat Martineaua67d7f62012-05-17 20:53:35 -07002777 l2cap_send_sframe(chan, &local_control);
Mat Martineau608bcc62012-05-17 20:53:32 -07002778
2779 chan->retry_count = 1;
2780 __set_monitor_timer(chan);
2781 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2782 }
2783 break;
2784 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2785 l2cap_process_reqseq(chan, control->reqseq);
2786
2787 /* Fall through */
2788
2789 case L2CAP_EV_RECV_FBIT:
2790 if (control && control->final) {
2791 __clear_monitor_timer(chan);
2792 if (chan->unacked_frames > 0)
2793 __set_retrans_timer(chan);
2794 chan->retry_count = 0;
2795 chan->tx_state = L2CAP_TX_STATE_XMIT;
2796 BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state);
2797 }
2798 break;
2799 case L2CAP_EV_EXPLICIT_POLL:
2800 /* Ignore */
2801 break;
2802 case L2CAP_EV_MONITOR_TO:
2803 if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) {
2804 l2cap_send_rr_or_rnr(chan, 1);
2805 __set_monitor_timer(chan);
2806 chan->retry_count++;
2807 } else {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02002808 l2cap_send_disconn_req(chan, ECONNABORTED);
Mat Martineau608bcc62012-05-17 20:53:32 -07002809 }
2810 break;
2811 default:
2812 break;
2813 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002814}
2815
Gustavo Padovand6603662012-05-21 13:58:22 -03002816static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
2817 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002818{
Mat Martineau608bcc62012-05-17 20:53:32 -07002819 BT_DBG("chan %p, control %p, skbs %p, event %d, state %d",
2820 chan, control, skbs, event, chan->tx_state);
2821
2822 switch (chan->tx_state) {
2823 case L2CAP_TX_STATE_XMIT:
Gustavo Padovand6603662012-05-21 13:58:22 -03002824 l2cap_tx_state_xmit(chan, control, skbs, event);
Mat Martineau608bcc62012-05-17 20:53:32 -07002825 break;
2826 case L2CAP_TX_STATE_WAIT_F:
Gustavo Padovand6603662012-05-21 13:58:22 -03002827 l2cap_tx_state_wait_f(chan, control, skbs, event);
Mat Martineau608bcc62012-05-17 20:53:32 -07002828 break;
2829 default:
2830 /* Ignore event */
2831 break;
2832 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002833}
2834
Mat Martineau4b51dae92012-05-17 20:53:37 -07002835static void l2cap_pass_to_tx(struct l2cap_chan *chan,
2836 struct l2cap_ctrl *control)
2837{
2838 BT_DBG("chan %p, control %p", chan, control);
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03002839 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT);
Mat Martineau4b51dae92012-05-17 20:53:37 -07002840}
2841
Mat Martineauf80842a2012-05-17 20:53:46 -07002842static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan,
2843 struct l2cap_ctrl *control)
2844{
2845 BT_DBG("chan %p, control %p", chan, control);
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03002846 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT);
Mat Martineauf80842a2012-05-17 20:53:46 -07002847}
2848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849/* Copy frame to all raw sockets on that connection */
2850static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
2851{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 struct sk_buff *nskb;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03002853 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
2855 BT_DBG("conn %p", conn);
2856
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002857 mutex_lock(&conn->chan_lock);
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -02002858
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002859 list_for_each_entry(chan, &conn->chan_l, list) {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03002860 if (chan->chan_type != L2CAP_CHAN_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 continue;
2862
Gustavo Padovan7f5396a2013-10-21 18:22:25 -02002863 /* Don't send frame to the channel it came from */
2864 if (bt_cb(skb)->chan == chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 continue;
Gustavo Padovan7f5396a2013-10-21 18:22:25 -02002866
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002867 nskb = skb_clone(skb, GFP_KERNEL);
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03002868 if (!nskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 continue;
Gustavo Padovan80b98022012-05-27 22:27:51 -03002870 if (chan->ops->recv(chan, nskb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 kfree_skb(nskb);
2872 }
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -02002873
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002874 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
2877/* ---- L2CAP signalling commands ---- */
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002878static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code,
2879 u8 ident, u16 dlen, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
2881 struct sk_buff *skb, **frag;
2882 struct l2cap_cmd_hdr *cmd;
2883 struct l2cap_hdr *lh;
2884 int len, count;
2885
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002886 BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u",
2887 conn, code, ident, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Anderson Lizardo300b9622013-06-02 16:30:40 -04002889 if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE)
2890 return NULL;
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
2893 count = min_t(unsigned int, conn->mtu, len);
2894
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002895 skb = bt_skb_alloc(count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (!skb)
2897 return NULL;
2898
2899 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002900 lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02002901
2902 if (conn->hcon->type == LE_LINK)
Joe Perchesdcf4adb2014-03-12 10:52:35 -07002903 lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02002904 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -07002905 lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
2907 cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
2908 cmd->code = code;
2909 cmd->ident = ident;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002910 cmd->len = cpu_to_le16(dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912 if (dlen) {
2913 count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
2914 memcpy(skb_put(skb, count), data, count);
2915 data += count;
2916 }
2917
2918 len -= skb->len;
2919
2920 /* Continuation fragments (no L2CAP header) */
2921 frag = &skb_shinfo(skb)->frag_list;
2922 while (len) {
2923 count = min_t(unsigned int, conn->mtu, len);
2924
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002925 *frag = bt_skb_alloc(count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 if (!*frag)
2927 goto fail;
2928
2929 memcpy(skb_put(*frag, count), data, count);
2930
2931 len -= count;
2932 data += count;
2933
2934 frag = &(*frag)->next;
2935 }
2936
2937 return skb;
2938
2939fail:
2940 kfree_skb(skb);
2941 return NULL;
2942}
2943
Gustavo Padovan2d792812012-10-06 10:07:01 +01002944static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen,
2945 unsigned long *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
2947 struct l2cap_conf_opt *opt = *ptr;
2948 int len;
2949
2950 len = L2CAP_CONF_OPT_SIZE + opt->len;
2951 *ptr += len;
2952
2953 *type = opt->type;
2954 *olen = opt->len;
2955
2956 switch (opt->len) {
2957 case 1:
2958 *val = *((u8 *) opt->val);
2959 break;
2960
2961 case 2:
steven miaobfaaeb32010-10-16 18:29:47 -04002962 *val = get_unaligned_le16(opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 break;
2964
2965 case 4:
steven miaobfaaeb32010-10-16 18:29:47 -04002966 *val = get_unaligned_le32(opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 break;
2968
2969 default:
2970 *val = (unsigned long) opt->val;
2971 break;
2972 }
2973
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002974 BT_DBG("type 0x%2.2x len %u val 0x%lx", *type, opt->len, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return len;
2976}
2977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
2979{
2980 struct l2cap_conf_opt *opt = *ptr;
2981
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002982 BT_DBG("type 0x%2.2x len %u val 0x%lx", type, len, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 opt->type = type;
2985 opt->len = len;
2986
2987 switch (len) {
2988 case 1:
2989 *((u8 *) opt->val) = val;
2990 break;
2991
2992 case 2:
Gustavo F. Padovan4f8b6912010-10-18 14:25:53 -02002993 put_unaligned_le16(val, opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 break;
2995
2996 case 4:
Gustavo F. Padovan4f8b6912010-10-18 14:25:53 -02002997 put_unaligned_le32(val, opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 break;
2999
3000 default:
3001 memcpy(opt->val, (void *) val, len);
3002 break;
3003 }
3004
3005 *ptr += L2CAP_CONF_OPT_SIZE + len;
3006}
3007
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003008static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan)
3009{
3010 struct l2cap_conf_efs efs;
3011
Szymon Janc1ec918c2011-11-16 09:32:21 +01003012 switch (chan->mode) {
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003013 case L2CAP_MODE_ERTM:
3014 efs.id = chan->local_id;
3015 efs.stype = chan->local_stype;
3016 efs.msdu = cpu_to_le16(chan->local_msdu);
3017 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003018 efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT);
3019 efs.flush_to = cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003020 break;
3021
3022 case L2CAP_MODE_STREAMING:
3023 efs.id = 1;
3024 efs.stype = L2CAP_SERV_BESTEFFORT;
3025 efs.msdu = cpu_to_le16(chan->local_msdu);
3026 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
3027 efs.acc_lat = 0;
3028 efs.flush_to = 0;
3029 break;
3030
3031 default:
3032 return;
3033 }
3034
3035 l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs),
Andrei Emeltchenko8936fa62012-10-08 11:14:41 +03003036 (unsigned long) &efs);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003037}
3038
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003039static void l2cap_ack_timeout(struct work_struct *work)
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003040{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003041 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau03625202012-05-17 20:53:51 -07003042 ack_timer.work);
3043 u16 frames_to_ack;
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003044
Gustavo F. Padovan2fb9b3d2011-12-22 16:56:05 -02003045 BT_DBG("chan %p", chan);
3046
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003047 l2cap_chan_lock(chan);
3048
Mat Martineau03625202012-05-17 20:53:51 -07003049 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
3050 chan->last_acked_seq);
3051
3052 if (frames_to_ack)
3053 l2cap_send_rr_or_rnr(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003054
3055 l2cap_chan_unlock(chan);
Szymon Janc09bfb2e2012-01-11 10:59:49 +01003056 l2cap_chan_put(chan);
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003057}
3058
Andrei Emeltchenko466f8002012-05-29 13:59:01 +03003059int l2cap_ertm_init(struct l2cap_chan *chan)
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003060{
Mat Martineau3c588192012-04-11 10:48:42 -07003061 int err;
3062
Mat Martineau105bdf92012-04-27 16:50:48 -07003063 chan->next_tx_seq = 0;
3064 chan->expected_tx_seq = 0;
Gustavo F. Padovan42e5c802011-03-25 19:58:34 -03003065 chan->expected_ack_seq = 0;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03003066 chan->unacked_frames = 0;
Gustavo F. Padovan42e5c802011-03-25 19:58:34 -03003067 chan->buffer_seq = 0;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03003068 chan->frames_sent = 0;
Mat Martineau105bdf92012-04-27 16:50:48 -07003069 chan->last_acked_seq = 0;
3070 chan->sdu = NULL;
3071 chan->sdu_last_frag = NULL;
3072 chan->sdu_len = 0;
3073
Mat Martineaud34c34f2012-05-14 14:49:27 -07003074 skb_queue_head_init(&chan->tx_q);
3075
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003076 chan->local_amp_id = AMP_ID_BREDR;
3077 chan->move_id = AMP_ID_BREDR;
Mat Martineau08333282012-10-23 15:24:06 -07003078 chan->move_state = L2CAP_MOVE_STABLE;
3079 chan->move_role = L2CAP_MOVE_ROLE_NONE;
3080
Mat Martineau105bdf92012-04-27 16:50:48 -07003081 if (chan->mode != L2CAP_MODE_ERTM)
3082 return 0;
3083
3084 chan->rx_state = L2CAP_RX_STATE_RECV;
3085 chan->tx_state = L2CAP_TX_STATE_XMIT;
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003086
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003087 INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout);
3088 INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout);
3089 INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003090
Gustavo F. Padovanf1c67752011-03-25 20:36:10 -03003091 skb_queue_head_init(&chan->srej_q);
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03003092
Mat Martineau3c588192012-04-11 10:48:42 -07003093 err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win);
3094 if (err < 0)
3095 return err;
3096
Mat Martineau9dc9aff2012-05-17 16:20:14 -07003097 err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win);
3098 if (err < 0)
3099 l2cap_seq_list_free(&chan->srej_list);
3100
3101 return err;
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003102}
3103
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003104static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
3105{
3106 switch (mode) {
3107 case L2CAP_MODE_STREAMING:
3108 case L2CAP_MODE_ERTM:
3109 if (l2cap_mode_supported(mode, remote_feat_mask))
3110 return mode;
3111 /* fall through */
3112 default:
3113 return L2CAP_MODE_BASIC;
3114 }
3115}
3116
Marcel Holtmann848566b2013-10-01 22:59:22 -07003117static inline bool __l2cap_ews_supported(struct l2cap_conn *conn)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003118{
Marcel Holtmann848566b2013-10-01 22:59:22 -07003119 return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_WINDOW;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003120}
3121
Marcel Holtmann848566b2013-10-01 22:59:22 -07003122static inline bool __l2cap_efs_supported(struct l2cap_conn *conn)
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003123{
Marcel Holtmann848566b2013-10-01 22:59:22 -07003124 return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_FLOW;
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003125}
3126
Mat Martineau36c86c82012-10-23 15:24:20 -07003127static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan,
3128 struct l2cap_conf_rfc *rfc)
3129{
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003130 if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) {
Mat Martineau36c86c82012-10-23 15:24:20 -07003131 u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to;
3132
3133 /* Class 1 devices have must have ERTM timeouts
3134 * exceeding the Link Supervision Timeout. The
3135 * default Link Supervision Timeout for AMP
3136 * controllers is 10 seconds.
3137 *
3138 * Class 1 devices use 0xffffffff for their
3139 * best-effort flush timeout, so the clamping logic
3140 * will result in a timeout that meets the above
3141 * requirement. ERTM timeouts are 16-bit values, so
3142 * the maximum timeout is 65.535 seconds.
3143 */
3144
3145 /* Convert timeout to milliseconds and round */
3146 ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000);
3147
3148 /* This is the recommended formula for class 2 devices
3149 * that start ERTM timers when packets are sent to the
3150 * controller.
3151 */
3152 ertm_to = 3 * ertm_to + 500;
3153
3154 if (ertm_to > 0xffff)
3155 ertm_to = 0xffff;
3156
3157 rfc->retrans_timeout = cpu_to_le16((u16) ertm_to);
3158 rfc->monitor_timeout = rfc->retrans_timeout;
3159 } else {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003160 rfc->retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
3161 rfc->monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
Mat Martineau36c86c82012-10-23 15:24:20 -07003162 }
3163}
3164
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003165static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
3166{
3167 if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW &&
Marcel Holtmann848566b2013-10-01 22:59:22 -07003168 __l2cap_ews_supported(chan->conn)) {
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003169 /* use extended control field */
3170 set_bit(FLAG_EXT_CTRL, &chan->flags);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003171 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
3172 } else {
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003173 chan->tx_win = min_t(u16, chan->tx_win,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003174 L2CAP_DEFAULT_TX_WINDOW);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003175 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
3176 }
Mat Martineauc20f8e32012-07-10 05:47:07 -07003177 chan->ack_win = chan->tx_win;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003178}
3179
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003180static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 struct l2cap_conf_req *req = data;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003183 struct l2cap_conf_rfc rfc = { .mode = chan->mode };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 void *ptr = req->data;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003185 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03003187 BT_DBG("chan %p", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003189 if (chan->num_conf_req || chan->num_conf_rsp)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003190 goto done;
3191
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003192 switch (chan->mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003193 case L2CAP_MODE_STREAMING:
3194 case L2CAP_MODE_ERTM:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003195 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003196 break;
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003197
Marcel Holtmann848566b2013-10-01 22:59:22 -07003198 if (__l2cap_efs_supported(chan->conn))
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003199 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3200
Gustavo F. Padovan2ba13ed2010-06-09 16:39:05 -03003201 /* fall through */
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003202 default:
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003203 chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003204 break;
3205 }
3206
3207done:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003208 if (chan->imtu != L2CAP_DEFAULT_MTU)
3209 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
Gustavo F. Padovan7990681c2011-01-24 16:01:43 -02003210
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003211 switch (chan->mode) {
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003212 case L2CAP_MODE_BASIC:
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003213 if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003214 !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003215 break;
3216
Gustavo F. Padovan62547752010-06-08 20:05:31 -03003217 rfc.mode = L2CAP_MODE_BASIC;
3218 rfc.txwin_size = 0;
3219 rfc.max_transmit = 0;
3220 rfc.retrans_timeout = 0;
3221 rfc.monitor_timeout = 0;
3222 rfc.max_pdu_size = 0;
3223
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003224 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003225 (unsigned long) &rfc);
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003226 break;
3227
3228 case L2CAP_MODE_ERTM:
3229 rfc.mode = L2CAP_MODE_ERTM;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003230 rfc.max_transmit = chan->max_tx;
Mat Martineau36c86c82012-10-23 15:24:20 -07003231
3232 __l2cap_set_ertm_timeouts(chan, &rfc);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003233
3234 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
Gustavo Padovan2d792812012-10-06 10:07:01 +01003235 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3236 L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003237 rfc.max_pdu_size = cpu_to_le16(size);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003238
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003239 l2cap_txwin_setup(chan);
3240
3241 rfc.txwin_size = min_t(u16, chan->tx_win,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003242 L2CAP_DEFAULT_TX_WINDOW);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003243
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003244 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003245 (unsigned long) &rfc);
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003246
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003247 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
3248 l2cap_add_opt_efs(&ptr, chan);
3249
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003250 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3251 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003252 chan->tx_win);
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003253
3254 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3255 if (chan->fcs == L2CAP_FCS_NONE ||
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003256 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003257 chan->fcs = L2CAP_FCS_NONE;
3258 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
3259 chan->fcs);
3260 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003261 break;
3262
3263 case L2CAP_MODE_STREAMING:
Mat Martineau273759e2012-05-17 20:53:53 -07003264 l2cap_txwin_setup(chan);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003265 rfc.mode = L2CAP_MODE_STREAMING;
3266 rfc.txwin_size = 0;
3267 rfc.max_transmit = 0;
3268 rfc.retrans_timeout = 0;
3269 rfc.monitor_timeout = 0;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003270
3271 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
Gustavo Padovan2d792812012-10-06 10:07:01 +01003272 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3273 L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003274 rfc.max_pdu_size = cpu_to_le16(size);
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003275
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003276 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003277 (unsigned long) &rfc);
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003278
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003279 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
3280 l2cap_add_opt_efs(&ptr, chan);
3281
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003282 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3283 if (chan->fcs == L2CAP_FCS_NONE ||
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003284 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003285 chan->fcs = L2CAP_FCS_NONE;
3286 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
3287 chan->fcs);
3288 }
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003289 break;
3290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003292 req->dcid = cpu_to_le16(chan->dcid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003293 req->flags = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
3295 return ptr - data;
3296}
3297
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003298static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003300 struct l2cap_conf_rsp *rsp = data;
3301 void *ptr = rsp->data;
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003302 void *req = chan->conf_req;
3303 int len = chan->conf_len;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003304 int type, hint, olen;
3305 unsigned long val;
Marcel Holtmann6464f352007-10-20 13:39:51 +02003306 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003307 struct l2cap_conf_efs efs;
3308 u8 remote_efs = 0;
Marcel Holtmann861d6882007-10-20 13:37:06 +02003309 u16 mtu = L2CAP_DEFAULT_MTU;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003310 u16 result = L2CAP_CONF_SUCCESS;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003311 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003313 BT_DBG("chan %p", chan);
Marcel Holtmann820ae1b2006-11-18 22:15:00 +01003314
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003315 while (len >= L2CAP_CONF_OPT_SIZE) {
3316 len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Gustavo F. Padovan589d2742009-04-20 01:31:07 -03003318 hint = type & L2CAP_CONF_HINT;
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -07003319 type &= L2CAP_CONF_MASK;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003320
3321 switch (type) {
3322 case L2CAP_CONF_MTU:
Marcel Holtmann861d6882007-10-20 13:37:06 +02003323 mtu = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003324 break;
3325
3326 case L2CAP_CONF_FLUSH_TO:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003327 chan->flush_to = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003328 break;
3329
3330 case L2CAP_CONF_QOS:
3331 break;
3332
Marcel Holtmann6464f352007-10-20 13:39:51 +02003333 case L2CAP_CONF_RFC:
3334 if (olen == sizeof(rfc))
3335 memcpy(&rfc, (void *) val, olen);
3336 break;
3337
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003338 case L2CAP_CONF_FCS:
3339 if (val == L2CAP_FCS_NONE)
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003340 set_bit(CONF_RECV_NO_FCS, &chan->conf_state);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003341 break;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003342
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003343 case L2CAP_CONF_EFS:
3344 remote_efs = 1;
3345 if (olen == sizeof(efs))
3346 memcpy(&efs, (void *) val, olen);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003347 break;
3348
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003349 case L2CAP_CONF_EWS:
Marcel Holtmann848566b2013-10-01 22:59:22 -07003350 if (!chan->conn->hs_enabled)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003351 return -ECONNREFUSED;
3352
3353 set_bit(FLAG_EXT_CTRL, &chan->flags);
3354 set_bit(CONF_EWS_RECV, &chan->conf_state);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003355 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003356 chan->remote_tx_win = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003357 break;
3358
3359 default:
3360 if (hint)
3361 break;
3362
3363 result = L2CAP_CONF_UNKNOWN;
3364 *((u8 *) ptr++) = type;
3365 break;
3366 }
3367 }
3368
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003369 if (chan->num_conf_rsp || chan->num_conf_req > 1)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003370 goto done;
3371
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003372 switch (chan->mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003373 case L2CAP_MODE_STREAMING:
3374 case L2CAP_MODE_ERTM:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003375 if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003376 chan->mode = l2cap_select_mode(rfc.mode,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003377 chan->conn->feat_mask);
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003378 break;
3379 }
3380
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003381 if (remote_efs) {
Marcel Holtmann848566b2013-10-01 22:59:22 -07003382 if (__l2cap_efs_supported(chan->conn))
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003383 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3384 else
3385 return -ECONNREFUSED;
3386 }
3387
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003388 if (chan->mode != rfc.mode)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003389 return -ECONNREFUSED;
Gustavo F. Padovan742e5192010-06-08 19:09:48 -03003390
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003391 break;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003392 }
3393
3394done:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003395 if (chan->mode != rfc.mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003396 result = L2CAP_CONF_UNACCEPT;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003397 rfc.mode = chan->mode;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003398
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003399 if (chan->num_conf_rsp == 1)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003400 return -ECONNREFUSED;
3401
Gustavo Padovan2d792812012-10-06 10:07:01 +01003402 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3403 (unsigned long) &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003404 }
3405
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003406 if (result == L2CAP_CONF_SUCCESS) {
3407 /* Configure output options and let the other side know
3408 * which ones we don't like. */
3409
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003410 if (mtu < L2CAP_DEFAULT_MIN_MTU)
3411 result = L2CAP_CONF_UNACCEPT;
3412 else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003413 chan->omtu = mtu;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003414 set_bit(CONF_MTU_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003415 }
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003416 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003417
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003418 if (remote_efs) {
3419 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003420 efs.stype != L2CAP_SERV_NOTRAFIC &&
3421 efs.stype != chan->local_stype) {
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003422
3423 result = L2CAP_CONF_UNACCEPT;
3424
3425 if (chan->num_conf_req >= 1)
3426 return -ECONNREFUSED;
3427
3428 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003429 sizeof(efs),
3430 (unsigned long) &efs);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003431 } else {
Gustavo F. Padovan3e6b3b92011-11-01 14:06:23 -02003432 /* Send PENDING Conf Rsp */
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003433 result = L2CAP_CONF_PENDING;
3434 set_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003435 }
3436 }
3437
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003438 switch (rfc.mode) {
3439 case L2CAP_MODE_BASIC:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003440 chan->fcs = L2CAP_FCS_NONE;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003441 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003442 break;
3443
3444 case L2CAP_MODE_ERTM:
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003445 if (!test_bit(CONF_EWS_RECV, &chan->conf_state))
3446 chan->remote_tx_win = rfc.txwin_size;
3447 else
3448 rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW;
3449
Gustavo F. Padovan2c03a7a2011-03-25 20:15:28 -03003450 chan->remote_max_tx = rfc.max_transmit;
Mat Martineau86b1b262010-08-05 15:54:22 -07003451
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003452 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003453 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3454 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003455 rfc.max_pdu_size = cpu_to_le16(size);
3456 chan->remote_mps = size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003457
Mat Martineau36c86c82012-10-23 15:24:20 -07003458 __l2cap_set_ertm_timeouts(chan, &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003459
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003460 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003461
3462 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003463 sizeof(rfc), (unsigned long) &rfc);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003464
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003465 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3466 chan->remote_id = efs.id;
3467 chan->remote_stype = efs.stype;
3468 chan->remote_msdu = le16_to_cpu(efs.msdu);
3469 chan->remote_flush_to =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003470 le32_to_cpu(efs.flush_to);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003471 chan->remote_acc_lat =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003472 le32_to_cpu(efs.acc_lat);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003473 chan->remote_sdu_itime =
3474 le32_to_cpu(efs.sdu_itime);
3475 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003476 sizeof(efs),
3477 (unsigned long) &efs);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003478 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003479 break;
3480
3481 case L2CAP_MODE_STREAMING:
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003482 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003483 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3484 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003485 rfc.max_pdu_size = cpu_to_le16(size);
3486 chan->remote_mps = size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003487
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003488 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003489
Gustavo Padovan2d792812012-10-06 10:07:01 +01003490 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3491 (unsigned long) &rfc);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003492
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003493 break;
3494
3495 default:
Marcel Holtmann6464f352007-10-20 13:39:51 +02003496 result = L2CAP_CONF_UNACCEPT;
3497
3498 memset(&rfc, 0, sizeof(rfc));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003499 rfc.mode = chan->mode;
Marcel Holtmann6464f352007-10-20 13:39:51 +02003500 }
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003501
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003502 if (result == L2CAP_CONF_SUCCESS)
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003503 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003504 }
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003505 rsp->scid = cpu_to_le16(chan->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003506 rsp->result = cpu_to_le16(result);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003507 rsp->flags = cpu_to_le16(0);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003508
3509 return ptr - data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
Gustavo Padovan2d792812012-10-06 10:07:01 +01003512static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
3513 void *data, u16 *result)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003514{
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003515 struct l2cap_conf_req *req = data;
3516 void *ptr = req->data;
3517 int type, olen;
3518 unsigned long val;
Mat Martineau36e999a2011-12-08 17:23:21 -08003519 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003520 struct l2cap_conf_efs efs;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003521
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003522 BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003523
3524 while (len >= L2CAP_CONF_OPT_SIZE) {
3525 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3526
3527 switch (type) {
3528 case L2CAP_CONF_MTU:
3529 if (val < L2CAP_DEFAULT_MIN_MTU) {
3530 *result = L2CAP_CONF_UNACCEPT;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003531 chan->imtu = L2CAP_DEFAULT_MIN_MTU;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003532 } else
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003533 chan->imtu = val;
3534 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003535 break;
3536
3537 case L2CAP_CONF_FLUSH_TO:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003538 chan->flush_to = val;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003539 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003540 2, chan->flush_to);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003541 break;
3542
3543 case L2CAP_CONF_RFC:
3544 if (olen == sizeof(rfc))
3545 memcpy(&rfc, (void *)val, olen);
3546
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003547 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003548 rfc.mode != chan->mode)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003549 return -ECONNREFUSED;
3550
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003551 chan->fcs = 0;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003552
3553 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003554 sizeof(rfc), (unsigned long) &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003555 break;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003556
3557 case L2CAP_CONF_EWS:
Mat Martineauc20f8e32012-07-10 05:47:07 -07003558 chan->ack_win = min_t(u16, val, chan->ack_win);
Gustavo F. Padovan3e6b3b92011-11-01 14:06:23 -02003559 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
Mat Martineauc20f8e32012-07-10 05:47:07 -07003560 chan->tx_win);
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003561 break;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003562
3563 case L2CAP_CONF_EFS:
3564 if (olen == sizeof(efs))
3565 memcpy(&efs, (void *)val, olen);
3566
3567 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003568 efs.stype != L2CAP_SERV_NOTRAFIC &&
3569 efs.stype != chan->local_stype)
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003570 return -ECONNREFUSED;
3571
Gustavo Padovan2d792812012-10-06 10:07:01 +01003572 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs),
3573 (unsigned long) &efs);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003574 break;
Andrei Emeltchenkocbabee72012-11-29 17:46:07 +02003575
3576 case L2CAP_CONF_FCS:
3577 if (*result == L2CAP_CONF_PENDING)
3578 if (val == L2CAP_FCS_NONE)
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003579 set_bit(CONF_RECV_NO_FCS,
Andrei Emeltchenkocbabee72012-11-29 17:46:07 +02003580 &chan->conf_state);
3581 break;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003582 }
3583 }
3584
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003585 if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
Gustavo F. Padovan6c2ea7a2010-06-08 20:08:49 -03003586 return -ECONNREFUSED;
3587
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003588 chan->mode = rfc.mode;
Gustavo F. Padovan6c2ea7a2010-06-08 20:08:49 -03003589
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003590 if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003591 switch (rfc.mode) {
3592 case L2CAP_MODE_ERTM:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003593 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3594 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
3595 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Mat Martineauc20f8e32012-07-10 05:47:07 -07003596 if (!test_bit(FLAG_EXT_CTRL, &chan->flags))
3597 chan->ack_win = min_t(u16, chan->ack_win,
3598 rfc.txwin_size);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003599
3600 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3601 chan->local_msdu = le16_to_cpu(efs.msdu);
3602 chan->local_sdu_itime =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003603 le32_to_cpu(efs.sdu_itime);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003604 chan->local_acc_lat = le32_to_cpu(efs.acc_lat);
3605 chan->local_flush_to =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003606 le32_to_cpu(efs.flush_to);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003607 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003608 break;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003609
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003610 case L2CAP_MODE_STREAMING:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003611 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003612 }
3613 }
3614
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003615 req->dcid = cpu_to_le16(chan->dcid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003616 req->flags = cpu_to_le16(0);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003617
3618 return ptr - data;
3619}
3620
Gustavo Padovan2d792812012-10-06 10:07:01 +01003621static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data,
3622 u16 result, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
3624 struct l2cap_conf_rsp *rsp = data;
3625 void *ptr = rsp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003627 BT_DBG("chan %p", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003629 rsp->scid = cpu_to_le16(chan->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003630 rsp->result = cpu_to_le16(result);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07003631 rsp->flags = cpu_to_le16(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
3633 return ptr - data;
3634}
3635
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03003636void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan)
3637{
3638 struct l2cap_le_conn_rsp rsp;
3639 struct l2cap_conn *conn = chan->conn;
3640
3641 BT_DBG("chan %p", chan);
3642
3643 rsp.dcid = cpu_to_le16(chan->scid);
3644 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02003645 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03003646 rsp.credits = cpu_to_le16(chan->rx_credits);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003647 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03003648
3649 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
3650 &rsp);
3651}
3652
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003653void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003654{
3655 struct l2cap_conn_rsp rsp;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003656 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003657 u8 buf[128];
Andrei Emeltchenko439f34a2012-10-31 15:46:28 +02003658 u8 rsp_code;
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003659
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003660 rsp.scid = cpu_to_le16(chan->dcid);
3661 rsp.dcid = cpu_to_le16(chan->scid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003662 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
3663 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Andrei Emeltchenko439f34a2012-10-31 15:46:28 +02003664
3665 if (chan->hs_hcon)
3666 rsp_code = L2CAP_CREATE_CHAN_RSP;
3667 else
3668 rsp_code = L2CAP_CONN_RSP;
3669
3670 BT_DBG("chan %p rsp_code %u", chan, rsp_code);
3671
3672 l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp);
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003673
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003674 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003675 return;
3676
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003677 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003678 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003679 chan->num_conf_req++;
3680}
3681
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003682static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003683{
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003684 int type, olen;
3685 unsigned long val;
Mat Martineauc20f8e32012-07-10 05:47:07 -07003686 /* Use sane default values in case a misbehaving remote device
3687 * did not send an RFC or extended window size option.
3688 */
3689 u16 txwin_ext = chan->ack_win;
3690 struct l2cap_conf_rfc rfc = {
3691 .mode = chan->mode,
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003692 .retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO),
3693 .monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO),
Mat Martineauc20f8e32012-07-10 05:47:07 -07003694 .max_pdu_size = cpu_to_le16(chan->imtu),
3695 .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW),
3696 };
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003697
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003698 BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003699
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003700 if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003701 return;
3702
3703 while (len >= L2CAP_CONF_OPT_SIZE) {
3704 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3705
Mat Martineauc20f8e32012-07-10 05:47:07 -07003706 switch (type) {
3707 case L2CAP_CONF_RFC:
3708 if (olen == sizeof(rfc))
3709 memcpy(&rfc, (void *)val, olen);
Szymon Janc8f321f82012-06-08 11:33:33 +02003710 break;
Mat Martineauc20f8e32012-07-10 05:47:07 -07003711 case L2CAP_CONF_EWS:
3712 txwin_ext = val;
3713 break;
3714 }
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003715 }
3716
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003717 switch (rfc.mode) {
3718 case L2CAP_MODE_ERTM:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003719 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3720 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
Mat Martineauc20f8e32012-07-10 05:47:07 -07003721 chan->mps = le16_to_cpu(rfc.max_pdu_size);
3722 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3723 chan->ack_win = min_t(u16, chan->ack_win, txwin_ext);
3724 else
3725 chan->ack_win = min_t(u16, chan->ack_win,
3726 rfc.txwin_size);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003727 break;
3728 case L2CAP_MODE_STREAMING:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003729 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003730 }
3731}
3732
Gustavo Padovan2d792812012-10-06 10:07:01 +01003733static inline int l2cap_command_rej(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003734 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3735 u8 *data)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003736{
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003737 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003738
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003739 if (cmd_len < sizeof(*rej))
3740 return -EPROTO;
3741
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003742 if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003743 return 0;
3744
3745 if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003746 cmd->ident == conn->info_ident) {
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02003747 cancel_delayed_work(&conn->info_timer);
Marcel Holtmann984947d2009-02-06 23:35:19 +01003748
3749 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01003750 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01003751
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003752 l2cap_conn_start(conn);
3753 }
3754
3755 return 0;
3756}
3757
Mat Martineau17009152012-10-23 15:24:07 -07003758static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3759 struct l2cap_cmd_hdr *cmd,
3760 u8 *data, u8 rsp_code, u8 amp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
3763 struct l2cap_conn_rsp rsp;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003764 struct l2cap_chan *chan = NULL, *pchan;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003765 int result, status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
3767 u16 dcid = 0, scid = __le16_to_cpu(req->scid);
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003768 __le16 psm = req->psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Andrei Emeltchenko097db762012-03-09 14:16:17 +02003770 BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
3772 /* Check if we have socket listening on psm */
Marcel Holtmann6f59b902013-10-13 05:24:01 -07003773 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
Johan Hedbergbf20fd42013-05-14 13:23:13 +03003774 &conn->hcon->dst, ACL_LINK);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003775 if (!pchan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 result = L2CAP_CR_BAD_PSM;
3777 goto sendresp;
3778 }
3779
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003780 mutex_lock(&conn->chan_lock);
Gustavo Padovan8ffb9292013-10-21 14:21:41 -02003781 l2cap_chan_lock(pchan);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00003782
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003783 /* Check if the ACL is secure enough (if not SDP) */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003784 if (psm != cpu_to_le16(L2CAP_PSM_SDP) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003785 !hci_conn_check_link_mode(conn->hcon)) {
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02003786 conn->disc_reason = HCI_ERROR_AUTH_FAILURE;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003787 result = L2CAP_CR_SEC_BLOCK;
3788 goto response;
3789 }
3790
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 result = L2CAP_CR_NO_MEM;
3792
Gustavo Padovan2dfa1002012-05-27 22:27:58 -03003793 /* Check if we already have channel with that dcid */
3794 if (__l2cap_get_chan_by_dcid(conn, scid))
3795 goto response;
3796
Gustavo Padovan80b98022012-05-27 22:27:51 -03003797 chan = pchan->ops->new_connection(pchan);
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03003798 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 goto response;
3800
Syam Sidhardhan330b6c12013-08-06 01:59:12 +09003801 /* For certain devices (ex: HID mouse), support for authentication,
3802 * pairing and bonding is optional. For such devices, inorder to avoid
3803 * the ACL alive for too long after L2CAP disconnection, reset the ACL
3804 * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect.
3805 */
3806 conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
3807
Marcel Holtmann7eafc592013-10-13 08:12:47 -07003808 bacpy(&chan->src, &conn->hcon->src);
3809 bacpy(&chan->dst, &conn->hcon->dst);
Marcel Holtmann4f1654e2013-10-13 08:50:41 -07003810 chan->src_type = bdaddr_type(conn->hcon, conn->hcon->src_type);
3811 chan->dst_type = bdaddr_type(conn->hcon, conn->hcon->dst_type);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003812 chan->psm = psm;
3813 chan->dcid = scid;
Mat Martineau17009152012-10-23 15:24:07 -07003814 chan->local_amp_id = amp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003816 __l2cap_chan_add(conn, chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003817
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003818 dcid = chan->scid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
Gustavo Padovan8d836d72013-10-15 19:24:47 -03003820 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03003822 chan->ident = cmd->ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
Marcel Holtmann984947d2009-02-06 23:35:19 +01003824 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
Gustavo F. Padovand45fc422011-11-05 19:54:24 -02003825 if (l2cap_chan_check_security(chan)) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07003826 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003827 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003828 result = L2CAP_CR_PEND;
3829 status = L2CAP_CS_AUTHOR_PEND;
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08003830 chan->ops->defer(chan);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003831 } else {
Mat Martineau17009152012-10-23 15:24:07 -07003832 /* Force pending result for AMP controllers.
3833 * The connection will succeed after the
3834 * physical link is up.
3835 */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003836 if (amp_id == AMP_ID_BREDR) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003837 l2cap_state_change(chan, BT_CONFIG);
Mat Martineau17009152012-10-23 15:24:07 -07003838 result = L2CAP_CR_SUCCESS;
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003839 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003840 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003841 result = L2CAP_CR_PEND;
Mat Martineau17009152012-10-23 15:24:07 -07003842 }
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003843 status = L2CAP_CS_NO_INFO;
3844 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003845 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003846 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003847 result = L2CAP_CR_PEND;
3848 status = L2CAP_CS_AUTHEN_PEND;
3849 }
3850 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003851 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003852 result = L2CAP_CR_PEND;
3853 status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 }
3855
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856response:
Gustavo Padovan8ffb9292013-10-21 14:21:41 -02003857 l2cap_chan_unlock(pchan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003858 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
3860sendresp:
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07003861 rsp.scid = cpu_to_le16(scid);
3862 rsp.dcid = cpu_to_le16(dcid);
3863 rsp.result = cpu_to_le16(result);
3864 rsp.status = cpu_to_le16(status);
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003865 l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003866
3867 if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
3868 struct l2cap_info_req info;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003869 info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003870
3871 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
3872 conn->info_ident = l2cap_get_ident(conn);
3873
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08003874 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003875
Gustavo Padovan2d792812012-10-06 10:07:01 +01003876 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
3877 sizeof(info), &info);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003878 }
3879
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003880 if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003881 result == L2CAP_CR_SUCCESS) {
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003882 u8 buf[128];
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003883 set_bit(CONF_REQ_SENT, &chan->conf_state);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003884 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003885 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003886 chan->num_conf_req++;
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003887 }
Mat Martineau17009152012-10-23 15:24:07 -07003888
3889 return chan;
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003890}
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003891
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003892static int l2cap_connect_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003893 struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003894{
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303895 struct hci_dev *hdev = conn->hcon->hdev;
3896 struct hci_conn *hcon = conn->hcon;
3897
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003898 if (cmd_len < sizeof(struct l2cap_conn_req))
3899 return -EPROTO;
3900
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303901 hci_dev_lock(hdev);
3902 if (test_bit(HCI_MGMT, &hdev->dev_flags) &&
3903 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags))
3904 mgmt_device_connected(hdev, &hcon->dst, hcon->type,
3905 hcon->dst_type, 0, NULL, 0,
3906 hcon->dev_class);
3907 hci_dev_unlock(hdev);
3908
Gustavo Padovan300229f2012-10-12 19:40:40 +08003909 l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 return 0;
3911}
3912
Mat Martineau5909cf32012-10-23 15:24:08 -07003913static int l2cap_connect_create_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003914 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3915 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
3917 struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
3918 u16 scid, dcid, result, status;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003919 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 u8 req[128];
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003921 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003923 if (cmd_len < sizeof(*rsp))
3924 return -EPROTO;
3925
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 scid = __le16_to_cpu(rsp->scid);
3927 dcid = __le16_to_cpu(rsp->dcid);
3928 result = __le16_to_cpu(rsp->result);
3929 status = __le16_to_cpu(rsp->status);
3930
Andrei Emeltchenko1b009c92012-02-21 12:54:54 +02003931 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 +01003932 dcid, scid, result, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003934 mutex_lock(&conn->chan_lock);
3935
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 if (scid) {
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003937 chan = __l2cap_get_chan_by_scid(conn, scid);
3938 if (!chan) {
Johan Hedberg21870b52013-09-16 13:05:14 +03003939 err = -EBADSLT;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003940 goto unlock;
3941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 } else {
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003943 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
3944 if (!chan) {
Johan Hedberg21870b52013-09-16 13:05:14 +03003945 err = -EBADSLT;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003946 goto unlock;
3947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 }
3949
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003950 err = 0;
3951
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003952 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003953
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 switch (result) {
3955 case L2CAP_CR_SUCCESS:
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03003956 l2cap_state_change(chan, BT_CONFIG);
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03003957 chan->ident = 0;
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003958 chan->dcid = dcid;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003959 clear_bit(CONF_CONNECT_PEND, &chan->conf_state);
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01003960
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003961 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003962 break;
3963
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003965 l2cap_build_conf_req(chan, req), req);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003966 chan->num_conf_req++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 break;
3968
3969 case L2CAP_CR_PEND:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003970 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 break;
3972
3973 default:
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003974 l2cap_chan_del(chan, ECONNREFUSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 break;
3976 }
3977
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003978 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003979
3980unlock:
3981 mutex_unlock(&conn->chan_lock);
3982
3983 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984}
3985
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003986static inline void set_default_fcs(struct l2cap_chan *chan)
Mat Martineau8c462b62010-08-24 15:35:42 -07003987{
3988 /* FCS is enabled only in ERTM or streaming mode, if one or both
3989 * sides request it.
3990 */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003991 if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003992 chan->fcs = L2CAP_FCS_NONE;
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003993 else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state))
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003994 chan->fcs = L2CAP_FCS_CRC16;
Mat Martineau8c462b62010-08-24 15:35:42 -07003995}
3996
Andrei Emeltchenko29d8a592012-09-21 12:30:05 +03003997static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data,
3998 u8 ident, u16 flags)
3999{
4000 struct l2cap_conn *conn = chan->conn;
4001
4002 BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident,
4003 flags);
4004
4005 clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
4006 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
4007
4008 l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP,
4009 l2cap_build_conf_rsp(chan, data,
4010 L2CAP_CONF_SUCCESS, flags), data);
4011}
4012
Johan Hedberg662d6522013-10-16 11:20:47 +03004013static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident,
4014 u16 scid, u16 dcid)
4015{
4016 struct l2cap_cmd_rej_cid rej;
4017
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004018 rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID);
Johan Hedberg662d6522013-10-16 11:20:47 +03004019 rej.scid = __cpu_to_le16(scid);
4020 rej.dcid = __cpu_to_le16(dcid);
4021
4022 l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
4023}
4024
Gustavo Padovan2d792812012-10-06 10:07:01 +01004025static inline int l2cap_config_req(struct l2cap_conn *conn,
4026 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4027 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028{
4029 struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
4030 u16 dcid, flags;
4031 u8 rsp[64];
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004032 struct l2cap_chan *chan;
Mat Martineau3c588192012-04-11 10:48:42 -07004033 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004035 if (cmd_len < sizeof(*req))
4036 return -EPROTO;
4037
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 dcid = __le16_to_cpu(req->dcid);
4039 flags = __le16_to_cpu(req->flags);
4040
4041 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
4042
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03004043 chan = l2cap_get_chan_by_scid(conn, dcid);
Johan Hedberg662d6522013-10-16 11:20:47 +03004044 if (!chan) {
4045 cmd_reject_invalid_cid(conn, cmd->ident, dcid, 0);
4046 return 0;
4047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
David S. Miller033b1142011-07-21 13:38:42 -07004049 if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) {
Johan Hedberg662d6522013-10-16 11:20:47 +03004050 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4051 chan->dcid);
Marcel Holtmann354f60a2006-11-18 22:15:20 +01004052 goto unlock;
Gustavo F. Padovandf6bd742010-06-14 02:26:15 -03004053 }
Marcel Holtmann354f60a2006-11-18 22:15:20 +01004054
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004055 /* Reject if config buffer is too small. */
Al Viro88219a02007-07-29 00:17:25 -07004056 len = cmd_len - sizeof(*req);
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004057 if (chan->conf_len + len > sizeof(chan->conf_req)) {
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004058 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004059 l2cap_build_conf_rsp(chan, rsp,
4060 L2CAP_CONF_REJECT, flags), rsp);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004061 goto unlock;
4062 }
4063
4064 /* Store config. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004065 memcpy(chan->conf_req + chan->conf_len, req->data, len);
4066 chan->conf_len += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03004068 if (flags & L2CAP_CONF_FLAG_CONTINUATION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 /* Incomplete config. Send empty response. */
4070 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004071 l2cap_build_conf_rsp(chan, rsp,
4072 L2CAP_CONF_SUCCESS, flags), rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 goto unlock;
4074 }
4075
4076 /* Complete config. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004077 len = l2cap_parse_conf_req(chan, rsp);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004078 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004079 l2cap_send_disconn_req(chan, ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 goto unlock;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Mat Martineau1500109b2012-10-23 15:24:15 -07004083 chan->ident = cmd->ident;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004084 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004085 chan->num_conf_rsp++;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004086
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004087 /* Reset config buffer. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004088 chan->conf_len = 0;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004089
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004090 if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state))
Marcel Holtmann876d9482007-10-20 13:35:42 +02004091 goto unlock;
4092
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004093 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004094 set_default_fcs(chan);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004095
Mat Martineau105bdf92012-04-27 16:50:48 -07004096 if (chan->mode == L2CAP_MODE_ERTM ||
4097 chan->mode == L2CAP_MODE_STREAMING)
Mat Martineau3c588192012-04-11 10:48:42 -07004098 err = l2cap_ertm_init(chan);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03004099
Mat Martineau3c588192012-04-11 10:48:42 -07004100 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004101 l2cap_send_disconn_req(chan, -err);
Mat Martineau3c588192012-04-11 10:48:42 -07004102 else
4103 l2cap_chan_ready(chan);
4104
Marcel Holtmann876d9482007-10-20 13:35:42 +02004105 goto unlock;
4106 }
4107
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004108 if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +02004109 u8 buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004111 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004112 chan->num_conf_req++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 }
4114
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004115 /* Got Conf Rsp PENDING from remote side and asume we sent
4116 Conf Rsp PENDING in the code above */
4117 if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) &&
Andrei Emeltchenko29d8a592012-09-21 12:30:05 +03004118 test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004119
4120 /* check compatibility */
4121
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004122 /* Send rsp for BR/EDR channel */
Andrei Emeltchenkof351bc72012-10-31 15:46:35 +02004123 if (!chan->hs_hcon)
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004124 l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags);
4125 else
4126 chan->ident = cmd->ident;
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004127 }
4128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129unlock:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004130 l2cap_chan_unlock(chan);
Mat Martineau3c588192012-04-11 10:48:42 -07004131 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132}
4133
Gustavo Padovan2d792812012-10-06 10:07:01 +01004134static inline int l2cap_config_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004135 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4136 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
4138 struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
4139 u16 scid, flags, result;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004140 struct l2cap_chan *chan;
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004141 int len = cmd_len - sizeof(*rsp);
Mat Martineau3c588192012-04-11 10:48:42 -07004142 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004144 if (cmd_len < sizeof(*rsp))
4145 return -EPROTO;
4146
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 scid = __le16_to_cpu(rsp->scid);
4148 flags = __le16_to_cpu(rsp->flags);
4149 result = __le16_to_cpu(rsp->result);
4150
Andrei Emeltchenko61386cb2012-03-12 12:13:07 +02004151 BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags,
4152 result, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03004154 chan = l2cap_get_chan_by_scid(conn, scid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004155 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 return 0;
4157
4158 switch (result) {
4159 case L2CAP_CONF_SUCCESS:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004160 l2cap_conf_rfc_get(chan, rsp->data, len);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004161 clear_bit(CONF_REM_CONF_PEND, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 break;
4163
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004164 case L2CAP_CONF_PENDING:
4165 set_bit(CONF_REM_CONF_PEND, &chan->conf_state);
4166
4167 if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
4168 char buf[64];
4169
4170 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004171 buf, &result);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004172 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004173 l2cap_send_disconn_req(chan, ECONNRESET);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004174 goto done;
4175 }
4176
Andrei Emeltchenkof351bc72012-10-31 15:46:35 +02004177 if (!chan->hs_hcon) {
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004178 l2cap_send_efs_conf_rsp(chan, buf, cmd->ident,
4179 0);
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +02004180 } else {
4181 if (l2cap_check_efs(chan)) {
4182 amp_create_logical_link(chan);
4183 chan->ident = cmd->ident;
4184 }
4185 }
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004186 }
4187 goto done;
4188
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 case L2CAP_CONF_UNACCEPT:
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004190 if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004191 char req[64];
4192
Andrei Emeltchenkoc2c77ec2010-03-19 10:26:28 +02004193 if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004194 l2cap_send_disconn_req(chan, ECONNRESET);
Andrei Emeltchenkoc2c77ec2010-03-19 10:26:28 +02004195 goto done;
4196 }
4197
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004198 /* throw out any old stored conf requests */
4199 result = L2CAP_CONF_SUCCESS;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03004200 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004201 req, &result);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004202 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004203 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004204 goto done;
4205 }
4206
4207 l2cap_send_cmd(conn, l2cap_get_ident(conn),
Gustavo Padovan2d792812012-10-06 10:07:01 +01004208 L2CAP_CONF_REQ, len, req);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004209 chan->num_conf_req++;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004210 if (result != L2CAP_CONF_SUCCESS)
4211 goto done;
4212 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 }
4214
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09004215 default:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004216 l2cap_chan_set_err(chan, ECONNRESET);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +02004217
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08004218 __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004219 l2cap_send_disconn_req(chan, ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 goto done;
4221 }
4222
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03004223 if (flags & L2CAP_CONF_FLAG_CONTINUATION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 goto done;
4225
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004226 set_bit(CONF_INPUT_DONE, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004228 if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) {
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004229 set_default_fcs(chan);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004230
Mat Martineau105bdf92012-04-27 16:50:48 -07004231 if (chan->mode == L2CAP_MODE_ERTM ||
4232 chan->mode == L2CAP_MODE_STREAMING)
Mat Martineau3c588192012-04-11 10:48:42 -07004233 err = l2cap_ertm_init(chan);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03004234
Mat Martineau3c588192012-04-11 10:48:42 -07004235 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004236 l2cap_send_disconn_req(chan, -err);
Mat Martineau3c588192012-04-11 10:48:42 -07004237 else
4238 l2cap_chan_ready(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 }
4240
4241done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004242 l2cap_chan_unlock(chan);
Mat Martineau3c588192012-04-11 10:48:42 -07004243 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244}
4245
Gustavo Padovan2d792812012-10-06 10:07:01 +01004246static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004247 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4248 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249{
4250 struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
4251 struct l2cap_disconn_rsp rsp;
4252 u16 dcid, scid;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004253 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004255 if (cmd_len != sizeof(*req))
4256 return -EPROTO;
4257
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 scid = __le16_to_cpu(req->scid);
4259 dcid = __le16_to_cpu(req->dcid);
4260
4261 BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
4262
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004263 mutex_lock(&conn->chan_lock);
4264
4265 chan = __l2cap_get_chan_by_scid(conn, dcid);
4266 if (!chan) {
4267 mutex_unlock(&conn->chan_lock);
Johan Hedberg662d6522013-10-16 11:20:47 +03004268 cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid);
4269 return 0;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004272 l2cap_chan_lock(chan);
4273
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03004274 rsp.dcid = cpu_to_le16(chan->scid);
4275 rsp.scid = cpu_to_le16(chan->dcid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
4277
Gustavo Padovan5ec1bbe2013-10-15 19:24:48 -03004278 chan->ops->set_shutdown(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279
Mat Martineau61d6ef32012-04-27 16:50:50 -07004280 l2cap_chan_hold(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004281 l2cap_chan_del(chan, ECONNRESET);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004282
4283 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Gustavo Padovan80b98022012-05-27 22:27:51 -03004285 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07004286 l2cap_chan_put(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004287
4288 mutex_unlock(&conn->chan_lock);
4289
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 return 0;
4291}
4292
Gustavo Padovan2d792812012-10-06 10:07:01 +01004293static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004294 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4295 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296{
4297 struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
4298 u16 dcid, scid;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004299 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004301 if (cmd_len != sizeof(*rsp))
4302 return -EPROTO;
4303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 scid = __le16_to_cpu(rsp->scid);
4305 dcid = __le16_to_cpu(rsp->dcid);
4306
4307 BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
4308
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004309 mutex_lock(&conn->chan_lock);
4310
4311 chan = __l2cap_get_chan_by_scid(conn, scid);
4312 if (!chan) {
4313 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 return 0;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004317 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004318
Mat Martineau61d6ef32012-04-27 16:50:50 -07004319 l2cap_chan_hold(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004320 l2cap_chan_del(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004321
4322 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
Gustavo Padovan80b98022012-05-27 22:27:51 -03004324 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07004325 l2cap_chan_put(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004326
4327 mutex_unlock(&conn->chan_lock);
4328
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 return 0;
4330}
4331
Gustavo Padovan2d792812012-10-06 10:07:01 +01004332static inline int l2cap_information_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004333 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4334 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335{
4336 struct l2cap_info_req *req = (struct l2cap_info_req *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 u16 type;
4338
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004339 if (cmd_len != sizeof(*req))
4340 return -EPROTO;
4341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 type = __le16_to_cpu(req->type);
4343
4344 BT_DBG("type 0x%4.4x", type);
4345
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004346 if (type == L2CAP_IT_FEAT_MASK) {
4347 u8 buf[8];
Marcel Holtmann44dd46d2009-05-02 19:09:01 -07004348 u32 feat_mask = l2cap_feat_mask;
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004349 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004350 rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
4351 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03004352 if (!disable_ertm)
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004353 feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
Gustavo Padovan2d792812012-10-06 10:07:01 +01004354 | L2CAP_FEAT_FCS;
Marcel Holtmann848566b2013-10-01 22:59:22 -07004355 if (conn->hs_enabled)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03004356 feat_mask |= L2CAP_FEAT_EXT_FLOW
Gustavo Padovan2d792812012-10-06 10:07:01 +01004357 | L2CAP_FEAT_EXT_WINDOW;
Andrei Emeltchenkoa5fd6f32011-09-16 16:26:32 +03004358
Gustavo F. Padovan1b7bf4e2009-08-24 00:45:20 -03004359 put_unaligned_le32(feat_mask, rsp->data);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004360 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4361 buf);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004362 } else if (type == L2CAP_IT_FIXED_CHAN) {
4363 u8 buf[12];
4364 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
Mat Martineau50a147c2011-11-02 16:18:34 -07004365
Marcel Holtmann848566b2013-10-01 22:59:22 -07004366 if (conn->hs_enabled)
Mat Martineau50a147c2011-11-02 16:18:34 -07004367 l2cap_fixed_chan[0] |= L2CAP_FC_A2MP;
4368 else
4369 l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP;
4370
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004371 rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
4372 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
Andrei Emeltchenkoc6337ea2011-10-20 17:02:44 +03004373 memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan));
Gustavo Padovan2d792812012-10-06 10:07:01 +01004374 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4375 buf);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004376 } else {
4377 struct l2cap_info_rsp rsp;
4378 rsp.type = cpu_to_le16(type);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004379 rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004380 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp),
4381 &rsp);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
4384 return 0;
4385}
4386
Gustavo Padovan2d792812012-10-06 10:07:01 +01004387static inline int l2cap_information_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004388 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4389 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390{
4391 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
4392 u16 type, result;
4393
Jaganath Kanakkassery3f6fa3d2013-06-21 19:55:11 +05304394 if (cmd_len < sizeof(*rsp))
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004395 return -EPROTO;
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 type = __le16_to_cpu(rsp->type);
4398 result = __le16_to_cpu(rsp->result);
4399
4400 BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
4401
Andrei Emeltchenkoe90165b2011-03-25 11:31:41 +02004402 /* L2CAP Info req/rsp are unbound to channels, add extra checks */
4403 if (cmd->ident != conn->info_ident ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01004404 conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
Andrei Emeltchenkoe90165b2011-03-25 11:31:41 +02004405 return 0;
4406
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02004407 cancel_delayed_work(&conn->info_timer);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004408
Ville Tervoadb08ed2010-08-04 09:43:33 +03004409 if (result != L2CAP_IR_SUCCESS) {
4410 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4411 conn->info_ident = 0;
4412
4413 l2cap_conn_start(conn);
4414
4415 return 0;
4416 }
4417
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004418 switch (type) {
4419 case L2CAP_IT_FEAT_MASK:
Harvey Harrison83985312008-05-02 16:25:46 -07004420 conn->feat_mask = get_unaligned_le32(rsp->data);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004421
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -07004422 if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004423 struct l2cap_info_req req;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004424 req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004425
4426 conn->info_ident = l2cap_get_ident(conn);
4427
4428 l2cap_send_cmd(conn, conn->info_ident,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004429 L2CAP_INFO_REQ, sizeof(req), &req);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004430 } else {
4431 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4432 conn->info_ident = 0;
4433
4434 l2cap_conn_start(conn);
4435 }
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004436 break;
4437
4438 case L2CAP_IT_FIXED_CHAN:
4439 conn->fixed_chan_mask = rsp->data[0];
Marcel Holtmann984947d2009-02-06 23:35:19 +01004440 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004441 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01004442
4443 l2cap_conn_start(conn);
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004444 break;
Marcel Holtmann984947d2009-02-06 23:35:19 +01004445 }
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 return 0;
4448}
4449
Mat Martineau17009152012-10-23 15:24:07 -07004450static int l2cap_create_channel_req(struct l2cap_conn *conn,
4451 struct l2cap_cmd_hdr *cmd,
4452 u16 cmd_len, void *data)
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004453{
4454 struct l2cap_create_chan_req *req = data;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004455 struct l2cap_create_chan_rsp rsp;
Mat Martineau17009152012-10-23 15:24:07 -07004456 struct l2cap_chan *chan;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004457 struct hci_dev *hdev;
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004458 u16 psm, scid;
4459
4460 if (cmd_len != sizeof(*req))
4461 return -EPROTO;
4462
Marcel Holtmann848566b2013-10-01 22:59:22 -07004463 if (!conn->hs_enabled)
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004464 return -EINVAL;
4465
4466 psm = le16_to_cpu(req->psm);
4467 scid = le16_to_cpu(req->scid);
4468
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004469 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 -07004470
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004471 /* For controller id 0 make BR/EDR connection */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004472 if (req->amp_id == AMP_ID_BREDR) {
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004473 l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4474 req->amp_id);
4475 return 0;
4476 }
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004477
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004478 /* Validate AMP controller id */
4479 hdev = hci_dev_get(req->amp_id);
4480 if (!hdev)
4481 goto error;
Mat Martineau17009152012-10-23 15:24:07 -07004482
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004483 if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) {
Mat Martineau17009152012-10-23 15:24:07 -07004484 hci_dev_put(hdev);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004485 goto error;
Mat Martineau17009152012-10-23 15:24:07 -07004486 }
4487
4488 chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4489 req->amp_id);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004490 if (chan) {
4491 struct amp_mgr *mgr = conn->hcon->amp_mgr;
4492 struct hci_conn *hs_hcon;
4493
Marcel Holtmann98e0f7e2013-10-13 02:23:40 -07004494 hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
4495 &conn->hcon->dst);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004496 if (!hs_hcon) {
4497 hci_dev_put(hdev);
Johan Hedberg662d6522013-10-16 11:20:47 +03004498 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4499 chan->dcid);
4500 return 0;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004501 }
4502
4503 BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon);
4504
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004505 mgr->bredr_chan = chan;
4506 chan->hs_hcon = hs_hcon;
Andrei Emeltchenkofd45bf42012-11-20 17:16:22 +02004507 chan->fcs = L2CAP_FCS_NONE;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004508 conn->mtu = hdev->block_mtu;
4509 }
4510
4511 hci_dev_put(hdev);
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004512
4513 return 0;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004514
4515error:
4516 rsp.dcid = 0;
4517 rsp.scid = cpu_to_le16(scid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004518 rsp.result = cpu_to_le16(L2CAP_CR_BAD_AMP);
4519 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004520
4521 l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
4522 sizeof(rsp), &rsp);
4523
Johan Hedbergdc280802013-09-16 13:05:13 +03004524 return 0;
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004525}
4526
Mat Martineau8eb200b2012-10-23 15:24:17 -07004527static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id)
4528{
4529 struct l2cap_move_chan_req req;
4530 u8 ident;
4531
4532 BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id);
4533
4534 ident = l2cap_get_ident(chan->conn);
4535 chan->ident = ident;
4536
4537 req.icid = cpu_to_le16(chan->scid);
4538 req.dest_amp_id = dest_amp_id;
4539
4540 l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req),
4541 &req);
4542
4543 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4544}
4545
Mat Martineau1500109b2012-10-23 15:24:15 -07004546static void l2cap_send_move_chan_rsp(struct l2cap_chan *chan, u16 result)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004547{
4548 struct l2cap_move_chan_rsp rsp;
4549
Mat Martineau1500109b2012-10-23 15:24:15 -07004550 BT_DBG("chan %p, result 0x%4.4x", chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004551
Mat Martineau1500109b2012-10-23 15:24:15 -07004552 rsp.icid = cpu_to_le16(chan->dcid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004553 rsp.result = cpu_to_le16(result);
4554
Mat Martineau1500109b2012-10-23 15:24:15 -07004555 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP,
4556 sizeof(rsp), &rsp);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004557}
4558
Mat Martineau5b155ef2012-10-23 15:24:14 -07004559static void l2cap_send_move_chan_cfm(struct l2cap_chan *chan, u16 result)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004560{
4561 struct l2cap_move_chan_cfm cfm;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004562
Mat Martineau5b155ef2012-10-23 15:24:14 -07004563 BT_DBG("chan %p, result 0x%4.4x", chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004564
Mat Martineau5b155ef2012-10-23 15:24:14 -07004565 chan->ident = l2cap_get_ident(chan->conn);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004566
Mat Martineau5b155ef2012-10-23 15:24:14 -07004567 cfm.icid = cpu_to_le16(chan->scid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004568 cfm.result = cpu_to_le16(result);
4569
Mat Martineau5b155ef2012-10-23 15:24:14 -07004570 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM,
4571 sizeof(cfm), &cfm);
4572
4573 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4574}
4575
4576static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid)
4577{
4578 struct l2cap_move_chan_cfm cfm;
4579
4580 BT_DBG("conn %p, icid 0x%4.4x", conn, icid);
4581
4582 cfm.icid = cpu_to_le16(icid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004583 cfm.result = cpu_to_le16(L2CAP_MC_UNCONFIRMED);
Mat Martineau5b155ef2012-10-23 15:24:14 -07004584
4585 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM,
4586 sizeof(cfm), &cfm);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004587}
4588
4589static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004590 u16 icid)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004591{
4592 struct l2cap_move_chan_cfm_rsp rsp;
4593
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004594 BT_DBG("icid 0x%4.4x", icid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004595
4596 rsp.icid = cpu_to_le16(icid);
4597 l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp);
4598}
4599
Mat Martineau5f3847a2012-10-23 15:24:12 -07004600static void __release_logical_link(struct l2cap_chan *chan)
4601{
4602 chan->hs_hchan = NULL;
4603 chan->hs_hcon = NULL;
4604
4605 /* Placeholder - release the logical link */
4606}
4607
Mat Martineau1500109b2012-10-23 15:24:15 -07004608static void l2cap_logical_fail(struct l2cap_chan *chan)
4609{
4610 /* Logical link setup failed */
4611 if (chan->state != BT_CONNECTED) {
4612 /* Create channel failure, disconnect */
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004613 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineau1500109b2012-10-23 15:24:15 -07004614 return;
4615 }
4616
4617 switch (chan->move_role) {
4618 case L2CAP_MOVE_ROLE_RESPONDER:
4619 l2cap_move_done(chan);
4620 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP);
4621 break;
4622 case L2CAP_MOVE_ROLE_INITIATOR:
4623 if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP ||
4624 chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) {
4625 /* Remote has only sent pending or
4626 * success responses, clean up
4627 */
4628 l2cap_move_done(chan);
4629 }
4630
4631 /* Other amp move states imply that the move
4632 * has already aborted
4633 */
4634 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
4635 break;
4636 }
4637}
4638
4639static void l2cap_logical_finish_create(struct l2cap_chan *chan,
4640 struct hci_chan *hchan)
4641{
4642 struct l2cap_conf_rsp rsp;
Mat Martineau1500109b2012-10-23 15:24:15 -07004643
Andrei Emeltchenko336178a2012-10-31 15:46:27 +02004644 chan->hs_hchan = hchan;
Mat Martineau1500109b2012-10-23 15:24:15 -07004645 chan->hs_hcon->l2cap_data = chan->conn;
4646
Andrei Emeltchenko35ba9562012-10-25 15:20:43 +03004647 l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0);
Mat Martineau1500109b2012-10-23 15:24:15 -07004648
4649 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
Andrei Emeltchenkofe79c6f2012-10-25 15:20:42 +03004650 int err;
Mat Martineau1500109b2012-10-23 15:24:15 -07004651
4652 set_default_fcs(chan);
4653
4654 err = l2cap_ertm_init(chan);
4655 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004656 l2cap_send_disconn_req(chan, -err);
Mat Martineau1500109b2012-10-23 15:24:15 -07004657 else
4658 l2cap_chan_ready(chan);
4659 }
4660}
4661
4662static void l2cap_logical_finish_move(struct l2cap_chan *chan,
4663 struct hci_chan *hchan)
4664{
4665 chan->hs_hcon = hchan->conn;
4666 chan->hs_hcon->l2cap_data = chan->conn;
4667
4668 BT_DBG("move_state %d", chan->move_state);
4669
4670 switch (chan->move_state) {
4671 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
4672 /* Move confirm will be sent after a success
4673 * response is received
4674 */
4675 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4676 break;
4677 case L2CAP_MOVE_WAIT_LOGICAL_CFM:
4678 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4679 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4680 } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
4681 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
4682 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
4683 } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
4684 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4685 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
4686 }
4687 break;
4688 default:
4689 /* Move was not in expected state, free the channel */
4690 __release_logical_link(chan);
4691
4692 chan->move_state = L2CAP_MOVE_STABLE;
4693 }
4694}
4695
4696/* Call with chan locked */
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03004697void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan,
4698 u8 status)
Mat Martineau5b155ef2012-10-23 15:24:14 -07004699{
Mat Martineau1500109b2012-10-23 15:24:15 -07004700 BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status);
4701
4702 if (status) {
4703 l2cap_logical_fail(chan);
4704 __release_logical_link(chan);
4705 return;
4706 }
4707
4708 if (chan->state != BT_CONNECTED) {
4709 /* Ignore logical link if channel is on BR/EDR */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004710 if (chan->local_amp_id != AMP_ID_BREDR)
Mat Martineau1500109b2012-10-23 15:24:15 -07004711 l2cap_logical_finish_create(chan, hchan);
4712 } else {
4713 l2cap_logical_finish_move(chan, hchan);
4714 }
Mat Martineau5b155ef2012-10-23 15:24:14 -07004715}
4716
Mat Martineau3f7a56c2012-10-23 15:24:23 -07004717void l2cap_move_start(struct l2cap_chan *chan)
4718{
4719 BT_DBG("chan %p", chan);
4720
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004721 if (chan->local_amp_id == AMP_ID_BREDR) {
Mat Martineau3f7a56c2012-10-23 15:24:23 -07004722 if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED)
4723 return;
4724 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
4725 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
4726 /* Placeholder - start physical link setup */
4727 } else {
4728 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
4729 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4730 chan->move_id = 0;
4731 l2cap_move_setup(chan);
4732 l2cap_send_move_chan_req(chan, 0);
4733 }
4734}
4735
Mat Martineau8eb200b2012-10-23 15:24:17 -07004736static void l2cap_do_create(struct l2cap_chan *chan, int result,
4737 u8 local_amp_id, u8 remote_amp_id)
4738{
Andrei Emeltchenko62748ca2012-11-20 17:16:19 +02004739 BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state),
4740 local_amp_id, remote_amp_id);
4741
Andrei Emeltchenko12d6cc62012-11-14 17:39:31 +02004742 chan->fcs = L2CAP_FCS_NONE;
4743
Andrei Emeltchenko62748ca2012-11-20 17:16:19 +02004744 /* Outgoing channel on AMP */
4745 if (chan->state == BT_CONNECT) {
4746 if (result == L2CAP_CR_SUCCESS) {
4747 chan->local_amp_id = local_amp_id;
4748 l2cap_send_create_chan_req(chan, remote_amp_id);
4749 } else {
4750 /* Revert to BR/EDR connect */
4751 l2cap_send_conn_req(chan);
4752 }
4753
4754 return;
4755 }
4756
4757 /* Incoming channel on AMP */
4758 if (__l2cap_no_conn_pending(chan)) {
Mat Martineau8eb200b2012-10-23 15:24:17 -07004759 struct l2cap_conn_rsp rsp;
4760 char buf[128];
4761 rsp.scid = cpu_to_le16(chan->dcid);
4762 rsp.dcid = cpu_to_le16(chan->scid);
4763
Mat Martineau8eb200b2012-10-23 15:24:17 -07004764 if (result == L2CAP_CR_SUCCESS) {
4765 /* Send successful response */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004766 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
4767 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004768 } else {
4769 /* Send negative response */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004770 rsp.result = cpu_to_le16(L2CAP_CR_NO_MEM);
4771 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004772 }
4773
4774 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP,
4775 sizeof(rsp), &rsp);
4776
4777 if (result == L2CAP_CR_SUCCESS) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02004778 l2cap_state_change(chan, BT_CONFIG);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004779 set_bit(CONF_REQ_SENT, &chan->conf_state);
4780 l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn),
4781 L2CAP_CONF_REQ,
4782 l2cap_build_conf_req(chan, buf), buf);
4783 chan->num_conf_req++;
4784 }
Mat Martineau8eb200b2012-10-23 15:24:17 -07004785 }
4786}
4787
4788static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id,
4789 u8 remote_amp_id)
4790{
4791 l2cap_move_setup(chan);
4792 chan->move_id = local_amp_id;
4793 chan->move_state = L2CAP_MOVE_WAIT_RSP;
4794
4795 l2cap_send_move_chan_req(chan, remote_amp_id);
4796}
4797
4798static void l2cap_do_move_respond(struct l2cap_chan *chan, int result)
4799{
4800 struct hci_chan *hchan = NULL;
4801
4802 /* Placeholder - get hci_chan for logical link */
4803
4804 if (hchan) {
4805 if (hchan->state == BT_CONNECTED) {
4806 /* Logical link is ready to go */
4807 chan->hs_hcon = hchan->conn;
4808 chan->hs_hcon->l2cap_data = chan->conn;
4809 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4810 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
4811
4812 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
4813 } else {
4814 /* Wait for logical link to be ready */
4815 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
4816 }
4817 } else {
4818 /* Logical link not available */
4819 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED);
4820 }
4821}
4822
4823static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result)
4824{
4825 if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
4826 u8 rsp_result;
4827 if (result == -EINVAL)
4828 rsp_result = L2CAP_MR_BAD_ID;
4829 else
4830 rsp_result = L2CAP_MR_NOT_ALLOWED;
4831
4832 l2cap_send_move_chan_rsp(chan, rsp_result);
4833 }
4834
4835 chan->move_role = L2CAP_MOVE_ROLE_NONE;
4836 chan->move_state = L2CAP_MOVE_STABLE;
4837
4838 /* Restart data transmission */
4839 l2cap_ertm_send(chan);
4840}
4841
Andrei Emeltchenkoa514b172012-11-14 17:39:30 +02004842/* Invoke with locked chan */
4843void __l2cap_physical_cfm(struct l2cap_chan *chan, int result)
Mat Martineau8eb200b2012-10-23 15:24:17 -07004844{
Andrei Emeltchenko770bfef2012-10-31 15:46:29 +02004845 u8 local_amp_id = chan->local_amp_id;
Andrei Emeltchenkofffadc02012-11-01 15:37:03 +02004846 u8 remote_amp_id = chan->remote_amp_id;
Andrei Emeltchenko770bfef2012-10-31 15:46:29 +02004847
Mat Martineau8eb200b2012-10-23 15:24:17 -07004848 BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d",
4849 chan, result, local_amp_id, remote_amp_id);
4850
Mat Martineau8eb200b2012-10-23 15:24:17 -07004851 if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) {
4852 l2cap_chan_unlock(chan);
4853 return;
4854 }
4855
4856 if (chan->state != BT_CONNECTED) {
4857 l2cap_do_create(chan, result, local_amp_id, remote_amp_id);
4858 } else if (result != L2CAP_MR_SUCCESS) {
4859 l2cap_do_move_cancel(chan, result);
4860 } else {
4861 switch (chan->move_role) {
4862 case L2CAP_MOVE_ROLE_INITIATOR:
4863 l2cap_do_move_initiate(chan, local_amp_id,
4864 remote_amp_id);
4865 break;
4866 case L2CAP_MOVE_ROLE_RESPONDER:
4867 l2cap_do_move_respond(chan, result);
4868 break;
4869 default:
4870 l2cap_do_move_cancel(chan, result);
4871 break;
4872 }
4873 }
Mat Martineau8eb200b2012-10-23 15:24:17 -07004874}
4875
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004876static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004877 struct l2cap_cmd_hdr *cmd,
4878 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004879{
4880 struct l2cap_move_chan_req *req = data;
Mat Martineau1500109b2012-10-23 15:24:15 -07004881 struct l2cap_move_chan_rsp rsp;
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004882 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004883 u16 icid = 0;
4884 u16 result = L2CAP_MR_NOT_ALLOWED;
4885
4886 if (cmd_len != sizeof(*req))
4887 return -EPROTO;
4888
4889 icid = le16_to_cpu(req->icid);
4890
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004891 BT_DBG("icid 0x%4.4x, dest_amp_id %d", icid, req->dest_amp_id);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004892
Marcel Holtmann848566b2013-10-01 22:59:22 -07004893 if (!conn->hs_enabled)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004894 return -EINVAL;
4895
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004896 chan = l2cap_get_chan_by_dcid(conn, icid);
4897 if (!chan) {
Mat Martineau1500109b2012-10-23 15:24:15 -07004898 rsp.icid = cpu_to_le16(icid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004899 rsp.result = cpu_to_le16(L2CAP_MR_NOT_ALLOWED);
Mat Martineau1500109b2012-10-23 15:24:15 -07004900 l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP,
4901 sizeof(rsp), &rsp);
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004902 return 0;
4903 }
4904
Mat Martineau1500109b2012-10-23 15:24:15 -07004905 chan->ident = cmd->ident;
4906
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004907 if (chan->scid < L2CAP_CID_DYN_START ||
4908 chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY ||
4909 (chan->mode != L2CAP_MODE_ERTM &&
4910 chan->mode != L2CAP_MODE_STREAMING)) {
4911 result = L2CAP_MR_NOT_ALLOWED;
4912 goto send_move_response;
4913 }
4914
4915 if (chan->local_amp_id == req->dest_amp_id) {
4916 result = L2CAP_MR_SAME_ID;
4917 goto send_move_response;
4918 }
4919
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004920 if (req->dest_amp_id != AMP_ID_BREDR) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004921 struct hci_dev *hdev;
4922 hdev = hci_dev_get(req->dest_amp_id);
4923 if (!hdev || hdev->dev_type != HCI_AMP ||
4924 !test_bit(HCI_UP, &hdev->flags)) {
4925 if (hdev)
4926 hci_dev_put(hdev);
4927
4928 result = L2CAP_MR_BAD_ID;
4929 goto send_move_response;
4930 }
4931 hci_dev_put(hdev);
4932 }
4933
4934 /* Detect a move collision. Only send a collision response
4935 * if this side has "lost", otherwise proceed with the move.
4936 * The winner has the larger bd_addr.
4937 */
4938 if ((__chan_is_moving(chan) ||
4939 chan->move_role != L2CAP_MOVE_ROLE_NONE) &&
Marcel Holtmann6f59b902013-10-13 05:24:01 -07004940 bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004941 result = L2CAP_MR_COLLISION;
4942 goto send_move_response;
4943 }
4944
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004945 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
4946 l2cap_move_setup(chan);
4947 chan->move_id = req->dest_amp_id;
4948 icid = chan->dcid;
4949
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004950 if (req->dest_amp_id == AMP_ID_BREDR) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004951 /* Moving to BR/EDR */
4952 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4953 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4954 result = L2CAP_MR_PEND;
4955 } else {
4956 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4957 result = L2CAP_MR_SUCCESS;
4958 }
4959 } else {
4960 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
4961 /* Placeholder - uncomment when amp functions are available */
4962 /*amp_accept_physical(chan, req->dest_amp_id);*/
4963 result = L2CAP_MR_PEND;
4964 }
4965
4966send_move_response:
Mat Martineau1500109b2012-10-23 15:24:15 -07004967 l2cap_send_move_chan_rsp(chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004968
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004969 l2cap_chan_unlock(chan);
4970
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004971 return 0;
4972}
4973
Mat Martineau5b155ef2012-10-23 15:24:14 -07004974static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result)
4975{
4976 struct l2cap_chan *chan;
4977 struct hci_chan *hchan = NULL;
4978
4979 chan = l2cap_get_chan_by_scid(conn, icid);
4980 if (!chan) {
4981 l2cap_send_move_chan_cfm_icid(conn, icid);
4982 return;
4983 }
4984
4985 __clear_chan_timer(chan);
4986 if (result == L2CAP_MR_PEND)
4987 __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT);
4988
4989 switch (chan->move_state) {
4990 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
4991 /* Move confirm will be sent when logical link
4992 * is complete.
4993 */
4994 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
4995 break;
4996 case L2CAP_MOVE_WAIT_RSP_SUCCESS:
4997 if (result == L2CAP_MR_PEND) {
4998 break;
4999 } else if (test_bit(CONN_LOCAL_BUSY,
5000 &chan->conn_state)) {
5001 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
5002 } else {
5003 /* Logical link is up or moving to BR/EDR,
5004 * proceed with move
5005 */
5006 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
5007 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
5008 }
5009 break;
5010 case L2CAP_MOVE_WAIT_RSP:
5011 /* Moving to AMP */
5012 if (result == L2CAP_MR_SUCCESS) {
5013 /* Remote is ready, send confirm immediately
5014 * after logical link is ready
5015 */
5016 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
5017 } else {
5018 /* Both logical link and move success
5019 * are required to confirm
5020 */
5021 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP;
5022 }
5023
5024 /* Placeholder - get hci_chan for logical link */
5025 if (!hchan) {
5026 /* Logical link not available */
5027 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5028 break;
5029 }
5030
5031 /* If the logical link is not yet connected, do not
5032 * send confirmation.
5033 */
5034 if (hchan->state != BT_CONNECTED)
5035 break;
5036
5037 /* Logical link is already ready to go */
5038
5039 chan->hs_hcon = hchan->conn;
5040 chan->hs_hcon->l2cap_data = chan->conn;
5041
5042 if (result == L2CAP_MR_SUCCESS) {
5043 /* Can confirm now */
5044 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
5045 } else {
5046 /* Now only need move success
5047 * to confirm
5048 */
5049 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
5050 }
5051
5052 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
5053 break;
5054 default:
5055 /* Any other amp move state means the move failed. */
5056 chan->move_id = chan->local_amp_id;
5057 l2cap_move_done(chan);
5058 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5059 }
5060
5061 l2cap_chan_unlock(chan);
5062}
5063
5064static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid,
5065 u16 result)
5066{
5067 struct l2cap_chan *chan;
5068
5069 chan = l2cap_get_chan_by_ident(conn, ident);
5070 if (!chan) {
5071 /* Could not locate channel, icid is best guess */
5072 l2cap_send_move_chan_cfm_icid(conn, icid);
5073 return;
5074 }
5075
5076 __clear_chan_timer(chan);
5077
5078 if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
5079 if (result == L2CAP_MR_COLLISION) {
5080 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
5081 } else {
5082 /* Cleanup - cancel move */
5083 chan->move_id = chan->local_amp_id;
5084 l2cap_move_done(chan);
5085 }
5086 }
5087
5088 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5089
5090 l2cap_chan_unlock(chan);
5091}
5092
5093static int l2cap_move_channel_rsp(struct l2cap_conn *conn,
5094 struct l2cap_cmd_hdr *cmd,
5095 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005096{
5097 struct l2cap_move_chan_rsp *rsp = data;
5098 u16 icid, result;
5099
5100 if (cmd_len != sizeof(*rsp))
5101 return -EPROTO;
5102
5103 icid = le16_to_cpu(rsp->icid);
5104 result = le16_to_cpu(rsp->result);
5105
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005106 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005107
Mat Martineau5b155ef2012-10-23 15:24:14 -07005108 if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND)
5109 l2cap_move_continue(conn, icid, result);
5110 else
5111 l2cap_move_fail(conn, cmd->ident, icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005112
5113 return 0;
5114}
5115
Mat Martineau5f3847a2012-10-23 15:24:12 -07005116static int l2cap_move_channel_confirm(struct l2cap_conn *conn,
5117 struct l2cap_cmd_hdr *cmd,
5118 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005119{
5120 struct l2cap_move_chan_cfm *cfm = data;
Mat Martineau5f3847a2012-10-23 15:24:12 -07005121 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005122 u16 icid, result;
5123
5124 if (cmd_len != sizeof(*cfm))
5125 return -EPROTO;
5126
5127 icid = le16_to_cpu(cfm->icid);
5128 result = le16_to_cpu(cfm->result);
5129
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005130 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005131
Mat Martineau5f3847a2012-10-23 15:24:12 -07005132 chan = l2cap_get_chan_by_dcid(conn, icid);
5133 if (!chan) {
5134 /* Spec requires a response even if the icid was not found */
5135 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5136 return 0;
5137 }
5138
5139 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) {
5140 if (result == L2CAP_MC_CONFIRMED) {
5141 chan->local_amp_id = chan->move_id;
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07005142 if (chan->local_amp_id == AMP_ID_BREDR)
Mat Martineau5f3847a2012-10-23 15:24:12 -07005143 __release_logical_link(chan);
5144 } else {
5145 chan->move_id = chan->local_amp_id;
5146 }
5147
5148 l2cap_move_done(chan);
5149 }
5150
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005151 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5152
Mat Martineau5f3847a2012-10-23 15:24:12 -07005153 l2cap_chan_unlock(chan);
5154
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005155 return 0;
5156}
5157
5158static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005159 struct l2cap_cmd_hdr *cmd,
5160 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005161{
5162 struct l2cap_move_chan_cfm_rsp *rsp = data;
Mat Martineau3fd71a02012-10-23 15:24:16 -07005163 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005164 u16 icid;
5165
5166 if (cmd_len != sizeof(*rsp))
5167 return -EPROTO;
5168
5169 icid = le16_to_cpu(rsp->icid);
5170
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005171 BT_DBG("icid 0x%4.4x", icid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005172
Mat Martineau3fd71a02012-10-23 15:24:16 -07005173 chan = l2cap_get_chan_by_scid(conn, icid);
5174 if (!chan)
5175 return 0;
5176
5177 __clear_chan_timer(chan);
5178
5179 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) {
5180 chan->local_amp_id = chan->move_id;
5181
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07005182 if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan)
Mat Martineau3fd71a02012-10-23 15:24:16 -07005183 __release_logical_link(chan);
5184
5185 l2cap_move_done(chan);
5186 }
5187
5188 l2cap_chan_unlock(chan);
5189
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005190 return 0;
5191}
5192
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03005193static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005194 u16 to_multiplier)
Claudio Takahaside731152011-02-11 19:28:55 -02005195{
5196 u16 max_latency;
5197
5198 if (min > max || min < 6 || max > 3200)
5199 return -EINVAL;
5200
5201 if (to_multiplier < 10 || to_multiplier > 3200)
5202 return -EINVAL;
5203
5204 if (max >= to_multiplier * 8)
5205 return -EINVAL;
5206
5207 max_latency = (to_multiplier * 8 / max) - 1;
5208 if (latency > 499 || latency > max_latency)
5209 return -EINVAL;
5210
5211 return 0;
5212}
5213
5214static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005215 struct l2cap_cmd_hdr *cmd,
Johan Hedberg203e6392013-05-15 10:07:15 +03005216 u16 cmd_len, u8 *data)
Claudio Takahaside731152011-02-11 19:28:55 -02005217{
5218 struct hci_conn *hcon = conn->hcon;
5219 struct l2cap_conn_param_update_req *req;
5220 struct l2cap_conn_param_update_rsp rsp;
Johan Hedberg203e6392013-05-15 10:07:15 +03005221 u16 min, max, latency, to_multiplier;
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005222 int err;
Claudio Takahaside731152011-02-11 19:28:55 -02005223
5224 if (!(hcon->link_mode & HCI_LM_MASTER))
5225 return -EINVAL;
5226
Claudio Takahaside731152011-02-11 19:28:55 -02005227 if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
5228 return -EPROTO;
5229
5230 req = (struct l2cap_conn_param_update_req *) data;
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03005231 min = __le16_to_cpu(req->min);
5232 max = __le16_to_cpu(req->max);
Claudio Takahaside731152011-02-11 19:28:55 -02005233 latency = __le16_to_cpu(req->latency);
5234 to_multiplier = __le16_to_cpu(req->to_multiplier);
5235
5236 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 +01005237 min, max, latency, to_multiplier);
Claudio Takahaside731152011-02-11 19:28:55 -02005238
5239 memset(&rsp, 0, sizeof(rsp));
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005240
5241 err = l2cap_check_conn_param(min, max, latency, to_multiplier);
5242 if (err)
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005243 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
Claudio Takahaside731152011-02-11 19:28:55 -02005244 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005245 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
Claudio Takahaside731152011-02-11 19:28:55 -02005246
5247 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005248 sizeof(rsp), &rsp);
Claudio Takahaside731152011-02-11 19:28:55 -02005249
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005250 if (!err)
5251 hci_le_conn_update(hcon, min, max, latency, to_multiplier);
5252
Claudio Takahaside731152011-02-11 19:28:55 -02005253 return 0;
5254}
5255
Johan Hedbergf1496de2013-05-13 14:15:56 +03005256static int l2cap_le_connect_rsp(struct l2cap_conn *conn,
5257 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5258 u8 *data)
5259{
5260 struct l2cap_le_conn_rsp *rsp = (struct l2cap_le_conn_rsp *) data;
5261 u16 dcid, mtu, mps, credits, result;
5262 struct l2cap_chan *chan;
5263 int err;
5264
5265 if (cmd_len < sizeof(*rsp))
5266 return -EPROTO;
5267
5268 dcid = __le16_to_cpu(rsp->dcid);
5269 mtu = __le16_to_cpu(rsp->mtu);
5270 mps = __le16_to_cpu(rsp->mps);
5271 credits = __le16_to_cpu(rsp->credits);
5272 result = __le16_to_cpu(rsp->result);
5273
5274 if (result == L2CAP_CR_SUCCESS && (mtu < 23 || mps < 23))
5275 return -EPROTO;
5276
5277 BT_DBG("dcid 0x%4.4x mtu %u mps %u credits %u result 0x%2.2x",
5278 dcid, mtu, mps, credits, result);
5279
5280 mutex_lock(&conn->chan_lock);
5281
5282 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
5283 if (!chan) {
5284 err = -EBADSLT;
5285 goto unlock;
5286 }
5287
5288 err = 0;
5289
5290 l2cap_chan_lock(chan);
5291
5292 switch (result) {
5293 case L2CAP_CR_SUCCESS:
5294 chan->ident = 0;
5295 chan->dcid = dcid;
5296 chan->omtu = mtu;
5297 chan->remote_mps = mps;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005298 chan->tx_credits = credits;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005299 l2cap_chan_ready(chan);
5300 break;
5301
5302 default:
5303 l2cap_chan_del(chan, ECONNREFUSED);
5304 break;
5305 }
5306
5307 l2cap_chan_unlock(chan);
5308
5309unlock:
5310 mutex_unlock(&conn->chan_lock);
5311
5312 return err;
5313}
5314
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005315static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005316 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5317 u8 *data)
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005318{
5319 int err = 0;
5320
5321 switch (cmd->code) {
5322 case L2CAP_COMMAND_REJ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005323 l2cap_command_rej(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005324 break;
5325
5326 case L2CAP_CONN_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005327 err = l2cap_connect_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005328 break;
5329
5330 case L2CAP_CONN_RSP:
Mat Martineauf5a25982012-10-11 17:48:21 +03005331 case L2CAP_CREATE_CHAN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005332 l2cap_connect_create_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005333 break;
5334
5335 case L2CAP_CONF_REQ:
5336 err = l2cap_config_req(conn, cmd, cmd_len, data);
5337 break;
5338
5339 case L2CAP_CONF_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005340 l2cap_config_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005341 break;
5342
5343 case L2CAP_DISCONN_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005344 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005345 break;
5346
5347 case L2CAP_DISCONN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005348 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005349 break;
5350
5351 case L2CAP_ECHO_REQ:
5352 l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
5353 break;
5354
5355 case L2CAP_ECHO_RSP:
5356 break;
5357
5358 case L2CAP_INFO_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005359 err = l2cap_information_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005360 break;
5361
5362 case L2CAP_INFO_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005363 l2cap_information_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005364 break;
5365
Mat Martineauf94ff6f2011-11-02 16:18:32 -07005366 case L2CAP_CREATE_CHAN_REQ:
5367 err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
5368 break;
5369
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005370 case L2CAP_MOVE_CHAN_REQ:
5371 err = l2cap_move_channel_req(conn, cmd, cmd_len, data);
5372 break;
5373
5374 case L2CAP_MOVE_CHAN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005375 l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005376 break;
5377
5378 case L2CAP_MOVE_CHAN_CFM:
5379 err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data);
5380 break;
5381
5382 case L2CAP_MOVE_CHAN_CFM_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005383 l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005384 break;
5385
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005386 default:
5387 BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
5388 err = -EINVAL;
5389 break;
5390 }
5391
5392 return err;
5393}
5394
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005395static int l2cap_le_connect_req(struct l2cap_conn *conn,
5396 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5397 u8 *data)
5398{
5399 struct l2cap_le_conn_req *req = (struct l2cap_le_conn_req *) data;
5400 struct l2cap_le_conn_rsp rsp;
5401 struct l2cap_chan *chan, *pchan;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005402 u16 dcid, scid, credits, mtu, mps;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005403 __le16 psm;
5404 u8 result;
5405
5406 if (cmd_len != sizeof(*req))
5407 return -EPROTO;
5408
5409 scid = __le16_to_cpu(req->scid);
5410 mtu = __le16_to_cpu(req->mtu);
5411 mps = __le16_to_cpu(req->mps);
5412 psm = req->psm;
5413 dcid = 0;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005414 credits = 0;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005415
5416 if (mtu < 23 || mps < 23)
5417 return -EPROTO;
5418
5419 BT_DBG("psm 0x%2.2x scid 0x%4.4x mtu %u mps %u", __le16_to_cpu(psm),
5420 scid, mtu, mps);
5421
5422 /* Check if we have socket listening on psm */
5423 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
5424 &conn->hcon->dst, LE_LINK);
5425 if (!pchan) {
5426 result = L2CAP_CR_BAD_PSM;
5427 chan = NULL;
5428 goto response;
5429 }
5430
5431 mutex_lock(&conn->chan_lock);
5432 l2cap_chan_lock(pchan);
5433
5434 if (!smp_sufficient_security(conn->hcon, pchan->sec_level)) {
5435 result = L2CAP_CR_AUTHENTICATION;
5436 chan = NULL;
5437 goto response_unlock;
5438 }
5439
5440 /* Check if we already have channel with that dcid */
5441 if (__l2cap_get_chan_by_dcid(conn, scid)) {
5442 result = L2CAP_CR_NO_MEM;
5443 chan = NULL;
5444 goto response_unlock;
5445 }
5446
5447 chan = pchan->ops->new_connection(pchan);
5448 if (!chan) {
5449 result = L2CAP_CR_NO_MEM;
5450 goto response_unlock;
5451 }
5452
Johan Hedberg0ce43ce2013-12-05 14:55:33 +02005453 l2cap_le_flowctl_init(chan);
5454
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005455 bacpy(&chan->src, &conn->hcon->src);
5456 bacpy(&chan->dst, &conn->hcon->dst);
5457 chan->src_type = bdaddr_type(conn->hcon, conn->hcon->src_type);
5458 chan->dst_type = bdaddr_type(conn->hcon, conn->hcon->dst_type);
5459 chan->psm = psm;
5460 chan->dcid = scid;
5461 chan->omtu = mtu;
5462 chan->remote_mps = mps;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005463 chan->tx_credits = __le16_to_cpu(req->credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005464
5465 __l2cap_chan_add(conn, chan);
5466 dcid = chan->scid;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005467 credits = chan->rx_credits;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005468
5469 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
5470
5471 chan->ident = cmd->ident;
5472
5473 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
5474 l2cap_state_change(chan, BT_CONNECT2);
5475 result = L2CAP_CR_PEND;
5476 chan->ops->defer(chan);
5477 } else {
5478 l2cap_chan_ready(chan);
5479 result = L2CAP_CR_SUCCESS;
5480 }
5481
5482response_unlock:
5483 l2cap_chan_unlock(pchan);
5484 mutex_unlock(&conn->chan_lock);
5485
5486 if (result == L2CAP_CR_PEND)
5487 return 0;
5488
5489response:
5490 if (chan) {
5491 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02005492 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005493 } else {
5494 rsp.mtu = 0;
5495 rsp.mps = 0;
5496 }
5497
5498 rsp.dcid = cpu_to_le16(dcid);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005499 rsp.credits = cpu_to_le16(credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005500 rsp.result = cpu_to_le16(result);
5501
5502 l2cap_send_cmd(conn, cmd->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), &rsp);
5503
5504 return 0;
5505}
5506
Johan Hedbergfad5fc82013-12-05 09:45:01 +02005507static inline int l2cap_le_credits(struct l2cap_conn *conn,
5508 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5509 u8 *data)
5510{
5511 struct l2cap_le_credits *pkt;
5512 struct l2cap_chan *chan;
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005513 u16 cid, credits, max_credits;
Johan Hedbergfad5fc82013-12-05 09:45:01 +02005514
5515 if (cmd_len != sizeof(*pkt))
5516 return -EPROTO;
5517
5518 pkt = (struct l2cap_le_credits *) data;
5519 cid = __le16_to_cpu(pkt->cid);
5520 credits = __le16_to_cpu(pkt->credits);
5521
5522 BT_DBG("cid 0x%4.4x credits 0x%4.4x", cid, credits);
5523
5524 chan = l2cap_get_chan_by_dcid(conn, cid);
5525 if (!chan)
5526 return -EBADSLT;
5527
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005528 max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits;
5529 if (credits > max_credits) {
5530 BT_ERR("LE credits overflow");
5531 l2cap_send_disconn_req(chan, ECONNRESET);
5532
5533 /* Return 0 so that we don't trigger an unnecessary
5534 * command reject packet.
5535 */
5536 return 0;
5537 }
5538
Johan Hedbergfad5fc82013-12-05 09:45:01 +02005539 chan->tx_credits += credits;
5540
5541 while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) {
5542 l2cap_do_send(chan, skb_dequeue(&chan->tx_q));
5543 chan->tx_credits--;
5544 }
5545
5546 if (chan->tx_credits)
5547 chan->ops->resume(chan);
5548
5549 l2cap_chan_unlock(chan);
5550
5551 return 0;
5552}
5553
Johan Hedberg71fb4192013-12-10 10:52:48 +02005554static inline int l2cap_le_command_rej(struct l2cap_conn *conn,
5555 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5556 u8 *data)
5557{
5558 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
5559 struct l2cap_chan *chan;
5560
5561 if (cmd_len < sizeof(*rej))
5562 return -EPROTO;
5563
5564 mutex_lock(&conn->chan_lock);
5565
5566 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
5567 if (!chan)
5568 goto done;
5569
5570 l2cap_chan_lock(chan);
5571 l2cap_chan_del(chan, ECONNREFUSED);
5572 l2cap_chan_unlock(chan);
5573
5574done:
5575 mutex_unlock(&conn->chan_lock);
5576 return 0;
5577}
5578
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005579static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
Johan Hedberg203e6392013-05-15 10:07:15 +03005580 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5581 u8 *data)
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005582{
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005583 int err = 0;
5584
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005585 switch (cmd->code) {
5586 case L2CAP_COMMAND_REJ:
Johan Hedberg71fb4192013-12-10 10:52:48 +02005587 l2cap_le_command_rej(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005588 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005589
5590 case L2CAP_CONN_PARAM_UPDATE_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005591 err = l2cap_conn_param_update_req(conn, cmd, cmd_len, data);
5592 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005593
5594 case L2CAP_CONN_PARAM_UPDATE_RSP:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005595 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005596
Johan Hedbergf1496de2013-05-13 14:15:56 +03005597 case L2CAP_LE_CONN_RSP:
5598 l2cap_le_connect_rsp(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005599 break;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005600
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005601 case L2CAP_LE_CONN_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005602 err = l2cap_le_connect_req(conn, cmd, cmd_len, data);
5603 break;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005604
Johan Hedbergfad5fc82013-12-05 09:45:01 +02005605 case L2CAP_LE_CREDITS:
5606 err = l2cap_le_credits(conn, cmd, cmd_len, data);
5607 break;
5608
Johan Hedberg3defe012013-05-15 10:16:06 +03005609 case L2CAP_DISCONN_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005610 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
5611 break;
Johan Hedberg3defe012013-05-15 10:16:06 +03005612
5613 case L2CAP_DISCONN_RSP:
5614 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005615 break;
Johan Hedberg3defe012013-05-15 10:16:06 +03005616
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005617 default:
5618 BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005619 err = -EINVAL;
5620 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005621 }
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005622
5623 return err;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005624}
5625
Johan Hedbergc5623552013-04-29 19:35:33 +03005626static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,
5627 struct sk_buff *skb)
5628{
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005629 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005630 struct l2cap_cmd_hdr *cmd;
5631 u16 len;
Johan Hedbergc5623552013-04-29 19:35:33 +03005632 int err;
5633
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005634 if (hcon->type != LE_LINK)
Marcel Holtmann3b166292013-10-02 08:28:21 -07005635 goto drop;
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005636
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005637 if (skb->len < L2CAP_CMD_HDR_SIZE)
5638 goto drop;
Johan Hedbergc5623552013-04-29 19:35:33 +03005639
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005640 cmd = (void *) skb->data;
5641 skb_pull(skb, L2CAP_CMD_HDR_SIZE);
Johan Hedbergc5623552013-04-29 19:35:33 +03005642
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005643 len = le16_to_cpu(cmd->len);
Johan Hedbergc5623552013-04-29 19:35:33 +03005644
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005645 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd->code, len, cmd->ident);
Johan Hedbergc5623552013-04-29 19:35:33 +03005646
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005647 if (len != skb->len || !cmd->ident) {
5648 BT_DBG("corrupted command");
5649 goto drop;
5650 }
Johan Hedbergc5623552013-04-29 19:35:33 +03005651
Johan Hedberg203e6392013-05-15 10:07:15 +03005652 err = l2cap_le_sig_cmd(conn, cmd, len, skb->data);
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005653 if (err) {
5654 struct l2cap_cmd_rej_unk rej;
Johan Hedbergc5623552013-04-29 19:35:33 +03005655
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005656 BT_ERR("Wrong link type (%d)", err);
Johan Hedbergc5623552013-04-29 19:35:33 +03005657
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005658 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005659 l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
5660 sizeof(rej), &rej);
Johan Hedbergc5623552013-04-29 19:35:33 +03005661 }
5662
Marcel Holtmann3b166292013-10-02 08:28:21 -07005663drop:
Johan Hedbergc5623552013-04-29 19:35:33 +03005664 kfree_skb(skb);
5665}
5666
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005667static inline void l2cap_sig_channel(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005668 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669{
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005670 struct hci_conn *hcon = conn->hcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 u8 *data = skb->data;
5672 int len = skb->len;
5673 struct l2cap_cmd_hdr cmd;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005674 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675
5676 l2cap_raw_recv(conn, skb);
5677
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005678 if (hcon->type != ACL_LINK)
Marcel Holtmann3b166292013-10-02 08:28:21 -07005679 goto drop;
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005680
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 while (len >= L2CAP_CMD_HDR_SIZE) {
Al Viro88219a02007-07-29 00:17:25 -07005682 u16 cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
5684 data += L2CAP_CMD_HDR_SIZE;
5685 len -= L2CAP_CMD_HDR_SIZE;
5686
Al Viro88219a02007-07-29 00:17:25 -07005687 cmd_len = le16_to_cpu(cmd.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
Gustavo Padovan2d792812012-10-06 10:07:01 +01005689 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len,
5690 cmd.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
Al Viro88219a02007-07-29 00:17:25 -07005692 if (cmd_len > len || !cmd.ident) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 BT_DBG("corrupted command");
5694 break;
5695 }
5696
Johan Hedbergc5623552013-04-29 19:35:33 +03005697 err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 if (err) {
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03005699 struct l2cap_cmd_rej_unk rej;
Gustavo F. Padovan2c6d1a22011-03-23 14:38:32 -03005700
5701 BT_ERR("Wrong link type (%d)", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005703 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
Gustavo Padovan2d792812012-10-06 10:07:01 +01005704 l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ,
5705 sizeof(rej), &rej);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 }
5707
Al Viro88219a02007-07-29 00:17:25 -07005708 data += cmd_len;
5709 len -= cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 }
5711
Marcel Holtmann3b166292013-10-02 08:28:21 -07005712drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 kfree_skb(skb);
5714}
5715
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03005716static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005717{
5718 u16 our_fcs, rcv_fcs;
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03005719 int hdr_size;
5720
5721 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
5722 hdr_size = L2CAP_EXT_HDR_SIZE;
5723 else
5724 hdr_size = L2CAP_ENH_HDR_SIZE;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005725
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03005726 if (chan->fcs == L2CAP_FCS_CRC16) {
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03005727 skb_trim(skb, skb->len - L2CAP_FCS_SIZE);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005728 rcv_fcs = get_unaligned_le16(skb->data + skb->len);
5729 our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
5730
5731 if (our_fcs != rcv_fcs)
João Paulo Rechi Vita7a560e52010-06-22 13:56:27 -03005732 return -EBADMSG;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005733 }
5734 return 0;
5735}
5736
Mat Martineau6ea00482012-05-17 20:53:52 -07005737static void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005738{
Mat Martineaue31f7632012-05-17 20:53:41 -07005739 struct l2cap_ctrl control;
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005740
Mat Martineaue31f7632012-05-17 20:53:41 -07005741 BT_DBG("chan %p", chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005742
Mat Martineaue31f7632012-05-17 20:53:41 -07005743 memset(&control, 0, sizeof(control));
5744 control.sframe = 1;
5745 control.final = 1;
5746 control.reqseq = chan->buffer_seq;
5747 set_bit(CONN_SEND_FBIT, &chan->conn_state);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005748
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03005749 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
Mat Martineaue31f7632012-05-17 20:53:41 -07005750 control.super = L2CAP_SUPER_RNR;
5751 l2cap_send_sframe(chan, &control);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005752 }
5753
Mat Martineaue31f7632012-05-17 20:53:41 -07005754 if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
5755 chan->unacked_frames > 0)
5756 __set_retrans_timer(chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005757
Mat Martineaue31f7632012-05-17 20:53:41 -07005758 /* Send pending iframes */
Gustavo F. Padovan525cd182011-03-25 19:43:39 -03005759 l2cap_ertm_send(chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005760
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03005761 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
Mat Martineaue31f7632012-05-17 20:53:41 -07005762 test_bit(CONN_SEND_FBIT, &chan->conn_state)) {
5763 /* F-bit wasn't sent in an s-frame or i-frame yet, so
5764 * send it now.
5765 */
5766 control.super = L2CAP_SUPER_RR;
5767 l2cap_send_sframe(chan, &control);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005768 }
5769}
5770
Gustavo Padovan2d792812012-10-06 10:07:01 +01005771static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag,
5772 struct sk_buff **last_frag)
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005773{
Mat Martineau84084a32011-07-22 14:54:00 -07005774 /* skb->len reflects data in skb as well as all fragments
5775 * skb->data_len reflects only data in fragments
5776 */
5777 if (!skb_has_frag_list(skb))
5778 skb_shinfo(skb)->frag_list = new_frag;
5779
5780 new_frag->next = NULL;
5781
5782 (*last_frag)->next = new_frag;
5783 *last_frag = new_frag;
5784
5785 skb->len += new_frag->len;
5786 skb->data_len += new_frag->len;
5787 skb->truesize += new_frag->truesize;
5788}
5789
Mat Martineau4b51dae92012-05-17 20:53:37 -07005790static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb,
5791 struct l2cap_ctrl *control)
Mat Martineau84084a32011-07-22 14:54:00 -07005792{
5793 int err = -EINVAL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005794
Mat Martineau4b51dae92012-05-17 20:53:37 -07005795 switch (control->sar) {
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005796 case L2CAP_SAR_UNSEGMENTED:
Mat Martineau84084a32011-07-22 14:54:00 -07005797 if (chan->sdu)
5798 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005799
Gustavo Padovan80b98022012-05-27 22:27:51 -03005800 err = chan->ops->recv(chan, skb);
Mat Martineau84084a32011-07-22 14:54:00 -07005801 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005802
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005803 case L2CAP_SAR_START:
Mat Martineau84084a32011-07-22 14:54:00 -07005804 if (chan->sdu)
5805 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005806
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005807 chan->sdu_len = get_unaligned_le16(skb->data);
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03005808 skb_pull(skb, L2CAP_SDULEN_SIZE);
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005809
Mat Martineau84084a32011-07-22 14:54:00 -07005810 if (chan->sdu_len > chan->imtu) {
5811 err = -EMSGSIZE;
5812 break;
5813 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005814
Mat Martineau84084a32011-07-22 14:54:00 -07005815 if (skb->len >= chan->sdu_len)
5816 break;
5817
5818 chan->sdu = skb;
5819 chan->sdu_last_frag = skb;
5820
5821 skb = NULL;
5822 err = 0;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005823 break;
5824
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005825 case L2CAP_SAR_CONTINUE:
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005826 if (!chan->sdu)
Mat Martineau84084a32011-07-22 14:54:00 -07005827 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005828
Mat Martineau84084a32011-07-22 14:54:00 -07005829 append_skb_frag(chan->sdu, skb,
5830 &chan->sdu_last_frag);
5831 skb = NULL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005832
Mat Martineau84084a32011-07-22 14:54:00 -07005833 if (chan->sdu->len >= chan->sdu_len)
5834 break;
Gustavo F. Padovan4178ba42010-05-01 16:15:45 -03005835
Mat Martineau84084a32011-07-22 14:54:00 -07005836 err = 0;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005837 break;
5838
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005839 case L2CAP_SAR_END:
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005840 if (!chan->sdu)
Mat Martineau84084a32011-07-22 14:54:00 -07005841 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005842
Mat Martineau84084a32011-07-22 14:54:00 -07005843 append_skb_frag(chan->sdu, skb,
5844 &chan->sdu_last_frag);
5845 skb = NULL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005846
Mat Martineau84084a32011-07-22 14:54:00 -07005847 if (chan->sdu->len != chan->sdu_len)
5848 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005849
Gustavo Padovan80b98022012-05-27 22:27:51 -03005850 err = chan->ops->recv(chan, chan->sdu);
Gustavo F. Padovan4178ba42010-05-01 16:15:45 -03005851
Mat Martineau84084a32011-07-22 14:54:00 -07005852 if (!err) {
5853 /* Reassembly complete */
5854 chan->sdu = NULL;
5855 chan->sdu_last_frag = NULL;
5856 chan->sdu_len = 0;
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03005857 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005858 break;
5859 }
5860
Mat Martineau84084a32011-07-22 14:54:00 -07005861 if (err) {
5862 kfree_skb(skb);
5863 kfree_skb(chan->sdu);
5864 chan->sdu = NULL;
5865 chan->sdu_last_frag = NULL;
5866 chan->sdu_len = 0;
5867 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005868
Mat Martineau84084a32011-07-22 14:54:00 -07005869 return err;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005870}
5871
Mat Martineau32b32732012-10-23 15:24:11 -07005872static int l2cap_resegment(struct l2cap_chan *chan)
5873{
5874 /* Placeholder */
5875 return 0;
5876}
5877
Mat Martineaue3281402011-07-07 09:39:02 -07005878void l2cap_chan_busy(struct l2cap_chan *chan, int busy)
Gustavo F. Padovan712132eb2010-06-21 19:39:50 -03005879{
Mat Martineau61aa4f52012-05-17 20:53:40 -07005880 u8 event;
5881
5882 if (chan->mode != L2CAP_MODE_ERTM)
5883 return;
5884
5885 event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR;
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03005886 l2cap_tx(chan, NULL, NULL, event);
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03005887}
5888
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005889static int l2cap_rx_queued_iframes(struct l2cap_chan *chan)
5890{
Mat Martineau63838722012-05-17 20:53:45 -07005891 int err = 0;
5892 /* Pass sequential frames to l2cap_reassemble_sdu()
5893 * until a gap is encountered.
5894 */
5895
5896 BT_DBG("chan %p", chan);
5897
5898 while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
5899 struct sk_buff *skb;
5900 BT_DBG("Searching for skb with txseq %d (queue len %d)",
5901 chan->buffer_seq, skb_queue_len(&chan->srej_q));
5902
5903 skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq);
5904
5905 if (!skb)
5906 break;
5907
5908 skb_unlink(skb, &chan->srej_q);
5909 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
5910 err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->control);
5911 if (err)
5912 break;
5913 }
5914
5915 if (skb_queue_empty(&chan->srej_q)) {
5916 chan->rx_state = L2CAP_RX_STATE_RECV;
5917 l2cap_send_ack(chan);
5918 }
5919
5920 return err;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005921}
5922
5923static void l2cap_handle_srej(struct l2cap_chan *chan,
5924 struct l2cap_ctrl *control)
5925{
Mat Martineauf80842a2012-05-17 20:53:46 -07005926 struct sk_buff *skb;
5927
5928 BT_DBG("chan %p, control %p", chan, control);
5929
5930 if (control->reqseq == chan->next_tx_seq) {
5931 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005932 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineauf80842a2012-05-17 20:53:46 -07005933 return;
5934 }
5935
5936 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
5937
5938 if (skb == NULL) {
5939 BT_DBG("Seq %d not available for retransmission",
5940 control->reqseq);
5941 return;
5942 }
5943
5944 if (chan->max_tx != 0 && bt_cb(skb)->control.retries >= chan->max_tx) {
5945 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005946 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineauf80842a2012-05-17 20:53:46 -07005947 return;
5948 }
5949
5950 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
5951
5952 if (control->poll) {
5953 l2cap_pass_to_tx(chan, control);
5954
5955 set_bit(CONN_SEND_FBIT, &chan->conn_state);
5956 l2cap_retransmit(chan, control);
5957 l2cap_ertm_send(chan);
5958
5959 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
5960 set_bit(CONN_SREJ_ACT, &chan->conn_state);
5961 chan->srej_save_reqseq = control->reqseq;
5962 }
5963 } else {
5964 l2cap_pass_to_tx_fbit(chan, control);
5965
5966 if (control->final) {
5967 if (chan->srej_save_reqseq != control->reqseq ||
5968 !test_and_clear_bit(CONN_SREJ_ACT,
5969 &chan->conn_state))
5970 l2cap_retransmit(chan, control);
5971 } else {
5972 l2cap_retransmit(chan, control);
5973 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
5974 set_bit(CONN_SREJ_ACT, &chan->conn_state);
5975 chan->srej_save_reqseq = control->reqseq;
5976 }
5977 }
5978 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005979}
5980
5981static void l2cap_handle_rej(struct l2cap_chan *chan,
5982 struct l2cap_ctrl *control)
5983{
Mat Martineaufcd289d2012-05-17 20:53:47 -07005984 struct sk_buff *skb;
5985
5986 BT_DBG("chan %p, control %p", chan, control);
5987
5988 if (control->reqseq == chan->next_tx_seq) {
5989 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005990 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaufcd289d2012-05-17 20:53:47 -07005991 return;
5992 }
5993
5994 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
5995
5996 if (chan->max_tx && skb &&
5997 bt_cb(skb)->control.retries >= chan->max_tx) {
5998 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005999 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaufcd289d2012-05-17 20:53:47 -07006000 return;
6001 }
6002
6003 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6004
6005 l2cap_pass_to_tx(chan, control);
6006
6007 if (control->final) {
6008 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
6009 l2cap_retransmit_all(chan, control);
6010 } else {
6011 l2cap_retransmit_all(chan, control);
6012 l2cap_ertm_send(chan);
6013 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F)
6014 set_bit(CONN_REJ_ACT, &chan->conn_state);
6015 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006016}
6017
Mat Martineau4b51dae92012-05-17 20:53:37 -07006018static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq)
6019{
6020 BT_DBG("chan %p, txseq %d", chan, txseq);
6021
6022 BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq,
6023 chan->expected_tx_seq);
6024
6025 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
6026 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
Gustavo Padovan2d792812012-10-06 10:07:01 +01006027 chan->tx_win) {
Mat Martineau4b51dae92012-05-17 20:53:37 -07006028 /* See notes below regarding "double poll" and
6029 * invalid packets.
6030 */
6031 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
6032 BT_DBG("Invalid/Ignore - after SREJ");
6033 return L2CAP_TXSEQ_INVALID_IGNORE;
6034 } else {
6035 BT_DBG("Invalid - in window after SREJ sent");
6036 return L2CAP_TXSEQ_INVALID;
6037 }
6038 }
6039
6040 if (chan->srej_list.head == txseq) {
6041 BT_DBG("Expected SREJ");
6042 return L2CAP_TXSEQ_EXPECTED_SREJ;
6043 }
6044
6045 if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) {
6046 BT_DBG("Duplicate SREJ - txseq already stored");
6047 return L2CAP_TXSEQ_DUPLICATE_SREJ;
6048 }
6049
6050 if (l2cap_seq_list_contains(&chan->srej_list, txseq)) {
6051 BT_DBG("Unexpected SREJ - not requested");
6052 return L2CAP_TXSEQ_UNEXPECTED_SREJ;
6053 }
6054 }
6055
6056 if (chan->expected_tx_seq == txseq) {
6057 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
6058 chan->tx_win) {
6059 BT_DBG("Invalid - txseq outside tx window");
6060 return L2CAP_TXSEQ_INVALID;
6061 } else {
6062 BT_DBG("Expected");
6063 return L2CAP_TXSEQ_EXPECTED;
6064 }
6065 }
6066
6067 if (__seq_offset(chan, txseq, chan->last_acked_seq) <
Gustavo Padovan2d792812012-10-06 10:07:01 +01006068 __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) {
Mat Martineau4b51dae92012-05-17 20:53:37 -07006069 BT_DBG("Duplicate - expected_tx_seq later than txseq");
6070 return L2CAP_TXSEQ_DUPLICATE;
6071 }
6072
6073 if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) {
6074 /* A source of invalid packets is a "double poll" condition,
6075 * where delays cause us to send multiple poll packets. If
6076 * the remote stack receives and processes both polls,
6077 * sequence numbers can wrap around in such a way that a
6078 * resent frame has a sequence number that looks like new data
6079 * with a sequence gap. This would trigger an erroneous SREJ
6080 * request.
6081 *
6082 * Fortunately, this is impossible with a tx window that's
6083 * less than half of the maximum sequence number, which allows
6084 * invalid frames to be safely ignored.
6085 *
6086 * With tx window sizes greater than half of the tx window
6087 * maximum, the frame is invalid and cannot be ignored. This
6088 * causes a disconnect.
6089 */
6090
6091 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
6092 BT_DBG("Invalid/Ignore - txseq outside tx window");
6093 return L2CAP_TXSEQ_INVALID_IGNORE;
6094 } else {
6095 BT_DBG("Invalid - txseq outside tx window");
6096 return L2CAP_TXSEQ_INVALID;
6097 }
6098 } else {
6099 BT_DBG("Unexpected - txseq indicates missing frames");
6100 return L2CAP_TXSEQ_UNEXPECTED;
6101 }
6102}
6103
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006104static int l2cap_rx_state_recv(struct l2cap_chan *chan,
6105 struct l2cap_ctrl *control,
6106 struct sk_buff *skb, u8 event)
6107{
6108 int err = 0;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006109 bool skb_in_use = false;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006110
6111 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6112 event);
6113
6114 switch (event) {
6115 case L2CAP_EV_RECV_IFRAME:
6116 switch (l2cap_classify_txseq(chan, control->txseq)) {
6117 case L2CAP_TXSEQ_EXPECTED:
6118 l2cap_pass_to_tx(chan, control);
6119
6120 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6121 BT_DBG("Busy, discarding expected seq %d",
6122 control->txseq);
6123 break;
6124 }
6125
6126 chan->expected_tx_seq = __next_seq(chan,
6127 control->txseq);
6128
6129 chan->buffer_seq = chan->expected_tx_seq;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006130 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006131
6132 err = l2cap_reassemble_sdu(chan, skb, control);
6133 if (err)
6134 break;
6135
6136 if (control->final) {
6137 if (!test_and_clear_bit(CONN_REJ_ACT,
6138 &chan->conn_state)) {
6139 control->final = 0;
6140 l2cap_retransmit_all(chan, control);
6141 l2cap_ertm_send(chan);
6142 }
6143 }
6144
6145 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
6146 l2cap_send_ack(chan);
6147 break;
6148 case L2CAP_TXSEQ_UNEXPECTED:
6149 l2cap_pass_to_tx(chan, control);
6150
6151 /* Can't issue SREJ frames in the local busy state.
6152 * Drop this frame, it will be seen as missing
6153 * when local busy is exited.
6154 */
6155 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6156 BT_DBG("Busy, discarding unexpected seq %d",
6157 control->txseq);
6158 break;
6159 }
6160
6161 /* There was a gap in the sequence, so an SREJ
6162 * must be sent for each missing frame. The
6163 * current frame is stored for later use.
6164 */
6165 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006166 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006167 BT_DBG("Queued %p (queue len %d)", skb,
6168 skb_queue_len(&chan->srej_q));
6169
6170 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
6171 l2cap_seq_list_clear(&chan->srej_list);
6172 l2cap_send_srej(chan, control->txseq);
6173
6174 chan->rx_state = L2CAP_RX_STATE_SREJ_SENT;
6175 break;
6176 case L2CAP_TXSEQ_DUPLICATE:
6177 l2cap_pass_to_tx(chan, control);
6178 break;
6179 case L2CAP_TXSEQ_INVALID_IGNORE:
6180 break;
6181 case L2CAP_TXSEQ_INVALID:
6182 default:
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006183 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006184 break;
6185 }
6186 break;
6187 case L2CAP_EV_RECV_RR:
6188 l2cap_pass_to_tx(chan, control);
6189 if (control->final) {
6190 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6191
Mat Martineaue6a3ee62012-10-23 15:24:22 -07006192 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) &&
6193 !__chan_is_moving(chan)) {
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006194 control->final = 0;
6195 l2cap_retransmit_all(chan, control);
6196 }
6197
6198 l2cap_ertm_send(chan);
6199 } else if (control->poll) {
6200 l2cap_send_i_or_rr_or_rnr(chan);
6201 } else {
6202 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6203 &chan->conn_state) &&
6204 chan->unacked_frames)
6205 __set_retrans_timer(chan);
6206
6207 l2cap_ertm_send(chan);
6208 }
6209 break;
6210 case L2CAP_EV_RECV_RNR:
6211 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6212 l2cap_pass_to_tx(chan, control);
6213 if (control && control->poll) {
6214 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6215 l2cap_send_rr_or_rnr(chan, 0);
6216 }
6217 __clear_retrans_timer(chan);
6218 l2cap_seq_list_clear(&chan->retrans_list);
6219 break;
6220 case L2CAP_EV_RECV_REJ:
6221 l2cap_handle_rej(chan, control);
6222 break;
6223 case L2CAP_EV_RECV_SREJ:
6224 l2cap_handle_srej(chan, control);
6225 break;
6226 default:
6227 break;
6228 }
6229
6230 if (skb && !skb_in_use) {
6231 BT_DBG("Freeing %p", skb);
6232 kfree_skb(skb);
6233 }
6234
6235 return err;
6236}
6237
6238static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
6239 struct l2cap_ctrl *control,
6240 struct sk_buff *skb, u8 event)
6241{
6242 int err = 0;
6243 u16 txseq = control->txseq;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006244 bool skb_in_use = false;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006245
6246 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6247 event);
6248
6249 switch (event) {
6250 case L2CAP_EV_RECV_IFRAME:
6251 switch (l2cap_classify_txseq(chan, txseq)) {
6252 case L2CAP_TXSEQ_EXPECTED:
6253 /* Keep frame for reassembly later */
6254 l2cap_pass_to_tx(chan, control);
6255 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006256 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006257 BT_DBG("Queued %p (queue len %d)", skb,
6258 skb_queue_len(&chan->srej_q));
6259
6260 chan->expected_tx_seq = __next_seq(chan, txseq);
6261 break;
6262 case L2CAP_TXSEQ_EXPECTED_SREJ:
6263 l2cap_seq_list_pop(&chan->srej_list);
6264
6265 l2cap_pass_to_tx(chan, control);
6266 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006267 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006268 BT_DBG("Queued %p (queue len %d)", skb,
6269 skb_queue_len(&chan->srej_q));
6270
6271 err = l2cap_rx_queued_iframes(chan);
6272 if (err)
6273 break;
6274
6275 break;
6276 case L2CAP_TXSEQ_UNEXPECTED:
6277 /* Got a frame that can't be reassembled yet.
6278 * Save it for later, and send SREJs to cover
6279 * the missing frames.
6280 */
6281 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006282 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006283 BT_DBG("Queued %p (queue len %d)", skb,
6284 skb_queue_len(&chan->srej_q));
6285
6286 l2cap_pass_to_tx(chan, control);
6287 l2cap_send_srej(chan, control->txseq);
6288 break;
6289 case L2CAP_TXSEQ_UNEXPECTED_SREJ:
6290 /* This frame was requested with an SREJ, but
6291 * some expected retransmitted frames are
6292 * missing. Request retransmission of missing
6293 * SREJ'd frames.
6294 */
6295 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006296 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006297 BT_DBG("Queued %p (queue len %d)", skb,
6298 skb_queue_len(&chan->srej_q));
6299
6300 l2cap_pass_to_tx(chan, control);
6301 l2cap_send_srej_list(chan, control->txseq);
6302 break;
6303 case L2CAP_TXSEQ_DUPLICATE_SREJ:
6304 /* We've already queued this frame. Drop this copy. */
6305 l2cap_pass_to_tx(chan, control);
6306 break;
6307 case L2CAP_TXSEQ_DUPLICATE:
6308 /* Expecting a later sequence number, so this frame
6309 * was already received. Ignore it completely.
6310 */
6311 break;
6312 case L2CAP_TXSEQ_INVALID_IGNORE:
6313 break;
6314 case L2CAP_TXSEQ_INVALID:
6315 default:
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006316 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006317 break;
6318 }
6319 break;
6320 case L2CAP_EV_RECV_RR:
6321 l2cap_pass_to_tx(chan, control);
6322 if (control->final) {
6323 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6324
6325 if (!test_and_clear_bit(CONN_REJ_ACT,
6326 &chan->conn_state)) {
6327 control->final = 0;
6328 l2cap_retransmit_all(chan, control);
6329 }
6330
6331 l2cap_ertm_send(chan);
6332 } else if (control->poll) {
6333 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6334 &chan->conn_state) &&
6335 chan->unacked_frames) {
6336 __set_retrans_timer(chan);
6337 }
6338
6339 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6340 l2cap_send_srej_tail(chan);
6341 } else {
6342 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6343 &chan->conn_state) &&
6344 chan->unacked_frames)
6345 __set_retrans_timer(chan);
6346
6347 l2cap_send_ack(chan);
6348 }
6349 break;
6350 case L2CAP_EV_RECV_RNR:
6351 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6352 l2cap_pass_to_tx(chan, control);
6353 if (control->poll) {
6354 l2cap_send_srej_tail(chan);
6355 } else {
6356 struct l2cap_ctrl rr_control;
6357 memset(&rr_control, 0, sizeof(rr_control));
6358 rr_control.sframe = 1;
6359 rr_control.super = L2CAP_SUPER_RR;
6360 rr_control.reqseq = chan->buffer_seq;
6361 l2cap_send_sframe(chan, &rr_control);
6362 }
6363
6364 break;
6365 case L2CAP_EV_RECV_REJ:
6366 l2cap_handle_rej(chan, control);
6367 break;
6368 case L2CAP_EV_RECV_SREJ:
6369 l2cap_handle_srej(chan, control);
6370 break;
6371 }
6372
6373 if (skb && !skb_in_use) {
6374 BT_DBG("Freeing %p", skb);
6375 kfree_skb(skb);
6376 }
6377
6378 return err;
6379}
6380
Mat Martineau32b32732012-10-23 15:24:11 -07006381static int l2cap_finish_move(struct l2cap_chan *chan)
6382{
6383 BT_DBG("chan %p", chan);
6384
6385 chan->rx_state = L2CAP_RX_STATE_RECV;
6386
6387 if (chan->hs_hcon)
6388 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
6389 else
6390 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
6391
6392 return l2cap_resegment(chan);
6393}
6394
6395static int l2cap_rx_state_wait_p(struct l2cap_chan *chan,
6396 struct l2cap_ctrl *control,
6397 struct sk_buff *skb, u8 event)
6398{
6399 int err;
6400
6401 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6402 event);
6403
6404 if (!control->poll)
6405 return -EPROTO;
6406
6407 l2cap_process_reqseq(chan, control->reqseq);
6408
6409 if (!skb_queue_empty(&chan->tx_q))
6410 chan->tx_send_head = skb_peek(&chan->tx_q);
6411 else
6412 chan->tx_send_head = NULL;
6413
6414 /* Rewind next_tx_seq to the point expected
6415 * by the receiver.
6416 */
6417 chan->next_tx_seq = control->reqseq;
6418 chan->unacked_frames = 0;
6419
6420 err = l2cap_finish_move(chan);
6421 if (err)
6422 return err;
6423
6424 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6425 l2cap_send_i_or_rr_or_rnr(chan);
6426
6427 if (event == L2CAP_EV_RECV_IFRAME)
6428 return -EPROTO;
6429
6430 return l2cap_rx_state_recv(chan, control, NULL, event);
6431}
6432
6433static int l2cap_rx_state_wait_f(struct l2cap_chan *chan,
6434 struct l2cap_ctrl *control,
6435 struct sk_buff *skb, u8 event)
6436{
6437 int err;
6438
6439 if (!control->final)
6440 return -EPROTO;
6441
6442 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6443
6444 chan->rx_state = L2CAP_RX_STATE_RECV;
6445 l2cap_process_reqseq(chan, control->reqseq);
6446
6447 if (!skb_queue_empty(&chan->tx_q))
6448 chan->tx_send_head = skb_peek(&chan->tx_q);
6449 else
6450 chan->tx_send_head = NULL;
6451
6452 /* Rewind next_tx_seq to the point expected
6453 * by the receiver.
6454 */
6455 chan->next_tx_seq = control->reqseq;
6456 chan->unacked_frames = 0;
6457
6458 if (chan->hs_hcon)
6459 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
6460 else
6461 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
6462
6463 err = l2cap_resegment(chan);
6464
6465 if (!err)
6466 err = l2cap_rx_state_recv(chan, control, skb, event);
6467
6468 return err;
6469}
6470
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006471static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq)
6472{
6473 /* Make sure reqseq is for a packet that has been sent but not acked */
6474 u16 unacked;
6475
6476 unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq);
6477 return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked;
6478}
6479
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006480static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6481 struct sk_buff *skb, u8 event)
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006482{
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006483 int err = 0;
6484
6485 BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan,
6486 control, skb, event, chan->rx_state);
6487
6488 if (__valid_reqseq(chan, control->reqseq)) {
6489 switch (chan->rx_state) {
6490 case L2CAP_RX_STATE_RECV:
6491 err = l2cap_rx_state_recv(chan, control, skb, event);
6492 break;
6493 case L2CAP_RX_STATE_SREJ_SENT:
6494 err = l2cap_rx_state_srej_sent(chan, control, skb,
6495 event);
6496 break;
Mat Martineau32b32732012-10-23 15:24:11 -07006497 case L2CAP_RX_STATE_WAIT_P:
6498 err = l2cap_rx_state_wait_p(chan, control, skb, event);
6499 break;
6500 case L2CAP_RX_STATE_WAIT_F:
6501 err = l2cap_rx_state_wait_f(chan, control, skb, event);
6502 break;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006503 default:
6504 /* shut it down */
6505 break;
6506 }
6507 } else {
6508 BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d",
6509 control->reqseq, chan->next_tx_seq,
6510 chan->expected_ack_seq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006511 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006512 }
6513
6514 return err;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006515}
6516
6517static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6518 struct sk_buff *skb)
6519{
Mat Martineau4b51dae92012-05-17 20:53:37 -07006520 int err = 0;
6521
6522 BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb,
6523 chan->rx_state);
6524
6525 if (l2cap_classify_txseq(chan, control->txseq) ==
6526 L2CAP_TXSEQ_EXPECTED) {
6527 l2cap_pass_to_tx(chan, control);
6528
6529 BT_DBG("buffer_seq %d->%d", chan->buffer_seq,
6530 __next_seq(chan, chan->buffer_seq));
6531
6532 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
6533
6534 l2cap_reassemble_sdu(chan, skb, control);
6535 } else {
6536 if (chan->sdu) {
6537 kfree_skb(chan->sdu);
6538 chan->sdu = NULL;
6539 }
6540 chan->sdu_last_frag = NULL;
6541 chan->sdu_len = 0;
6542
6543 if (skb) {
6544 BT_DBG("Freeing %p", skb);
6545 kfree_skb(skb);
6546 }
6547 }
6548
6549 chan->last_acked_seq = control->txseq;
6550 chan->expected_tx_seq = __next_seq(chan, control->txseq);
6551
6552 return err;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006553}
6554
6555static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
6556{
6557 struct l2cap_ctrl *control = &bt_cb(skb)->control;
6558 u16 len;
6559 u8 event;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006560
Mat Martineaub76bbd62012-04-11 10:48:43 -07006561 __unpack_control(chan, skb);
6562
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006563 len = skb->len;
6564
6565 /*
6566 * We can just drop the corrupted I-frame here.
6567 * Receiver will miss it and start proper recovery
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006568 * procedures and ask for retransmission.
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006569 */
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006570 if (l2cap_check_fcs(chan, skb))
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006571 goto drop;
6572
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006573 if (!control->sframe && control->sar == L2CAP_SAR_START)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03006574 len -= L2CAP_SDULEN_SIZE;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006575
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006576 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03006577 len -= L2CAP_FCS_SIZE;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006578
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006579 if (len > chan->mps) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006580 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006581 goto drop;
6582 }
6583
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006584 if (!control->sframe) {
6585 int err;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006586
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006587 BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d",
6588 control->sar, control->reqseq, control->final,
6589 control->txseq);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03006590
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006591 /* Validate F-bit - F=0 always valid, F=1 only
6592 * valid in TX WAIT_F
6593 */
6594 if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F)
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006595 goto drop;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006596
6597 if (chan->mode != L2CAP_MODE_STREAMING) {
6598 event = L2CAP_EV_RECV_IFRAME;
6599 err = l2cap_rx(chan, control, skb, event);
6600 } else {
6601 err = l2cap_stream_rx(chan, control, skb);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006602 }
6603
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006604 if (err)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006605 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006606 } else {
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006607 const u8 rx_func_to_event[4] = {
6608 L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ,
6609 L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ
6610 };
6611
6612 /* Only I-frames are expected in streaming mode */
6613 if (chan->mode == L2CAP_MODE_STREAMING)
6614 goto drop;
6615
6616 BT_DBG("sframe reqseq %d, final %d, poll %d, super %d",
6617 control->reqseq, control->final, control->poll,
6618 control->super);
6619
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006620 if (len != 0) {
Andrei Emeltchenko1bb166e2012-11-20 17:16:21 +02006621 BT_ERR("Trailing bytes: %d in sframe", len);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006622 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006623 goto drop;
6624 }
6625
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006626 /* Validate F and P bits */
6627 if (control->final && (control->poll ||
6628 chan->tx_state != L2CAP_TX_STATE_WAIT_F))
6629 goto drop;
6630
6631 event = rx_func_to_event[control->super];
6632 if (l2cap_rx(chan, control, skb, event))
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006633 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006634 }
6635
6636 return 0;
6637
6638drop:
6639 kfree_skb(skb);
6640 return 0;
6641}
6642
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006643static void l2cap_chan_le_send_credits(struct l2cap_chan *chan)
6644{
6645 struct l2cap_conn *conn = chan->conn;
6646 struct l2cap_le_credits pkt;
6647 u16 return_credits;
6648
6649 /* We return more credits to the sender only after the amount of
6650 * credits falls below half of the initial amount.
6651 */
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02006652 if (chan->rx_credits >= (le_max_credits + 1) / 2)
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006653 return;
6654
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02006655 return_credits = le_max_credits - chan->rx_credits;
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006656
6657 BT_DBG("chan %p returning %u credits to sender", chan, return_credits);
6658
6659 chan->rx_credits += return_credits;
6660
6661 pkt.cid = cpu_to_le16(chan->scid);
6662 pkt.credits = cpu_to_le16(return_credits);
6663
6664 chan->ident = l2cap_get_ident(conn);
6665
6666 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt);
6667}
6668
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006669static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
6670{
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006671 int err;
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006672
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006673 if (!chan->rx_credits) {
6674 BT_ERR("No credits to receive LE L2CAP data");
Johan Hedbergdfd97742014-01-27 15:11:34 -08006675 l2cap_send_disconn_req(chan, ECONNRESET);
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006676 return -ENOBUFS;
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006677 }
6678
6679 if (chan->imtu < skb->len) {
6680 BT_ERR("Too big LE L2CAP PDU");
6681 return -ENOBUFS;
6682 }
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006683
6684 chan->rx_credits--;
6685 BT_DBG("rx_credits %u -> %u", chan->rx_credits + 1, chan->rx_credits);
6686
6687 l2cap_chan_le_send_credits(chan);
6688
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006689 err = 0;
6690
6691 if (!chan->sdu) {
6692 u16 sdu_len;
6693
6694 sdu_len = get_unaligned_le16(skb->data);
6695 skb_pull(skb, L2CAP_SDULEN_SIZE);
6696
6697 BT_DBG("Start of new SDU. sdu_len %u skb->len %u imtu %u",
6698 sdu_len, skb->len, chan->imtu);
6699
6700 if (sdu_len > chan->imtu) {
6701 BT_ERR("Too big LE L2CAP SDU length received");
6702 err = -EMSGSIZE;
6703 goto failed;
6704 }
6705
6706 if (skb->len > sdu_len) {
6707 BT_ERR("Too much LE L2CAP data received");
6708 err = -EINVAL;
6709 goto failed;
6710 }
6711
6712 if (skb->len == sdu_len)
6713 return chan->ops->recv(chan, skb);
6714
6715 chan->sdu = skb;
6716 chan->sdu_len = sdu_len;
6717 chan->sdu_last_frag = skb;
6718
6719 return 0;
6720 }
6721
6722 BT_DBG("SDU fragment. chan->sdu->len %u skb->len %u chan->sdu_len %u",
6723 chan->sdu->len, skb->len, chan->sdu_len);
6724
6725 if (chan->sdu->len + skb->len > chan->sdu_len) {
6726 BT_ERR("Too much LE L2CAP data received");
6727 err = -EINVAL;
6728 goto failed;
6729 }
6730
6731 append_skb_frag(chan->sdu, skb, &chan->sdu_last_frag);
6732 skb = NULL;
6733
6734 if (chan->sdu->len == chan->sdu_len) {
6735 err = chan->ops->recv(chan, chan->sdu);
6736 if (!err) {
6737 chan->sdu = NULL;
6738 chan->sdu_last_frag = NULL;
6739 chan->sdu_len = 0;
6740 }
6741 }
6742
6743failed:
6744 if (err) {
6745 kfree_skb(skb);
6746 kfree_skb(chan->sdu);
6747 chan->sdu = NULL;
6748 chan->sdu_last_frag = NULL;
6749 chan->sdu_len = 0;
6750 }
6751
6752 /* We can't return an error here since we took care of the skb
6753 * freeing internally. An error return would cause the caller to
6754 * do a double-free of the skb.
6755 */
6756 return 0;
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006757}
6758
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006759static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid,
6760 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006762 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03006764 chan = l2cap_get_chan_by_scid(conn, cid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006765 if (!chan) {
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006766 if (cid == L2CAP_CID_A2MP) {
6767 chan = a2mp_channel_create(conn, skb);
6768 if (!chan) {
6769 kfree_skb(skb);
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006770 return;
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006771 }
6772
6773 l2cap_chan_lock(chan);
6774 } else {
6775 BT_DBG("unknown cid 0x%4.4x", cid);
6776 /* Drop packet and return */
6777 kfree_skb(skb);
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006778 return;
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780 }
6781
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03006782 BT_DBG("chan %p, len %d", chan, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03006784 if (chan->state != BT_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 goto drop;
6786
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006787 switch (chan->mode) {
Johan Hedberg38319712013-05-17 12:49:23 +03006788 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006789 if (l2cap_le_data_rcv(chan, skb) < 0)
6790 goto drop;
6791
6792 goto done;
6793
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006794 case L2CAP_MODE_BASIC:
6795 /* If socket recv buffers overflows we drop data here
6796 * which is *bad* because L2CAP has to be reliable.
6797 * But we don't have any other choice. L2CAP doesn't
6798 * provide flow control mechanism. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799
Szymon Janc2c96e032014-02-18 20:48:34 +01006800 if (chan->imtu < skb->len) {
6801 BT_ERR("Dropping L2CAP data: receive buffer overflow");
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006802 goto drop;
Szymon Janc2c96e032014-02-18 20:48:34 +01006803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804
Gustavo Padovan80b98022012-05-27 22:27:51 -03006805 if (!chan->ops->recv(chan, skb))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006806 goto done;
6807 break;
6808
6809 case L2CAP_MODE_ERTM:
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03006810 case L2CAP_MODE_STREAMING:
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006811 l2cap_data_rcv(chan, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03006812 goto done;
6813
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006814 default:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006815 BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006816 break;
6817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818
6819drop:
6820 kfree_skb(skb);
6821
6822done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006823 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824}
6825
Andrei Emeltchenko84104b22012-05-31 11:18:56 +03006826static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
6827 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828{
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006829 struct hci_conn *hcon = conn->hcon;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006830 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006832 if (hcon->type != ACL_LINK)
6833 goto drop;
6834
Johan Hedbergbf20fd42013-05-14 13:23:13 +03006835 chan = l2cap_global_chan_by_psm(0, psm, &hcon->src, &hcon->dst,
6836 ACL_LINK);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006837 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838 goto drop;
6839
Andrei Emeltchenko5b4ceda2012-02-24 16:35:32 +02006840 BT_DBG("chan %p, len %d", chan, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03006842 if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 goto drop;
6844
Vinicius Costa Gomese13e21d2011-06-17 22:46:27 -03006845 if (chan->imtu < skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 goto drop;
6847
Marcel Holtmann2edf8702013-10-13 12:55:29 -07006848 /* Store remote BD_ADDR and PSM for msg_name */
Marcel Holtmann06ae3312013-10-18 03:43:00 -07006849 bacpy(&bt_cb(skb)->bdaddr, &hcon->dst);
Marcel Holtmann2edf8702013-10-13 12:55:29 -07006850 bt_cb(skb)->psm = psm;
6851
Gustavo Padovan80b98022012-05-27 22:27:51 -03006852 if (!chan->ops->recv(chan, skb))
Andrei Emeltchenko84104b22012-05-31 11:18:56 +03006853 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854
6855drop:
6856 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857}
6858
Marcel Holtmann72f78352013-04-23 00:59:00 -07006859static void l2cap_att_channel(struct l2cap_conn *conn,
Andrei Emeltchenko6810fca2012-05-31 11:18:57 +03006860 struct sk_buff *skb)
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006861{
Marcel Holtmannb99707d2013-10-03 02:54:11 -07006862 struct hci_conn *hcon = conn->hcon;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006863 struct l2cap_chan *chan;
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006864
Marcel Holtmannb99707d2013-10-03 02:54:11 -07006865 if (hcon->type != LE_LINK)
6866 goto drop;
6867
Johan Hedbergaf1c0132013-04-29 19:35:42 +03006868 chan = l2cap_global_chan_by_scid(BT_CONNECTED, L2CAP_CID_ATT,
Marcel Holtmann06ae3312013-10-18 03:43:00 -07006869 &hcon->src, &hcon->dst);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006870 if (!chan)
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006871 goto drop;
6872
Andrei Emeltchenko5b4ceda2012-02-24 16:35:32 +02006873 BT_DBG("chan %p, len %d", chan, skb->len);
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006874
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07006875 if (hci_blacklist_lookup(hcon->hdev, &hcon->dst, hcon->dst_type))
6876 goto drop;
6877
Vinicius Costa Gomese13e21d2011-06-17 22:46:27 -03006878 if (chan->imtu < skb->len)
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006879 goto drop;
6880
Gustavo Padovan80b98022012-05-27 22:27:51 -03006881 if (!chan->ops->recv(chan, skb))
Andrei Emeltchenko6810fca2012-05-31 11:18:57 +03006882 return;
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006883
6884drop:
6885 kfree_skb(skb);
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006886}
6887
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
6889{
6890 struct l2cap_hdr *lh = (void *) skb->data;
Johan Hedberg61a939c2014-01-17 20:45:11 +02006891 struct hci_conn *hcon = conn->hcon;
Al Viro8e036fc2007-07-29 00:16:36 -07006892 u16 cid, len;
6893 __le16 psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894
Johan Hedberg61a939c2014-01-17 20:45:11 +02006895 if (hcon->state != BT_CONNECTED) {
6896 BT_DBG("queueing pending rx skb");
6897 skb_queue_tail(&conn->pending_rx, skb);
6898 return;
6899 }
6900
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 skb_pull(skb, L2CAP_HDR_SIZE);
6902 cid = __le16_to_cpu(lh->cid);
6903 len = __le16_to_cpu(lh->len);
6904
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006905 if (len != skb->len) {
6906 kfree_skb(skb);
6907 return;
6908 }
6909
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 BT_DBG("len %d, cid 0x%4.4x", len, cid);
6911
6912 switch (cid) {
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -03006913 case L2CAP_CID_SIGNALING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 l2cap_sig_channel(conn, skb);
6915 break;
6916
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -03006917 case L2CAP_CID_CONN_LESS:
Andrei Emeltchenko097db762012-03-09 14:16:17 +02006918 psm = get_unaligned((__le16 *) skb->data);
Andrei Emeltchenko0181a702012-05-29 10:04:05 +03006919 skb_pull(skb, L2CAP_PSMLEN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920 l2cap_conless_channel(conn, psm, skb);
6921 break;
6922
Johan Hedberg073d1cf2013-04-29 19:35:35 +03006923 case L2CAP_CID_ATT:
Marcel Holtmann72f78352013-04-23 00:59:00 -07006924 l2cap_att_channel(conn, skb);
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006925 break;
6926
Marcel Holtmanna2877622013-10-02 23:46:54 -07006927 case L2CAP_CID_LE_SIGNALING:
6928 l2cap_le_sig_channel(conn, skb);
6929 break;
6930
Anderson Brigliab501d6a2011-06-07 18:46:31 -03006931 case L2CAP_CID_SMP:
6932 if (smp_sig_channel(conn, skb))
6933 l2cap_conn_del(conn->hcon, EACCES);
6934 break;
6935
Jukka Rissanen18722c22013-12-11 17:05:37 +02006936 case L2CAP_FC_6LOWPAN:
6937 bt_6lowpan_recv(conn, skb);
6938 break;
6939
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 default:
6941 l2cap_data_channel(conn, cid, skb);
6942 break;
6943 }
6944}
6945
Johan Hedberg61a939c2014-01-17 20:45:11 +02006946static void process_pending_rx(struct work_struct *work)
6947{
6948 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
6949 pending_rx_work);
6950 struct sk_buff *skb;
6951
6952 BT_DBG("");
6953
6954 while ((skb = skb_dequeue(&conn->pending_rx)))
6955 l2cap_recv_frame(conn, skb);
6956}
6957
Johan Hedberg162b49e2014-01-17 20:45:10 +02006958static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon)
6959{
6960 struct l2cap_conn *conn = hcon->l2cap_data;
6961 struct hci_chan *hchan;
6962
6963 if (conn)
6964 return conn;
6965
6966 hchan = hci_chan_create(hcon);
6967 if (!hchan)
6968 return NULL;
6969
6970 conn = kzalloc(sizeof(struct l2cap_conn), GFP_KERNEL);
6971 if (!conn) {
6972 hci_chan_del(hchan);
6973 return NULL;
6974 }
6975
6976 kref_init(&conn->ref);
6977 hcon->l2cap_data = conn;
6978 conn->hcon = hcon;
6979 hci_conn_get(conn->hcon);
6980 conn->hchan = hchan;
6981
6982 BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan);
6983
6984 switch (hcon->type) {
6985 case LE_LINK:
6986 if (hcon->hdev->le_mtu) {
6987 conn->mtu = hcon->hdev->le_mtu;
6988 break;
6989 }
6990 /* fall through */
6991 default:
6992 conn->mtu = hcon->hdev->acl_mtu;
6993 break;
6994 }
6995
6996 conn->feat_mask = 0;
6997
6998 if (hcon->type == ACL_LINK)
6999 conn->hs_enabled = test_bit(HCI_HS_ENABLED,
7000 &hcon->hdev->dev_flags);
7001
7002 spin_lock_init(&conn->lock);
7003 mutex_init(&conn->chan_lock);
7004
7005 INIT_LIST_HEAD(&conn->chan_l);
7006 INIT_LIST_HEAD(&conn->users);
7007
7008 if (hcon->type == LE_LINK)
7009 INIT_DELAYED_WORK(&conn->security_timer, security_timeout);
7010 else
7011 INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout);
7012
Johan Hedberg61a939c2014-01-17 20:45:11 +02007013 skb_queue_head_init(&conn->pending_rx);
7014 INIT_WORK(&conn->pending_rx_work, process_pending_rx);
7015
Johan Hedberg162b49e2014-01-17 20:45:10 +02007016 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
7017
7018 return conn;
7019}
7020
7021static bool is_valid_psm(u16 psm, u8 dst_type) {
7022 if (!psm)
7023 return false;
7024
7025 if (bdaddr_type_is_le(dst_type))
7026 return (psm <= 0x00ff);
7027
7028 /* PSM must be odd and lsb of upper byte must be 0 */
7029 return ((psm & 0x0101) == 0x0001);
7030}
7031
7032int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
7033 bdaddr_t *dst, u8 dst_type)
7034{
7035 struct l2cap_conn *conn;
7036 struct hci_conn *hcon;
7037 struct hci_dev *hdev;
7038 __u8 auth_type;
7039 int err;
7040
7041 BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", &chan->src, dst,
7042 dst_type, __le16_to_cpu(psm));
7043
7044 hdev = hci_get_route(dst, &chan->src);
7045 if (!hdev)
7046 return -EHOSTUNREACH;
7047
7048 hci_dev_lock(hdev);
7049
7050 l2cap_chan_lock(chan);
7051
7052 if (!is_valid_psm(__le16_to_cpu(psm), dst_type) && !cid &&
7053 chan->chan_type != L2CAP_CHAN_RAW) {
7054 err = -EINVAL;
7055 goto done;
7056 }
7057
Johan Hedberg21626e62014-01-24 10:35:41 +02007058 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !psm) {
7059 err = -EINVAL;
7060 goto done;
7061 }
7062
7063 if (chan->chan_type == L2CAP_CHAN_FIXED && !cid) {
Johan Hedberg162b49e2014-01-17 20:45:10 +02007064 err = -EINVAL;
7065 goto done;
7066 }
7067
7068 switch (chan->mode) {
7069 case L2CAP_MODE_BASIC:
7070 break;
7071 case L2CAP_MODE_LE_FLOWCTL:
7072 l2cap_le_flowctl_init(chan);
7073 break;
7074 case L2CAP_MODE_ERTM:
7075 case L2CAP_MODE_STREAMING:
7076 if (!disable_ertm)
7077 break;
7078 /* fall through */
7079 default:
7080 err = -ENOTSUPP;
7081 goto done;
7082 }
7083
7084 switch (chan->state) {
7085 case BT_CONNECT:
7086 case BT_CONNECT2:
7087 case BT_CONFIG:
7088 /* Already connecting */
7089 err = 0;
7090 goto done;
7091
7092 case BT_CONNECTED:
7093 /* Already connected */
7094 err = -EISCONN;
7095 goto done;
7096
7097 case BT_OPEN:
7098 case BT_BOUND:
7099 /* Can connect */
7100 break;
7101
7102 default:
7103 err = -EBADFD;
7104 goto done;
7105 }
7106
7107 /* Set destination address and psm */
7108 bacpy(&chan->dst, dst);
7109 chan->dst_type = dst_type;
7110
7111 chan->psm = psm;
7112 chan->dcid = cid;
7113
7114 auth_type = l2cap_get_auth_type(chan);
7115
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007116 if (bdaddr_type_is_le(dst_type)) {
7117 /* Convert from L2CAP channel address type to HCI address type
7118 */
7119 if (dst_type == BDADDR_LE_PUBLIC)
7120 dst_type = ADDR_LE_DEV_PUBLIC;
7121 else
7122 dst_type = ADDR_LE_DEV_RANDOM;
7123
Andre Guedes04a6c582014-02-26 20:21:44 -03007124 hcon = hci_connect_le(hdev, dst, dst_type, chan->sec_level,
7125 auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007126 } else {
Andre Guedes04a6c582014-02-26 20:21:44 -03007127 hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007128 }
Johan Hedberg162b49e2014-01-17 20:45:10 +02007129
7130 if (IS_ERR(hcon)) {
7131 err = PTR_ERR(hcon);
7132 goto done;
7133 }
7134
7135 conn = l2cap_conn_add(hcon);
7136 if (!conn) {
7137 hci_conn_drop(hcon);
7138 err = -ENOMEM;
7139 goto done;
7140 }
7141
7142 if (cid && __l2cap_get_chan_by_dcid(conn, cid)) {
7143 hci_conn_drop(hcon);
7144 err = -EBUSY;
7145 goto done;
7146 }
7147
7148 /* Update source addr of the socket */
7149 bacpy(&chan->src, &hcon->src);
7150 chan->src_type = bdaddr_type(hcon, hcon->src_type);
7151
7152 l2cap_chan_unlock(chan);
7153 l2cap_chan_add(conn, chan);
7154 l2cap_chan_lock(chan);
7155
7156 /* l2cap_chan_add takes its own ref so we can drop this one */
7157 hci_conn_drop(hcon);
7158
7159 l2cap_state_change(chan, BT_CONNECT);
7160 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
7161
Johan Hedberg61202e42014-01-28 15:16:48 -08007162 /* Release chan->sport so that it can be reused by other
7163 * sockets (as it's only used for listening sockets).
7164 */
7165 write_lock(&chan_list_lock);
7166 chan->sport = 0;
7167 write_unlock(&chan_list_lock);
7168
Johan Hedberg162b49e2014-01-17 20:45:10 +02007169 if (hcon->state == BT_CONNECTED) {
7170 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
7171 __clear_chan_timer(chan);
7172 if (l2cap_chan_check_security(chan))
7173 l2cap_state_change(chan, BT_CONNECTED);
7174 } else
7175 l2cap_do_start(chan);
7176 }
7177
7178 err = 0;
7179
7180done:
7181 l2cap_chan_unlock(chan);
7182 hci_dev_unlock(hdev);
7183 hci_dev_put(hdev);
7184 return err;
7185}
7186
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187/* ---- L2CAP interface with lower layer (HCI) ---- */
7188
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007189int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190{
7191 int exact = 0, lm1 = 0, lm2 = 0;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007192 struct l2cap_chan *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03007194 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195
7196 /* Find listening sockets and check their link_mode */
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007197 read_lock(&chan_list_lock);
7198 list_for_each_entry(c, &chan_list, global_l) {
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03007199 if (c->state != BT_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200 continue;
7201
Marcel Holtmann7eafc592013-10-13 08:12:47 -07007202 if (!bacmp(&c->src, &hdev->bdaddr)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007203 lm1 |= HCI_LM_ACCEPT;
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +03007204 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007205 lm1 |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 exact++;
Marcel Holtmann7eafc592013-10-13 08:12:47 -07007207 } else if (!bacmp(&c->src, BDADDR_ANY)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007208 lm2 |= HCI_LM_ACCEPT;
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +03007209 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007210 lm2 |= HCI_LM_MASTER;
7211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 }
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007213 read_unlock(&chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214
7215 return exact ? lm1 : lm2;
7216}
7217
Andrei Emeltchenko9e664632012-07-24 16:06:15 +03007218void l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219{
Marcel Holtmann01394182006-07-03 10:02:46 +02007220 struct l2cap_conn *conn;
7221
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03007222 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 if (!status) {
Claudio Takahasibaf43252013-04-11 13:55:50 -03007225 conn = l2cap_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 if (conn)
7227 l2cap_conn_ready(conn);
Andrei Emeltchenkoba6fc312012-10-31 15:46:26 +02007228 } else {
Joe Perchese1750722011-06-29 18:18:29 -07007229 l2cap_conn_del(hcon, bt_to_errno(status));
Andrei Emeltchenkoba6fc312012-10-31 15:46:26 +02007230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231}
7232
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007233int l2cap_disconn_ind(struct hci_conn *hcon)
Marcel Holtmann2950f212009-02-12 14:02:50 +01007234{
7235 struct l2cap_conn *conn = hcon->l2cap_data;
7236
7237 BT_DBG("hcon %p", hcon);
7238
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007239 if (!conn)
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02007240 return HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +01007241 return conn->disc_reason;
7242}
7243
Andrei Emeltchenko9e664632012-07-24 16:06:15 +03007244void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245{
7246 BT_DBG("hcon %p reason %d", hcon, reason);
7247
Jukka Rissanen18722c22013-12-11 17:05:37 +02007248 bt_6lowpan_del_conn(hcon->l2cap_data);
7249
Joe Perchese1750722011-06-29 18:18:29 -07007250 l2cap_conn_del(hcon, bt_to_errno(reason));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251}
7252
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007253static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007254{
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03007255 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
Marcel Holtmann255c7602009-02-04 21:07:19 +01007256 return;
7257
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007258 if (encrypt == 0x00) {
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007259 if (chan->sec_level == BT_SECURITY_MEDIUM) {
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007260 __set_chan_timer(chan, L2CAP_ENC_TIMEOUT);
Marcel Holtmann7d513e92014-01-15 22:37:40 -08007261 } else if (chan->sec_level == BT_SECURITY_HIGH ||
7262 chan->sec_level == BT_SECURITY_FIPS)
Gustavo F. Padovan0f852722011-05-04 19:42:50 -03007263 l2cap_chan_close(chan, ECONNREFUSED);
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007264 } else {
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007265 if (chan->sec_level == BT_SECURITY_MEDIUM)
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -03007266 __clear_chan_timer(chan);
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007267 }
7268}
7269
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007270int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271{
Marcel Holtmann40be4922008-07-14 20:13:50 +02007272 struct l2cap_conn *conn = hcon->l2cap_data;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03007273 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274
Marcel Holtmann01394182006-07-03 10:02:46 +02007275 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 return 0;
Marcel Holtmann01394182006-07-03 10:02:46 +02007277
Andrei Emeltchenko89d8b402012-07-10 15:27:51 +03007278 BT_DBG("conn %p status 0x%2.2x encrypt %u", conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279
Vinicius Costa Gomes160dc6a2011-08-19 21:06:55 -03007280 if (hcon->type == LE_LINK) {
Hemant Gupta35d4adcc2012-04-18 14:46:26 +05307281 if (!status && encrypt)
Johan Hedberg4bd6d382014-02-26 23:33:45 +02007282 smp_distribute_keys(conn);
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02007283 cancel_delayed_work(&conn->security_timer);
Vinicius Costa Gomes160dc6a2011-08-19 21:06:55 -03007284 }
7285
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007286 mutex_lock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007288 list_for_each_entry(chan, &conn->chan_l, list) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007289 l2cap_chan_lock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290
Andrei Emeltchenko89d8b402012-07-10 15:27:51 +03007291 BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid,
7292 state_to_string(chan->state));
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007293
Johan Hedberg2338a7e2014-01-24 10:35:40 +02007294 if (chan->scid == L2CAP_CID_A2MP) {
Andrei Emeltchenko78eb2f92012-07-19 17:03:47 +03007295 l2cap_chan_unlock(chan);
7296 continue;
7297 }
7298
Johan Hedberg073d1cf2013-04-29 19:35:35 +03007299 if (chan->scid == L2CAP_CID_ATT) {
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007300 if (!status && encrypt) {
7301 chan->sec_level = hcon->sec_level;
Andrei Emeltchenkocf4cd002012-02-06 15:03:59 +02007302 l2cap_chan_ready(chan);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007303 }
7304
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007305 l2cap_chan_unlock(chan);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007306 continue;
7307 }
7308
Andrei Emeltchenko96eff462012-11-15 18:14:53 +02007309 if (!__l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007310 l2cap_chan_unlock(chan);
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01007311 continue;
7312 }
7313
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03007314 if (!status && (chan->state == BT_CONNECTED ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01007315 chan->state == BT_CONFIG)) {
Marcel Holtmannd97c8992013-10-14 02:53:54 -07007316 chan->ops->resume(chan);
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007317 l2cap_check_encryption(chan, encrypt);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007318 l2cap_chan_unlock(chan);
Marcel Holtmann9719f8a2008-07-14 20:13:45 +02007319 continue;
7320 }
7321
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03007322 if (chan->state == BT_CONNECT) {
Johan Hedberg6d3c15d2013-12-02 22:13:24 +02007323 if (!status)
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03007324 l2cap_start_connection(chan);
Johan Hedberg6d3c15d2013-12-02 22:13:24 +02007325 else
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007326 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03007327 } else if (chan->state == BT_CONNECT2) {
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007328 struct l2cap_conn_rsp rsp;
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007329 __u16 res, stat;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007330
7331 if (!status) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07007332 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007333 res = L2CAP_CR_PEND;
7334 stat = L2CAP_CS_AUTHOR_PEND;
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08007335 chan->ops->defer(chan);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007336 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02007337 l2cap_state_change(chan, BT_CONFIG);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007338 res = L2CAP_CR_SUCCESS;
7339 stat = L2CAP_CS_NO_INFO;
7340 }
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007341 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02007342 l2cap_state_change(chan, BT_DISCONN);
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007343 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007344 res = L2CAP_CR_SEC_BLOCK;
7345 stat = L2CAP_CS_NO_INFO;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007346 }
7347
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03007348 rsp.scid = cpu_to_le16(chan->dcid);
7349 rsp.dcid = cpu_to_le16(chan->scid);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007350 rsp.result = cpu_to_le16(res);
7351 rsp.status = cpu_to_le16(stat);
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03007352 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01007353 sizeof(rsp), &rsp);
Mat Martineau2d369352012-05-23 14:59:30 -07007354
7355 if (!test_bit(CONF_REQ_SENT, &chan->conf_state) &&
7356 res == L2CAP_CR_SUCCESS) {
7357 char buf[128];
7358 set_bit(CONF_REQ_SENT, &chan->conf_state);
7359 l2cap_send_cmd(conn, l2cap_get_ident(conn),
7360 L2CAP_CONF_REQ,
7361 l2cap_build_conf_req(chan, buf),
7362 buf);
7363 chan->num_conf_req++;
7364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 }
7366
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007367 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368 }
7369
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007370 mutex_unlock(&conn->chan_lock);
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007371
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372 return 0;
7373}
7374
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007375int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376{
7377 struct l2cap_conn *conn = hcon->l2cap_data;
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007378 struct l2cap_hdr *hdr;
7379 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380
Andrei Emeltchenko1d13a252012-10-15 11:58:41 +03007381 /* For AMP controller do not create l2cap conn */
7382 if (!conn && hcon->hdev->dev_type != HCI_BREDR)
7383 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02007385 if (!conn)
Claudio Takahasibaf43252013-04-11 13:55:50 -03007386 conn = l2cap_conn_add(hcon);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02007387
7388 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389 goto drop;
7390
7391 BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
7392
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007393 switch (flags) {
7394 case ACL_START:
7395 case ACL_START_NO_FLUSH:
7396 case ACL_COMPLETE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 if (conn->rx_len) {
7398 BT_ERR("Unexpected start frame (len %d)", skb->len);
7399 kfree_skb(conn->rx_skb);
7400 conn->rx_skb = NULL;
7401 conn->rx_len = 0;
7402 l2cap_conn_unreliable(conn, ECOMM);
7403 }
7404
Andrei Emeltchenkoaae7fe22010-09-15 14:28:43 +03007405 /* Start fragment always begin with Basic L2CAP header */
7406 if (skb->len < L2CAP_HDR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 BT_ERR("Frame is too short (len %d)", skb->len);
7408 l2cap_conn_unreliable(conn, ECOMM);
7409 goto drop;
7410 }
7411
7412 hdr = (struct l2cap_hdr *) skb->data;
7413 len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
7414
7415 if (len == skb->len) {
7416 /* Complete frame received */
7417 l2cap_recv_frame(conn, skb);
7418 return 0;
7419 }
7420
7421 BT_DBG("Start: total len %d, frag len %d", len, skb->len);
7422
7423 if (skb->len > len) {
7424 BT_ERR("Frame is too long (len %d, expected len %d)",
Gustavo Padovan2d792812012-10-06 10:07:01 +01007425 skb->len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 l2cap_conn_unreliable(conn, ECOMM);
7427 goto drop;
7428 }
7429
7430 /* Allocate skb for the complete frame (with header) */
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03007431 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03007432 if (!conn->rx_skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 goto drop;
7434
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03007435 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Gustavo Padovan2d792812012-10-06 10:07:01 +01007436 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437 conn->rx_len = len - skb->len;
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007438 break;
7439
7440 case ACL_CONT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
7442
7443 if (!conn->rx_len) {
7444 BT_ERR("Unexpected continuation frame (len %d)", skb->len);
7445 l2cap_conn_unreliable(conn, ECOMM);
7446 goto drop;
7447 }
7448
7449 if (skb->len > conn->rx_len) {
7450 BT_ERR("Fragment is too long (len %d, expected %d)",
Gustavo Padovan2d792812012-10-06 10:07:01 +01007451 skb->len, conn->rx_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 kfree_skb(conn->rx_skb);
7453 conn->rx_skb = NULL;
7454 conn->rx_len = 0;
7455 l2cap_conn_unreliable(conn, ECOMM);
7456 goto drop;
7457 }
7458
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03007459 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Gustavo Padovan2d792812012-10-06 10:07:01 +01007460 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 conn->rx_len -= skb->len;
7462
7463 if (!conn->rx_len) {
Johan Hedbergc4e5baf2013-10-10 13:33:37 +02007464 /* Complete frame received. l2cap_recv_frame
7465 * takes ownership of the skb so set the global
7466 * rx_skb pointer to NULL first.
7467 */
7468 struct sk_buff *rx_skb = conn->rx_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 conn->rx_skb = NULL;
Johan Hedbergc4e5baf2013-10-10 13:33:37 +02007470 l2cap_recv_frame(conn, rx_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471 }
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007472 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 }
7474
7475drop:
7476 kfree_skb(skb);
7477 return 0;
7478}
7479
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007480static int l2cap_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007482 struct l2cap_chan *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483
Gustavo F. Padovan333055f2011-12-22 15:14:39 -02007484 read_lock(&chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007486 list_for_each_entry(c, &chan_list, global_l) {
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03007487 seq_printf(f, "%pMR %pMR %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
Marcel Holtmann7eafc592013-10-13 08:12:47 -07007488 &c->src, &c->dst,
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03007489 c->state, __le16_to_cpu(c->psm),
7490 c->scid, c->dcid, c->imtu, c->omtu,
7491 c->sec_level, c->mode);
Andrei Emeltchenko61e1b4b2012-01-19 11:19:50 +02007492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493
Gustavo F. Padovan333055f2011-12-22 15:14:39 -02007494 read_unlock(&chan_list_lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08007495
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497}
7498
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007499static int l2cap_debugfs_open(struct inode *inode, struct file *file)
7500{
7501 return single_open(file, l2cap_debugfs_show, inode->i_private);
7502}
7503
7504static const struct file_operations l2cap_debugfs_fops = {
7505 .open = l2cap_debugfs_open,
7506 .read = seq_read,
7507 .llseek = seq_lseek,
7508 .release = single_release,
7509};
7510
7511static struct dentry *l2cap_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512
Gustavo F. Padovan64274512011-02-07 20:08:52 -02007513int __init l2cap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514{
7515 int err;
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08007516
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02007517 err = l2cap_init_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 if (err < 0)
7519 return err;
7520
Marcel Holtmann1120e4b2013-10-17 17:24:16 -07007521 if (IS_ERR_OR_NULL(bt_debugfs))
7522 return 0;
7523
7524 l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs,
7525 NULL, &l2cap_debugfs_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526
Samuel Ortiz40b93972014-05-14 17:53:35 +02007527 debugfs_create_u16("l2cap_le_max_credits", 0644, bt_debugfs,
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02007528 &le_max_credits);
Samuel Ortiz40b93972014-05-14 17:53:35 +02007529 debugfs_create_u16("l2cap_le_default_mps", 0644, bt_debugfs,
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02007530 &le_default_mps);
7531
Jukka Rissanen18722c22013-12-11 17:05:37 +02007532 bt_6lowpan_init();
7533
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535}
7536
Gustavo F. Padovan64274512011-02-07 20:08:52 -02007537void l2cap_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538{
Jukka Rissanen18722c22013-12-11 17:05:37 +02007539 bt_6lowpan_cleanup();
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007540 debugfs_remove(l2cap_debugfs);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02007541 l2cap_cleanup_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542}
7543
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03007544module_param(disable_ertm, bool, 0644);
7545MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");