blob: 0cd7ed99558b49f9dc49795ff3b68a8220f4da50 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Johan Hedberg0f1bfe42014-01-27 15:11:35 -080044#define LE_FLOWCTL_MAX_CREDITS 65535
45
Mat Martineaud1de6d42012-05-17 20:53:55 -070046bool disable_ertm;
Marcel Holtmannf0709e02007-10-20 13:38:51 +020047
Marcel Holtmann547d1032013-10-12 08:18:19 -070048static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN | L2CAP_FEAT_UCD;
Marcel Holtmanna6801ca2014-07-11 06:03:08 +020049static u8 l2cap_fixed_chan[8] = { L2CAP_FC_SIG_BREDR | L2CAP_FC_CONNLESS, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Johannes Bergb5ad8b72011-06-01 08:54:45 +020051static LIST_HEAD(chan_list);
52static DEFINE_RWLOCK(chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Johan Hedbergf15b8ec2013-12-03 15:08:25 +020054static u16 le_max_credits = L2CAP_LE_MAX_CREDITS;
55static u16 le_default_mps = L2CAP_LE_DEFAULT_MPS;
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +010058 u8 code, u8 ident, u16 dlen, void *data);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -030059static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
Gustavo Padovan2d792812012-10-06 10:07:01 +010060 void *data);
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -030061static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +020062static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Gustavo Padovand6603662012-05-21 13:58:22 -030064static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
Gustavo Padovan2d792812012-10-06 10:07:01 +010065 struct sk_buff_head *skbs, u8 event);
Mat Martineau608bcc62012-05-17 20:53:32 -070066
Marcel Holtmann4f1654e2013-10-13 08:50:41 -070067static inline __u8 bdaddr_type(struct hci_conn *hcon, __u8 type)
68{
69 if (hcon->type == LE_LINK) {
70 if (type == ADDR_LE_DEV_PUBLIC)
71 return BDADDR_LE_PUBLIC;
72 else
73 return BDADDR_LE_RANDOM;
74 }
75
76 return BDADDR_BREDR;
77}
78
Marcel Holtmann01394182006-07-03 10:02:46 +020079/* ---- L2CAP channels ---- */
Gustavo F. Padovan71ba0e52011-05-17 14:34:52 -030080
Gustavo Padovan2d792812012-10-06 10:07:01 +010081static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
82 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +020083{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020084 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -030085
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020086 list_for_each_entry(c, &conn->chan_l, list) {
87 if (c->dcid == cid)
88 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +020089 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020090 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +020091}
92
Gustavo Padovan2d792812012-10-06 10:07:01 +010093static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn,
94 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +020095{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020096 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -030097
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020098 list_for_each_entry(c, &conn->chan_l, list) {
99 if (c->scid == cid)
100 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +0200101 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200102 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200103}
104
105/* Find channel with given SCID.
Mat Martineauef191ad2012-05-02 09:42:00 -0700106 * Returns locked channel. */
Gustavo Padovan2d792812012-10-06 10:07:01 +0100107static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn,
108 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +0200109{
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300110 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300111
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200112 mutex_lock(&conn->chan_lock);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300113 c = __l2cap_get_chan_by_scid(conn, cid);
Mat Martineauef191ad2012-05-02 09:42:00 -0700114 if (c)
115 l2cap_chan_lock(c);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200116 mutex_unlock(&conn->chan_lock);
117
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300118 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +0200119}
120
Mat Martineaub1a130b2012-10-23 15:24:09 -0700121/* Find channel with given DCID.
122 * Returns locked channel.
123 */
124static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
125 u16 cid)
126{
127 struct l2cap_chan *c;
128
129 mutex_lock(&conn->chan_lock);
130 c = __l2cap_get_chan_by_dcid(conn, cid);
131 if (c)
132 l2cap_chan_lock(c);
133 mutex_unlock(&conn->chan_lock);
134
135 return c;
136}
137
Gustavo Padovan2d792812012-10-06 10:07:01 +0100138static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn,
139 u8 ident)
Marcel Holtmann01394182006-07-03 10:02:46 +0200140{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200141 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300142
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200143 list_for_each_entry(c, &conn->chan_l, list) {
144 if (c->ident == ident)
145 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +0200146 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200147 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200148}
149
Mat Martineau5b155ef2012-10-23 15:24:14 -0700150static struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn,
151 u8 ident)
152{
153 struct l2cap_chan *c;
154
155 mutex_lock(&conn->chan_lock);
156 c = __l2cap_get_chan_by_ident(conn, ident);
157 if (c)
158 l2cap_chan_lock(c);
159 mutex_unlock(&conn->chan_lock);
160
161 return c;
162}
163
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300164static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300165{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300166 struct l2cap_chan *c;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300167
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300168 list_for_each_entry(c, &chan_list, global_l) {
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700169 if (c->sport == psm && !bacmp(&c->src, src))
Szymon Janc250938c2011-11-16 09:32:22 +0100170 return c;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300171 }
Szymon Janc250938c2011-11-16 09:32:22 +0100172 return NULL;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300173}
174
175int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm)
176{
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300177 int err;
178
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200179 write_lock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300180
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300181 if (psm && __l2cap_global_chan_by_addr(psm, src)) {
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300182 err = -EADDRINUSE;
183 goto done;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300184 }
185
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300186 if (psm) {
187 chan->psm = psm;
188 chan->sport = psm;
189 err = 0;
190 } else {
191 u16 p;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300192
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300193 err = -EINVAL;
194 for (p = 0x1001; p < 0x1100; p += 2)
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300195 if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300196 chan->psm = cpu_to_le16(p);
197 chan->sport = cpu_to_le16(p);
198 err = 0;
199 break;
200 }
201 }
202
203done:
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200204 write_unlock(&chan_list_lock);
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300205 return err;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300206}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300207EXPORT_SYMBOL_GPL(l2cap_add_psm);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300208
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
Johan Hedberg14824302014-08-07 22:56:50 +0300213 /* Override the defaults (which are for conn-oriented) */
214 chan->omtu = L2CAP_DEFAULT_MTU;
215 chan->chan_type = L2CAP_CHAN_FIXED;
216
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300217 chan->scid = scid;
218
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200219 write_unlock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300220
221 return 0;
222}
223
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300224static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
Marcel Holtmann01394182006-07-03 10:02:46 +0200225{
Johan Hedberge77af752013-10-08 10:31:00 +0200226 u16 cid, dyn_end;
Marcel Holtmann01394182006-07-03 10:02:46 +0200227
Johan Hedberge77af752013-10-08 10:31:00 +0200228 if (conn->hcon->type == LE_LINK)
229 dyn_end = L2CAP_CID_LE_DYN_END;
230 else
231 dyn_end = L2CAP_CID_DYN_END;
232
233 for (cid = L2CAP_CID_DYN_START; cid < dyn_end; cid++) {
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300234 if (!__l2cap_get_chan_by_scid(conn, cid))
Marcel Holtmann01394182006-07-03 10:02:46 +0200235 return cid;
236 }
237
238 return 0;
239}
240
Gustavo Padovanf93fa272013-10-21 14:21:40 -0200241static void l2cap_state_change(struct l2cap_chan *chan, int state)
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300242{
Andrei Emeltchenko42d2d872012-02-17 11:40:57 +0200243 BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state),
Gustavo Padovan2d792812012-10-06 10:07:01 +0100244 state_to_string(state));
Gustavo F. Padovanbadaaa02011-11-23 20:11:46 -0200245
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300246 chan->state = state;
Gustavo Padovan53f52122013-10-15 19:24:45 -0300247 chan->ops->state_change(chan, state, 0);
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300248}
249
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300250static inline void l2cap_state_change_and_error(struct l2cap_chan *chan,
251 int state, int err)
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200252{
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300253 chan->state = state;
Gustavo Padovan53f52122013-10-15 19:24:45 -0300254 chan->ops->state_change(chan, chan->state, err);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200255}
256
257static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err)
258{
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300259 chan->ops->state_change(chan, chan->state, err);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200260}
261
Mat Martineau4239d162012-05-17 20:53:49 -0700262static void __set_retrans_timer(struct l2cap_chan *chan)
263{
264 if (!delayed_work_pending(&chan->monitor_timer) &&
265 chan->retrans_timeout) {
266 l2cap_set_timer(chan, &chan->retrans_timer,
267 msecs_to_jiffies(chan->retrans_timeout));
268 }
269}
270
271static void __set_monitor_timer(struct l2cap_chan *chan)
272{
273 __clear_retrans_timer(chan);
274 if (chan->monitor_timeout) {
275 l2cap_set_timer(chan, &chan->monitor_timer,
276 msecs_to_jiffies(chan->monitor_timeout));
277 }
278}
279
Mat Martineau608bcc62012-05-17 20:53:32 -0700280static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head,
281 u16 seq)
282{
283 struct sk_buff *skb;
284
285 skb_queue_walk(head, skb) {
286 if (bt_cb(skb)->control.txseq == seq)
287 return skb;
288 }
289
290 return NULL;
291}
292
Mat Martineau3c588192012-04-11 10:48:42 -0700293/* ---- L2CAP sequence number lists ---- */
294
295/* For ERTM, ordered lists of sequence numbers must be tracked for
296 * SREJ requests that are received and for frames that are to be
297 * retransmitted. These seq_list functions implement a singly-linked
298 * list in an array, where membership in the list can also be checked
299 * in constant time. Items can also be added to the tail of the list
300 * and removed from the head in constant time, without further memory
301 * allocs or frees.
302 */
303
304static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size)
305{
306 size_t alloc_size, i;
307
308 /* Allocated size is a power of 2 to map sequence numbers
309 * (which may be up to 14 bits) in to a smaller array that is
310 * sized for the negotiated ERTM transmit windows.
311 */
312 alloc_size = roundup_pow_of_two(size);
313
314 seq_list->list = kmalloc(sizeof(u16) * alloc_size, GFP_KERNEL);
315 if (!seq_list->list)
316 return -ENOMEM;
317
318 seq_list->mask = alloc_size - 1;
319 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
320 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
321 for (i = 0; i < alloc_size; i++)
322 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
323
324 return 0;
325}
326
327static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list)
328{
329 kfree(seq_list->list);
330}
331
332static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list,
333 u16 seq)
334{
335 /* Constant-time check for list membership */
336 return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR;
337}
338
Mat Martineau3c588192012-04-11 10:48:42 -0700339static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list)
340{
Johan Hedberg03a0c5d2014-01-18 21:32:59 +0200341 u16 seq = seq_list->head;
342 u16 mask = seq_list->mask;
343
344 seq_list->head = seq_list->list[seq & mask];
345 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR;
346
347 if (seq_list->head == L2CAP_SEQ_LIST_TAIL) {
348 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
349 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
350 }
351
352 return seq;
Mat Martineau3c588192012-04-11 10:48:42 -0700353}
354
355static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list)
356{
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300357 u16 i;
Mat Martineau3c588192012-04-11 10:48:42 -0700358
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300359 if (seq_list->head == L2CAP_SEQ_LIST_CLEAR)
360 return;
361
362 for (i = 0; i <= seq_list->mask; i++)
363 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
364
365 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
366 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
Mat Martineau3c588192012-04-11 10:48:42 -0700367}
368
369static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq)
370{
371 u16 mask = seq_list->mask;
372
373 /* All appends happen in constant time */
374
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300375 if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR)
376 return;
Mat Martineau3c588192012-04-11 10:48:42 -0700377
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300378 if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR)
379 seq_list->head = seq;
380 else
381 seq_list->list[seq_list->tail & mask] = seq;
382
383 seq_list->tail = seq;
384 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL;
Mat Martineau3c588192012-04-11 10:48:42 -0700385}
386
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300387static void l2cap_chan_timeout(struct work_struct *work)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300388{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300389 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100390 chan_timer.work);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200391 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300392 int reason;
393
Andrei Emeltchenkoe05dcc32012-02-17 11:40:56 +0200394 BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300395
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200396 mutex_lock(&conn->chan_lock);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200397 l2cap_chan_lock(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300398
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300399 if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300400 reason = ECONNREFUSED;
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300401 else if (chan->state == BT_CONNECT &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100402 chan->sec_level != BT_SECURITY_SDP)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300403 reason = ECONNREFUSED;
404 else
405 reason = ETIMEDOUT;
406
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300407 l2cap_chan_close(chan, reason);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300408
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200409 l2cap_chan_unlock(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300410
Gustavo Padovan80b98022012-05-27 22:27:51 -0300411 chan->ops->close(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200412 mutex_unlock(&conn->chan_lock);
413
Ulisses Furquim371fd832011-12-21 20:02:36 -0200414 l2cap_chan_put(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300415}
416
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -0300417struct l2cap_chan *l2cap_chan_create(void)
Marcel Holtmann01394182006-07-03 10:02:46 +0200418{
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300419 struct l2cap_chan *chan;
Marcel Holtmann01394182006-07-03 10:02:46 +0200420
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300421 chan = kzalloc(sizeof(*chan), GFP_ATOMIC);
422 if (!chan)
423 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200424
Andrei Emeltchenkoc03b3552012-02-21 12:54:56 +0200425 mutex_init(&chan->lock);
426
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200427 write_lock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300428 list_add(&chan->global_l, &chan_list);
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200429 write_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300430
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300431 INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300432
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300433 chan->state = BT_OPEN;
434
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530435 kref_init(&chan->kref);
Gustavo F. Padovan71ba0e52011-05-17 14:34:52 -0300436
Mat Martineau28270112012-05-17 21:14:09 -0700437 /* This flag is cleared in l2cap_chan_ready() */
438 set_bit(CONF_NOT_COMPLETE, &chan->conf_state);
439
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -0300440 BT_DBG("chan %p", chan);
Szymon Jancabc545b2011-11-03 16:05:44 +0100441
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300442 return chan;
Marcel Holtmann01394182006-07-03 10:02:46 +0200443}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300444EXPORT_SYMBOL_GPL(l2cap_chan_create);
Marcel Holtmann01394182006-07-03 10:02:46 +0200445
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530446static void l2cap_chan_destroy(struct kref *kref)
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300447{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530448 struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref);
449
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530450 BT_DBG("chan %p", chan);
451
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200452 write_lock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300453 list_del(&chan->global_l);
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200454 write_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300455
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530456 kfree(chan);
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300457}
458
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530459void l2cap_chan_hold(struct l2cap_chan *c)
460{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530461 BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount));
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530462
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530463 kref_get(&c->kref);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530464}
465
466void l2cap_chan_put(struct l2cap_chan *c)
467{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530468 BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount));
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530469
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530470 kref_put(&c->kref, l2cap_chan_destroy);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530471}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300472EXPORT_SYMBOL_GPL(l2cap_chan_put);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530473
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300474void l2cap_chan_set_defaults(struct l2cap_chan *chan)
475{
476 chan->fcs = L2CAP_FCS_CRC16;
477 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
478 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
479 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
Jukka Rissanen6a5e8162014-05-28 14:43:04 +0300480 chan->remote_max_tx = chan->max_tx;
481 chan->remote_tx_win = chan->tx_win;
Mat Martineauc20f8e32012-07-10 05:47:07 -0700482 chan->ack_win = L2CAP_DEFAULT_TX_WINDOW;
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300483 chan->sec_level = BT_SECURITY_LOW;
Jukka Rissanen6a5e8162014-05-28 14:43:04 +0300484 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
485 chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
486 chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
487 chan->conf_state = 0;
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300488
489 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
490}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300491EXPORT_SYMBOL_GPL(l2cap_chan_set_defaults);
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300492
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200493static void l2cap_le_flowctl_init(struct l2cap_chan *chan)
Johan Hedberg38319712013-05-17 12:49:23 +0300494{
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200495 chan->sdu = NULL;
496 chan->sdu_last_frag = NULL;
497 chan->sdu_len = 0;
Johan Hedberg0cd75f72013-05-17 13:09:05 +0300498 chan->tx_credits = 0;
Johan Hedbergf15b8ec2013-12-03 15:08:25 +0200499 chan->rx_credits = le_max_credits;
Johan Hedbergd1d79412014-01-27 15:11:33 -0800500 chan->mps = min_t(u16, chan->imtu, le_default_mps);
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200501
502 skb_queue_head_init(&chan->tx_q);
Johan Hedberg38319712013-05-17 12:49:23 +0300503}
504
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +0300505void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
Marcel Holtmann01394182006-07-03 10:02:46 +0200506{
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -0300507 BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
Andrei Emeltchenko097db762012-03-09 14:16:17 +0200508 __le16_to_cpu(chan->psm), chan->dcid);
Marcel Holtmann01394182006-07-03 10:02:46 +0200509
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +0200510 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100511
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300512 chan->conn = conn;
Marcel Holtmann01394182006-07-03 10:02:46 +0200513
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200514 switch (chan->chan_type) {
515 case L2CAP_CHAN_CONN_ORIENTED:
Johan Hedberg21626e62014-01-24 10:35:41 +0200516 /* Alloc CID for connection-oriented socket */
517 chan->scid = l2cap_alloc_cid(conn);
518 if (conn->hcon->type == ACL_LINK)
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300519 chan->omtu = L2CAP_DEFAULT_MTU;
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200520 break;
521
522 case L2CAP_CHAN_CONN_LESS:
Marcel Holtmann01394182006-07-03 10:02:46 +0200523 /* Connectionless socket */
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300524 chan->scid = L2CAP_CID_CONN_LESS;
525 chan->dcid = L2CAP_CID_CONN_LESS;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300526 chan->omtu = L2CAP_DEFAULT_MTU;
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200527 break;
528
Johan Hedberg2338a7e2014-01-24 10:35:40 +0200529 case L2CAP_CHAN_FIXED:
530 /* Caller will set CID and CID specific MTU values */
Andrei Emeltchenko416fa752012-05-29 13:59:16 +0300531 break;
532
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200533 default:
Marcel Holtmann01394182006-07-03 10:02:46 +0200534 /* Raw socket can send/recv signalling messages only */
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300535 chan->scid = L2CAP_CID_SIGNALING;
536 chan->dcid = L2CAP_CID_SIGNALING;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300537 chan->omtu = L2CAP_DEFAULT_MTU;
Marcel Holtmann01394182006-07-03 10:02:46 +0200538 }
539
Andrei Emeltchenko8f7975b2011-10-13 16:18:54 +0300540 chan->local_id = L2CAP_BESTEFFORT_ID;
541 chan->local_stype = L2CAP_SERV_BESTEFFORT;
542 chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE;
543 chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME;
544 chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT;
Andrei Emeltchenko8936fa62012-10-08 11:14:41 +0300545 chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO;
Andrei Emeltchenko8f7975b2011-10-13 16:18:54 +0300546
Ulisses Furquim371fd832011-12-21 20:02:36 -0200547 l2cap_chan_hold(chan);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300548
Johan Hedberg5ee98912013-04-29 19:35:43 +0300549 hci_conn_hold(conn->hcon);
550
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200551 list_add(&chan->list, &conn->chan_l);
Andrei Emeltchenko643162a2012-02-22 17:11:55 +0200552}
553
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300554void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
Andrei Emeltchenko643162a2012-02-22 17:11:55 +0200555{
556 mutex_lock(&conn->chan_lock);
557 __l2cap_chan_add(conn, chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200558 mutex_unlock(&conn->chan_lock);
Marcel Holtmann01394182006-07-03 10:02:46 +0200559}
560
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300561void l2cap_chan_del(struct l2cap_chan *chan, int err)
Marcel Holtmann01394182006-07-03 10:02:46 +0200562{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300563 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann01394182006-07-03 10:02:46 +0200564
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -0300565 __clear_chan_timer(chan);
Marcel Holtmann01394182006-07-03 10:02:46 +0200566
Gustavo F. Padovan49208c92011-04-04 15:59:54 -0300567 BT_DBG("chan %p, conn %p, err %d", chan, conn, err);
Marcel Holtmann01394182006-07-03 10:02:46 +0200568
Johan Hedberg72847ce2014-08-08 09:28:03 +0300569 chan->ops->teardown(chan, err);
570
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900571 if (conn) {
Andrei Emeltchenko56f60982012-10-15 11:58:44 +0300572 struct amp_mgr *mgr = conn->hcon->amp_mgr;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300573 /* Delete from channel list */
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200574 list_del(&chan->list);
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -0200575
Ulisses Furquim371fd832011-12-21 20:02:36 -0200576 l2cap_chan_put(chan);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300577
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300578 chan->conn = NULL;
Andrei Emeltchenko3cabbfd2012-05-31 11:01:37 +0300579
Johan Hedberg2338a7e2014-01-24 10:35:40 +0200580 if (chan->scid != L2CAP_CID_A2MP)
David Herrmann76a68ba2013-04-06 20:28:37 +0200581 hci_conn_drop(conn->hcon);
Andrei Emeltchenko56f60982012-10-15 11:58:44 +0300582
583 if (mgr && mgr->bredr_chan == chan)
584 mgr->bredr_chan = NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200585 }
586
Andrei Emeltchenko419e08c2012-10-31 15:46:34 +0200587 if (chan->hs_hchan) {
588 struct hci_chan *hs_hchan = chan->hs_hchan;
589
590 BT_DBG("chan %p disconnect hs_hchan %p", chan, hs_hchan);
591 amp_disconnect_logical_link(hs_hchan);
592 }
593
Mat Martineau28270112012-05-17 21:14:09 -0700594 if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state))
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300595 return;
Gustavo F. Padovan2ead70b2011-04-01 15:13:36 -0300596
Gustavo Padovanee556f62012-05-18 20:22:38 -0300597 switch(chan->mode) {
598 case L2CAP_MODE_BASIC:
599 break;
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300600
Johan Hedberg38319712013-05-17 12:49:23 +0300601 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedberg177f8f22013-05-31 17:54:51 +0300602 skb_queue_purge(&chan->tx_q);
Johan Hedberg38319712013-05-17 12:49:23 +0300603 break;
604
Gustavo Padovanee556f62012-05-18 20:22:38 -0300605 case L2CAP_MODE_ERTM:
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -0300606 __clear_retrans_timer(chan);
607 __clear_monitor_timer(chan);
608 __clear_ack_timer(chan);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300609
Gustavo F. Padovanf1c67752011-03-25 20:36:10 -0300610 skb_queue_purge(&chan->srej_q);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300611
Mat Martineau3c588192012-04-11 10:48:42 -0700612 l2cap_seq_list_free(&chan->srej_list);
613 l2cap_seq_list_free(&chan->retrans_list);
Gustavo Padovanee556f62012-05-18 20:22:38 -0300614
615 /* fall through */
616
617 case L2CAP_MODE_STREAMING:
618 skb_queue_purge(&chan->tx_q);
619 break;
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300620 }
Gustavo Padovanee556f62012-05-18 20:22:38 -0300621
622 return;
Marcel Holtmann01394182006-07-03 10:02:46 +0200623}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300624EXPORT_SYMBOL_GPL(l2cap_chan_del);
Marcel Holtmann01394182006-07-03 10:02:46 +0200625
Johan Hedberg387a33e2014-02-18 21:41:33 +0200626void l2cap_conn_update_id_addr(struct hci_conn *hcon)
627{
628 struct l2cap_conn *conn = hcon->l2cap_data;
629 struct l2cap_chan *chan;
630
631 mutex_lock(&conn->chan_lock);
632
633 list_for_each_entry(chan, &conn->chan_l, list) {
634 l2cap_chan_lock(chan);
635 bacpy(&chan->dst, &hcon->dst);
636 chan->dst_type = bdaddr_type(hcon, hcon->dst_type);
637 l2cap_chan_unlock(chan);
638 }
639
640 mutex_unlock(&conn->chan_lock);
641}
642
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300643static void l2cap_chan_le_connect_reject(struct l2cap_chan *chan)
644{
645 struct l2cap_conn *conn = chan->conn;
646 struct l2cap_le_conn_rsp rsp;
647 u16 result;
648
649 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
650 result = L2CAP_CR_AUTHORIZATION;
651 else
652 result = L2CAP_CR_BAD_PSM;
653
654 l2cap_state_change(chan, BT_DISCONN);
655
656 rsp.dcid = cpu_to_le16(chan->scid);
657 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +0200658 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +0300659 rsp.credits = cpu_to_le16(chan->rx_credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300660 rsp.result = cpu_to_le16(result);
661
662 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
663 &rsp);
664}
665
Johan Hedberg791d60f2013-05-14 22:24:44 +0300666static void l2cap_chan_connect_reject(struct l2cap_chan *chan)
667{
668 struct l2cap_conn *conn = chan->conn;
669 struct l2cap_conn_rsp rsp;
670 u16 result;
671
672 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
673 result = L2CAP_CR_SEC_BLOCK;
674 else
675 result = L2CAP_CR_BAD_PSM;
676
677 l2cap_state_change(chan, BT_DISCONN);
678
679 rsp.scid = cpu_to_le16(chan->dcid);
680 rsp.dcid = cpu_to_le16(chan->scid);
681 rsp.result = cpu_to_le16(result);
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700682 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Johan Hedberg791d60f2013-05-14 22:24:44 +0300683
684 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
685}
686
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300687void l2cap_chan_close(struct l2cap_chan *chan, int reason)
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300688{
689 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300690
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700691 BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300692
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -0300693 switch (chan->state) {
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300694 case BT_LISTEN:
Gustavo Padovanb699ec02012-10-06 11:51:54 +0100695 chan->ops->teardown(chan, 0);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300696 break;
697
698 case BT_CONNECTED:
699 case BT_CONFIG:
Johan Hedberg7b25c9b2014-01-28 15:28:04 -0800700 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
Gustavo Padovan8d836d72013-10-15 19:24:47 -0300701 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +0200702 l2cap_send_disconn_req(chan, reason);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300703 } else
704 l2cap_chan_del(chan, reason);
705 break;
706
707 case BT_CONNECT2:
Johan Hedberg791d60f2013-05-14 22:24:44 +0300708 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
709 if (conn->hcon->type == ACL_LINK)
710 l2cap_chan_connect_reject(chan);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300711 else if (conn->hcon->type == LE_LINK)
712 l2cap_chan_le_connect_reject(chan);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300713 }
714
715 l2cap_chan_del(chan, reason);
716 break;
717
718 case BT_CONNECT:
719 case BT_DISCONN:
720 l2cap_chan_del(chan, reason);
721 break;
722
723 default:
Gustavo Padovanb699ec02012-10-06 11:51:54 +0100724 chan->ops->teardown(chan, 0);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300725 break;
726 }
727}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300728EXPORT_SYMBOL(l2cap_chan_close);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300729
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300730static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
Johan Hedberg8556edd32011-01-19 12:06:50 +0530731{
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700732 switch (chan->chan_type) {
733 case L2CAP_CHAN_RAW:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300734 switch (chan->sec_level) {
Johan Hedberg8556edd32011-01-19 12:06:50 +0530735 case BT_SECURITY_HIGH:
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800736 case BT_SECURITY_FIPS:
Johan Hedberg8556edd32011-01-19 12:06:50 +0530737 return HCI_AT_DEDICATED_BONDING_MITM;
738 case BT_SECURITY_MEDIUM:
739 return HCI_AT_DEDICATED_BONDING;
740 default:
741 return HCI_AT_NO_BONDING;
742 }
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700743 break;
Marcel Holtmann3124b842013-10-12 07:19:32 -0700744 case L2CAP_CHAN_CONN_LESS:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700745 if (chan->psm == cpu_to_le16(L2CAP_PSM_3DSP)) {
Marcel Holtmann3124b842013-10-12 07:19:32 -0700746 if (chan->sec_level == BT_SECURITY_LOW)
747 chan->sec_level = BT_SECURITY_SDP;
748 }
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800749 if (chan->sec_level == BT_SECURITY_HIGH ||
750 chan->sec_level == BT_SECURITY_FIPS)
Marcel Holtmann3124b842013-10-12 07:19:32 -0700751 return HCI_AT_NO_BONDING_MITM;
752 else
753 return HCI_AT_NO_BONDING;
754 break;
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700755 case L2CAP_CHAN_CONN_ORIENTED:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700756 if (chan->psm == cpu_to_le16(L2CAP_PSM_SDP)) {
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700757 if (chan->sec_level == BT_SECURITY_LOW)
758 chan->sec_level = BT_SECURITY_SDP;
Johan Hedberg8556edd32011-01-19 12:06:50 +0530759
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800760 if (chan->sec_level == BT_SECURITY_HIGH ||
761 chan->sec_level == BT_SECURITY_FIPS)
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700762 return HCI_AT_NO_BONDING_MITM;
763 else
764 return HCI_AT_NO_BONDING;
765 }
766 /* fall through */
767 default:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300768 switch (chan->sec_level) {
Johan Hedberg8556edd32011-01-19 12:06:50 +0530769 case BT_SECURITY_HIGH:
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800770 case BT_SECURITY_FIPS:
Johan Hedberg8556edd32011-01-19 12:06:50 +0530771 return HCI_AT_GENERAL_BONDING_MITM;
772 case BT_SECURITY_MEDIUM:
773 return HCI_AT_GENERAL_BONDING;
774 default:
775 return HCI_AT_NO_BONDING;
776 }
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700777 break;
Johan Hedberg8556edd32011-01-19 12:06:50 +0530778 }
779}
780
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200781/* Service level security */
Johan Hedberge7cafc42014-07-17 15:35:38 +0300782int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator)
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200783{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300784 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100785 __u8 auth_type;
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200786
Johan Hedberga17de2f2013-05-14 13:25:37 +0300787 if (conn->hcon->type == LE_LINK)
788 return smp_conn_security(conn->hcon, chan->sec_level);
789
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300790 auth_type = l2cap_get_auth_type(chan);
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100791
Johan Hedberge7cafc42014-07-17 15:35:38 +0300792 return hci_conn_security(conn->hcon, chan->sec_level, auth_type,
793 initiator);
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200794}
795
Johannes Bergb5ad8b72011-06-01 08:54:45 +0200796static u8 l2cap_get_ident(struct l2cap_conn *conn)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200797{
798 u8 id;
799
800 /* Get next available identificator.
801 * 1 - 128 are used by kernel.
802 * 129 - 199 are reserved.
803 * 200 - 254 are used by utilities like l2ping, etc.
804 */
805
Marcel Holtmann5a54e7c2014-07-13 20:50:15 +0200806 mutex_lock(&conn->ident_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200807
808 if (++conn->tx_ident > 128)
809 conn->tx_ident = 1;
810
811 id = conn->tx_ident;
812
Marcel Holtmann5a54e7c2014-07-13 20:50:15 +0200813 mutex_unlock(&conn->ident_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200814
815 return id;
816}
817
Gustavo Padovan2d792812012-10-06 10:07:01 +0100818static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
819 void *data)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200820{
821 struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +0200822 u8 flags;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200823
824 BT_DBG("code 0x%2.2x", code);
825
826 if (!skb)
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -0300827 return;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200828
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +0200829 if (lmp_no_flush_capable(conn->hcon->hdev))
830 flags = ACL_START_NO_FLUSH;
831 else
832 flags = ACL_START;
833
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700834 bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +0200835 skb->priority = HCI_PRIO_MAX;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700836
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200837 hci_send_acl(conn->hchan, skb, flags);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200838}
839
Mat Martineau02b0fbb2012-10-23 15:24:10 -0700840static bool __chan_is_moving(struct l2cap_chan *chan)
841{
842 return chan->move_state != L2CAP_MOVE_STABLE &&
843 chan->move_state != L2CAP_MOVE_WAIT_PREPARE;
844}
845
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200846static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
847{
848 struct hci_conn *hcon = chan->conn->hcon;
849 u16 flags;
850
851 BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100852 skb->priority);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200853
Mat Martineaud5f8a752012-10-23 15:24:18 -0700854 if (chan->hs_hcon && !__chan_is_moving(chan)) {
855 if (chan->hs_hchan)
856 hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE);
857 else
858 kfree_skb(skb);
859
860 return;
861 }
862
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200863 if (!test_bit(FLAG_FLUSHABLE, &chan->flags) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100864 lmp_no_flush_capable(hcon->hdev))
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200865 flags = ACL_START_NO_FLUSH;
866 else
867 flags = ACL_START;
868
869 bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
870 hci_send_acl(chan->conn->hchan, skb, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700873static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control)
874{
875 control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT;
876 control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT;
877
878 if (enh & L2CAP_CTRL_FRAME_TYPE) {
879 /* S-Frame */
880 control->sframe = 1;
881 control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT;
882 control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT;
883
884 control->sar = 0;
885 control->txseq = 0;
886 } else {
887 /* I-Frame */
888 control->sframe = 0;
889 control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT;
890 control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT;
891
892 control->poll = 0;
893 control->super = 0;
894 }
895}
896
897static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control)
898{
899 control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT;
900 control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT;
901
902 if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) {
903 /* S-Frame */
904 control->sframe = 1;
905 control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT;
906 control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT;
907
908 control->sar = 0;
909 control->txseq = 0;
910 } else {
911 /* I-Frame */
912 control->sframe = 0;
913 control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT;
914 control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT;
915
916 control->poll = 0;
917 control->super = 0;
918 }
919}
920
921static inline void __unpack_control(struct l2cap_chan *chan,
922 struct sk_buff *skb)
923{
924 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
925 __unpack_extended_control(get_unaligned_le32(skb->data),
926 &bt_cb(skb)->control);
Mat Martineaucec8ab6e2012-05-17 20:53:36 -0700927 skb_pull(skb, L2CAP_EXT_CTRL_SIZE);
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700928 } else {
929 __unpack_enhanced_control(get_unaligned_le16(skb->data),
930 &bt_cb(skb)->control);
Mat Martineaucec8ab6e2012-05-17 20:53:36 -0700931 skb_pull(skb, L2CAP_ENH_CTRL_SIZE);
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700932 }
933}
934
935static u32 __pack_extended_control(struct l2cap_ctrl *control)
936{
937 u32 packed;
938
939 packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT;
940 packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT;
941
942 if (control->sframe) {
943 packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT;
944 packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT;
945 packed |= L2CAP_EXT_CTRL_FRAME_TYPE;
946 } else {
947 packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT;
948 packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT;
949 }
950
951 return packed;
952}
953
954static u16 __pack_enhanced_control(struct l2cap_ctrl *control)
955{
956 u16 packed;
957
958 packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT;
959 packed |= control->final << L2CAP_CTRL_FINAL_SHIFT;
960
961 if (control->sframe) {
962 packed |= control->poll << L2CAP_CTRL_POLL_SHIFT;
963 packed |= control->super << L2CAP_CTRL_SUPER_SHIFT;
964 packed |= L2CAP_CTRL_FRAME_TYPE;
965 } else {
966 packed |= control->sar << L2CAP_CTRL_SAR_SHIFT;
967 packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT;
968 }
969
970 return packed;
971}
972
973static inline void __pack_control(struct l2cap_chan *chan,
974 struct l2cap_ctrl *control,
975 struct sk_buff *skb)
976{
977 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
978 put_unaligned_le32(__pack_extended_control(control),
979 skb->data + L2CAP_HDR_SIZE);
980 } else {
981 put_unaligned_le16(__pack_enhanced_control(control),
982 skb->data + L2CAP_HDR_SIZE);
983 }
984}
985
Gustavo Padovanba7aa642012-05-29 13:29:16 -0300986static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan)
987{
988 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
989 return L2CAP_EXT_HDR_SIZE;
990 else
991 return L2CAP_ENH_HDR_SIZE;
992}
993
Mat Martineaua67d7f62012-05-17 20:53:35 -0700994static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan,
995 u32 control)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300996{
997 struct sk_buff *skb;
998 struct l2cap_hdr *lh;
Gustavo Padovanba7aa642012-05-29 13:29:16 -0300999 int hlen = __ertm_hdr_size(chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001000
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001001 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03001002 hlen += L2CAP_FCS_SIZE;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001003
Mat Martineaua67d7f62012-05-17 20:53:35 -07001004 skb = bt_skb_alloc(hlen, GFP_KERNEL);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001005
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001006 if (!skb)
Mat Martineaua67d7f62012-05-17 20:53:35 -07001007 return ERR_PTR(-ENOMEM);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001008
1009 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001010 lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001011 lh->cid = cpu_to_le16(chan->dcid);
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03001012
Mat Martineaua67d7f62012-05-17 20:53:35 -07001013 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1014 put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
1015 else
1016 put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001017
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001018 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineaua67d7f62012-05-17 20:53:35 -07001019 u16 fcs = crc16(0, (u8 *)skb->data, skb->len);
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03001020 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001021 }
1022
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001023 skb->priority = HCI_PRIO_MAX;
Mat Martineaua67d7f62012-05-17 20:53:35 -07001024 return skb;
1025}
1026
1027static void l2cap_send_sframe(struct l2cap_chan *chan,
1028 struct l2cap_ctrl *control)
1029{
1030 struct sk_buff *skb;
1031 u32 control_field;
1032
1033 BT_DBG("chan %p, control %p", chan, control);
1034
1035 if (!control->sframe)
1036 return;
1037
Mat Martineaub99e13a2012-10-23 15:24:19 -07001038 if (__chan_is_moving(chan))
1039 return;
1040
Mat Martineaua67d7f62012-05-17 20:53:35 -07001041 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) &&
1042 !control->poll)
1043 control->final = 1;
1044
1045 if (control->super == L2CAP_SUPER_RR)
1046 clear_bit(CONN_RNR_SENT, &chan->conn_state);
1047 else if (control->super == L2CAP_SUPER_RNR)
1048 set_bit(CONN_RNR_SENT, &chan->conn_state);
1049
1050 if (control->super != L2CAP_SUPER_SREJ) {
1051 chan->last_acked_seq = control->reqseq;
1052 __clear_ack_timer(chan);
1053 }
1054
1055 BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq,
1056 control->final, control->poll, control->super);
1057
1058 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1059 control_field = __pack_extended_control(control);
1060 else
1061 control_field = __pack_enhanced_control(control);
1062
1063 skb = l2cap_create_sframe_pdu(chan, control_field);
1064 if (!IS_ERR(skb))
1065 l2cap_do_send(chan, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001066}
1067
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001068static void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, bool poll)
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001069{
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001070 struct l2cap_ctrl control;
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001071
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001072 BT_DBG("chan %p, poll %d", chan, poll);
1073
1074 memset(&control, 0, sizeof(control));
1075 control.sframe = 1;
1076 control.poll = poll;
1077
1078 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
1079 control.super = L2CAP_SUPER_RNR;
1080 else
1081 control.super = L2CAP_SUPER_RR;
1082
1083 control.reqseq = chan->buffer_seq;
1084 l2cap_send_sframe(chan, &control);
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001085}
1086
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001087static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
Andrei Emeltchenkoe501d052010-07-08 12:14:41 +03001088{
Johan Hedberg5ff6f342014-08-07 22:56:43 +03001089 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
1090 return true;
1091
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001092 return !test_bit(CONF_CONNECT_PEND, &chan->conf_state);
Andrei Emeltchenkoe501d052010-07-08 12:14:41 +03001093}
1094
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001095static bool __amp_capable(struct l2cap_chan *chan)
1096{
1097 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann1df7b172013-10-05 11:47:49 -07001098 struct hci_dev *hdev;
1099 bool amp_available = false;
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001100
Marcel Holtmann1df7b172013-10-05 11:47:49 -07001101 if (!conn->hs_enabled)
1102 return false;
1103
1104 if (!(conn->fixed_chan_mask & L2CAP_FC_A2MP))
1105 return false;
1106
1107 read_lock(&hci_dev_list_lock);
1108 list_for_each_entry(hdev, &hci_dev_list, list) {
1109 if (hdev->amp_type != AMP_TYPE_BREDR &&
1110 test_bit(HCI_UP, &hdev->flags)) {
1111 amp_available = true;
1112 break;
1113 }
1114 }
1115 read_unlock(&hci_dev_list_lock);
1116
1117 if (chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED)
1118 return amp_available;
Marcel Holtmann848566b2013-10-01 22:59:22 -07001119
1120 return false;
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001121}
1122
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +02001123static bool l2cap_check_efs(struct l2cap_chan *chan)
1124{
1125 /* Check EFS parameters */
1126 return true;
1127}
1128
Andrei Emeltchenko2766be42012-09-27 17:26:21 +03001129void l2cap_send_conn_req(struct l2cap_chan *chan)
Andrei Emeltchenko9b27f352012-02-24 16:00:00 +02001130{
1131 struct l2cap_conn *conn = chan->conn;
1132 struct l2cap_conn_req req;
1133
1134 req.scid = cpu_to_le16(chan->scid);
1135 req.psm = chan->psm;
1136
1137 chan->ident = l2cap_get_ident(conn);
1138
1139 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
1140
1141 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req);
1142}
1143
Mat Martineau8eb200b2012-10-23 15:24:17 -07001144static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id)
1145{
1146 struct l2cap_create_chan_req req;
1147 req.scid = cpu_to_le16(chan->scid);
1148 req.psm = chan->psm;
1149 req.amp_id = amp_id;
1150
1151 chan->ident = l2cap_get_ident(chan->conn);
1152
1153 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ,
1154 sizeof(req), &req);
1155}
1156
Mat Martineau02b0fbb2012-10-23 15:24:10 -07001157static void l2cap_move_setup(struct l2cap_chan *chan)
1158{
1159 struct sk_buff *skb;
1160
1161 BT_DBG("chan %p", chan);
1162
1163 if (chan->mode != L2CAP_MODE_ERTM)
1164 return;
1165
1166 __clear_retrans_timer(chan);
1167 __clear_monitor_timer(chan);
1168 __clear_ack_timer(chan);
1169
1170 chan->retry_count = 0;
1171 skb_queue_walk(&chan->tx_q, skb) {
1172 if (bt_cb(skb)->control.retries)
1173 bt_cb(skb)->control.retries = 1;
1174 else
1175 break;
1176 }
1177
1178 chan->expected_tx_seq = chan->buffer_seq;
1179
1180 clear_bit(CONN_REJ_ACT, &chan->conn_state);
1181 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
1182 l2cap_seq_list_clear(&chan->retrans_list);
1183 l2cap_seq_list_clear(&chan->srej_list);
1184 skb_queue_purge(&chan->srej_q);
1185
1186 chan->tx_state = L2CAP_TX_STATE_XMIT;
1187 chan->rx_state = L2CAP_RX_STATE_MOVE;
1188
1189 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
1190}
1191
Mat Martineau5f3847a2012-10-23 15:24:12 -07001192static void l2cap_move_done(struct l2cap_chan *chan)
1193{
1194 u8 move_role = chan->move_role;
1195 BT_DBG("chan %p", chan);
1196
1197 chan->move_state = L2CAP_MOVE_STABLE;
1198 chan->move_role = L2CAP_MOVE_ROLE_NONE;
1199
1200 if (chan->mode != L2CAP_MODE_ERTM)
1201 return;
1202
1203 switch (move_role) {
1204 case L2CAP_MOVE_ROLE_INITIATOR:
1205 l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL);
1206 chan->rx_state = L2CAP_RX_STATE_WAIT_F;
1207 break;
1208 case L2CAP_MOVE_ROLE_RESPONDER:
1209 chan->rx_state = L2CAP_RX_STATE_WAIT_P;
1210 break;
1211 }
1212}
1213
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001214static void l2cap_chan_ready(struct l2cap_chan *chan)
1215{
Mat Martineau28270112012-05-17 21:14:09 -07001216 /* This clears all conf flags, including CONF_NOT_COMPLETE */
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001217 chan->conf_state = 0;
1218 __clear_chan_timer(chan);
1219
Johan Hedberg0ce43ce2013-12-05 14:55:33 +02001220 if (chan->mode == L2CAP_MODE_LE_FLOWCTL && !chan->tx_credits)
1221 chan->ops->suspend(chan);
Johan Hedberg177f8f22013-05-31 17:54:51 +03001222
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001223 chan->state = BT_CONNECTED;
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001224
Andrei Emeltchenkofd83e2c2012-05-30 09:55:32 +03001225 chan->ops->ready(chan);
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001226}
1227
Johan Hedbergf1496de2013-05-13 14:15:56 +03001228static void l2cap_le_connect(struct l2cap_chan *chan)
1229{
1230 struct l2cap_conn *conn = chan->conn;
1231 struct l2cap_le_conn_req req;
1232
Johan Hedberg595177f2013-12-02 22:12:22 +02001233 if (test_and_set_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags))
1234 return;
1235
Johan Hedbergf1496de2013-05-13 14:15:56 +03001236 req.psm = chan->psm;
1237 req.scid = cpu_to_le16(chan->scid);
1238 req.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02001239 req.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03001240 req.credits = cpu_to_le16(chan->rx_credits);
Johan Hedbergf1496de2013-05-13 14:15:56 +03001241
1242 chan->ident = l2cap_get_ident(conn);
1243
1244 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_REQ,
1245 sizeof(req), &req);
1246}
1247
1248static void l2cap_le_start(struct l2cap_chan *chan)
1249{
1250 struct l2cap_conn *conn = chan->conn;
1251
1252 if (!smp_conn_security(conn->hcon, chan->sec_level))
1253 return;
1254
1255 if (!chan->psm) {
1256 l2cap_chan_ready(chan);
1257 return;
1258 }
1259
1260 if (chan->state == BT_CONNECT)
1261 l2cap_le_connect(chan);
1262}
1263
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001264static void l2cap_start_connection(struct l2cap_chan *chan)
1265{
1266 if (__amp_capable(chan)) {
1267 BT_DBG("chan %p AMP capable: discover AMPs", chan);
1268 a2mp_discover_amp(chan);
Johan Hedbergf1496de2013-05-13 14:15:56 +03001269 } else if (chan->conn->hcon->type == LE_LINK) {
1270 l2cap_le_start(chan);
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001271 } else {
1272 l2cap_send_conn_req(chan);
1273 }
1274}
1275
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001276static void l2cap_do_start(struct l2cap_chan *chan)
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001277{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001278 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001279
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001280 if (conn->hcon->type == LE_LINK) {
Johan Hedberg96ac34f2013-05-13 11:15:07 +03001281 l2cap_le_start(chan);
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001282 return;
1283 }
1284
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001285 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
Marcel Holtmann984947d2009-02-06 23:35:19 +01001286 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
1287 return;
1288
Johan Hedberge7cafc42014-07-17 15:35:38 +03001289 if (l2cap_chan_check_security(chan, true) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01001290 __l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001291 l2cap_start_connection(chan);
1292 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001293 } else {
1294 struct l2cap_info_req req;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001295 req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001296
1297 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
1298 conn->info_ident = l2cap_get_ident(conn);
1299
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08001300 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001301
Gustavo Padovan2d792812012-10-06 10:07:01 +01001302 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
1303 sizeof(req), &req);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001304 }
1305}
1306
Gustavo F. Padovancf6c2c02010-06-07 20:54:45 -03001307static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
1308{
1309 u32 local_feat_mask = l2cap_feat_mask;
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03001310 if (!disable_ertm)
Gustavo F. Padovancf6c2c02010-06-07 20:54:45 -03001311 local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
1312
1313 switch (mode) {
1314 case L2CAP_MODE_ERTM:
1315 return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
1316 case L2CAP_MODE_STREAMING:
1317 return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
1318 default:
1319 return 0x00;
1320 }
1321}
1322
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001323static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err)
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001324{
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001325 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001326 struct l2cap_disconn_req req;
1327
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001328 if (!conn)
1329 return;
1330
Andrei Emeltchenkoaad3d0e2012-09-06 15:05:42 +03001331 if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) {
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -03001332 __clear_retrans_timer(chan);
1333 __clear_monitor_timer(chan);
1334 __clear_ack_timer(chan);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001335 }
1336
Johan Hedberg2338a7e2014-01-24 10:35:40 +02001337 if (chan->scid == L2CAP_CID_A2MP) {
Gustavo Padovand1177732012-10-06 11:47:38 +01001338 l2cap_state_change(chan, BT_DISCONN);
Andrei Emeltchenko416fa752012-05-29 13:59:16 +03001339 return;
1340 }
1341
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001342 req.dcid = cpu_to_le16(chan->dcid);
1343 req.scid = cpu_to_le16(chan->scid);
Gustavo Padovan2d792812012-10-06 10:07:01 +01001344 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ,
1345 sizeof(req), &req);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001346
Gustavo Padovanf8e73012013-10-15 19:24:46 -03001347 l2cap_state_change_and_error(chan, BT_DISCONN, err);
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001348}
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350/* ---- L2CAP connections ---- */
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001351static void l2cap_conn_start(struct l2cap_conn *conn)
1352{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001353 struct l2cap_chan *chan, *tmp;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001354
1355 BT_DBG("conn %p", conn);
1356
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001357 mutex_lock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001358
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001359 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001360 l2cap_chan_lock(chan);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001361
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001362 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001363 l2cap_chan_unlock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001364 continue;
1365 }
1366
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001367 if (chan->state == BT_CONNECT) {
Johan Hedberge7cafc42014-07-17 15:35:38 +03001368 if (!l2cap_chan_check_security(chan, true) ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01001369 !__l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001370 l2cap_chan_unlock(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001371 continue;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02001372 }
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001373
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001374 if (!l2cap_mode_supported(chan->mode, conn->feat_mask)
Gustavo Padovan2d792812012-10-06 10:07:01 +01001375 && test_bit(CONF_STATE2_DEVICE,
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001376 &chan->conf_state)) {
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001377 l2cap_chan_close(chan, ECONNRESET);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001378 l2cap_chan_unlock(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001379 continue;
1380 }
1381
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001382 l2cap_start_connection(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001383
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001384 } else if (chan->state == BT_CONNECT2) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001385 struct l2cap_conn_rsp rsp;
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001386 char buf[128];
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001387 rsp.scid = cpu_to_le16(chan->dcid);
1388 rsp.dcid = cpu_to_le16(chan->scid);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001389
Johan Hedberge7cafc42014-07-17 15:35:38 +03001390 if (l2cap_chan_check_security(chan, false)) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07001391 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001392 rsp.result = cpu_to_le16(L2CAP_CR_PEND);
1393 rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08001394 chan->ops->defer(chan);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001395
1396 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02001397 l2cap_state_change(chan, BT_CONFIG);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001398 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
1399 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001400 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001401 } else {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001402 rsp.result = cpu_to_le16(L2CAP_CR_PEND);
1403 rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001404 }
1405
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001406 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001407 sizeof(rsp), &rsp);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001408
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001409 if (test_bit(CONF_REQ_SENT, &chan->conf_state) ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01001410 rsp.result != L2CAP_CR_SUCCESS) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001411 l2cap_chan_unlock(chan);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001412 continue;
1413 }
1414
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001415 set_bit(CONF_REQ_SENT, &chan->conf_state);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001416 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001417 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03001418 chan->num_conf_req++;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001419 }
1420
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001421 l2cap_chan_unlock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001422 }
1423
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001424 mutex_unlock(&conn->chan_lock);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001425}
1426
Ville Tervob62f3282011-02-10 22:38:50 -03001427static void l2cap_le_conn_ready(struct l2cap_conn *conn)
1428{
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07001429 struct hci_conn *hcon = conn->hcon;
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001430 struct hci_dev *hdev = hcon->hdev;
Ville Tervob62f3282011-02-10 22:38:50 -03001431
Johan Hedberge760ec12014-08-07 22:56:47 +03001432 BT_DBG("%s conn %p", hdev->name, conn);
Ville Tervob62f3282011-02-10 22:38:50 -03001433
Johan Hedberge760ec12014-08-07 22:56:47 +03001434 /* For outgoing pairing which doesn't necessarily have an
1435 * associated socket (e.g. mgmt_pair_device).
1436 */
1437 if (hcon->out)
1438 smp_conn_security(hcon, hcon->pending_sec_level);
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07001439
Marcel Holtmann80afeb62014-06-23 12:18:51 +02001440 /* For LE slave connections, make sure the connection interval
1441 * is in the range of the minium and maximum interval that has
1442 * been configured for this connection. If not, then trigger
1443 * the connection update procedure.
1444 */
Johan Hedberg40bef302014-07-16 11:42:27 +03001445 if (hcon->role == HCI_ROLE_SLAVE &&
Marcel Holtmann80afeb62014-06-23 12:18:51 +02001446 (hcon->le_conn_interval < hcon->le_conn_min_interval ||
1447 hcon->le_conn_interval > hcon->le_conn_max_interval)) {
1448 struct l2cap_conn_param_update_req req;
1449
1450 req.min = cpu_to_le16(hcon->le_conn_min_interval);
1451 req.max = cpu_to_le16(hcon->le_conn_max_interval);
1452 req.latency = cpu_to_le16(hcon->le_conn_latency);
1453 req.to_multiplier = cpu_to_le16(hcon->le_supv_timeout);
1454
1455 l2cap_send_cmd(conn, l2cap_get_ident(conn),
1456 L2CAP_CONN_PARAM_UPDATE_REQ, sizeof(req), &req);
1457 }
Ville Tervob62f3282011-02-10 22:38:50 -03001458}
1459
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001460static void l2cap_conn_ready(struct l2cap_conn *conn)
1461{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001462 struct l2cap_chan *chan;
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001463 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001464
1465 BT_DBG("conn %p", conn);
1466
Johan Hedberge760ec12014-08-07 22:56:47 +03001467 mutex_lock(&conn->chan_lock);
1468
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001469 list_for_each_entry(chan, &conn->chan_l, list) {
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03001470
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001471 l2cap_chan_lock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001472
Johan Hedberg2338a7e2014-01-24 10:35:40 +02001473 if (chan->scid == L2CAP_CID_A2MP) {
Andrei Emeltchenko416fa752012-05-29 13:59:16 +03001474 l2cap_chan_unlock(chan);
1475 continue;
1476 }
1477
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001478 if (hcon->type == LE_LINK) {
Johan Hedbergf1496de2013-05-13 14:15:56 +03001479 l2cap_le_start(chan);
Vinicius Costa Gomes63128452011-06-17 22:46:26 -03001480 } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
Gustavo Padovan74e75742013-10-15 19:24:51 -03001481 l2cap_chan_ready(chan);
Anderson Brigliab501d6a2011-06-07 18:46:31 -03001482
Gustavo Padovan1c244f72012-12-07 03:29:10 -02001483 } else if (chan->state == BT_CONNECT) {
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001484 l2cap_do_start(chan);
Gustavo Padovan1c244f72012-12-07 03:29:10 -02001485 }
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001486
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001487 l2cap_chan_unlock(chan);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001488 }
1489
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001490 mutex_unlock(&conn->chan_lock);
Johan Hedberg61a939c2014-01-17 20:45:11 +02001491
Johan Hedberg79a05722014-08-08 09:28:04 +03001492 if (hcon->type == LE_LINK)
1493 l2cap_le_conn_ready(conn);
1494
Johan Hedberg61a939c2014-01-17 20:45:11 +02001495 queue_work(hcon->hdev->workqueue, &conn->pending_rx_work);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001496}
1497
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001498/* Notify sockets that we cannot guaranty reliability anymore */
1499static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
1500{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001501 struct l2cap_chan *chan;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001502
1503 BT_DBG("conn %p", conn);
1504
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001505 mutex_lock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001506
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001507 list_for_each_entry(chan, &conn->chan_l, list) {
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +03001508 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
Gustavo Padovan1d8b1fd2012-10-06 11:34:52 +01001509 l2cap_chan_set_err(chan, err);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001510 }
1511
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001512 mutex_unlock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001513}
1514
Gustavo F. Padovanf878fca2011-12-15 01:16:14 -02001515static void l2cap_info_timeout(struct work_struct *work)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001516{
Gustavo F. Padovanf878fca2011-12-15 01:16:14 -02001517 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001518 info_timer.work);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001519
Marcel Holtmann984947d2009-02-06 23:35:19 +01001520 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01001521 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01001522
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001523 l2cap_conn_start(conn);
1524}
1525
David Herrmann2c8e1412013-04-06 20:28:45 +02001526/*
1527 * l2cap_user
1528 * External modules can register l2cap_user objects on l2cap_conn. The ->probe
1529 * callback is called during registration. The ->remove callback is called
1530 * during unregistration.
1531 * An l2cap_user object can either be explicitly unregistered or when the
1532 * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon,
1533 * l2cap->hchan, .. are valid as long as the remove callback hasn't been called.
1534 * External modules must own a reference to the l2cap_conn object if they intend
1535 * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at
1536 * any time if they don't.
1537 */
1538
1539int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user)
1540{
1541 struct hci_dev *hdev = conn->hcon->hdev;
1542 int ret;
1543
1544 /* We need to check whether l2cap_conn is registered. If it is not, we
1545 * must not register the l2cap_user. l2cap_conn_del() is unregisters
1546 * l2cap_conn objects, but doesn't provide its own locking. Instead, it
1547 * relies on the parent hci_conn object to be locked. This itself relies
1548 * on the hci_dev object to be locked. So we must lock the hci device
1549 * here, too. */
1550
1551 hci_dev_lock(hdev);
1552
1553 if (user->list.next || user->list.prev) {
1554 ret = -EINVAL;
1555 goto out_unlock;
1556 }
1557
1558 /* conn->hchan is NULL after l2cap_conn_del() was called */
1559 if (!conn->hchan) {
1560 ret = -ENODEV;
1561 goto out_unlock;
1562 }
1563
1564 ret = user->probe(conn, user);
1565 if (ret)
1566 goto out_unlock;
1567
1568 list_add(&user->list, &conn->users);
1569 ret = 0;
1570
1571out_unlock:
1572 hci_dev_unlock(hdev);
1573 return ret;
1574}
1575EXPORT_SYMBOL(l2cap_register_user);
1576
1577void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user)
1578{
1579 struct hci_dev *hdev = conn->hcon->hdev;
1580
1581 hci_dev_lock(hdev);
1582
1583 if (!user->list.next || !user->list.prev)
1584 goto out_unlock;
1585
1586 list_del(&user->list);
1587 user->list.next = NULL;
1588 user->list.prev = NULL;
1589 user->remove(conn, user);
1590
1591out_unlock:
1592 hci_dev_unlock(hdev);
1593}
1594EXPORT_SYMBOL(l2cap_unregister_user);
1595
1596static void l2cap_unregister_all_users(struct l2cap_conn *conn)
1597{
1598 struct l2cap_user *user;
1599
1600 while (!list_empty(&conn->users)) {
1601 user = list_first_entry(&conn->users, struct l2cap_user, list);
1602 list_del(&user->list);
1603 user->list.next = NULL;
1604 user->list.prev = NULL;
1605 user->remove(conn, user);
1606 }
1607}
1608
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001609static void l2cap_conn_del(struct hci_conn *hcon, int err)
1610{
1611 struct l2cap_conn *conn = hcon->l2cap_data;
1612 struct l2cap_chan *chan, *l;
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001613
1614 if (!conn)
1615 return;
1616
1617 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
1618
1619 kfree_skb(conn->rx_skb);
1620
Johan Hedberg61a939c2014-01-17 20:45:11 +02001621 skb_queue_purge(&conn->pending_rx);
Jukka Taimisto7ab56c32014-06-12 10:15:13 +00001622
1623 /* We can not call flush_work(&conn->pending_rx_work) here since we
1624 * might block if we are running on a worker from the same workqueue
1625 * pending_rx_work is waiting on.
1626 */
1627 if (work_pending(&conn->pending_rx_work))
1628 cancel_work_sync(&conn->pending_rx_work);
Johan Hedberg61a939c2014-01-17 20:45:11 +02001629
Johan Hedbergdec5b492014-08-11 22:06:37 +03001630 if (work_pending(&conn->disconn_work))
1631 cancel_work_sync(&conn->disconn_work);
1632
David Herrmann2c8e1412013-04-06 20:28:45 +02001633 l2cap_unregister_all_users(conn);
1634
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001635 mutex_lock(&conn->chan_lock);
1636
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001637 /* Kill channels */
1638 list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
Mat Martineau61d6ef32012-04-27 16:50:50 -07001639 l2cap_chan_hold(chan);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001640 l2cap_chan_lock(chan);
1641
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001642 l2cap_chan_del(chan, err);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001643
1644 l2cap_chan_unlock(chan);
1645
Gustavo Padovan80b98022012-05-27 22:27:51 -03001646 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07001647 l2cap_chan_put(chan);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001648 }
1649
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001650 mutex_unlock(&conn->chan_lock);
1651
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001652 hci_chan_del(conn->hchan);
1653
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001654 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
Ulisses Furquim127074b2012-01-30 18:26:29 -02001655 cancel_delayed_work_sync(&conn->info_timer);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001656
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001657 hcon->l2cap_data = NULL;
David Herrmann9c903e32013-04-06 20:28:44 +02001658 conn->hchan = NULL;
1659 l2cap_conn_put(conn);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001660}
1661
Johan Hedbergdec5b492014-08-11 22:06:37 +03001662static void disconn_work(struct work_struct *work)
1663{
1664 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
1665 disconn_work);
1666
1667 BT_DBG("conn %p", conn);
1668
1669 l2cap_conn_del(conn->hcon, conn->disconn_err);
1670}
1671
1672void l2cap_conn_shutdown(struct l2cap_conn *conn, int err)
1673{
1674 struct hci_dev *hdev = conn->hcon->hdev;
1675
1676 BT_DBG("conn %p err %d", conn, err);
1677
1678 conn->disconn_err = err;
1679 queue_work(hdev->workqueue, &conn->disconn_work);
1680}
1681
David Herrmann9c903e32013-04-06 20:28:44 +02001682static void l2cap_conn_free(struct kref *ref)
1683{
1684 struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref);
1685
1686 hci_conn_put(conn->hcon);
1687 kfree(conn);
1688}
1689
1690void l2cap_conn_get(struct l2cap_conn *conn)
1691{
1692 kref_get(&conn->ref);
1693}
1694EXPORT_SYMBOL(l2cap_conn_get);
1695
1696void l2cap_conn_put(struct l2cap_conn *conn)
1697{
1698 kref_put(&conn->ref, l2cap_conn_free);
1699}
1700EXPORT_SYMBOL(l2cap_conn_put);
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702/* ---- Socket interface ---- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Ido Yarivc2287682012-04-20 15:46:07 -03001704/* Find socket with psm and source / destination bdaddr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 * Returns closest match.
1706 */
Ido Yarivc2287682012-04-20 15:46:07 -03001707static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm,
1708 bdaddr_t *src,
Johan Hedbergbf20fd42013-05-14 13:23:13 +03001709 bdaddr_t *dst,
1710 u8 link_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001712 struct l2cap_chan *c, *c1 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001714 read_lock(&chan_list_lock);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00001715
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001716 list_for_each_entry(c, &chan_list, global_l) {
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001717 if (state && c->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 continue;
1719
Johan Hedbergbf20fd42013-05-14 13:23:13 +03001720 if (link_type == ACL_LINK && c->src_type != BDADDR_BREDR)
1721 continue;
1722
1723 if (link_type == LE_LINK && c->src_type == BDADDR_BREDR)
1724 continue;
1725
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001726 if (c->psm == psm) {
Ido Yarivc2287682012-04-20 15:46:07 -03001727 int src_match, dst_match;
1728 int src_any, dst_any;
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /* Exact match. */
Marcel Holtmann7eafc592013-10-13 08:12:47 -07001731 src_match = !bacmp(&c->src, src);
1732 dst_match = !bacmp(&c->dst, dst);
Ido Yarivc2287682012-04-20 15:46:07 -03001733 if (src_match && dst_match) {
Johan Hedberga24cce12014-08-07 22:56:42 +03001734 l2cap_chan_hold(c);
Johannes Berga7567b22011-06-01 08:29:54 +02001735 read_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001736 return c;
1737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 /* Closest match */
Marcel Holtmann7eafc592013-10-13 08:12:47 -07001740 src_any = !bacmp(&c->src, BDADDR_ANY);
1741 dst_any = !bacmp(&c->dst, BDADDR_ANY);
Ido Yarivc2287682012-04-20 15:46:07 -03001742 if ((src_match && dst_any) || (src_any && dst_match) ||
1743 (src_any && dst_any))
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001744 c1 = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Johan Hedberga24cce12014-08-07 22:56:42 +03001748 if (c1)
1749 l2cap_chan_hold(c1);
1750
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001751 read_unlock(&chan_list_lock);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00001752
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001753 return c1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001756static void l2cap_monitor_timeout(struct work_struct *work)
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001757{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001758 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau4239d162012-05-17 20:53:49 -07001759 monitor_timer.work);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001760
Gustavo F. Padovan525cd182011-03-25 19:43:39 -03001761 BT_DBG("chan %p", chan);
Gustavo F. Padovan0e989582010-04-19 14:45:38 -03001762
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001763 l2cap_chan_lock(chan);
1764
Mat Martineau80909e02012-05-17 20:53:50 -07001765 if (!chan->conn) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001766 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001767 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001768 return;
1769 }
1770
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03001771 l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001772
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001773 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001774 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001775}
1776
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001777static void l2cap_retrans_timeout(struct work_struct *work)
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001778{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001779 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau4239d162012-05-17 20:53:49 -07001780 retrans_timer.work);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001781
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03001782 BT_DBG("chan %p", chan);
Gustavo F. Padovan0e989582010-04-19 14:45:38 -03001783
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001784 l2cap_chan_lock(chan);
1785
Mat Martineau80909e02012-05-17 20:53:50 -07001786 if (!chan->conn) {
1787 l2cap_chan_unlock(chan);
1788 l2cap_chan_put(chan);
1789 return;
1790 }
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001791
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03001792 l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001793 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001794 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001795}
1796
Gustavo Padovand6603662012-05-21 13:58:22 -03001797static void l2cap_streaming_send(struct l2cap_chan *chan,
1798 struct sk_buff_head *skbs)
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001799{
Gustavo F. Padovanccbb84a2010-08-30 18:44:44 -03001800 struct sk_buff *skb;
Mat Martineau37339372012-05-17 20:53:33 -07001801 struct l2cap_ctrl *control;
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001802
Mat Martineau37339372012-05-17 20:53:33 -07001803 BT_DBG("chan %p, skbs %p", chan, skbs);
1804
Mat Martineaub99e13a2012-10-23 15:24:19 -07001805 if (__chan_is_moving(chan))
1806 return;
1807
Mat Martineau37339372012-05-17 20:53:33 -07001808 skb_queue_splice_tail_init(skbs, &chan->tx_q);
1809
1810 while (!skb_queue_empty(&chan->tx_q)) {
1811
1812 skb = skb_dequeue(&chan->tx_q);
1813
1814 bt_cb(skb)->control.retries = 1;
1815 control = &bt_cb(skb)->control;
1816
1817 control->reqseq = 0;
1818 control->txseq = chan->next_tx_seq;
1819
1820 __pack_control(chan, control, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001821
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001822 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineau37339372012-05-17 20:53:33 -07001823 u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
1824 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001825 }
1826
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001827 l2cap_do_send(chan, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001828
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001829 BT_DBG("Sent txseq %u", control->txseq);
Mat Martineau37339372012-05-17 20:53:33 -07001830
Andrei Emeltchenko836be932011-10-17 12:19:57 +03001831 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
Mat Martineau37339372012-05-17 20:53:33 -07001832 chan->frames_sent++;
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001833 }
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001834}
1835
Szymon Janc67c9e842011-07-28 16:24:33 +02001836static int l2cap_ertm_send(struct l2cap_chan *chan)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001837{
1838 struct sk_buff *skb, *tx_skb;
Mat Martineau18a48e72012-05-17 20:53:34 -07001839 struct l2cap_ctrl *control;
1840 int sent = 0;
1841
1842 BT_DBG("chan %p", chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001843
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03001844 if (chan->state != BT_CONNECTED)
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001845 return -ENOTCONN;
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001846
Mat Martineau94122bb2012-05-02 09:42:02 -07001847 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
1848 return 0;
1849
Mat Martineaub99e13a2012-10-23 15:24:19 -07001850 if (__chan_is_moving(chan))
1851 return 0;
1852
Mat Martineau18a48e72012-05-17 20:53:34 -07001853 while (chan->tx_send_head &&
1854 chan->unacked_frames < chan->remote_tx_win &&
1855 chan->tx_state == L2CAP_TX_STATE_XMIT) {
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001856
Mat Martineau18a48e72012-05-17 20:53:34 -07001857 skb = chan->tx_send_head;
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001858
Mat Martineau18a48e72012-05-17 20:53:34 -07001859 bt_cb(skb)->control.retries = 1;
1860 control = &bt_cb(skb)->control;
Gustavo F. Padovan95ffa972010-06-18 20:37:33 -03001861
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03001862 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
Mat Martineau18a48e72012-05-17 20:53:34 -07001863 control->final = 1;
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03001864
Mat Martineau18a48e72012-05-17 20:53:34 -07001865 control->reqseq = chan->buffer_seq;
1866 chan->last_acked_seq = chan->buffer_seq;
1867 control->txseq = chan->next_tx_seq;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001868
Mat Martineau18a48e72012-05-17 20:53:34 -07001869 __pack_control(chan, control, skb);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001870
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001871 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineau18a48e72012-05-17 20:53:34 -07001872 u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
1873 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001874 }
1875
Mat Martineau18a48e72012-05-17 20:53:34 -07001876 /* Clone after data has been modified. Data is assumed to be
1877 read-only (for locking purposes) on cloned sk_buffs.
1878 */
1879 tx_skb = skb_clone(skb, GFP_KERNEL);
1880
1881 if (!tx_skb)
1882 break;
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -03001883
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -03001884 __set_retrans_timer(chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001885
Andrei Emeltchenko836be932011-10-17 12:19:57 +03001886 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
Mat Martineau18a48e72012-05-17 20:53:34 -07001887 chan->unacked_frames++;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03001888 chan->frames_sent++;
Mat Martineau18a48e72012-05-17 20:53:34 -07001889 sent++;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001890
Gustavo F. Padovan58d35f82011-04-04 16:16:44 -03001891 if (skb_queue_is_last(&chan->tx_q, skb))
1892 chan->tx_send_head = NULL;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001893 else
Gustavo F. Padovan58d35f82011-04-04 16:16:44 -03001894 chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
Mat Martineau18a48e72012-05-17 20:53:34 -07001895
1896 l2cap_do_send(chan, tx_skb);
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001897 BT_DBG("Sent txseq %u", control->txseq);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001898 }
1899
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001900 BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent,
1901 chan->unacked_frames, skb_queue_len(&chan->tx_q));
Mat Martineau18a48e72012-05-17 20:53:34 -07001902
1903 return sent;
Gustavo F. Padovan9e917af2010-05-01 16:15:37 -03001904}
1905
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001906static void l2cap_ertm_resend(struct l2cap_chan *chan)
1907{
1908 struct l2cap_ctrl control;
1909 struct sk_buff *skb;
1910 struct sk_buff *tx_skb;
1911 u16 seq;
1912
1913 BT_DBG("chan %p", chan);
1914
1915 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
1916 return;
1917
Mat Martineaub99e13a2012-10-23 15:24:19 -07001918 if (__chan_is_moving(chan))
1919 return;
1920
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001921 while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) {
1922 seq = l2cap_seq_list_pop(&chan->retrans_list);
1923
1924 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq);
1925 if (!skb) {
1926 BT_DBG("Error: Can't retransmit seq %d, frame missing",
Gustavo Padovan2d792812012-10-06 10:07:01 +01001927 seq);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001928 continue;
1929 }
1930
1931 bt_cb(skb)->control.retries++;
1932 control = bt_cb(skb)->control;
1933
1934 if (chan->max_tx != 0 &&
1935 bt_cb(skb)->control.retries > chan->max_tx) {
1936 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001937 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001938 l2cap_seq_list_clear(&chan->retrans_list);
1939 break;
1940 }
1941
1942 control.reqseq = chan->buffer_seq;
1943 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
1944 control.final = 1;
1945 else
1946 control.final = 0;
1947
1948 if (skb_cloned(skb)) {
1949 /* Cloned sk_buffs are read-only, so we need a
1950 * writeable copy
1951 */
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03001952 tx_skb = skb_copy(skb, GFP_KERNEL);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001953 } else {
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03001954 tx_skb = skb_clone(skb, GFP_KERNEL);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001955 }
1956
1957 if (!tx_skb) {
1958 l2cap_seq_list_clear(&chan->retrans_list);
1959 break;
1960 }
1961
1962 /* Update skb contents */
1963 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
1964 put_unaligned_le32(__pack_extended_control(&control),
1965 tx_skb->data + L2CAP_HDR_SIZE);
1966 } else {
1967 put_unaligned_le16(__pack_enhanced_control(&control),
1968 tx_skb->data + L2CAP_HDR_SIZE);
1969 }
1970
1971 if (chan->fcs == L2CAP_FCS_CRC16) {
1972 u16 fcs = crc16(0, (u8 *) tx_skb->data, tx_skb->len);
1973 put_unaligned_le16(fcs, skb_put(tx_skb,
1974 L2CAP_FCS_SIZE));
1975 }
1976
1977 l2cap_do_send(chan, tx_skb);
1978
1979 BT_DBG("Resent txseq %d", control.txseq);
1980
1981 chan->last_acked_seq = chan->buffer_seq;
1982 }
1983}
1984
Mat Martineauf80842a2012-05-17 20:53:46 -07001985static void l2cap_retransmit(struct l2cap_chan *chan,
1986 struct l2cap_ctrl *control)
1987{
1988 BT_DBG("chan %p, control %p", chan, control);
1989
1990 l2cap_seq_list_append(&chan->retrans_list, control->reqseq);
1991 l2cap_ertm_resend(chan);
1992}
1993
Mat Martineaud2a7ac52012-05-17 20:53:42 -07001994static void l2cap_retransmit_all(struct l2cap_chan *chan,
1995 struct l2cap_ctrl *control)
1996{
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001997 struct sk_buff *skb;
1998
1999 BT_DBG("chan %p, control %p", chan, control);
2000
2001 if (control->poll)
2002 set_bit(CONN_SEND_FBIT, &chan->conn_state);
2003
2004 l2cap_seq_list_clear(&chan->retrans_list);
2005
2006 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
2007 return;
2008
2009 if (chan->unacked_frames) {
2010 skb_queue_walk(&chan->tx_q, skb) {
2011 if (bt_cb(skb)->control.txseq == control->reqseq ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01002012 skb == chan->tx_send_head)
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002013 break;
2014 }
2015
2016 skb_queue_walk_from(&chan->tx_q, skb) {
2017 if (skb == chan->tx_send_head)
2018 break;
2019
2020 l2cap_seq_list_append(&chan->retrans_list,
2021 bt_cb(skb)->control.txseq);
2022 }
2023
2024 l2cap_ertm_resend(chan);
2025 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002026}
2027
Szymon Jancb17e73b2012-01-11 10:59:47 +01002028static void l2cap_send_ack(struct l2cap_chan *chan)
2029{
Mat Martineau0a0aba42012-05-17 20:53:39 -07002030 struct l2cap_ctrl control;
2031 u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
2032 chan->last_acked_seq);
2033 int threshold;
2034
2035 BT_DBG("chan %p last_acked_seq %d buffer_seq %d",
2036 chan, chan->last_acked_seq, chan->buffer_seq);
2037
2038 memset(&control, 0, sizeof(control));
2039 control.sframe = 1;
2040
2041 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
2042 chan->rx_state == L2CAP_RX_STATE_RECV) {
2043 __clear_ack_timer(chan);
2044 control.super = L2CAP_SUPER_RNR;
2045 control.reqseq = chan->buffer_seq;
2046 l2cap_send_sframe(chan, &control);
2047 } else {
2048 if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) {
2049 l2cap_ertm_send(chan);
2050 /* If any i-frames were sent, they included an ack */
2051 if (chan->buffer_seq == chan->last_acked_seq)
2052 frames_to_ack = 0;
2053 }
2054
Mat Martineauc20f8e32012-07-10 05:47:07 -07002055 /* Ack now if the window is 3/4ths full.
Mat Martineau0a0aba42012-05-17 20:53:39 -07002056 * Calculate without mul or div
2057 */
Mat Martineauc20f8e32012-07-10 05:47:07 -07002058 threshold = chan->ack_win;
Mat Martineau0a0aba42012-05-17 20:53:39 -07002059 threshold += threshold << 1;
2060 threshold >>= 2;
2061
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002062 BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack,
Mat Martineau0a0aba42012-05-17 20:53:39 -07002063 threshold);
2064
2065 if (frames_to_ack >= threshold) {
2066 __clear_ack_timer(chan);
2067 control.super = L2CAP_SUPER_RR;
2068 control.reqseq = chan->buffer_seq;
2069 l2cap_send_sframe(chan, &control);
2070 frames_to_ack = 0;
2071 }
2072
2073 if (frames_to_ack)
2074 __set_ack_timer(chan);
2075 }
Szymon Jancb17e73b2012-01-11 10:59:47 +01002076}
2077
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002078static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan,
2079 struct msghdr *msg, int len,
2080 int count, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002082 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002083 struct sk_buff **frag;
Gustavo Padovan90338942012-04-06 20:15:47 -03002084 int sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Jukka Rissanen04988782014-06-18 16:37:07 +03002086 if (chan->ops->memcpy_fromiovec(chan, skb_put(skb, count),
2087 msg->msg_iov, count))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002088 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 sent += count;
2091 len -= count;
2092
2093 /* Continuation fragments (no L2CAP header) */
2094 frag = &skb_shinfo(skb)->frag_list;
2095 while (len) {
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002096 struct sk_buff *tmp;
2097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 count = min_t(unsigned int, conn->mtu, len);
2099
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002100 tmp = chan->ops->alloc_skb(chan, 0, count,
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002101 msg->msg_flags & MSG_DONTWAIT);
2102 if (IS_ERR(tmp))
2103 return PTR_ERR(tmp);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002104
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002105 *frag = tmp;
2106
Jukka Rissanen04988782014-06-18 16:37:07 +03002107 if (chan->ops->memcpy_fromiovec(chan, skb_put(*frag, count),
2108 msg->msg_iov, count))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002109 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111 sent += count;
2112 len -= count;
2113
Gustavo Padovan2d0ed3d2012-05-11 13:16:12 -03002114 skb->len += (*frag)->len;
2115 skb->data_len += (*frag)->len;
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 frag = &(*frag)->next;
2118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 return sent;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002121}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002123static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan,
Marcel Holtmann8d463212014-06-05 15:22:51 +02002124 struct msghdr *msg, size_t len)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002125{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002126 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002127 struct sk_buff *skb;
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002128 int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002129 struct l2cap_hdr *lh;
2130
Marcel Holtmann8d463212014-06-05 15:22:51 +02002131 BT_DBG("chan %p psm 0x%2.2x len %zu", chan,
2132 __le16_to_cpu(chan->psm), len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002133
2134 count = min_t(unsigned int, (conn->mtu - hlen), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002135
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002136 skb = chan->ops->alloc_skb(chan, hlen, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002137 msg->msg_flags & MSG_DONTWAIT);
2138 if (IS_ERR(skb))
2139 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002140
2141 /* Create L2CAP header */
2142 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002143 lh->cid = cpu_to_le16(chan->dcid);
Andrei Emeltchenkodaf6a78c2012-05-03 10:55:52 +03002144 lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE);
Marcel Holtmann43b1b8d2013-10-12 06:01:26 -07002145 put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002146
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002147 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002148 if (unlikely(err < 0)) {
2149 kfree_skb(skb);
2150 return ERR_PTR(err);
2151 }
2152 return skb;
2153}
2154
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002155static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan,
Marcel Holtmann8d463212014-06-05 15:22:51 +02002156 struct msghdr *msg, size_t len)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002157{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002158 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002159 struct sk_buff *skb;
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002160 int err, count;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002161 struct l2cap_hdr *lh;
2162
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002163 BT_DBG("chan %p len %zu", chan, len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002164
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002165 count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002166
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002167 skb = chan->ops->alloc_skb(chan, L2CAP_HDR_SIZE, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002168 msg->msg_flags & MSG_DONTWAIT);
2169 if (IS_ERR(skb))
2170 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002171
2172 /* Create L2CAP header */
2173 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002174 lh->cid = cpu_to_le16(chan->dcid);
Gustavo Padovan6ff9b5e2012-05-02 11:56:17 -03002175 lh->len = cpu_to_le16(len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002176
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002177 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002178 if (unlikely(err < 0)) {
2179 kfree_skb(skb);
2180 return ERR_PTR(err);
2181 }
2182 return skb;
2183}
2184
Luiz Augusto von Dentzab0ff762011-09-12 20:00:50 +03002185static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan,
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002186 struct msghdr *msg, size_t len,
2187 u16 sdulen)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002188{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002189 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002190 struct sk_buff *skb;
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03002191 int err, count, hlen;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002192 struct l2cap_hdr *lh;
2193
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002194 BT_DBG("chan %p len %zu", chan, len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002195
Gustavo F. Padovan0ee0d202010-05-01 16:15:41 -03002196 if (!conn)
2197 return ERR_PTR(-ENOTCONN);
2198
Gustavo Padovanba7aa642012-05-29 13:29:16 -03002199 hlen = __ertm_hdr_size(chan);
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03002200
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002201 if (sdulen)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002202 hlen += L2CAP_SDULEN_SIZE;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002203
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03002204 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002205 hlen += L2CAP_FCS_SIZE;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03002206
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002207 count = min_t(unsigned int, (conn->mtu - hlen), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002208
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002209 skb = chan->ops->alloc_skb(chan, hlen, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002210 msg->msg_flags & MSG_DONTWAIT);
2211 if (IS_ERR(skb))
2212 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002213
2214 /* Create L2CAP header */
2215 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002216 lh->cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002217 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03002218
Mat Martineau18a48e72012-05-17 20:53:34 -07002219 /* Control header is populated later */
2220 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
2221 put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
2222 else
2223 put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03002224
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002225 if (sdulen)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002226 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002227
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002228 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002229 if (unlikely(err < 0)) {
2230 kfree_skb(skb);
2231 return ERR_PTR(err);
2232 }
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03002233
Mat Martineau18a48e72012-05-17 20:53:34 -07002234 bt_cb(skb)->control.fcs = chan->fcs;
Mat Martineau3ce35142012-04-25 16:36:14 -07002235 bt_cb(skb)->control.retries = 0;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002236 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
Mat Martineau94122bb2012-05-02 09:42:02 -07002239static int l2cap_segment_sdu(struct l2cap_chan *chan,
2240 struct sk_buff_head *seg_queue,
2241 struct msghdr *msg, size_t len)
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002242{
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002243 struct sk_buff *skb;
Mat Martineau94122bb2012-05-02 09:42:02 -07002244 u16 sdu_len;
2245 size_t pdu_len;
Mat Martineau94122bb2012-05-02 09:42:02 -07002246 u8 sar;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002247
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002248 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002249
Mat Martineau94122bb2012-05-02 09:42:02 -07002250 /* It is critical that ERTM PDUs fit in a single HCI fragment,
2251 * so fragmented skbs are not used. The HCI layer's handling
2252 * of fragmented skbs is not compatible with ERTM's queueing.
2253 */
2254
2255 /* PDU size is derived from the HCI MTU */
2256 pdu_len = chan->conn->mtu;
2257
Mat Martineaua5495742012-10-23 15:24:21 -07002258 /* Constrain PDU size for BR/EDR connections */
2259 if (!chan->hs_hcon)
2260 pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD);
Mat Martineau94122bb2012-05-02 09:42:02 -07002261
2262 /* Adjust for largest possible L2CAP overhead. */
Gustavo Padovan35d401d2012-05-25 18:57:05 -03002263 if (chan->fcs)
2264 pdu_len -= L2CAP_FCS_SIZE;
2265
Gustavo Padovanba7aa642012-05-29 13:29:16 -03002266 pdu_len -= __ertm_hdr_size(chan);
Mat Martineau94122bb2012-05-02 09:42:02 -07002267
2268 /* Remote device may have requested smaller PDUs */
2269 pdu_len = min_t(size_t, pdu_len, chan->remote_mps);
2270
2271 if (len <= pdu_len) {
2272 sar = L2CAP_SAR_UNSEGMENTED;
2273 sdu_len = 0;
2274 pdu_len = len;
2275 } else {
2276 sar = L2CAP_SAR_START;
2277 sdu_len = len;
2278 pdu_len -= L2CAP_SDULEN_SIZE;
2279 }
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002280
2281 while (len > 0) {
Mat Martineau94122bb2012-05-02 09:42:02 -07002282 skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002283
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002284 if (IS_ERR(skb)) {
Mat Martineau94122bb2012-05-02 09:42:02 -07002285 __skb_queue_purge(seg_queue);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002286 return PTR_ERR(skb);
2287 }
2288
Mat Martineau94122bb2012-05-02 09:42:02 -07002289 bt_cb(skb)->control.sar = sar;
2290 __skb_queue_tail(seg_queue, skb);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002291
Mat Martineau94122bb2012-05-02 09:42:02 -07002292 len -= pdu_len;
2293 if (sdu_len) {
2294 sdu_len = 0;
2295 pdu_len += L2CAP_SDULEN_SIZE;
2296 }
2297
2298 if (len <= pdu_len) {
2299 sar = L2CAP_SAR_END;
2300 pdu_len = len;
2301 } else {
2302 sar = L2CAP_SAR_CONTINUE;
2303 }
2304 }
2305
Gustavo Padovanf0f62792012-05-29 13:29:17 -03002306 return 0;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002307}
2308
Johan Hedberg177f8f22013-05-31 17:54:51 +03002309static struct sk_buff *l2cap_create_le_flowctl_pdu(struct l2cap_chan *chan,
2310 struct msghdr *msg,
2311 size_t len, u16 sdulen)
2312{
2313 struct l2cap_conn *conn = chan->conn;
2314 struct sk_buff *skb;
2315 int err, count, hlen;
2316 struct l2cap_hdr *lh;
2317
2318 BT_DBG("chan %p len %zu", chan, len);
2319
2320 if (!conn)
2321 return ERR_PTR(-ENOTCONN);
2322
2323 hlen = L2CAP_HDR_SIZE;
2324
2325 if (sdulen)
2326 hlen += L2CAP_SDULEN_SIZE;
2327
2328 count = min_t(unsigned int, (conn->mtu - hlen), len);
2329
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002330 skb = chan->ops->alloc_skb(chan, hlen, count,
Johan Hedberg177f8f22013-05-31 17:54:51 +03002331 msg->msg_flags & MSG_DONTWAIT);
2332 if (IS_ERR(skb))
2333 return skb;
2334
2335 /* Create L2CAP header */
2336 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
2337 lh->cid = cpu_to_le16(chan->dcid);
2338 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
2339
2340 if (sdulen)
2341 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
2342
2343 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
2344 if (unlikely(err < 0)) {
2345 kfree_skb(skb);
2346 return ERR_PTR(err);
2347 }
2348
2349 return skb;
2350}
2351
2352static int l2cap_segment_le_sdu(struct l2cap_chan *chan,
2353 struct sk_buff_head *seg_queue,
2354 struct msghdr *msg, size_t len)
2355{
2356 struct sk_buff *skb;
2357 size_t pdu_len;
2358 u16 sdu_len;
2359
2360 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
2361
2362 pdu_len = chan->conn->mtu - L2CAP_HDR_SIZE;
2363
2364 pdu_len = min_t(size_t, pdu_len, chan->remote_mps);
2365
2366 sdu_len = len;
2367 pdu_len -= L2CAP_SDULEN_SIZE;
2368
2369 while (len > 0) {
2370 if (len <= pdu_len)
2371 pdu_len = len;
2372
2373 skb = l2cap_create_le_flowctl_pdu(chan, msg, pdu_len, sdu_len);
2374 if (IS_ERR(skb)) {
2375 __skb_queue_purge(seg_queue);
2376 return PTR_ERR(skb);
2377 }
2378
2379 __skb_queue_tail(seg_queue, skb);
2380
2381 len -= pdu_len;
2382
2383 if (sdu_len) {
2384 sdu_len = 0;
2385 pdu_len += L2CAP_SDULEN_SIZE;
2386 }
2387 }
2388
2389 return 0;
2390}
2391
Marcel Holtmann8d463212014-06-05 15:22:51 +02002392int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002393{
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002394 struct sk_buff *skb;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002395 int err;
Mat Martineau94122bb2012-05-02 09:42:02 -07002396 struct sk_buff_head seg_queue;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002397
Seung-Woo Kim31e8ce82013-11-05 18:46:33 +09002398 if (!chan->conn)
2399 return -ENOTCONN;
2400
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002401 /* Connectionless channel */
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03002402 if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
Marcel Holtmann8d463212014-06-05 15:22:51 +02002403 skb = l2cap_create_connless_pdu(chan, msg, len);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002404 if (IS_ERR(skb))
2405 return PTR_ERR(skb);
2406
Andrzej Kaczmarekede81a22014-02-25 17:16:22 +01002407 /* Channel lock is released before requesting new skb and then
2408 * reacquired thus we need to recheck channel state.
2409 */
2410 if (chan->state != BT_CONNECTED) {
2411 kfree_skb(skb);
2412 return -ENOTCONN;
2413 }
2414
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002415 l2cap_do_send(chan, skb);
2416 return len;
2417 }
2418
2419 switch (chan->mode) {
Johan Hedberg38319712013-05-17 12:49:23 +03002420 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedberg177f8f22013-05-31 17:54:51 +03002421 /* Check outgoing MTU */
2422 if (len > chan->omtu)
2423 return -EMSGSIZE;
2424
Johan Hedbergfad5fc82013-12-05 09:45:01 +02002425 if (!chan->tx_credits)
2426 return -EAGAIN;
2427
Johan Hedberg177f8f22013-05-31 17:54:51 +03002428 __skb_queue_head_init(&seg_queue);
2429
2430 err = l2cap_segment_le_sdu(chan, &seg_queue, msg, len);
2431
2432 if (chan->state != BT_CONNECTED) {
2433 __skb_queue_purge(&seg_queue);
2434 err = -ENOTCONN;
2435 }
2436
2437 if (err)
2438 return err;
2439
2440 skb_queue_splice_tail_init(&seg_queue, &chan->tx_q);
2441
2442 while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) {
2443 l2cap_do_send(chan, skb_dequeue(&chan->tx_q));
2444 chan->tx_credits--;
2445 }
2446
2447 if (!chan->tx_credits)
2448 chan->ops->suspend(chan);
2449
2450 err = len;
2451
2452 break;
2453
Johan Hedbergfad5fc82013-12-05 09:45:01 +02002454 case L2CAP_MODE_BASIC:
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002455 /* Check outgoing MTU */
2456 if (len > chan->omtu)
2457 return -EMSGSIZE;
2458
2459 /* Create a basic PDU */
Marcel Holtmann8d463212014-06-05 15:22:51 +02002460 skb = l2cap_create_basic_pdu(chan, msg, len);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002461 if (IS_ERR(skb))
2462 return PTR_ERR(skb);
2463
Andrzej Kaczmarekede81a22014-02-25 17:16:22 +01002464 /* Channel lock is released before requesting new skb and then
2465 * reacquired thus we need to recheck channel state.
2466 */
2467 if (chan->state != BT_CONNECTED) {
2468 kfree_skb(skb);
2469 return -ENOTCONN;
2470 }
2471
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002472 l2cap_do_send(chan, skb);
2473 err = len;
2474 break;
2475
2476 case L2CAP_MODE_ERTM:
2477 case L2CAP_MODE_STREAMING:
Mat Martineau94122bb2012-05-02 09:42:02 -07002478 /* Check outgoing MTU */
2479 if (len > chan->omtu) {
2480 err = -EMSGSIZE;
2481 break;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002482 }
2483
Mat Martineau94122bb2012-05-02 09:42:02 -07002484 __skb_queue_head_init(&seg_queue);
2485
2486 /* Do segmentation before calling in to the state machine,
2487 * since it's possible to block while waiting for memory
2488 * allocation.
2489 */
2490 err = l2cap_segment_sdu(chan, &seg_queue, msg, len);
2491
2492 /* The channel could have been closed while segmenting,
2493 * check that it is still connected.
2494 */
2495 if (chan->state != BT_CONNECTED) {
2496 __skb_queue_purge(&seg_queue);
2497 err = -ENOTCONN;
2498 }
2499
2500 if (err)
2501 break;
2502
Mat Martineau37339372012-05-17 20:53:33 -07002503 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo Padovand6603662012-05-21 13:58:22 -03002504 l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST);
Mat Martineau37339372012-05-17 20:53:33 -07002505 else
Gustavo Padovand6603662012-05-21 13:58:22 -03002506 l2cap_streaming_send(chan, &seg_queue);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002507
Gustavo Padovand6603662012-05-21 13:58:22 -03002508 err = len;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002509
Mat Martineau94122bb2012-05-02 09:42:02 -07002510 /* If the skbs were not queued for sending, they'll still be in
2511 * seg_queue and need to be purged.
2512 */
2513 __skb_queue_purge(&seg_queue);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002514 break;
2515
2516 default:
2517 BT_DBG("bad state %1.1x", chan->mode);
2518 err = -EBADFD;
2519 }
2520
2521 return err;
2522}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03002523EXPORT_SYMBOL_GPL(l2cap_chan_send);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002524
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002525static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq)
2526{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002527 struct l2cap_ctrl control;
2528 u16 seq;
2529
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002530 BT_DBG("chan %p, txseq %u", chan, txseq);
Mat Martineaubed68bd2012-05-17 20:53:44 -07002531
2532 memset(&control, 0, sizeof(control));
2533 control.sframe = 1;
2534 control.super = L2CAP_SUPER_SREJ;
2535
2536 for (seq = chan->expected_tx_seq; seq != txseq;
2537 seq = __next_seq(chan, seq)) {
2538 if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) {
2539 control.reqseq = seq;
2540 l2cap_send_sframe(chan, &control);
2541 l2cap_seq_list_append(&chan->srej_list, seq);
2542 }
2543 }
2544
2545 chan->expected_tx_seq = __next_seq(chan, txseq);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002546}
2547
2548static void l2cap_send_srej_tail(struct l2cap_chan *chan)
2549{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002550 struct l2cap_ctrl control;
2551
2552 BT_DBG("chan %p", chan);
2553
2554 if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR)
2555 return;
2556
2557 memset(&control, 0, sizeof(control));
2558 control.sframe = 1;
2559 control.super = L2CAP_SUPER_SREJ;
2560 control.reqseq = chan->srej_list.tail;
2561 l2cap_send_sframe(chan, &control);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002562}
2563
2564static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq)
2565{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002566 struct l2cap_ctrl control;
2567 u16 initial_head;
2568 u16 seq;
2569
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002570 BT_DBG("chan %p, txseq %u", chan, txseq);
Mat Martineaubed68bd2012-05-17 20:53:44 -07002571
2572 memset(&control, 0, sizeof(control));
2573 control.sframe = 1;
2574 control.super = L2CAP_SUPER_SREJ;
2575
2576 /* Capture initial list head to allow only one pass through the list. */
2577 initial_head = chan->srej_list.head;
2578
2579 do {
2580 seq = l2cap_seq_list_pop(&chan->srej_list);
2581 if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR)
2582 break;
2583
2584 control.reqseq = seq;
2585 l2cap_send_sframe(chan, &control);
2586 l2cap_seq_list_append(&chan->srej_list, seq);
2587 } while (chan->srej_list.head != initial_head);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002588}
2589
Mat Martineau608bcc62012-05-17 20:53:32 -07002590static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq)
2591{
2592 struct sk_buff *acked_skb;
2593 u16 ackseq;
2594
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002595 BT_DBG("chan %p, reqseq %u", chan, reqseq);
Mat Martineau608bcc62012-05-17 20:53:32 -07002596
2597 if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq)
2598 return;
2599
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002600 BT_DBG("expected_ack_seq %u, unacked_frames %u",
Mat Martineau608bcc62012-05-17 20:53:32 -07002601 chan->expected_ack_seq, chan->unacked_frames);
2602
2603 for (ackseq = chan->expected_ack_seq; ackseq != reqseq;
2604 ackseq = __next_seq(chan, ackseq)) {
2605
2606 acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq);
2607 if (acked_skb) {
2608 skb_unlink(acked_skb, &chan->tx_q);
2609 kfree_skb(acked_skb);
2610 chan->unacked_frames--;
2611 }
2612 }
2613
2614 chan->expected_ack_seq = reqseq;
2615
2616 if (chan->unacked_frames == 0)
2617 __clear_retrans_timer(chan);
2618
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002619 BT_DBG("unacked_frames %u", chan->unacked_frames);
Mat Martineau608bcc62012-05-17 20:53:32 -07002620}
2621
2622static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan)
2623{
2624 BT_DBG("chan %p", chan);
2625
2626 chan->expected_tx_seq = chan->buffer_seq;
2627 l2cap_seq_list_clear(&chan->srej_list);
2628 skb_queue_purge(&chan->srej_q);
2629 chan->rx_state = L2CAP_RX_STATE_RECV;
2630}
2631
Gustavo Padovand6603662012-05-21 13:58:22 -03002632static void l2cap_tx_state_xmit(struct l2cap_chan *chan,
2633 struct l2cap_ctrl *control,
2634 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002635{
Mat Martineau608bcc62012-05-17 20:53:32 -07002636 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2637 event);
2638
2639 switch (event) {
2640 case L2CAP_EV_DATA_REQUEST:
2641 if (chan->tx_send_head == NULL)
2642 chan->tx_send_head = skb_peek(skbs);
2643
2644 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2645 l2cap_ertm_send(chan);
2646 break;
2647 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2648 BT_DBG("Enter LOCAL_BUSY");
2649 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2650
2651 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2652 /* The SREJ_SENT state must be aborted if we are to
2653 * enter the LOCAL_BUSY state.
2654 */
2655 l2cap_abort_rx_srej_sent(chan);
2656 }
2657
2658 l2cap_send_ack(chan);
2659
2660 break;
2661 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2662 BT_DBG("Exit LOCAL_BUSY");
2663 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2664
2665 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2666 struct l2cap_ctrl local_control;
2667
2668 memset(&local_control, 0, sizeof(local_control));
2669 local_control.sframe = 1;
2670 local_control.super = L2CAP_SUPER_RR;
2671 local_control.poll = 1;
2672 local_control.reqseq = chan->buffer_seq;
Mat Martineaua67d7f62012-05-17 20:53:35 -07002673 l2cap_send_sframe(chan, &local_control);
Mat Martineau608bcc62012-05-17 20:53:32 -07002674
2675 chan->retry_count = 1;
2676 __set_monitor_timer(chan);
2677 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2678 }
2679 break;
2680 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2681 l2cap_process_reqseq(chan, control->reqseq);
2682 break;
2683 case L2CAP_EV_EXPLICIT_POLL:
2684 l2cap_send_rr_or_rnr(chan, 1);
2685 chan->retry_count = 1;
2686 __set_monitor_timer(chan);
2687 __clear_ack_timer(chan);
2688 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2689 break;
2690 case L2CAP_EV_RETRANS_TO:
2691 l2cap_send_rr_or_rnr(chan, 1);
2692 chan->retry_count = 1;
2693 __set_monitor_timer(chan);
2694 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2695 break;
2696 case L2CAP_EV_RECV_FBIT:
2697 /* Nothing to process */
2698 break;
2699 default:
2700 break;
2701 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002702}
2703
Gustavo Padovand6603662012-05-21 13:58:22 -03002704static void l2cap_tx_state_wait_f(struct l2cap_chan *chan,
2705 struct l2cap_ctrl *control,
2706 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002707{
Mat Martineau608bcc62012-05-17 20:53:32 -07002708 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2709 event);
2710
2711 switch (event) {
2712 case L2CAP_EV_DATA_REQUEST:
2713 if (chan->tx_send_head == NULL)
2714 chan->tx_send_head = skb_peek(skbs);
2715 /* Queue data, but don't send. */
2716 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2717 break;
2718 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2719 BT_DBG("Enter LOCAL_BUSY");
2720 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2721
2722 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2723 /* The SREJ_SENT state must be aborted if we are to
2724 * enter the LOCAL_BUSY state.
2725 */
2726 l2cap_abort_rx_srej_sent(chan);
2727 }
2728
2729 l2cap_send_ack(chan);
2730
2731 break;
2732 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2733 BT_DBG("Exit LOCAL_BUSY");
2734 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2735
2736 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2737 struct l2cap_ctrl local_control;
2738 memset(&local_control, 0, sizeof(local_control));
2739 local_control.sframe = 1;
2740 local_control.super = L2CAP_SUPER_RR;
2741 local_control.poll = 1;
2742 local_control.reqseq = chan->buffer_seq;
Mat Martineaua67d7f62012-05-17 20:53:35 -07002743 l2cap_send_sframe(chan, &local_control);
Mat Martineau608bcc62012-05-17 20:53:32 -07002744
2745 chan->retry_count = 1;
2746 __set_monitor_timer(chan);
2747 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2748 }
2749 break;
2750 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2751 l2cap_process_reqseq(chan, control->reqseq);
2752
2753 /* Fall through */
2754
2755 case L2CAP_EV_RECV_FBIT:
2756 if (control && control->final) {
2757 __clear_monitor_timer(chan);
2758 if (chan->unacked_frames > 0)
2759 __set_retrans_timer(chan);
2760 chan->retry_count = 0;
2761 chan->tx_state = L2CAP_TX_STATE_XMIT;
2762 BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state);
2763 }
2764 break;
2765 case L2CAP_EV_EXPLICIT_POLL:
2766 /* Ignore */
2767 break;
2768 case L2CAP_EV_MONITOR_TO:
2769 if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) {
2770 l2cap_send_rr_or_rnr(chan, 1);
2771 __set_monitor_timer(chan);
2772 chan->retry_count++;
2773 } else {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02002774 l2cap_send_disconn_req(chan, ECONNABORTED);
Mat Martineau608bcc62012-05-17 20:53:32 -07002775 }
2776 break;
2777 default:
2778 break;
2779 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002780}
2781
Gustavo Padovand6603662012-05-21 13:58:22 -03002782static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
2783 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002784{
Mat Martineau608bcc62012-05-17 20:53:32 -07002785 BT_DBG("chan %p, control %p, skbs %p, event %d, state %d",
2786 chan, control, skbs, event, chan->tx_state);
2787
2788 switch (chan->tx_state) {
2789 case L2CAP_TX_STATE_XMIT:
Gustavo Padovand6603662012-05-21 13:58:22 -03002790 l2cap_tx_state_xmit(chan, control, skbs, event);
Mat Martineau608bcc62012-05-17 20:53:32 -07002791 break;
2792 case L2CAP_TX_STATE_WAIT_F:
Gustavo Padovand6603662012-05-21 13:58:22 -03002793 l2cap_tx_state_wait_f(chan, control, skbs, event);
Mat Martineau608bcc62012-05-17 20:53:32 -07002794 break;
2795 default:
2796 /* Ignore event */
2797 break;
2798 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002799}
2800
Mat Martineau4b51dae92012-05-17 20:53:37 -07002801static void l2cap_pass_to_tx(struct l2cap_chan *chan,
2802 struct l2cap_ctrl *control)
2803{
2804 BT_DBG("chan %p, control %p", chan, control);
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03002805 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT);
Mat Martineau4b51dae92012-05-17 20:53:37 -07002806}
2807
Mat Martineauf80842a2012-05-17 20:53:46 -07002808static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan,
2809 struct l2cap_ctrl *control)
2810{
2811 BT_DBG("chan %p, control %p", chan, control);
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03002812 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT);
Mat Martineauf80842a2012-05-17 20:53:46 -07002813}
2814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815/* Copy frame to all raw sockets on that connection */
2816static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
2817{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 struct sk_buff *nskb;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03002819 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 BT_DBG("conn %p", conn);
2822
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002823 mutex_lock(&conn->chan_lock);
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -02002824
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002825 list_for_each_entry(chan, &conn->chan_l, list) {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03002826 if (chan->chan_type != L2CAP_CHAN_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 continue;
2828
Gustavo Padovan7f5396a2013-10-21 18:22:25 -02002829 /* Don't send frame to the channel it came from */
2830 if (bt_cb(skb)->chan == chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 continue;
Gustavo Padovan7f5396a2013-10-21 18:22:25 -02002832
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002833 nskb = skb_clone(skb, GFP_KERNEL);
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03002834 if (!nskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 continue;
Gustavo Padovan80b98022012-05-27 22:27:51 -03002836 if (chan->ops->recv(chan, nskb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 kfree_skb(nskb);
2838 }
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -02002839
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002840 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841}
2842
2843/* ---- L2CAP signalling commands ---- */
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002844static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code,
2845 u8 ident, u16 dlen, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
2847 struct sk_buff *skb, **frag;
2848 struct l2cap_cmd_hdr *cmd;
2849 struct l2cap_hdr *lh;
2850 int len, count;
2851
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002852 BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u",
2853 conn, code, ident, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Anderson Lizardo300b9622013-06-02 16:30:40 -04002855 if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE)
2856 return NULL;
2857
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
2859 count = min_t(unsigned int, conn->mtu, len);
2860
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002861 skb = bt_skb_alloc(count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 if (!skb)
2863 return NULL;
2864
2865 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002866 lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02002867
2868 if (conn->hcon->type == LE_LINK)
Joe Perchesdcf4adb2014-03-12 10:52:35 -07002869 lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02002870 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -07002871 lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
2874 cmd->code = code;
2875 cmd->ident = ident;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002876 cmd->len = cpu_to_le16(dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
2878 if (dlen) {
2879 count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
2880 memcpy(skb_put(skb, count), data, count);
2881 data += count;
2882 }
2883
2884 len -= skb->len;
2885
2886 /* Continuation fragments (no L2CAP header) */
2887 frag = &skb_shinfo(skb)->frag_list;
2888 while (len) {
2889 count = min_t(unsigned int, conn->mtu, len);
2890
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002891 *frag = bt_skb_alloc(count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (!*frag)
2893 goto fail;
2894
2895 memcpy(skb_put(*frag, count), data, count);
2896
2897 len -= count;
2898 data += count;
2899
2900 frag = &(*frag)->next;
2901 }
2902
2903 return skb;
2904
2905fail:
2906 kfree_skb(skb);
2907 return NULL;
2908}
2909
Gustavo Padovan2d792812012-10-06 10:07:01 +01002910static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen,
2911 unsigned long *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
2913 struct l2cap_conf_opt *opt = *ptr;
2914 int len;
2915
2916 len = L2CAP_CONF_OPT_SIZE + opt->len;
2917 *ptr += len;
2918
2919 *type = opt->type;
2920 *olen = opt->len;
2921
2922 switch (opt->len) {
2923 case 1:
2924 *val = *((u8 *) opt->val);
2925 break;
2926
2927 case 2:
steven miaobfaaeb32010-10-16 18:29:47 -04002928 *val = get_unaligned_le16(opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 break;
2930
2931 case 4:
steven miaobfaaeb32010-10-16 18:29:47 -04002932 *val = get_unaligned_le32(opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 break;
2934
2935 default:
2936 *val = (unsigned long) opt->val;
2937 break;
2938 }
2939
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002940 BT_DBG("type 0x%2.2x len %u val 0x%lx", *type, opt->len, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return len;
2942}
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
2945{
2946 struct l2cap_conf_opt *opt = *ptr;
2947
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002948 BT_DBG("type 0x%2.2x len %u val 0x%lx", type, len, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
2950 opt->type = type;
2951 opt->len = len;
2952
2953 switch (len) {
2954 case 1:
2955 *((u8 *) opt->val) = val;
2956 break;
2957
2958 case 2:
Gustavo F. Padovan4f8b6912010-10-18 14:25:53 -02002959 put_unaligned_le16(val, opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 break;
2961
2962 case 4:
Gustavo F. Padovan4f8b6912010-10-18 14:25:53 -02002963 put_unaligned_le32(val, opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 break;
2965
2966 default:
2967 memcpy(opt->val, (void *) val, len);
2968 break;
2969 }
2970
2971 *ptr += L2CAP_CONF_OPT_SIZE + len;
2972}
2973
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03002974static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan)
2975{
2976 struct l2cap_conf_efs efs;
2977
Szymon Janc1ec918c2011-11-16 09:32:21 +01002978 switch (chan->mode) {
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03002979 case L2CAP_MODE_ERTM:
2980 efs.id = chan->local_id;
2981 efs.stype = chan->local_stype;
2982 efs.msdu = cpu_to_le16(chan->local_msdu);
2983 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07002984 efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT);
2985 efs.flush_to = cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03002986 break;
2987
2988 case L2CAP_MODE_STREAMING:
2989 efs.id = 1;
2990 efs.stype = L2CAP_SERV_BESTEFFORT;
2991 efs.msdu = cpu_to_le16(chan->local_msdu);
2992 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
2993 efs.acc_lat = 0;
2994 efs.flush_to = 0;
2995 break;
2996
2997 default:
2998 return;
2999 }
3000
3001 l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs),
Andrei Emeltchenko8936fa62012-10-08 11:14:41 +03003002 (unsigned long) &efs);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003003}
3004
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003005static void l2cap_ack_timeout(struct work_struct *work)
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003006{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003007 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau03625202012-05-17 20:53:51 -07003008 ack_timer.work);
3009 u16 frames_to_ack;
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003010
Gustavo F. Padovan2fb9b3d2011-12-22 16:56:05 -02003011 BT_DBG("chan %p", chan);
3012
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003013 l2cap_chan_lock(chan);
3014
Mat Martineau03625202012-05-17 20:53:51 -07003015 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
3016 chan->last_acked_seq);
3017
3018 if (frames_to_ack)
3019 l2cap_send_rr_or_rnr(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003020
3021 l2cap_chan_unlock(chan);
Szymon Janc09bfb2e2012-01-11 10:59:49 +01003022 l2cap_chan_put(chan);
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003023}
3024
Andrei Emeltchenko466f8002012-05-29 13:59:01 +03003025int l2cap_ertm_init(struct l2cap_chan *chan)
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003026{
Mat Martineau3c588192012-04-11 10:48:42 -07003027 int err;
3028
Mat Martineau105bdf92012-04-27 16:50:48 -07003029 chan->next_tx_seq = 0;
3030 chan->expected_tx_seq = 0;
Gustavo F. Padovan42e5c802011-03-25 19:58:34 -03003031 chan->expected_ack_seq = 0;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03003032 chan->unacked_frames = 0;
Gustavo F. Padovan42e5c802011-03-25 19:58:34 -03003033 chan->buffer_seq = 0;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03003034 chan->frames_sent = 0;
Mat Martineau105bdf92012-04-27 16:50:48 -07003035 chan->last_acked_seq = 0;
3036 chan->sdu = NULL;
3037 chan->sdu_last_frag = NULL;
3038 chan->sdu_len = 0;
3039
Mat Martineaud34c34f2012-05-14 14:49:27 -07003040 skb_queue_head_init(&chan->tx_q);
3041
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003042 chan->local_amp_id = AMP_ID_BREDR;
3043 chan->move_id = AMP_ID_BREDR;
Mat Martineau08333282012-10-23 15:24:06 -07003044 chan->move_state = L2CAP_MOVE_STABLE;
3045 chan->move_role = L2CAP_MOVE_ROLE_NONE;
3046
Mat Martineau105bdf92012-04-27 16:50:48 -07003047 if (chan->mode != L2CAP_MODE_ERTM)
3048 return 0;
3049
3050 chan->rx_state = L2CAP_RX_STATE_RECV;
3051 chan->tx_state = L2CAP_TX_STATE_XMIT;
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003052
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003053 INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout);
3054 INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout);
3055 INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003056
Gustavo F. Padovanf1c67752011-03-25 20:36:10 -03003057 skb_queue_head_init(&chan->srej_q);
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03003058
Mat Martineau3c588192012-04-11 10:48:42 -07003059 err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win);
3060 if (err < 0)
3061 return err;
3062
Mat Martineau9dc9aff2012-05-17 16:20:14 -07003063 err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win);
3064 if (err < 0)
3065 l2cap_seq_list_free(&chan->srej_list);
3066
3067 return err;
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003068}
3069
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003070static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
3071{
3072 switch (mode) {
3073 case L2CAP_MODE_STREAMING:
3074 case L2CAP_MODE_ERTM:
3075 if (l2cap_mode_supported(mode, remote_feat_mask))
3076 return mode;
3077 /* fall through */
3078 default:
3079 return L2CAP_MODE_BASIC;
3080 }
3081}
3082
Marcel Holtmann848566b2013-10-01 22:59:22 -07003083static inline bool __l2cap_ews_supported(struct l2cap_conn *conn)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003084{
Marcel Holtmann848566b2013-10-01 22:59:22 -07003085 return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_WINDOW;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003086}
3087
Marcel Holtmann848566b2013-10-01 22:59:22 -07003088static inline bool __l2cap_efs_supported(struct l2cap_conn *conn)
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003089{
Marcel Holtmann848566b2013-10-01 22:59:22 -07003090 return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_FLOW;
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003091}
3092
Mat Martineau36c86c82012-10-23 15:24:20 -07003093static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan,
3094 struct l2cap_conf_rfc *rfc)
3095{
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003096 if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) {
Mat Martineau36c86c82012-10-23 15:24:20 -07003097 u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to;
3098
3099 /* Class 1 devices have must have ERTM timeouts
3100 * exceeding the Link Supervision Timeout. The
3101 * default Link Supervision Timeout for AMP
3102 * controllers is 10 seconds.
3103 *
3104 * Class 1 devices use 0xffffffff for their
3105 * best-effort flush timeout, so the clamping logic
3106 * will result in a timeout that meets the above
3107 * requirement. ERTM timeouts are 16-bit values, so
3108 * the maximum timeout is 65.535 seconds.
3109 */
3110
3111 /* Convert timeout to milliseconds and round */
3112 ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000);
3113
3114 /* This is the recommended formula for class 2 devices
3115 * that start ERTM timers when packets are sent to the
3116 * controller.
3117 */
3118 ertm_to = 3 * ertm_to + 500;
3119
3120 if (ertm_to > 0xffff)
3121 ertm_to = 0xffff;
3122
3123 rfc->retrans_timeout = cpu_to_le16((u16) ertm_to);
3124 rfc->monitor_timeout = rfc->retrans_timeout;
3125 } else {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003126 rfc->retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
3127 rfc->monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
Mat Martineau36c86c82012-10-23 15:24:20 -07003128 }
3129}
3130
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003131static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
3132{
3133 if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW &&
Marcel Holtmann848566b2013-10-01 22:59:22 -07003134 __l2cap_ews_supported(chan->conn)) {
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003135 /* use extended control field */
3136 set_bit(FLAG_EXT_CTRL, &chan->flags);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003137 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
3138 } else {
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003139 chan->tx_win = min_t(u16, chan->tx_win,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003140 L2CAP_DEFAULT_TX_WINDOW);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003141 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
3142 }
Mat Martineauc20f8e32012-07-10 05:47:07 -07003143 chan->ack_win = chan->tx_win;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003144}
3145
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003146static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 struct l2cap_conf_req *req = data;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003149 struct l2cap_conf_rfc rfc = { .mode = chan->mode };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 void *ptr = req->data;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003151 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03003153 BT_DBG("chan %p", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003155 if (chan->num_conf_req || chan->num_conf_rsp)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003156 goto done;
3157
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003158 switch (chan->mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003159 case L2CAP_MODE_STREAMING:
3160 case L2CAP_MODE_ERTM:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003161 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003162 break;
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003163
Marcel Holtmann848566b2013-10-01 22:59:22 -07003164 if (__l2cap_efs_supported(chan->conn))
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003165 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3166
Gustavo F. Padovan2ba13ed2010-06-09 16:39:05 -03003167 /* fall through */
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003168 default:
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003169 chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003170 break;
3171 }
3172
3173done:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003174 if (chan->imtu != L2CAP_DEFAULT_MTU)
3175 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
Gustavo F. Padovan7990681c2011-01-24 16:01:43 -02003176
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003177 switch (chan->mode) {
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003178 case L2CAP_MODE_BASIC:
Marcel Holtmann6fea7ad2014-07-09 11:53:35 +02003179 if (disable_ertm)
3180 break;
3181
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003182 if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003183 !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003184 break;
3185
Gustavo F. Padovan62547752010-06-08 20:05:31 -03003186 rfc.mode = L2CAP_MODE_BASIC;
3187 rfc.txwin_size = 0;
3188 rfc.max_transmit = 0;
3189 rfc.retrans_timeout = 0;
3190 rfc.monitor_timeout = 0;
3191 rfc.max_pdu_size = 0;
3192
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003193 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003194 (unsigned long) &rfc);
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003195 break;
3196
3197 case L2CAP_MODE_ERTM:
3198 rfc.mode = L2CAP_MODE_ERTM;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003199 rfc.max_transmit = chan->max_tx;
Mat Martineau36c86c82012-10-23 15:24:20 -07003200
3201 __l2cap_set_ertm_timeouts(chan, &rfc);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003202
3203 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
Gustavo Padovan2d792812012-10-06 10:07:01 +01003204 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3205 L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003206 rfc.max_pdu_size = cpu_to_le16(size);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003207
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003208 l2cap_txwin_setup(chan);
3209
3210 rfc.txwin_size = min_t(u16, chan->tx_win,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003211 L2CAP_DEFAULT_TX_WINDOW);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003212
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003213 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003214 (unsigned long) &rfc);
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003215
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003216 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
3217 l2cap_add_opt_efs(&ptr, chan);
3218
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003219 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3220 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003221 chan->tx_win);
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003222
3223 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3224 if (chan->fcs == L2CAP_FCS_NONE ||
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003225 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003226 chan->fcs = L2CAP_FCS_NONE;
3227 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
3228 chan->fcs);
3229 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003230 break;
3231
3232 case L2CAP_MODE_STREAMING:
Mat Martineau273759e2012-05-17 20:53:53 -07003233 l2cap_txwin_setup(chan);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003234 rfc.mode = L2CAP_MODE_STREAMING;
3235 rfc.txwin_size = 0;
3236 rfc.max_transmit = 0;
3237 rfc.retrans_timeout = 0;
3238 rfc.monitor_timeout = 0;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003239
3240 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
Gustavo Padovan2d792812012-10-06 10:07:01 +01003241 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3242 L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003243 rfc.max_pdu_size = cpu_to_le16(size);
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003244
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003245 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003246 (unsigned long) &rfc);
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003247
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003248 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
3249 l2cap_add_opt_efs(&ptr, chan);
3250
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003251 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3252 if (chan->fcs == L2CAP_FCS_NONE ||
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003253 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003254 chan->fcs = L2CAP_FCS_NONE;
3255 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
3256 chan->fcs);
3257 }
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003258 break;
3259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003261 req->dcid = cpu_to_le16(chan->dcid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003262 req->flags = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
3264 return ptr - data;
3265}
3266
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003267static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003269 struct l2cap_conf_rsp *rsp = data;
3270 void *ptr = rsp->data;
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003271 void *req = chan->conf_req;
3272 int len = chan->conf_len;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003273 int type, hint, olen;
3274 unsigned long val;
Marcel Holtmann6464f352007-10-20 13:39:51 +02003275 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003276 struct l2cap_conf_efs efs;
3277 u8 remote_efs = 0;
Marcel Holtmann861d6882007-10-20 13:37:06 +02003278 u16 mtu = L2CAP_DEFAULT_MTU;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003279 u16 result = L2CAP_CONF_SUCCESS;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003280 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003282 BT_DBG("chan %p", chan);
Marcel Holtmann820ae1b2006-11-18 22:15:00 +01003283
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003284 while (len >= L2CAP_CONF_OPT_SIZE) {
3285 len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Gustavo F. Padovan589d2742009-04-20 01:31:07 -03003287 hint = type & L2CAP_CONF_HINT;
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -07003288 type &= L2CAP_CONF_MASK;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003289
3290 switch (type) {
3291 case L2CAP_CONF_MTU:
Marcel Holtmann861d6882007-10-20 13:37:06 +02003292 mtu = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003293 break;
3294
3295 case L2CAP_CONF_FLUSH_TO:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003296 chan->flush_to = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003297 break;
3298
3299 case L2CAP_CONF_QOS:
3300 break;
3301
Marcel Holtmann6464f352007-10-20 13:39:51 +02003302 case L2CAP_CONF_RFC:
3303 if (olen == sizeof(rfc))
3304 memcpy(&rfc, (void *) val, olen);
3305 break;
3306
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003307 case L2CAP_CONF_FCS:
3308 if (val == L2CAP_FCS_NONE)
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003309 set_bit(CONF_RECV_NO_FCS, &chan->conf_state);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003310 break;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003311
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003312 case L2CAP_CONF_EFS:
3313 remote_efs = 1;
3314 if (olen == sizeof(efs))
3315 memcpy(&efs, (void *) val, olen);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003316 break;
3317
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003318 case L2CAP_CONF_EWS:
Marcel Holtmann848566b2013-10-01 22:59:22 -07003319 if (!chan->conn->hs_enabled)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003320 return -ECONNREFUSED;
3321
3322 set_bit(FLAG_EXT_CTRL, &chan->flags);
3323 set_bit(CONF_EWS_RECV, &chan->conf_state);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003324 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003325 chan->remote_tx_win = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003326 break;
3327
3328 default:
3329 if (hint)
3330 break;
3331
3332 result = L2CAP_CONF_UNKNOWN;
3333 *((u8 *) ptr++) = type;
3334 break;
3335 }
3336 }
3337
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003338 if (chan->num_conf_rsp || chan->num_conf_req > 1)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003339 goto done;
3340
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003341 switch (chan->mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003342 case L2CAP_MODE_STREAMING:
3343 case L2CAP_MODE_ERTM:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003344 if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003345 chan->mode = l2cap_select_mode(rfc.mode,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003346 chan->conn->feat_mask);
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003347 break;
3348 }
3349
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003350 if (remote_efs) {
Marcel Holtmann848566b2013-10-01 22:59:22 -07003351 if (__l2cap_efs_supported(chan->conn))
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003352 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3353 else
3354 return -ECONNREFUSED;
3355 }
3356
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003357 if (chan->mode != rfc.mode)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003358 return -ECONNREFUSED;
Gustavo F. Padovan742e5192010-06-08 19:09:48 -03003359
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003360 break;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003361 }
3362
3363done:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003364 if (chan->mode != rfc.mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003365 result = L2CAP_CONF_UNACCEPT;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003366 rfc.mode = chan->mode;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003367
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003368 if (chan->num_conf_rsp == 1)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003369 return -ECONNREFUSED;
3370
Gustavo Padovan2d792812012-10-06 10:07:01 +01003371 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3372 (unsigned long) &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003373 }
3374
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003375 if (result == L2CAP_CONF_SUCCESS) {
3376 /* Configure output options and let the other side know
3377 * which ones we don't like. */
3378
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003379 if (mtu < L2CAP_DEFAULT_MIN_MTU)
3380 result = L2CAP_CONF_UNACCEPT;
3381 else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003382 chan->omtu = mtu;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003383 set_bit(CONF_MTU_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003384 }
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003385 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003386
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003387 if (remote_efs) {
3388 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003389 efs.stype != L2CAP_SERV_NOTRAFIC &&
3390 efs.stype != chan->local_stype) {
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003391
3392 result = L2CAP_CONF_UNACCEPT;
3393
3394 if (chan->num_conf_req >= 1)
3395 return -ECONNREFUSED;
3396
3397 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003398 sizeof(efs),
3399 (unsigned long) &efs);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003400 } else {
Gustavo F. Padovan3e6b3b92011-11-01 14:06:23 -02003401 /* Send PENDING Conf Rsp */
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003402 result = L2CAP_CONF_PENDING;
3403 set_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003404 }
3405 }
3406
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003407 switch (rfc.mode) {
3408 case L2CAP_MODE_BASIC:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003409 chan->fcs = L2CAP_FCS_NONE;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003410 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003411 break;
3412
3413 case L2CAP_MODE_ERTM:
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003414 if (!test_bit(CONF_EWS_RECV, &chan->conf_state))
3415 chan->remote_tx_win = rfc.txwin_size;
3416 else
3417 rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW;
3418
Gustavo F. Padovan2c03a7a2011-03-25 20:15:28 -03003419 chan->remote_max_tx = rfc.max_transmit;
Mat Martineau86b1b262010-08-05 15:54:22 -07003420
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003421 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003422 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3423 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003424 rfc.max_pdu_size = cpu_to_le16(size);
3425 chan->remote_mps = size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003426
Mat Martineau36c86c82012-10-23 15:24:20 -07003427 __l2cap_set_ertm_timeouts(chan, &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003428
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003429 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003430
3431 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003432 sizeof(rfc), (unsigned long) &rfc);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003433
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003434 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3435 chan->remote_id = efs.id;
3436 chan->remote_stype = efs.stype;
3437 chan->remote_msdu = le16_to_cpu(efs.msdu);
3438 chan->remote_flush_to =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003439 le32_to_cpu(efs.flush_to);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003440 chan->remote_acc_lat =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003441 le32_to_cpu(efs.acc_lat);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003442 chan->remote_sdu_itime =
3443 le32_to_cpu(efs.sdu_itime);
3444 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003445 sizeof(efs),
3446 (unsigned long) &efs);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003447 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003448 break;
3449
3450 case L2CAP_MODE_STREAMING:
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003451 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003452 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3453 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003454 rfc.max_pdu_size = cpu_to_le16(size);
3455 chan->remote_mps = size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003456
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003457 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003458
Gustavo Padovan2d792812012-10-06 10:07:01 +01003459 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3460 (unsigned long) &rfc);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003461
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003462 break;
3463
3464 default:
Marcel Holtmann6464f352007-10-20 13:39:51 +02003465 result = L2CAP_CONF_UNACCEPT;
3466
3467 memset(&rfc, 0, sizeof(rfc));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003468 rfc.mode = chan->mode;
Marcel Holtmann6464f352007-10-20 13:39:51 +02003469 }
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003470
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003471 if (result == L2CAP_CONF_SUCCESS)
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003472 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003473 }
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003474 rsp->scid = cpu_to_le16(chan->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003475 rsp->result = cpu_to_le16(result);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003476 rsp->flags = cpu_to_le16(0);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003477
3478 return ptr - data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479}
3480
Gustavo Padovan2d792812012-10-06 10:07:01 +01003481static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
3482 void *data, u16 *result)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003483{
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003484 struct l2cap_conf_req *req = data;
3485 void *ptr = req->data;
3486 int type, olen;
3487 unsigned long val;
Mat Martineau36e999a2011-12-08 17:23:21 -08003488 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003489 struct l2cap_conf_efs efs;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003490
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003491 BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003492
3493 while (len >= L2CAP_CONF_OPT_SIZE) {
3494 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3495
3496 switch (type) {
3497 case L2CAP_CONF_MTU:
3498 if (val < L2CAP_DEFAULT_MIN_MTU) {
3499 *result = L2CAP_CONF_UNACCEPT;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003500 chan->imtu = L2CAP_DEFAULT_MIN_MTU;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003501 } else
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003502 chan->imtu = val;
3503 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003504 break;
3505
3506 case L2CAP_CONF_FLUSH_TO:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003507 chan->flush_to = val;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003508 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003509 2, chan->flush_to);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003510 break;
3511
3512 case L2CAP_CONF_RFC:
3513 if (olen == sizeof(rfc))
3514 memcpy(&rfc, (void *)val, olen);
3515
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003516 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003517 rfc.mode != chan->mode)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003518 return -ECONNREFUSED;
3519
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003520 chan->fcs = 0;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003521
3522 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003523 sizeof(rfc), (unsigned long) &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003524 break;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003525
3526 case L2CAP_CONF_EWS:
Mat Martineauc20f8e32012-07-10 05:47:07 -07003527 chan->ack_win = min_t(u16, val, chan->ack_win);
Gustavo F. Padovan3e6b3b92011-11-01 14:06:23 -02003528 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
Mat Martineauc20f8e32012-07-10 05:47:07 -07003529 chan->tx_win);
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003530 break;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003531
3532 case L2CAP_CONF_EFS:
3533 if (olen == sizeof(efs))
3534 memcpy(&efs, (void *)val, olen);
3535
3536 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003537 efs.stype != L2CAP_SERV_NOTRAFIC &&
3538 efs.stype != chan->local_stype)
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003539 return -ECONNREFUSED;
3540
Gustavo Padovan2d792812012-10-06 10:07:01 +01003541 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs),
3542 (unsigned long) &efs);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003543 break;
Andrei Emeltchenkocbabee72012-11-29 17:46:07 +02003544
3545 case L2CAP_CONF_FCS:
3546 if (*result == L2CAP_CONF_PENDING)
3547 if (val == L2CAP_FCS_NONE)
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003548 set_bit(CONF_RECV_NO_FCS,
Andrei Emeltchenkocbabee72012-11-29 17:46:07 +02003549 &chan->conf_state);
3550 break;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003551 }
3552 }
3553
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003554 if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
Gustavo F. Padovan6c2ea7a2010-06-08 20:08:49 -03003555 return -ECONNREFUSED;
3556
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003557 chan->mode = rfc.mode;
Gustavo F. Padovan6c2ea7a2010-06-08 20:08:49 -03003558
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003559 if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003560 switch (rfc.mode) {
3561 case L2CAP_MODE_ERTM:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003562 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3563 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
3564 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Mat Martineauc20f8e32012-07-10 05:47:07 -07003565 if (!test_bit(FLAG_EXT_CTRL, &chan->flags))
3566 chan->ack_win = min_t(u16, chan->ack_win,
3567 rfc.txwin_size);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003568
3569 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3570 chan->local_msdu = le16_to_cpu(efs.msdu);
3571 chan->local_sdu_itime =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003572 le32_to_cpu(efs.sdu_itime);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003573 chan->local_acc_lat = le32_to_cpu(efs.acc_lat);
3574 chan->local_flush_to =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003575 le32_to_cpu(efs.flush_to);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003576 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003577 break;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003578
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003579 case L2CAP_MODE_STREAMING:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003580 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003581 }
3582 }
3583
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003584 req->dcid = cpu_to_le16(chan->dcid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003585 req->flags = cpu_to_le16(0);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003586
3587 return ptr - data;
3588}
3589
Gustavo Padovan2d792812012-10-06 10:07:01 +01003590static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data,
3591 u16 result, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592{
3593 struct l2cap_conf_rsp *rsp = data;
3594 void *ptr = rsp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003596 BT_DBG("chan %p", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003598 rsp->scid = cpu_to_le16(chan->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003599 rsp->result = cpu_to_le16(result);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07003600 rsp->flags = cpu_to_le16(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
3602 return ptr - data;
3603}
3604
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03003605void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan)
3606{
3607 struct l2cap_le_conn_rsp rsp;
3608 struct l2cap_conn *conn = chan->conn;
3609
3610 BT_DBG("chan %p", chan);
3611
3612 rsp.dcid = cpu_to_le16(chan->scid);
3613 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02003614 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03003615 rsp.credits = cpu_to_le16(chan->rx_credits);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003616 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03003617
3618 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
3619 &rsp);
3620}
3621
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003622void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003623{
3624 struct l2cap_conn_rsp rsp;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003625 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003626 u8 buf[128];
Andrei Emeltchenko439f34a2012-10-31 15:46:28 +02003627 u8 rsp_code;
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003628
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003629 rsp.scid = cpu_to_le16(chan->dcid);
3630 rsp.dcid = cpu_to_le16(chan->scid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003631 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
3632 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Andrei Emeltchenko439f34a2012-10-31 15:46:28 +02003633
3634 if (chan->hs_hcon)
3635 rsp_code = L2CAP_CREATE_CHAN_RSP;
3636 else
3637 rsp_code = L2CAP_CONN_RSP;
3638
3639 BT_DBG("chan %p rsp_code %u", chan, rsp_code);
3640
3641 l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp);
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003642
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003643 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003644 return;
3645
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003646 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003647 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003648 chan->num_conf_req++;
3649}
3650
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003651static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003652{
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003653 int type, olen;
3654 unsigned long val;
Mat Martineauc20f8e32012-07-10 05:47:07 -07003655 /* Use sane default values in case a misbehaving remote device
3656 * did not send an RFC or extended window size option.
3657 */
3658 u16 txwin_ext = chan->ack_win;
3659 struct l2cap_conf_rfc rfc = {
3660 .mode = chan->mode,
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003661 .retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO),
3662 .monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO),
Mat Martineauc20f8e32012-07-10 05:47:07 -07003663 .max_pdu_size = cpu_to_le16(chan->imtu),
3664 .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW),
3665 };
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003666
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003667 BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003668
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003669 if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003670 return;
3671
3672 while (len >= L2CAP_CONF_OPT_SIZE) {
3673 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3674
Mat Martineauc20f8e32012-07-10 05:47:07 -07003675 switch (type) {
3676 case L2CAP_CONF_RFC:
3677 if (olen == sizeof(rfc))
3678 memcpy(&rfc, (void *)val, olen);
Szymon Janc8f321f82012-06-08 11:33:33 +02003679 break;
Mat Martineauc20f8e32012-07-10 05:47:07 -07003680 case L2CAP_CONF_EWS:
3681 txwin_ext = val;
3682 break;
3683 }
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003684 }
3685
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003686 switch (rfc.mode) {
3687 case L2CAP_MODE_ERTM:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003688 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3689 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
Mat Martineauc20f8e32012-07-10 05:47:07 -07003690 chan->mps = le16_to_cpu(rfc.max_pdu_size);
3691 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3692 chan->ack_win = min_t(u16, chan->ack_win, txwin_ext);
3693 else
3694 chan->ack_win = min_t(u16, chan->ack_win,
3695 rfc.txwin_size);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003696 break;
3697 case L2CAP_MODE_STREAMING:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003698 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003699 }
3700}
3701
Gustavo Padovan2d792812012-10-06 10:07:01 +01003702static inline int l2cap_command_rej(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003703 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3704 u8 *data)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003705{
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003706 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003707
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003708 if (cmd_len < sizeof(*rej))
3709 return -EPROTO;
3710
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003711 if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003712 return 0;
3713
3714 if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003715 cmd->ident == conn->info_ident) {
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02003716 cancel_delayed_work(&conn->info_timer);
Marcel Holtmann984947d2009-02-06 23:35:19 +01003717
3718 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01003719 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01003720
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003721 l2cap_conn_start(conn);
3722 }
3723
3724 return 0;
3725}
3726
Mat Martineau17009152012-10-23 15:24:07 -07003727static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3728 struct l2cap_cmd_hdr *cmd,
3729 u8 *data, u8 rsp_code, u8 amp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
3732 struct l2cap_conn_rsp rsp;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003733 struct l2cap_chan *chan = NULL, *pchan;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003734 int result, status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
3736 u16 dcid = 0, scid = __le16_to_cpu(req->scid);
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003737 __le16 psm = req->psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Andrei Emeltchenko097db762012-03-09 14:16:17 +02003739 BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
3741 /* Check if we have socket listening on psm */
Marcel Holtmann6f59b902013-10-13 05:24:01 -07003742 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
Johan Hedbergbf20fd42013-05-14 13:23:13 +03003743 &conn->hcon->dst, ACL_LINK);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003744 if (!pchan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 result = L2CAP_CR_BAD_PSM;
3746 goto sendresp;
3747 }
3748
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003749 mutex_lock(&conn->chan_lock);
Gustavo Padovan8ffb9292013-10-21 14:21:41 -02003750 l2cap_chan_lock(pchan);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00003751
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003752 /* Check if the ACL is secure enough (if not SDP) */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003753 if (psm != cpu_to_le16(L2CAP_PSM_SDP) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003754 !hci_conn_check_link_mode(conn->hcon)) {
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02003755 conn->disc_reason = HCI_ERROR_AUTH_FAILURE;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003756 result = L2CAP_CR_SEC_BLOCK;
3757 goto response;
3758 }
3759
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 result = L2CAP_CR_NO_MEM;
3761
Gustavo Padovan2dfa1002012-05-27 22:27:58 -03003762 /* Check if we already have channel with that dcid */
3763 if (__l2cap_get_chan_by_dcid(conn, scid))
3764 goto response;
3765
Gustavo Padovan80b98022012-05-27 22:27:51 -03003766 chan = pchan->ops->new_connection(pchan);
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03003767 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 goto response;
3769
Syam Sidhardhan330b6c12013-08-06 01:59:12 +09003770 /* For certain devices (ex: HID mouse), support for authentication,
3771 * pairing and bonding is optional. For such devices, inorder to avoid
3772 * the ACL alive for too long after L2CAP disconnection, reset the ACL
3773 * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect.
3774 */
3775 conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
3776
Marcel Holtmann7eafc592013-10-13 08:12:47 -07003777 bacpy(&chan->src, &conn->hcon->src);
3778 bacpy(&chan->dst, &conn->hcon->dst);
Marcel Holtmann4f1654e2013-10-13 08:50:41 -07003779 chan->src_type = bdaddr_type(conn->hcon, conn->hcon->src_type);
3780 chan->dst_type = bdaddr_type(conn->hcon, conn->hcon->dst_type);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003781 chan->psm = psm;
3782 chan->dcid = scid;
Mat Martineau17009152012-10-23 15:24:07 -07003783 chan->local_amp_id = amp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003785 __l2cap_chan_add(conn, chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003786
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003787 dcid = chan->scid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
Gustavo Padovan8d836d72013-10-15 19:24:47 -03003789 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03003791 chan->ident = cmd->ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
Marcel Holtmann984947d2009-02-06 23:35:19 +01003793 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
Johan Hedberge7cafc42014-07-17 15:35:38 +03003794 if (l2cap_chan_check_security(chan, false)) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07003795 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003796 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003797 result = L2CAP_CR_PEND;
3798 status = L2CAP_CS_AUTHOR_PEND;
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08003799 chan->ops->defer(chan);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003800 } else {
Mat Martineau17009152012-10-23 15:24:07 -07003801 /* Force pending result for AMP controllers.
3802 * The connection will succeed after the
3803 * physical link is up.
3804 */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003805 if (amp_id == AMP_ID_BREDR) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003806 l2cap_state_change(chan, BT_CONFIG);
Mat Martineau17009152012-10-23 15:24:07 -07003807 result = L2CAP_CR_SUCCESS;
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003808 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003809 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003810 result = L2CAP_CR_PEND;
Mat Martineau17009152012-10-23 15:24:07 -07003811 }
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003812 status = L2CAP_CS_NO_INFO;
3813 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003814 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003815 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003816 result = L2CAP_CR_PEND;
3817 status = L2CAP_CS_AUTHEN_PEND;
3818 }
3819 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003820 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003821 result = L2CAP_CR_PEND;
3822 status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 }
3824
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825response:
Gustavo Padovan8ffb9292013-10-21 14:21:41 -02003826 l2cap_chan_unlock(pchan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003827 mutex_unlock(&conn->chan_lock);
Johan Hedberga24cce12014-08-07 22:56:42 +03003828 l2cap_chan_put(pchan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
3830sendresp:
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07003831 rsp.scid = cpu_to_le16(scid);
3832 rsp.dcid = cpu_to_le16(dcid);
3833 rsp.result = cpu_to_le16(result);
3834 rsp.status = cpu_to_le16(status);
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003835 l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003836
3837 if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
3838 struct l2cap_info_req info;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003839 info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003840
3841 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
3842 conn->info_ident = l2cap_get_ident(conn);
3843
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08003844 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003845
Gustavo Padovan2d792812012-10-06 10:07:01 +01003846 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
3847 sizeof(info), &info);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003848 }
3849
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003850 if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003851 result == L2CAP_CR_SUCCESS) {
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003852 u8 buf[128];
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003853 set_bit(CONF_REQ_SENT, &chan->conf_state);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003854 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003855 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003856 chan->num_conf_req++;
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003857 }
Mat Martineau17009152012-10-23 15:24:07 -07003858
3859 return chan;
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003860}
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003861
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003862static int l2cap_connect_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003863 struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003864{
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303865 struct hci_dev *hdev = conn->hcon->hdev;
3866 struct hci_conn *hcon = conn->hcon;
3867
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003868 if (cmd_len < sizeof(struct l2cap_conn_req))
3869 return -EPROTO;
3870
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303871 hci_dev_lock(hdev);
3872 if (test_bit(HCI_MGMT, &hdev->dev_flags) &&
3873 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags))
3874 mgmt_device_connected(hdev, &hcon->dst, hcon->type,
3875 hcon->dst_type, 0, NULL, 0,
3876 hcon->dev_class);
3877 hci_dev_unlock(hdev);
3878
Gustavo Padovan300229f2012-10-12 19:40:40 +08003879 l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 return 0;
3881}
3882
Mat Martineau5909cf32012-10-23 15:24:08 -07003883static int l2cap_connect_create_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003884 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3885 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886{
3887 struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
3888 u16 scid, dcid, result, status;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003889 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 u8 req[128];
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003891 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003893 if (cmd_len < sizeof(*rsp))
3894 return -EPROTO;
3895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 scid = __le16_to_cpu(rsp->scid);
3897 dcid = __le16_to_cpu(rsp->dcid);
3898 result = __le16_to_cpu(rsp->result);
3899 status = __le16_to_cpu(rsp->status);
3900
Andrei Emeltchenko1b009c92012-02-21 12:54:54 +02003901 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 +01003902 dcid, scid, result, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003904 mutex_lock(&conn->chan_lock);
3905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 if (scid) {
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003907 chan = __l2cap_get_chan_by_scid(conn, scid);
3908 if (!chan) {
Johan Hedberg21870b52013-09-16 13:05:14 +03003909 err = -EBADSLT;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003910 goto unlock;
3911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 } else {
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003913 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
3914 if (!chan) {
Johan Hedberg21870b52013-09-16 13:05:14 +03003915 err = -EBADSLT;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003916 goto unlock;
3917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 }
3919
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003920 err = 0;
3921
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003922 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003923
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 switch (result) {
3925 case L2CAP_CR_SUCCESS:
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03003926 l2cap_state_change(chan, BT_CONFIG);
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03003927 chan->ident = 0;
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003928 chan->dcid = dcid;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003929 clear_bit(CONF_CONNECT_PEND, &chan->conf_state);
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01003930
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003931 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003932 break;
3933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003935 l2cap_build_conf_req(chan, req), req);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003936 chan->num_conf_req++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 break;
3938
3939 case L2CAP_CR_PEND:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003940 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 break;
3942
3943 default:
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003944 l2cap_chan_del(chan, ECONNREFUSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 break;
3946 }
3947
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003948 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003949
3950unlock:
3951 mutex_unlock(&conn->chan_lock);
3952
3953 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954}
3955
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003956static inline void set_default_fcs(struct l2cap_chan *chan)
Mat Martineau8c462b62010-08-24 15:35:42 -07003957{
3958 /* FCS is enabled only in ERTM or streaming mode, if one or both
3959 * sides request it.
3960 */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003961 if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003962 chan->fcs = L2CAP_FCS_NONE;
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003963 else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state))
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003964 chan->fcs = L2CAP_FCS_CRC16;
Mat Martineau8c462b62010-08-24 15:35:42 -07003965}
3966
Andrei Emeltchenko29d8a592012-09-21 12:30:05 +03003967static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data,
3968 u8 ident, u16 flags)
3969{
3970 struct l2cap_conn *conn = chan->conn;
3971
3972 BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident,
3973 flags);
3974
3975 clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
3976 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
3977
3978 l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP,
3979 l2cap_build_conf_rsp(chan, data,
3980 L2CAP_CONF_SUCCESS, flags), data);
3981}
3982
Johan Hedberg662d6522013-10-16 11:20:47 +03003983static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident,
3984 u16 scid, u16 dcid)
3985{
3986 struct l2cap_cmd_rej_cid rej;
3987
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003988 rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID);
Johan Hedberg662d6522013-10-16 11:20:47 +03003989 rej.scid = __cpu_to_le16(scid);
3990 rej.dcid = __cpu_to_le16(dcid);
3991
3992 l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
3993}
3994
Gustavo Padovan2d792812012-10-06 10:07:01 +01003995static inline int l2cap_config_req(struct l2cap_conn *conn,
3996 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3997 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998{
3999 struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
4000 u16 dcid, flags;
4001 u8 rsp[64];
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004002 struct l2cap_chan *chan;
Mat Martineau3c588192012-04-11 10:48:42 -07004003 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004005 if (cmd_len < sizeof(*req))
4006 return -EPROTO;
4007
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 dcid = __le16_to_cpu(req->dcid);
4009 flags = __le16_to_cpu(req->flags);
4010
4011 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
4012
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03004013 chan = l2cap_get_chan_by_scid(conn, dcid);
Johan Hedberg662d6522013-10-16 11:20:47 +03004014 if (!chan) {
4015 cmd_reject_invalid_cid(conn, cmd->ident, dcid, 0);
4016 return 0;
4017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
David S. Miller033b1142011-07-21 13:38:42 -07004019 if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) {
Johan Hedberg662d6522013-10-16 11:20:47 +03004020 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4021 chan->dcid);
Marcel Holtmann354f60a2006-11-18 22:15:20 +01004022 goto unlock;
Gustavo F. Padovandf6bd742010-06-14 02:26:15 -03004023 }
Marcel Holtmann354f60a2006-11-18 22:15:20 +01004024
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004025 /* Reject if config buffer is too small. */
Al Viro88219a02007-07-29 00:17:25 -07004026 len = cmd_len - sizeof(*req);
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004027 if (chan->conf_len + len > sizeof(chan->conf_req)) {
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004028 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004029 l2cap_build_conf_rsp(chan, rsp,
4030 L2CAP_CONF_REJECT, flags), rsp);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004031 goto unlock;
4032 }
4033
4034 /* Store config. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004035 memcpy(chan->conf_req + chan->conf_len, req->data, len);
4036 chan->conf_len += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03004038 if (flags & L2CAP_CONF_FLAG_CONTINUATION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 /* Incomplete config. Send empty response. */
4040 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004041 l2cap_build_conf_rsp(chan, rsp,
4042 L2CAP_CONF_SUCCESS, flags), rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 goto unlock;
4044 }
4045
4046 /* Complete config. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004047 len = l2cap_parse_conf_req(chan, rsp);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004048 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004049 l2cap_send_disconn_req(chan, ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 goto unlock;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Mat Martineau1500109b2012-10-23 15:24:15 -07004053 chan->ident = cmd->ident;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004054 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004055 chan->num_conf_rsp++;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004056
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004057 /* Reset config buffer. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004058 chan->conf_len = 0;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004059
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004060 if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state))
Marcel Holtmann876d9482007-10-20 13:35:42 +02004061 goto unlock;
4062
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004063 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004064 set_default_fcs(chan);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004065
Mat Martineau105bdf92012-04-27 16:50:48 -07004066 if (chan->mode == L2CAP_MODE_ERTM ||
4067 chan->mode == L2CAP_MODE_STREAMING)
Mat Martineau3c588192012-04-11 10:48:42 -07004068 err = l2cap_ertm_init(chan);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03004069
Mat Martineau3c588192012-04-11 10:48:42 -07004070 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004071 l2cap_send_disconn_req(chan, -err);
Mat Martineau3c588192012-04-11 10:48:42 -07004072 else
4073 l2cap_chan_ready(chan);
4074
Marcel Holtmann876d9482007-10-20 13:35:42 +02004075 goto unlock;
4076 }
4077
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004078 if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +02004079 u8 buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004081 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004082 chan->num_conf_req++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 }
4084
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004085 /* Got Conf Rsp PENDING from remote side and asume we sent
4086 Conf Rsp PENDING in the code above */
4087 if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) &&
Andrei Emeltchenko29d8a592012-09-21 12:30:05 +03004088 test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004089
4090 /* check compatibility */
4091
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004092 /* Send rsp for BR/EDR channel */
Andrei Emeltchenkof351bc72012-10-31 15:46:35 +02004093 if (!chan->hs_hcon)
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004094 l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags);
4095 else
4096 chan->ident = cmd->ident;
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004097 }
4098
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099unlock:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004100 l2cap_chan_unlock(chan);
Mat Martineau3c588192012-04-11 10:48:42 -07004101 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102}
4103
Gustavo Padovan2d792812012-10-06 10:07:01 +01004104static inline int l2cap_config_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004105 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4106 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107{
4108 struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
4109 u16 scid, flags, result;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004110 struct l2cap_chan *chan;
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004111 int len = cmd_len - sizeof(*rsp);
Mat Martineau3c588192012-04-11 10:48:42 -07004112 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004114 if (cmd_len < sizeof(*rsp))
4115 return -EPROTO;
4116
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 scid = __le16_to_cpu(rsp->scid);
4118 flags = __le16_to_cpu(rsp->flags);
4119 result = __le16_to_cpu(rsp->result);
4120
Andrei Emeltchenko61386cb2012-03-12 12:13:07 +02004121 BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags,
4122 result, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03004124 chan = l2cap_get_chan_by_scid(conn, scid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004125 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 return 0;
4127
4128 switch (result) {
4129 case L2CAP_CONF_SUCCESS:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004130 l2cap_conf_rfc_get(chan, rsp->data, len);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004131 clear_bit(CONF_REM_CONF_PEND, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 break;
4133
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004134 case L2CAP_CONF_PENDING:
4135 set_bit(CONF_REM_CONF_PEND, &chan->conf_state);
4136
4137 if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
4138 char buf[64];
4139
4140 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004141 buf, &result);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004142 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004143 l2cap_send_disconn_req(chan, ECONNRESET);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004144 goto done;
4145 }
4146
Andrei Emeltchenkof351bc72012-10-31 15:46:35 +02004147 if (!chan->hs_hcon) {
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004148 l2cap_send_efs_conf_rsp(chan, buf, cmd->ident,
4149 0);
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +02004150 } else {
4151 if (l2cap_check_efs(chan)) {
4152 amp_create_logical_link(chan);
4153 chan->ident = cmd->ident;
4154 }
4155 }
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004156 }
4157 goto done;
4158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 case L2CAP_CONF_UNACCEPT:
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004160 if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004161 char req[64];
4162
Andrei Emeltchenkoc2c77ec2010-03-19 10:26:28 +02004163 if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004164 l2cap_send_disconn_req(chan, ECONNRESET);
Andrei Emeltchenkoc2c77ec2010-03-19 10:26:28 +02004165 goto done;
4166 }
4167
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004168 /* throw out any old stored conf requests */
4169 result = L2CAP_CONF_SUCCESS;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03004170 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004171 req, &result);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004172 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004173 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004174 goto done;
4175 }
4176
4177 l2cap_send_cmd(conn, l2cap_get_ident(conn),
Gustavo Padovan2d792812012-10-06 10:07:01 +01004178 L2CAP_CONF_REQ, len, req);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004179 chan->num_conf_req++;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004180 if (result != L2CAP_CONF_SUCCESS)
4181 goto done;
4182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 }
4184
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09004185 default:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004186 l2cap_chan_set_err(chan, ECONNRESET);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +02004187
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08004188 __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004189 l2cap_send_disconn_req(chan, ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 goto done;
4191 }
4192
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03004193 if (flags & L2CAP_CONF_FLAG_CONTINUATION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 goto done;
4195
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004196 set_bit(CONF_INPUT_DONE, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004198 if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) {
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004199 set_default_fcs(chan);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004200
Mat Martineau105bdf92012-04-27 16:50:48 -07004201 if (chan->mode == L2CAP_MODE_ERTM ||
4202 chan->mode == L2CAP_MODE_STREAMING)
Mat Martineau3c588192012-04-11 10:48:42 -07004203 err = l2cap_ertm_init(chan);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03004204
Mat Martineau3c588192012-04-11 10:48:42 -07004205 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004206 l2cap_send_disconn_req(chan, -err);
Mat Martineau3c588192012-04-11 10:48:42 -07004207 else
4208 l2cap_chan_ready(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 }
4210
4211done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004212 l2cap_chan_unlock(chan);
Mat Martineau3c588192012-04-11 10:48:42 -07004213 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214}
4215
Gustavo Padovan2d792812012-10-06 10:07:01 +01004216static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004217 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4218 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219{
4220 struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
4221 struct l2cap_disconn_rsp rsp;
4222 u16 dcid, scid;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004223 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004225 if (cmd_len != sizeof(*req))
4226 return -EPROTO;
4227
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 scid = __le16_to_cpu(req->scid);
4229 dcid = __le16_to_cpu(req->dcid);
4230
4231 BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
4232
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004233 mutex_lock(&conn->chan_lock);
4234
4235 chan = __l2cap_get_chan_by_scid(conn, dcid);
4236 if (!chan) {
4237 mutex_unlock(&conn->chan_lock);
Johan Hedberg662d6522013-10-16 11:20:47 +03004238 cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid);
4239 return 0;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004242 l2cap_chan_lock(chan);
4243
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03004244 rsp.dcid = cpu_to_le16(chan->scid);
4245 rsp.scid = cpu_to_le16(chan->dcid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
4247
Gustavo Padovan5ec1bbe2013-10-15 19:24:48 -03004248 chan->ops->set_shutdown(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Mat Martineau61d6ef32012-04-27 16:50:50 -07004250 l2cap_chan_hold(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004251 l2cap_chan_del(chan, ECONNRESET);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004252
4253 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Gustavo Padovan80b98022012-05-27 22:27:51 -03004255 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07004256 l2cap_chan_put(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004257
4258 mutex_unlock(&conn->chan_lock);
4259
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 return 0;
4261}
4262
Gustavo Padovan2d792812012-10-06 10:07:01 +01004263static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004264 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4265 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266{
4267 struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
4268 u16 dcid, scid;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004269 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004271 if (cmd_len != sizeof(*rsp))
4272 return -EPROTO;
4273
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 scid = __le16_to_cpu(rsp->scid);
4275 dcid = __le16_to_cpu(rsp->dcid);
4276
4277 BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
4278
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004279 mutex_lock(&conn->chan_lock);
4280
4281 chan = __l2cap_get_chan_by_scid(conn, scid);
4282 if (!chan) {
4283 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 return 0;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004287 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004288
Mat Martineau61d6ef32012-04-27 16:50:50 -07004289 l2cap_chan_hold(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004290 l2cap_chan_del(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004291
4292 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
Gustavo Padovan80b98022012-05-27 22:27:51 -03004294 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07004295 l2cap_chan_put(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004296
4297 mutex_unlock(&conn->chan_lock);
4298
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 return 0;
4300}
4301
Gustavo Padovan2d792812012-10-06 10:07:01 +01004302static inline int l2cap_information_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004303 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4304 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
4306 struct l2cap_info_req *req = (struct l2cap_info_req *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 u16 type;
4308
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004309 if (cmd_len != sizeof(*req))
4310 return -EPROTO;
4311
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 type = __le16_to_cpu(req->type);
4313
4314 BT_DBG("type 0x%4.4x", type);
4315
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004316 if (type == L2CAP_IT_FEAT_MASK) {
4317 u8 buf[8];
Marcel Holtmann44dd46d2009-05-02 19:09:01 -07004318 u32 feat_mask = l2cap_feat_mask;
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004319 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004320 rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
4321 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03004322 if (!disable_ertm)
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004323 feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
Gustavo Padovan2d792812012-10-06 10:07:01 +01004324 | L2CAP_FEAT_FCS;
Marcel Holtmann848566b2013-10-01 22:59:22 -07004325 if (conn->hs_enabled)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03004326 feat_mask |= L2CAP_FEAT_EXT_FLOW
Gustavo Padovan2d792812012-10-06 10:07:01 +01004327 | L2CAP_FEAT_EXT_WINDOW;
Andrei Emeltchenkoa5fd6f32011-09-16 16:26:32 +03004328
Gustavo F. Padovan1b7bf4e2009-08-24 00:45:20 -03004329 put_unaligned_le32(feat_mask, rsp->data);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004330 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4331 buf);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004332 } else if (type == L2CAP_IT_FIXED_CHAN) {
4333 u8 buf[12];
4334 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
Mat Martineau50a147c2011-11-02 16:18:34 -07004335
Marcel Holtmann848566b2013-10-01 22:59:22 -07004336 if (conn->hs_enabled)
Mat Martineau50a147c2011-11-02 16:18:34 -07004337 l2cap_fixed_chan[0] |= L2CAP_FC_A2MP;
4338 else
4339 l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP;
4340
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004341 rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
4342 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
Andrei Emeltchenkoc6337ea2011-10-20 17:02:44 +03004343 memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan));
Gustavo Padovan2d792812012-10-06 10:07:01 +01004344 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4345 buf);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004346 } else {
4347 struct l2cap_info_rsp rsp;
4348 rsp.type = cpu_to_le16(type);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004349 rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004350 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp),
4351 &rsp);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
4354 return 0;
4355}
4356
Gustavo Padovan2d792812012-10-06 10:07:01 +01004357static inline int l2cap_information_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004358 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4359 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360{
4361 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
4362 u16 type, result;
4363
Jaganath Kanakkassery3f6fa3d2013-06-21 19:55:11 +05304364 if (cmd_len < sizeof(*rsp))
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004365 return -EPROTO;
4366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 type = __le16_to_cpu(rsp->type);
4368 result = __le16_to_cpu(rsp->result);
4369
4370 BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
4371
Andrei Emeltchenkoe90165b2011-03-25 11:31:41 +02004372 /* L2CAP Info req/rsp are unbound to channels, add extra checks */
4373 if (cmd->ident != conn->info_ident ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01004374 conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
Andrei Emeltchenkoe90165b2011-03-25 11:31:41 +02004375 return 0;
4376
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02004377 cancel_delayed_work(&conn->info_timer);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004378
Ville Tervoadb08ed2010-08-04 09:43:33 +03004379 if (result != L2CAP_IR_SUCCESS) {
4380 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4381 conn->info_ident = 0;
4382
4383 l2cap_conn_start(conn);
4384
4385 return 0;
4386 }
4387
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004388 switch (type) {
4389 case L2CAP_IT_FEAT_MASK:
Harvey Harrison83985312008-05-02 16:25:46 -07004390 conn->feat_mask = get_unaligned_le32(rsp->data);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004391
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -07004392 if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004393 struct l2cap_info_req req;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004394 req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004395
4396 conn->info_ident = l2cap_get_ident(conn);
4397
4398 l2cap_send_cmd(conn, conn->info_ident,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004399 L2CAP_INFO_REQ, sizeof(req), &req);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004400 } else {
4401 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4402 conn->info_ident = 0;
4403
4404 l2cap_conn_start(conn);
4405 }
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004406 break;
4407
4408 case L2CAP_IT_FIXED_CHAN:
4409 conn->fixed_chan_mask = rsp->data[0];
Marcel Holtmann984947d2009-02-06 23:35:19 +01004410 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004411 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01004412
4413 l2cap_conn_start(conn);
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004414 break;
Marcel Holtmann984947d2009-02-06 23:35:19 +01004415 }
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004416
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 return 0;
4418}
4419
Mat Martineau17009152012-10-23 15:24:07 -07004420static int l2cap_create_channel_req(struct l2cap_conn *conn,
4421 struct l2cap_cmd_hdr *cmd,
4422 u16 cmd_len, void *data)
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004423{
4424 struct l2cap_create_chan_req *req = data;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004425 struct l2cap_create_chan_rsp rsp;
Mat Martineau17009152012-10-23 15:24:07 -07004426 struct l2cap_chan *chan;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004427 struct hci_dev *hdev;
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004428 u16 psm, scid;
4429
4430 if (cmd_len != sizeof(*req))
4431 return -EPROTO;
4432
Marcel Holtmann848566b2013-10-01 22:59:22 -07004433 if (!conn->hs_enabled)
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004434 return -EINVAL;
4435
4436 psm = le16_to_cpu(req->psm);
4437 scid = le16_to_cpu(req->scid);
4438
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004439 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 -07004440
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004441 /* For controller id 0 make BR/EDR connection */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004442 if (req->amp_id == AMP_ID_BREDR) {
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004443 l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4444 req->amp_id);
4445 return 0;
4446 }
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004447
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004448 /* Validate AMP controller id */
4449 hdev = hci_dev_get(req->amp_id);
4450 if (!hdev)
4451 goto error;
Mat Martineau17009152012-10-23 15:24:07 -07004452
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004453 if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) {
Mat Martineau17009152012-10-23 15:24:07 -07004454 hci_dev_put(hdev);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004455 goto error;
Mat Martineau17009152012-10-23 15:24:07 -07004456 }
4457
4458 chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4459 req->amp_id);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004460 if (chan) {
4461 struct amp_mgr *mgr = conn->hcon->amp_mgr;
4462 struct hci_conn *hs_hcon;
4463
Marcel Holtmann98e0f7e2013-10-13 02:23:40 -07004464 hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
4465 &conn->hcon->dst);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004466 if (!hs_hcon) {
4467 hci_dev_put(hdev);
Johan Hedberg662d6522013-10-16 11:20:47 +03004468 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4469 chan->dcid);
4470 return 0;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004471 }
4472
4473 BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon);
4474
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004475 mgr->bredr_chan = chan;
4476 chan->hs_hcon = hs_hcon;
Andrei Emeltchenkofd45bf42012-11-20 17:16:22 +02004477 chan->fcs = L2CAP_FCS_NONE;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004478 conn->mtu = hdev->block_mtu;
4479 }
4480
4481 hci_dev_put(hdev);
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004482
4483 return 0;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004484
4485error:
4486 rsp.dcid = 0;
4487 rsp.scid = cpu_to_le16(scid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004488 rsp.result = cpu_to_le16(L2CAP_CR_BAD_AMP);
4489 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004490
4491 l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
4492 sizeof(rsp), &rsp);
4493
Johan Hedbergdc280802013-09-16 13:05:13 +03004494 return 0;
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004495}
4496
Mat Martineau8eb200b2012-10-23 15:24:17 -07004497static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id)
4498{
4499 struct l2cap_move_chan_req req;
4500 u8 ident;
4501
4502 BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id);
4503
4504 ident = l2cap_get_ident(chan->conn);
4505 chan->ident = ident;
4506
4507 req.icid = cpu_to_le16(chan->scid);
4508 req.dest_amp_id = dest_amp_id;
4509
4510 l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req),
4511 &req);
4512
4513 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4514}
4515
Mat Martineau1500109b2012-10-23 15:24:15 -07004516static void l2cap_send_move_chan_rsp(struct l2cap_chan *chan, u16 result)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004517{
4518 struct l2cap_move_chan_rsp rsp;
4519
Mat Martineau1500109b2012-10-23 15:24:15 -07004520 BT_DBG("chan %p, result 0x%4.4x", chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004521
Mat Martineau1500109b2012-10-23 15:24:15 -07004522 rsp.icid = cpu_to_le16(chan->dcid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004523 rsp.result = cpu_to_le16(result);
4524
Mat Martineau1500109b2012-10-23 15:24:15 -07004525 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP,
4526 sizeof(rsp), &rsp);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004527}
4528
Mat Martineau5b155ef2012-10-23 15:24:14 -07004529static void l2cap_send_move_chan_cfm(struct l2cap_chan *chan, u16 result)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004530{
4531 struct l2cap_move_chan_cfm cfm;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004532
Mat Martineau5b155ef2012-10-23 15:24:14 -07004533 BT_DBG("chan %p, result 0x%4.4x", chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004534
Mat Martineau5b155ef2012-10-23 15:24:14 -07004535 chan->ident = l2cap_get_ident(chan->conn);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004536
Mat Martineau5b155ef2012-10-23 15:24:14 -07004537 cfm.icid = cpu_to_le16(chan->scid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004538 cfm.result = cpu_to_le16(result);
4539
Mat Martineau5b155ef2012-10-23 15:24:14 -07004540 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM,
4541 sizeof(cfm), &cfm);
4542
4543 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4544}
4545
4546static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid)
4547{
4548 struct l2cap_move_chan_cfm cfm;
4549
4550 BT_DBG("conn %p, icid 0x%4.4x", conn, icid);
4551
4552 cfm.icid = cpu_to_le16(icid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004553 cfm.result = cpu_to_le16(L2CAP_MC_UNCONFIRMED);
Mat Martineau5b155ef2012-10-23 15:24:14 -07004554
4555 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM,
4556 sizeof(cfm), &cfm);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004557}
4558
4559static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004560 u16 icid)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004561{
4562 struct l2cap_move_chan_cfm_rsp rsp;
4563
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004564 BT_DBG("icid 0x%4.4x", icid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004565
4566 rsp.icid = cpu_to_le16(icid);
4567 l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp);
4568}
4569
Mat Martineau5f3847a2012-10-23 15:24:12 -07004570static void __release_logical_link(struct l2cap_chan *chan)
4571{
4572 chan->hs_hchan = NULL;
4573 chan->hs_hcon = NULL;
4574
4575 /* Placeholder - release the logical link */
4576}
4577
Mat Martineau1500109b2012-10-23 15:24:15 -07004578static void l2cap_logical_fail(struct l2cap_chan *chan)
4579{
4580 /* Logical link setup failed */
4581 if (chan->state != BT_CONNECTED) {
4582 /* Create channel failure, disconnect */
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004583 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineau1500109b2012-10-23 15:24:15 -07004584 return;
4585 }
4586
4587 switch (chan->move_role) {
4588 case L2CAP_MOVE_ROLE_RESPONDER:
4589 l2cap_move_done(chan);
4590 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP);
4591 break;
4592 case L2CAP_MOVE_ROLE_INITIATOR:
4593 if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP ||
4594 chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) {
4595 /* Remote has only sent pending or
4596 * success responses, clean up
4597 */
4598 l2cap_move_done(chan);
4599 }
4600
4601 /* Other amp move states imply that the move
4602 * has already aborted
4603 */
4604 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
4605 break;
4606 }
4607}
4608
4609static void l2cap_logical_finish_create(struct l2cap_chan *chan,
4610 struct hci_chan *hchan)
4611{
4612 struct l2cap_conf_rsp rsp;
Mat Martineau1500109b2012-10-23 15:24:15 -07004613
Andrei Emeltchenko336178a2012-10-31 15:46:27 +02004614 chan->hs_hchan = hchan;
Mat Martineau1500109b2012-10-23 15:24:15 -07004615 chan->hs_hcon->l2cap_data = chan->conn;
4616
Andrei Emeltchenko35ba9562012-10-25 15:20:43 +03004617 l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0);
Mat Martineau1500109b2012-10-23 15:24:15 -07004618
4619 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
Andrei Emeltchenkofe79c6f2012-10-25 15:20:42 +03004620 int err;
Mat Martineau1500109b2012-10-23 15:24:15 -07004621
4622 set_default_fcs(chan);
4623
4624 err = l2cap_ertm_init(chan);
4625 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004626 l2cap_send_disconn_req(chan, -err);
Mat Martineau1500109b2012-10-23 15:24:15 -07004627 else
4628 l2cap_chan_ready(chan);
4629 }
4630}
4631
4632static void l2cap_logical_finish_move(struct l2cap_chan *chan,
4633 struct hci_chan *hchan)
4634{
4635 chan->hs_hcon = hchan->conn;
4636 chan->hs_hcon->l2cap_data = chan->conn;
4637
4638 BT_DBG("move_state %d", chan->move_state);
4639
4640 switch (chan->move_state) {
4641 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
4642 /* Move confirm will be sent after a success
4643 * response is received
4644 */
4645 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4646 break;
4647 case L2CAP_MOVE_WAIT_LOGICAL_CFM:
4648 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4649 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4650 } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
4651 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
4652 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
4653 } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
4654 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4655 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
4656 }
4657 break;
4658 default:
4659 /* Move was not in expected state, free the channel */
4660 __release_logical_link(chan);
4661
4662 chan->move_state = L2CAP_MOVE_STABLE;
4663 }
4664}
4665
4666/* Call with chan locked */
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03004667void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan,
4668 u8 status)
Mat Martineau5b155ef2012-10-23 15:24:14 -07004669{
Mat Martineau1500109b2012-10-23 15:24:15 -07004670 BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status);
4671
4672 if (status) {
4673 l2cap_logical_fail(chan);
4674 __release_logical_link(chan);
4675 return;
4676 }
4677
4678 if (chan->state != BT_CONNECTED) {
4679 /* Ignore logical link if channel is on BR/EDR */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004680 if (chan->local_amp_id != AMP_ID_BREDR)
Mat Martineau1500109b2012-10-23 15:24:15 -07004681 l2cap_logical_finish_create(chan, hchan);
4682 } else {
4683 l2cap_logical_finish_move(chan, hchan);
4684 }
Mat Martineau5b155ef2012-10-23 15:24:14 -07004685}
4686
Mat Martineau3f7a56c2012-10-23 15:24:23 -07004687void l2cap_move_start(struct l2cap_chan *chan)
4688{
4689 BT_DBG("chan %p", chan);
4690
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004691 if (chan->local_amp_id == AMP_ID_BREDR) {
Mat Martineau3f7a56c2012-10-23 15:24:23 -07004692 if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED)
4693 return;
4694 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
4695 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
4696 /* Placeholder - start physical link setup */
4697 } else {
4698 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
4699 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4700 chan->move_id = 0;
4701 l2cap_move_setup(chan);
4702 l2cap_send_move_chan_req(chan, 0);
4703 }
4704}
4705
Mat Martineau8eb200b2012-10-23 15:24:17 -07004706static void l2cap_do_create(struct l2cap_chan *chan, int result,
4707 u8 local_amp_id, u8 remote_amp_id)
4708{
Andrei Emeltchenko62748ca2012-11-20 17:16:19 +02004709 BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state),
4710 local_amp_id, remote_amp_id);
4711
Andrei Emeltchenko12d6cc62012-11-14 17:39:31 +02004712 chan->fcs = L2CAP_FCS_NONE;
4713
Andrei Emeltchenko62748ca2012-11-20 17:16:19 +02004714 /* Outgoing channel on AMP */
4715 if (chan->state == BT_CONNECT) {
4716 if (result == L2CAP_CR_SUCCESS) {
4717 chan->local_amp_id = local_amp_id;
4718 l2cap_send_create_chan_req(chan, remote_amp_id);
4719 } else {
4720 /* Revert to BR/EDR connect */
4721 l2cap_send_conn_req(chan);
4722 }
4723
4724 return;
4725 }
4726
4727 /* Incoming channel on AMP */
4728 if (__l2cap_no_conn_pending(chan)) {
Mat Martineau8eb200b2012-10-23 15:24:17 -07004729 struct l2cap_conn_rsp rsp;
4730 char buf[128];
4731 rsp.scid = cpu_to_le16(chan->dcid);
4732 rsp.dcid = cpu_to_le16(chan->scid);
4733
Mat Martineau8eb200b2012-10-23 15:24:17 -07004734 if (result == L2CAP_CR_SUCCESS) {
4735 /* Send successful response */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004736 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
4737 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004738 } else {
4739 /* Send negative response */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004740 rsp.result = cpu_to_le16(L2CAP_CR_NO_MEM);
4741 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004742 }
4743
4744 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP,
4745 sizeof(rsp), &rsp);
4746
4747 if (result == L2CAP_CR_SUCCESS) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02004748 l2cap_state_change(chan, BT_CONFIG);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004749 set_bit(CONF_REQ_SENT, &chan->conf_state);
4750 l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn),
4751 L2CAP_CONF_REQ,
4752 l2cap_build_conf_req(chan, buf), buf);
4753 chan->num_conf_req++;
4754 }
Mat Martineau8eb200b2012-10-23 15:24:17 -07004755 }
4756}
4757
4758static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id,
4759 u8 remote_amp_id)
4760{
4761 l2cap_move_setup(chan);
4762 chan->move_id = local_amp_id;
4763 chan->move_state = L2CAP_MOVE_WAIT_RSP;
4764
4765 l2cap_send_move_chan_req(chan, remote_amp_id);
4766}
4767
4768static void l2cap_do_move_respond(struct l2cap_chan *chan, int result)
4769{
4770 struct hci_chan *hchan = NULL;
4771
4772 /* Placeholder - get hci_chan for logical link */
4773
4774 if (hchan) {
4775 if (hchan->state == BT_CONNECTED) {
4776 /* Logical link is ready to go */
4777 chan->hs_hcon = hchan->conn;
4778 chan->hs_hcon->l2cap_data = chan->conn;
4779 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4780 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
4781
4782 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
4783 } else {
4784 /* Wait for logical link to be ready */
4785 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
4786 }
4787 } else {
4788 /* Logical link not available */
4789 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED);
4790 }
4791}
4792
4793static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result)
4794{
4795 if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
4796 u8 rsp_result;
4797 if (result == -EINVAL)
4798 rsp_result = L2CAP_MR_BAD_ID;
4799 else
4800 rsp_result = L2CAP_MR_NOT_ALLOWED;
4801
4802 l2cap_send_move_chan_rsp(chan, rsp_result);
4803 }
4804
4805 chan->move_role = L2CAP_MOVE_ROLE_NONE;
4806 chan->move_state = L2CAP_MOVE_STABLE;
4807
4808 /* Restart data transmission */
4809 l2cap_ertm_send(chan);
4810}
4811
Andrei Emeltchenkoa514b172012-11-14 17:39:30 +02004812/* Invoke with locked chan */
4813void __l2cap_physical_cfm(struct l2cap_chan *chan, int result)
Mat Martineau8eb200b2012-10-23 15:24:17 -07004814{
Andrei Emeltchenko770bfef2012-10-31 15:46:29 +02004815 u8 local_amp_id = chan->local_amp_id;
Andrei Emeltchenkofffadc02012-11-01 15:37:03 +02004816 u8 remote_amp_id = chan->remote_amp_id;
Andrei Emeltchenko770bfef2012-10-31 15:46:29 +02004817
Mat Martineau8eb200b2012-10-23 15:24:17 -07004818 BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d",
4819 chan, result, local_amp_id, remote_amp_id);
4820
Mat Martineau8eb200b2012-10-23 15:24:17 -07004821 if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) {
4822 l2cap_chan_unlock(chan);
4823 return;
4824 }
4825
4826 if (chan->state != BT_CONNECTED) {
4827 l2cap_do_create(chan, result, local_amp_id, remote_amp_id);
4828 } else if (result != L2CAP_MR_SUCCESS) {
4829 l2cap_do_move_cancel(chan, result);
4830 } else {
4831 switch (chan->move_role) {
4832 case L2CAP_MOVE_ROLE_INITIATOR:
4833 l2cap_do_move_initiate(chan, local_amp_id,
4834 remote_amp_id);
4835 break;
4836 case L2CAP_MOVE_ROLE_RESPONDER:
4837 l2cap_do_move_respond(chan, result);
4838 break;
4839 default:
4840 l2cap_do_move_cancel(chan, result);
4841 break;
4842 }
4843 }
Mat Martineau8eb200b2012-10-23 15:24:17 -07004844}
4845
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004846static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004847 struct l2cap_cmd_hdr *cmd,
4848 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004849{
4850 struct l2cap_move_chan_req *req = data;
Mat Martineau1500109b2012-10-23 15:24:15 -07004851 struct l2cap_move_chan_rsp rsp;
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004852 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004853 u16 icid = 0;
4854 u16 result = L2CAP_MR_NOT_ALLOWED;
4855
4856 if (cmd_len != sizeof(*req))
4857 return -EPROTO;
4858
4859 icid = le16_to_cpu(req->icid);
4860
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004861 BT_DBG("icid 0x%4.4x, dest_amp_id %d", icid, req->dest_amp_id);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004862
Marcel Holtmann848566b2013-10-01 22:59:22 -07004863 if (!conn->hs_enabled)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004864 return -EINVAL;
4865
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004866 chan = l2cap_get_chan_by_dcid(conn, icid);
4867 if (!chan) {
Mat Martineau1500109b2012-10-23 15:24:15 -07004868 rsp.icid = cpu_to_le16(icid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004869 rsp.result = cpu_to_le16(L2CAP_MR_NOT_ALLOWED);
Mat Martineau1500109b2012-10-23 15:24:15 -07004870 l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP,
4871 sizeof(rsp), &rsp);
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004872 return 0;
4873 }
4874
Mat Martineau1500109b2012-10-23 15:24:15 -07004875 chan->ident = cmd->ident;
4876
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004877 if (chan->scid < L2CAP_CID_DYN_START ||
4878 chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY ||
4879 (chan->mode != L2CAP_MODE_ERTM &&
4880 chan->mode != L2CAP_MODE_STREAMING)) {
4881 result = L2CAP_MR_NOT_ALLOWED;
4882 goto send_move_response;
4883 }
4884
4885 if (chan->local_amp_id == req->dest_amp_id) {
4886 result = L2CAP_MR_SAME_ID;
4887 goto send_move_response;
4888 }
4889
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004890 if (req->dest_amp_id != AMP_ID_BREDR) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004891 struct hci_dev *hdev;
4892 hdev = hci_dev_get(req->dest_amp_id);
4893 if (!hdev || hdev->dev_type != HCI_AMP ||
4894 !test_bit(HCI_UP, &hdev->flags)) {
4895 if (hdev)
4896 hci_dev_put(hdev);
4897
4898 result = L2CAP_MR_BAD_ID;
4899 goto send_move_response;
4900 }
4901 hci_dev_put(hdev);
4902 }
4903
4904 /* Detect a move collision. Only send a collision response
4905 * if this side has "lost", otherwise proceed with the move.
4906 * The winner has the larger bd_addr.
4907 */
4908 if ((__chan_is_moving(chan) ||
4909 chan->move_role != L2CAP_MOVE_ROLE_NONE) &&
Marcel Holtmann6f59b902013-10-13 05:24:01 -07004910 bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004911 result = L2CAP_MR_COLLISION;
4912 goto send_move_response;
4913 }
4914
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004915 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
4916 l2cap_move_setup(chan);
4917 chan->move_id = req->dest_amp_id;
4918 icid = chan->dcid;
4919
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004920 if (req->dest_amp_id == AMP_ID_BREDR) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004921 /* Moving to BR/EDR */
4922 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4923 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4924 result = L2CAP_MR_PEND;
4925 } else {
4926 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4927 result = L2CAP_MR_SUCCESS;
4928 }
4929 } else {
4930 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
4931 /* Placeholder - uncomment when amp functions are available */
4932 /*amp_accept_physical(chan, req->dest_amp_id);*/
4933 result = L2CAP_MR_PEND;
4934 }
4935
4936send_move_response:
Mat Martineau1500109b2012-10-23 15:24:15 -07004937 l2cap_send_move_chan_rsp(chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004938
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004939 l2cap_chan_unlock(chan);
4940
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004941 return 0;
4942}
4943
Mat Martineau5b155ef2012-10-23 15:24:14 -07004944static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result)
4945{
4946 struct l2cap_chan *chan;
4947 struct hci_chan *hchan = NULL;
4948
4949 chan = l2cap_get_chan_by_scid(conn, icid);
4950 if (!chan) {
4951 l2cap_send_move_chan_cfm_icid(conn, icid);
4952 return;
4953 }
4954
4955 __clear_chan_timer(chan);
4956 if (result == L2CAP_MR_PEND)
4957 __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT);
4958
4959 switch (chan->move_state) {
4960 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
4961 /* Move confirm will be sent when logical link
4962 * is complete.
4963 */
4964 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
4965 break;
4966 case L2CAP_MOVE_WAIT_RSP_SUCCESS:
4967 if (result == L2CAP_MR_PEND) {
4968 break;
4969 } else if (test_bit(CONN_LOCAL_BUSY,
4970 &chan->conn_state)) {
4971 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4972 } else {
4973 /* Logical link is up or moving to BR/EDR,
4974 * proceed with move
4975 */
4976 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
4977 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
4978 }
4979 break;
4980 case L2CAP_MOVE_WAIT_RSP:
4981 /* Moving to AMP */
4982 if (result == L2CAP_MR_SUCCESS) {
4983 /* Remote is ready, send confirm immediately
4984 * after logical link is ready
4985 */
4986 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
4987 } else {
4988 /* Both logical link and move success
4989 * are required to confirm
4990 */
4991 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP;
4992 }
4993
4994 /* Placeholder - get hci_chan for logical link */
4995 if (!hchan) {
4996 /* Logical link not available */
4997 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
4998 break;
4999 }
5000
5001 /* If the logical link is not yet connected, do not
5002 * send confirmation.
5003 */
5004 if (hchan->state != BT_CONNECTED)
5005 break;
5006
5007 /* Logical link is already ready to go */
5008
5009 chan->hs_hcon = hchan->conn;
5010 chan->hs_hcon->l2cap_data = chan->conn;
5011
5012 if (result == L2CAP_MR_SUCCESS) {
5013 /* Can confirm now */
5014 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
5015 } else {
5016 /* Now only need move success
5017 * to confirm
5018 */
5019 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
5020 }
5021
5022 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
5023 break;
5024 default:
5025 /* Any other amp move state means the move failed. */
5026 chan->move_id = chan->local_amp_id;
5027 l2cap_move_done(chan);
5028 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5029 }
5030
5031 l2cap_chan_unlock(chan);
5032}
5033
5034static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid,
5035 u16 result)
5036{
5037 struct l2cap_chan *chan;
5038
5039 chan = l2cap_get_chan_by_ident(conn, ident);
5040 if (!chan) {
5041 /* Could not locate channel, icid is best guess */
5042 l2cap_send_move_chan_cfm_icid(conn, icid);
5043 return;
5044 }
5045
5046 __clear_chan_timer(chan);
5047
5048 if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
5049 if (result == L2CAP_MR_COLLISION) {
5050 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
5051 } else {
5052 /* Cleanup - cancel move */
5053 chan->move_id = chan->local_amp_id;
5054 l2cap_move_done(chan);
5055 }
5056 }
5057
5058 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5059
5060 l2cap_chan_unlock(chan);
5061}
5062
5063static int l2cap_move_channel_rsp(struct l2cap_conn *conn,
5064 struct l2cap_cmd_hdr *cmd,
5065 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005066{
5067 struct l2cap_move_chan_rsp *rsp = data;
5068 u16 icid, result;
5069
5070 if (cmd_len != sizeof(*rsp))
5071 return -EPROTO;
5072
5073 icid = le16_to_cpu(rsp->icid);
5074 result = le16_to_cpu(rsp->result);
5075
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005076 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005077
Mat Martineau5b155ef2012-10-23 15:24:14 -07005078 if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND)
5079 l2cap_move_continue(conn, icid, result);
5080 else
5081 l2cap_move_fail(conn, cmd->ident, icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005082
5083 return 0;
5084}
5085
Mat Martineau5f3847a2012-10-23 15:24:12 -07005086static int l2cap_move_channel_confirm(struct l2cap_conn *conn,
5087 struct l2cap_cmd_hdr *cmd,
5088 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005089{
5090 struct l2cap_move_chan_cfm *cfm = data;
Mat Martineau5f3847a2012-10-23 15:24:12 -07005091 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005092 u16 icid, result;
5093
5094 if (cmd_len != sizeof(*cfm))
5095 return -EPROTO;
5096
5097 icid = le16_to_cpu(cfm->icid);
5098 result = le16_to_cpu(cfm->result);
5099
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005100 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005101
Mat Martineau5f3847a2012-10-23 15:24:12 -07005102 chan = l2cap_get_chan_by_dcid(conn, icid);
5103 if (!chan) {
5104 /* Spec requires a response even if the icid was not found */
5105 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5106 return 0;
5107 }
5108
5109 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) {
5110 if (result == L2CAP_MC_CONFIRMED) {
5111 chan->local_amp_id = chan->move_id;
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07005112 if (chan->local_amp_id == AMP_ID_BREDR)
Mat Martineau5f3847a2012-10-23 15:24:12 -07005113 __release_logical_link(chan);
5114 } else {
5115 chan->move_id = chan->local_amp_id;
5116 }
5117
5118 l2cap_move_done(chan);
5119 }
5120
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005121 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5122
Mat Martineau5f3847a2012-10-23 15:24:12 -07005123 l2cap_chan_unlock(chan);
5124
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005125 return 0;
5126}
5127
5128static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005129 struct l2cap_cmd_hdr *cmd,
5130 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005131{
5132 struct l2cap_move_chan_cfm_rsp *rsp = data;
Mat Martineau3fd71a02012-10-23 15:24:16 -07005133 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005134 u16 icid;
5135
5136 if (cmd_len != sizeof(*rsp))
5137 return -EPROTO;
5138
5139 icid = le16_to_cpu(rsp->icid);
5140
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005141 BT_DBG("icid 0x%4.4x", icid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005142
Mat Martineau3fd71a02012-10-23 15:24:16 -07005143 chan = l2cap_get_chan_by_scid(conn, icid);
5144 if (!chan)
5145 return 0;
5146
5147 __clear_chan_timer(chan);
5148
5149 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) {
5150 chan->local_amp_id = chan->move_id;
5151
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07005152 if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan)
Mat Martineau3fd71a02012-10-23 15:24:16 -07005153 __release_logical_link(chan);
5154
5155 l2cap_move_done(chan);
5156 }
5157
5158 l2cap_chan_unlock(chan);
5159
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005160 return 0;
5161}
5162
Claudio Takahaside731152011-02-11 19:28:55 -02005163static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005164 struct l2cap_cmd_hdr *cmd,
Johan Hedberg203e6392013-05-15 10:07:15 +03005165 u16 cmd_len, u8 *data)
Claudio Takahaside731152011-02-11 19:28:55 -02005166{
5167 struct hci_conn *hcon = conn->hcon;
5168 struct l2cap_conn_param_update_req *req;
5169 struct l2cap_conn_param_update_rsp rsp;
Johan Hedberg203e6392013-05-15 10:07:15 +03005170 u16 min, max, latency, to_multiplier;
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005171 int err;
Claudio Takahaside731152011-02-11 19:28:55 -02005172
Johan Hedberg40bef302014-07-16 11:42:27 +03005173 if (hcon->role != HCI_ROLE_MASTER)
Claudio Takahaside731152011-02-11 19:28:55 -02005174 return -EINVAL;
5175
Claudio Takahaside731152011-02-11 19:28:55 -02005176 if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
5177 return -EPROTO;
5178
5179 req = (struct l2cap_conn_param_update_req *) data;
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03005180 min = __le16_to_cpu(req->min);
5181 max = __le16_to_cpu(req->max);
Claudio Takahaside731152011-02-11 19:28:55 -02005182 latency = __le16_to_cpu(req->latency);
5183 to_multiplier = __le16_to_cpu(req->to_multiplier);
5184
5185 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 +01005186 min, max, latency, to_multiplier);
Claudio Takahaside731152011-02-11 19:28:55 -02005187
5188 memset(&rsp, 0, sizeof(rsp));
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005189
Andre Guedesd4905f22014-06-25 21:52:52 -03005190 err = hci_check_conn_params(min, max, latency, to_multiplier);
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005191 if (err)
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005192 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
Claudio Takahaside731152011-02-11 19:28:55 -02005193 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005194 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
Claudio Takahaside731152011-02-11 19:28:55 -02005195
5196 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005197 sizeof(rsp), &rsp);
Claudio Takahaside731152011-02-11 19:28:55 -02005198
Andre Guedesffb5a8272014-07-01 18:10:11 -03005199 if (!err) {
Johan Hedbergf4869e22014-07-02 17:37:32 +03005200 u8 store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03005201
Johan Hedbergf4869e22014-07-02 17:37:32 +03005202 store_hint = hci_le_conn_update(hcon, min, max, latency,
5203 to_multiplier);
5204 mgmt_new_conn_param(hcon->hdev, &hcon->dst, hcon->dst_type,
5205 store_hint, min, max, latency,
5206 to_multiplier);
5207
Andre Guedesffb5a8272014-07-01 18:10:11 -03005208 }
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005209
Claudio Takahaside731152011-02-11 19:28:55 -02005210 return 0;
5211}
5212
Johan Hedbergf1496de2013-05-13 14:15:56 +03005213static int l2cap_le_connect_rsp(struct l2cap_conn *conn,
5214 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5215 u8 *data)
5216{
5217 struct l2cap_le_conn_rsp *rsp = (struct l2cap_le_conn_rsp *) data;
5218 u16 dcid, mtu, mps, credits, result;
5219 struct l2cap_chan *chan;
5220 int err;
5221
5222 if (cmd_len < sizeof(*rsp))
5223 return -EPROTO;
5224
5225 dcid = __le16_to_cpu(rsp->dcid);
5226 mtu = __le16_to_cpu(rsp->mtu);
5227 mps = __le16_to_cpu(rsp->mps);
5228 credits = __le16_to_cpu(rsp->credits);
5229 result = __le16_to_cpu(rsp->result);
5230
5231 if (result == L2CAP_CR_SUCCESS && (mtu < 23 || mps < 23))
5232 return -EPROTO;
5233
5234 BT_DBG("dcid 0x%4.4x mtu %u mps %u credits %u result 0x%2.2x",
5235 dcid, mtu, mps, credits, result);
5236
5237 mutex_lock(&conn->chan_lock);
5238
5239 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
5240 if (!chan) {
5241 err = -EBADSLT;
5242 goto unlock;
5243 }
5244
5245 err = 0;
5246
5247 l2cap_chan_lock(chan);
5248
5249 switch (result) {
5250 case L2CAP_CR_SUCCESS:
5251 chan->ident = 0;
5252 chan->dcid = dcid;
5253 chan->omtu = mtu;
5254 chan->remote_mps = mps;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005255 chan->tx_credits = credits;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005256 l2cap_chan_ready(chan);
5257 break;
5258
5259 default:
5260 l2cap_chan_del(chan, ECONNREFUSED);
5261 break;
5262 }
5263
5264 l2cap_chan_unlock(chan);
5265
5266unlock:
5267 mutex_unlock(&conn->chan_lock);
5268
5269 return err;
5270}
5271
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005272static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005273 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5274 u8 *data)
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005275{
5276 int err = 0;
5277
5278 switch (cmd->code) {
5279 case L2CAP_COMMAND_REJ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005280 l2cap_command_rej(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005281 break;
5282
5283 case L2CAP_CONN_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005284 err = l2cap_connect_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005285 break;
5286
5287 case L2CAP_CONN_RSP:
Mat Martineauf5a25982012-10-11 17:48:21 +03005288 case L2CAP_CREATE_CHAN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005289 l2cap_connect_create_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005290 break;
5291
5292 case L2CAP_CONF_REQ:
5293 err = l2cap_config_req(conn, cmd, cmd_len, data);
5294 break;
5295
5296 case L2CAP_CONF_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005297 l2cap_config_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005298 break;
5299
5300 case L2CAP_DISCONN_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005301 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005302 break;
5303
5304 case L2CAP_DISCONN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005305 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005306 break;
5307
5308 case L2CAP_ECHO_REQ:
5309 l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
5310 break;
5311
5312 case L2CAP_ECHO_RSP:
5313 break;
5314
5315 case L2CAP_INFO_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005316 err = l2cap_information_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005317 break;
5318
5319 case L2CAP_INFO_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005320 l2cap_information_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005321 break;
5322
Mat Martineauf94ff6f2011-11-02 16:18:32 -07005323 case L2CAP_CREATE_CHAN_REQ:
5324 err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
5325 break;
5326
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005327 case L2CAP_MOVE_CHAN_REQ:
5328 err = l2cap_move_channel_req(conn, cmd, cmd_len, data);
5329 break;
5330
5331 case L2CAP_MOVE_CHAN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005332 l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005333 break;
5334
5335 case L2CAP_MOVE_CHAN_CFM:
5336 err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data);
5337 break;
5338
5339 case L2CAP_MOVE_CHAN_CFM_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005340 l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005341 break;
5342
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005343 default:
5344 BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
5345 err = -EINVAL;
5346 break;
5347 }
5348
5349 return err;
5350}
5351
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005352static int l2cap_le_connect_req(struct l2cap_conn *conn,
5353 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5354 u8 *data)
5355{
5356 struct l2cap_le_conn_req *req = (struct l2cap_le_conn_req *) data;
5357 struct l2cap_le_conn_rsp rsp;
5358 struct l2cap_chan *chan, *pchan;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005359 u16 dcid, scid, credits, mtu, mps;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005360 __le16 psm;
5361 u8 result;
5362
5363 if (cmd_len != sizeof(*req))
5364 return -EPROTO;
5365
5366 scid = __le16_to_cpu(req->scid);
5367 mtu = __le16_to_cpu(req->mtu);
5368 mps = __le16_to_cpu(req->mps);
5369 psm = req->psm;
5370 dcid = 0;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005371 credits = 0;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005372
5373 if (mtu < 23 || mps < 23)
5374 return -EPROTO;
5375
5376 BT_DBG("psm 0x%2.2x scid 0x%4.4x mtu %u mps %u", __le16_to_cpu(psm),
5377 scid, mtu, mps);
5378
5379 /* Check if we have socket listening on psm */
5380 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
5381 &conn->hcon->dst, LE_LINK);
5382 if (!pchan) {
5383 result = L2CAP_CR_BAD_PSM;
5384 chan = NULL;
5385 goto response;
5386 }
5387
5388 mutex_lock(&conn->chan_lock);
5389 l2cap_chan_lock(pchan);
5390
5391 if (!smp_sufficient_security(conn->hcon, pchan->sec_level)) {
5392 result = L2CAP_CR_AUTHENTICATION;
5393 chan = NULL;
5394 goto response_unlock;
5395 }
5396
5397 /* Check if we already have channel with that dcid */
5398 if (__l2cap_get_chan_by_dcid(conn, scid)) {
5399 result = L2CAP_CR_NO_MEM;
5400 chan = NULL;
5401 goto response_unlock;
5402 }
5403
5404 chan = pchan->ops->new_connection(pchan);
5405 if (!chan) {
5406 result = L2CAP_CR_NO_MEM;
5407 goto response_unlock;
5408 }
5409
Johan Hedberg0ce43ce2013-12-05 14:55:33 +02005410 l2cap_le_flowctl_init(chan);
5411
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005412 bacpy(&chan->src, &conn->hcon->src);
5413 bacpy(&chan->dst, &conn->hcon->dst);
5414 chan->src_type = bdaddr_type(conn->hcon, conn->hcon->src_type);
5415 chan->dst_type = bdaddr_type(conn->hcon, conn->hcon->dst_type);
5416 chan->psm = psm;
5417 chan->dcid = scid;
5418 chan->omtu = mtu;
5419 chan->remote_mps = mps;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005420 chan->tx_credits = __le16_to_cpu(req->credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005421
5422 __l2cap_chan_add(conn, chan);
5423 dcid = chan->scid;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005424 credits = chan->rx_credits;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005425
5426 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
5427
5428 chan->ident = cmd->ident;
5429
5430 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
5431 l2cap_state_change(chan, BT_CONNECT2);
5432 result = L2CAP_CR_PEND;
5433 chan->ops->defer(chan);
5434 } else {
5435 l2cap_chan_ready(chan);
5436 result = L2CAP_CR_SUCCESS;
5437 }
5438
5439response_unlock:
5440 l2cap_chan_unlock(pchan);
5441 mutex_unlock(&conn->chan_lock);
Johan Hedberga24cce12014-08-07 22:56:42 +03005442 l2cap_chan_put(pchan);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005443
5444 if (result == L2CAP_CR_PEND)
5445 return 0;
5446
5447response:
5448 if (chan) {
5449 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02005450 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005451 } else {
5452 rsp.mtu = 0;
5453 rsp.mps = 0;
5454 }
5455
5456 rsp.dcid = cpu_to_le16(dcid);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005457 rsp.credits = cpu_to_le16(credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005458 rsp.result = cpu_to_le16(result);
5459
5460 l2cap_send_cmd(conn, cmd->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), &rsp);
5461
5462 return 0;
5463}
5464
Johan Hedbergfad5fc82013-12-05 09:45:01 +02005465static inline int l2cap_le_credits(struct l2cap_conn *conn,
5466 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5467 u8 *data)
5468{
5469 struct l2cap_le_credits *pkt;
5470 struct l2cap_chan *chan;
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005471 u16 cid, credits, max_credits;
Johan Hedbergfad5fc82013-12-05 09:45:01 +02005472
5473 if (cmd_len != sizeof(*pkt))
5474 return -EPROTO;
5475
5476 pkt = (struct l2cap_le_credits *) data;
5477 cid = __le16_to_cpu(pkt->cid);
5478 credits = __le16_to_cpu(pkt->credits);
5479
5480 BT_DBG("cid 0x%4.4x credits 0x%4.4x", cid, credits);
5481
5482 chan = l2cap_get_chan_by_dcid(conn, cid);
5483 if (!chan)
5484 return -EBADSLT;
5485
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005486 max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits;
5487 if (credits > max_credits) {
5488 BT_ERR("LE credits overflow");
5489 l2cap_send_disconn_req(chan, ECONNRESET);
5490
5491 /* Return 0 so that we don't trigger an unnecessary
5492 * command reject packet.
5493 */
5494 return 0;
5495 }
5496
Johan Hedbergfad5fc82013-12-05 09:45:01 +02005497 chan->tx_credits += credits;
5498
5499 while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) {
5500 l2cap_do_send(chan, skb_dequeue(&chan->tx_q));
5501 chan->tx_credits--;
5502 }
5503
5504 if (chan->tx_credits)
5505 chan->ops->resume(chan);
5506
5507 l2cap_chan_unlock(chan);
5508
5509 return 0;
5510}
5511
Johan Hedberg71fb4192013-12-10 10:52:48 +02005512static inline int l2cap_le_command_rej(struct l2cap_conn *conn,
5513 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5514 u8 *data)
5515{
5516 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
5517 struct l2cap_chan *chan;
5518
5519 if (cmd_len < sizeof(*rej))
5520 return -EPROTO;
5521
5522 mutex_lock(&conn->chan_lock);
5523
5524 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
5525 if (!chan)
5526 goto done;
5527
5528 l2cap_chan_lock(chan);
5529 l2cap_chan_del(chan, ECONNREFUSED);
5530 l2cap_chan_unlock(chan);
5531
5532done:
5533 mutex_unlock(&conn->chan_lock);
5534 return 0;
5535}
5536
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005537static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
Johan Hedberg203e6392013-05-15 10:07:15 +03005538 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5539 u8 *data)
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005540{
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005541 int err = 0;
5542
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005543 switch (cmd->code) {
5544 case L2CAP_COMMAND_REJ:
Johan Hedberg71fb4192013-12-10 10:52:48 +02005545 l2cap_le_command_rej(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005546 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005547
5548 case L2CAP_CONN_PARAM_UPDATE_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005549 err = l2cap_conn_param_update_req(conn, cmd, cmd_len, data);
5550 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005551
5552 case L2CAP_CONN_PARAM_UPDATE_RSP:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005553 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005554
Johan Hedbergf1496de2013-05-13 14:15:56 +03005555 case L2CAP_LE_CONN_RSP:
5556 l2cap_le_connect_rsp(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005557 break;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005558
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005559 case L2CAP_LE_CONN_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005560 err = l2cap_le_connect_req(conn, cmd, cmd_len, data);
5561 break;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005562
Johan Hedbergfad5fc82013-12-05 09:45:01 +02005563 case L2CAP_LE_CREDITS:
5564 err = l2cap_le_credits(conn, cmd, cmd_len, data);
5565 break;
5566
Johan Hedberg3defe012013-05-15 10:16:06 +03005567 case L2CAP_DISCONN_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005568 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
5569 break;
Johan Hedberg3defe012013-05-15 10:16:06 +03005570
5571 case L2CAP_DISCONN_RSP:
5572 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005573 break;
Johan Hedberg3defe012013-05-15 10:16:06 +03005574
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005575 default:
5576 BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005577 err = -EINVAL;
5578 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005579 }
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005580
5581 return err;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005582}
5583
Johan Hedbergc5623552013-04-29 19:35:33 +03005584static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,
5585 struct sk_buff *skb)
5586{
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005587 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005588 struct l2cap_cmd_hdr *cmd;
5589 u16 len;
Johan Hedbergc5623552013-04-29 19:35:33 +03005590 int err;
5591
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005592 if (hcon->type != LE_LINK)
Marcel Holtmann3b166292013-10-02 08:28:21 -07005593 goto drop;
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005594
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005595 if (skb->len < L2CAP_CMD_HDR_SIZE)
5596 goto drop;
Johan Hedbergc5623552013-04-29 19:35:33 +03005597
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005598 cmd = (void *) skb->data;
5599 skb_pull(skb, L2CAP_CMD_HDR_SIZE);
Johan Hedbergc5623552013-04-29 19:35:33 +03005600
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005601 len = le16_to_cpu(cmd->len);
Johan Hedbergc5623552013-04-29 19:35:33 +03005602
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005603 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd->code, len, cmd->ident);
Johan Hedbergc5623552013-04-29 19:35:33 +03005604
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005605 if (len != skb->len || !cmd->ident) {
5606 BT_DBG("corrupted command");
5607 goto drop;
5608 }
Johan Hedbergc5623552013-04-29 19:35:33 +03005609
Johan Hedberg203e6392013-05-15 10:07:15 +03005610 err = l2cap_le_sig_cmd(conn, cmd, len, skb->data);
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005611 if (err) {
5612 struct l2cap_cmd_rej_unk rej;
Johan Hedbergc5623552013-04-29 19:35:33 +03005613
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005614 BT_ERR("Wrong link type (%d)", err);
Johan Hedbergc5623552013-04-29 19:35:33 +03005615
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005616 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005617 l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
5618 sizeof(rej), &rej);
Johan Hedbergc5623552013-04-29 19:35:33 +03005619 }
5620
Marcel Holtmann3b166292013-10-02 08:28:21 -07005621drop:
Johan Hedbergc5623552013-04-29 19:35:33 +03005622 kfree_skb(skb);
5623}
5624
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005625static inline void l2cap_sig_channel(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005626 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627{
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005628 struct hci_conn *hcon = conn->hcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 u8 *data = skb->data;
5630 int len = skb->len;
5631 struct l2cap_cmd_hdr cmd;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005632 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
5634 l2cap_raw_recv(conn, skb);
5635
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005636 if (hcon->type != ACL_LINK)
Marcel Holtmann3b166292013-10-02 08:28:21 -07005637 goto drop;
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005638
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 while (len >= L2CAP_CMD_HDR_SIZE) {
Al Viro88219a02007-07-29 00:17:25 -07005640 u16 cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
5642 data += L2CAP_CMD_HDR_SIZE;
5643 len -= L2CAP_CMD_HDR_SIZE;
5644
Al Viro88219a02007-07-29 00:17:25 -07005645 cmd_len = le16_to_cpu(cmd.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
Gustavo Padovan2d792812012-10-06 10:07:01 +01005647 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len,
5648 cmd.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
Al Viro88219a02007-07-29 00:17:25 -07005650 if (cmd_len > len || !cmd.ident) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 BT_DBG("corrupted command");
5652 break;
5653 }
5654
Johan Hedbergc5623552013-04-29 19:35:33 +03005655 err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 if (err) {
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03005657 struct l2cap_cmd_rej_unk rej;
Gustavo F. Padovan2c6d1a22011-03-23 14:38:32 -03005658
5659 BT_ERR("Wrong link type (%d)", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005661 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
Gustavo Padovan2d792812012-10-06 10:07:01 +01005662 l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ,
5663 sizeof(rej), &rej);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 }
5665
Al Viro88219a02007-07-29 00:17:25 -07005666 data += cmd_len;
5667 len -= cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 }
5669
Marcel Holtmann3b166292013-10-02 08:28:21 -07005670drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 kfree_skb(skb);
5672}
5673
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03005674static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005675{
5676 u16 our_fcs, rcv_fcs;
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03005677 int hdr_size;
5678
5679 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
5680 hdr_size = L2CAP_EXT_HDR_SIZE;
5681 else
5682 hdr_size = L2CAP_ENH_HDR_SIZE;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005683
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03005684 if (chan->fcs == L2CAP_FCS_CRC16) {
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03005685 skb_trim(skb, skb->len - L2CAP_FCS_SIZE);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005686 rcv_fcs = get_unaligned_le16(skb->data + skb->len);
5687 our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
5688
5689 if (our_fcs != rcv_fcs)
João Paulo Rechi Vita7a560e52010-06-22 13:56:27 -03005690 return -EBADMSG;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005691 }
5692 return 0;
5693}
5694
Mat Martineau6ea00482012-05-17 20:53:52 -07005695static void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005696{
Mat Martineaue31f7632012-05-17 20:53:41 -07005697 struct l2cap_ctrl control;
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005698
Mat Martineaue31f7632012-05-17 20:53:41 -07005699 BT_DBG("chan %p", chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005700
Mat Martineaue31f7632012-05-17 20:53:41 -07005701 memset(&control, 0, sizeof(control));
5702 control.sframe = 1;
5703 control.final = 1;
5704 control.reqseq = chan->buffer_seq;
5705 set_bit(CONN_SEND_FBIT, &chan->conn_state);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005706
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03005707 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
Mat Martineaue31f7632012-05-17 20:53:41 -07005708 control.super = L2CAP_SUPER_RNR;
5709 l2cap_send_sframe(chan, &control);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005710 }
5711
Mat Martineaue31f7632012-05-17 20:53:41 -07005712 if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
5713 chan->unacked_frames > 0)
5714 __set_retrans_timer(chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005715
Mat Martineaue31f7632012-05-17 20:53:41 -07005716 /* Send pending iframes */
Gustavo F. Padovan525cd182011-03-25 19:43:39 -03005717 l2cap_ertm_send(chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005718
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03005719 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
Mat Martineaue31f7632012-05-17 20:53:41 -07005720 test_bit(CONN_SEND_FBIT, &chan->conn_state)) {
5721 /* F-bit wasn't sent in an s-frame or i-frame yet, so
5722 * send it now.
5723 */
5724 control.super = L2CAP_SUPER_RR;
5725 l2cap_send_sframe(chan, &control);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005726 }
5727}
5728
Gustavo Padovan2d792812012-10-06 10:07:01 +01005729static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag,
5730 struct sk_buff **last_frag)
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005731{
Mat Martineau84084a32011-07-22 14:54:00 -07005732 /* skb->len reflects data in skb as well as all fragments
5733 * skb->data_len reflects only data in fragments
5734 */
5735 if (!skb_has_frag_list(skb))
5736 skb_shinfo(skb)->frag_list = new_frag;
5737
5738 new_frag->next = NULL;
5739
5740 (*last_frag)->next = new_frag;
5741 *last_frag = new_frag;
5742
5743 skb->len += new_frag->len;
5744 skb->data_len += new_frag->len;
5745 skb->truesize += new_frag->truesize;
5746}
5747
Mat Martineau4b51dae92012-05-17 20:53:37 -07005748static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb,
5749 struct l2cap_ctrl *control)
Mat Martineau84084a32011-07-22 14:54:00 -07005750{
5751 int err = -EINVAL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005752
Mat Martineau4b51dae92012-05-17 20:53:37 -07005753 switch (control->sar) {
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005754 case L2CAP_SAR_UNSEGMENTED:
Mat Martineau84084a32011-07-22 14:54:00 -07005755 if (chan->sdu)
5756 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005757
Gustavo Padovan80b98022012-05-27 22:27:51 -03005758 err = chan->ops->recv(chan, skb);
Mat Martineau84084a32011-07-22 14:54:00 -07005759 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005760
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005761 case L2CAP_SAR_START:
Mat Martineau84084a32011-07-22 14:54:00 -07005762 if (chan->sdu)
5763 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005764
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005765 chan->sdu_len = get_unaligned_le16(skb->data);
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03005766 skb_pull(skb, L2CAP_SDULEN_SIZE);
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005767
Mat Martineau84084a32011-07-22 14:54:00 -07005768 if (chan->sdu_len > chan->imtu) {
5769 err = -EMSGSIZE;
5770 break;
5771 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005772
Mat Martineau84084a32011-07-22 14:54:00 -07005773 if (skb->len >= chan->sdu_len)
5774 break;
5775
5776 chan->sdu = skb;
5777 chan->sdu_last_frag = skb;
5778
5779 skb = NULL;
5780 err = 0;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005781 break;
5782
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005783 case L2CAP_SAR_CONTINUE:
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005784 if (!chan->sdu)
Mat Martineau84084a32011-07-22 14:54:00 -07005785 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005786
Mat Martineau84084a32011-07-22 14:54:00 -07005787 append_skb_frag(chan->sdu, skb,
5788 &chan->sdu_last_frag);
5789 skb = NULL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005790
Mat Martineau84084a32011-07-22 14:54:00 -07005791 if (chan->sdu->len >= chan->sdu_len)
5792 break;
Gustavo F. Padovan4178ba42010-05-01 16:15:45 -03005793
Mat Martineau84084a32011-07-22 14:54:00 -07005794 err = 0;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005795 break;
5796
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005797 case L2CAP_SAR_END:
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005798 if (!chan->sdu)
Mat Martineau84084a32011-07-22 14:54:00 -07005799 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005800
Mat Martineau84084a32011-07-22 14:54:00 -07005801 append_skb_frag(chan->sdu, skb,
5802 &chan->sdu_last_frag);
5803 skb = NULL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005804
Mat Martineau84084a32011-07-22 14:54:00 -07005805 if (chan->sdu->len != chan->sdu_len)
5806 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005807
Gustavo Padovan80b98022012-05-27 22:27:51 -03005808 err = chan->ops->recv(chan, chan->sdu);
Gustavo F. Padovan4178ba42010-05-01 16:15:45 -03005809
Mat Martineau84084a32011-07-22 14:54:00 -07005810 if (!err) {
5811 /* Reassembly complete */
5812 chan->sdu = NULL;
5813 chan->sdu_last_frag = NULL;
5814 chan->sdu_len = 0;
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03005815 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005816 break;
5817 }
5818
Mat Martineau84084a32011-07-22 14:54:00 -07005819 if (err) {
5820 kfree_skb(skb);
5821 kfree_skb(chan->sdu);
5822 chan->sdu = NULL;
5823 chan->sdu_last_frag = NULL;
5824 chan->sdu_len = 0;
5825 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005826
Mat Martineau84084a32011-07-22 14:54:00 -07005827 return err;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005828}
5829
Mat Martineau32b32732012-10-23 15:24:11 -07005830static int l2cap_resegment(struct l2cap_chan *chan)
5831{
5832 /* Placeholder */
5833 return 0;
5834}
5835
Mat Martineaue3281402011-07-07 09:39:02 -07005836void l2cap_chan_busy(struct l2cap_chan *chan, int busy)
Gustavo F. Padovan712132eb2010-06-21 19:39:50 -03005837{
Mat Martineau61aa4f52012-05-17 20:53:40 -07005838 u8 event;
5839
5840 if (chan->mode != L2CAP_MODE_ERTM)
5841 return;
5842
5843 event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR;
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03005844 l2cap_tx(chan, NULL, NULL, event);
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03005845}
5846
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005847static int l2cap_rx_queued_iframes(struct l2cap_chan *chan)
5848{
Mat Martineau63838722012-05-17 20:53:45 -07005849 int err = 0;
5850 /* Pass sequential frames to l2cap_reassemble_sdu()
5851 * until a gap is encountered.
5852 */
5853
5854 BT_DBG("chan %p", chan);
5855
5856 while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
5857 struct sk_buff *skb;
5858 BT_DBG("Searching for skb with txseq %d (queue len %d)",
5859 chan->buffer_seq, skb_queue_len(&chan->srej_q));
5860
5861 skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq);
5862
5863 if (!skb)
5864 break;
5865
5866 skb_unlink(skb, &chan->srej_q);
5867 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
5868 err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->control);
5869 if (err)
5870 break;
5871 }
5872
5873 if (skb_queue_empty(&chan->srej_q)) {
5874 chan->rx_state = L2CAP_RX_STATE_RECV;
5875 l2cap_send_ack(chan);
5876 }
5877
5878 return err;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005879}
5880
5881static void l2cap_handle_srej(struct l2cap_chan *chan,
5882 struct l2cap_ctrl *control)
5883{
Mat Martineauf80842a2012-05-17 20:53:46 -07005884 struct sk_buff *skb;
5885
5886 BT_DBG("chan %p, control %p", chan, control);
5887
5888 if (control->reqseq == chan->next_tx_seq) {
5889 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005890 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineauf80842a2012-05-17 20:53:46 -07005891 return;
5892 }
5893
5894 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
5895
5896 if (skb == NULL) {
5897 BT_DBG("Seq %d not available for retransmission",
5898 control->reqseq);
5899 return;
5900 }
5901
5902 if (chan->max_tx != 0 && bt_cb(skb)->control.retries >= chan->max_tx) {
5903 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005904 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineauf80842a2012-05-17 20:53:46 -07005905 return;
5906 }
5907
5908 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
5909
5910 if (control->poll) {
5911 l2cap_pass_to_tx(chan, control);
5912
5913 set_bit(CONN_SEND_FBIT, &chan->conn_state);
5914 l2cap_retransmit(chan, control);
5915 l2cap_ertm_send(chan);
5916
5917 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
5918 set_bit(CONN_SREJ_ACT, &chan->conn_state);
5919 chan->srej_save_reqseq = control->reqseq;
5920 }
5921 } else {
5922 l2cap_pass_to_tx_fbit(chan, control);
5923
5924 if (control->final) {
5925 if (chan->srej_save_reqseq != control->reqseq ||
5926 !test_and_clear_bit(CONN_SREJ_ACT,
5927 &chan->conn_state))
5928 l2cap_retransmit(chan, control);
5929 } else {
5930 l2cap_retransmit(chan, control);
5931 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
5932 set_bit(CONN_SREJ_ACT, &chan->conn_state);
5933 chan->srej_save_reqseq = control->reqseq;
5934 }
5935 }
5936 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005937}
5938
5939static void l2cap_handle_rej(struct l2cap_chan *chan,
5940 struct l2cap_ctrl *control)
5941{
Mat Martineaufcd289d2012-05-17 20:53:47 -07005942 struct sk_buff *skb;
5943
5944 BT_DBG("chan %p, control %p", chan, control);
5945
5946 if (control->reqseq == chan->next_tx_seq) {
5947 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005948 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaufcd289d2012-05-17 20:53:47 -07005949 return;
5950 }
5951
5952 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
5953
5954 if (chan->max_tx && skb &&
5955 bt_cb(skb)->control.retries >= chan->max_tx) {
5956 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005957 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaufcd289d2012-05-17 20:53:47 -07005958 return;
5959 }
5960
5961 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
5962
5963 l2cap_pass_to_tx(chan, control);
5964
5965 if (control->final) {
5966 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
5967 l2cap_retransmit_all(chan, control);
5968 } else {
5969 l2cap_retransmit_all(chan, control);
5970 l2cap_ertm_send(chan);
5971 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F)
5972 set_bit(CONN_REJ_ACT, &chan->conn_state);
5973 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005974}
5975
Mat Martineau4b51dae92012-05-17 20:53:37 -07005976static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq)
5977{
5978 BT_DBG("chan %p, txseq %d", chan, txseq);
5979
5980 BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq,
5981 chan->expected_tx_seq);
5982
5983 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
5984 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
Gustavo Padovan2d792812012-10-06 10:07:01 +01005985 chan->tx_win) {
Mat Martineau4b51dae92012-05-17 20:53:37 -07005986 /* See notes below regarding "double poll" and
5987 * invalid packets.
5988 */
5989 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
5990 BT_DBG("Invalid/Ignore - after SREJ");
5991 return L2CAP_TXSEQ_INVALID_IGNORE;
5992 } else {
5993 BT_DBG("Invalid - in window after SREJ sent");
5994 return L2CAP_TXSEQ_INVALID;
5995 }
5996 }
5997
5998 if (chan->srej_list.head == txseq) {
5999 BT_DBG("Expected SREJ");
6000 return L2CAP_TXSEQ_EXPECTED_SREJ;
6001 }
6002
6003 if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) {
6004 BT_DBG("Duplicate SREJ - txseq already stored");
6005 return L2CAP_TXSEQ_DUPLICATE_SREJ;
6006 }
6007
6008 if (l2cap_seq_list_contains(&chan->srej_list, txseq)) {
6009 BT_DBG("Unexpected SREJ - not requested");
6010 return L2CAP_TXSEQ_UNEXPECTED_SREJ;
6011 }
6012 }
6013
6014 if (chan->expected_tx_seq == txseq) {
6015 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
6016 chan->tx_win) {
6017 BT_DBG("Invalid - txseq outside tx window");
6018 return L2CAP_TXSEQ_INVALID;
6019 } else {
6020 BT_DBG("Expected");
6021 return L2CAP_TXSEQ_EXPECTED;
6022 }
6023 }
6024
6025 if (__seq_offset(chan, txseq, chan->last_acked_seq) <
Gustavo Padovan2d792812012-10-06 10:07:01 +01006026 __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) {
Mat Martineau4b51dae92012-05-17 20:53:37 -07006027 BT_DBG("Duplicate - expected_tx_seq later than txseq");
6028 return L2CAP_TXSEQ_DUPLICATE;
6029 }
6030
6031 if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) {
6032 /* A source of invalid packets is a "double poll" condition,
6033 * where delays cause us to send multiple poll packets. If
6034 * the remote stack receives and processes both polls,
6035 * sequence numbers can wrap around in such a way that a
6036 * resent frame has a sequence number that looks like new data
6037 * with a sequence gap. This would trigger an erroneous SREJ
6038 * request.
6039 *
6040 * Fortunately, this is impossible with a tx window that's
6041 * less than half of the maximum sequence number, which allows
6042 * invalid frames to be safely ignored.
6043 *
6044 * With tx window sizes greater than half of the tx window
6045 * maximum, the frame is invalid and cannot be ignored. This
6046 * causes a disconnect.
6047 */
6048
6049 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
6050 BT_DBG("Invalid/Ignore - txseq outside tx window");
6051 return L2CAP_TXSEQ_INVALID_IGNORE;
6052 } else {
6053 BT_DBG("Invalid - txseq outside tx window");
6054 return L2CAP_TXSEQ_INVALID;
6055 }
6056 } else {
6057 BT_DBG("Unexpected - txseq indicates missing frames");
6058 return L2CAP_TXSEQ_UNEXPECTED;
6059 }
6060}
6061
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006062static int l2cap_rx_state_recv(struct l2cap_chan *chan,
6063 struct l2cap_ctrl *control,
6064 struct sk_buff *skb, u8 event)
6065{
6066 int err = 0;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006067 bool skb_in_use = false;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006068
6069 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6070 event);
6071
6072 switch (event) {
6073 case L2CAP_EV_RECV_IFRAME:
6074 switch (l2cap_classify_txseq(chan, control->txseq)) {
6075 case L2CAP_TXSEQ_EXPECTED:
6076 l2cap_pass_to_tx(chan, control);
6077
6078 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6079 BT_DBG("Busy, discarding expected seq %d",
6080 control->txseq);
6081 break;
6082 }
6083
6084 chan->expected_tx_seq = __next_seq(chan,
6085 control->txseq);
6086
6087 chan->buffer_seq = chan->expected_tx_seq;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006088 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006089
6090 err = l2cap_reassemble_sdu(chan, skb, control);
6091 if (err)
6092 break;
6093
6094 if (control->final) {
6095 if (!test_and_clear_bit(CONN_REJ_ACT,
6096 &chan->conn_state)) {
6097 control->final = 0;
6098 l2cap_retransmit_all(chan, control);
6099 l2cap_ertm_send(chan);
6100 }
6101 }
6102
6103 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
6104 l2cap_send_ack(chan);
6105 break;
6106 case L2CAP_TXSEQ_UNEXPECTED:
6107 l2cap_pass_to_tx(chan, control);
6108
6109 /* Can't issue SREJ frames in the local busy state.
6110 * Drop this frame, it will be seen as missing
6111 * when local busy is exited.
6112 */
6113 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6114 BT_DBG("Busy, discarding unexpected seq %d",
6115 control->txseq);
6116 break;
6117 }
6118
6119 /* There was a gap in the sequence, so an SREJ
6120 * must be sent for each missing frame. The
6121 * current frame is stored for later use.
6122 */
6123 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006124 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006125 BT_DBG("Queued %p (queue len %d)", skb,
6126 skb_queue_len(&chan->srej_q));
6127
6128 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
6129 l2cap_seq_list_clear(&chan->srej_list);
6130 l2cap_send_srej(chan, control->txseq);
6131
6132 chan->rx_state = L2CAP_RX_STATE_SREJ_SENT;
6133 break;
6134 case L2CAP_TXSEQ_DUPLICATE:
6135 l2cap_pass_to_tx(chan, control);
6136 break;
6137 case L2CAP_TXSEQ_INVALID_IGNORE:
6138 break;
6139 case L2CAP_TXSEQ_INVALID:
6140 default:
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006141 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006142 break;
6143 }
6144 break;
6145 case L2CAP_EV_RECV_RR:
6146 l2cap_pass_to_tx(chan, control);
6147 if (control->final) {
6148 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6149
Mat Martineaue6a3ee62012-10-23 15:24:22 -07006150 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) &&
6151 !__chan_is_moving(chan)) {
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006152 control->final = 0;
6153 l2cap_retransmit_all(chan, control);
6154 }
6155
6156 l2cap_ertm_send(chan);
6157 } else if (control->poll) {
6158 l2cap_send_i_or_rr_or_rnr(chan);
6159 } else {
6160 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6161 &chan->conn_state) &&
6162 chan->unacked_frames)
6163 __set_retrans_timer(chan);
6164
6165 l2cap_ertm_send(chan);
6166 }
6167 break;
6168 case L2CAP_EV_RECV_RNR:
6169 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6170 l2cap_pass_to_tx(chan, control);
6171 if (control && control->poll) {
6172 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6173 l2cap_send_rr_or_rnr(chan, 0);
6174 }
6175 __clear_retrans_timer(chan);
6176 l2cap_seq_list_clear(&chan->retrans_list);
6177 break;
6178 case L2CAP_EV_RECV_REJ:
6179 l2cap_handle_rej(chan, control);
6180 break;
6181 case L2CAP_EV_RECV_SREJ:
6182 l2cap_handle_srej(chan, control);
6183 break;
6184 default:
6185 break;
6186 }
6187
6188 if (skb && !skb_in_use) {
6189 BT_DBG("Freeing %p", skb);
6190 kfree_skb(skb);
6191 }
6192
6193 return err;
6194}
6195
6196static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
6197 struct l2cap_ctrl *control,
6198 struct sk_buff *skb, u8 event)
6199{
6200 int err = 0;
6201 u16 txseq = control->txseq;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006202 bool skb_in_use = false;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006203
6204 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6205 event);
6206
6207 switch (event) {
6208 case L2CAP_EV_RECV_IFRAME:
6209 switch (l2cap_classify_txseq(chan, txseq)) {
6210 case L2CAP_TXSEQ_EXPECTED:
6211 /* Keep frame for reassembly later */
6212 l2cap_pass_to_tx(chan, control);
6213 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006214 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006215 BT_DBG("Queued %p (queue len %d)", skb,
6216 skb_queue_len(&chan->srej_q));
6217
6218 chan->expected_tx_seq = __next_seq(chan, txseq);
6219 break;
6220 case L2CAP_TXSEQ_EXPECTED_SREJ:
6221 l2cap_seq_list_pop(&chan->srej_list);
6222
6223 l2cap_pass_to_tx(chan, control);
6224 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006225 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006226 BT_DBG("Queued %p (queue len %d)", skb,
6227 skb_queue_len(&chan->srej_q));
6228
6229 err = l2cap_rx_queued_iframes(chan);
6230 if (err)
6231 break;
6232
6233 break;
6234 case L2CAP_TXSEQ_UNEXPECTED:
6235 /* Got a frame that can't be reassembled yet.
6236 * Save it for later, and send SREJs to cover
6237 * the missing frames.
6238 */
6239 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006240 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006241 BT_DBG("Queued %p (queue len %d)", skb,
6242 skb_queue_len(&chan->srej_q));
6243
6244 l2cap_pass_to_tx(chan, control);
6245 l2cap_send_srej(chan, control->txseq);
6246 break;
6247 case L2CAP_TXSEQ_UNEXPECTED_SREJ:
6248 /* This frame was requested with an SREJ, but
6249 * some expected retransmitted frames are
6250 * missing. Request retransmission of missing
6251 * SREJ'd frames.
6252 */
6253 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006254 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006255 BT_DBG("Queued %p (queue len %d)", skb,
6256 skb_queue_len(&chan->srej_q));
6257
6258 l2cap_pass_to_tx(chan, control);
6259 l2cap_send_srej_list(chan, control->txseq);
6260 break;
6261 case L2CAP_TXSEQ_DUPLICATE_SREJ:
6262 /* We've already queued this frame. Drop this copy. */
6263 l2cap_pass_to_tx(chan, control);
6264 break;
6265 case L2CAP_TXSEQ_DUPLICATE:
6266 /* Expecting a later sequence number, so this frame
6267 * was already received. Ignore it completely.
6268 */
6269 break;
6270 case L2CAP_TXSEQ_INVALID_IGNORE:
6271 break;
6272 case L2CAP_TXSEQ_INVALID:
6273 default:
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006274 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006275 break;
6276 }
6277 break;
6278 case L2CAP_EV_RECV_RR:
6279 l2cap_pass_to_tx(chan, control);
6280 if (control->final) {
6281 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6282
6283 if (!test_and_clear_bit(CONN_REJ_ACT,
6284 &chan->conn_state)) {
6285 control->final = 0;
6286 l2cap_retransmit_all(chan, control);
6287 }
6288
6289 l2cap_ertm_send(chan);
6290 } else if (control->poll) {
6291 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6292 &chan->conn_state) &&
6293 chan->unacked_frames) {
6294 __set_retrans_timer(chan);
6295 }
6296
6297 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6298 l2cap_send_srej_tail(chan);
6299 } else {
6300 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6301 &chan->conn_state) &&
6302 chan->unacked_frames)
6303 __set_retrans_timer(chan);
6304
6305 l2cap_send_ack(chan);
6306 }
6307 break;
6308 case L2CAP_EV_RECV_RNR:
6309 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6310 l2cap_pass_to_tx(chan, control);
6311 if (control->poll) {
6312 l2cap_send_srej_tail(chan);
6313 } else {
6314 struct l2cap_ctrl rr_control;
6315 memset(&rr_control, 0, sizeof(rr_control));
6316 rr_control.sframe = 1;
6317 rr_control.super = L2CAP_SUPER_RR;
6318 rr_control.reqseq = chan->buffer_seq;
6319 l2cap_send_sframe(chan, &rr_control);
6320 }
6321
6322 break;
6323 case L2CAP_EV_RECV_REJ:
6324 l2cap_handle_rej(chan, control);
6325 break;
6326 case L2CAP_EV_RECV_SREJ:
6327 l2cap_handle_srej(chan, control);
6328 break;
6329 }
6330
6331 if (skb && !skb_in_use) {
6332 BT_DBG("Freeing %p", skb);
6333 kfree_skb(skb);
6334 }
6335
6336 return err;
6337}
6338
Mat Martineau32b32732012-10-23 15:24:11 -07006339static int l2cap_finish_move(struct l2cap_chan *chan)
6340{
6341 BT_DBG("chan %p", chan);
6342
6343 chan->rx_state = L2CAP_RX_STATE_RECV;
6344
6345 if (chan->hs_hcon)
6346 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
6347 else
6348 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
6349
6350 return l2cap_resegment(chan);
6351}
6352
6353static int l2cap_rx_state_wait_p(struct l2cap_chan *chan,
6354 struct l2cap_ctrl *control,
6355 struct sk_buff *skb, u8 event)
6356{
6357 int err;
6358
6359 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6360 event);
6361
6362 if (!control->poll)
6363 return -EPROTO;
6364
6365 l2cap_process_reqseq(chan, control->reqseq);
6366
6367 if (!skb_queue_empty(&chan->tx_q))
6368 chan->tx_send_head = skb_peek(&chan->tx_q);
6369 else
6370 chan->tx_send_head = NULL;
6371
6372 /* Rewind next_tx_seq to the point expected
6373 * by the receiver.
6374 */
6375 chan->next_tx_seq = control->reqseq;
6376 chan->unacked_frames = 0;
6377
6378 err = l2cap_finish_move(chan);
6379 if (err)
6380 return err;
6381
6382 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6383 l2cap_send_i_or_rr_or_rnr(chan);
6384
6385 if (event == L2CAP_EV_RECV_IFRAME)
6386 return -EPROTO;
6387
6388 return l2cap_rx_state_recv(chan, control, NULL, event);
6389}
6390
6391static int l2cap_rx_state_wait_f(struct l2cap_chan *chan,
6392 struct l2cap_ctrl *control,
6393 struct sk_buff *skb, u8 event)
6394{
6395 int err;
6396
6397 if (!control->final)
6398 return -EPROTO;
6399
6400 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6401
6402 chan->rx_state = L2CAP_RX_STATE_RECV;
6403 l2cap_process_reqseq(chan, control->reqseq);
6404
6405 if (!skb_queue_empty(&chan->tx_q))
6406 chan->tx_send_head = skb_peek(&chan->tx_q);
6407 else
6408 chan->tx_send_head = NULL;
6409
6410 /* Rewind next_tx_seq to the point expected
6411 * by the receiver.
6412 */
6413 chan->next_tx_seq = control->reqseq;
6414 chan->unacked_frames = 0;
6415
6416 if (chan->hs_hcon)
6417 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
6418 else
6419 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
6420
6421 err = l2cap_resegment(chan);
6422
6423 if (!err)
6424 err = l2cap_rx_state_recv(chan, control, skb, event);
6425
6426 return err;
6427}
6428
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006429static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq)
6430{
6431 /* Make sure reqseq is for a packet that has been sent but not acked */
6432 u16 unacked;
6433
6434 unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq);
6435 return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked;
6436}
6437
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006438static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6439 struct sk_buff *skb, u8 event)
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006440{
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006441 int err = 0;
6442
6443 BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan,
6444 control, skb, event, chan->rx_state);
6445
6446 if (__valid_reqseq(chan, control->reqseq)) {
6447 switch (chan->rx_state) {
6448 case L2CAP_RX_STATE_RECV:
6449 err = l2cap_rx_state_recv(chan, control, skb, event);
6450 break;
6451 case L2CAP_RX_STATE_SREJ_SENT:
6452 err = l2cap_rx_state_srej_sent(chan, control, skb,
6453 event);
6454 break;
Mat Martineau32b32732012-10-23 15:24:11 -07006455 case L2CAP_RX_STATE_WAIT_P:
6456 err = l2cap_rx_state_wait_p(chan, control, skb, event);
6457 break;
6458 case L2CAP_RX_STATE_WAIT_F:
6459 err = l2cap_rx_state_wait_f(chan, control, skb, event);
6460 break;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006461 default:
6462 /* shut it down */
6463 break;
6464 }
6465 } else {
6466 BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d",
6467 control->reqseq, chan->next_tx_seq,
6468 chan->expected_ack_seq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006469 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006470 }
6471
6472 return err;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006473}
6474
6475static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6476 struct sk_buff *skb)
6477{
Mat Martineau4b51dae92012-05-17 20:53:37 -07006478 int err = 0;
6479
6480 BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb,
6481 chan->rx_state);
6482
6483 if (l2cap_classify_txseq(chan, control->txseq) ==
6484 L2CAP_TXSEQ_EXPECTED) {
6485 l2cap_pass_to_tx(chan, control);
6486
6487 BT_DBG("buffer_seq %d->%d", chan->buffer_seq,
6488 __next_seq(chan, chan->buffer_seq));
6489
6490 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
6491
6492 l2cap_reassemble_sdu(chan, skb, control);
6493 } else {
6494 if (chan->sdu) {
6495 kfree_skb(chan->sdu);
6496 chan->sdu = NULL;
6497 }
6498 chan->sdu_last_frag = NULL;
6499 chan->sdu_len = 0;
6500
6501 if (skb) {
6502 BT_DBG("Freeing %p", skb);
6503 kfree_skb(skb);
6504 }
6505 }
6506
6507 chan->last_acked_seq = control->txseq;
6508 chan->expected_tx_seq = __next_seq(chan, control->txseq);
6509
6510 return err;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006511}
6512
6513static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
6514{
6515 struct l2cap_ctrl *control = &bt_cb(skb)->control;
6516 u16 len;
6517 u8 event;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006518
Mat Martineaub76bbd62012-04-11 10:48:43 -07006519 __unpack_control(chan, skb);
6520
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006521 len = skb->len;
6522
6523 /*
6524 * We can just drop the corrupted I-frame here.
6525 * Receiver will miss it and start proper recovery
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006526 * procedures and ask for retransmission.
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006527 */
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006528 if (l2cap_check_fcs(chan, skb))
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006529 goto drop;
6530
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006531 if (!control->sframe && control->sar == L2CAP_SAR_START)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03006532 len -= L2CAP_SDULEN_SIZE;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006533
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006534 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03006535 len -= L2CAP_FCS_SIZE;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006536
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006537 if (len > chan->mps) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006538 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006539 goto drop;
6540 }
6541
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006542 if (!control->sframe) {
6543 int err;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006544
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006545 BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d",
6546 control->sar, control->reqseq, control->final,
6547 control->txseq);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03006548
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006549 /* Validate F-bit - F=0 always valid, F=1 only
6550 * valid in TX WAIT_F
6551 */
6552 if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F)
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006553 goto drop;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006554
6555 if (chan->mode != L2CAP_MODE_STREAMING) {
6556 event = L2CAP_EV_RECV_IFRAME;
6557 err = l2cap_rx(chan, control, skb, event);
6558 } else {
6559 err = l2cap_stream_rx(chan, control, skb);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006560 }
6561
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006562 if (err)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006563 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006564 } else {
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006565 const u8 rx_func_to_event[4] = {
6566 L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ,
6567 L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ
6568 };
6569
6570 /* Only I-frames are expected in streaming mode */
6571 if (chan->mode == L2CAP_MODE_STREAMING)
6572 goto drop;
6573
6574 BT_DBG("sframe reqseq %d, final %d, poll %d, super %d",
6575 control->reqseq, control->final, control->poll,
6576 control->super);
6577
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006578 if (len != 0) {
Andrei Emeltchenko1bb166e2012-11-20 17:16:21 +02006579 BT_ERR("Trailing bytes: %d in sframe", len);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006580 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006581 goto drop;
6582 }
6583
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006584 /* Validate F and P bits */
6585 if (control->final && (control->poll ||
6586 chan->tx_state != L2CAP_TX_STATE_WAIT_F))
6587 goto drop;
6588
6589 event = rx_func_to_event[control->super];
6590 if (l2cap_rx(chan, control, skb, event))
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006591 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006592 }
6593
6594 return 0;
6595
6596drop:
6597 kfree_skb(skb);
6598 return 0;
6599}
6600
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006601static void l2cap_chan_le_send_credits(struct l2cap_chan *chan)
6602{
6603 struct l2cap_conn *conn = chan->conn;
6604 struct l2cap_le_credits pkt;
6605 u16 return_credits;
6606
6607 /* We return more credits to the sender only after the amount of
6608 * credits falls below half of the initial amount.
6609 */
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02006610 if (chan->rx_credits >= (le_max_credits + 1) / 2)
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006611 return;
6612
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02006613 return_credits = le_max_credits - chan->rx_credits;
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006614
6615 BT_DBG("chan %p returning %u credits to sender", chan, return_credits);
6616
6617 chan->rx_credits += return_credits;
6618
6619 pkt.cid = cpu_to_le16(chan->scid);
6620 pkt.credits = cpu_to_le16(return_credits);
6621
6622 chan->ident = l2cap_get_ident(conn);
6623
6624 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt);
6625}
6626
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006627static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
6628{
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006629 int err;
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006630
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006631 if (!chan->rx_credits) {
6632 BT_ERR("No credits to receive LE L2CAP data");
Johan Hedbergdfd97742014-01-27 15:11:34 -08006633 l2cap_send_disconn_req(chan, ECONNRESET);
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006634 return -ENOBUFS;
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006635 }
6636
6637 if (chan->imtu < skb->len) {
6638 BT_ERR("Too big LE L2CAP PDU");
6639 return -ENOBUFS;
6640 }
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006641
6642 chan->rx_credits--;
6643 BT_DBG("rx_credits %u -> %u", chan->rx_credits + 1, chan->rx_credits);
6644
6645 l2cap_chan_le_send_credits(chan);
6646
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006647 err = 0;
6648
6649 if (!chan->sdu) {
6650 u16 sdu_len;
6651
6652 sdu_len = get_unaligned_le16(skb->data);
6653 skb_pull(skb, L2CAP_SDULEN_SIZE);
6654
6655 BT_DBG("Start of new SDU. sdu_len %u skb->len %u imtu %u",
6656 sdu_len, skb->len, chan->imtu);
6657
6658 if (sdu_len > chan->imtu) {
6659 BT_ERR("Too big LE L2CAP SDU length received");
6660 err = -EMSGSIZE;
6661 goto failed;
6662 }
6663
6664 if (skb->len > sdu_len) {
6665 BT_ERR("Too much LE L2CAP data received");
6666 err = -EINVAL;
6667 goto failed;
6668 }
6669
6670 if (skb->len == sdu_len)
6671 return chan->ops->recv(chan, skb);
6672
6673 chan->sdu = skb;
6674 chan->sdu_len = sdu_len;
6675 chan->sdu_last_frag = skb;
6676
6677 return 0;
6678 }
6679
6680 BT_DBG("SDU fragment. chan->sdu->len %u skb->len %u chan->sdu_len %u",
6681 chan->sdu->len, skb->len, chan->sdu_len);
6682
6683 if (chan->sdu->len + skb->len > chan->sdu_len) {
6684 BT_ERR("Too much LE L2CAP data received");
6685 err = -EINVAL;
6686 goto failed;
6687 }
6688
6689 append_skb_frag(chan->sdu, skb, &chan->sdu_last_frag);
6690 skb = NULL;
6691
6692 if (chan->sdu->len == chan->sdu_len) {
6693 err = chan->ops->recv(chan, chan->sdu);
6694 if (!err) {
6695 chan->sdu = NULL;
6696 chan->sdu_last_frag = NULL;
6697 chan->sdu_len = 0;
6698 }
6699 }
6700
6701failed:
6702 if (err) {
6703 kfree_skb(skb);
6704 kfree_skb(chan->sdu);
6705 chan->sdu = NULL;
6706 chan->sdu_last_frag = NULL;
6707 chan->sdu_len = 0;
6708 }
6709
6710 /* We can't return an error here since we took care of the skb
6711 * freeing internally. An error return would cause the caller to
6712 * do a double-free of the skb.
6713 */
6714 return 0;
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006715}
6716
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006717static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid,
6718 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006720 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03006722 chan = l2cap_get_chan_by_scid(conn, cid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006723 if (!chan) {
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006724 if (cid == L2CAP_CID_A2MP) {
6725 chan = a2mp_channel_create(conn, skb);
6726 if (!chan) {
6727 kfree_skb(skb);
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006728 return;
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006729 }
6730
6731 l2cap_chan_lock(chan);
6732 } else {
6733 BT_DBG("unknown cid 0x%4.4x", cid);
6734 /* Drop packet and return */
6735 kfree_skb(skb);
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006736 return;
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 }
6739
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03006740 BT_DBG("chan %p, len %d", chan, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03006742 if (chan->state != BT_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 goto drop;
6744
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006745 switch (chan->mode) {
Johan Hedberg38319712013-05-17 12:49:23 +03006746 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedbergfad5fc82013-12-05 09:45:01 +02006747 if (l2cap_le_data_rcv(chan, skb) < 0)
6748 goto drop;
6749
6750 goto done;
6751
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006752 case L2CAP_MODE_BASIC:
6753 /* If socket recv buffers overflows we drop data here
6754 * which is *bad* because L2CAP has to be reliable.
6755 * But we don't have any other choice. L2CAP doesn't
6756 * provide flow control mechanism. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757
Szymon Janc2c96e032014-02-18 20:48:34 +01006758 if (chan->imtu < skb->len) {
6759 BT_ERR("Dropping L2CAP data: receive buffer overflow");
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006760 goto drop;
Szymon Janc2c96e032014-02-18 20:48:34 +01006761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762
Gustavo Padovan80b98022012-05-27 22:27:51 -03006763 if (!chan->ops->recv(chan, skb))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006764 goto done;
6765 break;
6766
6767 case L2CAP_MODE_ERTM:
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03006768 case L2CAP_MODE_STREAMING:
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006769 l2cap_data_rcv(chan, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03006770 goto done;
6771
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006772 default:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006773 BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006774 break;
6775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776
6777drop:
6778 kfree_skb(skb);
6779
6780done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006781 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782}
6783
Andrei Emeltchenko84104b22012-05-31 11:18:56 +03006784static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
6785 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786{
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006787 struct hci_conn *hcon = conn->hcon;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006788 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006790 if (hcon->type != ACL_LINK)
Johan Hedberga24cce12014-08-07 22:56:42 +03006791 goto free_skb;
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006792
Johan Hedbergbf20fd42013-05-14 13:23:13 +03006793 chan = l2cap_global_chan_by_psm(0, psm, &hcon->src, &hcon->dst,
6794 ACL_LINK);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006795 if (!chan)
Johan Hedberga24cce12014-08-07 22:56:42 +03006796 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797
Andrei Emeltchenko5b4ceda2012-02-24 16:35:32 +02006798 BT_DBG("chan %p, len %d", chan, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03006800 if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 goto drop;
6802
Vinicius Costa Gomese13e21d2011-06-17 22:46:27 -03006803 if (chan->imtu < skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 goto drop;
6805
Marcel Holtmann2edf8702013-10-13 12:55:29 -07006806 /* Store remote BD_ADDR and PSM for msg_name */
Marcel Holtmann06ae3312013-10-18 03:43:00 -07006807 bacpy(&bt_cb(skb)->bdaddr, &hcon->dst);
Marcel Holtmann2edf8702013-10-13 12:55:29 -07006808 bt_cb(skb)->psm = psm;
6809
Johan Hedberga24cce12014-08-07 22:56:42 +03006810 if (!chan->ops->recv(chan, skb)) {
6811 l2cap_chan_put(chan);
Andrei Emeltchenko84104b22012-05-31 11:18:56 +03006812 return;
Johan Hedberga24cce12014-08-07 22:56:42 +03006813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814
6815drop:
Johan Hedberga24cce12014-08-07 22:56:42 +03006816 l2cap_chan_put(chan);
6817free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819}
6820
6821static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
6822{
6823 struct l2cap_hdr *lh = (void *) skb->data;
Johan Hedberg61a939c2014-01-17 20:45:11 +02006824 struct hci_conn *hcon = conn->hcon;
Al Viro8e036fc2007-07-29 00:16:36 -07006825 u16 cid, len;
6826 __le16 psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827
Johan Hedberg61a939c2014-01-17 20:45:11 +02006828 if (hcon->state != BT_CONNECTED) {
6829 BT_DBG("queueing pending rx skb");
6830 skb_queue_tail(&conn->pending_rx, skb);
6831 return;
6832 }
6833
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 skb_pull(skb, L2CAP_HDR_SIZE);
6835 cid = __le16_to_cpu(lh->cid);
6836 len = __le16_to_cpu(lh->len);
6837
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006838 if (len != skb->len) {
6839 kfree_skb(skb);
6840 return;
6841 }
6842
Johan Hedberg9e1d7e12014-07-06 11:03:36 +03006843 /* Since we can't actively block incoming LE connections we must
6844 * at least ensure that we ignore incoming data from them.
6845 */
6846 if (hcon->type == LE_LINK &&
Johan Hedbergdcc36c12014-07-09 12:59:13 +03006847 hci_bdaddr_list_lookup(&hcon->hdev->blacklist, &hcon->dst,
6848 bdaddr_type(hcon, hcon->dst_type))) {
Johan Hedberge4931502014-07-02 09:36:21 +03006849 kfree_skb(skb);
6850 return;
6851 }
6852
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 BT_DBG("len %d, cid 0x%4.4x", len, cid);
6854
6855 switch (cid) {
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -03006856 case L2CAP_CID_SIGNALING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 l2cap_sig_channel(conn, skb);
6858 break;
6859
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -03006860 case L2CAP_CID_CONN_LESS:
Andrei Emeltchenko097db762012-03-09 14:16:17 +02006861 psm = get_unaligned((__le16 *) skb->data);
Andrei Emeltchenko0181a702012-05-29 10:04:05 +03006862 skb_pull(skb, L2CAP_PSMLEN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 l2cap_conless_channel(conn, psm, skb);
6864 break;
6865
Marcel Holtmanna2877622013-10-02 23:46:54 -07006866 case L2CAP_CID_LE_SIGNALING:
6867 l2cap_le_sig_channel(conn, skb);
6868 break;
6869
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 default:
6871 l2cap_data_channel(conn, cid, skb);
6872 break;
6873 }
6874}
6875
Johan Hedberg61a939c2014-01-17 20:45:11 +02006876static void process_pending_rx(struct work_struct *work)
6877{
6878 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
6879 pending_rx_work);
6880 struct sk_buff *skb;
6881
6882 BT_DBG("");
6883
6884 while ((skb = skb_dequeue(&conn->pending_rx)))
6885 l2cap_recv_frame(conn, skb);
6886}
6887
Johan Hedberg162b49e2014-01-17 20:45:10 +02006888static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon)
6889{
6890 struct l2cap_conn *conn = hcon->l2cap_data;
6891 struct hci_chan *hchan;
6892
6893 if (conn)
6894 return conn;
6895
6896 hchan = hci_chan_create(hcon);
6897 if (!hchan)
6898 return NULL;
6899
Johan Hedberg27f70f32014-07-21 10:50:06 +03006900 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
Johan Hedberg162b49e2014-01-17 20:45:10 +02006901 if (!conn) {
6902 hci_chan_del(hchan);
6903 return NULL;
6904 }
6905
6906 kref_init(&conn->ref);
6907 hcon->l2cap_data = conn;
6908 conn->hcon = hcon;
6909 hci_conn_get(conn->hcon);
6910 conn->hchan = hchan;
6911
6912 BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan);
6913
6914 switch (hcon->type) {
6915 case LE_LINK:
6916 if (hcon->hdev->le_mtu) {
6917 conn->mtu = hcon->hdev->le_mtu;
6918 break;
6919 }
6920 /* fall through */
6921 default:
6922 conn->mtu = hcon->hdev->acl_mtu;
6923 break;
6924 }
6925
6926 conn->feat_mask = 0;
6927
6928 if (hcon->type == ACL_LINK)
6929 conn->hs_enabled = test_bit(HCI_HS_ENABLED,
6930 &hcon->hdev->dev_flags);
6931
Marcel Holtmann5a54e7c2014-07-13 20:50:15 +02006932 mutex_init(&conn->ident_lock);
Johan Hedberg162b49e2014-01-17 20:45:10 +02006933 mutex_init(&conn->chan_lock);
6934
6935 INIT_LIST_HEAD(&conn->chan_l);
6936 INIT_LIST_HEAD(&conn->users);
6937
Johan Hedberg276d8072014-08-11 22:06:41 +03006938 INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout);
Johan Hedberg162b49e2014-01-17 20:45:10 +02006939
Johan Hedbergdec5b492014-08-11 22:06:37 +03006940 INIT_WORK(&conn->disconn_work, disconn_work);
6941
Johan Hedberg61a939c2014-01-17 20:45:11 +02006942 skb_queue_head_init(&conn->pending_rx);
6943 INIT_WORK(&conn->pending_rx_work, process_pending_rx);
6944
Johan Hedberg162b49e2014-01-17 20:45:10 +02006945 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
6946
6947 return conn;
6948}
6949
6950static bool is_valid_psm(u16 psm, u8 dst_type) {
6951 if (!psm)
6952 return false;
6953
6954 if (bdaddr_type_is_le(dst_type))
6955 return (psm <= 0x00ff);
6956
6957 /* PSM must be odd and lsb of upper byte must be 0 */
6958 return ((psm & 0x0101) == 0x0001);
6959}
6960
6961int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
6962 bdaddr_t *dst, u8 dst_type)
6963{
6964 struct l2cap_conn *conn;
6965 struct hci_conn *hcon;
6966 struct hci_dev *hdev;
Johan Hedberg162b49e2014-01-17 20:45:10 +02006967 int err;
6968
6969 BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", &chan->src, dst,
6970 dst_type, __le16_to_cpu(psm));
6971
6972 hdev = hci_get_route(dst, &chan->src);
6973 if (!hdev)
6974 return -EHOSTUNREACH;
6975
6976 hci_dev_lock(hdev);
6977
6978 l2cap_chan_lock(chan);
6979
6980 if (!is_valid_psm(__le16_to_cpu(psm), dst_type) && !cid &&
6981 chan->chan_type != L2CAP_CHAN_RAW) {
6982 err = -EINVAL;
6983 goto done;
6984 }
6985
Johan Hedberg21626e62014-01-24 10:35:41 +02006986 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !psm) {
6987 err = -EINVAL;
6988 goto done;
6989 }
6990
6991 if (chan->chan_type == L2CAP_CHAN_FIXED && !cid) {
Johan Hedberg162b49e2014-01-17 20:45:10 +02006992 err = -EINVAL;
6993 goto done;
6994 }
6995
6996 switch (chan->mode) {
6997 case L2CAP_MODE_BASIC:
6998 break;
6999 case L2CAP_MODE_LE_FLOWCTL:
7000 l2cap_le_flowctl_init(chan);
7001 break;
7002 case L2CAP_MODE_ERTM:
7003 case L2CAP_MODE_STREAMING:
7004 if (!disable_ertm)
7005 break;
7006 /* fall through */
7007 default:
Johan Hedbergbeb19e42014-07-18 11:15:26 +03007008 err = -EOPNOTSUPP;
Johan Hedberg162b49e2014-01-17 20:45:10 +02007009 goto done;
7010 }
7011
7012 switch (chan->state) {
7013 case BT_CONNECT:
7014 case BT_CONNECT2:
7015 case BT_CONFIG:
7016 /* Already connecting */
7017 err = 0;
7018 goto done;
7019
7020 case BT_CONNECTED:
7021 /* Already connected */
7022 err = -EISCONN;
7023 goto done;
7024
7025 case BT_OPEN:
7026 case BT_BOUND:
7027 /* Can connect */
7028 break;
7029
7030 default:
7031 err = -EBADFD;
7032 goto done;
7033 }
7034
7035 /* Set destination address and psm */
7036 bacpy(&chan->dst, dst);
7037 chan->dst_type = dst_type;
7038
7039 chan->psm = psm;
7040 chan->dcid = cid;
7041
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007042 if (bdaddr_type_is_le(dst_type)) {
Johan Hedberge804d252014-07-16 11:42:28 +03007043 u8 role;
Johan Hedbergcdd62752014-07-07 15:02:28 +03007044
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007045 /* Convert from L2CAP channel address type to HCI address type
7046 */
7047 if (dst_type == BDADDR_LE_PUBLIC)
7048 dst_type = ADDR_LE_DEV_PUBLIC;
7049 else
7050 dst_type = ADDR_LE_DEV_RANDOM;
7051
Johan Hedberge804d252014-07-16 11:42:28 +03007052 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
7053 role = HCI_ROLE_SLAVE;
7054 else
7055 role = HCI_ROLE_MASTER;
Johan Hedbergcdd62752014-07-07 15:02:28 +03007056
Andre Guedes04a6c582014-02-26 20:21:44 -03007057 hcon = hci_connect_le(hdev, dst, dst_type, chan->sec_level,
Johan Hedberge804d252014-07-16 11:42:28 +03007058 HCI_LE_CONN_TIMEOUT, role);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007059 } else {
Johan Hedbergd93375a2014-07-07 15:02:27 +03007060 u8 auth_type = l2cap_get_auth_type(chan);
Andre Guedes04a6c582014-02-26 20:21:44 -03007061 hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007062 }
Johan Hedberg162b49e2014-01-17 20:45:10 +02007063
7064 if (IS_ERR(hcon)) {
7065 err = PTR_ERR(hcon);
7066 goto done;
7067 }
7068
7069 conn = l2cap_conn_add(hcon);
7070 if (!conn) {
7071 hci_conn_drop(hcon);
7072 err = -ENOMEM;
7073 goto done;
7074 }
7075
7076 if (cid && __l2cap_get_chan_by_dcid(conn, cid)) {
7077 hci_conn_drop(hcon);
7078 err = -EBUSY;
7079 goto done;
7080 }
7081
7082 /* Update source addr of the socket */
7083 bacpy(&chan->src, &hcon->src);
7084 chan->src_type = bdaddr_type(hcon, hcon->src_type);
7085
7086 l2cap_chan_unlock(chan);
7087 l2cap_chan_add(conn, chan);
7088 l2cap_chan_lock(chan);
7089
7090 /* l2cap_chan_add takes its own ref so we can drop this one */
7091 hci_conn_drop(hcon);
7092
7093 l2cap_state_change(chan, BT_CONNECT);
7094 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
7095
Johan Hedberg61202e42014-01-28 15:16:48 -08007096 /* Release chan->sport so that it can be reused by other
7097 * sockets (as it's only used for listening sockets).
7098 */
7099 write_lock(&chan_list_lock);
7100 chan->sport = 0;
7101 write_unlock(&chan_list_lock);
7102
Johan Hedberg162b49e2014-01-17 20:45:10 +02007103 if (hcon->state == BT_CONNECTED) {
7104 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
7105 __clear_chan_timer(chan);
Johan Hedberge7cafc42014-07-17 15:35:38 +03007106 if (l2cap_chan_check_security(chan, true))
Johan Hedberg162b49e2014-01-17 20:45:10 +02007107 l2cap_state_change(chan, BT_CONNECTED);
7108 } else
7109 l2cap_do_start(chan);
7110 }
7111
7112 err = 0;
7113
7114done:
7115 l2cap_chan_unlock(chan);
7116 hci_dev_unlock(hdev);
7117 hci_dev_put(hdev);
7118 return err;
7119}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03007120EXPORT_SYMBOL_GPL(l2cap_chan_connect);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007121
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122/* ---- L2CAP interface with lower layer (HCI) ---- */
7123
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007124int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125{
7126 int exact = 0, lm1 = 0, lm2 = 0;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007127 struct l2cap_chan *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03007129 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130
7131 /* Find listening sockets and check their link_mode */
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007132 read_lock(&chan_list_lock);
7133 list_for_each_entry(c, &chan_list, global_l) {
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03007134 if (c->state != BT_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 continue;
7136
Marcel Holtmann7eafc592013-10-13 08:12:47 -07007137 if (!bacmp(&c->src, &hdev->bdaddr)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007138 lm1 |= HCI_LM_ACCEPT;
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +03007139 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007140 lm1 |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 exact++;
Marcel Holtmann7eafc592013-10-13 08:12:47 -07007142 } else if (!bacmp(&c->src, BDADDR_ANY)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007143 lm2 |= HCI_LM_ACCEPT;
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +03007144 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007145 lm2 |= HCI_LM_MASTER;
7146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 }
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007148 read_unlock(&chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
7150 return exact ? lm1 : lm2;
7151}
7152
Johan Hedberge760ec12014-08-07 22:56:47 +03007153/* Find the next fixed channel in BT_LISTEN state, continue iteration
7154 * from an existing channel in the list or from the beginning of the
7155 * global list (by passing NULL as first parameter).
7156 */
7157static struct l2cap_chan *l2cap_global_fixed_chan(struct l2cap_chan *c,
Johan Hedberg54a1b622014-08-07 22:56:48 +03007158 bdaddr_t *src, u8 link_type)
Johan Hedberge760ec12014-08-07 22:56:47 +03007159{
7160 read_lock(&chan_list_lock);
7161
7162 if (c)
7163 c = list_next_entry(c, global_l);
7164 else
7165 c = list_entry(chan_list.next, typeof(*c), global_l);
7166
7167 list_for_each_entry_from(c, &chan_list, global_l) {
7168 if (c->chan_type != L2CAP_CHAN_FIXED)
7169 continue;
7170 if (c->state != BT_LISTEN)
7171 continue;
7172 if (bacmp(&c->src, src) && bacmp(&c->src, BDADDR_ANY))
7173 continue;
Johan Hedberg54a1b622014-08-07 22:56:48 +03007174 if (link_type == ACL_LINK && c->src_type != BDADDR_BREDR)
7175 continue;
7176 if (link_type == LE_LINK && c->src_type == BDADDR_BREDR)
7177 continue;
Johan Hedberge760ec12014-08-07 22:56:47 +03007178
7179 l2cap_chan_hold(c);
7180 read_unlock(&chan_list_lock);
7181 return c;
7182 }
7183
7184 read_unlock(&chan_list_lock);
7185
7186 return NULL;
7187}
7188
Andrei Emeltchenko9e664632012-07-24 16:06:15 +03007189void l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190{
Johan Hedberge760ec12014-08-07 22:56:47 +03007191 struct hci_dev *hdev = hcon->hdev;
Marcel Holtmann01394182006-07-03 10:02:46 +02007192 struct l2cap_conn *conn;
Johan Hedberge760ec12014-08-07 22:56:47 +03007193 struct l2cap_chan *pchan;
7194 u8 dst_type;
Marcel Holtmann01394182006-07-03 10:02:46 +02007195
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03007196 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007198 if (status) {
Joe Perchese1750722011-06-29 18:18:29 -07007199 l2cap_conn_del(hcon, bt_to_errno(status));
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007200 return;
Andrei Emeltchenkoba6fc312012-10-31 15:46:26 +02007201 }
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007202
7203 conn = l2cap_conn_add(hcon);
7204 if (!conn)
7205 return;
7206
Johan Hedberge760ec12014-08-07 22:56:47 +03007207 dst_type = bdaddr_type(hcon, hcon->dst_type);
7208
7209 /* If device is blocked, do not create channels for it */
7210 if (hci_bdaddr_list_lookup(&hdev->blacklist, &hcon->dst, dst_type))
7211 return;
7212
7213 /* Find fixed channels and notify them of the new connection. We
7214 * use multiple individual lookups, continuing each time where
7215 * we left off, because the list lock would prevent calling the
7216 * potentially sleeping l2cap_chan_lock() function.
7217 */
Johan Hedberg54a1b622014-08-07 22:56:48 +03007218 pchan = l2cap_global_fixed_chan(NULL, &hdev->bdaddr, hcon->type);
Johan Hedberge760ec12014-08-07 22:56:47 +03007219 while (pchan) {
7220 struct l2cap_chan *chan, *next;
7221
7222 /* Client fixed channels should override server ones */
7223 if (__l2cap_get_chan_by_dcid(conn, pchan->scid))
7224 goto next;
7225
7226 l2cap_chan_lock(pchan);
7227 chan = pchan->ops->new_connection(pchan);
7228 if (chan) {
7229 bacpy(&chan->src, &hcon->src);
7230 bacpy(&chan->dst, &hcon->dst);
7231 chan->src_type = bdaddr_type(hcon, hcon->src_type);
7232 chan->dst_type = dst_type;
7233
7234 __l2cap_chan_add(conn, chan);
7235 }
7236
7237 l2cap_chan_unlock(pchan);
7238next:
Johan Hedberg54a1b622014-08-07 22:56:48 +03007239 next = l2cap_global_fixed_chan(pchan, &hdev->bdaddr,
7240 hcon->type);
Johan Hedberge760ec12014-08-07 22:56:47 +03007241 l2cap_chan_put(pchan);
7242 pchan = next;
7243 }
7244
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007245 l2cap_conn_ready(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246}
7247
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007248int l2cap_disconn_ind(struct hci_conn *hcon)
Marcel Holtmann2950f212009-02-12 14:02:50 +01007249{
7250 struct l2cap_conn *conn = hcon->l2cap_data;
7251
7252 BT_DBG("hcon %p", hcon);
7253
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007254 if (!conn)
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02007255 return HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +01007256 return conn->disc_reason;
7257}
7258
Andrei Emeltchenko9e664632012-07-24 16:06:15 +03007259void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260{
7261 BT_DBG("hcon %p reason %d", hcon, reason);
7262
Joe Perchese1750722011-06-29 18:18:29 -07007263 l2cap_conn_del(hcon, bt_to_errno(reason));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264}
7265
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007266static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007267{
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03007268 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
Marcel Holtmann255c7602009-02-04 21:07:19 +01007269 return;
7270
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007271 if (encrypt == 0x00) {
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007272 if (chan->sec_level == BT_SECURITY_MEDIUM) {
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007273 __set_chan_timer(chan, L2CAP_ENC_TIMEOUT);
Marcel Holtmann7d513e92014-01-15 22:37:40 -08007274 } else if (chan->sec_level == BT_SECURITY_HIGH ||
7275 chan->sec_level == BT_SECURITY_FIPS)
Gustavo F. Padovan0f852722011-05-04 19:42:50 -03007276 l2cap_chan_close(chan, ECONNREFUSED);
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007277 } else {
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007278 if (chan->sec_level == BT_SECURITY_MEDIUM)
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -03007279 __clear_chan_timer(chan);
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007280 }
7281}
7282
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007283int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284{
Marcel Holtmann40be4922008-07-14 20:13:50 +02007285 struct l2cap_conn *conn = hcon->l2cap_data;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03007286 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287
Marcel Holtmann01394182006-07-03 10:02:46 +02007288 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289 return 0;
Marcel Holtmann01394182006-07-03 10:02:46 +02007290
Andrei Emeltchenko89d8b402012-07-10 15:27:51 +03007291 BT_DBG("conn %p status 0x%2.2x encrypt %u", conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292
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 Hedberg191eb392014-08-07 22:56:45 +03007306 if (!status && encrypt)
7307 chan->sec_level = hcon->sec_level;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007308
Andrei Emeltchenko96eff462012-11-15 18:14:53 +02007309 if (!__l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007310 l2cap_chan_unlock(chan);
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01007311 continue;
7312 }
7313
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03007314 if (!status && (chan->state == BT_CONNECTED ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01007315 chan->state == BT_CONFIG)) {
Marcel Holtmannd97c8992013-10-14 02:53:54 -07007316 chan->ops->resume(chan);
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007317 l2cap_check_encryption(chan, encrypt);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007318 l2cap_chan_unlock(chan);
Marcel Holtmann9719f8a2008-07-14 20:13:45 +02007319 continue;
7320 }
7321
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03007322 if (chan->state == BT_CONNECT) {
Johan Hedberg6d3c15d2013-12-02 22:13:24 +02007323 if (!status)
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03007324 l2cap_start_connection(chan);
Johan Hedberg6d3c15d2013-12-02 22:13:24 +02007325 else
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007326 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
Gustavo F. Padovan89bc500e2011-06-03 00:19:47 -03007327 } else if (chan->state == BT_CONNECT2) {
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007328 struct l2cap_conn_rsp rsp;
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007329 __u16 res, stat;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007330
7331 if (!status) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07007332 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007333 res = L2CAP_CR_PEND;
7334 stat = L2CAP_CS_AUTHOR_PEND;
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08007335 chan->ops->defer(chan);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007336 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02007337 l2cap_state_change(chan, BT_CONFIG);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007338 res = L2CAP_CR_SUCCESS;
7339 stat = L2CAP_CS_NO_INFO;
7340 }
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007341 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02007342 l2cap_state_change(chan, BT_DISCONN);
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007343 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007344 res = L2CAP_CR_SEC_BLOCK;
7345 stat = L2CAP_CS_NO_INFO;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007346 }
7347
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03007348 rsp.scid = cpu_to_le16(chan->dcid);
7349 rsp.dcid = cpu_to_le16(chan->scid);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007350 rsp.result = cpu_to_le16(res);
7351 rsp.status = cpu_to_le16(stat);
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03007352 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01007353 sizeof(rsp), &rsp);
Mat Martineau2d369352012-05-23 14:59:30 -07007354
7355 if (!test_bit(CONF_REQ_SENT, &chan->conf_state) &&
7356 res == L2CAP_CR_SUCCESS) {
7357 char buf[128];
7358 set_bit(CONF_REQ_SENT, &chan->conf_state);
7359 l2cap_send_cmd(conn, l2cap_get_ident(conn),
7360 L2CAP_CONF_REQ,
7361 l2cap_build_conf_req(chan, buf),
7362 buf);
7363 chan->num_conf_req++;
7364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 }
7366
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007367 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368 }
7369
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007370 mutex_unlock(&conn->chan_lock);
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007371
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372 return 0;
7373}
7374
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007375int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376{
7377 struct l2cap_conn *conn = hcon->l2cap_data;
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007378 struct l2cap_hdr *hdr;
7379 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380
Andrei Emeltchenko1d13a252012-10-15 11:58:41 +03007381 /* For AMP controller do not create l2cap conn */
7382 if (!conn && hcon->hdev->dev_type != HCI_BREDR)
7383 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02007385 if (!conn)
Claudio Takahasibaf43252013-04-11 13:55:50 -03007386 conn = l2cap_conn_add(hcon);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02007387
7388 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389 goto drop;
7390
7391 BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
7392
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007393 switch (flags) {
7394 case ACL_START:
7395 case ACL_START_NO_FLUSH:
7396 case ACL_COMPLETE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 if (conn->rx_len) {
7398 BT_ERR("Unexpected start frame (len %d)", skb->len);
7399 kfree_skb(conn->rx_skb);
7400 conn->rx_skb = NULL;
7401 conn->rx_len = 0;
7402 l2cap_conn_unreliable(conn, ECOMM);
7403 }
7404
Andrei Emeltchenkoaae7fe22010-09-15 14:28:43 +03007405 /* Start fragment always begin with Basic L2CAP header */
7406 if (skb->len < L2CAP_HDR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 BT_ERR("Frame is too short (len %d)", skb->len);
7408 l2cap_conn_unreliable(conn, ECOMM);
7409 goto drop;
7410 }
7411
7412 hdr = (struct l2cap_hdr *) skb->data;
7413 len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
7414
7415 if (len == skb->len) {
7416 /* Complete frame received */
7417 l2cap_recv_frame(conn, skb);
7418 return 0;
7419 }
7420
7421 BT_DBG("Start: total len %d, frag len %d", len, skb->len);
7422
7423 if (skb->len > len) {
7424 BT_ERR("Frame is too long (len %d, expected len %d)",
Gustavo Padovan2d792812012-10-06 10:07:01 +01007425 skb->len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 l2cap_conn_unreliable(conn, ECOMM);
7427 goto drop;
7428 }
7429
7430 /* Allocate skb for the complete frame (with header) */
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03007431 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03007432 if (!conn->rx_skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 goto drop;
7434
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03007435 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Gustavo Padovan2d792812012-10-06 10:07:01 +01007436 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437 conn->rx_len = len - skb->len;
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007438 break;
7439
7440 case ACL_CONT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
7442
7443 if (!conn->rx_len) {
7444 BT_ERR("Unexpected continuation frame (len %d)", skb->len);
7445 l2cap_conn_unreliable(conn, ECOMM);
7446 goto drop;
7447 }
7448
7449 if (skb->len > conn->rx_len) {
7450 BT_ERR("Fragment is too long (len %d, expected %d)",
Gustavo Padovan2d792812012-10-06 10:07:01 +01007451 skb->len, conn->rx_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 kfree_skb(conn->rx_skb);
7453 conn->rx_skb = NULL;
7454 conn->rx_len = 0;
7455 l2cap_conn_unreliable(conn, ECOMM);
7456 goto drop;
7457 }
7458
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03007459 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Gustavo Padovan2d792812012-10-06 10:07:01 +01007460 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 conn->rx_len -= skb->len;
7462
7463 if (!conn->rx_len) {
Johan Hedbergc4e5baf2013-10-10 13:33:37 +02007464 /* Complete frame received. l2cap_recv_frame
7465 * takes ownership of the skb so set the global
7466 * rx_skb pointer to NULL first.
7467 */
7468 struct sk_buff *rx_skb = conn->rx_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 conn->rx_skb = NULL;
Johan Hedbergc4e5baf2013-10-10 13:33:37 +02007470 l2cap_recv_frame(conn, rx_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471 }
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007472 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 }
7474
7475drop:
7476 kfree_skb(skb);
7477 return 0;
7478}
7479
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007480static int l2cap_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007482 struct l2cap_chan *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483
Gustavo F. Padovan333055f2011-12-22 15:14:39 -02007484 read_lock(&chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007486 list_for_each_entry(c, &chan_list, global_l) {
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03007487 seq_printf(f, "%pMR %pMR %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
Marcel Holtmann7eafc592013-10-13 08:12:47 -07007488 &c->src, &c->dst,
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03007489 c->state, __le16_to_cpu(c->psm),
7490 c->scid, c->dcid, c->imtu, c->omtu,
7491 c->sec_level, c->mode);
Andrei Emeltchenko61e1b4b2012-01-19 11:19:50 +02007492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493
Gustavo F. Padovan333055f2011-12-22 15:14:39 -02007494 read_unlock(&chan_list_lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08007495
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497}
7498
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007499static int l2cap_debugfs_open(struct inode *inode, struct file *file)
7500{
7501 return single_open(file, l2cap_debugfs_show, inode->i_private);
7502}
7503
7504static const struct file_operations l2cap_debugfs_fops = {
7505 .open = l2cap_debugfs_open,
7506 .read = seq_read,
7507 .llseek = seq_lseek,
7508 .release = single_release,
7509};
7510
7511static struct dentry *l2cap_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512
Gustavo F. Padovan64274512011-02-07 20:08:52 -02007513int __init l2cap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514{
7515 int err;
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08007516
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02007517 err = l2cap_init_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 if (err < 0)
7519 return err;
7520
Marcel Holtmann1120e4b2013-10-17 17:24:16 -07007521 if (IS_ERR_OR_NULL(bt_debugfs))
7522 return 0;
7523
7524 l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs,
7525 NULL, &l2cap_debugfs_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526
Samuel Ortiz40b93972014-05-14 17:53:35 +02007527 debugfs_create_u16("l2cap_le_max_credits", 0644, bt_debugfs,
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02007528 &le_max_credits);
Samuel Ortiz40b93972014-05-14 17:53:35 +02007529 debugfs_create_u16("l2cap_le_default_mps", 0644, bt_debugfs,
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02007530 &le_default_mps);
7531
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533}
7534
Gustavo F. Padovan64274512011-02-07 20:08:52 -02007535void l2cap_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007537 debugfs_remove(l2cap_debugfs);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02007538 l2cap_cleanup_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539}
7540
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03007541module_param(disable_ertm, bool, 0644);
7542MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");