blob: 323f23cd2c37c4a9b95f3c195c48d170d9e73404 [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. Padovan89bc5002011-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. Padovan89bc5002011-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. Padovan89bc5002011-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. Padovan89bc5002011-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. Padovan89bc5002011-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. Padovan89bc5002011-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. Padovan89bc5002011-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. Padovan89bc5002011-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. Padovan89bc5002011-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. Padovan89bc5002011-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. Padovan89bc5002011-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. Padovan89bc5002011-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. Padovan89bc5002011-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
Gustavo F. Padovan59203a22010-05-01 16:15:43 -03002121 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002122 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 sent += count;
2125 len -= count;
2126
2127 /* Continuation fragments (no L2CAP header) */
2128 frag = &skb_shinfo(skb)->frag_list;
2129 while (len) {
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002130 struct sk_buff *tmp;
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 count = min_t(unsigned int, conn->mtu, len);
2133
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002134 tmp = chan->ops->alloc_skb(chan, count,
2135 msg->msg_flags & MSG_DONTWAIT);
2136 if (IS_ERR(tmp))
2137 return PTR_ERR(tmp);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002138
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002139 *frag = tmp;
2140
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002141 if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count))
2142 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002144 (*frag)->priority = skb->priority;
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 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,
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002159 struct msghdr *msg, size_t len,
2160 u32 priority)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002161{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002162 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002163 struct sk_buff *skb;
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002164 int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002165 struct l2cap_hdr *lh;
2166
Marcel Holtmann43b1b8d2013-10-12 06:01:26 -07002167 BT_DBG("chan %p psm 0x%2.2x len %zu priority %u", chan,
2168 __le16_to_cpu(chan->psm), len, priority);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002169
2170 count = min_t(unsigned int, (conn->mtu - hlen), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002171
2172 skb = chan->ops->alloc_skb(chan, count + hlen,
Gustavo Padovan90338942012-04-06 20:15:47 -03002173 msg->msg_flags & MSG_DONTWAIT);
2174 if (IS_ERR(skb))
2175 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002176
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002177 skb->priority = priority;
2178
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002179 /* Create L2CAP header */
2180 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002181 lh->cid = cpu_to_le16(chan->dcid);
Andrei Emeltchenkodaf6a78c2012-05-03 10:55:52 +03002182 lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE);
Marcel Holtmann43b1b8d2013-10-12 06:01:26 -07002183 put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002184
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002185 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002186 if (unlikely(err < 0)) {
2187 kfree_skb(skb);
2188 return ERR_PTR(err);
2189 }
2190 return skb;
2191}
2192
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002193static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan,
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002194 struct msghdr *msg, size_t len,
2195 u32 priority)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002196{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002197 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002198 struct sk_buff *skb;
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002199 int err, count;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002200 struct l2cap_hdr *lh;
2201
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002202 BT_DBG("chan %p len %zu", chan, len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002203
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002204 count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002205
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002206 skb = chan->ops->alloc_skb(chan, count + L2CAP_HDR_SIZE,
Gustavo Padovan90338942012-04-06 20:15:47 -03002207 msg->msg_flags & MSG_DONTWAIT);
2208 if (IS_ERR(skb))
2209 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002210
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002211 skb->priority = priority;
2212
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002213 /* Create L2CAP header */
2214 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002215 lh->cid = cpu_to_le16(chan->dcid);
Gustavo Padovan6ff9b5e2012-05-02 11:56:17 -03002216 lh->len = cpu_to_le16(len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002217
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002218 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002219 if (unlikely(err < 0)) {
2220 kfree_skb(skb);
2221 return ERR_PTR(err);
2222 }
2223 return skb;
2224}
2225
Luiz Augusto von Dentzab0ff762011-09-12 20:00:50 +03002226static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan,
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002227 struct msghdr *msg, size_t len,
2228 u16 sdulen)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002229{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002230 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002231 struct sk_buff *skb;
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03002232 int err, count, hlen;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002233 struct l2cap_hdr *lh;
2234
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002235 BT_DBG("chan %p len %zu", chan, len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002236
Gustavo F. Padovan0ee0d202010-05-01 16:15:41 -03002237 if (!conn)
2238 return ERR_PTR(-ENOTCONN);
2239
Gustavo Padovanba7aa642012-05-29 13:29:16 -03002240 hlen = __ertm_hdr_size(chan);
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03002241
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002242 if (sdulen)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002243 hlen += L2CAP_SDULEN_SIZE;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002244
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03002245 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002246 hlen += L2CAP_FCS_SIZE;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03002247
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002248 count = min_t(unsigned int, (conn->mtu - hlen), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002249
2250 skb = chan->ops->alloc_skb(chan, count + hlen,
Gustavo Padovan90338942012-04-06 20:15:47 -03002251 msg->msg_flags & MSG_DONTWAIT);
2252 if (IS_ERR(skb))
2253 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002254
2255 /* Create L2CAP header */
2256 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002257 lh->cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002258 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03002259
Mat Martineau18a48e72012-05-17 20:53:34 -07002260 /* Control header is populated later */
2261 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
2262 put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
2263 else
2264 put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03002265
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002266 if (sdulen)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002267 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002268
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002269 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002270 if (unlikely(err < 0)) {
2271 kfree_skb(skb);
2272 return ERR_PTR(err);
2273 }
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03002274
Mat Martineau18a48e72012-05-17 20:53:34 -07002275 bt_cb(skb)->control.fcs = chan->fcs;
Mat Martineau3ce35142012-04-25 16:36:14 -07002276 bt_cb(skb)->control.retries = 0;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002277 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
Mat Martineau94122bb2012-05-02 09:42:02 -07002280static int l2cap_segment_sdu(struct l2cap_chan *chan,
2281 struct sk_buff_head *seg_queue,
2282 struct msghdr *msg, size_t len)
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002283{
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002284 struct sk_buff *skb;
Mat Martineau94122bb2012-05-02 09:42:02 -07002285 u16 sdu_len;
2286 size_t pdu_len;
Mat Martineau94122bb2012-05-02 09:42:02 -07002287 u8 sar;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002288
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002289 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002290
Mat Martineau94122bb2012-05-02 09:42:02 -07002291 /* It is critical that ERTM PDUs fit in a single HCI fragment,
2292 * so fragmented skbs are not used. The HCI layer's handling
2293 * of fragmented skbs is not compatible with ERTM's queueing.
2294 */
2295
2296 /* PDU size is derived from the HCI MTU */
2297 pdu_len = chan->conn->mtu;
2298
Mat Martineaua5495742012-10-23 15:24:21 -07002299 /* Constrain PDU size for BR/EDR connections */
2300 if (!chan->hs_hcon)
2301 pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD);
Mat Martineau94122bb2012-05-02 09:42:02 -07002302
2303 /* Adjust for largest possible L2CAP overhead. */
Gustavo Padovan35d401d2012-05-25 18:57:05 -03002304 if (chan->fcs)
2305 pdu_len -= L2CAP_FCS_SIZE;
2306
Gustavo Padovanba7aa642012-05-29 13:29:16 -03002307 pdu_len -= __ertm_hdr_size(chan);
Mat Martineau94122bb2012-05-02 09:42:02 -07002308
2309 /* Remote device may have requested smaller PDUs */
2310 pdu_len = min_t(size_t, pdu_len, chan->remote_mps);
2311
2312 if (len <= pdu_len) {
2313 sar = L2CAP_SAR_UNSEGMENTED;
2314 sdu_len = 0;
2315 pdu_len = len;
2316 } else {
2317 sar = L2CAP_SAR_START;
2318 sdu_len = len;
2319 pdu_len -= L2CAP_SDULEN_SIZE;
2320 }
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002321
2322 while (len > 0) {
Mat Martineau94122bb2012-05-02 09:42:02 -07002323 skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002324
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002325 if (IS_ERR(skb)) {
Mat Martineau94122bb2012-05-02 09:42:02 -07002326 __skb_queue_purge(seg_queue);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002327 return PTR_ERR(skb);
2328 }
2329
Mat Martineau94122bb2012-05-02 09:42:02 -07002330 bt_cb(skb)->control.sar = sar;
2331 __skb_queue_tail(seg_queue, skb);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002332
Mat Martineau94122bb2012-05-02 09:42:02 -07002333 len -= pdu_len;
2334 if (sdu_len) {
2335 sdu_len = 0;
2336 pdu_len += L2CAP_SDULEN_SIZE;
2337 }
2338
2339 if (len <= pdu_len) {
2340 sar = L2CAP_SAR_END;
2341 pdu_len = len;
2342 } else {
2343 sar = L2CAP_SAR_CONTINUE;
2344 }
2345 }
2346
Gustavo Padovanf0f62792012-05-29 13:29:17 -03002347 return 0;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002348}
2349
Johan Hedberg177f8f22013-05-31 17:54:51 +03002350static struct sk_buff *l2cap_create_le_flowctl_pdu(struct l2cap_chan *chan,
2351 struct msghdr *msg,
2352 size_t len, u16 sdulen)
2353{
2354 struct l2cap_conn *conn = chan->conn;
2355 struct sk_buff *skb;
2356 int err, count, hlen;
2357 struct l2cap_hdr *lh;
2358
2359 BT_DBG("chan %p len %zu", chan, len);
2360
2361 if (!conn)
2362 return ERR_PTR(-ENOTCONN);
2363
2364 hlen = L2CAP_HDR_SIZE;
2365
2366 if (sdulen)
2367 hlen += L2CAP_SDULEN_SIZE;
2368
2369 count = min_t(unsigned int, (conn->mtu - hlen), len);
2370
2371 skb = chan->ops->alloc_skb(chan, count + hlen,
2372 msg->msg_flags & MSG_DONTWAIT);
2373 if (IS_ERR(skb))
2374 return skb;
2375
2376 /* Create L2CAP header */
2377 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
2378 lh->cid = cpu_to_le16(chan->dcid);
2379 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
2380
2381 if (sdulen)
2382 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
2383
2384 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
2385 if (unlikely(err < 0)) {
2386 kfree_skb(skb);
2387 return ERR_PTR(err);
2388 }
2389
2390 return skb;
2391}
2392
2393static int l2cap_segment_le_sdu(struct l2cap_chan *chan,
2394 struct sk_buff_head *seg_queue,
2395 struct msghdr *msg, size_t len)
2396{
2397 struct sk_buff *skb;
2398 size_t pdu_len;
2399 u16 sdu_len;
2400
2401 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
2402
2403 pdu_len = chan->conn->mtu - L2CAP_HDR_SIZE;
2404
2405 pdu_len = min_t(size_t, pdu_len, chan->remote_mps);
2406
2407 sdu_len = len;
2408 pdu_len -= L2CAP_SDULEN_SIZE;
2409
2410 while (len > 0) {
2411 if (len <= pdu_len)
2412 pdu_len = len;
2413
2414 skb = l2cap_create_le_flowctl_pdu(chan, msg, pdu_len, sdu_len);
2415 if (IS_ERR(skb)) {
2416 __skb_queue_purge(seg_queue);
2417 return PTR_ERR(skb);
2418 }
2419
2420 __skb_queue_tail(seg_queue, skb);
2421
2422 len -= pdu_len;
2423
2424 if (sdu_len) {
2425 sdu_len = 0;
2426 pdu_len += L2CAP_SDULEN_SIZE;
2427 }
2428 }
2429
2430 return 0;
2431}
2432
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002433int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len,
Gustavo Padovan2d792812012-10-06 10:07:01 +01002434 u32 priority)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002435{
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002436 struct sk_buff *skb;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002437 int err;
Mat Martineau94122bb2012-05-02 09:42:02 -07002438 struct sk_buff_head seg_queue;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002439
Seung-Woo Kim31e8ce82013-11-05 18:46:33 +09002440 if (!chan->conn)
2441 return -ENOTCONN;
2442
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002443 /* Connectionless channel */
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03002444 if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002445 skb = l2cap_create_connless_pdu(chan, msg, len, priority);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002446 if (IS_ERR(skb))
2447 return PTR_ERR(skb);
2448
Andrzej Kaczmarekede81a22014-02-25 17:16:22 +01002449 /* Channel lock is released before requesting new skb and then
2450 * reacquired thus we need to recheck channel state.
2451 */
2452 if (chan->state != BT_CONNECTED) {
2453 kfree_skb(skb);
2454 return -ENOTCONN;
2455 }
2456
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002457 l2cap_do_send(chan, skb);
2458 return len;
2459 }
2460
2461 switch (chan->mode) {
Johan Hedberg38319712013-05-17 12:49:23 +03002462 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedberg177f8f22013-05-31 17:54:51 +03002463 /* Check outgoing MTU */
2464 if (len > chan->omtu)
2465 return -EMSGSIZE;
2466
Johan Hedbergfad5fc892013-12-05 09:45:01 +02002467 if (!chan->tx_credits)
2468 return -EAGAIN;
2469
Johan Hedberg177f8f22013-05-31 17:54:51 +03002470 __skb_queue_head_init(&seg_queue);
2471
2472 err = l2cap_segment_le_sdu(chan, &seg_queue, msg, len);
2473
2474 if (chan->state != BT_CONNECTED) {
2475 __skb_queue_purge(&seg_queue);
2476 err = -ENOTCONN;
2477 }
2478
2479 if (err)
2480 return err;
2481
2482 skb_queue_splice_tail_init(&seg_queue, &chan->tx_q);
2483
2484 while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) {
2485 l2cap_do_send(chan, skb_dequeue(&chan->tx_q));
2486 chan->tx_credits--;
2487 }
2488
2489 if (!chan->tx_credits)
2490 chan->ops->suspend(chan);
2491
2492 err = len;
2493
2494 break;
2495
Johan Hedbergfad5fc892013-12-05 09:45:01 +02002496 case L2CAP_MODE_BASIC:
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002497 /* Check outgoing MTU */
2498 if (len > chan->omtu)
2499 return -EMSGSIZE;
2500
2501 /* Create a basic PDU */
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002502 skb = l2cap_create_basic_pdu(chan, msg, len, priority);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002503 if (IS_ERR(skb))
2504 return PTR_ERR(skb);
2505
Andrzej Kaczmarekede81a22014-02-25 17:16:22 +01002506 /* Channel lock is released before requesting new skb and then
2507 * reacquired thus we need to recheck channel state.
2508 */
2509 if (chan->state != BT_CONNECTED) {
2510 kfree_skb(skb);
2511 return -ENOTCONN;
2512 }
2513
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002514 l2cap_do_send(chan, skb);
2515 err = len;
2516 break;
2517
2518 case L2CAP_MODE_ERTM:
2519 case L2CAP_MODE_STREAMING:
Mat Martineau94122bb2012-05-02 09:42:02 -07002520 /* Check outgoing MTU */
2521 if (len > chan->omtu) {
2522 err = -EMSGSIZE;
2523 break;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002524 }
2525
Mat Martineau94122bb2012-05-02 09:42:02 -07002526 __skb_queue_head_init(&seg_queue);
2527
2528 /* Do segmentation before calling in to the state machine,
2529 * since it's possible to block while waiting for memory
2530 * allocation.
2531 */
2532 err = l2cap_segment_sdu(chan, &seg_queue, msg, len);
2533
2534 /* The channel could have been closed while segmenting,
2535 * check that it is still connected.
2536 */
2537 if (chan->state != BT_CONNECTED) {
2538 __skb_queue_purge(&seg_queue);
2539 err = -ENOTCONN;
2540 }
2541
2542 if (err)
2543 break;
2544
Mat Martineau37339372012-05-17 20:53:33 -07002545 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo Padovand6603662012-05-21 13:58:22 -03002546 l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST);
Mat Martineau37339372012-05-17 20:53:33 -07002547 else
Gustavo Padovand6603662012-05-21 13:58:22 -03002548 l2cap_streaming_send(chan, &seg_queue);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002549
Gustavo Padovand6603662012-05-21 13:58:22 -03002550 err = len;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002551
Mat Martineau94122bb2012-05-02 09:42:02 -07002552 /* If the skbs were not queued for sending, they'll still be in
2553 * seg_queue and need to be purged.
2554 */
2555 __skb_queue_purge(&seg_queue);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002556 break;
2557
2558 default:
2559 BT_DBG("bad state %1.1x", chan->mode);
2560 err = -EBADFD;
2561 }
2562
2563 return err;
2564}
2565
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002566static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq)
2567{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002568 struct l2cap_ctrl control;
2569 u16 seq;
2570
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002571 BT_DBG("chan %p, txseq %u", chan, txseq);
Mat Martineaubed68bd2012-05-17 20:53:44 -07002572
2573 memset(&control, 0, sizeof(control));
2574 control.sframe = 1;
2575 control.super = L2CAP_SUPER_SREJ;
2576
2577 for (seq = chan->expected_tx_seq; seq != txseq;
2578 seq = __next_seq(chan, seq)) {
2579 if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) {
2580 control.reqseq = seq;
2581 l2cap_send_sframe(chan, &control);
2582 l2cap_seq_list_append(&chan->srej_list, seq);
2583 }
2584 }
2585
2586 chan->expected_tx_seq = __next_seq(chan, txseq);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002587}
2588
2589static void l2cap_send_srej_tail(struct l2cap_chan *chan)
2590{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002591 struct l2cap_ctrl control;
2592
2593 BT_DBG("chan %p", chan);
2594
2595 if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR)
2596 return;
2597
2598 memset(&control, 0, sizeof(control));
2599 control.sframe = 1;
2600 control.super = L2CAP_SUPER_SREJ;
2601 control.reqseq = chan->srej_list.tail;
2602 l2cap_send_sframe(chan, &control);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002603}
2604
2605static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq)
2606{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002607 struct l2cap_ctrl control;
2608 u16 initial_head;
2609 u16 seq;
2610
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002611 BT_DBG("chan %p, txseq %u", chan, txseq);
Mat Martineaubed68bd2012-05-17 20:53:44 -07002612
2613 memset(&control, 0, sizeof(control));
2614 control.sframe = 1;
2615 control.super = L2CAP_SUPER_SREJ;
2616
2617 /* Capture initial list head to allow only one pass through the list. */
2618 initial_head = chan->srej_list.head;
2619
2620 do {
2621 seq = l2cap_seq_list_pop(&chan->srej_list);
2622 if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR)
2623 break;
2624
2625 control.reqseq = seq;
2626 l2cap_send_sframe(chan, &control);
2627 l2cap_seq_list_append(&chan->srej_list, seq);
2628 } while (chan->srej_list.head != initial_head);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002629}
2630
Mat Martineau608bcc62012-05-17 20:53:32 -07002631static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq)
2632{
2633 struct sk_buff *acked_skb;
2634 u16 ackseq;
2635
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002636 BT_DBG("chan %p, reqseq %u", chan, reqseq);
Mat Martineau608bcc62012-05-17 20:53:32 -07002637
2638 if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq)
2639 return;
2640
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002641 BT_DBG("expected_ack_seq %u, unacked_frames %u",
Mat Martineau608bcc62012-05-17 20:53:32 -07002642 chan->expected_ack_seq, chan->unacked_frames);
2643
2644 for (ackseq = chan->expected_ack_seq; ackseq != reqseq;
2645 ackseq = __next_seq(chan, ackseq)) {
2646
2647 acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq);
2648 if (acked_skb) {
2649 skb_unlink(acked_skb, &chan->tx_q);
2650 kfree_skb(acked_skb);
2651 chan->unacked_frames--;
2652 }
2653 }
2654
2655 chan->expected_ack_seq = reqseq;
2656
2657 if (chan->unacked_frames == 0)
2658 __clear_retrans_timer(chan);
2659
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002660 BT_DBG("unacked_frames %u", chan->unacked_frames);
Mat Martineau608bcc62012-05-17 20:53:32 -07002661}
2662
2663static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan)
2664{
2665 BT_DBG("chan %p", chan);
2666
2667 chan->expected_tx_seq = chan->buffer_seq;
2668 l2cap_seq_list_clear(&chan->srej_list);
2669 skb_queue_purge(&chan->srej_q);
2670 chan->rx_state = L2CAP_RX_STATE_RECV;
2671}
2672
Gustavo Padovand6603662012-05-21 13:58:22 -03002673static void l2cap_tx_state_xmit(struct l2cap_chan *chan,
2674 struct l2cap_ctrl *control,
2675 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002676{
Mat Martineau608bcc62012-05-17 20:53:32 -07002677 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2678 event);
2679
2680 switch (event) {
2681 case L2CAP_EV_DATA_REQUEST:
2682 if (chan->tx_send_head == NULL)
2683 chan->tx_send_head = skb_peek(skbs);
2684
2685 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2686 l2cap_ertm_send(chan);
2687 break;
2688 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2689 BT_DBG("Enter LOCAL_BUSY");
2690 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2691
2692 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2693 /* The SREJ_SENT state must be aborted if we are to
2694 * enter the LOCAL_BUSY state.
2695 */
2696 l2cap_abort_rx_srej_sent(chan);
2697 }
2698
2699 l2cap_send_ack(chan);
2700
2701 break;
2702 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2703 BT_DBG("Exit LOCAL_BUSY");
2704 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2705
2706 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2707 struct l2cap_ctrl local_control;
2708
2709 memset(&local_control, 0, sizeof(local_control));
2710 local_control.sframe = 1;
2711 local_control.super = L2CAP_SUPER_RR;
2712 local_control.poll = 1;
2713 local_control.reqseq = chan->buffer_seq;
Mat Martineaua67d7f62012-05-17 20:53:35 -07002714 l2cap_send_sframe(chan, &local_control);
Mat Martineau608bcc62012-05-17 20:53:32 -07002715
2716 chan->retry_count = 1;
2717 __set_monitor_timer(chan);
2718 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2719 }
2720 break;
2721 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2722 l2cap_process_reqseq(chan, control->reqseq);
2723 break;
2724 case L2CAP_EV_EXPLICIT_POLL:
2725 l2cap_send_rr_or_rnr(chan, 1);
2726 chan->retry_count = 1;
2727 __set_monitor_timer(chan);
2728 __clear_ack_timer(chan);
2729 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2730 break;
2731 case L2CAP_EV_RETRANS_TO:
2732 l2cap_send_rr_or_rnr(chan, 1);
2733 chan->retry_count = 1;
2734 __set_monitor_timer(chan);
2735 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2736 break;
2737 case L2CAP_EV_RECV_FBIT:
2738 /* Nothing to process */
2739 break;
2740 default:
2741 break;
2742 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002743}
2744
Gustavo Padovand6603662012-05-21 13:58:22 -03002745static void l2cap_tx_state_wait_f(struct l2cap_chan *chan,
2746 struct l2cap_ctrl *control,
2747 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002748{
Mat Martineau608bcc62012-05-17 20:53:32 -07002749 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2750 event);
2751
2752 switch (event) {
2753 case L2CAP_EV_DATA_REQUEST:
2754 if (chan->tx_send_head == NULL)
2755 chan->tx_send_head = skb_peek(skbs);
2756 /* Queue data, but don't send. */
2757 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2758 break;
2759 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2760 BT_DBG("Enter LOCAL_BUSY");
2761 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2762
2763 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2764 /* The SREJ_SENT state must be aborted if we are to
2765 * enter the LOCAL_BUSY state.
2766 */
2767 l2cap_abort_rx_srej_sent(chan);
2768 }
2769
2770 l2cap_send_ack(chan);
2771
2772 break;
2773 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2774 BT_DBG("Exit LOCAL_BUSY");
2775 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2776
2777 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2778 struct l2cap_ctrl local_control;
2779 memset(&local_control, 0, sizeof(local_control));
2780 local_control.sframe = 1;
2781 local_control.super = L2CAP_SUPER_RR;
2782 local_control.poll = 1;
2783 local_control.reqseq = chan->buffer_seq;
Mat Martineaua67d7f62012-05-17 20:53:35 -07002784 l2cap_send_sframe(chan, &local_control);
Mat Martineau608bcc62012-05-17 20:53:32 -07002785
2786 chan->retry_count = 1;
2787 __set_monitor_timer(chan);
2788 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2789 }
2790 break;
2791 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2792 l2cap_process_reqseq(chan, control->reqseq);
2793
2794 /* Fall through */
2795
2796 case L2CAP_EV_RECV_FBIT:
2797 if (control && control->final) {
2798 __clear_monitor_timer(chan);
2799 if (chan->unacked_frames > 0)
2800 __set_retrans_timer(chan);
2801 chan->retry_count = 0;
2802 chan->tx_state = L2CAP_TX_STATE_XMIT;
2803 BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state);
2804 }
2805 break;
2806 case L2CAP_EV_EXPLICIT_POLL:
2807 /* Ignore */
2808 break;
2809 case L2CAP_EV_MONITOR_TO:
2810 if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) {
2811 l2cap_send_rr_or_rnr(chan, 1);
2812 __set_monitor_timer(chan);
2813 chan->retry_count++;
2814 } else {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02002815 l2cap_send_disconn_req(chan, ECONNABORTED);
Mat Martineau608bcc62012-05-17 20:53:32 -07002816 }
2817 break;
2818 default:
2819 break;
2820 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002821}
2822
Gustavo Padovand6603662012-05-21 13:58:22 -03002823static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
2824 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002825{
Mat Martineau608bcc62012-05-17 20:53:32 -07002826 BT_DBG("chan %p, control %p, skbs %p, event %d, state %d",
2827 chan, control, skbs, event, chan->tx_state);
2828
2829 switch (chan->tx_state) {
2830 case L2CAP_TX_STATE_XMIT:
Gustavo Padovand6603662012-05-21 13:58:22 -03002831 l2cap_tx_state_xmit(chan, control, skbs, event);
Mat Martineau608bcc62012-05-17 20:53:32 -07002832 break;
2833 case L2CAP_TX_STATE_WAIT_F:
Gustavo Padovand6603662012-05-21 13:58:22 -03002834 l2cap_tx_state_wait_f(chan, control, skbs, event);
Mat Martineau608bcc62012-05-17 20:53:32 -07002835 break;
2836 default:
2837 /* Ignore event */
2838 break;
2839 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002840}
2841
Mat Martineau4b51dae92012-05-17 20:53:37 -07002842static void l2cap_pass_to_tx(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_REQSEQ_AND_FBIT);
Mat Martineau4b51dae92012-05-17 20:53:37 -07002847}
2848
Mat Martineauf80842a2012-05-17 20:53:46 -07002849static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan,
2850 struct l2cap_ctrl *control)
2851{
2852 BT_DBG("chan %p, control %p", chan, control);
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03002853 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT);
Mat Martineauf80842a2012-05-17 20:53:46 -07002854}
2855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856/* Copy frame to all raw sockets on that connection */
2857static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
2858{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 struct sk_buff *nskb;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03002860 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862 BT_DBG("conn %p", conn);
2863
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002864 mutex_lock(&conn->chan_lock);
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -02002865
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002866 list_for_each_entry(chan, &conn->chan_l, list) {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03002867 if (chan->chan_type != L2CAP_CHAN_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 continue;
2869
Gustavo Padovan7f5396a2013-10-21 18:22:25 -02002870 /* Don't send frame to the channel it came from */
2871 if (bt_cb(skb)->chan == chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 continue;
Gustavo Padovan7f5396a2013-10-21 18:22:25 -02002873
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002874 nskb = skb_clone(skb, GFP_KERNEL);
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03002875 if (!nskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 continue;
Gustavo Padovan80b98022012-05-27 22:27:51 -03002877 if (chan->ops->recv(chan, nskb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 kfree_skb(nskb);
2879 }
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -02002880
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002881 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882}
2883
2884/* ---- L2CAP signalling commands ---- */
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002885static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code,
2886 u8 ident, u16 dlen, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
2888 struct sk_buff *skb, **frag;
2889 struct l2cap_cmd_hdr *cmd;
2890 struct l2cap_hdr *lh;
2891 int len, count;
2892
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002893 BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u",
2894 conn, code, ident, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Anderson Lizardo300b9622013-06-02 16:30:40 -04002896 if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE)
2897 return NULL;
2898
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
2900 count = min_t(unsigned int, conn->mtu, len);
2901
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002902 skb = bt_skb_alloc(count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 if (!skb)
2904 return NULL;
2905
2906 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002907 lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02002908
2909 if (conn->hcon->type == LE_LINK)
Joe Perchesdcf4adb2014-03-12 10:52:35 -07002910 lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02002911 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -07002912 lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
2914 cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
2915 cmd->code = code;
2916 cmd->ident = ident;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002917 cmd->len = cpu_to_le16(dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
2919 if (dlen) {
2920 count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
2921 memcpy(skb_put(skb, count), data, count);
2922 data += count;
2923 }
2924
2925 len -= skb->len;
2926
2927 /* Continuation fragments (no L2CAP header) */
2928 frag = &skb_shinfo(skb)->frag_list;
2929 while (len) {
2930 count = min_t(unsigned int, conn->mtu, len);
2931
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002932 *frag = bt_skb_alloc(count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (!*frag)
2934 goto fail;
2935
2936 memcpy(skb_put(*frag, count), data, count);
2937
2938 len -= count;
2939 data += count;
2940
2941 frag = &(*frag)->next;
2942 }
2943
2944 return skb;
2945
2946fail:
2947 kfree_skb(skb);
2948 return NULL;
2949}
2950
Gustavo Padovan2d792812012-10-06 10:07:01 +01002951static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen,
2952 unsigned long *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
2954 struct l2cap_conf_opt *opt = *ptr;
2955 int len;
2956
2957 len = L2CAP_CONF_OPT_SIZE + opt->len;
2958 *ptr += len;
2959
2960 *type = opt->type;
2961 *olen = opt->len;
2962
2963 switch (opt->len) {
2964 case 1:
2965 *val = *((u8 *) opt->val);
2966 break;
2967
2968 case 2:
steven miaobfaaeb32010-10-16 18:29:47 -04002969 *val = get_unaligned_le16(opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 break;
2971
2972 case 4:
steven miaobfaaeb32010-10-16 18:29:47 -04002973 *val = get_unaligned_le32(opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 break;
2975
2976 default:
2977 *val = (unsigned long) opt->val;
2978 break;
2979 }
2980
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002981 BT_DBG("type 0x%2.2x len %u val 0x%lx", *type, opt->len, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 return len;
2983}
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
2986{
2987 struct l2cap_conf_opt *opt = *ptr;
2988
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002989 BT_DBG("type 0x%2.2x len %u val 0x%lx", type, len, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
2991 opt->type = type;
2992 opt->len = len;
2993
2994 switch (len) {
2995 case 1:
2996 *((u8 *) opt->val) = val;
2997 break;
2998
2999 case 2:
Gustavo F. Padovan4f8b6912010-10-18 14:25:53 -02003000 put_unaligned_le16(val, opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 break;
3002
3003 case 4:
Gustavo F. Padovan4f8b6912010-10-18 14:25:53 -02003004 put_unaligned_le32(val, opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 break;
3006
3007 default:
3008 memcpy(opt->val, (void *) val, len);
3009 break;
3010 }
3011
3012 *ptr += L2CAP_CONF_OPT_SIZE + len;
3013}
3014
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003015static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan)
3016{
3017 struct l2cap_conf_efs efs;
3018
Szymon Janc1ec918c2011-11-16 09:32:21 +01003019 switch (chan->mode) {
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003020 case L2CAP_MODE_ERTM:
3021 efs.id = chan->local_id;
3022 efs.stype = chan->local_stype;
3023 efs.msdu = cpu_to_le16(chan->local_msdu);
3024 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003025 efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT);
3026 efs.flush_to = cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003027 break;
3028
3029 case L2CAP_MODE_STREAMING:
3030 efs.id = 1;
3031 efs.stype = L2CAP_SERV_BESTEFFORT;
3032 efs.msdu = cpu_to_le16(chan->local_msdu);
3033 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
3034 efs.acc_lat = 0;
3035 efs.flush_to = 0;
3036 break;
3037
3038 default:
3039 return;
3040 }
3041
3042 l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs),
Andrei Emeltchenko8936fa62012-10-08 11:14:41 +03003043 (unsigned long) &efs);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003044}
3045
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003046static void l2cap_ack_timeout(struct work_struct *work)
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003047{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003048 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau03625202012-05-17 20:53:51 -07003049 ack_timer.work);
3050 u16 frames_to_ack;
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003051
Gustavo F. Padovan2fb9b3d2011-12-22 16:56:05 -02003052 BT_DBG("chan %p", chan);
3053
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003054 l2cap_chan_lock(chan);
3055
Mat Martineau03625202012-05-17 20:53:51 -07003056 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
3057 chan->last_acked_seq);
3058
3059 if (frames_to_ack)
3060 l2cap_send_rr_or_rnr(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003061
3062 l2cap_chan_unlock(chan);
Szymon Janc09bfb2e2012-01-11 10:59:49 +01003063 l2cap_chan_put(chan);
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003064}
3065
Andrei Emeltchenko466f8002012-05-29 13:59:01 +03003066int l2cap_ertm_init(struct l2cap_chan *chan)
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003067{
Mat Martineau3c588192012-04-11 10:48:42 -07003068 int err;
3069
Mat Martineau105bdf92012-04-27 16:50:48 -07003070 chan->next_tx_seq = 0;
3071 chan->expected_tx_seq = 0;
Gustavo F. Padovan42e5c802011-03-25 19:58:34 -03003072 chan->expected_ack_seq = 0;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03003073 chan->unacked_frames = 0;
Gustavo F. Padovan42e5c802011-03-25 19:58:34 -03003074 chan->buffer_seq = 0;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03003075 chan->frames_sent = 0;
Mat Martineau105bdf92012-04-27 16:50:48 -07003076 chan->last_acked_seq = 0;
3077 chan->sdu = NULL;
3078 chan->sdu_last_frag = NULL;
3079 chan->sdu_len = 0;
3080
Mat Martineaud34c34f2012-05-14 14:49:27 -07003081 skb_queue_head_init(&chan->tx_q);
3082
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003083 chan->local_amp_id = AMP_ID_BREDR;
3084 chan->move_id = AMP_ID_BREDR;
Mat Martineau08333282012-10-23 15:24:06 -07003085 chan->move_state = L2CAP_MOVE_STABLE;
3086 chan->move_role = L2CAP_MOVE_ROLE_NONE;
3087
Mat Martineau105bdf92012-04-27 16:50:48 -07003088 if (chan->mode != L2CAP_MODE_ERTM)
3089 return 0;
3090
3091 chan->rx_state = L2CAP_RX_STATE_RECV;
3092 chan->tx_state = L2CAP_TX_STATE_XMIT;
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003093
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003094 INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout);
3095 INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout);
3096 INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003097
Gustavo F. Padovanf1c67752011-03-25 20:36:10 -03003098 skb_queue_head_init(&chan->srej_q);
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03003099
Mat Martineau3c588192012-04-11 10:48:42 -07003100 err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win);
3101 if (err < 0)
3102 return err;
3103
Mat Martineau9dc9aff2012-05-17 16:20:14 -07003104 err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win);
3105 if (err < 0)
3106 l2cap_seq_list_free(&chan->srej_list);
3107
3108 return err;
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003109}
3110
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003111static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
3112{
3113 switch (mode) {
3114 case L2CAP_MODE_STREAMING:
3115 case L2CAP_MODE_ERTM:
3116 if (l2cap_mode_supported(mode, remote_feat_mask))
3117 return mode;
3118 /* fall through */
3119 default:
3120 return L2CAP_MODE_BASIC;
3121 }
3122}
3123
Marcel Holtmann848566b2013-10-01 22:59:22 -07003124static inline bool __l2cap_ews_supported(struct l2cap_conn *conn)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003125{
Marcel Holtmann848566b2013-10-01 22:59:22 -07003126 return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_WINDOW;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003127}
3128
Marcel Holtmann848566b2013-10-01 22:59:22 -07003129static inline bool __l2cap_efs_supported(struct l2cap_conn *conn)
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003130{
Marcel Holtmann848566b2013-10-01 22:59:22 -07003131 return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_FLOW;
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003132}
3133
Mat Martineau36c86c82012-10-23 15:24:20 -07003134static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan,
3135 struct l2cap_conf_rfc *rfc)
3136{
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003137 if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) {
Mat Martineau36c86c82012-10-23 15:24:20 -07003138 u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to;
3139
3140 /* Class 1 devices have must have ERTM timeouts
3141 * exceeding the Link Supervision Timeout. The
3142 * default Link Supervision Timeout for AMP
3143 * controllers is 10 seconds.
3144 *
3145 * Class 1 devices use 0xffffffff for their
3146 * best-effort flush timeout, so the clamping logic
3147 * will result in a timeout that meets the above
3148 * requirement. ERTM timeouts are 16-bit values, so
3149 * the maximum timeout is 65.535 seconds.
3150 */
3151
3152 /* Convert timeout to milliseconds and round */
3153 ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000);
3154
3155 /* This is the recommended formula for class 2 devices
3156 * that start ERTM timers when packets are sent to the
3157 * controller.
3158 */
3159 ertm_to = 3 * ertm_to + 500;
3160
3161 if (ertm_to > 0xffff)
3162 ertm_to = 0xffff;
3163
3164 rfc->retrans_timeout = cpu_to_le16((u16) ertm_to);
3165 rfc->monitor_timeout = rfc->retrans_timeout;
3166 } else {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003167 rfc->retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
3168 rfc->monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
Mat Martineau36c86c82012-10-23 15:24:20 -07003169 }
3170}
3171
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003172static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
3173{
3174 if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW &&
Marcel Holtmann848566b2013-10-01 22:59:22 -07003175 __l2cap_ews_supported(chan->conn)) {
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003176 /* use extended control field */
3177 set_bit(FLAG_EXT_CTRL, &chan->flags);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003178 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
3179 } else {
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003180 chan->tx_win = min_t(u16, chan->tx_win,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003181 L2CAP_DEFAULT_TX_WINDOW);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003182 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
3183 }
Mat Martineauc20f8e32012-07-10 05:47:07 -07003184 chan->ack_win = chan->tx_win;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003185}
3186
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003187static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 struct l2cap_conf_req *req = data;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003190 struct l2cap_conf_rfc rfc = { .mode = chan->mode };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 void *ptr = req->data;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003192 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03003194 BT_DBG("chan %p", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003196 if (chan->num_conf_req || chan->num_conf_rsp)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003197 goto done;
3198
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003199 switch (chan->mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003200 case L2CAP_MODE_STREAMING:
3201 case L2CAP_MODE_ERTM:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003202 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003203 break;
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003204
Marcel Holtmann848566b2013-10-01 22:59:22 -07003205 if (__l2cap_efs_supported(chan->conn))
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003206 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3207
Gustavo F. Padovan2ba13ed2010-06-09 16:39:05 -03003208 /* fall through */
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003209 default:
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003210 chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003211 break;
3212 }
3213
3214done:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003215 if (chan->imtu != L2CAP_DEFAULT_MTU)
3216 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
Gustavo F. Padovan7990681c2011-01-24 16:01:43 -02003217
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003218 switch (chan->mode) {
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003219 case L2CAP_MODE_BASIC:
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003220 if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003221 !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003222 break;
3223
Gustavo F. Padovan62547752010-06-08 20:05:31 -03003224 rfc.mode = L2CAP_MODE_BASIC;
3225 rfc.txwin_size = 0;
3226 rfc.max_transmit = 0;
3227 rfc.retrans_timeout = 0;
3228 rfc.monitor_timeout = 0;
3229 rfc.max_pdu_size = 0;
3230
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003231 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003232 (unsigned long) &rfc);
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003233 break;
3234
3235 case L2CAP_MODE_ERTM:
3236 rfc.mode = L2CAP_MODE_ERTM;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003237 rfc.max_transmit = chan->max_tx;
Mat Martineau36c86c82012-10-23 15:24:20 -07003238
3239 __l2cap_set_ertm_timeouts(chan, &rfc);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003240
3241 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
Gustavo Padovan2d792812012-10-06 10:07:01 +01003242 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3243 L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003244 rfc.max_pdu_size = cpu_to_le16(size);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003245
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003246 l2cap_txwin_setup(chan);
3247
3248 rfc.txwin_size = min_t(u16, chan->tx_win,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003249 L2CAP_DEFAULT_TX_WINDOW);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003250
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003251 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003252 (unsigned long) &rfc);
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003253
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003254 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
3255 l2cap_add_opt_efs(&ptr, chan);
3256
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003257 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3258 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003259 chan->tx_win);
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003260
3261 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3262 if (chan->fcs == L2CAP_FCS_NONE ||
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003263 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003264 chan->fcs = L2CAP_FCS_NONE;
3265 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
3266 chan->fcs);
3267 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003268 break;
3269
3270 case L2CAP_MODE_STREAMING:
Mat Martineau273759e2012-05-17 20:53:53 -07003271 l2cap_txwin_setup(chan);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003272 rfc.mode = L2CAP_MODE_STREAMING;
3273 rfc.txwin_size = 0;
3274 rfc.max_transmit = 0;
3275 rfc.retrans_timeout = 0;
3276 rfc.monitor_timeout = 0;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003277
3278 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
Gustavo Padovan2d792812012-10-06 10:07:01 +01003279 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3280 L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003281 rfc.max_pdu_size = cpu_to_le16(size);
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003282
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003283 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003284 (unsigned long) &rfc);
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003285
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003286 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
3287 l2cap_add_opt_efs(&ptr, chan);
3288
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003289 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3290 if (chan->fcs == L2CAP_FCS_NONE ||
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003291 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003292 chan->fcs = L2CAP_FCS_NONE;
3293 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
3294 chan->fcs);
3295 }
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003296 break;
3297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003299 req->dcid = cpu_to_le16(chan->dcid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003300 req->flags = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
3302 return ptr - data;
3303}
3304
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003305static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003307 struct l2cap_conf_rsp *rsp = data;
3308 void *ptr = rsp->data;
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003309 void *req = chan->conf_req;
3310 int len = chan->conf_len;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003311 int type, hint, olen;
3312 unsigned long val;
Marcel Holtmann6464f352007-10-20 13:39:51 +02003313 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003314 struct l2cap_conf_efs efs;
3315 u8 remote_efs = 0;
Marcel Holtmann861d6882007-10-20 13:37:06 +02003316 u16 mtu = L2CAP_DEFAULT_MTU;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003317 u16 result = L2CAP_CONF_SUCCESS;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003318 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003320 BT_DBG("chan %p", chan);
Marcel Holtmann820ae1b2006-11-18 22:15:00 +01003321
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003322 while (len >= L2CAP_CONF_OPT_SIZE) {
3323 len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Gustavo F. Padovan589d2742009-04-20 01:31:07 -03003325 hint = type & L2CAP_CONF_HINT;
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -07003326 type &= L2CAP_CONF_MASK;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003327
3328 switch (type) {
3329 case L2CAP_CONF_MTU:
Marcel Holtmann861d6882007-10-20 13:37:06 +02003330 mtu = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003331 break;
3332
3333 case L2CAP_CONF_FLUSH_TO:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003334 chan->flush_to = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003335 break;
3336
3337 case L2CAP_CONF_QOS:
3338 break;
3339
Marcel Holtmann6464f352007-10-20 13:39:51 +02003340 case L2CAP_CONF_RFC:
3341 if (olen == sizeof(rfc))
3342 memcpy(&rfc, (void *) val, olen);
3343 break;
3344
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003345 case L2CAP_CONF_FCS:
3346 if (val == L2CAP_FCS_NONE)
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003347 set_bit(CONF_RECV_NO_FCS, &chan->conf_state);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003348 break;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003349
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003350 case L2CAP_CONF_EFS:
3351 remote_efs = 1;
3352 if (olen == sizeof(efs))
3353 memcpy(&efs, (void *) val, olen);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003354 break;
3355
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003356 case L2CAP_CONF_EWS:
Marcel Holtmann848566b2013-10-01 22:59:22 -07003357 if (!chan->conn->hs_enabled)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003358 return -ECONNREFUSED;
3359
3360 set_bit(FLAG_EXT_CTRL, &chan->flags);
3361 set_bit(CONF_EWS_RECV, &chan->conf_state);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003362 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003363 chan->remote_tx_win = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003364 break;
3365
3366 default:
3367 if (hint)
3368 break;
3369
3370 result = L2CAP_CONF_UNKNOWN;
3371 *((u8 *) ptr++) = type;
3372 break;
3373 }
3374 }
3375
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003376 if (chan->num_conf_rsp || chan->num_conf_req > 1)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003377 goto done;
3378
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003379 switch (chan->mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003380 case L2CAP_MODE_STREAMING:
3381 case L2CAP_MODE_ERTM:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003382 if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003383 chan->mode = l2cap_select_mode(rfc.mode,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003384 chan->conn->feat_mask);
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003385 break;
3386 }
3387
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003388 if (remote_efs) {
Marcel Holtmann848566b2013-10-01 22:59:22 -07003389 if (__l2cap_efs_supported(chan->conn))
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003390 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3391 else
3392 return -ECONNREFUSED;
3393 }
3394
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003395 if (chan->mode != rfc.mode)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003396 return -ECONNREFUSED;
Gustavo F. Padovan742e5192010-06-08 19:09:48 -03003397
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003398 break;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003399 }
3400
3401done:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003402 if (chan->mode != rfc.mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003403 result = L2CAP_CONF_UNACCEPT;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003404 rfc.mode = chan->mode;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003405
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003406 if (chan->num_conf_rsp == 1)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003407 return -ECONNREFUSED;
3408
Gustavo Padovan2d792812012-10-06 10:07:01 +01003409 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3410 (unsigned long) &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003411 }
3412
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003413 if (result == L2CAP_CONF_SUCCESS) {
3414 /* Configure output options and let the other side know
3415 * which ones we don't like. */
3416
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003417 if (mtu < L2CAP_DEFAULT_MIN_MTU)
3418 result = L2CAP_CONF_UNACCEPT;
3419 else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003420 chan->omtu = mtu;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003421 set_bit(CONF_MTU_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003422 }
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003423 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003424
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003425 if (remote_efs) {
3426 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003427 efs.stype != L2CAP_SERV_NOTRAFIC &&
3428 efs.stype != chan->local_stype) {
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003429
3430 result = L2CAP_CONF_UNACCEPT;
3431
3432 if (chan->num_conf_req >= 1)
3433 return -ECONNREFUSED;
3434
3435 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003436 sizeof(efs),
3437 (unsigned long) &efs);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003438 } else {
Gustavo F. Padovan3e6b3b92011-11-01 14:06:23 -02003439 /* Send PENDING Conf Rsp */
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003440 result = L2CAP_CONF_PENDING;
3441 set_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003442 }
3443 }
3444
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003445 switch (rfc.mode) {
3446 case L2CAP_MODE_BASIC:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003447 chan->fcs = L2CAP_FCS_NONE;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003448 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003449 break;
3450
3451 case L2CAP_MODE_ERTM:
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003452 if (!test_bit(CONF_EWS_RECV, &chan->conf_state))
3453 chan->remote_tx_win = rfc.txwin_size;
3454 else
3455 rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW;
3456
Gustavo F. Padovan2c03a7a2011-03-25 20:15:28 -03003457 chan->remote_max_tx = rfc.max_transmit;
Mat Martineau86b1b262010-08-05 15:54:22 -07003458
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003459 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003460 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3461 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003462 rfc.max_pdu_size = cpu_to_le16(size);
3463 chan->remote_mps = size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003464
Mat Martineau36c86c82012-10-23 15:24:20 -07003465 __l2cap_set_ertm_timeouts(chan, &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003466
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003467 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003468
3469 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003470 sizeof(rfc), (unsigned long) &rfc);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003471
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003472 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3473 chan->remote_id = efs.id;
3474 chan->remote_stype = efs.stype;
3475 chan->remote_msdu = le16_to_cpu(efs.msdu);
3476 chan->remote_flush_to =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003477 le32_to_cpu(efs.flush_to);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003478 chan->remote_acc_lat =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003479 le32_to_cpu(efs.acc_lat);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003480 chan->remote_sdu_itime =
3481 le32_to_cpu(efs.sdu_itime);
3482 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003483 sizeof(efs),
3484 (unsigned long) &efs);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003485 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003486 break;
3487
3488 case L2CAP_MODE_STREAMING:
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003489 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003490 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3491 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003492 rfc.max_pdu_size = cpu_to_le16(size);
3493 chan->remote_mps = size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003494
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003495 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003496
Gustavo Padovan2d792812012-10-06 10:07:01 +01003497 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3498 (unsigned long) &rfc);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003499
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003500 break;
3501
3502 default:
Marcel Holtmann6464f352007-10-20 13:39:51 +02003503 result = L2CAP_CONF_UNACCEPT;
3504
3505 memset(&rfc, 0, sizeof(rfc));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003506 rfc.mode = chan->mode;
Marcel Holtmann6464f352007-10-20 13:39:51 +02003507 }
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003508
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003509 if (result == L2CAP_CONF_SUCCESS)
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003510 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003511 }
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003512 rsp->scid = cpu_to_le16(chan->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003513 rsp->result = cpu_to_le16(result);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003514 rsp->flags = cpu_to_le16(0);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003515
3516 return ptr - data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
Gustavo Padovan2d792812012-10-06 10:07:01 +01003519static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
3520 void *data, u16 *result)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003521{
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003522 struct l2cap_conf_req *req = data;
3523 void *ptr = req->data;
3524 int type, olen;
3525 unsigned long val;
Mat Martineau36e999a2011-12-08 17:23:21 -08003526 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003527 struct l2cap_conf_efs efs;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003528
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003529 BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003530
3531 while (len >= L2CAP_CONF_OPT_SIZE) {
3532 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3533
3534 switch (type) {
3535 case L2CAP_CONF_MTU:
3536 if (val < L2CAP_DEFAULT_MIN_MTU) {
3537 *result = L2CAP_CONF_UNACCEPT;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003538 chan->imtu = L2CAP_DEFAULT_MIN_MTU;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003539 } else
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003540 chan->imtu = val;
3541 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003542 break;
3543
3544 case L2CAP_CONF_FLUSH_TO:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003545 chan->flush_to = val;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003546 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003547 2, chan->flush_to);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003548 break;
3549
3550 case L2CAP_CONF_RFC:
3551 if (olen == sizeof(rfc))
3552 memcpy(&rfc, (void *)val, olen);
3553
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003554 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003555 rfc.mode != chan->mode)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003556 return -ECONNREFUSED;
3557
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003558 chan->fcs = 0;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003559
3560 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003561 sizeof(rfc), (unsigned long) &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003562 break;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003563
3564 case L2CAP_CONF_EWS:
Mat Martineauc20f8e32012-07-10 05:47:07 -07003565 chan->ack_win = min_t(u16, val, chan->ack_win);
Gustavo F. Padovan3e6b3b92011-11-01 14:06:23 -02003566 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
Mat Martineauc20f8e32012-07-10 05:47:07 -07003567 chan->tx_win);
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003568 break;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003569
3570 case L2CAP_CONF_EFS:
3571 if (olen == sizeof(efs))
3572 memcpy(&efs, (void *)val, olen);
3573
3574 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003575 efs.stype != L2CAP_SERV_NOTRAFIC &&
3576 efs.stype != chan->local_stype)
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003577 return -ECONNREFUSED;
3578
Gustavo Padovan2d792812012-10-06 10:07:01 +01003579 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs),
3580 (unsigned long) &efs);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003581 break;
Andrei Emeltchenkocbabee72012-11-29 17:46:07 +02003582
3583 case L2CAP_CONF_FCS:
3584 if (*result == L2CAP_CONF_PENDING)
3585 if (val == L2CAP_FCS_NONE)
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003586 set_bit(CONF_RECV_NO_FCS,
Andrei Emeltchenkocbabee72012-11-29 17:46:07 +02003587 &chan->conf_state);
3588 break;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003589 }
3590 }
3591
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003592 if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
Gustavo F. Padovan6c2ea7a2010-06-08 20:08:49 -03003593 return -ECONNREFUSED;
3594
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003595 chan->mode = rfc.mode;
Gustavo F. Padovan6c2ea7a2010-06-08 20:08:49 -03003596
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003597 if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003598 switch (rfc.mode) {
3599 case L2CAP_MODE_ERTM:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003600 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3601 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
3602 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Mat Martineauc20f8e32012-07-10 05:47:07 -07003603 if (!test_bit(FLAG_EXT_CTRL, &chan->flags))
3604 chan->ack_win = min_t(u16, chan->ack_win,
3605 rfc.txwin_size);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003606
3607 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3608 chan->local_msdu = le16_to_cpu(efs.msdu);
3609 chan->local_sdu_itime =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003610 le32_to_cpu(efs.sdu_itime);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003611 chan->local_acc_lat = le32_to_cpu(efs.acc_lat);
3612 chan->local_flush_to =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003613 le32_to_cpu(efs.flush_to);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003614 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003615 break;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003616
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003617 case L2CAP_MODE_STREAMING:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003618 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003619 }
3620 }
3621
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003622 req->dcid = cpu_to_le16(chan->dcid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003623 req->flags = cpu_to_le16(0);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003624
3625 return ptr - data;
3626}
3627
Gustavo Padovan2d792812012-10-06 10:07:01 +01003628static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data,
3629 u16 result, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
3631 struct l2cap_conf_rsp *rsp = data;
3632 void *ptr = rsp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003634 BT_DBG("chan %p", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003636 rsp->scid = cpu_to_le16(chan->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003637 rsp->result = cpu_to_le16(result);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07003638 rsp->flags = cpu_to_le16(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
3640 return ptr - data;
3641}
3642
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03003643void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan)
3644{
3645 struct l2cap_le_conn_rsp rsp;
3646 struct l2cap_conn *conn = chan->conn;
3647
3648 BT_DBG("chan %p", chan);
3649
3650 rsp.dcid = cpu_to_le16(chan->scid);
3651 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02003652 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03003653 rsp.credits = cpu_to_le16(chan->rx_credits);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003654 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03003655
3656 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
3657 &rsp);
3658}
3659
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003660void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003661{
3662 struct l2cap_conn_rsp rsp;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003663 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003664 u8 buf[128];
Andrei Emeltchenko439f34a2012-10-31 15:46:28 +02003665 u8 rsp_code;
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003666
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003667 rsp.scid = cpu_to_le16(chan->dcid);
3668 rsp.dcid = cpu_to_le16(chan->scid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003669 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
3670 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Andrei Emeltchenko439f34a2012-10-31 15:46:28 +02003671
3672 if (chan->hs_hcon)
3673 rsp_code = L2CAP_CREATE_CHAN_RSP;
3674 else
3675 rsp_code = L2CAP_CONN_RSP;
3676
3677 BT_DBG("chan %p rsp_code %u", chan, rsp_code);
3678
3679 l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp);
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003680
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003681 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003682 return;
3683
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003684 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003685 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003686 chan->num_conf_req++;
3687}
3688
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003689static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003690{
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003691 int type, olen;
3692 unsigned long val;
Mat Martineauc20f8e32012-07-10 05:47:07 -07003693 /* Use sane default values in case a misbehaving remote device
3694 * did not send an RFC or extended window size option.
3695 */
3696 u16 txwin_ext = chan->ack_win;
3697 struct l2cap_conf_rfc rfc = {
3698 .mode = chan->mode,
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003699 .retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO),
3700 .monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO),
Mat Martineauc20f8e32012-07-10 05:47:07 -07003701 .max_pdu_size = cpu_to_le16(chan->imtu),
3702 .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW),
3703 };
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003704
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003705 BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003706
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003707 if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003708 return;
3709
3710 while (len >= L2CAP_CONF_OPT_SIZE) {
3711 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3712
Mat Martineauc20f8e32012-07-10 05:47:07 -07003713 switch (type) {
3714 case L2CAP_CONF_RFC:
3715 if (olen == sizeof(rfc))
3716 memcpy(&rfc, (void *)val, olen);
Szymon Janc8f321f82012-06-08 11:33:33 +02003717 break;
Mat Martineauc20f8e32012-07-10 05:47:07 -07003718 case L2CAP_CONF_EWS:
3719 txwin_ext = val;
3720 break;
3721 }
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003722 }
3723
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003724 switch (rfc.mode) {
3725 case L2CAP_MODE_ERTM:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003726 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3727 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
Mat Martineauc20f8e32012-07-10 05:47:07 -07003728 chan->mps = le16_to_cpu(rfc.max_pdu_size);
3729 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3730 chan->ack_win = min_t(u16, chan->ack_win, txwin_ext);
3731 else
3732 chan->ack_win = min_t(u16, chan->ack_win,
3733 rfc.txwin_size);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003734 break;
3735 case L2CAP_MODE_STREAMING:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003736 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003737 }
3738}
3739
Gustavo Padovan2d792812012-10-06 10:07:01 +01003740static inline int l2cap_command_rej(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003741 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3742 u8 *data)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003743{
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003744 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003745
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003746 if (cmd_len < sizeof(*rej))
3747 return -EPROTO;
3748
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003749 if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003750 return 0;
3751
3752 if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003753 cmd->ident == conn->info_ident) {
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02003754 cancel_delayed_work(&conn->info_timer);
Marcel Holtmann984947d2009-02-06 23:35:19 +01003755
3756 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01003757 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01003758
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003759 l2cap_conn_start(conn);
3760 }
3761
3762 return 0;
3763}
3764
Mat Martineau17009152012-10-23 15:24:07 -07003765static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3766 struct l2cap_cmd_hdr *cmd,
3767 u8 *data, u8 rsp_code, u8 amp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
3770 struct l2cap_conn_rsp rsp;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003771 struct l2cap_chan *chan = NULL, *pchan;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003772 int result, status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
3774 u16 dcid = 0, scid = __le16_to_cpu(req->scid);
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003775 __le16 psm = req->psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Andrei Emeltchenko097db762012-03-09 14:16:17 +02003777 BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
3779 /* Check if we have socket listening on psm */
Marcel Holtmann6f59b902013-10-13 05:24:01 -07003780 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
Johan Hedbergbf20fd42013-05-14 13:23:13 +03003781 &conn->hcon->dst, ACL_LINK);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003782 if (!pchan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 result = L2CAP_CR_BAD_PSM;
3784 goto sendresp;
3785 }
3786
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003787 mutex_lock(&conn->chan_lock);
Gustavo Padovan8ffb9292013-10-21 14:21:41 -02003788 l2cap_chan_lock(pchan);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00003789
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003790 /* Check if the ACL is secure enough (if not SDP) */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003791 if (psm != cpu_to_le16(L2CAP_PSM_SDP) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003792 !hci_conn_check_link_mode(conn->hcon)) {
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02003793 conn->disc_reason = HCI_ERROR_AUTH_FAILURE;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003794 result = L2CAP_CR_SEC_BLOCK;
3795 goto response;
3796 }
3797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 result = L2CAP_CR_NO_MEM;
3799
Gustavo Padovan2dfa1002012-05-27 22:27:58 -03003800 /* Check if we already have channel with that dcid */
3801 if (__l2cap_get_chan_by_dcid(conn, scid))
3802 goto response;
3803
Gustavo Padovan80b98022012-05-27 22:27:51 -03003804 chan = pchan->ops->new_connection(pchan);
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03003805 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 goto response;
3807
Syam Sidhardhan330b6c12013-08-06 01:59:12 +09003808 /* For certain devices (ex: HID mouse), support for authentication,
3809 * pairing and bonding is optional. For such devices, inorder to avoid
3810 * the ACL alive for too long after L2CAP disconnection, reset the ACL
3811 * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect.
3812 */
3813 conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
3814
Marcel Holtmann7eafc592013-10-13 08:12:47 -07003815 bacpy(&chan->src, &conn->hcon->src);
3816 bacpy(&chan->dst, &conn->hcon->dst);
Marcel Holtmann4f1654e2013-10-13 08:50:41 -07003817 chan->src_type = bdaddr_type(conn->hcon, conn->hcon->src_type);
3818 chan->dst_type = bdaddr_type(conn->hcon, conn->hcon->dst_type);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003819 chan->psm = psm;
3820 chan->dcid = scid;
Mat Martineau17009152012-10-23 15:24:07 -07003821 chan->local_amp_id = amp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003823 __l2cap_chan_add(conn, chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003824
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003825 dcid = chan->scid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
Gustavo Padovan8d836d72013-10-15 19:24:47 -03003827 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03003829 chan->ident = cmd->ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
Marcel Holtmann984947d2009-02-06 23:35:19 +01003831 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
Gustavo F. Padovand45fc422011-11-05 19:54:24 -02003832 if (l2cap_chan_check_security(chan)) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07003833 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003834 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003835 result = L2CAP_CR_PEND;
3836 status = L2CAP_CS_AUTHOR_PEND;
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08003837 chan->ops->defer(chan);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003838 } else {
Mat Martineau17009152012-10-23 15:24:07 -07003839 /* Force pending result for AMP controllers.
3840 * The connection will succeed after the
3841 * physical link is up.
3842 */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003843 if (amp_id == AMP_ID_BREDR) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003844 l2cap_state_change(chan, BT_CONFIG);
Mat Martineau17009152012-10-23 15:24:07 -07003845 result = L2CAP_CR_SUCCESS;
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003846 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003847 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003848 result = L2CAP_CR_PEND;
Mat Martineau17009152012-10-23 15:24:07 -07003849 }
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003850 status = L2CAP_CS_NO_INFO;
3851 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003852 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003853 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003854 result = L2CAP_CR_PEND;
3855 status = L2CAP_CS_AUTHEN_PEND;
3856 }
3857 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003858 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003859 result = L2CAP_CR_PEND;
3860 status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 }
3862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863response:
Gustavo Padovan8ffb9292013-10-21 14:21:41 -02003864 l2cap_chan_unlock(pchan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003865 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
3867sendresp:
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07003868 rsp.scid = cpu_to_le16(scid);
3869 rsp.dcid = cpu_to_le16(dcid);
3870 rsp.result = cpu_to_le16(result);
3871 rsp.status = cpu_to_le16(status);
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003872 l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003873
3874 if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
3875 struct l2cap_info_req info;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003876 info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003877
3878 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
3879 conn->info_ident = l2cap_get_ident(conn);
3880
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08003881 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003882
Gustavo Padovan2d792812012-10-06 10:07:01 +01003883 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
3884 sizeof(info), &info);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003885 }
3886
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003887 if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003888 result == L2CAP_CR_SUCCESS) {
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003889 u8 buf[128];
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003890 set_bit(CONF_REQ_SENT, &chan->conf_state);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003891 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003892 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003893 chan->num_conf_req++;
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003894 }
Mat Martineau17009152012-10-23 15:24:07 -07003895
3896 return chan;
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003897}
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003898
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003899static int l2cap_connect_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003900 struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003901{
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303902 struct hci_dev *hdev = conn->hcon->hdev;
3903 struct hci_conn *hcon = conn->hcon;
3904
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003905 if (cmd_len < sizeof(struct l2cap_conn_req))
3906 return -EPROTO;
3907
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303908 hci_dev_lock(hdev);
3909 if (test_bit(HCI_MGMT, &hdev->dev_flags) &&
3910 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags))
3911 mgmt_device_connected(hdev, &hcon->dst, hcon->type,
3912 hcon->dst_type, 0, NULL, 0,
3913 hcon->dev_class);
3914 hci_dev_unlock(hdev);
3915
Gustavo Padovan300229f2012-10-12 19:40:40 +08003916 l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 return 0;
3918}
3919
Mat Martineau5909cf32012-10-23 15:24:08 -07003920static int l2cap_connect_create_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003921 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3922 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923{
3924 struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
3925 u16 scid, dcid, result, status;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003926 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 u8 req[128];
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003928 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003930 if (cmd_len < sizeof(*rsp))
3931 return -EPROTO;
3932
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 scid = __le16_to_cpu(rsp->scid);
3934 dcid = __le16_to_cpu(rsp->dcid);
3935 result = __le16_to_cpu(rsp->result);
3936 status = __le16_to_cpu(rsp->status);
3937
Andrei Emeltchenko1b009c92012-02-21 12:54:54 +02003938 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 +01003939 dcid, scid, result, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003941 mutex_lock(&conn->chan_lock);
3942
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 if (scid) {
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003944 chan = __l2cap_get_chan_by_scid(conn, scid);
3945 if (!chan) {
Johan Hedberg21870b52013-09-16 13:05:14 +03003946 err = -EBADSLT;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003947 goto unlock;
3948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 } else {
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003950 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
3951 if (!chan) {
Johan Hedberg21870b52013-09-16 13:05:14 +03003952 err = -EBADSLT;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003953 goto unlock;
3954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 }
3956
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003957 err = 0;
3958
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003959 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003960
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 switch (result) {
3962 case L2CAP_CR_SUCCESS:
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03003963 l2cap_state_change(chan, BT_CONFIG);
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03003964 chan->ident = 0;
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003965 chan->dcid = dcid;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003966 clear_bit(CONF_CONNECT_PEND, &chan->conf_state);
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01003967
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003968 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003969 break;
3970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003972 l2cap_build_conf_req(chan, req), req);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003973 chan->num_conf_req++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 break;
3975
3976 case L2CAP_CR_PEND:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003977 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 break;
3979
3980 default:
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003981 l2cap_chan_del(chan, ECONNREFUSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 break;
3983 }
3984
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003985 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003986
3987unlock:
3988 mutex_unlock(&conn->chan_lock);
3989
3990 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991}
3992
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003993static inline void set_default_fcs(struct l2cap_chan *chan)
Mat Martineau8c462b62010-08-24 15:35:42 -07003994{
3995 /* FCS is enabled only in ERTM or streaming mode, if one or both
3996 * sides request it.
3997 */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003998 if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003999 chan->fcs = L2CAP_FCS_NONE;
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02004000 else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state))
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004001 chan->fcs = L2CAP_FCS_CRC16;
Mat Martineau8c462b62010-08-24 15:35:42 -07004002}
4003
Andrei Emeltchenko29d8a592012-09-21 12:30:05 +03004004static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data,
4005 u8 ident, u16 flags)
4006{
4007 struct l2cap_conn *conn = chan->conn;
4008
4009 BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident,
4010 flags);
4011
4012 clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
4013 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
4014
4015 l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP,
4016 l2cap_build_conf_rsp(chan, data,
4017 L2CAP_CONF_SUCCESS, flags), data);
4018}
4019
Johan Hedberg662d6522013-10-16 11:20:47 +03004020static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident,
4021 u16 scid, u16 dcid)
4022{
4023 struct l2cap_cmd_rej_cid rej;
4024
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004025 rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID);
Johan Hedberg662d6522013-10-16 11:20:47 +03004026 rej.scid = __cpu_to_le16(scid);
4027 rej.dcid = __cpu_to_le16(dcid);
4028
4029 l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
4030}
4031
Gustavo Padovan2d792812012-10-06 10:07:01 +01004032static inline int l2cap_config_req(struct l2cap_conn *conn,
4033 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4034 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035{
4036 struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
4037 u16 dcid, flags;
4038 u8 rsp[64];
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004039 struct l2cap_chan *chan;
Mat Martineau3c588192012-04-11 10:48:42 -07004040 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004042 if (cmd_len < sizeof(*req))
4043 return -EPROTO;
4044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 dcid = __le16_to_cpu(req->dcid);
4046 flags = __le16_to_cpu(req->flags);
4047
4048 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
4049
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03004050 chan = l2cap_get_chan_by_scid(conn, dcid);
Johan Hedberg662d6522013-10-16 11:20:47 +03004051 if (!chan) {
4052 cmd_reject_invalid_cid(conn, cmd->ident, dcid, 0);
4053 return 0;
4054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
David S. Miller033b1142011-07-21 13:38:42 -07004056 if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) {
Johan Hedberg662d6522013-10-16 11:20:47 +03004057 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4058 chan->dcid);
Marcel Holtmann354f60a2006-11-18 22:15:20 +01004059 goto unlock;
Gustavo F. Padovandf6bd742010-06-14 02:26:15 -03004060 }
Marcel Holtmann354f60a2006-11-18 22:15:20 +01004061
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004062 /* Reject if config buffer is too small. */
Al Viro88219a02007-07-29 00:17:25 -07004063 len = cmd_len - sizeof(*req);
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004064 if (chan->conf_len + len > sizeof(chan->conf_req)) {
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004065 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004066 l2cap_build_conf_rsp(chan, rsp,
4067 L2CAP_CONF_REJECT, flags), rsp);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004068 goto unlock;
4069 }
4070
4071 /* Store config. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004072 memcpy(chan->conf_req + chan->conf_len, req->data, len);
4073 chan->conf_len += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03004075 if (flags & L2CAP_CONF_FLAG_CONTINUATION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 /* Incomplete config. Send empty response. */
4077 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004078 l2cap_build_conf_rsp(chan, rsp,
4079 L2CAP_CONF_SUCCESS, flags), rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 goto unlock;
4081 }
4082
4083 /* Complete config. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004084 len = l2cap_parse_conf_req(chan, rsp);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004085 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004086 l2cap_send_disconn_req(chan, ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 goto unlock;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
Mat Martineau1500109b2012-10-23 15:24:15 -07004090 chan->ident = cmd->ident;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004091 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004092 chan->num_conf_rsp++;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004093
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004094 /* Reset config buffer. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004095 chan->conf_len = 0;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004096
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004097 if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state))
Marcel Holtmann876d9482007-10-20 13:35:42 +02004098 goto unlock;
4099
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004100 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004101 set_default_fcs(chan);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004102
Mat Martineau105bdf92012-04-27 16:50:48 -07004103 if (chan->mode == L2CAP_MODE_ERTM ||
4104 chan->mode == L2CAP_MODE_STREAMING)
Mat Martineau3c588192012-04-11 10:48:42 -07004105 err = l2cap_ertm_init(chan);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03004106
Mat Martineau3c588192012-04-11 10:48:42 -07004107 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004108 l2cap_send_disconn_req(chan, -err);
Mat Martineau3c588192012-04-11 10:48:42 -07004109 else
4110 l2cap_chan_ready(chan);
4111
Marcel Holtmann876d9482007-10-20 13:35:42 +02004112 goto unlock;
4113 }
4114
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004115 if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +02004116 u8 buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004118 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004119 chan->num_conf_req++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 }
4121
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004122 /* Got Conf Rsp PENDING from remote side and asume we sent
4123 Conf Rsp PENDING in the code above */
4124 if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) &&
Andrei Emeltchenko29d8a592012-09-21 12:30:05 +03004125 test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004126
4127 /* check compatibility */
4128
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004129 /* Send rsp for BR/EDR channel */
Andrei Emeltchenkof351bc72012-10-31 15:46:35 +02004130 if (!chan->hs_hcon)
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004131 l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags);
4132 else
4133 chan->ident = cmd->ident;
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004134 }
4135
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136unlock:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004137 l2cap_chan_unlock(chan);
Mat Martineau3c588192012-04-11 10:48:42 -07004138 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139}
4140
Gustavo Padovan2d792812012-10-06 10:07:01 +01004141static inline int l2cap_config_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004142 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4143 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
4145 struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
4146 u16 scid, flags, result;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004147 struct l2cap_chan *chan;
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004148 int len = cmd_len - sizeof(*rsp);
Mat Martineau3c588192012-04-11 10:48:42 -07004149 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004151 if (cmd_len < sizeof(*rsp))
4152 return -EPROTO;
4153
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 scid = __le16_to_cpu(rsp->scid);
4155 flags = __le16_to_cpu(rsp->flags);
4156 result = __le16_to_cpu(rsp->result);
4157
Andrei Emeltchenko61386cb2012-03-12 12:13:07 +02004158 BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags,
4159 result, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03004161 chan = l2cap_get_chan_by_scid(conn, scid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004162 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 return 0;
4164
4165 switch (result) {
4166 case L2CAP_CONF_SUCCESS:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004167 l2cap_conf_rfc_get(chan, rsp->data, len);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004168 clear_bit(CONF_REM_CONF_PEND, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 break;
4170
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004171 case L2CAP_CONF_PENDING:
4172 set_bit(CONF_REM_CONF_PEND, &chan->conf_state);
4173
4174 if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
4175 char buf[64];
4176
4177 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004178 buf, &result);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004179 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004180 l2cap_send_disconn_req(chan, ECONNRESET);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004181 goto done;
4182 }
4183
Andrei Emeltchenkof351bc72012-10-31 15:46:35 +02004184 if (!chan->hs_hcon) {
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004185 l2cap_send_efs_conf_rsp(chan, buf, cmd->ident,
4186 0);
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +02004187 } else {
4188 if (l2cap_check_efs(chan)) {
4189 amp_create_logical_link(chan);
4190 chan->ident = cmd->ident;
4191 }
4192 }
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004193 }
4194 goto done;
4195
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 case L2CAP_CONF_UNACCEPT:
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004197 if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004198 char req[64];
4199
Andrei Emeltchenkoc2c77ec2010-03-19 10:26:28 +02004200 if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004201 l2cap_send_disconn_req(chan, ECONNRESET);
Andrei Emeltchenkoc2c77ec2010-03-19 10:26:28 +02004202 goto done;
4203 }
4204
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004205 /* throw out any old stored conf requests */
4206 result = L2CAP_CONF_SUCCESS;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03004207 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004208 req, &result);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004209 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004210 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004211 goto done;
4212 }
4213
4214 l2cap_send_cmd(conn, l2cap_get_ident(conn),
Gustavo Padovan2d792812012-10-06 10:07:01 +01004215 L2CAP_CONF_REQ, len, req);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004216 chan->num_conf_req++;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004217 if (result != L2CAP_CONF_SUCCESS)
4218 goto done;
4219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 }
4221
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09004222 default:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004223 l2cap_chan_set_err(chan, ECONNRESET);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +02004224
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08004225 __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004226 l2cap_send_disconn_req(chan, ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 goto done;
4228 }
4229
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03004230 if (flags & L2CAP_CONF_FLAG_CONTINUATION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 goto done;
4232
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004233 set_bit(CONF_INPUT_DONE, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004235 if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) {
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004236 set_default_fcs(chan);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004237
Mat Martineau105bdf92012-04-27 16:50:48 -07004238 if (chan->mode == L2CAP_MODE_ERTM ||
4239 chan->mode == L2CAP_MODE_STREAMING)
Mat Martineau3c588192012-04-11 10:48:42 -07004240 err = l2cap_ertm_init(chan);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03004241
Mat Martineau3c588192012-04-11 10:48:42 -07004242 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004243 l2cap_send_disconn_req(chan, -err);
Mat Martineau3c588192012-04-11 10:48:42 -07004244 else
4245 l2cap_chan_ready(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 }
4247
4248done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004249 l2cap_chan_unlock(chan);
Mat Martineau3c588192012-04-11 10:48:42 -07004250 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251}
4252
Gustavo Padovan2d792812012-10-06 10:07:01 +01004253static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004254 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4255 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256{
4257 struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
4258 struct l2cap_disconn_rsp rsp;
4259 u16 dcid, scid;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004260 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004262 if (cmd_len != sizeof(*req))
4263 return -EPROTO;
4264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 scid = __le16_to_cpu(req->scid);
4266 dcid = __le16_to_cpu(req->dcid);
4267
4268 BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
4269
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004270 mutex_lock(&conn->chan_lock);
4271
4272 chan = __l2cap_get_chan_by_scid(conn, dcid);
4273 if (!chan) {
4274 mutex_unlock(&conn->chan_lock);
Johan Hedberg662d6522013-10-16 11:20:47 +03004275 cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid);
4276 return 0;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004279 l2cap_chan_lock(chan);
4280
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03004281 rsp.dcid = cpu_to_le16(chan->scid);
4282 rsp.scid = cpu_to_le16(chan->dcid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
4284
Gustavo Padovan5ec1bbe2013-10-15 19:24:48 -03004285 chan->ops->set_shutdown(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Mat Martineau61d6ef32012-04-27 16:50:50 -07004287 l2cap_chan_hold(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004288 l2cap_chan_del(chan, ECONNRESET);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004289
4290 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
Gustavo Padovan80b98022012-05-27 22:27:51 -03004292 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07004293 l2cap_chan_put(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004294
4295 mutex_unlock(&conn->chan_lock);
4296
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 return 0;
4298}
4299
Gustavo Padovan2d792812012-10-06 10:07:01 +01004300static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004301 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4302 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303{
4304 struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
4305 u16 dcid, scid;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004306 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004308 if (cmd_len != sizeof(*rsp))
4309 return -EPROTO;
4310
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 scid = __le16_to_cpu(rsp->scid);
4312 dcid = __le16_to_cpu(rsp->dcid);
4313
4314 BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
4315
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004316 mutex_lock(&conn->chan_lock);
4317
4318 chan = __l2cap_get_chan_by_scid(conn, scid);
4319 if (!chan) {
4320 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return 0;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004324 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004325
Mat Martineau61d6ef32012-04-27 16:50:50 -07004326 l2cap_chan_hold(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004327 l2cap_chan_del(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004328
4329 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Gustavo Padovan80b98022012-05-27 22:27:51 -03004331 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07004332 l2cap_chan_put(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004333
4334 mutex_unlock(&conn->chan_lock);
4335
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 return 0;
4337}
4338
Gustavo Padovan2d792812012-10-06 10:07:01 +01004339static inline int l2cap_information_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004340 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4341 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
4343 struct l2cap_info_req *req = (struct l2cap_info_req *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 u16 type;
4345
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004346 if (cmd_len != sizeof(*req))
4347 return -EPROTO;
4348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 type = __le16_to_cpu(req->type);
4350
4351 BT_DBG("type 0x%4.4x", type);
4352
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004353 if (type == L2CAP_IT_FEAT_MASK) {
4354 u8 buf[8];
Marcel Holtmann44dd46d2009-05-02 19:09:01 -07004355 u32 feat_mask = l2cap_feat_mask;
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004356 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004357 rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
4358 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03004359 if (!disable_ertm)
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004360 feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
Gustavo Padovan2d792812012-10-06 10:07:01 +01004361 | L2CAP_FEAT_FCS;
Marcel Holtmann848566b2013-10-01 22:59:22 -07004362 if (conn->hs_enabled)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03004363 feat_mask |= L2CAP_FEAT_EXT_FLOW
Gustavo Padovan2d792812012-10-06 10:07:01 +01004364 | L2CAP_FEAT_EXT_WINDOW;
Andrei Emeltchenkoa5fd6f32011-09-16 16:26:32 +03004365
Gustavo F. Padovan1b7bf4e2009-08-24 00:45:20 -03004366 put_unaligned_le32(feat_mask, rsp->data);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004367 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4368 buf);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004369 } else if (type == L2CAP_IT_FIXED_CHAN) {
4370 u8 buf[12];
4371 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
Mat Martineau50a147c2011-11-02 16:18:34 -07004372
Marcel Holtmann848566b2013-10-01 22:59:22 -07004373 if (conn->hs_enabled)
Mat Martineau50a147c2011-11-02 16:18:34 -07004374 l2cap_fixed_chan[0] |= L2CAP_FC_A2MP;
4375 else
4376 l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP;
4377
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004378 rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
4379 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
Andrei Emeltchenkoc6337ea2011-10-20 17:02:44 +03004380 memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan));
Gustavo Padovan2d792812012-10-06 10:07:01 +01004381 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4382 buf);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004383 } else {
4384 struct l2cap_info_rsp rsp;
4385 rsp.type = cpu_to_le16(type);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004386 rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004387 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp),
4388 &rsp);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
4391 return 0;
4392}
4393
Gustavo Padovan2d792812012-10-06 10:07:01 +01004394static inline int l2cap_information_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004395 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4396 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397{
4398 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
4399 u16 type, result;
4400
Jaganath Kanakkassery3f6fa3d2013-06-21 19:55:11 +05304401 if (cmd_len < sizeof(*rsp))
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004402 return -EPROTO;
4403
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 type = __le16_to_cpu(rsp->type);
4405 result = __le16_to_cpu(rsp->result);
4406
4407 BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
4408
Andrei Emeltchenkoe90165b2011-03-25 11:31:41 +02004409 /* L2CAP Info req/rsp are unbound to channels, add extra checks */
4410 if (cmd->ident != conn->info_ident ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01004411 conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
Andrei Emeltchenkoe90165b2011-03-25 11:31:41 +02004412 return 0;
4413
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02004414 cancel_delayed_work(&conn->info_timer);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004415
Ville Tervoadb08ed2010-08-04 09:43:33 +03004416 if (result != L2CAP_IR_SUCCESS) {
4417 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4418 conn->info_ident = 0;
4419
4420 l2cap_conn_start(conn);
4421
4422 return 0;
4423 }
4424
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004425 switch (type) {
4426 case L2CAP_IT_FEAT_MASK:
Harvey Harrison83985312008-05-02 16:25:46 -07004427 conn->feat_mask = get_unaligned_le32(rsp->data);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004428
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -07004429 if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004430 struct l2cap_info_req req;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004431 req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004432
4433 conn->info_ident = l2cap_get_ident(conn);
4434
4435 l2cap_send_cmd(conn, conn->info_ident,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004436 L2CAP_INFO_REQ, sizeof(req), &req);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004437 } else {
4438 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4439 conn->info_ident = 0;
4440
4441 l2cap_conn_start(conn);
4442 }
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004443 break;
4444
4445 case L2CAP_IT_FIXED_CHAN:
4446 conn->fixed_chan_mask = rsp->data[0];
Marcel Holtmann984947d2009-02-06 23:35:19 +01004447 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004448 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01004449
4450 l2cap_conn_start(conn);
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004451 break;
Marcel Holtmann984947d2009-02-06 23:35:19 +01004452 }
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 return 0;
4455}
4456
Mat Martineau17009152012-10-23 15:24:07 -07004457static int l2cap_create_channel_req(struct l2cap_conn *conn,
4458 struct l2cap_cmd_hdr *cmd,
4459 u16 cmd_len, void *data)
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004460{
4461 struct l2cap_create_chan_req *req = data;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004462 struct l2cap_create_chan_rsp rsp;
Mat Martineau17009152012-10-23 15:24:07 -07004463 struct l2cap_chan *chan;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004464 struct hci_dev *hdev;
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004465 u16 psm, scid;
4466
4467 if (cmd_len != sizeof(*req))
4468 return -EPROTO;
4469
Marcel Holtmann848566b2013-10-01 22:59:22 -07004470 if (!conn->hs_enabled)
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004471 return -EINVAL;
4472
4473 psm = le16_to_cpu(req->psm);
4474 scid = le16_to_cpu(req->scid);
4475
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004476 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 -07004477
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004478 /* For controller id 0 make BR/EDR connection */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004479 if (req->amp_id == AMP_ID_BREDR) {
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004480 l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4481 req->amp_id);
4482 return 0;
4483 }
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004484
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004485 /* Validate AMP controller id */
4486 hdev = hci_dev_get(req->amp_id);
4487 if (!hdev)
4488 goto error;
Mat Martineau17009152012-10-23 15:24:07 -07004489
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004490 if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) {
Mat Martineau17009152012-10-23 15:24:07 -07004491 hci_dev_put(hdev);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004492 goto error;
Mat Martineau17009152012-10-23 15:24:07 -07004493 }
4494
4495 chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4496 req->amp_id);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004497 if (chan) {
4498 struct amp_mgr *mgr = conn->hcon->amp_mgr;
4499 struct hci_conn *hs_hcon;
4500
Marcel Holtmann98e0f7e2013-10-13 02:23:40 -07004501 hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
4502 &conn->hcon->dst);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004503 if (!hs_hcon) {
4504 hci_dev_put(hdev);
Johan Hedberg662d6522013-10-16 11:20:47 +03004505 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4506 chan->dcid);
4507 return 0;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004508 }
4509
4510 BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon);
4511
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004512 mgr->bredr_chan = chan;
4513 chan->hs_hcon = hs_hcon;
Andrei Emeltchenkofd45bf42012-11-20 17:16:22 +02004514 chan->fcs = L2CAP_FCS_NONE;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004515 conn->mtu = hdev->block_mtu;
4516 }
4517
4518 hci_dev_put(hdev);
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004519
4520 return 0;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004521
4522error:
4523 rsp.dcid = 0;
4524 rsp.scid = cpu_to_le16(scid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004525 rsp.result = cpu_to_le16(L2CAP_CR_BAD_AMP);
4526 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004527
4528 l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
4529 sizeof(rsp), &rsp);
4530
Johan Hedbergdc280802013-09-16 13:05:13 +03004531 return 0;
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004532}
4533
Mat Martineau8eb200b2012-10-23 15:24:17 -07004534static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id)
4535{
4536 struct l2cap_move_chan_req req;
4537 u8 ident;
4538
4539 BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id);
4540
4541 ident = l2cap_get_ident(chan->conn);
4542 chan->ident = ident;
4543
4544 req.icid = cpu_to_le16(chan->scid);
4545 req.dest_amp_id = dest_amp_id;
4546
4547 l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req),
4548 &req);
4549
4550 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4551}
4552
Mat Martineau1500109b2012-10-23 15:24:15 -07004553static void l2cap_send_move_chan_rsp(struct l2cap_chan *chan, u16 result)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004554{
4555 struct l2cap_move_chan_rsp rsp;
4556
Mat Martineau1500109b2012-10-23 15:24:15 -07004557 BT_DBG("chan %p, result 0x%4.4x", chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004558
Mat Martineau1500109b2012-10-23 15:24:15 -07004559 rsp.icid = cpu_to_le16(chan->dcid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004560 rsp.result = cpu_to_le16(result);
4561
Mat Martineau1500109b2012-10-23 15:24:15 -07004562 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP,
4563 sizeof(rsp), &rsp);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004564}
4565
Mat Martineau5b155ef2012-10-23 15:24:14 -07004566static void l2cap_send_move_chan_cfm(struct l2cap_chan *chan, u16 result)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004567{
4568 struct l2cap_move_chan_cfm cfm;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004569
Mat Martineau5b155ef2012-10-23 15:24:14 -07004570 BT_DBG("chan %p, result 0x%4.4x", chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004571
Mat Martineau5b155ef2012-10-23 15:24:14 -07004572 chan->ident = l2cap_get_ident(chan->conn);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004573
Mat Martineau5b155ef2012-10-23 15:24:14 -07004574 cfm.icid = cpu_to_le16(chan->scid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004575 cfm.result = cpu_to_le16(result);
4576
Mat Martineau5b155ef2012-10-23 15:24:14 -07004577 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM,
4578 sizeof(cfm), &cfm);
4579
4580 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4581}
4582
4583static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid)
4584{
4585 struct l2cap_move_chan_cfm cfm;
4586
4587 BT_DBG("conn %p, icid 0x%4.4x", conn, icid);
4588
4589 cfm.icid = cpu_to_le16(icid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004590 cfm.result = cpu_to_le16(L2CAP_MC_UNCONFIRMED);
Mat Martineau5b155ef2012-10-23 15:24:14 -07004591
4592 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM,
4593 sizeof(cfm), &cfm);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004594}
4595
4596static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004597 u16 icid)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004598{
4599 struct l2cap_move_chan_cfm_rsp rsp;
4600
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004601 BT_DBG("icid 0x%4.4x", icid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004602
4603 rsp.icid = cpu_to_le16(icid);
4604 l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp);
4605}
4606
Mat Martineau5f3847a2012-10-23 15:24:12 -07004607static void __release_logical_link(struct l2cap_chan *chan)
4608{
4609 chan->hs_hchan = NULL;
4610 chan->hs_hcon = NULL;
4611
4612 /* Placeholder - release the logical link */
4613}
4614
Mat Martineau1500109b2012-10-23 15:24:15 -07004615static void l2cap_logical_fail(struct l2cap_chan *chan)
4616{
4617 /* Logical link setup failed */
4618 if (chan->state != BT_CONNECTED) {
4619 /* Create channel failure, disconnect */
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004620 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineau1500109b2012-10-23 15:24:15 -07004621 return;
4622 }
4623
4624 switch (chan->move_role) {
4625 case L2CAP_MOVE_ROLE_RESPONDER:
4626 l2cap_move_done(chan);
4627 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP);
4628 break;
4629 case L2CAP_MOVE_ROLE_INITIATOR:
4630 if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP ||
4631 chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) {
4632 /* Remote has only sent pending or
4633 * success responses, clean up
4634 */
4635 l2cap_move_done(chan);
4636 }
4637
4638 /* Other amp move states imply that the move
4639 * has already aborted
4640 */
4641 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
4642 break;
4643 }
4644}
4645
4646static void l2cap_logical_finish_create(struct l2cap_chan *chan,
4647 struct hci_chan *hchan)
4648{
4649 struct l2cap_conf_rsp rsp;
Mat Martineau1500109b2012-10-23 15:24:15 -07004650
Andrei Emeltchenko336178a2012-10-31 15:46:27 +02004651 chan->hs_hchan = hchan;
Mat Martineau1500109b2012-10-23 15:24:15 -07004652 chan->hs_hcon->l2cap_data = chan->conn;
4653
Andrei Emeltchenko35ba9562012-10-25 15:20:43 +03004654 l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0);
Mat Martineau1500109b2012-10-23 15:24:15 -07004655
4656 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
Andrei Emeltchenkofe79c6f2012-10-25 15:20:42 +03004657 int err;
Mat Martineau1500109b2012-10-23 15:24:15 -07004658
4659 set_default_fcs(chan);
4660
4661 err = l2cap_ertm_init(chan);
4662 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004663 l2cap_send_disconn_req(chan, -err);
Mat Martineau1500109b2012-10-23 15:24:15 -07004664 else
4665 l2cap_chan_ready(chan);
4666 }
4667}
4668
4669static void l2cap_logical_finish_move(struct l2cap_chan *chan,
4670 struct hci_chan *hchan)
4671{
4672 chan->hs_hcon = hchan->conn;
4673 chan->hs_hcon->l2cap_data = chan->conn;
4674
4675 BT_DBG("move_state %d", chan->move_state);
4676
4677 switch (chan->move_state) {
4678 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
4679 /* Move confirm will be sent after a success
4680 * response is received
4681 */
4682 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4683 break;
4684 case L2CAP_MOVE_WAIT_LOGICAL_CFM:
4685 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4686 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4687 } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
4688 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
4689 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
4690 } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
4691 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4692 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
4693 }
4694 break;
4695 default:
4696 /* Move was not in expected state, free the channel */
4697 __release_logical_link(chan);
4698
4699 chan->move_state = L2CAP_MOVE_STABLE;
4700 }
4701}
4702
4703/* Call with chan locked */
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03004704void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan,
4705 u8 status)
Mat Martineau5b155ef2012-10-23 15:24:14 -07004706{
Mat Martineau1500109b2012-10-23 15:24:15 -07004707 BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status);
4708
4709 if (status) {
4710 l2cap_logical_fail(chan);
4711 __release_logical_link(chan);
4712 return;
4713 }
4714
4715 if (chan->state != BT_CONNECTED) {
4716 /* Ignore logical link if channel is on BR/EDR */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004717 if (chan->local_amp_id != AMP_ID_BREDR)
Mat Martineau1500109b2012-10-23 15:24:15 -07004718 l2cap_logical_finish_create(chan, hchan);
4719 } else {
4720 l2cap_logical_finish_move(chan, hchan);
4721 }
Mat Martineau5b155ef2012-10-23 15:24:14 -07004722}
4723
Mat Martineau3f7a56c2012-10-23 15:24:23 -07004724void l2cap_move_start(struct l2cap_chan *chan)
4725{
4726 BT_DBG("chan %p", chan);
4727
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004728 if (chan->local_amp_id == AMP_ID_BREDR) {
Mat Martineau3f7a56c2012-10-23 15:24:23 -07004729 if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED)
4730 return;
4731 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
4732 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
4733 /* Placeholder - start physical link setup */
4734 } else {
4735 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
4736 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4737 chan->move_id = 0;
4738 l2cap_move_setup(chan);
4739 l2cap_send_move_chan_req(chan, 0);
4740 }
4741}
4742
Mat Martineau8eb200b2012-10-23 15:24:17 -07004743static void l2cap_do_create(struct l2cap_chan *chan, int result,
4744 u8 local_amp_id, u8 remote_amp_id)
4745{
Andrei Emeltchenko62748ca2012-11-20 17:16:19 +02004746 BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state),
4747 local_amp_id, remote_amp_id);
4748
Andrei Emeltchenko12d6cc62012-11-14 17:39:31 +02004749 chan->fcs = L2CAP_FCS_NONE;
4750
Andrei Emeltchenko62748ca2012-11-20 17:16:19 +02004751 /* Outgoing channel on AMP */
4752 if (chan->state == BT_CONNECT) {
4753 if (result == L2CAP_CR_SUCCESS) {
4754 chan->local_amp_id = local_amp_id;
4755 l2cap_send_create_chan_req(chan, remote_amp_id);
4756 } else {
4757 /* Revert to BR/EDR connect */
4758 l2cap_send_conn_req(chan);
4759 }
4760
4761 return;
4762 }
4763
4764 /* Incoming channel on AMP */
4765 if (__l2cap_no_conn_pending(chan)) {
Mat Martineau8eb200b2012-10-23 15:24:17 -07004766 struct l2cap_conn_rsp rsp;
4767 char buf[128];
4768 rsp.scid = cpu_to_le16(chan->dcid);
4769 rsp.dcid = cpu_to_le16(chan->scid);
4770
Mat Martineau8eb200b2012-10-23 15:24:17 -07004771 if (result == L2CAP_CR_SUCCESS) {
4772 /* Send successful response */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004773 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
4774 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004775 } else {
4776 /* Send negative response */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004777 rsp.result = cpu_to_le16(L2CAP_CR_NO_MEM);
4778 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004779 }
4780
4781 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP,
4782 sizeof(rsp), &rsp);
4783
4784 if (result == L2CAP_CR_SUCCESS) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02004785 l2cap_state_change(chan, BT_CONFIG);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004786 set_bit(CONF_REQ_SENT, &chan->conf_state);
4787 l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn),
4788 L2CAP_CONF_REQ,
4789 l2cap_build_conf_req(chan, buf), buf);
4790 chan->num_conf_req++;
4791 }
Mat Martineau8eb200b2012-10-23 15:24:17 -07004792 }
4793}
4794
4795static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id,
4796 u8 remote_amp_id)
4797{
4798 l2cap_move_setup(chan);
4799 chan->move_id = local_amp_id;
4800 chan->move_state = L2CAP_MOVE_WAIT_RSP;
4801
4802 l2cap_send_move_chan_req(chan, remote_amp_id);
4803}
4804
4805static void l2cap_do_move_respond(struct l2cap_chan *chan, int result)
4806{
4807 struct hci_chan *hchan = NULL;
4808
4809 /* Placeholder - get hci_chan for logical link */
4810
4811 if (hchan) {
4812 if (hchan->state == BT_CONNECTED) {
4813 /* Logical link is ready to go */
4814 chan->hs_hcon = hchan->conn;
4815 chan->hs_hcon->l2cap_data = chan->conn;
4816 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4817 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
4818
4819 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
4820 } else {
4821 /* Wait for logical link to be ready */
4822 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
4823 }
4824 } else {
4825 /* Logical link not available */
4826 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED);
4827 }
4828}
4829
4830static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result)
4831{
4832 if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
4833 u8 rsp_result;
4834 if (result == -EINVAL)
4835 rsp_result = L2CAP_MR_BAD_ID;
4836 else
4837 rsp_result = L2CAP_MR_NOT_ALLOWED;
4838
4839 l2cap_send_move_chan_rsp(chan, rsp_result);
4840 }
4841
4842 chan->move_role = L2CAP_MOVE_ROLE_NONE;
4843 chan->move_state = L2CAP_MOVE_STABLE;
4844
4845 /* Restart data transmission */
4846 l2cap_ertm_send(chan);
4847}
4848
Andrei Emeltchenkoa514b172012-11-14 17:39:30 +02004849/* Invoke with locked chan */
4850void __l2cap_physical_cfm(struct l2cap_chan *chan, int result)
Mat Martineau8eb200b2012-10-23 15:24:17 -07004851{
Andrei Emeltchenko770bfef2012-10-31 15:46:29 +02004852 u8 local_amp_id = chan->local_amp_id;
Andrei Emeltchenkofffadc02012-11-01 15:37:03 +02004853 u8 remote_amp_id = chan->remote_amp_id;
Andrei Emeltchenko770bfef2012-10-31 15:46:29 +02004854
Mat Martineau8eb200b2012-10-23 15:24:17 -07004855 BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d",
4856 chan, result, local_amp_id, remote_amp_id);
4857
Mat Martineau8eb200b2012-10-23 15:24:17 -07004858 if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) {
4859 l2cap_chan_unlock(chan);
4860 return;
4861 }
4862
4863 if (chan->state != BT_CONNECTED) {
4864 l2cap_do_create(chan, result, local_amp_id, remote_amp_id);
4865 } else if (result != L2CAP_MR_SUCCESS) {
4866 l2cap_do_move_cancel(chan, result);
4867 } else {
4868 switch (chan->move_role) {
4869 case L2CAP_MOVE_ROLE_INITIATOR:
4870 l2cap_do_move_initiate(chan, local_amp_id,
4871 remote_amp_id);
4872 break;
4873 case L2CAP_MOVE_ROLE_RESPONDER:
4874 l2cap_do_move_respond(chan, result);
4875 break;
4876 default:
4877 l2cap_do_move_cancel(chan, result);
4878 break;
4879 }
4880 }
Mat Martineau8eb200b2012-10-23 15:24:17 -07004881}
4882
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004883static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004884 struct l2cap_cmd_hdr *cmd,
4885 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004886{
4887 struct l2cap_move_chan_req *req = data;
Mat Martineau1500109b2012-10-23 15:24:15 -07004888 struct l2cap_move_chan_rsp rsp;
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004889 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004890 u16 icid = 0;
4891 u16 result = L2CAP_MR_NOT_ALLOWED;
4892
4893 if (cmd_len != sizeof(*req))
4894 return -EPROTO;
4895
4896 icid = le16_to_cpu(req->icid);
4897
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004898 BT_DBG("icid 0x%4.4x, dest_amp_id %d", icid, req->dest_amp_id);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004899
Marcel Holtmann848566b2013-10-01 22:59:22 -07004900 if (!conn->hs_enabled)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004901 return -EINVAL;
4902
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004903 chan = l2cap_get_chan_by_dcid(conn, icid);
4904 if (!chan) {
Mat Martineau1500109b2012-10-23 15:24:15 -07004905 rsp.icid = cpu_to_le16(icid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004906 rsp.result = cpu_to_le16(L2CAP_MR_NOT_ALLOWED);
Mat Martineau1500109b2012-10-23 15:24:15 -07004907 l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP,
4908 sizeof(rsp), &rsp);
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004909 return 0;
4910 }
4911
Mat Martineau1500109b2012-10-23 15:24:15 -07004912 chan->ident = cmd->ident;
4913
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004914 if (chan->scid < L2CAP_CID_DYN_START ||
4915 chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY ||
4916 (chan->mode != L2CAP_MODE_ERTM &&
4917 chan->mode != L2CAP_MODE_STREAMING)) {
4918 result = L2CAP_MR_NOT_ALLOWED;
4919 goto send_move_response;
4920 }
4921
4922 if (chan->local_amp_id == req->dest_amp_id) {
4923 result = L2CAP_MR_SAME_ID;
4924 goto send_move_response;
4925 }
4926
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004927 if (req->dest_amp_id != AMP_ID_BREDR) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004928 struct hci_dev *hdev;
4929 hdev = hci_dev_get(req->dest_amp_id);
4930 if (!hdev || hdev->dev_type != HCI_AMP ||
4931 !test_bit(HCI_UP, &hdev->flags)) {
4932 if (hdev)
4933 hci_dev_put(hdev);
4934
4935 result = L2CAP_MR_BAD_ID;
4936 goto send_move_response;
4937 }
4938 hci_dev_put(hdev);
4939 }
4940
4941 /* Detect a move collision. Only send a collision response
4942 * if this side has "lost", otherwise proceed with the move.
4943 * The winner has the larger bd_addr.
4944 */
4945 if ((__chan_is_moving(chan) ||
4946 chan->move_role != L2CAP_MOVE_ROLE_NONE) &&
Marcel Holtmann6f59b902013-10-13 05:24:01 -07004947 bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004948 result = L2CAP_MR_COLLISION;
4949 goto send_move_response;
4950 }
4951
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004952 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
4953 l2cap_move_setup(chan);
4954 chan->move_id = req->dest_amp_id;
4955 icid = chan->dcid;
4956
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004957 if (req->dest_amp_id == AMP_ID_BREDR) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004958 /* Moving to BR/EDR */
4959 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4960 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4961 result = L2CAP_MR_PEND;
4962 } else {
4963 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4964 result = L2CAP_MR_SUCCESS;
4965 }
4966 } else {
4967 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
4968 /* Placeholder - uncomment when amp functions are available */
4969 /*amp_accept_physical(chan, req->dest_amp_id);*/
4970 result = L2CAP_MR_PEND;
4971 }
4972
4973send_move_response:
Mat Martineau1500109b2012-10-23 15:24:15 -07004974 l2cap_send_move_chan_rsp(chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004975
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004976 l2cap_chan_unlock(chan);
4977
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004978 return 0;
4979}
4980
Mat Martineau5b155ef2012-10-23 15:24:14 -07004981static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result)
4982{
4983 struct l2cap_chan *chan;
4984 struct hci_chan *hchan = NULL;
4985
4986 chan = l2cap_get_chan_by_scid(conn, icid);
4987 if (!chan) {
4988 l2cap_send_move_chan_cfm_icid(conn, icid);
4989 return;
4990 }
4991
4992 __clear_chan_timer(chan);
4993 if (result == L2CAP_MR_PEND)
4994 __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT);
4995
4996 switch (chan->move_state) {
4997 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
4998 /* Move confirm will be sent when logical link
4999 * is complete.
5000 */
5001 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
5002 break;
5003 case L2CAP_MOVE_WAIT_RSP_SUCCESS:
5004 if (result == L2CAP_MR_PEND) {
5005 break;
5006 } else if (test_bit(CONN_LOCAL_BUSY,
5007 &chan->conn_state)) {
5008 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
5009 } else {
5010 /* Logical link is up or moving to BR/EDR,
5011 * proceed with move
5012 */
5013 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
5014 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
5015 }
5016 break;
5017 case L2CAP_MOVE_WAIT_RSP:
5018 /* Moving to AMP */
5019 if (result == L2CAP_MR_SUCCESS) {
5020 /* Remote is ready, send confirm immediately
5021 * after logical link is ready
5022 */
5023 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
5024 } else {
5025 /* Both logical link and move success
5026 * are required to confirm
5027 */
5028 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP;
5029 }
5030
5031 /* Placeholder - get hci_chan for logical link */
5032 if (!hchan) {
5033 /* Logical link not available */
5034 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5035 break;
5036 }
5037
5038 /* If the logical link is not yet connected, do not
5039 * send confirmation.
5040 */
5041 if (hchan->state != BT_CONNECTED)
5042 break;
5043
5044 /* Logical link is already ready to go */
5045
5046 chan->hs_hcon = hchan->conn;
5047 chan->hs_hcon->l2cap_data = chan->conn;
5048
5049 if (result == L2CAP_MR_SUCCESS) {
5050 /* Can confirm now */
5051 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
5052 } else {
5053 /* Now only need move success
5054 * to confirm
5055 */
5056 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
5057 }
5058
5059 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
5060 break;
5061 default:
5062 /* Any other amp move state means the move failed. */
5063 chan->move_id = chan->local_amp_id;
5064 l2cap_move_done(chan);
5065 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5066 }
5067
5068 l2cap_chan_unlock(chan);
5069}
5070
5071static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid,
5072 u16 result)
5073{
5074 struct l2cap_chan *chan;
5075
5076 chan = l2cap_get_chan_by_ident(conn, ident);
5077 if (!chan) {
5078 /* Could not locate channel, icid is best guess */
5079 l2cap_send_move_chan_cfm_icid(conn, icid);
5080 return;
5081 }
5082
5083 __clear_chan_timer(chan);
5084
5085 if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
5086 if (result == L2CAP_MR_COLLISION) {
5087 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
5088 } else {
5089 /* Cleanup - cancel move */
5090 chan->move_id = chan->local_amp_id;
5091 l2cap_move_done(chan);
5092 }
5093 }
5094
5095 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5096
5097 l2cap_chan_unlock(chan);
5098}
5099
5100static int l2cap_move_channel_rsp(struct l2cap_conn *conn,
5101 struct l2cap_cmd_hdr *cmd,
5102 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005103{
5104 struct l2cap_move_chan_rsp *rsp = data;
5105 u16 icid, result;
5106
5107 if (cmd_len != sizeof(*rsp))
5108 return -EPROTO;
5109
5110 icid = le16_to_cpu(rsp->icid);
5111 result = le16_to_cpu(rsp->result);
5112
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005113 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005114
Mat Martineau5b155ef2012-10-23 15:24:14 -07005115 if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND)
5116 l2cap_move_continue(conn, icid, result);
5117 else
5118 l2cap_move_fail(conn, cmd->ident, icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005119
5120 return 0;
5121}
5122
Mat Martineau5f3847a2012-10-23 15:24:12 -07005123static int l2cap_move_channel_confirm(struct l2cap_conn *conn,
5124 struct l2cap_cmd_hdr *cmd,
5125 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005126{
5127 struct l2cap_move_chan_cfm *cfm = data;
Mat Martineau5f3847a2012-10-23 15:24:12 -07005128 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005129 u16 icid, result;
5130
5131 if (cmd_len != sizeof(*cfm))
5132 return -EPROTO;
5133
5134 icid = le16_to_cpu(cfm->icid);
5135 result = le16_to_cpu(cfm->result);
5136
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005137 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005138
Mat Martineau5f3847a2012-10-23 15:24:12 -07005139 chan = l2cap_get_chan_by_dcid(conn, icid);
5140 if (!chan) {
5141 /* Spec requires a response even if the icid was not found */
5142 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5143 return 0;
5144 }
5145
5146 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) {
5147 if (result == L2CAP_MC_CONFIRMED) {
5148 chan->local_amp_id = chan->move_id;
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07005149 if (chan->local_amp_id == AMP_ID_BREDR)
Mat Martineau5f3847a2012-10-23 15:24:12 -07005150 __release_logical_link(chan);
5151 } else {
5152 chan->move_id = chan->local_amp_id;
5153 }
5154
5155 l2cap_move_done(chan);
5156 }
5157
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005158 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5159
Mat Martineau5f3847a2012-10-23 15:24:12 -07005160 l2cap_chan_unlock(chan);
5161
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005162 return 0;
5163}
5164
5165static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005166 struct l2cap_cmd_hdr *cmd,
5167 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005168{
5169 struct l2cap_move_chan_cfm_rsp *rsp = data;
Mat Martineau3fd71a02012-10-23 15:24:16 -07005170 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005171 u16 icid;
5172
5173 if (cmd_len != sizeof(*rsp))
5174 return -EPROTO;
5175
5176 icid = le16_to_cpu(rsp->icid);
5177
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005178 BT_DBG("icid 0x%4.4x", icid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005179
Mat Martineau3fd71a02012-10-23 15:24:16 -07005180 chan = l2cap_get_chan_by_scid(conn, icid);
5181 if (!chan)
5182 return 0;
5183
5184 __clear_chan_timer(chan);
5185
5186 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) {
5187 chan->local_amp_id = chan->move_id;
5188
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07005189 if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan)
Mat Martineau3fd71a02012-10-23 15:24:16 -07005190 __release_logical_link(chan);
5191
5192 l2cap_move_done(chan);
5193 }
5194
5195 l2cap_chan_unlock(chan);
5196
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005197 return 0;
5198}
5199
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03005200static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005201 u16 to_multiplier)
Claudio Takahaside731152011-02-11 19:28:55 -02005202{
5203 u16 max_latency;
5204
5205 if (min > max || min < 6 || max > 3200)
5206 return -EINVAL;
5207
5208 if (to_multiplier < 10 || to_multiplier > 3200)
5209 return -EINVAL;
5210
5211 if (max >= to_multiplier * 8)
5212 return -EINVAL;
5213
5214 max_latency = (to_multiplier * 8 / max) - 1;
5215 if (latency > 499 || latency > max_latency)
5216 return -EINVAL;
5217
5218 return 0;
5219}
5220
5221static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005222 struct l2cap_cmd_hdr *cmd,
Johan Hedberg203e6392013-05-15 10:07:15 +03005223 u16 cmd_len, u8 *data)
Claudio Takahaside731152011-02-11 19:28:55 -02005224{
5225 struct hci_conn *hcon = conn->hcon;
5226 struct l2cap_conn_param_update_req *req;
5227 struct l2cap_conn_param_update_rsp rsp;
Johan Hedberg203e6392013-05-15 10:07:15 +03005228 u16 min, max, latency, to_multiplier;
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005229 int err;
Claudio Takahaside731152011-02-11 19:28:55 -02005230
5231 if (!(hcon->link_mode & HCI_LM_MASTER))
5232 return -EINVAL;
5233
Claudio Takahaside731152011-02-11 19:28:55 -02005234 if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
5235 return -EPROTO;
5236
5237 req = (struct l2cap_conn_param_update_req *) data;
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03005238 min = __le16_to_cpu(req->min);
5239 max = __le16_to_cpu(req->max);
Claudio Takahaside731152011-02-11 19:28:55 -02005240 latency = __le16_to_cpu(req->latency);
5241 to_multiplier = __le16_to_cpu(req->to_multiplier);
5242
5243 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 +01005244 min, max, latency, to_multiplier);
Claudio Takahaside731152011-02-11 19:28:55 -02005245
5246 memset(&rsp, 0, sizeof(rsp));
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005247
5248 err = l2cap_check_conn_param(min, max, latency, to_multiplier);
5249 if (err)
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005250 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
Claudio Takahaside731152011-02-11 19:28:55 -02005251 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005252 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
Claudio Takahaside731152011-02-11 19:28:55 -02005253
5254 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005255 sizeof(rsp), &rsp);
Claudio Takahaside731152011-02-11 19:28:55 -02005256
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005257 if (!err)
5258 hci_le_conn_update(hcon, min, max, latency, to_multiplier);
5259
Claudio Takahaside731152011-02-11 19:28:55 -02005260 return 0;
5261}
5262
Johan Hedbergf1496de2013-05-13 14:15:56 +03005263static int l2cap_le_connect_rsp(struct l2cap_conn *conn,
5264 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5265 u8 *data)
5266{
5267 struct l2cap_le_conn_rsp *rsp = (struct l2cap_le_conn_rsp *) data;
5268 u16 dcid, mtu, mps, credits, result;
5269 struct l2cap_chan *chan;
5270 int err;
5271
5272 if (cmd_len < sizeof(*rsp))
5273 return -EPROTO;
5274
5275 dcid = __le16_to_cpu(rsp->dcid);
5276 mtu = __le16_to_cpu(rsp->mtu);
5277 mps = __le16_to_cpu(rsp->mps);
5278 credits = __le16_to_cpu(rsp->credits);
5279 result = __le16_to_cpu(rsp->result);
5280
5281 if (result == L2CAP_CR_SUCCESS && (mtu < 23 || mps < 23))
5282 return -EPROTO;
5283
5284 BT_DBG("dcid 0x%4.4x mtu %u mps %u credits %u result 0x%2.2x",
5285 dcid, mtu, mps, credits, result);
5286
5287 mutex_lock(&conn->chan_lock);
5288
5289 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
5290 if (!chan) {
5291 err = -EBADSLT;
5292 goto unlock;
5293 }
5294
5295 err = 0;
5296
5297 l2cap_chan_lock(chan);
5298
5299 switch (result) {
5300 case L2CAP_CR_SUCCESS:
5301 chan->ident = 0;
5302 chan->dcid = dcid;
5303 chan->omtu = mtu;
5304 chan->remote_mps = mps;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005305 chan->tx_credits = credits;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005306 l2cap_chan_ready(chan);
5307 break;
5308
5309 default:
5310 l2cap_chan_del(chan, ECONNREFUSED);
5311 break;
5312 }
5313
5314 l2cap_chan_unlock(chan);
5315
5316unlock:
5317 mutex_unlock(&conn->chan_lock);
5318
5319 return err;
5320}
5321
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005322static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005323 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5324 u8 *data)
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005325{
5326 int err = 0;
5327
5328 switch (cmd->code) {
5329 case L2CAP_COMMAND_REJ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005330 l2cap_command_rej(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005331 break;
5332
5333 case L2CAP_CONN_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005334 err = l2cap_connect_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005335 break;
5336
5337 case L2CAP_CONN_RSP:
Mat Martineauf5a25982012-10-11 17:48:21 +03005338 case L2CAP_CREATE_CHAN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005339 l2cap_connect_create_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005340 break;
5341
5342 case L2CAP_CONF_REQ:
5343 err = l2cap_config_req(conn, cmd, cmd_len, data);
5344 break;
5345
5346 case L2CAP_CONF_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005347 l2cap_config_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005348 break;
5349
5350 case L2CAP_DISCONN_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005351 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005352 break;
5353
5354 case L2CAP_DISCONN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005355 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005356 break;
5357
5358 case L2CAP_ECHO_REQ:
5359 l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
5360 break;
5361
5362 case L2CAP_ECHO_RSP:
5363 break;
5364
5365 case L2CAP_INFO_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005366 err = l2cap_information_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005367 break;
5368
5369 case L2CAP_INFO_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005370 l2cap_information_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005371 break;
5372
Mat Martineauf94ff6f2011-11-02 16:18:32 -07005373 case L2CAP_CREATE_CHAN_REQ:
5374 err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
5375 break;
5376
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005377 case L2CAP_MOVE_CHAN_REQ:
5378 err = l2cap_move_channel_req(conn, cmd, cmd_len, data);
5379 break;
5380
5381 case L2CAP_MOVE_CHAN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005382 l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005383 break;
5384
5385 case L2CAP_MOVE_CHAN_CFM:
5386 err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data);
5387 break;
5388
5389 case L2CAP_MOVE_CHAN_CFM_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005390 l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005391 break;
5392
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005393 default:
5394 BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
5395 err = -EINVAL;
5396 break;
5397 }
5398
5399 return err;
5400}
5401
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005402static int l2cap_le_connect_req(struct l2cap_conn *conn,
5403 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5404 u8 *data)
5405{
5406 struct l2cap_le_conn_req *req = (struct l2cap_le_conn_req *) data;
5407 struct l2cap_le_conn_rsp rsp;
5408 struct l2cap_chan *chan, *pchan;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005409 u16 dcid, scid, credits, mtu, mps;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005410 __le16 psm;
5411 u8 result;
5412
5413 if (cmd_len != sizeof(*req))
5414 return -EPROTO;
5415
5416 scid = __le16_to_cpu(req->scid);
5417 mtu = __le16_to_cpu(req->mtu);
5418 mps = __le16_to_cpu(req->mps);
5419 psm = req->psm;
5420 dcid = 0;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005421 credits = 0;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005422
5423 if (mtu < 23 || mps < 23)
5424 return -EPROTO;
5425
5426 BT_DBG("psm 0x%2.2x scid 0x%4.4x mtu %u mps %u", __le16_to_cpu(psm),
5427 scid, mtu, mps);
5428
5429 /* Check if we have socket listening on psm */
5430 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
5431 &conn->hcon->dst, LE_LINK);
5432 if (!pchan) {
5433 result = L2CAP_CR_BAD_PSM;
5434 chan = NULL;
5435 goto response;
5436 }
5437
5438 mutex_lock(&conn->chan_lock);
5439 l2cap_chan_lock(pchan);
5440
5441 if (!smp_sufficient_security(conn->hcon, pchan->sec_level)) {
5442 result = L2CAP_CR_AUTHENTICATION;
5443 chan = NULL;
5444 goto response_unlock;
5445 }
5446
5447 /* Check if we already have channel with that dcid */
5448 if (__l2cap_get_chan_by_dcid(conn, scid)) {
5449 result = L2CAP_CR_NO_MEM;
5450 chan = NULL;
5451 goto response_unlock;
5452 }
5453
5454 chan = pchan->ops->new_connection(pchan);
5455 if (!chan) {
5456 result = L2CAP_CR_NO_MEM;
5457 goto response_unlock;
5458 }
5459
Johan Hedberg0ce43ce2013-12-05 14:55:33 +02005460 l2cap_le_flowctl_init(chan);
5461
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005462 bacpy(&chan->src, &conn->hcon->src);
5463 bacpy(&chan->dst, &conn->hcon->dst);
5464 chan->src_type = bdaddr_type(conn->hcon, conn->hcon->src_type);
5465 chan->dst_type = bdaddr_type(conn->hcon, conn->hcon->dst_type);
5466 chan->psm = psm;
5467 chan->dcid = scid;
5468 chan->omtu = mtu;
5469 chan->remote_mps = mps;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005470 chan->tx_credits = __le16_to_cpu(req->credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005471
5472 __l2cap_chan_add(conn, chan);
5473 dcid = chan->scid;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005474 credits = chan->rx_credits;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005475
5476 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
5477
5478 chan->ident = cmd->ident;
5479
5480 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
5481 l2cap_state_change(chan, BT_CONNECT2);
5482 result = L2CAP_CR_PEND;
5483 chan->ops->defer(chan);
5484 } else {
5485 l2cap_chan_ready(chan);
5486 result = L2CAP_CR_SUCCESS;
5487 }
5488
5489response_unlock:
5490 l2cap_chan_unlock(pchan);
5491 mutex_unlock(&conn->chan_lock);
5492
5493 if (result == L2CAP_CR_PEND)
5494 return 0;
5495
5496response:
5497 if (chan) {
5498 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02005499 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005500 } else {
5501 rsp.mtu = 0;
5502 rsp.mps = 0;
5503 }
5504
5505 rsp.dcid = cpu_to_le16(dcid);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005506 rsp.credits = cpu_to_le16(credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005507 rsp.result = cpu_to_le16(result);
5508
5509 l2cap_send_cmd(conn, cmd->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), &rsp);
5510
5511 return 0;
5512}
5513
Johan Hedbergfad5fc892013-12-05 09:45:01 +02005514static inline int l2cap_le_credits(struct l2cap_conn *conn,
5515 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5516 u8 *data)
5517{
5518 struct l2cap_le_credits *pkt;
5519 struct l2cap_chan *chan;
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005520 u16 cid, credits, max_credits;
Johan Hedbergfad5fc892013-12-05 09:45:01 +02005521
5522 if (cmd_len != sizeof(*pkt))
5523 return -EPROTO;
5524
5525 pkt = (struct l2cap_le_credits *) data;
5526 cid = __le16_to_cpu(pkt->cid);
5527 credits = __le16_to_cpu(pkt->credits);
5528
5529 BT_DBG("cid 0x%4.4x credits 0x%4.4x", cid, credits);
5530
5531 chan = l2cap_get_chan_by_dcid(conn, cid);
5532 if (!chan)
5533 return -EBADSLT;
5534
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005535 max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits;
5536 if (credits > max_credits) {
5537 BT_ERR("LE credits overflow");
5538 l2cap_send_disconn_req(chan, ECONNRESET);
5539
5540 /* Return 0 so that we don't trigger an unnecessary
5541 * command reject packet.
5542 */
5543 return 0;
5544 }
5545
Johan Hedbergfad5fc892013-12-05 09:45:01 +02005546 chan->tx_credits += credits;
5547
5548 while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) {
5549 l2cap_do_send(chan, skb_dequeue(&chan->tx_q));
5550 chan->tx_credits--;
5551 }
5552
5553 if (chan->tx_credits)
5554 chan->ops->resume(chan);
5555
5556 l2cap_chan_unlock(chan);
5557
5558 return 0;
5559}
5560
Johan Hedberg71fb4192013-12-10 10:52:48 +02005561static inline int l2cap_le_command_rej(struct l2cap_conn *conn,
5562 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5563 u8 *data)
5564{
5565 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
5566 struct l2cap_chan *chan;
5567
5568 if (cmd_len < sizeof(*rej))
5569 return -EPROTO;
5570
5571 mutex_lock(&conn->chan_lock);
5572
5573 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
5574 if (!chan)
5575 goto done;
5576
5577 l2cap_chan_lock(chan);
5578 l2cap_chan_del(chan, ECONNREFUSED);
5579 l2cap_chan_unlock(chan);
5580
5581done:
5582 mutex_unlock(&conn->chan_lock);
5583 return 0;
5584}
5585
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005586static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
Johan Hedberg203e6392013-05-15 10:07:15 +03005587 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5588 u8 *data)
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005589{
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005590 int err = 0;
5591
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005592 switch (cmd->code) {
5593 case L2CAP_COMMAND_REJ:
Johan Hedberg71fb4192013-12-10 10:52:48 +02005594 l2cap_le_command_rej(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005595 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005596
5597 case L2CAP_CONN_PARAM_UPDATE_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005598 err = l2cap_conn_param_update_req(conn, cmd, cmd_len, data);
5599 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005600
5601 case L2CAP_CONN_PARAM_UPDATE_RSP:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005602 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005603
Johan Hedbergf1496de2013-05-13 14:15:56 +03005604 case L2CAP_LE_CONN_RSP:
5605 l2cap_le_connect_rsp(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005606 break;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005607
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005608 case L2CAP_LE_CONN_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005609 err = l2cap_le_connect_req(conn, cmd, cmd_len, data);
5610 break;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005611
Johan Hedbergfad5fc892013-12-05 09:45:01 +02005612 case L2CAP_LE_CREDITS:
5613 err = l2cap_le_credits(conn, cmd, cmd_len, data);
5614 break;
5615
Johan Hedberg3defe012013-05-15 10:16:06 +03005616 case L2CAP_DISCONN_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005617 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
5618 break;
Johan Hedberg3defe012013-05-15 10:16:06 +03005619
5620 case L2CAP_DISCONN_RSP:
5621 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005622 break;
Johan Hedberg3defe012013-05-15 10:16:06 +03005623
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005624 default:
5625 BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005626 err = -EINVAL;
5627 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005628 }
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005629
5630 return err;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005631}
5632
Johan Hedbergc5623552013-04-29 19:35:33 +03005633static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,
5634 struct sk_buff *skb)
5635{
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005636 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005637 struct l2cap_cmd_hdr *cmd;
5638 u16 len;
Johan Hedbergc5623552013-04-29 19:35:33 +03005639 int err;
5640
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005641 if (hcon->type != LE_LINK)
Marcel Holtmann3b166292013-10-02 08:28:21 -07005642 goto drop;
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005643
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005644 if (skb->len < L2CAP_CMD_HDR_SIZE)
5645 goto drop;
Johan Hedbergc5623552013-04-29 19:35:33 +03005646
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005647 cmd = (void *) skb->data;
5648 skb_pull(skb, L2CAP_CMD_HDR_SIZE);
Johan Hedbergc5623552013-04-29 19:35:33 +03005649
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005650 len = le16_to_cpu(cmd->len);
Johan Hedbergc5623552013-04-29 19:35:33 +03005651
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005652 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd->code, len, cmd->ident);
Johan Hedbergc5623552013-04-29 19:35:33 +03005653
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005654 if (len != skb->len || !cmd->ident) {
5655 BT_DBG("corrupted command");
5656 goto drop;
5657 }
Johan Hedbergc5623552013-04-29 19:35:33 +03005658
Johan Hedberg203e6392013-05-15 10:07:15 +03005659 err = l2cap_le_sig_cmd(conn, cmd, len, skb->data);
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005660 if (err) {
5661 struct l2cap_cmd_rej_unk rej;
Johan Hedbergc5623552013-04-29 19:35:33 +03005662
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005663 BT_ERR("Wrong link type (%d)", err);
Johan Hedbergc5623552013-04-29 19:35:33 +03005664
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005665 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005666 l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
5667 sizeof(rej), &rej);
Johan Hedbergc5623552013-04-29 19:35:33 +03005668 }
5669
Marcel Holtmann3b166292013-10-02 08:28:21 -07005670drop:
Johan Hedbergc5623552013-04-29 19:35:33 +03005671 kfree_skb(skb);
5672}
5673
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005674static inline void l2cap_sig_channel(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005675 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676{
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005677 struct hci_conn *hcon = conn->hcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 u8 *data = skb->data;
5679 int len = skb->len;
5680 struct l2cap_cmd_hdr cmd;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005681 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
5683 l2cap_raw_recv(conn, skb);
5684
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005685 if (hcon->type != ACL_LINK)
Marcel Holtmann3b166292013-10-02 08:28:21 -07005686 goto drop;
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005687
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 while (len >= L2CAP_CMD_HDR_SIZE) {
Al Viro88219a02007-07-29 00:17:25 -07005689 u16 cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
5691 data += L2CAP_CMD_HDR_SIZE;
5692 len -= L2CAP_CMD_HDR_SIZE;
5693
Al Viro88219a02007-07-29 00:17:25 -07005694 cmd_len = le16_to_cpu(cmd.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
Gustavo Padovan2d792812012-10-06 10:07:01 +01005696 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len,
5697 cmd.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698
Al Viro88219a02007-07-29 00:17:25 -07005699 if (cmd_len > len || !cmd.ident) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 BT_DBG("corrupted command");
5701 break;
5702 }
5703
Johan Hedbergc5623552013-04-29 19:35:33 +03005704 err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 if (err) {
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03005706 struct l2cap_cmd_rej_unk rej;
Gustavo F. Padovan2c6d1a22011-03-23 14:38:32 -03005707
5708 BT_ERR("Wrong link type (%d)", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005710 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
Gustavo Padovan2d792812012-10-06 10:07:01 +01005711 l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ,
5712 sizeof(rej), &rej);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 }
5714
Al Viro88219a02007-07-29 00:17:25 -07005715 data += cmd_len;
5716 len -= cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 }
5718
Marcel Holtmann3b166292013-10-02 08:28:21 -07005719drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 kfree_skb(skb);
5721}
5722
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03005723static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005724{
5725 u16 our_fcs, rcv_fcs;
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03005726 int hdr_size;
5727
5728 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
5729 hdr_size = L2CAP_EXT_HDR_SIZE;
5730 else
5731 hdr_size = L2CAP_ENH_HDR_SIZE;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005732
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03005733 if (chan->fcs == L2CAP_FCS_CRC16) {
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03005734 skb_trim(skb, skb->len - L2CAP_FCS_SIZE);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005735 rcv_fcs = get_unaligned_le16(skb->data + skb->len);
5736 our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
5737
5738 if (our_fcs != rcv_fcs)
João Paulo Rechi Vita7a560e52010-06-22 13:56:27 -03005739 return -EBADMSG;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005740 }
5741 return 0;
5742}
5743
Mat Martineau6ea00482012-05-17 20:53:52 -07005744static void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005745{
Mat Martineaue31f7632012-05-17 20:53:41 -07005746 struct l2cap_ctrl control;
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005747
Mat Martineaue31f7632012-05-17 20:53:41 -07005748 BT_DBG("chan %p", chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005749
Mat Martineaue31f7632012-05-17 20:53:41 -07005750 memset(&control, 0, sizeof(control));
5751 control.sframe = 1;
5752 control.final = 1;
5753 control.reqseq = chan->buffer_seq;
5754 set_bit(CONN_SEND_FBIT, &chan->conn_state);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005755
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03005756 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
Mat Martineaue31f7632012-05-17 20:53:41 -07005757 control.super = L2CAP_SUPER_RNR;
5758 l2cap_send_sframe(chan, &control);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005759 }
5760
Mat Martineaue31f7632012-05-17 20:53:41 -07005761 if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
5762 chan->unacked_frames > 0)
5763 __set_retrans_timer(chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005764
Mat Martineaue31f7632012-05-17 20:53:41 -07005765 /* Send pending iframes */
Gustavo F. Padovan525cd182011-03-25 19:43:39 -03005766 l2cap_ertm_send(chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005767
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03005768 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
Mat Martineaue31f7632012-05-17 20:53:41 -07005769 test_bit(CONN_SEND_FBIT, &chan->conn_state)) {
5770 /* F-bit wasn't sent in an s-frame or i-frame yet, so
5771 * send it now.
5772 */
5773 control.super = L2CAP_SUPER_RR;
5774 l2cap_send_sframe(chan, &control);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005775 }
5776}
5777
Gustavo Padovan2d792812012-10-06 10:07:01 +01005778static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag,
5779 struct sk_buff **last_frag)
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005780{
Mat Martineau84084a32011-07-22 14:54:00 -07005781 /* skb->len reflects data in skb as well as all fragments
5782 * skb->data_len reflects only data in fragments
5783 */
5784 if (!skb_has_frag_list(skb))
5785 skb_shinfo(skb)->frag_list = new_frag;
5786
5787 new_frag->next = NULL;
5788
5789 (*last_frag)->next = new_frag;
5790 *last_frag = new_frag;
5791
5792 skb->len += new_frag->len;
5793 skb->data_len += new_frag->len;
5794 skb->truesize += new_frag->truesize;
5795}
5796
Mat Martineau4b51dae92012-05-17 20:53:37 -07005797static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb,
5798 struct l2cap_ctrl *control)
Mat Martineau84084a32011-07-22 14:54:00 -07005799{
5800 int err = -EINVAL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005801
Mat Martineau4b51dae92012-05-17 20:53:37 -07005802 switch (control->sar) {
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005803 case L2CAP_SAR_UNSEGMENTED:
Mat Martineau84084a32011-07-22 14:54:00 -07005804 if (chan->sdu)
5805 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005806
Gustavo Padovan80b98022012-05-27 22:27:51 -03005807 err = chan->ops->recv(chan, skb);
Mat Martineau84084a32011-07-22 14:54:00 -07005808 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005809
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005810 case L2CAP_SAR_START:
Mat Martineau84084a32011-07-22 14:54:00 -07005811 if (chan->sdu)
5812 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005813
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005814 chan->sdu_len = get_unaligned_le16(skb->data);
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03005815 skb_pull(skb, L2CAP_SDULEN_SIZE);
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005816
Mat Martineau84084a32011-07-22 14:54:00 -07005817 if (chan->sdu_len > chan->imtu) {
5818 err = -EMSGSIZE;
5819 break;
5820 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005821
Mat Martineau84084a32011-07-22 14:54:00 -07005822 if (skb->len >= chan->sdu_len)
5823 break;
5824
5825 chan->sdu = skb;
5826 chan->sdu_last_frag = skb;
5827
5828 skb = NULL;
5829 err = 0;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005830 break;
5831
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005832 case L2CAP_SAR_CONTINUE:
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005833 if (!chan->sdu)
Mat Martineau84084a32011-07-22 14:54:00 -07005834 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005835
Mat Martineau84084a32011-07-22 14:54:00 -07005836 append_skb_frag(chan->sdu, skb,
5837 &chan->sdu_last_frag);
5838 skb = NULL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005839
Mat Martineau84084a32011-07-22 14:54:00 -07005840 if (chan->sdu->len >= chan->sdu_len)
5841 break;
Gustavo F. Padovan4178ba42010-05-01 16:15:45 -03005842
Mat Martineau84084a32011-07-22 14:54:00 -07005843 err = 0;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005844 break;
5845
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005846 case L2CAP_SAR_END:
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005847 if (!chan->sdu)
Mat Martineau84084a32011-07-22 14:54:00 -07005848 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005849
Mat Martineau84084a32011-07-22 14:54:00 -07005850 append_skb_frag(chan->sdu, skb,
5851 &chan->sdu_last_frag);
5852 skb = NULL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005853
Mat Martineau84084a32011-07-22 14:54:00 -07005854 if (chan->sdu->len != chan->sdu_len)
5855 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005856
Gustavo Padovan80b98022012-05-27 22:27:51 -03005857 err = chan->ops->recv(chan, chan->sdu);
Gustavo F. Padovan4178ba42010-05-01 16:15:45 -03005858
Mat Martineau84084a32011-07-22 14:54:00 -07005859 if (!err) {
5860 /* Reassembly complete */
5861 chan->sdu = NULL;
5862 chan->sdu_last_frag = NULL;
5863 chan->sdu_len = 0;
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03005864 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005865 break;
5866 }
5867
Mat Martineau84084a32011-07-22 14:54:00 -07005868 if (err) {
5869 kfree_skb(skb);
5870 kfree_skb(chan->sdu);
5871 chan->sdu = NULL;
5872 chan->sdu_last_frag = NULL;
5873 chan->sdu_len = 0;
5874 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005875
Mat Martineau84084a32011-07-22 14:54:00 -07005876 return err;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005877}
5878
Mat Martineau32b32732012-10-23 15:24:11 -07005879static int l2cap_resegment(struct l2cap_chan *chan)
5880{
5881 /* Placeholder */
5882 return 0;
5883}
5884
Mat Martineaue3281402011-07-07 09:39:02 -07005885void l2cap_chan_busy(struct l2cap_chan *chan, int busy)
Gustavo F. Padovan712132e2010-06-21 19:39:50 -03005886{
Mat Martineau61aa4f52012-05-17 20:53:40 -07005887 u8 event;
5888
5889 if (chan->mode != L2CAP_MODE_ERTM)
5890 return;
5891
5892 event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR;
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03005893 l2cap_tx(chan, NULL, NULL, event);
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03005894}
5895
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005896static int l2cap_rx_queued_iframes(struct l2cap_chan *chan)
5897{
Mat Martineau63838722012-05-17 20:53:45 -07005898 int err = 0;
5899 /* Pass sequential frames to l2cap_reassemble_sdu()
5900 * until a gap is encountered.
5901 */
5902
5903 BT_DBG("chan %p", chan);
5904
5905 while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
5906 struct sk_buff *skb;
5907 BT_DBG("Searching for skb with txseq %d (queue len %d)",
5908 chan->buffer_seq, skb_queue_len(&chan->srej_q));
5909
5910 skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq);
5911
5912 if (!skb)
5913 break;
5914
5915 skb_unlink(skb, &chan->srej_q);
5916 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
5917 err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->control);
5918 if (err)
5919 break;
5920 }
5921
5922 if (skb_queue_empty(&chan->srej_q)) {
5923 chan->rx_state = L2CAP_RX_STATE_RECV;
5924 l2cap_send_ack(chan);
5925 }
5926
5927 return err;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005928}
5929
5930static void l2cap_handle_srej(struct l2cap_chan *chan,
5931 struct l2cap_ctrl *control)
5932{
Mat Martineauf80842a2012-05-17 20:53:46 -07005933 struct sk_buff *skb;
5934
5935 BT_DBG("chan %p, control %p", chan, control);
5936
5937 if (control->reqseq == chan->next_tx_seq) {
5938 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005939 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineauf80842a2012-05-17 20:53:46 -07005940 return;
5941 }
5942
5943 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
5944
5945 if (skb == NULL) {
5946 BT_DBG("Seq %d not available for retransmission",
5947 control->reqseq);
5948 return;
5949 }
5950
5951 if (chan->max_tx != 0 && bt_cb(skb)->control.retries >= chan->max_tx) {
5952 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005953 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineauf80842a2012-05-17 20:53:46 -07005954 return;
5955 }
5956
5957 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
5958
5959 if (control->poll) {
5960 l2cap_pass_to_tx(chan, control);
5961
5962 set_bit(CONN_SEND_FBIT, &chan->conn_state);
5963 l2cap_retransmit(chan, control);
5964 l2cap_ertm_send(chan);
5965
5966 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
5967 set_bit(CONN_SREJ_ACT, &chan->conn_state);
5968 chan->srej_save_reqseq = control->reqseq;
5969 }
5970 } else {
5971 l2cap_pass_to_tx_fbit(chan, control);
5972
5973 if (control->final) {
5974 if (chan->srej_save_reqseq != control->reqseq ||
5975 !test_and_clear_bit(CONN_SREJ_ACT,
5976 &chan->conn_state))
5977 l2cap_retransmit(chan, control);
5978 } else {
5979 l2cap_retransmit(chan, control);
5980 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
5981 set_bit(CONN_SREJ_ACT, &chan->conn_state);
5982 chan->srej_save_reqseq = control->reqseq;
5983 }
5984 }
5985 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005986}
5987
5988static void l2cap_handle_rej(struct l2cap_chan *chan,
5989 struct l2cap_ctrl *control)
5990{
Mat Martineaufcd289d2012-05-17 20:53:47 -07005991 struct sk_buff *skb;
5992
5993 BT_DBG("chan %p, control %p", chan, control);
5994
5995 if (control->reqseq == chan->next_tx_seq) {
5996 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005997 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaufcd289d2012-05-17 20:53:47 -07005998 return;
5999 }
6000
6001 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
6002
6003 if (chan->max_tx && skb &&
6004 bt_cb(skb)->control.retries >= chan->max_tx) {
6005 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006006 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaufcd289d2012-05-17 20:53:47 -07006007 return;
6008 }
6009
6010 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6011
6012 l2cap_pass_to_tx(chan, control);
6013
6014 if (control->final) {
6015 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
6016 l2cap_retransmit_all(chan, control);
6017 } else {
6018 l2cap_retransmit_all(chan, control);
6019 l2cap_ertm_send(chan);
6020 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F)
6021 set_bit(CONN_REJ_ACT, &chan->conn_state);
6022 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006023}
6024
Mat Martineau4b51dae92012-05-17 20:53:37 -07006025static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq)
6026{
6027 BT_DBG("chan %p, txseq %d", chan, txseq);
6028
6029 BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq,
6030 chan->expected_tx_seq);
6031
6032 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
6033 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
Gustavo Padovan2d792812012-10-06 10:07:01 +01006034 chan->tx_win) {
Mat Martineau4b51dae92012-05-17 20:53:37 -07006035 /* See notes below regarding "double poll" and
6036 * invalid packets.
6037 */
6038 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
6039 BT_DBG("Invalid/Ignore - after SREJ");
6040 return L2CAP_TXSEQ_INVALID_IGNORE;
6041 } else {
6042 BT_DBG("Invalid - in window after SREJ sent");
6043 return L2CAP_TXSEQ_INVALID;
6044 }
6045 }
6046
6047 if (chan->srej_list.head == txseq) {
6048 BT_DBG("Expected SREJ");
6049 return L2CAP_TXSEQ_EXPECTED_SREJ;
6050 }
6051
6052 if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) {
6053 BT_DBG("Duplicate SREJ - txseq already stored");
6054 return L2CAP_TXSEQ_DUPLICATE_SREJ;
6055 }
6056
6057 if (l2cap_seq_list_contains(&chan->srej_list, txseq)) {
6058 BT_DBG("Unexpected SREJ - not requested");
6059 return L2CAP_TXSEQ_UNEXPECTED_SREJ;
6060 }
6061 }
6062
6063 if (chan->expected_tx_seq == txseq) {
6064 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
6065 chan->tx_win) {
6066 BT_DBG("Invalid - txseq outside tx window");
6067 return L2CAP_TXSEQ_INVALID;
6068 } else {
6069 BT_DBG("Expected");
6070 return L2CAP_TXSEQ_EXPECTED;
6071 }
6072 }
6073
6074 if (__seq_offset(chan, txseq, chan->last_acked_seq) <
Gustavo Padovan2d792812012-10-06 10:07:01 +01006075 __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) {
Mat Martineau4b51dae92012-05-17 20:53:37 -07006076 BT_DBG("Duplicate - expected_tx_seq later than txseq");
6077 return L2CAP_TXSEQ_DUPLICATE;
6078 }
6079
6080 if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) {
6081 /* A source of invalid packets is a "double poll" condition,
6082 * where delays cause us to send multiple poll packets. If
6083 * the remote stack receives and processes both polls,
6084 * sequence numbers can wrap around in such a way that a
6085 * resent frame has a sequence number that looks like new data
6086 * with a sequence gap. This would trigger an erroneous SREJ
6087 * request.
6088 *
6089 * Fortunately, this is impossible with a tx window that's
6090 * less than half of the maximum sequence number, which allows
6091 * invalid frames to be safely ignored.
6092 *
6093 * With tx window sizes greater than half of the tx window
6094 * maximum, the frame is invalid and cannot be ignored. This
6095 * causes a disconnect.
6096 */
6097
6098 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
6099 BT_DBG("Invalid/Ignore - txseq outside tx window");
6100 return L2CAP_TXSEQ_INVALID_IGNORE;
6101 } else {
6102 BT_DBG("Invalid - txseq outside tx window");
6103 return L2CAP_TXSEQ_INVALID;
6104 }
6105 } else {
6106 BT_DBG("Unexpected - txseq indicates missing frames");
6107 return L2CAP_TXSEQ_UNEXPECTED;
6108 }
6109}
6110
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006111static int l2cap_rx_state_recv(struct l2cap_chan *chan,
6112 struct l2cap_ctrl *control,
6113 struct sk_buff *skb, u8 event)
6114{
6115 int err = 0;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006116 bool skb_in_use = false;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006117
6118 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6119 event);
6120
6121 switch (event) {
6122 case L2CAP_EV_RECV_IFRAME:
6123 switch (l2cap_classify_txseq(chan, control->txseq)) {
6124 case L2CAP_TXSEQ_EXPECTED:
6125 l2cap_pass_to_tx(chan, control);
6126
6127 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6128 BT_DBG("Busy, discarding expected seq %d",
6129 control->txseq);
6130 break;
6131 }
6132
6133 chan->expected_tx_seq = __next_seq(chan,
6134 control->txseq);
6135
6136 chan->buffer_seq = chan->expected_tx_seq;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006137 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006138
6139 err = l2cap_reassemble_sdu(chan, skb, control);
6140 if (err)
6141 break;
6142
6143 if (control->final) {
6144 if (!test_and_clear_bit(CONN_REJ_ACT,
6145 &chan->conn_state)) {
6146 control->final = 0;
6147 l2cap_retransmit_all(chan, control);
6148 l2cap_ertm_send(chan);
6149 }
6150 }
6151
6152 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
6153 l2cap_send_ack(chan);
6154 break;
6155 case L2CAP_TXSEQ_UNEXPECTED:
6156 l2cap_pass_to_tx(chan, control);
6157
6158 /* Can't issue SREJ frames in the local busy state.
6159 * Drop this frame, it will be seen as missing
6160 * when local busy is exited.
6161 */
6162 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6163 BT_DBG("Busy, discarding unexpected seq %d",
6164 control->txseq);
6165 break;
6166 }
6167
6168 /* There was a gap in the sequence, so an SREJ
6169 * must be sent for each missing frame. The
6170 * current frame is stored for later use.
6171 */
6172 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006173 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006174 BT_DBG("Queued %p (queue len %d)", skb,
6175 skb_queue_len(&chan->srej_q));
6176
6177 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
6178 l2cap_seq_list_clear(&chan->srej_list);
6179 l2cap_send_srej(chan, control->txseq);
6180
6181 chan->rx_state = L2CAP_RX_STATE_SREJ_SENT;
6182 break;
6183 case L2CAP_TXSEQ_DUPLICATE:
6184 l2cap_pass_to_tx(chan, control);
6185 break;
6186 case L2CAP_TXSEQ_INVALID_IGNORE:
6187 break;
6188 case L2CAP_TXSEQ_INVALID:
6189 default:
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006190 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006191 break;
6192 }
6193 break;
6194 case L2CAP_EV_RECV_RR:
6195 l2cap_pass_to_tx(chan, control);
6196 if (control->final) {
6197 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6198
Mat Martineaue6a3ee62012-10-23 15:24:22 -07006199 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) &&
6200 !__chan_is_moving(chan)) {
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006201 control->final = 0;
6202 l2cap_retransmit_all(chan, control);
6203 }
6204
6205 l2cap_ertm_send(chan);
6206 } else if (control->poll) {
6207 l2cap_send_i_or_rr_or_rnr(chan);
6208 } else {
6209 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6210 &chan->conn_state) &&
6211 chan->unacked_frames)
6212 __set_retrans_timer(chan);
6213
6214 l2cap_ertm_send(chan);
6215 }
6216 break;
6217 case L2CAP_EV_RECV_RNR:
6218 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6219 l2cap_pass_to_tx(chan, control);
6220 if (control && control->poll) {
6221 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6222 l2cap_send_rr_or_rnr(chan, 0);
6223 }
6224 __clear_retrans_timer(chan);
6225 l2cap_seq_list_clear(&chan->retrans_list);
6226 break;
6227 case L2CAP_EV_RECV_REJ:
6228 l2cap_handle_rej(chan, control);
6229 break;
6230 case L2CAP_EV_RECV_SREJ:
6231 l2cap_handle_srej(chan, control);
6232 break;
6233 default:
6234 break;
6235 }
6236
6237 if (skb && !skb_in_use) {
6238 BT_DBG("Freeing %p", skb);
6239 kfree_skb(skb);
6240 }
6241
6242 return err;
6243}
6244
6245static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
6246 struct l2cap_ctrl *control,
6247 struct sk_buff *skb, u8 event)
6248{
6249 int err = 0;
6250 u16 txseq = control->txseq;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006251 bool skb_in_use = false;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006252
6253 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6254 event);
6255
6256 switch (event) {
6257 case L2CAP_EV_RECV_IFRAME:
6258 switch (l2cap_classify_txseq(chan, txseq)) {
6259 case L2CAP_TXSEQ_EXPECTED:
6260 /* Keep frame for reassembly later */
6261 l2cap_pass_to_tx(chan, control);
6262 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006263 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006264 BT_DBG("Queued %p (queue len %d)", skb,
6265 skb_queue_len(&chan->srej_q));
6266
6267 chan->expected_tx_seq = __next_seq(chan, txseq);
6268 break;
6269 case L2CAP_TXSEQ_EXPECTED_SREJ:
6270 l2cap_seq_list_pop(&chan->srej_list);
6271
6272 l2cap_pass_to_tx(chan, control);
6273 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006274 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006275 BT_DBG("Queued %p (queue len %d)", skb,
6276 skb_queue_len(&chan->srej_q));
6277
6278 err = l2cap_rx_queued_iframes(chan);
6279 if (err)
6280 break;
6281
6282 break;
6283 case L2CAP_TXSEQ_UNEXPECTED:
6284 /* Got a frame that can't be reassembled yet.
6285 * Save it for later, and send SREJs to cover
6286 * the missing frames.
6287 */
6288 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006289 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006290 BT_DBG("Queued %p (queue len %d)", skb,
6291 skb_queue_len(&chan->srej_q));
6292
6293 l2cap_pass_to_tx(chan, control);
6294 l2cap_send_srej(chan, control->txseq);
6295 break;
6296 case L2CAP_TXSEQ_UNEXPECTED_SREJ:
6297 /* This frame was requested with an SREJ, but
6298 * some expected retransmitted frames are
6299 * missing. Request retransmission of missing
6300 * SREJ'd frames.
6301 */
6302 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006303 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006304 BT_DBG("Queued %p (queue len %d)", skb,
6305 skb_queue_len(&chan->srej_q));
6306
6307 l2cap_pass_to_tx(chan, control);
6308 l2cap_send_srej_list(chan, control->txseq);
6309 break;
6310 case L2CAP_TXSEQ_DUPLICATE_SREJ:
6311 /* We've already queued this frame. Drop this copy. */
6312 l2cap_pass_to_tx(chan, control);
6313 break;
6314 case L2CAP_TXSEQ_DUPLICATE:
6315 /* Expecting a later sequence number, so this frame
6316 * was already received. Ignore it completely.
6317 */
6318 break;
6319 case L2CAP_TXSEQ_INVALID_IGNORE:
6320 break;
6321 case L2CAP_TXSEQ_INVALID:
6322 default:
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006323 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006324 break;
6325 }
6326 break;
6327 case L2CAP_EV_RECV_RR:
6328 l2cap_pass_to_tx(chan, control);
6329 if (control->final) {
6330 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6331
6332 if (!test_and_clear_bit(CONN_REJ_ACT,
6333 &chan->conn_state)) {
6334 control->final = 0;
6335 l2cap_retransmit_all(chan, control);
6336 }
6337
6338 l2cap_ertm_send(chan);
6339 } else if (control->poll) {
6340 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6341 &chan->conn_state) &&
6342 chan->unacked_frames) {
6343 __set_retrans_timer(chan);
6344 }
6345
6346 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6347 l2cap_send_srej_tail(chan);
6348 } else {
6349 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6350 &chan->conn_state) &&
6351 chan->unacked_frames)
6352 __set_retrans_timer(chan);
6353
6354 l2cap_send_ack(chan);
6355 }
6356 break;
6357 case L2CAP_EV_RECV_RNR:
6358 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6359 l2cap_pass_to_tx(chan, control);
6360 if (control->poll) {
6361 l2cap_send_srej_tail(chan);
6362 } else {
6363 struct l2cap_ctrl rr_control;
6364 memset(&rr_control, 0, sizeof(rr_control));
6365 rr_control.sframe = 1;
6366 rr_control.super = L2CAP_SUPER_RR;
6367 rr_control.reqseq = chan->buffer_seq;
6368 l2cap_send_sframe(chan, &rr_control);
6369 }
6370
6371 break;
6372 case L2CAP_EV_RECV_REJ:
6373 l2cap_handle_rej(chan, control);
6374 break;
6375 case L2CAP_EV_RECV_SREJ:
6376 l2cap_handle_srej(chan, control);
6377 break;
6378 }
6379
6380 if (skb && !skb_in_use) {
6381 BT_DBG("Freeing %p", skb);
6382 kfree_skb(skb);
6383 }
6384
6385 return err;
6386}
6387
Mat Martineau32b32732012-10-23 15:24:11 -07006388static int l2cap_finish_move(struct l2cap_chan *chan)
6389{
6390 BT_DBG("chan %p", chan);
6391
6392 chan->rx_state = L2CAP_RX_STATE_RECV;
6393
6394 if (chan->hs_hcon)
6395 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
6396 else
6397 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
6398
6399 return l2cap_resegment(chan);
6400}
6401
6402static int l2cap_rx_state_wait_p(struct l2cap_chan *chan,
6403 struct l2cap_ctrl *control,
6404 struct sk_buff *skb, u8 event)
6405{
6406 int err;
6407
6408 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6409 event);
6410
6411 if (!control->poll)
6412 return -EPROTO;
6413
6414 l2cap_process_reqseq(chan, control->reqseq);
6415
6416 if (!skb_queue_empty(&chan->tx_q))
6417 chan->tx_send_head = skb_peek(&chan->tx_q);
6418 else
6419 chan->tx_send_head = NULL;
6420
6421 /* Rewind next_tx_seq to the point expected
6422 * by the receiver.
6423 */
6424 chan->next_tx_seq = control->reqseq;
6425 chan->unacked_frames = 0;
6426
6427 err = l2cap_finish_move(chan);
6428 if (err)
6429 return err;
6430
6431 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6432 l2cap_send_i_or_rr_or_rnr(chan);
6433
6434 if (event == L2CAP_EV_RECV_IFRAME)
6435 return -EPROTO;
6436
6437 return l2cap_rx_state_recv(chan, control, NULL, event);
6438}
6439
6440static int l2cap_rx_state_wait_f(struct l2cap_chan *chan,
6441 struct l2cap_ctrl *control,
6442 struct sk_buff *skb, u8 event)
6443{
6444 int err;
6445
6446 if (!control->final)
6447 return -EPROTO;
6448
6449 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6450
6451 chan->rx_state = L2CAP_RX_STATE_RECV;
6452 l2cap_process_reqseq(chan, control->reqseq);
6453
6454 if (!skb_queue_empty(&chan->tx_q))
6455 chan->tx_send_head = skb_peek(&chan->tx_q);
6456 else
6457 chan->tx_send_head = NULL;
6458
6459 /* Rewind next_tx_seq to the point expected
6460 * by the receiver.
6461 */
6462 chan->next_tx_seq = control->reqseq;
6463 chan->unacked_frames = 0;
6464
6465 if (chan->hs_hcon)
6466 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
6467 else
6468 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
6469
6470 err = l2cap_resegment(chan);
6471
6472 if (!err)
6473 err = l2cap_rx_state_recv(chan, control, skb, event);
6474
6475 return err;
6476}
6477
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006478static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq)
6479{
6480 /* Make sure reqseq is for a packet that has been sent but not acked */
6481 u16 unacked;
6482
6483 unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq);
6484 return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked;
6485}
6486
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006487static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6488 struct sk_buff *skb, u8 event)
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006489{
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006490 int err = 0;
6491
6492 BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan,
6493 control, skb, event, chan->rx_state);
6494
6495 if (__valid_reqseq(chan, control->reqseq)) {
6496 switch (chan->rx_state) {
6497 case L2CAP_RX_STATE_RECV:
6498 err = l2cap_rx_state_recv(chan, control, skb, event);
6499 break;
6500 case L2CAP_RX_STATE_SREJ_SENT:
6501 err = l2cap_rx_state_srej_sent(chan, control, skb,
6502 event);
6503 break;
Mat Martineau32b32732012-10-23 15:24:11 -07006504 case L2CAP_RX_STATE_WAIT_P:
6505 err = l2cap_rx_state_wait_p(chan, control, skb, event);
6506 break;
6507 case L2CAP_RX_STATE_WAIT_F:
6508 err = l2cap_rx_state_wait_f(chan, control, skb, event);
6509 break;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006510 default:
6511 /* shut it down */
6512 break;
6513 }
6514 } else {
6515 BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d",
6516 control->reqseq, chan->next_tx_seq,
6517 chan->expected_ack_seq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006518 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006519 }
6520
6521 return err;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006522}
6523
6524static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6525 struct sk_buff *skb)
6526{
Mat Martineau4b51dae92012-05-17 20:53:37 -07006527 int err = 0;
6528
6529 BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb,
6530 chan->rx_state);
6531
6532 if (l2cap_classify_txseq(chan, control->txseq) ==
6533 L2CAP_TXSEQ_EXPECTED) {
6534 l2cap_pass_to_tx(chan, control);
6535
6536 BT_DBG("buffer_seq %d->%d", chan->buffer_seq,
6537 __next_seq(chan, chan->buffer_seq));
6538
6539 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
6540
6541 l2cap_reassemble_sdu(chan, skb, control);
6542 } else {
6543 if (chan->sdu) {
6544 kfree_skb(chan->sdu);
6545 chan->sdu = NULL;
6546 }
6547 chan->sdu_last_frag = NULL;
6548 chan->sdu_len = 0;
6549
6550 if (skb) {
6551 BT_DBG("Freeing %p", skb);
6552 kfree_skb(skb);
6553 }
6554 }
6555
6556 chan->last_acked_seq = control->txseq;
6557 chan->expected_tx_seq = __next_seq(chan, control->txseq);
6558
6559 return err;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006560}
6561
6562static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
6563{
6564 struct l2cap_ctrl *control = &bt_cb(skb)->control;
6565 u16 len;
6566 u8 event;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006567
Mat Martineaub76bbd62012-04-11 10:48:43 -07006568 __unpack_control(chan, skb);
6569
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006570 len = skb->len;
6571
6572 /*
6573 * We can just drop the corrupted I-frame here.
6574 * Receiver will miss it and start proper recovery
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006575 * procedures and ask for retransmission.
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006576 */
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006577 if (l2cap_check_fcs(chan, skb))
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006578 goto drop;
6579
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006580 if (!control->sframe && control->sar == L2CAP_SAR_START)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03006581 len -= L2CAP_SDULEN_SIZE;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006582
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006583 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03006584 len -= L2CAP_FCS_SIZE;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006585
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006586 if (len > chan->mps) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006587 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006588 goto drop;
6589 }
6590
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006591 if (!control->sframe) {
6592 int err;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006593
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006594 BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d",
6595 control->sar, control->reqseq, control->final,
6596 control->txseq);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03006597
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006598 /* Validate F-bit - F=0 always valid, F=1 only
6599 * valid in TX WAIT_F
6600 */
6601 if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F)
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006602 goto drop;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006603
6604 if (chan->mode != L2CAP_MODE_STREAMING) {
6605 event = L2CAP_EV_RECV_IFRAME;
6606 err = l2cap_rx(chan, control, skb, event);
6607 } else {
6608 err = l2cap_stream_rx(chan, control, skb);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006609 }
6610
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006611 if (err)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006612 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006613 } else {
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006614 const u8 rx_func_to_event[4] = {
6615 L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ,
6616 L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ
6617 };
6618
6619 /* Only I-frames are expected in streaming mode */
6620 if (chan->mode == L2CAP_MODE_STREAMING)
6621 goto drop;
6622
6623 BT_DBG("sframe reqseq %d, final %d, poll %d, super %d",
6624 control->reqseq, control->final, control->poll,
6625 control->super);
6626
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006627 if (len != 0) {
Andrei Emeltchenko1bb166e2012-11-20 17:16:21 +02006628 BT_ERR("Trailing bytes: %d in sframe", len);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006629 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006630 goto drop;
6631 }
6632
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006633 /* Validate F and P bits */
6634 if (control->final && (control->poll ||
6635 chan->tx_state != L2CAP_TX_STATE_WAIT_F))
6636 goto drop;
6637
6638 event = rx_func_to_event[control->super];
6639 if (l2cap_rx(chan, control, skb, event))
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006640 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006641 }
6642
6643 return 0;
6644
6645drop:
6646 kfree_skb(skb);
6647 return 0;
6648}
6649
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006650static void l2cap_chan_le_send_credits(struct l2cap_chan *chan)
6651{
6652 struct l2cap_conn *conn = chan->conn;
6653 struct l2cap_le_credits pkt;
6654 u16 return_credits;
6655
6656 /* We return more credits to the sender only after the amount of
6657 * credits falls below half of the initial amount.
6658 */
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02006659 if (chan->rx_credits >= (le_max_credits + 1) / 2)
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006660 return;
6661
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02006662 return_credits = le_max_credits - chan->rx_credits;
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006663
6664 BT_DBG("chan %p returning %u credits to sender", chan, return_credits);
6665
6666 chan->rx_credits += return_credits;
6667
6668 pkt.cid = cpu_to_le16(chan->scid);
6669 pkt.credits = cpu_to_le16(return_credits);
6670
6671 chan->ident = l2cap_get_ident(conn);
6672
6673 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt);
6674}
6675
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006676static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
6677{
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006678 int err;
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006679
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006680 if (!chan->rx_credits) {
6681 BT_ERR("No credits to receive LE L2CAP data");
Johan Hedbergdfd97742014-01-27 15:11:34 -08006682 l2cap_send_disconn_req(chan, ECONNRESET);
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006683 return -ENOBUFS;
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006684 }
6685
6686 if (chan->imtu < skb->len) {
6687 BT_ERR("Too big LE L2CAP PDU");
6688 return -ENOBUFS;
6689 }
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006690
6691 chan->rx_credits--;
6692 BT_DBG("rx_credits %u -> %u", chan->rx_credits + 1, chan->rx_credits);
6693
6694 l2cap_chan_le_send_credits(chan);
6695
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006696 err = 0;
6697
6698 if (!chan->sdu) {
6699 u16 sdu_len;
6700
6701 sdu_len = get_unaligned_le16(skb->data);
6702 skb_pull(skb, L2CAP_SDULEN_SIZE);
6703
6704 BT_DBG("Start of new SDU. sdu_len %u skb->len %u imtu %u",
6705 sdu_len, skb->len, chan->imtu);
6706
6707 if (sdu_len > chan->imtu) {
6708 BT_ERR("Too big LE L2CAP SDU length received");
6709 err = -EMSGSIZE;
6710 goto failed;
6711 }
6712
6713 if (skb->len > sdu_len) {
6714 BT_ERR("Too much LE L2CAP data received");
6715 err = -EINVAL;
6716 goto failed;
6717 }
6718
6719 if (skb->len == sdu_len)
6720 return chan->ops->recv(chan, skb);
6721
6722 chan->sdu = skb;
6723 chan->sdu_len = sdu_len;
6724 chan->sdu_last_frag = skb;
6725
6726 return 0;
6727 }
6728
6729 BT_DBG("SDU fragment. chan->sdu->len %u skb->len %u chan->sdu_len %u",
6730 chan->sdu->len, skb->len, chan->sdu_len);
6731
6732 if (chan->sdu->len + skb->len > chan->sdu_len) {
6733 BT_ERR("Too much LE L2CAP data received");
6734 err = -EINVAL;
6735 goto failed;
6736 }
6737
6738 append_skb_frag(chan->sdu, skb, &chan->sdu_last_frag);
6739 skb = NULL;
6740
6741 if (chan->sdu->len == chan->sdu_len) {
6742 err = chan->ops->recv(chan, chan->sdu);
6743 if (!err) {
6744 chan->sdu = NULL;
6745 chan->sdu_last_frag = NULL;
6746 chan->sdu_len = 0;
6747 }
6748 }
6749
6750failed:
6751 if (err) {
6752 kfree_skb(skb);
6753 kfree_skb(chan->sdu);
6754 chan->sdu = NULL;
6755 chan->sdu_last_frag = NULL;
6756 chan->sdu_len = 0;
6757 }
6758
6759 /* We can't return an error here since we took care of the skb
6760 * freeing internally. An error return would cause the caller to
6761 * do a double-free of the skb.
6762 */
6763 return 0;
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006764}
6765
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006766static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid,
6767 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006769 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03006771 chan = l2cap_get_chan_by_scid(conn, cid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006772 if (!chan) {
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006773 if (cid == L2CAP_CID_A2MP) {
6774 chan = a2mp_channel_create(conn, skb);
6775 if (!chan) {
6776 kfree_skb(skb);
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006777 return;
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006778 }
6779
6780 l2cap_chan_lock(chan);
6781 } else {
6782 BT_DBG("unknown cid 0x%4.4x", cid);
6783 /* Drop packet and return */
6784 kfree_skb(skb);
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006785 return;
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 }
6788
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03006789 BT_DBG("chan %p, len %d", chan, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03006791 if (chan->state != BT_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 goto drop;
6793
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006794 switch (chan->mode) {
Johan Hedberg38319712013-05-17 12:49:23 +03006795 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006796 if (l2cap_le_data_rcv(chan, skb) < 0)
6797 goto drop;
6798
6799 goto done;
6800
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006801 case L2CAP_MODE_BASIC:
6802 /* If socket recv buffers overflows we drop data here
6803 * which is *bad* because L2CAP has to be reliable.
6804 * But we don't have any other choice. L2CAP doesn't
6805 * provide flow control mechanism. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
Szymon Janc2c96e032014-02-18 20:48:34 +01006807 if (chan->imtu < skb->len) {
6808 BT_ERR("Dropping L2CAP data: receive buffer overflow");
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006809 goto drop;
Szymon Janc2c96e032014-02-18 20:48:34 +01006810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811
Gustavo Padovan80b98022012-05-27 22:27:51 -03006812 if (!chan->ops->recv(chan, skb))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006813 goto done;
6814 break;
6815
6816 case L2CAP_MODE_ERTM:
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03006817 case L2CAP_MODE_STREAMING:
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006818 l2cap_data_rcv(chan, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03006819 goto done;
6820
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006821 default:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006822 BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006823 break;
6824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825
6826drop:
6827 kfree_skb(skb);
6828
6829done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006830 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831}
6832
Andrei Emeltchenko84104b22012-05-31 11:18:56 +03006833static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
6834 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835{
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006836 struct hci_conn *hcon = conn->hcon;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006837 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006839 if (hcon->type != ACL_LINK)
6840 goto drop;
6841
Johan Hedbergbf20fd42013-05-14 13:23:13 +03006842 chan = l2cap_global_chan_by_psm(0, psm, &hcon->src, &hcon->dst,
6843 ACL_LINK);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006844 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 goto drop;
6846
Andrei Emeltchenko5b4ceda2012-02-24 16:35:32 +02006847 BT_DBG("chan %p, len %d", chan, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03006849 if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850 goto drop;
6851
Vinicius Costa Gomese13e21d2011-06-17 22:46:27 -03006852 if (chan->imtu < skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 goto drop;
6854
Marcel Holtmann2edf8702013-10-13 12:55:29 -07006855 /* Store remote BD_ADDR and PSM for msg_name */
Marcel Holtmann06ae3312013-10-18 03:43:00 -07006856 bacpy(&bt_cb(skb)->bdaddr, &hcon->dst);
Marcel Holtmann2edf8702013-10-13 12:55:29 -07006857 bt_cb(skb)->psm = psm;
6858
Gustavo Padovan80b98022012-05-27 22:27:51 -03006859 if (!chan->ops->recv(chan, skb))
Andrei Emeltchenko84104b22012-05-31 11:18:56 +03006860 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861
6862drop:
6863 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864}
6865
Marcel Holtmann72f78352013-04-23 00:59:00 -07006866static void l2cap_att_channel(struct l2cap_conn *conn,
Andrei Emeltchenko6810fca2012-05-31 11:18:57 +03006867 struct sk_buff *skb)
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006868{
Marcel Holtmannb99707d2013-10-03 02:54:11 -07006869 struct hci_conn *hcon = conn->hcon;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006870 struct l2cap_chan *chan;
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006871
Marcel Holtmannb99707d2013-10-03 02:54:11 -07006872 if (hcon->type != LE_LINK)
6873 goto drop;
6874
Johan Hedbergaf1c0132013-04-29 19:35:42 +03006875 chan = l2cap_global_chan_by_scid(BT_CONNECTED, L2CAP_CID_ATT,
Marcel Holtmann06ae3312013-10-18 03:43:00 -07006876 &hcon->src, &hcon->dst);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006877 if (!chan)
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006878 goto drop;
6879
Andrei Emeltchenko5b4ceda2012-02-24 16:35:32 +02006880 BT_DBG("chan %p, len %d", chan, skb->len);
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006881
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07006882 if (hci_blacklist_lookup(hcon->hdev, &hcon->dst, hcon->dst_type))
6883 goto drop;
6884
Vinicius Costa Gomese13e21d2011-06-17 22:46:27 -03006885 if (chan->imtu < skb->len)
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006886 goto drop;
6887
Gustavo Padovan80b98022012-05-27 22:27:51 -03006888 if (!chan->ops->recv(chan, skb))
Andrei Emeltchenko6810fca2012-05-31 11:18:57 +03006889 return;
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006890
6891drop:
6892 kfree_skb(skb);
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006893}
6894
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
6896{
6897 struct l2cap_hdr *lh = (void *) skb->data;
Johan Hedberg61a939c2014-01-17 20:45:11 +02006898 struct hci_conn *hcon = conn->hcon;
Al Viro8e036fc2007-07-29 00:16:36 -07006899 u16 cid, len;
6900 __le16 psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901
Johan Hedberg61a939c2014-01-17 20:45:11 +02006902 if (hcon->state != BT_CONNECTED) {
6903 BT_DBG("queueing pending rx skb");
6904 skb_queue_tail(&conn->pending_rx, skb);
6905 return;
6906 }
6907
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 skb_pull(skb, L2CAP_HDR_SIZE);
6909 cid = __le16_to_cpu(lh->cid);
6910 len = __le16_to_cpu(lh->len);
6911
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006912 if (len != skb->len) {
6913 kfree_skb(skb);
6914 return;
6915 }
6916
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 BT_DBG("len %d, cid 0x%4.4x", len, cid);
6918
6919 switch (cid) {
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -03006920 case L2CAP_CID_SIGNALING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 l2cap_sig_channel(conn, skb);
6922 break;
6923
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -03006924 case L2CAP_CID_CONN_LESS:
Andrei Emeltchenko097db762012-03-09 14:16:17 +02006925 psm = get_unaligned((__le16 *) skb->data);
Andrei Emeltchenko0181a702012-05-29 10:04:05 +03006926 skb_pull(skb, L2CAP_PSMLEN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 l2cap_conless_channel(conn, psm, skb);
6928 break;
6929
Johan Hedberg073d1cf2013-04-29 19:35:35 +03006930 case L2CAP_CID_ATT:
Marcel Holtmann72f78352013-04-23 00:59:00 -07006931 l2cap_att_channel(conn, skb);
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006932 break;
6933
Marcel Holtmanna2877622013-10-02 23:46:54 -07006934 case L2CAP_CID_LE_SIGNALING:
6935 l2cap_le_sig_channel(conn, skb);
6936 break;
6937
Anderson Brigliab501d6a2011-06-07 18:46:31 -03006938 case L2CAP_CID_SMP:
6939 if (smp_sig_channel(conn, skb))
6940 l2cap_conn_del(conn->hcon, EACCES);
6941 break;
6942
Jukka Rissanen18722c22013-12-11 17:05:37 +02006943 case L2CAP_FC_6LOWPAN:
6944 bt_6lowpan_recv(conn, skb);
6945 break;
6946
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 default:
6948 l2cap_data_channel(conn, cid, skb);
6949 break;
6950 }
6951}
6952
Johan Hedberg61a939c2014-01-17 20:45:11 +02006953static void process_pending_rx(struct work_struct *work)
6954{
6955 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
6956 pending_rx_work);
6957 struct sk_buff *skb;
6958
6959 BT_DBG("");
6960
6961 while ((skb = skb_dequeue(&conn->pending_rx)))
6962 l2cap_recv_frame(conn, skb);
6963}
6964
Johan Hedberg162b49e2014-01-17 20:45:10 +02006965static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon)
6966{
6967 struct l2cap_conn *conn = hcon->l2cap_data;
6968 struct hci_chan *hchan;
6969
6970 if (conn)
6971 return conn;
6972
6973 hchan = hci_chan_create(hcon);
6974 if (!hchan)
6975 return NULL;
6976
6977 conn = kzalloc(sizeof(struct l2cap_conn), GFP_KERNEL);
6978 if (!conn) {
6979 hci_chan_del(hchan);
6980 return NULL;
6981 }
6982
6983 kref_init(&conn->ref);
6984 hcon->l2cap_data = conn;
6985 conn->hcon = hcon;
6986 hci_conn_get(conn->hcon);
6987 conn->hchan = hchan;
6988
6989 BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan);
6990
6991 switch (hcon->type) {
6992 case LE_LINK:
6993 if (hcon->hdev->le_mtu) {
6994 conn->mtu = hcon->hdev->le_mtu;
6995 break;
6996 }
6997 /* fall through */
6998 default:
6999 conn->mtu = hcon->hdev->acl_mtu;
7000 break;
7001 }
7002
7003 conn->feat_mask = 0;
7004
7005 if (hcon->type == ACL_LINK)
7006 conn->hs_enabled = test_bit(HCI_HS_ENABLED,
7007 &hcon->hdev->dev_flags);
7008
7009 spin_lock_init(&conn->lock);
7010 mutex_init(&conn->chan_lock);
7011
7012 INIT_LIST_HEAD(&conn->chan_l);
7013 INIT_LIST_HEAD(&conn->users);
7014
7015 if (hcon->type == LE_LINK)
7016 INIT_DELAYED_WORK(&conn->security_timer, security_timeout);
7017 else
7018 INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout);
7019
Johan Hedberg61a939c2014-01-17 20:45:11 +02007020 skb_queue_head_init(&conn->pending_rx);
7021 INIT_WORK(&conn->pending_rx_work, process_pending_rx);
7022
Johan Hedberg162b49e2014-01-17 20:45:10 +02007023 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
7024
7025 return conn;
7026}
7027
7028static bool is_valid_psm(u16 psm, u8 dst_type) {
7029 if (!psm)
7030 return false;
7031
7032 if (bdaddr_type_is_le(dst_type))
7033 return (psm <= 0x00ff);
7034
7035 /* PSM must be odd and lsb of upper byte must be 0 */
7036 return ((psm & 0x0101) == 0x0001);
7037}
7038
7039int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
7040 bdaddr_t *dst, u8 dst_type)
7041{
7042 struct l2cap_conn *conn;
7043 struct hci_conn *hcon;
7044 struct hci_dev *hdev;
7045 __u8 auth_type;
7046 int err;
7047
7048 BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", &chan->src, dst,
7049 dst_type, __le16_to_cpu(psm));
7050
7051 hdev = hci_get_route(dst, &chan->src);
7052 if (!hdev)
7053 return -EHOSTUNREACH;
7054
7055 hci_dev_lock(hdev);
7056
7057 l2cap_chan_lock(chan);
7058
7059 if (!is_valid_psm(__le16_to_cpu(psm), dst_type) && !cid &&
7060 chan->chan_type != L2CAP_CHAN_RAW) {
7061 err = -EINVAL;
7062 goto done;
7063 }
7064
Johan Hedberg21626e62014-01-24 10:35:41 +02007065 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !psm) {
7066 err = -EINVAL;
7067 goto done;
7068 }
7069
7070 if (chan->chan_type == L2CAP_CHAN_FIXED && !cid) {
Johan Hedberg162b49e2014-01-17 20:45:10 +02007071 err = -EINVAL;
7072 goto done;
7073 }
7074
7075 switch (chan->mode) {
7076 case L2CAP_MODE_BASIC:
7077 break;
7078 case L2CAP_MODE_LE_FLOWCTL:
7079 l2cap_le_flowctl_init(chan);
7080 break;
7081 case L2CAP_MODE_ERTM:
7082 case L2CAP_MODE_STREAMING:
7083 if (!disable_ertm)
7084 break;
7085 /* fall through */
7086 default:
7087 err = -ENOTSUPP;
7088 goto done;
7089 }
7090
7091 switch (chan->state) {
7092 case BT_CONNECT:
7093 case BT_CONNECT2:
7094 case BT_CONFIG:
7095 /* Already connecting */
7096 err = 0;
7097 goto done;
7098
7099 case BT_CONNECTED:
7100 /* Already connected */
7101 err = -EISCONN;
7102 goto done;
7103
7104 case BT_OPEN:
7105 case BT_BOUND:
7106 /* Can connect */
7107 break;
7108
7109 default:
7110 err = -EBADFD;
7111 goto done;
7112 }
7113
7114 /* Set destination address and psm */
7115 bacpy(&chan->dst, dst);
7116 chan->dst_type = dst_type;
7117
7118 chan->psm = psm;
7119 chan->dcid = cid;
7120
7121 auth_type = l2cap_get_auth_type(chan);
7122
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007123 if (bdaddr_type_is_le(dst_type)) {
7124 /* Convert from L2CAP channel address type to HCI address type
7125 */
7126 if (dst_type == BDADDR_LE_PUBLIC)
7127 dst_type = ADDR_LE_DEV_PUBLIC;
7128 else
7129 dst_type = ADDR_LE_DEV_RANDOM;
7130
Andre Guedes04a6c582014-02-26 20:21:44 -03007131 hcon = hci_connect_le(hdev, dst, dst_type, chan->sec_level,
7132 auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007133 } else {
Andre Guedes04a6c582014-02-26 20:21:44 -03007134 hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007135 }
Johan Hedberg162b49e2014-01-17 20:45:10 +02007136
7137 if (IS_ERR(hcon)) {
7138 err = PTR_ERR(hcon);
7139 goto done;
7140 }
7141
7142 conn = l2cap_conn_add(hcon);
7143 if (!conn) {
7144 hci_conn_drop(hcon);
7145 err = -ENOMEM;
7146 goto done;
7147 }
7148
7149 if (cid && __l2cap_get_chan_by_dcid(conn, cid)) {
7150 hci_conn_drop(hcon);
7151 err = -EBUSY;
7152 goto done;
7153 }
7154
7155 /* Update source addr of the socket */
7156 bacpy(&chan->src, &hcon->src);
7157 chan->src_type = bdaddr_type(hcon, hcon->src_type);
7158
7159 l2cap_chan_unlock(chan);
7160 l2cap_chan_add(conn, chan);
7161 l2cap_chan_lock(chan);
7162
7163 /* l2cap_chan_add takes its own ref so we can drop this one */
7164 hci_conn_drop(hcon);
7165
7166 l2cap_state_change(chan, BT_CONNECT);
7167 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
7168
Johan Hedberg61202e42014-01-28 15:16:48 -08007169 /* Release chan->sport so that it can be reused by other
7170 * sockets (as it's only used for listening sockets).
7171 */
7172 write_lock(&chan_list_lock);
7173 chan->sport = 0;
7174 write_unlock(&chan_list_lock);
7175
Johan Hedberg162b49e2014-01-17 20:45:10 +02007176 if (hcon->state == BT_CONNECTED) {
7177 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
7178 __clear_chan_timer(chan);
7179 if (l2cap_chan_check_security(chan))
7180 l2cap_state_change(chan, BT_CONNECTED);
7181 } else
7182 l2cap_do_start(chan);
7183 }
7184
7185 err = 0;
7186
7187done:
7188 l2cap_chan_unlock(chan);
7189 hci_dev_unlock(hdev);
7190 hci_dev_put(hdev);
7191 return err;
7192}
7193
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194/* ---- L2CAP interface with lower layer (HCI) ---- */
7195
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007196int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197{
7198 int exact = 0, lm1 = 0, lm2 = 0;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007199 struct l2cap_chan *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03007201 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202
7203 /* Find listening sockets and check their link_mode */
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007204 read_lock(&chan_list_lock);
7205 list_for_each_entry(c, &chan_list, global_l) {
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03007206 if (c->state != BT_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207 continue;
7208
Marcel Holtmann7eafc592013-10-13 08:12:47 -07007209 if (!bacmp(&c->src, &hdev->bdaddr)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007210 lm1 |= HCI_LM_ACCEPT;
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +03007211 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007212 lm1 |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 exact++;
Marcel Holtmann7eafc592013-10-13 08:12:47 -07007214 } else if (!bacmp(&c->src, BDADDR_ANY)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007215 lm2 |= HCI_LM_ACCEPT;
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +03007216 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007217 lm2 |= HCI_LM_MASTER;
7218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 }
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007220 read_unlock(&chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221
7222 return exact ? lm1 : lm2;
7223}
7224
Andrei Emeltchenko9e664632012-07-24 16:06:15 +03007225void l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226{
Marcel Holtmann01394182006-07-03 10:02:46 +02007227 struct l2cap_conn *conn;
7228
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03007229 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 if (!status) {
Claudio Takahasibaf43252013-04-11 13:55:50 -03007232 conn = l2cap_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 if (conn)
7234 l2cap_conn_ready(conn);
Andrei Emeltchenkoba6fc312012-10-31 15:46:26 +02007235 } else {
Joe Perchese1750722011-06-29 18:18:29 -07007236 l2cap_conn_del(hcon, bt_to_errno(status));
Andrei Emeltchenkoba6fc312012-10-31 15:46:26 +02007237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238}
7239
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007240int l2cap_disconn_ind(struct hci_conn *hcon)
Marcel Holtmann2950f212009-02-12 14:02:50 +01007241{
7242 struct l2cap_conn *conn = hcon->l2cap_data;
7243
7244 BT_DBG("hcon %p", hcon);
7245
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007246 if (!conn)
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02007247 return HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +01007248 return conn->disc_reason;
7249}
7250
Andrei Emeltchenko9e664632012-07-24 16:06:15 +03007251void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252{
7253 BT_DBG("hcon %p reason %d", hcon, reason);
7254
Jukka Rissanen18722c22013-12-11 17:05:37 +02007255 bt_6lowpan_del_conn(hcon->l2cap_data);
7256
Joe Perchese1750722011-06-29 18:18:29 -07007257 l2cap_conn_del(hcon, bt_to_errno(reason));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258}
7259
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007260static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007261{
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03007262 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
Marcel Holtmann255c7602009-02-04 21:07:19 +01007263 return;
7264
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007265 if (encrypt == 0x00) {
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007266 if (chan->sec_level == BT_SECURITY_MEDIUM) {
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007267 __set_chan_timer(chan, L2CAP_ENC_TIMEOUT);
Marcel Holtmann7d513e92014-01-15 22:37:40 -08007268 } else if (chan->sec_level == BT_SECURITY_HIGH ||
7269 chan->sec_level == BT_SECURITY_FIPS)
Gustavo F. Padovan0f852722011-05-04 19:42:50 -03007270 l2cap_chan_close(chan, ECONNREFUSED);
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007271 } else {
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007272 if (chan->sec_level == BT_SECURITY_MEDIUM)
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -03007273 __clear_chan_timer(chan);
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007274 }
7275}
7276
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007277int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278{
Marcel Holtmann40be4922008-07-14 20:13:50 +02007279 struct l2cap_conn *conn = hcon->l2cap_data;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03007280 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281
Marcel Holtmann01394182006-07-03 10:02:46 +02007282 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 return 0;
Marcel Holtmann01394182006-07-03 10:02:46 +02007284
Andrei Emeltchenko89d8b402012-07-10 15:27:51 +03007285 BT_DBG("conn %p status 0x%2.2x encrypt %u", conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286
Vinicius Costa Gomes160dc6a2011-08-19 21:06:55 -03007287 if (hcon->type == LE_LINK) {
Hemant Gupta35d4adcc2012-04-18 14:46:26 +05307288 if (!status && encrypt)
Johan Hedberg4bd6d382014-02-26 23:33:45 +02007289 smp_distribute_keys(conn);
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02007290 cancel_delayed_work(&conn->security_timer);
Vinicius Costa Gomes160dc6a2011-08-19 21:06:55 -03007291 }
7292
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007293 mutex_lock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007295 list_for_each_entry(chan, &conn->chan_l, list) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007296 l2cap_chan_lock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297
Andrei Emeltchenko89d8b402012-07-10 15:27:51 +03007298 BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid,
7299 state_to_string(chan->state));
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007300
Johan Hedberg2338a7e2014-01-24 10:35:40 +02007301 if (chan->scid == L2CAP_CID_A2MP) {
Andrei Emeltchenko78eb2f92012-07-19 17:03:47 +03007302 l2cap_chan_unlock(chan);
7303 continue;
7304 }
7305
Johan Hedberg073d1cf2013-04-29 19:35:35 +03007306 if (chan->scid == L2CAP_CID_ATT) {
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007307 if (!status && encrypt) {
7308 chan->sec_level = hcon->sec_level;
Andrei Emeltchenkocf4cd002012-02-06 15:03:59 +02007309 l2cap_chan_ready(chan);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007310 }
7311
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007312 l2cap_chan_unlock(chan);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007313 continue;
7314 }
7315
Andrei Emeltchenko96eff462012-11-15 18:14:53 +02007316 if (!__l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007317 l2cap_chan_unlock(chan);
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01007318 continue;
7319 }
7320
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03007321 if (!status && (chan->state == BT_CONNECTED ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01007322 chan->state == BT_CONFIG)) {
Marcel Holtmannd97c8992013-10-14 02:53:54 -07007323 chan->ops->resume(chan);
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007324 l2cap_check_encryption(chan, encrypt);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007325 l2cap_chan_unlock(chan);
Marcel Holtmann9719f8a2008-07-14 20:13:45 +02007326 continue;
7327 }
7328
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03007329 if (chan->state == BT_CONNECT) {
Johan Hedberg6d3c15d2013-12-02 22:13:24 +02007330 if (!status)
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03007331 l2cap_start_connection(chan);
Johan Hedberg6d3c15d2013-12-02 22:13:24 +02007332 else
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007333 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03007334 } else if (chan->state == BT_CONNECT2) {
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007335 struct l2cap_conn_rsp rsp;
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007336 __u16 res, stat;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007337
7338 if (!status) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07007339 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007340 res = L2CAP_CR_PEND;
7341 stat = L2CAP_CS_AUTHOR_PEND;
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08007342 chan->ops->defer(chan);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007343 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02007344 l2cap_state_change(chan, BT_CONFIG);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007345 res = L2CAP_CR_SUCCESS;
7346 stat = L2CAP_CS_NO_INFO;
7347 }
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007348 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02007349 l2cap_state_change(chan, BT_DISCONN);
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007350 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007351 res = L2CAP_CR_SEC_BLOCK;
7352 stat = L2CAP_CS_NO_INFO;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007353 }
7354
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03007355 rsp.scid = cpu_to_le16(chan->dcid);
7356 rsp.dcid = cpu_to_le16(chan->scid);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007357 rsp.result = cpu_to_le16(res);
7358 rsp.status = cpu_to_le16(stat);
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03007359 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01007360 sizeof(rsp), &rsp);
Mat Martineau2d369352012-05-23 14:59:30 -07007361
7362 if (!test_bit(CONF_REQ_SENT, &chan->conf_state) &&
7363 res == L2CAP_CR_SUCCESS) {
7364 char buf[128];
7365 set_bit(CONF_REQ_SENT, &chan->conf_state);
7366 l2cap_send_cmd(conn, l2cap_get_ident(conn),
7367 L2CAP_CONF_REQ,
7368 l2cap_build_conf_req(chan, buf),
7369 buf);
7370 chan->num_conf_req++;
7371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372 }
7373
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007374 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 }
7376
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007377 mutex_unlock(&conn->chan_lock);
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007378
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379 return 0;
7380}
7381
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007382int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383{
7384 struct l2cap_conn *conn = hcon->l2cap_data;
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007385 struct l2cap_hdr *hdr;
7386 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387
Andrei Emeltchenko1d13a252012-10-15 11:58:41 +03007388 /* For AMP controller do not create l2cap conn */
7389 if (!conn && hcon->hdev->dev_type != HCI_BREDR)
7390 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02007392 if (!conn)
Claudio Takahasibaf43252013-04-11 13:55:50 -03007393 conn = l2cap_conn_add(hcon);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02007394
7395 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 goto drop;
7397
7398 BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
7399
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007400 switch (flags) {
7401 case ACL_START:
7402 case ACL_START_NO_FLUSH:
7403 case ACL_COMPLETE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 if (conn->rx_len) {
7405 BT_ERR("Unexpected start frame (len %d)", skb->len);
7406 kfree_skb(conn->rx_skb);
7407 conn->rx_skb = NULL;
7408 conn->rx_len = 0;
7409 l2cap_conn_unreliable(conn, ECOMM);
7410 }
7411
Andrei Emeltchenkoaae7fe22010-09-15 14:28:43 +03007412 /* Start fragment always begin with Basic L2CAP header */
7413 if (skb->len < L2CAP_HDR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414 BT_ERR("Frame is too short (len %d)", skb->len);
7415 l2cap_conn_unreliable(conn, ECOMM);
7416 goto drop;
7417 }
7418
7419 hdr = (struct l2cap_hdr *) skb->data;
7420 len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
7421
7422 if (len == skb->len) {
7423 /* Complete frame received */
7424 l2cap_recv_frame(conn, skb);
7425 return 0;
7426 }
7427
7428 BT_DBG("Start: total len %d, frag len %d", len, skb->len);
7429
7430 if (skb->len > len) {
7431 BT_ERR("Frame is too long (len %d, expected len %d)",
Gustavo Padovan2d792812012-10-06 10:07:01 +01007432 skb->len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 l2cap_conn_unreliable(conn, ECOMM);
7434 goto drop;
7435 }
7436
7437 /* Allocate skb for the complete frame (with header) */
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03007438 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03007439 if (!conn->rx_skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440 goto drop;
7441
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03007442 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Gustavo Padovan2d792812012-10-06 10:07:01 +01007443 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444 conn->rx_len = len - skb->len;
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007445 break;
7446
7447 case ACL_CONT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
7449
7450 if (!conn->rx_len) {
7451 BT_ERR("Unexpected continuation frame (len %d)", skb->len);
7452 l2cap_conn_unreliable(conn, ECOMM);
7453 goto drop;
7454 }
7455
7456 if (skb->len > conn->rx_len) {
7457 BT_ERR("Fragment is too long (len %d, expected %d)",
Gustavo Padovan2d792812012-10-06 10:07:01 +01007458 skb->len, conn->rx_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 kfree_skb(conn->rx_skb);
7460 conn->rx_skb = NULL;
7461 conn->rx_len = 0;
7462 l2cap_conn_unreliable(conn, ECOMM);
7463 goto drop;
7464 }
7465
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03007466 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Gustavo Padovan2d792812012-10-06 10:07:01 +01007467 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 conn->rx_len -= skb->len;
7469
7470 if (!conn->rx_len) {
Johan Hedbergc4e5baf2013-10-10 13:33:37 +02007471 /* Complete frame received. l2cap_recv_frame
7472 * takes ownership of the skb so set the global
7473 * rx_skb pointer to NULL first.
7474 */
7475 struct sk_buff *rx_skb = conn->rx_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 conn->rx_skb = NULL;
Johan Hedbergc4e5baf2013-10-10 13:33:37 +02007477 l2cap_recv_frame(conn, rx_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 }
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007479 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 }
7481
7482drop:
7483 kfree_skb(skb);
7484 return 0;
7485}
7486
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007487static int l2cap_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007489 struct l2cap_chan *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490
Gustavo F. Padovan333055f2011-12-22 15:14:39 -02007491 read_lock(&chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007493 list_for_each_entry(c, &chan_list, global_l) {
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03007494 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 -07007495 &c->src, &c->dst,
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03007496 c->state, __le16_to_cpu(c->psm),
7497 c->scid, c->dcid, c->imtu, c->omtu,
7498 c->sec_level, c->mode);
Andrei Emeltchenko61e1b4b2012-01-19 11:19:50 +02007499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500
Gustavo F. Padovan333055f2011-12-22 15:14:39 -02007501 read_unlock(&chan_list_lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08007502
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007503 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504}
7505
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007506static int l2cap_debugfs_open(struct inode *inode, struct file *file)
7507{
7508 return single_open(file, l2cap_debugfs_show, inode->i_private);
7509}
7510
7511static const struct file_operations l2cap_debugfs_fops = {
7512 .open = l2cap_debugfs_open,
7513 .read = seq_read,
7514 .llseek = seq_lseek,
7515 .release = single_release,
7516};
7517
7518static struct dentry *l2cap_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519
Gustavo F. Padovan64274512011-02-07 20:08:52 -02007520int __init l2cap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521{
7522 int err;
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08007523
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02007524 err = l2cap_init_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 if (err < 0)
7526 return err;
7527
Marcel Holtmann1120e4b2013-10-17 17:24:16 -07007528 if (IS_ERR_OR_NULL(bt_debugfs))
7529 return 0;
7530
7531 l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs,
7532 NULL, &l2cap_debugfs_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533
Samuel Ortiz40b93972014-05-14 17:53:35 +02007534 debugfs_create_u16("l2cap_le_max_credits", 0644, bt_debugfs,
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02007535 &le_max_credits);
Samuel Ortiz40b93972014-05-14 17:53:35 +02007536 debugfs_create_u16("l2cap_le_default_mps", 0644, bt_debugfs,
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02007537 &le_default_mps);
7538
Jukka Rissanen18722c22013-12-11 17:05:37 +02007539 bt_6lowpan_init();
7540
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542}
7543
Gustavo F. Padovan64274512011-02-07 20:08:52 -02007544void l2cap_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545{
Jukka Rissanen18722c22013-12-11 17:05:37 +02007546 bt_6lowpan_cleanup();
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007547 debugfs_remove(l2cap_debugfs);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02007548 l2cap_cleanup_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549}
7550
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03007551module_param(disable_ertm, bool, 0644);
7552MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");