blob: 63fa11109a1c391725d5efec2075c1524d9f1a1a [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>
Anderson Brigliab501d6a2011-06-07 18:46:31 -030039#include <net/bluetooth/smp.h>
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +030040#include <net/bluetooth/a2mp.h>
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +020041#include <net/bluetooth/amp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Mat Martineaud1de6d42012-05-17 20:53:55 -070043bool disable_ertm;
Marcel Holtmannf0709e02007-10-20 13:38:51 +020044
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -070045static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN;
Mat Martineau50a147c2011-11-02 16:18:34 -070046static u8 l2cap_fixed_chan[8] = { L2CAP_FC_L2CAP, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Johannes Bergb5ad8b72011-06-01 08:54:45 +020048static LIST_HEAD(chan_list);
49static DEFINE_RWLOCK(chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +010052 u8 code, u8 ident, u16 dlen, void *data);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -030053static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
Gustavo Padovan2d792812012-10-06 10:07:01 +010054 void *data);
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -030055static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +020056static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Gustavo Padovand6603662012-05-21 13:58:22 -030058static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
Gustavo Padovan2d792812012-10-06 10:07:01 +010059 struct sk_buff_head *skbs, u8 event);
Mat Martineau608bcc62012-05-17 20:53:32 -070060
Marcel Holtmann01394182006-07-03 10:02:46 +020061/* ---- L2CAP channels ---- */
Gustavo F. Padovan71ba0e52011-05-17 14:34:52 -030062
Gustavo Padovan2d792812012-10-06 10:07:01 +010063static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
64 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +020065{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020066 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -030067
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020068 list_for_each_entry(c, &conn->chan_l, list) {
69 if (c->dcid == cid)
70 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +020071 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020072 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +020073}
74
Gustavo Padovan2d792812012-10-06 10:07:01 +010075static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn,
76 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +020077{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020078 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -030079
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020080 list_for_each_entry(c, &conn->chan_l, list) {
81 if (c->scid == cid)
82 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +020083 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020084 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +020085}
86
87/* Find channel with given SCID.
Mat Martineauef191ad2012-05-02 09:42:00 -070088 * Returns locked channel. */
Gustavo Padovan2d792812012-10-06 10:07:01 +010089static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn,
90 u16 cid)
Marcel Holtmann01394182006-07-03 10:02:46 +020091{
Gustavo F. Padovan48454072011-03-25 00:22:30 -030092 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -030093
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020094 mutex_lock(&conn->chan_lock);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -030095 c = __l2cap_get_chan_by_scid(conn, cid);
Mat Martineauef191ad2012-05-02 09:42:00 -070096 if (c)
97 l2cap_chan_lock(c);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +020098 mutex_unlock(&conn->chan_lock);
99
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300100 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +0200101}
102
Mat Martineaub1a130b2012-10-23 15:24:09 -0700103/* Find channel with given DCID.
104 * Returns locked channel.
105 */
106static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
107 u16 cid)
108{
109 struct l2cap_chan *c;
110
111 mutex_lock(&conn->chan_lock);
112 c = __l2cap_get_chan_by_dcid(conn, cid);
113 if (c)
114 l2cap_chan_lock(c);
115 mutex_unlock(&conn->chan_lock);
116
117 return c;
118}
119
Gustavo Padovan2d792812012-10-06 10:07:01 +0100120static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn,
121 u8 ident)
Marcel Holtmann01394182006-07-03 10:02:46 +0200122{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200123 struct l2cap_chan *c;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300124
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200125 list_for_each_entry(c, &conn->chan_l, list) {
126 if (c->ident == ident)
127 return c;
Marcel Holtmann01394182006-07-03 10:02:46 +0200128 }
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200129 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200130}
131
Mat Martineau5b155ef2012-10-23 15:24:14 -0700132static struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn,
133 u8 ident)
134{
135 struct l2cap_chan *c;
136
137 mutex_lock(&conn->chan_lock);
138 c = __l2cap_get_chan_by_ident(conn, ident);
139 if (c)
140 l2cap_chan_lock(c);
141 mutex_unlock(&conn->chan_lock);
142
143 return c;
144}
145
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300146static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300147{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300148 struct l2cap_chan *c;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300149
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300150 list_for_each_entry(c, &chan_list, global_l) {
151 if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src))
Szymon Janc250938c2011-11-16 09:32:22 +0100152 return c;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300153 }
Szymon Janc250938c2011-11-16 09:32:22 +0100154 return NULL;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300155}
156
157int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm)
158{
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300159 int err;
160
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200161 write_lock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300162
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300163 if (psm && __l2cap_global_chan_by_addr(psm, src)) {
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300164 err = -EADDRINUSE;
165 goto done;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300166 }
167
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300168 if (psm) {
169 chan->psm = psm;
170 chan->sport = psm;
171 err = 0;
172 } else {
173 u16 p;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300174
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300175 err = -EINVAL;
176 for (p = 0x1001; p < 0x1100; p += 2)
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300177 if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) {
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300178 chan->psm = cpu_to_le16(p);
179 chan->sport = cpu_to_le16(p);
180 err = 0;
181 break;
182 }
183 }
184
185done:
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200186 write_unlock(&chan_list_lock);
Gustavo F. Padovan73b2ec12011-04-18 19:36:44 -0300187 return err;
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300188}
189
190int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid)
191{
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200192 write_lock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300193
194 chan->scid = scid;
195
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200196 write_unlock(&chan_list_lock);
Gustavo F. Padovan9e4425f2011-04-18 18:38:43 -0300197
198 return 0;
199}
200
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300201static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
Marcel Holtmann01394182006-07-03 10:02:46 +0200202{
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -0300203 u16 cid = L2CAP_CID_DYN_START;
Marcel Holtmann01394182006-07-03 10:02:46 +0200204
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -0300205 for (; cid < L2CAP_CID_DYN_END; cid++) {
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300206 if (!__l2cap_get_chan_by_scid(conn, cid))
Marcel Holtmann01394182006-07-03 10:02:46 +0200207 return cid;
208 }
209
210 return 0;
211}
212
Andrei Emeltchenko0e587be2012-02-21 12:54:57 +0200213static void __l2cap_state_change(struct l2cap_chan *chan, int state)
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300214{
Andrei Emeltchenko42d2d872012-02-17 11:40:57 +0200215 BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state),
Gustavo Padovan2d792812012-10-06 10:07:01 +0100216 state_to_string(state));
Gustavo F. Padovanbadaaa02011-11-23 20:11:46 -0200217
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300218 chan->state = state;
Gustavo Padovan80b98022012-05-27 22:27:51 -0300219 chan->ops->state_change(chan, state);
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300220}
221
Andrei Emeltchenko0e587be2012-02-21 12:54:57 +0200222static void l2cap_state_change(struct l2cap_chan *chan, int state)
223{
224 struct sock *sk = chan->sk;
225
226 lock_sock(sk);
227 __l2cap_state_change(chan, state);
228 release_sock(sk);
229}
230
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +0200231static inline void __l2cap_chan_set_err(struct l2cap_chan *chan, int err)
232{
233 struct sock *sk = chan->sk;
234
235 sk->sk_err = err;
236}
237
238static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err)
239{
240 struct sock *sk = chan->sk;
241
242 lock_sock(sk);
243 __l2cap_chan_set_err(chan, err);
244 release_sock(sk);
245}
246
Mat Martineau4239d162012-05-17 20:53:49 -0700247static void __set_retrans_timer(struct l2cap_chan *chan)
248{
249 if (!delayed_work_pending(&chan->monitor_timer) &&
250 chan->retrans_timeout) {
251 l2cap_set_timer(chan, &chan->retrans_timer,
252 msecs_to_jiffies(chan->retrans_timeout));
253 }
254}
255
256static void __set_monitor_timer(struct l2cap_chan *chan)
257{
258 __clear_retrans_timer(chan);
259 if (chan->monitor_timeout) {
260 l2cap_set_timer(chan, &chan->monitor_timer,
261 msecs_to_jiffies(chan->monitor_timeout));
262 }
263}
264
Mat Martineau608bcc62012-05-17 20:53:32 -0700265static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head,
266 u16 seq)
267{
268 struct sk_buff *skb;
269
270 skb_queue_walk(head, skb) {
271 if (bt_cb(skb)->control.txseq == seq)
272 return skb;
273 }
274
275 return NULL;
276}
277
Mat Martineau3c588192012-04-11 10:48:42 -0700278/* ---- L2CAP sequence number lists ---- */
279
280/* For ERTM, ordered lists of sequence numbers must be tracked for
281 * SREJ requests that are received and for frames that are to be
282 * retransmitted. These seq_list functions implement a singly-linked
283 * list in an array, where membership in the list can also be checked
284 * in constant time. Items can also be added to the tail of the list
285 * and removed from the head in constant time, without further memory
286 * allocs or frees.
287 */
288
289static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size)
290{
291 size_t alloc_size, i;
292
293 /* Allocated size is a power of 2 to map sequence numbers
294 * (which may be up to 14 bits) in to a smaller array that is
295 * sized for the negotiated ERTM transmit windows.
296 */
297 alloc_size = roundup_pow_of_two(size);
298
299 seq_list->list = kmalloc(sizeof(u16) * alloc_size, GFP_KERNEL);
300 if (!seq_list->list)
301 return -ENOMEM;
302
303 seq_list->mask = alloc_size - 1;
304 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
305 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
306 for (i = 0; i < alloc_size; i++)
307 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
308
309 return 0;
310}
311
312static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list)
313{
314 kfree(seq_list->list);
315}
316
317static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list,
318 u16 seq)
319{
320 /* Constant-time check for list membership */
321 return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR;
322}
323
324static u16 l2cap_seq_list_remove(struct l2cap_seq_list *seq_list, u16 seq)
325{
326 u16 mask = seq_list->mask;
327
328 if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) {
329 /* In case someone tries to pop the head of an empty list */
330 return L2CAP_SEQ_LIST_CLEAR;
331 } else if (seq_list->head == seq) {
332 /* Head can be removed in constant time */
333 seq_list->head = seq_list->list[seq & mask];
334 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR;
335
336 if (seq_list->head == L2CAP_SEQ_LIST_TAIL) {
337 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
338 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
339 }
340 } else {
341 /* Walk the list to find the sequence number */
342 u16 prev = seq_list->head;
343 while (seq_list->list[prev & mask] != seq) {
344 prev = seq_list->list[prev & mask];
345 if (prev == L2CAP_SEQ_LIST_TAIL)
346 return L2CAP_SEQ_LIST_CLEAR;
347 }
348
349 /* Unlink the number from the list and clear it */
350 seq_list->list[prev & mask] = seq_list->list[seq & mask];
351 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR;
352 if (seq_list->tail == seq)
353 seq_list->tail = prev;
354 }
355 return seq;
356}
357
358static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list)
359{
360 /* Remove the head in constant time */
361 return l2cap_seq_list_remove(seq_list, seq_list->head);
362}
363
364static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list)
365{
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300366 u16 i;
Mat Martineau3c588192012-04-11 10:48:42 -0700367
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300368 if (seq_list->head == L2CAP_SEQ_LIST_CLEAR)
369 return;
370
371 for (i = 0; i <= seq_list->mask; i++)
372 seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
373
374 seq_list->head = L2CAP_SEQ_LIST_CLEAR;
375 seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
Mat Martineau3c588192012-04-11 10:48:42 -0700376}
377
378static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq)
379{
380 u16 mask = seq_list->mask;
381
382 /* All appends happen in constant time */
383
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300384 if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR)
385 return;
Mat Martineau3c588192012-04-11 10:48:42 -0700386
Gustavo Padovanf522ae32012-05-09 18:28:00 -0300387 if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR)
388 seq_list->head = seq;
389 else
390 seq_list->list[seq_list->tail & mask] = seq;
391
392 seq_list->tail = seq;
393 seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL;
Mat Martineau3c588192012-04-11 10:48:42 -0700394}
395
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300396static void l2cap_chan_timeout(struct work_struct *work)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300397{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300398 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100399 chan_timer.work);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200400 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300401 int reason;
402
Andrei Emeltchenkoe05dcc32012-02-17 11:40:56 +0200403 BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300404
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200405 mutex_lock(&conn->chan_lock);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200406 l2cap_chan_lock(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300407
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300408 if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300409 reason = ECONNREFUSED;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300410 else if (chan->state == BT_CONNECT &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100411 chan->sec_level != BT_SECURITY_SDP)
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300412 reason = ECONNREFUSED;
413 else
414 reason = ETIMEDOUT;
415
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300416 l2cap_chan_close(chan, reason);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300417
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200418 l2cap_chan_unlock(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300419
Gustavo Padovan80b98022012-05-27 22:27:51 -0300420 chan->ops->close(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200421 mutex_unlock(&conn->chan_lock);
422
Ulisses Furquim371fd832011-12-21 20:02:36 -0200423 l2cap_chan_put(chan);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300424}
425
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -0300426struct l2cap_chan *l2cap_chan_create(void)
Marcel Holtmann01394182006-07-03 10:02:46 +0200427{
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300428 struct l2cap_chan *chan;
Marcel Holtmann01394182006-07-03 10:02:46 +0200429
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300430 chan = kzalloc(sizeof(*chan), GFP_ATOMIC);
431 if (!chan)
432 return NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200433
Andrei Emeltchenkoc03b3552012-02-21 12:54:56 +0200434 mutex_init(&chan->lock);
435
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200436 write_lock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300437 list_add(&chan->global_l, &chan_list);
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200438 write_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300439
Gustavo F. Padovan721c4182011-06-23 19:29:58 -0300440 INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout);
Gustavo F. Padovanab078012011-05-02 18:25:01 -0300441
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300442 chan->state = BT_OPEN;
443
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530444 kref_init(&chan->kref);
Gustavo F. Padovan71ba0e52011-05-17 14:34:52 -0300445
Mat Martineau28270112012-05-17 21:14:09 -0700446 /* This flag is cleared in l2cap_chan_ready() */
447 set_bit(CONF_NOT_COMPLETE, &chan->conf_state);
448
Gustavo Padovaneef1d9b2012-03-25 13:59:16 -0300449 BT_DBG("chan %p", chan);
Szymon Jancabc545b2011-11-03 16:05:44 +0100450
Gustavo F. Padovan48454072011-03-25 00:22:30 -0300451 return chan;
Marcel Holtmann01394182006-07-03 10:02:46 +0200452}
453
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530454static void l2cap_chan_destroy(struct kref *kref)
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300455{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530456 struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref);
457
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530458 BT_DBG("chan %p", chan);
459
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200460 write_lock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300461 list_del(&chan->global_l);
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200462 write_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -0300463
Jaganath Kanakkassery4af66c62012-07-13 18:17:55 +0530464 kfree(chan);
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300465}
466
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530467void l2cap_chan_hold(struct l2cap_chan *c)
468{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530469 BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount));
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530470
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530471 kref_get(&c->kref);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530472}
473
474void l2cap_chan_put(struct l2cap_chan *c)
475{
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530476 BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount));
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530477
Syam Sidhardhan144ad332012-07-27 23:51:21 +0530478 kref_put(&c->kref, l2cap_chan_destroy);
Jaganath Kanakkassery30648372012-07-13 18:17:54 +0530479}
480
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300481void l2cap_chan_set_defaults(struct l2cap_chan *chan)
482{
483 chan->fcs = L2CAP_FCS_CRC16;
484 chan->max_tx = L2CAP_DEFAULT_MAX_TX;
485 chan->tx_win = L2CAP_DEFAULT_TX_WINDOW;
486 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
Mat Martineauc20f8e32012-07-10 05:47:07 -0700487 chan->ack_win = L2CAP_DEFAULT_TX_WINDOW;
Andrei Emeltchenkobd4b1652012-03-28 16:31:25 +0300488 chan->sec_level = BT_SECURITY_LOW;
489
490 set_bit(FLAG_FORCE_ACTIVE, &chan->flags);
491}
492
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +0300493void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
Marcel Holtmann01394182006-07-03 10:02:46 +0200494{
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -0300495 BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
Andrei Emeltchenko097db762012-03-09 14:16:17 +0200496 __le16_to_cpu(chan->psm), chan->dcid);
Marcel Holtmann01394182006-07-03 10:02:46 +0200497
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +0200498 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100499
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300500 chan->conn = conn;
Marcel Holtmann01394182006-07-03 10:02:46 +0200501
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200502 switch (chan->chan_type) {
503 case L2CAP_CHAN_CONN_ORIENTED:
Ville Tervob62f3282011-02-10 22:38:50 -0300504 if (conn->hcon->type == LE_LINK) {
505 /* LE connection */
Andre Guedes6fcb06a2012-05-31 17:01:33 -0300506 chan->omtu = L2CAP_DEFAULT_MTU;
Johan Hedberg9f22398c2013-04-29 19:35:38 +0300507 if (chan->dcid == L2CAP_CID_ATT)
508 chan->scid = L2CAP_CID_ATT;
509 else
510 chan->scid = l2cap_alloc_cid(conn);
Ville Tervob62f3282011-02-10 22:38:50 -0300511 } else {
512 /* Alloc CID for connection-oriented socket */
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300513 chan->scid = l2cap_alloc_cid(conn);
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300514 chan->omtu = L2CAP_DEFAULT_MTU;
Ville Tervob62f3282011-02-10 22:38:50 -0300515 }
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200516 break;
517
518 case L2CAP_CHAN_CONN_LESS:
Marcel Holtmann01394182006-07-03 10:02:46 +0200519 /* Connectionless socket */
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300520 chan->scid = L2CAP_CID_CONN_LESS;
521 chan->dcid = L2CAP_CID_CONN_LESS;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300522 chan->omtu = L2CAP_DEFAULT_MTU;
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200523 break;
524
Andrei Emeltchenko416fa752012-05-29 13:59:16 +0300525 case L2CAP_CHAN_CONN_FIX_A2MP:
526 chan->scid = L2CAP_CID_A2MP;
527 chan->dcid = L2CAP_CID_A2MP;
528 chan->omtu = L2CAP_A2MP_DEFAULT_MTU;
529 chan->imtu = L2CAP_A2MP_DEFAULT_MTU;
530 break;
531
Andrei Emeltchenko54911202012-02-06 15:04:00 +0200532 default:
Marcel Holtmann01394182006-07-03 10:02:46 +0200533 /* Raw socket can send/recv signalling messages only */
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300534 chan->scid = L2CAP_CID_SIGNALING;
535 chan->dcid = L2CAP_CID_SIGNALING;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -0300536 chan->omtu = L2CAP_DEFAULT_MTU;
Marcel Holtmann01394182006-07-03 10:02:46 +0200537 }
538
Andrei Emeltchenko8f7975b2011-10-13 16:18:54 +0300539 chan->local_id = L2CAP_BESTEFFORT_ID;
540 chan->local_stype = L2CAP_SERV_BESTEFFORT;
541 chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE;
542 chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME;
543 chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT;
Andrei Emeltchenko8936fa62012-10-08 11:14:41 +0300544 chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO;
Andrei Emeltchenko8f7975b2011-10-13 16:18:54 +0300545
Ulisses Furquim371fd832011-12-21 20:02:36 -0200546 l2cap_chan_hold(chan);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300547
Johan Hedberg5ee98912013-04-29 19:35:43 +0300548 hci_conn_hold(conn->hcon);
549
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200550 list_add(&chan->list, &conn->chan_l);
Andrei Emeltchenko643162a2012-02-22 17:11:55 +0200551}
552
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300553void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan)
Andrei Emeltchenko643162a2012-02-22 17:11:55 +0200554{
555 mutex_lock(&conn->chan_lock);
556 __l2cap_chan_add(conn, chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200557 mutex_unlock(&conn->chan_lock);
Marcel Holtmann01394182006-07-03 10:02:46 +0200558}
559
Andrei Emeltchenko466f8002012-05-29 13:59:01 +0300560void l2cap_chan_del(struct l2cap_chan *chan, int err)
Marcel Holtmann01394182006-07-03 10:02:46 +0200561{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300562 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann01394182006-07-03 10:02:46 +0200563
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -0300564 __clear_chan_timer(chan);
Marcel Holtmann01394182006-07-03 10:02:46 +0200565
Gustavo F. Padovan49208c92011-04-04 15:59:54 -0300566 BT_DBG("chan %p, conn %p, err %d", chan, conn, err);
Marcel Holtmann01394182006-07-03 10:02:46 +0200567
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900568 if (conn) {
Andrei Emeltchenko56f60982012-10-15 11:58:44 +0300569 struct amp_mgr *mgr = conn->hcon->amp_mgr;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300570 /* Delete from channel list */
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +0200571 list_del(&chan->list);
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -0200572
Ulisses Furquim371fd832011-12-21 20:02:36 -0200573 l2cap_chan_put(chan);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -0300574
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300575 chan->conn = NULL;
Andrei Emeltchenko3cabbfd2012-05-31 11:01:37 +0300576
577 if (chan->chan_type != L2CAP_CHAN_CONN_FIX_A2MP)
David Herrmann76a68ba2013-04-06 20:28:37 +0200578 hci_conn_drop(conn->hcon);
Andrei Emeltchenko56f60982012-10-15 11:58:44 +0300579
580 if (mgr && mgr->bredr_chan == chan)
581 mgr->bredr_chan = NULL;
Marcel Holtmann01394182006-07-03 10:02:46 +0200582 }
583
Andrei Emeltchenko419e08c2012-10-31 15:46:34 +0200584 if (chan->hs_hchan) {
585 struct hci_chan *hs_hchan = chan->hs_hchan;
586
587 BT_DBG("chan %p disconnect hs_hchan %p", chan, hs_hchan);
588 amp_disconnect_logical_link(hs_hchan);
589 }
590
Gustavo Padovanb699ec02012-10-06 11:51:54 +0100591 chan->ops->teardown(chan, err);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +0200592
Mat Martineau28270112012-05-17 21:14:09 -0700593 if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state))
Gustavo F. Padovan6ff5abb2011-04-25 15:10:41 -0300594 return;
Gustavo F. Padovan2ead70b2011-04-01 15:13:36 -0300595
Gustavo Padovanee556f62012-05-18 20:22:38 -0300596 switch(chan->mode) {
597 case L2CAP_MODE_BASIC:
598 break;
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300599
Gustavo Padovanee556f62012-05-18 20:22:38 -0300600 case L2CAP_MODE_ERTM:
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -0300601 __clear_retrans_timer(chan);
602 __clear_monitor_timer(chan);
603 __clear_ack_timer(chan);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300604
Gustavo F. Padovanf1c67752011-03-25 20:36:10 -0300605 skb_queue_purge(&chan->srej_q);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300606
Mat Martineau3c588192012-04-11 10:48:42 -0700607 l2cap_seq_list_free(&chan->srej_list);
608 l2cap_seq_list_free(&chan->retrans_list);
Gustavo Padovanee556f62012-05-18 20:22:38 -0300609
610 /* fall through */
611
612 case L2CAP_MODE_STREAMING:
613 skb_queue_purge(&chan->tx_q);
614 break;
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -0300615 }
Gustavo Padovanee556f62012-05-18 20:22:38 -0300616
617 return;
Marcel Holtmann01394182006-07-03 10:02:46 +0200618}
619
Gustavo F. Padovan0f852722011-05-04 19:42:50 -0300620void l2cap_chan_close(struct l2cap_chan *chan, int reason)
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300621{
622 struct l2cap_conn *conn = chan->conn;
623 struct sock *sk = chan->sk;
624
Gustavo Padovan2d792812012-10-06 10:07:01 +0100625 BT_DBG("chan %p state %s sk %p", chan, state_to_string(chan->state),
626 sk);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300627
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300628 switch (chan->state) {
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300629 case BT_LISTEN:
Gustavo Padovanb699ec02012-10-06 11:51:54 +0100630 chan->ops->teardown(chan, 0);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300631 break;
632
633 case BT_CONNECTED:
634 case BT_CONFIG:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300635 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100636 conn->hcon->type == ACL_LINK) {
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -0300637 __set_chan_timer(chan, sk->sk_sndtimeo);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +0200638 l2cap_send_disconn_req(chan, reason);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300639 } else
640 l2cap_chan_del(chan, reason);
641 break;
642
643 case BT_CONNECT2:
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300644 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100645 conn->hcon->type == ACL_LINK) {
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300646 struct l2cap_conn_rsp rsp;
647 __u16 result;
648
Gustavo Padovanc5daa682012-05-16 12:17:10 -0300649 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300650 result = L2CAP_CR_SEC_BLOCK;
651 else
652 result = L2CAP_CR_BAD_PSM;
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -0300653 l2cap_state_change(chan, BT_DISCONN);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300654
655 rsp.scid = cpu_to_le16(chan->dcid);
656 rsp.dcid = cpu_to_le16(chan->scid);
657 rsp.result = cpu_to_le16(result);
Andrei Emeltchenkoac734982012-05-24 15:42:51 +0300658 rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300659 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100660 sizeof(rsp), &rsp);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300661 }
662
663 l2cap_chan_del(chan, reason);
664 break;
665
666 case BT_CONNECT:
667 case BT_DISCONN:
668 l2cap_chan_del(chan, reason);
669 break;
670
671 default:
Gustavo Padovanb699ec02012-10-06 11:51:54 +0100672 chan->ops->teardown(chan, 0);
Gustavo F. Padovan4519de92011-04-28 17:55:53 -0300673 break;
674 }
675}
676
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300677static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
Johan Hedberg8556edd32011-01-19 12:06:50 +0530678{
Gustavo F. Padovan715ec002011-05-02 17:13:55 -0300679 if (chan->chan_type == L2CAP_CHAN_RAW) {
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300680 switch (chan->sec_level) {
Johan Hedberg8556edd32011-01-19 12:06:50 +0530681 case BT_SECURITY_HIGH:
682 return HCI_AT_DEDICATED_BONDING_MITM;
683 case BT_SECURITY_MEDIUM:
684 return HCI_AT_DEDICATED_BONDING;
685 default:
686 return HCI_AT_NO_BONDING;
687 }
Andrei Emeltchenko2983fd62012-05-24 15:42:50 +0300688 } else if (chan->psm == __constant_cpu_to_le16(L2CAP_PSM_SDP)) {
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300689 if (chan->sec_level == BT_SECURITY_LOW)
690 chan->sec_level = BT_SECURITY_SDP;
Johan Hedberg8556edd32011-01-19 12:06:50 +0530691
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300692 if (chan->sec_level == BT_SECURITY_HIGH)
Johan Hedberg8556edd32011-01-19 12:06:50 +0530693 return HCI_AT_NO_BONDING_MITM;
694 else
695 return HCI_AT_NO_BONDING;
696 } else {
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300697 switch (chan->sec_level) {
Johan Hedberg8556edd32011-01-19 12:06:50 +0530698 case BT_SECURITY_HIGH:
699 return HCI_AT_GENERAL_BONDING_MITM;
700 case BT_SECURITY_MEDIUM:
701 return HCI_AT_GENERAL_BONDING;
702 default:
703 return HCI_AT_NO_BONDING;
704 }
705 }
706}
707
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200708/* Service level security */
Gustavo F. Padovand45fc422011-11-05 19:54:24 -0200709int l2cap_chan_check_security(struct l2cap_chan *chan)
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200710{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -0300711 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100712 __u8 auth_type;
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200713
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300714 auth_type = l2cap_get_auth_type(chan);
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100715
Gustavo F. Padovan43434782011-04-12 18:31:57 -0300716 return hci_conn_security(conn->hcon, chan->sec_level, auth_type);
Marcel Holtmann79d554a2008-07-14 20:13:44 +0200717}
718
Johannes Bergb5ad8b72011-06-01 08:54:45 +0200719static u8 l2cap_get_ident(struct l2cap_conn *conn)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200720{
721 u8 id;
722
723 /* Get next available identificator.
724 * 1 - 128 are used by kernel.
725 * 129 - 199 are reserved.
726 * 200 - 254 are used by utilities like l2ping, etc.
727 */
728
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200729 spin_lock(&conn->lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200730
731 if (++conn->tx_ident > 128)
732 conn->tx_ident = 1;
733
734 id = conn->tx_ident;
735
Gustavo F. Padovan333055f2011-12-22 15:14:39 -0200736 spin_unlock(&conn->lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200737
738 return id;
739}
740
Gustavo Padovan2d792812012-10-06 10:07:01 +0100741static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
742 void *data)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200743{
744 struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +0200745 u8 flags;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200746
747 BT_DBG("code 0x%2.2x", code);
748
749 if (!skb)
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -0300750 return;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200751
Andrei Emeltchenkoe7021122011-01-03 11:14:36 +0200752 if (lmp_no_flush_capable(conn->hcon->hdev))
753 flags = ACL_START_NO_FLUSH;
754 else
755 flags = ACL_START;
756
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700757 bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +0200758 skb->priority = HCI_PRIO_MAX;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700759
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200760 hci_send_acl(conn->hchan, skb, flags);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +0200761}
762
Mat Martineau02b0fbb2012-10-23 15:24:10 -0700763static bool __chan_is_moving(struct l2cap_chan *chan)
764{
765 return chan->move_state != L2CAP_MOVE_STABLE &&
766 chan->move_state != L2CAP_MOVE_WAIT_PREPARE;
767}
768
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200769static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
770{
771 struct hci_conn *hcon = chan->conn->hcon;
772 u16 flags;
773
774 BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len,
Gustavo Padovan2d792812012-10-06 10:07:01 +0100775 skb->priority);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200776
Mat Martineaud5f8a752012-10-23 15:24:18 -0700777 if (chan->hs_hcon && !__chan_is_moving(chan)) {
778 if (chan->hs_hchan)
779 hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE);
780 else
781 kfree_skb(skb);
782
783 return;
784 }
785
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200786 if (!test_bit(FLAG_FLUSHABLE, &chan->flags) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +0100787 lmp_no_flush_capable(hcon->hdev))
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200788 flags = ACL_START_NO_FLUSH;
789 else
790 flags = ACL_START;
791
792 bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
793 hci_send_acl(chan->conn->hchan, skb, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700796static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control)
797{
798 control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT;
799 control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT;
800
801 if (enh & L2CAP_CTRL_FRAME_TYPE) {
802 /* S-Frame */
803 control->sframe = 1;
804 control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT;
805 control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT;
806
807 control->sar = 0;
808 control->txseq = 0;
809 } else {
810 /* I-Frame */
811 control->sframe = 0;
812 control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT;
813 control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT;
814
815 control->poll = 0;
816 control->super = 0;
817 }
818}
819
820static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control)
821{
822 control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT;
823 control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT;
824
825 if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) {
826 /* S-Frame */
827 control->sframe = 1;
828 control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT;
829 control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT;
830
831 control->sar = 0;
832 control->txseq = 0;
833 } else {
834 /* I-Frame */
835 control->sframe = 0;
836 control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT;
837 control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT;
838
839 control->poll = 0;
840 control->super = 0;
841 }
842}
843
844static inline void __unpack_control(struct l2cap_chan *chan,
845 struct sk_buff *skb)
846{
847 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
848 __unpack_extended_control(get_unaligned_le32(skb->data),
849 &bt_cb(skb)->control);
Mat Martineaucec8ab6e2012-05-17 20:53:36 -0700850 skb_pull(skb, L2CAP_EXT_CTRL_SIZE);
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700851 } else {
852 __unpack_enhanced_control(get_unaligned_le16(skb->data),
853 &bt_cb(skb)->control);
Mat Martineaucec8ab6e2012-05-17 20:53:36 -0700854 skb_pull(skb, L2CAP_ENH_CTRL_SIZE);
Mat Martineaub5c6aae2012-04-25 16:36:15 -0700855 }
856}
857
858static u32 __pack_extended_control(struct l2cap_ctrl *control)
859{
860 u32 packed;
861
862 packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT;
863 packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT;
864
865 if (control->sframe) {
866 packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT;
867 packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT;
868 packed |= L2CAP_EXT_CTRL_FRAME_TYPE;
869 } else {
870 packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT;
871 packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT;
872 }
873
874 return packed;
875}
876
877static u16 __pack_enhanced_control(struct l2cap_ctrl *control)
878{
879 u16 packed;
880
881 packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT;
882 packed |= control->final << L2CAP_CTRL_FINAL_SHIFT;
883
884 if (control->sframe) {
885 packed |= control->poll << L2CAP_CTRL_POLL_SHIFT;
886 packed |= control->super << L2CAP_CTRL_SUPER_SHIFT;
887 packed |= L2CAP_CTRL_FRAME_TYPE;
888 } else {
889 packed |= control->sar << L2CAP_CTRL_SAR_SHIFT;
890 packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT;
891 }
892
893 return packed;
894}
895
896static inline void __pack_control(struct l2cap_chan *chan,
897 struct l2cap_ctrl *control,
898 struct sk_buff *skb)
899{
900 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
901 put_unaligned_le32(__pack_extended_control(control),
902 skb->data + L2CAP_HDR_SIZE);
903 } else {
904 put_unaligned_le16(__pack_enhanced_control(control),
905 skb->data + L2CAP_HDR_SIZE);
906 }
907}
908
Gustavo Padovanba7aa642012-05-29 13:29:16 -0300909static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan)
910{
911 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
912 return L2CAP_EXT_HDR_SIZE;
913 else
914 return L2CAP_ENH_HDR_SIZE;
915}
916
Mat Martineaua67d7f62012-05-17 20:53:35 -0700917static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan,
918 u32 control)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300919{
920 struct sk_buff *skb;
921 struct l2cap_hdr *lh;
Gustavo Padovanba7aa642012-05-29 13:29:16 -0300922 int hlen = __ertm_hdr_size(chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300923
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300924 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +0300925 hlen += L2CAP_FCS_SIZE;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300926
Mat Martineaua67d7f62012-05-17 20:53:35 -0700927 skb = bt_skb_alloc(hlen, GFP_KERNEL);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300928
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300929 if (!skb)
Mat Martineaua67d7f62012-05-17 20:53:35 -0700930 return ERR_PTR(-ENOMEM);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300931
932 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -0300933 lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -0300934 lh->cid = cpu_to_le16(chan->dcid);
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +0300935
Mat Martineaua67d7f62012-05-17 20:53:35 -0700936 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
937 put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
938 else
939 put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300940
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -0300941 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineaua67d7f62012-05-17 20:53:35 -0700942 u16 fcs = crc16(0, (u8 *)skb->data, skb->len);
Andrei Emeltchenko03a51212011-10-17 12:19:58 +0300943 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -0300944 }
945
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +0200946 skb->priority = HCI_PRIO_MAX;
Mat Martineaua67d7f62012-05-17 20:53:35 -0700947 return skb;
948}
949
950static void l2cap_send_sframe(struct l2cap_chan *chan,
951 struct l2cap_ctrl *control)
952{
953 struct sk_buff *skb;
954 u32 control_field;
955
956 BT_DBG("chan %p, control %p", chan, control);
957
958 if (!control->sframe)
959 return;
960
Mat Martineaub99e13a2012-10-23 15:24:19 -0700961 if (__chan_is_moving(chan))
962 return;
963
Mat Martineaua67d7f62012-05-17 20:53:35 -0700964 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) &&
965 !control->poll)
966 control->final = 1;
967
968 if (control->super == L2CAP_SUPER_RR)
969 clear_bit(CONN_RNR_SENT, &chan->conn_state);
970 else if (control->super == L2CAP_SUPER_RNR)
971 set_bit(CONN_RNR_SENT, &chan->conn_state);
972
973 if (control->super != L2CAP_SUPER_SREJ) {
974 chan->last_acked_seq = control->reqseq;
975 __clear_ack_timer(chan);
976 }
977
978 BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq,
979 control->final, control->poll, control->super);
980
981 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
982 control_field = __pack_extended_control(control);
983 else
984 control_field = __pack_enhanced_control(control);
985
986 skb = l2cap_create_sframe_pdu(chan, control_field);
987 if (!IS_ERR(skb))
988 l2cap_do_send(chan, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -0300989}
990
Mat Martineauc9e3d5e2012-05-17 20:53:48 -0700991static void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, bool poll)
Gustavo F. Padovan7e743092009-08-26 04:04:03 -0300992{
Mat Martineauc9e3d5e2012-05-17 20:53:48 -0700993 struct l2cap_ctrl control;
Gustavo F. Padovan7e743092009-08-26 04:04:03 -0300994
Mat Martineauc9e3d5e2012-05-17 20:53:48 -0700995 BT_DBG("chan %p, poll %d", chan, poll);
996
997 memset(&control, 0, sizeof(control));
998 control.sframe = 1;
999 control.poll = poll;
1000
1001 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
1002 control.super = L2CAP_SUPER_RNR;
1003 else
1004 control.super = L2CAP_SUPER_RR;
1005
1006 control.reqseq = chan->buffer_seq;
1007 l2cap_send_sframe(chan, &control);
Gustavo F. Padovan7e743092009-08-26 04:04:03 -03001008}
1009
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03001010static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
Andrei Emeltchenkoe501d052010-07-08 12:14:41 +03001011{
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001012 return !test_bit(CONF_CONNECT_PEND, &chan->conf_state);
Andrei Emeltchenkoe501d052010-07-08 12:14:41 +03001013}
1014
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001015static bool __amp_capable(struct l2cap_chan *chan)
1016{
1017 struct l2cap_conn *conn = chan->conn;
1018
1019 if (enable_hs &&
Andrei Emeltchenko5d054162012-11-29 17:46:05 +02001020 hci_amp_capable() &&
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001021 chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED &&
1022 conn->fixed_chan_mask & L2CAP_FC_A2MP)
1023 return true;
1024 else
1025 return false;
1026}
1027
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +02001028static bool l2cap_check_efs(struct l2cap_chan *chan)
1029{
1030 /* Check EFS parameters */
1031 return true;
1032}
1033
Andrei Emeltchenko2766be42012-09-27 17:26:21 +03001034void l2cap_send_conn_req(struct l2cap_chan *chan)
Andrei Emeltchenko9b27f352012-02-24 16:00:00 +02001035{
1036 struct l2cap_conn *conn = chan->conn;
1037 struct l2cap_conn_req req;
1038
1039 req.scid = cpu_to_le16(chan->scid);
1040 req.psm = chan->psm;
1041
1042 chan->ident = l2cap_get_ident(conn);
1043
1044 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
1045
1046 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req);
1047}
1048
Mat Martineau8eb200b2012-10-23 15:24:17 -07001049static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id)
1050{
1051 struct l2cap_create_chan_req req;
1052 req.scid = cpu_to_le16(chan->scid);
1053 req.psm = chan->psm;
1054 req.amp_id = amp_id;
1055
1056 chan->ident = l2cap_get_ident(chan->conn);
1057
1058 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ,
1059 sizeof(req), &req);
1060}
1061
Mat Martineau02b0fbb2012-10-23 15:24:10 -07001062static void l2cap_move_setup(struct l2cap_chan *chan)
1063{
1064 struct sk_buff *skb;
1065
1066 BT_DBG("chan %p", chan);
1067
1068 if (chan->mode != L2CAP_MODE_ERTM)
1069 return;
1070
1071 __clear_retrans_timer(chan);
1072 __clear_monitor_timer(chan);
1073 __clear_ack_timer(chan);
1074
1075 chan->retry_count = 0;
1076 skb_queue_walk(&chan->tx_q, skb) {
1077 if (bt_cb(skb)->control.retries)
1078 bt_cb(skb)->control.retries = 1;
1079 else
1080 break;
1081 }
1082
1083 chan->expected_tx_seq = chan->buffer_seq;
1084
1085 clear_bit(CONN_REJ_ACT, &chan->conn_state);
1086 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
1087 l2cap_seq_list_clear(&chan->retrans_list);
1088 l2cap_seq_list_clear(&chan->srej_list);
1089 skb_queue_purge(&chan->srej_q);
1090
1091 chan->tx_state = L2CAP_TX_STATE_XMIT;
1092 chan->rx_state = L2CAP_RX_STATE_MOVE;
1093
1094 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
1095}
1096
Mat Martineau5f3847a2012-10-23 15:24:12 -07001097static void l2cap_move_done(struct l2cap_chan *chan)
1098{
1099 u8 move_role = chan->move_role;
1100 BT_DBG("chan %p", chan);
1101
1102 chan->move_state = L2CAP_MOVE_STABLE;
1103 chan->move_role = L2CAP_MOVE_ROLE_NONE;
1104
1105 if (chan->mode != L2CAP_MODE_ERTM)
1106 return;
1107
1108 switch (move_role) {
1109 case L2CAP_MOVE_ROLE_INITIATOR:
1110 l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL);
1111 chan->rx_state = L2CAP_RX_STATE_WAIT_F;
1112 break;
1113 case L2CAP_MOVE_ROLE_RESPONDER:
1114 chan->rx_state = L2CAP_RX_STATE_WAIT_P;
1115 break;
1116 }
1117}
1118
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001119static void l2cap_chan_ready(struct l2cap_chan *chan)
1120{
Mat Martineau28270112012-05-17 21:14:09 -07001121 /* This clears all conf flags, including CONF_NOT_COMPLETE */
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001122 chan->conf_state = 0;
1123 __clear_chan_timer(chan);
1124
Andrei Emeltchenko54a59aa2012-05-27 22:27:53 -03001125 chan->state = BT_CONNECTED;
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001126
Andrei Emeltchenkofd83e2c2012-05-30 09:55:32 +03001127 chan->ops->ready(chan);
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001128}
1129
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001130static void l2cap_start_connection(struct l2cap_chan *chan)
1131{
1132 if (__amp_capable(chan)) {
1133 BT_DBG("chan %p AMP capable: discover AMPs", chan);
1134 a2mp_discover_amp(chan);
1135 } else {
1136 l2cap_send_conn_req(chan);
1137 }
1138}
1139
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001140static void l2cap_do_start(struct l2cap_chan *chan)
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001141{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001142 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001143
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001144 if (conn->hcon->type == LE_LINK) {
1145 l2cap_chan_ready(chan);
1146 return;
1147 }
1148
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001149 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
Marcel Holtmann984947d2009-02-06 23:35:19 +01001150 if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
1151 return;
1152
Gustavo F. Padovand45fc422011-11-05 19:54:24 -02001153 if (l2cap_chan_check_security(chan) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01001154 __l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001155 l2cap_start_connection(chan);
1156 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001157 } else {
1158 struct l2cap_info_req req;
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03001159 req.type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001160
1161 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
1162 conn->info_ident = l2cap_get_ident(conn);
1163
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08001164 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001165
Gustavo Padovan2d792812012-10-06 10:07:01 +01001166 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
1167 sizeof(req), &req);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001168 }
1169}
1170
Gustavo F. Padovancf6c2c02010-06-07 20:54:45 -03001171static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
1172{
1173 u32 local_feat_mask = l2cap_feat_mask;
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03001174 if (!disable_ertm)
Gustavo F. Padovancf6c2c02010-06-07 20:54:45 -03001175 local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
1176
1177 switch (mode) {
1178 case L2CAP_MODE_ERTM:
1179 return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
1180 case L2CAP_MODE_STREAMING:
1181 return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
1182 default:
1183 return 0x00;
1184 }
1185}
1186
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001187static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err)
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001188{
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001189 struct sock *sk = chan->sk;
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02001190 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001191 struct l2cap_disconn_req req;
1192
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001193 if (!conn)
1194 return;
1195
Andrei Emeltchenkoaad3d0e2012-09-06 15:05:42 +03001196 if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) {
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -03001197 __clear_retrans_timer(chan);
1198 __clear_monitor_timer(chan);
1199 __clear_ack_timer(chan);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001200 }
1201
Andrei Emeltchenko416fa752012-05-29 13:59:16 +03001202 if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) {
Gustavo Padovand1177732012-10-06 11:47:38 +01001203 l2cap_state_change(chan, BT_DISCONN);
Andrei Emeltchenko416fa752012-05-29 13:59:16 +03001204 return;
1205 }
1206
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001207 req.dcid = cpu_to_le16(chan->dcid);
1208 req.scid = cpu_to_le16(chan->scid);
Gustavo Padovan2d792812012-10-06 10:07:01 +01001209 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ,
1210 sizeof(req), &req);
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001211
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001212 lock_sock(sk);
Andrei Emeltchenko0e587be2012-02-21 12:54:57 +02001213 __l2cap_state_change(chan, BT_DISCONN);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +02001214 __l2cap_chan_set_err(chan, err);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001215 release_sock(sk);
Gustavo F. Padovan22121fc2009-07-23 10:27:23 -03001216}
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218/* ---- L2CAP connections ---- */
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001219static void l2cap_conn_start(struct l2cap_conn *conn)
1220{
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001221 struct l2cap_chan *chan, *tmp;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001222
1223 BT_DBG("conn %p", conn);
1224
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001225 mutex_lock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001226
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001227 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001228 struct sock *sk = chan->sk;
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03001229
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001230 l2cap_chan_lock(chan);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001231
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001232 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001233 l2cap_chan_unlock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001234 continue;
1235 }
1236
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001237 if (chan->state == BT_CONNECT) {
Gustavo F. Padovand45fc422011-11-05 19:54:24 -02001238 if (!l2cap_chan_check_security(chan) ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01001239 !__l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001240 l2cap_chan_unlock(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001241 continue;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02001242 }
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001243
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001244 if (!l2cap_mode_supported(chan->mode, conn->feat_mask)
Gustavo Padovan2d792812012-10-06 10:07:01 +01001245 && test_bit(CONF_STATE2_DEVICE,
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001246 &chan->conf_state)) {
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001247 l2cap_chan_close(chan, ECONNRESET);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001248 l2cap_chan_unlock(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001249 continue;
1250 }
1251
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03001252 l2cap_start_connection(chan);
Gustavo F. Padovan47731de2010-07-09 16:38:35 -03001253
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001254 } else if (chan->state == BT_CONNECT2) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001255 struct l2cap_conn_rsp rsp;
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001256 char buf[128];
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001257 rsp.scid = cpu_to_le16(chan->dcid);
1258 rsp.dcid = cpu_to_le16(chan->scid);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001259
Gustavo F. Padovand45fc422011-11-05 19:54:24 -02001260 if (l2cap_chan_check_security(chan)) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001261 lock_sock(sk);
Gustavo Padovanc5daa682012-05-16 12:17:10 -03001262 if (test_bit(BT_SK_DEFER_SETUP,
1263 &bt_sk(sk)->flags)) {
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03001264 rsp.result = __constant_cpu_to_le16(L2CAP_CR_PEND);
1265 rsp.status = __constant_cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08001266 chan->ops->defer(chan);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001267
1268 } else {
Andrei Emeltchenko0e587be2012-02-21 12:54:57 +02001269 __l2cap_state_change(chan, BT_CONFIG);
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03001270 rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS);
1271 rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01001272 }
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001273 release_sock(sk);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001274 } else {
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03001275 rsp.result = __constant_cpu_to_le16(L2CAP_CR_PEND);
1276 rsp.status = __constant_cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001277 }
1278
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001279 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001280 sizeof(rsp), &rsp);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001281
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001282 if (test_bit(CONF_REQ_SENT, &chan->conf_state) ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01001283 rsp.result != L2CAP_CR_SUCCESS) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001284 l2cap_chan_unlock(chan);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001285 continue;
1286 }
1287
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03001288 set_bit(CONF_REQ_SENT, &chan->conf_state);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03001289 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001290 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03001291 chan->num_conf_req++;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001292 }
1293
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001294 l2cap_chan_unlock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001295 }
1296
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001297 mutex_unlock(&conn->chan_lock);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001298}
1299
Ido Yarivc2287682012-04-20 15:46:07 -03001300/* Find socket with cid and source/destination bdaddr.
Ville Tervob62f3282011-02-10 22:38:50 -03001301 * Returns closest match, locked.
1302 */
Andrei Emeltchenkod9b88702012-03-12 12:13:08 +02001303static struct l2cap_chan *l2cap_global_chan_by_scid(int state, u16 cid,
Ido Yarivc2287682012-04-20 15:46:07 -03001304 bdaddr_t *src,
1305 bdaddr_t *dst)
Ville Tervob62f3282011-02-10 22:38:50 -03001306{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001307 struct l2cap_chan *c, *c1 = NULL;
Ville Tervob62f3282011-02-10 22:38:50 -03001308
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001309 read_lock(&chan_list_lock);
Ville Tervob62f3282011-02-10 22:38:50 -03001310
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001311 list_for_each_entry(c, &chan_list, global_l) {
1312 struct sock *sk = c->sk;
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001313
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001314 if (state && c->state != state)
Ville Tervob62f3282011-02-10 22:38:50 -03001315 continue;
1316
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001317 if (c->scid == cid) {
Ido Yarivc2287682012-04-20 15:46:07 -03001318 int src_match, dst_match;
1319 int src_any, dst_any;
1320
Ville Tervob62f3282011-02-10 22:38:50 -03001321 /* Exact match. */
Ido Yarivc2287682012-04-20 15:46:07 -03001322 src_match = !bacmp(&bt_sk(sk)->src, src);
1323 dst_match = !bacmp(&bt_sk(sk)->dst, dst);
1324 if (src_match && dst_match) {
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001325 read_unlock(&chan_list_lock);
1326 return c;
1327 }
Ville Tervob62f3282011-02-10 22:38:50 -03001328
1329 /* Closest match */
Ido Yarivc2287682012-04-20 15:46:07 -03001330 src_any = !bacmp(&bt_sk(sk)->src, BDADDR_ANY);
1331 dst_any = !bacmp(&bt_sk(sk)->dst, BDADDR_ANY);
1332 if ((src_match && dst_any) || (src_any && dst_match) ||
1333 (src_any && dst_any))
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001334 c1 = c;
Ville Tervob62f3282011-02-10 22:38:50 -03001335 }
1336 }
Gustavo F. Padovan280f2942011-04-13 19:01:22 -03001337
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001338 read_unlock(&chan_list_lock);
Ville Tervob62f3282011-02-10 22:38:50 -03001339
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001340 return c1;
Ville Tervob62f3282011-02-10 22:38:50 -03001341}
1342
1343static void l2cap_le_conn_ready(struct l2cap_conn *conn)
1344{
Johan Hedberg60bac182013-04-29 19:35:41 +03001345 struct sock *parent;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001346 struct l2cap_chan *chan, *pchan;
Ville Tervob62f3282011-02-10 22:38:50 -03001347
1348 BT_DBG("");
1349
1350 /* Check if we have socket listening on cid */
Johan Hedberg073d1cf2013-04-29 19:35:35 +03001351 pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_ATT,
Ido Yarivc2287682012-04-20 15:46:07 -03001352 conn->src, conn->dst);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001353 if (!pchan)
Ville Tervob62f3282011-02-10 22:38:50 -03001354 return;
1355
Johan Hedberg44f3b0f2013-04-29 19:35:45 +03001356 /* Client ATT sockets should override the server one */
1357 if (__l2cap_get_chan_by_dcid(conn, L2CAP_CID_ATT))
1358 return;
1359
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001360 parent = pchan->sk;
1361
Gustavo F. Padovanaa2ac882011-06-24 01:53:01 -03001362 lock_sock(parent);
Gustavo F. Padovan62f3a2c2011-04-14 18:34:34 -03001363
Gustavo Padovan80b98022012-05-27 22:27:51 -03001364 chan = pchan->ops->new_connection(pchan);
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03001365 if (!chan)
Ville Tervob62f3282011-02-10 22:38:50 -03001366 goto clean;
1367
Johan Hedberg9f22398c2013-04-29 19:35:38 +03001368 chan->dcid = L2CAP_CID_ATT;
1369
Johan Hedberg60bac182013-04-29 19:35:41 +03001370 bacpy(&bt_sk(chan->sk)->src, conn->src);
1371 bacpy(&bt_sk(chan->sk)->dst, conn->dst);
Ville Tervob62f3282011-02-10 22:38:50 -03001372
Johan Hedberg44f3b0f2013-04-29 19:35:45 +03001373 __l2cap_chan_add(conn, chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001374
Ville Tervob62f3282011-02-10 22:38:50 -03001375clean:
Gustavo F. Padovanaa2ac882011-06-24 01:53:01 -03001376 release_sock(parent);
Ville Tervob62f3282011-02-10 22:38:50 -03001377}
1378
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001379static void l2cap_conn_ready(struct l2cap_conn *conn)
1380{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001381 struct l2cap_chan *chan;
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001382 struct hci_conn *hcon = conn->hcon;
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001383
1384 BT_DBG("conn %p", conn);
1385
Johan Hedbergd8729922013-04-29 19:35:39 +03001386 /* For outgoing pairing which doesn't necessarily have an
1387 * associated socket (e.g. mgmt_pair_device).
1388 */
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001389 if (hcon->out && hcon->type == LE_LINK)
1390 smp_conn_security(hcon, hcon->pending_sec_level);
Vinicius Costa Gomes160dc6a2011-08-19 21:06:55 -03001391
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001392 mutex_lock(&conn->chan_lock);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001393
Johan Hedberg44f3b0f2013-04-29 19:35:45 +03001394 if (hcon->type == LE_LINK)
1395 l2cap_le_conn_ready(conn);
1396
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001397 list_for_each_entry(chan, &conn->chan_l, list) {
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03001398
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001399 l2cap_chan_lock(chan);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001400
Andrei Emeltchenko416fa752012-05-29 13:59:16 +03001401 if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) {
1402 l2cap_chan_unlock(chan);
1403 continue;
1404 }
1405
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03001406 if (hcon->type == LE_LINK) {
1407 if (smp_conn_security(hcon, chan->sec_level))
Andrei Emeltchenkocf4cd002012-02-06 15:03:59 +02001408 l2cap_chan_ready(chan);
Ville Tervoacd7d372011-02-10 22:38:49 -03001409
Vinicius Costa Gomes63128452011-06-17 22:46:26 -03001410 } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001411 struct sock *sk = chan->sk;
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -03001412 __clear_chan_timer(chan);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001413 lock_sock(sk);
Andrei Emeltchenko0e587be2012-02-21 12:54:57 +02001414 __l2cap_state_change(chan, BT_CONNECTED);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001415 sk->sk_state_change(sk);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001416 release_sock(sk);
Anderson Brigliab501d6a2011-06-07 18:46:31 -03001417
Gustavo Padovan1c244f72012-12-07 03:29:10 -02001418 } else if (chan->state == BT_CONNECT) {
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001419 l2cap_do_start(chan);
Gustavo Padovan1c244f72012-12-07 03:29:10 -02001420 }
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001421
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001422 l2cap_chan_unlock(chan);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001423 }
1424
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001425 mutex_unlock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001426}
1427
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001428/* Notify sockets that we cannot guaranty reliability anymore */
1429static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
1430{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001431 struct l2cap_chan *chan;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001432
1433 BT_DBG("conn %p", conn);
1434
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001435 mutex_lock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001436
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001437 list_for_each_entry(chan, &conn->chan_l, list) {
Andrei Emeltchenkoecf61bd2011-10-11 14:04:32 +03001438 if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags))
Gustavo Padovan1d8b1fd2012-10-06 11:34:52 +01001439 l2cap_chan_set_err(chan, err);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001440 }
1441
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001442 mutex_unlock(&conn->chan_lock);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001443}
1444
Gustavo F. Padovanf878fca2011-12-15 01:16:14 -02001445static void l2cap_info_timeout(struct work_struct *work)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001446{
Gustavo F. Padovanf878fca2011-12-15 01:16:14 -02001447 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001448 info_timer.work);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001449
Marcel Holtmann984947d2009-02-06 23:35:19 +01001450 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01001451 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01001452
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001453 l2cap_conn_start(conn);
1454}
1455
David Herrmann2c8e1412013-04-06 20:28:45 +02001456/*
1457 * l2cap_user
1458 * External modules can register l2cap_user objects on l2cap_conn. The ->probe
1459 * callback is called during registration. The ->remove callback is called
1460 * during unregistration.
1461 * An l2cap_user object can either be explicitly unregistered or when the
1462 * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon,
1463 * l2cap->hchan, .. are valid as long as the remove callback hasn't been called.
1464 * External modules must own a reference to the l2cap_conn object if they intend
1465 * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at
1466 * any time if they don't.
1467 */
1468
1469int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user)
1470{
1471 struct hci_dev *hdev = conn->hcon->hdev;
1472 int ret;
1473
1474 /* We need to check whether l2cap_conn is registered. If it is not, we
1475 * must not register the l2cap_user. l2cap_conn_del() is unregisters
1476 * l2cap_conn objects, but doesn't provide its own locking. Instead, it
1477 * relies on the parent hci_conn object to be locked. This itself relies
1478 * on the hci_dev object to be locked. So we must lock the hci device
1479 * here, too. */
1480
1481 hci_dev_lock(hdev);
1482
1483 if (user->list.next || user->list.prev) {
1484 ret = -EINVAL;
1485 goto out_unlock;
1486 }
1487
1488 /* conn->hchan is NULL after l2cap_conn_del() was called */
1489 if (!conn->hchan) {
1490 ret = -ENODEV;
1491 goto out_unlock;
1492 }
1493
1494 ret = user->probe(conn, user);
1495 if (ret)
1496 goto out_unlock;
1497
1498 list_add(&user->list, &conn->users);
1499 ret = 0;
1500
1501out_unlock:
1502 hci_dev_unlock(hdev);
1503 return ret;
1504}
1505EXPORT_SYMBOL(l2cap_register_user);
1506
1507void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user)
1508{
1509 struct hci_dev *hdev = conn->hcon->hdev;
1510
1511 hci_dev_lock(hdev);
1512
1513 if (!user->list.next || !user->list.prev)
1514 goto out_unlock;
1515
1516 list_del(&user->list);
1517 user->list.next = NULL;
1518 user->list.prev = NULL;
1519 user->remove(conn, user);
1520
1521out_unlock:
1522 hci_dev_unlock(hdev);
1523}
1524EXPORT_SYMBOL(l2cap_unregister_user);
1525
1526static void l2cap_unregister_all_users(struct l2cap_conn *conn)
1527{
1528 struct l2cap_user *user;
1529
1530 while (!list_empty(&conn->users)) {
1531 user = list_first_entry(&conn->users, struct l2cap_user, list);
1532 list_del(&user->list);
1533 user->list.next = NULL;
1534 user->list.prev = NULL;
1535 user->remove(conn, user);
1536 }
1537}
1538
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001539static void l2cap_conn_del(struct hci_conn *hcon, int err)
1540{
1541 struct l2cap_conn *conn = hcon->l2cap_data;
1542 struct l2cap_chan *chan, *l;
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001543
1544 if (!conn)
1545 return;
1546
1547 BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
1548
1549 kfree_skb(conn->rx_skb);
1550
David Herrmann2c8e1412013-04-06 20:28:45 +02001551 l2cap_unregister_all_users(conn);
1552
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001553 mutex_lock(&conn->chan_lock);
1554
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001555 /* Kill channels */
1556 list_for_each_entry_safe(chan, l, &conn->chan_l, list) {
Mat Martineau61d6ef32012-04-27 16:50:50 -07001557 l2cap_chan_hold(chan);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001558 l2cap_chan_lock(chan);
1559
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001560 l2cap_chan_del(chan, err);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001561
1562 l2cap_chan_unlock(chan);
1563
Gustavo Padovan80b98022012-05-27 22:27:51 -03001564 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07001565 l2cap_chan_put(chan);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001566 }
1567
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001568 mutex_unlock(&conn->chan_lock);
1569
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001570 hci_chan_del(conn->hchan);
1571
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001572 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
Ulisses Furquim127074b2012-01-30 18:26:29 -02001573 cancel_delayed_work_sync(&conn->info_timer);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001574
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001575 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) {
Ulisses Furquim127074b2012-01-30 18:26:29 -02001576 cancel_delayed_work_sync(&conn->security_timer);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001577 smp_chan_destroy(conn);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001578 }
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001579
1580 hcon->l2cap_data = NULL;
David Herrmann9c903e32013-04-06 20:28:44 +02001581 conn->hchan = NULL;
1582 l2cap_conn_put(conn);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001583}
1584
Gustavo F. Padovan6c9d42a2011-12-20 10:57:27 -02001585static void security_timeout(struct work_struct *work)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001586{
Gustavo F. Padovan6c9d42a2011-12-20 10:57:27 -02001587 struct l2cap_conn *conn = container_of(work, struct l2cap_conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01001588 security_timer.work);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001589
Johan Hedbergd06cc412012-06-06 18:44:11 +08001590 BT_DBG("conn %p", conn);
1591
1592 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) {
1593 smp_chan_destroy(conn);
1594 l2cap_conn_del(conn->hcon, ETIMEDOUT);
1595 }
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001596}
1597
Claudio Takahasibaf43252013-04-11 13:55:50 -03001598static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Marcel Holtmann01394182006-07-03 10:02:46 +02001600 struct l2cap_conn *conn = hcon->l2cap_data;
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001601 struct hci_chan *hchan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Claudio Takahasibaf43252013-04-11 13:55:50 -03001603 if (conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return conn;
1605
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001606 hchan = hci_chan_create(hcon);
1607 if (!hchan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03001610 conn = kzalloc(sizeof(struct l2cap_conn), GFP_KERNEL);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001611 if (!conn) {
1612 hci_chan_del(hchan);
1613 return NULL;
1614 }
1615
David Herrmann9c903e32013-04-06 20:28:44 +02001616 kref_init(&conn->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 hcon->l2cap_data = conn;
1618 conn->hcon = hcon;
David Herrmann9c903e32013-04-06 20:28:44 +02001619 hci_conn_get(conn->hcon);
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001620 conn->hchan = hchan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Luiz Augusto von Dentz73d80de2011-11-02 15:52:01 +02001622 BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan);
Marcel Holtmann01394182006-07-03 10:02:46 +02001623
Andrei Emeltchenkodcc042d2012-10-05 16:56:57 +03001624 switch (hcon->type) {
Andrei Emeltchenkodcc042d2012-10-05 16:56:57 +03001625 case LE_LINK:
1626 if (hcon->hdev->le_mtu) {
1627 conn->mtu = hcon->hdev->le_mtu;
1628 break;
1629 }
1630 /* fall through */
Andrei Emeltchenkodcc042d2012-10-05 16:56:57 +03001631 default:
Ville Tervoacd7d372011-02-10 22:38:49 -03001632 conn->mtu = hcon->hdev->acl_mtu;
Andrei Emeltchenkodcc042d2012-10-05 16:56:57 +03001633 break;
1634 }
Ville Tervoacd7d372011-02-10 22:38:49 -03001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 conn->src = &hcon->hdev->bdaddr;
1637 conn->dst = &hcon->dst;
1638
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02001639 conn->feat_mask = 0;
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 spin_lock_init(&conn->lock);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02001642 mutex_init(&conn->chan_lock);
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03001643
1644 INIT_LIST_HEAD(&conn->chan_l);
David Herrmann2c8e1412013-04-06 20:28:45 +02001645 INIT_LIST_HEAD(&conn->users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001647 if (hcon->type == LE_LINK)
Gustavo F. Padovan6c9d42a2011-12-20 10:57:27 -02001648 INIT_DELAYED_WORK(&conn->security_timer, security_timeout);
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -03001649 else
Gustavo F. Padovan030013d2011-12-20 10:57:28 -02001650 INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout);
Dave Young45054dc2009-10-18 20:28:30 +00001651
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02001652 conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +01001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return conn;
1655}
1656
David Herrmann9c903e32013-04-06 20:28:44 +02001657static void l2cap_conn_free(struct kref *ref)
1658{
1659 struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref);
1660
1661 hci_conn_put(conn->hcon);
1662 kfree(conn);
1663}
1664
1665void l2cap_conn_get(struct l2cap_conn *conn)
1666{
1667 kref_get(&conn->ref);
1668}
1669EXPORT_SYMBOL(l2cap_conn_get);
1670
1671void l2cap_conn_put(struct l2cap_conn *conn)
1672{
1673 kref_put(&conn->ref, l2cap_conn_free);
1674}
1675EXPORT_SYMBOL(l2cap_conn_put);
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677/* ---- Socket interface ---- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Ido Yarivc2287682012-04-20 15:46:07 -03001679/* Find socket with psm and source / destination bdaddr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 * Returns closest match.
1681 */
Ido Yarivc2287682012-04-20 15:46:07 -03001682static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm,
1683 bdaddr_t *src,
1684 bdaddr_t *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001686 struct l2cap_chan *c, *c1 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001688 read_lock(&chan_list_lock);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00001689
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001690 list_for_each_entry(c, &chan_list, global_l) {
1691 struct sock *sk = c->sk;
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03001692
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001693 if (state && c->state != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 continue;
1695
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001696 if (c->psm == psm) {
Ido Yarivc2287682012-04-20 15:46:07 -03001697 int src_match, dst_match;
1698 int src_any, dst_any;
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /* Exact match. */
Ido Yarivc2287682012-04-20 15:46:07 -03001701 src_match = !bacmp(&bt_sk(sk)->src, src);
1702 dst_match = !bacmp(&bt_sk(sk)->dst, dst);
1703 if (src_match && dst_match) {
Johannes Berga7567b22011-06-01 08:29:54 +02001704 read_unlock(&chan_list_lock);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001705 return c;
1706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 /* Closest match */
Ido Yarivc2287682012-04-20 15:46:07 -03001709 src_any = !bacmp(&bt_sk(sk)->src, BDADDR_ANY);
1710 dst_any = !bacmp(&bt_sk(sk)->dst, BDADDR_ANY);
1711 if ((src_match && dst_any) || (src_any && dst_match) ||
1712 (src_any && dst_any))
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001713 c1 = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001717 read_unlock(&chan_list_lock);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00001718
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03001719 return c1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
Andre Guedes8e9f9892012-04-24 21:02:55 -03001722int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
1723 bdaddr_t *dst, u8 dst_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Gustavo F. Padovan5d41ce12011-04-08 15:40:02 -03001725 struct sock *sk = chan->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 bdaddr_t *src = &bt_sk(sk)->src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 struct l2cap_conn *conn;
1728 struct hci_conn *hcon;
1729 struct hci_dev *hdev;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +02001730 __u8 auth_type;
Marcel Holtmann44d0e482009-04-20 07:09:16 +02001731 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001733 BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", src, dst,
Syam Sidhardhanab195162012-07-27 23:51:22 +05301734 dst_type, __le16_to_cpu(psm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03001736 hdev = hci_get_route(dst, src);
1737 if (!hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return -EHOSTUNREACH;
1739
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001740 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001742 l2cap_chan_lock(chan);
Gustavo F. Padovan03a00192011-12-09 04:48:17 -02001743
1744 /* PSM must be odd and lsb of upper byte must be 0 */
1745 if ((__le16_to_cpu(psm) & 0x0101) != 0x0001 && !cid &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01001746 chan->chan_type != L2CAP_CHAN_RAW) {
Gustavo F. Padovan03a00192011-12-09 04:48:17 -02001747 err = -EINVAL;
1748 goto done;
1749 }
1750
1751 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !(psm || cid)) {
1752 err = -EINVAL;
1753 goto done;
1754 }
1755
1756 switch (chan->mode) {
1757 case L2CAP_MODE_BASIC:
1758 break;
1759 case L2CAP_MODE_ERTM:
1760 case L2CAP_MODE_STREAMING:
1761 if (!disable_ertm)
1762 break;
1763 /* fall through */
1764 default:
1765 err = -ENOTSUPP;
1766 goto done;
1767 }
1768
Gustavo Padovan0797e012012-05-27 22:27:54 -03001769 switch (chan->state) {
Gustavo F. Padovan03a00192011-12-09 04:48:17 -02001770 case BT_CONNECT:
1771 case BT_CONNECT2:
1772 case BT_CONFIG:
1773 /* Already connecting */
1774 err = 0;
1775 goto done;
1776
1777 case BT_CONNECTED:
1778 /* Already connected */
1779 err = -EISCONN;
1780 goto done;
1781
1782 case BT_OPEN:
1783 case BT_BOUND:
1784 /* Can connect */
1785 break;
1786
1787 default:
1788 err = -EBADFD;
1789 goto done;
1790 }
1791
1792 /* Set destination address and psm */
Gustavo Padovan0797e012012-05-27 22:27:54 -03001793 lock_sock(sk);
Gustavo F. Padovan9219b2a2012-01-02 20:08:04 -02001794 bacpy(&bt_sk(sk)->dst, dst);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001795 release_sock(sk);
1796
Gustavo F. Padovan03a00192011-12-09 04:48:17 -02001797 chan->psm = psm;
1798 chan->dcid = cid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001800 auth_type = l2cap_get_auth_type(chan);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +02001801
Johan Hedbergf224ca52013-04-29 19:35:36 +03001802 if (bdaddr_type_is_le(dst_type))
Andre Guedes8e9f9892012-04-24 21:02:55 -03001803 hcon = hci_connect(hdev, LE_LINK, dst, dst_type,
Andre Guedesb12f62c2012-04-24 21:02:54 -03001804 chan->sec_level, auth_type);
Ville Tervoacd7d372011-02-10 22:38:49 -03001805 else
Andre Guedes8e9f9892012-04-24 21:02:55 -03001806 hcon = hci_connect(hdev, ACL_LINK, dst, dst_type,
Andre Guedesb12f62c2012-04-24 21:02:54 -03001807 chan->sec_level, auth_type);
Ville Tervoacd7d372011-02-10 22:38:49 -03001808
Ville Tervo30e76272011-02-22 16:10:53 -03001809 if (IS_ERR(hcon)) {
1810 err = PTR_ERR(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 goto done;
Ville Tervo30e76272011-02-22 16:10:53 -03001812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Claudio Takahasibaf43252013-04-11 13:55:50 -03001814 conn = l2cap_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (!conn) {
David Herrmann76a68ba2013-04-06 20:28:37 +02001816 hci_conn_drop(hcon);
Ville Tervo30e76272011-02-22 16:10:53 -03001817 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 goto done;
1819 }
1820
Johan Hedberg141d5702013-04-29 19:35:37 +03001821 if (cid && __l2cap_get_chan_by_dcid(conn, cid)) {
1822 hci_conn_drop(hcon);
1823 err = -EBUSY;
1824 goto done;
Vinicius Costa Gomes9f0caeb2012-04-20 15:46:08 -03001825 }
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 /* Update source addr of the socket */
1828 bacpy(src, conn->src);
1829
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001830 l2cap_chan_unlock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001831 l2cap_chan_add(conn, chan);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001832 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03001833
Johan Hedberg5ee98912013-04-29 19:35:43 +03001834 /* l2cap_chan_add takes its own ref so we can drop this one */
1835 hci_conn_drop(hcon);
1836
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001837 l2cap_state_change(chan, BT_CONNECT);
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -03001838 __set_chan_timer(chan, sk->sk_sndtimeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 if (hcon->state == BT_CONNECTED) {
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03001841 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -03001842 __clear_chan_timer(chan);
Gustavo F. Padovand45fc422011-11-05 19:54:24 -02001843 if (l2cap_chan_check_security(chan))
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001844 l2cap_state_change(chan, BT_CONNECTED);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02001845 } else
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03001846 l2cap_do_start(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
1848
Ville Tervo30e76272011-02-22 16:10:53 -03001849 err = 0;
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001852 l2cap_chan_unlock(chan);
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001853 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 hci_dev_put(hdev);
1855 return err;
1856}
1857
Gustavo F. Padovandcba0db2011-02-04 03:08:36 -02001858int __l2cap_wait_ack(struct sock *sk)
Gustavo F. Padovan6161c032010-05-01 16:15:44 -03001859{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03001860 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
Gustavo F. Padovan6161c032010-05-01 16:15:44 -03001861 DECLARE_WAITQUEUE(wait, current);
1862 int err = 0;
1863 int timeo = HZ/5;
1864
Marcel Holtmann2b0b05d2010-05-10 11:33:10 +02001865 add_wait_queue(sk_sleep(sk), &wait);
Peter Hurleya71a0cf2011-07-25 18:36:26 -04001866 set_current_state(TASK_INTERRUPTIBLE);
1867 while (chan->unacked_frames > 0 && chan->conn) {
Gustavo F. Padovan6161c032010-05-01 16:15:44 -03001868 if (!timeo)
1869 timeo = HZ/5;
1870
1871 if (signal_pending(current)) {
1872 err = sock_intr_errno(timeo);
1873 break;
1874 }
1875
1876 release_sock(sk);
1877 timeo = schedule_timeout(timeo);
1878 lock_sock(sk);
Peter Hurleya71a0cf2011-07-25 18:36:26 -04001879 set_current_state(TASK_INTERRUPTIBLE);
Gustavo F. Padovan6161c032010-05-01 16:15:44 -03001880
1881 err = sock_error(sk);
1882 if (err)
1883 break;
1884 }
1885 set_current_state(TASK_RUNNING);
Marcel Holtmann2b0b05d2010-05-10 11:33:10 +02001886 remove_wait_queue(sk_sleep(sk), &wait);
Gustavo F. Padovan6161c032010-05-01 16:15:44 -03001887 return err;
1888}
1889
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001890static void l2cap_monitor_timeout(struct work_struct *work)
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001891{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001892 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau4239d162012-05-17 20:53:49 -07001893 monitor_timer.work);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001894
Gustavo F. Padovan525cd182011-03-25 19:43:39 -03001895 BT_DBG("chan %p", chan);
Gustavo F. Padovan0e989582010-04-19 14:45:38 -03001896
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001897 l2cap_chan_lock(chan);
1898
Mat Martineau80909e02012-05-17 20:53:50 -07001899 if (!chan->conn) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001900 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001901 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001902 return;
1903 }
1904
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03001905 l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001906
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001907 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001908 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001909}
1910
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001911static void l2cap_retrans_timeout(struct work_struct *work)
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001912{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03001913 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau4239d162012-05-17 20:53:49 -07001914 retrans_timer.work);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001915
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03001916 BT_DBG("chan %p", chan);
Gustavo F. Padovan0e989582010-04-19 14:45:38 -03001917
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001918 l2cap_chan_lock(chan);
1919
Mat Martineau80909e02012-05-17 20:53:50 -07001920 if (!chan->conn) {
1921 l2cap_chan_unlock(chan);
1922 l2cap_chan_put(chan);
1923 return;
1924 }
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001925
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03001926 l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02001927 l2cap_chan_unlock(chan);
Andrei Emeltchenko8d7e1c72012-03-23 09:42:15 +02001928 l2cap_chan_put(chan);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001929}
1930
Gustavo Padovand6603662012-05-21 13:58:22 -03001931static void l2cap_streaming_send(struct l2cap_chan *chan,
1932 struct sk_buff_head *skbs)
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001933{
Gustavo F. Padovanccbb84a2010-08-30 18:44:44 -03001934 struct sk_buff *skb;
Mat Martineau37339372012-05-17 20:53:33 -07001935 struct l2cap_ctrl *control;
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001936
Mat Martineau37339372012-05-17 20:53:33 -07001937 BT_DBG("chan %p, skbs %p", chan, skbs);
1938
Mat Martineaub99e13a2012-10-23 15:24:19 -07001939 if (__chan_is_moving(chan))
1940 return;
1941
Mat Martineau37339372012-05-17 20:53:33 -07001942 skb_queue_splice_tail_init(skbs, &chan->tx_q);
1943
1944 while (!skb_queue_empty(&chan->tx_q)) {
1945
1946 skb = skb_dequeue(&chan->tx_q);
1947
1948 bt_cb(skb)->control.retries = 1;
1949 control = &bt_cb(skb)->control;
1950
1951 control->reqseq = 0;
1952 control->txseq = chan->next_tx_seq;
1953
1954 __pack_control(chan, control, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001955
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03001956 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineau37339372012-05-17 20:53:33 -07001957 u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
1958 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03001959 }
1960
Gustavo F. Padovan43434782011-04-12 18:31:57 -03001961 l2cap_do_send(chan, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001962
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03001963 BT_DBG("Sent txseq %u", control->txseq);
Mat Martineau37339372012-05-17 20:53:33 -07001964
Andrei Emeltchenko836be932011-10-17 12:19:57 +03001965 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
Mat Martineau37339372012-05-17 20:53:33 -07001966 chan->frames_sent++;
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001967 }
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03001968}
1969
Szymon Janc67c9e842011-07-28 16:24:33 +02001970static int l2cap_ertm_send(struct l2cap_chan *chan)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001971{
1972 struct sk_buff *skb, *tx_skb;
Mat Martineau18a48e72012-05-17 20:53:34 -07001973 struct l2cap_ctrl *control;
1974 int sent = 0;
1975
1976 BT_DBG("chan %p", chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001977
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03001978 if (chan->state != BT_CONNECTED)
Gustavo F. Padovanc13ffa62010-05-13 20:50:12 -03001979 return -ENOTCONN;
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001980
Mat Martineau94122bb2012-05-02 09:42:02 -07001981 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
1982 return 0;
1983
Mat Martineaub99e13a2012-10-23 15:24:19 -07001984 if (__chan_is_moving(chan))
1985 return 0;
1986
Mat Martineau18a48e72012-05-17 20:53:34 -07001987 while (chan->tx_send_head &&
1988 chan->unacked_frames < chan->remote_tx_win &&
1989 chan->tx_state == L2CAP_TX_STATE_XMIT) {
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03001990
Mat Martineau18a48e72012-05-17 20:53:34 -07001991 skb = chan->tx_send_head;
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03001992
Mat Martineau18a48e72012-05-17 20:53:34 -07001993 bt_cb(skb)->control.retries = 1;
1994 control = &bt_cb(skb)->control;
Gustavo F. Padovan95ffa972010-06-18 20:37:33 -03001995
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03001996 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
Mat Martineau18a48e72012-05-17 20:53:34 -07001997 control->final = 1;
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03001998
Mat Martineau18a48e72012-05-17 20:53:34 -07001999 control->reqseq = chan->buffer_seq;
2000 chan->last_acked_seq = chan->buffer_seq;
2001 control->txseq = chan->next_tx_seq;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002002
Mat Martineau18a48e72012-05-17 20:53:34 -07002003 __pack_control(chan, control, skb);
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03002004
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03002005 if (chan->fcs == L2CAP_FCS_CRC16) {
Mat Martineau18a48e72012-05-17 20:53:34 -07002006 u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
2007 put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03002008 }
2009
Mat Martineau18a48e72012-05-17 20:53:34 -07002010 /* Clone after data has been modified. Data is assumed to be
2011 read-only (for locking purposes) on cloned sk_buffs.
2012 */
2013 tx_skb = skb_clone(skb, GFP_KERNEL);
2014
2015 if (!tx_skb)
2016 break;
Gustavo F. Padovan9a9c6a32010-05-01 16:15:43 -03002017
Gustavo F. Padovan1a09bcb2011-05-17 15:13:19 -03002018 __set_retrans_timer(chan);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002019
Andrei Emeltchenko836be932011-10-17 12:19:57 +03002020 chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq);
Mat Martineau18a48e72012-05-17 20:53:34 -07002021 chan->unacked_frames++;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03002022 chan->frames_sent++;
Mat Martineau18a48e72012-05-17 20:53:34 -07002023 sent++;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002024
Gustavo F. Padovan58d35f82011-04-04 16:16:44 -03002025 if (skb_queue_is_last(&chan->tx_q, skb))
2026 chan->tx_send_head = NULL;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002027 else
Gustavo F. Padovan58d35f82011-04-04 16:16:44 -03002028 chan->tx_send_head = skb_queue_next(&chan->tx_q, skb);
Mat Martineau18a48e72012-05-17 20:53:34 -07002029
2030 l2cap_do_send(chan, tx_skb);
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002031 BT_DBG("Sent txseq %u", control->txseq);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002032 }
2033
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002034 BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent,
2035 chan->unacked_frames, skb_queue_len(&chan->tx_q));
Mat Martineau18a48e72012-05-17 20:53:34 -07002036
2037 return sent;
Gustavo F. Padovan9e917af2010-05-01 16:15:37 -03002038}
2039
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002040static void l2cap_ertm_resend(struct l2cap_chan *chan)
2041{
2042 struct l2cap_ctrl control;
2043 struct sk_buff *skb;
2044 struct sk_buff *tx_skb;
2045 u16 seq;
2046
2047 BT_DBG("chan %p", chan);
2048
2049 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
2050 return;
2051
Mat Martineaub99e13a2012-10-23 15:24:19 -07002052 if (__chan_is_moving(chan))
2053 return;
2054
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002055 while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) {
2056 seq = l2cap_seq_list_pop(&chan->retrans_list);
2057
2058 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq);
2059 if (!skb) {
2060 BT_DBG("Error: Can't retransmit seq %d, frame missing",
Gustavo Padovan2d792812012-10-06 10:07:01 +01002061 seq);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002062 continue;
2063 }
2064
2065 bt_cb(skb)->control.retries++;
2066 control = bt_cb(skb)->control;
2067
2068 if (chan->max_tx != 0 &&
2069 bt_cb(skb)->control.retries > chan->max_tx) {
2070 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02002071 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002072 l2cap_seq_list_clear(&chan->retrans_list);
2073 break;
2074 }
2075
2076 control.reqseq = chan->buffer_seq;
2077 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
2078 control.final = 1;
2079 else
2080 control.final = 0;
2081
2082 if (skb_cloned(skb)) {
2083 /* Cloned sk_buffs are read-only, so we need a
2084 * writeable copy
2085 */
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002086 tx_skb = skb_copy(skb, GFP_KERNEL);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002087 } else {
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002088 tx_skb = skb_clone(skb, GFP_KERNEL);
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002089 }
2090
2091 if (!tx_skb) {
2092 l2cap_seq_list_clear(&chan->retrans_list);
2093 break;
2094 }
2095
2096 /* Update skb contents */
2097 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
2098 put_unaligned_le32(__pack_extended_control(&control),
2099 tx_skb->data + L2CAP_HDR_SIZE);
2100 } else {
2101 put_unaligned_le16(__pack_enhanced_control(&control),
2102 tx_skb->data + L2CAP_HDR_SIZE);
2103 }
2104
2105 if (chan->fcs == L2CAP_FCS_CRC16) {
2106 u16 fcs = crc16(0, (u8 *) tx_skb->data, tx_skb->len);
2107 put_unaligned_le16(fcs, skb_put(tx_skb,
2108 L2CAP_FCS_SIZE));
2109 }
2110
2111 l2cap_do_send(chan, tx_skb);
2112
2113 BT_DBG("Resent txseq %d", control.txseq);
2114
2115 chan->last_acked_seq = chan->buffer_seq;
2116 }
2117}
2118
Mat Martineauf80842a2012-05-17 20:53:46 -07002119static void l2cap_retransmit(struct l2cap_chan *chan,
2120 struct l2cap_ctrl *control)
2121{
2122 BT_DBG("chan %p, control %p", chan, control);
2123
2124 l2cap_seq_list_append(&chan->retrans_list, control->reqseq);
2125 l2cap_ertm_resend(chan);
2126}
2127
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002128static void l2cap_retransmit_all(struct l2cap_chan *chan,
2129 struct l2cap_ctrl *control)
2130{
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002131 struct sk_buff *skb;
2132
2133 BT_DBG("chan %p, control %p", chan, control);
2134
2135 if (control->poll)
2136 set_bit(CONN_SEND_FBIT, &chan->conn_state);
2137
2138 l2cap_seq_list_clear(&chan->retrans_list);
2139
2140 if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state))
2141 return;
2142
2143 if (chan->unacked_frames) {
2144 skb_queue_walk(&chan->tx_q, skb) {
2145 if (bt_cb(skb)->control.txseq == control->reqseq ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01002146 skb == chan->tx_send_head)
Mat Martineaue1fbd4c2012-05-17 20:53:43 -07002147 break;
2148 }
2149
2150 skb_queue_walk_from(&chan->tx_q, skb) {
2151 if (skb == chan->tx_send_head)
2152 break;
2153
2154 l2cap_seq_list_append(&chan->retrans_list,
2155 bt_cb(skb)->control.txseq);
2156 }
2157
2158 l2cap_ertm_resend(chan);
2159 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002160}
2161
Szymon Jancb17e73b2012-01-11 10:59:47 +01002162static void l2cap_send_ack(struct l2cap_chan *chan)
2163{
Mat Martineau0a0aba42012-05-17 20:53:39 -07002164 struct l2cap_ctrl control;
2165 u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
2166 chan->last_acked_seq);
2167 int threshold;
2168
2169 BT_DBG("chan %p last_acked_seq %d buffer_seq %d",
2170 chan, chan->last_acked_seq, chan->buffer_seq);
2171
2172 memset(&control, 0, sizeof(control));
2173 control.sframe = 1;
2174
2175 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
2176 chan->rx_state == L2CAP_RX_STATE_RECV) {
2177 __clear_ack_timer(chan);
2178 control.super = L2CAP_SUPER_RNR;
2179 control.reqseq = chan->buffer_seq;
2180 l2cap_send_sframe(chan, &control);
2181 } else {
2182 if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) {
2183 l2cap_ertm_send(chan);
2184 /* If any i-frames were sent, they included an ack */
2185 if (chan->buffer_seq == chan->last_acked_seq)
2186 frames_to_ack = 0;
2187 }
2188
Mat Martineauc20f8e32012-07-10 05:47:07 -07002189 /* Ack now if the window is 3/4ths full.
Mat Martineau0a0aba42012-05-17 20:53:39 -07002190 * Calculate without mul or div
2191 */
Mat Martineauc20f8e32012-07-10 05:47:07 -07002192 threshold = chan->ack_win;
Mat Martineau0a0aba42012-05-17 20:53:39 -07002193 threshold += threshold << 1;
2194 threshold >>= 2;
2195
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002196 BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack,
Mat Martineau0a0aba42012-05-17 20:53:39 -07002197 threshold);
2198
2199 if (frames_to_ack >= threshold) {
2200 __clear_ack_timer(chan);
2201 control.super = L2CAP_SUPER_RR;
2202 control.reqseq = chan->buffer_seq;
2203 l2cap_send_sframe(chan, &control);
2204 frames_to_ack = 0;
2205 }
2206
2207 if (frames_to_ack)
2208 __set_ack_timer(chan);
2209 }
Szymon Jancb17e73b2012-01-11 10:59:47 +01002210}
2211
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002212static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan,
2213 struct msghdr *msg, int len,
2214 int count, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002216 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002217 struct sk_buff **frag;
Gustavo Padovan90338942012-04-06 20:15:47 -03002218 int sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Gustavo F. Padovan59203a22010-05-01 16:15:43 -03002220 if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002221 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223 sent += count;
2224 len -= count;
2225
2226 /* Continuation fragments (no L2CAP header) */
2227 frag = &skb_shinfo(skb)->frag_list;
2228 while (len) {
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002229 struct sk_buff *tmp;
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 count = min_t(unsigned int, conn->mtu, len);
2232
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002233 tmp = chan->ops->alloc_skb(chan, count,
2234 msg->msg_flags & MSG_DONTWAIT);
2235 if (IS_ERR(tmp))
2236 return PTR_ERR(tmp);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002237
Gustavo Padovanfbe00702012-05-15 13:22:55 -03002238 *frag = tmp;
2239
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002240 if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count))
2241 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002243 (*frag)->priority = skb->priority;
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 sent += count;
2246 len -= count;
2247
Gustavo Padovan2d0ed3d2012-05-11 13:16:12 -03002248 skb->len += (*frag)->len;
2249 skb->data_len += (*frag)->len;
2250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 frag = &(*frag)->next;
2252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 return sent;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002257static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan,
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002258 struct msghdr *msg, size_t len,
2259 u32 priority)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002260{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002261 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002262 struct sk_buff *skb;
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002263 int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002264 struct l2cap_hdr *lh;
2265
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002266 BT_DBG("chan %p len %zu priority %u", chan, len, priority);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002267
2268 count = min_t(unsigned int, (conn->mtu - hlen), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002269
2270 skb = chan->ops->alloc_skb(chan, count + hlen,
Gustavo Padovan90338942012-04-06 20:15:47 -03002271 msg->msg_flags & MSG_DONTWAIT);
2272 if (IS_ERR(skb))
2273 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002274
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002275 skb->priority = priority;
2276
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002277 /* Create L2CAP header */
2278 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002279 lh->cid = cpu_to_le16(chan->dcid);
Andrei Emeltchenkodaf6a78c2012-05-03 10:55:52 +03002280 lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE);
2281 put_unaligned(chan->psm, skb_put(skb, L2CAP_PSMLEN_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002282
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002283 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002284 if (unlikely(err < 0)) {
2285 kfree_skb(skb);
2286 return ERR_PTR(err);
2287 }
2288 return skb;
2289}
2290
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002291static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan,
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002292 struct msghdr *msg, size_t len,
2293 u32 priority)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002294{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002295 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002296 struct sk_buff *skb;
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002297 int err, count;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002298 struct l2cap_hdr *lh;
2299
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002300 BT_DBG("chan %p len %zu", chan, len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002301
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002302 count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002303
Gustavo Padovanf2ba7fa2012-05-03 04:54:21 -03002304 skb = chan->ops->alloc_skb(chan, count + L2CAP_HDR_SIZE,
Gustavo Padovan90338942012-04-06 20:15:47 -03002305 msg->msg_flags & MSG_DONTWAIT);
2306 if (IS_ERR(skb))
2307 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002308
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002309 skb->priority = priority;
2310
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002311 /* Create L2CAP header */
2312 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002313 lh->cid = cpu_to_le16(chan->dcid);
Gustavo Padovan6ff9b5e2012-05-02 11:56:17 -03002314 lh->len = cpu_to_le16(len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002315
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002316 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002317 if (unlikely(err < 0)) {
2318 kfree_skb(skb);
2319 return ERR_PTR(err);
2320 }
2321 return skb;
2322}
2323
Luiz Augusto von Dentzab0ff762011-09-12 20:00:50 +03002324static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan,
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002325 struct msghdr *msg, size_t len,
2326 u16 sdulen)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002327{
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03002328 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002329 struct sk_buff *skb;
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03002330 int err, count, hlen;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002331 struct l2cap_hdr *lh;
2332
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002333 BT_DBG("chan %p len %zu", chan, len);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002334
Gustavo F. Padovan0ee0d202010-05-01 16:15:41 -03002335 if (!conn)
2336 return ERR_PTR(-ENOTCONN);
2337
Gustavo Padovanba7aa642012-05-29 13:29:16 -03002338 hlen = __ertm_hdr_size(chan);
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03002339
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002340 if (sdulen)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002341 hlen += L2CAP_SDULEN_SIZE;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002342
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03002343 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002344 hlen += L2CAP_FCS_SIZE;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03002345
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002346 count = min_t(unsigned int, (conn->mtu - hlen), len);
Andrei Emeltchenko2f7719c2012-01-20 14:08:03 +02002347
2348 skb = chan->ops->alloc_skb(chan, count + hlen,
Gustavo Padovan90338942012-04-06 20:15:47 -03002349 msg->msg_flags & MSG_DONTWAIT);
2350 if (IS_ERR(skb))
2351 return skb;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002352
2353 /* Create L2CAP header */
2354 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03002355 lh->cid = cpu_to_le16(chan->dcid);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002356 lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03002357
Mat Martineau18a48e72012-05-17 20:53:34 -07002358 /* Control header is populated later */
2359 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
2360 put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE));
2361 else
2362 put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE));
Andrei Emeltchenko88843ab2011-10-17 12:19:56 +03002363
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002364 if (sdulen)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03002365 put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002366
Andrei Emeltchenko0952a572012-01-13 17:21:43 +02002367 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002368 if (unlikely(err < 0)) {
2369 kfree_skb(skb);
2370 return ERR_PTR(err);
2371 }
Gustavo F. Padovane90bac02009-08-20 22:26:00 -03002372
Mat Martineau18a48e72012-05-17 20:53:34 -07002373 bt_cb(skb)->control.fcs = chan->fcs;
Mat Martineau3ce35142012-04-25 16:36:14 -07002374 bt_cb(skb)->control.retries = 0;
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03002375 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
2377
Mat Martineau94122bb2012-05-02 09:42:02 -07002378static int l2cap_segment_sdu(struct l2cap_chan *chan,
2379 struct sk_buff_head *seg_queue,
2380 struct msghdr *msg, size_t len)
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002381{
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002382 struct sk_buff *skb;
Mat Martineau94122bb2012-05-02 09:42:02 -07002383 u16 sdu_len;
2384 size_t pdu_len;
Mat Martineau94122bb2012-05-02 09:42:02 -07002385 u8 sar;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002386
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002387 BT_DBG("chan %p, msg %p, len %zu", chan, msg, len);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002388
Mat Martineau94122bb2012-05-02 09:42:02 -07002389 /* It is critical that ERTM PDUs fit in a single HCI fragment,
2390 * so fragmented skbs are not used. The HCI layer's handling
2391 * of fragmented skbs is not compatible with ERTM's queueing.
2392 */
2393
2394 /* PDU size is derived from the HCI MTU */
2395 pdu_len = chan->conn->mtu;
2396
Mat Martineaua5495742012-10-23 15:24:21 -07002397 /* Constrain PDU size for BR/EDR connections */
2398 if (!chan->hs_hcon)
2399 pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD);
Mat Martineau94122bb2012-05-02 09:42:02 -07002400
2401 /* Adjust for largest possible L2CAP overhead. */
Gustavo Padovan35d401d2012-05-25 18:57:05 -03002402 if (chan->fcs)
2403 pdu_len -= L2CAP_FCS_SIZE;
2404
Gustavo Padovanba7aa642012-05-29 13:29:16 -03002405 pdu_len -= __ertm_hdr_size(chan);
Mat Martineau94122bb2012-05-02 09:42:02 -07002406
2407 /* Remote device may have requested smaller PDUs */
2408 pdu_len = min_t(size_t, pdu_len, chan->remote_mps);
2409
2410 if (len <= pdu_len) {
2411 sar = L2CAP_SAR_UNSEGMENTED;
2412 sdu_len = 0;
2413 pdu_len = len;
2414 } else {
2415 sar = L2CAP_SAR_START;
2416 sdu_len = len;
2417 pdu_len -= L2CAP_SDULEN_SIZE;
2418 }
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002419
2420 while (len > 0) {
Mat Martineau94122bb2012-05-02 09:42:02 -07002421 skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002422
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002423 if (IS_ERR(skb)) {
Mat Martineau94122bb2012-05-02 09:42:02 -07002424 __skb_queue_purge(seg_queue);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002425 return PTR_ERR(skb);
2426 }
2427
Mat Martineau94122bb2012-05-02 09:42:02 -07002428 bt_cb(skb)->control.sar = sar;
2429 __skb_queue_tail(seg_queue, skb);
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002430
Mat Martineau94122bb2012-05-02 09:42:02 -07002431 len -= pdu_len;
2432 if (sdu_len) {
2433 sdu_len = 0;
2434 pdu_len += L2CAP_SDULEN_SIZE;
2435 }
2436
2437 if (len <= pdu_len) {
2438 sar = L2CAP_SAR_END;
2439 pdu_len = len;
2440 } else {
2441 sar = L2CAP_SAR_CONTINUE;
2442 }
2443 }
2444
Gustavo Padovanf0f62792012-05-29 13:29:17 -03002445 return 0;
Gustavo F. Padovanc74e5602009-08-20 22:25:58 -03002446}
2447
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002448int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len,
Gustavo Padovan2d792812012-10-06 10:07:01 +01002449 u32 priority)
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002450{
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002451 struct sk_buff *skb;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002452 int err;
Mat Martineau94122bb2012-05-02 09:42:02 -07002453 struct sk_buff_head seg_queue;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002454
2455 /* Connectionless channel */
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03002456 if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002457 skb = l2cap_create_connless_pdu(chan, msg, len, priority);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002458 if (IS_ERR(skb))
2459 return PTR_ERR(skb);
2460
2461 l2cap_do_send(chan, skb);
2462 return len;
2463 }
2464
2465 switch (chan->mode) {
2466 case L2CAP_MODE_BASIC:
2467 /* Check outgoing MTU */
2468 if (len > chan->omtu)
2469 return -EMSGSIZE;
2470
2471 /* Create a basic PDU */
Luiz Augusto von Dentz5e59b792011-11-01 10:58:57 +02002472 skb = l2cap_create_basic_pdu(chan, msg, len, priority);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002473 if (IS_ERR(skb))
2474 return PTR_ERR(skb);
2475
2476 l2cap_do_send(chan, skb);
2477 err = len;
2478 break;
2479
2480 case L2CAP_MODE_ERTM:
2481 case L2CAP_MODE_STREAMING:
Mat Martineau94122bb2012-05-02 09:42:02 -07002482 /* Check outgoing MTU */
2483 if (len > chan->omtu) {
2484 err = -EMSGSIZE;
2485 break;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002486 }
2487
Mat Martineau94122bb2012-05-02 09:42:02 -07002488 __skb_queue_head_init(&seg_queue);
2489
2490 /* Do segmentation before calling in to the state machine,
2491 * since it's possible to block while waiting for memory
2492 * allocation.
2493 */
2494 err = l2cap_segment_sdu(chan, &seg_queue, msg, len);
2495
2496 /* The channel could have been closed while segmenting,
2497 * check that it is still connected.
2498 */
2499 if (chan->state != BT_CONNECTED) {
2500 __skb_queue_purge(&seg_queue);
2501 err = -ENOTCONN;
2502 }
2503
2504 if (err)
2505 break;
2506
Mat Martineau37339372012-05-17 20:53:33 -07002507 if (chan->mode == L2CAP_MODE_ERTM)
Gustavo Padovand6603662012-05-21 13:58:22 -03002508 l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST);
Mat Martineau37339372012-05-17 20:53:33 -07002509 else
Gustavo Padovand6603662012-05-21 13:58:22 -03002510 l2cap_streaming_send(chan, &seg_queue);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002511
Gustavo Padovand6603662012-05-21 13:58:22 -03002512 err = len;
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002513
Mat Martineau94122bb2012-05-02 09:42:02 -07002514 /* If the skbs were not queued for sending, they'll still be in
2515 * seg_queue and need to be purged.
2516 */
2517 __skb_queue_purge(&seg_queue);
Gustavo F. Padovan9a91a042011-04-28 18:50:17 -03002518 break;
2519
2520 default:
2521 BT_DBG("bad state %1.1x", chan->mode);
2522 err = -EBADFD;
2523 }
2524
2525 return err;
2526}
2527
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002528static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq)
2529{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002530 struct l2cap_ctrl control;
2531 u16 seq;
2532
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002533 BT_DBG("chan %p, txseq %u", chan, txseq);
Mat Martineaubed68bd2012-05-17 20:53:44 -07002534
2535 memset(&control, 0, sizeof(control));
2536 control.sframe = 1;
2537 control.super = L2CAP_SUPER_SREJ;
2538
2539 for (seq = chan->expected_tx_seq; seq != txseq;
2540 seq = __next_seq(chan, seq)) {
2541 if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) {
2542 control.reqseq = seq;
2543 l2cap_send_sframe(chan, &control);
2544 l2cap_seq_list_append(&chan->srej_list, seq);
2545 }
2546 }
2547
2548 chan->expected_tx_seq = __next_seq(chan, txseq);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002549}
2550
2551static void l2cap_send_srej_tail(struct l2cap_chan *chan)
2552{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002553 struct l2cap_ctrl control;
2554
2555 BT_DBG("chan %p", chan);
2556
2557 if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR)
2558 return;
2559
2560 memset(&control, 0, sizeof(control));
2561 control.sframe = 1;
2562 control.super = L2CAP_SUPER_SREJ;
2563 control.reqseq = chan->srej_list.tail;
2564 l2cap_send_sframe(chan, &control);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002565}
2566
2567static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq)
2568{
Mat Martineaubed68bd2012-05-17 20:53:44 -07002569 struct l2cap_ctrl control;
2570 u16 initial_head;
2571 u16 seq;
2572
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002573 BT_DBG("chan %p, txseq %u", chan, txseq);
Mat Martineaubed68bd2012-05-17 20:53:44 -07002574
2575 memset(&control, 0, sizeof(control));
2576 control.sframe = 1;
2577 control.super = L2CAP_SUPER_SREJ;
2578
2579 /* Capture initial list head to allow only one pass through the list. */
2580 initial_head = chan->srej_list.head;
2581
2582 do {
2583 seq = l2cap_seq_list_pop(&chan->srej_list);
2584 if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR)
2585 break;
2586
2587 control.reqseq = seq;
2588 l2cap_send_sframe(chan, &control);
2589 l2cap_seq_list_append(&chan->srej_list, seq);
2590 } while (chan->srej_list.head != initial_head);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07002591}
2592
Mat Martineau608bcc62012-05-17 20:53:32 -07002593static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq)
2594{
2595 struct sk_buff *acked_skb;
2596 u16 ackseq;
2597
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002598 BT_DBG("chan %p, reqseq %u", chan, reqseq);
Mat Martineau608bcc62012-05-17 20:53:32 -07002599
2600 if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq)
2601 return;
2602
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002603 BT_DBG("expected_ack_seq %u, unacked_frames %u",
Mat Martineau608bcc62012-05-17 20:53:32 -07002604 chan->expected_ack_seq, chan->unacked_frames);
2605
2606 for (ackseq = chan->expected_ack_seq; ackseq != reqseq;
2607 ackseq = __next_seq(chan, ackseq)) {
2608
2609 acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq);
2610 if (acked_skb) {
2611 skb_unlink(acked_skb, &chan->tx_q);
2612 kfree_skb(acked_skb);
2613 chan->unacked_frames--;
2614 }
2615 }
2616
2617 chan->expected_ack_seq = reqseq;
2618
2619 if (chan->unacked_frames == 0)
2620 __clear_retrans_timer(chan);
2621
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002622 BT_DBG("unacked_frames %u", chan->unacked_frames);
Mat Martineau608bcc62012-05-17 20:53:32 -07002623}
2624
2625static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan)
2626{
2627 BT_DBG("chan %p", chan);
2628
2629 chan->expected_tx_seq = chan->buffer_seq;
2630 l2cap_seq_list_clear(&chan->srej_list);
2631 skb_queue_purge(&chan->srej_q);
2632 chan->rx_state = L2CAP_RX_STATE_RECV;
2633}
2634
Gustavo Padovand6603662012-05-21 13:58:22 -03002635static void l2cap_tx_state_xmit(struct l2cap_chan *chan,
2636 struct l2cap_ctrl *control,
2637 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002638{
Mat Martineau608bcc62012-05-17 20:53:32 -07002639 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2640 event);
2641
2642 switch (event) {
2643 case L2CAP_EV_DATA_REQUEST:
2644 if (chan->tx_send_head == NULL)
2645 chan->tx_send_head = skb_peek(skbs);
2646
2647 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2648 l2cap_ertm_send(chan);
2649 break;
2650 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2651 BT_DBG("Enter LOCAL_BUSY");
2652 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2653
2654 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2655 /* The SREJ_SENT state must be aborted if we are to
2656 * enter the LOCAL_BUSY state.
2657 */
2658 l2cap_abort_rx_srej_sent(chan);
2659 }
2660
2661 l2cap_send_ack(chan);
2662
2663 break;
2664 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2665 BT_DBG("Exit LOCAL_BUSY");
2666 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2667
2668 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2669 struct l2cap_ctrl local_control;
2670
2671 memset(&local_control, 0, sizeof(local_control));
2672 local_control.sframe = 1;
2673 local_control.super = L2CAP_SUPER_RR;
2674 local_control.poll = 1;
2675 local_control.reqseq = chan->buffer_seq;
Mat Martineaua67d7f62012-05-17 20:53:35 -07002676 l2cap_send_sframe(chan, &local_control);
Mat Martineau608bcc62012-05-17 20:53:32 -07002677
2678 chan->retry_count = 1;
2679 __set_monitor_timer(chan);
2680 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2681 }
2682 break;
2683 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2684 l2cap_process_reqseq(chan, control->reqseq);
2685 break;
2686 case L2CAP_EV_EXPLICIT_POLL:
2687 l2cap_send_rr_or_rnr(chan, 1);
2688 chan->retry_count = 1;
2689 __set_monitor_timer(chan);
2690 __clear_ack_timer(chan);
2691 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2692 break;
2693 case L2CAP_EV_RETRANS_TO:
2694 l2cap_send_rr_or_rnr(chan, 1);
2695 chan->retry_count = 1;
2696 __set_monitor_timer(chan);
2697 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2698 break;
2699 case L2CAP_EV_RECV_FBIT:
2700 /* Nothing to process */
2701 break;
2702 default:
2703 break;
2704 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002705}
2706
Gustavo Padovand6603662012-05-21 13:58:22 -03002707static void l2cap_tx_state_wait_f(struct l2cap_chan *chan,
2708 struct l2cap_ctrl *control,
2709 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002710{
Mat Martineau608bcc62012-05-17 20:53:32 -07002711 BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs,
2712 event);
2713
2714 switch (event) {
2715 case L2CAP_EV_DATA_REQUEST:
2716 if (chan->tx_send_head == NULL)
2717 chan->tx_send_head = skb_peek(skbs);
2718 /* Queue data, but don't send. */
2719 skb_queue_splice_tail_init(skbs, &chan->tx_q);
2720 break;
2721 case L2CAP_EV_LOCAL_BUSY_DETECTED:
2722 BT_DBG("Enter LOCAL_BUSY");
2723 set_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2724
2725 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
2726 /* The SREJ_SENT state must be aborted if we are to
2727 * enter the LOCAL_BUSY state.
2728 */
2729 l2cap_abort_rx_srej_sent(chan);
2730 }
2731
2732 l2cap_send_ack(chan);
2733
2734 break;
2735 case L2CAP_EV_LOCAL_BUSY_CLEAR:
2736 BT_DBG("Exit LOCAL_BUSY");
2737 clear_bit(CONN_LOCAL_BUSY, &chan->conn_state);
2738
2739 if (test_bit(CONN_RNR_SENT, &chan->conn_state)) {
2740 struct l2cap_ctrl local_control;
2741 memset(&local_control, 0, sizeof(local_control));
2742 local_control.sframe = 1;
2743 local_control.super = L2CAP_SUPER_RR;
2744 local_control.poll = 1;
2745 local_control.reqseq = chan->buffer_seq;
Mat Martineaua67d7f62012-05-17 20:53:35 -07002746 l2cap_send_sframe(chan, &local_control);
Mat Martineau608bcc62012-05-17 20:53:32 -07002747
2748 chan->retry_count = 1;
2749 __set_monitor_timer(chan);
2750 chan->tx_state = L2CAP_TX_STATE_WAIT_F;
2751 }
2752 break;
2753 case L2CAP_EV_RECV_REQSEQ_AND_FBIT:
2754 l2cap_process_reqseq(chan, control->reqseq);
2755
2756 /* Fall through */
2757
2758 case L2CAP_EV_RECV_FBIT:
2759 if (control && control->final) {
2760 __clear_monitor_timer(chan);
2761 if (chan->unacked_frames > 0)
2762 __set_retrans_timer(chan);
2763 chan->retry_count = 0;
2764 chan->tx_state = L2CAP_TX_STATE_XMIT;
2765 BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state);
2766 }
2767 break;
2768 case L2CAP_EV_EXPLICIT_POLL:
2769 /* Ignore */
2770 break;
2771 case L2CAP_EV_MONITOR_TO:
2772 if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) {
2773 l2cap_send_rr_or_rnr(chan, 1);
2774 __set_monitor_timer(chan);
2775 chan->retry_count++;
2776 } else {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02002777 l2cap_send_disconn_req(chan, ECONNABORTED);
Mat Martineau608bcc62012-05-17 20:53:32 -07002778 }
2779 break;
2780 default:
2781 break;
2782 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002783}
2784
Gustavo Padovand6603662012-05-21 13:58:22 -03002785static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
2786 struct sk_buff_head *skbs, u8 event)
Mat Martineau608bcc62012-05-17 20:53:32 -07002787{
Mat Martineau608bcc62012-05-17 20:53:32 -07002788 BT_DBG("chan %p, control %p, skbs %p, event %d, state %d",
2789 chan, control, skbs, event, chan->tx_state);
2790
2791 switch (chan->tx_state) {
2792 case L2CAP_TX_STATE_XMIT:
Gustavo Padovand6603662012-05-21 13:58:22 -03002793 l2cap_tx_state_xmit(chan, control, skbs, event);
Mat Martineau608bcc62012-05-17 20:53:32 -07002794 break;
2795 case L2CAP_TX_STATE_WAIT_F:
Gustavo Padovand6603662012-05-21 13:58:22 -03002796 l2cap_tx_state_wait_f(chan, control, skbs, event);
Mat Martineau608bcc62012-05-17 20:53:32 -07002797 break;
2798 default:
2799 /* Ignore event */
2800 break;
2801 }
Mat Martineau608bcc62012-05-17 20:53:32 -07002802}
2803
Mat Martineau4b51dae92012-05-17 20:53:37 -07002804static void l2cap_pass_to_tx(struct l2cap_chan *chan,
2805 struct l2cap_ctrl *control)
2806{
2807 BT_DBG("chan %p, control %p", chan, control);
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03002808 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT);
Mat Martineau4b51dae92012-05-17 20:53:37 -07002809}
2810
Mat Martineauf80842a2012-05-17 20:53:46 -07002811static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan,
2812 struct l2cap_ctrl *control)
2813{
2814 BT_DBG("chan %p, control %p", chan, control);
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03002815 l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT);
Mat Martineauf80842a2012-05-17 20:53:46 -07002816}
2817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818/* Copy frame to all raw sockets on that connection */
2819static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
2820{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 struct sk_buff *nskb;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03002822 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824 BT_DBG("conn %p", conn);
2825
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002826 mutex_lock(&conn->chan_lock);
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -02002827
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002828 list_for_each_entry(chan, &conn->chan_l, list) {
Gustavo F. Padovan48454072011-03-25 00:22:30 -03002829 struct sock *sk = chan->sk;
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03002830 if (chan->chan_type != L2CAP_CHAN_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 continue;
2832
2833 /* Don't send frame to the socket it came from */
2834 if (skb->sk == sk)
2835 continue;
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002836 nskb = skb_clone(skb, GFP_KERNEL);
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03002837 if (!nskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 continue;
2839
Gustavo Padovan80b98022012-05-27 22:27:51 -03002840 if (chan->ops->recv(chan, nskb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 kfree_skb(nskb);
2842 }
Gustavo F. Padovan3d57dc62011-12-17 10:56:45 -02002843
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02002844 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845}
2846
2847/* ---- L2CAP signalling commands ---- */
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002848static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code,
2849 u8 ident, u16 dlen, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
2851 struct sk_buff *skb, **frag;
2852 struct l2cap_cmd_hdr *cmd;
2853 struct l2cap_hdr *lh;
2854 int len, count;
2855
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002856 BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u",
2857 conn, code, ident, dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Anderson Lizardo300b9622013-06-02 16:30:40 -04002859 if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE)
2860 return NULL;
2861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
2863 count = min_t(unsigned int, conn->mtu, len);
2864
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002865 skb = bt_skb_alloc(count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (!skb)
2867 return NULL;
2868
2869 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002870 lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02002871
2872 if (conn->hcon->type == LE_LINK)
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03002873 lh->cid = __constant_cpu_to_le16(L2CAP_CID_LE_SIGNALING);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02002874 else
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03002875 lh->cid = __constant_cpu_to_le16(L2CAP_CID_SIGNALING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
2877 cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
2878 cmd->code = code;
2879 cmd->ident = ident;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07002880 cmd->len = cpu_to_le16(dlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
2882 if (dlen) {
2883 count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
2884 memcpy(skb_put(skb, count), data, count);
2885 data += count;
2886 }
2887
2888 len -= skb->len;
2889
2890 /* Continuation fragments (no L2CAP header) */
2891 frag = &skb_shinfo(skb)->frag_list;
2892 while (len) {
2893 count = min_t(unsigned int, conn->mtu, len);
2894
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03002895 *frag = bt_skb_alloc(count, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (!*frag)
2897 goto fail;
2898
2899 memcpy(skb_put(*frag, count), data, count);
2900
2901 len -= count;
2902 data += count;
2903
2904 frag = &(*frag)->next;
2905 }
2906
2907 return skb;
2908
2909fail:
2910 kfree_skb(skb);
2911 return NULL;
2912}
2913
Gustavo Padovan2d792812012-10-06 10:07:01 +01002914static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen,
2915 unsigned long *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
2917 struct l2cap_conf_opt *opt = *ptr;
2918 int len;
2919
2920 len = L2CAP_CONF_OPT_SIZE + opt->len;
2921 *ptr += len;
2922
2923 *type = opt->type;
2924 *olen = opt->len;
2925
2926 switch (opt->len) {
2927 case 1:
2928 *val = *((u8 *) opt->val);
2929 break;
2930
2931 case 2:
steven miaobfaaeb32010-10-16 18:29:47 -04002932 *val = get_unaligned_le16(opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 break;
2934
2935 case 4:
steven miaobfaaeb32010-10-16 18:29:47 -04002936 *val = get_unaligned_le32(opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 break;
2938
2939 default:
2940 *val = (unsigned long) opt->val;
2941 break;
2942 }
2943
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002944 BT_DBG("type 0x%2.2x len %u val 0x%lx", *type, opt->len, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return len;
2946}
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val)
2949{
2950 struct l2cap_conf_opt *opt = *ptr;
2951
Andrei Emeltchenkob4400672012-07-10 15:27:49 +03002952 BT_DBG("type 0x%2.2x len %u val 0x%lx", type, len, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
2954 opt->type = type;
2955 opt->len = len;
2956
2957 switch (len) {
2958 case 1:
2959 *((u8 *) opt->val) = val;
2960 break;
2961
2962 case 2:
Gustavo F. Padovan4f8b6912010-10-18 14:25:53 -02002963 put_unaligned_le16(val, opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 break;
2965
2966 case 4:
Gustavo F. Padovan4f8b6912010-10-18 14:25:53 -02002967 put_unaligned_le32(val, opt->val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 break;
2969
2970 default:
2971 memcpy(opt->val, (void *) val, len);
2972 break;
2973 }
2974
2975 *ptr += L2CAP_CONF_OPT_SIZE + len;
2976}
2977
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03002978static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan)
2979{
2980 struct l2cap_conf_efs efs;
2981
Szymon Janc1ec918c2011-11-16 09:32:21 +01002982 switch (chan->mode) {
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03002983 case L2CAP_MODE_ERTM:
2984 efs.id = chan->local_id;
2985 efs.stype = chan->local_stype;
2986 efs.msdu = cpu_to_le16(chan->local_msdu);
2987 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03002988 efs.acc_lat = __constant_cpu_to_le32(L2CAP_DEFAULT_ACC_LAT);
Andrei Emeltchenko8936fa62012-10-08 11:14:41 +03002989 efs.flush_to = __constant_cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03002990 break;
2991
2992 case L2CAP_MODE_STREAMING:
2993 efs.id = 1;
2994 efs.stype = L2CAP_SERV_BESTEFFORT;
2995 efs.msdu = cpu_to_le16(chan->local_msdu);
2996 efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime);
2997 efs.acc_lat = 0;
2998 efs.flush_to = 0;
2999 break;
3000
3001 default:
3002 return;
3003 }
3004
3005 l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs),
Andrei Emeltchenko8936fa62012-10-08 11:14:41 +03003006 (unsigned long) &efs);
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003007}
3008
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003009static void l2cap_ack_timeout(struct work_struct *work)
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003010{
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003011 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
Mat Martineau03625202012-05-17 20:53:51 -07003012 ack_timer.work);
3013 u16 frames_to_ack;
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003014
Gustavo F. Padovan2fb9b3d2011-12-22 16:56:05 -02003015 BT_DBG("chan %p", chan);
3016
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003017 l2cap_chan_lock(chan);
3018
Mat Martineau03625202012-05-17 20:53:51 -07003019 frames_to_ack = __seq_offset(chan, chan->buffer_seq,
3020 chan->last_acked_seq);
3021
3022 if (frames_to_ack)
3023 l2cap_send_rr_or_rnr(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003024
3025 l2cap_chan_unlock(chan);
Szymon Janc09bfb2e2012-01-11 10:59:49 +01003026 l2cap_chan_put(chan);
Gustavo F. Padovanc1b4f432010-05-01 16:15:39 -03003027}
3028
Andrei Emeltchenko466f8002012-05-29 13:59:01 +03003029int l2cap_ertm_init(struct l2cap_chan *chan)
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003030{
Mat Martineau3c588192012-04-11 10:48:42 -07003031 int err;
3032
Mat Martineau105bdf92012-04-27 16:50:48 -07003033 chan->next_tx_seq = 0;
3034 chan->expected_tx_seq = 0;
Gustavo F. Padovan42e5c802011-03-25 19:58:34 -03003035 chan->expected_ack_seq = 0;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03003036 chan->unacked_frames = 0;
Gustavo F. Padovan42e5c802011-03-25 19:58:34 -03003037 chan->buffer_seq = 0;
Gustavo F. Padovan6a026612011-04-01 00:38:50 -03003038 chan->frames_sent = 0;
Mat Martineau105bdf92012-04-27 16:50:48 -07003039 chan->last_acked_seq = 0;
3040 chan->sdu = NULL;
3041 chan->sdu_last_frag = NULL;
3042 chan->sdu_len = 0;
3043
Mat Martineaud34c34f2012-05-14 14:49:27 -07003044 skb_queue_head_init(&chan->tx_q);
3045
Mat Martineau08333282012-10-23 15:24:06 -07003046 chan->local_amp_id = 0;
3047 chan->move_id = 0;
3048 chan->move_state = L2CAP_MOVE_STABLE;
3049 chan->move_role = L2CAP_MOVE_ROLE_NONE;
3050
Mat Martineau105bdf92012-04-27 16:50:48 -07003051 if (chan->mode != L2CAP_MODE_ERTM)
3052 return 0;
3053
3054 chan->rx_state = L2CAP_RX_STATE_RECV;
3055 chan->tx_state = L2CAP_TX_STATE_XMIT;
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003056
Gustavo F. Padovan721c4182011-06-23 19:29:58 -03003057 INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout);
3058 INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout);
3059 INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003060
Gustavo F. Padovanf1c67752011-03-25 20:36:10 -03003061 skb_queue_head_init(&chan->srej_q);
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03003062
Mat Martineau3c588192012-04-11 10:48:42 -07003063 err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win);
3064 if (err < 0)
3065 return err;
3066
Mat Martineau9dc9aff2012-05-17 16:20:14 -07003067 err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win);
3068 if (err < 0)
3069 l2cap_seq_list_free(&chan->srej_list);
3070
3071 return err;
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03003072}
3073
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003074static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
3075{
3076 switch (mode) {
3077 case L2CAP_MODE_STREAMING:
3078 case L2CAP_MODE_ERTM:
3079 if (l2cap_mode_supported(mode, remote_feat_mask))
3080 return mode;
3081 /* fall through */
3082 default:
3083 return L2CAP_MODE_BASIC;
3084 }
3085}
3086
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003087static inline bool __l2cap_ews_supported(struct l2cap_chan *chan)
3088{
3089 return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW;
3090}
3091
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003092static inline bool __l2cap_efs_supported(struct l2cap_chan *chan)
3093{
3094 return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW;
3095}
3096
Mat Martineau36c86c82012-10-23 15:24:20 -07003097static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan,
3098 struct l2cap_conf_rfc *rfc)
3099{
3100 if (chan->local_amp_id && chan->hs_hcon) {
3101 u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to;
3102
3103 /* Class 1 devices have must have ERTM timeouts
3104 * exceeding the Link Supervision Timeout. The
3105 * default Link Supervision Timeout for AMP
3106 * controllers is 10 seconds.
3107 *
3108 * Class 1 devices use 0xffffffff for their
3109 * best-effort flush timeout, so the clamping logic
3110 * will result in a timeout that meets the above
3111 * requirement. ERTM timeouts are 16-bit values, so
3112 * the maximum timeout is 65.535 seconds.
3113 */
3114
3115 /* Convert timeout to milliseconds and round */
3116 ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000);
3117
3118 /* This is the recommended formula for class 2 devices
3119 * that start ERTM timers when packets are sent to the
3120 * controller.
3121 */
3122 ertm_to = 3 * ertm_to + 500;
3123
3124 if (ertm_to > 0xffff)
3125 ertm_to = 0xffff;
3126
3127 rfc->retrans_timeout = cpu_to_le16((u16) ertm_to);
3128 rfc->monitor_timeout = rfc->retrans_timeout;
3129 } else {
3130 rfc->retrans_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
3131 rfc->monitor_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
3132 }
3133}
3134
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003135static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
3136{
3137 if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003138 __l2cap_ews_supported(chan)) {
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003139 /* use extended control field */
3140 set_bit(FLAG_EXT_CTRL, &chan->flags);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003141 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
3142 } else {
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003143 chan->tx_win = min_t(u16, chan->tx_win,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003144 L2CAP_DEFAULT_TX_WINDOW);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003145 chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW;
3146 }
Mat Martineauc20f8e32012-07-10 05:47:07 -07003147 chan->ack_win = chan->tx_win;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003148}
3149
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003150static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 struct l2cap_conf_req *req = data;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003153 struct l2cap_conf_rfc rfc = { .mode = chan->mode };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 void *ptr = req->data;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003155 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03003157 BT_DBG("chan %p", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003159 if (chan->num_conf_req || chan->num_conf_rsp)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003160 goto done;
3161
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003162 switch (chan->mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003163 case L2CAP_MODE_STREAMING:
3164 case L2CAP_MODE_ERTM:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003165 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003166 break;
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003167
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003168 if (__l2cap_efs_supported(chan))
3169 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3170
Gustavo F. Padovan2ba13ed2010-06-09 16:39:05 -03003171 /* fall through */
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003172 default:
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003173 chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003174 break;
3175 }
3176
3177done:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003178 if (chan->imtu != L2CAP_DEFAULT_MTU)
3179 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
Gustavo F. Padovan7990681c2011-01-24 16:01:43 -02003180
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003181 switch (chan->mode) {
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003182 case L2CAP_MODE_BASIC:
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003183 if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003184 !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING))
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003185 break;
3186
Gustavo F. Padovan62547752010-06-08 20:05:31 -03003187 rfc.mode = L2CAP_MODE_BASIC;
3188 rfc.txwin_size = 0;
3189 rfc.max_transmit = 0;
3190 rfc.retrans_timeout = 0;
3191 rfc.monitor_timeout = 0;
3192 rfc.max_pdu_size = 0;
3193
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003194 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003195 (unsigned long) &rfc);
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003196 break;
3197
3198 case L2CAP_MODE_ERTM:
3199 rfc.mode = L2CAP_MODE_ERTM;
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003200 rfc.max_transmit = chan->max_tx;
Mat Martineau36c86c82012-10-23 15:24:20 -07003201
3202 __l2cap_set_ertm_timeouts(chan, &rfc);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003203
3204 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
Gustavo Padovan2d792812012-10-06 10:07:01 +01003205 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3206 L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003207 rfc.max_pdu_size = cpu_to_le16(size);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003208
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003209 l2cap_txwin_setup(chan);
3210
3211 rfc.txwin_size = min_t(u16, chan->tx_win,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003212 L2CAP_DEFAULT_TX_WINDOW);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003213
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003214 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003215 (unsigned long) &rfc);
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003216
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003217 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
3218 l2cap_add_opt_efs(&ptr, chan);
3219
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003220 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3221 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003222 chan->tx_win);
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003223
3224 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3225 if (chan->fcs == L2CAP_FCS_NONE ||
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003226 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003227 chan->fcs = L2CAP_FCS_NONE;
3228 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
3229 chan->fcs);
3230 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003231 break;
3232
3233 case L2CAP_MODE_STREAMING:
Mat Martineau273759e2012-05-17 20:53:53 -07003234 l2cap_txwin_setup(chan);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003235 rfc.mode = L2CAP_MODE_STREAMING;
3236 rfc.txwin_size = 0;
3237 rfc.max_transmit = 0;
3238 rfc.retrans_timeout = 0;
3239 rfc.monitor_timeout = 0;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003240
3241 size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu -
Gustavo Padovan2d792812012-10-06 10:07:01 +01003242 L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE -
3243 L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003244 rfc.max_pdu_size = cpu_to_le16(size);
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003245
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003246 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003247 (unsigned long) &rfc);
Gustavo F. Padovan63406502010-08-03 23:49:29 -03003248
Andrei Emeltchenkof89cef02011-10-13 16:18:55 +03003249 if (test_bit(FLAG_EFS_ENABLE, &chan->flags))
3250 l2cap_add_opt_efs(&ptr, chan);
3251
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003252 if (chan->conn->feat_mask & L2CAP_FEAT_FCS)
3253 if (chan->fcs == L2CAP_FCS_NONE ||
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003254 test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) {
Andrei Emeltchenko60918912012-11-29 17:46:06 +02003255 chan->fcs = L2CAP_FCS_NONE;
3256 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1,
3257 chan->fcs);
3258 }
Marcel Holtmann65c7c492009-05-02 23:07:53 -07003259 break;
3260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003262 req->dcid = cpu_to_le16(chan->dcid);
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03003263 req->flags = __constant_cpu_to_le16(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
3265 return ptr - data;
3266}
3267
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003268static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003270 struct l2cap_conf_rsp *rsp = data;
3271 void *ptr = rsp->data;
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003272 void *req = chan->conf_req;
3273 int len = chan->conf_len;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003274 int type, hint, olen;
3275 unsigned long val;
Marcel Holtmann6464f352007-10-20 13:39:51 +02003276 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003277 struct l2cap_conf_efs efs;
3278 u8 remote_efs = 0;
Marcel Holtmann861d6882007-10-20 13:37:06 +02003279 u16 mtu = L2CAP_DEFAULT_MTU;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003280 u16 result = L2CAP_CONF_SUCCESS;
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003281 u16 size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003283 BT_DBG("chan %p", chan);
Marcel Holtmann820ae1b2006-11-18 22:15:00 +01003284
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003285 while (len >= L2CAP_CONF_OPT_SIZE) {
3286 len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Gustavo F. Padovan589d2742009-04-20 01:31:07 -03003288 hint = type & L2CAP_CONF_HINT;
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -07003289 type &= L2CAP_CONF_MASK;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003290
3291 switch (type) {
3292 case L2CAP_CONF_MTU:
Marcel Holtmann861d6882007-10-20 13:37:06 +02003293 mtu = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003294 break;
3295
3296 case L2CAP_CONF_FLUSH_TO:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003297 chan->flush_to = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003298 break;
3299
3300 case L2CAP_CONF_QOS:
3301 break;
3302
Marcel Holtmann6464f352007-10-20 13:39:51 +02003303 case L2CAP_CONF_RFC:
3304 if (olen == sizeof(rfc))
3305 memcpy(&rfc, (void *) val, olen);
3306 break;
3307
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003308 case L2CAP_CONF_FCS:
3309 if (val == L2CAP_FCS_NONE)
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003310 set_bit(CONF_RECV_NO_FCS, &chan->conf_state);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003311 break;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003312
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003313 case L2CAP_CONF_EFS:
3314 remote_efs = 1;
3315 if (olen == sizeof(efs))
3316 memcpy(&efs, (void *) val, olen);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03003317 break;
3318
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003319 case L2CAP_CONF_EWS:
3320 if (!enable_hs)
3321 return -ECONNREFUSED;
3322
3323 set_bit(FLAG_EXT_CTRL, &chan->flags);
3324 set_bit(CONF_EWS_RECV, &chan->conf_state);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03003325 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003326 chan->remote_tx_win = val;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003327 break;
3328
3329 default:
3330 if (hint)
3331 break;
3332
3333 result = L2CAP_CONF_UNKNOWN;
3334 *((u8 *) ptr++) = type;
3335 break;
3336 }
3337 }
3338
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003339 if (chan->num_conf_rsp || chan->num_conf_req > 1)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003340 goto done;
3341
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003342 switch (chan->mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003343 case L2CAP_MODE_STREAMING:
3344 case L2CAP_MODE_ERTM:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003345 if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003346 chan->mode = l2cap_select_mode(rfc.mode,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003347 chan->conn->feat_mask);
Gustavo F. Padovan85eb53c2010-06-03 18:43:28 -03003348 break;
3349 }
3350
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003351 if (remote_efs) {
3352 if (__l2cap_efs_supported(chan))
3353 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3354 else
3355 return -ECONNREFUSED;
3356 }
3357
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003358 if (chan->mode != rfc.mode)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003359 return -ECONNREFUSED;
Gustavo F. Padovan742e5192010-06-08 19:09:48 -03003360
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003361 break;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003362 }
3363
3364done:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003365 if (chan->mode != rfc.mode) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003366 result = L2CAP_CONF_UNACCEPT;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003367 rfc.mode = chan->mode;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003368
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003369 if (chan->num_conf_rsp == 1)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003370 return -ECONNREFUSED;
3371
Gustavo Padovan2d792812012-10-06 10:07:01 +01003372 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3373 (unsigned long) &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003374 }
3375
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003376 if (result == L2CAP_CONF_SUCCESS) {
3377 /* Configure output options and let the other side know
3378 * which ones we don't like. */
3379
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003380 if (mtu < L2CAP_DEFAULT_MIN_MTU)
3381 result = L2CAP_CONF_UNACCEPT;
3382 else {
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003383 chan->omtu = mtu;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003384 set_bit(CONF_MTU_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003385 }
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003386 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003387
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003388 if (remote_efs) {
3389 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003390 efs.stype != L2CAP_SERV_NOTRAFIC &&
3391 efs.stype != chan->local_stype) {
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003392
3393 result = L2CAP_CONF_UNACCEPT;
3394
3395 if (chan->num_conf_req >= 1)
3396 return -ECONNREFUSED;
3397
3398 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003399 sizeof(efs),
3400 (unsigned long) &efs);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003401 } else {
Gustavo F. Padovan3e6b3b92011-11-01 14:06:23 -02003402 /* Send PENDING Conf Rsp */
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003403 result = L2CAP_CONF_PENDING;
3404 set_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003405 }
3406 }
3407
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003408 switch (rfc.mode) {
3409 case L2CAP_MODE_BASIC:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003410 chan->fcs = L2CAP_FCS_NONE;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003411 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003412 break;
3413
3414 case L2CAP_MODE_ERTM:
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003415 if (!test_bit(CONF_EWS_RECV, &chan->conf_state))
3416 chan->remote_tx_win = rfc.txwin_size;
3417 else
3418 rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW;
3419
Gustavo F. Padovan2c03a7a2011-03-25 20:15:28 -03003420 chan->remote_max_tx = rfc.max_transmit;
Mat Martineau86b1b262010-08-05 15:54:22 -07003421
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003422 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003423 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3424 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003425 rfc.max_pdu_size = cpu_to_le16(size);
3426 chan->remote_mps = size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003427
Mat Martineau36c86c82012-10-23 15:24:20 -07003428 __l2cap_set_ertm_timeouts(chan, &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003429
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003430 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003431
3432 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003433 sizeof(rfc), (unsigned long) &rfc);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003434
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003435 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3436 chan->remote_id = efs.id;
3437 chan->remote_stype = efs.stype;
3438 chan->remote_msdu = le16_to_cpu(efs.msdu);
3439 chan->remote_flush_to =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003440 le32_to_cpu(efs.flush_to);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003441 chan->remote_acc_lat =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003442 le32_to_cpu(efs.acc_lat);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003443 chan->remote_sdu_itime =
3444 le32_to_cpu(efs.sdu_itime);
3445 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003446 sizeof(efs),
3447 (unsigned long) &efs);
Andrei Emeltchenko42dceae2011-10-17 14:35:30 +03003448 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003449 break;
3450
3451 case L2CAP_MODE_STREAMING:
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003452 size = min_t(u16, le16_to_cpu(rfc.max_pdu_size),
Gustavo Padovan2d792812012-10-06 10:07:01 +01003453 chan->conn->mtu - L2CAP_EXT_HDR_SIZE -
3454 L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE);
Andrei Emeltchenkoc8f79162011-10-17 12:19:59 +03003455 rfc.max_pdu_size = cpu_to_le16(size);
3456 chan->remote_mps = size;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003457
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003458 set_bit(CONF_MODE_DONE, &chan->conf_state);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003459
Gustavo Padovan2d792812012-10-06 10:07:01 +01003460 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
3461 (unsigned long) &rfc);
Gustavo F. Padovan68ae6632009-10-17 21:41:01 -03003462
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003463 break;
3464
3465 default:
Marcel Holtmann6464f352007-10-20 13:39:51 +02003466 result = L2CAP_CONF_UNACCEPT;
3467
3468 memset(&rfc, 0, sizeof(rfc));
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003469 rfc.mode = chan->mode;
Marcel Holtmann6464f352007-10-20 13:39:51 +02003470 }
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003471
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003472 if (result == L2CAP_CONF_SUCCESS)
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003473 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003474 }
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003475 rsp->scid = cpu_to_le16(chan->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003476 rsp->result = cpu_to_le16(result);
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03003477 rsp->flags = __constant_cpu_to_le16(0);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003478
3479 return ptr - data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480}
3481
Gustavo Padovan2d792812012-10-06 10:07:01 +01003482static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len,
3483 void *data, u16 *result)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003484{
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003485 struct l2cap_conf_req *req = data;
3486 void *ptr = req->data;
3487 int type, olen;
3488 unsigned long val;
Mat Martineau36e999a2011-12-08 17:23:21 -08003489 struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003490 struct l2cap_conf_efs efs;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003491
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003492 BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003493
3494 while (len >= L2CAP_CONF_OPT_SIZE) {
3495 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3496
3497 switch (type) {
3498 case L2CAP_CONF_MTU:
3499 if (val < L2CAP_DEFAULT_MIN_MTU) {
3500 *result = L2CAP_CONF_UNACCEPT;
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003501 chan->imtu = L2CAP_DEFAULT_MIN_MTU;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003502 } else
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003503 chan->imtu = val;
3504 l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003505 break;
3506
3507 case L2CAP_CONF_FLUSH_TO:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003508 chan->flush_to = val;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003509 l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003510 2, chan->flush_to);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003511 break;
3512
3513 case L2CAP_CONF_RFC:
3514 if (olen == sizeof(rfc))
3515 memcpy(&rfc, (void *)val, olen);
3516
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003517 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003518 rfc.mode != chan->mode)
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003519 return -ECONNREFUSED;
3520
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003521 chan->fcs = 0;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003522
3523 l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003524 sizeof(rfc), (unsigned long) &rfc);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003525 break;
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003526
3527 case L2CAP_CONF_EWS:
Mat Martineauc20f8e32012-07-10 05:47:07 -07003528 chan->ack_win = min_t(u16, val, chan->ack_win);
Gustavo F. Padovan3e6b3b92011-11-01 14:06:23 -02003529 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2,
Mat Martineauc20f8e32012-07-10 05:47:07 -07003530 chan->tx_win);
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03003531 break;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003532
3533 case L2CAP_CONF_EFS:
3534 if (olen == sizeof(efs))
3535 memcpy(&efs, (void *)val, olen);
3536
3537 if (chan->local_stype != L2CAP_SERV_NOTRAFIC &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003538 efs.stype != L2CAP_SERV_NOTRAFIC &&
3539 efs.stype != chan->local_stype)
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003540 return -ECONNREFUSED;
3541
Gustavo Padovan2d792812012-10-06 10:07:01 +01003542 l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs),
3543 (unsigned long) &efs);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003544 break;
Andrei Emeltchenkocbabee72012-11-29 17:46:07 +02003545
3546 case L2CAP_CONF_FCS:
3547 if (*result == L2CAP_CONF_PENDING)
3548 if (val == L2CAP_FCS_NONE)
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003549 set_bit(CONF_RECV_NO_FCS,
Andrei Emeltchenkocbabee72012-11-29 17:46:07 +02003550 &chan->conf_state);
3551 break;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003552 }
3553 }
3554
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003555 if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode)
Gustavo F. Padovan6c2ea7a2010-06-08 20:08:49 -03003556 return -ECONNREFUSED;
3557
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003558 chan->mode = rfc.mode;
Gustavo F. Padovan6c2ea7a2010-06-08 20:08:49 -03003559
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03003560 if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003561 switch (rfc.mode) {
3562 case L2CAP_MODE_ERTM:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003563 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3564 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
3565 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Mat Martineauc20f8e32012-07-10 05:47:07 -07003566 if (!test_bit(FLAG_EXT_CTRL, &chan->flags))
3567 chan->ack_win = min_t(u16, chan->ack_win,
3568 rfc.txwin_size);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003569
3570 if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) {
3571 chan->local_msdu = le16_to_cpu(efs.msdu);
3572 chan->local_sdu_itime =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003573 le32_to_cpu(efs.sdu_itime);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003574 chan->local_acc_lat = le32_to_cpu(efs.acc_lat);
3575 chan->local_flush_to =
Gustavo Padovan2d792812012-10-06 10:07:01 +01003576 le32_to_cpu(efs.flush_to);
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003577 }
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003578 break;
Andrei Emeltchenko66af7aa2011-11-07 14:20:33 +02003579
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003580 case L2CAP_MODE_STREAMING:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003581 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003582 }
3583 }
3584
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003585 req->dcid = cpu_to_le16(chan->dcid);
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03003586 req->flags = __constant_cpu_to_le16(0);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03003587
3588 return ptr - data;
3589}
3590
Gustavo Padovan2d792812012-10-06 10:07:01 +01003591static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data,
3592 u16 result, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
3594 struct l2cap_conf_rsp *rsp = data;
3595 void *ptr = rsp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003597 BT_DBG("chan %p", chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003599 rsp->scid = cpu_to_le16(chan->dcid);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02003600 rsp->result = cpu_to_le16(result);
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07003601 rsp->flags = cpu_to_le16(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
3603 return ptr - data;
3604}
3605
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003606void __l2cap_connect_rsp_defer(struct l2cap_chan *chan)
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003607{
3608 struct l2cap_conn_rsp rsp;
Gustavo F. Padovan8c1d7872011-04-13 20:23:55 -03003609 struct l2cap_conn *conn = chan->conn;
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003610 u8 buf[128];
Andrei Emeltchenko439f34a2012-10-31 15:46:28 +02003611 u8 rsp_code;
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003612
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003613 rsp.scid = cpu_to_le16(chan->dcid);
3614 rsp.dcid = cpu_to_le16(chan->scid);
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03003615 rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS);
3616 rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO);
Andrei Emeltchenko439f34a2012-10-31 15:46:28 +02003617
3618 if (chan->hs_hcon)
3619 rsp_code = L2CAP_CREATE_CHAN_RSP;
3620 else
3621 rsp_code = L2CAP_CONN_RSP;
3622
3623 BT_DBG("chan %p rsp_code %u", chan, rsp_code);
3624
3625 l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp);
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003626
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003627 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003628 return;
3629
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003630 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003631 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan710f9b0a2011-03-25 14:30:37 -03003632 chan->num_conf_req++;
3633}
3634
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003635static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len)
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003636{
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003637 int type, olen;
3638 unsigned long val;
Mat Martineauc20f8e32012-07-10 05:47:07 -07003639 /* Use sane default values in case a misbehaving remote device
3640 * did not send an RFC or extended window size option.
3641 */
3642 u16 txwin_ext = chan->ack_win;
3643 struct l2cap_conf_rfc rfc = {
3644 .mode = chan->mode,
3645 .retrans_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO),
3646 .monitor_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO),
3647 .max_pdu_size = cpu_to_le16(chan->imtu),
3648 .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW),
3649 };
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003650
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003651 BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003652
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003653 if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING))
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003654 return;
3655
3656 while (len >= L2CAP_CONF_OPT_SIZE) {
3657 len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
3658
Mat Martineauc20f8e32012-07-10 05:47:07 -07003659 switch (type) {
3660 case L2CAP_CONF_RFC:
3661 if (olen == sizeof(rfc))
3662 memcpy(&rfc, (void *)val, olen);
Szymon Janc8f321f82012-06-08 11:33:33 +02003663 break;
Mat Martineauc20f8e32012-07-10 05:47:07 -07003664 case L2CAP_CONF_EWS:
3665 txwin_ext = val;
3666 break;
3667 }
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003668 }
3669
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003670 switch (rfc.mode) {
3671 case L2CAP_MODE_ERTM:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003672 chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
3673 chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
Mat Martineauc20f8e32012-07-10 05:47:07 -07003674 chan->mps = le16_to_cpu(rfc.max_pdu_size);
3675 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
3676 chan->ack_win = min_t(u16, chan->ack_win, txwin_ext);
3677 else
3678 chan->ack_win = min_t(u16, chan->ack_win,
3679 rfc.txwin_size);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003680 break;
3681 case L2CAP_MODE_STREAMING:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003682 chan->mps = le16_to_cpu(rfc.max_pdu_size);
Gustavo F. Padovan7b1c0042010-05-01 16:15:39 -03003683 }
3684}
3685
Gustavo Padovan2d792812012-10-06 10:07:01 +01003686static inline int l2cap_command_rej(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003687 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3688 u8 *data)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003689{
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003690 struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data;
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003691
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003692 if (cmd_len < sizeof(*rej))
3693 return -EPROTO;
3694
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003695 if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD)
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003696 return 0;
3697
3698 if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003699 cmd->ident == conn->info_ident) {
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02003700 cancel_delayed_work(&conn->info_timer);
Marcel Holtmann984947d2009-02-06 23:35:19 +01003701
3702 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01003703 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01003704
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02003705 l2cap_conn_start(conn);
3706 }
3707
3708 return 0;
3709}
3710
Mat Martineau17009152012-10-23 15:24:07 -07003711static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3712 struct l2cap_cmd_hdr *cmd,
3713 u8 *data, u8 rsp_code, u8 amp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
3716 struct l2cap_conn_rsp rsp;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003717 struct l2cap_chan *chan = NULL, *pchan;
Nathan Holsteind793fe82010-10-15 11:54:02 -04003718 struct sock *parent, *sk = NULL;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003719 int result, status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
3721 u16 dcid = 0, scid = __le16_to_cpu(req->scid);
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003722 __le16 psm = req->psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
Andrei Emeltchenko097db762012-03-09 14:16:17 +02003724 BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
3726 /* Check if we have socket listening on psm */
Ido Yarivc2287682012-04-20 15:46:07 -03003727 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src, conn->dst);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003728 if (!pchan) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 result = L2CAP_CR_BAD_PSM;
3730 goto sendresp;
3731 }
3732
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03003733 parent = pchan->sk;
3734
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003735 mutex_lock(&conn->chan_lock);
Gustavo F. Padovanaa2ac882011-06-24 01:53:01 -03003736 lock_sock(parent);
Gustavo F. Padovane0f0cb52010-11-01 18:43:53 +00003737
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003738 /* Check if the ACL is secure enough (if not SDP) */
Andrei Emeltchenko2983fd62012-05-24 15:42:50 +03003739 if (psm != __constant_cpu_to_le16(L2CAP_PSM_SDP) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003740 !hci_conn_check_link_mode(conn->hcon)) {
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02003741 conn->disc_reason = HCI_ERROR_AUTH_FAILURE;
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +02003742 result = L2CAP_CR_SEC_BLOCK;
3743 goto response;
3744 }
3745
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 result = L2CAP_CR_NO_MEM;
3747
Gustavo Padovan2dfa1002012-05-27 22:27:58 -03003748 /* Check if we already have channel with that dcid */
3749 if (__l2cap_get_chan_by_dcid(conn, scid))
3750 goto response;
3751
Gustavo Padovan80b98022012-05-27 22:27:51 -03003752 chan = pchan->ops->new_connection(pchan);
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03003753 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 goto response;
3755
Gustavo F. Padovan80808e42011-05-16 17:24:37 -03003756 sk = chan->sk;
3757
Syam Sidhardhan330b6c12013-08-06 01:59:12 +09003758 /* For certain devices (ex: HID mouse), support for authentication,
3759 * pairing and bonding is optional. For such devices, inorder to avoid
3760 * the ACL alive for too long after L2CAP disconnection, reset the ACL
3761 * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect.
3762 */
3763 conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
3764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 bacpy(&bt_sk(sk)->src, conn->src);
3766 bacpy(&bt_sk(sk)->dst, conn->dst);
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003767 chan->psm = psm;
3768 chan->dcid = scid;
Mat Martineau17009152012-10-23 15:24:07 -07003769 chan->local_amp_id = amp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003771 __l2cap_chan_add(conn, chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003772
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003773 dcid = chan->scid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -03003775 __set_chan_timer(chan, sk->sk_sndtimeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03003777 chan->ident = cmd->ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Marcel Holtmann984947d2009-02-06 23:35:19 +01003779 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
Gustavo F. Padovand45fc422011-11-05 19:54:24 -02003780 if (l2cap_chan_check_security(chan)) {
Gustavo Padovanc5daa682012-05-16 12:17:10 -03003781 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
Andrei Emeltchenko0e587be2012-02-21 12:54:57 +02003782 __l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003783 result = L2CAP_CR_PEND;
3784 status = L2CAP_CS_AUTHOR_PEND;
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08003785 chan->ops->defer(chan);
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003786 } else {
Mat Martineau17009152012-10-23 15:24:07 -07003787 /* Force pending result for AMP controllers.
3788 * The connection will succeed after the
3789 * physical link is up.
3790 */
3791 if (amp_id) {
3792 __l2cap_state_change(chan, BT_CONNECT2);
3793 result = L2CAP_CR_PEND;
3794 } else {
3795 __l2cap_state_change(chan, BT_CONFIG);
3796 result = L2CAP_CR_SUCCESS;
3797 }
Marcel Holtmannf66dc812009-01-15 21:57:00 +01003798 status = L2CAP_CS_NO_INFO;
3799 }
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003800 } else {
Andrei Emeltchenko0e587be2012-02-21 12:54:57 +02003801 __l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003802 result = L2CAP_CR_PEND;
3803 status = L2CAP_CS_AUTHEN_PEND;
3804 }
3805 } else {
Andrei Emeltchenko0e587be2012-02-21 12:54:57 +02003806 __l2cap_state_change(chan, BT_CONNECT2);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003807 result = L2CAP_CR_PEND;
3808 status = L2CAP_CS_NO_INFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 }
3810
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811response:
Gustavo F. Padovanaa2ac882011-06-24 01:53:01 -03003812 release_sock(parent);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003813 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
3815sendresp:
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07003816 rsp.scid = cpu_to_le16(scid);
3817 rsp.dcid = cpu_to_le16(dcid);
3818 rsp.result = cpu_to_le16(result);
3819 rsp.status = cpu_to_le16(status);
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003820 l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003821
3822 if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) {
3823 struct l2cap_info_req info;
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03003824 info.type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003825
3826 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
3827 conn->info_ident = l2cap_get_ident(conn);
3828
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08003829 schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003830
Gustavo Padovan2d792812012-10-06 10:07:01 +01003831 l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ,
3832 sizeof(info), &info);
Marcel Holtmann79d554a2008-07-14 20:13:44 +02003833 }
3834
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003835 if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) &&
Gustavo Padovan2d792812012-10-06 10:07:01 +01003836 result == L2CAP_CR_SUCCESS) {
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003837 u8 buf[128];
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003838 set_bit(CONF_REQ_SENT, &chan->conf_state);
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003839 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003840 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003841 chan->num_conf_req++;
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003842 }
Mat Martineau17009152012-10-23 15:24:07 -07003843
3844 return chan;
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003845}
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003846
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003847static int l2cap_connect_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003848 struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
Mat Martineau4c89b6a2012-10-11 17:48:22 +03003849{
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303850 struct hci_dev *hdev = conn->hcon->hdev;
3851 struct hci_conn *hcon = conn->hcon;
3852
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003853 if (cmd_len < sizeof(struct l2cap_conn_req))
3854 return -EPROTO;
3855
Jaganath Kanakkassery7b064ed2013-01-10 10:28:35 +05303856 hci_dev_lock(hdev);
3857 if (test_bit(HCI_MGMT, &hdev->dev_flags) &&
3858 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags))
3859 mgmt_device_connected(hdev, &hcon->dst, hcon->type,
3860 hcon->dst_type, 0, NULL, 0,
3861 hcon->dev_class);
3862 hci_dev_unlock(hdev);
3863
Gustavo Padovan300229f2012-10-12 19:40:40 +08003864 l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 return 0;
3866}
3867
Mat Martineau5909cf32012-10-23 15:24:08 -07003868static int l2cap_connect_create_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003869 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3870 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871{
3872 struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
3873 u16 scid, dcid, result, status;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003874 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 u8 req[128];
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003876 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003878 if (cmd_len < sizeof(*rsp))
3879 return -EPROTO;
3880
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 scid = __le16_to_cpu(rsp->scid);
3882 dcid = __le16_to_cpu(rsp->dcid);
3883 result = __le16_to_cpu(rsp->result);
3884 status = __le16_to_cpu(rsp->status);
3885
Andrei Emeltchenko1b009c92012-02-21 12:54:54 +02003886 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 +01003887 dcid, scid, result, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003889 mutex_lock(&conn->chan_lock);
3890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 if (scid) {
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003892 chan = __l2cap_get_chan_by_scid(conn, scid);
3893 if (!chan) {
3894 err = -EFAULT;
3895 goto unlock;
3896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 } else {
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003898 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
3899 if (!chan) {
3900 err = -EFAULT;
3901 goto unlock;
3902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 }
3904
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003905 err = 0;
3906
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003907 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003908
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 switch (result) {
3910 case L2CAP_CR_SUCCESS:
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03003911 l2cap_state_change(chan, BT_CONFIG);
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03003912 chan->ident = 0;
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03003913 chan->dcid = dcid;
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003914 clear_bit(CONF_CONNECT_PEND, &chan->conf_state);
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01003915
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003916 if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state))
Gustavo F. Padovane9aeb2d2010-07-08 20:08:18 -03003917 break;
3918
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003920 l2cap_build_conf_req(chan, req), req);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03003921 chan->num_conf_req++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 break;
3923
3924 case L2CAP_CR_PEND:
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03003925 set_bit(CONF_CONNECT_PEND, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 break;
3927
3928 default:
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003929 l2cap_chan_del(chan, ECONNREFUSED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 break;
3931 }
3932
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02003933 l2cap_chan_unlock(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02003934
3935unlock:
3936 mutex_unlock(&conn->chan_lock);
3937
3938 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939}
3940
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003941static inline void set_default_fcs(struct l2cap_chan *chan)
Mat Martineau8c462b62010-08-24 15:35:42 -07003942{
3943 /* FCS is enabled only in ERTM or streaming mode, if one or both
3944 * sides request it.
3945 */
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03003946 if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING)
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003947 chan->fcs = L2CAP_FCS_NONE;
Andrei Emeltchenkof2592d32012-11-29 17:46:08 +02003948 else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state))
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03003949 chan->fcs = L2CAP_FCS_CRC16;
Mat Martineau8c462b62010-08-24 15:35:42 -07003950}
3951
Andrei Emeltchenko29d8a592012-09-21 12:30:05 +03003952static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data,
3953 u8 ident, u16 flags)
3954{
3955 struct l2cap_conn *conn = chan->conn;
3956
3957 BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident,
3958 flags);
3959
3960 clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state);
3961 set_bit(CONF_OUTPUT_DONE, &chan->conf_state);
3962
3963 l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP,
3964 l2cap_build_conf_rsp(chan, data,
3965 L2CAP_CONF_SUCCESS, flags), data);
3966}
3967
Gustavo Padovan2d792812012-10-06 10:07:01 +01003968static inline int l2cap_config_req(struct l2cap_conn *conn,
3969 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3970 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971{
3972 struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
3973 u16 dcid, flags;
3974 u8 rsp[64];
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003975 struct l2cap_chan *chan;
Mat Martineau3c588192012-04-11 10:48:42 -07003976 int len, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Johan Hedbergcb3b3152013-05-28 13:46:30 +03003978 if (cmd_len < sizeof(*req))
3979 return -EPROTO;
3980
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 dcid = __le16_to_cpu(req->dcid);
3982 flags = __le16_to_cpu(req->flags);
3983
3984 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
3985
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03003986 chan = l2cap_get_chan_by_scid(conn, dcid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03003987 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 return -ENOENT;
3989
David S. Miller033b1142011-07-21 13:38:42 -07003990 if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) {
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003991 struct l2cap_cmd_rej_cid rej;
Gustavo F. Padovandf6bd742010-06-14 02:26:15 -03003992
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03003993 rej.reason = __constant_cpu_to_le16(L2CAP_REJ_INVALID_CID);
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03003994 rej.scid = cpu_to_le16(chan->scid);
3995 rej.dcid = cpu_to_le16(chan->dcid);
3996
Gustavo F. Padovandf6bd742010-06-14 02:26:15 -03003997 l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01003998 sizeof(rej), &rej);
Marcel Holtmann354f60a2006-11-18 22:15:20 +01003999 goto unlock;
Gustavo F. Padovandf6bd742010-06-14 02:26:15 -03004000 }
Marcel Holtmann354f60a2006-11-18 22:15:20 +01004001
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004002 /* Reject if config buffer is too small. */
Al Viro88219a02007-07-29 00:17:25 -07004003 len = cmd_len - sizeof(*req);
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004004 if (chan->conf_len + len > sizeof(chan->conf_req)) {
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004005 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004006 l2cap_build_conf_rsp(chan, rsp,
4007 L2CAP_CONF_REJECT, flags), rsp);
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004008 goto unlock;
4009 }
4010
4011 /* Store config. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004012 memcpy(chan->conf_req + chan->conf_len, req->data, len);
4013 chan->conf_len += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03004015 if (flags & L2CAP_CONF_FLAG_CONTINUATION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 /* Incomplete config. Send empty response. */
4017 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004018 l2cap_build_conf_rsp(chan, rsp,
4019 L2CAP_CONF_SUCCESS, flags), rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 goto unlock;
4021 }
4022
4023 /* Complete config. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004024 len = l2cap_parse_conf_req(chan, rsp);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004025 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004026 l2cap_send_disconn_req(chan, ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 goto unlock;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Mat Martineau1500109b2012-10-23 15:24:15 -07004030 chan->ident = cmd->ident;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004031 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004032 chan->num_conf_rsp++;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004033
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004034 /* Reset config buffer. */
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004035 chan->conf_len = 0;
Marcel Holtmann5dee9e72007-05-24 14:27:19 +02004036
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004037 if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state))
Marcel Holtmann876d9482007-10-20 13:35:42 +02004038 goto unlock;
4039
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004040 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004041 set_default_fcs(chan);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004042
Mat Martineau105bdf92012-04-27 16:50:48 -07004043 if (chan->mode == L2CAP_MODE_ERTM ||
4044 chan->mode == L2CAP_MODE_STREAMING)
Mat Martineau3c588192012-04-11 10:48:42 -07004045 err = l2cap_ertm_init(chan);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03004046
Mat Martineau3c588192012-04-11 10:48:42 -07004047 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004048 l2cap_send_disconn_req(chan, -err);
Mat Martineau3c588192012-04-11 10:48:42 -07004049 else
4050 l2cap_chan_ready(chan);
4051
Marcel Holtmann876d9482007-10-20 13:35:42 +02004052 goto unlock;
4053 }
4054
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004055 if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) {
Marcel Holtmann79d554a2008-07-14 20:13:44 +02004056 u8 buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004058 l2cap_build_conf_req(chan, buf), buf);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004059 chan->num_conf_req++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
4061
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004062 /* Got Conf Rsp PENDING from remote side and asume we sent
4063 Conf Rsp PENDING in the code above */
4064 if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) &&
Andrei Emeltchenko29d8a592012-09-21 12:30:05 +03004065 test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004066
4067 /* check compatibility */
4068
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004069 /* Send rsp for BR/EDR channel */
Andrei Emeltchenkof351bc72012-10-31 15:46:35 +02004070 if (!chan->hs_hcon)
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004071 l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags);
4072 else
4073 chan->ident = cmd->ident;
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004074 }
4075
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076unlock:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004077 l2cap_chan_unlock(chan);
Mat Martineau3c588192012-04-11 10:48:42 -07004078 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079}
4080
Gustavo Padovan2d792812012-10-06 10:07:01 +01004081static inline int l2cap_config_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004082 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4083 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
4085 struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
4086 u16 scid, flags, result;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004087 struct l2cap_chan *chan;
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004088 int len = cmd_len - sizeof(*rsp);
Mat Martineau3c588192012-04-11 10:48:42 -07004089 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004091 if (cmd_len < sizeof(*rsp))
4092 return -EPROTO;
4093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 scid = __le16_to_cpu(rsp->scid);
4095 flags = __le16_to_cpu(rsp->flags);
4096 result = __le16_to_cpu(rsp->result);
4097
Andrei Emeltchenko61386cb2012-03-12 12:13:07 +02004098 BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags,
4099 result, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03004101 chan = l2cap_get_chan_by_scid(conn, scid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004102 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 return 0;
4104
4105 switch (result) {
4106 case L2CAP_CONF_SUCCESS:
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004107 l2cap_conf_rfc_get(chan, rsp->data, len);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004108 clear_bit(CONF_REM_CONF_PEND, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 break;
4110
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004111 case L2CAP_CONF_PENDING:
4112 set_bit(CONF_REM_CONF_PEND, &chan->conf_state);
4113
4114 if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) {
4115 char buf[64];
4116
4117 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004118 buf, &result);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004119 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004120 l2cap_send_disconn_req(chan, ECONNRESET);
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004121 goto done;
4122 }
4123
Andrei Emeltchenkof351bc72012-10-31 15:46:35 +02004124 if (!chan->hs_hcon) {
Andrei Emeltchenko79de8862012-10-15 11:58:42 +03004125 l2cap_send_efs_conf_rsp(chan, buf, cmd->ident,
4126 0);
Andrei Emeltchenko5ce66b52012-10-31 15:46:30 +02004127 } else {
4128 if (l2cap_check_efs(chan)) {
4129 amp_create_logical_link(chan);
4130 chan->ident = cmd->ident;
4131 }
4132 }
Andrei Emeltchenko0e8b2072011-10-17 14:35:32 +03004133 }
4134 goto done;
4135
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 case L2CAP_CONF_UNACCEPT:
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004137 if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004138 char req[64];
4139
Andrei Emeltchenkoc2c77ec2010-03-19 10:26:28 +02004140 if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004141 l2cap_send_disconn_req(chan, ECONNRESET);
Andrei Emeltchenkoc2c77ec2010-03-19 10:26:28 +02004142 goto done;
4143 }
4144
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004145 /* throw out any old stored conf requests */
4146 result = L2CAP_CONF_SUCCESS;
Gustavo F. Padovanb4450032011-04-12 18:15:09 -03004147 len = l2cap_parse_conf_rsp(chan, rsp->data, len,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004148 req, &result);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004149 if (len < 0) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004150 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004151 goto done;
4152 }
4153
4154 l2cap_send_cmd(conn, l2cap_get_ident(conn),
Gustavo Padovan2d792812012-10-06 10:07:01 +01004155 L2CAP_CONF_REQ, len, req);
Gustavo F. Padovan73ffa902011-03-25 14:16:54 -03004156 chan->num_conf_req++;
Gustavo F. Padovanf2fcfcd2009-07-04 15:06:24 -03004157 if (result != L2CAP_CONF_SUCCESS)
4158 goto done;
4159 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 }
4161
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09004162 default:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004163 l2cap_chan_set_err(chan, ECONNRESET);
Andrei Emeltchenko2e0052e2012-02-21 12:54:58 +02004164
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08004165 __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004166 l2cap_send_disconn_req(chan, ECONNRESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 goto done;
4168 }
4169
Andrei Emeltchenko59e54bd2012-05-23 15:44:06 +03004170 if (flags & L2CAP_CONF_FLAG_CONTINUATION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 goto done;
4172
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004173 set_bit(CONF_INPUT_DONE, &chan->conf_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Gustavo F. Padovanc1360a12011-06-10 17:02:12 -03004175 if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) {
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03004176 set_default_fcs(chan);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004177
Mat Martineau105bdf92012-04-27 16:50:48 -07004178 if (chan->mode == L2CAP_MODE_ERTM ||
4179 chan->mode == L2CAP_MODE_STREAMING)
Mat Martineau3c588192012-04-11 10:48:42 -07004180 err = l2cap_ertm_init(chan);
Gustavo F. Padovan0565c1c2009-10-03 02:34:36 -03004181
Mat Martineau3c588192012-04-11 10:48:42 -07004182 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004183 l2cap_send_disconn_req(chan, -err);
Mat Martineau3c588192012-04-11 10:48:42 -07004184 else
4185 l2cap_chan_ready(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 }
4187
4188done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004189 l2cap_chan_unlock(chan);
Mat Martineau3c588192012-04-11 10:48:42 -07004190 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191}
4192
Gustavo Padovan2d792812012-10-06 10:07:01 +01004193static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004194 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4195 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196{
4197 struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
4198 struct l2cap_disconn_rsp rsp;
4199 u16 dcid, scid;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004200 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 struct sock *sk;
4202
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004203 if (cmd_len != sizeof(*req))
4204 return -EPROTO;
4205
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 scid = __le16_to_cpu(req->scid);
4207 dcid = __le16_to_cpu(req->dcid);
4208
4209 BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
4210
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004211 mutex_lock(&conn->chan_lock);
4212
4213 chan = __l2cap_get_chan_by_scid(conn, dcid);
4214 if (!chan) {
4215 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 return 0;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004219 l2cap_chan_lock(chan);
4220
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004221 sk = chan->sk;
4222
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03004223 rsp.dcid = cpu_to_le16(chan->scid);
4224 rsp.scid = cpu_to_le16(chan->dcid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
4226
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004227 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 sk->sk_shutdown = SHUTDOWN_MASK;
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004229 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
Mat Martineau61d6ef32012-04-27 16:50:50 -07004231 l2cap_chan_hold(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004232 l2cap_chan_del(chan, ECONNRESET);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004233
4234 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
Gustavo Padovan80b98022012-05-27 22:27:51 -03004236 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07004237 l2cap_chan_put(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004238
4239 mutex_unlock(&conn->chan_lock);
4240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return 0;
4242}
4243
Gustavo Padovan2d792812012-10-06 10:07:01 +01004244static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004245 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4246 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247{
4248 struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
4249 u16 dcid, scid;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004250 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004252 if (cmd_len != sizeof(*rsp))
4253 return -EPROTO;
4254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 scid = __le16_to_cpu(rsp->scid);
4256 dcid = __le16_to_cpu(rsp->dcid);
4257
4258 BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
4259
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004260 mutex_lock(&conn->chan_lock);
4261
4262 chan = __l2cap_get_chan_by_scid(conn, scid);
4263 if (!chan) {
4264 mutex_unlock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 return 0;
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004268 l2cap_chan_lock(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004269
Mat Martineau61d6ef32012-04-27 16:50:50 -07004270 l2cap_chan_hold(chan);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03004271 l2cap_chan_del(chan, 0);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02004272
4273 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Gustavo Padovan80b98022012-05-27 22:27:51 -03004275 chan->ops->close(chan);
Mat Martineau61d6ef32012-04-27 16:50:50 -07004276 l2cap_chan_put(chan);
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02004277
4278 mutex_unlock(&conn->chan_lock);
4279
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 return 0;
4281}
4282
Gustavo Padovan2d792812012-10-06 10:07:01 +01004283static inline int l2cap_information_req(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004284 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4285 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
4287 struct l2cap_info_req *req = (struct l2cap_info_req *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 u16 type;
4289
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004290 if (cmd_len != sizeof(*req))
4291 return -EPROTO;
4292
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 type = __le16_to_cpu(req->type);
4294
4295 BT_DBG("type 0x%4.4x", type);
4296
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004297 if (type == L2CAP_IT_FEAT_MASK) {
4298 u8 buf[8];
Marcel Holtmann44dd46d2009-05-02 19:09:01 -07004299 u32 feat_mask = l2cap_feat_mask;
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004300 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03004301 rsp->type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK);
4302 rsp->result = __constant_cpu_to_le16(L2CAP_IR_SUCCESS);
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03004303 if (!disable_ertm)
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03004304 feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
Gustavo Padovan2d792812012-10-06 10:07:01 +01004305 | L2CAP_FEAT_FCS;
Andrei Emeltchenkoa5fd6f32011-09-16 16:26:32 +03004306 if (enable_hs)
Andrei Emeltchenko6327eb92011-10-11 13:37:42 +03004307 feat_mask |= L2CAP_FEAT_EXT_FLOW
Gustavo Padovan2d792812012-10-06 10:07:01 +01004308 | L2CAP_FEAT_EXT_WINDOW;
Andrei Emeltchenkoa5fd6f32011-09-16 16:26:32 +03004309
Gustavo F. Padovan1b7bf4e2009-08-24 00:45:20 -03004310 put_unaligned_le32(feat_mask, rsp->data);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004311 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4312 buf);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004313 } else if (type == L2CAP_IT_FIXED_CHAN) {
4314 u8 buf[12];
4315 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
Mat Martineau50a147c2011-11-02 16:18:34 -07004316
4317 if (enable_hs)
4318 l2cap_fixed_chan[0] |= L2CAP_FC_A2MP;
4319 else
4320 l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP;
4321
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03004322 rsp->type = __constant_cpu_to_le16(L2CAP_IT_FIXED_CHAN);
4323 rsp->result = __constant_cpu_to_le16(L2CAP_IR_SUCCESS);
Andrei Emeltchenkoc6337ea2011-10-20 17:02:44 +03004324 memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan));
Gustavo Padovan2d792812012-10-06 10:07:01 +01004325 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf),
4326 buf);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004327 } else {
4328 struct l2cap_info_rsp rsp;
4329 rsp.type = cpu_to_le16(type);
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03004330 rsp.result = __constant_cpu_to_le16(L2CAP_IR_NOTSUPP);
Gustavo Padovan2d792812012-10-06 10:07:01 +01004331 l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp),
4332 &rsp);
Marcel Holtmannf0709e02007-10-20 13:38:51 +02004333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
4335 return 0;
4336}
4337
Gustavo Padovan2d792812012-10-06 10:07:01 +01004338static inline int l2cap_information_rsp(struct l2cap_conn *conn,
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004339 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
4340 u8 *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341{
4342 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
4343 u16 type, result;
4344
Jaganath Kanakkassery3f6fa3d2013-06-21 19:55:11 +05304345 if (cmd_len < sizeof(*rsp))
Johan Hedbergcb3b3152013-05-28 13:46:30 +03004346 return -EPROTO;
4347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 type = __le16_to_cpu(rsp->type);
4349 result = __le16_to_cpu(rsp->result);
4350
4351 BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
4352
Andrei Emeltchenkoe90165b2011-03-25 11:31:41 +02004353 /* L2CAP Info req/rsp are unbound to channels, add extra checks */
4354 if (cmd->ident != conn->info_ident ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01004355 conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
Andrei Emeltchenkoe90165b2011-03-25 11:31:41 +02004356 return 0;
4357
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02004358 cancel_delayed_work(&conn->info_timer);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004359
Ville Tervoadb08ed2010-08-04 09:43:33 +03004360 if (result != L2CAP_IR_SUCCESS) {
4361 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4362 conn->info_ident = 0;
4363
4364 l2cap_conn_start(conn);
4365
4366 return 0;
4367 }
4368
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004369 switch (type) {
4370 case L2CAP_IT_FEAT_MASK:
Harvey Harrison83985312008-05-02 16:25:46 -07004371 conn->feat_mask = get_unaligned_le32(rsp->data);
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004372
Marcel Holtmann47ec1dcd2009-05-02 18:57:55 -07004373 if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004374 struct l2cap_info_req req;
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03004375 req.type = __constant_cpu_to_le16(L2CAP_IT_FIXED_CHAN);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004376
4377 conn->info_ident = l2cap_get_ident(conn);
4378
4379 l2cap_send_cmd(conn, conn->info_ident,
Gustavo Padovan2d792812012-10-06 10:07:01 +01004380 L2CAP_INFO_REQ, sizeof(req), &req);
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004381 } else {
4382 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
4383 conn->info_ident = 0;
4384
4385 l2cap_conn_start(conn);
4386 }
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004387 break;
4388
4389 case L2CAP_IT_FIXED_CHAN:
4390 conn->fixed_chan_mask = rsp->data[0];
Marcel Holtmann984947d2009-02-06 23:35:19 +01004391 conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
Marcel Holtmanne1027a72009-02-09 09:18:02 +01004392 conn->info_ident = 0;
Marcel Holtmann984947d2009-02-06 23:35:19 +01004393
4394 l2cap_conn_start(conn);
Andrei Emeltchenko978c93b2012-02-29 10:41:41 +02004395 break;
Marcel Holtmann984947d2009-02-06 23:35:19 +01004396 }
Marcel Holtmann4e8402a2007-10-20 13:37:56 +02004397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return 0;
4399}
4400
Mat Martineau17009152012-10-23 15:24:07 -07004401static int l2cap_create_channel_req(struct l2cap_conn *conn,
4402 struct l2cap_cmd_hdr *cmd,
4403 u16 cmd_len, void *data)
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004404{
4405 struct l2cap_create_chan_req *req = data;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004406 struct l2cap_create_chan_rsp rsp;
Mat Martineau17009152012-10-23 15:24:07 -07004407 struct l2cap_chan *chan;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004408 struct hci_dev *hdev;
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004409 u16 psm, scid;
4410
4411 if (cmd_len != sizeof(*req))
4412 return -EPROTO;
4413
4414 if (!enable_hs)
4415 return -EINVAL;
4416
4417 psm = le16_to_cpu(req->psm);
4418 scid = le16_to_cpu(req->scid);
4419
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004420 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 -07004421
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004422 /* For controller id 0 make BR/EDR connection */
4423 if (req->amp_id == HCI_BREDR_ID) {
4424 l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4425 req->amp_id);
4426 return 0;
4427 }
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004428
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004429 /* Validate AMP controller id */
4430 hdev = hci_dev_get(req->amp_id);
4431 if (!hdev)
4432 goto error;
Mat Martineau17009152012-10-23 15:24:07 -07004433
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004434 if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) {
Mat Martineau17009152012-10-23 15:24:07 -07004435 hci_dev_put(hdev);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004436 goto error;
Mat Martineau17009152012-10-23 15:24:07 -07004437 }
4438
4439 chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4440 req->amp_id);
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004441 if (chan) {
4442 struct amp_mgr *mgr = conn->hcon->amp_mgr;
4443 struct hci_conn *hs_hcon;
4444
4445 hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, conn->dst);
4446 if (!hs_hcon) {
4447 hci_dev_put(hdev);
4448 return -EFAULT;
4449 }
4450
4451 BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon);
4452
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004453 mgr->bredr_chan = chan;
4454 chan->hs_hcon = hs_hcon;
Andrei Emeltchenkofd45bf42012-11-20 17:16:22 +02004455 chan->fcs = L2CAP_FCS_NONE;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004456 conn->mtu = hdev->block_mtu;
4457 }
4458
4459 hci_dev_put(hdev);
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004460
4461 return 0;
Andrei Emeltchenko6e1df6a2012-11-01 15:37:02 +02004462
4463error:
4464 rsp.dcid = 0;
4465 rsp.scid = cpu_to_le16(scid);
4466 rsp.result = __constant_cpu_to_le16(L2CAP_CR_BAD_AMP);
4467 rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO);
4468
4469 l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
4470 sizeof(rsp), &rsp);
4471
4472 return -EFAULT;
Mat Martineauf94ff6f2011-11-02 16:18:32 -07004473}
4474
Mat Martineau8eb200b2012-10-23 15:24:17 -07004475static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id)
4476{
4477 struct l2cap_move_chan_req req;
4478 u8 ident;
4479
4480 BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id);
4481
4482 ident = l2cap_get_ident(chan->conn);
4483 chan->ident = ident;
4484
4485 req.icid = cpu_to_le16(chan->scid);
4486 req.dest_amp_id = dest_amp_id;
4487
4488 l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req),
4489 &req);
4490
4491 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4492}
4493
Mat Martineau1500109b2012-10-23 15:24:15 -07004494static void l2cap_send_move_chan_rsp(struct l2cap_chan *chan, u16 result)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004495{
4496 struct l2cap_move_chan_rsp rsp;
4497
Mat Martineau1500109b2012-10-23 15:24:15 -07004498 BT_DBG("chan %p, result 0x%4.4x", chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004499
Mat Martineau1500109b2012-10-23 15:24:15 -07004500 rsp.icid = cpu_to_le16(chan->dcid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004501 rsp.result = cpu_to_le16(result);
4502
Mat Martineau1500109b2012-10-23 15:24:15 -07004503 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP,
4504 sizeof(rsp), &rsp);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004505}
4506
Mat Martineau5b155ef2012-10-23 15:24:14 -07004507static void l2cap_send_move_chan_cfm(struct l2cap_chan *chan, u16 result)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004508{
4509 struct l2cap_move_chan_cfm cfm;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004510
Mat Martineau5b155ef2012-10-23 15:24:14 -07004511 BT_DBG("chan %p, result 0x%4.4x", chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004512
Mat Martineau5b155ef2012-10-23 15:24:14 -07004513 chan->ident = l2cap_get_ident(chan->conn);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004514
Mat Martineau5b155ef2012-10-23 15:24:14 -07004515 cfm.icid = cpu_to_le16(chan->scid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004516 cfm.result = cpu_to_le16(result);
4517
Mat Martineau5b155ef2012-10-23 15:24:14 -07004518 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM,
4519 sizeof(cfm), &cfm);
4520
4521 __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT);
4522}
4523
4524static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid)
4525{
4526 struct l2cap_move_chan_cfm cfm;
4527
4528 BT_DBG("conn %p, icid 0x%4.4x", conn, icid);
4529
4530 cfm.icid = cpu_to_le16(icid);
4531 cfm.result = __constant_cpu_to_le16(L2CAP_MC_UNCONFIRMED);
4532
4533 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM,
4534 sizeof(cfm), &cfm);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004535}
4536
4537static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004538 u16 icid)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004539{
4540 struct l2cap_move_chan_cfm_rsp rsp;
4541
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004542 BT_DBG("icid 0x%4.4x", icid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004543
4544 rsp.icid = cpu_to_le16(icid);
4545 l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp);
4546}
4547
Mat Martineau5f3847a2012-10-23 15:24:12 -07004548static void __release_logical_link(struct l2cap_chan *chan)
4549{
4550 chan->hs_hchan = NULL;
4551 chan->hs_hcon = NULL;
4552
4553 /* Placeholder - release the logical link */
4554}
4555
Mat Martineau1500109b2012-10-23 15:24:15 -07004556static void l2cap_logical_fail(struct l2cap_chan *chan)
4557{
4558 /* Logical link setup failed */
4559 if (chan->state != BT_CONNECTED) {
4560 /* Create channel failure, disconnect */
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004561 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineau1500109b2012-10-23 15:24:15 -07004562 return;
4563 }
4564
4565 switch (chan->move_role) {
4566 case L2CAP_MOVE_ROLE_RESPONDER:
4567 l2cap_move_done(chan);
4568 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP);
4569 break;
4570 case L2CAP_MOVE_ROLE_INITIATOR:
4571 if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP ||
4572 chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) {
4573 /* Remote has only sent pending or
4574 * success responses, clean up
4575 */
4576 l2cap_move_done(chan);
4577 }
4578
4579 /* Other amp move states imply that the move
4580 * has already aborted
4581 */
4582 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
4583 break;
4584 }
4585}
4586
4587static void l2cap_logical_finish_create(struct l2cap_chan *chan,
4588 struct hci_chan *hchan)
4589{
4590 struct l2cap_conf_rsp rsp;
Mat Martineau1500109b2012-10-23 15:24:15 -07004591
Andrei Emeltchenko336178a2012-10-31 15:46:27 +02004592 chan->hs_hchan = hchan;
Mat Martineau1500109b2012-10-23 15:24:15 -07004593 chan->hs_hcon->l2cap_data = chan->conn;
4594
Andrei Emeltchenko35ba9562012-10-25 15:20:43 +03004595 l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0);
Mat Martineau1500109b2012-10-23 15:24:15 -07004596
4597 if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) {
Andrei Emeltchenkofe79c6f2012-10-25 15:20:42 +03004598 int err;
Mat Martineau1500109b2012-10-23 15:24:15 -07004599
4600 set_default_fcs(chan);
4601
4602 err = l2cap_ertm_init(chan);
4603 if (err < 0)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02004604 l2cap_send_disconn_req(chan, -err);
Mat Martineau1500109b2012-10-23 15:24:15 -07004605 else
4606 l2cap_chan_ready(chan);
4607 }
4608}
4609
4610static void l2cap_logical_finish_move(struct l2cap_chan *chan,
4611 struct hci_chan *hchan)
4612{
4613 chan->hs_hcon = hchan->conn;
4614 chan->hs_hcon->l2cap_data = chan->conn;
4615
4616 BT_DBG("move_state %d", chan->move_state);
4617
4618 switch (chan->move_state) {
4619 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
4620 /* Move confirm will be sent after a success
4621 * response is received
4622 */
4623 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4624 break;
4625 case L2CAP_MOVE_WAIT_LOGICAL_CFM:
4626 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4627 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4628 } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
4629 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
4630 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
4631 } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
4632 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4633 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
4634 }
4635 break;
4636 default:
4637 /* Move was not in expected state, free the channel */
4638 __release_logical_link(chan);
4639
4640 chan->move_state = L2CAP_MOVE_STABLE;
4641 }
4642}
4643
4644/* Call with chan locked */
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03004645void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan,
4646 u8 status)
Mat Martineau5b155ef2012-10-23 15:24:14 -07004647{
Mat Martineau1500109b2012-10-23 15:24:15 -07004648 BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status);
4649
4650 if (status) {
4651 l2cap_logical_fail(chan);
4652 __release_logical_link(chan);
4653 return;
4654 }
4655
4656 if (chan->state != BT_CONNECTED) {
4657 /* Ignore logical link if channel is on BR/EDR */
4658 if (chan->local_amp_id)
4659 l2cap_logical_finish_create(chan, hchan);
4660 } else {
4661 l2cap_logical_finish_move(chan, hchan);
4662 }
Mat Martineau5b155ef2012-10-23 15:24:14 -07004663}
4664
Mat Martineau3f7a56c2012-10-23 15:24:23 -07004665void l2cap_move_start(struct l2cap_chan *chan)
4666{
4667 BT_DBG("chan %p", chan);
4668
4669 if (chan->local_amp_id == HCI_BREDR_ID) {
4670 if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED)
4671 return;
4672 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
4673 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
4674 /* Placeholder - start physical link setup */
4675 } else {
4676 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
4677 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4678 chan->move_id = 0;
4679 l2cap_move_setup(chan);
4680 l2cap_send_move_chan_req(chan, 0);
4681 }
4682}
4683
Mat Martineau8eb200b2012-10-23 15:24:17 -07004684static void l2cap_do_create(struct l2cap_chan *chan, int result,
4685 u8 local_amp_id, u8 remote_amp_id)
4686{
Andrei Emeltchenko62748ca2012-11-20 17:16:19 +02004687 BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state),
4688 local_amp_id, remote_amp_id);
4689
Andrei Emeltchenko12d6cc62012-11-14 17:39:31 +02004690 chan->fcs = L2CAP_FCS_NONE;
4691
Andrei Emeltchenko62748ca2012-11-20 17:16:19 +02004692 /* Outgoing channel on AMP */
4693 if (chan->state == BT_CONNECT) {
4694 if (result == L2CAP_CR_SUCCESS) {
4695 chan->local_amp_id = local_amp_id;
4696 l2cap_send_create_chan_req(chan, remote_amp_id);
4697 } else {
4698 /* Revert to BR/EDR connect */
4699 l2cap_send_conn_req(chan);
4700 }
4701
4702 return;
4703 }
4704
4705 /* Incoming channel on AMP */
4706 if (__l2cap_no_conn_pending(chan)) {
Mat Martineau8eb200b2012-10-23 15:24:17 -07004707 struct l2cap_conn_rsp rsp;
4708 char buf[128];
4709 rsp.scid = cpu_to_le16(chan->dcid);
4710 rsp.dcid = cpu_to_le16(chan->scid);
4711
Mat Martineau8eb200b2012-10-23 15:24:17 -07004712 if (result == L2CAP_CR_SUCCESS) {
4713 /* Send successful response */
Andrei Emeltchenko62cd50e2012-11-14 17:39:32 +02004714 rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS);
4715 rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004716 } else {
4717 /* Send negative response */
Andrei Emeltchenko62cd50e2012-11-14 17:39:32 +02004718 rsp.result = __constant_cpu_to_le16(L2CAP_CR_NO_MEM);
4719 rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO);
Mat Martineau8eb200b2012-10-23 15:24:17 -07004720 }
4721
4722 l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP,
4723 sizeof(rsp), &rsp);
4724
4725 if (result == L2CAP_CR_SUCCESS) {
4726 __l2cap_state_change(chan, BT_CONFIG);
4727 set_bit(CONF_REQ_SENT, &chan->conf_state);
4728 l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn),
4729 L2CAP_CONF_REQ,
4730 l2cap_build_conf_req(chan, buf), buf);
4731 chan->num_conf_req++;
4732 }
Mat Martineau8eb200b2012-10-23 15:24:17 -07004733 }
4734}
4735
4736static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id,
4737 u8 remote_amp_id)
4738{
4739 l2cap_move_setup(chan);
4740 chan->move_id = local_amp_id;
4741 chan->move_state = L2CAP_MOVE_WAIT_RSP;
4742
4743 l2cap_send_move_chan_req(chan, remote_amp_id);
4744}
4745
4746static void l2cap_do_move_respond(struct l2cap_chan *chan, int result)
4747{
4748 struct hci_chan *hchan = NULL;
4749
4750 /* Placeholder - get hci_chan for logical link */
4751
4752 if (hchan) {
4753 if (hchan->state == BT_CONNECTED) {
4754 /* Logical link is ready to go */
4755 chan->hs_hcon = hchan->conn;
4756 chan->hs_hcon->l2cap_data = chan->conn;
4757 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4758 l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS);
4759
4760 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
4761 } else {
4762 /* Wait for logical link to be ready */
4763 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
4764 }
4765 } else {
4766 /* Logical link not available */
4767 l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED);
4768 }
4769}
4770
4771static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result)
4772{
4773 if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) {
4774 u8 rsp_result;
4775 if (result == -EINVAL)
4776 rsp_result = L2CAP_MR_BAD_ID;
4777 else
4778 rsp_result = L2CAP_MR_NOT_ALLOWED;
4779
4780 l2cap_send_move_chan_rsp(chan, rsp_result);
4781 }
4782
4783 chan->move_role = L2CAP_MOVE_ROLE_NONE;
4784 chan->move_state = L2CAP_MOVE_STABLE;
4785
4786 /* Restart data transmission */
4787 l2cap_ertm_send(chan);
4788}
4789
Andrei Emeltchenkoa514b172012-11-14 17:39:30 +02004790/* Invoke with locked chan */
4791void __l2cap_physical_cfm(struct l2cap_chan *chan, int result)
Mat Martineau8eb200b2012-10-23 15:24:17 -07004792{
Andrei Emeltchenko770bfef2012-10-31 15:46:29 +02004793 u8 local_amp_id = chan->local_amp_id;
Andrei Emeltchenkofffadc02012-11-01 15:37:03 +02004794 u8 remote_amp_id = chan->remote_amp_id;
Andrei Emeltchenko770bfef2012-10-31 15:46:29 +02004795
Mat Martineau8eb200b2012-10-23 15:24:17 -07004796 BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d",
4797 chan, result, local_amp_id, remote_amp_id);
4798
Mat Martineau8eb200b2012-10-23 15:24:17 -07004799 if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) {
4800 l2cap_chan_unlock(chan);
4801 return;
4802 }
4803
4804 if (chan->state != BT_CONNECTED) {
4805 l2cap_do_create(chan, result, local_amp_id, remote_amp_id);
4806 } else if (result != L2CAP_MR_SUCCESS) {
4807 l2cap_do_move_cancel(chan, result);
4808 } else {
4809 switch (chan->move_role) {
4810 case L2CAP_MOVE_ROLE_INITIATOR:
4811 l2cap_do_move_initiate(chan, local_amp_id,
4812 remote_amp_id);
4813 break;
4814 case L2CAP_MOVE_ROLE_RESPONDER:
4815 l2cap_do_move_respond(chan, result);
4816 break;
4817 default:
4818 l2cap_do_move_cancel(chan, result);
4819 break;
4820 }
4821 }
Mat Martineau8eb200b2012-10-23 15:24:17 -07004822}
4823
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004824static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004825 struct l2cap_cmd_hdr *cmd,
4826 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004827{
4828 struct l2cap_move_chan_req *req = data;
Mat Martineau1500109b2012-10-23 15:24:15 -07004829 struct l2cap_move_chan_rsp rsp;
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004830 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004831 u16 icid = 0;
4832 u16 result = L2CAP_MR_NOT_ALLOWED;
4833
4834 if (cmd_len != sizeof(*req))
4835 return -EPROTO;
4836
4837 icid = le16_to_cpu(req->icid);
4838
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03004839 BT_DBG("icid 0x%4.4x, dest_amp_id %d", icid, req->dest_amp_id);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004840
4841 if (!enable_hs)
4842 return -EINVAL;
4843
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004844 chan = l2cap_get_chan_by_dcid(conn, icid);
4845 if (!chan) {
Mat Martineau1500109b2012-10-23 15:24:15 -07004846 rsp.icid = cpu_to_le16(icid);
4847 rsp.result = __constant_cpu_to_le16(L2CAP_MR_NOT_ALLOWED);
4848 l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP,
4849 sizeof(rsp), &rsp);
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004850 return 0;
4851 }
4852
Mat Martineau1500109b2012-10-23 15:24:15 -07004853 chan->ident = cmd->ident;
4854
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004855 if (chan->scid < L2CAP_CID_DYN_START ||
4856 chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY ||
4857 (chan->mode != L2CAP_MODE_ERTM &&
4858 chan->mode != L2CAP_MODE_STREAMING)) {
4859 result = L2CAP_MR_NOT_ALLOWED;
4860 goto send_move_response;
4861 }
4862
4863 if (chan->local_amp_id == req->dest_amp_id) {
4864 result = L2CAP_MR_SAME_ID;
4865 goto send_move_response;
4866 }
4867
4868 if (req->dest_amp_id) {
4869 struct hci_dev *hdev;
4870 hdev = hci_dev_get(req->dest_amp_id);
4871 if (!hdev || hdev->dev_type != HCI_AMP ||
4872 !test_bit(HCI_UP, &hdev->flags)) {
4873 if (hdev)
4874 hci_dev_put(hdev);
4875
4876 result = L2CAP_MR_BAD_ID;
4877 goto send_move_response;
4878 }
4879 hci_dev_put(hdev);
4880 }
4881
4882 /* Detect a move collision. Only send a collision response
4883 * if this side has "lost", otherwise proceed with the move.
4884 * The winner has the larger bd_addr.
4885 */
4886 if ((__chan_is_moving(chan) ||
4887 chan->move_role != L2CAP_MOVE_ROLE_NONE) &&
4888 bacmp(conn->src, conn->dst) > 0) {
4889 result = L2CAP_MR_COLLISION;
4890 goto send_move_response;
4891 }
4892
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004893 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
4894 l2cap_move_setup(chan);
4895 chan->move_id = req->dest_amp_id;
4896 icid = chan->dcid;
4897
4898 if (!req->dest_amp_id) {
4899 /* Moving to BR/EDR */
4900 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4901 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4902 result = L2CAP_MR_PEND;
4903 } else {
4904 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM;
4905 result = L2CAP_MR_SUCCESS;
4906 }
4907 } else {
4908 chan->move_state = L2CAP_MOVE_WAIT_PREPARE;
4909 /* Placeholder - uncomment when amp functions are available */
4910 /*amp_accept_physical(chan, req->dest_amp_id);*/
4911 result = L2CAP_MR_PEND;
4912 }
4913
4914send_move_response:
Mat Martineau1500109b2012-10-23 15:24:15 -07004915 l2cap_send_move_chan_rsp(chan, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004916
Mat Martineau02b0fbb2012-10-23 15:24:10 -07004917 l2cap_chan_unlock(chan);
4918
Mat Martineau8d5a04a2011-11-02 16:18:35 -07004919 return 0;
4920}
4921
Mat Martineau5b155ef2012-10-23 15:24:14 -07004922static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result)
4923{
4924 struct l2cap_chan *chan;
4925 struct hci_chan *hchan = NULL;
4926
4927 chan = l2cap_get_chan_by_scid(conn, icid);
4928 if (!chan) {
4929 l2cap_send_move_chan_cfm_icid(conn, icid);
4930 return;
4931 }
4932
4933 __clear_chan_timer(chan);
4934 if (result == L2CAP_MR_PEND)
4935 __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT);
4936
4937 switch (chan->move_state) {
4938 case L2CAP_MOVE_WAIT_LOGICAL_COMP:
4939 /* Move confirm will be sent when logical link
4940 * is complete.
4941 */
4942 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
4943 break;
4944 case L2CAP_MOVE_WAIT_RSP_SUCCESS:
4945 if (result == L2CAP_MR_PEND) {
4946 break;
4947 } else if (test_bit(CONN_LOCAL_BUSY,
4948 &chan->conn_state)) {
4949 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
4950 } else {
4951 /* Logical link is up or moving to BR/EDR,
4952 * proceed with move
4953 */
4954 chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP;
4955 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
4956 }
4957 break;
4958 case L2CAP_MOVE_WAIT_RSP:
4959 /* Moving to AMP */
4960 if (result == L2CAP_MR_SUCCESS) {
4961 /* Remote is ready, send confirm immediately
4962 * after logical link is ready
4963 */
4964 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM;
4965 } else {
4966 /* Both logical link and move success
4967 * are required to confirm
4968 */
4969 chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP;
4970 }
4971
4972 /* Placeholder - get hci_chan for logical link */
4973 if (!hchan) {
4974 /* Logical link not available */
4975 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
4976 break;
4977 }
4978
4979 /* If the logical link is not yet connected, do not
4980 * send confirmation.
4981 */
4982 if (hchan->state != BT_CONNECTED)
4983 break;
4984
4985 /* Logical link is already ready to go */
4986
4987 chan->hs_hcon = hchan->conn;
4988 chan->hs_hcon->l2cap_data = chan->conn;
4989
4990 if (result == L2CAP_MR_SUCCESS) {
4991 /* Can confirm now */
4992 l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED);
4993 } else {
4994 /* Now only need move success
4995 * to confirm
4996 */
4997 chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS;
4998 }
4999
5000 l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS);
5001 break;
5002 default:
5003 /* Any other amp move state means the move failed. */
5004 chan->move_id = chan->local_amp_id;
5005 l2cap_move_done(chan);
5006 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5007 }
5008
5009 l2cap_chan_unlock(chan);
5010}
5011
5012static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid,
5013 u16 result)
5014{
5015 struct l2cap_chan *chan;
5016
5017 chan = l2cap_get_chan_by_ident(conn, ident);
5018 if (!chan) {
5019 /* Could not locate channel, icid is best guess */
5020 l2cap_send_move_chan_cfm_icid(conn, icid);
5021 return;
5022 }
5023
5024 __clear_chan_timer(chan);
5025
5026 if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) {
5027 if (result == L2CAP_MR_COLLISION) {
5028 chan->move_role = L2CAP_MOVE_ROLE_RESPONDER;
5029 } else {
5030 /* Cleanup - cancel move */
5031 chan->move_id = chan->local_amp_id;
5032 l2cap_move_done(chan);
5033 }
5034 }
5035
5036 l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED);
5037
5038 l2cap_chan_unlock(chan);
5039}
5040
5041static int l2cap_move_channel_rsp(struct l2cap_conn *conn,
5042 struct l2cap_cmd_hdr *cmd,
5043 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005044{
5045 struct l2cap_move_chan_rsp *rsp = data;
5046 u16 icid, result;
5047
5048 if (cmd_len != sizeof(*rsp))
5049 return -EPROTO;
5050
5051 icid = le16_to_cpu(rsp->icid);
5052 result = le16_to_cpu(rsp->result);
5053
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005054 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005055
Mat Martineau5b155ef2012-10-23 15:24:14 -07005056 if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND)
5057 l2cap_move_continue(conn, icid, result);
5058 else
5059 l2cap_move_fail(conn, cmd->ident, icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005060
5061 return 0;
5062}
5063
Mat Martineau5f3847a2012-10-23 15:24:12 -07005064static int l2cap_move_channel_confirm(struct l2cap_conn *conn,
5065 struct l2cap_cmd_hdr *cmd,
5066 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005067{
5068 struct l2cap_move_chan_cfm *cfm = data;
Mat Martineau5f3847a2012-10-23 15:24:12 -07005069 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005070 u16 icid, result;
5071
5072 if (cmd_len != sizeof(*cfm))
5073 return -EPROTO;
5074
5075 icid = le16_to_cpu(cfm->icid);
5076 result = le16_to_cpu(cfm->result);
5077
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005078 BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005079
Mat Martineau5f3847a2012-10-23 15:24:12 -07005080 chan = l2cap_get_chan_by_dcid(conn, icid);
5081 if (!chan) {
5082 /* Spec requires a response even if the icid was not found */
5083 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5084 return 0;
5085 }
5086
5087 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) {
5088 if (result == L2CAP_MC_CONFIRMED) {
5089 chan->local_amp_id = chan->move_id;
5090 if (!chan->local_amp_id)
5091 __release_logical_link(chan);
5092 } else {
5093 chan->move_id = chan->local_amp_id;
5094 }
5095
5096 l2cap_move_done(chan);
5097 }
5098
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005099 l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
5100
Mat Martineau5f3847a2012-10-23 15:24:12 -07005101 l2cap_chan_unlock(chan);
5102
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005103 return 0;
5104}
5105
5106static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005107 struct l2cap_cmd_hdr *cmd,
5108 u16 cmd_len, void *data)
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005109{
5110 struct l2cap_move_chan_cfm_rsp *rsp = data;
Mat Martineau3fd71a02012-10-23 15:24:16 -07005111 struct l2cap_chan *chan;
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005112 u16 icid;
5113
5114 if (cmd_len != sizeof(*rsp))
5115 return -EPROTO;
5116
5117 icid = le16_to_cpu(rsp->icid);
5118
Andrei Emeltchenkoad0ac6c2012-07-10 15:27:50 +03005119 BT_DBG("icid 0x%4.4x", icid);
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005120
Mat Martineau3fd71a02012-10-23 15:24:16 -07005121 chan = l2cap_get_chan_by_scid(conn, icid);
5122 if (!chan)
5123 return 0;
5124
5125 __clear_chan_timer(chan);
5126
5127 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) {
5128 chan->local_amp_id = chan->move_id;
5129
5130 if (!chan->local_amp_id && chan->hs_hchan)
5131 __release_logical_link(chan);
5132
5133 l2cap_move_done(chan);
5134 }
5135
5136 l2cap_chan_unlock(chan);
5137
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005138 return 0;
5139}
5140
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03005141static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005142 u16 to_multiplier)
Claudio Takahaside731152011-02-11 19:28:55 -02005143{
5144 u16 max_latency;
5145
5146 if (min > max || min < 6 || max > 3200)
5147 return -EINVAL;
5148
5149 if (to_multiplier < 10 || to_multiplier > 3200)
5150 return -EINVAL;
5151
5152 if (max >= to_multiplier * 8)
5153 return -EINVAL;
5154
5155 max_latency = (to_multiplier * 8 / max) - 1;
5156 if (latency > 499 || latency > max_latency)
5157 return -EINVAL;
5158
5159 return 0;
5160}
5161
5162static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005163 struct l2cap_cmd_hdr *cmd,
5164 u8 *data)
Claudio Takahaside731152011-02-11 19:28:55 -02005165{
5166 struct hci_conn *hcon = conn->hcon;
5167 struct l2cap_conn_param_update_req *req;
5168 struct l2cap_conn_param_update_rsp rsp;
5169 u16 min, max, latency, to_multiplier, cmd_len;
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005170 int err;
Claudio Takahaside731152011-02-11 19:28:55 -02005171
5172 if (!(hcon->link_mode & HCI_LM_MASTER))
5173 return -EINVAL;
5174
5175 cmd_len = __le16_to_cpu(cmd->len);
5176 if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
5177 return -EPROTO;
5178
5179 req = (struct l2cap_conn_param_update_req *) data;
Gustavo F. Padovane2174ca2011-02-17 19:16:55 -03005180 min = __le16_to_cpu(req->min);
5181 max = __le16_to_cpu(req->max);
Claudio Takahaside731152011-02-11 19:28:55 -02005182 latency = __le16_to_cpu(req->latency);
5183 to_multiplier = __le16_to_cpu(req->to_multiplier);
5184
5185 BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x",
Gustavo Padovan2d792812012-10-06 10:07:01 +01005186 min, max, latency, to_multiplier);
Claudio Takahaside731152011-02-11 19:28:55 -02005187
5188 memset(&rsp, 0, sizeof(rsp));
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005189
5190 err = l2cap_check_conn_param(min, max, latency, to_multiplier);
5191 if (err)
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03005192 rsp.result = __constant_cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
Claudio Takahaside731152011-02-11 19:28:55 -02005193 else
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03005194 rsp.result = __constant_cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
Claudio Takahaside731152011-02-11 19:28:55 -02005195
5196 l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005197 sizeof(rsp), &rsp);
Claudio Takahaside731152011-02-11 19:28:55 -02005198
Claudio Takahasi2ce603e2011-02-16 20:44:53 -02005199 if (!err)
5200 hci_le_conn_update(hcon, min, max, latency, to_multiplier);
5201
Claudio Takahaside731152011-02-11 19:28:55 -02005202 return 0;
5203}
5204
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005205static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005206 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
5207 u8 *data)
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005208{
5209 int err = 0;
5210
5211 switch (cmd->code) {
5212 case L2CAP_COMMAND_REJ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005213 l2cap_command_rej(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005214 break;
5215
5216 case L2CAP_CONN_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005217 err = l2cap_connect_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005218 break;
5219
5220 case L2CAP_CONN_RSP:
Mat Martineauf5a25982012-10-11 17:48:21 +03005221 case L2CAP_CREATE_CHAN_RSP:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005222 err = l2cap_connect_create_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005223 break;
5224
5225 case L2CAP_CONF_REQ:
5226 err = l2cap_config_req(conn, cmd, cmd_len, data);
5227 break;
5228
5229 case L2CAP_CONF_RSP:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005230 err = l2cap_config_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005231 break;
5232
5233 case L2CAP_DISCONN_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005234 err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005235 break;
5236
5237 case L2CAP_DISCONN_RSP:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005238 err = l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005239 break;
5240
5241 case L2CAP_ECHO_REQ:
5242 l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
5243 break;
5244
5245 case L2CAP_ECHO_RSP:
5246 break;
5247
5248 case L2CAP_INFO_REQ:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005249 err = l2cap_information_req(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005250 break;
5251
5252 case L2CAP_INFO_RSP:
Johan Hedbergcb3b3152013-05-28 13:46:30 +03005253 err = l2cap_information_rsp(conn, cmd, cmd_len, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005254 break;
5255
Mat Martineauf94ff6f2011-11-02 16:18:32 -07005256 case L2CAP_CREATE_CHAN_REQ:
5257 err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
5258 break;
5259
Mat Martineau8d5a04a2011-11-02 16:18:35 -07005260 case L2CAP_MOVE_CHAN_REQ:
5261 err = l2cap_move_channel_req(conn, cmd, cmd_len, data);
5262 break;
5263
5264 case L2CAP_MOVE_CHAN_RSP:
5265 err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
5266 break;
5267
5268 case L2CAP_MOVE_CHAN_CFM:
5269 err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data);
5270 break;
5271
5272 case L2CAP_MOVE_CHAN_CFM_RSP:
5273 err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
5274 break;
5275
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005276 default:
5277 BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
5278 err = -EINVAL;
5279 break;
5280 }
5281
5282 return err;
5283}
5284
5285static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005286 struct l2cap_cmd_hdr *cmd, u8 *data)
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005287{
5288 switch (cmd->code) {
5289 case L2CAP_COMMAND_REJ:
5290 return 0;
5291
5292 case L2CAP_CONN_PARAM_UPDATE_REQ:
Claudio Takahaside731152011-02-11 19:28:55 -02005293 return l2cap_conn_param_update_req(conn, cmd, data);
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005294
5295 case L2CAP_CONN_PARAM_UPDATE_RSP:
5296 return 0;
5297
5298 default:
5299 BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
5300 return -EINVAL;
5301 }
5302}
5303
Johan Hedbergc5623552013-04-29 19:35:33 +03005304static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,
5305 struct sk_buff *skb)
5306{
5307 u8 *data = skb->data;
5308 int len = skb->len;
5309 struct l2cap_cmd_hdr cmd;
5310 int err;
5311
5312 l2cap_raw_recv(conn, skb);
5313
5314 while (len >= L2CAP_CMD_HDR_SIZE) {
5315 u16 cmd_len;
5316 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
5317 data += L2CAP_CMD_HDR_SIZE;
5318 len -= L2CAP_CMD_HDR_SIZE;
5319
5320 cmd_len = le16_to_cpu(cmd.len);
5321
5322 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len,
5323 cmd.ident);
5324
5325 if (cmd_len > len || !cmd.ident) {
5326 BT_DBG("corrupted command");
5327 break;
5328 }
5329
5330 err = l2cap_le_sig_cmd(conn, &cmd, data);
5331 if (err) {
5332 struct l2cap_cmd_rej_unk rej;
5333
5334 BT_ERR("Wrong link type (%d)", err);
5335
5336 /* FIXME: Map err to a valid reason */
5337 rej.reason = __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
5338 l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ,
5339 sizeof(rej), &rej);
5340 }
5341
5342 data += cmd_len;
5343 len -= cmd_len;
5344 }
5345
5346 kfree_skb(skb);
5347}
5348
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005349static inline void l2cap_sig_channel(struct l2cap_conn *conn,
Gustavo Padovan2d792812012-10-06 10:07:01 +01005350 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351{
5352 u8 *data = skb->data;
5353 int len = skb->len;
5354 struct l2cap_cmd_hdr cmd;
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02005355 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356
5357 l2cap_raw_recv(conn, skb);
5358
5359 while (len >= L2CAP_CMD_HDR_SIZE) {
Al Viro88219a02007-07-29 00:17:25 -07005360 u16 cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
5362 data += L2CAP_CMD_HDR_SIZE;
5363 len -= L2CAP_CMD_HDR_SIZE;
5364
Al Viro88219a02007-07-29 00:17:25 -07005365 cmd_len = le16_to_cpu(cmd.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366
Gustavo Padovan2d792812012-10-06 10:07:01 +01005367 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len,
5368 cmd.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
Al Viro88219a02007-07-29 00:17:25 -07005370 if (cmd_len > len || !cmd.ident) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 BT_DBG("corrupted command");
5372 break;
5373 }
5374
Johan Hedbergc5623552013-04-29 19:35:33 +03005375 err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 if (err) {
Ilia Kolomisnkye2fd3182011-07-10 08:47:44 +03005377 struct l2cap_cmd_rej_unk rej;
Gustavo F. Padovan2c6d1a22011-03-23 14:38:32 -03005378
5379 BT_ERR("Wrong link type (%d)", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
5381 /* FIXME: Map err to a valid reason */
Andrei Emeltchenkoac734982012-05-24 15:42:51 +03005382 rej.reason = __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
Gustavo Padovan2d792812012-10-06 10:07:01 +01005383 l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ,
5384 sizeof(rej), &rej);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 }
5386
Al Viro88219a02007-07-29 00:17:25 -07005387 data += cmd_len;
5388 len -= cmd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 }
5390
5391 kfree_skb(skb);
5392}
5393
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03005394static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb)
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005395{
5396 u16 our_fcs, rcv_fcs;
Andrei Emeltchenkoe4ca6d92011-10-11 13:37:52 +03005397 int hdr_size;
5398
5399 if (test_bit(FLAG_EXT_CTRL, &chan->flags))
5400 hdr_size = L2CAP_EXT_HDR_SIZE;
5401 else
5402 hdr_size = L2CAP_ENH_HDR_SIZE;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005403
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03005404 if (chan->fcs == L2CAP_FCS_CRC16) {
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03005405 skb_trim(skb, skb->len - L2CAP_FCS_SIZE);
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005406 rcv_fcs = get_unaligned_le16(skb->data + skb->len);
5407 our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
5408
5409 if (our_fcs != rcv_fcs)
João Paulo Rechi Vita7a560e52010-06-22 13:56:27 -03005410 return -EBADMSG;
Gustavo F. Padovanfcc203c2009-08-20 22:26:02 -03005411 }
5412 return 0;
5413}
5414
Mat Martineau6ea00482012-05-17 20:53:52 -07005415static void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan)
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005416{
Mat Martineaue31f7632012-05-17 20:53:41 -07005417 struct l2cap_ctrl control;
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005418
Mat Martineaue31f7632012-05-17 20:53:41 -07005419 BT_DBG("chan %p", chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005420
Mat Martineaue31f7632012-05-17 20:53:41 -07005421 memset(&control, 0, sizeof(control));
5422 control.sframe = 1;
5423 control.final = 1;
5424 control.reqseq = chan->buffer_seq;
5425 set_bit(CONN_SEND_FBIT, &chan->conn_state);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005426
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03005427 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
Mat Martineaue31f7632012-05-17 20:53:41 -07005428 control.super = L2CAP_SUPER_RNR;
5429 l2cap_send_sframe(chan, &control);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005430 }
5431
Mat Martineaue31f7632012-05-17 20:53:41 -07005432 if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) &&
5433 chan->unacked_frames > 0)
5434 __set_retrans_timer(chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005435
Mat Martineaue31f7632012-05-17 20:53:41 -07005436 /* Send pending iframes */
Gustavo F. Padovan525cd182011-03-25 19:43:39 -03005437 l2cap_ertm_send(chan);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005438
Gustavo F. Padovane2ab4352011-06-10 21:28:49 -03005439 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) &&
Mat Martineaue31f7632012-05-17 20:53:41 -07005440 test_bit(CONN_SEND_FBIT, &chan->conn_state)) {
5441 /* F-bit wasn't sent in an s-frame or i-frame yet, so
5442 * send it now.
5443 */
5444 control.super = L2CAP_SUPER_RR;
5445 l2cap_send_sframe(chan, &control);
Gustavo F. Padovand5392c82010-05-01 16:15:36 -03005446 }
5447}
5448
Gustavo Padovan2d792812012-10-06 10:07:01 +01005449static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag,
5450 struct sk_buff **last_frag)
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005451{
Mat Martineau84084a32011-07-22 14:54:00 -07005452 /* skb->len reflects data in skb as well as all fragments
5453 * skb->data_len reflects only data in fragments
5454 */
5455 if (!skb_has_frag_list(skb))
5456 skb_shinfo(skb)->frag_list = new_frag;
5457
5458 new_frag->next = NULL;
5459
5460 (*last_frag)->next = new_frag;
5461 *last_frag = new_frag;
5462
5463 skb->len += new_frag->len;
5464 skb->data_len += new_frag->len;
5465 skb->truesize += new_frag->truesize;
5466}
5467
Mat Martineau4b51dae92012-05-17 20:53:37 -07005468static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb,
5469 struct l2cap_ctrl *control)
Mat Martineau84084a32011-07-22 14:54:00 -07005470{
5471 int err = -EINVAL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005472
Mat Martineau4b51dae92012-05-17 20:53:37 -07005473 switch (control->sar) {
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005474 case L2CAP_SAR_UNSEGMENTED:
Mat Martineau84084a32011-07-22 14:54:00 -07005475 if (chan->sdu)
5476 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005477
Gustavo Padovan80b98022012-05-27 22:27:51 -03005478 err = chan->ops->recv(chan, skb);
Mat Martineau84084a32011-07-22 14:54:00 -07005479 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005480
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005481 case L2CAP_SAR_START:
Mat Martineau84084a32011-07-22 14:54:00 -07005482 if (chan->sdu)
5483 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005484
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005485 chan->sdu_len = get_unaligned_le16(skb->data);
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03005486 skb_pull(skb, L2CAP_SDULEN_SIZE);
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005487
Mat Martineau84084a32011-07-22 14:54:00 -07005488 if (chan->sdu_len > chan->imtu) {
5489 err = -EMSGSIZE;
5490 break;
5491 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005492
Mat Martineau84084a32011-07-22 14:54:00 -07005493 if (skb->len >= chan->sdu_len)
5494 break;
5495
5496 chan->sdu = skb;
5497 chan->sdu_last_frag = skb;
5498
5499 skb = NULL;
5500 err = 0;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005501 break;
5502
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005503 case L2CAP_SAR_CONTINUE:
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005504 if (!chan->sdu)
Mat Martineau84084a32011-07-22 14:54:00 -07005505 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005506
Mat Martineau84084a32011-07-22 14:54:00 -07005507 append_skb_frag(chan->sdu, skb,
5508 &chan->sdu_last_frag);
5509 skb = NULL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005510
Mat Martineau84084a32011-07-22 14:54:00 -07005511 if (chan->sdu->len >= chan->sdu_len)
5512 break;
Gustavo F. Padovan4178ba42010-05-01 16:15:45 -03005513
Mat Martineau84084a32011-07-22 14:54:00 -07005514 err = 0;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005515 break;
5516
Andrei Emeltchenko7e0ef6e2011-10-11 13:37:45 +03005517 case L2CAP_SAR_END:
Gustavo F. Padovan6f61fd472011-03-25 20:09:37 -03005518 if (!chan->sdu)
Mat Martineau84084a32011-07-22 14:54:00 -07005519 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005520
Mat Martineau84084a32011-07-22 14:54:00 -07005521 append_skb_frag(chan->sdu, skb,
5522 &chan->sdu_last_frag);
5523 skb = NULL;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005524
Mat Martineau84084a32011-07-22 14:54:00 -07005525 if (chan->sdu->len != chan->sdu_len)
5526 break;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005527
Gustavo Padovan80b98022012-05-27 22:27:51 -03005528 err = chan->ops->recv(chan, chan->sdu);
Gustavo F. Padovan4178ba42010-05-01 16:15:45 -03005529
Mat Martineau84084a32011-07-22 14:54:00 -07005530 if (!err) {
5531 /* Reassembly complete */
5532 chan->sdu = NULL;
5533 chan->sdu_last_frag = NULL;
5534 chan->sdu_len = 0;
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03005535 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005536 break;
5537 }
5538
Mat Martineau84084a32011-07-22 14:54:00 -07005539 if (err) {
5540 kfree_skb(skb);
5541 kfree_skb(chan->sdu);
5542 chan->sdu = NULL;
5543 chan->sdu_last_frag = NULL;
5544 chan->sdu_len = 0;
5545 }
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005546
Mat Martineau84084a32011-07-22 14:54:00 -07005547 return err;
Gustavo F. Padovan18778a62010-05-01 16:15:44 -03005548}
5549
Mat Martineau32b32732012-10-23 15:24:11 -07005550static int l2cap_resegment(struct l2cap_chan *chan)
5551{
5552 /* Placeholder */
5553 return 0;
5554}
5555
Mat Martineaue3281402011-07-07 09:39:02 -07005556void l2cap_chan_busy(struct l2cap_chan *chan, int busy)
Gustavo F. Padovan712132e2010-06-21 19:39:50 -03005557{
Mat Martineau61aa4f52012-05-17 20:53:40 -07005558 u8 event;
5559
5560 if (chan->mode != L2CAP_MODE_ERTM)
5561 return;
5562
5563 event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR;
Andrei Emeltchenko401bb1f2012-05-21 15:47:46 +03005564 l2cap_tx(chan, NULL, NULL, event);
Gustavo F. Padovan1890d362010-05-01 16:15:44 -03005565}
5566
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005567static int l2cap_rx_queued_iframes(struct l2cap_chan *chan)
5568{
Mat Martineau63838722012-05-17 20:53:45 -07005569 int err = 0;
5570 /* Pass sequential frames to l2cap_reassemble_sdu()
5571 * until a gap is encountered.
5572 */
5573
5574 BT_DBG("chan %p", chan);
5575
5576 while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
5577 struct sk_buff *skb;
5578 BT_DBG("Searching for skb with txseq %d (queue len %d)",
5579 chan->buffer_seq, skb_queue_len(&chan->srej_q));
5580
5581 skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq);
5582
5583 if (!skb)
5584 break;
5585
5586 skb_unlink(skb, &chan->srej_q);
5587 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
5588 err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->control);
5589 if (err)
5590 break;
5591 }
5592
5593 if (skb_queue_empty(&chan->srej_q)) {
5594 chan->rx_state = L2CAP_RX_STATE_RECV;
5595 l2cap_send_ack(chan);
5596 }
5597
5598 return err;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005599}
5600
5601static void l2cap_handle_srej(struct l2cap_chan *chan,
5602 struct l2cap_ctrl *control)
5603{
Mat Martineauf80842a2012-05-17 20:53:46 -07005604 struct sk_buff *skb;
5605
5606 BT_DBG("chan %p, control %p", chan, control);
5607
5608 if (control->reqseq == chan->next_tx_seq) {
5609 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005610 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineauf80842a2012-05-17 20:53:46 -07005611 return;
5612 }
5613
5614 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
5615
5616 if (skb == NULL) {
5617 BT_DBG("Seq %d not available for retransmission",
5618 control->reqseq);
5619 return;
5620 }
5621
5622 if (chan->max_tx != 0 && bt_cb(skb)->control.retries >= chan->max_tx) {
5623 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005624 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineauf80842a2012-05-17 20:53:46 -07005625 return;
5626 }
5627
5628 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
5629
5630 if (control->poll) {
5631 l2cap_pass_to_tx(chan, control);
5632
5633 set_bit(CONN_SEND_FBIT, &chan->conn_state);
5634 l2cap_retransmit(chan, control);
5635 l2cap_ertm_send(chan);
5636
5637 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
5638 set_bit(CONN_SREJ_ACT, &chan->conn_state);
5639 chan->srej_save_reqseq = control->reqseq;
5640 }
5641 } else {
5642 l2cap_pass_to_tx_fbit(chan, control);
5643
5644 if (control->final) {
5645 if (chan->srej_save_reqseq != control->reqseq ||
5646 !test_and_clear_bit(CONN_SREJ_ACT,
5647 &chan->conn_state))
5648 l2cap_retransmit(chan, control);
5649 } else {
5650 l2cap_retransmit(chan, control);
5651 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) {
5652 set_bit(CONN_SREJ_ACT, &chan->conn_state);
5653 chan->srej_save_reqseq = control->reqseq;
5654 }
5655 }
5656 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005657}
5658
5659static void l2cap_handle_rej(struct l2cap_chan *chan,
5660 struct l2cap_ctrl *control)
5661{
Mat Martineaufcd289d2012-05-17 20:53:47 -07005662 struct sk_buff *skb;
5663
5664 BT_DBG("chan %p, control %p", chan, control);
5665
5666 if (control->reqseq == chan->next_tx_seq) {
5667 BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005668 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaufcd289d2012-05-17 20:53:47 -07005669 return;
5670 }
5671
5672 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
5673
5674 if (chan->max_tx && skb &&
5675 bt_cb(skb)->control.retries >= chan->max_tx) {
5676 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005677 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaufcd289d2012-05-17 20:53:47 -07005678 return;
5679 }
5680
5681 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
5682
5683 l2cap_pass_to_tx(chan, control);
5684
5685 if (control->final) {
5686 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state))
5687 l2cap_retransmit_all(chan, control);
5688 } else {
5689 l2cap_retransmit_all(chan, control);
5690 l2cap_ertm_send(chan);
5691 if (chan->tx_state == L2CAP_TX_STATE_WAIT_F)
5692 set_bit(CONN_REJ_ACT, &chan->conn_state);
5693 }
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005694}
5695
Mat Martineau4b51dae92012-05-17 20:53:37 -07005696static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq)
5697{
5698 BT_DBG("chan %p, txseq %d", chan, txseq);
5699
5700 BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq,
5701 chan->expected_tx_seq);
5702
5703 if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) {
5704 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
Gustavo Padovan2d792812012-10-06 10:07:01 +01005705 chan->tx_win) {
Mat Martineau4b51dae92012-05-17 20:53:37 -07005706 /* See notes below regarding "double poll" and
5707 * invalid packets.
5708 */
5709 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
5710 BT_DBG("Invalid/Ignore - after SREJ");
5711 return L2CAP_TXSEQ_INVALID_IGNORE;
5712 } else {
5713 BT_DBG("Invalid - in window after SREJ sent");
5714 return L2CAP_TXSEQ_INVALID;
5715 }
5716 }
5717
5718 if (chan->srej_list.head == txseq) {
5719 BT_DBG("Expected SREJ");
5720 return L2CAP_TXSEQ_EXPECTED_SREJ;
5721 }
5722
5723 if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) {
5724 BT_DBG("Duplicate SREJ - txseq already stored");
5725 return L2CAP_TXSEQ_DUPLICATE_SREJ;
5726 }
5727
5728 if (l2cap_seq_list_contains(&chan->srej_list, txseq)) {
5729 BT_DBG("Unexpected SREJ - not requested");
5730 return L2CAP_TXSEQ_UNEXPECTED_SREJ;
5731 }
5732 }
5733
5734 if (chan->expected_tx_seq == txseq) {
5735 if (__seq_offset(chan, txseq, chan->last_acked_seq) >=
5736 chan->tx_win) {
5737 BT_DBG("Invalid - txseq outside tx window");
5738 return L2CAP_TXSEQ_INVALID;
5739 } else {
5740 BT_DBG("Expected");
5741 return L2CAP_TXSEQ_EXPECTED;
5742 }
5743 }
5744
5745 if (__seq_offset(chan, txseq, chan->last_acked_seq) <
Gustavo Padovan2d792812012-10-06 10:07:01 +01005746 __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) {
Mat Martineau4b51dae92012-05-17 20:53:37 -07005747 BT_DBG("Duplicate - expected_tx_seq later than txseq");
5748 return L2CAP_TXSEQ_DUPLICATE;
5749 }
5750
5751 if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) {
5752 /* A source of invalid packets is a "double poll" condition,
5753 * where delays cause us to send multiple poll packets. If
5754 * the remote stack receives and processes both polls,
5755 * sequence numbers can wrap around in such a way that a
5756 * resent frame has a sequence number that looks like new data
5757 * with a sequence gap. This would trigger an erroneous SREJ
5758 * request.
5759 *
5760 * Fortunately, this is impossible with a tx window that's
5761 * less than half of the maximum sequence number, which allows
5762 * invalid frames to be safely ignored.
5763 *
5764 * With tx window sizes greater than half of the tx window
5765 * maximum, the frame is invalid and cannot be ignored. This
5766 * causes a disconnect.
5767 */
5768
5769 if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) {
5770 BT_DBG("Invalid/Ignore - txseq outside tx window");
5771 return L2CAP_TXSEQ_INVALID_IGNORE;
5772 } else {
5773 BT_DBG("Invalid - txseq outside tx window");
5774 return L2CAP_TXSEQ_INVALID;
5775 }
5776 } else {
5777 BT_DBG("Unexpected - txseq indicates missing frames");
5778 return L2CAP_TXSEQ_UNEXPECTED;
5779 }
5780}
5781
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005782static int l2cap_rx_state_recv(struct l2cap_chan *chan,
5783 struct l2cap_ctrl *control,
5784 struct sk_buff *skb, u8 event)
5785{
5786 int err = 0;
5787 bool skb_in_use = 0;
5788
5789 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
5790 event);
5791
5792 switch (event) {
5793 case L2CAP_EV_RECV_IFRAME:
5794 switch (l2cap_classify_txseq(chan, control->txseq)) {
5795 case L2CAP_TXSEQ_EXPECTED:
5796 l2cap_pass_to_tx(chan, control);
5797
5798 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
5799 BT_DBG("Busy, discarding expected seq %d",
5800 control->txseq);
5801 break;
5802 }
5803
5804 chan->expected_tx_seq = __next_seq(chan,
5805 control->txseq);
5806
5807 chan->buffer_seq = chan->expected_tx_seq;
5808 skb_in_use = 1;
5809
5810 err = l2cap_reassemble_sdu(chan, skb, control);
5811 if (err)
5812 break;
5813
5814 if (control->final) {
5815 if (!test_and_clear_bit(CONN_REJ_ACT,
5816 &chan->conn_state)) {
5817 control->final = 0;
5818 l2cap_retransmit_all(chan, control);
5819 l2cap_ertm_send(chan);
5820 }
5821 }
5822
5823 if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state))
5824 l2cap_send_ack(chan);
5825 break;
5826 case L2CAP_TXSEQ_UNEXPECTED:
5827 l2cap_pass_to_tx(chan, control);
5828
5829 /* Can't issue SREJ frames in the local busy state.
5830 * Drop this frame, it will be seen as missing
5831 * when local busy is exited.
5832 */
5833 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
5834 BT_DBG("Busy, discarding unexpected seq %d",
5835 control->txseq);
5836 break;
5837 }
5838
5839 /* There was a gap in the sequence, so an SREJ
5840 * must be sent for each missing frame. The
5841 * current frame is stored for later use.
5842 */
5843 skb_queue_tail(&chan->srej_q, skb);
5844 skb_in_use = 1;
5845 BT_DBG("Queued %p (queue len %d)", skb,
5846 skb_queue_len(&chan->srej_q));
5847
5848 clear_bit(CONN_SREJ_ACT, &chan->conn_state);
5849 l2cap_seq_list_clear(&chan->srej_list);
5850 l2cap_send_srej(chan, control->txseq);
5851
5852 chan->rx_state = L2CAP_RX_STATE_SREJ_SENT;
5853 break;
5854 case L2CAP_TXSEQ_DUPLICATE:
5855 l2cap_pass_to_tx(chan, control);
5856 break;
5857 case L2CAP_TXSEQ_INVALID_IGNORE:
5858 break;
5859 case L2CAP_TXSEQ_INVALID:
5860 default:
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005861 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005862 break;
5863 }
5864 break;
5865 case L2CAP_EV_RECV_RR:
5866 l2cap_pass_to_tx(chan, control);
5867 if (control->final) {
5868 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
5869
Mat Martineaue6a3ee62012-10-23 15:24:22 -07005870 if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) &&
5871 !__chan_is_moving(chan)) {
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005872 control->final = 0;
5873 l2cap_retransmit_all(chan, control);
5874 }
5875
5876 l2cap_ertm_send(chan);
5877 } else if (control->poll) {
5878 l2cap_send_i_or_rr_or_rnr(chan);
5879 } else {
5880 if (test_and_clear_bit(CONN_REMOTE_BUSY,
5881 &chan->conn_state) &&
5882 chan->unacked_frames)
5883 __set_retrans_timer(chan);
5884
5885 l2cap_ertm_send(chan);
5886 }
5887 break;
5888 case L2CAP_EV_RECV_RNR:
5889 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
5890 l2cap_pass_to_tx(chan, control);
5891 if (control && control->poll) {
5892 set_bit(CONN_SEND_FBIT, &chan->conn_state);
5893 l2cap_send_rr_or_rnr(chan, 0);
5894 }
5895 __clear_retrans_timer(chan);
5896 l2cap_seq_list_clear(&chan->retrans_list);
5897 break;
5898 case L2CAP_EV_RECV_REJ:
5899 l2cap_handle_rej(chan, control);
5900 break;
5901 case L2CAP_EV_RECV_SREJ:
5902 l2cap_handle_srej(chan, control);
5903 break;
5904 default:
5905 break;
5906 }
5907
5908 if (skb && !skb_in_use) {
5909 BT_DBG("Freeing %p", skb);
5910 kfree_skb(skb);
5911 }
5912
5913 return err;
5914}
5915
5916static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
5917 struct l2cap_ctrl *control,
5918 struct sk_buff *skb, u8 event)
5919{
5920 int err = 0;
5921 u16 txseq = control->txseq;
5922 bool skb_in_use = 0;
5923
5924 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
5925 event);
5926
5927 switch (event) {
5928 case L2CAP_EV_RECV_IFRAME:
5929 switch (l2cap_classify_txseq(chan, txseq)) {
5930 case L2CAP_TXSEQ_EXPECTED:
5931 /* Keep frame for reassembly later */
5932 l2cap_pass_to_tx(chan, control);
5933 skb_queue_tail(&chan->srej_q, skb);
5934 skb_in_use = 1;
5935 BT_DBG("Queued %p (queue len %d)", skb,
5936 skb_queue_len(&chan->srej_q));
5937
5938 chan->expected_tx_seq = __next_seq(chan, txseq);
5939 break;
5940 case L2CAP_TXSEQ_EXPECTED_SREJ:
5941 l2cap_seq_list_pop(&chan->srej_list);
5942
5943 l2cap_pass_to_tx(chan, control);
5944 skb_queue_tail(&chan->srej_q, skb);
5945 skb_in_use = 1;
5946 BT_DBG("Queued %p (queue len %d)", skb,
5947 skb_queue_len(&chan->srej_q));
5948
5949 err = l2cap_rx_queued_iframes(chan);
5950 if (err)
5951 break;
5952
5953 break;
5954 case L2CAP_TXSEQ_UNEXPECTED:
5955 /* Got a frame that can't be reassembled yet.
5956 * Save it for later, and send SREJs to cover
5957 * the missing frames.
5958 */
5959 skb_queue_tail(&chan->srej_q, skb);
5960 skb_in_use = 1;
5961 BT_DBG("Queued %p (queue len %d)", skb,
5962 skb_queue_len(&chan->srej_q));
5963
5964 l2cap_pass_to_tx(chan, control);
5965 l2cap_send_srej(chan, control->txseq);
5966 break;
5967 case L2CAP_TXSEQ_UNEXPECTED_SREJ:
5968 /* This frame was requested with an SREJ, but
5969 * some expected retransmitted frames are
5970 * missing. Request retransmission of missing
5971 * SREJ'd frames.
5972 */
5973 skb_queue_tail(&chan->srej_q, skb);
5974 skb_in_use = 1;
5975 BT_DBG("Queued %p (queue len %d)", skb,
5976 skb_queue_len(&chan->srej_q));
5977
5978 l2cap_pass_to_tx(chan, control);
5979 l2cap_send_srej_list(chan, control->txseq);
5980 break;
5981 case L2CAP_TXSEQ_DUPLICATE_SREJ:
5982 /* We've already queued this frame. Drop this copy. */
5983 l2cap_pass_to_tx(chan, control);
5984 break;
5985 case L2CAP_TXSEQ_DUPLICATE:
5986 /* Expecting a later sequence number, so this frame
5987 * was already received. Ignore it completely.
5988 */
5989 break;
5990 case L2CAP_TXSEQ_INVALID_IGNORE:
5991 break;
5992 case L2CAP_TXSEQ_INVALID:
5993 default:
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02005994 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07005995 break;
5996 }
5997 break;
5998 case L2CAP_EV_RECV_RR:
5999 l2cap_pass_to_tx(chan, control);
6000 if (control->final) {
6001 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6002
6003 if (!test_and_clear_bit(CONN_REJ_ACT,
6004 &chan->conn_state)) {
6005 control->final = 0;
6006 l2cap_retransmit_all(chan, control);
6007 }
6008
6009 l2cap_ertm_send(chan);
6010 } else if (control->poll) {
6011 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6012 &chan->conn_state) &&
6013 chan->unacked_frames) {
6014 __set_retrans_timer(chan);
6015 }
6016
6017 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6018 l2cap_send_srej_tail(chan);
6019 } else {
6020 if (test_and_clear_bit(CONN_REMOTE_BUSY,
6021 &chan->conn_state) &&
6022 chan->unacked_frames)
6023 __set_retrans_timer(chan);
6024
6025 l2cap_send_ack(chan);
6026 }
6027 break;
6028 case L2CAP_EV_RECV_RNR:
6029 set_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6030 l2cap_pass_to_tx(chan, control);
6031 if (control->poll) {
6032 l2cap_send_srej_tail(chan);
6033 } else {
6034 struct l2cap_ctrl rr_control;
6035 memset(&rr_control, 0, sizeof(rr_control));
6036 rr_control.sframe = 1;
6037 rr_control.super = L2CAP_SUPER_RR;
6038 rr_control.reqseq = chan->buffer_seq;
6039 l2cap_send_sframe(chan, &rr_control);
6040 }
6041
6042 break;
6043 case L2CAP_EV_RECV_REJ:
6044 l2cap_handle_rej(chan, control);
6045 break;
6046 case L2CAP_EV_RECV_SREJ:
6047 l2cap_handle_srej(chan, control);
6048 break;
6049 }
6050
6051 if (skb && !skb_in_use) {
6052 BT_DBG("Freeing %p", skb);
6053 kfree_skb(skb);
6054 }
6055
6056 return err;
6057}
6058
Mat Martineau32b32732012-10-23 15:24:11 -07006059static int l2cap_finish_move(struct l2cap_chan *chan)
6060{
6061 BT_DBG("chan %p", chan);
6062
6063 chan->rx_state = L2CAP_RX_STATE_RECV;
6064
6065 if (chan->hs_hcon)
6066 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
6067 else
6068 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
6069
6070 return l2cap_resegment(chan);
6071}
6072
6073static int l2cap_rx_state_wait_p(struct l2cap_chan *chan,
6074 struct l2cap_ctrl *control,
6075 struct sk_buff *skb, u8 event)
6076{
6077 int err;
6078
6079 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
6080 event);
6081
6082 if (!control->poll)
6083 return -EPROTO;
6084
6085 l2cap_process_reqseq(chan, control->reqseq);
6086
6087 if (!skb_queue_empty(&chan->tx_q))
6088 chan->tx_send_head = skb_peek(&chan->tx_q);
6089 else
6090 chan->tx_send_head = NULL;
6091
6092 /* Rewind next_tx_seq to the point expected
6093 * by the receiver.
6094 */
6095 chan->next_tx_seq = control->reqseq;
6096 chan->unacked_frames = 0;
6097
6098 err = l2cap_finish_move(chan);
6099 if (err)
6100 return err;
6101
6102 set_bit(CONN_SEND_FBIT, &chan->conn_state);
6103 l2cap_send_i_or_rr_or_rnr(chan);
6104
6105 if (event == L2CAP_EV_RECV_IFRAME)
6106 return -EPROTO;
6107
6108 return l2cap_rx_state_recv(chan, control, NULL, event);
6109}
6110
6111static int l2cap_rx_state_wait_f(struct l2cap_chan *chan,
6112 struct l2cap_ctrl *control,
6113 struct sk_buff *skb, u8 event)
6114{
6115 int err;
6116
6117 if (!control->final)
6118 return -EPROTO;
6119
6120 clear_bit(CONN_REMOTE_BUSY, &chan->conn_state);
6121
6122 chan->rx_state = L2CAP_RX_STATE_RECV;
6123 l2cap_process_reqseq(chan, control->reqseq);
6124
6125 if (!skb_queue_empty(&chan->tx_q))
6126 chan->tx_send_head = skb_peek(&chan->tx_q);
6127 else
6128 chan->tx_send_head = NULL;
6129
6130 /* Rewind next_tx_seq to the point expected
6131 * by the receiver.
6132 */
6133 chan->next_tx_seq = control->reqseq;
6134 chan->unacked_frames = 0;
6135
6136 if (chan->hs_hcon)
6137 chan->conn->mtu = chan->hs_hcon->hdev->block_mtu;
6138 else
6139 chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu;
6140
6141 err = l2cap_resegment(chan);
6142
6143 if (!err)
6144 err = l2cap_rx_state_recv(chan, control, skb, event);
6145
6146 return err;
6147}
6148
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006149static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq)
6150{
6151 /* Make sure reqseq is for a packet that has been sent but not acked */
6152 u16 unacked;
6153
6154 unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq);
6155 return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked;
6156}
6157
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006158static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6159 struct sk_buff *skb, u8 event)
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006160{
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006161 int err = 0;
6162
6163 BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan,
6164 control, skb, event, chan->rx_state);
6165
6166 if (__valid_reqseq(chan, control->reqseq)) {
6167 switch (chan->rx_state) {
6168 case L2CAP_RX_STATE_RECV:
6169 err = l2cap_rx_state_recv(chan, control, skb, event);
6170 break;
6171 case L2CAP_RX_STATE_SREJ_SENT:
6172 err = l2cap_rx_state_srej_sent(chan, control, skb,
6173 event);
6174 break;
Mat Martineau32b32732012-10-23 15:24:11 -07006175 case L2CAP_RX_STATE_WAIT_P:
6176 err = l2cap_rx_state_wait_p(chan, control, skb, event);
6177 break;
6178 case L2CAP_RX_STATE_WAIT_F:
6179 err = l2cap_rx_state_wait_f(chan, control, skb, event);
6180 break;
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006181 default:
6182 /* shut it down */
6183 break;
6184 }
6185 } else {
6186 BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d",
6187 control->reqseq, chan->next_tx_seq,
6188 chan->expected_ack_seq);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006189 l2cap_send_disconn_req(chan, ECONNRESET);
Mat Martineaud2a7ac52012-05-17 20:53:42 -07006190 }
6191
6192 return err;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006193}
6194
6195static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6196 struct sk_buff *skb)
6197{
Mat Martineau4b51dae92012-05-17 20:53:37 -07006198 int err = 0;
6199
6200 BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb,
6201 chan->rx_state);
6202
6203 if (l2cap_classify_txseq(chan, control->txseq) ==
6204 L2CAP_TXSEQ_EXPECTED) {
6205 l2cap_pass_to_tx(chan, control);
6206
6207 BT_DBG("buffer_seq %d->%d", chan->buffer_seq,
6208 __next_seq(chan, chan->buffer_seq));
6209
6210 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
6211
6212 l2cap_reassemble_sdu(chan, skb, control);
6213 } else {
6214 if (chan->sdu) {
6215 kfree_skb(chan->sdu);
6216 chan->sdu = NULL;
6217 }
6218 chan->sdu_last_frag = NULL;
6219 chan->sdu_len = 0;
6220
6221 if (skb) {
6222 BT_DBG("Freeing %p", skb);
6223 kfree_skb(skb);
6224 }
6225 }
6226
6227 chan->last_acked_seq = control->txseq;
6228 chan->expected_tx_seq = __next_seq(chan, control->txseq);
6229
6230 return err;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006231}
6232
6233static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
6234{
6235 struct l2cap_ctrl *control = &bt_cb(skb)->control;
6236 u16 len;
6237 u8 event;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006238
Mat Martineaub76bbd62012-04-11 10:48:43 -07006239 __unpack_control(chan, skb);
6240
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006241 len = skb->len;
6242
6243 /*
6244 * We can just drop the corrupted I-frame here.
6245 * Receiver will miss it and start proper recovery
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006246 * procedures and ask for retransmission.
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006247 */
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006248 if (l2cap_check_fcs(chan, skb))
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006249 goto drop;
6250
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006251 if (!control->sframe && control->sar == L2CAP_SAR_START)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03006252 len -= L2CAP_SDULEN_SIZE;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006253
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006254 if (chan->fcs == L2CAP_FCS_CRC16)
Andrei Emeltchenko03a51212011-10-17 12:19:58 +03006255 len -= L2CAP_FCS_SIZE;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006256
Gustavo F. Padovan47d1ec62011-04-13 15:57:03 -03006257 if (len > chan->mps) {
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006258 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006259 goto drop;
6260 }
6261
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006262 if (!control->sframe) {
6263 int err;
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006264
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006265 BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d",
6266 control->sar, control->reqseq, control->final,
6267 control->txseq);
Andrei Emeltchenko836be932011-10-17 12:19:57 +03006268
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006269 /* Validate F-bit - F=0 always valid, F=1 only
6270 * valid in TX WAIT_F
6271 */
6272 if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F)
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006273 goto drop;
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006274
6275 if (chan->mode != L2CAP_MODE_STREAMING) {
6276 event = L2CAP_EV_RECV_IFRAME;
6277 err = l2cap_rx(chan, control, skb, event);
6278 } else {
6279 err = l2cap_stream_rx(chan, control, skb);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006280 }
6281
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006282 if (err)
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006283 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006284 } else {
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006285 const u8 rx_func_to_event[4] = {
6286 L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ,
6287 L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ
6288 };
6289
6290 /* Only I-frames are expected in streaming mode */
6291 if (chan->mode == L2CAP_MODE_STREAMING)
6292 goto drop;
6293
6294 BT_DBG("sframe reqseq %d, final %d, poll %d, super %d",
6295 control->reqseq, control->final, control->poll,
6296 control->super);
6297
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006298 if (len != 0) {
Andrei Emeltchenko1bb166e2012-11-20 17:16:21 +02006299 BT_ERR("Trailing bytes: %d in sframe", len);
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006300 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006301 goto drop;
6302 }
6303
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006304 /* Validate F and P bits */
6305 if (control->final && (control->poll ||
6306 chan->tx_state != L2CAP_TX_STATE_WAIT_F))
6307 goto drop;
6308
6309 event = rx_func_to_event[control->super];
6310 if (l2cap_rx(chan, control, skb, event))
Andrei Emeltchenko5e4e3972012-11-28 17:59:39 +02006311 l2cap_send_disconn_req(chan, ECONNRESET);
Gustavo F. Padovan218bb9d2010-06-21 18:53:22 -03006312 }
6313
6314 return 0;
6315
6316drop:
6317 kfree_skb(skb);
6318 return 0;
6319}
6320
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006321static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid,
6322 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323{
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006324 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325
Gustavo F. Padovanbaa7e1f2011-03-31 16:17:41 -03006326 chan = l2cap_get_chan_by_scid(conn, cid);
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006327 if (!chan) {
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006328 if (cid == L2CAP_CID_A2MP) {
6329 chan = a2mp_channel_create(conn, skb);
6330 if (!chan) {
6331 kfree_skb(skb);
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006332 return;
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006333 }
6334
6335 l2cap_chan_lock(chan);
6336 } else {
6337 BT_DBG("unknown cid 0x%4.4x", cid);
6338 /* Drop packet and return */
6339 kfree_skb(skb);
Andrei Emeltchenko13ca56e2012-05-31 11:18:55 +03006340 return;
Andrei Emeltchenko97e8e892012-05-29 13:59:17 +03006341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 }
6343
Gustavo F. Padovan49208c92011-04-04 15:59:54 -03006344 BT_DBG("chan %p, len %d", chan, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03006346 if (chan->state != BT_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 goto drop;
6348
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006349 switch (chan->mode) {
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006350 case L2CAP_MODE_BASIC:
6351 /* If socket recv buffers overflows we drop data here
6352 * which is *bad* because L2CAP has to be reliable.
6353 * But we don't have any other choice. L2CAP doesn't
6354 * provide flow control mechanism. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006356 if (chan->imtu < skb->len)
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006357 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
Gustavo Padovan80b98022012-05-27 22:27:51 -03006359 if (!chan->ops->recv(chan, skb))
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006360 goto done;
6361 break;
6362
6363 case L2CAP_MODE_ERTM:
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03006364 case L2CAP_MODE_STREAMING:
Mat Martineaucec8ab6e2012-05-17 20:53:36 -07006365 l2cap_data_rcv(chan, skb);
Gustavo F. Padovan6840ed02009-08-20 22:26:01 -03006366 goto done;
6367
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006368 default:
Gustavo F. Padovan0c1bc5c2011-04-13 17:20:49 -03006369 BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode);
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006370 break;
6371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372
6373drop:
6374 kfree_skb(skb);
6375
6376done:
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006377 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378}
6379
Andrei Emeltchenko84104b22012-05-31 11:18:56 +03006380static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
6381 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006383 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384
Ido Yarivc2287682012-04-20 15:46:07 -03006385 chan = l2cap_global_chan_by_psm(0, psm, conn->src, conn->dst);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006386 if (!chan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 goto drop;
6388
Andrei Emeltchenko5b4ceda2012-02-24 16:35:32 +02006389 BT_DBG("chan %p, len %d", chan, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03006391 if (chan->state != BT_BOUND && chan->state != BT_CONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 goto drop;
6393
Vinicius Costa Gomese13e21d2011-06-17 22:46:27 -03006394 if (chan->imtu < skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 goto drop;
6396
Gustavo Padovan80b98022012-05-27 22:27:51 -03006397 if (!chan->ops->recv(chan, skb))
Andrei Emeltchenko84104b22012-05-31 11:18:56 +03006398 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399
6400drop:
6401 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402}
6403
Marcel Holtmann72f78352013-04-23 00:59:00 -07006404static void l2cap_att_channel(struct l2cap_conn *conn,
Andrei Emeltchenko6810fca2012-05-31 11:18:57 +03006405 struct sk_buff *skb)
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006406{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006407 struct l2cap_chan *chan;
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006408
Johan Hedbergaf1c0132013-04-29 19:35:42 +03006409 chan = l2cap_global_chan_by_scid(BT_CONNECTED, L2CAP_CID_ATT,
Marcel Holtmann72f78352013-04-23 00:59:00 -07006410 conn->src, conn->dst);
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006411 if (!chan)
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006412 goto drop;
6413
Andrei Emeltchenko5b4ceda2012-02-24 16:35:32 +02006414 BT_DBG("chan %p, len %d", chan, skb->len);
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006415
Vinicius Costa Gomese13e21d2011-06-17 22:46:27 -03006416 if (chan->imtu < skb->len)
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006417 goto drop;
6418
Gustavo Padovan80b98022012-05-27 22:27:51 -03006419 if (!chan->ops->recv(chan, skb))
Andrei Emeltchenko6810fca2012-05-31 11:18:57 +03006420 return;
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006421
6422drop:
6423 kfree_skb(skb);
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006424}
6425
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
6427{
6428 struct l2cap_hdr *lh = (void *) skb->data;
Al Viro8e036fc2007-07-29 00:16:36 -07006429 u16 cid, len;
6430 __le16 psm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431
6432 skb_pull(skb, L2CAP_HDR_SIZE);
6433 cid = __le16_to_cpu(lh->cid);
6434 len = __le16_to_cpu(lh->len);
6435
Gustavo F. Padovan1c2acff2009-08-20 22:25:57 -03006436 if (len != skb->len) {
6437 kfree_skb(skb);
6438 return;
6439 }
6440
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 BT_DBG("len %d, cid 0x%4.4x", len, cid);
6442
6443 switch (cid) {
Claudio Takahasi3300d9a2011-02-11 19:28:54 -02006444 case L2CAP_CID_LE_SIGNALING:
Johan Hedbergc5623552013-04-29 19:35:33 +03006445 l2cap_le_sig_channel(conn, skb);
6446 break;
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -03006447 case L2CAP_CID_SIGNALING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 l2cap_sig_channel(conn, skb);
6449 break;
6450
Gustavo F. Padovan8db4dc42009-04-20 01:31:05 -03006451 case L2CAP_CID_CONN_LESS:
Andrei Emeltchenko097db762012-03-09 14:16:17 +02006452 psm = get_unaligned((__le16 *) skb->data);
Andrei Emeltchenko0181a702012-05-29 10:04:05 +03006453 skb_pull(skb, L2CAP_PSMLEN_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 l2cap_conless_channel(conn, psm, skb);
6455 break;
6456
Johan Hedberg073d1cf2013-04-29 19:35:35 +03006457 case L2CAP_CID_ATT:
Marcel Holtmann72f78352013-04-23 00:59:00 -07006458 l2cap_att_channel(conn, skb);
Gustavo F. Padovan9f69bda2011-04-07 16:40:25 -03006459 break;
6460
Anderson Brigliab501d6a2011-06-07 18:46:31 -03006461 case L2CAP_CID_SMP:
6462 if (smp_sig_channel(conn, skb))
6463 l2cap_conn_del(conn->hcon, EACCES);
6464 break;
6465
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 default:
6467 l2cap_data_channel(conn, cid, skb);
6468 break;
6469 }
6470}
6471
6472/* ---- L2CAP interface with lower layer (HCI) ---- */
6473
Ulisses Furquim686ebf22011-12-21 10:11:33 -02006474int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475{
6476 int exact = 0, lm1 = 0, lm2 = 0;
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006477 struct l2cap_chan *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03006479 BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480
6481 /* Find listening sockets and check their link_mode */
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006482 read_lock(&chan_list_lock);
6483 list_for_each_entry(c, &chan_list, global_l) {
6484 struct sock *sk = c->sk;
Gustavo F. Padovan43434782011-04-12 18:31:57 -03006485
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03006486 if (c->state != BT_LISTEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 continue;
6488
6489 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01006490 lm1 |= HCI_LM_ACCEPT;
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +03006491 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01006492 lm1 |= HCI_LM_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 exact++;
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01006494 } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
6495 lm2 |= HCI_LM_ACCEPT;
Andrei Emeltchenko43bd0f32011-10-11 14:04:34 +03006496 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
Marcel Holtmann2af6b9d2009-01-15 21:58:38 +01006497 lm2 |= HCI_LM_MASTER;
6498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 }
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006500 read_unlock(&chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501
6502 return exact ? lm1 : lm2;
6503}
6504
Andrei Emeltchenko9e664632012-07-24 16:06:15 +03006505void l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506{
Marcel Holtmann01394182006-07-03 10:02:46 +02006507 struct l2cap_conn *conn;
6508
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03006509 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 if (!status) {
Claudio Takahasibaf43252013-04-11 13:55:50 -03006512 conn = l2cap_conn_add(hcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 if (conn)
6514 l2cap_conn_ready(conn);
Andrei Emeltchenkoba6fc312012-10-31 15:46:26 +02006515 } else {
Joe Perchese1750722011-06-29 18:18:29 -07006516 l2cap_conn_del(hcon, bt_to_errno(status));
Andrei Emeltchenkoba6fc312012-10-31 15:46:26 +02006517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518}
6519
Ulisses Furquim686ebf22011-12-21 10:11:33 -02006520int l2cap_disconn_ind(struct hci_conn *hcon)
Marcel Holtmann2950f212009-02-12 14:02:50 +01006521{
6522 struct l2cap_conn *conn = hcon->l2cap_data;
6523
6524 BT_DBG("hcon %p", hcon);
6525
Ulisses Furquim686ebf22011-12-21 10:11:33 -02006526 if (!conn)
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02006527 return HCI_ERROR_REMOTE_USER_TERM;
Marcel Holtmann2950f212009-02-12 14:02:50 +01006528 return conn->disc_reason;
6529}
6530
Andrei Emeltchenko9e664632012-07-24 16:06:15 +03006531void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532{
6533 BT_DBG("hcon %p reason %d", hcon, reason);
6534
Joe Perchese1750722011-06-29 18:18:29 -07006535 l2cap_conn_del(hcon, bt_to_errno(reason));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536}
6537
Gustavo F. Padovan43434782011-04-12 18:31:57 -03006538static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
Marcel Holtmannf62e4322009-01-15 21:58:44 +01006539{
Gustavo F. Padovan715ec002011-05-02 17:13:55 -03006540 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED)
Marcel Holtmann255c7602009-02-04 21:07:19 +01006541 return;
6542
Marcel Holtmannf62e4322009-01-15 21:58:44 +01006543 if (encrypt == 0x00) {
Gustavo F. Padovan43434782011-04-12 18:31:57 -03006544 if (chan->sec_level == BT_SECURITY_MEDIUM) {
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08006545 __set_chan_timer(chan, L2CAP_ENC_TIMEOUT);
Gustavo F. Padovan43434782011-04-12 18:31:57 -03006546 } else if (chan->sec_level == BT_SECURITY_HIGH)
Gustavo F. Padovan0f852722011-05-04 19:42:50 -03006547 l2cap_chan_close(chan, ECONNREFUSED);
Marcel Holtmannf62e4322009-01-15 21:58:44 +01006548 } else {
Gustavo F. Padovan43434782011-04-12 18:31:57 -03006549 if (chan->sec_level == BT_SECURITY_MEDIUM)
Gustavo F. Padovanc9b66672011-05-17 14:59:01 -03006550 __clear_chan_timer(chan);
Marcel Holtmannf62e4322009-01-15 21:58:44 +01006551 }
6552}
6553
Ulisses Furquim686ebf22011-12-21 10:11:33 -02006554int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555{
Marcel Holtmann40be4922008-07-14 20:13:50 +02006556 struct l2cap_conn *conn = hcon->l2cap_data;
Gustavo F. Padovan48454072011-03-25 00:22:30 -03006557 struct l2cap_chan *chan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558
Marcel Holtmann01394182006-07-03 10:02:46 +02006559 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 return 0;
Marcel Holtmann01394182006-07-03 10:02:46 +02006561
Andrei Emeltchenko89d8b402012-07-10 15:27:51 +03006562 BT_DBG("conn %p status 0x%2.2x encrypt %u", conn, status, encrypt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563
Vinicius Costa Gomes160dc6a2011-08-19 21:06:55 -03006564 if (hcon->type == LE_LINK) {
Hemant Gupta35d4adcc2012-04-18 14:46:26 +05306565 if (!status && encrypt)
6566 smp_distribute_keys(conn, 0);
Ulisses Furquim17cd3f32012-01-30 18:26:28 -02006567 cancel_delayed_work(&conn->security_timer);
Vinicius Costa Gomes160dc6a2011-08-19 21:06:55 -03006568 }
6569
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02006570 mutex_lock(&conn->chan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02006572 list_for_each_entry(chan, &conn->chan_l, list) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006573 l2cap_chan_lock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574
Andrei Emeltchenko89d8b402012-07-10 15:27:51 +03006575 BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid,
6576 state_to_string(chan->state));
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03006577
Andrei Emeltchenko78eb2f92012-07-19 17:03:47 +03006578 if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) {
6579 l2cap_chan_unlock(chan);
6580 continue;
6581 }
6582
Johan Hedberg073d1cf2013-04-29 19:35:35 +03006583 if (chan->scid == L2CAP_CID_ATT) {
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03006584 if (!status && encrypt) {
6585 chan->sec_level = hcon->sec_level;
Andrei Emeltchenkocf4cd002012-02-06 15:03:59 +02006586 l2cap_chan_ready(chan);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03006587 }
6588
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006589 l2cap_chan_unlock(chan);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03006590 continue;
6591 }
6592
Andrei Emeltchenko96eff462012-11-15 18:14:53 +02006593 if (!__l2cap_no_conn_pending(chan)) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006594 l2cap_chan_unlock(chan);
Marcel Holtmann6a8d3012009-02-06 23:56:36 +01006595 continue;
6596 }
6597
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03006598 if (!status && (chan->state == BT_CONNECTED ||
Gustavo Padovan2d792812012-10-06 10:07:01 +01006599 chan->state == BT_CONFIG)) {
Gustavo Padovana7d77232012-05-13 03:20:07 -03006600 struct sock *sk = chan->sk;
6601
Gustavo Padovanc5daa682012-05-16 12:17:10 -03006602 clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
Gustavo Padovana7d77232012-05-13 03:20:07 -03006603 sk->sk_state_change(sk);
6604
Gustavo F. Padovan43434782011-04-12 18:31:57 -03006605 l2cap_check_encryption(chan, encrypt);
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006606 l2cap_chan_unlock(chan);
Marcel Holtmann9719f8a2008-07-14 20:13:45 +02006607 continue;
6608 }
6609
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03006610 if (chan->state == BT_CONNECT) {
Marcel Holtmannb1235d72008-07-14 20:13:54 +02006611 if (!status) {
Andrei Emeltchenko93c3e8f2012-09-27 17:26:16 +03006612 l2cap_start_connection(chan);
Marcel Holtmannb1235d72008-07-14 20:13:54 +02006613 } else {
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08006614 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
Marcel Holtmannb1235d72008-07-14 20:13:54 +02006615 }
Gustavo F. Padovan89bc5002011-06-03 00:19:47 -03006616 } else if (chan->state == BT_CONNECT2) {
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006617 struct sock *sk = chan->sk;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02006618 struct l2cap_conn_rsp rsp;
Johan Hedbergdf3c3932011-06-14 12:48:19 +03006619 __u16 res, stat;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02006620
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006621 lock_sock(sk);
6622
Marcel Holtmannb1235d72008-07-14 20:13:54 +02006623 if (!status) {
Gustavo Padovanc5daa682012-05-16 12:17:10 -03006624 if (test_bit(BT_SK_DEFER_SETUP,
6625 &bt_sk(sk)->flags)) {
Johan Hedbergdf3c3932011-06-14 12:48:19 +03006626 res = L2CAP_CR_PEND;
6627 stat = L2CAP_CS_AUTHOR_PEND;
Gustavo Padovan2dc4e512012-10-12 19:35:24 +08006628 chan->ops->defer(chan);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03006629 } else {
Andrei Emeltchenko0e587be2012-02-21 12:54:57 +02006630 __l2cap_state_change(chan, BT_CONFIG);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03006631 res = L2CAP_CR_SUCCESS;
6632 stat = L2CAP_CS_NO_INFO;
6633 }
Marcel Holtmannb1235d72008-07-14 20:13:54 +02006634 } else {
Andrei Emeltchenko0e587be2012-02-21 12:54:57 +02006635 __l2cap_state_change(chan, BT_DISCONN);
Marcel Holtmannba13ccd2012-03-01 14:25:33 -08006636 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03006637 res = L2CAP_CR_SEC_BLOCK;
6638 stat = L2CAP_CS_NO_INFO;
Marcel Holtmannb1235d72008-07-14 20:13:54 +02006639 }
6640
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006641 release_sock(sk);
6642
Gustavo F. Padovanfe4128e2011-04-13 19:50:45 -03006643 rsp.scid = cpu_to_le16(chan->dcid);
6644 rsp.dcid = cpu_to_le16(chan->scid);
Johan Hedbergdf3c3932011-06-14 12:48:19 +03006645 rsp.result = cpu_to_le16(res);
6646 rsp.status = cpu_to_le16(stat);
Gustavo F. Padovanfc7f8a72011-03-25 13:59:37 -03006647 l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP,
Gustavo Padovan2d792812012-10-06 10:07:01 +01006648 sizeof(rsp), &rsp);
Mat Martineau2d369352012-05-23 14:59:30 -07006649
6650 if (!test_bit(CONF_REQ_SENT, &chan->conf_state) &&
6651 res == L2CAP_CR_SUCCESS) {
6652 char buf[128];
6653 set_bit(CONF_REQ_SENT, &chan->conf_state);
6654 l2cap_send_cmd(conn, l2cap_get_ident(conn),
6655 L2CAP_CONF_REQ,
6656 l2cap_build_conf_req(chan, buf),
6657 buf);
6658 chan->num_conf_req++;
6659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 }
6661
Andrei Emeltchenko6be36552012-02-22 17:11:56 +02006662 l2cap_chan_unlock(chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 }
6664
Andrei Emeltchenko3df91ea2012-02-21 12:54:55 +02006665 mutex_unlock(&conn->chan_lock);
Marcel Holtmannb1235d72008-07-14 20:13:54 +02006666
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667 return 0;
6668}
6669
Ulisses Furquim686ebf22011-12-21 10:11:33 -02006670int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671{
6672 struct l2cap_conn *conn = hcon->l2cap_data;
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03006673 struct l2cap_hdr *hdr;
6674 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675
Andrei Emeltchenko1d13a252012-10-15 11:58:41 +03006676 /* For AMP controller do not create l2cap conn */
6677 if (!conn && hcon->hdev->dev_type != HCI_BREDR)
6678 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02006680 if (!conn)
Claudio Takahasibaf43252013-04-11 13:55:50 -03006681 conn = l2cap_conn_add(hcon);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02006682
6683 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 goto drop;
6685
6686 BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
6687
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03006688 switch (flags) {
6689 case ACL_START:
6690 case ACL_START_NO_FLUSH:
6691 case ACL_COMPLETE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692 if (conn->rx_len) {
6693 BT_ERR("Unexpected start frame (len %d)", skb->len);
6694 kfree_skb(conn->rx_skb);
6695 conn->rx_skb = NULL;
6696 conn->rx_len = 0;
6697 l2cap_conn_unreliable(conn, ECOMM);
6698 }
6699
Andrei Emeltchenkoaae7fe22010-09-15 14:28:43 +03006700 /* Start fragment always begin with Basic L2CAP header */
6701 if (skb->len < L2CAP_HDR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 BT_ERR("Frame is too short (len %d)", skb->len);
6703 l2cap_conn_unreliable(conn, ECOMM);
6704 goto drop;
6705 }
6706
6707 hdr = (struct l2cap_hdr *) skb->data;
6708 len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
6709
6710 if (len == skb->len) {
6711 /* Complete frame received */
6712 l2cap_recv_frame(conn, skb);
6713 return 0;
6714 }
6715
6716 BT_DBG("Start: total len %d, frag len %d", len, skb->len);
6717
6718 if (skb->len > len) {
6719 BT_ERR("Frame is too long (len %d, expected len %d)",
Gustavo Padovan2d792812012-10-06 10:07:01 +01006720 skb->len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 l2cap_conn_unreliable(conn, ECOMM);
6722 goto drop;
6723 }
6724
6725 /* Allocate skb for the complete frame (with header) */
Gustavo Padovan8bcde1f2012-05-28 19:18:14 -03006726 conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL);
Gustavo F. Padovanaf05b30b2009-04-20 01:31:08 -03006727 if (!conn->rx_skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 goto drop;
6729
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03006730 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Gustavo Padovan2d792812012-10-06 10:07:01 +01006731 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 conn->rx_len = len - skb->len;
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03006733 break;
6734
6735 case ACL_CONT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
6737
6738 if (!conn->rx_len) {
6739 BT_ERR("Unexpected continuation frame (len %d)", skb->len);
6740 l2cap_conn_unreliable(conn, ECOMM);
6741 goto drop;
6742 }
6743
6744 if (skb->len > conn->rx_len) {
6745 BT_ERR("Fragment is too long (len %d, expected %d)",
Gustavo Padovan2d792812012-10-06 10:07:01 +01006746 skb->len, conn->rx_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 kfree_skb(conn->rx_skb);
6748 conn->rx_skb = NULL;
6749 conn->rx_len = 0;
6750 l2cap_conn_unreliable(conn, ECOMM);
6751 goto drop;
6752 }
6753
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03006754 skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
Gustavo Padovan2d792812012-10-06 10:07:01 +01006755 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 conn->rx_len -= skb->len;
6757
6758 if (!conn->rx_len) {
6759 /* Complete frame received */
6760 l2cap_recv_frame(conn, conn->rx_skb);
6761 conn->rx_skb = NULL;
6762 }
Andrei Emeltchenkod73a0982012-10-15 11:58:40 +03006763 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 }
6765
6766drop:
6767 kfree_skb(skb);
6768 return 0;
6769}
6770
Marcel Holtmannaef7d972010-03-21 05:27:45 +01006771static int l2cap_debugfs_show(struct seq_file *f, void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772{
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006773 struct l2cap_chan *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774
Gustavo F. Padovan333055f2011-12-22 15:14:39 -02006775 read_lock(&chan_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776
Gustavo F. Padovan23691d72011-04-27 18:26:32 -03006777 list_for_each_entry(c, &chan_list, global_l) {
6778 struct sock *sk = c->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779
Andrei Emeltchenkofcb73332012-09-25 12:49:44 +03006780 seq_printf(f, "%pMR %pMR %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
6781 &bt_sk(sk)->src, &bt_sk(sk)->dst,
6782 c->state, __le16_to_cpu(c->psm),
6783 c->scid, c->dcid, c->imtu, c->omtu,
6784 c->sec_level, c->mode);
Andrei Emeltchenko61e1b4b2012-01-19 11:19:50 +02006785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786
Gustavo F. Padovan333055f2011-12-22 15:14:39 -02006787 read_unlock(&chan_list_lock);
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08006788
Marcel Holtmannaef7d972010-03-21 05:27:45 +01006789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790}
6791
Marcel Holtmannaef7d972010-03-21 05:27:45 +01006792static int l2cap_debugfs_open(struct inode *inode, struct file *file)
6793{
6794 return single_open(file, l2cap_debugfs_show, inode->i_private);
6795}
6796
6797static const struct file_operations l2cap_debugfs_fops = {
6798 .open = l2cap_debugfs_open,
6799 .read = seq_read,
6800 .llseek = seq_lseek,
6801 .release = single_release,
6802};
6803
6804static struct dentry *l2cap_debugfs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805
Gustavo F. Padovan64274512011-02-07 20:08:52 -02006806int __init l2cap_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807{
6808 int err;
Marcel Holtmannbe9d1222005-11-08 09:57:38 -08006809
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02006810 err = l2cap_init_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 if (err < 0)
6812 return err;
6813
Marcel Holtmannaef7d972010-03-21 05:27:45 +01006814 if (bt_debugfs) {
Gustavo Padovan2d792812012-10-06 10:07:01 +01006815 l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs,
6816 NULL, &l2cap_debugfs_fops);
Marcel Holtmannaef7d972010-03-21 05:27:45 +01006817 if (!l2cap_debugfs)
6818 BT_ERR("Failed to create L2CAP debug file");
6819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822}
6823
Gustavo F. Padovan64274512011-02-07 20:08:52 -02006824void l2cap_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825{
Marcel Holtmannaef7d972010-03-21 05:27:45 +01006826 debugfs_remove(l2cap_debugfs);
Gustavo F. Padovanbb58f742011-02-03 20:50:35 -02006827 l2cap_cleanup_sockets();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828}
6829
Gustavo F. Padovand1c4a172010-07-18 16:25:54 -03006830module_param(disable_ertm, bool, 0644);
6831MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");