blob: 54ceb1f2cc9a2a6026dec5bda6d9006191dcf053 [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. Padovan710f9b02011-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 {
Johan Hedberg92594a52016-01-26 17:19:10 -0500200 u16 p, start, end, incr;
201
202 if (chan->src_type == BDADDR_BREDR) {
203 start = L2CAP_PSM_DYN_START;
204 end = L2CAP_PSM_AUTO_END;
205 incr = 2;
206 } else {
207 start = L2CAP_PSM_LE_DYN_START;
208 end = L2CAP_PSM_LE_DYN_END;
209 incr = 1;
210 }
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300211
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300212 err = -EINVAL;
Johan Hedberg92594a52016-01-26 17:19:10 -0500213 for (p = start; p <= end; p += incr)
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300214 if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300215 chan->psm = cpu_to_le16(p);
216 chan->sport = cpu_to_le16(p);
217 err = 0;
218 break;
219 }
220 }
221
222done:
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200223 write_unlock(&chan_list_lock);
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300224 return err;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300225}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300226EXPORT_SYMBOL_GPL(l2cap_add_psm);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300227
228int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid)
229{
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200230 write_lock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300231
Johan Hedberg14824302014-08-07 22:56:50 +0300232 /* Override the defaults (which are for conn-oriented) */
233 chan->omtu = L2CAP_DEFAULT_MTU;
234 chan->chan_type = L2CAP_CHAN_FIXED;
235
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300236 chan->scid = scid;
237
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200238 write_unlock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300239
240 return 0;
241}
242
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300243static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
Marcel Holtmann01394182006-07-03 10:02:46 +0200244{
Johan Hedberge77af752013-10-08 10:31:00 +0200245 u16 cid, dyn_end;
Marcel Holtmann01394182006-07-03 10:02:46 +0200246
Johan Hedberge77af752013-10-08 10:31:00 +0200247 if (conn->hcon->type == LE_LINK)
248 dyn_end = L2CAP_CID_LE_DYN_END;
249 else
250 dyn_end = L2CAP_CID_DYN_END;
251
Johan Hedbergab0c1272015-11-02 14:39:16 +0200252 for (cid = L2CAP_CID_DYN_START; cid <= dyn_end; cid++) {
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300253 if (!__l2cap_get_chan_by_scid(conn, cid))
Marcel Holtmann01394182006-07-03 10:02:46 +0200254 return cid;
255 }
256
257 return 0;
258}
259
Gustavo Padovanf93fa272013-10-21 14:21:40 -0200260static void l2cap_state_change(struct l2cap_chan *chan, int state)
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300261{
Andrei Emeltchenko42d2d872012-02-17 11:40:57 +0200262 BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state),
Gustavo Padovan2d792812012-10-06 10:07:01 +0100263 state_to_string(state));
Gustavo F. Padovanbadaaa02011-11-23 20:11:46 -0200264
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300265 chan->state = state;
Gustavo Padovan53f52122013-10-15 19:24:45 -0300266 chan->ops->state_change(chan, state, 0);
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300267}
268
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300269static inline void l2cap_state_change_and_error(struct l2cap_chan *chan,
270 int state, int err)
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200271{
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300272 chan->state = state;
Gustavo Padovan53f52122013-10-15 19:24:45 -0300273 chan->ops->state_change(chan, chan->state, err);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200274}
275
276static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err)
277{
Gustavo Padovanf8e73012013-10-15 19:24:46 -0300278 chan->ops->state_change(chan, chan->state, err);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200279}
280
Mat Martineau4239d162012-05-17 20:53:49 -0700281static void __set_retrans_timer(struct l2cap_chan *chan)
282{
283 if (!delayed_work_pending(&chan->monitor_timer) &&
284 chan->retrans_timeout) {
285 l2cap_set_timer(chan, &chan->retrans_timer,
286 msecs_to_jiffies(chan->retrans_timeout));
287 }
288}
289
290static void __set_monitor_timer(struct l2cap_chan *chan)
291{
292 __clear_retrans_timer(chan);
293 if (chan->monitor_timeout) {
294 l2cap_set_timer(chan, &chan->monitor_timer,
295 msecs_to_jiffies(chan->monitor_timeout));
296 }
297}
298
Mat Martineau608bcc62012-05-17 20:53:32 -0700299static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head,
300 u16 seq)
301{
302 struct sk_buff *skb;
303
304 skb_queue_walk(head, skb) {
Johan Hedberga4368ff2015-03-30 23:21:01 +0300305 if (bt_cb(skb)->l2cap.txseq == seq)
Mat Martineau608bcc62012-05-17 20:53:32 -0700306 return skb;
307 }
308
309 return NULL;
310}
311
Mat Martineau3c588192012-04-11 10:48:42 -0700312/* ---- L2CAP sequence number lists ---- */
313
314/* For ERTM, ordered lists of sequence numbers must be tracked for
315 * SREJ requests that are received and for frames that are to be
316 * retransmitted. These seq_list functions implement a singly-linked
317 * list in an array, where membership in the list can also be checked
318 * in constant time. Items can also be added to the tail of the list
319 * and removed from the head in constant time, without further memory
320 * allocs or frees.
321 */
322
323static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size)
324{
325 size_t alloc_size, i;
326
327 /* Allocated size is a power of 2 to map sequence numbers
328 * (which may be up to 14 bits) in to a smaller array that is
329 * sized for the negotiated ERTM transmit windows.
330 */
331 alloc_size = roundup_pow_of_two(size);
332
333 seq_list->list = kmalloc(sizeof(u16) * alloc_size, GFP_KERNEL);
334 if (!seq_list->list)
335 return -ENOMEM;
336
337 seq_list->mask = alloc_size - 1;
338 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
339 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
340 for (i = 0; i < alloc_size; i++)
341 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
342
343 return 0;
344}
345
346static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list)
347{
348 kfree(seq_list->list);
349}
350
351static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list,
352 u16 seq)
353{
354 /* Constant-time check for list membership */
355 return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR;
356}
357
Mat Martineau3c588192012-04-11 10:48:42 -0700358static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list)
359{
Johan Hedberg03a0c5d2014-01-18 21:32:59 +0200360 u16 seq = seq_list->head;
361 u16 mask = seq_list->mask;
362
363 seq_list->head = seq_list->list[seq & mask];
364 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR;
365
366 if (seq_list->head == L2CAP_SEQ_LIST_TAIL) {
367 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
368 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
369 }
370
371 return seq;
Mat Martineau3c588192012-04-11 10:48:42 -0700372}
373
374static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list)
375{
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300376 u16 i;
Mat Martineau3c588192012-04-11 10:48:42 -0700377
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300378 if (seq_list->head == L2CAP_SEQ_LIST_CLEAR)
379 return;
380
381 for (i = 0; i <= seq_list->mask; i++)
382 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
383
384 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
385 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
Mat Martineau3c588192012-04-11 10:48:42 -0700386}
387
388static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq)
389{
390 u16 mask = seq_list->mask;
391
392 /* All appends happen in constant time */
393
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300394 if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR)
395 return;
Mat Martineau3c588192012-04-11 10:48:42 -0700396
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300397 if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR)
398 seq_list->head = seq;
399 else
400 seq_list->list[seq_list->tail & mask] = seq;
401
402 seq_list->tail = seq;
403 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL;
Mat Martineau3c588192012-04-11 10:48:42 -0700404}
405
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300406static void l2cap_chan_timeout(struct work_struct *work)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300407{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300408 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100409 chan_timer.work);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200410 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300411 int reason;
412
Andrei Emeltchenkoe05dcc32012-02-17 11:40:56 +0200413 BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300414
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200415 mutex_lock(&conn->chan_lock);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200416 l2cap_chan_lock(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300417
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300418 if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300419 reason = ECONNREFUSED;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300420 else if (chan->state == BT_CONNECT &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100421 chan->sec_level != BT_SECURITY_SDP)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300422 reason = ECONNREFUSED;
423 else
424 reason = ETIMEDOUT;
425
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300426 l2cap_chan_close(chan, reason);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300427
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200428 l2cap_chan_unlock(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300429
Gustavo Padovan80b98022012-05-27 22:27:51 -0300430 chan->ops->close(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200431 mutex_unlock(&conn->chan_lock);
432
Ulisses Furquim371fd832011-12-21 20:02:36 -0200433 l2cap_chan_put(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300434}
435
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -0300436struct l2cap_chan *l2cap_chan_create(void)
Marcel Holtmann01394182006-07-03 10:02:46 +0200437{
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300438 struct l2cap_chan *chan;
Marcel Holtmann01394182006-07-03 10:02:46 +0200439
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300440 chan = kzalloc(sizeof(*chan), GFP_ATOMIC);
441 if (!chan)
442 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200443
Andrei Emeltchenkoc03b3552012-02-21 12:54:56 +0200444 mutex_init(&chan->lock);
445
Johan Hedbergff714112014-11-13 09:46:04 +0200446 /* Set default lock nesting level */
447 atomic_set(&chan->nesting, L2CAP_NESTING_NORMAL);
448
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200449 write_lock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300450 list_add(&chan->global_l, &chan_list);
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200451 write_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300452
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300453 INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300454
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300455 chan->state = BT_OPEN;
456
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530457 kref_init(&chan->kref);
Gustavo F. Padovan71ba0e52011-05-17 14:34:52 -0300458
Mat Martineau28270112012-05-17 21:14:09 -0700459 /* This flag is cleared in l2cap_chan_ready() */
460 set_bit(CONF_NOT_COMPLETE, &chan->conf_state);
461
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -0300462 BT_DBG("chan %p", chan);
Szymon Jancabc545b2011-11-03 16:05:44 +0100463
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300464 return chan;
Marcel Holtmann01394182006-07-03 10:02:46 +0200465}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300466EXPORT_SYMBOL_GPL(l2cap_chan_create);
Marcel Holtmann01394182006-07-03 10:02:46 +0200467
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530468static void l2cap_chan_destroy(struct kref *kref)
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300469{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530470 struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref);
471
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530472 BT_DBG("chan %p", chan);
473
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200474 write_lock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300475 list_del(&chan->global_l);
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200476 write_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300477
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530478 kfree(chan);
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300479}
480
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530481void l2cap_chan_hold(struct l2cap_chan *c)
482{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530483 BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount));
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530484
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530485 kref_get(&c->kref);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530486}
487
488void l2cap_chan_put(struct l2cap_chan *c)
489{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530490 BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount));
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530491
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530492 kref_put(&c->kref, l2cap_chan_destroy);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530493}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300494EXPORT_SYMBOL_GPL(l2cap_chan_put);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530495
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300496void l2cap_chan_set_defaults(struct l2cap_chan *chan)
497{
498 chan->fcs = L2CAP_FCS_CRC16;
499 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
500 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
501 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
Jukka Rissanen6a5e8162014-05-28 14:43:04 +0300502 chan->remote_max_tx = chan->max_tx;
503 chan->remote_tx_win = chan->tx_win;
Mat Martineauc20f8e32012-07-10 05:47:07 -0700504 chan->ack_win = L2CAP_DEFAULT_TX_WINDOW;
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300505 chan->sec_level = BT_SECURITY_LOW;
Jukka Rissanen6a5e8162014-05-28 14:43:04 +0300506 chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
507 chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
508 chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
509 chan->conf_state = 0;
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300510
511 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
512}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300513EXPORT_SYMBOL_GPL(l2cap_chan_set_defaults);
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300514
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200515static void l2cap_le_flowctl_init(struct l2cap_chan *chan)
Johan Hedberg38319712013-05-17 12:49:23 +0300516{
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200517 chan->sdu = NULL;
518 chan->sdu_last_frag = NULL;
519 chan->sdu_len = 0;
Johan Hedberg0cd75f72013-05-17 13:09:05 +0300520 chan->tx_credits = 0;
Johan Hedbergf15b8ec2013-12-03 15:08:25 +0200521 chan->rx_credits = le_max_credits;
Johan Hedbergd1d79412014-01-27 15:11:33 -0800522 chan->mps = min_t(u16, chan->imtu, le_default_mps);
Johan Hedberg0ce43ce2013-12-05 14:55:33 +0200523
524 skb_queue_head_init(&chan->tx_q);
Johan Hedberg38319712013-05-17 12:49:23 +0300525}
526
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +0300527void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
Marcel Holtmann01394182006-07-03 10:02:46 +0200528{
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -0300529 BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
Andrei Emeltchenko097db762012-03-09 14:16:17 +0200530 __le16_to_cpu(chan->psm), chan->dcid);
Marcel Holtmann01394182006-07-03 10:02:46 +0200531
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +0200532 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100533
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300534 chan->conn = conn;
Marcel Holtmann01394182006-07-03 10:02:46 +0200535
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200536 switch (chan->chan_type) {
537 case L2CAP_CHAN_CONN_ORIENTED:
Johan Hedberg21626e62014-01-24 10:35:41 +0200538 /* Alloc CID for connection-oriented socket */
539 chan->scid = l2cap_alloc_cid(conn);
540 if (conn->hcon->type == ACL_LINK)
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300541 chan->omtu = L2CAP_DEFAULT_MTU;
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200542 break;
543
544 case L2CAP_CHAN_CONN_LESS:
Marcel Holtmann01394182006-07-03 10:02:46 +0200545 /* Connectionless socket */
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300546 chan->scid = L2CAP_CID_CONN_LESS;
547 chan->dcid = L2CAP_CID_CONN_LESS;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300548 chan->omtu = L2CAP_DEFAULT_MTU;
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200549 break;
550
Johan Hedberg2338a7e2014-01-24 10:35:40 +0200551 case L2CAP_CHAN_FIXED:
552 /* Caller will set CID and CID specific MTU values */
Andrei Emeltchenko416fa752012-05-29 13:59:16 +0300553 break;
554
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200555 default:
Marcel Holtmann01394182006-07-03 10:02:46 +0200556 /* Raw socket can send/recv signalling messages only */
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300557 chan->scid = L2CAP_CID_SIGNALING;
558 chan->dcid = L2CAP_CID_SIGNALING;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300559 chan->omtu = L2CAP_DEFAULT_MTU;
Marcel Holtmann01394182006-07-03 10:02:46 +0200560 }
561
Andrei Emeltchenko8f7975b2011-10-13 16:18:54 +0300562 chan->local_id = L2CAP_BESTEFFORT_ID;
563 chan->local_stype = L2CAP_SERV_BESTEFFORT;
564 chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE;
565 chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME;
566 chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT;
Andrei Emeltchenko8936fa62012-10-08 11:14:41 +0300567 chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO;
Andrei Emeltchenko8f7975b2011-10-13 16:18:54 +0300568
Ulisses Furquim371fd832011-12-21 20:02:36 -0200569 l2cap_chan_hold(chan);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300570
Johan Hedbergc16900c2014-08-15 21:17:06 +0300571 /* Only keep a reference for fixed channels if they requested it */
572 if (chan->chan_type != L2CAP_CHAN_FIXED ||
573 test_bit(FLAG_HOLD_HCI_CONN, &chan->flags))
574 hci_conn_hold(conn->hcon);
Johan Hedberg5ee98912013-04-29 19:35:43 +0300575
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200576 list_add(&chan->list, &conn->chan_l);
Andrei Emeltchenko643162a2012-02-22 17:11:55 +0200577}
578
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300579void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
Andrei Emeltchenko643162a2012-02-22 17:11:55 +0200580{
581 mutex_lock(&conn->chan_lock);
582 __l2cap_chan_add(conn, chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200583 mutex_unlock(&conn->chan_lock);
Marcel Holtmann01394182006-07-03 10:02:46 +0200584}
585
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300586void l2cap_chan_del(struct l2cap_chan *chan, int err)
Marcel Holtmann01394182006-07-03 10:02:46 +0200587{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300588 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann01394182006-07-03 10:02:46 +0200589
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -0300590 __clear_chan_timer(chan);
Marcel Holtmann01394182006-07-03 10:02:46 +0200591
Johan Hedberg49d11742014-11-13 14:37:50 +0200592 BT_DBG("chan %p, conn %p, err %d, state %s", chan, conn, err,
593 state_to_string(chan->state));
Marcel Holtmann01394182006-07-03 10:02:46 +0200594
Johan Hedberg72847ce2014-08-08 09:28:03 +0300595 chan->ops->teardown(chan, err);
596
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900597 if (conn) {
Andrei Emeltchenko56f60982012-10-15 11:58:44 +0300598 struct amp_mgr *mgr = conn->hcon->amp_mgr;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300599 /* Delete from channel list */
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200600 list_del(&chan->list);
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -0200601
Ulisses Furquim371fd832011-12-21 20:02:36 -0200602 l2cap_chan_put(chan);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300603
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300604 chan->conn = NULL;
Andrei Emeltchenko3cabbfd2012-05-31 11:01:37 +0300605
Johan Hedbergc16900c2014-08-15 21:17:06 +0300606 /* Reference was only held for non-fixed channels or
607 * fixed channels that explicitly requested it using the
608 * FLAG_HOLD_HCI_CONN flag.
609 */
610 if (chan->chan_type != L2CAP_CHAN_FIXED ||
611 test_bit(FLAG_HOLD_HCI_CONN, &chan->flags))
David Herrmann76a68ba2013-04-06 20:28:37 +0200612 hci_conn_drop(conn->hcon);
Andrei Emeltchenko56f60982012-10-15 11:58:44 +0300613
614 if (mgr && mgr->bredr_chan == chan)
615 mgr->bredr_chan = NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200616 }
617
Andrei Emeltchenko419e08c2012-10-31 15:46:34 +0200618 if (chan->hs_hchan) {
619 struct hci_chan *hs_hchan = chan->hs_hchan;
620
621 BT_DBG("chan %p disconnect hs_hchan %p", chan, hs_hchan);
622 amp_disconnect_logical_link(hs_hchan);
623 }
624
Mat Martineau28270112012-05-17 21:14:09 -0700625 if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state))
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300626 return;
Gustavo F. Padovan2ead70b2011-04-01 15:13:36 -0300627
Gustavo Padovanee556f62012-05-18 20:22:38 -0300628 switch(chan->mode) {
629 case L2CAP_MODE_BASIC:
630 break;
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300631
Johan Hedberg38319712013-05-17 12:49:23 +0300632 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedberg177f8f22013-05-31 17:54:51 +0300633 skb_queue_purge(&chan->tx_q);
Johan Hedberg38319712013-05-17 12:49:23 +0300634 break;
635
Gustavo Padovanee556f62012-05-18 20:22:38 -0300636 case L2CAP_MODE_ERTM:
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -0300637 __clear_retrans_timer(chan);
638 __clear_monitor_timer(chan);
639 __clear_ack_timer(chan);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300640
Gustavo F. Padovanf1c67752011-03-25 20:36:10 -0300641 skb_queue_purge(&chan->srej_q);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300642
Mat Martineau3c588192012-04-11 10:48:42 -0700643 l2cap_seq_list_free(&chan->srej_list);
644 l2cap_seq_list_free(&chan->retrans_list);
Gustavo Padovanee556f62012-05-18 20:22:38 -0300645
646 /* fall through */
647
648 case L2CAP_MODE_STREAMING:
649 skb_queue_purge(&chan->tx_q);
650 break;
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300651 }
Gustavo Padovanee556f62012-05-18 20:22:38 -0300652
653 return;
Marcel Holtmann01394182006-07-03 10:02:46 +0200654}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300655EXPORT_SYMBOL_GPL(l2cap_chan_del);
Marcel Holtmann01394182006-07-03 10:02:46 +0200656
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300657static void l2cap_conn_update_id_addr(struct work_struct *work)
Johan Hedberg387a33e2014-02-18 21:41:33 +0200658{
Johan Hedbergf3d82d02014-09-05 22:19:50 +0300659 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
660 id_addr_update_work);
661 struct hci_conn *hcon = conn->hcon;
Johan Hedberg387a33e2014-02-18 21:41:33 +0200662 struct l2cap_chan *chan;
663
664 mutex_lock(&conn->chan_lock);
665
666 list_for_each_entry(chan, &conn->chan_l, list) {
667 l2cap_chan_lock(chan);
668 bacpy(&chan->dst, &hcon->dst);
Johan Hedberga250e042015-01-15 13:06:44 +0200669 chan->dst_type = bdaddr_dst_type(hcon);
Johan Hedberg387a33e2014-02-18 21:41:33 +0200670 l2cap_chan_unlock(chan);
671 }
672
673 mutex_unlock(&conn->chan_lock);
674}
675
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300676static void l2cap_chan_le_connect_reject(struct l2cap_chan *chan)
677{
678 struct l2cap_conn *conn = chan->conn;
679 struct l2cap_le_conn_rsp rsp;
680 u16 result;
681
682 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
683 result = L2CAP_CR_AUTHORIZATION;
684 else
685 result = L2CAP_CR_BAD_PSM;
686
687 l2cap_state_change(chan, BT_DISCONN);
688
689 rsp.dcid = cpu_to_le16(chan->scid);
690 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +0200691 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +0300692 rsp.credits = cpu_to_le16(chan->rx_credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300693 rsp.result = cpu_to_le16(result);
694
695 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
696 &rsp);
697}
698
Johan Hedberg791d60f2013-05-14 22:24:44 +0300699static void l2cap_chan_connect_reject(struct l2cap_chan *chan)
700{
701 struct l2cap_conn *conn = chan->conn;
702 struct l2cap_conn_rsp rsp;
703 u16 result;
704
705 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
706 result = L2CAP_CR_SEC_BLOCK;
707 else
708 result = L2CAP_CR_BAD_PSM;
709
710 l2cap_state_change(chan, BT_DISCONN);
711
712 rsp.scid = cpu_to_le16(chan->dcid);
713 rsp.dcid = cpu_to_le16(chan->scid);
714 rsp.result = cpu_to_le16(result);
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700715 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Johan Hedberg791d60f2013-05-14 22:24:44 +0300716
717 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp);
718}
719
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300720void l2cap_chan_close(struct l2cap_chan *chan, int reason)
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300721{
722 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300723
Marcel Holtmann7eafc592013-10-13 08:12:47 -0700724 BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300725
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300726 switch (chan->state) {
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300727 case BT_LISTEN:
Gustavo Padovanb699ec02012-10-06 11:51:54 +0100728 chan->ops->teardown(chan, 0);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300729 break;
730
731 case BT_CONNECTED:
732 case BT_CONFIG:
Johan Hedberg7b25c9b2014-01-28 15:28:04 -0800733 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
Gustavo Padovan8d836d72013-10-15 19:24:47 -0300734 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +0200735 l2cap_send_disconn_req(chan, reason);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300736 } else
737 l2cap_chan_del(chan, reason);
738 break;
739
740 case BT_CONNECT2:
Johan Hedberg791d60f2013-05-14 22:24:44 +0300741 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) {
742 if (conn->hcon->type == ACL_LINK)
743 l2cap_chan_connect_reject(chan);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +0300744 else if (conn->hcon->type == LE_LINK)
745 l2cap_chan_le_connect_reject(chan);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300746 }
747
748 l2cap_chan_del(chan, reason);
749 break;
750
751 case BT_CONNECT:
752 case BT_DISCONN:
753 l2cap_chan_del(chan, reason);
754 break;
755
756 default:
Gustavo Padovanb699ec02012-10-06 11:51:54 +0100757 chan->ops->teardown(chan, 0);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300758 break;
759 }
760}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300761EXPORT_SYMBOL(l2cap_chan_close);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300762
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300763static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
Johan Hedberg8556edd32011-01-19 12:06:50 +0530764{
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700765 switch (chan->chan_type) {
766 case L2CAP_CHAN_RAW:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300767 switch (chan->sec_level) {
Johan Hedberg8556edd32011-01-19 12:06:50 +0530768 case BT_SECURITY_HIGH:
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800769 case BT_SECURITY_FIPS:
Johan Hedberg8556edd32011-01-19 12:06:50 +0530770 return HCI_AT_DEDICATED_BONDING_MITM;
771 case BT_SECURITY_MEDIUM:
772 return HCI_AT_DEDICATED_BONDING;
773 default:
774 return HCI_AT_NO_BONDING;
775 }
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700776 break;
Marcel Holtmann3124b842013-10-12 07:19:32 -0700777 case L2CAP_CHAN_CONN_LESS:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700778 if (chan->psm == cpu_to_le16(L2CAP_PSM_3DSP)) {
Marcel Holtmann3124b842013-10-12 07:19:32 -0700779 if (chan->sec_level == BT_SECURITY_LOW)
780 chan->sec_level = BT_SECURITY_SDP;
781 }
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800782 if (chan->sec_level == BT_SECURITY_HIGH ||
783 chan->sec_level == BT_SECURITY_FIPS)
Marcel Holtmann3124b842013-10-12 07:19:32 -0700784 return HCI_AT_NO_BONDING_MITM;
785 else
786 return HCI_AT_NO_BONDING;
787 break;
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700788 case L2CAP_CHAN_CONN_ORIENTED:
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700789 if (chan->psm == cpu_to_le16(L2CAP_PSM_SDP)) {
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700790 if (chan->sec_level == BT_SECURITY_LOW)
791 chan->sec_level = BT_SECURITY_SDP;
Johan Hedberg8556edd32011-01-19 12:06:50 +0530792
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800793 if (chan->sec_level == BT_SECURITY_HIGH ||
794 chan->sec_level == BT_SECURITY_FIPS)
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700795 return HCI_AT_NO_BONDING_MITM;
796 else
797 return HCI_AT_NO_BONDING;
798 }
799 /* fall through */
800 default:
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300801 switch (chan->sec_level) {
Johan Hedberg8556edd32011-01-19 12:06:50 +0530802 case BT_SECURITY_HIGH:
Marcel Holtmann7d513e92014-01-15 22:37:40 -0800803 case BT_SECURITY_FIPS:
Johan Hedberg8556edd32011-01-19 12:06:50 +0530804 return HCI_AT_GENERAL_BONDING_MITM;
805 case BT_SECURITY_MEDIUM:
806 return HCI_AT_GENERAL_BONDING;
807 default:
808 return HCI_AT_NO_BONDING;
809 }
Marcel Holtmann6a974b52013-10-12 07:19:31 -0700810 break;
Johan Hedberg8556edd32011-01-19 12:06:50 +0530811 }
812}
813
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200814/* Service level security */
Johan Hedberge7cafc42014-07-17 15:35:38 +0300815int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator)
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200816{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300817 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100818 __u8 auth_type;
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200819
Johan Hedberga17de2f2013-05-14 13:25:37 +0300820 if (conn->hcon->type == LE_LINK)
821 return smp_conn_security(conn->hcon, chan->sec_level);
822
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300823 auth_type = l2cap_get_auth_type(chan);
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100824
Johan Hedberge7cafc42014-07-17 15:35:38 +0300825 return hci_conn_security(conn->hcon, chan->sec_level, auth_type,
826 initiator);
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200827}
828
Johannes Bergb5ad8b72011-06-01 08:54:45 +0200829static u8 l2cap_get_ident(struct l2cap_conn *conn)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200830{
831 u8 id;
832
833 /* Get next available identificator.
834 * 1 - 128 are used by kernel.
835 * 129 - 199 are reserved.
836 * 200 - 254 are used by utilities like l2ping, etc.
837 */
838
Marcel Holtmann5a54e7c2014-07-13 20:50:15 +0200839 mutex_lock(&conn->ident_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200840
841 if (++conn->tx_ident > 128)
842 conn->tx_ident = 1;
843
844 id = conn->tx_ident;
845
Marcel Holtmann5a54e7c2014-07-13 20:50:15 +0200846 mutex_unlock(&conn->ident_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200847
848 return id;
849}
850
Gustavo Padovan2d792812012-10-06 10:07:01 +0100851static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
852 void *data)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200853{
854 struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +0200855 u8 flags;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200856
857 BT_DBG("code 0x%2.2x", code);
858
859 if (!skb)
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -0300860 return;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200861
Steven Walterf6af6752014-11-19 09:41:17 -0500862 /* Use NO_FLUSH if supported or we have an LE link (which does
863 * not support auto-flushing packets) */
864 if (lmp_no_flush_capable(conn->hcon->hdev) ||
865 conn->hcon->type == LE_LINK)
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +0200866 flags = ACL_START_NO_FLUSH;
867 else
868 flags = ACL_START;
869
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700870 bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +0200871 skb->priority = HCI_PRIO_MAX;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700872
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200873 hci_send_acl(conn->hchan, skb, flags);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200874}
875
Mat Martineau02b0fbb2012-10-23 15:24:10 -0700876static bool __chan_is_moving(struct l2cap_chan *chan)
877{
878 return chan->move_state != L2CAP_MOVE_STABLE &&
879 chan->move_state != L2CAP_MOVE_WAIT_PREPARE;
880}
881
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200882static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
883{
884 struct hci_conn *hcon = chan->conn->hcon;
885 u16 flags;
886
887 BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100888 skb->priority);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200889
Mat Martineaud5f8a752012-10-23 15:24:18 -0700890 if (chan->hs_hcon && !__chan_is_moving(chan)) {
891 if (chan->hs_hchan)
892 hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE);
893 else
894 kfree_skb(skb);
895
896 return;
897 }
898
Steven Walterf6af6752014-11-19 09:41:17 -0500899 /* Use NO_FLUSH for LE links (where this is the only option) or
900 * if the BR/EDR link supports it and flushing has not been
901 * explicitly requested (through FLAG_FLUSHABLE).
902 */
903 if (hcon->type == LE_LINK ||
904 (!test_bit(FLAG_FLUSHABLE, &chan->flags) &&
905 lmp_no_flush_capable(hcon->hdev)))
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200906 flags = ACL_START_NO_FLUSH;
907 else
908 flags = ACL_START;
909
910 bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
911 hci_send_acl(chan->conn->hchan, skb, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700914static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control)
915{
916 control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT;
917 control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT;
918
919 if (enh & L2CAP_CTRL_FRAME_TYPE) {
920 /* S-Frame */
921 control->sframe = 1;
922 control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT;
923 control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT;
924
925 control->sar = 0;
926 control->txseq = 0;
927 } else {
928 /* I-Frame */
929 control->sframe = 0;
930 control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT;
931 control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT;
932
933 control->poll = 0;
934 control->super = 0;
935 }
936}
937
938static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control)
939{
940 control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT;
941 control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT;
942
943 if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) {
944 /* S-Frame */
945 control->sframe = 1;
946 control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT;
947 control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT;
948
949 control->sar = 0;
950 control->txseq = 0;
951 } else {
952 /* I-Frame */
953 control->sframe = 0;
954 control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT;
955 control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT;
956
957 control->poll = 0;
958 control->super = 0;
959 }
960}
961
962static inline void __unpack_control(struct l2cap_chan *chan,
963 struct sk_buff *skb)
964{
965 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
966 __unpack_extended_control(get_unaligned_le32(skb->data),
Johan Hedberga4368ff2015-03-30 23:21:01 +0300967 &bt_cb(skb)->l2cap);
Mat Martineaucec8ab6e2012-05-17 20:53:36 -0700968 skb_pull(skb, L2CAP_EXT_CTRL_SIZE);
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700969 } else {
970 __unpack_enhanced_control(get_unaligned_le16(skb->data),
Johan Hedberga4368ff2015-03-30 23:21:01 +0300971 &bt_cb(skb)->l2cap);
Mat Martineaucec8ab6e2012-05-17 20:53:36 -0700972 skb_pull(skb, L2CAP_ENH_CTRL_SIZE);
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700973 }
974}
975
976static u32 __pack_extended_control(struct l2cap_ctrl *control)
977{
978 u32 packed;
979
980 packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT;
981 packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT;
982
983 if (control->sframe) {
984 packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT;
985 packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT;
986 packed |= L2CAP_EXT_CTRL_FRAME_TYPE;
987 } else {
988 packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT;
989 packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT;
990 }
991
992 return packed;
993}
994
995static u16 __pack_enhanced_control(struct l2cap_ctrl *control)
996{
997 u16 packed;
998
999 packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT;
1000 packed |= control->final << L2CAP_CTRL_FINAL_SHIFT;
1001
1002 if (control->sframe) {
1003 packed |= control->poll << L2CAP_CTRL_POLL_SHIFT;
1004 packed |= control->super << L2CAP_CTRL_SUPER_SHIFT;
1005 packed |= L2CAP_CTRL_FRAME_TYPE;
1006 } else {
1007 packed |= control->sar << L2CAP_CTRL_SAR_SHIFT;
1008 packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT;
1009 }
1010
1011 return packed;
1012}
1013
1014static inline void __pack_control(struct l2cap_chan *chan,
1015 struct l2cap_ctrl *control,
1016 struct sk_buff *skb)
1017{
1018 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
1019 put_unaligned_le32(__pack_extended_control(control),
1020 skb->data + L2CAP_HDR_SIZE);
1021 } else {
1022 put_unaligned_le16(__pack_enhanced_control(control),
1023 skb->data + L2CAP_HDR_SIZE);
1024 }
1025}
1026
Gustavo Padovanba7aa642012-05-29 13:29:16 -03001027static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan)
1028{
1029 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1030 return L2CAP_EXT_HDR_SIZE;
1031 else
1032 return L2CAP_ENH_HDR_SIZE;
1033}
1034
Mat Martineaua67d7f62012-05-17 20:53:35 -07001035static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan,
1036 u32 control)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001037{
1038 struct sk_buff *skb;
1039 struct l2cap_hdr *lh;
Gustavo Padovanba7aa642012-05-29 13:29:16 -03001040 int hlen = __ertm_hdr_size(chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001041
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001042 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03001043 hlen += L2CAP_FCS_SIZE;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001044
Mat Martineaua67d7f62012-05-17 20:53:35 -07001045 skb = bt_skb_alloc(hlen, GFP_KERNEL);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001046
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001047 if (!skb)
Mat Martineaua67d7f62012-05-17 20:53:35 -07001048 return ERR_PTR(-ENOMEM);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001049
1050 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001051 lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001052 lh->cid = cpu_to_le16(chan->dcid);
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03001053
Mat Martineaua67d7f62012-05-17 20:53:35 -07001054 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1055 put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
1056 else
1057 put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001058
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001059 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineaua67d7f62012-05-17 20:53:35 -07001060 u16 fcs = crc16(0, (u8 *)skb->data, skb->len);
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03001061 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001062 }
1063
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001064 skb->priority = HCI_PRIO_MAX;
Mat Martineaua67d7f62012-05-17 20:53:35 -07001065 return skb;
1066}
1067
1068static void l2cap_send_sframe(struct l2cap_chan *chan,
1069 struct l2cap_ctrl *control)
1070{
1071 struct sk_buff *skb;
1072 u32 control_field;
1073
1074 BT_DBG("chan %p, control %p", chan, control);
1075
1076 if (!control->sframe)
1077 return;
1078
Mat Martineaub99e13a2012-10-23 15:24:19 -07001079 if (__chan_is_moving(chan))
1080 return;
1081
Mat Martineaua67d7f62012-05-17 20:53:35 -07001082 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) &&
1083 !control->poll)
1084 control->final = 1;
1085
1086 if (control->super == L2CAP_SUPER_RR)
1087 clear_bit(CONN_RNR_SENT, &chan->conn_state);
1088 else if (control->super == L2CAP_SUPER_RNR)
1089 set_bit(CONN_RNR_SENT, &chan->conn_state);
1090
1091 if (control->super != L2CAP_SUPER_SREJ) {
1092 chan->last_acked_seq = control->reqseq;
1093 __clear_ack_timer(chan);
1094 }
1095
1096 BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq,
1097 control->final, control->poll, control->super);
1098
1099 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
1100 control_field = __pack_extended_control(control);
1101 else
1102 control_field = __pack_enhanced_control(control);
1103
1104 skb = l2cap_create_sframe_pdu(chan, control_field);
1105 if (!IS_ERR(skb))
1106 l2cap_do_send(chan, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001107}
1108
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001109static void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, bool poll)
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001110{
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001111 struct l2cap_ctrl control;
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001112
Mat Martineauc9e3d5e2012-05-17 20:53:48 -07001113 BT_DBG("chan %p, poll %d", chan, poll);
1114
1115 memset(&control, 0, sizeof(control));
1116 control.sframe = 1;
1117 control.poll = poll;
1118
1119 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
1120 control.super = L2CAP_SUPER_RNR;
1121 else
1122 control.super = L2CAP_SUPER_RR;
1123
1124 control.reqseq = chan->buffer_seq;
1125 l2cap_send_sframe(chan, &control);
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001126}
1127
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001128static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
Andrei Emeltchenkoe501d052010-07-08 12:14:41 +03001129{
Johan Hedberg5ff6f342014-08-07 22:56:43 +03001130 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
1131 return true;
1132
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001133 return !test_bit(CONF_CONNECT_PEND, &chan->conf_state);
Andrei Emeltchenkoe501d052010-07-08 12:14:41 +03001134}
1135
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001136static bool __amp_capable(struct l2cap_chan *chan)
1137{
1138 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann1df7b172013-10-05 11:47:49 -07001139 struct hci_dev *hdev;
1140 bool amp_available = false;
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001141
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02001142 if (!(conn->local_fixed_chan & L2CAP_FC_A2MP))
Marcel Holtmann1df7b172013-10-05 11:47:49 -07001143 return false;
1144
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02001145 if (!(conn->remote_fixed_chan & L2CAP_FC_A2MP))
Marcel Holtmann1df7b172013-10-05 11:47:49 -07001146 return false;
1147
1148 read_lock(&hci_dev_list_lock);
1149 list_for_each_entry(hdev, &hci_dev_list, list) {
1150 if (hdev->amp_type != AMP_TYPE_BREDR &&
1151 test_bit(HCI_UP, &hdev->flags)) {
1152 amp_available = true;
1153 break;
1154 }
1155 }
1156 read_unlock(&hci_dev_list_lock);
1157
1158 if (chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED)
1159 return amp_available;
Marcel Holtmann848566b2013-10-01 22:59:22 -07001160
1161 return false;
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001162}
1163
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +02001164static bool l2cap_check_efs(struct l2cap_chan *chan)
1165{
1166 /* Check EFS parameters */
1167 return true;
1168}
1169
Andrei Emeltchenko2766be42012-09-27 17:26:21 +03001170void l2cap_send_conn_req(struct l2cap_chan *chan)
Andrei Emeltchenko9b27f352012-02-24 16:00:00 +02001171{
1172 struct l2cap_conn *conn = chan->conn;
1173 struct l2cap_conn_req req;
1174
1175 req.scid = cpu_to_le16(chan->scid);
1176 req.psm = chan->psm;
1177
1178 chan->ident = l2cap_get_ident(conn);
1179
1180 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
1181
1182 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req);
1183}
1184
Mat Martineau8eb200b2012-10-23 15:24:17 -07001185static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id)
1186{
1187 struct l2cap_create_chan_req req;
1188 req.scid = cpu_to_le16(chan->scid);
1189 req.psm = chan->psm;
1190 req.amp_id = amp_id;
1191
1192 chan->ident = l2cap_get_ident(chan->conn);
1193
1194 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ,
1195 sizeof(req), &req);
1196}
1197
Mat Martineau02b0fbb2012-10-23 15:24:10 -07001198static void l2cap_move_setup(struct l2cap_chan *chan)
1199{
1200 struct sk_buff *skb;
1201
1202 BT_DBG("chan %p", chan);
1203
1204 if (chan->mode != L2CAP_MODE_ERTM)
1205 return;
1206
1207 __clear_retrans_timer(chan);
1208 __clear_monitor_timer(chan);
1209 __clear_ack_timer(chan);
1210
1211 chan->retry_count = 0;
1212 skb_queue_walk(&chan->tx_q, skb) {
Johan Hedberga4368ff2015-03-30 23:21:01 +03001213 if (bt_cb(skb)->l2cap.retries)
1214 bt_cb(skb)->l2cap.retries = 1;
Mat Martineau02b0fbb2012-10-23 15:24:10 -07001215 else
1216 break;
1217 }
1218
1219 chan->expected_tx_seq = chan->buffer_seq;
1220
1221 clear_bit(CONN_REJ_ACT, &chan->conn_state);
1222 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
1223 l2cap_seq_list_clear(&chan->retrans_list);
1224 l2cap_seq_list_clear(&chan->srej_list);
1225 skb_queue_purge(&chan->srej_q);
1226
1227 chan->tx_state = L2CAP_TX_STATE_XMIT;
1228 chan->rx_state = L2CAP_RX_STATE_MOVE;
1229
1230 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
1231}
1232
Mat Martineau5f3847a2012-10-23 15:24:12 -07001233static void l2cap_move_done(struct l2cap_chan *chan)
1234{
1235 u8 move_role = chan->move_role;
1236 BT_DBG("chan %p", chan);
1237
1238 chan->move_state = L2CAP_MOVE_STABLE;
1239 chan->move_role = L2CAP_MOVE_ROLE_NONE;
1240
1241 if (chan->mode != L2CAP_MODE_ERTM)
1242 return;
1243
1244 switch (move_role) {
1245 case L2CAP_MOVE_ROLE_INITIATOR:
1246 l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL);
1247 chan->rx_state = L2CAP_RX_STATE_WAIT_F;
1248 break;
1249 case L2CAP_MOVE_ROLE_RESPONDER:
1250 chan->rx_state = L2CAP_RX_STATE_WAIT_P;
1251 break;
1252 }
1253}
1254
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001255static void l2cap_chan_ready(struct l2cap_chan *chan)
1256{
Johan Hedberg315917e2015-02-16 11:42:11 +02001257 /* The channel may have already been flagged as connected in
1258 * case of receiving data before the L2CAP info req/rsp
1259 * procedure is complete.
1260 */
1261 if (chan->state == BT_CONNECTED)
1262 return;
1263
Mat Martineau28270112012-05-17 21:14:09 -07001264 /* This clears all conf flags, including CONF_NOT_COMPLETE */
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001265 chan->conf_state = 0;
1266 __clear_chan_timer(chan);
1267
Johan Hedberg0ce43ce2013-12-05 14:55:33 +02001268 if (chan->mode == L2CAP_MODE_LE_FLOWCTL && !chan->tx_credits)
1269 chan->ops->suspend(chan);
Johan Hedberg177f8f22013-05-31 17:54:51 +03001270
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001271 chan->state = BT_CONNECTED;
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001272
Andrei Emeltchenkofd83e2c2012-05-30 09:55:32 +03001273 chan->ops->ready(chan);
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001274}
1275
Johan Hedbergf1496de2013-05-13 14:15:56 +03001276static void l2cap_le_connect(struct l2cap_chan *chan)
1277{
1278 struct l2cap_conn *conn = chan->conn;
1279 struct l2cap_le_conn_req req;
1280
Johan Hedberg595177f2013-12-02 22:12:22 +02001281 if (test_and_set_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags))
1282 return;
1283
Johan Hedbergf1496de2013-05-13 14:15:56 +03001284 req.psm = chan->psm;
1285 req.scid = cpu_to_le16(chan->scid);
1286 req.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02001287 req.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03001288 req.credits = cpu_to_le16(chan->rx_credits);
Johan Hedbergf1496de2013-05-13 14:15:56 +03001289
1290 chan->ident = l2cap_get_ident(conn);
1291
1292 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_REQ,
1293 sizeof(req), &req);
1294}
1295
1296static void l2cap_le_start(struct l2cap_chan *chan)
1297{
1298 struct l2cap_conn *conn = chan->conn;
1299
1300 if (!smp_conn_security(conn->hcon, chan->sec_level))
1301 return;
1302
1303 if (!chan->psm) {
1304 l2cap_chan_ready(chan);
1305 return;
1306 }
1307
1308 if (chan->state == BT_CONNECT)
1309 l2cap_le_connect(chan);
1310}
1311
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001312static void l2cap_start_connection(struct l2cap_chan *chan)
1313{
1314 if (__amp_capable(chan)) {
1315 BT_DBG("chan %p AMP capable: discover AMPs", chan);
1316 a2mp_discover_amp(chan);
Johan Hedbergf1496de2013-05-13 14:15:56 +03001317 } else if (chan->conn->hcon->type == LE_LINK) {
1318 l2cap_le_start(chan);
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001319 } else {
1320 l2cap_send_conn_req(chan);
1321 }
1322}
1323
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001324static void l2cap_request_info(struct l2cap_conn *conn)
1325{
1326 struct l2cap_info_req req;
1327
1328 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
1329 return;
1330
1331 req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
1332
1333 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
1334 conn->info_ident = l2cap_get_ident(conn);
1335
1336 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
1337
1338 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
1339 sizeof(req), &req);
1340}
1341
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001342static void l2cap_do_start(struct l2cap_chan *chan)
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001343{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001344 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001345
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001346 if (conn->hcon->type == LE_LINK) {
Johan Hedberg96ac34f2013-05-13 11:15:07 +03001347 l2cap_le_start(chan);
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001348 return;
1349 }
1350
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001351 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)) {
1352 l2cap_request_info(conn);
1353 return;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001354 }
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001355
1356 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
1357 return;
1358
1359 if (l2cap_chan_check_security(chan, true) &&
1360 __l2cap_no_conn_pending(chan))
1361 l2cap_start_connection(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001362}
1363
Gustavo F. Padovancf6c2c02010-06-07 20:54:45 -03001364static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
1365{
1366 u32 local_feat_mask = l2cap_feat_mask;
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03001367 if (!disable_ertm)
Gustavo F. Padovancf6c2c02010-06-07 20:54:45 -03001368 local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
1369
1370 switch (mode) {
1371 case L2CAP_MODE_ERTM:
1372 return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
1373 case L2CAP_MODE_STREAMING:
1374 return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
1375 default:
1376 return 0x00;
1377 }
1378}
1379
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001380static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err)
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001381{
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001382 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001383 struct l2cap_disconn_req req;
1384
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001385 if (!conn)
1386 return;
1387
Andrei Emeltchenkoaad3d0e2012-09-06 15:05:42 +03001388 if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) {
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -03001389 __clear_retrans_timer(chan);
1390 __clear_monitor_timer(chan);
1391 __clear_ack_timer(chan);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001392 }
1393
Johan Hedberg2338a7e2014-01-24 10:35:40 +02001394 if (chan->scid == L2CAP_CID_A2MP) {
Gustavo Padovand1177732012-10-06 11:47:38 +01001395 l2cap_state_change(chan, BT_DISCONN);
Andrei Emeltchenko416fa752012-05-29 13:59:16 +03001396 return;
1397 }
1398
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001399 req.dcid = cpu_to_le16(chan->dcid);
1400 req.scid = cpu_to_le16(chan->scid);
Gustavo Padovan2d792812012-10-06 10:07:01 +01001401 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ,
1402 sizeof(req), &req);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001403
Gustavo Padovanf8e73012013-10-15 19:24:46 -03001404 l2cap_state_change_and_error(chan, BT_DISCONN, err);
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001405}
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407/* ---- L2CAP connections ---- */
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001408static void l2cap_conn_start(struct l2cap_conn *conn)
1409{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001410 struct l2cap_chan *chan, *tmp;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001411
1412 BT_DBG("conn %p", conn);
1413
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001414 mutex_lock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001415
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001416 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001417 l2cap_chan_lock(chan);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001418
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001419 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001420 l2cap_chan_ready(chan);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001421 l2cap_chan_unlock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001422 continue;
1423 }
1424
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001425 if (chan->state == BT_CONNECT) {
Johan Hedberge7cafc42014-07-17 15:35:38 +03001426 if (!l2cap_chan_check_security(chan, true) ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01001427 !__l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001428 l2cap_chan_unlock(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001429 continue;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02001430 }
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001431
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001432 if (!l2cap_mode_supported(chan->mode, conn->feat_mask)
Gustavo Padovan2d792812012-10-06 10:07:01 +01001433 && test_bit(CONF_STATE2_DEVICE,
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001434 &chan->conf_state)) {
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001435 l2cap_chan_close(chan, ECONNRESET);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001436 l2cap_chan_unlock(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001437 continue;
1438 }
1439
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001440 l2cap_start_connection(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001441
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001442 } else if (chan->state == BT_CONNECT2) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001443 struct l2cap_conn_rsp rsp;
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001444 char buf[128];
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001445 rsp.scid = cpu_to_le16(chan->dcid);
1446 rsp.dcid = cpu_to_le16(chan->scid);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001447
Johan Hedberge7cafc42014-07-17 15:35:38 +03001448 if (l2cap_chan_check_security(chan, false)) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07001449 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001450 rsp.result = cpu_to_le16(L2CAP_CR_PEND);
1451 rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08001452 chan->ops->defer(chan);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001453
1454 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02001455 l2cap_state_change(chan, BT_CONFIG);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001456 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
1457 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001458 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001459 } else {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001460 rsp.result = cpu_to_le16(L2CAP_CR_PEND);
1461 rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001462 }
1463
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001464 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001465 sizeof(rsp), &rsp);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001466
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001467 if (test_bit(CONF_REQ_SENT, &chan->conf_state) ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01001468 rsp.result != L2CAP_CR_SUCCESS) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001469 l2cap_chan_unlock(chan);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001470 continue;
1471 }
1472
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001473 set_bit(CONF_REQ_SENT, &chan->conf_state);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001474 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001475 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03001476 chan->num_conf_req++;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001477 }
1478
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001479 l2cap_chan_unlock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001480 }
1481
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001482 mutex_unlock(&conn->chan_lock);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001483}
1484
Ville Tervob62f3282011-02-10 22:38:50 -03001485static void l2cap_le_conn_ready(struct l2cap_conn *conn)
1486{
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07001487 struct hci_conn *hcon = conn->hcon;
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001488 struct hci_dev *hdev = hcon->hdev;
Ville Tervob62f3282011-02-10 22:38:50 -03001489
Johan Hedberge760ec12014-08-07 22:56:47 +03001490 BT_DBG("%s conn %p", hdev->name, conn);
Ville Tervob62f3282011-02-10 22:38:50 -03001491
Johan Hedberge760ec12014-08-07 22:56:47 +03001492 /* For outgoing pairing which doesn't necessarily have an
1493 * associated socket (e.g. mgmt_pair_device).
1494 */
1495 if (hcon->out)
1496 smp_conn_security(hcon, hcon->pending_sec_level);
Marcel Holtmanncc8dba22013-10-18 03:43:01 -07001497
Marcel Holtmann80afeb62014-06-23 12:18:51 +02001498 /* For LE slave connections, make sure the connection interval
1499 * is in the range of the minium and maximum interval that has
1500 * been configured for this connection. If not, then trigger
1501 * the connection update procedure.
1502 */
Johan Hedberg40bef302014-07-16 11:42:27 +03001503 if (hcon->role == HCI_ROLE_SLAVE &&
Marcel Holtmann80afeb62014-06-23 12:18:51 +02001504 (hcon->le_conn_interval < hcon->le_conn_min_interval ||
1505 hcon->le_conn_interval > hcon->le_conn_max_interval)) {
1506 struct l2cap_conn_param_update_req req;
1507
1508 req.min = cpu_to_le16(hcon->le_conn_min_interval);
1509 req.max = cpu_to_le16(hcon->le_conn_max_interval);
1510 req.latency = cpu_to_le16(hcon->le_conn_latency);
1511 req.to_multiplier = cpu_to_le16(hcon->le_supv_timeout);
1512
1513 l2cap_send_cmd(conn, l2cap_get_ident(conn),
1514 L2CAP_CONN_PARAM_UPDATE_REQ, sizeof(req), &req);
1515 }
Ville Tervob62f3282011-02-10 22:38:50 -03001516}
1517
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001518static void l2cap_conn_ready(struct l2cap_conn *conn)
1519{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001520 struct l2cap_chan *chan;
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001521 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001522
1523 BT_DBG("conn %p", conn);
1524
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001525 if (hcon->type == ACL_LINK)
1526 l2cap_request_info(conn);
1527
Johan Hedberge760ec12014-08-07 22:56:47 +03001528 mutex_lock(&conn->chan_lock);
1529
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001530 list_for_each_entry(chan, &conn->chan_l, list) {
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03001531
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001532 l2cap_chan_lock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001533
Johan Hedberg2338a7e2014-01-24 10:35:40 +02001534 if (chan->scid == L2CAP_CID_A2MP) {
Andrei Emeltchenko416fa752012-05-29 13:59:16 +03001535 l2cap_chan_unlock(chan);
1536 continue;
1537 }
1538
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001539 if (hcon->type == LE_LINK) {
Johan Hedbergf1496de2013-05-13 14:15:56 +03001540 l2cap_le_start(chan);
Vinicius Costa Gomes63128452011-06-17 22:46:26 -03001541 } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
Johan Hedbergaeaeb4b2014-09-10 17:37:46 -07001542 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
1543 l2cap_chan_ready(chan);
Gustavo Padovan1c244f72012-12-07 03:29:10 -02001544 } else if (chan->state == BT_CONNECT) {
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001545 l2cap_do_start(chan);
Gustavo Padovan1c244f72012-12-07 03:29:10 -02001546 }
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001547
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001548 l2cap_chan_unlock(chan);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001549 }
1550
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001551 mutex_unlock(&conn->chan_lock);
Johan Hedberg61a939c2014-01-17 20:45:11 +02001552
Johan Hedberg79a05722014-08-08 09:28:04 +03001553 if (hcon->type == LE_LINK)
1554 l2cap_le_conn_ready(conn);
1555
Johan Hedberg61a939c2014-01-17 20:45:11 +02001556 queue_work(hcon->hdev->workqueue, &conn->pending_rx_work);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001557}
1558
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001559/* Notify sockets that we cannot guaranty reliability anymore */
1560static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
1561{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001562 struct l2cap_chan *chan;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001563
1564 BT_DBG("conn %p", conn);
1565
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001566 mutex_lock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001567
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001568 list_for_each_entry(chan, &conn->chan_l, list) {
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +03001569 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
Gustavo Padovan1d8b1fd2012-10-06 11:34:52 +01001570 l2cap_chan_set_err(chan, err);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001571 }
1572
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001573 mutex_unlock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001574}
1575
Gustavo F. Padovanf878fca2011-12-15 01:16:14 -02001576static void l2cap_info_timeout(struct work_struct *work)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001577{
Gustavo F. Padovanf878fca2011-12-15 01:16:14 -02001578 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001579 info_timer.work);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001580
Marcel Holtmann984947d2009-02-06 23:35:19 +01001581 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01001582 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01001583
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001584 l2cap_conn_start(conn);
1585}
1586
David Herrmann2c8e1412013-04-06 20:28:45 +02001587/*
1588 * l2cap_user
1589 * External modules can register l2cap_user objects on l2cap_conn. The ->probe
1590 * callback is called during registration. The ->remove callback is called
1591 * during unregistration.
1592 * An l2cap_user object can either be explicitly unregistered or when the
1593 * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon,
1594 * l2cap->hchan, .. are valid as long as the remove callback hasn't been called.
1595 * External modules must own a reference to the l2cap_conn object if they intend
1596 * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at
1597 * any time if they don't.
1598 */
1599
1600int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user)
1601{
1602 struct hci_dev *hdev = conn->hcon->hdev;
1603 int ret;
1604
1605 /* We need to check whether l2cap_conn is registered. If it is not, we
1606 * must not register the l2cap_user. l2cap_conn_del() is unregisters
1607 * l2cap_conn objects, but doesn't provide its own locking. Instead, it
1608 * relies on the parent hci_conn object to be locked. This itself relies
1609 * on the hci_dev object to be locked. So we must lock the hci device
1610 * here, too. */
1611
1612 hci_dev_lock(hdev);
1613
Alexey Dobriyan835a6a22015-06-10 20:28:33 +03001614 if (!list_empty(&user->list)) {
David Herrmann2c8e1412013-04-06 20:28:45 +02001615 ret = -EINVAL;
1616 goto out_unlock;
1617 }
1618
1619 /* conn->hchan is NULL after l2cap_conn_del() was called */
1620 if (!conn->hchan) {
1621 ret = -ENODEV;
1622 goto out_unlock;
1623 }
1624
1625 ret = user->probe(conn, user);
1626 if (ret)
1627 goto out_unlock;
1628
1629 list_add(&user->list, &conn->users);
1630 ret = 0;
1631
1632out_unlock:
1633 hci_dev_unlock(hdev);
1634 return ret;
1635}
1636EXPORT_SYMBOL(l2cap_register_user);
1637
1638void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user)
1639{
1640 struct hci_dev *hdev = conn->hcon->hdev;
1641
1642 hci_dev_lock(hdev);
1643
Alexey Dobriyan835a6a22015-06-10 20:28:33 +03001644 if (list_empty(&user->list))
David Herrmann2c8e1412013-04-06 20:28:45 +02001645 goto out_unlock;
1646
Tedd Ho-Jeong Anab944c82015-06-30 11:43:40 -07001647 list_del_init(&user->list);
David Herrmann2c8e1412013-04-06 20:28:45 +02001648 user->remove(conn, user);
1649
1650out_unlock:
1651 hci_dev_unlock(hdev);
1652}
1653EXPORT_SYMBOL(l2cap_unregister_user);
1654
1655static void l2cap_unregister_all_users(struct l2cap_conn *conn)
1656{
1657 struct l2cap_user *user;
1658
1659 while (!list_empty(&conn->users)) {
1660 user = list_first_entry(&conn->users, struct l2cap_user, list);
Tedd Ho-Jeong Anab944c82015-06-30 11:43:40 -07001661 list_del_init(&user->list);
David Herrmann2c8e1412013-04-06 20:28:45 +02001662 user->remove(conn, user);
1663 }
1664}
1665
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001666static void l2cap_conn_del(struct hci_conn *hcon, int err)
1667{
1668 struct l2cap_conn *conn = hcon->l2cap_data;
1669 struct l2cap_chan *chan, *l;
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001670
1671 if (!conn)
1672 return;
1673
1674 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
1675
1676 kfree_skb(conn->rx_skb);
1677
Johan Hedberg61a939c2014-01-17 20:45:11 +02001678 skb_queue_purge(&conn->pending_rx);
Jukka Taimisto7ab56c32014-06-12 10:15:13 +00001679
1680 /* We can not call flush_work(&conn->pending_rx_work) here since we
1681 * might block if we are running on a worker from the same workqueue
1682 * pending_rx_work is waiting on.
1683 */
1684 if (work_pending(&conn->pending_rx_work))
1685 cancel_work_sync(&conn->pending_rx_work);
Johan Hedberg61a939c2014-01-17 20:45:11 +02001686
Johan Hedbergf3d82d02014-09-05 22:19:50 +03001687 if (work_pending(&conn->id_addr_update_work))
1688 cancel_work_sync(&conn->id_addr_update_work);
1689
David Herrmann2c8e1412013-04-06 20:28:45 +02001690 l2cap_unregister_all_users(conn);
1691
Johan Hedberge31fb862014-08-18 20:33:28 +03001692 /* Force the connection to be immediately dropped */
1693 hcon->disc_timeout = 0;
1694
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001695 mutex_lock(&conn->chan_lock);
1696
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001697 /* Kill channels */
1698 list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
Mat Martineau61d6ef32012-04-27 16:50:50 -07001699 l2cap_chan_hold(chan);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001700 l2cap_chan_lock(chan);
1701
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001702 l2cap_chan_del(chan, err);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001703
1704 l2cap_chan_unlock(chan);
1705
Gustavo Padovan80b98022012-05-27 22:27:51 -03001706 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07001707 l2cap_chan_put(chan);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001708 }
1709
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001710 mutex_unlock(&conn->chan_lock);
1711
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001712 hci_chan_del(conn->hchan);
1713
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001714 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
Ulisses Furquim127074b2012-01-30 18:26:29 -02001715 cancel_delayed_work_sync(&conn->info_timer);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001716
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001717 hcon->l2cap_data = NULL;
David Herrmann9c903e32013-04-06 20:28:44 +02001718 conn->hchan = NULL;
1719 l2cap_conn_put(conn);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001720}
1721
David Herrmann9c903e32013-04-06 20:28:44 +02001722static void l2cap_conn_free(struct kref *ref)
1723{
1724 struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref);
1725
1726 hci_conn_put(conn->hcon);
1727 kfree(conn);
1728}
1729
Johan Hedberg51bb84572014-08-15 21:06:57 +03001730struct l2cap_conn *l2cap_conn_get(struct l2cap_conn *conn)
David Herrmann9c903e32013-04-06 20:28:44 +02001731{
1732 kref_get(&conn->ref);
Johan Hedberg51bb84572014-08-15 21:06:57 +03001733 return conn;
David Herrmann9c903e32013-04-06 20:28:44 +02001734}
1735EXPORT_SYMBOL(l2cap_conn_get);
1736
1737void l2cap_conn_put(struct l2cap_conn *conn)
1738{
1739 kref_put(&conn->ref, l2cap_conn_free);
1740}
1741EXPORT_SYMBOL(l2cap_conn_put);
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743/* ---- Socket interface ---- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Ido Yarivc2287682012-04-20 15:46:07 -03001745/* Find socket with psm and source / destination bdaddr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 * Returns closest match.
1747 */
Ido Yarivc2287682012-04-20 15:46:07 -03001748static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm,
1749 bdaddr_t *src,
Johan Hedbergbf20fd42013-05-14 13:23:13 +03001750 bdaddr_t *dst,
1751 u8 link_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001753 struct l2cap_chan *c, *c1 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001755 read_lock(&chan_list_lock);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00001756
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001757 list_for_each_entry(c, &chan_list, global_l) {
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001758 if (state && c->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 continue;
1760
Johan Hedbergbf20fd42013-05-14 13:23:13 +03001761 if (link_type == ACL_LINK && c->src_type != BDADDR_BREDR)
1762 continue;
1763
1764 if (link_type == LE_LINK && c->src_type == BDADDR_BREDR)
1765 continue;
1766
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001767 if (c->psm == psm) {
Ido Yarivc2287682012-04-20 15:46:07 -03001768 int src_match, dst_match;
1769 int src_any, dst_any;
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 /* Exact match. */
Marcel Holtmann7eafc592013-10-13 08:12:47 -07001772 src_match = !bacmp(&c->src, src);
1773 dst_match = !bacmp(&c->dst, dst);
Ido Yarivc2287682012-04-20 15:46:07 -03001774 if (src_match && dst_match) {
Johan Hedberga24cce12014-08-07 22:56:42 +03001775 l2cap_chan_hold(c);
Johannes Berga7567b22011-06-01 08:29:54 +02001776 read_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001777 return c;
1778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 /* Closest match */
Marcel Holtmann7eafc592013-10-13 08:12:47 -07001781 src_any = !bacmp(&c->src, BDADDR_ANY);
1782 dst_any = !bacmp(&c->dst, BDADDR_ANY);
Ido Yarivc2287682012-04-20 15:46:07 -03001783 if ((src_match && dst_any) || (src_any && dst_match) ||
1784 (src_any && dst_any))
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001785 c1 = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Johan Hedberga24cce12014-08-07 22:56:42 +03001789 if (c1)
1790 l2cap_chan_hold(c1);
1791
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001792 read_unlock(&chan_list_lock);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00001793
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001794 return c1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001797static void l2cap_monitor_timeout(struct work_struct *work)
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001798{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001799 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau4239d162012-05-17 20:53:49 -07001800 monitor_timer.work);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001801
Gustavo F. Padovan525cd182011-03-25 19:43:39 -03001802 BT_DBG("chan %p", chan);
Gustavo F. Padovan0e989582010-04-19 14:45:38 -03001803
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001804 l2cap_chan_lock(chan);
1805
Mat Martineau80909e02012-05-17 20:53:50 -07001806 if (!chan->conn) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001807 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001808 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001809 return;
1810 }
1811
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03001812 l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001813
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001814 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001815 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001816}
1817
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001818static void l2cap_retrans_timeout(struct work_struct *work)
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001819{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001820 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau4239d162012-05-17 20:53:49 -07001821 retrans_timer.work);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001822
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03001823 BT_DBG("chan %p", chan);
Gustavo F. Padovan0e989582010-04-19 14:45:38 -03001824
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001825 l2cap_chan_lock(chan);
1826
Mat Martineau80909e02012-05-17 20:53:50 -07001827 if (!chan->conn) {
1828 l2cap_chan_unlock(chan);
1829 l2cap_chan_put(chan);
1830 return;
1831 }
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001832
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03001833 l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001834 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001835 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001836}
1837
Gustavo Padovand6603662012-05-21 13:58:22 -03001838static void l2cap_streaming_send(struct l2cap_chan *chan,
1839 struct sk_buff_head *skbs)
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001840{
Gustavo F. Padovanccbb84a2010-08-30 18:44:44 -03001841 struct sk_buff *skb;
Mat Martineau37339372012-05-17 20:53:33 -07001842 struct l2cap_ctrl *control;
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001843
Mat Martineau37339372012-05-17 20:53:33 -07001844 BT_DBG("chan %p, skbs %p", chan, skbs);
1845
Mat Martineaub99e13a2012-10-23 15:24:19 -07001846 if (__chan_is_moving(chan))
1847 return;
1848
Mat Martineau37339372012-05-17 20:53:33 -07001849 skb_queue_splice_tail_init(skbs, &chan->tx_q);
1850
1851 while (!skb_queue_empty(&chan->tx_q)) {
1852
1853 skb = skb_dequeue(&chan->tx_q);
1854
Johan Hedberga4368ff2015-03-30 23:21:01 +03001855 bt_cb(skb)->l2cap.retries = 1;
1856 control = &bt_cb(skb)->l2cap;
Mat Martineau37339372012-05-17 20:53:33 -07001857
1858 control->reqseq = 0;
1859 control->txseq = chan->next_tx_seq;
1860
1861 __pack_control(chan, control, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001862
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001863 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineau37339372012-05-17 20:53:33 -07001864 u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
1865 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001866 }
1867
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001868 l2cap_do_send(chan, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001869
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001870 BT_DBG("Sent txseq %u", control->txseq);
Mat Martineau37339372012-05-17 20:53:33 -07001871
Andrei Emeltchenko836be932011-10-17 12:19:57 +03001872 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
Mat Martineau37339372012-05-17 20:53:33 -07001873 chan->frames_sent++;
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001874 }
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001875}
1876
Szymon Janc67c9e842011-07-28 16:24:33 +02001877static int l2cap_ertm_send(struct l2cap_chan *chan)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001878{
1879 struct sk_buff *skb, *tx_skb;
Mat Martineau18a48e72012-05-17 20:53:34 -07001880 struct l2cap_ctrl *control;
1881 int sent = 0;
1882
1883 BT_DBG("chan %p", chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001884
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001885 if (chan->state != BT_CONNECTED)
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001886 return -ENOTCONN;
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001887
Mat Martineau94122bb2012-05-02 09:42:02 -07001888 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
1889 return 0;
1890
Mat Martineaub99e13a2012-10-23 15:24:19 -07001891 if (__chan_is_moving(chan))
1892 return 0;
1893
Mat Martineau18a48e72012-05-17 20:53:34 -07001894 while (chan->tx_send_head &&
1895 chan->unacked_frames < chan->remote_tx_win &&
1896 chan->tx_state == L2CAP_TX_STATE_XMIT) {
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001897
Mat Martineau18a48e72012-05-17 20:53:34 -07001898 skb = chan->tx_send_head;
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001899
Johan Hedberga4368ff2015-03-30 23:21:01 +03001900 bt_cb(skb)->l2cap.retries = 1;
1901 control = &bt_cb(skb)->l2cap;
Gustavo F. Padovan95ffa972010-06-18 20:37:33 -03001902
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03001903 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
Mat Martineau18a48e72012-05-17 20:53:34 -07001904 control->final = 1;
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03001905
Mat Martineau18a48e72012-05-17 20:53:34 -07001906 control->reqseq = chan->buffer_seq;
1907 chan->last_acked_seq = chan->buffer_seq;
1908 control->txseq = chan->next_tx_seq;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001909
Mat Martineau18a48e72012-05-17 20:53:34 -07001910 __pack_control(chan, control, skb);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001911
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001912 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineau18a48e72012-05-17 20:53:34 -07001913 u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
1914 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001915 }
1916
Mat Martineau18a48e72012-05-17 20:53:34 -07001917 /* Clone after data has been modified. Data is assumed to be
1918 read-only (for locking purposes) on cloned sk_buffs.
1919 */
1920 tx_skb = skb_clone(skb, GFP_KERNEL);
1921
1922 if (!tx_skb)
1923 break;
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -03001924
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -03001925 __set_retrans_timer(chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001926
Andrei Emeltchenko836be932011-10-17 12:19:57 +03001927 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
Mat Martineau18a48e72012-05-17 20:53:34 -07001928 chan->unacked_frames++;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03001929 chan->frames_sent++;
Mat Martineau18a48e72012-05-17 20:53:34 -07001930 sent++;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001931
Gustavo F. Padovan58d35f82011-04-04 16:16:44 -03001932 if (skb_queue_is_last(&chan->tx_q, skb))
1933 chan->tx_send_head = NULL;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001934 else
Gustavo F. Padovan58d35f82011-04-04 16:16:44 -03001935 chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
Mat Martineau18a48e72012-05-17 20:53:34 -07001936
1937 l2cap_do_send(chan, tx_skb);
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001938 BT_DBG("Sent txseq %u", control->txseq);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001939 }
1940
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001941 BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent,
1942 chan->unacked_frames, skb_queue_len(&chan->tx_q));
Mat Martineau18a48e72012-05-17 20:53:34 -07001943
1944 return sent;
Gustavo F. Padovan9e917af2010-05-01 16:15:37 -03001945}
1946
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001947static void l2cap_ertm_resend(struct l2cap_chan *chan)
1948{
1949 struct l2cap_ctrl control;
1950 struct sk_buff *skb;
1951 struct sk_buff *tx_skb;
1952 u16 seq;
1953
1954 BT_DBG("chan %p", chan);
1955
1956 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
1957 return;
1958
Mat Martineaub99e13a2012-10-23 15:24:19 -07001959 if (__chan_is_moving(chan))
1960 return;
1961
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001962 while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) {
1963 seq = l2cap_seq_list_pop(&chan->retrans_list);
1964
1965 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq);
1966 if (!skb) {
1967 BT_DBG("Error: Can't retransmit seq %d, frame missing",
Gustavo Padovan2d792812012-10-06 10:07:01 +01001968 seq);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001969 continue;
1970 }
1971
Johan Hedberga4368ff2015-03-30 23:21:01 +03001972 bt_cb(skb)->l2cap.retries++;
1973 control = bt_cb(skb)->l2cap;
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001974
1975 if (chan->max_tx != 0 &&
Johan Hedberga4368ff2015-03-30 23:21:01 +03001976 bt_cb(skb)->l2cap.retries > chan->max_tx) {
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001977 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001978 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001979 l2cap_seq_list_clear(&chan->retrans_list);
1980 break;
1981 }
1982
1983 control.reqseq = chan->buffer_seq;
1984 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
1985 control.final = 1;
1986 else
1987 control.final = 0;
1988
1989 if (skb_cloned(skb)) {
1990 /* Cloned sk_buffs are read-only, so we need a
1991 * writeable copy
1992 */
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03001993 tx_skb = skb_copy(skb, GFP_KERNEL);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001994 } else {
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03001995 tx_skb = skb_clone(skb, GFP_KERNEL);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07001996 }
1997
1998 if (!tx_skb) {
1999 l2cap_seq_list_clear(&chan->retrans_list);
2000 break;
2001 }
2002
2003 /* Update skb contents */
2004 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
2005 put_unaligned_le32(__pack_extended_control(&control),
2006 tx_skb->data + L2CAP_HDR_SIZE);
2007 } else {
2008 put_unaligned_le16(__pack_enhanced_control(&control),
2009 tx_skb->data + L2CAP_HDR_SIZE);
2010 }
2011
Lukasz Rymanowski13cac152014-08-14 09:35:34 +02002012 /* Update FCS */
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002013 if (chan->fcs == L2CAP_FCS_CRC16) {
Lukasz Rymanowski13cac152014-08-14 09:35:34 +02002014 u16 fcs = crc16(0, (u8 *) tx_skb->data,
2015 tx_skb->len - L2CAP_FCS_SIZE);
2016 put_unaligned_le16(fcs, skb_tail_pointer(tx_skb) -
2017 L2CAP_FCS_SIZE);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002018 }
2019
2020 l2cap_do_send(chan, tx_skb);
2021
2022 BT_DBG("Resent txseq %d", control.txseq);
2023
2024 chan->last_acked_seq = chan->buffer_seq;
2025 }
2026}
2027
Mat Martineauf80842a2012-05-17 20:53:46 -07002028static void l2cap_retransmit(struct l2cap_chan *chan,
2029 struct l2cap_ctrl *control)
2030{
2031 BT_DBG("chan %p, control %p", chan, control);
2032
2033 l2cap_seq_list_append(&chan->retrans_list, control->reqseq);
2034 l2cap_ertm_resend(chan);
2035}
2036
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002037static void l2cap_retransmit_all(struct l2cap_chan *chan,
2038 struct l2cap_ctrl *control)
2039{
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002040 struct sk_buff *skb;
2041
2042 BT_DBG("chan %p, control %p", chan, control);
2043
2044 if (control->poll)
2045 set_bit(CONN_SEND_FBIT, &chan->conn_state);
2046
2047 l2cap_seq_list_clear(&chan->retrans_list);
2048
2049 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
2050 return;
2051
2052 if (chan->unacked_frames) {
2053 skb_queue_walk(&chan->tx_q, skb) {
Johan Hedberga4368ff2015-03-30 23:21:01 +03002054 if (bt_cb(skb)->l2cap.txseq == control->reqseq ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01002055 skb == chan->tx_send_head)
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002056 break;
2057 }
2058
2059 skb_queue_walk_from(&chan->tx_q, skb) {
2060 if (skb == chan->tx_send_head)
2061 break;
2062
2063 l2cap_seq_list_append(&chan->retrans_list,
Johan Hedberga4368ff2015-03-30 23:21:01 +03002064 bt_cb(skb)->l2cap.txseq);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002065 }
2066
2067 l2cap_ertm_resend(chan);
2068 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002069}
2070
Szymon Jancb17e73b2012-01-11 10:59:47 +01002071static void l2cap_send_ack(struct l2cap_chan *chan)
2072{
Mat Martineau0a0aba42012-05-17 20:53:39 -07002073 struct l2cap_ctrl control;
2074 u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
2075 chan->last_acked_seq);
2076 int threshold;
2077
2078 BT_DBG("chan %p last_acked_seq %d buffer_seq %d",
2079 chan, chan->last_acked_seq, chan->buffer_seq);
2080
2081 memset(&control, 0, sizeof(control));
2082 control.sframe = 1;
2083
2084 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
2085 chan->rx_state == L2CAP_RX_STATE_RECV) {
2086 __clear_ack_timer(chan);
2087 control.super = L2CAP_SUPER_RNR;
2088 control.reqseq = chan->buffer_seq;
2089 l2cap_send_sframe(chan, &control);
2090 } else {
2091 if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) {
2092 l2cap_ertm_send(chan);
2093 /* If any i-frames were sent, they included an ack */
2094 if (chan->buffer_seq == chan->last_acked_seq)
2095 frames_to_ack = 0;
2096 }
2097
Mat Martineauc20f8e32012-07-10 05:47:07 -07002098 /* Ack now if the window is 3/4ths full.
Mat Martineau0a0aba42012-05-17 20:53:39 -07002099 * Calculate without mul or div
2100 */
Mat Martineauc20f8e32012-07-10 05:47:07 -07002101 threshold = chan->ack_win;
Mat Martineau0a0aba42012-05-17 20:53:39 -07002102 threshold += threshold << 1;
2103 threshold >>= 2;
2104
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002105 BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack,
Mat Martineau0a0aba42012-05-17 20:53:39 -07002106 threshold);
2107
2108 if (frames_to_ack >= threshold) {
2109 __clear_ack_timer(chan);
2110 control.super = L2CAP_SUPER_RR;
2111 control.reqseq = chan->buffer_seq;
2112 l2cap_send_sframe(chan, &control);
2113 frames_to_ack = 0;
2114 }
2115
2116 if (frames_to_ack)
2117 __set_ack_timer(chan);
2118 }
Szymon Jancb17e73b2012-01-11 10:59:47 +01002119}
2120
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002121static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan,
2122 struct msghdr *msg, int len,
2123 int count, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002125 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002126 struct sk_buff **frag;
Gustavo Padovan90338942012-04-06 20:15:47 -03002127 int sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Al Viro17836392014-11-24 17:07:38 -05002129 if (copy_from_iter(skb_put(skb, count), count, &msg->msg_iter) != count)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002130 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 sent += count;
2133 len -= count;
2134
2135 /* Continuation fragments (no L2CAP header) */
2136 frag = &skb_shinfo(skb)->frag_list;
2137 while (len) {
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002138 struct sk_buff *tmp;
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 count = min_t(unsigned int, conn->mtu, len);
2141
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002142 tmp = chan->ops->alloc_skb(chan, 0, count,
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002143 msg->msg_flags & MSG_DONTWAIT);
2144 if (IS_ERR(tmp))
2145 return PTR_ERR(tmp);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002146
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002147 *frag = tmp;
2148
Al Viro17836392014-11-24 17:07:38 -05002149 if (copy_from_iter(skb_put(*frag, count), count,
2150 &msg->msg_iter) != count)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002151 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 sent += count;
2154 len -= count;
2155
Gustavo Padovan2d0ed3d2012-05-11 13:16:12 -03002156 skb->len += (*frag)->len;
2157 skb->data_len += (*frag)->len;
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 frag = &(*frag)->next;
2160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 return sent;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002163}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002165static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan,
Marcel Holtmann8d463212014-06-05 15:22:51 +02002166 struct msghdr *msg, size_t len)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002167{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002168 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002169 struct sk_buff *skb;
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002170 int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002171 struct l2cap_hdr *lh;
2172
Marcel Holtmann8d463212014-06-05 15:22:51 +02002173 BT_DBG("chan %p psm 0x%2.2x len %zu", chan,
2174 __le16_to_cpu(chan->psm), len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002175
2176 count = min_t(unsigned int, (conn->mtu - hlen), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002177
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002178 skb = chan->ops->alloc_skb(chan, hlen, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002179 msg->msg_flags & MSG_DONTWAIT);
2180 if (IS_ERR(skb))
2181 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002182
2183 /* Create L2CAP header */
2184 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002185 lh->cid = cpu_to_le16(chan->dcid);
Andrei Emeltchenkodaf6a78c2012-05-03 10:55:52 +03002186 lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE);
Marcel Holtmann43b1b8d2013-10-12 06:01:26 -07002187 put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002188
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002189 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002190 if (unlikely(err < 0)) {
2191 kfree_skb(skb);
2192 return ERR_PTR(err);
2193 }
2194 return skb;
2195}
2196
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002197static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan,
Marcel Holtmann8d463212014-06-05 15:22:51 +02002198 struct msghdr *msg, size_t len)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002199{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002200 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002201 struct sk_buff *skb;
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002202 int err, count;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002203 struct l2cap_hdr *lh;
2204
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002205 BT_DBG("chan %p len %zu", chan, len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002206
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002207 count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002208
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002209 skb = chan->ops->alloc_skb(chan, L2CAP_HDR_SIZE, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002210 msg->msg_flags & MSG_DONTWAIT);
2211 if (IS_ERR(skb))
2212 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002213
2214 /* Create L2CAP header */
2215 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002216 lh->cid = cpu_to_le16(chan->dcid);
Gustavo Padovan6ff9b5e2012-05-02 11:56:17 -03002217 lh->len = cpu_to_le16(len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002218
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002219 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002220 if (unlikely(err < 0)) {
2221 kfree_skb(skb);
2222 return ERR_PTR(err);
2223 }
2224 return skb;
2225}
2226
Luiz Augusto von Dentzab0ff762011-09-12 20:00:50 +03002227static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan,
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002228 struct msghdr *msg, size_t len,
2229 u16 sdulen)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002230{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002231 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002232 struct sk_buff *skb;
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03002233 int err, count, hlen;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002234 struct l2cap_hdr *lh;
2235
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002236 BT_DBG("chan %p len %zu", chan, len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002237
Gustavo F. Padovan0ee0d202010-05-01 16:15:41 -03002238 if (!conn)
2239 return ERR_PTR(-ENOTCONN);
2240
Gustavo Padovanba7aa642012-05-29 13:29:16 -03002241 hlen = __ertm_hdr_size(chan);
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03002242
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002243 if (sdulen)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002244 hlen += L2CAP_SDULEN_SIZE;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002245
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03002246 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002247 hlen += L2CAP_FCS_SIZE;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03002248
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002249 count = min_t(unsigned int, (conn->mtu - hlen), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002250
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002251 skb = chan->ops->alloc_skb(chan, hlen, count,
Gustavo Padovan90338942012-04-06 20:15:47 -03002252 msg->msg_flags & MSG_DONTWAIT);
2253 if (IS_ERR(skb))
2254 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002255
2256 /* Create L2CAP header */
2257 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002258 lh->cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002259 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03002260
Mat Martineau18a48e72012-05-17 20:53:34 -07002261 /* Control header is populated later */
2262 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
2263 put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
2264 else
2265 put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03002266
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002267 if (sdulen)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002268 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002269
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002270 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002271 if (unlikely(err < 0)) {
2272 kfree_skb(skb);
2273 return ERR_PTR(err);
2274 }
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03002275
Johan Hedberga4368ff2015-03-30 23:21:01 +03002276 bt_cb(skb)->l2cap.fcs = chan->fcs;
2277 bt_cb(skb)->l2cap.retries = 0;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002278 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
Mat Martineau94122bb2012-05-02 09:42:02 -07002281static int l2cap_segment_sdu(struct l2cap_chan *chan,
2282 struct sk_buff_head *seg_queue,
2283 struct msghdr *msg, size_t len)
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002284{
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002285 struct sk_buff *skb;
Mat Martineau94122bb2012-05-02 09:42:02 -07002286 u16 sdu_len;
2287 size_t pdu_len;
Mat Martineau94122bb2012-05-02 09:42:02 -07002288 u8 sar;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002289
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002290 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002291
Mat Martineau94122bb2012-05-02 09:42:02 -07002292 /* It is critical that ERTM PDUs fit in a single HCI fragment,
2293 * so fragmented skbs are not used. The HCI layer's handling
2294 * of fragmented skbs is not compatible with ERTM's queueing.
2295 */
2296
2297 /* PDU size is derived from the HCI MTU */
2298 pdu_len = chan->conn->mtu;
2299
Mat Martineaua5495742012-10-23 15:24:21 -07002300 /* Constrain PDU size for BR/EDR connections */
2301 if (!chan->hs_hcon)
2302 pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD);
Mat Martineau94122bb2012-05-02 09:42:02 -07002303
2304 /* Adjust for largest possible L2CAP overhead. */
Gustavo Padovan35d401d2012-05-25 18:57:05 -03002305 if (chan->fcs)
2306 pdu_len -= L2CAP_FCS_SIZE;
2307
Gustavo Padovanba7aa642012-05-29 13:29:16 -03002308 pdu_len -= __ertm_hdr_size(chan);
Mat Martineau94122bb2012-05-02 09:42:02 -07002309
2310 /* Remote device may have requested smaller PDUs */
2311 pdu_len = min_t(size_t, pdu_len, chan->remote_mps);
2312
2313 if (len <= pdu_len) {
2314 sar = L2CAP_SAR_UNSEGMENTED;
2315 sdu_len = 0;
2316 pdu_len = len;
2317 } else {
2318 sar = L2CAP_SAR_START;
2319 sdu_len = len;
Mat Martineau94122bb2012-05-02 09:42:02 -07002320 }
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002321
2322 while (len > 0) {
Mat Martineau94122bb2012-05-02 09:42:02 -07002323 skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002324
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002325 if (IS_ERR(skb)) {
Mat Martineau94122bb2012-05-02 09:42:02 -07002326 __skb_queue_purge(seg_queue);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002327 return PTR_ERR(skb);
2328 }
2329
Johan Hedberga4368ff2015-03-30 23:21:01 +03002330 bt_cb(skb)->l2cap.sar = sar;
Mat Martineau94122bb2012-05-02 09:42:02 -07002331 __skb_queue_tail(seg_queue, skb);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002332
Mat Martineau94122bb2012-05-02 09:42:02 -07002333 len -= pdu_len;
Lukasz Rymanowski069cb272014-08-13 16:01:41 +02002334 if (sdu_len)
Mat Martineau94122bb2012-05-02 09:42:02 -07002335 sdu_len = 0;
Mat Martineau94122bb2012-05-02 09:42:02 -07002336
2337 if (len <= pdu_len) {
2338 sar = L2CAP_SAR_END;
2339 pdu_len = len;
2340 } else {
2341 sar = L2CAP_SAR_CONTINUE;
2342 }
2343 }
2344
Gustavo Padovanf0f62792012-05-29 13:29:17 -03002345 return 0;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002346}
2347
Johan Hedberg177f8f22013-05-31 17:54:51 +03002348static struct sk_buff *l2cap_create_le_flowctl_pdu(struct l2cap_chan *chan,
2349 struct msghdr *msg,
2350 size_t len, u16 sdulen)
2351{
2352 struct l2cap_conn *conn = chan->conn;
2353 struct sk_buff *skb;
2354 int err, count, hlen;
2355 struct l2cap_hdr *lh;
2356
2357 BT_DBG("chan %p len %zu", chan, len);
2358
2359 if (!conn)
2360 return ERR_PTR(-ENOTCONN);
2361
2362 hlen = L2CAP_HDR_SIZE;
2363
2364 if (sdulen)
2365 hlen += L2CAP_SDULEN_SIZE;
2366
2367 count = min_t(unsigned int, (conn->mtu - hlen), len);
2368
Marcel Holtmannd9fbd022014-06-08 11:22:28 +02002369 skb = chan->ops->alloc_skb(chan, hlen, count,
Johan Hedberg177f8f22013-05-31 17:54:51 +03002370 msg->msg_flags & MSG_DONTWAIT);
2371 if (IS_ERR(skb))
2372 return skb;
2373
2374 /* Create L2CAP header */
2375 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
2376 lh->cid = cpu_to_le16(chan->dcid);
2377 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
2378
2379 if (sdulen)
2380 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
2381
2382 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
2383 if (unlikely(err < 0)) {
2384 kfree_skb(skb);
2385 return ERR_PTR(err);
2386 }
2387
2388 return skb;
2389}
2390
2391static int l2cap_segment_le_sdu(struct l2cap_chan *chan,
2392 struct sk_buff_head *seg_queue,
2393 struct msghdr *msg, size_t len)
2394{
2395 struct sk_buff *skb;
2396 size_t pdu_len;
2397 u16 sdu_len;
2398
2399 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
2400
Johan Hedberg177f8f22013-05-31 17:54:51 +03002401 sdu_len = len;
Johan Hedberg72c6fb92014-08-15 21:06:51 +03002402 pdu_len = chan->remote_mps - L2CAP_SDULEN_SIZE;
Johan Hedberg177f8f22013-05-31 17:54:51 +03002403
2404 while (len > 0) {
2405 if (len <= pdu_len)
2406 pdu_len = len;
2407
2408 skb = l2cap_create_le_flowctl_pdu(chan, msg, pdu_len, sdu_len);
2409 if (IS_ERR(skb)) {
2410 __skb_queue_purge(seg_queue);
2411 return PTR_ERR(skb);
2412 }
2413
2414 __skb_queue_tail(seg_queue, skb);
2415
2416 len -= pdu_len;
2417
2418 if (sdu_len) {
2419 sdu_len = 0;
2420 pdu_len += L2CAP_SDULEN_SIZE;
2421 }
2422 }
2423
2424 return 0;
2425}
2426
Marcel Holtmann8d463212014-06-05 15:22:51 +02002427int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002428{
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002429 struct sk_buff *skb;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002430 int err;
Mat Martineau94122bb2012-05-02 09:42:02 -07002431 struct sk_buff_head seg_queue;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002432
Seung-Woo Kim31e8ce82013-11-05 18:46:33 +09002433 if (!chan->conn)
2434 return -ENOTCONN;
2435
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002436 /* Connectionless channel */
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03002437 if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
Marcel Holtmann8d463212014-06-05 15:22:51 +02002438 skb = l2cap_create_connless_pdu(chan, msg, len);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002439 if (IS_ERR(skb))
2440 return PTR_ERR(skb);
2441
Andrzej Kaczmarekede81a22014-02-25 17:16:22 +01002442 /* Channel lock is released before requesting new skb and then
2443 * reacquired thus we need to recheck channel state.
2444 */
2445 if (chan->state != BT_CONNECTED) {
2446 kfree_skb(skb);
2447 return -ENOTCONN;
2448 }
2449
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002450 l2cap_do_send(chan, skb);
2451 return len;
2452 }
2453
2454 switch (chan->mode) {
Johan Hedberg38319712013-05-17 12:49:23 +03002455 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedberg177f8f22013-05-31 17:54:51 +03002456 /* Check outgoing MTU */
2457 if (len > chan->omtu)
2458 return -EMSGSIZE;
2459
Johan Hedbergfad5fc892013-12-05 09:45:01 +02002460 if (!chan->tx_credits)
2461 return -EAGAIN;
2462
Johan Hedberg177f8f22013-05-31 17:54:51 +03002463 __skb_queue_head_init(&seg_queue);
2464
2465 err = l2cap_segment_le_sdu(chan, &seg_queue, msg, len);
2466
2467 if (chan->state != BT_CONNECTED) {
2468 __skb_queue_purge(&seg_queue);
2469 err = -ENOTCONN;
2470 }
2471
2472 if (err)
2473 return err;
2474
2475 skb_queue_splice_tail_init(&seg_queue, &chan->tx_q);
2476
2477 while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) {
2478 l2cap_do_send(chan, skb_dequeue(&chan->tx_q));
2479 chan->tx_credits--;
2480 }
2481
2482 if (!chan->tx_credits)
2483 chan->ops->suspend(chan);
2484
2485 err = len;
2486
2487 break;
2488
Johan Hedbergfad5fc892013-12-05 09:45:01 +02002489 case L2CAP_MODE_BASIC:
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002490 /* Check outgoing MTU */
2491 if (len > chan->omtu)
2492 return -EMSGSIZE;
2493
2494 /* Create a basic PDU */
Marcel Holtmann8d463212014-06-05 15:22:51 +02002495 skb = l2cap_create_basic_pdu(chan, msg, len);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002496 if (IS_ERR(skb))
2497 return PTR_ERR(skb);
2498
Andrzej Kaczmarekede81a22014-02-25 17:16:22 +01002499 /* Channel lock is released before requesting new skb and then
2500 * reacquired thus we need to recheck channel state.
2501 */
2502 if (chan->state != BT_CONNECTED) {
2503 kfree_skb(skb);
2504 return -ENOTCONN;
2505 }
2506
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002507 l2cap_do_send(chan, skb);
2508 err = len;
2509 break;
2510
2511 case L2CAP_MODE_ERTM:
2512 case L2CAP_MODE_STREAMING:
Mat Martineau94122bb2012-05-02 09:42:02 -07002513 /* Check outgoing MTU */
2514 if (len > chan->omtu) {
2515 err = -EMSGSIZE;
2516 break;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002517 }
2518
Mat Martineau94122bb2012-05-02 09:42:02 -07002519 __skb_queue_head_init(&seg_queue);
2520
2521 /* Do segmentation before calling in to the state machine,
2522 * since it's possible to block while waiting for memory
2523 * allocation.
2524 */
2525 err = l2cap_segment_sdu(chan, &seg_queue, msg, len);
2526
2527 /* The channel could have been closed while segmenting,
2528 * check that it is still connected.
2529 */
2530 if (chan->state != BT_CONNECTED) {
2531 __skb_queue_purge(&seg_queue);
2532 err = -ENOTCONN;
2533 }
2534
2535 if (err)
2536 break;
2537
Mat Martineau37339372012-05-17 20:53:33 -07002538 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo Padovand6603662012-05-21 13:58:22 -03002539 l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST);
Mat Martineau37339372012-05-17 20:53:33 -07002540 else
Gustavo Padovand6603662012-05-21 13:58:22 -03002541 l2cap_streaming_send(chan, &seg_queue);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002542
Gustavo Padovand6603662012-05-21 13:58:22 -03002543 err = len;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002544
Mat Martineau94122bb2012-05-02 09:42:02 -07002545 /* If the skbs were not queued for sending, they'll still be in
2546 * seg_queue and need to be purged.
2547 */
2548 __skb_queue_purge(&seg_queue);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002549 break;
2550
2551 default:
2552 BT_DBG("bad state %1.1x", chan->mode);
2553 err = -EBADFD;
2554 }
2555
2556 return err;
2557}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03002558EXPORT_SYMBOL_GPL(l2cap_chan_send);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002559
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002560static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq)
2561{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002562 struct l2cap_ctrl control;
2563 u16 seq;
2564
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002565 BT_DBG("chan %p, txseq %u", chan, txseq);
Mat Martineaubed68bd2012-05-17 20:53:44 -07002566
2567 memset(&control, 0, sizeof(control));
2568 control.sframe = 1;
2569 control.super = L2CAP_SUPER_SREJ;
2570
2571 for (seq = chan->expected_tx_seq; seq != txseq;
2572 seq = __next_seq(chan, seq)) {
2573 if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) {
2574 control.reqseq = seq;
2575 l2cap_send_sframe(chan, &control);
2576 l2cap_seq_list_append(&chan->srej_list, seq);
2577 }
2578 }
2579
2580 chan->expected_tx_seq = __next_seq(chan, txseq);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002581}
2582
2583static void l2cap_send_srej_tail(struct l2cap_chan *chan)
2584{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002585 struct l2cap_ctrl control;
2586
2587 BT_DBG("chan %p", chan);
2588
2589 if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR)
2590 return;
2591
2592 memset(&control, 0, sizeof(control));
2593 control.sframe = 1;
2594 control.super = L2CAP_SUPER_SREJ;
2595 control.reqseq = chan->srej_list.tail;
2596 l2cap_send_sframe(chan, &control);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002597}
2598
2599static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq)
2600{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002601 struct l2cap_ctrl control;
2602 u16 initial_head;
2603 u16 seq;
2604
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002605 BT_DBG("chan %p, txseq %u", chan, txseq);
Mat Martineaubed68bd2012-05-17 20:53:44 -07002606
2607 memset(&control, 0, sizeof(control));
2608 control.sframe = 1;
2609 control.super = L2CAP_SUPER_SREJ;
2610
2611 /* Capture initial list head to allow only one pass through the list. */
2612 initial_head = chan->srej_list.head;
2613
2614 do {
2615 seq = l2cap_seq_list_pop(&chan->srej_list);
2616 if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR)
2617 break;
2618
2619 control.reqseq = seq;
2620 l2cap_send_sframe(chan, &control);
2621 l2cap_seq_list_append(&chan->srej_list, seq);
2622 } while (chan->srej_list.head != initial_head);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002623}
2624
Mat Martineau608bcc62012-05-17 20:53:32 -07002625static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq)
2626{
2627 struct sk_buff *acked_skb;
2628 u16 ackseq;
2629
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002630 BT_DBG("chan %p, reqseq %u", chan, reqseq);
Mat Martineau608bcc62012-05-17 20:53:32 -07002631
2632 if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq)
2633 return;
2634
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002635 BT_DBG("expected_ack_seq %u, unacked_frames %u",
Mat Martineau608bcc62012-05-17 20:53:32 -07002636 chan->expected_ack_seq, chan->unacked_frames);
2637
2638 for (ackseq = chan->expected_ack_seq; ackseq != reqseq;
2639 ackseq = __next_seq(chan, ackseq)) {
2640
2641 acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq);
2642 if (acked_skb) {
2643 skb_unlink(acked_skb, &chan->tx_q);
2644 kfree_skb(acked_skb);
2645 chan->unacked_frames--;
2646 }
2647 }
2648
2649 chan->expected_ack_seq = reqseq;
2650
2651 if (chan->unacked_frames == 0)
2652 __clear_retrans_timer(chan);
2653
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002654 BT_DBG("unacked_frames %u", chan->unacked_frames);
Mat Martineau608bcc62012-05-17 20:53:32 -07002655}
2656
2657static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan)
2658{
2659 BT_DBG("chan %p", chan);
2660
2661 chan->expected_tx_seq = chan->buffer_seq;
2662 l2cap_seq_list_clear(&chan->srej_list);
2663 skb_queue_purge(&chan->srej_q);
2664 chan->rx_state = L2CAP_RX_STATE_RECV;
2665}
2666
Gustavo Padovand6603662012-05-21 13:58:22 -03002667static void l2cap_tx_state_xmit(struct l2cap_chan *chan,
2668 struct l2cap_ctrl *control,
2669 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002670{
Mat Martineau608bcc62012-05-17 20:53:32 -07002671 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2672 event);
2673
2674 switch (event) {
2675 case L2CAP_EV_DATA_REQUEST:
2676 if (chan->tx_send_head == NULL)
2677 chan->tx_send_head = skb_peek(skbs);
2678
2679 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2680 l2cap_ertm_send(chan);
2681 break;
2682 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2683 BT_DBG("Enter LOCAL_BUSY");
2684 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2685
2686 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2687 /* The SREJ_SENT state must be aborted if we are to
2688 * enter the LOCAL_BUSY state.
2689 */
2690 l2cap_abort_rx_srej_sent(chan);
2691 }
2692
2693 l2cap_send_ack(chan);
2694
2695 break;
2696 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2697 BT_DBG("Exit LOCAL_BUSY");
2698 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2699
2700 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2701 struct l2cap_ctrl local_control;
2702
2703 memset(&local_control, 0, sizeof(local_control));
2704 local_control.sframe = 1;
2705 local_control.super = L2CAP_SUPER_RR;
2706 local_control.poll = 1;
2707 local_control.reqseq = chan->buffer_seq;
Mat Martineaua67d7f62012-05-17 20:53:35 -07002708 l2cap_send_sframe(chan, &local_control);
Mat Martineau608bcc62012-05-17 20:53:32 -07002709
2710 chan->retry_count = 1;
2711 __set_monitor_timer(chan);
2712 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2713 }
2714 break;
2715 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2716 l2cap_process_reqseq(chan, control->reqseq);
2717 break;
2718 case L2CAP_EV_EXPLICIT_POLL:
2719 l2cap_send_rr_or_rnr(chan, 1);
2720 chan->retry_count = 1;
2721 __set_monitor_timer(chan);
2722 __clear_ack_timer(chan);
2723 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2724 break;
2725 case L2CAP_EV_RETRANS_TO:
2726 l2cap_send_rr_or_rnr(chan, 1);
2727 chan->retry_count = 1;
2728 __set_monitor_timer(chan);
2729 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2730 break;
2731 case L2CAP_EV_RECV_FBIT:
2732 /* Nothing to process */
2733 break;
2734 default:
2735 break;
2736 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002737}
2738
Gustavo Padovand6603662012-05-21 13:58:22 -03002739static void l2cap_tx_state_wait_f(struct l2cap_chan *chan,
2740 struct l2cap_ctrl *control,
2741 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002742{
Mat Martineau608bcc62012-05-17 20:53:32 -07002743 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2744 event);
2745
2746 switch (event) {
2747 case L2CAP_EV_DATA_REQUEST:
2748 if (chan->tx_send_head == NULL)
2749 chan->tx_send_head = skb_peek(skbs);
2750 /* Queue data, but don't send. */
2751 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2752 break;
2753 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2754 BT_DBG("Enter LOCAL_BUSY");
2755 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2756
2757 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2758 /* The SREJ_SENT state must be aborted if we are to
2759 * enter the LOCAL_BUSY state.
2760 */
2761 l2cap_abort_rx_srej_sent(chan);
2762 }
2763
2764 l2cap_send_ack(chan);
2765
2766 break;
2767 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2768 BT_DBG("Exit LOCAL_BUSY");
2769 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2770
2771 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2772 struct l2cap_ctrl local_control;
2773 memset(&local_control, 0, sizeof(local_control));
2774 local_control.sframe = 1;
2775 local_control.super = L2CAP_SUPER_RR;
2776 local_control.poll = 1;
2777 local_control.reqseq = chan->buffer_seq;
Mat Martineaua67d7f62012-05-17 20:53:35 -07002778 l2cap_send_sframe(chan, &local_control);
Mat Martineau608bcc62012-05-17 20:53:32 -07002779
2780 chan->retry_count = 1;
2781 __set_monitor_timer(chan);
2782 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2783 }
2784 break;
2785 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2786 l2cap_process_reqseq(chan, control->reqseq);
2787
2788 /* Fall through */
2789
2790 case L2CAP_EV_RECV_FBIT:
2791 if (control && control->final) {
2792 __clear_monitor_timer(chan);
2793 if (chan->unacked_frames > 0)
2794 __set_retrans_timer(chan);
2795 chan->retry_count = 0;
2796 chan->tx_state = L2CAP_TX_STATE_XMIT;
2797 BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state);
2798 }
2799 break;
2800 case L2CAP_EV_EXPLICIT_POLL:
2801 /* Ignore */
2802 break;
2803 case L2CAP_EV_MONITOR_TO:
2804 if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) {
2805 l2cap_send_rr_or_rnr(chan, 1);
2806 __set_monitor_timer(chan);
2807 chan->retry_count++;
2808 } else {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02002809 l2cap_send_disconn_req(chan, ECONNABORTED);
Mat Martineau608bcc62012-05-17 20:53:32 -07002810 }
2811 break;
2812 default:
2813 break;
2814 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002815}
2816
Gustavo Padovand6603662012-05-21 13:58:22 -03002817static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
2818 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002819{
Mat Martineau608bcc62012-05-17 20:53:32 -07002820 BT_DBG("chan %p, control %p, skbs %p, event %d, state %d",
2821 chan, control, skbs, event, chan->tx_state);
2822
2823 switch (chan->tx_state) {
2824 case L2CAP_TX_STATE_XMIT:
Gustavo Padovand6603662012-05-21 13:58:22 -03002825 l2cap_tx_state_xmit(chan, control, skbs, event);
Mat Martineau608bcc62012-05-17 20:53:32 -07002826 break;
2827 case L2CAP_TX_STATE_WAIT_F:
Gustavo Padovand6603662012-05-21 13:58:22 -03002828 l2cap_tx_state_wait_f(chan, control, skbs, event);
Mat Martineau608bcc62012-05-17 20:53:32 -07002829 break;
2830 default:
2831 /* Ignore event */
2832 break;
2833 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002834}
2835
Mat Martineau4b51dae92012-05-17 20:53:37 -07002836static void l2cap_pass_to_tx(struct l2cap_chan *chan,
2837 struct l2cap_ctrl *control)
2838{
2839 BT_DBG("chan %p, control %p", chan, control);
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03002840 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT);
Mat Martineau4b51dae92012-05-17 20:53:37 -07002841}
2842
Mat Martineauf80842a2012-05-17 20:53:46 -07002843static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan,
2844 struct l2cap_ctrl *control)
2845{
2846 BT_DBG("chan %p, control %p", chan, control);
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03002847 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT);
Mat Martineauf80842a2012-05-17 20:53:46 -07002848}
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850/* Copy frame to all raw sockets on that connection */
2851static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
2852{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 struct sk_buff *nskb;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03002854 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 BT_DBG("conn %p", conn);
2857
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002858 mutex_lock(&conn->chan_lock);
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -02002859
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002860 list_for_each_entry(chan, &conn->chan_l, list) {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03002861 if (chan->chan_type != L2CAP_CHAN_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 continue;
2863
Gustavo Padovan7f5396a2013-10-21 18:22:25 -02002864 /* Don't send frame to the channel it came from */
Johan Hedberga4368ff2015-03-30 23:21:01 +03002865 if (bt_cb(skb)->l2cap.chan == chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 continue;
Gustavo Padovan7f5396a2013-10-21 18:22:25 -02002867
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002868 nskb = skb_clone(skb, GFP_KERNEL);
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03002869 if (!nskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 continue;
Gustavo Padovan80b98022012-05-27 22:27:51 -03002871 if (chan->ops->recv(chan, nskb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 kfree_skb(nskb);
2873 }
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -02002874
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002875 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876}
2877
2878/* ---- L2CAP signalling commands ---- */
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002879static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code,
2880 u8 ident, u16 dlen, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
2882 struct sk_buff *skb, **frag;
2883 struct l2cap_cmd_hdr *cmd;
2884 struct l2cap_hdr *lh;
2885 int len, count;
2886
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002887 BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u",
2888 conn, code, ident, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Anderson Lizardo300b9622013-06-02 16:30:40 -04002890 if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE)
2891 return NULL;
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
2894 count = min_t(unsigned int, conn->mtu, len);
2895
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002896 skb = bt_skb_alloc(count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (!skb)
2898 return NULL;
2899
2900 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002901 lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02002902
2903 if (conn->hcon->type == LE_LINK)
Joe Perchesdcf4adb2014-03-12 10:52:35 -07002904 lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02002905 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -07002906 lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
2909 cmd->code = code;
2910 cmd->ident = ident;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002911 cmd->len = cpu_to_le16(dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
2913 if (dlen) {
2914 count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
2915 memcpy(skb_put(skb, count), data, count);
2916 data += count;
2917 }
2918
2919 len -= skb->len;
2920
2921 /* Continuation fragments (no L2CAP header) */
2922 frag = &skb_shinfo(skb)->frag_list;
2923 while (len) {
2924 count = min_t(unsigned int, conn->mtu, len);
2925
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002926 *frag = bt_skb_alloc(count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 if (!*frag)
2928 goto fail;
2929
2930 memcpy(skb_put(*frag, count), data, count);
2931
2932 len -= count;
2933 data += count;
2934
2935 frag = &(*frag)->next;
2936 }
2937
2938 return skb;
2939
2940fail:
2941 kfree_skb(skb);
2942 return NULL;
2943}
2944
Gustavo Padovan2d792812012-10-06 10:07:01 +01002945static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen,
2946 unsigned long *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
2948 struct l2cap_conf_opt *opt = *ptr;
2949 int len;
2950
2951 len = L2CAP_CONF_OPT_SIZE + opt->len;
2952 *ptr += len;
2953
2954 *type = opt->type;
2955 *olen = opt->len;
2956
2957 switch (opt->len) {
2958 case 1:
2959 *val = *((u8 *) opt->val);
2960 break;
2961
2962 case 2:
steven miaobfaaeb32010-10-16 18:29:47 -04002963 *val = get_unaligned_le16(opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 break;
2965
2966 case 4:
steven miaobfaaeb32010-10-16 18:29:47 -04002967 *val = get_unaligned_le32(opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 break;
2969
2970 default:
2971 *val = (unsigned long) opt->val;
2972 break;
2973 }
2974
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002975 BT_DBG("type 0x%2.2x len %u val 0x%lx", *type, opt->len, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return len;
2977}
2978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
2980{
2981 struct l2cap_conf_opt *opt = *ptr;
2982
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002983 BT_DBG("type 0x%2.2x len %u val 0x%lx", type, len, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
2985 opt->type = type;
2986 opt->len = len;
2987
2988 switch (len) {
2989 case 1:
2990 *((u8 *) opt->val) = val;
2991 break;
2992
2993 case 2:
Gustavo F. Padovan4f8b6912010-10-18 14:25:53 -02002994 put_unaligned_le16(val, opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 break;
2996
2997 case 4:
Gustavo F. Padovan4f8b6912010-10-18 14:25:53 -02002998 put_unaligned_le32(val, opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 break;
3000
3001 default:
3002 memcpy(opt->val, (void *) val, len);
3003 break;
3004 }
3005
3006 *ptr += L2CAP_CONF_OPT_SIZE + len;
3007}
3008
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003009static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan)
3010{
3011 struct l2cap_conf_efs efs;
3012
Szymon Janc1ec918c2011-11-16 09:32:21 +01003013 switch (chan->mode) {
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003014 case L2CAP_MODE_ERTM:
3015 efs.id = chan->local_id;
3016 efs.stype = chan->local_stype;
3017 efs.msdu = cpu_to_le16(chan->local_msdu);
3018 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003019 efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT);
3020 efs.flush_to = cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003021 break;
3022
3023 case L2CAP_MODE_STREAMING:
3024 efs.id = 1;
3025 efs.stype = L2CAP_SERV_BESTEFFORT;
3026 efs.msdu = cpu_to_le16(chan->local_msdu);
3027 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
3028 efs.acc_lat = 0;
3029 efs.flush_to = 0;
3030 break;
3031
3032 default:
3033 return;
3034 }
3035
3036 l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs),
Andrei Emeltchenko8936fa62012-10-08 11:14:41 +03003037 (unsigned long) &efs);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003038}
3039
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003040static void l2cap_ack_timeout(struct work_struct *work)
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003041{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003042 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau03625202012-05-17 20:53:51 -07003043 ack_timer.work);
3044 u16 frames_to_ack;
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003045
Gustavo F. Padovan2fb9b3d2011-12-22 16:56:05 -02003046 BT_DBG("chan %p", chan);
3047
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003048 l2cap_chan_lock(chan);
3049
Mat Martineau03625202012-05-17 20:53:51 -07003050 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
3051 chan->last_acked_seq);
3052
3053 if (frames_to_ack)
3054 l2cap_send_rr_or_rnr(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003055
3056 l2cap_chan_unlock(chan);
Szymon Janc09bfb2e2012-01-11 10:59:49 +01003057 l2cap_chan_put(chan);
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003058}
3059
Andrei Emeltchenko466f8002012-05-29 13:59:01 +03003060int l2cap_ertm_init(struct l2cap_chan *chan)
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003061{
Mat Martineau3c588192012-04-11 10:48:42 -07003062 int err;
3063
Mat Martineau105bdf92012-04-27 16:50:48 -07003064 chan->next_tx_seq = 0;
3065 chan->expected_tx_seq = 0;
Gustavo F. Padovan42e5c802011-03-25 19:58:34 -03003066 chan->expected_ack_seq = 0;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03003067 chan->unacked_frames = 0;
Gustavo F. Padovan42e5c802011-03-25 19:58:34 -03003068 chan->buffer_seq = 0;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03003069 chan->frames_sent = 0;
Mat Martineau105bdf92012-04-27 16:50:48 -07003070 chan->last_acked_seq = 0;
3071 chan->sdu = NULL;
3072 chan->sdu_last_frag = NULL;
3073 chan->sdu_len = 0;
3074
Mat Martineaud34c34f2012-05-14 14:49:27 -07003075 skb_queue_head_init(&chan->tx_q);
3076
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003077 chan->local_amp_id = AMP_ID_BREDR;
3078 chan->move_id = AMP_ID_BREDR;
Mat Martineau08333282012-10-23 15:24:06 -07003079 chan->move_state = L2CAP_MOVE_STABLE;
3080 chan->move_role = L2CAP_MOVE_ROLE_NONE;
3081
Mat Martineau105bdf92012-04-27 16:50:48 -07003082 if (chan->mode != L2CAP_MODE_ERTM)
3083 return 0;
3084
3085 chan->rx_state = L2CAP_RX_STATE_RECV;
3086 chan->tx_state = L2CAP_TX_STATE_XMIT;
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003087
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003088 INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout);
3089 INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout);
3090 INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003091
Gustavo F. Padovanf1c67752011-03-25 20:36:10 -03003092 skb_queue_head_init(&chan->srej_q);
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03003093
Mat Martineau3c588192012-04-11 10:48:42 -07003094 err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win);
3095 if (err < 0)
3096 return err;
3097
Mat Martineau9dc9aff2012-05-17 16:20:14 -07003098 err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win);
3099 if (err < 0)
3100 l2cap_seq_list_free(&chan->srej_list);
3101
3102 return err;
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003103}
3104
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003105static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
3106{
3107 switch (mode) {
3108 case L2CAP_MODE_STREAMING:
3109 case L2CAP_MODE_ERTM:
3110 if (l2cap_mode_supported(mode, remote_feat_mask))
3111 return mode;
3112 /* fall through */
3113 default:
3114 return L2CAP_MODE_BASIC;
3115 }
3116}
3117
Marcel Holtmann848566b2013-10-01 22:59:22 -07003118static inline bool __l2cap_ews_supported(struct l2cap_conn *conn)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003119{
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02003120 return ((conn->local_fixed_chan & L2CAP_FC_A2MP) &&
3121 (conn->feat_mask & L2CAP_FEAT_EXT_WINDOW));
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003122}
3123
Marcel Holtmann848566b2013-10-01 22:59:22 -07003124static inline bool __l2cap_efs_supported(struct l2cap_conn *conn)
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003125{
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02003126 return ((conn->local_fixed_chan & L2CAP_FC_A2MP) &&
3127 (conn->feat_mask & L2CAP_FEAT_EXT_FLOW));
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003128}
3129
Mat Martineau36c86c82012-10-23 15:24:20 -07003130static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan,
3131 struct l2cap_conf_rfc *rfc)
3132{
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003133 if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) {
Mat Martineau36c86c82012-10-23 15:24:20 -07003134 u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to;
3135
3136 /* Class 1 devices have must have ERTM timeouts
3137 * exceeding the Link Supervision Timeout. The
3138 * default Link Supervision Timeout for AMP
3139 * controllers is 10 seconds.
3140 *
3141 * Class 1 devices use 0xffffffff for their
3142 * best-effort flush timeout, so the clamping logic
3143 * will result in a timeout that meets the above
3144 * requirement. ERTM timeouts are 16-bit values, so
3145 * the maximum timeout is 65.535 seconds.
3146 */
3147
3148 /* Convert timeout to milliseconds and round */
3149 ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000);
3150
3151 /* This is the recommended formula for class 2 devices
3152 * that start ERTM timers when packets are sent to the
3153 * controller.
3154 */
3155 ertm_to = 3 * ertm_to + 500;
3156
3157 if (ertm_to > 0xffff)
3158 ertm_to = 0xffff;
3159
3160 rfc->retrans_timeout = cpu_to_le16((u16) ertm_to);
3161 rfc->monitor_timeout = rfc->retrans_timeout;
3162 } else {
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003163 rfc->retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
3164 rfc->monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
Mat Martineau36c86c82012-10-23 15:24:20 -07003165 }
3166}
3167
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003168static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
3169{
3170 if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW &&
Marcel Holtmann848566b2013-10-01 22:59:22 -07003171 __l2cap_ews_supported(chan->conn)) {
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003172 /* use extended control field */
3173 set_bit(FLAG_EXT_CTRL, &chan->flags);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003174 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
3175 } else {
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003176 chan->tx_win = min_t(u16, chan->tx_win,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003177 L2CAP_DEFAULT_TX_WINDOW);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003178 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
3179 }
Mat Martineauc20f8e32012-07-10 05:47:07 -07003180 chan->ack_win = chan->tx_win;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003181}
3182
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003183static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 struct l2cap_conf_req *req = data;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003186 struct l2cap_conf_rfc rfc = { .mode = chan->mode };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 void *ptr = req->data;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003188 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03003190 BT_DBG("chan %p", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003192 if (chan->num_conf_req || chan->num_conf_rsp)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003193 goto done;
3194
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003195 switch (chan->mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003196 case L2CAP_MODE_STREAMING:
3197 case L2CAP_MODE_ERTM:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003198 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003199 break;
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003200
Marcel Holtmann848566b2013-10-01 22:59:22 -07003201 if (__l2cap_efs_supported(chan->conn))
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003202 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3203
Gustavo F. Padovan2ba13ed2010-06-09 16:39:05 -03003204 /* fall through */
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003205 default:
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003206 chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003207 break;
3208 }
3209
3210done:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003211 if (chan->imtu != L2CAP_DEFAULT_MTU)
3212 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
Gustavo F. Padovan7990681c2011-01-24 16:01:43 -02003213
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003214 switch (chan->mode) {
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003215 case L2CAP_MODE_BASIC:
Marcel Holtmann6fea7ad2014-07-09 11:53:35 +02003216 if (disable_ertm)
3217 break;
3218
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003219 if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003220 !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003221 break;
3222
Gustavo F. Padovan62547752010-06-08 20:05:31 -03003223 rfc.mode = L2CAP_MODE_BASIC;
3224 rfc.txwin_size = 0;
3225 rfc.max_transmit = 0;
3226 rfc.retrans_timeout = 0;
3227 rfc.monitor_timeout = 0;
3228 rfc.max_pdu_size = 0;
3229
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003230 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003231 (unsigned long) &rfc);
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003232 break;
3233
3234 case L2CAP_MODE_ERTM:
3235 rfc.mode = L2CAP_MODE_ERTM;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003236 rfc.max_transmit = chan->max_tx;
Mat Martineau36c86c82012-10-23 15:24:20 -07003237
3238 __l2cap_set_ertm_timeouts(chan, &rfc);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003239
3240 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
Gustavo Padovan2d792812012-10-06 10:07:01 +01003241 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3242 L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003243 rfc.max_pdu_size = cpu_to_le16(size);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003244
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003245 l2cap_txwin_setup(chan);
3246
3247 rfc.txwin_size = min_t(u16, chan->tx_win,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003248 L2CAP_DEFAULT_TX_WINDOW);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003249
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003250 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003251 (unsigned long) &rfc);
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003252
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003253 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
3254 l2cap_add_opt_efs(&ptr, chan);
3255
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003256 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3257 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003258 chan->tx_win);
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003259
3260 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3261 if (chan->fcs == L2CAP_FCS_NONE ||
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003262 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003263 chan->fcs = L2CAP_FCS_NONE;
3264 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
3265 chan->fcs);
3266 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003267 break;
3268
3269 case L2CAP_MODE_STREAMING:
Mat Martineau273759e2012-05-17 20:53:53 -07003270 l2cap_txwin_setup(chan);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003271 rfc.mode = L2CAP_MODE_STREAMING;
3272 rfc.txwin_size = 0;
3273 rfc.max_transmit = 0;
3274 rfc.retrans_timeout = 0;
3275 rfc.monitor_timeout = 0;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003276
3277 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
Gustavo Padovan2d792812012-10-06 10:07:01 +01003278 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3279 L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003280 rfc.max_pdu_size = cpu_to_le16(size);
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003281
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003282 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003283 (unsigned long) &rfc);
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003284
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003285 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
3286 l2cap_add_opt_efs(&ptr, chan);
3287
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003288 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3289 if (chan->fcs == L2CAP_FCS_NONE ||
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003290 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003291 chan->fcs = L2CAP_FCS_NONE;
3292 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
3293 chan->fcs);
3294 }
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003295 break;
3296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003298 req->dcid = cpu_to_le16(chan->dcid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003299 req->flags = cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
3301 return ptr - data;
3302}
3303
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003304static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003306 struct l2cap_conf_rsp *rsp = data;
3307 void *ptr = rsp->data;
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003308 void *req = chan->conf_req;
3309 int len = chan->conf_len;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003310 int type, hint, olen;
3311 unsigned long val;
Marcel Holtmann6464f352007-10-20 13:39:51 +02003312 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003313 struct l2cap_conf_efs efs;
3314 u8 remote_efs = 0;
Marcel Holtmann861d6882007-10-20 13:37:06 +02003315 u16 mtu = L2CAP_DEFAULT_MTU;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003316 u16 result = L2CAP_CONF_SUCCESS;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003317 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003319 BT_DBG("chan %p", chan);
Marcel Holtmann820ae1b2006-11-18 22:15:00 +01003320
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003321 while (len >= L2CAP_CONF_OPT_SIZE) {
3322 len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
Gustavo F. Padovan589d2742009-04-20 01:31:07 -03003324 hint = type & L2CAP_CONF_HINT;
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -07003325 type &= L2CAP_CONF_MASK;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003326
3327 switch (type) {
3328 case L2CAP_CONF_MTU:
Marcel Holtmann861d6882007-10-20 13:37:06 +02003329 mtu = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003330 break;
3331
3332 case L2CAP_CONF_FLUSH_TO:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003333 chan->flush_to = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003334 break;
3335
3336 case L2CAP_CONF_QOS:
3337 break;
3338
Marcel Holtmann6464f352007-10-20 13:39:51 +02003339 case L2CAP_CONF_RFC:
3340 if (olen == sizeof(rfc))
3341 memcpy(&rfc, (void *) val, olen);
3342 break;
3343
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003344 case L2CAP_CONF_FCS:
3345 if (val == L2CAP_FCS_NONE)
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003346 set_bit(CONF_RECV_NO_FCS, &chan->conf_state);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003347 break;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003348
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003349 case L2CAP_CONF_EFS:
3350 remote_efs = 1;
3351 if (olen == sizeof(efs))
3352 memcpy(&efs, (void *) val, olen);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003353 break;
3354
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003355 case L2CAP_CONF_EWS:
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02003356 if (!(chan->conn->local_fixed_chan & L2CAP_FC_A2MP))
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003357 return -ECONNREFUSED;
3358
3359 set_bit(FLAG_EXT_CTRL, &chan->flags);
3360 set_bit(CONF_EWS_RECV, &chan->conf_state);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003361 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003362 chan->remote_tx_win = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003363 break;
3364
3365 default:
3366 if (hint)
3367 break;
3368
3369 result = L2CAP_CONF_UNKNOWN;
3370 *((u8 *) ptr++) = type;
3371 break;
3372 }
3373 }
3374
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003375 if (chan->num_conf_rsp || chan->num_conf_req > 1)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003376 goto done;
3377
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003378 switch (chan->mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003379 case L2CAP_MODE_STREAMING:
3380 case L2CAP_MODE_ERTM:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003381 if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003382 chan->mode = l2cap_select_mode(rfc.mode,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003383 chan->conn->feat_mask);
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003384 break;
3385 }
3386
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003387 if (remote_efs) {
Marcel Holtmann848566b2013-10-01 22:59:22 -07003388 if (__l2cap_efs_supported(chan->conn))
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003389 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3390 else
3391 return -ECONNREFUSED;
3392 }
3393
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003394 if (chan->mode != rfc.mode)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003395 return -ECONNREFUSED;
Gustavo F. Padovan742e5192010-06-08 19:09:48 -03003396
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003397 break;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003398 }
3399
3400done:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003401 if (chan->mode != rfc.mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003402 result = L2CAP_CONF_UNACCEPT;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003403 rfc.mode = chan->mode;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003404
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003405 if (chan->num_conf_rsp == 1)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003406 return -ECONNREFUSED;
3407
Gustavo Padovan2d792812012-10-06 10:07:01 +01003408 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3409 (unsigned long) &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003410 }
3411
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003412 if (result == L2CAP_CONF_SUCCESS) {
3413 /* Configure output options and let the other side know
3414 * which ones we don't like. */
3415
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003416 if (mtu < L2CAP_DEFAULT_MIN_MTU)
3417 result = L2CAP_CONF_UNACCEPT;
3418 else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003419 chan->omtu = mtu;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003420 set_bit(CONF_MTU_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003421 }
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003422 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003423
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003424 if (remote_efs) {
3425 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003426 efs.stype != L2CAP_SERV_NOTRAFIC &&
3427 efs.stype != chan->local_stype) {
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003428
3429 result = L2CAP_CONF_UNACCEPT;
3430
3431 if (chan->num_conf_req >= 1)
3432 return -ECONNREFUSED;
3433
3434 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003435 sizeof(efs),
3436 (unsigned long) &efs);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003437 } else {
Gustavo F. Padovan3e6b3b92011-11-01 14:06:23 -02003438 /* Send PENDING Conf Rsp */
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003439 result = L2CAP_CONF_PENDING;
3440 set_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003441 }
3442 }
3443
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003444 switch (rfc.mode) {
3445 case L2CAP_MODE_BASIC:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003446 chan->fcs = L2CAP_FCS_NONE;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003447 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003448 break;
3449
3450 case L2CAP_MODE_ERTM:
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003451 if (!test_bit(CONF_EWS_RECV, &chan->conf_state))
3452 chan->remote_tx_win = rfc.txwin_size;
3453 else
3454 rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW;
3455
Gustavo F. Padovan2c03a7a2011-03-25 20:15:28 -03003456 chan->remote_max_tx = rfc.max_transmit;
Mat Martineau86b1b262010-08-05 15:54:22 -07003457
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003458 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003459 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3460 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003461 rfc.max_pdu_size = cpu_to_le16(size);
3462 chan->remote_mps = size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003463
Mat Martineau36c86c82012-10-23 15:24:20 -07003464 __l2cap_set_ertm_timeouts(chan, &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003465
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003466 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003467
3468 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003469 sizeof(rfc), (unsigned long) &rfc);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003470
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003471 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3472 chan->remote_id = efs.id;
3473 chan->remote_stype = efs.stype;
3474 chan->remote_msdu = le16_to_cpu(efs.msdu);
3475 chan->remote_flush_to =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003476 le32_to_cpu(efs.flush_to);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003477 chan->remote_acc_lat =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003478 le32_to_cpu(efs.acc_lat);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003479 chan->remote_sdu_itime =
3480 le32_to_cpu(efs.sdu_itime);
3481 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003482 sizeof(efs),
3483 (unsigned long) &efs);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003484 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003485 break;
3486
3487 case L2CAP_MODE_STREAMING:
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003488 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003489 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3490 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003491 rfc.max_pdu_size = cpu_to_le16(size);
3492 chan->remote_mps = size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003493
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003494 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003495
Gustavo Padovan2d792812012-10-06 10:07:01 +01003496 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3497 (unsigned long) &rfc);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003498
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003499 break;
3500
3501 default:
Marcel Holtmann6464f352007-10-20 13:39:51 +02003502 result = L2CAP_CONF_UNACCEPT;
3503
3504 memset(&rfc, 0, sizeof(rfc));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003505 rfc.mode = chan->mode;
Marcel Holtmann6464f352007-10-20 13:39:51 +02003506 }
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003507
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003508 if (result == L2CAP_CONF_SUCCESS)
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003509 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003510 }
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003511 rsp->scid = cpu_to_le16(chan->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003512 rsp->result = cpu_to_le16(result);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003513 rsp->flags = cpu_to_le16(0);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003514
3515 return ptr - data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516}
3517
Gustavo Padovan2d792812012-10-06 10:07:01 +01003518static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
3519 void *data, u16 *result)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003520{
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003521 struct l2cap_conf_req *req = data;
3522 void *ptr = req->data;
3523 int type, olen;
3524 unsigned long val;
Mat Martineau36e999a2011-12-08 17:23:21 -08003525 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003526 struct l2cap_conf_efs efs;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003527
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003528 BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003529
3530 while (len >= L2CAP_CONF_OPT_SIZE) {
3531 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3532
3533 switch (type) {
3534 case L2CAP_CONF_MTU:
3535 if (val < L2CAP_DEFAULT_MIN_MTU) {
3536 *result = L2CAP_CONF_UNACCEPT;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003537 chan->imtu = L2CAP_DEFAULT_MIN_MTU;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003538 } else
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003539 chan->imtu = val;
3540 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003541 break;
3542
3543 case L2CAP_CONF_FLUSH_TO:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003544 chan->flush_to = val;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003545 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003546 2, chan->flush_to);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003547 break;
3548
3549 case L2CAP_CONF_RFC:
3550 if (olen == sizeof(rfc))
3551 memcpy(&rfc, (void *)val, olen);
3552
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003553 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003554 rfc.mode != chan->mode)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003555 return -ECONNREFUSED;
3556
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003557 chan->fcs = 0;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003558
3559 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003560 sizeof(rfc), (unsigned long) &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003561 break;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003562
3563 case L2CAP_CONF_EWS:
Mat Martineauc20f8e32012-07-10 05:47:07 -07003564 chan->ack_win = min_t(u16, val, chan->ack_win);
Gustavo F. Padovan3e6b3b92011-11-01 14:06:23 -02003565 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
Mat Martineauc20f8e32012-07-10 05:47:07 -07003566 chan->tx_win);
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003567 break;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003568
3569 case L2CAP_CONF_EFS:
3570 if (olen == sizeof(efs))
3571 memcpy(&efs, (void *)val, olen);
3572
3573 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003574 efs.stype != L2CAP_SERV_NOTRAFIC &&
3575 efs.stype != chan->local_stype)
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003576 return -ECONNREFUSED;
3577
Gustavo Padovan2d792812012-10-06 10:07:01 +01003578 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs),
3579 (unsigned long) &efs);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003580 break;
Andrei Emeltchenkocbabee72012-11-29 17:46:07 +02003581
3582 case L2CAP_CONF_FCS:
3583 if (*result == L2CAP_CONF_PENDING)
3584 if (val == L2CAP_FCS_NONE)
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003585 set_bit(CONF_RECV_NO_FCS,
Andrei Emeltchenkocbabee72012-11-29 17:46:07 +02003586 &chan->conf_state);
3587 break;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003588 }
3589 }
3590
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003591 if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
Gustavo F. Padovan6c2ea7a2010-06-08 20:08:49 -03003592 return -ECONNREFUSED;
3593
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003594 chan->mode = rfc.mode;
Gustavo F. Padovan6c2ea7a2010-06-08 20:08:49 -03003595
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003596 if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003597 switch (rfc.mode) {
3598 case L2CAP_MODE_ERTM:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003599 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3600 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
3601 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Mat Martineauc20f8e32012-07-10 05:47:07 -07003602 if (!test_bit(FLAG_EXT_CTRL, &chan->flags))
3603 chan->ack_win = min_t(u16, chan->ack_win,
3604 rfc.txwin_size);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003605
3606 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3607 chan->local_msdu = le16_to_cpu(efs.msdu);
3608 chan->local_sdu_itime =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003609 le32_to_cpu(efs.sdu_itime);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003610 chan->local_acc_lat = le32_to_cpu(efs.acc_lat);
3611 chan->local_flush_to =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003612 le32_to_cpu(efs.flush_to);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003613 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003614 break;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003615
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003616 case L2CAP_MODE_STREAMING:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003617 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003618 }
3619 }
3620
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003621 req->dcid = cpu_to_le16(chan->dcid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003622 req->flags = cpu_to_le16(0);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003623
3624 return ptr - data;
3625}
3626
Gustavo Padovan2d792812012-10-06 10:07:01 +01003627static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data,
3628 u16 result, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
3630 struct l2cap_conf_rsp *rsp = data;
3631 void *ptr = rsp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003633 BT_DBG("chan %p", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003635 rsp->scid = cpu_to_le16(chan->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003636 rsp->result = cpu_to_le16(result);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07003637 rsp->flags = cpu_to_le16(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
3639 return ptr - data;
3640}
3641
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03003642void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan)
3643{
3644 struct l2cap_le_conn_rsp rsp;
3645 struct l2cap_conn *conn = chan->conn;
3646
3647 BT_DBG("chan %p", chan);
3648
3649 rsp.dcid = cpu_to_le16(chan->scid);
3650 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02003651 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03003652 rsp.credits = cpu_to_le16(chan->rx_credits);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003653 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03003654
3655 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp),
3656 &rsp);
3657}
3658
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003659void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003660{
3661 struct l2cap_conn_rsp rsp;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003662 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003663 u8 buf[128];
Andrei Emeltchenko439f34a2012-10-31 15:46:28 +02003664 u8 rsp_code;
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003665
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003666 rsp.scid = cpu_to_le16(chan->dcid);
3667 rsp.dcid = cpu_to_le16(chan->scid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003668 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
3669 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Andrei Emeltchenko439f34a2012-10-31 15:46:28 +02003670
3671 if (chan->hs_hcon)
3672 rsp_code = L2CAP_CREATE_CHAN_RSP;
3673 else
3674 rsp_code = L2CAP_CONN_RSP;
3675
3676 BT_DBG("chan %p rsp_code %u", chan, rsp_code);
3677
3678 l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp);
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003679
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003680 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003681 return;
3682
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003683 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003684 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan710f9b02011-03-25 14:30:37 -03003685 chan->num_conf_req++;
3686}
3687
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003688static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003689{
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003690 int type, olen;
3691 unsigned long val;
Mat Martineauc20f8e32012-07-10 05:47:07 -07003692 /* Use sane default values in case a misbehaving remote device
3693 * did not send an RFC or extended window size option.
3694 */
3695 u16 txwin_ext = chan->ack_win;
3696 struct l2cap_conf_rfc rfc = {
3697 .mode = chan->mode,
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003698 .retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO),
3699 .monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO),
Mat Martineauc20f8e32012-07-10 05:47:07 -07003700 .max_pdu_size = cpu_to_le16(chan->imtu),
3701 .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW),
3702 };
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003703
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003704 BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003705
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003706 if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003707 return;
3708
3709 while (len >= L2CAP_CONF_OPT_SIZE) {
3710 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3711
Mat Martineauc20f8e32012-07-10 05:47:07 -07003712 switch (type) {
3713 case L2CAP_CONF_RFC:
3714 if (olen == sizeof(rfc))
3715 memcpy(&rfc, (void *)val, olen);
Szymon Janc8f321f82012-06-08 11:33:33 +02003716 break;
Mat Martineauc20f8e32012-07-10 05:47:07 -07003717 case L2CAP_CONF_EWS:
3718 txwin_ext = val;
3719 break;
3720 }
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003721 }
3722
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003723 switch (rfc.mode) {
3724 case L2CAP_MODE_ERTM:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003725 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3726 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
Mat Martineauc20f8e32012-07-10 05:47:07 -07003727 chan->mps = le16_to_cpu(rfc.max_pdu_size);
3728 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3729 chan->ack_win = min_t(u16, chan->ack_win, txwin_ext);
3730 else
3731 chan->ack_win = min_t(u16, chan->ack_win,
3732 rfc.txwin_size);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003733 break;
3734 case L2CAP_MODE_STREAMING:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003735 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003736 }
3737}
3738
Gustavo Padovan2d792812012-10-06 10:07:01 +01003739static inline int l2cap_command_rej(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003740 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3741 u8 *data)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003742{
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003743 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003744
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003745 if (cmd_len < sizeof(*rej))
3746 return -EPROTO;
3747
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003748 if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003749 return 0;
3750
3751 if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003752 cmd->ident == conn->info_ident) {
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02003753 cancel_delayed_work(&conn->info_timer);
Marcel Holtmann984947d2009-02-06 23:35:19 +01003754
3755 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01003756 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01003757
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003758 l2cap_conn_start(conn);
3759 }
3760
3761 return 0;
3762}
3763
Mat Martineau17009152012-10-23 15:24:07 -07003764static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3765 struct l2cap_cmd_hdr *cmd,
3766 u8 *data, u8 rsp_code, u8 amp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
3769 struct l2cap_conn_rsp rsp;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003770 struct l2cap_chan *chan = NULL, *pchan;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003771 int result, status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
3773 u16 dcid = 0, scid = __le16_to_cpu(req->scid);
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003774 __le16 psm = req->psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Andrei Emeltchenko097db762012-03-09 14:16:17 +02003776 BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
3778 /* Check if we have socket listening on psm */
Marcel Holtmann6f59b902013-10-13 05:24:01 -07003779 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
Johan Hedbergbf20fd42013-05-14 13:23:13 +03003780 &conn->hcon->dst, ACL_LINK);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003781 if (!pchan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 result = L2CAP_CR_BAD_PSM;
3783 goto sendresp;
3784 }
3785
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003786 mutex_lock(&conn->chan_lock);
Gustavo Padovan8ffb9292013-10-21 14:21:41 -02003787 l2cap_chan_lock(pchan);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00003788
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003789 /* Check if the ACL is secure enough (if not SDP) */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003790 if (psm != cpu_to_le16(L2CAP_PSM_SDP) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003791 !hci_conn_check_link_mode(conn->hcon)) {
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02003792 conn->disc_reason = HCI_ERROR_AUTH_FAILURE;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003793 result = L2CAP_CR_SEC_BLOCK;
3794 goto response;
3795 }
3796
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 result = L2CAP_CR_NO_MEM;
3798
Gustavo Padovan2dfa1002012-05-27 22:27:58 -03003799 /* Check if we already have channel with that dcid */
3800 if (__l2cap_get_chan_by_dcid(conn, scid))
3801 goto response;
3802
Gustavo Padovan80b98022012-05-27 22:27:51 -03003803 chan = pchan->ops->new_connection(pchan);
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03003804 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 goto response;
3806
Syam Sidhardhan330b6c12013-08-06 01:59:12 +09003807 /* For certain devices (ex: HID mouse), support for authentication,
3808 * pairing and bonding is optional. For such devices, inorder to avoid
3809 * the ACL alive for too long after L2CAP disconnection, reset the ACL
3810 * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect.
3811 */
3812 conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
3813
Marcel Holtmann7eafc592013-10-13 08:12:47 -07003814 bacpy(&chan->src, &conn->hcon->src);
3815 bacpy(&chan->dst, &conn->hcon->dst);
Johan Hedberga250e042015-01-15 13:06:44 +02003816 chan->src_type = bdaddr_src_type(conn->hcon);
3817 chan->dst_type = bdaddr_dst_type(conn->hcon);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003818 chan->psm = psm;
3819 chan->dcid = scid;
Mat Martineau17009152012-10-23 15:24:07 -07003820 chan->local_amp_id = amp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003822 __l2cap_chan_add(conn, chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003823
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003824 dcid = chan->scid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
Gustavo Padovan8d836d72013-10-15 19:24:47 -03003826 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03003828 chan->ident = cmd->ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Marcel Holtmann984947d2009-02-06 23:35:19 +01003830 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
Johan Hedberge7cafc42014-07-17 15:35:38 +03003831 if (l2cap_chan_check_security(chan, false)) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07003832 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003833 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003834 result = L2CAP_CR_PEND;
3835 status = L2CAP_CS_AUTHOR_PEND;
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08003836 chan->ops->defer(chan);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003837 } else {
Mat Martineau17009152012-10-23 15:24:07 -07003838 /* Force pending result for AMP controllers.
3839 * The connection will succeed after the
3840 * physical link is up.
3841 */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003842 if (amp_id == AMP_ID_BREDR) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003843 l2cap_state_change(chan, BT_CONFIG);
Mat Martineau17009152012-10-23 15:24:07 -07003844 result = L2CAP_CR_SUCCESS;
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003845 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003846 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07003847 result = L2CAP_CR_PEND;
Mat Martineau17009152012-10-23 15:24:07 -07003848 }
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003849 status = L2CAP_CS_NO_INFO;
3850 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003851 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003852 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003853 result = L2CAP_CR_PEND;
3854 status = L2CAP_CS_AUTHEN_PEND;
3855 }
3856 } else {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02003857 l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003858 result = L2CAP_CR_PEND;
3859 status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 }
3861
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862response:
Gustavo Padovan8ffb9292013-10-21 14:21:41 -02003863 l2cap_chan_unlock(pchan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003864 mutex_unlock(&conn->chan_lock);
Johan Hedberga24cce12014-08-07 22:56:42 +03003865 l2cap_chan_put(pchan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
3867sendresp:
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07003868 rsp.scid = cpu_to_le16(scid);
3869 rsp.dcid = cpu_to_le16(dcid);
3870 rsp.result = cpu_to_le16(result);
3871 rsp.status = cpu_to_le16(status);
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003872 l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003873
3874 if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
3875 struct l2cap_info_req info;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07003876 info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003877
3878 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
3879 conn->info_ident = l2cap_get_ident(conn);
3880
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08003881 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003882
Gustavo Padovan2d792812012-10-06 10:07:01 +01003883 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
3884 sizeof(info), &info);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003885 }
3886
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003887 if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003888 result == L2CAP_CR_SUCCESS) {
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003889 u8 buf[128];
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003890 set_bit(CONF_REQ_SENT, &chan->conf_state);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003891 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003892 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003893 chan->num_conf_req++;
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003894 }
Mat Martineau17009152012-10-23 15:24:07 -07003895
3896 return chan;
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003897}
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003898
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003899static int l2cap_connect_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003900 struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003901{
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303902 struct hci_dev *hdev = conn->hcon->hdev;
3903 struct hci_conn *hcon = conn->hcon;
3904
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003905 if (cmd_len < sizeof(struct l2cap_conn_req))
3906 return -EPROTO;
3907
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303908 hci_dev_lock(hdev);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003909 if (hci_dev_test_flag(hdev, HCI_MGMT) &&
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303910 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00003911 mgmt_device_connected(hdev, hcon, 0, NULL, 0);
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303912 hci_dev_unlock(hdev);
3913
Gustavo Padovan300229f2012-10-12 19:40:40 +08003914 l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 return 0;
3916}
3917
Mat Martineau5909cf32012-10-23 15:24:08 -07003918static int l2cap_connect_create_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003919 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3920 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921{
3922 struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
3923 u16 scid, dcid, result, status;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003924 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 u8 req[128];
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003926 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003928 if (cmd_len < sizeof(*rsp))
3929 return -EPROTO;
3930
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 scid = __le16_to_cpu(rsp->scid);
3932 dcid = __le16_to_cpu(rsp->dcid);
3933 result = __le16_to_cpu(rsp->result);
3934 status = __le16_to_cpu(rsp->status);
3935
Andrei Emeltchenko1b009c92012-02-21 12:54:54 +02003936 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 +01003937 dcid, scid, result, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003939 mutex_lock(&conn->chan_lock);
3940
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 if (scid) {
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003942 chan = __l2cap_get_chan_by_scid(conn, scid);
3943 if (!chan) {
Johan Hedberg21870b52013-09-16 13:05:14 +03003944 err = -EBADSLT;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003945 goto unlock;
3946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 } else {
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003948 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
3949 if (!chan) {
Johan Hedberg21870b52013-09-16 13:05:14 +03003950 err = -EBADSLT;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003951 goto unlock;
3952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 }
3954
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003955 err = 0;
3956
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003957 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003958
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 switch (result) {
3960 case L2CAP_CR_SUCCESS:
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03003961 l2cap_state_change(chan, BT_CONFIG);
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03003962 chan->ident = 0;
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003963 chan->dcid = dcid;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003964 clear_bit(CONF_CONNECT_PEND, &chan->conf_state);
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01003965
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003966 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003967 break;
3968
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003970 l2cap_build_conf_req(chan, req), req);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003971 chan->num_conf_req++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 break;
3973
3974 case L2CAP_CR_PEND:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003975 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 break;
3977
3978 default:
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003979 l2cap_chan_del(chan, ECONNREFUSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 break;
3981 }
3982
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003983 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003984
3985unlock:
3986 mutex_unlock(&conn->chan_lock);
3987
3988 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989}
3990
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003991static inline void set_default_fcs(struct l2cap_chan *chan)
Mat Martineau8c462b62010-08-24 15:35:42 -07003992{
3993 /* FCS is enabled only in ERTM or streaming mode, if one or both
3994 * sides request it.
3995 */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003996 if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003997 chan->fcs = L2CAP_FCS_NONE;
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003998 else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state))
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003999 chan->fcs = L2CAP_FCS_CRC16;
Mat Martineau8c462b62010-08-24 15:35:42 -07004000}
4001
Andrei Emeltchenko29d8a592012-09-21 12:30:05 +03004002static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data,
4003 u8 ident, u16 flags)
4004{
4005 struct l2cap_conn *conn = chan->conn;
4006
4007 BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident,
4008 flags);
4009
4010 clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
4011 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
4012
4013 l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP,
4014 l2cap_build_conf_rsp(chan, data,
4015 L2CAP_CONF_SUCCESS, flags), data);
4016}
4017
Johan Hedberg662d6522013-10-16 11:20:47 +03004018static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident,
4019 u16 scid, u16 dcid)
4020{
4021 struct l2cap_cmd_rej_cid rej;
4022
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004023 rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID);
Johan Hedberg662d6522013-10-16 11:20:47 +03004024 rej.scid = __cpu_to_le16(scid);
4025 rej.dcid = __cpu_to_le16(dcid);
4026
4027 l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
4028}
4029
Gustavo Padovan2d792812012-10-06 10:07:01 +01004030static inline int l2cap_config_req(struct l2cap_conn *conn,
4031 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4032 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
4034 struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
4035 u16 dcid, flags;
4036 u8 rsp[64];
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004037 struct l2cap_chan *chan;
Mat Martineau3c588192012-04-11 10:48:42 -07004038 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004040 if (cmd_len < sizeof(*req))
4041 return -EPROTO;
4042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 dcid = __le16_to_cpu(req->dcid);
4044 flags = __le16_to_cpu(req->flags);
4045
4046 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
4047
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03004048 chan = l2cap_get_chan_by_scid(conn, dcid);
Johan Hedberg662d6522013-10-16 11:20:47 +03004049 if (!chan) {
4050 cmd_reject_invalid_cid(conn, cmd->ident, dcid, 0);
4051 return 0;
4052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
David S. Miller033b1142011-07-21 13:38:42 -07004054 if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) {
Johan Hedberg662d6522013-10-16 11:20:47 +03004055 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4056 chan->dcid);
Marcel Holtmann354f60a2006-11-18 22:15:20 +01004057 goto unlock;
Gustavo F. Padovandf6bd742010-06-14 02:26:15 -03004058 }
Marcel Holtmann354f60a2006-11-18 22:15:20 +01004059
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004060 /* Reject if config buffer is too small. */
Al Viro88219a02007-07-29 00:17:25 -07004061 len = cmd_len - sizeof(*req);
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004062 if (chan->conf_len + len > sizeof(chan->conf_req)) {
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004063 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004064 l2cap_build_conf_rsp(chan, rsp,
4065 L2CAP_CONF_REJECT, flags), rsp);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004066 goto unlock;
4067 }
4068
4069 /* Store config. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004070 memcpy(chan->conf_req + chan->conf_len, req->data, len);
4071 chan->conf_len += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03004073 if (flags & L2CAP_CONF_FLAG_CONTINUATION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 /* Incomplete config. Send empty response. */
4075 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004076 l2cap_build_conf_rsp(chan, rsp,
4077 L2CAP_CONF_SUCCESS, flags), rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 goto unlock;
4079 }
4080
4081 /* Complete config. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004082 len = l2cap_parse_conf_req(chan, rsp);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004083 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004084 l2cap_send_disconn_req(chan, ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 goto unlock;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
Mat Martineau1500109b2012-10-23 15:24:15 -07004088 chan->ident = cmd->ident;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004089 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004090 chan->num_conf_rsp++;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004091
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004092 /* Reset config buffer. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004093 chan->conf_len = 0;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004094
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004095 if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state))
Marcel Holtmann876d9482007-10-20 13:35:42 +02004096 goto unlock;
4097
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004098 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004099 set_default_fcs(chan);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004100
Mat Martineau105bdf92012-04-27 16:50:48 -07004101 if (chan->mode == L2CAP_MODE_ERTM ||
4102 chan->mode == L2CAP_MODE_STREAMING)
Mat Martineau3c588192012-04-11 10:48:42 -07004103 err = l2cap_ertm_init(chan);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03004104
Mat Martineau3c588192012-04-11 10:48:42 -07004105 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004106 l2cap_send_disconn_req(chan, -err);
Mat Martineau3c588192012-04-11 10:48:42 -07004107 else
4108 l2cap_chan_ready(chan);
4109
Marcel Holtmann876d9482007-10-20 13:35:42 +02004110 goto unlock;
4111 }
4112
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004113 if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +02004114 u8 buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004116 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004117 chan->num_conf_req++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 }
4119
Stephen Hemminger49c922b2014-10-27 21:12:20 -07004120 /* Got Conf Rsp PENDING from remote side and assume we sent
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004121 Conf Rsp PENDING in the code above */
4122 if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) &&
Andrei Emeltchenko29d8a592012-09-21 12:30:05 +03004123 test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004124
4125 /* check compatibility */
4126
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004127 /* Send rsp for BR/EDR channel */
Andrei Emeltchenkof351bc72012-10-31 15:46:35 +02004128 if (!chan->hs_hcon)
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004129 l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags);
4130 else
4131 chan->ident = cmd->ident;
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004132 }
4133
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134unlock:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004135 l2cap_chan_unlock(chan);
Mat Martineau3c588192012-04-11 10:48:42 -07004136 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137}
4138
Gustavo Padovan2d792812012-10-06 10:07:01 +01004139static inline int l2cap_config_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004140 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4141 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142{
4143 struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
4144 u16 scid, flags, result;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004145 struct l2cap_chan *chan;
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004146 int len = cmd_len - sizeof(*rsp);
Mat Martineau3c588192012-04-11 10:48:42 -07004147 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004149 if (cmd_len < sizeof(*rsp))
4150 return -EPROTO;
4151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 scid = __le16_to_cpu(rsp->scid);
4153 flags = __le16_to_cpu(rsp->flags);
4154 result = __le16_to_cpu(rsp->result);
4155
Andrei Emeltchenko61386cb2012-03-12 12:13:07 +02004156 BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags,
4157 result, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03004159 chan = l2cap_get_chan_by_scid(conn, scid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004160 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 return 0;
4162
4163 switch (result) {
4164 case L2CAP_CONF_SUCCESS:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004165 l2cap_conf_rfc_get(chan, rsp->data, len);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004166 clear_bit(CONF_REM_CONF_PEND, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 break;
4168
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004169 case L2CAP_CONF_PENDING:
4170 set_bit(CONF_REM_CONF_PEND, &chan->conf_state);
4171
4172 if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
4173 char buf[64];
4174
4175 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004176 buf, &result);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004177 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004178 l2cap_send_disconn_req(chan, ECONNRESET);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004179 goto done;
4180 }
4181
Andrei Emeltchenkof351bc72012-10-31 15:46:35 +02004182 if (!chan->hs_hcon) {
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004183 l2cap_send_efs_conf_rsp(chan, buf, cmd->ident,
4184 0);
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +02004185 } else {
4186 if (l2cap_check_efs(chan)) {
4187 amp_create_logical_link(chan);
4188 chan->ident = cmd->ident;
4189 }
4190 }
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004191 }
4192 goto done;
4193
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 case L2CAP_CONF_UNACCEPT:
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004195 if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004196 char req[64];
4197
Andrei Emeltchenkoc2c77ec2010-03-19 10:26:28 +02004198 if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004199 l2cap_send_disconn_req(chan, ECONNRESET);
Andrei Emeltchenkoc2c77ec2010-03-19 10:26:28 +02004200 goto done;
4201 }
4202
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004203 /* throw out any old stored conf requests */
4204 result = L2CAP_CONF_SUCCESS;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03004205 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004206 req, &result);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004207 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004208 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004209 goto done;
4210 }
4211
4212 l2cap_send_cmd(conn, l2cap_get_ident(conn),
Gustavo Padovan2d792812012-10-06 10:07:01 +01004213 L2CAP_CONF_REQ, len, req);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004214 chan->num_conf_req++;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004215 if (result != L2CAP_CONF_SUCCESS)
4216 goto done;
4217 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 }
4219
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09004220 default:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004221 l2cap_chan_set_err(chan, ECONNRESET);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +02004222
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08004223 __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004224 l2cap_send_disconn_req(chan, ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 goto done;
4226 }
4227
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03004228 if (flags & L2CAP_CONF_FLAG_CONTINUATION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 goto done;
4230
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004231 set_bit(CONF_INPUT_DONE, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004233 if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) {
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004234 set_default_fcs(chan);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004235
Mat Martineau105bdf92012-04-27 16:50:48 -07004236 if (chan->mode == L2CAP_MODE_ERTM ||
4237 chan->mode == L2CAP_MODE_STREAMING)
Mat Martineau3c588192012-04-11 10:48:42 -07004238 err = l2cap_ertm_init(chan);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03004239
Mat Martineau3c588192012-04-11 10:48:42 -07004240 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004241 l2cap_send_disconn_req(chan, -err);
Mat Martineau3c588192012-04-11 10:48:42 -07004242 else
4243 l2cap_chan_ready(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 }
4245
4246done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004247 l2cap_chan_unlock(chan);
Mat Martineau3c588192012-04-11 10:48:42 -07004248 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249}
4250
Gustavo Padovan2d792812012-10-06 10:07:01 +01004251static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004252 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4253 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254{
4255 struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
4256 struct l2cap_disconn_rsp rsp;
4257 u16 dcid, scid;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004258 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004260 if (cmd_len != sizeof(*req))
4261 return -EPROTO;
4262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 scid = __le16_to_cpu(req->scid);
4264 dcid = __le16_to_cpu(req->dcid);
4265
4266 BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
4267
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004268 mutex_lock(&conn->chan_lock);
4269
4270 chan = __l2cap_get_chan_by_scid(conn, dcid);
4271 if (!chan) {
4272 mutex_unlock(&conn->chan_lock);
Johan Hedberg662d6522013-10-16 11:20:47 +03004273 cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid);
4274 return 0;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004277 l2cap_chan_lock(chan);
4278
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03004279 rsp.dcid = cpu_to_le16(chan->scid);
4280 rsp.scid = cpu_to_le16(chan->dcid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
4282
Gustavo Padovan5ec1bbe2013-10-15 19:24:48 -03004283 chan->ops->set_shutdown(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Mat Martineau61d6ef32012-04-27 16:50:50 -07004285 l2cap_chan_hold(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004286 l2cap_chan_del(chan, ECONNRESET);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004287
4288 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
Gustavo Padovan80b98022012-05-27 22:27:51 -03004290 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07004291 l2cap_chan_put(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004292
4293 mutex_unlock(&conn->chan_lock);
4294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 return 0;
4296}
4297
Gustavo Padovan2d792812012-10-06 10:07:01 +01004298static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004299 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4300 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301{
4302 struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
4303 u16 dcid, scid;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004304 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004306 if (cmd_len != sizeof(*rsp))
4307 return -EPROTO;
4308
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 scid = __le16_to_cpu(rsp->scid);
4310 dcid = __le16_to_cpu(rsp->dcid);
4311
4312 BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
4313
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004314 mutex_lock(&conn->chan_lock);
4315
4316 chan = __l2cap_get_chan_by_scid(conn, scid);
4317 if (!chan) {
4318 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 return 0;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004322 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004323
Mat Martineau61d6ef32012-04-27 16:50:50 -07004324 l2cap_chan_hold(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004325 l2cap_chan_del(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004326
4327 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
Gustavo Padovan80b98022012-05-27 22:27:51 -03004329 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07004330 l2cap_chan_put(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004331
4332 mutex_unlock(&conn->chan_lock);
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 return 0;
4335}
4336
Gustavo Padovan2d792812012-10-06 10:07:01 +01004337static inline int l2cap_information_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004338 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4339 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340{
4341 struct l2cap_info_req *req = (struct l2cap_info_req *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 u16 type;
4343
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004344 if (cmd_len != sizeof(*req))
4345 return -EPROTO;
4346
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 type = __le16_to_cpu(req->type);
4348
4349 BT_DBG("type 0x%4.4x", type);
4350
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004351 if (type == L2CAP_IT_FEAT_MASK) {
4352 u8 buf[8];
Marcel Holtmann44dd46d2009-05-02 19:09:01 -07004353 u32 feat_mask = l2cap_feat_mask;
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004354 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004355 rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
4356 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03004357 if (!disable_ertm)
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004358 feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
Gustavo Padovan2d792812012-10-06 10:07:01 +01004359 | L2CAP_FEAT_FCS;
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004360 if (conn->local_fixed_chan & L2CAP_FC_A2MP)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03004361 feat_mask |= L2CAP_FEAT_EXT_FLOW
Gustavo Padovan2d792812012-10-06 10:07:01 +01004362 | L2CAP_FEAT_EXT_WINDOW;
Andrei Emeltchenkoa5fd6f32011-09-16 16:26:32 +03004363
Gustavo F. Padovan1b7bf4e2009-08-24 00:45:20 -03004364 put_unaligned_le32(feat_mask, rsp->data);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004365 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4366 buf);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004367 } else if (type == L2CAP_IT_FIXED_CHAN) {
4368 u8 buf[12];
4369 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
Mat Martineau50a147c2011-11-02 16:18:34 -07004370
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004371 rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
4372 rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004373 rsp->data[0] = conn->local_fixed_chan;
4374 memset(rsp->data + 1, 0, 7);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004375 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4376 buf);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004377 } else {
4378 struct l2cap_info_rsp rsp;
4379 rsp.type = cpu_to_le16(type);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004380 rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004381 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp),
4382 &rsp);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
4385 return 0;
4386}
4387
Gustavo Padovan2d792812012-10-06 10:07:01 +01004388static inline int l2cap_information_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004389 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4390 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391{
4392 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
4393 u16 type, result;
4394
Jaganath Kanakkassery3f6fa3d2013-06-21 19:55:11 +05304395 if (cmd_len < sizeof(*rsp))
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004396 return -EPROTO;
4397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 type = __le16_to_cpu(rsp->type);
4399 result = __le16_to_cpu(rsp->result);
4400
4401 BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
4402
Andrei Emeltchenkoe90165b2011-03-25 11:31:41 +02004403 /* L2CAP Info req/rsp are unbound to channels, add extra checks */
4404 if (cmd->ident != conn->info_ident ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01004405 conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
Andrei Emeltchenkoe90165b2011-03-25 11:31:41 +02004406 return 0;
4407
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02004408 cancel_delayed_work(&conn->info_timer);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004409
Ville Tervoadb08ed2010-08-04 09:43:33 +03004410 if (result != L2CAP_IR_SUCCESS) {
4411 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4412 conn->info_ident = 0;
4413
4414 l2cap_conn_start(conn);
4415
4416 return 0;
4417 }
4418
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004419 switch (type) {
4420 case L2CAP_IT_FEAT_MASK:
Harvey Harrison83985312008-05-02 16:25:46 -07004421 conn->feat_mask = get_unaligned_le32(rsp->data);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004422
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -07004423 if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004424 struct l2cap_info_req req;
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004425 req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004426
4427 conn->info_ident = l2cap_get_ident(conn);
4428
4429 l2cap_send_cmd(conn, conn->info_ident,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004430 L2CAP_INFO_REQ, sizeof(req), &req);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004431 } else {
4432 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4433 conn->info_ident = 0;
4434
4435 l2cap_conn_start(conn);
4436 }
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004437 break;
4438
4439 case L2CAP_IT_FIXED_CHAN:
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004440 conn->remote_fixed_chan = rsp->data[0];
Marcel Holtmann984947d2009-02-06 23:35:19 +01004441 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004442 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01004443
4444 l2cap_conn_start(conn);
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004445 break;
Marcel Holtmann984947d2009-02-06 23:35:19 +01004446 }
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 return 0;
4449}
4450
Mat Martineau17009152012-10-23 15:24:07 -07004451static int l2cap_create_channel_req(struct l2cap_conn *conn,
4452 struct l2cap_cmd_hdr *cmd,
4453 u16 cmd_len, void *data)
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004454{
4455 struct l2cap_create_chan_req *req = data;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004456 struct l2cap_create_chan_rsp rsp;
Mat Martineau17009152012-10-23 15:24:07 -07004457 struct l2cap_chan *chan;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004458 struct hci_dev *hdev;
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004459 u16 psm, scid;
4460
4461 if (cmd_len != sizeof(*req))
4462 return -EPROTO;
4463
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004464 if (!(conn->local_fixed_chan & L2CAP_FC_A2MP))
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004465 return -EINVAL;
4466
4467 psm = le16_to_cpu(req->psm);
4468 scid = le16_to_cpu(req->scid);
4469
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004470 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 -07004471
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004472 /* For controller id 0 make BR/EDR connection */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004473 if (req->amp_id == AMP_ID_BREDR) {
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004474 l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4475 req->amp_id);
4476 return 0;
4477 }
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004478
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004479 /* Validate AMP controller id */
4480 hdev = hci_dev_get(req->amp_id);
4481 if (!hdev)
4482 goto error;
Mat Martineau17009152012-10-23 15:24:07 -07004483
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004484 if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) {
Mat Martineau17009152012-10-23 15:24:07 -07004485 hci_dev_put(hdev);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004486 goto error;
Mat Martineau17009152012-10-23 15:24:07 -07004487 }
4488
4489 chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4490 req->amp_id);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004491 if (chan) {
4492 struct amp_mgr *mgr = conn->hcon->amp_mgr;
4493 struct hci_conn *hs_hcon;
4494
Marcel Holtmann98e0f7e2013-10-13 02:23:40 -07004495 hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
4496 &conn->hcon->dst);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004497 if (!hs_hcon) {
4498 hci_dev_put(hdev);
Johan Hedberg662d6522013-10-16 11:20:47 +03004499 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4500 chan->dcid);
4501 return 0;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004502 }
4503
4504 BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon);
4505
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004506 mgr->bredr_chan = chan;
4507 chan->hs_hcon = hs_hcon;
Andrei Emeltchenkofd45bf42012-11-20 17:16:22 +02004508 chan->fcs = L2CAP_FCS_NONE;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004509 conn->mtu = hdev->block_mtu;
4510 }
4511
4512 hci_dev_put(hdev);
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004513
4514 return 0;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004515
4516error:
4517 rsp.dcid = 0;
4518 rsp.scid = cpu_to_le16(scid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004519 rsp.result = cpu_to_le16(L2CAP_CR_BAD_AMP);
4520 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004521
4522 l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
4523 sizeof(rsp), &rsp);
4524
Johan Hedbergdc280802013-09-16 13:05:13 +03004525 return 0;
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004526}
4527
Mat Martineau8eb200b2012-10-23 15:24:17 -07004528static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id)
4529{
4530 struct l2cap_move_chan_req req;
4531 u8 ident;
4532
4533 BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id);
4534
4535 ident = l2cap_get_ident(chan->conn);
4536 chan->ident = ident;
4537
4538 req.icid = cpu_to_le16(chan->scid);
4539 req.dest_amp_id = dest_amp_id;
4540
4541 l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req),
4542 &req);
4543
4544 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4545}
4546
Mat Martineau1500109b2012-10-23 15:24:15 -07004547static void l2cap_send_move_chan_rsp(struct l2cap_chan *chan, u16 result)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004548{
4549 struct l2cap_move_chan_rsp rsp;
4550
Mat Martineau1500109b2012-10-23 15:24:15 -07004551 BT_DBG("chan %p, result 0x%4.4x", chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004552
Mat Martineau1500109b2012-10-23 15:24:15 -07004553 rsp.icid = cpu_to_le16(chan->dcid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004554 rsp.result = cpu_to_le16(result);
4555
Mat Martineau1500109b2012-10-23 15:24:15 -07004556 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP,
4557 sizeof(rsp), &rsp);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004558}
4559
Mat Martineau5b155ef2012-10-23 15:24:14 -07004560static void l2cap_send_move_chan_cfm(struct l2cap_chan *chan, u16 result)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004561{
4562 struct l2cap_move_chan_cfm cfm;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004563
Mat Martineau5b155ef2012-10-23 15:24:14 -07004564 BT_DBG("chan %p, result 0x%4.4x", chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004565
Mat Martineau5b155ef2012-10-23 15:24:14 -07004566 chan->ident = l2cap_get_ident(chan->conn);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004567
Mat Martineau5b155ef2012-10-23 15:24:14 -07004568 cfm.icid = cpu_to_le16(chan->scid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004569 cfm.result = cpu_to_le16(result);
4570
Mat Martineau5b155ef2012-10-23 15:24:14 -07004571 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM,
4572 sizeof(cfm), &cfm);
4573
4574 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4575}
4576
4577static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid)
4578{
4579 struct l2cap_move_chan_cfm cfm;
4580
4581 BT_DBG("conn %p, icid 0x%4.4x", conn, icid);
4582
4583 cfm.icid = cpu_to_le16(icid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004584 cfm.result = cpu_to_le16(L2CAP_MC_UNCONFIRMED);
Mat Martineau5b155ef2012-10-23 15:24:14 -07004585
4586 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM,
4587 sizeof(cfm), &cfm);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004588}
4589
4590static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004591 u16 icid)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004592{
4593 struct l2cap_move_chan_cfm_rsp rsp;
4594
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004595 BT_DBG("icid 0x%4.4x", icid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004596
4597 rsp.icid = cpu_to_le16(icid);
4598 l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp);
4599}
4600
Mat Martineau5f3847a2012-10-23 15:24:12 -07004601static void __release_logical_link(struct l2cap_chan *chan)
4602{
4603 chan->hs_hchan = NULL;
4604 chan->hs_hcon = NULL;
4605
4606 /* Placeholder - release the logical link */
4607}
4608
Mat Martineau1500109b2012-10-23 15:24:15 -07004609static void l2cap_logical_fail(struct l2cap_chan *chan)
4610{
4611 /* Logical link setup failed */
4612 if (chan->state != BT_CONNECTED) {
4613 /* Create channel failure, disconnect */
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004614 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineau1500109b2012-10-23 15:24:15 -07004615 return;
4616 }
4617
4618 switch (chan->move_role) {
4619 case L2CAP_MOVE_ROLE_RESPONDER:
4620 l2cap_move_done(chan);
4621 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP);
4622 break;
4623 case L2CAP_MOVE_ROLE_INITIATOR:
4624 if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP ||
4625 chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) {
4626 /* Remote has only sent pending or
4627 * success responses, clean up
4628 */
4629 l2cap_move_done(chan);
4630 }
4631
4632 /* Other amp move states imply that the move
4633 * has already aborted
4634 */
4635 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
4636 break;
4637 }
4638}
4639
4640static void l2cap_logical_finish_create(struct l2cap_chan *chan,
4641 struct hci_chan *hchan)
4642{
4643 struct l2cap_conf_rsp rsp;
Mat Martineau1500109b2012-10-23 15:24:15 -07004644
Andrei Emeltchenko336178a2012-10-31 15:46:27 +02004645 chan->hs_hchan = hchan;
Mat Martineau1500109b2012-10-23 15:24:15 -07004646 chan->hs_hcon->l2cap_data = chan->conn;
4647
Andrei Emeltchenko35ba9562012-10-25 15:20:43 +03004648 l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0);
Mat Martineau1500109b2012-10-23 15:24:15 -07004649
4650 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
Andrei Emeltchenkofe79c6f2012-10-25 15:20:42 +03004651 int err;
Mat Martineau1500109b2012-10-23 15:24:15 -07004652
4653 set_default_fcs(chan);
4654
4655 err = l2cap_ertm_init(chan);
4656 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004657 l2cap_send_disconn_req(chan, -err);
Mat Martineau1500109b2012-10-23 15:24:15 -07004658 else
4659 l2cap_chan_ready(chan);
4660 }
4661}
4662
4663static void l2cap_logical_finish_move(struct l2cap_chan *chan,
4664 struct hci_chan *hchan)
4665{
4666 chan->hs_hcon = hchan->conn;
4667 chan->hs_hcon->l2cap_data = chan->conn;
4668
4669 BT_DBG("move_state %d", chan->move_state);
4670
4671 switch (chan->move_state) {
4672 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
4673 /* Move confirm will be sent after a success
4674 * response is received
4675 */
4676 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4677 break;
4678 case L2CAP_MOVE_WAIT_LOGICAL_CFM:
4679 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4680 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4681 } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
4682 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
4683 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
4684 } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
4685 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4686 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
4687 }
4688 break;
4689 default:
4690 /* Move was not in expected state, free the channel */
4691 __release_logical_link(chan);
4692
4693 chan->move_state = L2CAP_MOVE_STABLE;
4694 }
4695}
4696
4697/* Call with chan locked */
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03004698void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan,
4699 u8 status)
Mat Martineau5b155ef2012-10-23 15:24:14 -07004700{
Mat Martineau1500109b2012-10-23 15:24:15 -07004701 BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status);
4702
4703 if (status) {
4704 l2cap_logical_fail(chan);
4705 __release_logical_link(chan);
4706 return;
4707 }
4708
4709 if (chan->state != BT_CONNECTED) {
4710 /* Ignore logical link if channel is on BR/EDR */
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004711 if (chan->local_amp_id != AMP_ID_BREDR)
Mat Martineau1500109b2012-10-23 15:24:15 -07004712 l2cap_logical_finish_create(chan, hchan);
4713 } else {
4714 l2cap_logical_finish_move(chan, hchan);
4715 }
Mat Martineau5b155ef2012-10-23 15:24:14 -07004716}
4717
Mat Martineau3f7a56c2012-10-23 15:24:23 -07004718void l2cap_move_start(struct l2cap_chan *chan)
4719{
4720 BT_DBG("chan %p", chan);
4721
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004722 if (chan->local_amp_id == AMP_ID_BREDR) {
Mat Martineau3f7a56c2012-10-23 15:24:23 -07004723 if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED)
4724 return;
4725 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
4726 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
4727 /* Placeholder - start physical link setup */
4728 } else {
4729 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
4730 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4731 chan->move_id = 0;
4732 l2cap_move_setup(chan);
4733 l2cap_send_move_chan_req(chan, 0);
4734 }
4735}
4736
Mat Martineau8eb200b2012-10-23 15:24:17 -07004737static void l2cap_do_create(struct l2cap_chan *chan, int result,
4738 u8 local_amp_id, u8 remote_amp_id)
4739{
Andrei Emeltchenko62748ca2012-11-20 17:16:19 +02004740 BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state),
4741 local_amp_id, remote_amp_id);
4742
Andrei Emeltchenko12d6cc62012-11-14 17:39:31 +02004743 chan->fcs = L2CAP_FCS_NONE;
4744
Andrei Emeltchenko62748ca2012-11-20 17:16:19 +02004745 /* Outgoing channel on AMP */
4746 if (chan->state == BT_CONNECT) {
4747 if (result == L2CAP_CR_SUCCESS) {
4748 chan->local_amp_id = local_amp_id;
4749 l2cap_send_create_chan_req(chan, remote_amp_id);
4750 } else {
4751 /* Revert to BR/EDR connect */
4752 l2cap_send_conn_req(chan);
4753 }
4754
4755 return;
4756 }
4757
4758 /* Incoming channel on AMP */
4759 if (__l2cap_no_conn_pending(chan)) {
Mat Martineau8eb200b2012-10-23 15:24:17 -07004760 struct l2cap_conn_rsp rsp;
4761 char buf[128];
4762 rsp.scid = cpu_to_le16(chan->dcid);
4763 rsp.dcid = cpu_to_le16(chan->scid);
4764
Mat Martineau8eb200b2012-10-23 15:24:17 -07004765 if (result == L2CAP_CR_SUCCESS) {
4766 /* Send successful response */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004767 rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
4768 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004769 } else {
4770 /* Send negative response */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004771 rsp.result = cpu_to_le16(L2CAP_CR_NO_MEM);
4772 rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004773 }
4774
4775 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP,
4776 sizeof(rsp), &rsp);
4777
4778 if (result == L2CAP_CR_SUCCESS) {
Gustavo Padovanf93fa272013-10-21 14:21:40 -02004779 l2cap_state_change(chan, BT_CONFIG);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004780 set_bit(CONF_REQ_SENT, &chan->conf_state);
4781 l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn),
4782 L2CAP_CONF_REQ,
4783 l2cap_build_conf_req(chan, buf), buf);
4784 chan->num_conf_req++;
4785 }
Mat Martineau8eb200b2012-10-23 15:24:17 -07004786 }
4787}
4788
4789static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id,
4790 u8 remote_amp_id)
4791{
4792 l2cap_move_setup(chan);
4793 chan->move_id = local_amp_id;
4794 chan->move_state = L2CAP_MOVE_WAIT_RSP;
4795
4796 l2cap_send_move_chan_req(chan, remote_amp_id);
4797}
4798
4799static void l2cap_do_move_respond(struct l2cap_chan *chan, int result)
4800{
4801 struct hci_chan *hchan = NULL;
4802
4803 /* Placeholder - get hci_chan for logical link */
4804
4805 if (hchan) {
4806 if (hchan->state == BT_CONNECTED) {
4807 /* Logical link is ready to go */
4808 chan->hs_hcon = hchan->conn;
4809 chan->hs_hcon->l2cap_data = chan->conn;
4810 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4811 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
4812
4813 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
4814 } else {
4815 /* Wait for logical link to be ready */
4816 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
4817 }
4818 } else {
4819 /* Logical link not available */
4820 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED);
4821 }
4822}
4823
4824static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result)
4825{
4826 if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
4827 u8 rsp_result;
4828 if (result == -EINVAL)
4829 rsp_result = L2CAP_MR_BAD_ID;
4830 else
4831 rsp_result = L2CAP_MR_NOT_ALLOWED;
4832
4833 l2cap_send_move_chan_rsp(chan, rsp_result);
4834 }
4835
4836 chan->move_role = L2CAP_MOVE_ROLE_NONE;
4837 chan->move_state = L2CAP_MOVE_STABLE;
4838
4839 /* Restart data transmission */
4840 l2cap_ertm_send(chan);
4841}
4842
Andrei Emeltchenkoa514b172012-11-14 17:39:30 +02004843/* Invoke with locked chan */
4844void __l2cap_physical_cfm(struct l2cap_chan *chan, int result)
Mat Martineau8eb200b2012-10-23 15:24:17 -07004845{
Andrei Emeltchenko770bfef2012-10-31 15:46:29 +02004846 u8 local_amp_id = chan->local_amp_id;
Andrei Emeltchenkofffadc02012-11-01 15:37:03 +02004847 u8 remote_amp_id = chan->remote_amp_id;
Andrei Emeltchenko770bfef2012-10-31 15:46:29 +02004848
Mat Martineau8eb200b2012-10-23 15:24:17 -07004849 BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d",
4850 chan, result, local_amp_id, remote_amp_id);
4851
Mat Martineau8eb200b2012-10-23 15:24:17 -07004852 if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) {
4853 l2cap_chan_unlock(chan);
4854 return;
4855 }
4856
4857 if (chan->state != BT_CONNECTED) {
4858 l2cap_do_create(chan, result, local_amp_id, remote_amp_id);
4859 } else if (result != L2CAP_MR_SUCCESS) {
4860 l2cap_do_move_cancel(chan, result);
4861 } else {
4862 switch (chan->move_role) {
4863 case L2CAP_MOVE_ROLE_INITIATOR:
4864 l2cap_do_move_initiate(chan, local_amp_id,
4865 remote_amp_id);
4866 break;
4867 case L2CAP_MOVE_ROLE_RESPONDER:
4868 l2cap_do_move_respond(chan, result);
4869 break;
4870 default:
4871 l2cap_do_move_cancel(chan, result);
4872 break;
4873 }
4874 }
Mat Martineau8eb200b2012-10-23 15:24:17 -07004875}
4876
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004877static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004878 struct l2cap_cmd_hdr *cmd,
4879 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004880{
4881 struct l2cap_move_chan_req *req = data;
Mat Martineau1500109b2012-10-23 15:24:15 -07004882 struct l2cap_move_chan_rsp rsp;
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004883 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004884 u16 icid = 0;
4885 u16 result = L2CAP_MR_NOT_ALLOWED;
4886
4887 if (cmd_len != sizeof(*req))
4888 return -EPROTO;
4889
4890 icid = le16_to_cpu(req->icid);
4891
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004892 BT_DBG("icid 0x%4.4x, dest_amp_id %d", icid, req->dest_amp_id);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004893
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02004894 if (!(conn->local_fixed_chan & L2CAP_FC_A2MP))
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004895 return -EINVAL;
4896
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004897 chan = l2cap_get_chan_by_dcid(conn, icid);
4898 if (!chan) {
Mat Martineau1500109b2012-10-23 15:24:15 -07004899 rsp.icid = cpu_to_le16(icid);
Joe Perchesdcf4adb2014-03-12 10:52:35 -07004900 rsp.result = cpu_to_le16(L2CAP_MR_NOT_ALLOWED);
Mat Martineau1500109b2012-10-23 15:24:15 -07004901 l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP,
4902 sizeof(rsp), &rsp);
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004903 return 0;
4904 }
4905
Mat Martineau1500109b2012-10-23 15:24:15 -07004906 chan->ident = cmd->ident;
4907
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004908 if (chan->scid < L2CAP_CID_DYN_START ||
4909 chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY ||
4910 (chan->mode != L2CAP_MODE_ERTM &&
4911 chan->mode != L2CAP_MODE_STREAMING)) {
4912 result = L2CAP_MR_NOT_ALLOWED;
4913 goto send_move_response;
4914 }
4915
4916 if (chan->local_amp_id == req->dest_amp_id) {
4917 result = L2CAP_MR_SAME_ID;
4918 goto send_move_response;
4919 }
4920
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004921 if (req->dest_amp_id != AMP_ID_BREDR) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004922 struct hci_dev *hdev;
4923 hdev = hci_dev_get(req->dest_amp_id);
4924 if (!hdev || hdev->dev_type != HCI_AMP ||
4925 !test_bit(HCI_UP, &hdev->flags)) {
4926 if (hdev)
4927 hci_dev_put(hdev);
4928
4929 result = L2CAP_MR_BAD_ID;
4930 goto send_move_response;
4931 }
4932 hci_dev_put(hdev);
4933 }
4934
4935 /* Detect a move collision. Only send a collision response
4936 * if this side has "lost", otherwise proceed with the move.
4937 * The winner has the larger bd_addr.
4938 */
4939 if ((__chan_is_moving(chan) ||
4940 chan->move_role != L2CAP_MOVE_ROLE_NONE) &&
Marcel Holtmann6f59b902013-10-13 05:24:01 -07004941 bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004942 result = L2CAP_MR_COLLISION;
4943 goto send_move_response;
4944 }
4945
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004946 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
4947 l2cap_move_setup(chan);
4948 chan->move_id = req->dest_amp_id;
4949 icid = chan->dcid;
4950
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07004951 if (req->dest_amp_id == AMP_ID_BREDR) {
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004952 /* Moving to BR/EDR */
4953 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4954 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4955 result = L2CAP_MR_PEND;
4956 } else {
4957 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4958 result = L2CAP_MR_SUCCESS;
4959 }
4960 } else {
4961 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
4962 /* Placeholder - uncomment when amp functions are available */
4963 /*amp_accept_physical(chan, req->dest_amp_id);*/
4964 result = L2CAP_MR_PEND;
4965 }
4966
4967send_move_response:
Mat Martineau1500109b2012-10-23 15:24:15 -07004968 l2cap_send_move_chan_rsp(chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004969
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004970 l2cap_chan_unlock(chan);
4971
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004972 return 0;
4973}
4974
Mat Martineau5b155ef2012-10-23 15:24:14 -07004975static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result)
4976{
4977 struct l2cap_chan *chan;
4978 struct hci_chan *hchan = NULL;
4979
4980 chan = l2cap_get_chan_by_scid(conn, icid);
4981 if (!chan) {
4982 l2cap_send_move_chan_cfm_icid(conn, icid);
4983 return;
4984 }
4985
4986 __clear_chan_timer(chan);
4987 if (result == L2CAP_MR_PEND)
4988 __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT);
4989
4990 switch (chan->move_state) {
4991 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
4992 /* Move confirm will be sent when logical link
4993 * is complete.
4994 */
4995 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
4996 break;
4997 case L2CAP_MOVE_WAIT_RSP_SUCCESS:
4998 if (result == L2CAP_MR_PEND) {
4999 break;
5000 } else if (test_bit(CONN_LOCAL_BUSY,
5001 &chan->conn_state)) {
5002 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
5003 } else {
5004 /* Logical link is up or moving to BR/EDR,
5005 * proceed with move
5006 */
5007 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
5008 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
5009 }
5010 break;
5011 case L2CAP_MOVE_WAIT_RSP:
5012 /* Moving to AMP */
5013 if (result == L2CAP_MR_SUCCESS) {
5014 /* Remote is ready, send confirm immediately
5015 * after logical link is ready
5016 */
5017 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
5018 } else {
5019 /* Both logical link and move success
5020 * are required to confirm
5021 */
5022 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP;
5023 }
5024
5025 /* Placeholder - get hci_chan for logical link */
5026 if (!hchan) {
5027 /* Logical link not available */
5028 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5029 break;
5030 }
5031
5032 /* If the logical link is not yet connected, do not
5033 * send confirmation.
5034 */
5035 if (hchan->state != BT_CONNECTED)
5036 break;
5037
5038 /* Logical link is already ready to go */
5039
5040 chan->hs_hcon = hchan->conn;
5041 chan->hs_hcon->l2cap_data = chan->conn;
5042
5043 if (result == L2CAP_MR_SUCCESS) {
5044 /* Can confirm now */
5045 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
5046 } else {
5047 /* Now only need move success
5048 * to confirm
5049 */
5050 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
5051 }
5052
5053 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
5054 break;
5055 default:
5056 /* Any other amp move state means the move failed. */
5057 chan->move_id = chan->local_amp_id;
5058 l2cap_move_done(chan);
5059 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5060 }
5061
5062 l2cap_chan_unlock(chan);
5063}
5064
5065static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid,
5066 u16 result)
5067{
5068 struct l2cap_chan *chan;
5069
5070 chan = l2cap_get_chan_by_ident(conn, ident);
5071 if (!chan) {
5072 /* Could not locate channel, icid is best guess */
5073 l2cap_send_move_chan_cfm_icid(conn, icid);
5074 return;
5075 }
5076
5077 __clear_chan_timer(chan);
5078
5079 if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
5080 if (result == L2CAP_MR_COLLISION) {
5081 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
5082 } else {
5083 /* Cleanup - cancel move */
5084 chan->move_id = chan->local_amp_id;
5085 l2cap_move_done(chan);
5086 }
5087 }
5088
5089 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5090
5091 l2cap_chan_unlock(chan);
5092}
5093
5094static int l2cap_move_channel_rsp(struct l2cap_conn *conn,
5095 struct l2cap_cmd_hdr *cmd,
5096 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005097{
5098 struct l2cap_move_chan_rsp *rsp = data;
5099 u16 icid, result;
5100
5101 if (cmd_len != sizeof(*rsp))
5102 return -EPROTO;
5103
5104 icid = le16_to_cpu(rsp->icid);
5105 result = le16_to_cpu(rsp->result);
5106
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005107 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005108
Mat Martineau5b155ef2012-10-23 15:24:14 -07005109 if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND)
5110 l2cap_move_continue(conn, icid, result);
5111 else
5112 l2cap_move_fail(conn, cmd->ident, icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005113
5114 return 0;
5115}
5116
Mat Martineau5f3847a2012-10-23 15:24:12 -07005117static int l2cap_move_channel_confirm(struct l2cap_conn *conn,
5118 struct l2cap_cmd_hdr *cmd,
5119 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005120{
5121 struct l2cap_move_chan_cfm *cfm = data;
Mat Martineau5f3847a2012-10-23 15:24:12 -07005122 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005123 u16 icid, result;
5124
5125 if (cmd_len != sizeof(*cfm))
5126 return -EPROTO;
5127
5128 icid = le16_to_cpu(cfm->icid);
5129 result = le16_to_cpu(cfm->result);
5130
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005131 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005132
Mat Martineau5f3847a2012-10-23 15:24:12 -07005133 chan = l2cap_get_chan_by_dcid(conn, icid);
5134 if (!chan) {
5135 /* Spec requires a response even if the icid was not found */
5136 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5137 return 0;
5138 }
5139
5140 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) {
5141 if (result == L2CAP_MC_CONFIRMED) {
5142 chan->local_amp_id = chan->move_id;
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07005143 if (chan->local_amp_id == AMP_ID_BREDR)
Mat Martineau5f3847a2012-10-23 15:24:12 -07005144 __release_logical_link(chan);
5145 } else {
5146 chan->move_id = chan->local_amp_id;
5147 }
5148
5149 l2cap_move_done(chan);
5150 }
5151
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005152 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5153
Mat Martineau5f3847a2012-10-23 15:24:12 -07005154 l2cap_chan_unlock(chan);
5155
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005156 return 0;
5157}
5158
5159static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005160 struct l2cap_cmd_hdr *cmd,
5161 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005162{
5163 struct l2cap_move_chan_cfm_rsp *rsp = data;
Mat Martineau3fd71a02012-10-23 15:24:16 -07005164 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005165 u16 icid;
5166
5167 if (cmd_len != sizeof(*rsp))
5168 return -EPROTO;
5169
5170 icid = le16_to_cpu(rsp->icid);
5171
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005172 BT_DBG("icid 0x%4.4x", icid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005173
Mat Martineau3fd71a02012-10-23 15:24:16 -07005174 chan = l2cap_get_chan_by_scid(conn, icid);
5175 if (!chan)
5176 return 0;
5177
5178 __clear_chan_timer(chan);
5179
5180 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) {
5181 chan->local_amp_id = chan->move_id;
5182
Marcel Holtmann6ed971c2013-10-05 11:47:44 -07005183 if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan)
Mat Martineau3fd71a02012-10-23 15:24:16 -07005184 __release_logical_link(chan);
5185
5186 l2cap_move_done(chan);
5187 }
5188
5189 l2cap_chan_unlock(chan);
5190
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005191 return 0;
5192}
5193
Claudio Takahaside731152011-02-11 19:28:55 -02005194static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005195 struct l2cap_cmd_hdr *cmd,
Johan Hedberg203e6392013-05-15 10:07:15 +03005196 u16 cmd_len, u8 *data)
Claudio Takahaside731152011-02-11 19:28:55 -02005197{
5198 struct hci_conn *hcon = conn->hcon;
5199 struct l2cap_conn_param_update_req *req;
5200 struct l2cap_conn_param_update_rsp rsp;
Johan Hedberg203e6392013-05-15 10:07:15 +03005201 u16 min, max, latency, to_multiplier;
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005202 int err;
Claudio Takahaside731152011-02-11 19:28:55 -02005203
Johan Hedberg40bef302014-07-16 11:42:27 +03005204 if (hcon->role != HCI_ROLE_MASTER)
Claudio Takahaside731152011-02-11 19:28:55 -02005205 return -EINVAL;
5206
Claudio Takahaside731152011-02-11 19:28:55 -02005207 if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
5208 return -EPROTO;
5209
5210 req = (struct l2cap_conn_param_update_req *) data;
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03005211 min = __le16_to_cpu(req->min);
5212 max = __le16_to_cpu(req->max);
Claudio Takahaside731152011-02-11 19:28:55 -02005213 latency = __le16_to_cpu(req->latency);
5214 to_multiplier = __le16_to_cpu(req->to_multiplier);
5215
5216 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 +01005217 min, max, latency, to_multiplier);
Claudio Takahaside731152011-02-11 19:28:55 -02005218
5219 memset(&rsp, 0, sizeof(rsp));
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005220
Andre Guedesd4905f22014-06-25 21:52:52 -03005221 err = hci_check_conn_params(min, max, latency, to_multiplier);
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005222 if (err)
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005223 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
Claudio Takahaside731152011-02-11 19:28:55 -02005224 else
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005225 rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
Claudio Takahaside731152011-02-11 19:28:55 -02005226
5227 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005228 sizeof(rsp), &rsp);
Claudio Takahaside731152011-02-11 19:28:55 -02005229
Andre Guedesffb5a8272014-07-01 18:10:11 -03005230 if (!err) {
Johan Hedbergf4869e22014-07-02 17:37:32 +03005231 u8 store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03005232
Johan Hedbergf4869e22014-07-02 17:37:32 +03005233 store_hint = hci_le_conn_update(hcon, min, max, latency,
5234 to_multiplier);
5235 mgmt_new_conn_param(hcon->hdev, &hcon->dst, hcon->dst_type,
5236 store_hint, min, max, latency,
5237 to_multiplier);
5238
Andre Guedesffb5a8272014-07-01 18:10:11 -03005239 }
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005240
Claudio Takahaside731152011-02-11 19:28:55 -02005241 return 0;
5242}
5243
Johan Hedbergf1496de2013-05-13 14:15:56 +03005244static int l2cap_le_connect_rsp(struct l2cap_conn *conn,
5245 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5246 u8 *data)
5247{
5248 struct l2cap_le_conn_rsp *rsp = (struct l2cap_le_conn_rsp *) data;
Johan Hedberg3e64b7b2014-11-13 10:55:19 +02005249 struct hci_conn *hcon = conn->hcon;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005250 u16 dcid, mtu, mps, credits, result;
5251 struct l2cap_chan *chan;
Johan Hedberg3e64b7b2014-11-13 10:55:19 +02005252 int err, sec_level;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005253
5254 if (cmd_len < sizeof(*rsp))
5255 return -EPROTO;
5256
5257 dcid = __le16_to_cpu(rsp->dcid);
5258 mtu = __le16_to_cpu(rsp->mtu);
5259 mps = __le16_to_cpu(rsp->mps);
5260 credits = __le16_to_cpu(rsp->credits);
5261 result = __le16_to_cpu(rsp->result);
5262
Johan Hedberg40624182015-11-02 14:39:17 +02005263 if (result == L2CAP_CR_SUCCESS && (mtu < 23 || mps < 23 ||
5264 dcid < L2CAP_CID_DYN_START ||
5265 dcid > L2CAP_CID_LE_DYN_END))
Johan Hedbergf1496de2013-05-13 14:15:56 +03005266 return -EPROTO;
5267
5268 BT_DBG("dcid 0x%4.4x mtu %u mps %u credits %u result 0x%2.2x",
5269 dcid, mtu, mps, credits, result);
5270
5271 mutex_lock(&conn->chan_lock);
5272
5273 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
5274 if (!chan) {
5275 err = -EBADSLT;
5276 goto unlock;
5277 }
5278
5279 err = 0;
5280
5281 l2cap_chan_lock(chan);
5282
5283 switch (result) {
5284 case L2CAP_CR_SUCCESS:
Johan Hedberg40624182015-11-02 14:39:17 +02005285 if (__l2cap_get_chan_by_dcid(conn, dcid)) {
5286 err = -EBADSLT;
5287 break;
5288 }
5289
Johan Hedbergf1496de2013-05-13 14:15:56 +03005290 chan->ident = 0;
5291 chan->dcid = dcid;
5292 chan->omtu = mtu;
5293 chan->remote_mps = mps;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005294 chan->tx_credits = credits;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005295 l2cap_chan_ready(chan);
5296 break;
5297
Johan Hedberg3e64b7b2014-11-13 10:55:19 +02005298 case L2CAP_CR_AUTHENTICATION:
5299 case L2CAP_CR_ENCRYPTION:
5300 /* If we already have MITM protection we can't do
5301 * anything.
5302 */
5303 if (hcon->sec_level > BT_SECURITY_MEDIUM) {
5304 l2cap_chan_del(chan, ECONNREFUSED);
5305 break;
5306 }
5307
5308 sec_level = hcon->sec_level + 1;
5309 if (chan->sec_level < sec_level)
5310 chan->sec_level = sec_level;
5311
5312 /* We'll need to send a new Connect Request */
5313 clear_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags);
5314
5315 smp_conn_security(hcon, chan->sec_level);
5316 break;
5317
Johan Hedbergf1496de2013-05-13 14:15:56 +03005318 default:
5319 l2cap_chan_del(chan, ECONNREFUSED);
5320 break;
5321 }
5322
5323 l2cap_chan_unlock(chan);
5324
5325unlock:
5326 mutex_unlock(&conn->chan_lock);
5327
5328 return err;
5329}
5330
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005331static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005332 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5333 u8 *data)
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005334{
5335 int err = 0;
5336
5337 switch (cmd->code) {
5338 case L2CAP_COMMAND_REJ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005339 l2cap_command_rej(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005340 break;
5341
5342 case L2CAP_CONN_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005343 err = l2cap_connect_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005344 break;
5345
5346 case L2CAP_CONN_RSP:
Mat Martineauf5a25982012-10-11 17:48:21 +03005347 case L2CAP_CREATE_CHAN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005348 l2cap_connect_create_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005349 break;
5350
5351 case L2CAP_CONF_REQ:
5352 err = l2cap_config_req(conn, cmd, cmd_len, data);
5353 break;
5354
5355 case L2CAP_CONF_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005356 l2cap_config_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005357 break;
5358
5359 case L2CAP_DISCONN_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005360 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005361 break;
5362
5363 case L2CAP_DISCONN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005364 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005365 break;
5366
5367 case L2CAP_ECHO_REQ:
5368 l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
5369 break;
5370
5371 case L2CAP_ECHO_RSP:
5372 break;
5373
5374 case L2CAP_INFO_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005375 err = l2cap_information_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005376 break;
5377
5378 case L2CAP_INFO_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005379 l2cap_information_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005380 break;
5381
Mat Martineauf94ff6f2011-11-02 16:18:32 -07005382 case L2CAP_CREATE_CHAN_REQ:
5383 err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
5384 break;
5385
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005386 case L2CAP_MOVE_CHAN_REQ:
5387 err = l2cap_move_channel_req(conn, cmd, cmd_len, data);
5388 break;
5389
5390 case L2CAP_MOVE_CHAN_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005391 l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005392 break;
5393
5394 case L2CAP_MOVE_CHAN_CFM:
5395 err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data);
5396 break;
5397
5398 case L2CAP_MOVE_CHAN_CFM_RSP:
Johan Hedberg9245e732013-09-16 13:05:17 +03005399 l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005400 break;
5401
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005402 default:
5403 BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
5404 err = -EINVAL;
5405 break;
5406 }
5407
5408 return err;
5409}
5410
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005411static int l2cap_le_connect_req(struct l2cap_conn *conn,
5412 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5413 u8 *data)
5414{
5415 struct l2cap_le_conn_req *req = (struct l2cap_le_conn_req *) data;
5416 struct l2cap_le_conn_rsp rsp;
5417 struct l2cap_chan *chan, *pchan;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005418 u16 dcid, scid, credits, mtu, mps;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005419 __le16 psm;
5420 u8 result;
5421
5422 if (cmd_len != sizeof(*req))
5423 return -EPROTO;
5424
5425 scid = __le16_to_cpu(req->scid);
5426 mtu = __le16_to_cpu(req->mtu);
5427 mps = __le16_to_cpu(req->mps);
5428 psm = req->psm;
5429 dcid = 0;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005430 credits = 0;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005431
5432 if (mtu < 23 || mps < 23)
5433 return -EPROTO;
5434
5435 BT_DBG("psm 0x%2.2x scid 0x%4.4x mtu %u mps %u", __le16_to_cpu(psm),
5436 scid, mtu, mps);
5437
5438 /* Check if we have socket listening on psm */
5439 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
5440 &conn->hcon->dst, LE_LINK);
5441 if (!pchan) {
5442 result = L2CAP_CR_BAD_PSM;
5443 chan = NULL;
5444 goto response;
5445 }
5446
5447 mutex_lock(&conn->chan_lock);
5448 l2cap_chan_lock(pchan);
5449
Johan Hedberg35dc6f82014-11-13 10:55:18 +02005450 if (!smp_sufficient_security(conn->hcon, pchan->sec_level,
5451 SMP_ALLOW_STK)) {
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005452 result = L2CAP_CR_AUTHENTICATION;
5453 chan = NULL;
5454 goto response_unlock;
5455 }
5456
Johan Hedberg8a7889c2015-11-02 14:39:15 +02005457 /* Check for valid dynamic CID range */
5458 if (scid < L2CAP_CID_DYN_START || scid > L2CAP_CID_LE_DYN_END) {
5459 result = L2CAP_CR_INVALID_SCID;
5460 chan = NULL;
5461 goto response_unlock;
5462 }
5463
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005464 /* Check if we already have channel with that dcid */
5465 if (__l2cap_get_chan_by_dcid(conn, scid)) {
Johan Hedberg8a7889c2015-11-02 14:39:15 +02005466 result = L2CAP_CR_SCID_IN_USE;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005467 chan = NULL;
5468 goto response_unlock;
5469 }
5470
5471 chan = pchan->ops->new_connection(pchan);
5472 if (!chan) {
5473 result = L2CAP_CR_NO_MEM;
5474 goto response_unlock;
5475 }
5476
Johan Hedberg0ce43ce2013-12-05 14:55:33 +02005477 l2cap_le_flowctl_init(chan);
5478
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005479 bacpy(&chan->src, &conn->hcon->src);
5480 bacpy(&chan->dst, &conn->hcon->dst);
Johan Hedberga250e042015-01-15 13:06:44 +02005481 chan->src_type = bdaddr_src_type(conn->hcon);
5482 chan->dst_type = bdaddr_dst_type(conn->hcon);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005483 chan->psm = psm;
5484 chan->dcid = scid;
5485 chan->omtu = mtu;
5486 chan->remote_mps = mps;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005487 chan->tx_credits = __le16_to_cpu(req->credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005488
5489 __l2cap_chan_add(conn, chan);
5490 dcid = chan->scid;
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005491 credits = chan->rx_credits;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005492
5493 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
5494
5495 chan->ident = cmd->ident;
5496
5497 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
5498 l2cap_state_change(chan, BT_CONNECT2);
Johan Hedberg434714d2014-09-01 09:45:03 +03005499 /* The following result value is actually not defined
5500 * for LE CoC but we use it to let the function know
5501 * that it should bail out after doing its cleanup
5502 * instead of sending a response.
5503 */
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005504 result = L2CAP_CR_PEND;
5505 chan->ops->defer(chan);
5506 } else {
5507 l2cap_chan_ready(chan);
5508 result = L2CAP_CR_SUCCESS;
5509 }
5510
5511response_unlock:
5512 l2cap_chan_unlock(pchan);
5513 mutex_unlock(&conn->chan_lock);
Johan Hedberga24cce12014-08-07 22:56:42 +03005514 l2cap_chan_put(pchan);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005515
5516 if (result == L2CAP_CR_PEND)
5517 return 0;
5518
5519response:
5520 if (chan) {
5521 rsp.mtu = cpu_to_le16(chan->imtu);
Johan Hedberg3916aed2013-10-07 15:35:26 +02005522 rsp.mps = cpu_to_le16(chan->mps);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005523 } else {
5524 rsp.mtu = 0;
5525 rsp.mps = 0;
5526 }
5527
5528 rsp.dcid = cpu_to_le16(dcid);
Johan Hedberg0cd75f72013-05-17 13:09:05 +03005529 rsp.credits = cpu_to_le16(credits);
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005530 rsp.result = cpu_to_le16(result);
5531
5532 l2cap_send_cmd(conn, cmd->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), &rsp);
5533
5534 return 0;
5535}
5536
Johan Hedbergfad5fc892013-12-05 09:45:01 +02005537static inline int l2cap_le_credits(struct l2cap_conn *conn,
5538 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5539 u8 *data)
5540{
5541 struct l2cap_le_credits *pkt;
5542 struct l2cap_chan *chan;
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005543 u16 cid, credits, max_credits;
Johan Hedbergfad5fc892013-12-05 09:45:01 +02005544
5545 if (cmd_len != sizeof(*pkt))
5546 return -EPROTO;
5547
5548 pkt = (struct l2cap_le_credits *) data;
5549 cid = __le16_to_cpu(pkt->cid);
5550 credits = __le16_to_cpu(pkt->credits);
5551
5552 BT_DBG("cid 0x%4.4x credits 0x%4.4x", cid, credits);
5553
5554 chan = l2cap_get_chan_by_dcid(conn, cid);
5555 if (!chan)
5556 return -EBADSLT;
5557
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005558 max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits;
5559 if (credits > max_credits) {
5560 BT_ERR("LE credits overflow");
5561 l2cap_send_disconn_req(chan, ECONNRESET);
Martin Townsendee930532014-10-13 19:24:45 +01005562 l2cap_chan_unlock(chan);
Johan Hedberg0f1bfe42014-01-27 15:11:35 -08005563
5564 /* Return 0 so that we don't trigger an unnecessary
5565 * command reject packet.
5566 */
5567 return 0;
5568 }
5569
Johan Hedbergfad5fc892013-12-05 09:45:01 +02005570 chan->tx_credits += credits;
5571
5572 while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) {
5573 l2cap_do_send(chan, skb_dequeue(&chan->tx_q));
5574 chan->tx_credits--;
5575 }
5576
5577 if (chan->tx_credits)
5578 chan->ops->resume(chan);
5579
5580 l2cap_chan_unlock(chan);
5581
5582 return 0;
5583}
5584
Johan Hedberg71fb4192013-12-10 10:52:48 +02005585static inline int l2cap_le_command_rej(struct l2cap_conn *conn,
5586 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5587 u8 *data)
5588{
5589 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
5590 struct l2cap_chan *chan;
5591
5592 if (cmd_len < sizeof(*rej))
5593 return -EPROTO;
5594
5595 mutex_lock(&conn->chan_lock);
5596
5597 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
5598 if (!chan)
5599 goto done;
5600
5601 l2cap_chan_lock(chan);
5602 l2cap_chan_del(chan, ECONNREFUSED);
5603 l2cap_chan_unlock(chan);
5604
5605done:
5606 mutex_unlock(&conn->chan_lock);
5607 return 0;
5608}
5609
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005610static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
Johan Hedberg203e6392013-05-15 10:07:15 +03005611 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5612 u8 *data)
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005613{
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005614 int err = 0;
5615
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005616 switch (cmd->code) {
5617 case L2CAP_COMMAND_REJ:
Johan Hedberg71fb4192013-12-10 10:52:48 +02005618 l2cap_le_command_rej(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005619 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005620
5621 case L2CAP_CONN_PARAM_UPDATE_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005622 err = l2cap_conn_param_update_req(conn, cmd, cmd_len, data);
5623 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005624
5625 case L2CAP_CONN_PARAM_UPDATE_RSP:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005626 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005627
Johan Hedbergf1496de2013-05-13 14:15:56 +03005628 case L2CAP_LE_CONN_RSP:
5629 l2cap_le_connect_rsp(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005630 break;
Johan Hedbergf1496de2013-05-13 14:15:56 +03005631
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005632 case L2CAP_LE_CONN_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005633 err = l2cap_le_connect_req(conn, cmd, cmd_len, data);
5634 break;
Johan Hedberg27e2d4c2013-05-14 13:27:21 +03005635
Johan Hedbergfad5fc892013-12-05 09:45:01 +02005636 case L2CAP_LE_CREDITS:
5637 err = l2cap_le_credits(conn, cmd, cmd_len, data);
5638 break;
5639
Johan Hedberg3defe012013-05-15 10:16:06 +03005640 case L2CAP_DISCONN_REQ:
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005641 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
5642 break;
Johan Hedberg3defe012013-05-15 10:16:06 +03005643
5644 case L2CAP_DISCONN_RSP:
5645 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005646 break;
Johan Hedberg3defe012013-05-15 10:16:06 +03005647
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005648 default:
5649 BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005650 err = -EINVAL;
5651 break;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005652 }
Johan Hedbergb5ecba62013-12-02 12:21:29 +02005653
5654 return err;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005655}
5656
Johan Hedbergc5623552013-04-29 19:35:33 +03005657static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,
5658 struct sk_buff *skb)
5659{
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005660 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005661 struct l2cap_cmd_hdr *cmd;
5662 u16 len;
Johan Hedbergc5623552013-04-29 19:35:33 +03005663 int err;
5664
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005665 if (hcon->type != LE_LINK)
Marcel Holtmann3b166292013-10-02 08:28:21 -07005666 goto drop;
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005667
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005668 if (skb->len < L2CAP_CMD_HDR_SIZE)
5669 goto drop;
Johan Hedbergc5623552013-04-29 19:35:33 +03005670
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005671 cmd = (void *) skb->data;
5672 skb_pull(skb, L2CAP_CMD_HDR_SIZE);
Johan Hedbergc5623552013-04-29 19:35:33 +03005673
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005674 len = le16_to_cpu(cmd->len);
Johan Hedbergc5623552013-04-29 19:35:33 +03005675
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005676 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd->code, len, cmd->ident);
Johan Hedbergc5623552013-04-29 19:35:33 +03005677
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005678 if (len != skb->len || !cmd->ident) {
5679 BT_DBG("corrupted command");
5680 goto drop;
5681 }
Johan Hedbergc5623552013-04-29 19:35:33 +03005682
Johan Hedberg203e6392013-05-15 10:07:15 +03005683 err = l2cap_le_sig_cmd(conn, cmd, len, skb->data);
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005684 if (err) {
5685 struct l2cap_cmd_rej_unk rej;
Johan Hedbergc5623552013-04-29 19:35:33 +03005686
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005687 BT_ERR("Wrong link type (%d)", err);
Johan Hedbergc5623552013-04-29 19:35:33 +03005688
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005689 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
Marcel Holtmann4f3e2192013-10-03 01:26:37 -07005690 l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
5691 sizeof(rej), &rej);
Johan Hedbergc5623552013-04-29 19:35:33 +03005692 }
5693
Marcel Holtmann3b166292013-10-02 08:28:21 -07005694drop:
Johan Hedbergc5623552013-04-29 19:35:33 +03005695 kfree_skb(skb);
5696}
5697
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005698static inline void l2cap_sig_channel(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005699 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700{
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005701 struct hci_conn *hcon = conn->hcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 u8 *data = skb->data;
5703 int len = skb->len;
5704 struct l2cap_cmd_hdr cmd;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005705 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706
5707 l2cap_raw_recv(conn, skb);
5708
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005709 if (hcon->type != ACL_LINK)
Marcel Holtmann3b166292013-10-02 08:28:21 -07005710 goto drop;
Johan Hedberg69c4e4e2013-09-16 13:05:18 +03005711
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 while (len >= L2CAP_CMD_HDR_SIZE) {
Al Viro88219a02007-07-29 00:17:25 -07005713 u16 cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
5715 data += L2CAP_CMD_HDR_SIZE;
5716 len -= L2CAP_CMD_HDR_SIZE;
5717
Al Viro88219a02007-07-29 00:17:25 -07005718 cmd_len = le16_to_cpu(cmd.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719
Gustavo Padovan2d792812012-10-06 10:07:01 +01005720 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len,
5721 cmd.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
Al Viro88219a02007-07-29 00:17:25 -07005723 if (cmd_len > len || !cmd.ident) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 BT_DBG("corrupted command");
5725 break;
5726 }
5727
Johan Hedbergc5623552013-04-29 19:35:33 +03005728 err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 if (err) {
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03005730 struct l2cap_cmd_rej_unk rej;
Gustavo F. Padovan2c6d1a22011-03-23 14:38:32 -03005731
5732 BT_ERR("Wrong link type (%d)", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733
Joe Perchesdcf4adb2014-03-12 10:52:35 -07005734 rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
Gustavo Padovan2d792812012-10-06 10:07:01 +01005735 l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ,
5736 sizeof(rej), &rej);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 }
5738
Al Viro88219a02007-07-29 00:17:25 -07005739 data += cmd_len;
5740 len -= cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 }
5742
Marcel Holtmann3b166292013-10-02 08:28:21 -07005743drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 kfree_skb(skb);
5745}
5746
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03005747static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005748{
5749 u16 our_fcs, rcv_fcs;
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03005750 int hdr_size;
5751
5752 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
5753 hdr_size = L2CAP_EXT_HDR_SIZE;
5754 else
5755 hdr_size = L2CAP_ENH_HDR_SIZE;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005756
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03005757 if (chan->fcs == L2CAP_FCS_CRC16) {
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03005758 skb_trim(skb, skb->len - L2CAP_FCS_SIZE);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005759 rcv_fcs = get_unaligned_le16(skb->data + skb->len);
5760 our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
5761
5762 if (our_fcs != rcv_fcs)
João Paulo Rechi Vita7a560e52010-06-22 13:56:27 -03005763 return -EBADMSG;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005764 }
5765 return 0;
5766}
5767
Mat Martineau6ea00482012-05-17 20:53:52 -07005768static void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005769{
Mat Martineaue31f7632012-05-17 20:53:41 -07005770 struct l2cap_ctrl control;
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005771
Mat Martineaue31f7632012-05-17 20:53:41 -07005772 BT_DBG("chan %p", chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005773
Mat Martineaue31f7632012-05-17 20:53:41 -07005774 memset(&control, 0, sizeof(control));
5775 control.sframe = 1;
5776 control.final = 1;
5777 control.reqseq = chan->buffer_seq;
5778 set_bit(CONN_SEND_FBIT, &chan->conn_state);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005779
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03005780 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
Mat Martineaue31f7632012-05-17 20:53:41 -07005781 control.super = L2CAP_SUPER_RNR;
5782 l2cap_send_sframe(chan, &control);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005783 }
5784
Mat Martineaue31f7632012-05-17 20:53:41 -07005785 if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
5786 chan->unacked_frames > 0)
5787 __set_retrans_timer(chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005788
Mat Martineaue31f7632012-05-17 20:53:41 -07005789 /* Send pending iframes */
Gustavo F. Padovan525cd182011-03-25 19:43:39 -03005790 l2cap_ertm_send(chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005791
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03005792 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
Mat Martineaue31f7632012-05-17 20:53:41 -07005793 test_bit(CONN_SEND_FBIT, &chan->conn_state)) {
5794 /* F-bit wasn't sent in an s-frame or i-frame yet, so
5795 * send it now.
5796 */
5797 control.super = L2CAP_SUPER_RR;
5798 l2cap_send_sframe(chan, &control);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005799 }
5800}
5801
Gustavo Padovan2d792812012-10-06 10:07:01 +01005802static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag,
5803 struct sk_buff **last_frag)
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005804{
Mat Martineau84084a32011-07-22 14:54:00 -07005805 /* skb->len reflects data in skb as well as all fragments
5806 * skb->data_len reflects only data in fragments
5807 */
5808 if (!skb_has_frag_list(skb))
5809 skb_shinfo(skb)->frag_list = new_frag;
5810
5811 new_frag->next = NULL;
5812
5813 (*last_frag)->next = new_frag;
5814 *last_frag = new_frag;
5815
5816 skb->len += new_frag->len;
5817 skb->data_len += new_frag->len;
5818 skb->truesize += new_frag->truesize;
5819}
5820
Mat Martineau4b51dae92012-05-17 20:53:37 -07005821static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb,
5822 struct l2cap_ctrl *control)
Mat Martineau84084a32011-07-22 14:54:00 -07005823{
5824 int err = -EINVAL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005825
Mat Martineau4b51dae92012-05-17 20:53:37 -07005826 switch (control->sar) {
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005827 case L2CAP_SAR_UNSEGMENTED:
Mat Martineau84084a32011-07-22 14:54:00 -07005828 if (chan->sdu)
5829 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005830
Gustavo Padovan80b98022012-05-27 22:27:51 -03005831 err = chan->ops->recv(chan, skb);
Mat Martineau84084a32011-07-22 14:54:00 -07005832 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005833
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005834 case L2CAP_SAR_START:
Mat Martineau84084a32011-07-22 14:54:00 -07005835 if (chan->sdu)
5836 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005837
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005838 chan->sdu_len = get_unaligned_le16(skb->data);
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03005839 skb_pull(skb, L2CAP_SDULEN_SIZE);
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005840
Mat Martineau84084a32011-07-22 14:54:00 -07005841 if (chan->sdu_len > chan->imtu) {
5842 err = -EMSGSIZE;
5843 break;
5844 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005845
Mat Martineau84084a32011-07-22 14:54:00 -07005846 if (skb->len >= chan->sdu_len)
5847 break;
5848
5849 chan->sdu = skb;
5850 chan->sdu_last_frag = skb;
5851
5852 skb = NULL;
5853 err = 0;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005854 break;
5855
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005856 case L2CAP_SAR_CONTINUE:
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005857 if (!chan->sdu)
Mat Martineau84084a32011-07-22 14:54:00 -07005858 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005859
Mat Martineau84084a32011-07-22 14:54:00 -07005860 append_skb_frag(chan->sdu, skb,
5861 &chan->sdu_last_frag);
5862 skb = NULL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005863
Mat Martineau84084a32011-07-22 14:54:00 -07005864 if (chan->sdu->len >= chan->sdu_len)
5865 break;
Gustavo F. Padovan4178ba42010-05-01 16:15:45 -03005866
Mat Martineau84084a32011-07-22 14:54:00 -07005867 err = 0;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005868 break;
5869
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005870 case L2CAP_SAR_END:
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005871 if (!chan->sdu)
Mat Martineau84084a32011-07-22 14:54:00 -07005872 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005873
Mat Martineau84084a32011-07-22 14:54:00 -07005874 append_skb_frag(chan->sdu, skb,
5875 &chan->sdu_last_frag);
5876 skb = NULL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005877
Mat Martineau84084a32011-07-22 14:54:00 -07005878 if (chan->sdu->len != chan->sdu_len)
5879 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005880
Gustavo Padovan80b98022012-05-27 22:27:51 -03005881 err = chan->ops->recv(chan, chan->sdu);
Gustavo F. Padovan4178ba42010-05-01 16:15:45 -03005882
Mat Martineau84084a32011-07-22 14:54:00 -07005883 if (!err) {
5884 /* Reassembly complete */
5885 chan->sdu = NULL;
5886 chan->sdu_last_frag = NULL;
5887 chan->sdu_len = 0;
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03005888 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005889 break;
5890 }
5891
Mat Martineau84084a32011-07-22 14:54:00 -07005892 if (err) {
5893 kfree_skb(skb);
5894 kfree_skb(chan->sdu);
5895 chan->sdu = NULL;
5896 chan->sdu_last_frag = NULL;
5897 chan->sdu_len = 0;
5898 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005899
Mat Martineau84084a32011-07-22 14:54:00 -07005900 return err;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005901}
5902
Mat Martineau32b32732012-10-23 15:24:11 -07005903static int l2cap_resegment(struct l2cap_chan *chan)
5904{
5905 /* Placeholder */
5906 return 0;
5907}
5908
Mat Martineaue3281402011-07-07 09:39:02 -07005909void l2cap_chan_busy(struct l2cap_chan *chan, int busy)
Gustavo F. Padovan712132e2010-06-21 19:39:50 -03005910{
Mat Martineau61aa4f52012-05-17 20:53:40 -07005911 u8 event;
5912
5913 if (chan->mode != L2CAP_MODE_ERTM)
5914 return;
5915
5916 event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR;
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03005917 l2cap_tx(chan, NULL, NULL, event);
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03005918}
5919
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005920static int l2cap_rx_queued_iframes(struct l2cap_chan *chan)
5921{
Mat Martineau63838722012-05-17 20:53:45 -07005922 int err = 0;
5923 /* Pass sequential frames to l2cap_reassemble_sdu()
5924 * until a gap is encountered.
5925 */
5926
5927 BT_DBG("chan %p", chan);
5928
5929 while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
5930 struct sk_buff *skb;
5931 BT_DBG("Searching for skb with txseq %d (queue len %d)",
5932 chan->buffer_seq, skb_queue_len(&chan->srej_q));
5933
5934 skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq);
5935
5936 if (!skb)
5937 break;
5938
5939 skb_unlink(skb, &chan->srej_q);
5940 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
Johan Hedberga4368ff2015-03-30 23:21:01 +03005941 err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->l2cap);
Mat Martineau63838722012-05-17 20:53:45 -07005942 if (err)
5943 break;
5944 }
5945
5946 if (skb_queue_empty(&chan->srej_q)) {
5947 chan->rx_state = L2CAP_RX_STATE_RECV;
5948 l2cap_send_ack(chan);
5949 }
5950
5951 return err;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005952}
5953
5954static void l2cap_handle_srej(struct l2cap_chan *chan,
5955 struct l2cap_ctrl *control)
5956{
Mat Martineauf80842a2012-05-17 20:53:46 -07005957 struct sk_buff *skb;
5958
5959 BT_DBG("chan %p, control %p", chan, control);
5960
5961 if (control->reqseq == chan->next_tx_seq) {
5962 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005963 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineauf80842a2012-05-17 20:53:46 -07005964 return;
5965 }
5966
5967 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
5968
5969 if (skb == NULL) {
5970 BT_DBG("Seq %d not available for retransmission",
5971 control->reqseq);
5972 return;
5973 }
5974
Johan Hedberga4368ff2015-03-30 23:21:01 +03005975 if (chan->max_tx != 0 && bt_cb(skb)->l2cap.retries >= chan->max_tx) {
Mat Martineauf80842a2012-05-17 20:53:46 -07005976 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005977 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineauf80842a2012-05-17 20:53:46 -07005978 return;
5979 }
5980
5981 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
5982
5983 if (control->poll) {
5984 l2cap_pass_to_tx(chan, control);
5985
5986 set_bit(CONN_SEND_FBIT, &chan->conn_state);
5987 l2cap_retransmit(chan, control);
5988 l2cap_ertm_send(chan);
5989
5990 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
5991 set_bit(CONN_SREJ_ACT, &chan->conn_state);
5992 chan->srej_save_reqseq = control->reqseq;
5993 }
5994 } else {
5995 l2cap_pass_to_tx_fbit(chan, control);
5996
5997 if (control->final) {
5998 if (chan->srej_save_reqseq != control->reqseq ||
5999 !test_and_clear_bit(CONN_SREJ_ACT,
6000 &chan->conn_state))
6001 l2cap_retransmit(chan, control);
6002 } else {
6003 l2cap_retransmit(chan, control);
6004 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
6005 set_bit(CONN_SREJ_ACT, &chan->conn_state);
6006 chan->srej_save_reqseq = control->reqseq;
6007 }
6008 }
6009 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006010}
6011
6012static void l2cap_handle_rej(struct l2cap_chan *chan,
6013 struct l2cap_ctrl *control)
6014{
Mat Martineaufcd289d2012-05-17 20:53:47 -07006015 struct sk_buff *skb;
6016
6017 BT_DBG("chan %p, control %p", chan, control);
6018
6019 if (control->reqseq == chan->next_tx_seq) {
6020 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006021 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaufcd289d2012-05-17 20:53:47 -07006022 return;
6023 }
6024
6025 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
6026
6027 if (chan->max_tx && skb &&
Johan Hedberga4368ff2015-03-30 23:21:01 +03006028 bt_cb(skb)->l2cap.retries >= chan->max_tx) {
Mat Martineaufcd289d2012-05-17 20:53:47 -07006029 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006030 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaufcd289d2012-05-17 20:53:47 -07006031 return;
6032 }
6033
6034 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6035
6036 l2cap_pass_to_tx(chan, control);
6037
6038 if (control->final) {
6039 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
6040 l2cap_retransmit_all(chan, control);
6041 } else {
6042 l2cap_retransmit_all(chan, control);
6043 l2cap_ertm_send(chan);
6044 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F)
6045 set_bit(CONN_REJ_ACT, &chan->conn_state);
6046 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006047}
6048
Mat Martineau4b51dae92012-05-17 20:53:37 -07006049static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq)
6050{
6051 BT_DBG("chan %p, txseq %d", chan, txseq);
6052
6053 BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq,
6054 chan->expected_tx_seq);
6055
6056 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
6057 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
Gustavo Padovan2d792812012-10-06 10:07:01 +01006058 chan->tx_win) {
Mat Martineau4b51dae92012-05-17 20:53:37 -07006059 /* See notes below regarding "double poll" and
6060 * invalid packets.
6061 */
6062 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
6063 BT_DBG("Invalid/Ignore - after SREJ");
6064 return L2CAP_TXSEQ_INVALID_IGNORE;
6065 } else {
6066 BT_DBG("Invalid - in window after SREJ sent");
6067 return L2CAP_TXSEQ_INVALID;
6068 }
6069 }
6070
6071 if (chan->srej_list.head == txseq) {
6072 BT_DBG("Expected SREJ");
6073 return L2CAP_TXSEQ_EXPECTED_SREJ;
6074 }
6075
6076 if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) {
6077 BT_DBG("Duplicate SREJ - txseq already stored");
6078 return L2CAP_TXSEQ_DUPLICATE_SREJ;
6079 }
6080
6081 if (l2cap_seq_list_contains(&chan->srej_list, txseq)) {
6082 BT_DBG("Unexpected SREJ - not requested");
6083 return L2CAP_TXSEQ_UNEXPECTED_SREJ;
6084 }
6085 }
6086
6087 if (chan->expected_tx_seq == txseq) {
6088 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
6089 chan->tx_win) {
6090 BT_DBG("Invalid - txseq outside tx window");
6091 return L2CAP_TXSEQ_INVALID;
6092 } else {
6093 BT_DBG("Expected");
6094 return L2CAP_TXSEQ_EXPECTED;
6095 }
6096 }
6097
6098 if (__seq_offset(chan, txseq, chan->last_acked_seq) <
Gustavo Padovan2d792812012-10-06 10:07:01 +01006099 __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) {
Mat Martineau4b51dae92012-05-17 20:53:37 -07006100 BT_DBG("Duplicate - expected_tx_seq later than txseq");
6101 return L2CAP_TXSEQ_DUPLICATE;
6102 }
6103
6104 if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) {
6105 /* A source of invalid packets is a "double poll" condition,
6106 * where delays cause us to send multiple poll packets. If
6107 * the remote stack receives and processes both polls,
6108 * sequence numbers can wrap around in such a way that a
6109 * resent frame has a sequence number that looks like new data
6110 * with a sequence gap. This would trigger an erroneous SREJ
6111 * request.
6112 *
6113 * Fortunately, this is impossible with a tx window that's
6114 * less than half of the maximum sequence number, which allows
6115 * invalid frames to be safely ignored.
6116 *
6117 * With tx window sizes greater than half of the tx window
6118 * maximum, the frame is invalid and cannot be ignored. This
6119 * causes a disconnect.
6120 */
6121
6122 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
6123 BT_DBG("Invalid/Ignore - txseq outside tx window");
6124 return L2CAP_TXSEQ_INVALID_IGNORE;
6125 } else {
6126 BT_DBG("Invalid - txseq outside tx window");
6127 return L2CAP_TXSEQ_INVALID;
6128 }
6129 } else {
6130 BT_DBG("Unexpected - txseq indicates missing frames");
6131 return L2CAP_TXSEQ_UNEXPECTED;
6132 }
6133}
6134
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006135static int l2cap_rx_state_recv(struct l2cap_chan *chan,
6136 struct l2cap_ctrl *control,
6137 struct sk_buff *skb, u8 event)
6138{
6139 int err = 0;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006140 bool skb_in_use = false;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006141
6142 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6143 event);
6144
6145 switch (event) {
6146 case L2CAP_EV_RECV_IFRAME:
6147 switch (l2cap_classify_txseq(chan, control->txseq)) {
6148 case L2CAP_TXSEQ_EXPECTED:
6149 l2cap_pass_to_tx(chan, control);
6150
6151 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6152 BT_DBG("Busy, discarding expected seq %d",
6153 control->txseq);
6154 break;
6155 }
6156
6157 chan->expected_tx_seq = __next_seq(chan,
6158 control->txseq);
6159
6160 chan->buffer_seq = chan->expected_tx_seq;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006161 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006162
6163 err = l2cap_reassemble_sdu(chan, skb, control);
6164 if (err)
6165 break;
6166
6167 if (control->final) {
6168 if (!test_and_clear_bit(CONN_REJ_ACT,
6169 &chan->conn_state)) {
6170 control->final = 0;
6171 l2cap_retransmit_all(chan, control);
6172 l2cap_ertm_send(chan);
6173 }
6174 }
6175
6176 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
6177 l2cap_send_ack(chan);
6178 break;
6179 case L2CAP_TXSEQ_UNEXPECTED:
6180 l2cap_pass_to_tx(chan, control);
6181
6182 /* Can't issue SREJ frames in the local busy state.
6183 * Drop this frame, it will be seen as missing
6184 * when local busy is exited.
6185 */
6186 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
6187 BT_DBG("Busy, discarding unexpected seq %d",
6188 control->txseq);
6189 break;
6190 }
6191
6192 /* There was a gap in the sequence, so an SREJ
6193 * must be sent for each missing frame. The
6194 * current frame is stored for later use.
6195 */
6196 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006197 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006198 BT_DBG("Queued %p (queue len %d)", skb,
6199 skb_queue_len(&chan->srej_q));
6200
6201 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
6202 l2cap_seq_list_clear(&chan->srej_list);
6203 l2cap_send_srej(chan, control->txseq);
6204
6205 chan->rx_state = L2CAP_RX_STATE_SREJ_SENT;
6206 break;
6207 case L2CAP_TXSEQ_DUPLICATE:
6208 l2cap_pass_to_tx(chan, control);
6209 break;
6210 case L2CAP_TXSEQ_INVALID_IGNORE:
6211 break;
6212 case L2CAP_TXSEQ_INVALID:
6213 default:
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006214 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006215 break;
6216 }
6217 break;
6218 case L2CAP_EV_RECV_RR:
6219 l2cap_pass_to_tx(chan, control);
6220 if (control->final) {
6221 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6222
Mat Martineaue6a3ee62012-10-23 15:24:22 -07006223 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) &&
6224 !__chan_is_moving(chan)) {
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006225 control->final = 0;
6226 l2cap_retransmit_all(chan, control);
6227 }
6228
6229 l2cap_ertm_send(chan);
6230 } else if (control->poll) {
6231 l2cap_send_i_or_rr_or_rnr(chan);
6232 } else {
6233 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6234 &chan->conn_state) &&
6235 chan->unacked_frames)
6236 __set_retrans_timer(chan);
6237
6238 l2cap_ertm_send(chan);
6239 }
6240 break;
6241 case L2CAP_EV_RECV_RNR:
6242 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6243 l2cap_pass_to_tx(chan, control);
6244 if (control && control->poll) {
6245 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6246 l2cap_send_rr_or_rnr(chan, 0);
6247 }
6248 __clear_retrans_timer(chan);
6249 l2cap_seq_list_clear(&chan->retrans_list);
6250 break;
6251 case L2CAP_EV_RECV_REJ:
6252 l2cap_handle_rej(chan, control);
6253 break;
6254 case L2CAP_EV_RECV_SREJ:
6255 l2cap_handle_srej(chan, control);
6256 break;
6257 default:
6258 break;
6259 }
6260
6261 if (skb && !skb_in_use) {
6262 BT_DBG("Freeing %p", skb);
6263 kfree_skb(skb);
6264 }
6265
6266 return err;
6267}
6268
6269static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
6270 struct l2cap_ctrl *control,
6271 struct sk_buff *skb, u8 event)
6272{
6273 int err = 0;
6274 u16 txseq = control->txseq;
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006275 bool skb_in_use = false;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006276
6277 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6278 event);
6279
6280 switch (event) {
6281 case L2CAP_EV_RECV_IFRAME:
6282 switch (l2cap_classify_txseq(chan, txseq)) {
6283 case L2CAP_TXSEQ_EXPECTED:
6284 /* Keep frame for reassembly later */
6285 l2cap_pass_to_tx(chan, control);
6286 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006287 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006288 BT_DBG("Queued %p (queue len %d)", skb,
6289 skb_queue_len(&chan->srej_q));
6290
6291 chan->expected_tx_seq = __next_seq(chan, txseq);
6292 break;
6293 case L2CAP_TXSEQ_EXPECTED_SREJ:
6294 l2cap_seq_list_pop(&chan->srej_list);
6295
6296 l2cap_pass_to_tx(chan, control);
6297 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006298 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006299 BT_DBG("Queued %p (queue len %d)", skb,
6300 skb_queue_len(&chan->srej_q));
6301
6302 err = l2cap_rx_queued_iframes(chan);
6303 if (err)
6304 break;
6305
6306 break;
6307 case L2CAP_TXSEQ_UNEXPECTED:
6308 /* Got a frame that can't be reassembled yet.
6309 * Save it for later, and send SREJs to cover
6310 * the missing frames.
6311 */
6312 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006313 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006314 BT_DBG("Queued %p (queue len %d)", skb,
6315 skb_queue_len(&chan->srej_q));
6316
6317 l2cap_pass_to_tx(chan, control);
6318 l2cap_send_srej(chan, control->txseq);
6319 break;
6320 case L2CAP_TXSEQ_UNEXPECTED_SREJ:
6321 /* This frame was requested with an SREJ, but
6322 * some expected retransmitted frames are
6323 * missing. Request retransmission of missing
6324 * SREJ'd frames.
6325 */
6326 skb_queue_tail(&chan->srej_q, skb);
Peter Senna Tschudin941247f2013-09-22 20:44:10 +02006327 skb_in_use = true;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006328 BT_DBG("Queued %p (queue len %d)", skb,
6329 skb_queue_len(&chan->srej_q));
6330
6331 l2cap_pass_to_tx(chan, control);
6332 l2cap_send_srej_list(chan, control->txseq);
6333 break;
6334 case L2CAP_TXSEQ_DUPLICATE_SREJ:
6335 /* We've already queued this frame. Drop this copy. */
6336 l2cap_pass_to_tx(chan, control);
6337 break;
6338 case L2CAP_TXSEQ_DUPLICATE:
6339 /* Expecting a later sequence number, so this frame
6340 * was already received. Ignore it completely.
6341 */
6342 break;
6343 case L2CAP_TXSEQ_INVALID_IGNORE:
6344 break;
6345 case L2CAP_TXSEQ_INVALID:
6346 default:
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006347 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006348 break;
6349 }
6350 break;
6351 case L2CAP_EV_RECV_RR:
6352 l2cap_pass_to_tx(chan, control);
6353 if (control->final) {
6354 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6355
6356 if (!test_and_clear_bit(CONN_REJ_ACT,
6357 &chan->conn_state)) {
6358 control->final = 0;
6359 l2cap_retransmit_all(chan, control);
6360 }
6361
6362 l2cap_ertm_send(chan);
6363 } else if (control->poll) {
6364 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6365 &chan->conn_state) &&
6366 chan->unacked_frames) {
6367 __set_retrans_timer(chan);
6368 }
6369
6370 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6371 l2cap_send_srej_tail(chan);
6372 } else {
6373 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6374 &chan->conn_state) &&
6375 chan->unacked_frames)
6376 __set_retrans_timer(chan);
6377
6378 l2cap_send_ack(chan);
6379 }
6380 break;
6381 case L2CAP_EV_RECV_RNR:
6382 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6383 l2cap_pass_to_tx(chan, control);
6384 if (control->poll) {
6385 l2cap_send_srej_tail(chan);
6386 } else {
6387 struct l2cap_ctrl rr_control;
6388 memset(&rr_control, 0, sizeof(rr_control));
6389 rr_control.sframe = 1;
6390 rr_control.super = L2CAP_SUPER_RR;
6391 rr_control.reqseq = chan->buffer_seq;
6392 l2cap_send_sframe(chan, &rr_control);
6393 }
6394
6395 break;
6396 case L2CAP_EV_RECV_REJ:
6397 l2cap_handle_rej(chan, control);
6398 break;
6399 case L2CAP_EV_RECV_SREJ:
6400 l2cap_handle_srej(chan, control);
6401 break;
6402 }
6403
6404 if (skb && !skb_in_use) {
6405 BT_DBG("Freeing %p", skb);
6406 kfree_skb(skb);
6407 }
6408
6409 return err;
6410}
6411
Mat Martineau32b32732012-10-23 15:24:11 -07006412static int l2cap_finish_move(struct l2cap_chan *chan)
6413{
6414 BT_DBG("chan %p", chan);
6415
6416 chan->rx_state = L2CAP_RX_STATE_RECV;
6417
6418 if (chan->hs_hcon)
6419 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
6420 else
6421 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
6422
6423 return l2cap_resegment(chan);
6424}
6425
6426static int l2cap_rx_state_wait_p(struct l2cap_chan *chan,
6427 struct l2cap_ctrl *control,
6428 struct sk_buff *skb, u8 event)
6429{
6430 int err;
6431
6432 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6433 event);
6434
6435 if (!control->poll)
6436 return -EPROTO;
6437
6438 l2cap_process_reqseq(chan, control->reqseq);
6439
6440 if (!skb_queue_empty(&chan->tx_q))
6441 chan->tx_send_head = skb_peek(&chan->tx_q);
6442 else
6443 chan->tx_send_head = NULL;
6444
6445 /* Rewind next_tx_seq to the point expected
6446 * by the receiver.
6447 */
6448 chan->next_tx_seq = control->reqseq;
6449 chan->unacked_frames = 0;
6450
6451 err = l2cap_finish_move(chan);
6452 if (err)
6453 return err;
6454
6455 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6456 l2cap_send_i_or_rr_or_rnr(chan);
6457
6458 if (event == L2CAP_EV_RECV_IFRAME)
6459 return -EPROTO;
6460
6461 return l2cap_rx_state_recv(chan, control, NULL, event);
6462}
6463
6464static int l2cap_rx_state_wait_f(struct l2cap_chan *chan,
6465 struct l2cap_ctrl *control,
6466 struct sk_buff *skb, u8 event)
6467{
6468 int err;
6469
6470 if (!control->final)
6471 return -EPROTO;
6472
6473 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6474
6475 chan->rx_state = L2CAP_RX_STATE_RECV;
6476 l2cap_process_reqseq(chan, control->reqseq);
6477
6478 if (!skb_queue_empty(&chan->tx_q))
6479 chan->tx_send_head = skb_peek(&chan->tx_q);
6480 else
6481 chan->tx_send_head = NULL;
6482
6483 /* Rewind next_tx_seq to the point expected
6484 * by the receiver.
6485 */
6486 chan->next_tx_seq = control->reqseq;
6487 chan->unacked_frames = 0;
6488
6489 if (chan->hs_hcon)
6490 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
6491 else
6492 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
6493
6494 err = l2cap_resegment(chan);
6495
6496 if (!err)
6497 err = l2cap_rx_state_recv(chan, control, skb, event);
6498
6499 return err;
6500}
6501
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006502static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq)
6503{
6504 /* Make sure reqseq is for a packet that has been sent but not acked */
6505 u16 unacked;
6506
6507 unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq);
6508 return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked;
6509}
6510
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006511static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6512 struct sk_buff *skb, u8 event)
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006513{
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006514 int err = 0;
6515
6516 BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan,
6517 control, skb, event, chan->rx_state);
6518
6519 if (__valid_reqseq(chan, control->reqseq)) {
6520 switch (chan->rx_state) {
6521 case L2CAP_RX_STATE_RECV:
6522 err = l2cap_rx_state_recv(chan, control, skb, event);
6523 break;
6524 case L2CAP_RX_STATE_SREJ_SENT:
6525 err = l2cap_rx_state_srej_sent(chan, control, skb,
6526 event);
6527 break;
Mat Martineau32b32732012-10-23 15:24:11 -07006528 case L2CAP_RX_STATE_WAIT_P:
6529 err = l2cap_rx_state_wait_p(chan, control, skb, event);
6530 break;
6531 case L2CAP_RX_STATE_WAIT_F:
6532 err = l2cap_rx_state_wait_f(chan, control, skb, event);
6533 break;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006534 default:
6535 /* shut it down */
6536 break;
6537 }
6538 } else {
6539 BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d",
6540 control->reqseq, chan->next_tx_seq,
6541 chan->expected_ack_seq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006542 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006543 }
6544
6545 return err;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006546}
6547
6548static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6549 struct sk_buff *skb)
6550{
Mat Martineau4b51dae92012-05-17 20:53:37 -07006551 BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb,
6552 chan->rx_state);
6553
6554 if (l2cap_classify_txseq(chan, control->txseq) ==
6555 L2CAP_TXSEQ_EXPECTED) {
6556 l2cap_pass_to_tx(chan, control);
6557
6558 BT_DBG("buffer_seq %d->%d", chan->buffer_seq,
6559 __next_seq(chan, chan->buffer_seq));
6560
6561 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
6562
6563 l2cap_reassemble_sdu(chan, skb, control);
6564 } else {
6565 if (chan->sdu) {
6566 kfree_skb(chan->sdu);
6567 chan->sdu = NULL;
6568 }
6569 chan->sdu_last_frag = NULL;
6570 chan->sdu_len = 0;
6571
6572 if (skb) {
6573 BT_DBG("Freeing %p", skb);
6574 kfree_skb(skb);
6575 }
6576 }
6577
6578 chan->last_acked_seq = control->txseq;
6579 chan->expected_tx_seq = __next_seq(chan, control->txseq);
6580
Prasanna Karthik9a544212015-11-19 12:05:35 +00006581 return 0;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006582}
6583
6584static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
6585{
Johan Hedberga4368ff2015-03-30 23:21:01 +03006586 struct l2cap_ctrl *control = &bt_cb(skb)->l2cap;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006587 u16 len;
6588 u8 event;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006589
Mat Martineaub76bbd62012-04-11 10:48:43 -07006590 __unpack_control(chan, skb);
6591
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006592 len = skb->len;
6593
6594 /*
6595 * We can just drop the corrupted I-frame here.
6596 * Receiver will miss it and start proper recovery
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006597 * procedures and ask for retransmission.
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006598 */
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006599 if (l2cap_check_fcs(chan, skb))
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006600 goto drop;
6601
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006602 if (!control->sframe && control->sar == L2CAP_SAR_START)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03006603 len -= L2CAP_SDULEN_SIZE;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006604
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006605 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03006606 len -= L2CAP_FCS_SIZE;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006607
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006608 if (len > chan->mps) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006609 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006610 goto drop;
6611 }
6612
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006613 if (!control->sframe) {
6614 int err;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006615
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006616 BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d",
6617 control->sar, control->reqseq, control->final,
6618 control->txseq);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03006619
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006620 /* Validate F-bit - F=0 always valid, F=1 only
6621 * valid in TX WAIT_F
6622 */
6623 if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F)
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006624 goto drop;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006625
6626 if (chan->mode != L2CAP_MODE_STREAMING) {
6627 event = L2CAP_EV_RECV_IFRAME;
6628 err = l2cap_rx(chan, control, skb, event);
6629 } else {
6630 err = l2cap_stream_rx(chan, control, skb);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006631 }
6632
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006633 if (err)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006634 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006635 } else {
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006636 const u8 rx_func_to_event[4] = {
6637 L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ,
6638 L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ
6639 };
6640
6641 /* Only I-frames are expected in streaming mode */
6642 if (chan->mode == L2CAP_MODE_STREAMING)
6643 goto drop;
6644
6645 BT_DBG("sframe reqseq %d, final %d, poll %d, super %d",
6646 control->reqseq, control->final, control->poll,
6647 control->super);
6648
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006649 if (len != 0) {
Andrei Emeltchenko1bb166e2012-11-20 17:16:21 +02006650 BT_ERR("Trailing bytes: %d in sframe", len);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006651 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006652 goto drop;
6653 }
6654
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006655 /* Validate F and P bits */
6656 if (control->final && (control->poll ||
6657 chan->tx_state != L2CAP_TX_STATE_WAIT_F))
6658 goto drop;
6659
6660 event = rx_func_to_event[control->super];
6661 if (l2cap_rx(chan, control, skb, event))
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006662 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006663 }
6664
6665 return 0;
6666
6667drop:
6668 kfree_skb(skb);
6669 return 0;
6670}
6671
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006672static void l2cap_chan_le_send_credits(struct l2cap_chan *chan)
6673{
6674 struct l2cap_conn *conn = chan->conn;
6675 struct l2cap_le_credits pkt;
6676 u16 return_credits;
6677
6678 /* We return more credits to the sender only after the amount of
6679 * credits falls below half of the initial amount.
6680 */
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02006681 if (chan->rx_credits >= (le_max_credits + 1) / 2)
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006682 return;
6683
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02006684 return_credits = le_max_credits - chan->rx_credits;
Johan Hedbergb1c325c2013-12-05 09:43:34 +02006685
6686 BT_DBG("chan %p returning %u credits to sender", chan, return_credits);
6687
6688 chan->rx_credits += return_credits;
6689
6690 pkt.cid = cpu_to_le16(chan->scid);
6691 pkt.credits = cpu_to_le16(return_credits);
6692
6693 chan->ident = l2cap_get_ident(conn);
6694
6695 l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt);
6696}
6697
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006698static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
6699{
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006700 int err;
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006701
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006702 if (!chan->rx_credits) {
6703 BT_ERR("No credits to receive LE L2CAP data");
Johan Hedbergdfd97742014-01-27 15:11:34 -08006704 l2cap_send_disconn_req(chan, ECONNRESET);
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006705 return -ENOBUFS;
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006706 }
6707
6708 if (chan->imtu < skb->len) {
6709 BT_ERR("Too big LE L2CAP PDU");
6710 return -ENOBUFS;
6711 }
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006712
6713 chan->rx_credits--;
6714 BT_DBG("rx_credits %u -> %u", chan->rx_credits + 1, chan->rx_credits);
6715
6716 l2cap_chan_le_send_credits(chan);
6717
Johan Hedbergaac23bf2013-06-01 10:14:57 +03006718 err = 0;
6719
6720 if (!chan->sdu) {
6721 u16 sdu_len;
6722
6723 sdu_len = get_unaligned_le16(skb->data);
6724 skb_pull(skb, L2CAP_SDULEN_SIZE);
6725
6726 BT_DBG("Start of new SDU. sdu_len %u skb->len %u imtu %u",
6727 sdu_len, skb->len, chan->imtu);
6728
6729 if (sdu_len > chan->imtu) {
6730 BT_ERR("Too big LE L2CAP SDU length received");
6731 err = -EMSGSIZE;
6732 goto failed;
6733 }
6734
6735 if (skb->len > sdu_len) {
6736 BT_ERR("Too much LE L2CAP data received");
6737 err = -EINVAL;
6738 goto failed;
6739 }
6740
6741 if (skb->len == sdu_len)
6742 return chan->ops->recv(chan, skb);
6743
6744 chan->sdu = skb;
6745 chan->sdu_len = sdu_len;
6746 chan->sdu_last_frag = skb;
6747
6748 return 0;
6749 }
6750
6751 BT_DBG("SDU fragment. chan->sdu->len %u skb->len %u chan->sdu_len %u",
6752 chan->sdu->len, skb->len, chan->sdu_len);
6753
6754 if (chan->sdu->len + skb->len > chan->sdu_len) {
6755 BT_ERR("Too much LE L2CAP data received");
6756 err = -EINVAL;
6757 goto failed;
6758 }
6759
6760 append_skb_frag(chan->sdu, skb, &chan->sdu_last_frag);
6761 skb = NULL;
6762
6763 if (chan->sdu->len == chan->sdu_len) {
6764 err = chan->ops->recv(chan, chan->sdu);
6765 if (!err) {
6766 chan->sdu = NULL;
6767 chan->sdu_last_frag = NULL;
6768 chan->sdu_len = 0;
6769 }
6770 }
6771
6772failed:
6773 if (err) {
6774 kfree_skb(skb);
6775 kfree_skb(chan->sdu);
6776 chan->sdu = NULL;
6777 chan->sdu_last_frag = NULL;
6778 chan->sdu_len = 0;
6779 }
6780
6781 /* We can't return an error here since we took care of the skb
6782 * freeing internally. An error return would cause the caller to
6783 * do a double-free of the skb.
6784 */
6785 return 0;
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006786}
6787
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006788static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid,
6789 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006791 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03006793 chan = l2cap_get_chan_by_scid(conn, cid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006794 if (!chan) {
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006795 if (cid == L2CAP_CID_A2MP) {
6796 chan = a2mp_channel_create(conn, skb);
6797 if (!chan) {
6798 kfree_skb(skb);
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006799 return;
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006800 }
6801
6802 l2cap_chan_lock(chan);
6803 } else {
6804 BT_DBG("unknown cid 0x%4.4x", cid);
6805 /* Drop packet and return */
6806 kfree_skb(skb);
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006807 return;
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 }
6810
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03006811 BT_DBG("chan %p, len %d", chan, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812
Johan Hedberg315917e2015-02-16 11:42:11 +02006813 /* If we receive data on a fixed channel before the info req/rsp
6814 * procdure is done simply assume that the channel is supported
6815 * and mark it as ready.
6816 */
6817 if (chan->chan_type == L2CAP_CHAN_FIXED)
6818 l2cap_chan_ready(chan);
6819
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03006820 if (chan->state != BT_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 goto drop;
6822
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006823 switch (chan->mode) {
Johan Hedberg38319712013-05-17 12:49:23 +03006824 case L2CAP_MODE_LE_FLOWCTL:
Johan Hedbergfad5fc892013-12-05 09:45:01 +02006825 if (l2cap_le_data_rcv(chan, skb) < 0)
6826 goto drop;
6827
6828 goto done;
6829
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006830 case L2CAP_MODE_BASIC:
6831 /* If socket recv buffers overflows we drop data here
6832 * which is *bad* because L2CAP has to be reliable.
6833 * But we don't have any other choice. L2CAP doesn't
6834 * provide flow control mechanism. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835
Szymon Janc2c96e032014-02-18 20:48:34 +01006836 if (chan->imtu < skb->len) {
6837 BT_ERR("Dropping L2CAP data: receive buffer overflow");
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006838 goto drop;
Szymon Janc2c96e032014-02-18 20:48:34 +01006839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840
Gustavo Padovan80b98022012-05-27 22:27:51 -03006841 if (!chan->ops->recv(chan, skb))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006842 goto done;
6843 break;
6844
6845 case L2CAP_MODE_ERTM:
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03006846 case L2CAP_MODE_STREAMING:
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006847 l2cap_data_rcv(chan, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03006848 goto done;
6849
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006850 default:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006851 BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006852 break;
6853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854
6855drop:
6856 kfree_skb(skb);
6857
6858done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006859 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860}
6861
Andrei Emeltchenko84104b22012-05-31 11:18:56 +03006862static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
6863 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864{
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006865 struct hci_conn *hcon = conn->hcon;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006866 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006868 if (hcon->type != ACL_LINK)
Johan Hedberga24cce12014-08-07 22:56:42 +03006869 goto free_skb;
Marcel Holtmannae4fd2d2013-10-03 00:03:39 -07006870
Johan Hedbergbf20fd42013-05-14 13:23:13 +03006871 chan = l2cap_global_chan_by_psm(0, psm, &hcon->src, &hcon->dst,
6872 ACL_LINK);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006873 if (!chan)
Johan Hedberga24cce12014-08-07 22:56:42 +03006874 goto free_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875
Andrei Emeltchenko5b4ceda2012-02-24 16:35:32 +02006876 BT_DBG("chan %p, len %d", chan, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03006878 if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 goto drop;
6880
Vinicius Costa Gomese13e21d2011-06-17 22:46:27 -03006881 if (chan->imtu < skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 goto drop;
6883
Marcel Holtmann2edf8702013-10-13 12:55:29 -07006884 /* Store remote BD_ADDR and PSM for msg_name */
Johan Hedberga4368ff2015-03-30 23:21:01 +03006885 bacpy(&bt_cb(skb)->l2cap.bdaddr, &hcon->dst);
6886 bt_cb(skb)->l2cap.psm = psm;
Marcel Holtmann2edf8702013-10-13 12:55:29 -07006887
Johan Hedberga24cce12014-08-07 22:56:42 +03006888 if (!chan->ops->recv(chan, skb)) {
6889 l2cap_chan_put(chan);
Andrei Emeltchenko84104b22012-05-31 11:18:56 +03006890 return;
Johan Hedberga24cce12014-08-07 22:56:42 +03006891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892
6893drop:
Johan Hedberga24cce12014-08-07 22:56:42 +03006894 l2cap_chan_put(chan);
6895free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897}
6898
6899static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
6900{
6901 struct l2cap_hdr *lh = (void *) skb->data;
Johan Hedberg61a939c2014-01-17 20:45:11 +02006902 struct hci_conn *hcon = conn->hcon;
Al Viro8e036fc2007-07-29 00:16:36 -07006903 u16 cid, len;
6904 __le16 psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905
Johan Hedberg61a939c2014-01-17 20:45:11 +02006906 if (hcon->state != BT_CONNECTED) {
6907 BT_DBG("queueing pending rx skb");
6908 skb_queue_tail(&conn->pending_rx, skb);
6909 return;
6910 }
6911
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 skb_pull(skb, L2CAP_HDR_SIZE);
6913 cid = __le16_to_cpu(lh->cid);
6914 len = __le16_to_cpu(lh->len);
6915
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006916 if (len != skb->len) {
6917 kfree_skb(skb);
6918 return;
6919 }
6920
Johan Hedberg9e1d7e12014-07-06 11:03:36 +03006921 /* Since we can't actively block incoming LE connections we must
6922 * at least ensure that we ignore incoming data from them.
6923 */
6924 if (hcon->type == LE_LINK &&
Johan Hedbergdcc36c12014-07-09 12:59:13 +03006925 hci_bdaddr_list_lookup(&hcon->hdev->blacklist, &hcon->dst,
Johan Hedberga250e042015-01-15 13:06:44 +02006926 bdaddr_dst_type(hcon))) {
Johan Hedberge4931502014-07-02 09:36:21 +03006927 kfree_skb(skb);
6928 return;
6929 }
6930
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931 BT_DBG("len %d, cid 0x%4.4x", len, cid);
6932
6933 switch (cid) {
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -03006934 case L2CAP_CID_SIGNALING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 l2cap_sig_channel(conn, skb);
6936 break;
6937
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -03006938 case L2CAP_CID_CONN_LESS:
Andrei Emeltchenko097db762012-03-09 14:16:17 +02006939 psm = get_unaligned((__le16 *) skb->data);
Andrei Emeltchenko0181a702012-05-29 10:04:05 +03006940 skb_pull(skb, L2CAP_PSMLEN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 l2cap_conless_channel(conn, psm, skb);
6942 break;
6943
Marcel Holtmanna2877622013-10-02 23:46:54 -07006944 case L2CAP_CID_LE_SIGNALING:
6945 l2cap_le_sig_channel(conn, skb);
6946 break;
6947
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 default:
6949 l2cap_data_channel(conn, cid, skb);
6950 break;
6951 }
6952}
6953
Johan Hedberg61a939c2014-01-17 20:45:11 +02006954static void process_pending_rx(struct work_struct *work)
6955{
6956 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
6957 pending_rx_work);
6958 struct sk_buff *skb;
6959
6960 BT_DBG("");
6961
6962 while ((skb = skb_dequeue(&conn->pending_rx)))
6963 l2cap_recv_frame(conn, skb);
6964}
6965
Johan Hedberg162b49e2014-01-17 20:45:10 +02006966static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon)
6967{
6968 struct l2cap_conn *conn = hcon->l2cap_data;
6969 struct hci_chan *hchan;
6970
6971 if (conn)
6972 return conn;
6973
6974 hchan = hci_chan_create(hcon);
6975 if (!hchan)
6976 return NULL;
6977
Johan Hedberg27f70f32014-07-21 10:50:06 +03006978 conn = kzalloc(sizeof(*conn), GFP_KERNEL);
Johan Hedberg162b49e2014-01-17 20:45:10 +02006979 if (!conn) {
6980 hci_chan_del(hchan);
6981 return NULL;
6982 }
6983
6984 kref_init(&conn->ref);
6985 hcon->l2cap_data = conn;
Johan Hedberg51bb84572014-08-15 21:06:57 +03006986 conn->hcon = hci_conn_get(hcon);
Johan Hedberg162b49e2014-01-17 20:45:10 +02006987 conn->hchan = hchan;
6988
6989 BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan);
6990
6991 switch (hcon->type) {
6992 case LE_LINK:
6993 if (hcon->hdev->le_mtu) {
6994 conn->mtu = hcon->hdev->le_mtu;
6995 break;
6996 }
6997 /* fall through */
6998 default:
6999 conn->mtu = hcon->hdev->acl_mtu;
7000 break;
7001 }
7002
7003 conn->feat_mask = 0;
7004
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02007005 conn->local_fixed_chan = L2CAP_FC_SIG_BREDR | L2CAP_FC_CONNLESS;
7006
7007 if (hcon->type == ACL_LINK &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007008 hci_dev_test_flag(hcon->hdev, HCI_HS_ENABLED))
Johan Hedberg0bd49fc2014-12-02 10:09:26 +02007009 conn->local_fixed_chan |= L2CAP_FC_A2MP;
Johan Hedberg162b49e2014-01-17 20:45:10 +02007010
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007011 if (hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED) &&
Marcel Holtmannf9be9e82014-12-06 00:35:45 +01007012 (bredr_sc_enabled(hcon->hdev) ||
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07007013 hci_dev_test_flag(hcon->hdev, HCI_FORCE_BREDR_SMP)))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03007014 conn->local_fixed_chan |= L2CAP_FC_SMP_BREDR;
7015
Marcel Holtmann5a54e7c2014-07-13 20:50:15 +02007016 mutex_init(&conn->ident_lock);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007017 mutex_init(&conn->chan_lock);
7018
7019 INIT_LIST_HEAD(&conn->chan_l);
7020 INIT_LIST_HEAD(&conn->users);
7021
Johan Hedberg276d8072014-08-11 22:06:41 +03007022 INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007023
Johan Hedberg61a939c2014-01-17 20:45:11 +02007024 skb_queue_head_init(&conn->pending_rx);
7025 INIT_WORK(&conn->pending_rx_work, process_pending_rx);
Johan Hedbergf3d82d02014-09-05 22:19:50 +03007026 INIT_WORK(&conn->id_addr_update_work, l2cap_conn_update_id_addr);
Johan Hedberg61a939c2014-01-17 20:45:11 +02007027
Johan Hedberg162b49e2014-01-17 20:45:10 +02007028 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
7029
7030 return conn;
7031}
7032
7033static bool is_valid_psm(u16 psm, u8 dst_type) {
7034 if (!psm)
7035 return false;
7036
7037 if (bdaddr_type_is_le(dst_type))
7038 return (psm <= 0x00ff);
7039
7040 /* PSM must be odd and lsb of upper byte must be 0 */
7041 return ((psm & 0x0101) == 0x0001);
7042}
7043
7044int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
7045 bdaddr_t *dst, u8 dst_type)
7046{
7047 struct l2cap_conn *conn;
7048 struct hci_conn *hcon;
7049 struct hci_dev *hdev;
Johan Hedberg162b49e2014-01-17 20:45:10 +02007050 int err;
7051
7052 BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", &chan->src, dst,
7053 dst_type, __le16_to_cpu(psm));
7054
7055 hdev = hci_get_route(dst, &chan->src);
7056 if (!hdev)
7057 return -EHOSTUNREACH;
7058
7059 hci_dev_lock(hdev);
7060
Johan Hedberg162b49e2014-01-17 20:45:10 +02007061 if (!is_valid_psm(__le16_to_cpu(psm), dst_type) && !cid &&
7062 chan->chan_type != L2CAP_CHAN_RAW) {
7063 err = -EINVAL;
7064 goto done;
7065 }
7066
Johan Hedberg21626e62014-01-24 10:35:41 +02007067 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !psm) {
7068 err = -EINVAL;
7069 goto done;
7070 }
7071
7072 if (chan->chan_type == L2CAP_CHAN_FIXED && !cid) {
Johan Hedberg162b49e2014-01-17 20:45:10 +02007073 err = -EINVAL;
7074 goto done;
7075 }
7076
7077 switch (chan->mode) {
7078 case L2CAP_MODE_BASIC:
7079 break;
7080 case L2CAP_MODE_LE_FLOWCTL:
7081 l2cap_le_flowctl_init(chan);
7082 break;
7083 case L2CAP_MODE_ERTM:
7084 case L2CAP_MODE_STREAMING:
7085 if (!disable_ertm)
7086 break;
7087 /* fall through */
7088 default:
Johan Hedbergbeb19e42014-07-18 11:15:26 +03007089 err = -EOPNOTSUPP;
Johan Hedberg162b49e2014-01-17 20:45:10 +02007090 goto done;
7091 }
7092
7093 switch (chan->state) {
7094 case BT_CONNECT:
7095 case BT_CONNECT2:
7096 case BT_CONFIG:
7097 /* Already connecting */
7098 err = 0;
7099 goto done;
7100
7101 case BT_CONNECTED:
7102 /* Already connected */
7103 err = -EISCONN;
7104 goto done;
7105
7106 case BT_OPEN:
7107 case BT_BOUND:
7108 /* Can connect */
7109 break;
7110
7111 default:
7112 err = -EBADFD;
7113 goto done;
7114 }
7115
7116 /* Set destination address and psm */
7117 bacpy(&chan->dst, dst);
7118 chan->dst_type = dst_type;
7119
7120 chan->psm = psm;
7121 chan->dcid = cid;
7122
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007123 if (bdaddr_type_is_le(dst_type)) {
7124 /* Convert from L2CAP channel address type to HCI address type
7125 */
7126 if (dst_type == BDADDR_LE_PUBLIC)
7127 dst_type = ADDR_LE_DEV_PUBLIC;
7128 else
7129 dst_type = ADDR_LE_DEV_RANDOM;
7130
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007131 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedberg0ad06aa2015-11-11 14:44:57 +02007132 hcon = hci_connect_le(hdev, dst, dst_type,
7133 chan->sec_level,
7134 HCI_LE_CONN_TIMEOUT,
7135 HCI_ROLE_SLAVE);
Johan Hedberge804d252014-07-16 11:42:28 +03007136 else
Johan Hedberg0ad06aa2015-11-11 14:44:57 +02007137 hcon = hci_connect_le_scan(hdev, dst, dst_type,
7138 chan->sec_level,
7139 HCI_LE_CONN_TIMEOUT);
Johan Hedbergcdd62752014-07-07 15:02:28 +03007140
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007141 } else {
Johan Hedbergd93375a2014-07-07 15:02:27 +03007142 u8 auth_type = l2cap_get_auth_type(chan);
Andre Guedes04a6c582014-02-26 20:21:44 -03007143 hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03007144 }
Johan Hedberg162b49e2014-01-17 20:45:10 +02007145
7146 if (IS_ERR(hcon)) {
7147 err = PTR_ERR(hcon);
7148 goto done;
7149 }
7150
7151 conn = l2cap_conn_add(hcon);
7152 if (!conn) {
7153 hci_conn_drop(hcon);
7154 err = -ENOMEM;
7155 goto done;
7156 }
7157
Johan Hedberg02e246ae2014-10-02 10:16:22 +03007158 mutex_lock(&conn->chan_lock);
7159 l2cap_chan_lock(chan);
7160
Johan Hedberg162b49e2014-01-17 20:45:10 +02007161 if (cid && __l2cap_get_chan_by_dcid(conn, cid)) {
7162 hci_conn_drop(hcon);
7163 err = -EBUSY;
Johan Hedberg02e246ae2014-10-02 10:16:22 +03007164 goto chan_unlock;
Johan Hedberg162b49e2014-01-17 20:45:10 +02007165 }
7166
7167 /* Update source addr of the socket */
7168 bacpy(&chan->src, &hcon->src);
Johan Hedberga250e042015-01-15 13:06:44 +02007169 chan->src_type = bdaddr_src_type(hcon);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007170
Johan Hedberg02e246ae2014-10-02 10:16:22 +03007171 __l2cap_chan_add(conn, chan);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007172
7173 /* l2cap_chan_add takes its own ref so we can drop this one */
7174 hci_conn_drop(hcon);
7175
7176 l2cap_state_change(chan, BT_CONNECT);
7177 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
7178
Johan Hedberg61202e42014-01-28 15:16:48 -08007179 /* Release chan->sport so that it can be reused by other
7180 * sockets (as it's only used for listening sockets).
7181 */
7182 write_lock(&chan_list_lock);
7183 chan->sport = 0;
7184 write_unlock(&chan_list_lock);
7185
Johan Hedberg162b49e2014-01-17 20:45:10 +02007186 if (hcon->state == BT_CONNECTED) {
7187 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
7188 __clear_chan_timer(chan);
Johan Hedberge7cafc42014-07-17 15:35:38 +03007189 if (l2cap_chan_check_security(chan, true))
Johan Hedberg162b49e2014-01-17 20:45:10 +02007190 l2cap_state_change(chan, BT_CONNECTED);
7191 } else
7192 l2cap_do_start(chan);
7193 }
7194
7195 err = 0;
7196
Johan Hedberg02e246ae2014-10-02 10:16:22 +03007197chan_unlock:
Johan Hedberg162b49e2014-01-17 20:45:10 +02007198 l2cap_chan_unlock(chan);
Johan Hedberg02e246ae2014-10-02 10:16:22 +03007199 mutex_unlock(&conn->chan_lock);
7200done:
Johan Hedberg162b49e2014-01-17 20:45:10 +02007201 hci_dev_unlock(hdev);
7202 hci_dev_put(hdev);
7203 return err;
7204}
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03007205EXPORT_SYMBOL_GPL(l2cap_chan_connect);
Johan Hedberg162b49e2014-01-17 20:45:10 +02007206
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207/* ---- L2CAP interface with lower layer (HCI) ---- */
7208
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007209int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210{
7211 int exact = 0, lm1 = 0, lm2 = 0;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007212 struct l2cap_chan *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03007214 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215
7216 /* Find listening sockets and check their link_mode */
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007217 read_lock(&chan_list_lock);
7218 list_for_each_entry(c, &chan_list, global_l) {
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03007219 if (c->state != BT_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 continue;
7221
Marcel Holtmann7eafc592013-10-13 08:12:47 -07007222 if (!bacmp(&c->src, &hdev->bdaddr)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007223 lm1 |= HCI_LM_ACCEPT;
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +03007224 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007225 lm1 |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 exact++;
Marcel Holtmann7eafc592013-10-13 08:12:47 -07007227 } else if (!bacmp(&c->src, BDADDR_ANY)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007228 lm2 |= HCI_LM_ACCEPT;
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +03007229 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01007230 lm2 |= HCI_LM_MASTER;
7231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 }
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007233 read_unlock(&chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234
7235 return exact ? lm1 : lm2;
7236}
7237
Johan Hedberge760ec12014-08-07 22:56:47 +03007238/* Find the next fixed channel in BT_LISTEN state, continue iteration
7239 * from an existing channel in the list or from the beginning of the
7240 * global list (by passing NULL as first parameter).
7241 */
7242static struct l2cap_chan *l2cap_global_fixed_chan(struct l2cap_chan *c,
Johan Hedberg327a7192015-01-15 13:06:45 +02007243 struct hci_conn *hcon)
Johan Hedberge760ec12014-08-07 22:56:47 +03007244{
Johan Hedberg327a7192015-01-15 13:06:45 +02007245 u8 src_type = bdaddr_src_type(hcon);
7246
Johan Hedberge760ec12014-08-07 22:56:47 +03007247 read_lock(&chan_list_lock);
7248
7249 if (c)
7250 c = list_next_entry(c, global_l);
7251 else
7252 c = list_entry(chan_list.next, typeof(*c), global_l);
7253
7254 list_for_each_entry_from(c, &chan_list, global_l) {
7255 if (c->chan_type != L2CAP_CHAN_FIXED)
7256 continue;
7257 if (c->state != BT_LISTEN)
7258 continue;
Johan Hedberg327a7192015-01-15 13:06:45 +02007259 if (bacmp(&c->src, &hcon->src) && bacmp(&c->src, BDADDR_ANY))
Johan Hedberge760ec12014-08-07 22:56:47 +03007260 continue;
Johan Hedberg327a7192015-01-15 13:06:45 +02007261 if (src_type != c->src_type)
Johan Hedberg54a1b622014-08-07 22:56:48 +03007262 continue;
Johan Hedberge760ec12014-08-07 22:56:47 +03007263
7264 l2cap_chan_hold(c);
7265 read_unlock(&chan_list_lock);
7266 return c;
7267 }
7268
7269 read_unlock(&chan_list_lock);
7270
7271 return NULL;
7272}
7273
Johan Hedberg539c4962015-02-18 14:53:57 +02007274static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275{
Johan Hedberge760ec12014-08-07 22:56:47 +03007276 struct hci_dev *hdev = hcon->hdev;
Marcel Holtmann01394182006-07-03 10:02:46 +02007277 struct l2cap_conn *conn;
Johan Hedberge760ec12014-08-07 22:56:47 +03007278 struct l2cap_chan *pchan;
7279 u8 dst_type;
Marcel Holtmann01394182006-07-03 10:02:46 +02007280
Johan Hedberg539c4962015-02-18 14:53:57 +02007281 if (hcon->type != ACL_LINK && hcon->type != LE_LINK)
7282 return;
7283
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03007284 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007286 if (status) {
Joe Perchese1750722011-06-29 18:18:29 -07007287 l2cap_conn_del(hcon, bt_to_errno(status));
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007288 return;
Andrei Emeltchenkoba6fc312012-10-31 15:46:26 +02007289 }
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007290
7291 conn = l2cap_conn_add(hcon);
7292 if (!conn)
7293 return;
7294
Johan Hedberga250e042015-01-15 13:06:44 +02007295 dst_type = bdaddr_dst_type(hcon);
Johan Hedberge760ec12014-08-07 22:56:47 +03007296
7297 /* If device is blocked, do not create channels for it */
7298 if (hci_bdaddr_list_lookup(&hdev->blacklist, &hcon->dst, dst_type))
7299 return;
7300
7301 /* Find fixed channels and notify them of the new connection. We
7302 * use multiple individual lookups, continuing each time where
7303 * we left off, because the list lock would prevent calling the
7304 * potentially sleeping l2cap_chan_lock() function.
7305 */
Johan Hedberg327a7192015-01-15 13:06:45 +02007306 pchan = l2cap_global_fixed_chan(NULL, hcon);
Johan Hedberge760ec12014-08-07 22:56:47 +03007307 while (pchan) {
7308 struct l2cap_chan *chan, *next;
7309
7310 /* Client fixed channels should override server ones */
7311 if (__l2cap_get_chan_by_dcid(conn, pchan->scid))
7312 goto next;
7313
7314 l2cap_chan_lock(pchan);
7315 chan = pchan->ops->new_connection(pchan);
7316 if (chan) {
7317 bacpy(&chan->src, &hcon->src);
7318 bacpy(&chan->dst, &hcon->dst);
Johan Hedberga250e042015-01-15 13:06:44 +02007319 chan->src_type = bdaddr_src_type(hcon);
Johan Hedberge760ec12014-08-07 22:56:47 +03007320 chan->dst_type = dst_type;
7321
7322 __l2cap_chan_add(conn, chan);
7323 }
7324
7325 l2cap_chan_unlock(pchan);
7326next:
Johan Hedberg327a7192015-01-15 13:06:45 +02007327 next = l2cap_global_fixed_chan(pchan, hcon);
Johan Hedberge760ec12014-08-07 22:56:47 +03007328 l2cap_chan_put(pchan);
7329 pchan = next;
7330 }
7331
Johan Hedbergdc0f5082014-08-07 22:56:46 +03007332 l2cap_conn_ready(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333}
7334
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007335int l2cap_disconn_ind(struct hci_conn *hcon)
Marcel Holtmann2950f212009-02-12 14:02:50 +01007336{
7337 struct l2cap_conn *conn = hcon->l2cap_data;
7338
7339 BT_DBG("hcon %p", hcon);
7340
Ulisses Furquim686ebf22011-12-21 10:11:33 -02007341 if (!conn)
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02007342 return HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +01007343 return conn->disc_reason;
7344}
7345
Johan Hedberg3a6d5762015-02-18 14:53:58 +02007346static void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347{
Johan Hedberg3a6d5762015-02-18 14:53:58 +02007348 if (hcon->type != ACL_LINK && hcon->type != LE_LINK)
7349 return;
7350
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351 BT_DBG("hcon %p reason %d", hcon, reason);
7352
Joe Perchese1750722011-06-29 18:18:29 -07007353 l2cap_conn_del(hcon, bt_to_errno(reason));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354}
7355
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007356static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007357{
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03007358 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
Marcel Holtmann255c7602009-02-04 21:07:19 +01007359 return;
7360
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007361 if (encrypt == 0x00) {
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007362 if (chan->sec_level == BT_SECURITY_MEDIUM) {
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007363 __set_chan_timer(chan, L2CAP_ENC_TIMEOUT);
Marcel Holtmann7d513e92014-01-15 22:37:40 -08007364 } else if (chan->sec_level == BT_SECURITY_HIGH ||
7365 chan->sec_level == BT_SECURITY_FIPS)
Gustavo F. Padovan0f852722011-05-04 19:42:50 -03007366 l2cap_chan_close(chan, ECONNREFUSED);
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007367 } else {
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007368 if (chan->sec_level == BT_SECURITY_MEDIUM)
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -03007369 __clear_chan_timer(chan);
Marcel Holtmannf62e4322009-01-15 21:58:44 +01007370 }
7371}
7372
Johan Hedberg354fe802015-02-18 14:53:56 +02007373static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374{
Marcel Holtmann40be4922008-07-14 20:13:50 +02007375 struct l2cap_conn *conn = hcon->l2cap_data;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03007376 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377
Marcel Holtmann01394182006-07-03 10:02:46 +02007378 if (!conn)
Johan Hedberg354fe802015-02-18 14:53:56 +02007379 return;
Marcel Holtmann01394182006-07-03 10:02:46 +02007380
Andrei Emeltchenko89d8b402012-07-10 15:27:51 +03007381 BT_DBG("conn %p status 0x%2.2x encrypt %u", conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007383 mutex_lock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007385 list_for_each_entry(chan, &conn->chan_l, list) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007386 l2cap_chan_lock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387
Andrei Emeltchenko89d8b402012-07-10 15:27:51 +03007388 BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid,
7389 state_to_string(chan->state));
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007390
Johan Hedberg2338a7e2014-01-24 10:35:40 +02007391 if (chan->scid == L2CAP_CID_A2MP) {
Andrei Emeltchenko78eb2f92012-07-19 17:03:47 +03007392 l2cap_chan_unlock(chan);
7393 continue;
7394 }
7395
Johan Hedberg191eb392014-08-07 22:56:45 +03007396 if (!status && encrypt)
7397 chan->sec_level = hcon->sec_level;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03007398
Andrei Emeltchenko96eff462012-11-15 18:14:53 +02007399 if (!__l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007400 l2cap_chan_unlock(chan);
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01007401 continue;
7402 }
7403
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03007404 if (!status && (chan->state == BT_CONNECTED ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01007405 chan->state == BT_CONFIG)) {
Marcel Holtmannd97c8992013-10-14 02:53:54 -07007406 chan->ops->resume(chan);
Gustavo F. Padovan43434782011-04-12 18:31:57 -03007407 l2cap_check_encryption(chan, encrypt);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007408 l2cap_chan_unlock(chan);
Marcel Holtmann9719f8a2008-07-14 20:13:45 +02007409 continue;
7410 }
7411
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03007412 if (chan->state == BT_CONNECT) {
Johan Hedberg6d3c15d2013-12-02 22:13:24 +02007413 if (!status)
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03007414 l2cap_start_connection(chan);
Johan Hedberg6d3c15d2013-12-02 22:13:24 +02007415 else
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007416 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
Johan Hedbergfa37c1a2014-11-13 10:55:17 +02007417 } else if (chan->state == BT_CONNECT2 &&
7418 chan->mode != L2CAP_MODE_LE_FLOWCTL) {
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007419 struct l2cap_conn_rsp rsp;
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007420 __u16 res, stat;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007421
7422 if (!status) {
Marcel Holtmannbdc25782013-10-14 02:45:34 -07007423 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007424 res = L2CAP_CR_PEND;
7425 stat = L2CAP_CS_AUTHOR_PEND;
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08007426 chan->ops->defer(chan);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007427 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02007428 l2cap_state_change(chan, BT_CONFIG);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007429 res = L2CAP_CR_SUCCESS;
7430 stat = L2CAP_CS_NO_INFO;
7431 }
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007432 } else {
Gustavo Padovanacdcabf2013-10-21 14:21:39 -02007433 l2cap_state_change(chan, BT_DISCONN);
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08007434 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007435 res = L2CAP_CR_SEC_BLOCK;
7436 stat = L2CAP_CS_NO_INFO;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02007437 }
7438
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03007439 rsp.scid = cpu_to_le16(chan->dcid);
7440 rsp.dcid = cpu_to_le16(chan->scid);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03007441 rsp.result = cpu_to_le16(res);
7442 rsp.status = cpu_to_le16(stat);
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03007443 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01007444 sizeof(rsp), &rsp);
Mat Martineau2d369352012-05-23 14:59:30 -07007445
7446 if (!test_bit(CONF_REQ_SENT, &chan->conf_state) &&
7447 res == L2CAP_CR_SUCCESS) {
7448 char buf[128];
7449 set_bit(CONF_REQ_SENT, &chan->conf_state);
7450 l2cap_send_cmd(conn, l2cap_get_ident(conn),
7451 L2CAP_CONF_REQ,
7452 l2cap_build_conf_req(chan, buf),
7453 buf);
7454 chan->num_conf_req++;
7455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 }
7457
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02007458 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 }
7460
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02007461 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462}
7463
Arron Wang9b4c3332015-06-09 17:47:22 +08007464void l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465{
7466 struct l2cap_conn *conn = hcon->l2cap_data;
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007467 struct l2cap_hdr *hdr;
7468 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469
Andrei Emeltchenko1d13a252012-10-15 11:58:41 +03007470 /* For AMP controller do not create l2cap conn */
Marcel Holtmannca8bee52016-07-05 14:30:14 +02007471 if (!conn && hcon->hdev->dev_type != HCI_PRIMARY)
Andrei Emeltchenko1d13a252012-10-15 11:58:41 +03007472 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02007474 if (!conn)
Claudio Takahasibaf43252013-04-11 13:55:50 -03007475 conn = l2cap_conn_add(hcon);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02007476
7477 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478 goto drop;
7479
7480 BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
7481
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007482 switch (flags) {
7483 case ACL_START:
7484 case ACL_START_NO_FLUSH:
7485 case ACL_COMPLETE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486 if (conn->rx_len) {
7487 BT_ERR("Unexpected start frame (len %d)", skb->len);
7488 kfree_skb(conn->rx_skb);
7489 conn->rx_skb = NULL;
7490 conn->rx_len = 0;
7491 l2cap_conn_unreliable(conn, ECOMM);
7492 }
7493
Andrei Emeltchenkoaae7fe22010-09-15 14:28:43 +03007494 /* Start fragment always begin with Basic L2CAP header */
7495 if (skb->len < L2CAP_HDR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496 BT_ERR("Frame is too short (len %d)", skb->len);
7497 l2cap_conn_unreliable(conn, ECOMM);
7498 goto drop;
7499 }
7500
7501 hdr = (struct l2cap_hdr *) skb->data;
7502 len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
7503
7504 if (len == skb->len) {
7505 /* Complete frame received */
7506 l2cap_recv_frame(conn, skb);
Arron Wang9b4c3332015-06-09 17:47:22 +08007507 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508 }
7509
7510 BT_DBG("Start: total len %d, frag len %d", len, skb->len);
7511
7512 if (skb->len > len) {
7513 BT_ERR("Frame is too long (len %d, expected len %d)",
Gustavo Padovan2d792812012-10-06 10:07:01 +01007514 skb->len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 l2cap_conn_unreliable(conn, ECOMM);
7516 goto drop;
7517 }
7518
7519 /* Allocate skb for the complete frame (with header) */
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03007520 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03007521 if (!conn->rx_skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522 goto drop;
7523
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03007524 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Gustavo Padovan2d792812012-10-06 10:07:01 +01007525 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526 conn->rx_len = len - skb->len;
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007527 break;
7528
7529 case ACL_CONT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
7531
7532 if (!conn->rx_len) {
7533 BT_ERR("Unexpected continuation frame (len %d)", skb->len);
7534 l2cap_conn_unreliable(conn, ECOMM);
7535 goto drop;
7536 }
7537
7538 if (skb->len > conn->rx_len) {
7539 BT_ERR("Fragment is too long (len %d, expected %d)",
Gustavo Padovan2d792812012-10-06 10:07:01 +01007540 skb->len, conn->rx_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541 kfree_skb(conn->rx_skb);
7542 conn->rx_skb = NULL;
7543 conn->rx_len = 0;
7544 l2cap_conn_unreliable(conn, ECOMM);
7545 goto drop;
7546 }
7547
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03007548 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Gustavo Padovan2d792812012-10-06 10:07:01 +01007549 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550 conn->rx_len -= skb->len;
7551
7552 if (!conn->rx_len) {
Johan Hedbergc4e5baf2013-10-10 13:33:37 +02007553 /* Complete frame received. l2cap_recv_frame
7554 * takes ownership of the skb so set the global
7555 * rx_skb pointer to NULL first.
7556 */
7557 struct sk_buff *rx_skb = conn->rx_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558 conn->rx_skb = NULL;
Johan Hedbergc4e5baf2013-10-10 13:33:37 +02007559 l2cap_recv_frame(conn, rx_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 }
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03007561 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 }
7563
7564drop:
7565 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566}
7567
Johan Hedberg354fe802015-02-18 14:53:56 +02007568static struct hci_cb l2cap_cb = {
7569 .name = "L2CAP",
Johan Hedberg539c4962015-02-18 14:53:57 +02007570 .connect_cfm = l2cap_connect_cfm,
Johan Hedberg3a6d5762015-02-18 14:53:58 +02007571 .disconn_cfm = l2cap_disconn_cfm,
Johan Hedberg354fe802015-02-18 14:53:56 +02007572 .security_cfm = l2cap_security_cfm,
7573};
7574
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007575static int l2cap_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007577 struct l2cap_chan *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578
Gustavo F. Padovan333055f2011-12-22 15:14:39 -02007579 read_lock(&chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03007581 list_for_each_entry(c, &chan_list, global_l) {
Marcel Holtmanneeb5a062015-01-14 13:44:21 -08007582 seq_printf(f, "%pMR (%u) %pMR (%u) %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
7583 &c->src, c->src_type, &c->dst, c->dst_type,
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03007584 c->state, __le16_to_cpu(c->psm),
7585 c->scid, c->dcid, c->imtu, c->omtu,
7586 c->sec_level, c->mode);
Andrei Emeltchenko61e1b4b2012-01-19 11:19:50 +02007587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588
Gustavo F. Padovan333055f2011-12-22 15:14:39 -02007589 read_unlock(&chan_list_lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08007590
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592}
7593
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007594static int l2cap_debugfs_open(struct inode *inode, struct file *file)
7595{
7596 return single_open(file, l2cap_debugfs_show, inode->i_private);
7597}
7598
7599static const struct file_operations l2cap_debugfs_fops = {
7600 .open = l2cap_debugfs_open,
7601 .read = seq_read,
7602 .llseek = seq_lseek,
7603 .release = single_release,
7604};
7605
7606static struct dentry *l2cap_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607
Gustavo F. Padovan64274512011-02-07 20:08:52 -02007608int __init l2cap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609{
7610 int err;
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08007611
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02007612 err = l2cap_init_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613 if (err < 0)
7614 return err;
7615
Johan Hedberg354fe802015-02-18 14:53:56 +02007616 hci_register_cb(&l2cap_cb);
7617
Marcel Holtmann1120e4b2013-10-17 17:24:16 -07007618 if (IS_ERR_OR_NULL(bt_debugfs))
7619 return 0;
7620
7621 l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs,
7622 NULL, &l2cap_debugfs_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623
Samuel Ortiz40b93972014-05-14 17:53:35 +02007624 debugfs_create_u16("l2cap_le_max_credits", 0644, bt_debugfs,
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02007625 &le_max_credits);
Samuel Ortiz40b93972014-05-14 17:53:35 +02007626 debugfs_create_u16("l2cap_le_default_mps", 0644, bt_debugfs,
Johan Hedbergf15b8ec2013-12-03 15:08:25 +02007627 &le_default_mps);
7628
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630}
7631
Gustavo F. Padovan64274512011-02-07 20:08:52 -02007632void l2cap_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01007634 debugfs_remove(l2cap_debugfs);
Johan Hedberg354fe802015-02-18 14:53:56 +02007635 hci_unregister_cb(&l2cap_cb);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02007636 l2cap_cleanup_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637}
7638
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03007639module_param(disable_ertm, bool, 0644);
7640MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");