blob: 6ba33f9631e8e5830374ab4e51720c493969c67c [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Johannes Bergb5ad8b72011-06-01 08:54:45 +020050static LIST_HEAD(chan_list);
51static DEFINE_RWLOCK(chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Johan Hedbergf15b8ec2013-12-03 15:08:25 +020053static u16 le_max_credits = L2CAP_LE_MAX_CREDITS;
54static u16 le_default_mps = L2CAP_LE_DEFAULT_MPS;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +010057 u8 code, u8 ident, u16 dlen, void *data);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -030058static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
Gustavo Padovan2d792812012-10-06 10:07:01 +010059 void *data);
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -030060static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +020061static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Gustavo Padovand6603662012-05-21 13:58:22 -030063static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
Gustavo Padovan2d792812012-10-06 10:07:01 +010064 struct sk_buff_head *skbs, u8 event);
Mat Martineau608bcc62012-05-17 20:53:32 -070065
Johan Hedberga250e042015-01-15 13:06:44 +020066static inline u8 bdaddr_type(u8 link_type, u8 bdaddr_type)
Marcel Holtmann4f1654e2013-10-13 08:50:41 -070067{
Johan Hedberga250e042015-01-15 13:06:44 +020068 if (link_type == LE_LINK) {
69 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
Marcel Holtmann4f1654e2013-10-13 08:50:41 -070070 return BDADDR_LE_PUBLIC;
71 else
72 return BDADDR_LE_RANDOM;
73 }
74
75 return BDADDR_BREDR;
76}
77
Johan Hedberga250e042015-01-15 13:06:44 +020078static inline u8 bdaddr_src_type(struct hci_conn *hcon)
79{
80 return bdaddr_type(hcon->type, hcon->src_type);
81}
82
83static inline u8 bdaddr_dst_type(struct hci_conn *hcon)
84{
85 return bdaddr_type(hcon->type, hcon->dst_type);
86}
87
Marcel Holtmann01394182006-07-03 10:02:46 +020088/* ---- L2CAP channels ---- */
Gustavo F. Padovan71ba0e52011-05-17 14:34:52 -030089
Gustavo Padovan2d792812012-10-06 10:07:01 +010090static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
91 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +020092{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020093 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -030094
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020095 list_for_each_entry(c, &conn->chan_l, list) {
96 if (c->dcid == cid)
97 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +020098 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020099 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200100}
101
Gustavo Padovan2d792812012-10-06 10:07:01 +0100102static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn,
103 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +0200104{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200105 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300106
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200107 list_for_each_entry(c, &conn->chan_l, list) {
108 if (c->scid == cid)
109 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +0200110 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200111 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200112}
113
114/* Find channel with given SCID.
Mat Martineauef191ad2012-05-02 09:42:00 -0700115 * Returns locked channel. */
Gustavo Padovan2d792812012-10-06 10:07:01 +0100116static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn,
117 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +0200118{
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300119 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300120
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200121 mutex_lock(&conn->chan_lock);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300122 c = __l2cap_get_chan_by_scid(conn, cid);
Mat Martineauef191ad2012-05-02 09:42:00 -0700123 if (c)
124 l2cap_chan_lock(c);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200125 mutex_unlock(&conn->chan_lock);
126
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300127 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +0200128}
129
Mat Martineaub1a130b2012-10-23 15:24:09 -0700130/* Find channel with given DCID.
131 * Returns locked channel.
132 */
133static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
134 u16 cid)
135{
136 struct l2cap_chan *c;
137
138 mutex_lock(&conn->chan_lock);
139 c = __l2cap_get_chan_by_dcid(conn, cid);
140 if (c)
141 l2cap_chan_lock(c);
142 mutex_unlock(&conn->chan_lock);
143
144 return c;
145}
146
Gustavo Padovan2d792812012-10-06 10:07:01 +0100147static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn,
148 u8 ident)
Marcel Holtmann01394182006-07-03 10:02:46 +0200149{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200150 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300151
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200152 list_for_each_entry(c, &conn->chan_l, list) {
153 if (c->ident == ident)
154 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +0200155 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200156 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200157}
158
Mat Martineau5b155ef2012-10-23 15:24:14 -0700159static struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn,
160 u8 ident)
161{
162 struct l2cap_chan *c;
163
164 mutex_lock(&conn->chan_lock);
165 c = __l2cap_get_chan_by_ident(conn, ident);
166 if (c)
167 l2cap_chan_lock(c);
168 mutex_unlock(&conn->chan_lock);
169
170 return c;
171}
172
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300173static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300174{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300175 struct l2cap_chan *c;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300176
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300177 list_for_each_entry(c, &chan_list, global_l) {
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700178 if (c->sport == psm && !bacmp(&c->src, src))
Szymon Janc250938c2011-11-16 09:32:22 +0100179 return c;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300180 }
Szymon Janc250938c2011-11-16 09:32:22 +0100181 return NULL;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300182}
183
184int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm)
185{
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300186 int err;
187
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200188 write_lock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300189
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300190 if (psm && __l2cap_global_chan_by_addr(psm, src)) {
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300191 err = -EADDRINUSE;
192 goto done;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300193 }
194
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300195 if (psm) {
196 chan->psm = psm;
197 chan->sport = psm;
198 err = 0;
199 } else {
200 u16 p;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300201
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300202 err = -EINVAL;
203 for (p = 0x1001; p < 0x1100; p += 2)
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300204 if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300205 chan->psm = cpu_to_le16(p);
206 chan->sport = cpu_to_le16(p);
207 err = 0;
208 break;
209 }
210 }
211
212done:
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200213 write_unlock(&chan_list_lock);
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300214 return err;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300215}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300216EXPORT_SYMBOL_GPL(l2cap_add_psm);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300217
218int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid)
219{
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200220 write_lock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300221
Johan Hedberg14824302014-08-07 22:56:50 +0300222 /* Override the defaults (which are for conn-oriented) */
223 chan->omtu = L2CAP_DEFAULT_MTU;
224 chan->chan_type = L2CAP_CHAN_FIXED;
225
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300226 chan->scid = scid;
227
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200228 write_unlock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300229
230 return 0;
231}
232
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300233static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
Marcel Holtmann01394182006-07-03 10:02:46 +0200234{
Johan Hedberge77af752013-10-08 10:31:00 +0200235 u16 cid, dyn_end;
Marcel Holtmann01394182006-07-03 10:02:46 +0200236
Johan Hedberge77af752013-10-08 10:31:00 +0200237 if (conn->hcon->type == LE_LINK)
238 dyn_end = L2CAP_CID_LE_DYN_END;
239 else
240 dyn_end = L2CAP_CID_DYN_END;
241
242 for (cid = L2CAP_CID_DYN_START; cid < dyn_end; cid++) {
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300243 if (!__l2cap_get_chan_by_scid(conn, cid))
Marcel Holtmann01394182006-07-03 10:02:46 +0200244 return cid;
245 }
246
247 return 0;
248}
249
Gustavo Padovanf93fa272013-10-21 14:21:40 -0200250static void l2cap_state_change(struct l2cap_chan *chan, int state)
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300251{
Andrei Emeltchenko42d2d872012-02-17 11:40:57 +0200252 BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state),
Gustavo Padovan2d792812012-10-06 10:07:01 +0100253 state_to_string(state));
Gustavo F. Padovanbadaaa02011-11-23 20:11:46 -0200254
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300255 chan->state = state;
Gustavo Padovan53f52122013-10-15 19:24:45 -0300256 chan->ops->state_change(chan, state, 0);
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300257}
258
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300259static inline void l2cap_state_change_and_error(struct l2cap_chan *chan,
260 int state, int err)
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200261{
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300262 chan->state = state;
Gustavo Padovan53f52122013-10-15 19:24:45 -0300263 chan->ops->state_change(chan, chan->state, err);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200264}
265
266static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err)
267{
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300268 chan->ops->state_change(chan, chan->state, err);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200269}
270
Mat Martineau4239d162012-05-17 20:53:49 -0700271static void __set_retrans_timer(struct l2cap_chan *chan)
272{
273 if (!delayed_work_pending(&chan->monitor_timer) &&
274 chan->retrans_timeout) {
275 l2cap_set_timer(chan, &chan->retrans_timer,
276 msecs_to_jiffies(chan->retrans_timeout));
277 }
278}
279
280static void __set_monitor_timer(struct l2cap_chan *chan)
281{
282 __clear_retrans_timer(chan);
283 if (chan->monitor_timeout) {
284 l2cap_set_timer(chan, &chan->monitor_timer,
285 msecs_to_jiffies(chan->monitor_timeout));
286 }
287}
288
Mat Martineau608bcc62012-05-17 20:53:32 -0700289static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head,
290 u16 seq)
291{
292 struct sk_buff *skb;
293
294 skb_queue_walk(head, skb) {
295 if (bt_cb(skb)->control.txseq == seq)
296 return skb;
297 }
298
299 return NULL;
300}
301
Mat Martineau3c588192012-04-11 10:48:42 -0700302/* ---- L2CAP sequence number lists ---- */
303
304/* For ERTM, ordered lists of sequence numbers must be tracked for
305 * SREJ requests that are received and for frames that are to be
306 * retransmitted. These seq_list functions implement a singly-linked
307 * list in an array, where membership in the list can also be checked
308 * in constant time. Items can also be added to the tail of the list
309 * and removed from the head in constant time, without further memory
310 * allocs or frees.
311 */
312
313static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size)
314{
315 size_t alloc_size, i;
316
317 /* Allocated size is a power of 2 to map sequence numbers
318 * (which may be up to 14 bits) in to a smaller array that is
319 * sized for the negotiated ERTM transmit windows.
320 */
321 alloc_size = roundup_pow_of_two(size);
322
323 seq_list->list = kmalloc(sizeof(u16) * alloc_size, GFP_KERNEL);
324 if (!seq_list->list)
325 return -ENOMEM;
326
327 seq_list->mask = alloc_size - 1;
328 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
329 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
330 for (i = 0; i < alloc_size; i++)
331 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
332
333 return 0;
334}
335
336static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list)
337{
338 kfree(seq_list->list);
339}
340
341static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list,
342 u16 seq)
343{
344 /* Constant-time check for list membership */
345 return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR;
346}
347
Mat Martineau3c588192012-04-11 10:48:42 -0700348static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list)
349{
Johan Hedberg03a0c5d2014-01-18 21:32:59 +0200350 u16 seq = seq_list->head;
351 u16 mask = seq_list->mask;
352
353 seq_list->head = seq_list->list[seq & mask];
354 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR;
355
356 if (seq_list->head == L2CAP_SEQ_LIST_TAIL) {
357 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
358 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
359 }
360
361 return seq;
Mat Martineau3c588192012-04-11 10:48:42 -0700362}
363
364static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list)
365{
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300366 u16 i;
Mat Martineau3c588192012-04-11 10:48:42 -0700367
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300368 if (seq_list->head == L2CAP_SEQ_LIST_CLEAR)
369 return;
370
371 for (i = 0; i <= seq_list->mask; i++)
372 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
373
374 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
375 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
Mat Martineau3c588192012-04-11 10:48:42 -0700376}
377
378static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq)
379{
380 u16 mask = seq_list->mask;
381
382 /* All appends happen in constant time */
383
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300384 if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR)
385 return;
Mat Martineau3c588192012-04-11 10:48:42 -0700386
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300387 if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR)
388 seq_list->head = seq;
389 else
390 seq_list->list[seq_list->tail & mask] = seq;
391
392 seq_list->tail = seq;
393 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL;
Mat Martineau3c588192012-04-11 10:48:42 -0700394}
395
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300396static void l2cap_chan_timeout(struct work_struct *work)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300397{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300398 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100399 chan_timer.work);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200400 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300401 int reason;
402
Andrei Emeltchenkoe05dcc32012-02-17 11:40:56 +0200403 BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300404
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200405 mutex_lock(&conn->chan_lock);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200406 l2cap_chan_lock(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300407
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300408 if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300409 reason = ECONNREFUSED;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300410 else if (chan->state == BT_CONNECT &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100411 chan->sec_level != BT_SECURITY_SDP)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300412 reason = ECONNREFUSED;
413 else
414 reason = ETIMEDOUT;
415
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300416 l2cap_chan_close(chan, reason);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300417
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200418 l2cap_chan_unlock(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300419
Gustavo Padovan80b98022012-05-27 22:27:51 -0300420 chan->ops->close(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200421 mutex_unlock(&conn->chan_lock);
422
Ulisses Furquim371fd832011-12-21 20:02:36 -0200423 l2cap_chan_put(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300424}
425
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -0300426struct l2cap_chan *l2cap_chan_create(void)
Marcel Holtmann01394182006-07-03 10:02:46 +0200427{
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300428 struct l2cap_chan *chan;
Marcel Holtmann01394182006-07-03 10:02:46 +0200429
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300430 chan = kzalloc(sizeof(*chan), GFP_ATOMIC);
431 if (!chan)
432 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200433
Andrei Emeltchenkoc03b3552012-02-21 12:54:56 +0200434 mutex_init(&chan->lock);
435
Johan Hedbergff714112014-11-13 09:46:04 +0200436 /* Set default lock nesting level */
437 atomic_set(&chan->nesting, L2CAP_NESTING_NORMAL);
438
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200439 write_lock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300440 list_add(&chan->global_l, &chan_list);
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200441 write_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300442
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300443 INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300444
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300445 chan->state = BT_OPEN;
446
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530447 kref_init(&chan->kref);
Gustavo F. Padovan71ba0e52011-05-17 14:34:52 -0300448
Mat Martineau28270112012-05-17 21:14:09 -0700449 /* This flag is cleared in l2cap_chan_ready() */
450 set_bit(CONF_NOT_COMPLETE, &chan->conf_state);
451
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -0300452 BT_DBG("chan %p", chan);
Szymon Jancabc545b2011-11-03 16:05:44 +0100453
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300454 return chan;
Marcel Holtmann01394182006-07-03 10:02:46 +0200455}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300456EXPORT_SYMBOL_GPL(l2cap_chan_create);
Marcel Holtmann01394182006-07-03 10:02:46 +0200457
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530458static void l2cap_chan_destroy(struct kref *kref)
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300459{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530460 struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref);
461
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530462 BT_DBG("chan %p", chan);
463
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200464 write_lock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300465 list_del(&chan->global_l);
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200466 write_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300467
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530468 kfree(chan);
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300469}
470
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530471void l2cap_chan_hold(struct l2cap_chan *c)
472{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530473 BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount));
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530474
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530475 kref_get(&c->kref);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530476}
477
478void l2cap_chan_put(struct l2cap_chan *c)
479{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530480 BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount));
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530481
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530482 kref_put(&c->kref, l2cap_chan_destroy);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530483}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300484EXPORT_SYMBOL_GPL(l2cap_chan_put);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530485
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300486void l2cap_chan_set_defaults(struct l2cap_chan *chan)
487{
488 chan->fcs = L2CAP_FCS_CRC16;
489 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
490 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
491 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
Jukka Rissanen6a5e8162014-05-28 14:43:04 +0300492 chan->remote_max_tx = chan->max_tx;
493 chan->remote_tx_win = chan->tx_win;
Mat Martineauc20f8e32012-07-10 05:47:07 -0700494 chan->ack_win = L2CAP_DEFAULT_TX_WINDOW;
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300495 chan->sec_level = BT_SECURITY_LOW;
Jukka Rissanen6a5e8162014-05-28 14:43:04 +0300496 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
497 chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
498 chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
499 chan->conf_state = 0;
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300500
501 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
502}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300503EXPORT_SYMBOL_GPL(l2cap_chan_set_defaults);
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300504
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200505static void l2cap_le_flowctl_init(struct l2cap_chan *chan)
Johan Hedberg38319712013-05-17 12:49:23 +0300506{
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200507 chan->sdu = NULL;
508 chan->sdu_last_frag = NULL;
509 chan->sdu_len = 0;
Johan Hedberg0cd75f72013-05-17 13:09:05 +0300510 chan->tx_credits = 0;
Johan Hedbergf15b8ec2013-12-03 15:08:25 +0200511 chan->rx_credits = le_max_credits;
Johan Hedbergd1d79412014-01-27 15:11:33 -0800512 chan->mps = min_t(u16, chan->imtu, le_default_mps);
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200513
514 skb_queue_head_init(&chan->tx_q);
Johan Hedberg38319712013-05-17 12:49:23 +0300515}
516
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +0300517void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
Marcel Holtmann01394182006-07-03 10:02:46 +0200518{
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -0300519 BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
Andrei Emeltchenko097db762012-03-09 14:16:17 +0200520 __le16_to_cpu(chan->psm), chan->dcid);
Marcel Holtmann01394182006-07-03 10:02:46 +0200521
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +0200522 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100523
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300524 chan->conn = conn;
Marcel Holtmann01394182006-07-03 10:02:46 +0200525
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200526 switch (chan->chan_type) {
527 case L2CAP_CHAN_CONN_ORIENTED:
Johan Hedberg21626e62014-01-24 10:35:41 +0200528 /* Alloc CID for connection-oriented socket */
529 chan->scid = l2cap_alloc_cid(conn);
530 if (conn->hcon->type == ACL_LINK)
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300531 chan->omtu = L2CAP_DEFAULT_MTU;
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200532 break;
533
534 case L2CAP_CHAN_CONN_LESS:
Marcel Holtmann01394182006-07-03 10:02:46 +0200535 /* Connectionless socket */
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300536 chan->scid = L2CAP_CID_CONN_LESS;
537 chan->dcid = L2CAP_CID_CONN_LESS;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300538 chan->omtu = L2CAP_DEFAULT_MTU;
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200539 break;
540
Johan Hedberg2338a7e2014-01-24 10:35:40 +0200541 case L2CAP_CHAN_FIXED:
542 /* Caller will set CID and CID specific MTU values */
Andrei Emeltchenko416fa752012-05-29 13:59:16 +0300543 break;
544
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200545 default:
Marcel Holtmann01394182006-07-03 10:02:46 +0200546 /* Raw socket can send/recv signalling messages only */
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300547 chan->scid = L2CAP_CID_SIGNALING;
548 chan->dcid = L2CAP_CID_SIGNALING;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300549 chan->omtu = L2CAP_DEFAULT_MTU;
Marcel Holtmann01394182006-07-03 10:02:46 +0200550 }
551
Andrei Emeltchenko8f7975b2011-10-13 16:18:54 +0300552 chan->local_id = L2CAP_BESTEFFORT_ID;
553 chan->local_stype = L2CAP_SERV_BESTEFFORT;
554 chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE;
555 chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME;
556 chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT;
Andrei Emeltchenko8936fa62012-10-08 11:14:41 +0300557 chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO;
Andrei Emeltchenko8f7975b2011-10-13 16:18:54 +0300558
Ulisses Furquim371fd832011-12-21 20:02:36 -0200559 l2cap_chan_hold(chan);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300560
Johan Hedbergc16900c2014-08-15 21:17:06 +0300561 /* Only keep a reference for fixed channels if they requested it */
562 if (chan->chan_type != L2CAP_CHAN_FIXED ||
563 test_bit(FLAG_HOLD_HCI_CONN, &chan->flags))
564 hci_conn_hold(conn->hcon);
Johan Hedberg5ee98912013-04-29 19:35:43 +0300565
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200566 list_add(&chan->list, &conn->chan_l);
Andrei Emeltchenko643162a2012-02-22 17:11:55 +0200567}
568
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300569void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
Andrei Emeltchenko643162a2012-02-22 17:11:55 +0200570{
571 mutex_lock(&conn->chan_lock);
572 __l2cap_chan_add(conn, chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200573 mutex_unlock(&conn->chan_lock);
Marcel Holtmann01394182006-07-03 10:02:46 +0200574}
575
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300576void l2cap_chan_del(struct l2cap_chan *chan, int err)
Marcel Holtmann01394182006-07-03 10:02:46 +0200577{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300578 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann01394182006-07-03 10:02:46 +0200579
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -0300580 __clear_chan_timer(chan);
Marcel Holtmann01394182006-07-03 10:02:46 +0200581
Johan Hedberg49d11742014-11-13 14:37:50 +0200582 BT_DBG("chan %p, conn %p, err %d, state %s", chan, conn, err,
583 state_to_string(chan->state));
Marcel Holtmann01394182006-07-03 10:02:46 +0200584
Johan Hedberg72847ce2014-08-08 09:28:03 +0300585 chan->ops->teardown(chan, err);
586
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900587 if (conn) {
Andrei Emeltchenko56f60982012-10-15 11:58:44 +0300588 struct amp_mgr *mgr = conn->hcon->amp_mgr;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300589 /* Delete from channel list */
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200590 list_del(&chan->list);
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -0200591
Ulisses Furquim371fd832011-12-21 20:02:36 -0200592 l2cap_chan_put(chan);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300593
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300594 chan->conn = NULL;
Andrei Emeltchenko3cabbfd2012-05-31 11:01:37 +0300595
Johan Hedbergc16900c2014-08-15 21:17:06 +0300596 /* Reference was only held for non-fixed channels or
597 * fixed channels that explicitly requested it using the
598 * FLAG_HOLD_HCI_CONN flag.
599 */
600 if (chan->chan_type != L2CAP_CHAN_FIXED ||
601 test_bit(FLAG_HOLD_HCI_CONN, &chan->flags))
David Herrmann76a68ba2013-04-06 20:28:37 +0200602 hci_conn_drop(conn->hcon);
Andrei Emeltchenko56f60982012-10-15 11:58:44 +0300603
604 if (mgr && mgr->bredr_chan == chan)
605 mgr->bredr_chan = NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200606 }
607
Andrei Emeltchenko419e08c2012-10-31 15:46:34 +0200608 if (chan->hs_hchan) {
609 struct hci_chan *hs_hchan = chan->hs_hchan;
610
611 BT_DBG("chan %p disconnect hs_hchan %p", chan, hs_hchan);
612 amp_disconnect_logical_link(hs_hchan);
613 }
614
Mat Martineau28270112012-05-17 21:14:09 -0700615 if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state))
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300616 return;
Gustavo F. Padovan2ead70b2011-04-01 15:13:36 -0300617
Gustavo Padovanee556f62012-05-18 20:22:38 -0300618 switch(chan->mode) {
619 case L2CAP_MODE_BASIC:
620 break;
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300621
Johan Hedberg38319712013-05-17 12:49:23 +0300622 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedberg177f8f22013-05-31 17:54:51 +0300623 skb_queue_purge(&chan->tx_q);
Johan Hedberg38319712013-05-17 12:49:23 +0300624 break;
625
Gustavo Padovanee556f62012-05-18 20:22:38 -0300626 case L2CAP_MODE_ERTM:
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -0300627 __clear_retrans_timer(chan);
628 __clear_monitor_timer(chan);
629 __clear_ack_timer(chan);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300630
Gustavo F. Padovanf1c67752011-03-25 20:36:10 -0300631 skb_queue_purge(&chan->srej_q);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300632
Mat Martineau3c588192012-04-11 10:48:42 -0700633 l2cap_seq_list_free(&chan->srej_list);
634 l2cap_seq_list_free(&chan->retrans_list);
Gustavo Padovanee556f62012-05-18 20:22:38 -0300635
636 /* fall through */
637
638 case L2CAP_MODE_STREAMING:
639 skb_queue_purge(&chan->tx_q);
640 break;
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300641 }
Gustavo Padovanee556f62012-05-18 20:22:38 -0300642
643 return;
Marcel Holtmann01394182006-07-03 10:02:46 +0200644}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300645EXPORT_SYMBOL_GPL(l2cap_chan_del);
Marcel Holtmann01394182006-07-03 10:02:46 +0200646
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300647static void l2cap_conn_update_id_addr(struct work_struct *work)
Johan Hedberg387a33e2014-02-18 21:41:33 +0200648{
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300649 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
650 id_addr_update_work);
651 struct hci_conn *hcon = conn->hcon;
Johan Hedberg387a33e2014-02-18 21:41:33 +0200652 struct l2cap_chan *chan;
653
654 mutex_lock(&conn->chan_lock);
655
656 list_for_each_entry(chan, &conn->chan_l, list) {
657 l2cap_chan_lock(chan);
658 bacpy(&chan->dst, &hcon->dst);
Johan Hedberga250e042015-01-15 13:06:44 +0200659 chan->dst_type = bdaddr_dst_type(hcon);
Johan Hedberg387a33e2014-02-18 21:41:33 +0200660 l2cap_chan_unlock(chan);
661 }
662
663 mutex_unlock(&conn->chan_lock);
664}
665
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300666static void l2cap_chan_le_connect_reject(struct l2cap_chan *chan)
667{
668 struct l2cap_conn *conn = chan->conn;
669 struct l2cap_le_conn_rsp rsp;
670 u16 result;
671
672 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
673 result = L2CAP_CR_AUTHORIZATION;
674 else
675 result = L2CAP_CR_BAD_PSM;
676
677 l2cap_state_change(chan, BT_DISCONN);
678
679 rsp.dcid = cpu_to_le16(chan->scid);
680 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +0200681 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +0300682 rsp.credits = cpu_to_le16(chan->rx_credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300683 rsp.result = cpu_to_le16(result);
684
685 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
686 &rsp);
687}
688
Johan Hedberg791d60f2013-05-14 22:24:44 +0300689static void l2cap_chan_connect_reject(struct l2cap_chan *chan)
690{
691 struct l2cap_conn *conn = chan->conn;
692 struct l2cap_conn_rsp rsp;
693 u16 result;
694
695 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
696 result = L2CAP_CR_SEC_BLOCK;
697 else
698 result = L2CAP_CR_BAD_PSM;
699
700 l2cap_state_change(chan, BT_DISCONN);
701
702 rsp.scid = cpu_to_le16(chan->dcid);
703 rsp.dcid = cpu_to_le16(chan->scid);
704 rsp.result = cpu_to_le16(result);
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700705 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Johan Hedberg791d60f2013-05-14 22:24:44 +0300706
707 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
708}
709
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300710void l2cap_chan_close(struct l2cap_chan *chan, int reason)
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300711{
712 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300713
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700714 BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300715
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300716 switch (chan->state) {
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300717 case BT_LISTEN:
Gustavo Padovanb699ec02012-10-06 11:51:54 +0100718 chan->ops->teardown(chan, 0);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300719 break;
720
721 case BT_CONNECTED:
722 case BT_CONFIG:
Johan Hedberg7b25c9b2014-01-28 15:28:04 -0800723 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
Gustavo Padovan8d836d72013-10-15 19:24:47 -0300724 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +0200725 l2cap_send_disconn_req(chan, reason);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300726 } else
727 l2cap_chan_del(chan, reason);
728 break;
729
730 case BT_CONNECT2:
Johan Hedberg791d60f2013-05-14 22:24:44 +0300731 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
732 if (conn->hcon->type == ACL_LINK)
733 l2cap_chan_connect_reject(chan);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300734 else if (conn->hcon->type == LE_LINK)
735 l2cap_chan_le_connect_reject(chan);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300736 }
737
738 l2cap_chan_del(chan, reason);
739 break;
740
741 case BT_CONNECT:
742 case BT_DISCONN:
743 l2cap_chan_del(chan, reason);
744 break;
745
746 default:
Gustavo Padovanb699ec02012-10-06 11:51:54 +0100747 chan->ops->teardown(chan, 0);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300748 break;
749 }
750}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300751EXPORT_SYMBOL(l2cap_chan_close);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300752
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300753static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
Johan Hedberg8556edd32011-01-19 12:06:50 +0530754{
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700755 switch (chan->chan_type) {
756 case L2CAP_CHAN_RAW:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300757 switch (chan->sec_level) {
Johan Hedberg8556edd32011-01-19 12:06:50 +0530758 case BT_SECURITY_HIGH:
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800759 case BT_SECURITY_FIPS:
Johan Hedberg8556edd32011-01-19 12:06:50 +0530760 return HCI_AT_DEDICATED_BONDING_MITM;
761 case BT_SECURITY_MEDIUM:
762 return HCI_AT_DEDICATED_BONDING;
763 default:
764 return HCI_AT_NO_BONDING;
765 }
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700766 break;
Marcel Holtmann3124b842013-10-12 07:19:32 -0700767 case L2CAP_CHAN_CONN_LESS:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700768 if (chan->psm == cpu_to_le16(L2CAP_PSM_3DSP)) {
Marcel Holtmann3124b842013-10-12 07:19:32 -0700769 if (chan->sec_level == BT_SECURITY_LOW)
770 chan->sec_level = BT_SECURITY_SDP;
771 }
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800772 if (chan->sec_level == BT_SECURITY_HIGH ||
773 chan->sec_level == BT_SECURITY_FIPS)
Marcel Holtmann3124b842013-10-12 07:19:32 -0700774 return HCI_AT_NO_BONDING_MITM;
775 else
776 return HCI_AT_NO_BONDING;
777 break;
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700778 case L2CAP_CHAN_CONN_ORIENTED:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700779 if (chan->psm == cpu_to_le16(L2CAP_PSM_SDP)) {
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700780 if (chan->sec_level == BT_SECURITY_LOW)
781 chan->sec_level = BT_SECURITY_SDP;
Johan Hedberg8556edd32011-01-19 12:06:50 +0530782
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800783 if (chan->sec_level == BT_SECURITY_HIGH ||
784 chan->sec_level == BT_SECURITY_FIPS)
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700785 return HCI_AT_NO_BONDING_MITM;
786 else
787 return HCI_AT_NO_BONDING;
788 }
789 /* fall through */
790 default:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300791 switch (chan->sec_level) {
Johan Hedberg8556edd32011-01-19 12:06:50 +0530792 case BT_SECURITY_HIGH:
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800793 case BT_SECURITY_FIPS:
Johan Hedberg8556edd32011-01-19 12:06:50 +0530794 return HCI_AT_GENERAL_BONDING_MITM;
795 case BT_SECURITY_MEDIUM:
796 return HCI_AT_GENERAL_BONDING;
797 default:
798 return HCI_AT_NO_BONDING;
799 }
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700800 break;
Johan Hedberg8556edd32011-01-19 12:06:50 +0530801 }
802}
803
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200804/* Service level security */
Johan Hedberge7cafc42014-07-17 15:35:38 +0300805int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator)
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200806{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300807 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100808 __u8 auth_type;
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200809
Johan Hedberga17de2f2013-05-14 13:25:37 +0300810 if (conn->hcon->type == LE_LINK)
811 return smp_conn_security(conn->hcon, chan->sec_level);
812
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300813 auth_type = l2cap_get_auth_type(chan);
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100814
Johan Hedberge7cafc42014-07-17 15:35:38 +0300815 return hci_conn_security(conn->hcon, chan->sec_level, auth_type,
816 initiator);
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200817}
818
Johannes Bergb5ad8b72011-06-01 08:54:45 +0200819static u8 l2cap_get_ident(struct l2cap_conn *conn)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200820{
821 u8 id;
822
823 /* Get next available identificator.
824 * 1 - 128 are used by kernel.
825 * 129 - 199 are reserved.
826 * 200 - 254 are used by utilities like l2ping, etc.
827 */
828
Marcel Holtmann5a54e7c2014-07-13 20:50:15 +0200829 mutex_lock(&conn->ident_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200830
831 if (++conn->tx_ident > 128)
832 conn->tx_ident = 1;
833
834 id = conn->tx_ident;
835
Marcel Holtmann5a54e7c2014-07-13 20:50:15 +0200836 mutex_unlock(&conn->ident_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200837
838 return id;
839}
840
Gustavo Padovan2d792812012-10-06 10:07:01 +0100841static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
842 void *data)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200843{
844 struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +0200845 u8 flags;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200846
847 BT_DBG("code 0x%2.2x", code);
848
849 if (!skb)
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -0300850 return;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200851
Steven Walterf6af6752014-11-19 09:41:17 -0500852 /* Use NO_FLUSH if supported or we have an LE link (which does
853 * not support auto-flushing packets) */
854 if (lmp_no_flush_capable(conn->hcon->hdev) ||
855 conn->hcon->type == LE_LINK)
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +0200856 flags = ACL_START_NO_FLUSH;
857 else
858 flags = ACL_START;
859
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700860 bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +0200861 skb->priority = HCI_PRIO_MAX;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700862
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200863 hci_send_acl(conn->hchan, skb, flags);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200864}
865
Mat Martineau02b0fbb2012-10-23 15:24:10 -0700866static bool __chan_is_moving(struct l2cap_chan *chan)
867{
868 return chan->move_state != L2CAP_MOVE_STABLE &&
869 chan->move_state != L2CAP_MOVE_WAIT_PREPARE;
870}
871
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200872static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
873{
874 struct hci_conn *hcon = chan->conn->hcon;
875 u16 flags;
876
877 BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100878 skb->priority);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200879
Mat Martineaud5f8a752012-10-23 15:24:18 -0700880 if (chan->hs_hcon && !__chan_is_moving(chan)) {
881 if (chan->hs_hchan)
882 hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE);
883 else
884 kfree_skb(skb);
885
886 return;
887 }
888
Steven Walterf6af6752014-11-19 09:41:17 -0500889 /* Use NO_FLUSH for LE links (where this is the only option) or
890 * if the BR/EDR link supports it and flushing has not been
891 * explicitly requested (through FLAG_FLUSHABLE).
892 */
893 if (hcon->type == LE_LINK ||
894 (!test_bit(FLAG_FLUSHABLE, &chan->flags) &&
895 lmp_no_flush_capable(hcon->hdev)))
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200896 flags = ACL_START_NO_FLUSH;
897 else
898 flags = ACL_START;
899
900 bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
901 hci_send_acl(chan->conn->hchan, skb, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700904static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control)
905{
906 control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT;
907 control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT;
908
909 if (enh & L2CAP_CTRL_FRAME_TYPE) {
910 /* S-Frame */
911 control->sframe = 1;
912 control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT;
913 control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT;
914
915 control->sar = 0;
916 control->txseq = 0;
917 } else {
918 /* I-Frame */
919 control->sframe = 0;
920 control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT;
921 control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT;
922
923 control->poll = 0;
924 control->super = 0;
925 }
926}
927
928static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control)
929{
930 control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT;
931 control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT;
932
933 if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) {
934 /* S-Frame */
935 control->sframe = 1;
936 control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT;
937 control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT;
938
939 control->sar = 0;
940 control->txseq = 0;
941 } else {
942 /* I-Frame */
943 control->sframe = 0;
944 control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT;
945 control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT;
946
947 control->poll = 0;
948 control->super = 0;
949 }
950}
951
952static inline void __unpack_control(struct l2cap_chan *chan,
953 struct sk_buff *skb)
954{
955 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
956 __unpack_extended_control(get_unaligned_le32(skb->data),
957 &bt_cb(skb)->control);
Mat Martineaucec8ab6e2012-05-17 20:53:36 -0700958 skb_pull(skb, L2CAP_EXT_CTRL_SIZE);
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700959 } else {
960 __unpack_enhanced_control(get_unaligned_le16(skb->data),
961 &bt_cb(skb)->control);
Mat Martineaucec8ab6e2012-05-17 20:53:36 -0700962 skb_pull(skb, L2CAP_ENH_CTRL_SIZE);
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700963 }
964}
965
966static u32 __pack_extended_control(struct l2cap_ctrl *control)
967{
968 u32 packed;
969
970 packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT;
971 packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT;
972
973 if (control->sframe) {
974 packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT;
975 packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT;
976 packed |= L2CAP_EXT_CTRL_FRAME_TYPE;
977 } else {
978 packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT;
979 packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT;
980 }
981
982 return packed;
983}
984
985static u16 __pack_enhanced_control(struct l2cap_ctrl *control)
986{
987 u16 packed;
988
989 packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT;
990 packed |= control->final << L2CAP_CTRL_FINAL_SHIFT;
991
992 if (control->sframe) {
993 packed |= control->poll << L2CAP_CTRL_POLL_SHIFT;
994 packed |= control->super << L2CAP_CTRL_SUPER_SHIFT;
995 packed |= L2CAP_CTRL_FRAME_TYPE;
996 } else {
997 packed |= control->sar << L2CAP_CTRL_SAR_SHIFT;
998 packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT;
999 }
1000
1001 return packed;
1002}
1003
1004static inline void __pack_control(struct l2cap_chan *chan,
1005 struct l2cap_ctrl *control,
1006 struct sk_buff *skb)
1007{
1008 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
1009 put_unaligned_le32(__pack_extended_control(control),
1010 skb->data + L2CAP_HDR_SIZE);
1011 } else {
1012 put_unaligned_le16(__pack_enhanced_control(control),
1013 skb->data + L2CAP_HDR_SIZE);
1014 }
1015}
1016
Gustavo Padovanba7aa642012-05-29 13:29:16 -03001017static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan)
1018{
1019 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1020 return L2CAP_EXT_HDR_SIZE;
1021 else
1022 return L2CAP_ENH_HDR_SIZE;
1023}
1024
Mat Martineaua67d7f62012-05-17 20:53:35 -07001025static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan,
1026 u32 control)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001027{
1028 struct sk_buff *skb;
1029 struct l2cap_hdr *lh;
Gustavo Padovanba7aa642012-05-29 13:29:16 -03001030 int hlen = __ertm_hdr_size(chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001031
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001032 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03001033 hlen += L2CAP_FCS_SIZE;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001034
Mat Martineaua67d7f62012-05-17 20:53:35 -07001035 skb = bt_skb_alloc(hlen, GFP_KERNEL);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001036
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001037 if (!skb)
Mat Martineaua67d7f62012-05-17 20:53:35 -07001038 return ERR_PTR(-ENOMEM);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001039
1040 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001041 lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001042 lh->cid = cpu_to_le16(chan->dcid);
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03001043
Mat Martineaua67d7f62012-05-17 20:53:35 -07001044 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1045 put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
1046 else
1047 put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001048
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001049 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineaua67d7f62012-05-17 20:53:35 -07001050 u16 fcs = crc16(0, (u8 *)skb->data, skb->len);
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03001051 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001052 }
1053
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001054 skb->priority = HCI_PRIO_MAX;
Mat Martineaua67d7f62012-05-17 20:53:35 -07001055 return skb;
1056}
1057
1058static void l2cap_send_sframe(struct l2cap_chan *chan,
1059 struct l2cap_ctrl *control)
1060{
1061 struct sk_buff *skb;
1062 u32 control_field;
1063
1064 BT_DBG("chan %p, control %p", chan, control);
1065
1066 if (!control->sframe)
1067 return;
1068
Mat Martineaub99e13a2012-10-23 15:24:19 -07001069 if (__chan_is_moving(chan))
1070 return;
1071
Mat Martineaua67d7f62012-05-17 20:53:35 -07001072 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) &&
1073 !control->poll)
1074 control->final = 1;
1075
1076 if (control->super == L2CAP_SUPER_RR)
1077 clear_bit(CONN_RNR_SENT, &chan->conn_state);
1078 else if (control->super == L2CAP_SUPER_RNR)
1079 set_bit(CONN_RNR_SENT, &chan->conn_state);
1080
1081 if (control->super != L2CAP_SUPER_SREJ) {
1082 chan->last_acked_seq = control->reqseq;
1083 __clear_ack_timer(chan);
1084 }
1085
1086 BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq,
1087 control->final, control->poll, control->super);
1088
1089 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1090 control_field = __pack_extended_control(control);
1091 else
1092 control_field = __pack_enhanced_control(control);
1093
1094 skb = l2cap_create_sframe_pdu(chan, control_field);
1095 if (!IS_ERR(skb))
1096 l2cap_do_send(chan, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001097}
1098
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001099static void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, bool poll)
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001100{
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001101 struct l2cap_ctrl control;
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001102
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001103 BT_DBG("chan %p, poll %d", chan, poll);
1104
1105 memset(&control, 0, sizeof(control));
1106 control.sframe = 1;
1107 control.poll = poll;
1108
1109 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
1110 control.super = L2CAP_SUPER_RNR;
1111 else
1112 control.super = L2CAP_SUPER_RR;
1113
1114 control.reqseq = chan->buffer_seq;
1115 l2cap_send_sframe(chan, &control);
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001116}
1117
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001118static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
Andrei Emeltchenkoe501d052010-07-08 12:14:41 +03001119{
Johan Hedberg5ff6f342014-08-07 22:56:43 +03001120 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
1121 return true;
1122
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001123 return !test_bit(CONF_CONNECT_PEND, &chan->conf_state);
Andrei Emeltchenkoe501d052010-07-08 12:14:41 +03001124}
1125
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001126static bool __amp_capable(struct l2cap_chan *chan)
1127{
1128 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann1df7b172013-10-05 11:47:49 -07001129 struct hci_dev *hdev;
1130 bool amp_available = false;
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001131
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02001132 if (!(conn->local_fixed_chan & L2CAP_FC_A2MP))
Marcel Holtmann1df7b172013-10-05 11:47:49 -07001133 return false;
1134
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02001135 if (!(conn->remote_fixed_chan & L2CAP_FC_A2MP))
Marcel Holtmann1df7b172013-10-05 11:47:49 -07001136 return false;
1137
1138 read_lock(&hci_dev_list_lock);
1139 list_for_each_entry(hdev, &hci_dev_list, list) {
1140 if (hdev->amp_type != AMP_TYPE_BREDR &&
1141 test_bit(HCI_UP, &hdev->flags)) {
1142 amp_available = true;
1143 break;
1144 }
1145 }
1146 read_unlock(&hci_dev_list_lock);
1147
1148 if (chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED)
1149 return amp_available;
Marcel Holtmann848566b2013-10-01 22:59:22 -07001150
1151 return false;
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001152}
1153
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +02001154static bool l2cap_check_efs(struct l2cap_chan *chan)
1155{
1156 /* Check EFS parameters */
1157 return true;
1158}
1159
Andrei Emeltchenko2766be42012-09-27 17:26:21 +03001160void l2cap_send_conn_req(struct l2cap_chan *chan)
Andrei Emeltchenko9b27f352012-02-24 16:00:00 +02001161{
1162 struct l2cap_conn *conn = chan->conn;
1163 struct l2cap_conn_req req;
1164
1165 req.scid = cpu_to_le16(chan->scid);
1166 req.psm = chan->psm;
1167
1168 chan->ident = l2cap_get_ident(conn);
1169
1170 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
1171
1172 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req);
1173}
1174
Mat Martineau8eb200b2012-10-23 15:24:17 -07001175static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id)
1176{
1177 struct l2cap_create_chan_req req;
1178 req.scid = cpu_to_le16(chan->scid);
1179 req.psm = chan->psm;
1180 req.amp_id = amp_id;
1181
1182 chan->ident = l2cap_get_ident(chan->conn);
1183
1184 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ,
1185 sizeof(req), &req);
1186}
1187
Mat Martineau02b0fbb2012-10-23 15:24:10 -07001188static void l2cap_move_setup(struct l2cap_chan *chan)
1189{
1190 struct sk_buff *skb;
1191
1192 BT_DBG("chan %p", chan);
1193
1194 if (chan->mode != L2CAP_MODE_ERTM)
1195 return;
1196
1197 __clear_retrans_timer(chan);
1198 __clear_monitor_timer(chan);
1199 __clear_ack_timer(chan);
1200
1201 chan->retry_count = 0;
1202 skb_queue_walk(&chan->tx_q, skb) {
1203 if (bt_cb(skb)->control.retries)
1204 bt_cb(skb)->control.retries = 1;
1205 else
1206 break;
1207 }
1208
1209 chan->expected_tx_seq = chan->buffer_seq;
1210
1211 clear_bit(CONN_REJ_ACT, &chan->conn_state);
1212 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
1213 l2cap_seq_list_clear(&chan->retrans_list);
1214 l2cap_seq_list_clear(&chan->srej_list);
1215 skb_queue_purge(&chan->srej_q);
1216
1217 chan->tx_state = L2CAP_TX_STATE_XMIT;
1218 chan->rx_state = L2CAP_RX_STATE_MOVE;
1219
1220 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
1221}
1222
Mat Martineau5f3847a2012-10-23 15:24:12 -07001223static void l2cap_move_done(struct l2cap_chan *chan)
1224{
1225 u8 move_role = chan->move_role;
1226 BT_DBG("chan %p", chan);
1227
1228 chan->move_state = L2CAP_MOVE_STABLE;
1229 chan->move_role = L2CAP_MOVE_ROLE_NONE;
1230
1231 if (chan->mode != L2CAP_MODE_ERTM)
1232 return;
1233
1234 switch (move_role) {
1235 case L2CAP_MOVE_ROLE_INITIATOR:
1236 l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL);
1237 chan->rx_state = L2CAP_RX_STATE_WAIT_F;
1238 break;
1239 case L2CAP_MOVE_ROLE_RESPONDER:
1240 chan->rx_state = L2CAP_RX_STATE_WAIT_P;
1241 break;
1242 }
1243}
1244
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001245static void l2cap_chan_ready(struct l2cap_chan *chan)
1246{
Mat Martineau28270112012-05-17 21:14:09 -07001247 /* This clears all conf flags, including CONF_NOT_COMPLETE */
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001248 chan->conf_state = 0;
1249 __clear_chan_timer(chan);
1250
Johan Hedberg0ce43ce2013-12-05 14:55:33 +02001251 if (chan->mode == L2CAP_MODE_LE_FLOWCTL && !chan->tx_credits)
1252 chan->ops->suspend(chan);
Johan Hedberg177f8f22013-05-31 17:54:51 +03001253
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001254 chan->state = BT_CONNECTED;
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001255
Andrei Emeltchenkofd83e2c2012-05-30 09:55:32 +03001256 chan->ops->ready(chan);
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001257}
1258
Johan Hedbergf1496de2013-05-13 14:15:56 +03001259static void l2cap_le_connect(struct l2cap_chan *chan)
1260{
1261 struct l2cap_conn *conn = chan->conn;
1262 struct l2cap_le_conn_req req;
1263
Johan Hedberg595177f2013-12-02 22:12:22 +02001264 if (test_and_set_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags))
1265 return;
1266
Johan Hedbergf1496de2013-05-13 14:15:56 +03001267 req.psm = chan->psm;
1268 req.scid = cpu_to_le16(chan->scid);
1269 req.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02001270 req.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03001271 req.credits = cpu_to_le16(chan->rx_credits);
Johan Hedbergf1496de2013-05-13 14:15:56 +03001272
1273 chan->ident = l2cap_get_ident(conn);
1274
1275 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_REQ,
1276 sizeof(req), &req);
1277}
1278
1279static void l2cap_le_start(struct l2cap_chan *chan)
1280{
1281 struct l2cap_conn *conn = chan->conn;
1282
1283 if (!smp_conn_security(conn->hcon, chan->sec_level))
1284 return;
1285
1286 if (!chan->psm) {
1287 l2cap_chan_ready(chan);
1288 return;
1289 }
1290
1291 if (chan->state == BT_CONNECT)
1292 l2cap_le_connect(chan);
1293}
1294
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001295static void l2cap_start_connection(struct l2cap_chan *chan)
1296{
1297 if (__amp_capable(chan)) {
1298 BT_DBG("chan %p AMP capable: discover AMPs", chan);
1299 a2mp_discover_amp(chan);
Johan Hedbergf1496de2013-05-13 14:15:56 +03001300 } else if (chan->conn->hcon->type == LE_LINK) {
1301 l2cap_le_start(chan);
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001302 } else {
1303 l2cap_send_conn_req(chan);
1304 }
1305}
1306
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001307static void l2cap_request_info(struct l2cap_conn *conn)
1308{
1309 struct l2cap_info_req req;
1310
1311 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
1312 return;
1313
1314 req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
1315
1316 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
1317 conn->info_ident = l2cap_get_ident(conn);
1318
1319 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
1320
1321 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
1322 sizeof(req), &req);
1323}
1324
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001325static void l2cap_do_start(struct l2cap_chan *chan)
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001326{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001327 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001328
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001329 if (conn->hcon->type == LE_LINK) {
Johan Hedberg96ac34f2013-05-13 11:15:07 +03001330 l2cap_le_start(chan);
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001331 return;
1332 }
1333
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001334 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)) {
1335 l2cap_request_info(conn);
1336 return;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001337 }
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001338
1339 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
1340 return;
1341
1342 if (l2cap_chan_check_security(chan, true) &&
1343 __l2cap_no_conn_pending(chan))
1344 l2cap_start_connection(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001345}
1346
Gustavo F. Padovancf6c2c02010-06-07 20:54:45 -03001347static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
1348{
1349 u32 local_feat_mask = l2cap_feat_mask;
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03001350 if (!disable_ertm)
Gustavo F. Padovancf6c2c02010-06-07 20:54:45 -03001351 local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
1352
1353 switch (mode) {
1354 case L2CAP_MODE_ERTM:
1355 return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
1356 case L2CAP_MODE_STREAMING:
1357 return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
1358 default:
1359 return 0x00;
1360 }
1361}
1362
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001363static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err)
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001364{
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001365 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001366 struct l2cap_disconn_req req;
1367
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001368 if (!conn)
1369 return;
1370
Andrei Emeltchenkoaad3d0e2012-09-06 15:05:42 +03001371 if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) {
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -03001372 __clear_retrans_timer(chan);
1373 __clear_monitor_timer(chan);
1374 __clear_ack_timer(chan);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001375 }
1376
Johan Hedberg2338a7e2014-01-24 10:35:40 +02001377 if (chan->scid == L2CAP_CID_A2MP) {
Gustavo Padovand1177732012-10-06 11:47:38 +01001378 l2cap_state_change(chan, BT_DISCONN);
Andrei Emeltchenko416fa752012-05-29 13:59:16 +03001379 return;
1380 }
1381
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001382 req.dcid = cpu_to_le16(chan->dcid);
1383 req.scid = cpu_to_le16(chan->scid);
Gustavo Padovan2d792812012-10-06 10:07:01 +01001384 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ,
1385 sizeof(req), &req);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001386
Gustavo Padovanf8e73012013-10-15 19:24:46 -03001387 l2cap_state_change_and_error(chan, BT_DISCONN, err);
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001388}
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390/* ---- L2CAP connections ---- */
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001391static void l2cap_conn_start(struct l2cap_conn *conn)
1392{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001393 struct l2cap_chan *chan, *tmp;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001394
1395 BT_DBG("conn %p", conn);
1396
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001397 mutex_lock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001398
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001399 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001400 l2cap_chan_lock(chan);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001401
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001402 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001403 l2cap_chan_ready(chan);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001404 l2cap_chan_unlock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001405 continue;
1406 }
1407
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001408 if (chan->state == BT_CONNECT) {
Johan Hedberge7cafc42014-07-17 15:35:38 +03001409 if (!l2cap_chan_check_security(chan, true) ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01001410 !__l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001411 l2cap_chan_unlock(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001412 continue;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02001413 }
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001414
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001415 if (!l2cap_mode_supported(chan->mode, conn->feat_mask)
Gustavo Padovan2d792812012-10-06 10:07:01 +01001416 && test_bit(CONF_STATE2_DEVICE,
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001417 &chan->conf_state)) {
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001418 l2cap_chan_close(chan, ECONNRESET);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001419 l2cap_chan_unlock(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001420 continue;
1421 }
1422
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001423 l2cap_start_connection(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001424
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001425 } else if (chan->state == BT_CONNECT2) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001426 struct l2cap_conn_rsp rsp;
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001427 char buf[128];
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001428 rsp.scid = cpu_to_le16(chan->dcid);
1429 rsp.dcid = cpu_to_le16(chan->scid);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001430
Johan Hedberge7cafc42014-07-17 15:35:38 +03001431 if (l2cap_chan_check_security(chan, false)) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07001432 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001433 rsp.result = cpu_to_le16(L2CAP_CR_PEND);
1434 rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08001435 chan->ops->defer(chan);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001436
1437 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02001438 l2cap_state_change(chan, BT_CONFIG);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001439 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
1440 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001441 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001442 } else {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001443 rsp.result = cpu_to_le16(L2CAP_CR_PEND);
1444 rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001445 }
1446
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001447 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001448 sizeof(rsp), &rsp);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001449
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001450 if (test_bit(CONF_REQ_SENT, &chan->conf_state) ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01001451 rsp.result != L2CAP_CR_SUCCESS) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001452 l2cap_chan_unlock(chan);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001453 continue;
1454 }
1455
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001456 set_bit(CONF_REQ_SENT, &chan->conf_state);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001457 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001458 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03001459 chan->num_conf_req++;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001460 }
1461
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001462 l2cap_chan_unlock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001463 }
1464
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001465 mutex_unlock(&conn->chan_lock);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001466}
1467
Ville Tervob62f3282011-02-10 22:38:50 -03001468static void l2cap_le_conn_ready(struct l2cap_conn *conn)
1469{
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07001470 struct hci_conn *hcon = conn->hcon;
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001471 struct hci_dev *hdev = hcon->hdev;
Ville Tervob62f3282011-02-10 22:38:50 -03001472
Johan Hedberge760ec12014-08-07 22:56:47 +03001473 BT_DBG("%s conn %p", hdev->name, conn);
Ville Tervob62f3282011-02-10 22:38:50 -03001474
Johan Hedberge760ec12014-08-07 22:56:47 +03001475 /* For outgoing pairing which doesn't necessarily have an
1476 * associated socket (e.g. mgmt_pair_device).
1477 */
1478 if (hcon->out)
1479 smp_conn_security(hcon, hcon->pending_sec_level);
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07001480
Marcel Holtmann80afeb62014-06-23 12:18:51 +02001481 /* For LE slave connections, make sure the connection interval
1482 * is in the range of the minium and maximum interval that has
1483 * been configured for this connection. If not, then trigger
1484 * the connection update procedure.
1485 */
Johan Hedberg40bef302014-07-16 11:42:27 +03001486 if (hcon->role == HCI_ROLE_SLAVE &&
Marcel Holtmann80afeb62014-06-23 12:18:51 +02001487 (hcon->le_conn_interval < hcon->le_conn_min_interval ||
1488 hcon->le_conn_interval > hcon->le_conn_max_interval)) {
1489 struct l2cap_conn_param_update_req req;
1490
1491 req.min = cpu_to_le16(hcon->le_conn_min_interval);
1492 req.max = cpu_to_le16(hcon->le_conn_max_interval);
1493 req.latency = cpu_to_le16(hcon->le_conn_latency);
1494 req.to_multiplier = cpu_to_le16(hcon->le_supv_timeout);
1495
1496 l2cap_send_cmd(conn, l2cap_get_ident(conn),
1497 L2CAP_CONN_PARAM_UPDATE_REQ, sizeof(req), &req);
1498 }
Ville Tervob62f3282011-02-10 22:38:50 -03001499}
1500
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001501static void l2cap_conn_ready(struct l2cap_conn *conn)
1502{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001503 struct l2cap_chan *chan;
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001504 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001505
1506 BT_DBG("conn %p", conn);
1507
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001508 if (hcon->type == ACL_LINK)
1509 l2cap_request_info(conn);
1510
Johan Hedberge760ec12014-08-07 22:56:47 +03001511 mutex_lock(&conn->chan_lock);
1512
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001513 list_for_each_entry(chan, &conn->chan_l, list) {
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03001514
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001515 l2cap_chan_lock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001516
Johan Hedberg2338a7e2014-01-24 10:35:40 +02001517 if (chan->scid == L2CAP_CID_A2MP) {
Andrei Emeltchenko416fa752012-05-29 13:59:16 +03001518 l2cap_chan_unlock(chan);
1519 continue;
1520 }
1521
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001522 if (hcon->type == LE_LINK) {
Johan Hedbergf1496de2013-05-13 14:15:56 +03001523 l2cap_le_start(chan);
Vinicius Costa Gomes63128452011-06-17 22:46:26 -03001524 } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001525 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
1526 l2cap_chan_ready(chan);
Gustavo Padovan1c244f72012-12-07 03:29:10 -02001527 } else if (chan->state == BT_CONNECT) {
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001528 l2cap_do_start(chan);
Gustavo Padovan1c244f72012-12-07 03:29:10 -02001529 }
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001530
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001531 l2cap_chan_unlock(chan);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001532 }
1533
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001534 mutex_unlock(&conn->chan_lock);
Johan Hedberg61a939c2014-01-17 20:45:11 +02001535
Johan Hedberg79a05722014-08-08 09:28:04 +03001536 if (hcon->type == LE_LINK)
1537 l2cap_le_conn_ready(conn);
1538
Johan Hedberg61a939c2014-01-17 20:45:11 +02001539 queue_work(hcon->hdev->workqueue, &conn->pending_rx_work);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001540}
1541
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001542/* Notify sockets that we cannot guaranty reliability anymore */
1543static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
1544{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001545 struct l2cap_chan *chan;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001546
1547 BT_DBG("conn %p", conn);
1548
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001549 mutex_lock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001550
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001551 list_for_each_entry(chan, &conn->chan_l, list) {
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +03001552 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
Gustavo Padovan1d8b1fd2012-10-06 11:34:52 +01001553 l2cap_chan_set_err(chan, err);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001554 }
1555
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001556 mutex_unlock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001557}
1558
Gustavo F. Padovanf878fca2011-12-15 01:16:14 -02001559static void l2cap_info_timeout(struct work_struct *work)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001560{
Gustavo F. Padovanf878fca2011-12-15 01:16:14 -02001561 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001562 info_timer.work);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001563
Marcel Holtmann984947d2009-02-06 23:35:19 +01001564 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01001565 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01001566
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001567 l2cap_conn_start(conn);
1568}
1569
David Herrmann2c8e1412013-04-06 20:28:45 +02001570/*
1571 * l2cap_user
1572 * External modules can register l2cap_user objects on l2cap_conn. The ->probe
1573 * callback is called during registration. The ->remove callback is called
1574 * during unregistration.
1575 * An l2cap_user object can either be explicitly unregistered or when the
1576 * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon,
1577 * l2cap->hchan, .. are valid as long as the remove callback hasn't been called.
1578 * External modules must own a reference to the l2cap_conn object if they intend
1579 * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at
1580 * any time if they don't.
1581 */
1582
1583int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user)
1584{
1585 struct hci_dev *hdev = conn->hcon->hdev;
1586 int ret;
1587
1588 /* We need to check whether l2cap_conn is registered. If it is not, we
1589 * must not register the l2cap_user. l2cap_conn_del() is unregisters
1590 * l2cap_conn objects, but doesn't provide its own locking. Instead, it
1591 * relies on the parent hci_conn object to be locked. This itself relies
1592 * on the hci_dev object to be locked. So we must lock the hci device
1593 * here, too. */
1594
1595 hci_dev_lock(hdev);
1596
1597 if (user->list.next || user->list.prev) {
1598 ret = -EINVAL;
1599 goto out_unlock;
1600 }
1601
1602 /* conn->hchan is NULL after l2cap_conn_del() was called */
1603 if (!conn->hchan) {
1604 ret = -ENODEV;
1605 goto out_unlock;
1606 }
1607
1608 ret = user->probe(conn, user);
1609 if (ret)
1610 goto out_unlock;
1611
1612 list_add(&user->list, &conn->users);
1613 ret = 0;
1614
1615out_unlock:
1616 hci_dev_unlock(hdev);
1617 return ret;
1618}
1619EXPORT_SYMBOL(l2cap_register_user);
1620
1621void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user)
1622{
1623 struct hci_dev *hdev = conn->hcon->hdev;
1624
1625 hci_dev_lock(hdev);
1626
1627 if (!user->list.next || !user->list.prev)
1628 goto out_unlock;
1629
1630 list_del(&user->list);
1631 user->list.next = NULL;
1632 user->list.prev = NULL;
1633 user->remove(conn, user);
1634
1635out_unlock:
1636 hci_dev_unlock(hdev);
1637}
1638EXPORT_SYMBOL(l2cap_unregister_user);
1639
1640static void l2cap_unregister_all_users(struct l2cap_conn *conn)
1641{
1642 struct l2cap_user *user;
1643
1644 while (!list_empty(&conn->users)) {
1645 user = list_first_entry(&conn->users, struct l2cap_user, list);
1646 list_del(&user->list);
1647 user->list.next = NULL;
1648 user->list.prev = NULL;
1649 user->remove(conn, user);
1650 }
1651}
1652
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001653static void l2cap_conn_del(struct hci_conn *hcon, int err)
1654{
1655 struct l2cap_conn *conn = hcon->l2cap_data;
1656 struct l2cap_chan *chan, *l;
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001657
1658 if (!conn)
1659 return;
1660
1661 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
1662
1663 kfree_skb(conn->rx_skb);
1664
Johan Hedberg61a939c2014-01-17 20:45:11 +02001665 skb_queue_purge(&conn->pending_rx);
Jukka Taimisto7ab56c32014-06-12 10:15:13 +00001666
1667 /* We can not call flush_work(&conn->pending_rx_work) here since we
1668 * might block if we are running on a worker from the same workqueue
1669 * pending_rx_work is waiting on.
1670 */
1671 if (work_pending(&conn->pending_rx_work))
1672 cancel_work_sync(&conn->pending_rx_work);
Johan Hedberg61a939c2014-01-17 20:45:11 +02001673
Johan Hedbergf3d82d02014-09-05 22:19:50 +03001674 if (work_pending(&conn->id_addr_update_work))
1675 cancel_work_sync(&conn->id_addr_update_work);
1676
David Herrmann2c8e1412013-04-06 20:28:45 +02001677 l2cap_unregister_all_users(conn);
1678
Johan Hedberge31fb862014-08-18 20:33:28 +03001679 /* Force the connection to be immediately dropped */
1680 hcon->disc_timeout = 0;
1681
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001682 mutex_lock(&conn->chan_lock);
1683
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001684 /* Kill channels */
1685 list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
Mat Martineau61d6ef32012-04-27 16:50:50 -07001686 l2cap_chan_hold(chan);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001687 l2cap_chan_lock(chan);
1688
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001689 l2cap_chan_del(chan, err);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001690
1691 l2cap_chan_unlock(chan);
1692
Gustavo Padovan80b98022012-05-27 22:27:51 -03001693 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07001694 l2cap_chan_put(chan);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001695 }
1696
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001697 mutex_unlock(&conn->chan_lock);
1698
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001699 hci_chan_del(conn->hchan);
1700
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001701 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
Ulisses Furquim127074b2012-01-30 18:26:29 -02001702 cancel_delayed_work_sync(&conn->info_timer);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001703
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001704 hcon->l2cap_data = NULL;
David Herrmann9c903e32013-04-06 20:28:44 +02001705 conn->hchan = NULL;
1706 l2cap_conn_put(conn);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001707}
1708
David Herrmann9c903e32013-04-06 20:28:44 +02001709static void l2cap_conn_free(struct kref *ref)
1710{
1711 struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref);
1712
1713 hci_conn_put(conn->hcon);
1714 kfree(conn);
1715}
1716
Johan Hedberg51bb84572014-08-15 21:06:57 +03001717struct l2cap_conn *l2cap_conn_get(struct l2cap_conn *conn)
David Herrmann9c903e32013-04-06 20:28:44 +02001718{
1719 kref_get(&conn->ref);
Johan Hedberg51bb84572014-08-15 21:06:57 +03001720 return conn;
David Herrmann9c903e32013-04-06 20:28:44 +02001721}
1722EXPORT_SYMBOL(l2cap_conn_get);
1723
1724void l2cap_conn_put(struct l2cap_conn *conn)
1725{
1726 kref_put(&conn->ref, l2cap_conn_free);
1727}
1728EXPORT_SYMBOL(l2cap_conn_put);
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730/* ---- Socket interface ---- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Ido Yarivc2287682012-04-20 15:46:07 -03001732/* Find socket with psm and source / destination bdaddr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 * Returns closest match.
1734 */
Ido Yarivc2287682012-04-20 15:46:07 -03001735static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm,
1736 bdaddr_t *src,
Johan Hedbergbf20fd42013-05-14 13:23:13 +03001737 bdaddr_t *dst,
1738 u8 link_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001740 struct l2cap_chan *c, *c1 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001742 read_lock(&chan_list_lock);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00001743
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001744 list_for_each_entry(c, &chan_list, global_l) {
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001745 if (state && c->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 continue;
1747
Johan Hedbergbf20fd42013-05-14 13:23:13 +03001748 if (link_type == ACL_LINK && c->src_type != BDADDR_BREDR)
1749 continue;
1750
1751 if (link_type == LE_LINK && c->src_type == BDADDR_BREDR)
1752 continue;
1753
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001754 if (c->psm == psm) {
Ido Yarivc2287682012-04-20 15:46:07 -03001755 int src_match, dst_match;
1756 int src_any, dst_any;
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /* Exact match. */
Marcel Holtmann7eafc592013-10-13 08:12:47 -07001759 src_match = !bacmp(&c->src, src);
1760 dst_match = !bacmp(&c->dst, dst);
Ido Yarivc2287682012-04-20 15:46:07 -03001761 if (src_match && dst_match) {
Johan Hedberga24cce12014-08-07 22:56:42 +03001762 l2cap_chan_hold(c);
Johannes Berga7567b22011-06-01 08:29:54 +02001763 read_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001764 return c;
1765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 /* Closest match */
Marcel Holtmann7eafc592013-10-13 08:12:47 -07001768 src_any = !bacmp(&c->src, BDADDR_ANY);
1769 dst_any = !bacmp(&c->dst, BDADDR_ANY);
Ido Yarivc2287682012-04-20 15:46:07 -03001770 if ((src_match && dst_any) || (src_any && dst_match) ||
1771 (src_any && dst_any))
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001772 c1 = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Johan Hedberga24cce12014-08-07 22:56:42 +03001776 if (c1)
1777 l2cap_chan_hold(c1);
1778
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001779 read_unlock(&chan_list_lock);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00001780
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001781 return c1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001784static void l2cap_monitor_timeout(struct work_struct *work)
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001785{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001786 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau4239d162012-05-17 20:53:49 -07001787 monitor_timer.work);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001788
Gustavo F. Padovan525cd182011-03-25 19:43:39 -03001789 BT_DBG("chan %p", chan);
Gustavo F. Padovan0e989582010-04-19 14:45:38 -03001790
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001791 l2cap_chan_lock(chan);
1792
Mat Martineau80909e02012-05-17 20:53:50 -07001793 if (!chan->conn) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001794 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001795 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001796 return;
1797 }
1798
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03001799 l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001800
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001801 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001802 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001803}
1804
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001805static void l2cap_retrans_timeout(struct work_struct *work)
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001806{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001807 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau4239d162012-05-17 20:53:49 -07001808 retrans_timer.work);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001809
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03001810 BT_DBG("chan %p", chan);
Gustavo F. Padovan0e989582010-04-19 14:45:38 -03001811
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001812 l2cap_chan_lock(chan);
1813
Mat Martineau80909e02012-05-17 20:53:50 -07001814 if (!chan->conn) {
1815 l2cap_chan_unlock(chan);
1816 l2cap_chan_put(chan);
1817 return;
1818 }
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001819
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03001820 l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001821 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001822 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001823}
1824
Gustavo Padovand6603662012-05-21 13:58:22 -03001825static void l2cap_streaming_send(struct l2cap_chan *chan,
1826 struct sk_buff_head *skbs)
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001827{
Gustavo F. Padovanccbb84a2010-08-30 18:44:44 -03001828 struct sk_buff *skb;
Mat Martineau37339372012-05-17 20:53:33 -07001829 struct l2cap_ctrl *control;
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001830
Mat Martineau37339372012-05-17 20:53:33 -07001831 BT_DBG("chan %p, skbs %p", chan, skbs);
1832
Mat Martineaub99e13a2012-10-23 15:24:19 -07001833 if (__chan_is_moving(chan))
1834 return;
1835
Mat Martineau37339372012-05-17 20:53:33 -07001836 skb_queue_splice_tail_init(skbs, &chan->tx_q);
1837
1838 while (!skb_queue_empty(&chan->tx_q)) {
1839
1840 skb = skb_dequeue(&chan->tx_q);
1841
1842 bt_cb(skb)->control.retries = 1;
1843 control = &bt_cb(skb)->control;
1844
1845 control->reqseq = 0;
1846 control->txseq = chan->next_tx_seq;
1847
1848 __pack_control(chan, control, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001849
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001850 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineau37339372012-05-17 20:53:33 -07001851 u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
1852 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001853 }
1854
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001855 l2cap_do_send(chan, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001856
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001857 BT_DBG("Sent txseq %u", control->txseq);
Mat Martineau37339372012-05-17 20:53:33 -07001858
Andrei Emeltchenko836be932011-10-17 12:19:57 +03001859 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
Mat Martineau37339372012-05-17 20:53:33 -07001860 chan->frames_sent++;
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001861 }
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001862}
1863
Szymon Janc67c9e842011-07-28 16:24:33 +02001864static int l2cap_ertm_send(struct l2cap_chan *chan)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001865{
1866 struct sk_buff *skb, *tx_skb;
Mat Martineau18a48e72012-05-17 20:53:34 -07001867 struct l2cap_ctrl *control;
1868 int sent = 0;
1869
1870 BT_DBG("chan %p", chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001871
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001872 if (chan->state != BT_CONNECTED)
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001873 return -ENOTCONN;
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001874
Mat Martineau94122bb2012-05-02 09:42:02 -07001875 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
1876 return 0;
1877
Mat Martineaub99e13a2012-10-23 15:24:19 -07001878 if (__chan_is_moving(chan))
1879 return 0;
1880
Mat Martineau18a48e72012-05-17 20:53:34 -07001881 while (chan->tx_send_head &&
1882 chan->unacked_frames < chan->remote_tx_win &&
1883 chan->tx_state == L2CAP_TX_STATE_XMIT) {
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001884
Mat Martineau18a48e72012-05-17 20:53:34 -07001885 skb = chan->tx_send_head;
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001886
Mat Martineau18a48e72012-05-17 20:53:34 -07001887 bt_cb(skb)->control.retries = 1;
1888 control = &bt_cb(skb)->control;
Gustavo F. Padovan95ffa972010-06-18 20:37:33 -03001889
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03001890 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
Mat Martineau18a48e72012-05-17 20:53:34 -07001891 control->final = 1;
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03001892
Mat Martineau18a48e72012-05-17 20:53:34 -07001893 control->reqseq = chan->buffer_seq;
1894 chan->last_acked_seq = chan->buffer_seq;
1895 control->txseq = chan->next_tx_seq;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001896
Mat Martineau18a48e72012-05-17 20:53:34 -07001897 __pack_control(chan, control, skb);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001898
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001899 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineau18a48e72012-05-17 20:53:34 -07001900 u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
1901 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001902 }
1903
Mat Martineau18a48e72012-05-17 20:53:34 -07001904 /* Clone after data has been modified. Data is assumed to be
1905 read-only (for locking purposes) on cloned sk_buffs.
1906 */
1907 tx_skb = skb_clone(skb, GFP_KERNEL);
1908
1909 if (!tx_skb)
1910 break;
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -03001911
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -03001912 __set_retrans_timer(chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001913
Andrei Emeltchenko836be932011-10-17 12:19:57 +03001914 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
Mat Martineau18a48e72012-05-17 20:53:34 -07001915 chan->unacked_frames++;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03001916 chan->frames_sent++;
Mat Martineau18a48e72012-05-17 20:53:34 -07001917 sent++;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001918
Gustavo F. Padovan58d35f82011-04-04 16:16:44 -03001919 if (skb_queue_is_last(&chan->tx_q, skb))
1920 chan->tx_send_head = NULL;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001921 else
Gustavo F. Padovan58d35f82011-04-04 16:16:44 -03001922 chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
Mat Martineau18a48e72012-05-17 20:53:34 -07001923
1924 l2cap_do_send(chan, tx_skb);
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001925 BT_DBG("Sent txseq %u", control->txseq);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001926 }
1927
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001928 BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent,
1929 chan->unacked_frames, skb_queue_len(&chan->tx_q));
Mat Martineau18a48e72012-05-17 20:53:34 -07001930
1931 return sent;
Gustavo F. Padovan9e917af2010-05-01 16:15:37 -03001932}
1933
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001934static void l2cap_ertm_resend(struct l2cap_chan *chan)
1935{
1936 struct l2cap_ctrl control;
1937 struct sk_buff *skb;
1938 struct sk_buff *tx_skb;
1939 u16 seq;
1940
1941 BT_DBG("chan %p", chan);
1942
1943 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
1944 return;
1945
Mat Martineaub99e13a2012-10-23 15:24:19 -07001946 if (__chan_is_moving(chan))
1947 return;
1948
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001949 while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) {
1950 seq = l2cap_seq_list_pop(&chan->retrans_list);
1951
1952 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq);
1953 if (!skb) {
1954 BT_DBG("Error: Can't retransmit seq %d, frame missing",
Gustavo Padovan2d792812012-10-06 10:07:01 +01001955 seq);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001956 continue;
1957 }
1958
1959 bt_cb(skb)->control.retries++;
1960 control = bt_cb(skb)->control;
1961
1962 if (chan->max_tx != 0 &&
1963 bt_cb(skb)->control.retries > chan->max_tx) {
1964 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001965 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001966 l2cap_seq_list_clear(&chan->retrans_list);
1967 break;
1968 }
1969
1970 control.reqseq = chan->buffer_seq;
1971 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
1972 control.final = 1;
1973 else
1974 control.final = 0;
1975
1976 if (skb_cloned(skb)) {
1977 /* Cloned sk_buffs are read-only, so we need a
1978 * writeable copy
1979 */
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03001980 tx_skb = skb_copy(skb, GFP_KERNEL);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001981 } else {
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03001982 tx_skb = skb_clone(skb, GFP_KERNEL);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001983 }
1984
1985 if (!tx_skb) {
1986 l2cap_seq_list_clear(&chan->retrans_list);
1987 break;
1988 }
1989
1990 /* Update skb contents */
1991 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
1992 put_unaligned_le32(__pack_extended_control(&control),
1993 tx_skb->data + L2CAP_HDR_SIZE);
1994 } else {
1995 put_unaligned_le16(__pack_enhanced_control(&control),
1996 tx_skb->data + L2CAP_HDR_SIZE);
1997 }
1998
Lukasz Rymanowski13cac152014-08-14 09:35:34 +02001999 /* Update FCS */
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002000 if (chan->fcs == L2CAP_FCS_CRC16) {
Lukasz Rymanowski13cac152014-08-14 09:35:34 +02002001 u16 fcs = crc16(0, (u8 *) tx_skb->data,
2002 tx_skb->len - L2CAP_FCS_SIZE);
2003 put_unaligned_le16(fcs, skb_tail_pointer(tx_skb) -
2004 L2CAP_FCS_SIZE);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002005 }
2006
2007 l2cap_do_send(chan, tx_skb);
2008
2009 BT_DBG("Resent txseq %d", control.txseq);
2010
2011 chan->last_acked_seq = chan->buffer_seq;
2012 }
2013}
2014
Mat Martineauf80842a2012-05-17 20:53:46 -07002015static void l2cap_retransmit(struct l2cap_chan *chan,
2016 struct l2cap_ctrl *control)
2017{
2018 BT_DBG("chan %p, control %p", chan, control);
2019
2020 l2cap_seq_list_append(&chan->retrans_list, control->reqseq);
2021 l2cap_ertm_resend(chan);
2022}
2023
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002024static void l2cap_retransmit_all(struct l2cap_chan *chan,
2025 struct l2cap_ctrl *control)
2026{
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002027 struct sk_buff *skb;
2028
2029 BT_DBG("chan %p, control %p", chan, control);
2030
2031 if (control->poll)
2032 set_bit(CONN_SEND_FBIT, &chan->conn_state);
2033
2034 l2cap_seq_list_clear(&chan->retrans_list);
2035
2036 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
2037 return;
2038
2039 if (chan->unacked_frames) {
2040 skb_queue_walk(&chan->tx_q, skb) {
2041 if (bt_cb(skb)->control.txseq == control->reqseq ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01002042 skb == chan->tx_send_head)
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002043 break;
2044 }
2045
2046 skb_queue_walk_from(&chan->tx_q, skb) {
2047 if (skb == chan->tx_send_head)
2048 break;
2049
2050 l2cap_seq_list_append(&chan->retrans_list,
2051 bt_cb(skb)->control.txseq);
2052 }
2053
2054 l2cap_ertm_resend(chan);
2055 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002056}
2057
Szymon Jancb17e73b2012-01-11 10:59:47 +01002058static void l2cap_send_ack(struct l2cap_chan *chan)
2059{
Mat Martineau0a0aba42012-05-17 20:53:39 -07002060 struct l2cap_ctrl control;
2061 u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
2062 chan->last_acked_seq);
2063 int threshold;
2064
2065 BT_DBG("chan %p last_acked_seq %d buffer_seq %d",
2066 chan, chan->last_acked_seq, chan->buffer_seq);
2067
2068 memset(&control, 0, sizeof(control));
2069 control.sframe = 1;
2070
2071 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
2072 chan->rx_state == L2CAP_RX_STATE_RECV) {
2073 __clear_ack_timer(chan);
2074 control.super = L2CAP_SUPER_RNR;
2075 control.reqseq = chan->buffer_seq;
2076 l2cap_send_sframe(chan, &control);
2077 } else {
2078 if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) {
2079 l2cap_ertm_send(chan);
2080 /* If any i-frames were sent, they included an ack */
2081 if (chan->buffer_seq == chan->last_acked_seq)
2082 frames_to_ack = 0;
2083 }
2084
Mat Martineauc20f8e32012-07-10 05:47:07 -07002085 /* Ack now if the window is 3/4ths full.
Mat Martineau0a0aba42012-05-17 20:53:39 -07002086 * Calculate without mul or div
2087 */
Mat Martineauc20f8e32012-07-10 05:47:07 -07002088 threshold = chan->ack_win;
Mat Martineau0a0aba42012-05-17 20:53:39 -07002089 threshold += threshold << 1;
2090 threshold >>= 2;
2091
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002092 BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack,
Mat Martineau0a0aba42012-05-17 20:53:39 -07002093 threshold);
2094
2095 if (frames_to_ack >= threshold) {
2096 __clear_ack_timer(chan);
2097 control.super = L2CAP_SUPER_RR;
2098 control.reqseq = chan->buffer_seq;
2099 l2cap_send_sframe(chan, &control);
2100 frames_to_ack = 0;
2101 }
2102
2103 if (frames_to_ack)
2104 __set_ack_timer(chan);
2105 }
Szymon Jancb17e73b2012-01-11 10:59:47 +01002106}
2107
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002108static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan,
2109 struct msghdr *msg, int len,
2110 int count, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002112 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002113 struct sk_buff **frag;
Gustavo Padovan90338942012-04-06 20:15:47 -03002114 int sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Al Viro17836392014-11-24 17:07:38 -05002116 if (copy_from_iter(skb_put(skb, count), count, &msg->msg_iter) != count)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002117 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 sent += count;
2120 len -= count;
2121
2122 /* Continuation fragments (no L2CAP header) */
2123 frag = &skb_shinfo(skb)->frag_list;
2124 while (len) {
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002125 struct sk_buff *tmp;
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 count = min_t(unsigned int, conn->mtu, len);
2128
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002129 tmp = chan->ops->alloc_skb(chan, 0, count,
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002130 msg->msg_flags & MSG_DONTWAIT);
2131 if (IS_ERR(tmp))
2132 return PTR_ERR(tmp);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002133
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002134 *frag = tmp;
2135
Al Viro17836392014-11-24 17:07:38 -05002136 if (copy_from_iter(skb_put(*frag, count), count,
2137 &msg->msg_iter) != count)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002138 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 sent += count;
2141 len -= count;
2142
Gustavo Padovan2d0ed3d2012-05-11 13:16:12 -03002143 skb->len += (*frag)->len;
2144 skb->data_len += (*frag)->len;
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 frag = &(*frag)->next;
2147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 return sent;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002150}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002152static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan,
Marcel Holtmann8d463212014-06-05 15:22:51 +02002153 struct msghdr *msg, size_t len)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002154{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002155 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002156 struct sk_buff *skb;
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002157 int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002158 struct l2cap_hdr *lh;
2159
Marcel Holtmann8d463212014-06-05 15:22:51 +02002160 BT_DBG("chan %p psm 0x%2.2x len %zu", chan,
2161 __le16_to_cpu(chan->psm), len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002162
2163 count = min_t(unsigned int, (conn->mtu - hlen), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002164
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002165 skb = chan->ops->alloc_skb(chan, hlen, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002166 msg->msg_flags & MSG_DONTWAIT);
2167 if (IS_ERR(skb))
2168 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002169
2170 /* Create L2CAP header */
2171 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002172 lh->cid = cpu_to_le16(chan->dcid);
Andrei Emeltchenkodaf6a78c2012-05-03 10:55:52 +03002173 lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE);
Marcel Holtmann43b1b8d2013-10-12 06:01:26 -07002174 put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002175
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002176 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002177 if (unlikely(err < 0)) {
2178 kfree_skb(skb);
2179 return ERR_PTR(err);
2180 }
2181 return skb;
2182}
2183
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002184static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan,
Marcel Holtmann8d463212014-06-05 15:22:51 +02002185 struct msghdr *msg, size_t len)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002186{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002187 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002188 struct sk_buff *skb;
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002189 int err, count;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002190 struct l2cap_hdr *lh;
2191
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002192 BT_DBG("chan %p len %zu", chan, len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002193
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002194 count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002195
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002196 skb = chan->ops->alloc_skb(chan, L2CAP_HDR_SIZE, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002197 msg->msg_flags & MSG_DONTWAIT);
2198 if (IS_ERR(skb))
2199 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002200
2201 /* Create L2CAP header */
2202 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002203 lh->cid = cpu_to_le16(chan->dcid);
Gustavo Padovan6ff9b5e2012-05-02 11:56:17 -03002204 lh->len = cpu_to_le16(len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002205
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002206 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002207 if (unlikely(err < 0)) {
2208 kfree_skb(skb);
2209 return ERR_PTR(err);
2210 }
2211 return skb;
2212}
2213
Luiz Augusto von Dentzab0ff762011-09-12 20:00:50 +03002214static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan,
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002215 struct msghdr *msg, size_t len,
2216 u16 sdulen)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002217{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002218 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002219 struct sk_buff *skb;
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03002220 int err, count, hlen;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002221 struct l2cap_hdr *lh;
2222
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002223 BT_DBG("chan %p len %zu", chan, len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002224
Gustavo F. Padovan0ee0d202010-05-01 16:15:41 -03002225 if (!conn)
2226 return ERR_PTR(-ENOTCONN);
2227
Gustavo Padovanba7aa642012-05-29 13:29:16 -03002228 hlen = __ertm_hdr_size(chan);
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03002229
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002230 if (sdulen)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002231 hlen += L2CAP_SDULEN_SIZE;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002232
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03002233 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002234 hlen += L2CAP_FCS_SIZE;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03002235
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002236 count = min_t(unsigned int, (conn->mtu - hlen), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002237
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002238 skb = chan->ops->alloc_skb(chan, hlen, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002239 msg->msg_flags & MSG_DONTWAIT);
2240 if (IS_ERR(skb))
2241 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002242
2243 /* Create L2CAP header */
2244 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002245 lh->cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002246 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03002247
Mat Martineau18a48e72012-05-17 20:53:34 -07002248 /* Control header is populated later */
2249 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
2250 put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
2251 else
2252 put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03002253
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002254 if (sdulen)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002255 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002256
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002257 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002258 if (unlikely(err < 0)) {
2259 kfree_skb(skb);
2260 return ERR_PTR(err);
2261 }
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03002262
Mat Martineau18a48e72012-05-17 20:53:34 -07002263 bt_cb(skb)->control.fcs = chan->fcs;
Mat Martineau3ce35142012-04-25 16:36:14 -07002264 bt_cb(skb)->control.retries = 0;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002265 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Mat Martineau94122bb2012-05-02 09:42:02 -07002268static int l2cap_segment_sdu(struct l2cap_chan *chan,
2269 struct sk_buff_head *seg_queue,
2270 struct msghdr *msg, size_t len)
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002271{
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002272 struct sk_buff *skb;
Mat Martineau94122bb2012-05-02 09:42:02 -07002273 u16 sdu_len;
2274 size_t pdu_len;
Mat Martineau94122bb2012-05-02 09:42:02 -07002275 u8 sar;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002276
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002277 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002278
Mat Martineau94122bb2012-05-02 09:42:02 -07002279 /* It is critical that ERTM PDUs fit in a single HCI fragment,
2280 * so fragmented skbs are not used. The HCI layer's handling
2281 * of fragmented skbs is not compatible with ERTM's queueing.
2282 */
2283
2284 /* PDU size is derived from the HCI MTU */
2285 pdu_len = chan->conn->mtu;
2286
Mat Martineaua5495742012-10-23 15:24:21 -07002287 /* Constrain PDU size for BR/EDR connections */
2288 if (!chan->hs_hcon)
2289 pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD);
Mat Martineau94122bb2012-05-02 09:42:02 -07002290
2291 /* Adjust for largest possible L2CAP overhead. */
Gustavo Padovan35d401d2012-05-25 18:57:05 -03002292 if (chan->fcs)
2293 pdu_len -= L2CAP_FCS_SIZE;
2294
Gustavo Padovanba7aa642012-05-29 13:29:16 -03002295 pdu_len -= __ertm_hdr_size(chan);
Mat Martineau94122bb2012-05-02 09:42:02 -07002296
2297 /* Remote device may have requested smaller PDUs */
2298 pdu_len = min_t(size_t, pdu_len, chan->remote_mps);
2299
2300 if (len <= pdu_len) {
2301 sar = L2CAP_SAR_UNSEGMENTED;
2302 sdu_len = 0;
2303 pdu_len = len;
2304 } else {
2305 sar = L2CAP_SAR_START;
2306 sdu_len = len;
Mat Martineau94122bb2012-05-02 09:42:02 -07002307 }
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002308
2309 while (len > 0) {
Mat Martineau94122bb2012-05-02 09:42:02 -07002310 skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002311
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002312 if (IS_ERR(skb)) {
Mat Martineau94122bb2012-05-02 09:42:02 -07002313 __skb_queue_purge(seg_queue);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002314 return PTR_ERR(skb);
2315 }
2316
Mat Martineau94122bb2012-05-02 09:42:02 -07002317 bt_cb(skb)->control.sar = sar;
2318 __skb_queue_tail(seg_queue, skb);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002319
Mat Martineau94122bb2012-05-02 09:42:02 -07002320 len -= pdu_len;
Lukasz Rymanowski069cb272014-08-13 16:01:41 +02002321 if (sdu_len)
Mat Martineau94122bb2012-05-02 09:42:02 -07002322 sdu_len = 0;
Mat Martineau94122bb2012-05-02 09:42:02 -07002323
2324 if (len <= pdu_len) {
2325 sar = L2CAP_SAR_END;
2326 pdu_len = len;
2327 } else {
2328 sar = L2CAP_SAR_CONTINUE;
2329 }
2330 }
2331
Gustavo Padovanf0f62792012-05-29 13:29:17 -03002332 return 0;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002333}
2334
Johan Hedberg177f8f22013-05-31 17:54:51 +03002335static struct sk_buff *l2cap_create_le_flowctl_pdu(struct l2cap_chan *chan,
2336 struct msghdr *msg,
2337 size_t len, u16 sdulen)
2338{
2339 struct l2cap_conn *conn = chan->conn;
2340 struct sk_buff *skb;
2341 int err, count, hlen;
2342 struct l2cap_hdr *lh;
2343
2344 BT_DBG("chan %p len %zu", chan, len);
2345
2346 if (!conn)
2347 return ERR_PTR(-ENOTCONN);
2348
2349 hlen = L2CAP_HDR_SIZE;
2350
2351 if (sdulen)
2352 hlen += L2CAP_SDULEN_SIZE;
2353
2354 count = min_t(unsigned int, (conn->mtu - hlen), len);
2355
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002356 skb = chan->ops->alloc_skb(chan, hlen, count,
Johan Hedberg177f8f22013-05-31 17:54:51 +03002357 msg->msg_flags & MSG_DONTWAIT);
2358 if (IS_ERR(skb))
2359 return skb;
2360
2361 /* Create L2CAP header */
2362 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
2363 lh->cid = cpu_to_le16(chan->dcid);
2364 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
2365
2366 if (sdulen)
2367 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
2368
2369 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
2370 if (unlikely(err < 0)) {
2371 kfree_skb(skb);
2372 return ERR_PTR(err);
2373 }
2374
2375 return skb;
2376}
2377
2378static int l2cap_segment_le_sdu(struct l2cap_chan *chan,
2379 struct sk_buff_head *seg_queue,
2380 struct msghdr *msg, size_t len)
2381{
2382 struct sk_buff *skb;
2383 size_t pdu_len;
2384 u16 sdu_len;
2385
2386 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
2387
Johan Hedberg177f8f22013-05-31 17:54:51 +03002388 sdu_len = len;
Johan Hedberg72c6fb92014-08-15 21:06:51 +03002389 pdu_len = chan->remote_mps - L2CAP_SDULEN_SIZE;
Johan Hedberg177f8f22013-05-31 17:54:51 +03002390
2391 while (len > 0) {
2392 if (len <= pdu_len)
2393 pdu_len = len;
2394
2395 skb = l2cap_create_le_flowctl_pdu(chan, msg, pdu_len, sdu_len);
2396 if (IS_ERR(skb)) {
2397 __skb_queue_purge(seg_queue);
2398 return PTR_ERR(skb);
2399 }
2400
2401 __skb_queue_tail(seg_queue, skb);
2402
2403 len -= pdu_len;
2404
2405 if (sdu_len) {
2406 sdu_len = 0;
2407 pdu_len += L2CAP_SDULEN_SIZE;
2408 }
2409 }
2410
2411 return 0;
2412}
2413
Marcel Holtmann8d463212014-06-05 15:22:51 +02002414int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002415{
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002416 struct sk_buff *skb;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002417 int err;
Mat Martineau94122bb2012-05-02 09:42:02 -07002418 struct sk_buff_head seg_queue;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002419
Seung-Woo Kim31e8ce82013-11-05 18:46:33 +09002420 if (!chan->conn)
2421 return -ENOTCONN;
2422
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002423 /* Connectionless channel */
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03002424 if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
Marcel Holtmann8d463212014-06-05 15:22:51 +02002425 skb = l2cap_create_connless_pdu(chan, msg, len);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002426 if (IS_ERR(skb))
2427 return PTR_ERR(skb);
2428
Andrzej Kaczmarekede81a22014-02-25 17:16:22 +01002429 /* Channel lock is released before requesting new skb and then
2430 * reacquired thus we need to recheck channel state.
2431 */
2432 if (chan->state != BT_CONNECTED) {
2433 kfree_skb(skb);
2434 return -ENOTCONN;
2435 }
2436
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002437 l2cap_do_send(chan, skb);
2438 return len;
2439 }
2440
2441 switch (chan->mode) {
Johan Hedberg38319712013-05-17 12:49:23 +03002442 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedberg177f8f22013-05-31 17:54:51 +03002443 /* Check outgoing MTU */
2444 if (len > chan->omtu)
2445 return -EMSGSIZE;
2446
Johan Hedbergfad5fc892013-12-05 09:45:01 +02002447 if (!chan->tx_credits)
2448 return -EAGAIN;
2449
Johan Hedberg177f8f22013-05-31 17:54:51 +03002450 __skb_queue_head_init(&seg_queue);
2451
2452 err = l2cap_segment_le_sdu(chan, &seg_queue, msg, len);
2453
2454 if (chan->state != BT_CONNECTED) {
2455 __skb_queue_purge(&seg_queue);
2456 err = -ENOTCONN;
2457 }
2458
2459 if (err)
2460 return err;
2461
2462 skb_queue_splice_tail_init(&seg_queue, &chan->tx_q);
2463
2464 while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) {
2465 l2cap_do_send(chan, skb_dequeue(&chan->tx_q));
2466 chan->tx_credits--;
2467 }
2468
2469 if (!chan->tx_credits)
2470 chan->ops->suspend(chan);
2471
2472 err = len;
2473
2474 break;
2475
Johan Hedbergfad5fc892013-12-05 09:45:01 +02002476 case L2CAP_MODE_BASIC:
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002477 /* Check outgoing MTU */
2478 if (len > chan->omtu)
2479 return -EMSGSIZE;
2480
2481 /* Create a basic PDU */
Marcel Holtmann8d463212014-06-05 15:22:51 +02002482 skb = l2cap_create_basic_pdu(chan, msg, len);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002483 if (IS_ERR(skb))
2484 return PTR_ERR(skb);
2485
Andrzej Kaczmarekede81a22014-02-25 17:16:22 +01002486 /* Channel lock is released before requesting new skb and then
2487 * reacquired thus we need to recheck channel state.
2488 */
2489 if (chan->state != BT_CONNECTED) {
2490 kfree_skb(skb);
2491 return -ENOTCONN;
2492 }
2493
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002494 l2cap_do_send(chan, skb);
2495 err = len;
2496 break;
2497
2498 case L2CAP_MODE_ERTM:
2499 case L2CAP_MODE_STREAMING:
Mat Martineau94122bb2012-05-02 09:42:02 -07002500 /* Check outgoing MTU */
2501 if (len > chan->omtu) {
2502 err = -EMSGSIZE;
2503 break;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002504 }
2505
Mat Martineau94122bb2012-05-02 09:42:02 -07002506 __skb_queue_head_init(&seg_queue);
2507
2508 /* Do segmentation before calling in to the state machine,
2509 * since it's possible to block while waiting for memory
2510 * allocation.
2511 */
2512 err = l2cap_segment_sdu(chan, &seg_queue, msg, len);
2513
2514 /* The channel could have been closed while segmenting,
2515 * check that it is still connected.
2516 */
2517 if (chan->state != BT_CONNECTED) {
2518 __skb_queue_purge(&seg_queue);
2519 err = -ENOTCONN;
2520 }
2521
2522 if (err)
2523 break;
2524
Mat Martineau37339372012-05-17 20:53:33 -07002525 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo Padovand6603662012-05-21 13:58:22 -03002526 l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST);
Mat Martineau37339372012-05-17 20:53:33 -07002527 else
Gustavo Padovand6603662012-05-21 13:58:22 -03002528 l2cap_streaming_send(chan, &seg_queue);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002529
Gustavo Padovand6603662012-05-21 13:58:22 -03002530 err = len;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002531
Mat Martineau94122bb2012-05-02 09:42:02 -07002532 /* If the skbs were not queued for sending, they'll still be in
2533 * seg_queue and need to be purged.
2534 */
2535 __skb_queue_purge(&seg_queue);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002536 break;
2537
2538 default:
2539 BT_DBG("bad state %1.1x", chan->mode);
2540 err = -EBADFD;
2541 }
2542
2543 return err;
2544}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03002545EXPORT_SYMBOL_GPL(l2cap_chan_send);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002546
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002547static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq)
2548{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002549 struct l2cap_ctrl control;
2550 u16 seq;
2551
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002552 BT_DBG("chan %p, txseq %u", chan, txseq);
Mat Martineaubed68bd2012-05-17 20:53:44 -07002553
2554 memset(&control, 0, sizeof(control));
2555 control.sframe = 1;
2556 control.super = L2CAP_SUPER_SREJ;
2557
2558 for (seq = chan->expected_tx_seq; seq != txseq;
2559 seq = __next_seq(chan, seq)) {
2560 if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) {
2561 control.reqseq = seq;
2562 l2cap_send_sframe(chan, &control);
2563 l2cap_seq_list_append(&chan->srej_list, seq);
2564 }
2565 }
2566
2567 chan->expected_tx_seq = __next_seq(chan, txseq);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002568}
2569
2570static void l2cap_send_srej_tail(struct l2cap_chan *chan)
2571{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002572 struct l2cap_ctrl control;
2573
2574 BT_DBG("chan %p", chan);
2575
2576 if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR)
2577 return;
2578
2579 memset(&control, 0, sizeof(control));
2580 control.sframe = 1;
2581 control.super = L2CAP_SUPER_SREJ;
2582 control.reqseq = chan->srej_list.tail;
2583 l2cap_send_sframe(chan, &control);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002584}
2585
2586static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq)
2587{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002588 struct l2cap_ctrl control;
2589 u16 initial_head;
2590 u16 seq;
2591
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002592 BT_DBG("chan %p, txseq %u", chan, txseq);
Mat Martineaubed68bd2012-05-17 20:53:44 -07002593
2594 memset(&control, 0, sizeof(control));
2595 control.sframe = 1;
2596 control.super = L2CAP_SUPER_SREJ;
2597
2598 /* Capture initial list head to allow only one pass through the list. */
2599 initial_head = chan->srej_list.head;
2600
2601 do {
2602 seq = l2cap_seq_list_pop(&chan->srej_list);
2603 if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR)
2604 break;
2605
2606 control.reqseq = seq;
2607 l2cap_send_sframe(chan, &control);
2608 l2cap_seq_list_append(&chan->srej_list, seq);
2609 } while (chan->srej_list.head != initial_head);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002610}
2611
Mat Martineau608bcc62012-05-17 20:53:32 -07002612static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq)
2613{
2614 struct sk_buff *acked_skb;
2615 u16 ackseq;
2616
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002617 BT_DBG("chan %p, reqseq %u", chan, reqseq);
Mat Martineau608bcc62012-05-17 20:53:32 -07002618
2619 if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq)
2620 return;
2621
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002622 BT_DBG("expected_ack_seq %u, unacked_frames %u",
Mat Martineau608bcc62012-05-17 20:53:32 -07002623 chan->expected_ack_seq, chan->unacked_frames);
2624
2625 for (ackseq = chan->expected_ack_seq; ackseq != reqseq;
2626 ackseq = __next_seq(chan, ackseq)) {
2627
2628 acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq);
2629 if (acked_skb) {
2630 skb_unlink(acked_skb, &chan->tx_q);
2631 kfree_skb(acked_skb);
2632 chan->unacked_frames--;
2633 }
2634 }
2635
2636 chan->expected_ack_seq = reqseq;
2637
2638 if (chan->unacked_frames == 0)
2639 __clear_retrans_timer(chan);
2640
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002641 BT_DBG("unacked_frames %u", chan->unacked_frames);
Mat Martineau608bcc62012-05-17 20:53:32 -07002642}
2643
2644static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan)
2645{
2646 BT_DBG("chan %p", chan);
2647
2648 chan->expected_tx_seq = chan->buffer_seq;
2649 l2cap_seq_list_clear(&chan->srej_list);
2650 skb_queue_purge(&chan->srej_q);
2651 chan->rx_state = L2CAP_RX_STATE_RECV;
2652}
2653
Gustavo Padovand6603662012-05-21 13:58:22 -03002654static void l2cap_tx_state_xmit(struct l2cap_chan *chan,
2655 struct l2cap_ctrl *control,
2656 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002657{
Mat Martineau608bcc62012-05-17 20:53:32 -07002658 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2659 event);
2660
2661 switch (event) {
2662 case L2CAP_EV_DATA_REQUEST:
2663 if (chan->tx_send_head == NULL)
2664 chan->tx_send_head = skb_peek(skbs);
2665
2666 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2667 l2cap_ertm_send(chan);
2668 break;
2669 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2670 BT_DBG("Enter LOCAL_BUSY");
2671 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2672
2673 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2674 /* The SREJ_SENT state must be aborted if we are to
2675 * enter the LOCAL_BUSY state.
2676 */
2677 l2cap_abort_rx_srej_sent(chan);
2678 }
2679
2680 l2cap_send_ack(chan);
2681
2682 break;
2683 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2684 BT_DBG("Exit LOCAL_BUSY");
2685 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2686
2687 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2688 struct l2cap_ctrl local_control;
2689
2690 memset(&local_control, 0, sizeof(local_control));
2691 local_control.sframe = 1;
2692 local_control.super = L2CAP_SUPER_RR;
2693 local_control.poll = 1;
2694 local_control.reqseq = chan->buffer_seq;
Mat Martineaua67d7f62012-05-17 20:53:35 -07002695 l2cap_send_sframe(chan, &local_control);
Mat Martineau608bcc62012-05-17 20:53:32 -07002696
2697 chan->retry_count = 1;
2698 __set_monitor_timer(chan);
2699 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2700 }
2701 break;
2702 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2703 l2cap_process_reqseq(chan, control->reqseq);
2704 break;
2705 case L2CAP_EV_EXPLICIT_POLL:
2706 l2cap_send_rr_or_rnr(chan, 1);
2707 chan->retry_count = 1;
2708 __set_monitor_timer(chan);
2709 __clear_ack_timer(chan);
2710 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2711 break;
2712 case L2CAP_EV_RETRANS_TO:
2713 l2cap_send_rr_or_rnr(chan, 1);
2714 chan->retry_count = 1;
2715 __set_monitor_timer(chan);
2716 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2717 break;
2718 case L2CAP_EV_RECV_FBIT:
2719 /* Nothing to process */
2720 break;
2721 default:
2722 break;
2723 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002724}
2725
Gustavo Padovand6603662012-05-21 13:58:22 -03002726static void l2cap_tx_state_wait_f(struct l2cap_chan *chan,
2727 struct l2cap_ctrl *control,
2728 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002729{
Mat Martineau608bcc62012-05-17 20:53:32 -07002730 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2731 event);
2732
2733 switch (event) {
2734 case L2CAP_EV_DATA_REQUEST:
2735 if (chan->tx_send_head == NULL)
2736 chan->tx_send_head = skb_peek(skbs);
2737 /* Queue data, but don't send. */
2738 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2739 break;
2740 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2741 BT_DBG("Enter LOCAL_BUSY");
2742 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2743
2744 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2745 /* The SREJ_SENT state must be aborted if we are to
2746 * enter the LOCAL_BUSY state.
2747 */
2748 l2cap_abort_rx_srej_sent(chan);
2749 }
2750
2751 l2cap_send_ack(chan);
2752
2753 break;
2754 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2755 BT_DBG("Exit LOCAL_BUSY");
2756 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2757
2758 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2759 struct l2cap_ctrl local_control;
2760 memset(&local_control, 0, sizeof(local_control));
2761 local_control.sframe = 1;
2762 local_control.super = L2CAP_SUPER_RR;
2763 local_control.poll = 1;
2764 local_control.reqseq = chan->buffer_seq;
Mat Martineaua67d7f62012-05-17 20:53:35 -07002765 l2cap_send_sframe(chan, &local_control);
Mat Martineau608bcc62012-05-17 20:53:32 -07002766
2767 chan->retry_count = 1;
2768 __set_monitor_timer(chan);
2769 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2770 }
2771 break;
2772 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2773 l2cap_process_reqseq(chan, control->reqseq);
2774
2775 /* Fall through */
2776
2777 case L2CAP_EV_RECV_FBIT:
2778 if (control && control->final) {
2779 __clear_monitor_timer(chan);
2780 if (chan->unacked_frames > 0)
2781 __set_retrans_timer(chan);
2782 chan->retry_count = 0;
2783 chan->tx_state = L2CAP_TX_STATE_XMIT;
2784 BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state);
2785 }
2786 break;
2787 case L2CAP_EV_EXPLICIT_POLL:
2788 /* Ignore */
2789 break;
2790 case L2CAP_EV_MONITOR_TO:
2791 if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) {
2792 l2cap_send_rr_or_rnr(chan, 1);
2793 __set_monitor_timer(chan);
2794 chan->retry_count++;
2795 } else {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02002796 l2cap_send_disconn_req(chan, ECONNABORTED);
Mat Martineau608bcc62012-05-17 20:53:32 -07002797 }
2798 break;
2799 default:
2800 break;
2801 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002802}
2803
Gustavo Padovand6603662012-05-21 13:58:22 -03002804static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
2805 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002806{
Mat Martineau608bcc62012-05-17 20:53:32 -07002807 BT_DBG("chan %p, control %p, skbs %p, event %d, state %d",
2808 chan, control, skbs, event, chan->tx_state);
2809
2810 switch (chan->tx_state) {
2811 case L2CAP_TX_STATE_XMIT:
Gustavo Padovand6603662012-05-21 13:58:22 -03002812 l2cap_tx_state_xmit(chan, control, skbs, event);
Mat Martineau608bcc62012-05-17 20:53:32 -07002813 break;
2814 case L2CAP_TX_STATE_WAIT_F:
Gustavo Padovand6603662012-05-21 13:58:22 -03002815 l2cap_tx_state_wait_f(chan, control, skbs, event);
Mat Martineau608bcc62012-05-17 20:53:32 -07002816 break;
2817 default:
2818 /* Ignore event */
2819 break;
2820 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002821}
2822
Mat Martineau4b51dae92012-05-17 20:53:37 -07002823static void l2cap_pass_to_tx(struct l2cap_chan *chan,
2824 struct l2cap_ctrl *control)
2825{
2826 BT_DBG("chan %p, control %p", chan, control);
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03002827 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT);
Mat Martineau4b51dae92012-05-17 20:53:37 -07002828}
2829
Mat Martineauf80842a2012-05-17 20:53:46 -07002830static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan,
2831 struct l2cap_ctrl *control)
2832{
2833 BT_DBG("chan %p, control %p", chan, control);
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03002834 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT);
Mat Martineauf80842a2012-05-17 20:53:46 -07002835}
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837/* Copy frame to all raw sockets on that connection */
2838static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
2839{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 struct sk_buff *nskb;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03002841 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 BT_DBG("conn %p", conn);
2844
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002845 mutex_lock(&conn->chan_lock);
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -02002846
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002847 list_for_each_entry(chan, &conn->chan_l, list) {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03002848 if (chan->chan_type != L2CAP_CHAN_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 continue;
2850
Gustavo Padovan7f5396a2013-10-21 18:22:25 -02002851 /* Don't send frame to the channel it came from */
2852 if (bt_cb(skb)->chan == chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 continue;
Gustavo Padovan7f5396a2013-10-21 18:22:25 -02002854
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002855 nskb = skb_clone(skb, GFP_KERNEL);
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03002856 if (!nskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 continue;
Gustavo Padovan80b98022012-05-27 22:27:51 -03002858 if (chan->ops->recv(chan, nskb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 kfree_skb(nskb);
2860 }
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -02002861
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002862 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863}
2864
2865/* ---- L2CAP signalling commands ---- */
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002866static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code,
2867 u8 ident, u16 dlen, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
2869 struct sk_buff *skb, **frag;
2870 struct l2cap_cmd_hdr *cmd;
2871 struct l2cap_hdr *lh;
2872 int len, count;
2873
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002874 BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u",
2875 conn, code, ident, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Anderson Lizardo300b9622013-06-02 16:30:40 -04002877 if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE)
2878 return NULL;
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
2881 count = min_t(unsigned int, conn->mtu, len);
2882
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002883 skb = bt_skb_alloc(count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (!skb)
2885 return NULL;
2886
2887 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002888 lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02002889
2890 if (conn->hcon->type == LE_LINK)
Joe Perchesdcf4adb2014-03-12 10:52:35 -07002891 lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02002892 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -07002893 lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
2895 cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
2896 cmd->code = code;
2897 cmd->ident = ident;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002898 cmd->len = cpu_to_le16(dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
2900 if (dlen) {
2901 count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
2902 memcpy(skb_put(skb, count), data, count);
2903 data += count;
2904 }
2905
2906 len -= skb->len;
2907
2908 /* Continuation fragments (no L2CAP header) */
2909 frag = &skb_shinfo(skb)->frag_list;
2910 while (len) {
2911 count = min_t(unsigned int, conn->mtu, len);
2912
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002913 *frag = bt_skb_alloc(count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 if (!*frag)
2915 goto fail;
2916
2917 memcpy(skb_put(*frag, count), data, count);
2918
2919 len -= count;
2920 data += count;
2921
2922 frag = &(*frag)->next;
2923 }
2924
2925 return skb;
2926
2927fail:
2928 kfree_skb(skb);
2929 return NULL;
2930}
2931
Gustavo Padovan2d792812012-10-06 10:07:01 +01002932static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen,
2933 unsigned long *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
2935 struct l2cap_conf_opt *opt = *ptr;
2936 int len;
2937
2938 len = L2CAP_CONF_OPT_SIZE + opt->len;
2939 *ptr += len;
2940
2941 *type = opt->type;
2942 *olen = opt->len;
2943
2944 switch (opt->len) {
2945 case 1:
2946 *val = *((u8 *) opt->val);
2947 break;
2948
2949 case 2:
steven miaobfaaeb32010-10-16 18:29:47 -04002950 *val = get_unaligned_le16(opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 break;
2952
2953 case 4:
steven miaobfaaeb32010-10-16 18:29:47 -04002954 *val = get_unaligned_le32(opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 break;
2956
2957 default:
2958 *val = (unsigned long) opt->val;
2959 break;
2960 }
2961
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002962 BT_DBG("type 0x%2.2x len %u val 0x%lx", *type, opt->len, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return len;
2964}
2965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
2967{
2968 struct l2cap_conf_opt *opt = *ptr;
2969
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002970 BT_DBG("type 0x%2.2x len %u val 0x%lx", type, len, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 opt->type = type;
2973 opt->len = len;
2974
2975 switch (len) {
2976 case 1:
2977 *((u8 *) opt->val) = val;
2978 break;
2979
2980 case 2:
Gustavo F. Padovan4f8b6912010-10-18 14:25:53 -02002981 put_unaligned_le16(val, opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 break;
2983
2984 case 4:
Gustavo F. Padovan4f8b6912010-10-18 14:25:53 -02002985 put_unaligned_le32(val, opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 break;
2987
2988 default:
2989 memcpy(opt->val, (void *) val, len);
2990 break;
2991 }
2992
2993 *ptr += L2CAP_CONF_OPT_SIZE + len;
2994}
2995
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03002996static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan)
2997{
2998 struct l2cap_conf_efs efs;
2999
Szymon Janc1ec918c2011-11-16 09:32:21 +01003000 switch (chan->mode) {
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003001 case L2CAP_MODE_ERTM:
3002 efs.id = chan->local_id;
3003 efs.stype = chan->local_stype;
3004 efs.msdu = cpu_to_le16(chan->local_msdu);
3005 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003006 efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT);
3007 efs.flush_to = cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003008 break;
3009
3010 case L2CAP_MODE_STREAMING:
3011 efs.id = 1;
3012 efs.stype = L2CAP_SERV_BESTEFFORT;
3013 efs.msdu = cpu_to_le16(chan->local_msdu);
3014 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
3015 efs.acc_lat = 0;
3016 efs.flush_to = 0;
3017 break;
3018
3019 default:
3020 return;
3021 }
3022
3023 l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs),
Andrei Emeltchenko8936fa62012-10-08 11:14:41 +03003024 (unsigned long) &efs);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003025}
3026
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003027static void l2cap_ack_timeout(struct work_struct *work)
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003028{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003029 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau03625202012-05-17 20:53:51 -07003030 ack_timer.work);
3031 u16 frames_to_ack;
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003032
Gustavo F. Padovan2fb9b3d2011-12-22 16:56:05 -02003033 BT_DBG("chan %p", chan);
3034
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003035 l2cap_chan_lock(chan);
3036
Mat Martineau03625202012-05-17 20:53:51 -07003037 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
3038 chan->last_acked_seq);
3039
3040 if (frames_to_ack)
3041 l2cap_send_rr_or_rnr(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003042
3043 l2cap_chan_unlock(chan);
Szymon Janc09bfb2e2012-01-11 10:59:49 +01003044 l2cap_chan_put(chan);
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003045}
3046
Andrei Emeltchenko466f8002012-05-29 13:59:01 +03003047int l2cap_ertm_init(struct l2cap_chan *chan)
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003048{
Mat Martineau3c588192012-04-11 10:48:42 -07003049 int err;
3050
Mat Martineau105bdf92012-04-27 16:50:48 -07003051 chan->next_tx_seq = 0;
3052 chan->expected_tx_seq = 0;
Gustavo F. Padovan42e5c802011-03-25 19:58:34 -03003053 chan->expected_ack_seq = 0;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03003054 chan->unacked_frames = 0;
Gustavo F. Padovan42e5c802011-03-25 19:58:34 -03003055 chan->buffer_seq = 0;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03003056 chan->frames_sent = 0;
Mat Martineau105bdf92012-04-27 16:50:48 -07003057 chan->last_acked_seq = 0;
3058 chan->sdu = NULL;
3059 chan->sdu_last_frag = NULL;
3060 chan->sdu_len = 0;
3061
Mat Martineaud34c34f2012-05-14 14:49:27 -07003062 skb_queue_head_init(&chan->tx_q);
3063
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003064 chan->local_amp_id = AMP_ID_BREDR;
3065 chan->move_id = AMP_ID_BREDR;
Mat Martineau08333282012-10-23 15:24:06 -07003066 chan->move_state = L2CAP_MOVE_STABLE;
3067 chan->move_role = L2CAP_MOVE_ROLE_NONE;
3068
Mat Martineau105bdf92012-04-27 16:50:48 -07003069 if (chan->mode != L2CAP_MODE_ERTM)
3070 return 0;
3071
3072 chan->rx_state = L2CAP_RX_STATE_RECV;
3073 chan->tx_state = L2CAP_TX_STATE_XMIT;
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003074
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003075 INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout);
3076 INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout);
3077 INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003078
Gustavo F. Padovanf1c67752011-03-25 20:36:10 -03003079 skb_queue_head_init(&chan->srej_q);
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03003080
Mat Martineau3c588192012-04-11 10:48:42 -07003081 err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win);
3082 if (err < 0)
3083 return err;
3084
Mat Martineau9dc9aff2012-05-17 16:20:14 -07003085 err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win);
3086 if (err < 0)
3087 l2cap_seq_list_free(&chan->srej_list);
3088
3089 return err;
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003090}
3091
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003092static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
3093{
3094 switch (mode) {
3095 case L2CAP_MODE_STREAMING:
3096 case L2CAP_MODE_ERTM:
3097 if (l2cap_mode_supported(mode, remote_feat_mask))
3098 return mode;
3099 /* fall through */
3100 default:
3101 return L2CAP_MODE_BASIC;
3102 }
3103}
3104
Marcel Holtmann848566b2013-10-01 22:59:22 -07003105static inline bool __l2cap_ews_supported(struct l2cap_conn *conn)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003106{
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02003107 return ((conn->local_fixed_chan & L2CAP_FC_A2MP) &&
3108 (conn->feat_mask & L2CAP_FEAT_EXT_WINDOW));
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003109}
3110
Marcel Holtmann848566b2013-10-01 22:59:22 -07003111static inline bool __l2cap_efs_supported(struct l2cap_conn *conn)
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003112{
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02003113 return ((conn->local_fixed_chan & L2CAP_FC_A2MP) &&
3114 (conn->feat_mask & L2CAP_FEAT_EXT_FLOW));
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003115}
3116
Mat Martineau36c86c82012-10-23 15:24:20 -07003117static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan,
3118 struct l2cap_conf_rfc *rfc)
3119{
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003120 if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) {
Mat Martineau36c86c82012-10-23 15:24:20 -07003121 u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to;
3122
3123 /* Class 1 devices have must have ERTM timeouts
3124 * exceeding the Link Supervision Timeout. The
3125 * default Link Supervision Timeout for AMP
3126 * controllers is 10 seconds.
3127 *
3128 * Class 1 devices use 0xffffffff for their
3129 * best-effort flush timeout, so the clamping logic
3130 * will result in a timeout that meets the above
3131 * requirement. ERTM timeouts are 16-bit values, so
3132 * the maximum timeout is 65.535 seconds.
3133 */
3134
3135 /* Convert timeout to milliseconds and round */
3136 ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000);
3137
3138 /* This is the recommended formula for class 2 devices
3139 * that start ERTM timers when packets are sent to the
3140 * controller.
3141 */
3142 ertm_to = 3 * ertm_to + 500;
3143
3144 if (ertm_to > 0xffff)
3145 ertm_to = 0xffff;
3146
3147 rfc->retrans_timeout = cpu_to_le16((u16) ertm_to);
3148 rfc->monitor_timeout = rfc->retrans_timeout;
3149 } else {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003150 rfc->retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
3151 rfc->monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
Mat Martineau36c86c82012-10-23 15:24:20 -07003152 }
3153}
3154
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003155static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
3156{
3157 if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW &&
Marcel Holtmann848566b2013-10-01 22:59:22 -07003158 __l2cap_ews_supported(chan->conn)) {
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003159 /* use extended control field */
3160 set_bit(FLAG_EXT_CTRL, &chan->flags);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003161 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
3162 } else {
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003163 chan->tx_win = min_t(u16, chan->tx_win,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003164 L2CAP_DEFAULT_TX_WINDOW);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003165 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
3166 }
Mat Martineauc20f8e32012-07-10 05:47:07 -07003167 chan->ack_win = chan->tx_win;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003168}
3169
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003170static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 struct l2cap_conf_req *req = data;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003173 struct l2cap_conf_rfc rfc = { .mode = chan->mode };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 void *ptr = req->data;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003175 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03003177 BT_DBG("chan %p", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003179 if (chan->num_conf_req || chan->num_conf_rsp)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003180 goto done;
3181
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003182 switch (chan->mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003183 case L2CAP_MODE_STREAMING:
3184 case L2CAP_MODE_ERTM:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003185 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003186 break;
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003187
Marcel Holtmann848566b2013-10-01 22:59:22 -07003188 if (__l2cap_efs_supported(chan->conn))
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003189 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3190
Gustavo F. Padovan2ba13ed2010-06-09 16:39:05 -03003191 /* fall through */
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003192 default:
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003193 chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003194 break;
3195 }
3196
3197done:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003198 if (chan->imtu != L2CAP_DEFAULT_MTU)
3199 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
Gustavo F. Padovan7990681c2011-01-24 16:01:43 -02003200
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003201 switch (chan->mode) {
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003202 case L2CAP_MODE_BASIC:
Marcel Holtmann6fea7ad2014-07-09 11:53:35 +02003203 if (disable_ertm)
3204 break;
3205
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003206 if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003207 !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003208 break;
3209
Gustavo F. Padovan62547752010-06-08 20:05:31 -03003210 rfc.mode = L2CAP_MODE_BASIC;
3211 rfc.txwin_size = 0;
3212 rfc.max_transmit = 0;
3213 rfc.retrans_timeout = 0;
3214 rfc.monitor_timeout = 0;
3215 rfc.max_pdu_size = 0;
3216
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003217 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003218 (unsigned long) &rfc);
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003219 break;
3220
3221 case L2CAP_MODE_ERTM:
3222 rfc.mode = L2CAP_MODE_ERTM;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003223 rfc.max_transmit = chan->max_tx;
Mat Martineau36c86c82012-10-23 15:24:20 -07003224
3225 __l2cap_set_ertm_timeouts(chan, &rfc);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003226
3227 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
Gustavo Padovan2d792812012-10-06 10:07:01 +01003228 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3229 L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003230 rfc.max_pdu_size = cpu_to_le16(size);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003231
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003232 l2cap_txwin_setup(chan);
3233
3234 rfc.txwin_size = min_t(u16, chan->tx_win,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003235 L2CAP_DEFAULT_TX_WINDOW);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003236
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003237 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003238 (unsigned long) &rfc);
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003239
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003240 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
3241 l2cap_add_opt_efs(&ptr, chan);
3242
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003243 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3244 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003245 chan->tx_win);
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003246
3247 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3248 if (chan->fcs == L2CAP_FCS_NONE ||
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003249 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003250 chan->fcs = L2CAP_FCS_NONE;
3251 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
3252 chan->fcs);
3253 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003254 break;
3255
3256 case L2CAP_MODE_STREAMING:
Mat Martineau273759e2012-05-17 20:53:53 -07003257 l2cap_txwin_setup(chan);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003258 rfc.mode = L2CAP_MODE_STREAMING;
3259 rfc.txwin_size = 0;
3260 rfc.max_transmit = 0;
3261 rfc.retrans_timeout = 0;
3262 rfc.monitor_timeout = 0;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003263
3264 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
Gustavo Padovan2d792812012-10-06 10:07:01 +01003265 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3266 L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003267 rfc.max_pdu_size = cpu_to_le16(size);
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003268
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003269 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003270 (unsigned long) &rfc);
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003271
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003272 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
3273 l2cap_add_opt_efs(&ptr, chan);
3274
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003275 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3276 if (chan->fcs == L2CAP_FCS_NONE ||
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003277 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003278 chan->fcs = L2CAP_FCS_NONE;
3279 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
3280 chan->fcs);
3281 }
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003282 break;
3283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003285 req->dcid = cpu_to_le16(chan->dcid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003286 req->flags = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 return ptr - data;
3289}
3290
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003291static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003293 struct l2cap_conf_rsp *rsp = data;
3294 void *ptr = rsp->data;
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003295 void *req = chan->conf_req;
3296 int len = chan->conf_len;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003297 int type, hint, olen;
3298 unsigned long val;
Marcel Holtmann6464f352007-10-20 13:39:51 +02003299 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003300 struct l2cap_conf_efs efs;
3301 u8 remote_efs = 0;
Marcel Holtmann861d6882007-10-20 13:37:06 +02003302 u16 mtu = L2CAP_DEFAULT_MTU;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003303 u16 result = L2CAP_CONF_SUCCESS;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003304 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003306 BT_DBG("chan %p", chan);
Marcel Holtmann820ae1b2006-11-18 22:15:00 +01003307
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003308 while (len >= L2CAP_CONF_OPT_SIZE) {
3309 len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Gustavo F. Padovan589d2742009-04-20 01:31:07 -03003311 hint = type & L2CAP_CONF_HINT;
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -07003312 type &= L2CAP_CONF_MASK;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003313
3314 switch (type) {
3315 case L2CAP_CONF_MTU:
Marcel Holtmann861d6882007-10-20 13:37:06 +02003316 mtu = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003317 break;
3318
3319 case L2CAP_CONF_FLUSH_TO:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003320 chan->flush_to = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003321 break;
3322
3323 case L2CAP_CONF_QOS:
3324 break;
3325
Marcel Holtmann6464f352007-10-20 13:39:51 +02003326 case L2CAP_CONF_RFC:
3327 if (olen == sizeof(rfc))
3328 memcpy(&rfc, (void *) val, olen);
3329 break;
3330
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003331 case L2CAP_CONF_FCS:
3332 if (val == L2CAP_FCS_NONE)
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003333 set_bit(CONF_RECV_NO_FCS, &chan->conf_state);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003334 break;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003335
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003336 case L2CAP_CONF_EFS:
3337 remote_efs = 1;
3338 if (olen == sizeof(efs))
3339 memcpy(&efs, (void *) val, olen);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003340 break;
3341
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003342 case L2CAP_CONF_EWS:
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02003343 if (!(chan->conn->local_fixed_chan & L2CAP_FC_A2MP))
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003344 return -ECONNREFUSED;
3345
3346 set_bit(FLAG_EXT_CTRL, &chan->flags);
3347 set_bit(CONF_EWS_RECV, &chan->conf_state);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003348 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003349 chan->remote_tx_win = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003350 break;
3351
3352 default:
3353 if (hint)
3354 break;
3355
3356 result = L2CAP_CONF_UNKNOWN;
3357 *((u8 *) ptr++) = type;
3358 break;
3359 }
3360 }
3361
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003362 if (chan->num_conf_rsp || chan->num_conf_req > 1)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003363 goto done;
3364
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003365 switch (chan->mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003366 case L2CAP_MODE_STREAMING:
3367 case L2CAP_MODE_ERTM:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003368 if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003369 chan->mode = l2cap_select_mode(rfc.mode,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003370 chan->conn->feat_mask);
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003371 break;
3372 }
3373
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003374 if (remote_efs) {
Marcel Holtmann848566b2013-10-01 22:59:22 -07003375 if (__l2cap_efs_supported(chan->conn))
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003376 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3377 else
3378 return -ECONNREFUSED;
3379 }
3380
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003381 if (chan->mode != rfc.mode)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003382 return -ECONNREFUSED;
Gustavo F. Padovan742e5192010-06-08 19:09:48 -03003383
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003384 break;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003385 }
3386
3387done:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003388 if (chan->mode != rfc.mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003389 result = L2CAP_CONF_UNACCEPT;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003390 rfc.mode = chan->mode;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003391
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003392 if (chan->num_conf_rsp == 1)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003393 return -ECONNREFUSED;
3394
Gustavo Padovan2d792812012-10-06 10:07:01 +01003395 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3396 (unsigned long) &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003397 }
3398
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003399 if (result == L2CAP_CONF_SUCCESS) {
3400 /* Configure output options and let the other side know
3401 * which ones we don't like. */
3402
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003403 if (mtu < L2CAP_DEFAULT_MIN_MTU)
3404 result = L2CAP_CONF_UNACCEPT;
3405 else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003406 chan->omtu = mtu;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003407 set_bit(CONF_MTU_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003408 }
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003409 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003410
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003411 if (remote_efs) {
3412 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003413 efs.stype != L2CAP_SERV_NOTRAFIC &&
3414 efs.stype != chan->local_stype) {
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003415
3416 result = L2CAP_CONF_UNACCEPT;
3417
3418 if (chan->num_conf_req >= 1)
3419 return -ECONNREFUSED;
3420
3421 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003422 sizeof(efs),
3423 (unsigned long) &efs);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003424 } else {
Gustavo F. Padovan3e6b3b92011-11-01 14:06:23 -02003425 /* Send PENDING Conf Rsp */
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003426 result = L2CAP_CONF_PENDING;
3427 set_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003428 }
3429 }
3430
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003431 switch (rfc.mode) {
3432 case L2CAP_MODE_BASIC:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003433 chan->fcs = L2CAP_FCS_NONE;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003434 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003435 break;
3436
3437 case L2CAP_MODE_ERTM:
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003438 if (!test_bit(CONF_EWS_RECV, &chan->conf_state))
3439 chan->remote_tx_win = rfc.txwin_size;
3440 else
3441 rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW;
3442
Gustavo F. Padovan2c03a7a2011-03-25 20:15:28 -03003443 chan->remote_max_tx = rfc.max_transmit;
Mat Martineau86b1b262010-08-05 15:54:22 -07003444
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003445 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003446 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3447 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003448 rfc.max_pdu_size = cpu_to_le16(size);
3449 chan->remote_mps = size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003450
Mat Martineau36c86c82012-10-23 15:24:20 -07003451 __l2cap_set_ertm_timeouts(chan, &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003452
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003453 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003454
3455 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003456 sizeof(rfc), (unsigned long) &rfc);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003457
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003458 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3459 chan->remote_id = efs.id;
3460 chan->remote_stype = efs.stype;
3461 chan->remote_msdu = le16_to_cpu(efs.msdu);
3462 chan->remote_flush_to =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003463 le32_to_cpu(efs.flush_to);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003464 chan->remote_acc_lat =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003465 le32_to_cpu(efs.acc_lat);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003466 chan->remote_sdu_itime =
3467 le32_to_cpu(efs.sdu_itime);
3468 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003469 sizeof(efs),
3470 (unsigned long) &efs);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003471 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003472 break;
3473
3474 case L2CAP_MODE_STREAMING:
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003475 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003476 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3477 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003478 rfc.max_pdu_size = cpu_to_le16(size);
3479 chan->remote_mps = size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003480
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003481 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003482
Gustavo Padovan2d792812012-10-06 10:07:01 +01003483 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3484 (unsigned long) &rfc);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003485
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003486 break;
3487
3488 default:
Marcel Holtmann6464f352007-10-20 13:39:51 +02003489 result = L2CAP_CONF_UNACCEPT;
3490
3491 memset(&rfc, 0, sizeof(rfc));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003492 rfc.mode = chan->mode;
Marcel Holtmann6464f352007-10-20 13:39:51 +02003493 }
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003494
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003495 if (result == L2CAP_CONF_SUCCESS)
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003496 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003497 }
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003498 rsp->scid = cpu_to_le16(chan->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003499 rsp->result = cpu_to_le16(result);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003500 rsp->flags = cpu_to_le16(0);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003501
3502 return ptr - data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503}
3504
Gustavo Padovan2d792812012-10-06 10:07:01 +01003505static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
3506 void *data, u16 *result)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003507{
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003508 struct l2cap_conf_req *req = data;
3509 void *ptr = req->data;
3510 int type, olen;
3511 unsigned long val;
Mat Martineau36e999a2011-12-08 17:23:21 -08003512 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003513 struct l2cap_conf_efs efs;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003514
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003515 BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003516
3517 while (len >= L2CAP_CONF_OPT_SIZE) {
3518 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3519
3520 switch (type) {
3521 case L2CAP_CONF_MTU:
3522 if (val < L2CAP_DEFAULT_MIN_MTU) {
3523 *result = L2CAP_CONF_UNACCEPT;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003524 chan->imtu = L2CAP_DEFAULT_MIN_MTU;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003525 } else
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003526 chan->imtu = val;
3527 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003528 break;
3529
3530 case L2CAP_CONF_FLUSH_TO:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003531 chan->flush_to = val;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003532 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003533 2, chan->flush_to);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003534 break;
3535
3536 case L2CAP_CONF_RFC:
3537 if (olen == sizeof(rfc))
3538 memcpy(&rfc, (void *)val, olen);
3539
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003540 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003541 rfc.mode != chan->mode)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003542 return -ECONNREFUSED;
3543
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003544 chan->fcs = 0;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003545
3546 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003547 sizeof(rfc), (unsigned long) &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003548 break;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003549
3550 case L2CAP_CONF_EWS:
Mat Martineauc20f8e32012-07-10 05:47:07 -07003551 chan->ack_win = min_t(u16, val, chan->ack_win);
Gustavo F. Padovan3e6b3b92011-11-01 14:06:23 -02003552 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
Mat Martineauc20f8e32012-07-10 05:47:07 -07003553 chan->tx_win);
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003554 break;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003555
3556 case L2CAP_CONF_EFS:
3557 if (olen == sizeof(efs))
3558 memcpy(&efs, (void *)val, olen);
3559
3560 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003561 efs.stype != L2CAP_SERV_NOTRAFIC &&
3562 efs.stype != chan->local_stype)
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003563 return -ECONNREFUSED;
3564
Gustavo Padovan2d792812012-10-06 10:07:01 +01003565 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs),
3566 (unsigned long) &efs);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003567 break;
Andrei Emeltchenkocbabee72012-11-29 17:46:07 +02003568
3569 case L2CAP_CONF_FCS:
3570 if (*result == L2CAP_CONF_PENDING)
3571 if (val == L2CAP_FCS_NONE)
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003572 set_bit(CONF_RECV_NO_FCS,
Andrei Emeltchenkocbabee72012-11-29 17:46:07 +02003573 &chan->conf_state);
3574 break;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003575 }
3576 }
3577
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003578 if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
Gustavo F. Padovan6c2ea7a2010-06-08 20:08:49 -03003579 return -ECONNREFUSED;
3580
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003581 chan->mode = rfc.mode;
Gustavo F. Padovan6c2ea7a2010-06-08 20:08:49 -03003582
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003583 if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003584 switch (rfc.mode) {
3585 case L2CAP_MODE_ERTM:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003586 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3587 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
3588 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Mat Martineauc20f8e32012-07-10 05:47:07 -07003589 if (!test_bit(FLAG_EXT_CTRL, &chan->flags))
3590 chan->ack_win = min_t(u16, chan->ack_win,
3591 rfc.txwin_size);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003592
3593 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3594 chan->local_msdu = le16_to_cpu(efs.msdu);
3595 chan->local_sdu_itime =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003596 le32_to_cpu(efs.sdu_itime);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003597 chan->local_acc_lat = le32_to_cpu(efs.acc_lat);
3598 chan->local_flush_to =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003599 le32_to_cpu(efs.flush_to);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003600 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003601 break;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003602
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003603 case L2CAP_MODE_STREAMING:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003604 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003605 }
3606 }
3607
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003608 req->dcid = cpu_to_le16(chan->dcid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003609 req->flags = cpu_to_le16(0);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003610
3611 return ptr - data;
3612}
3613
Gustavo Padovan2d792812012-10-06 10:07:01 +01003614static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data,
3615 u16 result, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616{
3617 struct l2cap_conf_rsp *rsp = data;
3618 void *ptr = rsp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003620 BT_DBG("chan %p", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003622 rsp->scid = cpu_to_le16(chan->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003623 rsp->result = cpu_to_le16(result);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07003624 rsp->flags = cpu_to_le16(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
3626 return ptr - data;
3627}
3628
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03003629void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan)
3630{
3631 struct l2cap_le_conn_rsp rsp;
3632 struct l2cap_conn *conn = chan->conn;
3633
3634 BT_DBG("chan %p", chan);
3635
3636 rsp.dcid = cpu_to_le16(chan->scid);
3637 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02003638 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03003639 rsp.credits = cpu_to_le16(chan->rx_credits);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003640 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03003641
3642 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
3643 &rsp);
3644}
3645
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003646void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003647{
3648 struct l2cap_conn_rsp rsp;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003649 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003650 u8 buf[128];
Andrei Emeltchenko439f34a2012-10-31 15:46:28 +02003651 u8 rsp_code;
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003652
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003653 rsp.scid = cpu_to_le16(chan->dcid);
3654 rsp.dcid = cpu_to_le16(chan->scid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003655 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
3656 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Andrei Emeltchenko439f34a2012-10-31 15:46:28 +02003657
3658 if (chan->hs_hcon)
3659 rsp_code = L2CAP_CREATE_CHAN_RSP;
3660 else
3661 rsp_code = L2CAP_CONN_RSP;
3662
3663 BT_DBG("chan %p rsp_code %u", chan, rsp_code);
3664
3665 l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp);
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003666
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003667 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003668 return;
3669
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003670 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003671 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003672 chan->num_conf_req++;
3673}
3674
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003675static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003676{
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003677 int type, olen;
3678 unsigned long val;
Mat Martineauc20f8e32012-07-10 05:47:07 -07003679 /* Use sane default values in case a misbehaving remote device
3680 * did not send an RFC or extended window size option.
3681 */
3682 u16 txwin_ext = chan->ack_win;
3683 struct l2cap_conf_rfc rfc = {
3684 .mode = chan->mode,
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003685 .retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO),
3686 .monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO),
Mat Martineauc20f8e32012-07-10 05:47:07 -07003687 .max_pdu_size = cpu_to_le16(chan->imtu),
3688 .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW),
3689 };
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003690
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003691 BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003692
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003693 if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003694 return;
3695
3696 while (len >= L2CAP_CONF_OPT_SIZE) {
3697 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3698
Mat Martineauc20f8e32012-07-10 05:47:07 -07003699 switch (type) {
3700 case L2CAP_CONF_RFC:
3701 if (olen == sizeof(rfc))
3702 memcpy(&rfc, (void *)val, olen);
Szymon Janc8f321f82012-06-08 11:33:33 +02003703 break;
Mat Martineauc20f8e32012-07-10 05:47:07 -07003704 case L2CAP_CONF_EWS:
3705 txwin_ext = val;
3706 break;
3707 }
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003708 }
3709
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003710 switch (rfc.mode) {
3711 case L2CAP_MODE_ERTM:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003712 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3713 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
Mat Martineauc20f8e32012-07-10 05:47:07 -07003714 chan->mps = le16_to_cpu(rfc.max_pdu_size);
3715 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3716 chan->ack_win = min_t(u16, chan->ack_win, txwin_ext);
3717 else
3718 chan->ack_win = min_t(u16, chan->ack_win,
3719 rfc.txwin_size);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003720 break;
3721 case L2CAP_MODE_STREAMING:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003722 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003723 }
3724}
3725
Gustavo Padovan2d792812012-10-06 10:07:01 +01003726static inline int l2cap_command_rej(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003727 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3728 u8 *data)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003729{
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003730 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003731
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003732 if (cmd_len < sizeof(*rej))
3733 return -EPROTO;
3734
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003735 if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003736 return 0;
3737
3738 if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003739 cmd->ident == conn->info_ident) {
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02003740 cancel_delayed_work(&conn->info_timer);
Marcel Holtmann984947d2009-02-06 23:35:19 +01003741
3742 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01003743 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01003744
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003745 l2cap_conn_start(conn);
3746 }
3747
3748 return 0;
3749}
3750
Mat Martineau17009152012-10-23 15:24:07 -07003751static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3752 struct l2cap_cmd_hdr *cmd,
3753 u8 *data, u8 rsp_code, u8 amp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
3756 struct l2cap_conn_rsp rsp;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003757 struct l2cap_chan *chan = NULL, *pchan;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003758 int result, status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
3760 u16 dcid = 0, scid = __le16_to_cpu(req->scid);
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003761 __le16 psm = req->psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Andrei Emeltchenko097db762012-03-09 14:16:17 +02003763 BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
3765 /* Check if we have socket listening on psm */
Marcel Holtmann6f59b902013-10-13 05:24:01 -07003766 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
Johan Hedbergbf20fd42013-05-14 13:23:13 +03003767 &conn->hcon->dst, ACL_LINK);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003768 if (!pchan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 result = L2CAP_CR_BAD_PSM;
3770 goto sendresp;
3771 }
3772
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003773 mutex_lock(&conn->chan_lock);
Gustavo Padovan8ffb9292013-10-21 14:21:41 -02003774 l2cap_chan_lock(pchan);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00003775
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003776 /* Check if the ACL is secure enough (if not SDP) */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003777 if (psm != cpu_to_le16(L2CAP_PSM_SDP) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003778 !hci_conn_check_link_mode(conn->hcon)) {
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02003779 conn->disc_reason = HCI_ERROR_AUTH_FAILURE;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003780 result = L2CAP_CR_SEC_BLOCK;
3781 goto response;
3782 }
3783
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 result = L2CAP_CR_NO_MEM;
3785
Gustavo Padovan2dfa1002012-05-27 22:27:58 -03003786 /* Check if we already have channel with that dcid */
3787 if (__l2cap_get_chan_by_dcid(conn, scid))
3788 goto response;
3789
Gustavo Padovan80b98022012-05-27 22:27:51 -03003790 chan = pchan->ops->new_connection(pchan);
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03003791 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 goto response;
3793
Syam Sidhardhan330b6c12013-08-06 01:59:12 +09003794 /* For certain devices (ex: HID mouse), support for authentication,
3795 * pairing and bonding is optional. For such devices, inorder to avoid
3796 * the ACL alive for too long after L2CAP disconnection, reset the ACL
3797 * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect.
3798 */
3799 conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
3800
Marcel Holtmann7eafc592013-10-13 08:12:47 -07003801 bacpy(&chan->src, &conn->hcon->src);
3802 bacpy(&chan->dst, &conn->hcon->dst);
Johan Hedberga250e042015-01-15 13:06:44 +02003803 chan->src_type = bdaddr_src_type(conn->hcon);
3804 chan->dst_type = bdaddr_dst_type(conn->hcon);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003805 chan->psm = psm;
3806 chan->dcid = scid;
Mat Martineau17009152012-10-23 15:24:07 -07003807 chan->local_amp_id = amp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003809 __l2cap_chan_add(conn, chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003810
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003811 dcid = chan->scid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
Gustavo Padovan8d836d72013-10-15 19:24:47 -03003813 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03003815 chan->ident = cmd->ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
Marcel Holtmann984947d2009-02-06 23:35:19 +01003817 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
Johan Hedberge7cafc42014-07-17 15:35:38 +03003818 if (l2cap_chan_check_security(chan, false)) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07003819 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003820 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003821 result = L2CAP_CR_PEND;
3822 status = L2CAP_CS_AUTHOR_PEND;
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08003823 chan->ops->defer(chan);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003824 } else {
Mat Martineau17009152012-10-23 15:24:07 -07003825 /* Force pending result for AMP controllers.
3826 * The connection will succeed after the
3827 * physical link is up.
3828 */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003829 if (amp_id == AMP_ID_BREDR) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003830 l2cap_state_change(chan, BT_CONFIG);
Mat Martineau17009152012-10-23 15:24:07 -07003831 result = L2CAP_CR_SUCCESS;
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003832 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003833 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003834 result = L2CAP_CR_PEND;
Mat Martineau17009152012-10-23 15:24:07 -07003835 }
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003836 status = L2CAP_CS_NO_INFO;
3837 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003838 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003839 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003840 result = L2CAP_CR_PEND;
3841 status = L2CAP_CS_AUTHEN_PEND;
3842 }
3843 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003844 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003845 result = L2CAP_CR_PEND;
3846 status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 }
3848
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849response:
Gustavo Padovan8ffb9292013-10-21 14:21:41 -02003850 l2cap_chan_unlock(pchan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003851 mutex_unlock(&conn->chan_lock);
Johan Hedberga24cce12014-08-07 22:56:42 +03003852 l2cap_chan_put(pchan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
3854sendresp:
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07003855 rsp.scid = cpu_to_le16(scid);
3856 rsp.dcid = cpu_to_le16(dcid);
3857 rsp.result = cpu_to_le16(result);
3858 rsp.status = cpu_to_le16(status);
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003859 l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003860
3861 if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
3862 struct l2cap_info_req info;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003863 info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003864
3865 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
3866 conn->info_ident = l2cap_get_ident(conn);
3867
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08003868 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003869
Gustavo Padovan2d792812012-10-06 10:07:01 +01003870 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
3871 sizeof(info), &info);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003872 }
3873
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003874 if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003875 result == L2CAP_CR_SUCCESS) {
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003876 u8 buf[128];
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003877 set_bit(CONF_REQ_SENT, &chan->conf_state);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003878 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003879 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003880 chan->num_conf_req++;
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003881 }
Mat Martineau17009152012-10-23 15:24:07 -07003882
3883 return chan;
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003884}
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003885
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003886static int l2cap_connect_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003887 struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003888{
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303889 struct hci_dev *hdev = conn->hcon->hdev;
3890 struct hci_conn *hcon = conn->hcon;
3891
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003892 if (cmd_len < sizeof(struct l2cap_conn_req))
3893 return -EPROTO;
3894
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303895 hci_dev_lock(hdev);
3896 if (test_bit(HCI_MGMT, &hdev->dev_flags) &&
3897 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00003898 mgmt_device_connected(hdev, hcon, 0, NULL, 0);
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303899 hci_dev_unlock(hdev);
3900
Gustavo Padovan300229f2012-10-12 19:40:40 +08003901 l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 return 0;
3903}
3904
Mat Martineau5909cf32012-10-23 15:24:08 -07003905static int l2cap_connect_create_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003906 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3907 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908{
3909 struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
3910 u16 scid, dcid, result, status;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003911 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 u8 req[128];
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003913 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003915 if (cmd_len < sizeof(*rsp))
3916 return -EPROTO;
3917
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 scid = __le16_to_cpu(rsp->scid);
3919 dcid = __le16_to_cpu(rsp->dcid);
3920 result = __le16_to_cpu(rsp->result);
3921 status = __le16_to_cpu(rsp->status);
3922
Andrei Emeltchenko1b009c92012-02-21 12:54:54 +02003923 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 +01003924 dcid, scid, result, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003926 mutex_lock(&conn->chan_lock);
3927
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 if (scid) {
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003929 chan = __l2cap_get_chan_by_scid(conn, scid);
3930 if (!chan) {
Johan Hedberg21870b52013-09-16 13:05:14 +03003931 err = -EBADSLT;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003932 goto unlock;
3933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 } else {
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003935 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
3936 if (!chan) {
Johan Hedberg21870b52013-09-16 13:05:14 +03003937 err = -EBADSLT;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003938 goto unlock;
3939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 }
3941
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003942 err = 0;
3943
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003944 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003945
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 switch (result) {
3947 case L2CAP_CR_SUCCESS:
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03003948 l2cap_state_change(chan, BT_CONFIG);
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03003949 chan->ident = 0;
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003950 chan->dcid = dcid;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003951 clear_bit(CONF_CONNECT_PEND, &chan->conf_state);
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01003952
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003953 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003954 break;
3955
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003957 l2cap_build_conf_req(chan, req), req);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003958 chan->num_conf_req++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 break;
3960
3961 case L2CAP_CR_PEND:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003962 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 break;
3964
3965 default:
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003966 l2cap_chan_del(chan, ECONNREFUSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 break;
3968 }
3969
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003970 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003971
3972unlock:
3973 mutex_unlock(&conn->chan_lock);
3974
3975 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976}
3977
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003978static inline void set_default_fcs(struct l2cap_chan *chan)
Mat Martineau8c462b62010-08-24 15:35:42 -07003979{
3980 /* FCS is enabled only in ERTM or streaming mode, if one or both
3981 * sides request it.
3982 */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003983 if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003984 chan->fcs = L2CAP_FCS_NONE;
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003985 else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state))
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003986 chan->fcs = L2CAP_FCS_CRC16;
Mat Martineau8c462b62010-08-24 15:35:42 -07003987}
3988
Andrei Emeltchenko29d8a592012-09-21 12:30:05 +03003989static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data,
3990 u8 ident, u16 flags)
3991{
3992 struct l2cap_conn *conn = chan->conn;
3993
3994 BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident,
3995 flags);
3996
3997 clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
3998 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
3999
4000 l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP,
4001 l2cap_build_conf_rsp(chan, data,
4002 L2CAP_CONF_SUCCESS, flags), data);
4003}
4004
Johan Hedberg662d6522013-10-16 11:20:47 +03004005static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident,
4006 u16 scid, u16 dcid)
4007{
4008 struct l2cap_cmd_rej_cid rej;
4009
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004010 rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID);
Johan Hedberg662d6522013-10-16 11:20:47 +03004011 rej.scid = __cpu_to_le16(scid);
4012 rej.dcid = __cpu_to_le16(dcid);
4013
4014 l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
4015}
4016
Gustavo Padovan2d792812012-10-06 10:07:01 +01004017static inline int l2cap_config_req(struct l2cap_conn *conn,
4018 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4019 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020{
4021 struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
4022 u16 dcid, flags;
4023 u8 rsp[64];
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004024 struct l2cap_chan *chan;
Mat Martineau3c588192012-04-11 10:48:42 -07004025 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004027 if (cmd_len < sizeof(*req))
4028 return -EPROTO;
4029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 dcid = __le16_to_cpu(req->dcid);
4031 flags = __le16_to_cpu(req->flags);
4032
4033 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
4034
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03004035 chan = l2cap_get_chan_by_scid(conn, dcid);
Johan Hedberg662d6522013-10-16 11:20:47 +03004036 if (!chan) {
4037 cmd_reject_invalid_cid(conn, cmd->ident, dcid, 0);
4038 return 0;
4039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
David S. Miller033b1142011-07-21 13:38:42 -07004041 if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) {
Johan Hedberg662d6522013-10-16 11:20:47 +03004042 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4043 chan->dcid);
Marcel Holtmann354f60a2006-11-18 22:15:20 +01004044 goto unlock;
Gustavo F. Padovandf6bd742010-06-14 02:26:15 -03004045 }
Marcel Holtmann354f60a2006-11-18 22:15:20 +01004046
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004047 /* Reject if config buffer is too small. */
Al Viro88219a02007-07-29 00:17:25 -07004048 len = cmd_len - sizeof(*req);
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004049 if (chan->conf_len + len > sizeof(chan->conf_req)) {
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004050 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004051 l2cap_build_conf_rsp(chan, rsp,
4052 L2CAP_CONF_REJECT, flags), rsp);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004053 goto unlock;
4054 }
4055
4056 /* Store config. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004057 memcpy(chan->conf_req + chan->conf_len, req->data, len);
4058 chan->conf_len += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03004060 if (flags & L2CAP_CONF_FLAG_CONTINUATION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 /* Incomplete config. Send empty response. */
4062 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004063 l2cap_build_conf_rsp(chan, rsp,
4064 L2CAP_CONF_SUCCESS, flags), rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 goto unlock;
4066 }
4067
4068 /* Complete config. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004069 len = l2cap_parse_conf_req(chan, rsp);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004070 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004071 l2cap_send_disconn_req(chan, ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 goto unlock;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Mat Martineau1500109b2012-10-23 15:24:15 -07004075 chan->ident = cmd->ident;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004076 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004077 chan->num_conf_rsp++;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004078
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004079 /* Reset config buffer. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004080 chan->conf_len = 0;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004081
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004082 if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state))
Marcel Holtmann876d9482007-10-20 13:35:42 +02004083 goto unlock;
4084
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004085 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004086 set_default_fcs(chan);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004087
Mat Martineau105bdf92012-04-27 16:50:48 -07004088 if (chan->mode == L2CAP_MODE_ERTM ||
4089 chan->mode == L2CAP_MODE_STREAMING)
Mat Martineau3c588192012-04-11 10:48:42 -07004090 err = l2cap_ertm_init(chan);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03004091
Mat Martineau3c588192012-04-11 10:48:42 -07004092 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004093 l2cap_send_disconn_req(chan, -err);
Mat Martineau3c588192012-04-11 10:48:42 -07004094 else
4095 l2cap_chan_ready(chan);
4096
Marcel Holtmann876d9482007-10-20 13:35:42 +02004097 goto unlock;
4098 }
4099
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004100 if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +02004101 u8 buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004103 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004104 chan->num_conf_req++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 }
4106
Stephen Hemminger49c922b2014-10-27 21:12:20 -07004107 /* Got Conf Rsp PENDING from remote side and assume we sent
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004108 Conf Rsp PENDING in the code above */
4109 if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) &&
Andrei Emeltchenko29d8a592012-09-21 12:30:05 +03004110 test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004111
4112 /* check compatibility */
4113
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004114 /* Send rsp for BR/EDR channel */
Andrei Emeltchenkof351bc72012-10-31 15:46:35 +02004115 if (!chan->hs_hcon)
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004116 l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags);
4117 else
4118 chan->ident = cmd->ident;
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004119 }
4120
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121unlock:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004122 l2cap_chan_unlock(chan);
Mat Martineau3c588192012-04-11 10:48:42 -07004123 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124}
4125
Gustavo Padovan2d792812012-10-06 10:07:01 +01004126static inline int l2cap_config_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004127 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4128 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129{
4130 struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
4131 u16 scid, flags, result;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004132 struct l2cap_chan *chan;
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004133 int len = cmd_len - sizeof(*rsp);
Mat Martineau3c588192012-04-11 10:48:42 -07004134 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004136 if (cmd_len < sizeof(*rsp))
4137 return -EPROTO;
4138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 scid = __le16_to_cpu(rsp->scid);
4140 flags = __le16_to_cpu(rsp->flags);
4141 result = __le16_to_cpu(rsp->result);
4142
Andrei Emeltchenko61386cb2012-03-12 12:13:07 +02004143 BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags,
4144 result, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03004146 chan = l2cap_get_chan_by_scid(conn, scid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004147 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 return 0;
4149
4150 switch (result) {
4151 case L2CAP_CONF_SUCCESS:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004152 l2cap_conf_rfc_get(chan, rsp->data, len);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004153 clear_bit(CONF_REM_CONF_PEND, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 break;
4155
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004156 case L2CAP_CONF_PENDING:
4157 set_bit(CONF_REM_CONF_PEND, &chan->conf_state);
4158
4159 if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
4160 char buf[64];
4161
4162 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004163 buf, &result);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004164 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004165 l2cap_send_disconn_req(chan, ECONNRESET);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004166 goto done;
4167 }
4168
Andrei Emeltchenkof351bc72012-10-31 15:46:35 +02004169 if (!chan->hs_hcon) {
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004170 l2cap_send_efs_conf_rsp(chan, buf, cmd->ident,
4171 0);
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +02004172 } else {
4173 if (l2cap_check_efs(chan)) {
4174 amp_create_logical_link(chan);
4175 chan->ident = cmd->ident;
4176 }
4177 }
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004178 }
4179 goto done;
4180
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 case L2CAP_CONF_UNACCEPT:
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004182 if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004183 char req[64];
4184
Andrei Emeltchenkoc2c77ec2010-03-19 10:26:28 +02004185 if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004186 l2cap_send_disconn_req(chan, ECONNRESET);
Andrei Emeltchenkoc2c77ec2010-03-19 10:26:28 +02004187 goto done;
4188 }
4189
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004190 /* throw out any old stored conf requests */
4191 result = L2CAP_CONF_SUCCESS;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03004192 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004193 req, &result);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004194 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004195 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004196 goto done;
4197 }
4198
4199 l2cap_send_cmd(conn, l2cap_get_ident(conn),
Gustavo Padovan2d792812012-10-06 10:07:01 +01004200 L2CAP_CONF_REQ, len, req);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004201 chan->num_conf_req++;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004202 if (result != L2CAP_CONF_SUCCESS)
4203 goto done;
4204 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 }
4206
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09004207 default:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004208 l2cap_chan_set_err(chan, ECONNRESET);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +02004209
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08004210 __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004211 l2cap_send_disconn_req(chan, ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 goto done;
4213 }
4214
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03004215 if (flags & L2CAP_CONF_FLAG_CONTINUATION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 goto done;
4217
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004218 set_bit(CONF_INPUT_DONE, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004220 if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) {
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004221 set_default_fcs(chan);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004222
Mat Martineau105bdf92012-04-27 16:50:48 -07004223 if (chan->mode == L2CAP_MODE_ERTM ||
4224 chan->mode == L2CAP_MODE_STREAMING)
Mat Martineau3c588192012-04-11 10:48:42 -07004225 err = l2cap_ertm_init(chan);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03004226
Mat Martineau3c588192012-04-11 10:48:42 -07004227 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004228 l2cap_send_disconn_req(chan, -err);
Mat Martineau3c588192012-04-11 10:48:42 -07004229 else
4230 l2cap_chan_ready(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 }
4232
4233done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004234 l2cap_chan_unlock(chan);
Mat Martineau3c588192012-04-11 10:48:42 -07004235 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236}
4237
Gustavo Padovan2d792812012-10-06 10:07:01 +01004238static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004239 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4240 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241{
4242 struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
4243 struct l2cap_disconn_rsp rsp;
4244 u16 dcid, scid;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004245 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004247 if (cmd_len != sizeof(*req))
4248 return -EPROTO;
4249
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 scid = __le16_to_cpu(req->scid);
4251 dcid = __le16_to_cpu(req->dcid);
4252
4253 BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
4254
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004255 mutex_lock(&conn->chan_lock);
4256
4257 chan = __l2cap_get_chan_by_scid(conn, dcid);
4258 if (!chan) {
4259 mutex_unlock(&conn->chan_lock);
Johan Hedberg662d6522013-10-16 11:20:47 +03004260 cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid);
4261 return 0;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004264 l2cap_chan_lock(chan);
4265
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03004266 rsp.dcid = cpu_to_le16(chan->scid);
4267 rsp.scid = cpu_to_le16(chan->dcid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
4269
Gustavo Padovan5ec1bbe2013-10-15 19:24:48 -03004270 chan->ops->set_shutdown(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Mat Martineau61d6ef32012-04-27 16:50:50 -07004272 l2cap_chan_hold(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004273 l2cap_chan_del(chan, ECONNRESET);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004274
4275 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
Gustavo Padovan80b98022012-05-27 22:27:51 -03004277 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07004278 l2cap_chan_put(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004279
4280 mutex_unlock(&conn->chan_lock);
4281
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 return 0;
4283}
4284
Gustavo Padovan2d792812012-10-06 10:07:01 +01004285static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004286 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4287 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
4289 struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
4290 u16 dcid, scid;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004291 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004293 if (cmd_len != sizeof(*rsp))
4294 return -EPROTO;
4295
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 scid = __le16_to_cpu(rsp->scid);
4297 dcid = __le16_to_cpu(rsp->dcid);
4298
4299 BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
4300
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004301 mutex_lock(&conn->chan_lock);
4302
4303 chan = __l2cap_get_chan_by_scid(conn, scid);
4304 if (!chan) {
4305 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 return 0;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004309 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004310
Mat Martineau61d6ef32012-04-27 16:50:50 -07004311 l2cap_chan_hold(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004312 l2cap_chan_del(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004313
4314 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Gustavo Padovan80b98022012-05-27 22:27:51 -03004316 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07004317 l2cap_chan_put(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004318
4319 mutex_unlock(&conn->chan_lock);
4320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return 0;
4322}
4323
Gustavo Padovan2d792812012-10-06 10:07:01 +01004324static inline int l2cap_information_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004325 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4326 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327{
4328 struct l2cap_info_req *req = (struct l2cap_info_req *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 u16 type;
4330
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004331 if (cmd_len != sizeof(*req))
4332 return -EPROTO;
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 type = __le16_to_cpu(req->type);
4335
4336 BT_DBG("type 0x%4.4x", type);
4337
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004338 if (type == L2CAP_IT_FEAT_MASK) {
4339 u8 buf[8];
Marcel Holtmann44dd46d2009-05-02 19:09:01 -07004340 u32 feat_mask = l2cap_feat_mask;
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004341 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004342 rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
4343 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03004344 if (!disable_ertm)
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004345 feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
Gustavo Padovan2d792812012-10-06 10:07:01 +01004346 | L2CAP_FEAT_FCS;
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004347 if (conn->local_fixed_chan & L2CAP_FC_A2MP)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03004348 feat_mask |= L2CAP_FEAT_EXT_FLOW
Gustavo Padovan2d792812012-10-06 10:07:01 +01004349 | L2CAP_FEAT_EXT_WINDOW;
Andrei Emeltchenkoa5fd6f32011-09-16 16:26:32 +03004350
Gustavo F. Padovan1b7bf4e2009-08-24 00:45:20 -03004351 put_unaligned_le32(feat_mask, rsp->data);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004352 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4353 buf);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004354 } else if (type == L2CAP_IT_FIXED_CHAN) {
4355 u8 buf[12];
4356 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
Mat Martineau50a147c2011-11-02 16:18:34 -07004357
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004358 rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
4359 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004360 rsp->data[0] = conn->local_fixed_chan;
4361 memset(rsp->data + 1, 0, 7);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004362 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4363 buf);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004364 } else {
4365 struct l2cap_info_rsp rsp;
4366 rsp.type = cpu_to_le16(type);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004367 rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004368 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp),
4369 &rsp);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
4372 return 0;
4373}
4374
Gustavo Padovan2d792812012-10-06 10:07:01 +01004375static inline int l2cap_information_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004376 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4377 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378{
4379 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
4380 u16 type, result;
4381
Jaganath Kanakkassery3f6fa3d2013-06-21 19:55:11 +05304382 if (cmd_len < sizeof(*rsp))
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004383 return -EPROTO;
4384
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 type = __le16_to_cpu(rsp->type);
4386 result = __le16_to_cpu(rsp->result);
4387
4388 BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
4389
Andrei Emeltchenkoe90165b2011-03-25 11:31:41 +02004390 /* L2CAP Info req/rsp are unbound to channels, add extra checks */
4391 if (cmd->ident != conn->info_ident ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01004392 conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
Andrei Emeltchenkoe90165b2011-03-25 11:31:41 +02004393 return 0;
4394
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02004395 cancel_delayed_work(&conn->info_timer);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004396
Ville Tervoadb08ed2010-08-04 09:43:33 +03004397 if (result != L2CAP_IR_SUCCESS) {
4398 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4399 conn->info_ident = 0;
4400
4401 l2cap_conn_start(conn);
4402
4403 return 0;
4404 }
4405
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004406 switch (type) {
4407 case L2CAP_IT_FEAT_MASK:
Harvey Harrison83985312008-05-02 16:25:46 -07004408 conn->feat_mask = get_unaligned_le32(rsp->data);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004409
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -07004410 if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004411 struct l2cap_info_req req;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004412 req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004413
4414 conn->info_ident = l2cap_get_ident(conn);
4415
4416 l2cap_send_cmd(conn, conn->info_ident,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004417 L2CAP_INFO_REQ, sizeof(req), &req);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004418 } else {
4419 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4420 conn->info_ident = 0;
4421
4422 l2cap_conn_start(conn);
4423 }
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004424 break;
4425
4426 case L2CAP_IT_FIXED_CHAN:
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004427 conn->remote_fixed_chan = rsp->data[0];
Marcel Holtmann984947d2009-02-06 23:35:19 +01004428 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004429 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01004430
4431 l2cap_conn_start(conn);
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004432 break;
Marcel Holtmann984947d2009-02-06 23:35:19 +01004433 }
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 return 0;
4436}
4437
Mat Martineau17009152012-10-23 15:24:07 -07004438static int l2cap_create_channel_req(struct l2cap_conn *conn,
4439 struct l2cap_cmd_hdr *cmd,
4440 u16 cmd_len, void *data)
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004441{
4442 struct l2cap_create_chan_req *req = data;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004443 struct l2cap_create_chan_rsp rsp;
Mat Martineau17009152012-10-23 15:24:07 -07004444 struct l2cap_chan *chan;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004445 struct hci_dev *hdev;
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004446 u16 psm, scid;
4447
4448 if (cmd_len != sizeof(*req))
4449 return -EPROTO;
4450
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004451 if (!(conn->local_fixed_chan & L2CAP_FC_A2MP))
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004452 return -EINVAL;
4453
4454 psm = le16_to_cpu(req->psm);
4455 scid = le16_to_cpu(req->scid);
4456
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004457 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 -07004458
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004459 /* For controller id 0 make BR/EDR connection */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004460 if (req->amp_id == AMP_ID_BREDR) {
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004461 l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4462 req->amp_id);
4463 return 0;
4464 }
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004465
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004466 /* Validate AMP controller id */
4467 hdev = hci_dev_get(req->amp_id);
4468 if (!hdev)
4469 goto error;
Mat Martineau17009152012-10-23 15:24:07 -07004470
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004471 if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) {
Mat Martineau17009152012-10-23 15:24:07 -07004472 hci_dev_put(hdev);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004473 goto error;
Mat Martineau17009152012-10-23 15:24:07 -07004474 }
4475
4476 chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4477 req->amp_id);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004478 if (chan) {
4479 struct amp_mgr *mgr = conn->hcon->amp_mgr;
4480 struct hci_conn *hs_hcon;
4481
Marcel Holtmann98e0f7e2013-10-13 02:23:40 -07004482 hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
4483 &conn->hcon->dst);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004484 if (!hs_hcon) {
4485 hci_dev_put(hdev);
Johan Hedberg662d6522013-10-16 11:20:47 +03004486 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4487 chan->dcid);
4488 return 0;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004489 }
4490
4491 BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon);
4492
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004493 mgr->bredr_chan = chan;
4494 chan->hs_hcon = hs_hcon;
Andrei Emeltchenkofd45bf42012-11-20 17:16:22 +02004495 chan->fcs = L2CAP_FCS_NONE;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004496 conn->mtu = hdev->block_mtu;
4497 }
4498
4499 hci_dev_put(hdev);
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004500
4501 return 0;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004502
4503error:
4504 rsp.dcid = 0;
4505 rsp.scid = cpu_to_le16(scid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004506 rsp.result = cpu_to_le16(L2CAP_CR_BAD_AMP);
4507 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004508
4509 l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
4510 sizeof(rsp), &rsp);
4511
Johan Hedbergdc280802013-09-16 13:05:13 +03004512 return 0;
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004513}
4514
Mat Martineau8eb200b2012-10-23 15:24:17 -07004515static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id)
4516{
4517 struct l2cap_move_chan_req req;
4518 u8 ident;
4519
4520 BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id);
4521
4522 ident = l2cap_get_ident(chan->conn);
4523 chan->ident = ident;
4524
4525 req.icid = cpu_to_le16(chan->scid);
4526 req.dest_amp_id = dest_amp_id;
4527
4528 l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req),
4529 &req);
4530
4531 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4532}
4533
Mat Martineau1500109b2012-10-23 15:24:15 -07004534static void l2cap_send_move_chan_rsp(struct l2cap_chan *chan, u16 result)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004535{
4536 struct l2cap_move_chan_rsp rsp;
4537
Mat Martineau1500109b2012-10-23 15:24:15 -07004538 BT_DBG("chan %p, result 0x%4.4x", chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004539
Mat Martineau1500109b2012-10-23 15:24:15 -07004540 rsp.icid = cpu_to_le16(chan->dcid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004541 rsp.result = cpu_to_le16(result);
4542
Mat Martineau1500109b2012-10-23 15:24:15 -07004543 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP,
4544 sizeof(rsp), &rsp);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004545}
4546
Mat Martineau5b155ef2012-10-23 15:24:14 -07004547static void l2cap_send_move_chan_cfm(struct l2cap_chan *chan, u16 result)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004548{
4549 struct l2cap_move_chan_cfm cfm;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004550
Mat Martineau5b155ef2012-10-23 15:24:14 -07004551 BT_DBG("chan %p, result 0x%4.4x", chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004552
Mat Martineau5b155ef2012-10-23 15:24:14 -07004553 chan->ident = l2cap_get_ident(chan->conn);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004554
Mat Martineau5b155ef2012-10-23 15:24:14 -07004555 cfm.icid = cpu_to_le16(chan->scid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004556 cfm.result = cpu_to_le16(result);
4557
Mat Martineau5b155ef2012-10-23 15:24:14 -07004558 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM,
4559 sizeof(cfm), &cfm);
4560
4561 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4562}
4563
4564static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid)
4565{
4566 struct l2cap_move_chan_cfm cfm;
4567
4568 BT_DBG("conn %p, icid 0x%4.4x", conn, icid);
4569
4570 cfm.icid = cpu_to_le16(icid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004571 cfm.result = cpu_to_le16(L2CAP_MC_UNCONFIRMED);
Mat Martineau5b155ef2012-10-23 15:24:14 -07004572
4573 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM,
4574 sizeof(cfm), &cfm);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004575}
4576
4577static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004578 u16 icid)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004579{
4580 struct l2cap_move_chan_cfm_rsp rsp;
4581
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004582 BT_DBG("icid 0x%4.4x", icid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004583
4584 rsp.icid = cpu_to_le16(icid);
4585 l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp);
4586}
4587
Mat Martineau5f3847a2012-10-23 15:24:12 -07004588static void __release_logical_link(struct l2cap_chan *chan)
4589{
4590 chan->hs_hchan = NULL;
4591 chan->hs_hcon = NULL;
4592
4593 /* Placeholder - release the logical link */
4594}
4595
Mat Martineau1500109b2012-10-23 15:24:15 -07004596static void l2cap_logical_fail(struct l2cap_chan *chan)
4597{
4598 /* Logical link setup failed */
4599 if (chan->state != BT_CONNECTED) {
4600 /* Create channel failure, disconnect */
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004601 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineau1500109b2012-10-23 15:24:15 -07004602 return;
4603 }
4604
4605 switch (chan->move_role) {
4606 case L2CAP_MOVE_ROLE_RESPONDER:
4607 l2cap_move_done(chan);
4608 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP);
4609 break;
4610 case L2CAP_MOVE_ROLE_INITIATOR:
4611 if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP ||
4612 chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) {
4613 /* Remote has only sent pending or
4614 * success responses, clean up
4615 */
4616 l2cap_move_done(chan);
4617 }
4618
4619 /* Other amp move states imply that the move
4620 * has already aborted
4621 */
4622 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
4623 break;
4624 }
4625}
4626
4627static void l2cap_logical_finish_create(struct l2cap_chan *chan,
4628 struct hci_chan *hchan)
4629{
4630 struct l2cap_conf_rsp rsp;
Mat Martineau1500109b2012-10-23 15:24:15 -07004631
Andrei Emeltchenko336178a2012-10-31 15:46:27 +02004632 chan->hs_hchan = hchan;
Mat Martineau1500109b2012-10-23 15:24:15 -07004633 chan->hs_hcon->l2cap_data = chan->conn;
4634
Andrei Emeltchenko35ba9562012-10-25 15:20:43 +03004635 l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0);
Mat Martineau1500109b2012-10-23 15:24:15 -07004636
4637 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
Andrei Emeltchenkofe79c6f2012-10-25 15:20:42 +03004638 int err;
Mat Martineau1500109b2012-10-23 15:24:15 -07004639
4640 set_default_fcs(chan);
4641
4642 err = l2cap_ertm_init(chan);
4643 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004644 l2cap_send_disconn_req(chan, -err);
Mat Martineau1500109b2012-10-23 15:24:15 -07004645 else
4646 l2cap_chan_ready(chan);
4647 }
4648}
4649
4650static void l2cap_logical_finish_move(struct l2cap_chan *chan,
4651 struct hci_chan *hchan)
4652{
4653 chan->hs_hcon = hchan->conn;
4654 chan->hs_hcon->l2cap_data = chan->conn;
4655
4656 BT_DBG("move_state %d", chan->move_state);
4657
4658 switch (chan->move_state) {
4659 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
4660 /* Move confirm will be sent after a success
4661 * response is received
4662 */
4663 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4664 break;
4665 case L2CAP_MOVE_WAIT_LOGICAL_CFM:
4666 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4667 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4668 } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
4669 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
4670 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
4671 } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
4672 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4673 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
4674 }
4675 break;
4676 default:
4677 /* Move was not in expected state, free the channel */
4678 __release_logical_link(chan);
4679
4680 chan->move_state = L2CAP_MOVE_STABLE;
4681 }
4682}
4683
4684/* Call with chan locked */
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03004685void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan,
4686 u8 status)
Mat Martineau5b155ef2012-10-23 15:24:14 -07004687{
Mat Martineau1500109b2012-10-23 15:24:15 -07004688 BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status);
4689
4690 if (status) {
4691 l2cap_logical_fail(chan);
4692 __release_logical_link(chan);
4693 return;
4694 }
4695
4696 if (chan->state != BT_CONNECTED) {
4697 /* Ignore logical link if channel is on BR/EDR */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004698 if (chan->local_amp_id != AMP_ID_BREDR)
Mat Martineau1500109b2012-10-23 15:24:15 -07004699 l2cap_logical_finish_create(chan, hchan);
4700 } else {
4701 l2cap_logical_finish_move(chan, hchan);
4702 }
Mat Martineau5b155ef2012-10-23 15:24:14 -07004703}
4704
Mat Martineau3f7a56c2012-10-23 15:24:23 -07004705void l2cap_move_start(struct l2cap_chan *chan)
4706{
4707 BT_DBG("chan %p", chan);
4708
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004709 if (chan->local_amp_id == AMP_ID_BREDR) {
Mat Martineau3f7a56c2012-10-23 15:24:23 -07004710 if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED)
4711 return;
4712 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
4713 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
4714 /* Placeholder - start physical link setup */
4715 } else {
4716 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
4717 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4718 chan->move_id = 0;
4719 l2cap_move_setup(chan);
4720 l2cap_send_move_chan_req(chan, 0);
4721 }
4722}
4723
Mat Martineau8eb200b2012-10-23 15:24:17 -07004724static void l2cap_do_create(struct l2cap_chan *chan, int result,
4725 u8 local_amp_id, u8 remote_amp_id)
4726{
Andrei Emeltchenko62748ca2012-11-20 17:16:19 +02004727 BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state),
4728 local_amp_id, remote_amp_id);
4729
Andrei Emeltchenko12d6cc62012-11-14 17:39:31 +02004730 chan->fcs = L2CAP_FCS_NONE;
4731
Andrei Emeltchenko62748ca2012-11-20 17:16:19 +02004732 /* Outgoing channel on AMP */
4733 if (chan->state == BT_CONNECT) {
4734 if (result == L2CAP_CR_SUCCESS) {
4735 chan->local_amp_id = local_amp_id;
4736 l2cap_send_create_chan_req(chan, remote_amp_id);
4737 } else {
4738 /* Revert to BR/EDR connect */
4739 l2cap_send_conn_req(chan);
4740 }
4741
4742 return;
4743 }
4744
4745 /* Incoming channel on AMP */
4746 if (__l2cap_no_conn_pending(chan)) {
Mat Martineau8eb200b2012-10-23 15:24:17 -07004747 struct l2cap_conn_rsp rsp;
4748 char buf[128];
4749 rsp.scid = cpu_to_le16(chan->dcid);
4750 rsp.dcid = cpu_to_le16(chan->scid);
4751
Mat Martineau8eb200b2012-10-23 15:24:17 -07004752 if (result == L2CAP_CR_SUCCESS) {
4753 /* Send successful response */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004754 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
4755 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004756 } else {
4757 /* Send negative response */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004758 rsp.result = cpu_to_le16(L2CAP_CR_NO_MEM);
4759 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004760 }
4761
4762 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP,
4763 sizeof(rsp), &rsp);
4764
4765 if (result == L2CAP_CR_SUCCESS) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02004766 l2cap_state_change(chan, BT_CONFIG);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004767 set_bit(CONF_REQ_SENT, &chan->conf_state);
4768 l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn),
4769 L2CAP_CONF_REQ,
4770 l2cap_build_conf_req(chan, buf), buf);
4771 chan->num_conf_req++;
4772 }
Mat Martineau8eb200b2012-10-23 15:24:17 -07004773 }
4774}
4775
4776static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id,
4777 u8 remote_amp_id)
4778{
4779 l2cap_move_setup(chan);
4780 chan->move_id = local_amp_id;
4781 chan->move_state = L2CAP_MOVE_WAIT_RSP;
4782
4783 l2cap_send_move_chan_req(chan, remote_amp_id);
4784}
4785
4786static void l2cap_do_move_respond(struct l2cap_chan *chan, int result)
4787{
4788 struct hci_chan *hchan = NULL;
4789
4790 /* Placeholder - get hci_chan for logical link */
4791
4792 if (hchan) {
4793 if (hchan->state == BT_CONNECTED) {
4794 /* Logical link is ready to go */
4795 chan->hs_hcon = hchan->conn;
4796 chan->hs_hcon->l2cap_data = chan->conn;
4797 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4798 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
4799
4800 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
4801 } else {
4802 /* Wait for logical link to be ready */
4803 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
4804 }
4805 } else {
4806 /* Logical link not available */
4807 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED);
4808 }
4809}
4810
4811static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result)
4812{
4813 if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
4814 u8 rsp_result;
4815 if (result == -EINVAL)
4816 rsp_result = L2CAP_MR_BAD_ID;
4817 else
4818 rsp_result = L2CAP_MR_NOT_ALLOWED;
4819
4820 l2cap_send_move_chan_rsp(chan, rsp_result);
4821 }
4822
4823 chan->move_role = L2CAP_MOVE_ROLE_NONE;
4824 chan->move_state = L2CAP_MOVE_STABLE;
4825
4826 /* Restart data transmission */
4827 l2cap_ertm_send(chan);
4828}
4829
Andrei Emeltchenkoa514b172012-11-14 17:39:30 +02004830/* Invoke with locked chan */
4831void __l2cap_physical_cfm(struct l2cap_chan *chan, int result)
Mat Martineau8eb200b2012-10-23 15:24:17 -07004832{
Andrei Emeltchenko770bfef2012-10-31 15:46:29 +02004833 u8 local_amp_id = chan->local_amp_id;
Andrei Emeltchenkofffadc02012-11-01 15:37:03 +02004834 u8 remote_amp_id = chan->remote_amp_id;
Andrei Emeltchenko770bfef2012-10-31 15:46:29 +02004835
Mat Martineau8eb200b2012-10-23 15:24:17 -07004836 BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d",
4837 chan, result, local_amp_id, remote_amp_id);
4838
Mat Martineau8eb200b2012-10-23 15:24:17 -07004839 if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) {
4840 l2cap_chan_unlock(chan);
4841 return;
4842 }
4843
4844 if (chan->state != BT_CONNECTED) {
4845 l2cap_do_create(chan, result, local_amp_id, remote_amp_id);
4846 } else if (result != L2CAP_MR_SUCCESS) {
4847 l2cap_do_move_cancel(chan, result);
4848 } else {
4849 switch (chan->move_role) {
4850 case L2CAP_MOVE_ROLE_INITIATOR:
4851 l2cap_do_move_initiate(chan, local_amp_id,
4852 remote_amp_id);
4853 break;
4854 case L2CAP_MOVE_ROLE_RESPONDER:
4855 l2cap_do_move_respond(chan, result);
4856 break;
4857 default:
4858 l2cap_do_move_cancel(chan, result);
4859 break;
4860 }
4861 }
Mat Martineau8eb200b2012-10-23 15:24:17 -07004862}
4863
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004864static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004865 struct l2cap_cmd_hdr *cmd,
4866 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004867{
4868 struct l2cap_move_chan_req *req = data;
Mat Martineau1500109b2012-10-23 15:24:15 -07004869 struct l2cap_move_chan_rsp rsp;
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004870 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004871 u16 icid = 0;
4872 u16 result = L2CAP_MR_NOT_ALLOWED;
4873
4874 if (cmd_len != sizeof(*req))
4875 return -EPROTO;
4876
4877 icid = le16_to_cpu(req->icid);
4878
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004879 BT_DBG("icid 0x%4.4x, dest_amp_id %d", icid, req->dest_amp_id);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004880
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004881 if (!(conn->local_fixed_chan & L2CAP_FC_A2MP))
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004882 return -EINVAL;
4883
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004884 chan = l2cap_get_chan_by_dcid(conn, icid);
4885 if (!chan) {
Mat Martineau1500109b2012-10-23 15:24:15 -07004886 rsp.icid = cpu_to_le16(icid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004887 rsp.result = cpu_to_le16(L2CAP_MR_NOT_ALLOWED);
Mat Martineau1500109b2012-10-23 15:24:15 -07004888 l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP,
4889 sizeof(rsp), &rsp);
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004890 return 0;
4891 }
4892
Mat Martineau1500109b2012-10-23 15:24:15 -07004893 chan->ident = cmd->ident;
4894
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004895 if (chan->scid < L2CAP_CID_DYN_START ||
4896 chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY ||
4897 (chan->mode != L2CAP_MODE_ERTM &&
4898 chan->mode != L2CAP_MODE_STREAMING)) {
4899 result = L2CAP_MR_NOT_ALLOWED;
4900 goto send_move_response;
4901 }
4902
4903 if (chan->local_amp_id == req->dest_amp_id) {
4904 result = L2CAP_MR_SAME_ID;
4905 goto send_move_response;
4906 }
4907
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004908 if (req->dest_amp_id != AMP_ID_BREDR) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004909 struct hci_dev *hdev;
4910 hdev = hci_dev_get(req->dest_amp_id);
4911 if (!hdev || hdev->dev_type != HCI_AMP ||
4912 !test_bit(HCI_UP, &hdev->flags)) {
4913 if (hdev)
4914 hci_dev_put(hdev);
4915
4916 result = L2CAP_MR_BAD_ID;
4917 goto send_move_response;
4918 }
4919 hci_dev_put(hdev);
4920 }
4921
4922 /* Detect a move collision. Only send a collision response
4923 * if this side has "lost", otherwise proceed with the move.
4924 * The winner has the larger bd_addr.
4925 */
4926 if ((__chan_is_moving(chan) ||
4927 chan->move_role != L2CAP_MOVE_ROLE_NONE) &&
Marcel Holtmann6f59b902013-10-13 05:24:01 -07004928 bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004929 result = L2CAP_MR_COLLISION;
4930 goto send_move_response;
4931 }
4932
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004933 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
4934 l2cap_move_setup(chan);
4935 chan->move_id = req->dest_amp_id;
4936 icid = chan->dcid;
4937
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004938 if (req->dest_amp_id == AMP_ID_BREDR) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004939 /* Moving to BR/EDR */
4940 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4941 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4942 result = L2CAP_MR_PEND;
4943 } else {
4944 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4945 result = L2CAP_MR_SUCCESS;
4946 }
4947 } else {
4948 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
4949 /* Placeholder - uncomment when amp functions are available */
4950 /*amp_accept_physical(chan, req->dest_amp_id);*/
4951 result = L2CAP_MR_PEND;
4952 }
4953
4954send_move_response:
Mat Martineau1500109b2012-10-23 15:24:15 -07004955 l2cap_send_move_chan_rsp(chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004956
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004957 l2cap_chan_unlock(chan);
4958
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004959 return 0;
4960}
4961
Mat Martineau5b155ef2012-10-23 15:24:14 -07004962static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result)
4963{
4964 struct l2cap_chan *chan;
4965 struct hci_chan *hchan = NULL;
4966
4967 chan = l2cap_get_chan_by_scid(conn, icid);
4968 if (!chan) {
4969 l2cap_send_move_chan_cfm_icid(conn, icid);
4970 return;
4971 }
4972
4973 __clear_chan_timer(chan);
4974 if (result == L2CAP_MR_PEND)
4975 __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT);
4976
4977 switch (chan->move_state) {
4978 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
4979 /* Move confirm will be sent when logical link
4980 * is complete.
4981 */
4982 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
4983 break;
4984 case L2CAP_MOVE_WAIT_RSP_SUCCESS:
4985 if (result == L2CAP_MR_PEND) {
4986 break;
4987 } else if (test_bit(CONN_LOCAL_BUSY,
4988 &chan->conn_state)) {
4989 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4990 } else {
4991 /* Logical link is up or moving to BR/EDR,
4992 * proceed with move
4993 */
4994 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
4995 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
4996 }
4997 break;
4998 case L2CAP_MOVE_WAIT_RSP:
4999 /* Moving to AMP */
5000 if (result == L2CAP_MR_SUCCESS) {
5001 /* Remote is ready, send confirm immediately
5002 * after logical link is ready
5003 */
5004 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
5005 } else {
5006 /* Both logical link and move success
5007 * are required to confirm
5008 */
5009 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP;
5010 }
5011
5012 /* Placeholder - get hci_chan for logical link */
5013 if (!hchan) {
5014 /* Logical link not available */
5015 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5016 break;
5017 }
5018
5019 /* If the logical link is not yet connected, do not
5020 * send confirmation.
5021 */
5022 if (hchan->state != BT_CONNECTED)
5023 break;
5024
5025 /* Logical link is already ready to go */
5026
5027 chan->hs_hcon = hchan->conn;
5028 chan->hs_hcon->l2cap_data = chan->conn;
5029
5030 if (result == L2CAP_MR_SUCCESS) {
5031 /* Can confirm now */
5032 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
5033 } else {
5034 /* Now only need move success
5035 * to confirm
5036 */
5037 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
5038 }
5039
5040 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
5041 break;
5042 default:
5043 /* Any other amp move state means the move failed. */
5044 chan->move_id = chan->local_amp_id;
5045 l2cap_move_done(chan);
5046 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5047 }
5048
5049 l2cap_chan_unlock(chan);
5050}
5051
5052static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid,
5053 u16 result)
5054{
5055 struct l2cap_chan *chan;
5056
5057 chan = l2cap_get_chan_by_ident(conn, ident);
5058 if (!chan) {
5059 /* Could not locate channel, icid is best guess */
5060 l2cap_send_move_chan_cfm_icid(conn, icid);
5061 return;
5062 }
5063
5064 __clear_chan_timer(chan);
5065
5066 if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
5067 if (result == L2CAP_MR_COLLISION) {
5068 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
5069 } else {
5070 /* Cleanup - cancel move */
5071 chan->move_id = chan->local_amp_id;
5072 l2cap_move_done(chan);
5073 }
5074 }
5075
5076 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5077
5078 l2cap_chan_unlock(chan);
5079}
5080
5081static int l2cap_move_channel_rsp(struct l2cap_conn *conn,
5082 struct l2cap_cmd_hdr *cmd,
5083 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005084{
5085 struct l2cap_move_chan_rsp *rsp = data;
5086 u16 icid, result;
5087
5088 if (cmd_len != sizeof(*rsp))
5089 return -EPROTO;
5090
5091 icid = le16_to_cpu(rsp->icid);
5092 result = le16_to_cpu(rsp->result);
5093
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005094 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005095
Mat Martineau5b155ef2012-10-23 15:24:14 -07005096 if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND)
5097 l2cap_move_continue(conn, icid, result);
5098 else
5099 l2cap_move_fail(conn, cmd->ident, icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005100
5101 return 0;
5102}
5103
Mat Martineau5f3847a2012-10-23 15:24:12 -07005104static int l2cap_move_channel_confirm(struct l2cap_conn *conn,
5105 struct l2cap_cmd_hdr *cmd,
5106 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005107{
5108 struct l2cap_move_chan_cfm *cfm = data;
Mat Martineau5f3847a2012-10-23 15:24:12 -07005109 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005110 u16 icid, result;
5111
5112 if (cmd_len != sizeof(*cfm))
5113 return -EPROTO;
5114
5115 icid = le16_to_cpu(cfm->icid);
5116 result = le16_to_cpu(cfm->result);
5117
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005118 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005119
Mat Martineau5f3847a2012-10-23 15:24:12 -07005120 chan = l2cap_get_chan_by_dcid(conn, icid);
5121 if (!chan) {
5122 /* Spec requires a response even if the icid was not found */
5123 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5124 return 0;
5125 }
5126
5127 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) {
5128 if (result == L2CAP_MC_CONFIRMED) {
5129 chan->local_amp_id = chan->move_id;
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07005130 if (chan->local_amp_id == AMP_ID_BREDR)
Mat Martineau5f3847a2012-10-23 15:24:12 -07005131 __release_logical_link(chan);
5132 } else {
5133 chan->move_id = chan->local_amp_id;
5134 }
5135
5136 l2cap_move_done(chan);
5137 }
5138
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005139 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5140
Mat Martineau5f3847a2012-10-23 15:24:12 -07005141 l2cap_chan_unlock(chan);
5142
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005143 return 0;
5144}
5145
5146static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005147 struct l2cap_cmd_hdr *cmd,
5148 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005149{
5150 struct l2cap_move_chan_cfm_rsp *rsp = data;
Mat Martineau3fd71a02012-10-23 15:24:16 -07005151 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005152 u16 icid;
5153
5154 if (cmd_len != sizeof(*rsp))
5155 return -EPROTO;
5156
5157 icid = le16_to_cpu(rsp->icid);
5158
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005159 BT_DBG("icid 0x%4.4x", icid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005160
Mat Martineau3fd71a02012-10-23 15:24:16 -07005161 chan = l2cap_get_chan_by_scid(conn, icid);
5162 if (!chan)
5163 return 0;
5164
5165 __clear_chan_timer(chan);
5166
5167 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) {
5168 chan->local_amp_id = chan->move_id;
5169
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07005170 if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan)
Mat Martineau3fd71a02012-10-23 15:24:16 -07005171 __release_logical_link(chan);
5172
5173 l2cap_move_done(chan);
5174 }
5175
5176 l2cap_chan_unlock(chan);
5177
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005178 return 0;
5179}
5180
Claudio Takahaside731152011-02-11 19:28:55 -02005181static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005182 struct l2cap_cmd_hdr *cmd,
Johan Hedberg203e6392013-05-15 10:07:15 +03005183 u16 cmd_len, u8 *data)
Claudio Takahaside731152011-02-11 19:28:55 -02005184{
5185 struct hci_conn *hcon = conn->hcon;
5186 struct l2cap_conn_param_update_req *req;
5187 struct l2cap_conn_param_update_rsp rsp;
Johan Hedberg203e6392013-05-15 10:07:15 +03005188 u16 min, max, latency, to_multiplier;
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005189 int err;
Claudio Takahaside731152011-02-11 19:28:55 -02005190
Johan Hedberg40bef302014-07-16 11:42:27 +03005191 if (hcon->role != HCI_ROLE_MASTER)
Claudio Takahaside731152011-02-11 19:28:55 -02005192 return -EINVAL;
5193
Claudio Takahaside731152011-02-11 19:28:55 -02005194 if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
5195 return -EPROTO;
5196
5197 req = (struct l2cap_conn_param_update_req *) data;
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03005198 min = __le16_to_cpu(req->min);
5199 max = __le16_to_cpu(req->max);
Claudio Takahaside731152011-02-11 19:28:55 -02005200 latency = __le16_to_cpu(req->latency);
5201 to_multiplier = __le16_to_cpu(req->to_multiplier);
5202
5203 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 +01005204 min, max, latency, to_multiplier);
Claudio Takahaside731152011-02-11 19:28:55 -02005205
5206 memset(&rsp, 0, sizeof(rsp));
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005207
Andre Guedesd4905f22014-06-25 21:52:52 -03005208 err = hci_check_conn_params(min, max, latency, to_multiplier);
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005209 if (err)
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005210 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
Claudio Takahaside731152011-02-11 19:28:55 -02005211 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005212 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
Claudio Takahaside731152011-02-11 19:28:55 -02005213
5214 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005215 sizeof(rsp), &rsp);
Claudio Takahaside731152011-02-11 19:28:55 -02005216
Andre Guedesffb5a8272014-07-01 18:10:11 -03005217 if (!err) {
Johan Hedbergf4869e22014-07-02 17:37:32 +03005218 u8 store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03005219
Johan Hedbergf4869e22014-07-02 17:37:32 +03005220 store_hint = hci_le_conn_update(hcon, min, max, latency,
5221 to_multiplier);
5222 mgmt_new_conn_param(hcon->hdev, &hcon->dst, hcon->dst_type,
5223 store_hint, min, max, latency,
5224 to_multiplier);
5225
Andre Guedesffb5a8272014-07-01 18:10:11 -03005226 }
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005227
Claudio Takahaside731152011-02-11 19:28:55 -02005228 return 0;
5229}
5230
Johan Hedbergf1496de2013-05-13 14:15:56 +03005231static int l2cap_le_connect_rsp(struct l2cap_conn *conn,
5232 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5233 u8 *data)
5234{
5235 struct l2cap_le_conn_rsp *rsp = (struct l2cap_le_conn_rsp *) data;
Johan Hedberg3e64b7b2014-11-13 10:55:19 +02005236 struct hci_conn *hcon = conn->hcon;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005237 u16 dcid, mtu, mps, credits, result;
5238 struct l2cap_chan *chan;
Johan Hedberg3e64b7b2014-11-13 10:55:19 +02005239 int err, sec_level;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005240
5241 if (cmd_len < sizeof(*rsp))
5242 return -EPROTO;
5243
5244 dcid = __le16_to_cpu(rsp->dcid);
5245 mtu = __le16_to_cpu(rsp->mtu);
5246 mps = __le16_to_cpu(rsp->mps);
5247 credits = __le16_to_cpu(rsp->credits);
5248 result = __le16_to_cpu(rsp->result);
5249
5250 if (result == L2CAP_CR_SUCCESS && (mtu < 23 || mps < 23))
5251 return -EPROTO;
5252
5253 BT_DBG("dcid 0x%4.4x mtu %u mps %u credits %u result 0x%2.2x",
5254 dcid, mtu, mps, credits, result);
5255
5256 mutex_lock(&conn->chan_lock);
5257
5258 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
5259 if (!chan) {
5260 err = -EBADSLT;
5261 goto unlock;
5262 }
5263
5264 err = 0;
5265
5266 l2cap_chan_lock(chan);
5267
5268 switch (result) {
5269 case L2CAP_CR_SUCCESS:
5270 chan->ident = 0;
5271 chan->dcid = dcid;
5272 chan->omtu = mtu;
5273 chan->remote_mps = mps;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005274 chan->tx_credits = credits;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005275 l2cap_chan_ready(chan);
5276 break;
5277
Johan Hedberg3e64b7b2014-11-13 10:55:19 +02005278 case L2CAP_CR_AUTHENTICATION:
5279 case L2CAP_CR_ENCRYPTION:
5280 /* If we already have MITM protection we can't do
5281 * anything.
5282 */
5283 if (hcon->sec_level > BT_SECURITY_MEDIUM) {
5284 l2cap_chan_del(chan, ECONNREFUSED);
5285 break;
5286 }
5287
5288 sec_level = hcon->sec_level + 1;
5289 if (chan->sec_level < sec_level)
5290 chan->sec_level = sec_level;
5291
5292 /* We'll need to send a new Connect Request */
5293 clear_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags);
5294
5295 smp_conn_security(hcon, chan->sec_level);
5296 break;
5297
Johan Hedbergf1496de2013-05-13 14:15:56 +03005298 default:
5299 l2cap_chan_del(chan, ECONNREFUSED);
5300 break;
5301 }
5302
5303 l2cap_chan_unlock(chan);
5304
5305unlock:
5306 mutex_unlock(&conn->chan_lock);
5307
5308 return err;
5309}
5310
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005311static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005312 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5313 u8 *data)
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005314{
5315 int err = 0;
5316
5317 switch (cmd->code) {
5318 case L2CAP_COMMAND_REJ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005319 l2cap_command_rej(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005320 break;
5321
5322 case L2CAP_CONN_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005323 err = l2cap_connect_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005324 break;
5325
5326 case L2CAP_CONN_RSP:
Mat Martineauf5a25982012-10-11 17:48:21 +03005327 case L2CAP_CREATE_CHAN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005328 l2cap_connect_create_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005329 break;
5330
5331 case L2CAP_CONF_REQ:
5332 err = l2cap_config_req(conn, cmd, cmd_len, data);
5333 break;
5334
5335 case L2CAP_CONF_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005336 l2cap_config_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005337 break;
5338
5339 case L2CAP_DISCONN_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005340 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005341 break;
5342
5343 case L2CAP_DISCONN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005344 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005345 break;
5346
5347 case L2CAP_ECHO_REQ:
5348 l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
5349 break;
5350
5351 case L2CAP_ECHO_RSP:
5352 break;
5353
5354 case L2CAP_INFO_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005355 err = l2cap_information_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005356 break;
5357
5358 case L2CAP_INFO_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005359 l2cap_information_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005360 break;
5361
Mat Martineauf94ff6f2011-11-02 16:18:32 -07005362 case L2CAP_CREATE_CHAN_REQ:
5363 err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
5364 break;
5365
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005366 case L2CAP_MOVE_CHAN_REQ:
5367 err = l2cap_move_channel_req(conn, cmd, cmd_len, data);
5368 break;
5369
5370 case L2CAP_MOVE_CHAN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005371 l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005372 break;
5373
5374 case L2CAP_MOVE_CHAN_CFM:
5375 err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data);
5376 break;
5377
5378 case L2CAP_MOVE_CHAN_CFM_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005379 l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005380 break;
5381
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005382 default:
5383 BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
5384 err = -EINVAL;
5385 break;
5386 }
5387
5388 return err;
5389}
5390
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005391static int l2cap_le_connect_req(struct l2cap_conn *conn,
5392 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5393 u8 *data)
5394{
5395 struct l2cap_le_conn_req *req = (struct l2cap_le_conn_req *) data;
5396 struct l2cap_le_conn_rsp rsp;
5397 struct l2cap_chan *chan, *pchan;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005398 u16 dcid, scid, credits, mtu, mps;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005399 __le16 psm;
5400 u8 result;
5401
5402 if (cmd_len != sizeof(*req))
5403 return -EPROTO;
5404
5405 scid = __le16_to_cpu(req->scid);
5406 mtu = __le16_to_cpu(req->mtu);
5407 mps = __le16_to_cpu(req->mps);
5408 psm = req->psm;
5409 dcid = 0;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005410 credits = 0;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005411
5412 if (mtu < 23 || mps < 23)
5413 return -EPROTO;
5414
5415 BT_DBG("psm 0x%2.2x scid 0x%4.4x mtu %u mps %u", __le16_to_cpu(psm),
5416 scid, mtu, mps);
5417
5418 /* Check if we have socket listening on psm */
5419 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
5420 &conn->hcon->dst, LE_LINK);
5421 if (!pchan) {
5422 result = L2CAP_CR_BAD_PSM;
5423 chan = NULL;
5424 goto response;
5425 }
5426
5427 mutex_lock(&conn->chan_lock);
5428 l2cap_chan_lock(pchan);
5429
Johan Hedberg35dc6f82014-11-13 10:55:18 +02005430 if (!smp_sufficient_security(conn->hcon, pchan->sec_level,
5431 SMP_ALLOW_STK)) {
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005432 result = L2CAP_CR_AUTHENTICATION;
5433 chan = NULL;
5434 goto response_unlock;
5435 }
5436
5437 /* Check if we already have channel with that dcid */
5438 if (__l2cap_get_chan_by_dcid(conn, scid)) {
5439 result = L2CAP_CR_NO_MEM;
5440 chan = NULL;
5441 goto response_unlock;
5442 }
5443
5444 chan = pchan->ops->new_connection(pchan);
5445 if (!chan) {
5446 result = L2CAP_CR_NO_MEM;
5447 goto response_unlock;
5448 }
5449
Johan Hedberg0ce43ce2013-12-05 14:55:33 +02005450 l2cap_le_flowctl_init(chan);
5451
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005452 bacpy(&chan->src, &conn->hcon->src);
5453 bacpy(&chan->dst, &conn->hcon->dst);
Johan Hedberga250e042015-01-15 13:06:44 +02005454 chan->src_type = bdaddr_src_type(conn->hcon);
5455 chan->dst_type = bdaddr_dst_type(conn->hcon);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005456 chan->psm = psm;
5457 chan->dcid = scid;
5458 chan->omtu = mtu;
5459 chan->remote_mps = mps;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005460 chan->tx_credits = __le16_to_cpu(req->credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005461
5462 __l2cap_chan_add(conn, chan);
5463 dcid = chan->scid;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005464 credits = chan->rx_credits;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005465
5466 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
5467
5468 chan->ident = cmd->ident;
5469
5470 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
5471 l2cap_state_change(chan, BT_CONNECT2);
Johan Hedberg434714d2014-09-01 09:45:03 +03005472 /* The following result value is actually not defined
5473 * for LE CoC but we use it to let the function know
5474 * that it should bail out after doing its cleanup
5475 * instead of sending a response.
5476 */
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005477 result = L2CAP_CR_PEND;
5478 chan->ops->defer(chan);
5479 } else {
5480 l2cap_chan_ready(chan);
5481 result = L2CAP_CR_SUCCESS;
5482 }
5483
5484response_unlock:
5485 l2cap_chan_unlock(pchan);
5486 mutex_unlock(&conn->chan_lock);
Johan Hedberga24cce12014-08-07 22:56:42 +03005487 l2cap_chan_put(pchan);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005488
5489 if (result == L2CAP_CR_PEND)
5490 return 0;
5491
5492response:
5493 if (chan) {
5494 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02005495 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005496 } else {
5497 rsp.mtu = 0;
5498 rsp.mps = 0;
5499 }
5500
5501 rsp.dcid = cpu_to_le16(dcid);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005502 rsp.credits = cpu_to_le16(credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005503 rsp.result = cpu_to_le16(result);
5504
5505 l2cap_send_cmd(conn, cmd->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), &rsp);
5506
5507 return 0;
5508}
5509
Johan Hedbergfad5fc892013-12-05 09:45:01 +02005510static inline int l2cap_le_credits(struct l2cap_conn *conn,
5511 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5512 u8 *data)
5513{
5514 struct l2cap_le_credits *pkt;
5515 struct l2cap_chan *chan;
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005516 u16 cid, credits, max_credits;
Johan Hedbergfad5fc892013-12-05 09:45:01 +02005517
5518 if (cmd_len != sizeof(*pkt))
5519 return -EPROTO;
5520
5521 pkt = (struct l2cap_le_credits *) data;
5522 cid = __le16_to_cpu(pkt->cid);
5523 credits = __le16_to_cpu(pkt->credits);
5524
5525 BT_DBG("cid 0x%4.4x credits 0x%4.4x", cid, credits);
5526
5527 chan = l2cap_get_chan_by_dcid(conn, cid);
5528 if (!chan)
5529 return -EBADSLT;
5530
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005531 max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits;
5532 if (credits > max_credits) {
5533 BT_ERR("LE credits overflow");
5534 l2cap_send_disconn_req(chan, ECONNRESET);
Martin Townsendee930532014-10-13 19:24:45 +01005535 l2cap_chan_unlock(chan);
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005536
5537 /* Return 0 so that we don't trigger an unnecessary
5538 * command reject packet.
5539 */
5540 return 0;
5541 }
5542
Johan Hedbergfad5fc892013-12-05 09:45:01 +02005543 chan->tx_credits += credits;
5544
5545 while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) {
5546 l2cap_do_send(chan, skb_dequeue(&chan->tx_q));
5547 chan->tx_credits--;
5548 }
5549
5550 if (chan->tx_credits)
5551 chan->ops->resume(chan);
5552
5553 l2cap_chan_unlock(chan);
5554
5555 return 0;
5556}
5557
Johan Hedberg71fb4192013-12-10 10:52:48 +02005558static inline int l2cap_le_command_rej(struct l2cap_conn *conn,
5559 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5560 u8 *data)
5561{
5562 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
5563 struct l2cap_chan *chan;
5564
5565 if (cmd_len < sizeof(*rej))
5566 return -EPROTO;
5567
5568 mutex_lock(&conn->chan_lock);
5569
5570 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
5571 if (!chan)
5572 goto done;
5573
5574 l2cap_chan_lock(chan);
5575 l2cap_chan_del(chan, ECONNREFUSED);
5576 l2cap_chan_unlock(chan);
5577
5578done:
5579 mutex_unlock(&conn->chan_lock);
5580 return 0;
5581}
5582
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005583static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
Johan Hedberg203e6392013-05-15 10:07:15 +03005584 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5585 u8 *data)
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005586{
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005587 int err = 0;
5588
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005589 switch (cmd->code) {
5590 case L2CAP_COMMAND_REJ:
Johan Hedberg71fb4192013-12-10 10:52:48 +02005591 l2cap_le_command_rej(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005592 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005593
5594 case L2CAP_CONN_PARAM_UPDATE_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005595 err = l2cap_conn_param_update_req(conn, cmd, cmd_len, data);
5596 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005597
5598 case L2CAP_CONN_PARAM_UPDATE_RSP:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005599 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005600
Johan Hedbergf1496de2013-05-13 14:15:56 +03005601 case L2CAP_LE_CONN_RSP:
5602 l2cap_le_connect_rsp(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005603 break;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005604
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005605 case L2CAP_LE_CONN_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005606 err = l2cap_le_connect_req(conn, cmd, cmd_len, data);
5607 break;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005608
Johan Hedbergfad5fc892013-12-05 09:45:01 +02005609 case L2CAP_LE_CREDITS:
5610 err = l2cap_le_credits(conn, cmd, cmd_len, data);
5611 break;
5612
Johan Hedberg3defe012013-05-15 10:16:06 +03005613 case L2CAP_DISCONN_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005614 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
5615 break;
Johan Hedberg3defe012013-05-15 10:16:06 +03005616
5617 case L2CAP_DISCONN_RSP:
5618 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005619 break;
Johan Hedberg3defe012013-05-15 10:16:06 +03005620
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005621 default:
5622 BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005623 err = -EINVAL;
5624 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005625 }
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005626
5627 return err;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005628}
5629
Johan Hedbergc5623552013-04-29 19:35:33 +03005630static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,
5631 struct sk_buff *skb)
5632{
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005633 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005634 struct l2cap_cmd_hdr *cmd;
5635 u16 len;
Johan Hedbergc5623552013-04-29 19:35:33 +03005636 int err;
5637
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005638 if (hcon->type != LE_LINK)
Marcel Holtmann3b166292013-10-02 08:28:21 -07005639 goto drop;
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005640
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005641 if (skb->len < L2CAP_CMD_HDR_SIZE)
5642 goto drop;
Johan Hedbergc5623552013-04-29 19:35:33 +03005643
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005644 cmd = (void *) skb->data;
5645 skb_pull(skb, L2CAP_CMD_HDR_SIZE);
Johan Hedbergc5623552013-04-29 19:35:33 +03005646
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005647 len = le16_to_cpu(cmd->len);
Johan Hedbergc5623552013-04-29 19:35:33 +03005648
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005649 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd->code, len, cmd->ident);
Johan Hedbergc5623552013-04-29 19:35:33 +03005650
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005651 if (len != skb->len || !cmd->ident) {
5652 BT_DBG("corrupted command");
5653 goto drop;
5654 }
Johan Hedbergc5623552013-04-29 19:35:33 +03005655
Johan Hedberg203e6392013-05-15 10:07:15 +03005656 err = l2cap_le_sig_cmd(conn, cmd, len, skb->data);
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005657 if (err) {
5658 struct l2cap_cmd_rej_unk rej;
Johan Hedbergc5623552013-04-29 19:35:33 +03005659
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005660 BT_ERR("Wrong link type (%d)", err);
Johan Hedbergc5623552013-04-29 19:35:33 +03005661
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005662 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005663 l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
5664 sizeof(rej), &rej);
Johan Hedbergc5623552013-04-29 19:35:33 +03005665 }
5666
Marcel Holtmann3b166292013-10-02 08:28:21 -07005667drop:
Johan Hedbergc5623552013-04-29 19:35:33 +03005668 kfree_skb(skb);
5669}
5670
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005671static inline void l2cap_sig_channel(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005672 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673{
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005674 struct hci_conn *hcon = conn->hcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 u8 *data = skb->data;
5676 int len = skb->len;
5677 struct l2cap_cmd_hdr cmd;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005678 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679
5680 l2cap_raw_recv(conn, skb);
5681
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005682 if (hcon->type != ACL_LINK)
Marcel Holtmann3b166292013-10-02 08:28:21 -07005683 goto drop;
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005684
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 while (len >= L2CAP_CMD_HDR_SIZE) {
Al Viro88219a02007-07-29 00:17:25 -07005686 u16 cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
5688 data += L2CAP_CMD_HDR_SIZE;
5689 len -= L2CAP_CMD_HDR_SIZE;
5690
Al Viro88219a02007-07-29 00:17:25 -07005691 cmd_len = le16_to_cpu(cmd.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
Gustavo Padovan2d792812012-10-06 10:07:01 +01005693 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len,
5694 cmd.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
Al Viro88219a02007-07-29 00:17:25 -07005696 if (cmd_len > len || !cmd.ident) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 BT_DBG("corrupted command");
5698 break;
5699 }
5700
Johan Hedbergc5623552013-04-29 19:35:33 +03005701 err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 if (err) {
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03005703 struct l2cap_cmd_rej_unk rej;
Gustavo F. Padovan2c6d1a22011-03-23 14:38:32 -03005704
5705 BT_ERR("Wrong link type (%d)", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005707 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
Gustavo Padovan2d792812012-10-06 10:07:01 +01005708 l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ,
5709 sizeof(rej), &rej);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 }
5711
Al Viro88219a02007-07-29 00:17:25 -07005712 data += cmd_len;
5713 len -= cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 }
5715
Marcel Holtmann3b166292013-10-02 08:28:21 -07005716drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 kfree_skb(skb);
5718}
5719
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03005720static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005721{
5722 u16 our_fcs, rcv_fcs;
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03005723 int hdr_size;
5724
5725 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
5726 hdr_size = L2CAP_EXT_HDR_SIZE;
5727 else
5728 hdr_size = L2CAP_ENH_HDR_SIZE;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005729
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03005730 if (chan->fcs == L2CAP_FCS_CRC16) {
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03005731 skb_trim(skb, skb->len - L2CAP_FCS_SIZE);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005732 rcv_fcs = get_unaligned_le16(skb->data + skb->len);
5733 our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
5734
5735 if (our_fcs != rcv_fcs)
João Paulo Rechi Vita7a560e52010-06-22 13:56:27 -03005736 return -EBADMSG;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005737 }
5738 return 0;
5739}
5740
Mat Martineau6ea00482012-05-17 20:53:52 -07005741static void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005742{
Mat Martineaue31f7632012-05-17 20:53:41 -07005743 struct l2cap_ctrl control;
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005744
Mat Martineaue31f7632012-05-17 20:53:41 -07005745 BT_DBG("chan %p", chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005746
Mat Martineaue31f7632012-05-17 20:53:41 -07005747 memset(&control, 0, sizeof(control));
5748 control.sframe = 1;
5749 control.final = 1;
5750 control.reqseq = chan->buffer_seq;
5751 set_bit(CONN_SEND_FBIT, &chan->conn_state);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005752
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03005753 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
Mat Martineaue31f7632012-05-17 20:53:41 -07005754 control.super = L2CAP_SUPER_RNR;
5755 l2cap_send_sframe(chan, &control);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005756 }
5757
Mat Martineaue31f7632012-05-17 20:53:41 -07005758 if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
5759 chan->unacked_frames > 0)
5760 __set_retrans_timer(chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005761
Mat Martineaue31f7632012-05-17 20:53:41 -07005762 /* Send pending iframes */
Gustavo F. Padovan525cd182011-03-25 19:43:39 -03005763 l2cap_ertm_send(chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005764
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03005765 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
Mat Martineaue31f7632012-05-17 20:53:41 -07005766 test_bit(CONN_SEND_FBIT, &chan->conn_state)) {
5767 /* F-bit wasn't sent in an s-frame or i-frame yet, so
5768 * send it now.
5769 */
5770 control.super = L2CAP_SUPER_RR;
5771 l2cap_send_sframe(chan, &control);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005772 }
5773}
5774
Gustavo Padovan2d792812012-10-06 10:07:01 +01005775static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag,
5776 struct sk_buff **last_frag)
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005777{
Mat Martineau84084a32011-07-22 14:54:00 -07005778 /* skb->len reflects data in skb as well as all fragments
5779 * skb->data_len reflects only data in fragments
5780 */
5781 if (!skb_has_frag_list(skb))
5782 skb_shinfo(skb)->frag_list = new_frag;
5783
5784 new_frag->next = NULL;
5785
5786 (*last_frag)->next = new_frag;
5787 *last_frag = new_frag;
5788
5789 skb->len += new_frag->len;
5790 skb->data_len += new_frag->len;
5791 skb->truesize += new_frag->truesize;
5792}
5793
Mat Martineau4b51dae92012-05-17 20:53:37 -07005794static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb,
5795 struct l2cap_ctrl *control)
Mat Martineau84084a32011-07-22 14:54:00 -07005796{
5797 int err = -EINVAL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005798
Mat Martineau4b51dae92012-05-17 20:53:37 -07005799 switch (control->sar) {
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005800 case L2CAP_SAR_UNSEGMENTED:
Mat Martineau84084a32011-07-22 14:54:00 -07005801 if (chan->sdu)
5802 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005803
Gustavo Padovan80b98022012-05-27 22:27:51 -03005804 err = chan->ops->recv(chan, skb);
Mat Martineau84084a32011-07-22 14:54:00 -07005805 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005806
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005807 case L2CAP_SAR_START:
Mat Martineau84084a32011-07-22 14:54:00 -07005808 if (chan->sdu)
5809 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005810
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005811 chan->sdu_len = get_unaligned_le16(skb->data);
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03005812 skb_pull(skb, L2CAP_SDULEN_SIZE);
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005813
Mat Martineau84084a32011-07-22 14:54:00 -07005814 if (chan->sdu_len > chan->imtu) {
5815 err = -EMSGSIZE;
5816 break;
5817 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005818
Mat Martineau84084a32011-07-22 14:54:00 -07005819 if (skb->len >= chan->sdu_len)
5820 break;
5821
5822 chan->sdu = skb;
5823 chan->sdu_last_frag = skb;
5824
5825 skb = NULL;
5826 err = 0;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005827 break;
5828
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005829 case L2CAP_SAR_CONTINUE:
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005830 if (!chan->sdu)
Mat Martineau84084a32011-07-22 14:54:00 -07005831 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005832
Mat Martineau84084a32011-07-22 14:54:00 -07005833 append_skb_frag(chan->sdu, skb,
5834 &chan->sdu_last_frag);
5835 skb = NULL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005836
Mat Martineau84084a32011-07-22 14:54:00 -07005837 if (chan->sdu->len >= chan->sdu_len)
5838 break;
Gustavo F. Padovan4178ba42010-05-01 16:15:45 -03005839
Mat Martineau84084a32011-07-22 14:54:00 -07005840 err = 0;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005841 break;
5842
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005843 case L2CAP_SAR_END:
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005844 if (!chan->sdu)
Mat Martineau84084a32011-07-22 14:54:00 -07005845 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005846
Mat Martineau84084a32011-07-22 14:54:00 -07005847 append_skb_frag(chan->sdu, skb,
5848 &chan->sdu_last_frag);
5849 skb = NULL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005850
Mat Martineau84084a32011-07-22 14:54:00 -07005851 if (chan->sdu->len != chan->sdu_len)
5852 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005853
Gustavo Padovan80b98022012-05-27 22:27:51 -03005854 err = chan->ops->recv(chan, chan->sdu);
Gustavo F. Padovan4178ba42010-05-01 16:15:45 -03005855
Mat Martineau84084a32011-07-22 14:54:00 -07005856 if (!err) {
5857 /* Reassembly complete */
5858 chan->sdu = NULL;
5859 chan->sdu_last_frag = NULL;
5860 chan->sdu_len = 0;
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03005861 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005862 break;
5863 }
5864
Mat Martineau84084a32011-07-22 14:54:00 -07005865 if (err) {
5866 kfree_skb(skb);
5867 kfree_skb(chan->sdu);
5868 chan->sdu = NULL;
5869 chan->sdu_last_frag = NULL;
5870 chan->sdu_len = 0;
5871 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005872
Mat Martineau84084a32011-07-22 14:54:00 -07005873 return err;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005874}
5875
Mat Martineau32b32732012-10-23 15:24:11 -07005876static int l2cap_resegment(struct l2cap_chan *chan)
5877{
5878 /* Placeholder */
5879 return 0;
5880}
5881
Mat Martineaue3281402011-07-07 09:39:02 -07005882void l2cap_chan_busy(struct l2cap_chan *chan, int busy)
Gustavo F. Padovan712132e2010-06-21 19:39:50 -03005883{
Mat Martineau61aa4f52012-05-17 20:53:40 -07005884 u8 event;
5885
5886 if (chan->mode != L2CAP_MODE_ERTM)
5887 return;
5888
5889 event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR;
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03005890 l2cap_tx(chan, NULL, NULL, event);
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03005891}
5892
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005893static int l2cap_rx_queued_iframes(struct l2cap_chan *chan)
5894{
Mat Martineau63838722012-05-17 20:53:45 -07005895 int err = 0;
5896 /* Pass sequential frames to l2cap_reassemble_sdu()
5897 * until a gap is encountered.
5898 */
5899
5900 BT_DBG("chan %p", chan);
5901
5902 while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
5903 struct sk_buff *skb;
5904 BT_DBG("Searching for skb with txseq %d (queue len %d)",
5905 chan->buffer_seq, skb_queue_len(&chan->srej_q));
5906
5907 skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq);
5908
5909 if (!skb)
5910 break;
5911
5912 skb_unlink(skb, &chan->srej_q);
5913 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
5914 err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->control);
5915 if (err)
5916 break;
5917 }
5918
5919 if (skb_queue_empty(&chan->srej_q)) {
5920 chan->rx_state = L2CAP_RX_STATE_RECV;
5921 l2cap_send_ack(chan);
5922 }
5923
5924 return err;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005925}
5926
5927static void l2cap_handle_srej(struct l2cap_chan *chan,
5928 struct l2cap_ctrl *control)
5929{
Mat Martineauf80842a2012-05-17 20:53:46 -07005930 struct sk_buff *skb;
5931
5932 BT_DBG("chan %p, control %p", chan, control);
5933
5934 if (control->reqseq == chan->next_tx_seq) {
5935 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005936 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineauf80842a2012-05-17 20:53:46 -07005937 return;
5938 }
5939
5940 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
5941
5942 if (skb == NULL) {
5943 BT_DBG("Seq %d not available for retransmission",
5944 control->reqseq);
5945 return;
5946 }
5947
5948 if (chan->max_tx != 0 && bt_cb(skb)->control.retries >= chan->max_tx) {
5949 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005950 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineauf80842a2012-05-17 20:53:46 -07005951 return;
5952 }
5953
5954 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
5955
5956 if (control->poll) {
5957 l2cap_pass_to_tx(chan, control);
5958
5959 set_bit(CONN_SEND_FBIT, &chan->conn_state);
5960 l2cap_retransmit(chan, control);
5961 l2cap_ertm_send(chan);
5962
5963 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
5964 set_bit(CONN_SREJ_ACT, &chan->conn_state);
5965 chan->srej_save_reqseq = control->reqseq;
5966 }
5967 } else {
5968 l2cap_pass_to_tx_fbit(chan, control);
5969
5970 if (control->final) {
5971 if (chan->srej_save_reqseq != control->reqseq ||
5972 !test_and_clear_bit(CONN_SREJ_ACT,
5973 &chan->conn_state))
5974 l2cap_retransmit(chan, control);
5975 } else {
5976 l2cap_retransmit(chan, control);
5977 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
5978 set_bit(CONN_SREJ_ACT, &chan->conn_state);
5979 chan->srej_save_reqseq = control->reqseq;
5980 }
5981 }
5982 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005983}
5984
5985static void l2cap_handle_rej(struct l2cap_chan *chan,
5986 struct l2cap_ctrl *control)
5987{
Mat Martineaufcd289d2012-05-17 20:53:47 -07005988 struct sk_buff *skb;
5989
5990 BT_DBG("chan %p, control %p", chan, control);
5991
5992 if (control->reqseq == chan->next_tx_seq) {
5993 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005994 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaufcd289d2012-05-17 20:53:47 -07005995 return;
5996 }
5997
5998 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
5999
6000 if (chan->max_tx && skb &&
6001 bt_cb(skb)->control.retries >= chan->max_tx) {
6002 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006003 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaufcd289d2012-05-17 20:53:47 -07006004 return;
6005 }
6006
6007 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6008
6009 l2cap_pass_to_tx(chan, control);
6010
6011 if (control->final) {
6012 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
6013 l2cap_retransmit_all(chan, control);
6014 } else {
6015 l2cap_retransmit_all(chan, control);
6016 l2cap_ertm_send(chan);
6017 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F)
6018 set_bit(CONN_REJ_ACT, &chan->conn_state);
6019 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006020}
6021
Mat Martineau4b51dae92012-05-17 20:53:37 -07006022static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq)
6023{
6024 BT_DBG("chan %p, txseq %d", chan, txseq);
6025
6026 BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq,
6027 chan->expected_tx_seq);
6028
6029 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
6030 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
Gustavo Padovan2d792812012-10-06 10:07:01 +01006031 chan->tx_win) {
Mat Martineau4b51dae92012-05-17 20:53:37 -07006032 /* See notes below regarding "double poll" and
6033 * invalid packets.
6034 */
6035 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
6036 BT_DBG("Invalid/Ignore - after SREJ");
6037 return L2CAP_TXSEQ_INVALID_IGNORE;
6038 } else {
6039 BT_DBG("Invalid - in window after SREJ sent");
6040 return L2CAP_TXSEQ_INVALID;
6041 }
6042 }
6043
6044 if (chan->srej_list.head == txseq) {
6045 BT_DBG("Expected SREJ");
6046 return L2CAP_TXSEQ_EXPECTED_SREJ;
6047 }
6048
6049 if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) {
6050 BT_DBG("Duplicate SREJ - txseq already stored");
6051 return L2CAP_TXSEQ_DUPLICATE_SREJ;
6052 }
6053
6054 if (l2cap_seq_list_contains(&chan->srej_list, txseq)) {
6055 BT_DBG("Unexpected SREJ - not requested");
6056 return L2CAP_TXSEQ_UNEXPECTED_SREJ;
6057 }
6058 }
6059
6060 if (chan->expected_tx_seq == txseq) {
6061 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
6062 chan->tx_win) {
6063 BT_DBG("Invalid - txseq outside tx window");
6064 return L2CAP_TXSEQ_INVALID;
6065 } else {
6066 BT_DBG("Expected");
6067 return L2CAP_TXSEQ_EXPECTED;
6068 }
6069 }
6070
6071 if (__seq_offset(chan, txseq, chan->last_acked_seq) <
Gustavo Padovan2d792812012-10-06 10:07:01 +01006072 __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) {
Mat Martineau4b51dae92012-05-17 20:53:37 -07006073 BT_DBG("Duplicate - expected_tx_seq later than txseq");
6074 return L2CAP_TXSEQ_DUPLICATE;
6075 }
6076
6077 if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) {
6078 /* A source of invalid packets is a "double poll" condition,
6079 * where delays cause us to send multiple poll packets. If
6080 * the remote stack receives and processes both polls,
6081 * sequence numbers can wrap around in such a way that a
6082 * resent frame has a sequence number that looks like new data
6083 * with a sequence gap. This would trigger an erroneous SREJ
6084 * request.
6085 *
6086 * Fortunately, this is impossible with a tx window that's
6087 * less than half of the maximum sequence number, which allows
6088 * invalid frames to be safely ignored.
6089 *
6090 * With tx window sizes greater than half of the tx window
6091 * maximum, the frame is invalid and cannot be ignored. This
6092 * causes a disconnect.
6093 */
6094
6095 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
6096 BT_DBG("Invalid/Ignore - txseq outside tx window");
6097 return L2CAP_TXSEQ_INVALID_IGNORE;
6098 } else {
6099 BT_DBG("Invalid - txseq outside tx window");
6100 return L2CAP_TXSEQ_INVALID;
6101 }
6102 } else {
6103 BT_DBG("Unexpected - txseq indicates missing frames");
6104 return L2CAP_TXSEQ_UNEXPECTED;
6105 }
6106}
6107
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006108static int l2cap_rx_state_recv(struct l2cap_chan *chan,
6109 struct l2cap_ctrl *control,
6110 struct sk_buff *skb, u8 event)
6111{
6112 int err = 0;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006113 bool skb_in_use = false;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006114
6115 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6116 event);
6117
6118 switch (event) {
6119 case L2CAP_EV_RECV_IFRAME:
6120 switch (l2cap_classify_txseq(chan, control->txseq)) {
6121 case L2CAP_TXSEQ_EXPECTED:
6122 l2cap_pass_to_tx(chan, control);
6123
6124 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6125 BT_DBG("Busy, discarding expected seq %d",
6126 control->txseq);
6127 break;
6128 }
6129
6130 chan->expected_tx_seq = __next_seq(chan,
6131 control->txseq);
6132
6133 chan->buffer_seq = chan->expected_tx_seq;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006134 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006135
6136 err = l2cap_reassemble_sdu(chan, skb, control);
6137 if (err)
6138 break;
6139
6140 if (control->final) {
6141 if (!test_and_clear_bit(CONN_REJ_ACT,
6142 &chan->conn_state)) {
6143 control->final = 0;
6144 l2cap_retransmit_all(chan, control);
6145 l2cap_ertm_send(chan);
6146 }
6147 }
6148
6149 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
6150 l2cap_send_ack(chan);
6151 break;
6152 case L2CAP_TXSEQ_UNEXPECTED:
6153 l2cap_pass_to_tx(chan, control);
6154
6155 /* Can't issue SREJ frames in the local busy state.
6156 * Drop this frame, it will be seen as missing
6157 * when local busy is exited.
6158 */
6159 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6160 BT_DBG("Busy, discarding unexpected seq %d",
6161 control->txseq);
6162 break;
6163 }
6164
6165 /* There was a gap in the sequence, so an SREJ
6166 * must be sent for each missing frame. The
6167 * current frame is stored for later use.
6168 */
6169 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006170 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006171 BT_DBG("Queued %p (queue len %d)", skb,
6172 skb_queue_len(&chan->srej_q));
6173
6174 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
6175 l2cap_seq_list_clear(&chan->srej_list);
6176 l2cap_send_srej(chan, control->txseq);
6177
6178 chan->rx_state = L2CAP_RX_STATE_SREJ_SENT;
6179 break;
6180 case L2CAP_TXSEQ_DUPLICATE:
6181 l2cap_pass_to_tx(chan, control);
6182 break;
6183 case L2CAP_TXSEQ_INVALID_IGNORE:
6184 break;
6185 case L2CAP_TXSEQ_INVALID:
6186 default:
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006187 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006188 break;
6189 }
6190 break;
6191 case L2CAP_EV_RECV_RR:
6192 l2cap_pass_to_tx(chan, control);
6193 if (control->final) {
6194 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6195
Mat Martineaue6a3ee62012-10-23 15:24:22 -07006196 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) &&
6197 !__chan_is_moving(chan)) {
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006198 control->final = 0;
6199 l2cap_retransmit_all(chan, control);
6200 }
6201
6202 l2cap_ertm_send(chan);
6203 } else if (control->poll) {
6204 l2cap_send_i_or_rr_or_rnr(chan);
6205 } else {
6206 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6207 &chan->conn_state) &&
6208 chan->unacked_frames)
6209 __set_retrans_timer(chan);
6210
6211 l2cap_ertm_send(chan);
6212 }
6213 break;
6214 case L2CAP_EV_RECV_RNR:
6215 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6216 l2cap_pass_to_tx(chan, control);
6217 if (control && control->poll) {
6218 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6219 l2cap_send_rr_or_rnr(chan, 0);
6220 }
6221 __clear_retrans_timer(chan);
6222 l2cap_seq_list_clear(&chan->retrans_list);
6223 break;
6224 case L2CAP_EV_RECV_REJ:
6225 l2cap_handle_rej(chan, control);
6226 break;
6227 case L2CAP_EV_RECV_SREJ:
6228 l2cap_handle_srej(chan, control);
6229 break;
6230 default:
6231 break;
6232 }
6233
6234 if (skb && !skb_in_use) {
6235 BT_DBG("Freeing %p", skb);
6236 kfree_skb(skb);
6237 }
6238
6239 return err;
6240}
6241
6242static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
6243 struct l2cap_ctrl *control,
6244 struct sk_buff *skb, u8 event)
6245{
6246 int err = 0;
6247 u16 txseq = control->txseq;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006248 bool skb_in_use = false;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006249
6250 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6251 event);
6252
6253 switch (event) {
6254 case L2CAP_EV_RECV_IFRAME:
6255 switch (l2cap_classify_txseq(chan, txseq)) {
6256 case L2CAP_TXSEQ_EXPECTED:
6257 /* Keep frame for reassembly later */
6258 l2cap_pass_to_tx(chan, control);
6259 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006260 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006261 BT_DBG("Queued %p (queue len %d)", skb,
6262 skb_queue_len(&chan->srej_q));
6263
6264 chan->expected_tx_seq = __next_seq(chan, txseq);
6265 break;
6266 case L2CAP_TXSEQ_EXPECTED_SREJ:
6267 l2cap_seq_list_pop(&chan->srej_list);
6268
6269 l2cap_pass_to_tx(chan, control);
6270 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006271 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006272 BT_DBG("Queued %p (queue len %d)", skb,
6273 skb_queue_len(&chan->srej_q));
6274
6275 err = l2cap_rx_queued_iframes(chan);
6276 if (err)
6277 break;
6278
6279 break;
6280 case L2CAP_TXSEQ_UNEXPECTED:
6281 /* Got a frame that can't be reassembled yet.
6282 * Save it for later, and send SREJs to cover
6283 * the missing frames.
6284 */
6285 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006286 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006287 BT_DBG("Queued %p (queue len %d)", skb,
6288 skb_queue_len(&chan->srej_q));
6289
6290 l2cap_pass_to_tx(chan, control);
6291 l2cap_send_srej(chan, control->txseq);
6292 break;
6293 case L2CAP_TXSEQ_UNEXPECTED_SREJ:
6294 /* This frame was requested with an SREJ, but
6295 * some expected retransmitted frames are
6296 * missing. Request retransmission of missing
6297 * SREJ'd frames.
6298 */
6299 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006300 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006301 BT_DBG("Queued %p (queue len %d)", skb,
6302 skb_queue_len(&chan->srej_q));
6303
6304 l2cap_pass_to_tx(chan, control);
6305 l2cap_send_srej_list(chan, control->txseq);
6306 break;
6307 case L2CAP_TXSEQ_DUPLICATE_SREJ:
6308 /* We've already queued this frame. Drop this copy. */
6309 l2cap_pass_to_tx(chan, control);
6310 break;
6311 case L2CAP_TXSEQ_DUPLICATE:
6312 /* Expecting a later sequence number, so this frame
6313 * was already received. Ignore it completely.
6314 */
6315 break;
6316 case L2CAP_TXSEQ_INVALID_IGNORE:
6317 break;
6318 case L2CAP_TXSEQ_INVALID:
6319 default:
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006320 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006321 break;
6322 }
6323 break;
6324 case L2CAP_EV_RECV_RR:
6325 l2cap_pass_to_tx(chan, control);
6326 if (control->final) {
6327 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6328
6329 if (!test_and_clear_bit(CONN_REJ_ACT,
6330 &chan->conn_state)) {
6331 control->final = 0;
6332 l2cap_retransmit_all(chan, control);
6333 }
6334
6335 l2cap_ertm_send(chan);
6336 } else if (control->poll) {
6337 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6338 &chan->conn_state) &&
6339 chan->unacked_frames) {
6340 __set_retrans_timer(chan);
6341 }
6342
6343 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6344 l2cap_send_srej_tail(chan);
6345 } else {
6346 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6347 &chan->conn_state) &&
6348 chan->unacked_frames)
6349 __set_retrans_timer(chan);
6350
6351 l2cap_send_ack(chan);
6352 }
6353 break;
6354 case L2CAP_EV_RECV_RNR:
6355 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6356 l2cap_pass_to_tx(chan, control);
6357 if (control->poll) {
6358 l2cap_send_srej_tail(chan);
6359 } else {
6360 struct l2cap_ctrl rr_control;
6361 memset(&rr_control, 0, sizeof(rr_control));
6362 rr_control.sframe = 1;
6363 rr_control.super = L2CAP_SUPER_RR;
6364 rr_control.reqseq = chan->buffer_seq;
6365 l2cap_send_sframe(chan, &rr_control);
6366 }
6367
6368 break;
6369 case L2CAP_EV_RECV_REJ:
6370 l2cap_handle_rej(chan, control);
6371 break;
6372 case L2CAP_EV_RECV_SREJ:
6373 l2cap_handle_srej(chan, control);
6374 break;
6375 }
6376
6377 if (skb && !skb_in_use) {
6378 BT_DBG("Freeing %p", skb);
6379 kfree_skb(skb);
6380 }
6381
6382 return err;
6383}
6384
Mat Martineau32b32732012-10-23 15:24:11 -07006385static int l2cap_finish_move(struct l2cap_chan *chan)
6386{
6387 BT_DBG("chan %p", chan);
6388
6389 chan->rx_state = L2CAP_RX_STATE_RECV;
6390
6391 if (chan->hs_hcon)
6392 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
6393 else
6394 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
6395
6396 return l2cap_resegment(chan);
6397}
6398
6399static int l2cap_rx_state_wait_p(struct l2cap_chan *chan,
6400 struct l2cap_ctrl *control,
6401 struct sk_buff *skb, u8 event)
6402{
6403 int err;
6404
6405 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6406 event);
6407
6408 if (!control->poll)
6409 return -EPROTO;
6410
6411 l2cap_process_reqseq(chan, control->reqseq);
6412
6413 if (!skb_queue_empty(&chan->tx_q))
6414 chan->tx_send_head = skb_peek(&chan->tx_q);
6415 else
6416 chan->tx_send_head = NULL;
6417
6418 /* Rewind next_tx_seq to the point expected
6419 * by the receiver.
6420 */
6421 chan->next_tx_seq = control->reqseq;
6422 chan->unacked_frames = 0;
6423
6424 err = l2cap_finish_move(chan);
6425 if (err)
6426 return err;
6427
6428 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6429 l2cap_send_i_or_rr_or_rnr(chan);
6430
6431 if (event == L2CAP_EV_RECV_IFRAME)
6432 return -EPROTO;
6433
6434 return l2cap_rx_state_recv(chan, control, NULL, event);
6435}
6436
6437static int l2cap_rx_state_wait_f(struct l2cap_chan *chan,
6438 struct l2cap_ctrl *control,
6439 struct sk_buff *skb, u8 event)
6440{
6441 int err;
6442
6443 if (!control->final)
6444 return -EPROTO;
6445
6446 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6447
6448 chan->rx_state = L2CAP_RX_STATE_RECV;
6449 l2cap_process_reqseq(chan, control->reqseq);
6450
6451 if (!skb_queue_empty(&chan->tx_q))
6452 chan->tx_send_head = skb_peek(&chan->tx_q);
6453 else
6454 chan->tx_send_head = NULL;
6455
6456 /* Rewind next_tx_seq to the point expected
6457 * by the receiver.
6458 */
6459 chan->next_tx_seq = control->reqseq;
6460 chan->unacked_frames = 0;
6461
6462 if (chan->hs_hcon)
6463 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
6464 else
6465 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
6466
6467 err = l2cap_resegment(chan);
6468
6469 if (!err)
6470 err = l2cap_rx_state_recv(chan, control, skb, event);
6471
6472 return err;
6473}
6474
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006475static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq)
6476{
6477 /* Make sure reqseq is for a packet that has been sent but not acked */
6478 u16 unacked;
6479
6480 unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq);
6481 return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked;
6482}
6483
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006484static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6485 struct sk_buff *skb, u8 event)
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006486{
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006487 int err = 0;
6488
6489 BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan,
6490 control, skb, event, chan->rx_state);
6491
6492 if (__valid_reqseq(chan, control->reqseq)) {
6493 switch (chan->rx_state) {
6494 case L2CAP_RX_STATE_RECV:
6495 err = l2cap_rx_state_recv(chan, control, skb, event);
6496 break;
6497 case L2CAP_RX_STATE_SREJ_SENT:
6498 err = l2cap_rx_state_srej_sent(chan, control, skb,
6499 event);
6500 break;
Mat Martineau32b32732012-10-23 15:24:11 -07006501 case L2CAP_RX_STATE_WAIT_P:
6502 err = l2cap_rx_state_wait_p(chan, control, skb, event);
6503 break;
6504 case L2CAP_RX_STATE_WAIT_F:
6505 err = l2cap_rx_state_wait_f(chan, control, skb, event);
6506 break;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006507 default:
6508 /* shut it down */
6509 break;
6510 }
6511 } else {
6512 BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d",
6513 control->reqseq, chan->next_tx_seq,
6514 chan->expected_ack_seq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006515 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006516 }
6517
6518 return err;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006519}
6520
6521static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6522 struct sk_buff *skb)
6523{
Mat Martineau4b51dae92012-05-17 20:53:37 -07006524 int err = 0;
6525
6526 BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb,
6527 chan->rx_state);
6528
6529 if (l2cap_classify_txseq(chan, control->txseq) ==
6530 L2CAP_TXSEQ_EXPECTED) {
6531 l2cap_pass_to_tx(chan, control);
6532
6533 BT_DBG("buffer_seq %d->%d", chan->buffer_seq,
6534 __next_seq(chan, chan->buffer_seq));
6535
6536 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
6537
6538 l2cap_reassemble_sdu(chan, skb, control);
6539 } else {
6540 if (chan->sdu) {
6541 kfree_skb(chan->sdu);
6542 chan->sdu = NULL;
6543 }
6544 chan->sdu_last_frag = NULL;
6545 chan->sdu_len = 0;
6546
6547 if (skb) {
6548 BT_DBG("Freeing %p", skb);
6549 kfree_skb(skb);
6550 }
6551 }
6552
6553 chan->last_acked_seq = control->txseq;
6554 chan->expected_tx_seq = __next_seq(chan, control->txseq);
6555
6556 return err;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006557}
6558
6559static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
6560{
6561 struct l2cap_ctrl *control = &bt_cb(skb)->control;
6562 u16 len;
6563 u8 event;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006564
Mat Martineaub76bbd62012-04-11 10:48:43 -07006565 __unpack_control(chan, skb);
6566
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006567 len = skb->len;
6568
6569 /*
6570 * We can just drop the corrupted I-frame here.
6571 * Receiver will miss it and start proper recovery
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006572 * procedures and ask for retransmission.
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006573 */
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006574 if (l2cap_check_fcs(chan, skb))
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006575 goto drop;
6576
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006577 if (!control->sframe && control->sar == L2CAP_SAR_START)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03006578 len -= L2CAP_SDULEN_SIZE;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006579
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006580 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03006581 len -= L2CAP_FCS_SIZE;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006582
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006583 if (len > chan->mps) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006584 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006585 goto drop;
6586 }
6587
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006588 if (!control->sframe) {
6589 int err;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006590
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006591 BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d",
6592 control->sar, control->reqseq, control->final,
6593 control->txseq);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03006594
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006595 /* Validate F-bit - F=0 always valid, F=1 only
6596 * valid in TX WAIT_F
6597 */
6598 if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F)
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006599 goto drop;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006600
6601 if (chan->mode != L2CAP_MODE_STREAMING) {
6602 event = L2CAP_EV_RECV_IFRAME;
6603 err = l2cap_rx(chan, control, skb, event);
6604 } else {
6605 err = l2cap_stream_rx(chan, control, skb);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006606 }
6607
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006608 if (err)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006609 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006610 } else {
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006611 const u8 rx_func_to_event[4] = {
6612 L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ,
6613 L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ
6614 };
6615
6616 /* Only I-frames are expected in streaming mode */
6617 if (chan->mode == L2CAP_MODE_STREAMING)
6618 goto drop;
6619
6620 BT_DBG("sframe reqseq %d, final %d, poll %d, super %d",
6621 control->reqseq, control->final, control->poll,
6622 control->super);
6623
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006624 if (len != 0) {
Andrei Emeltchenko1bb166e2012-11-20 17:16:21 +02006625 BT_ERR("Trailing bytes: %d in sframe", len);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006626 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006627 goto drop;
6628 }
6629
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006630 /* Validate F and P bits */
6631 if (control->final && (control->poll ||
6632 chan->tx_state != L2CAP_TX_STATE_WAIT_F))
6633 goto drop;
6634
6635 event = rx_func_to_event[control->super];
6636 if (l2cap_rx(chan, control, skb, event))
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006637 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006638 }
6639
6640 return 0;
6641
6642drop:
6643 kfree_skb(skb);
6644 return 0;
6645}
6646
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006647static void l2cap_chan_le_send_credits(struct l2cap_chan *chan)
6648{
6649 struct l2cap_conn *conn = chan->conn;
6650 struct l2cap_le_credits pkt;
6651 u16 return_credits;
6652
6653 /* We return more credits to the sender only after the amount of
6654 * credits falls below half of the initial amount.
6655 */
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02006656 if (chan->rx_credits >= (le_max_credits + 1) / 2)
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006657 return;
6658
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02006659 return_credits = le_max_credits - chan->rx_credits;
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006660
6661 BT_DBG("chan %p returning %u credits to sender", chan, return_credits);
6662
6663 chan->rx_credits += return_credits;
6664
6665 pkt.cid = cpu_to_le16(chan->scid);
6666 pkt.credits = cpu_to_le16(return_credits);
6667
6668 chan->ident = l2cap_get_ident(conn);
6669
6670 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt);
6671}
6672
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006673static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
6674{
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006675 int err;
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006676
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006677 if (!chan->rx_credits) {
6678 BT_ERR("No credits to receive LE L2CAP data");
Johan Hedbergdfd97742014-01-27 15:11:34 -08006679 l2cap_send_disconn_req(chan, ECONNRESET);
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006680 return -ENOBUFS;
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006681 }
6682
6683 if (chan->imtu < skb->len) {
6684 BT_ERR("Too big LE L2CAP PDU");
6685 return -ENOBUFS;
6686 }
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006687
6688 chan->rx_credits--;
6689 BT_DBG("rx_credits %u -> %u", chan->rx_credits + 1, chan->rx_credits);
6690
6691 l2cap_chan_le_send_credits(chan);
6692
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006693 err = 0;
6694
6695 if (!chan->sdu) {
6696 u16 sdu_len;
6697
6698 sdu_len = get_unaligned_le16(skb->data);
6699 skb_pull(skb, L2CAP_SDULEN_SIZE);
6700
6701 BT_DBG("Start of new SDU. sdu_len %u skb->len %u imtu %u",
6702 sdu_len, skb->len, chan->imtu);
6703
6704 if (sdu_len > chan->imtu) {
6705 BT_ERR("Too big LE L2CAP SDU length received");
6706 err = -EMSGSIZE;
6707 goto failed;
6708 }
6709
6710 if (skb->len > sdu_len) {
6711 BT_ERR("Too much LE L2CAP data received");
6712 err = -EINVAL;
6713 goto failed;
6714 }
6715
6716 if (skb->len == sdu_len)
6717 return chan->ops->recv(chan, skb);
6718
6719 chan->sdu = skb;
6720 chan->sdu_len = sdu_len;
6721 chan->sdu_last_frag = skb;
6722
6723 return 0;
6724 }
6725
6726 BT_DBG("SDU fragment. chan->sdu->len %u skb->len %u chan->sdu_len %u",
6727 chan->sdu->len, skb->len, chan->sdu_len);
6728
6729 if (chan->sdu->len + skb->len > chan->sdu_len) {
6730 BT_ERR("Too much LE L2CAP data received");
6731 err = -EINVAL;
6732 goto failed;
6733 }
6734
6735 append_skb_frag(chan->sdu, skb, &chan->sdu_last_frag);
6736 skb = NULL;
6737
6738 if (chan->sdu->len == chan->sdu_len) {
6739 err = chan->ops->recv(chan, chan->sdu);
6740 if (!err) {
6741 chan->sdu = NULL;
6742 chan->sdu_last_frag = NULL;
6743 chan->sdu_len = 0;
6744 }
6745 }
6746
6747failed:
6748 if (err) {
6749 kfree_skb(skb);
6750 kfree_skb(chan->sdu);
6751 chan->sdu = NULL;
6752 chan->sdu_last_frag = NULL;
6753 chan->sdu_len = 0;
6754 }
6755
6756 /* We can't return an error here since we took care of the skb
6757 * freeing internally. An error return would cause the caller to
6758 * do a double-free of the skb.
6759 */
6760 return 0;
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006761}
6762
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006763static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid,
6764 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006766 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03006768 chan = l2cap_get_chan_by_scid(conn, cid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006769 if (!chan) {
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006770 if (cid == L2CAP_CID_A2MP) {
6771 chan = a2mp_channel_create(conn, skb);
6772 if (!chan) {
6773 kfree_skb(skb);
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006774 return;
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006775 }
6776
6777 l2cap_chan_lock(chan);
6778 } else {
6779 BT_DBG("unknown cid 0x%4.4x", cid);
6780 /* Drop packet and return */
6781 kfree_skb(skb);
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006782 return;
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 }
6785
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03006786 BT_DBG("chan %p, len %d", chan, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03006788 if (chan->state != BT_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789 goto drop;
6790
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006791 switch (chan->mode) {
Johan Hedberg38319712013-05-17 12:49:23 +03006792 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006793 if (l2cap_le_data_rcv(chan, skb) < 0)
6794 goto drop;
6795
6796 goto done;
6797
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006798 case L2CAP_MODE_BASIC:
6799 /* If socket recv buffers overflows we drop data here
6800 * which is *bad* because L2CAP has to be reliable.
6801 * But we don't have any other choice. L2CAP doesn't
6802 * provide flow control mechanism. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803
Szymon Janc2c96e032014-02-18 20:48:34 +01006804 if (chan->imtu < skb->len) {
6805 BT_ERR("Dropping L2CAP data: receive buffer overflow");
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006806 goto drop;
Szymon Janc2c96e032014-02-18 20:48:34 +01006807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808
Gustavo Padovan80b98022012-05-27 22:27:51 -03006809 if (!chan->ops->recv(chan, skb))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006810 goto done;
6811 break;
6812
6813 case L2CAP_MODE_ERTM:
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03006814 case L2CAP_MODE_STREAMING:
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006815 l2cap_data_rcv(chan, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03006816 goto done;
6817
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006818 default:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006819 BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006820 break;
6821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822
6823drop:
6824 kfree_skb(skb);
6825
6826done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006827 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828}
6829
Andrei Emeltchenko84104b22012-05-31 11:18:56 +03006830static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
6831 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832{
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006833 struct hci_conn *hcon = conn->hcon;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006834 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006836 if (hcon->type != ACL_LINK)
Johan Hedberga24cce12014-08-07 22:56:42 +03006837 goto free_skb;
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006838
Johan Hedbergbf20fd42013-05-14 13:23:13 +03006839 chan = l2cap_global_chan_by_psm(0, psm, &hcon->src, &hcon->dst,
6840 ACL_LINK);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006841 if (!chan)
Johan Hedberga24cce12014-08-07 22:56:42 +03006842 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843
Andrei Emeltchenko5b4ceda2012-02-24 16:35:32 +02006844 BT_DBG("chan %p, len %d", chan, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03006846 if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 goto drop;
6848
Vinicius Costa Gomese13e21d2011-06-17 22:46:27 -03006849 if (chan->imtu < skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850 goto drop;
6851
Marcel Holtmann2edf8702013-10-13 12:55:29 -07006852 /* Store remote BD_ADDR and PSM for msg_name */
Marcel Holtmann06ae3312013-10-18 03:43:00 -07006853 bacpy(&bt_cb(skb)->bdaddr, &hcon->dst);
Marcel Holtmann2edf8702013-10-13 12:55:29 -07006854 bt_cb(skb)->psm = psm;
6855
Johan Hedberga24cce12014-08-07 22:56:42 +03006856 if (!chan->ops->recv(chan, skb)) {
6857 l2cap_chan_put(chan);
Andrei Emeltchenko84104b22012-05-31 11:18:56 +03006858 return;
Johan Hedberga24cce12014-08-07 22:56:42 +03006859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860
6861drop:
Johan Hedberga24cce12014-08-07 22:56:42 +03006862 l2cap_chan_put(chan);
6863free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865}
6866
6867static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
6868{
6869 struct l2cap_hdr *lh = (void *) skb->data;
Johan Hedberg61a939c2014-01-17 20:45:11 +02006870 struct hci_conn *hcon = conn->hcon;
Al Viro8e036fc2007-07-29 00:16:36 -07006871 u16 cid, len;
6872 __le16 psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873
Johan Hedberg61a939c2014-01-17 20:45:11 +02006874 if (hcon->state != BT_CONNECTED) {
6875 BT_DBG("queueing pending rx skb");
6876 skb_queue_tail(&conn->pending_rx, skb);
6877 return;
6878 }
6879
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880 skb_pull(skb, L2CAP_HDR_SIZE);
6881 cid = __le16_to_cpu(lh->cid);
6882 len = __le16_to_cpu(lh->len);
6883
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006884 if (len != skb->len) {
6885 kfree_skb(skb);
6886 return;
6887 }
6888
Johan Hedberg9e1d7e12014-07-06 11:03:36 +03006889 /* Since we can't actively block incoming LE connections we must
6890 * at least ensure that we ignore incoming data from them.
6891 */
6892 if (hcon->type == LE_LINK &&
Johan Hedbergdcc36c12014-07-09 12:59:13 +03006893 hci_bdaddr_list_lookup(&hcon->hdev->blacklist, &hcon->dst,
Johan Hedberga250e042015-01-15 13:06:44 +02006894 bdaddr_dst_type(hcon))) {
Johan Hedberge4931502014-07-02 09:36:21 +03006895 kfree_skb(skb);
6896 return;
6897 }
6898
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 BT_DBG("len %d, cid 0x%4.4x", len, cid);
6900
6901 switch (cid) {
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -03006902 case L2CAP_CID_SIGNALING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 l2cap_sig_channel(conn, skb);
6904 break;
6905
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -03006906 case L2CAP_CID_CONN_LESS:
Andrei Emeltchenko097db762012-03-09 14:16:17 +02006907 psm = get_unaligned((__le16 *) skb->data);
Andrei Emeltchenko0181a702012-05-29 10:04:05 +03006908 skb_pull(skb, L2CAP_PSMLEN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 l2cap_conless_channel(conn, psm, skb);
6910 break;
6911
Marcel Holtmanna2877622013-10-02 23:46:54 -07006912 case L2CAP_CID_LE_SIGNALING:
6913 l2cap_le_sig_channel(conn, skb);
6914 break;
6915
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 default:
6917 l2cap_data_channel(conn, cid, skb);
6918 break;
6919 }
6920}
6921
Johan Hedberg61a939c2014-01-17 20:45:11 +02006922static void process_pending_rx(struct work_struct *work)
6923{
6924 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
6925 pending_rx_work);
6926 struct sk_buff *skb;
6927
6928 BT_DBG("");
6929
6930 while ((skb = skb_dequeue(&conn->pending_rx)))
6931 l2cap_recv_frame(conn, skb);
6932}
6933
Johan Hedberg162b49e2014-01-17 20:45:10 +02006934static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon)
6935{
6936 struct l2cap_conn *conn = hcon->l2cap_data;
6937 struct hci_chan *hchan;
6938
6939 if (conn)
6940 return conn;
6941
6942 hchan = hci_chan_create(hcon);
6943 if (!hchan)
6944 return NULL;
6945
Johan Hedberg27f70f32014-07-21 10:50:06 +03006946 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
Johan Hedberg162b49e2014-01-17 20:45:10 +02006947 if (!conn) {
6948 hci_chan_del(hchan);
6949 return NULL;
6950 }
6951
6952 kref_init(&conn->ref);
6953 hcon->l2cap_data = conn;
Johan Hedberg51bb84572014-08-15 21:06:57 +03006954 conn->hcon = hci_conn_get(hcon);
Johan Hedberg162b49e2014-01-17 20:45:10 +02006955 conn->hchan = hchan;
6956
6957 BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan);
6958
6959 switch (hcon->type) {
6960 case LE_LINK:
6961 if (hcon->hdev->le_mtu) {
6962 conn->mtu = hcon->hdev->le_mtu;
6963 break;
6964 }
6965 /* fall through */
6966 default:
6967 conn->mtu = hcon->hdev->acl_mtu;
6968 break;
6969 }
6970
6971 conn->feat_mask = 0;
6972
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02006973 conn->local_fixed_chan = L2CAP_FC_SIG_BREDR | L2CAP_FC_CONNLESS;
6974
6975 if (hcon->type == ACL_LINK &&
6976 test_bit(HCI_HS_ENABLED, &hcon->hdev->dev_flags))
6977 conn->local_fixed_chan |= L2CAP_FC_A2MP;
Johan Hedberg162b49e2014-01-17 20:45:10 +02006978
Marcel Holtmannf9be9e82014-12-06 00:35:45 +01006979 if (test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags) &&
6980 (bredr_sc_enabled(hcon->hdev) ||
Marcel Holtmann300acfde2014-12-31 14:43:16 -08006981 test_bit(HCI_FORCE_BREDR_SMP, &hcon->hdev->dbg_flags)))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03006982 conn->local_fixed_chan |= L2CAP_FC_SMP_BREDR;
6983
Marcel Holtmann5a54e7c2014-07-13 20:50:15 +02006984 mutex_init(&conn->ident_lock);
Johan Hedberg162b49e2014-01-17 20:45:10 +02006985 mutex_init(&conn->chan_lock);
6986
6987 INIT_LIST_HEAD(&conn->chan_l);
6988 INIT_LIST_HEAD(&conn->users);
6989
Johan Hedberg276d8072014-08-11 22:06:41 +03006990 INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout);
Johan Hedberg162b49e2014-01-17 20:45:10 +02006991
Johan Hedberg61a939c2014-01-17 20:45:11 +02006992 skb_queue_head_init(&conn->pending_rx);
6993 INIT_WORK(&conn->pending_rx_work, process_pending_rx);
Johan Hedbergf3d82d02014-09-05 22:19:50 +03006994 INIT_WORK(&conn->id_addr_update_work, l2cap_conn_update_id_addr);
Johan Hedberg61a939c2014-01-17 20:45:11 +02006995
Johan Hedberg162b49e2014-01-17 20:45:10 +02006996 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
6997
6998 return conn;
6999}
7000
7001static bool is_valid_psm(u16 psm, u8 dst_type) {
7002 if (!psm)
7003 return false;
7004
7005 if (bdaddr_type_is_le(dst_type))
7006 return (psm <= 0x00ff);
7007
7008 /* PSM must be odd and lsb of upper byte must be 0 */
7009 return ((psm & 0x0101) == 0x0001);
7010}
7011
7012int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
7013 bdaddr_t *dst, u8 dst_type)
7014{
7015 struct l2cap_conn *conn;
7016 struct hci_conn *hcon;
7017 struct hci_dev *hdev;
Johan Hedberg162b49e2014-01-17 20:45:10 +02007018 int err;
7019
7020 BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", &chan->src, dst,
7021 dst_type, __le16_to_cpu(psm));
7022
7023 hdev = hci_get_route(dst, &chan->src);
7024 if (!hdev)
7025 return -EHOSTUNREACH;
7026
7027 hci_dev_lock(hdev);
7028
Johan Hedberg162b49e2014-01-17 20:45:10 +02007029 if (!is_valid_psm(__le16_to_cpu(psm), dst_type) && !cid &&
7030 chan->chan_type != L2CAP_CHAN_RAW) {
7031 err = -EINVAL;
7032 goto done;
7033 }
7034
Johan Hedberg21626e62014-01-24 10:35:41 +02007035 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !psm) {
7036 err = -EINVAL;
7037 goto done;
7038 }
7039
7040 if (chan->chan_type == L2CAP_CHAN_FIXED && !cid) {
Johan Hedberg162b49e2014-01-17 20:45:10 +02007041 err = -EINVAL;
7042 goto done;
7043 }
7044
7045 switch (chan->mode) {
7046 case L2CAP_MODE_BASIC:
7047 break;
7048 case L2CAP_MODE_LE_FLOWCTL:
7049 l2cap_le_flowctl_init(chan);
7050 break;
7051 case L2CAP_MODE_ERTM:
7052 case L2CAP_MODE_STREAMING:
7053 if (!disable_ertm)
7054 break;
7055 /* fall through */
7056 default:
Johan Hedbergbeb19e42014-07-18 11:15:26 +03007057 err = -EOPNOTSUPP;
Johan Hedberg162b49e2014-01-17 20:45:10 +02007058 goto done;
7059 }
7060
7061 switch (chan->state) {
7062 case BT_CONNECT:
7063 case BT_CONNECT2:
7064 case BT_CONFIG:
7065 /* Already connecting */
7066 err = 0;
7067 goto done;
7068
7069 case BT_CONNECTED:
7070 /* Already connected */
7071 err = -EISCONN;
7072 goto done;
7073
7074 case BT_OPEN:
7075 case BT_BOUND:
7076 /* Can connect */
7077 break;
7078
7079 default:
7080 err = -EBADFD;
7081 goto done;
7082 }
7083
7084 /* Set destination address and psm */
7085 bacpy(&chan->dst, dst);
7086 chan->dst_type = dst_type;
7087
7088 chan->psm = psm;
7089 chan->dcid = cid;
7090
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007091 if (bdaddr_type_is_le(dst_type)) {
Johan Hedberge804d252014-07-16 11:42:28 +03007092 u8 role;
Johan Hedbergcdd62752014-07-07 15:02:28 +03007093
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007094 /* Convert from L2CAP channel address type to HCI address type
7095 */
7096 if (dst_type == BDADDR_LE_PUBLIC)
7097 dst_type = ADDR_LE_DEV_PUBLIC;
7098 else
7099 dst_type = ADDR_LE_DEV_RANDOM;
7100
Johan Hedberge804d252014-07-16 11:42:28 +03007101 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
7102 role = HCI_ROLE_SLAVE;
7103 else
7104 role = HCI_ROLE_MASTER;
Johan Hedbergcdd62752014-07-07 15:02:28 +03007105
Andre Guedes04a6c582014-02-26 20:21:44 -03007106 hcon = hci_connect_le(hdev, dst, dst_type, chan->sec_level,
Johan Hedberge804d252014-07-16 11:42:28 +03007107 HCI_LE_CONN_TIMEOUT, role);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007108 } else {
Johan Hedbergd93375a2014-07-07 15:02:27 +03007109 u8 auth_type = l2cap_get_auth_type(chan);
Andre Guedes04a6c582014-02-26 20:21:44 -03007110 hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007111 }
Johan Hedberg162b49e2014-01-17 20:45:10 +02007112
7113 if (IS_ERR(hcon)) {
7114 err = PTR_ERR(hcon);
7115 goto done;
7116 }
7117
7118 conn = l2cap_conn_add(hcon);
7119 if (!conn) {
7120 hci_conn_drop(hcon);
7121 err = -ENOMEM;
7122 goto done;
7123 }
7124
Johan Hedberg02e246a2014-10-02 10:16:22 +03007125 mutex_lock(&conn->chan_lock);
7126 l2cap_chan_lock(chan);
7127
Johan Hedberg162b49e2014-01-17 20:45:10 +02007128 if (cid && __l2cap_get_chan_by_dcid(conn, cid)) {
7129 hci_conn_drop(hcon);
7130 err = -EBUSY;
Johan Hedberg02e246a2014-10-02 10:16:22 +03007131 goto chan_unlock;
Johan Hedberg162b49e2014-01-17 20:45:10 +02007132 }
7133
7134 /* Update source addr of the socket */
7135 bacpy(&chan->src, &hcon->src);
Johan Hedberga250e042015-01-15 13:06:44 +02007136 chan->src_type = bdaddr_src_type(hcon);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007137
Johan Hedberg02e246a2014-10-02 10:16:22 +03007138 __l2cap_chan_add(conn, chan);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007139
7140 /* l2cap_chan_add takes its own ref so we can drop this one */
7141 hci_conn_drop(hcon);
7142
7143 l2cap_state_change(chan, BT_CONNECT);
7144 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
7145
Johan Hedberg61202e42014-01-28 15:16:48 -08007146 /* Release chan->sport so that it can be reused by other
7147 * sockets (as it's only used for listening sockets).
7148 */
7149 write_lock(&chan_list_lock);
7150 chan->sport = 0;
7151 write_unlock(&chan_list_lock);
7152
Johan Hedberg162b49e2014-01-17 20:45:10 +02007153 if (hcon->state == BT_CONNECTED) {
7154 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
7155 __clear_chan_timer(chan);
Johan Hedberge7cafc42014-07-17 15:35:38 +03007156 if (l2cap_chan_check_security(chan, true))
Johan Hedberg162b49e2014-01-17 20:45:10 +02007157 l2cap_state_change(chan, BT_CONNECTED);
7158 } else
7159 l2cap_do_start(chan);
7160 }
7161
7162 err = 0;
7163
Johan Hedberg02e246a2014-10-02 10:16:22 +03007164chan_unlock:
Johan Hedberg162b49e2014-01-17 20:45:10 +02007165 l2cap_chan_unlock(chan);
Johan Hedberg02e246a2014-10-02 10:16:22 +03007166 mutex_unlock(&conn->chan_lock);
7167done:
Johan Hedberg162b49e2014-01-17 20:45:10 +02007168 hci_dev_unlock(hdev);
7169 hci_dev_put(hdev);
7170 return err;
7171}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03007172EXPORT_SYMBOL_GPL(l2cap_chan_connect);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007173
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174/* ---- L2CAP interface with lower layer (HCI) ---- */
7175
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007176int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177{
7178 int exact = 0, lm1 = 0, lm2 = 0;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007179 struct l2cap_chan *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03007181 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182
7183 /* Find listening sockets and check their link_mode */
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007184 read_lock(&chan_list_lock);
7185 list_for_each_entry(c, &chan_list, global_l) {
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03007186 if (c->state != BT_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187 continue;
7188
Marcel Holtmann7eafc592013-10-13 08:12:47 -07007189 if (!bacmp(&c->src, &hdev->bdaddr)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007190 lm1 |= HCI_LM_ACCEPT;
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +03007191 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007192 lm1 |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 exact++;
Marcel Holtmann7eafc592013-10-13 08:12:47 -07007194 } else if (!bacmp(&c->src, BDADDR_ANY)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007195 lm2 |= HCI_LM_ACCEPT;
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +03007196 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007197 lm2 |= HCI_LM_MASTER;
7198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 }
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007200 read_unlock(&chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201
7202 return exact ? lm1 : lm2;
7203}
7204
Johan Hedberge760ec12014-08-07 22:56:47 +03007205/* Find the next fixed channel in BT_LISTEN state, continue iteration
7206 * from an existing channel in the list or from the beginning of the
7207 * global list (by passing NULL as first parameter).
7208 */
7209static struct l2cap_chan *l2cap_global_fixed_chan(struct l2cap_chan *c,
Johan Hedberg327a7192015-01-15 13:06:45 +02007210 struct hci_conn *hcon)
Johan Hedberge760ec12014-08-07 22:56:47 +03007211{
Johan Hedberg327a7192015-01-15 13:06:45 +02007212 u8 src_type = bdaddr_src_type(hcon);
7213
Johan Hedberge760ec12014-08-07 22:56:47 +03007214 read_lock(&chan_list_lock);
7215
7216 if (c)
7217 c = list_next_entry(c, global_l);
7218 else
7219 c = list_entry(chan_list.next, typeof(*c), global_l);
7220
7221 list_for_each_entry_from(c, &chan_list, global_l) {
7222 if (c->chan_type != L2CAP_CHAN_FIXED)
7223 continue;
7224 if (c->state != BT_LISTEN)
7225 continue;
Johan Hedberg327a7192015-01-15 13:06:45 +02007226 if (bacmp(&c->src, &hcon->src) && bacmp(&c->src, BDADDR_ANY))
Johan Hedberge760ec12014-08-07 22:56:47 +03007227 continue;
Johan Hedberg327a7192015-01-15 13:06:45 +02007228 if (src_type != c->src_type)
Johan Hedberg54a1b622014-08-07 22:56:48 +03007229 continue;
Johan Hedberge760ec12014-08-07 22:56:47 +03007230
7231 l2cap_chan_hold(c);
7232 read_unlock(&chan_list_lock);
7233 return c;
7234 }
7235
7236 read_unlock(&chan_list_lock);
7237
7238 return NULL;
7239}
7240
Andrei Emeltchenko9e664632012-07-24 16:06:15 +03007241void l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242{
Johan Hedberge760ec12014-08-07 22:56:47 +03007243 struct hci_dev *hdev = hcon->hdev;
Marcel Holtmann01394182006-07-03 10:02:46 +02007244 struct l2cap_conn *conn;
Johan Hedberge760ec12014-08-07 22:56:47 +03007245 struct l2cap_chan *pchan;
7246 u8 dst_type;
Marcel Holtmann01394182006-07-03 10:02:46 +02007247
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03007248 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007250 if (status) {
Joe Perchese1750722011-06-29 18:18:29 -07007251 l2cap_conn_del(hcon, bt_to_errno(status));
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007252 return;
Andrei Emeltchenkoba6fc312012-10-31 15:46:26 +02007253 }
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007254
7255 conn = l2cap_conn_add(hcon);
7256 if (!conn)
7257 return;
7258
Johan Hedberga250e042015-01-15 13:06:44 +02007259 dst_type = bdaddr_dst_type(hcon);
Johan Hedberge760ec12014-08-07 22:56:47 +03007260
7261 /* If device is blocked, do not create channels for it */
7262 if (hci_bdaddr_list_lookup(&hdev->blacklist, &hcon->dst, dst_type))
7263 return;
7264
7265 /* Find fixed channels and notify them of the new connection. We
7266 * use multiple individual lookups, continuing each time where
7267 * we left off, because the list lock would prevent calling the
7268 * potentially sleeping l2cap_chan_lock() function.
7269 */
Johan Hedberg327a7192015-01-15 13:06:45 +02007270 pchan = l2cap_global_fixed_chan(NULL, hcon);
Johan Hedberge760ec12014-08-07 22:56:47 +03007271 while (pchan) {
7272 struct l2cap_chan *chan, *next;
7273
7274 /* Client fixed channels should override server ones */
7275 if (__l2cap_get_chan_by_dcid(conn, pchan->scid))
7276 goto next;
7277
7278 l2cap_chan_lock(pchan);
7279 chan = pchan->ops->new_connection(pchan);
7280 if (chan) {
7281 bacpy(&chan->src, &hcon->src);
7282 bacpy(&chan->dst, &hcon->dst);
Johan Hedberga250e042015-01-15 13:06:44 +02007283 chan->src_type = bdaddr_src_type(hcon);
Johan Hedberge760ec12014-08-07 22:56:47 +03007284 chan->dst_type = dst_type;
7285
7286 __l2cap_chan_add(conn, chan);
7287 }
7288
7289 l2cap_chan_unlock(pchan);
7290next:
Johan Hedberg327a7192015-01-15 13:06:45 +02007291 next = l2cap_global_fixed_chan(pchan, hcon);
Johan Hedberge760ec12014-08-07 22:56:47 +03007292 l2cap_chan_put(pchan);
7293 pchan = next;
7294 }
7295
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007296 l2cap_conn_ready(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297}
7298
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007299int l2cap_disconn_ind(struct hci_conn *hcon)
Marcel Holtmann2950f212009-02-12 14:02:50 +01007300{
7301 struct l2cap_conn *conn = hcon->l2cap_data;
7302
7303 BT_DBG("hcon %p", hcon);
7304
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007305 if (!conn)
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02007306 return HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +01007307 return conn->disc_reason;
7308}
7309
Andrei Emeltchenko9e664632012-07-24 16:06:15 +03007310void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311{
7312 BT_DBG("hcon %p reason %d", hcon, reason);
7313
Joe Perchese1750722011-06-29 18:18:29 -07007314 l2cap_conn_del(hcon, bt_to_errno(reason));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007315}
7316
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007317static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007318{
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03007319 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
Marcel Holtmann255c7602009-02-04 21:07:19 +01007320 return;
7321
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007322 if (encrypt == 0x00) {
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007323 if (chan->sec_level == BT_SECURITY_MEDIUM) {
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007324 __set_chan_timer(chan, L2CAP_ENC_TIMEOUT);
Marcel Holtmann7d513e92014-01-15 22:37:40 -08007325 } else if (chan->sec_level == BT_SECURITY_HIGH ||
7326 chan->sec_level == BT_SECURITY_FIPS)
Gustavo F. Padovan0f852722011-05-04 19:42:50 -03007327 l2cap_chan_close(chan, ECONNREFUSED);
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007328 } else {
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007329 if (chan->sec_level == BT_SECURITY_MEDIUM)
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -03007330 __clear_chan_timer(chan);
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007331 }
7332}
7333
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007334int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335{
Marcel Holtmann40be4922008-07-14 20:13:50 +02007336 struct l2cap_conn *conn = hcon->l2cap_data;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03007337 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338
Marcel Holtmann01394182006-07-03 10:02:46 +02007339 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 return 0;
Marcel Holtmann01394182006-07-03 10:02:46 +02007341
Andrei Emeltchenko89d8b402012-07-10 15:27:51 +03007342 BT_DBG("conn %p status 0x%2.2x encrypt %u", conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007344 mutex_lock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007346 list_for_each_entry(chan, &conn->chan_l, list) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007347 l2cap_chan_lock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348
Andrei Emeltchenko89d8b402012-07-10 15:27:51 +03007349 BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid,
7350 state_to_string(chan->state));
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007351
Johan Hedberg2338a7e2014-01-24 10:35:40 +02007352 if (chan->scid == L2CAP_CID_A2MP) {
Andrei Emeltchenko78eb2f92012-07-19 17:03:47 +03007353 l2cap_chan_unlock(chan);
7354 continue;
7355 }
7356
Johan Hedberg191eb392014-08-07 22:56:45 +03007357 if (!status && encrypt)
7358 chan->sec_level = hcon->sec_level;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007359
Andrei Emeltchenko96eff462012-11-15 18:14:53 +02007360 if (!__l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007361 l2cap_chan_unlock(chan);
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01007362 continue;
7363 }
7364
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03007365 if (!status && (chan->state == BT_CONNECTED ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01007366 chan->state == BT_CONFIG)) {
Marcel Holtmannd97c8992013-10-14 02:53:54 -07007367 chan->ops->resume(chan);
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007368 l2cap_check_encryption(chan, encrypt);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007369 l2cap_chan_unlock(chan);
Marcel Holtmann9719f8a2008-07-14 20:13:45 +02007370 continue;
7371 }
7372
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03007373 if (chan->state == BT_CONNECT) {
Johan Hedberg6d3c15d2013-12-02 22:13:24 +02007374 if (!status)
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03007375 l2cap_start_connection(chan);
Johan Hedberg6d3c15d2013-12-02 22:13:24 +02007376 else
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007377 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
Johan Hedbergfa37c1a2014-11-13 10:55:17 +02007378 } else if (chan->state == BT_CONNECT2 &&
7379 chan->mode != L2CAP_MODE_LE_FLOWCTL) {
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007380 struct l2cap_conn_rsp rsp;
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007381 __u16 res, stat;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007382
7383 if (!status) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07007384 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007385 res = L2CAP_CR_PEND;
7386 stat = L2CAP_CS_AUTHOR_PEND;
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08007387 chan->ops->defer(chan);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007388 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02007389 l2cap_state_change(chan, BT_CONFIG);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007390 res = L2CAP_CR_SUCCESS;
7391 stat = L2CAP_CS_NO_INFO;
7392 }
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007393 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02007394 l2cap_state_change(chan, BT_DISCONN);
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007395 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007396 res = L2CAP_CR_SEC_BLOCK;
7397 stat = L2CAP_CS_NO_INFO;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007398 }
7399
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03007400 rsp.scid = cpu_to_le16(chan->dcid);
7401 rsp.dcid = cpu_to_le16(chan->scid);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007402 rsp.result = cpu_to_le16(res);
7403 rsp.status = cpu_to_le16(stat);
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03007404 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01007405 sizeof(rsp), &rsp);
Mat Martineau2d369352012-05-23 14:59:30 -07007406
7407 if (!test_bit(CONF_REQ_SENT, &chan->conf_state) &&
7408 res == L2CAP_CR_SUCCESS) {
7409 char buf[128];
7410 set_bit(CONF_REQ_SENT, &chan->conf_state);
7411 l2cap_send_cmd(conn, l2cap_get_ident(conn),
7412 L2CAP_CONF_REQ,
7413 l2cap_build_conf_req(chan, buf),
7414 buf);
7415 chan->num_conf_req++;
7416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417 }
7418
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007419 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420 }
7421
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007422 mutex_unlock(&conn->chan_lock);
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007423
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424 return 0;
7425}
7426
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007427int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428{
7429 struct l2cap_conn *conn = hcon->l2cap_data;
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007430 struct l2cap_hdr *hdr;
7431 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432
Andrei Emeltchenko1d13a252012-10-15 11:58:41 +03007433 /* For AMP controller do not create l2cap conn */
7434 if (!conn && hcon->hdev->dev_type != HCI_BREDR)
7435 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02007437 if (!conn)
Claudio Takahasibaf43252013-04-11 13:55:50 -03007438 conn = l2cap_conn_add(hcon);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02007439
7440 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 goto drop;
7442
7443 BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
7444
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007445 switch (flags) {
7446 case ACL_START:
7447 case ACL_START_NO_FLUSH:
7448 case ACL_COMPLETE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449 if (conn->rx_len) {
7450 BT_ERR("Unexpected start frame (len %d)", skb->len);
7451 kfree_skb(conn->rx_skb);
7452 conn->rx_skb = NULL;
7453 conn->rx_len = 0;
7454 l2cap_conn_unreliable(conn, ECOMM);
7455 }
7456
Andrei Emeltchenkoaae7fe22010-09-15 14:28:43 +03007457 /* Start fragment always begin with Basic L2CAP header */
7458 if (skb->len < L2CAP_HDR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 BT_ERR("Frame is too short (len %d)", skb->len);
7460 l2cap_conn_unreliable(conn, ECOMM);
7461 goto drop;
7462 }
7463
7464 hdr = (struct l2cap_hdr *) skb->data;
7465 len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
7466
7467 if (len == skb->len) {
7468 /* Complete frame received */
7469 l2cap_recv_frame(conn, skb);
7470 return 0;
7471 }
7472
7473 BT_DBG("Start: total len %d, frag len %d", len, skb->len);
7474
7475 if (skb->len > len) {
7476 BT_ERR("Frame is too long (len %d, expected len %d)",
Gustavo Padovan2d792812012-10-06 10:07:01 +01007477 skb->len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 l2cap_conn_unreliable(conn, ECOMM);
7479 goto drop;
7480 }
7481
7482 /* Allocate skb for the complete frame (with header) */
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03007483 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03007484 if (!conn->rx_skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485 goto drop;
7486
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03007487 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Gustavo Padovan2d792812012-10-06 10:07:01 +01007488 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 conn->rx_len = len - skb->len;
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007490 break;
7491
7492 case ACL_CONT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
7494
7495 if (!conn->rx_len) {
7496 BT_ERR("Unexpected continuation frame (len %d)", skb->len);
7497 l2cap_conn_unreliable(conn, ECOMM);
7498 goto drop;
7499 }
7500
7501 if (skb->len > conn->rx_len) {
7502 BT_ERR("Fragment is too long (len %d, expected %d)",
Gustavo Padovan2d792812012-10-06 10:07:01 +01007503 skb->len, conn->rx_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 kfree_skb(conn->rx_skb);
7505 conn->rx_skb = NULL;
7506 conn->rx_len = 0;
7507 l2cap_conn_unreliable(conn, ECOMM);
7508 goto drop;
7509 }
7510
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03007511 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Gustavo Padovan2d792812012-10-06 10:07:01 +01007512 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 conn->rx_len -= skb->len;
7514
7515 if (!conn->rx_len) {
Johan Hedbergc4e5baf2013-10-10 13:33:37 +02007516 /* Complete frame received. l2cap_recv_frame
7517 * takes ownership of the skb so set the global
7518 * rx_skb pointer to NULL first.
7519 */
7520 struct sk_buff *rx_skb = conn->rx_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521 conn->rx_skb = NULL;
Johan Hedbergc4e5baf2013-10-10 13:33:37 +02007522 l2cap_recv_frame(conn, rx_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007523 }
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 }
7526
7527drop:
7528 kfree_skb(skb);
7529 return 0;
7530}
7531
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007532static int l2cap_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007534 struct l2cap_chan *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535
Gustavo F. Padovan333055f2011-12-22 15:14:39 -02007536 read_lock(&chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007538 list_for_each_entry(c, &chan_list, global_l) {
Marcel Holtmanneeb5a062015-01-14 13:44:21 -08007539 seq_printf(f, "%pMR (%u) %pMR (%u) %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
7540 &c->src, c->src_type, &c->dst, c->dst_type,
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03007541 c->state, __le16_to_cpu(c->psm),
7542 c->scid, c->dcid, c->imtu, c->omtu,
7543 c->sec_level, c->mode);
Andrei Emeltchenko61e1b4b2012-01-19 11:19:50 +02007544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545
Gustavo F. Padovan333055f2011-12-22 15:14:39 -02007546 read_unlock(&chan_list_lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08007547
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549}
7550
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007551static int l2cap_debugfs_open(struct inode *inode, struct file *file)
7552{
7553 return single_open(file, l2cap_debugfs_show, inode->i_private);
7554}
7555
7556static const struct file_operations l2cap_debugfs_fops = {
7557 .open = l2cap_debugfs_open,
7558 .read = seq_read,
7559 .llseek = seq_lseek,
7560 .release = single_release,
7561};
7562
7563static struct dentry *l2cap_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564
Gustavo F. Padovan64274512011-02-07 20:08:52 -02007565int __init l2cap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566{
7567 int err;
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08007568
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02007569 err = l2cap_init_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 if (err < 0)
7571 return err;
7572
Marcel Holtmann1120e4b2013-10-17 17:24:16 -07007573 if (IS_ERR_OR_NULL(bt_debugfs))
7574 return 0;
7575
7576 l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs,
7577 NULL, &l2cap_debugfs_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578
Samuel Ortiz40b93972014-05-14 17:53:35 +02007579 debugfs_create_u16("l2cap_le_max_credits", 0644, bt_debugfs,
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02007580 &le_max_credits);
Samuel Ortiz40b93972014-05-14 17:53:35 +02007581 debugfs_create_u16("l2cap_le_default_mps", 0644, bt_debugfs,
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02007582 &le_default_mps);
7583
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585}
7586
Gustavo F. Padovan64274512011-02-07 20:08:52 -02007587void l2cap_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007589 debugfs_remove(l2cap_debugfs);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02007590 l2cap_cleanup_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591}
7592
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03007593module_param(disable_ertm, bool, 0644);
7594MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");