Commit | Line | Data |
---|---|---|
8e87d142 | 1 | /* |
1da177e4 LT |
2 | BlueZ - Bluetooth protocol stack for Linux |
3 | Copyright (C) 2000-2001 Qualcomm Incorporated | |
ce5706bd | 4 | Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org> |
5d8868ff | 5 | Copyright (C) 2010 Google Inc. |
590051de | 6 | Copyright (C) 2011 ProFUSION Embedded Systems |
422e925b | 7 | Copyright (c) 2012 Code Aurora Forum. All rights reserved. |
1da177e4 LT |
8 | |
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 | |
8e87d142 YH |
19 | 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 | |
1da177e4 LT |
22 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
23 | ||
8e87d142 YH |
24 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
25 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS | |
1da177e4 LT |
26 | SOFTWARE IS DISCLAIMED. |
27 | */ | |
28 | ||
bb58f747 | 29 | /* Bluetooth L2CAP core. */ |
1da177e4 | 30 | |
1da177e4 LT |
31 | #include <linux/module.h> |
32 | ||
aef7d97c | 33 | #include <linux/debugfs.h> |
fcc203c3 | 34 | #include <linux/crc16.h> |
1da177e4 LT |
35 | |
36 | #include <net/bluetooth/bluetooth.h> | |
37 | #include <net/bluetooth/hci_core.h> | |
38 | #include <net/bluetooth/l2cap.h> | |
b501d6a1 | 39 | #include <net/bluetooth/smp.h> |
97e8e89d | 40 | #include <net/bluetooth/a2mp.h> |
1da177e4 | 41 | |
d1de6d46 | 42 | bool disable_ertm; |
f0709e03 | 43 | |
47ec1dcd | 44 | static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN; |
50a147cd | 45 | static u8 l2cap_fixed_chan[8] = { L2CAP_FC_L2CAP, }; |
1da177e4 | 46 | |
b5ad8b7f JB |
47 | static LIST_HEAD(chan_list); |
48 | static DEFINE_RWLOCK(chan_list_lock); | |
1da177e4 | 49 | |
1da177e4 | 50 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, |
2d792818 | 51 | u8 code, u8 ident, u16 dlen, void *data); |
4519de9a | 52 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, |
2d792818 | 53 | void *data); |
710f9b0a | 54 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data); |
4519de9a | 55 | static void l2cap_send_disconn_req(struct l2cap_conn *conn, |
c5daa683 | 56 | struct l2cap_chan *chan, int err); |
1da177e4 | 57 | |
d660366d | 58 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
2d792818 | 59 | struct sk_buff_head *skbs, u8 event); |
608bcc6d | 60 | |
0139418c | 61 | /* ---- L2CAP channels ---- */ |
71ba0e56 | 62 | |
2d792818 GP |
63 | static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, |
64 | u16 cid) | |
0139418c | 65 | { |
3df91ea2 | 66 | struct l2cap_chan *c; |
3d57dc68 | 67 | |
3df91ea2 AE |
68 | list_for_each_entry(c, &conn->chan_l, list) { |
69 | if (c->dcid == cid) | |
70 | return c; | |
0139418c | 71 | } |
3df91ea2 | 72 | return NULL; |
0139418c MH |
73 | } |
74 | ||
2d792818 GP |
75 | static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, |
76 | u16 cid) | |
0139418c | 77 | { |
3df91ea2 | 78 | struct l2cap_chan *c; |
3d57dc68 | 79 | |
3df91ea2 AE |
80 | list_for_each_entry(c, &conn->chan_l, list) { |
81 | if (c->scid == cid) | |
82 | return c; | |
0139418c | 83 | } |
3df91ea2 | 84 | return NULL; |
0139418c MH |
85 | } |
86 | ||
87 | /* Find channel with given SCID. | |
ef191ade | 88 | * Returns locked channel. */ |
2d792818 GP |
89 | static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, |
90 | u16 cid) | |
0139418c | 91 | { |
48454079 | 92 | struct l2cap_chan *c; |
baa7e1fa | 93 | |
3df91ea2 | 94 | mutex_lock(&conn->chan_lock); |
baa7e1fa | 95 | c = __l2cap_get_chan_by_scid(conn, cid); |
ef191ade MM |
96 | if (c) |
97 | l2cap_chan_lock(c); | |
3df91ea2 AE |
98 | mutex_unlock(&conn->chan_lock); |
99 | ||
48454079 | 100 | return c; |
0139418c MH |
101 | } |
102 | ||
b1a130b7 MM |
103 | /* Find channel with given DCID. |
104 | * Returns locked channel. | |
105 | */ | |
106 | static 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 | ||
2d792818 GP |
120 | static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, |
121 | u8 ident) | |
0139418c | 122 | { |
3df91ea2 | 123 | struct l2cap_chan *c; |
3d57dc68 | 124 | |
3df91ea2 AE |
125 | list_for_each_entry(c, &conn->chan_l, list) { |
126 | if (c->ident == ident) | |
127 | return c; | |
0139418c | 128 | } |
3df91ea2 | 129 | return NULL; |
0139418c MH |
130 | } |
131 | ||
5b155ef9 MM |
132 | static 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 | ||
23691d75 | 146 | static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src) |
9e4425ff | 147 | { |
23691d75 | 148 | struct l2cap_chan *c; |
9e4425ff | 149 | |
23691d75 GP |
150 | list_for_each_entry(c, &chan_list, global_l) { |
151 | if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src)) | |
250938cb | 152 | return c; |
9e4425ff | 153 | } |
250938cb | 154 | return NULL; |
9e4425ff GP |
155 | } |
156 | ||
157 | int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) | |
158 | { | |
73b2ec18 GP |
159 | int err; |
160 | ||
333055f2 | 161 | write_lock(&chan_list_lock); |
9e4425ff | 162 | |
23691d75 | 163 | if (psm && __l2cap_global_chan_by_addr(psm, src)) { |
73b2ec18 GP |
164 | err = -EADDRINUSE; |
165 | goto done; | |
9e4425ff GP |
166 | } |
167 | ||
73b2ec18 GP |
168 | if (psm) { |
169 | chan->psm = psm; | |
170 | chan->sport = psm; | |
171 | err = 0; | |
172 | } else { | |
173 | u16 p; | |
174 | ||
175 | err = -EINVAL; | |
176 | for (p = 0x1001; p < 0x1100; p += 2) | |
23691d75 | 177 | if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { |
73b2ec18 GP |
178 | chan->psm = cpu_to_le16(p); |
179 | chan->sport = cpu_to_le16(p); | |
180 | err = 0; | |
181 | break; | |
182 | } | |
183 | } | |
9e4425ff | 184 | |
73b2ec18 | 185 | done: |
333055f2 | 186 | write_unlock(&chan_list_lock); |
73b2ec18 | 187 | return err; |
9e4425ff GP |
188 | } |
189 | ||
190 | int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) | |
191 | { | |
333055f2 | 192 | write_lock(&chan_list_lock); |
9e4425ff GP |
193 | |
194 | chan->scid = scid; | |
195 | ||
333055f2 | 196 | write_unlock(&chan_list_lock); |
9e4425ff GP |
197 | |
198 | return 0; | |
199 | } | |
200 | ||
baa7e1fa | 201 | static u16 l2cap_alloc_cid(struct l2cap_conn *conn) |
0139418c | 202 | { |
8db4dc46 | 203 | u16 cid = L2CAP_CID_DYN_START; |
0139418c | 204 | |
8db4dc46 | 205 | for (; cid < L2CAP_CID_DYN_END; cid++) { |
baa7e1fa | 206 | if (!__l2cap_get_chan_by_scid(conn, cid)) |
0139418c MH |
207 | return cid; |
208 | } | |
209 | ||
210 | return 0; | |
211 | } | |
212 | ||
0e587be7 | 213 | static void __l2cap_state_change(struct l2cap_chan *chan, int state) |
89bc500e | 214 | { |
42d2d87c | 215 | BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state), |
2d792818 | 216 | state_to_string(state)); |
badaaa00 | 217 | |
89bc500e | 218 | chan->state = state; |
80b98027 | 219 | chan->ops->state_change(chan, state); |
89bc500e GP |
220 | } |
221 | ||
0e587be7 AE |
222 | static 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 | ||
2e0052e4 AE |
231 | static 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 | ||
238 | static 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 | ||
4239d16f MM |
247 | static 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 | ||
256 | static 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 | ||
608bcc6d MM |
265 | static 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 | ||
3c588192 MM |
278 | /* ---- 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 | ||
289 | static 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 | ||
312 | static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list) | |
313 | { | |
314 | kfree(seq_list->list); | |
315 | } | |
316 | ||
317 | static 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 | ||
324 | static 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 | ||
358 | static 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 | ||
364 | static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list) | |
365 | { | |
f522ae36 | 366 | u16 i; |
3c588192 | 367 | |
f522ae36 GP |
368 | 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; | |
3c588192 MM |
376 | } |
377 | ||
378 | static 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 | ||
f522ae36 GP |
384 | if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR) |
385 | return; | |
3c588192 | 386 | |
f522ae36 GP |
387 | 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; | |
3c588192 MM |
394 | } |
395 | ||
721c4181 | 396 | static void l2cap_chan_timeout(struct work_struct *work) |
ab07801d | 397 | { |
721c4181 | 398 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
2d792818 | 399 | chan_timer.work); |
3df91ea2 | 400 | struct l2cap_conn *conn = chan->conn; |
ab07801d GP |
401 | int reason; |
402 | ||
e05dcc32 | 403 | BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); |
ab07801d | 404 | |
3df91ea2 | 405 | mutex_lock(&conn->chan_lock); |
6be36555 | 406 | l2cap_chan_lock(chan); |
ab07801d | 407 | |
89bc500e | 408 | if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) |
ab07801d | 409 | reason = ECONNREFUSED; |
89bc500e | 410 | else if (chan->state == BT_CONNECT && |
2d792818 | 411 | chan->sec_level != BT_SECURITY_SDP) |
ab07801d GP |
412 | reason = ECONNREFUSED; |
413 | else | |
414 | reason = ETIMEDOUT; | |
415 | ||
0f852724 | 416 | l2cap_chan_close(chan, reason); |
ab07801d | 417 | |
6be36555 | 418 | l2cap_chan_unlock(chan); |
ab07801d | 419 | |
80b98027 | 420 | chan->ops->close(chan); |
3df91ea2 AE |
421 | mutex_unlock(&conn->chan_lock); |
422 | ||
371fd835 | 423 | l2cap_chan_put(chan); |
ab07801d GP |
424 | } |
425 | ||
eef1d9b6 | 426 | struct l2cap_chan *l2cap_chan_create(void) |
48454079 GP |
427 | { |
428 | struct l2cap_chan *chan; | |
429 | ||
430 | chan = kzalloc(sizeof(*chan), GFP_ATOMIC); | |
431 | if (!chan) | |
432 | return NULL; | |
433 | ||
c03b355e AE |
434 | mutex_init(&chan->lock); |
435 | ||
333055f2 | 436 | write_lock(&chan_list_lock); |
23691d75 | 437 | list_add(&chan->global_l, &chan_list); |
333055f2 | 438 | write_unlock(&chan_list_lock); |
23691d75 | 439 | |
721c4181 | 440 | INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); |
ab07801d | 441 | |
89bc500e GP |
442 | chan->state = BT_OPEN; |
443 | ||
144ad330 | 444 | kref_init(&chan->kref); |
71ba0e56 | 445 | |
2827011f MM |
446 | /* This flag is cleared in l2cap_chan_ready() */ |
447 | set_bit(CONF_NOT_COMPLETE, &chan->conf_state); | |
448 | ||
eef1d9b6 | 449 | BT_DBG("chan %p", chan); |
abc545b8 | 450 | |
48454079 GP |
451 | return chan; |
452 | } | |
453 | ||
144ad330 | 454 | static void l2cap_chan_destroy(struct kref *kref) |
6ff5abbf | 455 | { |
144ad330 SS |
456 | struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref); |
457 | ||
4af66c69 JK |
458 | BT_DBG("chan %p", chan); |
459 | ||
333055f2 | 460 | write_lock(&chan_list_lock); |
23691d75 | 461 | list_del(&chan->global_l); |
333055f2 | 462 | write_unlock(&chan_list_lock); |
23691d75 | 463 | |
4af66c69 | 464 | kfree(chan); |
6ff5abbf GP |
465 | } |
466 | ||
30648372 JK |
467 | void l2cap_chan_hold(struct l2cap_chan *c) |
468 | { | |
144ad330 | 469 | BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount)); |
30648372 | 470 | |
144ad330 | 471 | kref_get(&c->kref); |
30648372 JK |
472 | } |
473 | ||
474 | void l2cap_chan_put(struct l2cap_chan *c) | |
475 | { | |
144ad330 | 476 | BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount)); |
30648372 | 477 | |
144ad330 | 478 | kref_put(&c->kref, l2cap_chan_destroy); |
30648372 JK |
479 | } |
480 | ||
bd4b1653 AE |
481 | void 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; | |
c20f8e35 | 487 | chan->ack_win = L2CAP_DEFAULT_TX_WINDOW; |
bd4b1653 AE |
488 | chan->sec_level = BT_SECURITY_LOW; |
489 | ||
490 | set_bit(FLAG_FORCE_ACTIVE, &chan->flags); | |
491 | } | |
492 | ||
93c3e8f5 | 493 | void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
0139418c | 494 | { |
af05b30b | 495 | BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, |
097db76c | 496 | __le16_to_cpu(chan->psm), chan->dcid); |
0139418c | 497 | |
9f5a0d7b | 498 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
2950f21a | 499 | |
8c1d787b | 500 | chan->conn = conn; |
0139418c | 501 | |
5491120e AE |
502 | switch (chan->chan_type) { |
503 | case L2CAP_CHAN_CONN_ORIENTED: | |
b62f328b VT |
504 | if (conn->hcon->type == LE_LINK) { |
505 | /* LE connection */ | |
6fcb06a2 | 506 | chan->omtu = L2CAP_DEFAULT_MTU; |
fe4128e0 GP |
507 | chan->scid = L2CAP_CID_LE_DATA; |
508 | chan->dcid = L2CAP_CID_LE_DATA; | |
b62f328b VT |
509 | } else { |
510 | /* Alloc CID for connection-oriented socket */ | |
fe4128e0 | 511 | chan->scid = l2cap_alloc_cid(conn); |
0c1bc5c6 | 512 | chan->omtu = L2CAP_DEFAULT_MTU; |
b62f328b | 513 | } |
5491120e AE |
514 | break; |
515 | ||
516 | case L2CAP_CHAN_CONN_LESS: | |
0139418c | 517 | /* Connectionless socket */ |
fe4128e0 GP |
518 | chan->scid = L2CAP_CID_CONN_LESS; |
519 | chan->dcid = L2CAP_CID_CONN_LESS; | |
0c1bc5c6 | 520 | chan->omtu = L2CAP_DEFAULT_MTU; |
5491120e AE |
521 | break; |
522 | ||
416fa752 AE |
523 | case L2CAP_CHAN_CONN_FIX_A2MP: |
524 | chan->scid = L2CAP_CID_A2MP; | |
525 | chan->dcid = L2CAP_CID_A2MP; | |
526 | chan->omtu = L2CAP_A2MP_DEFAULT_MTU; | |
527 | chan->imtu = L2CAP_A2MP_DEFAULT_MTU; | |
528 | break; | |
529 | ||
5491120e | 530 | default: |
0139418c | 531 | /* Raw socket can send/recv signalling messages only */ |
fe4128e0 GP |
532 | chan->scid = L2CAP_CID_SIGNALING; |
533 | chan->dcid = L2CAP_CID_SIGNALING; | |
0c1bc5c6 | 534 | chan->omtu = L2CAP_DEFAULT_MTU; |
0139418c MH |
535 | } |
536 | ||
8f7975b1 AE |
537 | chan->local_id = L2CAP_BESTEFFORT_ID; |
538 | chan->local_stype = L2CAP_SERV_BESTEFFORT; | |
539 | chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; | |
540 | chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; | |
541 | chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; | |
8936fa6d | 542 | chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO; |
8f7975b1 | 543 | |
371fd835 | 544 | l2cap_chan_hold(chan); |
baa7e1fa | 545 | |
3df91ea2 | 546 | list_add(&chan->list, &conn->chan_l); |
643162a8 AE |
547 | } |
548 | ||
466f8004 | 549 | void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
643162a8 AE |
550 | { |
551 | mutex_lock(&conn->chan_lock); | |
552 | __l2cap_chan_add(conn, chan); | |
3df91ea2 | 553 | mutex_unlock(&conn->chan_lock); |
0139418c MH |
554 | } |
555 | ||
466f8004 | 556 | void l2cap_chan_del(struct l2cap_chan *chan, int err) |
0139418c | 557 | { |
8c1d787b | 558 | struct l2cap_conn *conn = chan->conn; |
0139418c | 559 | |
c9b66675 | 560 | __clear_chan_timer(chan); |
0139418c | 561 | |
49208c9c | 562 | BT_DBG("chan %p, conn %p, err %d", chan, conn, err); |
0139418c | 563 | |
8e87d142 | 564 | if (conn) { |
56f60984 | 565 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
baa7e1fa | 566 | /* Delete from channel list */ |
3df91ea2 | 567 | list_del(&chan->list); |
3d57dc68 | 568 | |
371fd835 | 569 | l2cap_chan_put(chan); |
baa7e1fa | 570 | |
8c1d787b | 571 | chan->conn = NULL; |
3cabbfda AE |
572 | |
573 | if (chan->chan_type != L2CAP_CHAN_CONN_FIX_A2MP) | |
574 | hci_conn_put(conn->hcon); | |
56f60984 AE |
575 | |
576 | if (mgr && mgr->bredr_chan == chan) | |
577 | mgr->bredr_chan = NULL; | |
0139418c MH |
578 | } |
579 | ||
b699ec0d | 580 | chan->ops->teardown(chan, err); |
6be36555 | 581 | |
2827011f | 582 | if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) |
6ff5abbf | 583 | return; |
2ead70b8 | 584 | |
ee556f66 GP |
585 | switch(chan->mode) { |
586 | case L2CAP_MODE_BASIC: | |
587 | break; | |
c13ffa62 | 588 | |
ee556f66 | 589 | case L2CAP_MODE_ERTM: |
1a09bcb9 GP |
590 | __clear_retrans_timer(chan); |
591 | __clear_monitor_timer(chan); | |
592 | __clear_ack_timer(chan); | |
c13ffa62 | 593 | |
f1c6775b | 594 | skb_queue_purge(&chan->srej_q); |
c13ffa62 | 595 | |
3c588192 MM |
596 | l2cap_seq_list_free(&chan->srej_list); |
597 | l2cap_seq_list_free(&chan->retrans_list); | |
ee556f66 GP |
598 | |
599 | /* fall through */ | |
600 | ||
601 | case L2CAP_MODE_STREAMING: | |
602 | skb_queue_purge(&chan->tx_q); | |
603 | break; | |
c13ffa62 | 604 | } |
ee556f66 GP |
605 | |
606 | return; | |
0139418c MH |
607 | } |
608 | ||
0f852724 | 609 | void l2cap_chan_close(struct l2cap_chan *chan, int reason) |
4519de9a GP |
610 | { |
611 | struct l2cap_conn *conn = chan->conn; | |
612 | struct sock *sk = chan->sk; | |
613 | ||
2d792818 GP |
614 | BT_DBG("chan %p state %s sk %p", chan, state_to_string(chan->state), |
615 | sk); | |
4519de9a | 616 | |
89bc500e | 617 | switch (chan->state) { |
4519de9a | 618 | case BT_LISTEN: |
b699ec0d | 619 | chan->ops->teardown(chan, 0); |
4519de9a GP |
620 | break; |
621 | ||
622 | case BT_CONNECTED: | |
623 | case BT_CONFIG: | |
715ec005 | 624 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && |
2d792818 | 625 | conn->hcon->type == ACL_LINK) { |
c9b66675 | 626 | __set_chan_timer(chan, sk->sk_sndtimeo); |
4519de9a GP |
627 | l2cap_send_disconn_req(conn, chan, reason); |
628 | } else | |
629 | l2cap_chan_del(chan, reason); | |
630 | break; | |
631 | ||
632 | case BT_CONNECT2: | |
715ec005 | 633 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && |
2d792818 | 634 | conn->hcon->type == ACL_LINK) { |
4519de9a GP |
635 | struct l2cap_conn_rsp rsp; |
636 | __u16 result; | |
637 | ||
c5daa683 | 638 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) |
4519de9a GP |
639 | result = L2CAP_CR_SEC_BLOCK; |
640 | else | |
641 | result = L2CAP_CR_BAD_PSM; | |
89bc500e | 642 | l2cap_state_change(chan, BT_DISCONN); |
4519de9a GP |
643 | |
644 | rsp.scid = cpu_to_le16(chan->dcid); | |
645 | rsp.dcid = cpu_to_le16(chan->scid); | |
646 | rsp.result = cpu_to_le16(result); | |
ac73498c | 647 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
4519de9a | 648 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
2d792818 | 649 | sizeof(rsp), &rsp); |
4519de9a GP |
650 | } |
651 | ||
652 | l2cap_chan_del(chan, reason); | |
653 | break; | |
654 | ||
655 | case BT_CONNECT: | |
656 | case BT_DISCONN: | |
657 | l2cap_chan_del(chan, reason); | |
658 | break; | |
659 | ||
660 | default: | |
b699ec0d | 661 | chan->ops->teardown(chan, 0); |
4519de9a GP |
662 | break; |
663 | } | |
664 | } | |
665 | ||
4343478f | 666 | static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) |
79d554a6 | 667 | { |
715ec005 | 668 | if (chan->chan_type == L2CAP_CHAN_RAW) { |
4343478f | 669 | switch (chan->sec_level) { |
8556edd3 JH |
670 | case BT_SECURITY_HIGH: |
671 | return HCI_AT_DEDICATED_BONDING_MITM; | |
672 | case BT_SECURITY_MEDIUM: | |
673 | return HCI_AT_DEDICATED_BONDING; | |
674 | default: | |
675 | return HCI_AT_NO_BONDING; | |
676 | } | |
2983fd68 | 677 | } else if (chan->psm == __constant_cpu_to_le16(L2CAP_PSM_SDP)) { |
4343478f GP |
678 | if (chan->sec_level == BT_SECURITY_LOW) |
679 | chan->sec_level = BT_SECURITY_SDP; | |
0684e5f9 | 680 | |
4343478f | 681 | if (chan->sec_level == BT_SECURITY_HIGH) |
8556edd3 | 682 | return HCI_AT_NO_BONDING_MITM; |
00ae4af9 | 683 | else |
8556edd3 | 684 | return HCI_AT_NO_BONDING; |
00ae4af9 | 685 | } else { |
4343478f | 686 | switch (chan->sec_level) { |
00ae4af9 | 687 | case BT_SECURITY_HIGH: |
8556edd3 | 688 | return HCI_AT_GENERAL_BONDING_MITM; |
00ae4af9 | 689 | case BT_SECURITY_MEDIUM: |
8556edd3 | 690 | return HCI_AT_GENERAL_BONDING; |
00ae4af9 | 691 | default: |
8556edd3 | 692 | return HCI_AT_NO_BONDING; |
00ae4af9 | 693 | } |
0684e5f9 | 694 | } |
8556edd3 JH |
695 | } |
696 | ||
697 | /* Service level security */ | |
d45fc423 | 698 | int l2cap_chan_check_security(struct l2cap_chan *chan) |
8556edd3 | 699 | { |
8c1d787b | 700 | struct l2cap_conn *conn = chan->conn; |
8556edd3 JH |
701 | __u8 auth_type; |
702 | ||
4343478f | 703 | auth_type = l2cap_get_auth_type(chan); |
79d554a6 | 704 | |
4343478f | 705 | return hci_conn_security(conn->hcon, chan->sec_level, auth_type); |
79d554a6 MH |
706 | } |
707 | ||
b5ad8b7f | 708 | static u8 l2cap_get_ident(struct l2cap_conn *conn) |
4e8402a3 MH |
709 | { |
710 | u8 id; | |
711 | ||
712 | /* Get next available identificator. | |
713 | * 1 - 128 are used by kernel. | |
714 | * 129 - 199 are reserved. | |
715 | * 200 - 254 are used by utilities like l2ping, etc. | |
716 | */ | |
717 | ||
333055f2 | 718 | spin_lock(&conn->lock); |
4e8402a3 MH |
719 | |
720 | if (++conn->tx_ident > 128) | |
721 | conn->tx_ident = 1; | |
722 | ||
723 | id = conn->tx_ident; | |
724 | ||
333055f2 | 725 | spin_unlock(&conn->lock); |
4e8402a3 MH |
726 | |
727 | return id; | |
728 | } | |
729 | ||
2d792818 GP |
730 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, |
731 | void *data) | |
4e8402a3 MH |
732 | { |
733 | struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); | |
e702112f | 734 | u8 flags; |
4e8402a3 MH |
735 | |
736 | BT_DBG("code 0x%2.2x", code); | |
737 | ||
738 | if (!skb) | |
9a9c6a34 | 739 | return; |
4e8402a3 | 740 | |
e702112f AE |
741 | if (lmp_no_flush_capable(conn->hcon->hdev)) |
742 | flags = ACL_START_NO_FLUSH; | |
743 | else | |
744 | flags = ACL_START; | |
745 | ||
14b12d0b | 746 | bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; |
5e59b791 | 747 | skb->priority = HCI_PRIO_MAX; |
14b12d0b | 748 | |
73d80deb LAD |
749 | hci_send_acl(conn->hchan, skb, flags); |
750 | } | |
751 | ||
02b0fbb9 MM |
752 | static bool __chan_is_moving(struct l2cap_chan *chan) |
753 | { | |
754 | return chan->move_state != L2CAP_MOVE_STABLE && | |
755 | chan->move_state != L2CAP_MOVE_WAIT_PREPARE; | |
756 | } | |
757 | ||
73d80deb LAD |
758 | static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) |
759 | { | |
760 | struct hci_conn *hcon = chan->conn->hcon; | |
761 | u16 flags; | |
762 | ||
763 | BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len, | |
2d792818 | 764 | skb->priority); |
73d80deb | 765 | |
d5f8a75d MM |
766 | if (chan->hs_hcon && !__chan_is_moving(chan)) { |
767 | if (chan->hs_hchan) | |
768 | hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE); | |
769 | else | |
770 | kfree_skb(skb); | |
771 | ||
772 | return; | |
773 | } | |
774 | ||
73d80deb | 775 | if (!test_bit(FLAG_FLUSHABLE, &chan->flags) && |
2d792818 | 776 | lmp_no_flush_capable(hcon->hdev)) |
73d80deb LAD |
777 | flags = ACL_START_NO_FLUSH; |
778 | else | |
779 | flags = ACL_START; | |
14b12d0b | 780 | |
73d80deb LAD |
781 | bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
782 | hci_send_acl(chan->conn->hchan, skb, flags); | |
4e8402a3 MH |
783 | } |
784 | ||
b76bbd66 MM |
785 | static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control) |
786 | { | |
787 | control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT; | |
788 | control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT; | |
789 | ||
790 | if (enh & L2CAP_CTRL_FRAME_TYPE) { | |
791 | /* S-Frame */ | |
792 | control->sframe = 1; | |
793 | control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT; | |
794 | control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT; | |
795 | ||
796 | control->sar = 0; | |
797 | control->txseq = 0; | |
798 | } else { | |
799 | /* I-Frame */ | |
800 | control->sframe = 0; | |
801 | control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT; | |
802 | control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT; | |
803 | ||
804 | control->poll = 0; | |
805 | control->super = 0; | |
806 | } | |
807 | } | |
808 | ||
b76bbd66 MM |
809 | static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control) |
810 | { | |
811 | control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT; | |
812 | control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT; | |
813 | ||
814 | if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) { | |
815 | /* S-Frame */ | |
816 | control->sframe = 1; | |
817 | control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT; | |
818 | control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT; | |
819 | ||
820 | control->sar = 0; | |
821 | control->txseq = 0; | |
822 | } else { | |
823 | /* I-Frame */ | |
824 | control->sframe = 0; | |
825 | control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT; | |
826 | control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT; | |
827 | ||
828 | control->poll = 0; | |
829 | control->super = 0; | |
830 | } | |
831 | } | |
832 | ||
833 | static inline void __unpack_control(struct l2cap_chan *chan, | |
834 | struct sk_buff *skb) | |
835 | { | |
836 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { | |
837 | __unpack_extended_control(get_unaligned_le32(skb->data), | |
838 | &bt_cb(skb)->control); | |
cec8ab6e | 839 | skb_pull(skb, L2CAP_EXT_CTRL_SIZE); |
b76bbd66 MM |
840 | } else { |
841 | __unpack_enhanced_control(get_unaligned_le16(skb->data), | |
842 | &bt_cb(skb)->control); | |
cec8ab6e | 843 | skb_pull(skb, L2CAP_ENH_CTRL_SIZE); |
b76bbd66 MM |
844 | } |
845 | } | |
846 | ||
b5c6aaed MM |
847 | static u32 __pack_extended_control(struct l2cap_ctrl *control) |
848 | { | |
849 | u32 packed; | |
850 | ||
851 | packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT; | |
852 | packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT; | |
853 | ||
854 | if (control->sframe) { | |
855 | packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT; | |
856 | packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT; | |
857 | packed |= L2CAP_EXT_CTRL_FRAME_TYPE; | |
858 | } else { | |
859 | packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT; | |
860 | packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT; | |
861 | } | |
862 | ||
863 | return packed; | |
864 | } | |
865 | ||
866 | static u16 __pack_enhanced_control(struct l2cap_ctrl *control) | |
867 | { | |
868 | u16 packed; | |
869 | ||
870 | packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT; | |
871 | packed |= control->final << L2CAP_CTRL_FINAL_SHIFT; | |
872 | ||
873 | if (control->sframe) { | |
874 | packed |= control->poll << L2CAP_CTRL_POLL_SHIFT; | |
875 | packed |= control->super << L2CAP_CTRL_SUPER_SHIFT; | |
876 | packed |= L2CAP_CTRL_FRAME_TYPE; | |
877 | } else { | |
878 | packed |= control->sar << L2CAP_CTRL_SAR_SHIFT; | |
879 | packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT; | |
880 | } | |
881 | ||
882 | return packed; | |
883 | } | |
884 | ||
b76bbd66 MM |
885 | static inline void __pack_control(struct l2cap_chan *chan, |
886 | struct l2cap_ctrl *control, | |
887 | struct sk_buff *skb) | |
888 | { | |
889 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { | |
890 | put_unaligned_le32(__pack_extended_control(control), | |
891 | skb->data + L2CAP_HDR_SIZE); | |
892 | } else { | |
893 | put_unaligned_le16(__pack_enhanced_control(control), | |
894 | skb->data + L2CAP_HDR_SIZE); | |
895 | } | |
896 | } | |
897 | ||
ba7aa64f GP |
898 | static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan) |
899 | { | |
900 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) | |
901 | return L2CAP_EXT_HDR_SIZE; | |
902 | else | |
903 | return L2CAP_ENH_HDR_SIZE; | |
904 | } | |
905 | ||
a67d7f6f MM |
906 | static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan, |
907 | u32 control) | |
b5c6aaed MM |
908 | { |
909 | struct sk_buff *skb; | |
910 | struct l2cap_hdr *lh; | |
ba7aa64f | 911 | int hlen = __ertm_hdr_size(chan); |
b5c6aaed MM |
912 | |
913 | if (chan->fcs == L2CAP_FCS_CRC16) | |
914 | hlen += L2CAP_FCS_SIZE; | |
915 | ||
a67d7f6f | 916 | skb = bt_skb_alloc(hlen, GFP_KERNEL); |
b5c6aaed | 917 | |
b5c6aaed | 918 | if (!skb) |
a67d7f6f | 919 | return ERR_PTR(-ENOMEM); |
b5c6aaed MM |
920 | |
921 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); | |
922 | lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); | |
923 | lh->cid = cpu_to_le16(chan->dcid); | |
924 | ||
a67d7f6f MM |
925 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
926 | put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); | |
927 | else | |
928 | put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); | |
b5c6aaed MM |
929 | |
930 | if (chan->fcs == L2CAP_FCS_CRC16) { | |
a67d7f6f | 931 | u16 fcs = crc16(0, (u8 *)skb->data, skb->len); |
b5c6aaed MM |
932 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
933 | } | |
934 | ||
935 | skb->priority = HCI_PRIO_MAX; | |
a67d7f6f MM |
936 | return skb; |
937 | } | |
938 | ||
939 | static void l2cap_send_sframe(struct l2cap_chan *chan, | |
940 | struct l2cap_ctrl *control) | |
941 | { | |
942 | struct sk_buff *skb; | |
943 | u32 control_field; | |
944 | ||
945 | BT_DBG("chan %p, control %p", chan, control); | |
946 | ||
947 | if (!control->sframe) | |
948 | return; | |
949 | ||
b99e13ad MM |
950 | if (__chan_is_moving(chan)) |
951 | return; | |
952 | ||
a67d7f6f MM |
953 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) && |
954 | !control->poll) | |
955 | control->final = 1; | |
956 | ||
957 | if (control->super == L2CAP_SUPER_RR) | |
958 | clear_bit(CONN_RNR_SENT, &chan->conn_state); | |
959 | else if (control->super == L2CAP_SUPER_RNR) | |
960 | set_bit(CONN_RNR_SENT, &chan->conn_state); | |
961 | ||
962 | if (control->super != L2CAP_SUPER_SREJ) { | |
963 | chan->last_acked_seq = control->reqseq; | |
964 | __clear_ack_timer(chan); | |
965 | } | |
966 | ||
967 | BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq, | |
968 | control->final, control->poll, control->super); | |
969 | ||
970 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) | |
971 | control_field = __pack_extended_control(control); | |
972 | else | |
973 | control_field = __pack_enhanced_control(control); | |
974 | ||
975 | skb = l2cap_create_sframe_pdu(chan, control_field); | |
976 | if (!IS_ERR(skb)) | |
977 | l2cap_do_send(chan, skb); | |
b5c6aaed MM |
978 | } |
979 | ||
c9e3d5e0 | 980 | static void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, bool poll) |
b5c6aaed | 981 | { |
c9e3d5e0 MM |
982 | struct l2cap_ctrl control; |
983 | ||
984 | BT_DBG("chan %p, poll %d", chan, poll); | |
985 | ||
986 | memset(&control, 0, sizeof(control)); | |
987 | control.sframe = 1; | |
988 | control.poll = poll; | |
989 | ||
990 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) | |
991 | control.super = L2CAP_SUPER_RNR; | |
992 | else | |
993 | control.super = L2CAP_SUPER_RR; | |
b5c6aaed | 994 | |
c9e3d5e0 MM |
995 | control.reqseq = chan->buffer_seq; |
996 | l2cap_send_sframe(chan, &control); | |
b5c6aaed MM |
997 | } |
998 | ||
b4450035 | 999 | static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) |
e501d055 | 1000 | { |
c1360a1c | 1001 | return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); |
e501d055 AE |
1002 | } |
1003 | ||
93c3e8f5 AE |
1004 | static bool __amp_capable(struct l2cap_chan *chan) |
1005 | { | |
1006 | struct l2cap_conn *conn = chan->conn; | |
1007 | ||
1008 | if (enable_hs && | |
1009 | chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED && | |
1010 | conn->fixed_chan_mask & L2CAP_FC_A2MP) | |
1011 | return true; | |
1012 | else | |
1013 | return false; | |
1014 | } | |
1015 | ||
2766be48 | 1016 | void l2cap_send_conn_req(struct l2cap_chan *chan) |
9b27f350 AE |
1017 | { |
1018 | struct l2cap_conn *conn = chan->conn; | |
1019 | struct l2cap_conn_req req; | |
1020 | ||
1021 | req.scid = cpu_to_le16(chan->scid); | |
1022 | req.psm = chan->psm; | |
1023 | ||
1024 | chan->ident = l2cap_get_ident(conn); | |
1025 | ||
1026 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); | |
1027 | ||
1028 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req); | |
1029 | } | |
1030 | ||
8eb200bd MM |
1031 | static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id) |
1032 | { | |
1033 | struct l2cap_create_chan_req req; | |
1034 | req.scid = cpu_to_le16(chan->scid); | |
1035 | req.psm = chan->psm; | |
1036 | req.amp_id = amp_id; | |
1037 | ||
1038 | chan->ident = l2cap_get_ident(chan->conn); | |
1039 | ||
1040 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ, | |
1041 | sizeof(req), &req); | |
1042 | } | |
1043 | ||
02b0fbb9 MM |
1044 | static void l2cap_move_setup(struct l2cap_chan *chan) |
1045 | { | |
1046 | struct sk_buff *skb; | |
1047 | ||
1048 | BT_DBG("chan %p", chan); | |
1049 | ||
1050 | if (chan->mode != L2CAP_MODE_ERTM) | |
1051 | return; | |
1052 | ||
1053 | __clear_retrans_timer(chan); | |
1054 | __clear_monitor_timer(chan); | |
1055 | __clear_ack_timer(chan); | |
1056 | ||
1057 | chan->retry_count = 0; | |
1058 | skb_queue_walk(&chan->tx_q, skb) { | |
1059 | if (bt_cb(skb)->control.retries) | |
1060 | bt_cb(skb)->control.retries = 1; | |
1061 | else | |
1062 | break; | |
1063 | } | |
1064 | ||
1065 | chan->expected_tx_seq = chan->buffer_seq; | |
1066 | ||
1067 | clear_bit(CONN_REJ_ACT, &chan->conn_state); | |
1068 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); | |
1069 | l2cap_seq_list_clear(&chan->retrans_list); | |
1070 | l2cap_seq_list_clear(&chan->srej_list); | |
1071 | skb_queue_purge(&chan->srej_q); | |
1072 | ||
1073 | chan->tx_state = L2CAP_TX_STATE_XMIT; | |
1074 | chan->rx_state = L2CAP_RX_STATE_MOVE; | |
1075 | ||
1076 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); | |
1077 | } | |
1078 | ||
5f3847a4 MM |
1079 | static void l2cap_move_done(struct l2cap_chan *chan) |
1080 | { | |
1081 | u8 move_role = chan->move_role; | |
1082 | BT_DBG("chan %p", chan); | |
1083 | ||
1084 | chan->move_state = L2CAP_MOVE_STABLE; | |
1085 | chan->move_role = L2CAP_MOVE_ROLE_NONE; | |
1086 | ||
1087 | if (chan->mode != L2CAP_MODE_ERTM) | |
1088 | return; | |
1089 | ||
1090 | switch (move_role) { | |
1091 | case L2CAP_MOVE_ROLE_INITIATOR: | |
1092 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL); | |
1093 | chan->rx_state = L2CAP_RX_STATE_WAIT_F; | |
1094 | break; | |
1095 | case L2CAP_MOVE_ROLE_RESPONDER: | |
1096 | chan->rx_state = L2CAP_RX_STATE_WAIT_P; | |
1097 | break; | |
1098 | } | |
1099 | } | |
1100 | ||
9f0caeb1 VCG |
1101 | static void l2cap_chan_ready(struct l2cap_chan *chan) |
1102 | { | |
2827011f | 1103 | /* This clears all conf flags, including CONF_NOT_COMPLETE */ |
9f0caeb1 VCG |
1104 | chan->conf_state = 0; |
1105 | __clear_chan_timer(chan); | |
1106 | ||
54a59aa2 | 1107 | chan->state = BT_CONNECTED; |
9f0caeb1 | 1108 | |
fd83e2c2 | 1109 | chan->ops->ready(chan); |
9f0caeb1 VCG |
1110 | } |
1111 | ||
93c3e8f5 AE |
1112 | static void l2cap_start_connection(struct l2cap_chan *chan) |
1113 | { | |
1114 | if (__amp_capable(chan)) { | |
1115 | BT_DBG("chan %p AMP capable: discover AMPs", chan); | |
1116 | a2mp_discover_amp(chan); | |
1117 | } else { | |
1118 | l2cap_send_conn_req(chan); | |
1119 | } | |
1120 | } | |
1121 | ||
fc7f8a7e | 1122 | static void l2cap_do_start(struct l2cap_chan *chan) |
79d554a6 | 1123 | { |
8c1d787b | 1124 | struct l2cap_conn *conn = chan->conn; |
79d554a6 | 1125 | |
9f0caeb1 VCG |
1126 | if (conn->hcon->type == LE_LINK) { |
1127 | l2cap_chan_ready(chan); | |
1128 | return; | |
1129 | } | |
1130 | ||
79d554a6 | 1131 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { |
984947dc MH |
1132 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) |
1133 | return; | |
1134 | ||
d45fc423 | 1135 | if (l2cap_chan_check_security(chan) && |
2d792818 | 1136 | __l2cap_no_conn_pending(chan)) { |
93c3e8f5 AE |
1137 | l2cap_start_connection(chan); |
1138 | } | |
79d554a6 MH |
1139 | } else { |
1140 | struct l2cap_info_req req; | |
ac73498c | 1141 | req.type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK); |
79d554a6 MH |
1142 | |
1143 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; | |
1144 | conn->info_ident = l2cap_get_ident(conn); | |
1145 | ||
ba13ccd9 | 1146 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
79d554a6 | 1147 | |
2d792818 GP |
1148 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
1149 | sizeof(req), &req); | |
79d554a6 MH |
1150 | } |
1151 | } | |
1152 | ||
cf6c2c0b GP |
1153 | static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) |
1154 | { | |
1155 | u32 local_feat_mask = l2cap_feat_mask; | |
d1c4a17d | 1156 | if (!disable_ertm) |
cf6c2c0b GP |
1157 | local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; |
1158 | ||
1159 | switch (mode) { | |
1160 | case L2CAP_MODE_ERTM: | |
1161 | return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; | |
1162 | case L2CAP_MODE_STREAMING: | |
1163 | return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; | |
1164 | default: | |
1165 | return 0x00; | |
1166 | } | |
1167 | } | |
1168 | ||
2d792818 GP |
1169 | static void l2cap_send_disconn_req(struct l2cap_conn *conn, |
1170 | struct l2cap_chan *chan, int err) | |
22121fc9 | 1171 | { |
6be36555 | 1172 | struct sock *sk = chan->sk; |
22121fc9 GP |
1173 | struct l2cap_disconn_req req; |
1174 | ||
c13ffa62 GP |
1175 | if (!conn) |
1176 | return; | |
1177 | ||
aad3d0e3 | 1178 | if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) { |
1a09bcb9 GP |
1179 | __clear_retrans_timer(chan); |
1180 | __clear_monitor_timer(chan); | |
1181 | __clear_ack_timer(chan); | |
c13ffa62 GP |
1182 | } |
1183 | ||
416fa752 | 1184 | if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) { |
d117773c | 1185 | l2cap_state_change(chan, BT_DISCONN); |
416fa752 AE |
1186 | return; |
1187 | } | |
1188 | ||
fe4128e0 GP |
1189 | req.dcid = cpu_to_le16(chan->dcid); |
1190 | req.scid = cpu_to_le16(chan->scid); | |
2d792818 GP |
1191 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ, |
1192 | sizeof(req), &req); | |
c13ffa62 | 1193 | |
6be36555 | 1194 | lock_sock(sk); |
0e587be7 | 1195 | __l2cap_state_change(chan, BT_DISCONN); |
2e0052e4 | 1196 | __l2cap_chan_set_err(chan, err); |
6be36555 | 1197 | release_sock(sk); |
22121fc9 GP |
1198 | } |
1199 | ||
1da177e4 | 1200 | /* ---- L2CAP connections ---- */ |
4e8402a3 MH |
1201 | static void l2cap_conn_start(struct l2cap_conn *conn) |
1202 | { | |
3df91ea2 | 1203 | struct l2cap_chan *chan, *tmp; |
4e8402a3 MH |
1204 | |
1205 | BT_DBG("conn %p", conn); | |
1206 | ||
3df91ea2 | 1207 | mutex_lock(&conn->chan_lock); |
4e8402a3 | 1208 | |
3df91ea2 | 1209 | list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { |
48454079 | 1210 | struct sock *sk = chan->sk; |
baa7e1fa | 1211 | |
6be36555 | 1212 | l2cap_chan_lock(chan); |
4e8402a3 | 1213 | |
715ec005 | 1214 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
6be36555 | 1215 | l2cap_chan_unlock(chan); |
79d554a6 MH |
1216 | continue; |
1217 | } | |
1218 | ||
89bc500e | 1219 | if (chan->state == BT_CONNECT) { |
d45fc423 | 1220 | if (!l2cap_chan_check_security(chan) || |
2d792818 | 1221 | !__l2cap_no_conn_pending(chan)) { |
6be36555 | 1222 | l2cap_chan_unlock(chan); |
47731de7 GP |
1223 | continue; |
1224 | } | |
79d554a6 | 1225 | |
c1360a1c | 1226 | if (!l2cap_mode_supported(chan->mode, conn->feat_mask) |
2d792818 | 1227 | && test_bit(CONF_STATE2_DEVICE, |
c1360a1c | 1228 | &chan->conf_state)) { |
89bc500e | 1229 | l2cap_chan_close(chan, ECONNRESET); |
6be36555 | 1230 | l2cap_chan_unlock(chan); |
47731de7 | 1231 | continue; |
b1235d79 | 1232 | } |
47731de7 | 1233 | |
93c3e8f5 | 1234 | l2cap_start_connection(chan); |
47731de7 | 1235 | |
89bc500e | 1236 | } else if (chan->state == BT_CONNECT2) { |
79d554a6 | 1237 | struct l2cap_conn_rsp rsp; |
e9aeb2dd | 1238 | char buf[128]; |
fe4128e0 GP |
1239 | rsp.scid = cpu_to_le16(chan->dcid); |
1240 | rsp.dcid = cpu_to_le16(chan->scid); | |
79d554a6 | 1241 | |
d45fc423 | 1242 | if (l2cap_chan_check_security(chan)) { |
6be36555 | 1243 | lock_sock(sk); |
c5daa683 GP |
1244 | if (test_bit(BT_SK_DEFER_SETUP, |
1245 | &bt_sk(sk)->flags)) { | |
ac73498c AE |
1246 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_PEND); |
1247 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_AUTHOR_PEND); | |
2dc4e510 | 1248 | chan->ops->defer(chan); |
f66dc81f MH |
1249 | |
1250 | } else { | |
0e587be7 | 1251 | __l2cap_state_change(chan, BT_CONFIG); |
ac73498c AE |
1252 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS); |
1253 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); | |
f66dc81f | 1254 | } |
6be36555 | 1255 | release_sock(sk); |
79d554a6 | 1256 | } else { |
ac73498c AE |
1257 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_PEND); |
1258 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_AUTHEN_PEND); | |
79d554a6 MH |
1259 | } |
1260 | ||
fc7f8a7e | 1261 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
2d792818 | 1262 | sizeof(rsp), &rsp); |
e9aeb2dd | 1263 | |
c1360a1c | 1264 | if (test_bit(CONF_REQ_SENT, &chan->conf_state) || |
2d792818 | 1265 | rsp.result != L2CAP_CR_SUCCESS) { |
6be36555 | 1266 | l2cap_chan_unlock(chan); |
e9aeb2dd GP |
1267 | continue; |
1268 | } | |
1269 | ||
c1360a1c | 1270 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
e9aeb2dd | 1271 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
2d792818 | 1272 | l2cap_build_conf_req(chan, buf), buf); |
73ffa904 | 1273 | chan->num_conf_req++; |
4e8402a3 MH |
1274 | } |
1275 | ||
6be36555 | 1276 | l2cap_chan_unlock(chan); |
4e8402a3 MH |
1277 | } |
1278 | ||
3df91ea2 | 1279 | mutex_unlock(&conn->chan_lock); |
4e8402a3 MH |
1280 | } |
1281 | ||
c2287681 | 1282 | /* Find socket with cid and source/destination bdaddr. |
b62f328b VT |
1283 | * Returns closest match, locked. |
1284 | */ | |
d9b88702 | 1285 | static struct l2cap_chan *l2cap_global_chan_by_scid(int state, u16 cid, |
c2287681 IY |
1286 | bdaddr_t *src, |
1287 | bdaddr_t *dst) | |
b62f328b | 1288 | { |
23691d75 | 1289 | struct l2cap_chan *c, *c1 = NULL; |
b62f328b | 1290 | |
23691d75 | 1291 | read_lock(&chan_list_lock); |
b62f328b | 1292 | |
23691d75 GP |
1293 | list_for_each_entry(c, &chan_list, global_l) { |
1294 | struct sock *sk = c->sk; | |
fe4128e0 | 1295 | |
89bc500e | 1296 | if (state && c->state != state) |
b62f328b VT |
1297 | continue; |
1298 | ||
23691d75 | 1299 | if (c->scid == cid) { |
c2287681 IY |
1300 | int src_match, dst_match; |
1301 | int src_any, dst_any; | |
1302 | ||
b62f328b | 1303 | /* Exact match. */ |
c2287681 IY |
1304 | src_match = !bacmp(&bt_sk(sk)->src, src); |
1305 | dst_match = !bacmp(&bt_sk(sk)->dst, dst); | |
1306 | if (src_match && dst_match) { | |
23691d75 GP |
1307 | read_unlock(&chan_list_lock); |
1308 | return c; | |
1309 | } | |
b62f328b VT |
1310 | |
1311 | /* Closest match */ | |
c2287681 IY |
1312 | src_any = !bacmp(&bt_sk(sk)->src, BDADDR_ANY); |
1313 | dst_any = !bacmp(&bt_sk(sk)->dst, BDADDR_ANY); | |
1314 | if ((src_match && dst_any) || (src_any && dst_match) || | |
1315 | (src_any && dst_any)) | |
23691d75 | 1316 | c1 = c; |
b62f328b VT |
1317 | } |
1318 | } | |
280f294f | 1319 | |
23691d75 | 1320 | read_unlock(&chan_list_lock); |
b62f328b | 1321 | |
23691d75 | 1322 | return c1; |
b62f328b VT |
1323 | } |
1324 | ||
1325 | static void l2cap_le_conn_ready(struct l2cap_conn *conn) | |
1326 | { | |
c916fbe4 | 1327 | struct sock *parent, *sk; |
23691d75 | 1328 | struct l2cap_chan *chan, *pchan; |
b62f328b VT |
1329 | |
1330 | BT_DBG(""); | |
1331 | ||
1332 | /* Check if we have socket listening on cid */ | |
23691d75 | 1333 | pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, |
c2287681 | 1334 | conn->src, conn->dst); |
23691d75 | 1335 | if (!pchan) |
b62f328b VT |
1336 | return; |
1337 | ||
23691d75 GP |
1338 | parent = pchan->sk; |
1339 | ||
aa2ac881 | 1340 | lock_sock(parent); |
62f3a2cf | 1341 | |
80b98027 | 1342 | chan = pchan->ops->new_connection(pchan); |
80808e43 | 1343 | if (!chan) |
b62f328b VT |
1344 | goto clean; |
1345 | ||
80808e43 | 1346 | sk = chan->sk; |
5d41ce1d | 1347 | |
b62f328b | 1348 | hci_conn_hold(conn->hcon); |
a9ea3ed9 | 1349 | conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT; |
b62f328b | 1350 | |
b62f328b VT |
1351 | bacpy(&bt_sk(sk)->src, conn->src); |
1352 | bacpy(&bt_sk(sk)->dst, conn->dst); | |
1353 | ||
3d57dc68 | 1354 | l2cap_chan_add(conn, chan); |
48454079 | 1355 | |
6754e0df | 1356 | l2cap_chan_ready(chan); |
b62f328b | 1357 | |
b62f328b | 1358 | clean: |
aa2ac881 | 1359 | release_sock(parent); |
b62f328b VT |
1360 | } |
1361 | ||
4e8402a3 MH |
1362 | static void l2cap_conn_ready(struct l2cap_conn *conn) |
1363 | { | |
48454079 | 1364 | struct l2cap_chan *chan; |
cc110922 | 1365 | struct hci_conn *hcon = conn->hcon; |
4e8402a3 | 1366 | |
79d554a6 | 1367 | BT_DBG("conn %p", conn); |
4e8402a3 | 1368 | |
cc110922 | 1369 | if (!hcon->out && hcon->type == LE_LINK) |
b62f328b VT |
1370 | l2cap_le_conn_ready(conn); |
1371 | ||
cc110922 VCG |
1372 | if (hcon->out && hcon->type == LE_LINK) |
1373 | smp_conn_security(hcon, hcon->pending_sec_level); | |
160dc6ac | 1374 | |
3df91ea2 | 1375 | mutex_lock(&conn->chan_lock); |
4e8402a3 | 1376 | |
3df91ea2 | 1377 | list_for_each_entry(chan, &conn->chan_l, list) { |
baa7e1fa | 1378 | |
6be36555 | 1379 | l2cap_chan_lock(chan); |
4e8402a3 | 1380 | |
416fa752 AE |
1381 | if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) { |
1382 | l2cap_chan_unlock(chan); | |
1383 | continue; | |
1384 | } | |
1385 | ||
cc110922 VCG |
1386 | if (hcon->type == LE_LINK) { |
1387 | if (smp_conn_security(hcon, chan->sec_level)) | |
cf4cd009 | 1388 | l2cap_chan_ready(chan); |
acd7d370 | 1389 | |
63128451 | 1390 | } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
6be36555 | 1391 | struct sock *sk = chan->sk; |
c9b66675 | 1392 | __clear_chan_timer(chan); |
6be36555 | 1393 | lock_sock(sk); |
0e587be7 | 1394 | __l2cap_state_change(chan, BT_CONNECTED); |
79d554a6 | 1395 | sk->sk_state_change(sk); |
6be36555 | 1396 | release_sock(sk); |
b501d6a1 | 1397 | |
89bc500e | 1398 | } else if (chan->state == BT_CONNECT) |
fc7f8a7e | 1399 | l2cap_do_start(chan); |
4e8402a3 | 1400 | |
6be36555 | 1401 | l2cap_chan_unlock(chan); |
4e8402a3 | 1402 | } |
79d554a6 | 1403 | |
3df91ea2 | 1404 | mutex_unlock(&conn->chan_lock); |
4e8402a3 MH |
1405 | } |
1406 | ||
1407 | /* Notify sockets that we cannot guaranty reliability anymore */ | |
1408 | static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) | |
1409 | { | |
48454079 | 1410 | struct l2cap_chan *chan; |
4e8402a3 MH |
1411 | |
1412 | BT_DBG("conn %p", conn); | |
1413 | ||
3df91ea2 | 1414 | mutex_lock(&conn->chan_lock); |
4e8402a3 | 1415 | |
3df91ea2 | 1416 | list_for_each_entry(chan, &conn->chan_l, list) { |
ecf61bdb | 1417 | if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) |
1d8b1fd5 | 1418 | l2cap_chan_set_err(chan, err); |
4e8402a3 MH |
1419 | } |
1420 | ||
3df91ea2 | 1421 | mutex_unlock(&conn->chan_lock); |
4e8402a3 MH |
1422 | } |
1423 | ||
f878fcad | 1424 | static void l2cap_info_timeout(struct work_struct *work) |
4e8402a3 | 1425 | { |
f878fcad | 1426 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
2d792818 | 1427 | info_timer.work); |
4e8402a3 | 1428 | |
984947dc | 1429 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
e1027a7c | 1430 | conn->info_ident = 0; |
984947dc | 1431 | |
4e8402a3 MH |
1432 | l2cap_conn_start(conn); |
1433 | } | |
1434 | ||
5d3de7df VCG |
1435 | static void l2cap_conn_del(struct hci_conn *hcon, int err) |
1436 | { | |
1437 | struct l2cap_conn *conn = hcon->l2cap_data; | |
1438 | struct l2cap_chan *chan, *l; | |
5d3de7df VCG |
1439 | |
1440 | if (!conn) | |
1441 | return; | |
1442 | ||
1443 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); | |
1444 | ||
1445 | kfree_skb(conn->rx_skb); | |
1446 | ||
3df91ea2 AE |
1447 | mutex_lock(&conn->chan_lock); |
1448 | ||
5d3de7df VCG |
1449 | /* Kill channels */ |
1450 | list_for_each_entry_safe(chan, l, &conn->chan_l, list) { | |
61d6ef3e | 1451 | l2cap_chan_hold(chan); |
6be36555 AE |
1452 | l2cap_chan_lock(chan); |
1453 | ||
5d3de7df | 1454 | l2cap_chan_del(chan, err); |
6be36555 AE |
1455 | |
1456 | l2cap_chan_unlock(chan); | |
1457 | ||
80b98027 | 1458 | chan->ops->close(chan); |
61d6ef3e | 1459 | l2cap_chan_put(chan); |
5d3de7df VCG |
1460 | } |
1461 | ||
3df91ea2 AE |
1462 | mutex_unlock(&conn->chan_lock); |
1463 | ||
73d80deb LAD |
1464 | hci_chan_del(conn->hchan); |
1465 | ||
5d3de7df | 1466 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
127074bf | 1467 | cancel_delayed_work_sync(&conn->info_timer); |
5d3de7df | 1468 | |
51a8efd7 | 1469 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) { |
127074bf | 1470 | cancel_delayed_work_sync(&conn->security_timer); |
8aab4757 | 1471 | smp_chan_destroy(conn); |
d26a2345 | 1472 | } |
5d3de7df VCG |
1473 | |
1474 | hcon->l2cap_data = NULL; | |
1475 | kfree(conn); | |
1476 | } | |
1477 | ||
6c9d42a1 | 1478 | static void security_timeout(struct work_struct *work) |
5d3de7df | 1479 | { |
6c9d42a1 | 1480 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
2d792818 | 1481 | security_timer.work); |
5d3de7df | 1482 | |
d06cc416 JH |
1483 | BT_DBG("conn %p", conn); |
1484 | ||
1485 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) { | |
1486 | smp_chan_destroy(conn); | |
1487 | l2cap_conn_del(conn->hcon, ETIMEDOUT); | |
1488 | } | |
5d3de7df VCG |
1489 | } |
1490 | ||
1da177e4 LT |
1491 | static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) |
1492 | { | |
0139418c | 1493 | struct l2cap_conn *conn = hcon->l2cap_data; |
73d80deb | 1494 | struct hci_chan *hchan; |
1da177e4 | 1495 | |
0139418c | 1496 | if (conn || status) |
1da177e4 LT |
1497 | return conn; |
1498 | ||
73d80deb LAD |
1499 | hchan = hci_chan_create(hcon); |
1500 | if (!hchan) | |
1501 | return NULL; | |
1502 | ||
8bcde1f2 | 1503 | conn = kzalloc(sizeof(struct l2cap_conn), GFP_KERNEL); |
73d80deb LAD |
1504 | if (!conn) { |
1505 | hci_chan_del(hchan); | |
1da177e4 | 1506 | return NULL; |
73d80deb | 1507 | } |
1da177e4 LT |
1508 | |
1509 | hcon->l2cap_data = conn; | |
1510 | conn->hcon = hcon; | |
73d80deb | 1511 | conn->hchan = hchan; |
1da177e4 | 1512 | |
73d80deb | 1513 | BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); |
0139418c | 1514 | |
dcc042d5 AE |
1515 | switch (hcon->type) { |
1516 | case AMP_LINK: | |
1517 | conn->mtu = hcon->hdev->block_mtu; | |
1518 | break; | |
1519 | ||
1520 | case LE_LINK: | |
1521 | if (hcon->hdev->le_mtu) { | |
1522 | conn->mtu = hcon->hdev->le_mtu; | |
1523 | break; | |
1524 | } | |
1525 | /* fall through */ | |
1526 | ||
1527 | default: | |
acd7d370 | 1528 | conn->mtu = hcon->hdev->acl_mtu; |
dcc042d5 AE |
1529 | break; |
1530 | } | |
acd7d370 | 1531 | |
1da177e4 LT |
1532 | conn->src = &hcon->hdev->bdaddr; |
1533 | conn->dst = &hcon->dst; | |
1534 | ||
4e8402a3 MH |
1535 | conn->feat_mask = 0; |
1536 | ||
1da177e4 | 1537 | spin_lock_init(&conn->lock); |
3df91ea2 | 1538 | mutex_init(&conn->chan_lock); |
baa7e1fa GP |
1539 | |
1540 | INIT_LIST_HEAD(&conn->chan_l); | |
1da177e4 | 1541 | |
5d3de7df | 1542 | if (hcon->type == LE_LINK) |
6c9d42a1 | 1543 | INIT_DELAYED_WORK(&conn->security_timer, security_timeout); |
5d3de7df | 1544 | else |
030013d8 | 1545 | INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); |
45054dc1 | 1546 | |
9f5a0d7b | 1547 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
2950f21a | 1548 | |
1da177e4 LT |
1549 | return conn; |
1550 | } | |
1551 | ||
1da177e4 | 1552 | /* ---- Socket interface ---- */ |
1da177e4 | 1553 | |
c2287681 | 1554 | /* Find socket with psm and source / destination bdaddr. |
1da177e4 LT |
1555 | * Returns closest match. |
1556 | */ | |
c2287681 IY |
1557 | static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, |
1558 | bdaddr_t *src, | |
1559 | bdaddr_t *dst) | |
1da177e4 | 1560 | { |
23691d75 | 1561 | struct l2cap_chan *c, *c1 = NULL; |
1da177e4 | 1562 | |
23691d75 | 1563 | read_lock(&chan_list_lock); |
e0f0cb56 | 1564 | |
23691d75 GP |
1565 | list_for_each_entry(c, &chan_list, global_l) { |
1566 | struct sock *sk = c->sk; | |
fe4128e0 | 1567 | |
89bc500e | 1568 | if (state && c->state != state) |
1da177e4 LT |
1569 | continue; |
1570 | ||
23691d75 | 1571 | if (c->psm == psm) { |
c2287681 IY |
1572 | int src_match, dst_match; |
1573 | int src_any, dst_any; | |
1574 | ||
1da177e4 | 1575 | /* Exact match. */ |
c2287681 IY |
1576 | src_match = !bacmp(&bt_sk(sk)->src, src); |
1577 | dst_match = !bacmp(&bt_sk(sk)->dst, dst); | |
1578 | if (src_match && dst_match) { | |
a7567b20 | 1579 | read_unlock(&chan_list_lock); |
23691d75 GP |
1580 | return c; |
1581 | } | |
1da177e4 LT |
1582 | |
1583 | /* Closest match */ | |
c2287681 IY |
1584 | src_any = !bacmp(&bt_sk(sk)->src, BDADDR_ANY); |
1585 | dst_any = !bacmp(&bt_sk(sk)->dst, BDADDR_ANY); | |
1586 | if ((src_match && dst_any) || (src_any && dst_match) || | |
1587 | (src_any && dst_any)) | |
23691d75 | 1588 | c1 = c; |
1da177e4 LT |
1589 | } |
1590 | } | |
1da177e4 | 1591 | |
23691d75 | 1592 | read_unlock(&chan_list_lock); |
e0f0cb56 | 1593 | |
23691d75 | 1594 | return c1; |
1da177e4 LT |
1595 | } |
1596 | ||
8e9f9892 AG |
1597 | int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, |
1598 | bdaddr_t *dst, u8 dst_type) | |
1da177e4 | 1599 | { |
5d41ce1d | 1600 | struct sock *sk = chan->sk; |
1da177e4 | 1601 | bdaddr_t *src = &bt_sk(sk)->src; |
1da177e4 LT |
1602 | struct l2cap_conn *conn; |
1603 | struct hci_conn *hcon; | |
1604 | struct hci_dev *hdev; | |
09ab6f4c | 1605 | __u8 auth_type; |
44d0e48e | 1606 | int err; |
1da177e4 | 1607 | |
6ed93dc6 | 1608 | BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", src, dst, |
ab19516a | 1609 | dst_type, __le16_to_cpu(psm)); |
1da177e4 | 1610 | |
af05b30b GP |
1611 | hdev = hci_get_route(dst, src); |
1612 | if (!hdev) | |
1da177e4 LT |
1613 | return -EHOSTUNREACH; |
1614 | ||
09fd0de5 | 1615 | hci_dev_lock(hdev); |
1da177e4 | 1616 | |
6be36555 | 1617 | l2cap_chan_lock(chan); |
03a00194 GP |
1618 | |
1619 | /* PSM must be odd and lsb of upper byte must be 0 */ | |
1620 | if ((__le16_to_cpu(psm) & 0x0101) != 0x0001 && !cid && | |
2d792818 | 1621 | chan->chan_type != L2CAP_CHAN_RAW) { |
03a00194 GP |
1622 | err = -EINVAL; |
1623 | goto done; | |
1624 | } | |
1625 | ||
1626 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !(psm || cid)) { | |
1627 | err = -EINVAL; | |
1628 | goto done; | |
1629 | } | |
1630 | ||
1631 | switch (chan->mode) { | |
1632 | case L2CAP_MODE_BASIC: | |
1633 | break; | |
1634 | case L2CAP_MODE_ERTM: | |
1635 | case L2CAP_MODE_STREAMING: | |
1636 | if (!disable_ertm) | |
1637 | break; | |
1638 | /* fall through */ | |
1639 | default: | |
1640 | err = -ENOTSUPP; | |
1641 | goto done; | |
1642 | } | |
1643 | ||
0797e01d | 1644 | switch (chan->state) { |
03a00194 GP |
1645 | case BT_CONNECT: |
1646 | case BT_CONNECT2: | |
1647 | case BT_CONFIG: | |
1648 | /* Already connecting */ | |
1649 | err = 0; | |
1650 | goto done; | |
1651 | ||
1652 | case BT_CONNECTED: | |
1653 | /* Already connected */ | |
1654 | err = -EISCONN; | |
1655 | goto done; | |
1656 | ||
1657 | case BT_OPEN: | |
1658 | case BT_BOUND: | |
1659 | /* Can connect */ | |
1660 | break; | |
1661 | ||
1662 | default: | |
1663 | err = -EBADFD; | |
1664 | goto done; | |
1665 | } | |
1666 | ||
1667 | /* Set destination address and psm */ | |
0797e01d | 1668 | lock_sock(sk); |
9219b2a0 | 1669 | bacpy(&bt_sk(sk)->dst, dst); |
6be36555 AE |
1670 | release_sock(sk); |
1671 | ||
03a00194 GP |
1672 | chan->psm = psm; |
1673 | chan->dcid = cid; | |
1da177e4 | 1674 | |
4343478f | 1675 | auth_type = l2cap_get_auth_type(chan); |
09ab6f4c | 1676 | |
fe4128e0 | 1677 | if (chan->dcid == L2CAP_CID_LE_DATA) |
8e9f9892 | 1678 | hcon = hci_connect(hdev, LE_LINK, dst, dst_type, |
b12f62cf | 1679 | chan->sec_level, auth_type); |
acd7d370 | 1680 | else |
8e9f9892 | 1681 | hcon = hci_connect(hdev, ACL_LINK, dst, dst_type, |
b12f62cf | 1682 | chan->sec_level, auth_type); |
acd7d370 | 1683 | |
30e76272 VT |
1684 | if (IS_ERR(hcon)) { |
1685 | err = PTR_ERR(hcon); | |
1da177e4 | 1686 | goto done; |
30e76272 | 1687 | } |
1da177e4 LT |
1688 | |
1689 | conn = l2cap_conn_add(hcon, 0); | |
1690 | if (!conn) { | |
1691 | hci_conn_put(hcon); | |
30e76272 | 1692 | err = -ENOMEM; |
1da177e4 LT |
1693 | goto done; |
1694 | } | |
1695 | ||
9f0caeb1 VCG |
1696 | if (hcon->type == LE_LINK) { |
1697 | err = 0; | |
1698 | ||
1699 | if (!list_empty(&conn->chan_l)) { | |
1700 | err = -EBUSY; | |
1701 | hci_conn_put(hcon); | |
1702 | } | |
1703 | ||
1704 | if (err) | |
1705 | goto done; | |
1706 | } | |
1707 | ||
1da177e4 LT |
1708 | /* Update source addr of the socket */ |
1709 | bacpy(src, conn->src); | |
1710 | ||
6be36555 | 1711 | l2cap_chan_unlock(chan); |
48454079 | 1712 | l2cap_chan_add(conn, chan); |
6be36555 | 1713 | l2cap_chan_lock(chan); |
48454079 | 1714 | |
6be36555 | 1715 | l2cap_state_change(chan, BT_CONNECT); |
c9b66675 | 1716 | __set_chan_timer(chan, sk->sk_sndtimeo); |
1da177e4 LT |
1717 | |
1718 | if (hcon->state == BT_CONNECTED) { | |
715ec005 | 1719 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
c9b66675 | 1720 | __clear_chan_timer(chan); |
d45fc423 | 1721 | if (l2cap_chan_check_security(chan)) |
6be36555 | 1722 | l2cap_state_change(chan, BT_CONNECTED); |
79d554a6 | 1723 | } else |
fc7f8a7e | 1724 | l2cap_do_start(chan); |
1da177e4 LT |
1725 | } |
1726 | ||
30e76272 VT |
1727 | err = 0; |
1728 | ||
1da177e4 | 1729 | done: |
6be36555 | 1730 | l2cap_chan_unlock(chan); |
09fd0de5 | 1731 | hci_dev_unlock(hdev); |
1da177e4 LT |
1732 | hci_dev_put(hdev); |
1733 | return err; | |
1734 | } | |
1735 | ||
dcba0dba | 1736 | int __l2cap_wait_ack(struct sock *sk) |
6161c038 | 1737 | { |
8c1d787b | 1738 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
6161c038 GP |
1739 | DECLARE_WAITQUEUE(wait, current); |
1740 | int err = 0; | |
1741 | int timeo = HZ/5; | |
1742 | ||
2b0b05dd | 1743 | add_wait_queue(sk_sleep(sk), &wait); |
a71a0cf4 PH |
1744 | set_current_state(TASK_INTERRUPTIBLE); |
1745 | while (chan->unacked_frames > 0 && chan->conn) { | |
6161c038 GP |
1746 | if (!timeo) |
1747 | timeo = HZ/5; | |
1748 | ||
1749 | if (signal_pending(current)) { | |
1750 | err = sock_intr_errno(timeo); | |
1751 | break; | |
1752 | } | |
1753 | ||
1754 | release_sock(sk); | |
1755 | timeo = schedule_timeout(timeo); | |
1756 | lock_sock(sk); | |
a71a0cf4 | 1757 | set_current_state(TASK_INTERRUPTIBLE); |
6161c038 GP |
1758 | |
1759 | err = sock_error(sk); | |
1760 | if (err) | |
1761 | break; | |
1762 | } | |
1763 | set_current_state(TASK_RUNNING); | |
2b0b05dd | 1764 | remove_wait_queue(sk_sleep(sk), &wait); |
6161c038 GP |
1765 | return err; |
1766 | } | |
1767 | ||
721c4181 | 1768 | static void l2cap_monitor_timeout(struct work_struct *work) |
e90bac06 | 1769 | { |
721c4181 | 1770 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
4239d16f | 1771 | monitor_timer.work); |
e90bac06 | 1772 | |
525cd185 | 1773 | BT_DBG("chan %p", chan); |
0e98958d | 1774 | |
6be36555 AE |
1775 | l2cap_chan_lock(chan); |
1776 | ||
80909e04 | 1777 | if (!chan->conn) { |
6be36555 | 1778 | l2cap_chan_unlock(chan); |
8d7e1c7f | 1779 | l2cap_chan_put(chan); |
e90bac06 GP |
1780 | return; |
1781 | } | |
1782 | ||
401bb1f7 | 1783 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO); |
e90bac06 | 1784 | |
6be36555 | 1785 | l2cap_chan_unlock(chan); |
8d7e1c7f | 1786 | l2cap_chan_put(chan); |
e90bac06 GP |
1787 | } |
1788 | ||
721c4181 | 1789 | static void l2cap_retrans_timeout(struct work_struct *work) |
e90bac06 | 1790 | { |
721c4181 | 1791 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
4239d16f | 1792 | retrans_timer.work); |
e90bac06 | 1793 | |
49208c9c | 1794 | BT_DBG("chan %p", chan); |
0e98958d | 1795 | |
6be36555 AE |
1796 | l2cap_chan_lock(chan); |
1797 | ||
80909e04 MM |
1798 | if (!chan->conn) { |
1799 | l2cap_chan_unlock(chan); | |
1800 | l2cap_chan_put(chan); | |
1801 | return; | |
1802 | } | |
6be36555 | 1803 | |
401bb1f7 | 1804 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO); |
6be36555 | 1805 | l2cap_chan_unlock(chan); |
8d7e1c7f | 1806 | l2cap_chan_put(chan); |
e90bac06 GP |
1807 | } |
1808 | ||
d660366d GP |
1809 | static void l2cap_streaming_send(struct l2cap_chan *chan, |
1810 | struct sk_buff_head *skbs) | |
6840ed07 | 1811 | { |
ccbb84af | 1812 | struct sk_buff *skb; |
3733937d | 1813 | struct l2cap_ctrl *control; |
6840ed07 | 1814 | |
3733937d MM |
1815 | BT_DBG("chan %p, skbs %p", chan, skbs); |
1816 | ||
b99e13ad MM |
1817 | if (__chan_is_moving(chan)) |
1818 | return; | |
1819 | ||
3733937d MM |
1820 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
1821 | ||
1822 | while (!skb_queue_empty(&chan->tx_q)) { | |
1823 | ||
1824 | skb = skb_dequeue(&chan->tx_q); | |
1825 | ||
1826 | bt_cb(skb)->control.retries = 1; | |
1827 | control = &bt_cb(skb)->control; | |
1828 | ||
1829 | control->reqseq = 0; | |
1830 | control->txseq = chan->next_tx_seq; | |
1831 | ||
1832 | __pack_control(chan, control, skb); | |
6840ed07 | 1833 | |
47d1ec61 | 1834 | if (chan->fcs == L2CAP_FCS_CRC16) { |
3733937d MM |
1835 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
1836 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); | |
fcc203c3 GP |
1837 | } |
1838 | ||
4343478f | 1839 | l2cap_do_send(chan, skb); |
6840ed07 | 1840 | |
b4400672 | 1841 | BT_DBG("Sent txseq %u", control->txseq); |
3733937d | 1842 | |
836be934 | 1843 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
3733937d | 1844 | chan->frames_sent++; |
6840ed07 | 1845 | } |
6840ed07 GP |
1846 | } |
1847 | ||
67c9e840 | 1848 | static int l2cap_ertm_send(struct l2cap_chan *chan) |
1c2acffb GP |
1849 | { |
1850 | struct sk_buff *skb, *tx_skb; | |
18a48e76 MM |
1851 | struct l2cap_ctrl *control; |
1852 | int sent = 0; | |
1853 | ||
1854 | BT_DBG("chan %p", chan); | |
1c2acffb | 1855 | |
89bc500e | 1856 | if (chan->state != BT_CONNECTED) |
c13ffa62 | 1857 | return -ENOTCONN; |
e90bac06 | 1858 | |
94122bbe MM |
1859 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
1860 | return 0; | |
1861 | ||
b99e13ad MM |
1862 | if (__chan_is_moving(chan)) |
1863 | return 0; | |
1864 | ||
18a48e76 MM |
1865 | while (chan->tx_send_head && |
1866 | chan->unacked_frames < chan->remote_tx_win && | |
1867 | chan->tx_state == L2CAP_TX_STATE_XMIT) { | |
e420aba3 | 1868 | |
18a48e76 | 1869 | skb = chan->tx_send_head; |
e90bac06 | 1870 | |
18a48e76 MM |
1871 | bt_cb(skb)->control.retries = 1; |
1872 | control = &bt_cb(skb)->control; | |
95ffa978 | 1873 | |
e2ab4353 | 1874 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
18a48e76 | 1875 | control->final = 1; |
e2ab4353 | 1876 | |
18a48e76 MM |
1877 | control->reqseq = chan->buffer_seq; |
1878 | chan->last_acked_seq = chan->buffer_seq; | |
1879 | control->txseq = chan->next_tx_seq; | |
1c2acffb | 1880 | |
18a48e76 | 1881 | __pack_control(chan, control, skb); |
e90bac06 | 1882 | |
47d1ec61 | 1883 | if (chan->fcs == L2CAP_FCS_CRC16) { |
18a48e76 MM |
1884 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
1885 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); | |
fcc203c3 GP |
1886 | } |
1887 | ||
18a48e76 MM |
1888 | /* Clone after data has been modified. Data is assumed to be |
1889 | read-only (for locking purposes) on cloned sk_buffs. | |
1890 | */ | |
1891 | tx_skb = skb_clone(skb, GFP_KERNEL); | |
9a9c6a34 | 1892 | |
18a48e76 MM |
1893 | if (!tx_skb) |
1894 | break; | |
1c2acffb | 1895 | |
18a48e76 | 1896 | __set_retrans_timer(chan); |
836be934 AE |
1897 | |
1898 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); | |
18a48e76 | 1899 | chan->unacked_frames++; |
6a026610 | 1900 | chan->frames_sent++; |
18a48e76 | 1901 | sent++; |
1c2acffb | 1902 | |
58d35f87 GP |
1903 | if (skb_queue_is_last(&chan->tx_q, skb)) |
1904 | chan->tx_send_head = NULL; | |
1c2acffb | 1905 | else |
58d35f87 | 1906 | chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); |
18a48e76 MM |
1907 | |
1908 | l2cap_do_send(chan, tx_skb); | |
b4400672 | 1909 | BT_DBG("Sent txseq %u", control->txseq); |
1c2acffb GP |
1910 | } |
1911 | ||
b4400672 AE |
1912 | BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent, |
1913 | chan->unacked_frames, skb_queue_len(&chan->tx_q)); | |
18a48e76 MM |
1914 | |
1915 | return sent; | |
9e917af1 GP |
1916 | } |
1917 | ||
e1fbd4c1 MM |
1918 | static void l2cap_ertm_resend(struct l2cap_chan *chan) |
1919 | { | |
1920 | struct l2cap_ctrl control; | |
1921 | struct sk_buff *skb; | |
1922 | struct sk_buff *tx_skb; | |
1923 | u16 seq; | |
1924 | ||
1925 | BT_DBG("chan %p", chan); | |
1926 | ||
1927 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) | |
1928 | return; | |
1929 | ||
b99e13ad MM |
1930 | if (__chan_is_moving(chan)) |
1931 | return; | |
1932 | ||
e1fbd4c1 MM |
1933 | while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) { |
1934 | seq = l2cap_seq_list_pop(&chan->retrans_list); | |
1935 | ||
1936 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq); | |
1937 | if (!skb) { | |
1938 | BT_DBG("Error: Can't retransmit seq %d, frame missing", | |
2d792818 | 1939 | seq); |
e1fbd4c1 MM |
1940 | continue; |
1941 | } | |
1942 | ||
1943 | bt_cb(skb)->control.retries++; | |
1944 | control = bt_cb(skb)->control; | |
1945 | ||
1946 | if (chan->max_tx != 0 && | |
1947 | bt_cb(skb)->control.retries > chan->max_tx) { | |
1948 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); | |
1949 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); | |
1950 | l2cap_seq_list_clear(&chan->retrans_list); | |
1951 | break; | |
1952 | } | |
1953 | ||
1954 | control.reqseq = chan->buffer_seq; | |
1955 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) | |
1956 | control.final = 1; | |
1957 | else | |
1958 | control.final = 0; | |
1959 | ||
1960 | if (skb_cloned(skb)) { | |
1961 | /* Cloned sk_buffs are read-only, so we need a | |
1962 | * writeable copy | |
1963 | */ | |
8bcde1f2 | 1964 | tx_skb = skb_copy(skb, GFP_KERNEL); |
e1fbd4c1 | 1965 | } else { |
8bcde1f2 | 1966 | tx_skb = skb_clone(skb, GFP_KERNEL); |
e1fbd4c1 MM |
1967 | } |
1968 | ||
1969 | if (!tx_skb) { | |
1970 | l2cap_seq_list_clear(&chan->retrans_list); | |
1971 | break; | |
1972 | } | |
1973 | ||
1974 | /* Update skb contents */ | |
1975 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { | |
1976 | put_unaligned_le32(__pack_extended_control(&control), | |
1977 | tx_skb->data + L2CAP_HDR_SIZE); | |
1978 | } else { | |
1979 | put_unaligned_le16(__pack_enhanced_control(&control), | |
1980 | tx_skb->data + L2CAP_HDR_SIZE); | |
1981 | } | |
1982 | ||
1983 | if (chan->fcs == L2CAP_FCS_CRC16) { | |
1984 | u16 fcs = crc16(0, (u8 *) tx_skb->data, tx_skb->len); | |
1985 | put_unaligned_le16(fcs, skb_put(tx_skb, | |
1986 | L2CAP_FCS_SIZE)); | |
1987 | } | |
1988 | ||
1989 | l2cap_do_send(chan, tx_skb); | |
1990 | ||
1991 | BT_DBG("Resent txseq %d", control.txseq); | |
1992 | ||
1993 | chan->last_acked_seq = chan->buffer_seq; | |
1994 | } | |
1995 | } | |
1996 | ||
f80842a8 MM |
1997 | static void l2cap_retransmit(struct l2cap_chan *chan, |
1998 | struct l2cap_ctrl *control) | |
1999 | { | |
2000 | BT_DBG("chan %p, control %p", chan, control); | |
2001 | ||
2002 | l2cap_seq_list_append(&chan->retrans_list, control->reqseq); | |
2003 | l2cap_ertm_resend(chan); | |
2004 | } | |
2005 | ||
d2a7ac5d MM |
2006 | static void l2cap_retransmit_all(struct l2cap_chan *chan, |
2007 | struct l2cap_ctrl *control) | |
2008 | { | |
e1fbd4c1 MM |
2009 | struct sk_buff *skb; |
2010 | ||
2011 | BT_DBG("chan %p, control %p", chan, control); | |
2012 | ||
2013 | if (control->poll) | |
2014 | set_bit(CONN_SEND_FBIT, &chan->conn_state); | |
2015 | ||
2016 | l2cap_seq_list_clear(&chan->retrans_list); | |
2017 | ||
2018 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) | |
2019 | return; | |
2020 | ||
2021 | if (chan->unacked_frames) { | |
2022 | skb_queue_walk(&chan->tx_q, skb) { | |
2023 | if (bt_cb(skb)->control.txseq == control->reqseq || | |
2d792818 | 2024 | skb == chan->tx_send_head) |
e1fbd4c1 MM |
2025 | break; |
2026 | } | |
2027 | ||
2028 | skb_queue_walk_from(&chan->tx_q, skb) { | |
2029 | if (skb == chan->tx_send_head) | |
2030 | break; | |
2031 | ||
2032 | l2cap_seq_list_append(&chan->retrans_list, | |
2033 | bt_cb(skb)->control.txseq); | |
2034 | } | |
2035 | ||
2036 | l2cap_ertm_resend(chan); | |
2037 | } | |
d2a7ac5d MM |
2038 | } |
2039 | ||
0a0aba42 | 2040 | static void l2cap_send_ack(struct l2cap_chan *chan) |
9e917af1 | 2041 | { |
0a0aba42 MM |
2042 | struct l2cap_ctrl control; |
2043 | u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq, | |
2044 | chan->last_acked_seq); | |
2045 | int threshold; | |
9e917af1 | 2046 | |
0a0aba42 MM |
2047 | BT_DBG("chan %p last_acked_seq %d buffer_seq %d", |
2048 | chan, chan->last_acked_seq, chan->buffer_seq); | |
9e917af1 | 2049 | |
0a0aba42 MM |
2050 | memset(&control, 0, sizeof(control)); |
2051 | control.sframe = 1; | |
dfc909be | 2052 | |
0a0aba42 MM |
2053 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
2054 | chan->rx_state == L2CAP_RX_STATE_RECV) { | |
2055 | __clear_ack_timer(chan); | |
2056 | control.super = L2CAP_SUPER_RNR; | |
2057 | control.reqseq = chan->buffer_seq; | |
2058 | l2cap_send_sframe(chan, &control); | |
2059 | } else { | |
2060 | if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) { | |
2061 | l2cap_ertm_send(chan); | |
2062 | /* If any i-frames were sent, they included an ack */ | |
2063 | if (chan->buffer_seq == chan->last_acked_seq) | |
2064 | frames_to_ack = 0; | |
2065 | } | |
dfc909be | 2066 | |
c20f8e35 | 2067 | /* Ack now if the window is 3/4ths full. |
0a0aba42 MM |
2068 | * Calculate without mul or div |
2069 | */ | |
c20f8e35 | 2070 | threshold = chan->ack_win; |
0a0aba42 MM |
2071 | threshold += threshold << 1; |
2072 | threshold >>= 2; | |
2073 | ||
b4400672 | 2074 | BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack, |
0a0aba42 MM |
2075 | threshold); |
2076 | ||
2077 | if (frames_to_ack >= threshold) { | |
2078 | __clear_ack_timer(chan); | |
2079 | control.super = L2CAP_SUPER_RR; | |
2080 | control.reqseq = chan->buffer_seq; | |
2081 | l2cap_send_sframe(chan, &control); | |
2082 | frames_to_ack = 0; | |
2083 | } | |
1c2acffb | 2084 | |
0a0aba42 MM |
2085 | if (frames_to_ack) |
2086 | __set_ack_timer(chan); | |
2087 | } | |
b17e73bb SJ |
2088 | } |
2089 | ||
04124681 GP |
2090 | static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, |
2091 | struct msghdr *msg, int len, | |
2092 | int count, struct sk_buff *skb) | |
1c2acffb | 2093 | { |
0952a57a | 2094 | struct l2cap_conn *conn = chan->conn; |
1c2acffb | 2095 | struct sk_buff **frag; |
90338947 | 2096 | int sent = 0; |
1da177e4 | 2097 | |
59203a21 | 2098 | if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) |
1c2acffb | 2099 | return -EFAULT; |
1da177e4 LT |
2100 | |
2101 | sent += count; | |
2102 | len -= count; | |
2103 | ||
2104 | /* Continuation fragments (no L2CAP header) */ | |
2105 | frag = &skb_shinfo(skb)->frag_list; | |
2106 | while (len) { | |
fbe00700 GP |
2107 | struct sk_buff *tmp; |
2108 | ||
1da177e4 LT |
2109 | count = min_t(unsigned int, conn->mtu, len); |
2110 | ||
fbe00700 GP |
2111 | tmp = chan->ops->alloc_skb(chan, count, |
2112 | msg->msg_flags & MSG_DONTWAIT); | |
2113 | if (IS_ERR(tmp)) | |
2114 | return PTR_ERR(tmp); | |
2115 | ||
2116 | *frag = tmp; | |
2f7719ce | 2117 | |
1c2acffb GP |
2118 | if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) |
2119 | return -EFAULT; | |
1da177e4 | 2120 | |
5e59b791 LAD |
2121 | (*frag)->priority = skb->priority; |
2122 | ||
1da177e4 LT |
2123 | sent += count; |
2124 | len -= count; | |
2125 | ||
2d0ed3d5 GP |
2126 | skb->len += (*frag)->len; |
2127 | skb->data_len += (*frag)->len; | |
2128 | ||
1da177e4 LT |
2129 | frag = &(*frag)->next; |
2130 | } | |
1da177e4 LT |
2131 | |
2132 | return sent; | |
1c2acffb | 2133 | } |
1da177e4 | 2134 | |
5e59b791 | 2135 | static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, |
b4400672 AE |
2136 | struct msghdr *msg, size_t len, |
2137 | u32 priority) | |
1c2acffb | 2138 | { |
8c1d787b | 2139 | struct l2cap_conn *conn = chan->conn; |
1c2acffb | 2140 | struct sk_buff *skb; |
03a51213 | 2141 | int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; |
1c2acffb GP |
2142 | struct l2cap_hdr *lh; |
2143 | ||
b4400672 | 2144 | BT_DBG("chan %p len %zu priority %u", chan, len, priority); |
1c2acffb GP |
2145 | |
2146 | count = min_t(unsigned int, (conn->mtu - hlen), len); | |
2f7719ce AE |
2147 | |
2148 | skb = chan->ops->alloc_skb(chan, count + hlen, | |
90338947 GP |
2149 | msg->msg_flags & MSG_DONTWAIT); |
2150 | if (IS_ERR(skb)) | |
2151 | return skb; | |
1c2acffb | 2152 | |
5e59b791 LAD |
2153 | skb->priority = priority; |
2154 | ||
1c2acffb GP |
2155 | /* Create L2CAP header */ |
2156 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); | |
fe4128e0 | 2157 | lh->cid = cpu_to_le16(chan->dcid); |
daf6a78c AE |
2158 | lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE); |
2159 | put_unaligned(chan->psm, skb_put(skb, L2CAP_PSMLEN_SIZE)); | |
1c2acffb | 2160 | |
0952a57a | 2161 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
1c2acffb GP |
2162 | if (unlikely(err < 0)) { |
2163 | kfree_skb(skb); | |
2164 | return ERR_PTR(err); | |
2165 | } | |
2166 | return skb; | |
2167 | } | |
2168 | ||
5e59b791 | 2169 | static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, |
b4400672 AE |
2170 | struct msghdr *msg, size_t len, |
2171 | u32 priority) | |
1c2acffb | 2172 | { |
8c1d787b | 2173 | struct l2cap_conn *conn = chan->conn; |
1c2acffb | 2174 | struct sk_buff *skb; |
f2ba7fae | 2175 | int err, count; |
1c2acffb GP |
2176 | struct l2cap_hdr *lh; |
2177 | ||
b4400672 | 2178 | BT_DBG("chan %p len %zu", chan, len); |
1c2acffb | 2179 | |
f2ba7fae | 2180 | count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len); |
2f7719ce | 2181 | |
f2ba7fae | 2182 | skb = chan->ops->alloc_skb(chan, count + L2CAP_HDR_SIZE, |
90338947 GP |
2183 | msg->msg_flags & MSG_DONTWAIT); |
2184 | if (IS_ERR(skb)) | |
2185 | return skb; | |
1c2acffb | 2186 | |
5e59b791 LAD |
2187 | skb->priority = priority; |
2188 | ||
1c2acffb GP |
2189 | /* Create L2CAP header */ |
2190 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); | |
fe4128e0 | 2191 | lh->cid = cpu_to_le16(chan->dcid); |
6ff9b5ef | 2192 | lh->len = cpu_to_le16(len); |
1c2acffb | 2193 | |
0952a57a | 2194 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
1c2acffb GP |
2195 | if (unlikely(err < 0)) { |
2196 | kfree_skb(skb); | |
2197 | return ERR_PTR(err); | |
2198 | } | |
2199 | return skb; | |
2200 | } | |
2201 | ||
ab0ff76d | 2202 | static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, |
b4400672 AE |
2203 | struct msghdr *msg, size_t len, |
2204 | u16 sdulen) | |
1c2acffb | 2205 | { |
8c1d787b | 2206 | struct l2cap_conn *conn = chan->conn; |
1c2acffb | 2207 | struct sk_buff *skb; |
e4ca6d98 | 2208 | int err, count, hlen; |
1c2acffb GP |
2209 | struct l2cap_hdr *lh; |
2210 | ||
b4400672 | 2211 | BT_DBG("chan %p len %zu", chan, len); |
1c2acffb | 2212 | |
0ee0d208 GP |
2213 | if (!conn) |
2214 | return ERR_PTR(-ENOTCONN); | |
2215 | ||
ba7aa64f | 2216 | hlen = __ertm_hdr_size(chan); |
e4ca6d98 | 2217 | |
c74e560c | 2218 | if (sdulen) |
03a51213 | 2219 | hlen += L2CAP_SDULEN_SIZE; |
c74e560c | 2220 | |
47d1ec61 | 2221 | if (chan->fcs == L2CAP_FCS_CRC16) |
03a51213 | 2222 | hlen += L2CAP_FCS_SIZE; |
fcc203c3 | 2223 | |
1c2acffb | 2224 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
2f7719ce AE |
2225 | |
2226 | skb = chan->ops->alloc_skb(chan, count + hlen, | |
90338947 GP |
2227 | msg->msg_flags & MSG_DONTWAIT); |
2228 | if (IS_ERR(skb)) | |
2229 | return skb; | |
1c2acffb GP |
2230 | |
2231 | /* Create L2CAP header */ | |
2232 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); | |
fe4128e0 | 2233 | lh->cid = cpu_to_le16(chan->dcid); |
1c2acffb | 2234 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
88843ab0 | 2235 | |
18a48e76 MM |
2236 | /* Control header is populated later */ |
2237 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) | |
2238 | put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); | |
2239 | else | |
2240 | put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); | |
88843ab0 | 2241 | |
c74e560c | 2242 | if (sdulen) |
03a51213 | 2243 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
1c2acffb | 2244 | |
0952a57a | 2245 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
1c2acffb GP |
2246 | if (unlikely(err < 0)) { |
2247 | kfree_skb(skb); | |
2248 | return ERR_PTR(err); | |
2249 | } | |
e90bac06 | 2250 | |
18a48e76 | 2251 | bt_cb(skb)->control.fcs = chan->fcs; |
3ce3514f | 2252 | bt_cb(skb)->control.retries = 0; |
1c2acffb | 2253 | return skb; |
1da177e4 LT |
2254 | } |
2255 | ||
94122bbe MM |
2256 | static int l2cap_segment_sdu(struct l2cap_chan *chan, |
2257 | struct sk_buff_head *seg_queue, | |
2258 | struct msghdr *msg, size_t len) | |
c74e560c | 2259 | { |
c74e560c | 2260 | struct sk_buff *skb; |
94122bbe MM |
2261 | u16 sdu_len; |
2262 | size_t pdu_len; | |
94122bbe | 2263 | u8 sar; |
c74e560c | 2264 | |
b4400672 | 2265 | BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); |
c74e560c | 2266 | |
94122bbe MM |
2267 | /* It is critical that ERTM PDUs fit in a single HCI fragment, |
2268 | * so fragmented skbs are not used. The HCI layer's handling | |
2269 | * of fragmented skbs is not compatible with ERTM's queueing. | |
2270 | */ | |
c74e560c | 2271 | |
94122bbe MM |
2272 | /* PDU size is derived from the HCI MTU */ |
2273 | pdu_len = chan->conn->mtu; | |
c74e560c | 2274 | |
a549574d MM |
2275 | /* Constrain PDU size for BR/EDR connections */ |
2276 | if (!chan->hs_hcon) | |
2277 | pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD); | |
94122bbe MM |
2278 | |
2279 | /* Adjust for largest possible L2CAP overhead. */ | |
35d401df GP |
2280 | if (chan->fcs) |
2281 | pdu_len -= L2CAP_FCS_SIZE; | |
2282 | ||
ba7aa64f | 2283 | pdu_len -= __ertm_hdr_size(chan); |
94122bbe MM |
2284 | |
2285 | /* Remote device may have requested smaller PDUs */ | |
2286 | pdu_len = min_t(size_t, pdu_len, chan->remote_mps); | |
2287 | ||
2288 | if (len <= pdu_len) { | |
2289 | sar = L2CAP_SAR_UNSEGMENTED; | |
2290 | sdu_len = 0; | |
2291 | pdu_len = len; | |
2292 | } else { | |
2293 | sar = L2CAP_SAR_START; | |
2294 | sdu_len = len; | |
2295 | pdu_len -= L2CAP_SDULEN_SIZE; | |
2296 | } | |
2297 | ||
2298 | while (len > 0) { | |
2299 | skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len); | |
c74e560c | 2300 | |
c74e560c | 2301 | if (IS_ERR(skb)) { |
94122bbe | 2302 | __skb_queue_purge(seg_queue); |
c74e560c GP |
2303 | return PTR_ERR(skb); |
2304 | } | |
2305 | ||
94122bbe MM |
2306 | bt_cb(skb)->control.sar = sar; |
2307 | __skb_queue_tail(seg_queue, skb); | |
2308 | ||
2309 | len -= pdu_len; | |
2310 | if (sdu_len) { | |
2311 | sdu_len = 0; | |
2312 | pdu_len += L2CAP_SDULEN_SIZE; | |
2313 | } | |
2314 | ||
2315 | if (len <= pdu_len) { | |
2316 | sar = L2CAP_SAR_END; | |
2317 | pdu_len = len; | |
2318 | } else { | |
2319 | sar = L2CAP_SAR_CONTINUE; | |
2320 | } | |
c74e560c | 2321 | } |
c74e560c | 2322 | |
f0f62799 | 2323 | return 0; |
c74e560c GP |
2324 | } |
2325 | ||
5e59b791 | 2326 | int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, |
2d792818 | 2327 | u32 priority) |
9a91a04a | 2328 | { |
9a91a04a | 2329 | struct sk_buff *skb; |
9a91a04a | 2330 | int err; |
94122bbe | 2331 | struct sk_buff_head seg_queue; |
9a91a04a GP |
2332 | |
2333 | /* Connectionless channel */ | |
715ec005 | 2334 | if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { |
5e59b791 | 2335 | skb = l2cap_create_connless_pdu(chan, msg, len, priority); |
9a91a04a GP |
2336 | if (IS_ERR(skb)) |
2337 | return PTR_ERR(skb); | |
2338 | ||
2339 | l2cap_do_send(chan, skb); | |
2340 | return len; | |
2341 | } | |
2342 | ||
2343 | switch (chan->mode) { | |
2344 | case L2CAP_MODE_BASIC: | |
2345 | /* Check outgoing MTU */ | |
2346 | if (len > chan->omtu) | |
2347 | return -EMSGSIZE; | |
2348 | ||
2349 | /* Create a basic PDU */ | |
5e59b791 | 2350 | skb = l2cap_create_basic_pdu(chan, msg, len, priority); |
9a91a04a GP |
2351 | if (IS_ERR(skb)) |
2352 | return PTR_ERR(skb); | |
2353 | ||
2354 | l2cap_do_send(chan, skb); | |
2355 | err = len; | |
2356 | break; | |
2357 | ||
2358 | case L2CAP_MODE_ERTM: | |
2359 | case L2CAP_MODE_STREAMING: | |
94122bbe MM |
2360 | /* Check outgoing MTU */ |
2361 | if (len > chan->omtu) { | |
2362 | err = -EMSGSIZE; | |
2363 | break; | |
2364 | } | |
9a91a04a | 2365 | |
94122bbe | 2366 | __skb_queue_head_init(&seg_queue); |
9a91a04a | 2367 | |
94122bbe MM |
2368 | /* Do segmentation before calling in to the state machine, |
2369 | * since it's possible to block while waiting for memory | |
2370 | * allocation. | |
2371 | */ | |
2372 | err = l2cap_segment_sdu(chan, &seg_queue, msg, len); | |
9a91a04a | 2373 | |
94122bbe MM |
2374 | /* The channel could have been closed while segmenting, |
2375 | * check that it is still connected. | |
2376 | */ | |
2377 | if (chan->state != BT_CONNECTED) { | |
2378 | __skb_queue_purge(&seg_queue); | |
2379 | err = -ENOTCONN; | |
9a91a04a GP |
2380 | } |
2381 | ||
94122bbe | 2382 | if (err) |
9a91a04a | 2383 | break; |
9a91a04a | 2384 | |
3733937d | 2385 | if (chan->mode == L2CAP_MODE_ERTM) |
d660366d | 2386 | l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST); |
3733937d | 2387 | else |
d660366d | 2388 | l2cap_streaming_send(chan, &seg_queue); |
9a91a04a | 2389 | |
d660366d | 2390 | err = len; |
9a91a04a | 2391 | |
94122bbe MM |
2392 | /* If the skbs were not queued for sending, they'll still be in |
2393 | * seg_queue and need to be purged. | |
2394 | */ | |
2395 | __skb_queue_purge(&seg_queue); | |
9a91a04a GP |
2396 | break; |
2397 | ||
2398 | default: | |
2399 | BT_DBG("bad state %1.1x", chan->mode); | |
2400 | err = -EBADFD; | |
2401 | } | |
2402 | ||
2403 | return err; | |
2404 | } | |
2405 | ||
d2a7ac5d MM |
2406 | static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq) |
2407 | { | |
bed68bde MM |
2408 | struct l2cap_ctrl control; |
2409 | u16 seq; | |
2410 | ||
b4400672 | 2411 | BT_DBG("chan %p, txseq %u", chan, txseq); |
bed68bde MM |
2412 | |
2413 | memset(&control, 0, sizeof(control)); | |
2414 | control.sframe = 1; | |
2415 | control.super = L2CAP_SUPER_SREJ; | |
2416 | ||
2417 | for (seq = chan->expected_tx_seq; seq != txseq; | |
2418 | seq = __next_seq(chan, seq)) { | |
2419 | if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) { | |
2420 | control.reqseq = seq; | |
2421 | l2cap_send_sframe(chan, &control); | |
2422 | l2cap_seq_list_append(&chan->srej_list, seq); | |
2423 | } | |
2424 | } | |
2425 | ||
2426 | chan->expected_tx_seq = __next_seq(chan, txseq); | |
d2a7ac5d MM |
2427 | } |
2428 | ||
2429 | static void l2cap_send_srej_tail(struct l2cap_chan *chan) | |
2430 | { | |
bed68bde MM |
2431 | struct l2cap_ctrl control; |
2432 | ||
2433 | BT_DBG("chan %p", chan); | |
2434 | ||
2435 | if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR) | |
2436 | return; | |
2437 | ||
2438 | memset(&control, 0, sizeof(control)); | |
2439 | control.sframe = 1; | |
2440 | control.super = L2CAP_SUPER_SREJ; | |
2441 | control.reqseq = chan->srej_list.tail; | |
2442 | l2cap_send_sframe(chan, &control); | |
d2a7ac5d MM |
2443 | } |
2444 | ||
2445 | static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq) | |
2446 | { | |
bed68bde MM |
2447 | struct l2cap_ctrl control; |
2448 | u16 initial_head; | |
2449 | u16 seq; | |
2450 | ||
b4400672 | 2451 | BT_DBG("chan %p, txseq %u", chan, txseq); |
bed68bde MM |
2452 | |
2453 | memset(&control, 0, sizeof(control)); | |
2454 | control.sframe = 1; | |
2455 | control.super = L2CAP_SUPER_SREJ; | |
2456 | ||
2457 | /* Capture initial list head to allow only one pass through the list. */ | |
2458 | initial_head = chan->srej_list.head; | |
2459 | ||
2460 | do { | |
2461 | seq = l2cap_seq_list_pop(&chan->srej_list); | |
2462 | if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR) | |
2463 | break; | |
2464 | ||
2465 | control.reqseq = seq; | |
2466 | l2cap_send_sframe(chan, &control); | |
2467 | l2cap_seq_list_append(&chan->srej_list, seq); | |
2468 | } while (chan->srej_list.head != initial_head); | |
d2a7ac5d MM |
2469 | } |
2470 | ||
608bcc6d MM |
2471 | static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq) |
2472 | { | |
2473 | struct sk_buff *acked_skb; | |
2474 | u16 ackseq; | |
2475 | ||
b4400672 | 2476 | BT_DBG("chan %p, reqseq %u", chan, reqseq); |
608bcc6d MM |
2477 | |
2478 | if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq) | |
2479 | return; | |
2480 | ||
b4400672 | 2481 | BT_DBG("expected_ack_seq %u, unacked_frames %u", |
608bcc6d MM |
2482 | chan->expected_ack_seq, chan->unacked_frames); |
2483 | ||
2484 | for (ackseq = chan->expected_ack_seq; ackseq != reqseq; | |
2485 | ackseq = __next_seq(chan, ackseq)) { | |
2486 | ||
2487 | acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq); | |
2488 | if (acked_skb) { | |
2489 | skb_unlink(acked_skb, &chan->tx_q); | |
2490 | kfree_skb(acked_skb); | |
2491 | chan->unacked_frames--; | |
2492 | } | |
2493 | } | |
2494 | ||
2495 | chan->expected_ack_seq = reqseq; | |
2496 | ||
2497 | if (chan->unacked_frames == 0) | |
2498 | __clear_retrans_timer(chan); | |
2499 | ||
b4400672 | 2500 | BT_DBG("unacked_frames %u", chan->unacked_frames); |
608bcc6d MM |
2501 | } |
2502 | ||
2503 | static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan) | |
2504 | { | |
2505 | BT_DBG("chan %p", chan); | |
2506 | ||
2507 | chan->expected_tx_seq = chan->buffer_seq; | |
2508 | l2cap_seq_list_clear(&chan->srej_list); | |
2509 | skb_queue_purge(&chan->srej_q); | |
2510 | chan->rx_state = L2CAP_RX_STATE_RECV; | |
2511 | } | |
2512 | ||
d660366d GP |
2513 | static void l2cap_tx_state_xmit(struct l2cap_chan *chan, |
2514 | struct l2cap_ctrl *control, | |
2515 | struct sk_buff_head *skbs, u8 event) | |
608bcc6d | 2516 | { |
608bcc6d MM |
2517 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
2518 | event); | |
2519 | ||
2520 | switch (event) { | |
2521 | case L2CAP_EV_DATA_REQUEST: | |
2522 | if (chan->tx_send_head == NULL) | |
2523 | chan->tx_send_head = skb_peek(skbs); | |
2524 | ||
2525 | skb_queue_splice_tail_init(skbs, &chan->tx_q); | |
2526 | l2cap_ertm_send(chan); | |
2527 | break; | |
2528 | case L2CAP_EV_LOCAL_BUSY_DETECTED: | |
2529 | BT_DBG("Enter LOCAL_BUSY"); | |
2530 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); | |
2531 | ||
2532 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { | |
2533 | /* The SREJ_SENT state must be aborted if we are to | |
2534 | * enter the LOCAL_BUSY state. | |
2535 | */ | |
2536 | l2cap_abort_rx_srej_sent(chan); | |
2537 | } | |
2538 | ||
2539 | l2cap_send_ack(chan); | |
2540 | ||
2541 | break; | |
2542 | case L2CAP_EV_LOCAL_BUSY_CLEAR: | |
2543 | BT_DBG("Exit LOCAL_BUSY"); | |
2544 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); | |
2545 | ||
2546 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { | |
2547 | struct l2cap_ctrl local_control; | |
2548 | ||
2549 | memset(&local_control, 0, sizeof(local_control)); | |
2550 | local_control.sframe = 1; | |
2551 | local_control.super = L2CAP_SUPER_RR; | |
2552 | local_control.poll = 1; | |
2553 | local_control.reqseq = chan->buffer_seq; | |
a67d7f6f | 2554 | l2cap_send_sframe(chan, &local_control); |
608bcc6d MM |
2555 | |
2556 | chan->retry_count = 1; | |
2557 | __set_monitor_timer(chan); | |
2558 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; | |
2559 | } | |
2560 | break; | |
2561 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: | |
2562 | l2cap_process_reqseq(chan, control->reqseq); | |
2563 | break; | |
2564 | case L2CAP_EV_EXPLICIT_POLL: | |
2565 | l2cap_send_rr_or_rnr(chan, 1); | |
2566 | chan->retry_count = 1; | |
2567 | __set_monitor_timer(chan); | |
2568 | __clear_ack_timer(chan); | |
2569 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; | |
2570 | break; | |
2571 | case L2CAP_EV_RETRANS_TO: | |
2572 | l2cap_send_rr_or_rnr(chan, 1); | |
2573 | chan->retry_count = 1; | |
2574 | __set_monitor_timer(chan); | |
2575 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; | |
2576 | break; | |
2577 | case L2CAP_EV_RECV_FBIT: | |
2578 | /* Nothing to process */ | |
2579 | break; | |
2580 | default: | |
2581 | break; | |
2582 | } | |
608bcc6d MM |
2583 | } |
2584 | ||
d660366d GP |
2585 | static void l2cap_tx_state_wait_f(struct l2cap_chan *chan, |
2586 | struct l2cap_ctrl *control, | |
2587 | struct sk_buff_head *skbs, u8 event) | |
608bcc6d | 2588 | { |
608bcc6d MM |
2589 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
2590 | event); | |
2591 | ||
2592 | switch (event) { | |
2593 | case L2CAP_EV_DATA_REQUEST: | |
2594 | if (chan->tx_send_head == NULL) | |
2595 | chan->tx_send_head = skb_peek(skbs); | |
2596 | /* Queue data, but don't send. */ | |
2597 | skb_queue_splice_tail_init(skbs, &chan->tx_q); | |
2598 | break; | |
2599 | case L2CAP_EV_LOCAL_BUSY_DETECTED: | |
2600 | BT_DBG("Enter LOCAL_BUSY"); | |
2601 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); | |
2602 | ||
2603 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { | |
2604 | /* The SREJ_SENT state must be aborted if we are to | |
2605 | * enter the LOCAL_BUSY state. | |
2606 | */ | |
2607 | l2cap_abort_rx_srej_sent(chan); | |
2608 | } | |
2609 | ||
2610 | l2cap_send_ack(chan); | |
2611 | ||
2612 | break; | |
2613 | case L2CAP_EV_LOCAL_BUSY_CLEAR: | |
2614 | BT_DBG("Exit LOCAL_BUSY"); | |
2615 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); | |
2616 | ||
2617 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { | |
2618 | struct l2cap_ctrl local_control; | |
2619 | memset(&local_control, 0, sizeof(local_control)); | |
2620 | local_control.sframe = 1; | |
2621 | local_control.super = L2CAP_SUPER_RR; | |
2622 | local_control.poll = 1; | |
2623 | local_control.reqseq = chan->buffer_seq; | |
a67d7f6f | 2624 | l2cap_send_sframe(chan, &local_control); |
608bcc6d MM |
2625 | |
2626 | chan->retry_count = 1; | |
2627 | __set_monitor_timer(chan); | |
2628 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; | |
2629 | } | |
2630 | break; | |
2631 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: | |
2632 | l2cap_process_reqseq(chan, control->reqseq); | |
2633 | ||
2634 | /* Fall through */ | |
2635 | ||
2636 | case L2CAP_EV_RECV_FBIT: | |
2637 | if (control && control->final) { | |
2638 | __clear_monitor_timer(chan); | |
2639 | if (chan->unacked_frames > 0) | |
2640 | __set_retrans_timer(chan); | |
2641 | chan->retry_count = 0; | |
2642 | chan->tx_state = L2CAP_TX_STATE_XMIT; | |
2643 | BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state); | |
2644 | } | |
2645 | break; | |
2646 | case L2CAP_EV_EXPLICIT_POLL: | |
2647 | /* Ignore */ | |
2648 | break; | |
2649 | case L2CAP_EV_MONITOR_TO: | |
2650 | if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) { | |
2651 | l2cap_send_rr_or_rnr(chan, 1); | |
2652 | __set_monitor_timer(chan); | |
2653 | chan->retry_count++; | |
2654 | } else { | |
2655 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); | |
2656 | } | |
2657 | break; | |
2658 | default: | |
2659 | break; | |
2660 | } | |
608bcc6d MM |
2661 | } |
2662 | ||
d660366d GP |
2663 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
2664 | struct sk_buff_head *skbs, u8 event) | |
608bcc6d | 2665 | { |
608bcc6d MM |
2666 | BT_DBG("chan %p, control %p, skbs %p, event %d, state %d", |
2667 | chan, control, skbs, event, chan->tx_state); | |
2668 | ||
2669 | switch (chan->tx_state) { | |
2670 | case L2CAP_TX_STATE_XMIT: | |
d660366d | 2671 | l2cap_tx_state_xmit(chan, control, skbs, event); |
608bcc6d MM |
2672 | break; |
2673 | case L2CAP_TX_STATE_WAIT_F: | |
d660366d | 2674 | l2cap_tx_state_wait_f(chan, control, skbs, event); |
608bcc6d MM |
2675 | break; |
2676 | default: | |
2677 | /* Ignore event */ | |
2678 | break; | |
2679 | } | |
608bcc6d MM |
2680 | } |
2681 | ||
4b51dae9 MM |
2682 | static void l2cap_pass_to_tx(struct l2cap_chan *chan, |
2683 | struct l2cap_ctrl *control) | |
2684 | { | |
2685 | BT_DBG("chan %p, control %p", chan, control); | |
401bb1f7 | 2686 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT); |
4b51dae9 MM |
2687 | } |
2688 | ||
f80842a8 MM |
2689 | static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan, |
2690 | struct l2cap_ctrl *control) | |
2691 | { | |
2692 | BT_DBG("chan %p, control %p", chan, control); | |
401bb1f7 | 2693 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT); |
f80842a8 MM |
2694 | } |
2695 | ||
1da177e4 LT |
2696 | /* Copy frame to all raw sockets on that connection */ |
2697 | static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) | |
2698 | { | |
1da177e4 | 2699 | struct sk_buff *nskb; |
48454079 | 2700 | struct l2cap_chan *chan; |
1da177e4 LT |
2701 | |
2702 | BT_DBG("conn %p", conn); | |
2703 | ||
3df91ea2 | 2704 | mutex_lock(&conn->chan_lock); |
3d57dc68 | 2705 | |
3df91ea2 | 2706 | list_for_each_entry(chan, &conn->chan_l, list) { |
48454079 | 2707 | struct sock *sk = chan->sk; |
715ec005 | 2708 | if (chan->chan_type != L2CAP_CHAN_RAW) |
1da177e4 LT |
2709 | continue; |
2710 | ||
2711 | /* Don't send frame to the socket it came from */ | |
2712 | if (skb->sk == sk) | |
2713 | continue; | |
8bcde1f2 | 2714 | nskb = skb_clone(skb, GFP_KERNEL); |
af05b30b | 2715 | if (!nskb) |
1da177e4 LT |
2716 | continue; |
2717 | ||
80b98027 | 2718 | if (chan->ops->recv(chan, nskb)) |
1da177e4 LT |
2719 | kfree_skb(nskb); |
2720 | } | |
3d57dc68 | 2721 | |
3df91ea2 | 2722 | mutex_unlock(&conn->chan_lock); |
1da177e4 LT |
2723 | } |
2724 | ||
2725 | /* ---- L2CAP signalling commands ---- */ | |
b4400672 AE |
2726 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code, |
2727 | u8 ident, u16 dlen, void *data) | |
1da177e4 LT |
2728 | { |
2729 | struct sk_buff *skb, **frag; | |
2730 | struct l2cap_cmd_hdr *cmd; | |
2731 | struct l2cap_hdr *lh; | |
2732 | int len, count; | |
2733 | ||
b4400672 AE |
2734 | BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u", |
2735 | conn, code, ident, dlen); | |
1da177e4 LT |
2736 | |
2737 | len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; | |
2738 | count = min_t(unsigned int, conn->mtu, len); | |
2739 | ||
8bcde1f2 | 2740 | skb = bt_skb_alloc(count, GFP_KERNEL); |
1da177e4 LT |
2741 | if (!skb) |
2742 | return NULL; | |
2743 | ||
2744 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); | |
aca3192c | 2745 | lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); |
3300d9a9 CT |
2746 | |
2747 | if (conn->hcon->type == LE_LINK) | |
ac73498c | 2748 | lh->cid = __constant_cpu_to_le16(L2CAP_CID_LE_SIGNALING); |
3300d9a9 | 2749 | else |
ac73498c | 2750 | lh->cid = __constant_cpu_to_le16(L2CAP_CID_SIGNALING); |
1da177e4 LT |
2751 | |
2752 | cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); | |
2753 | cmd->code = code; | |
2754 | cmd->ident = ident; | |
aca3192c | 2755 | cmd->len = cpu_to_le16(dlen); |
1da177e4 LT |
2756 | |
2757 | if (dlen) { | |
2758 | count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; | |
2759 | memcpy(skb_put(skb, count), data, count); | |
2760 | data += count; | |
2761 | } | |
2762 | ||
2763 | len -= skb->len; | |
2764 | ||
2765 | /* Continuation fragments (no L2CAP header) */ | |
2766 | frag = &skb_shinfo(skb)->frag_list; | |
2767 | while (len) { | |
2768 | count = min_t(unsigned int, conn->mtu, len); | |
2769 | ||
8bcde1f2 | 2770 | *frag = bt_skb_alloc(count, GFP_KERNEL); |
1da177e4 LT |
2771 | if (!*frag) |
2772 | goto fail; | |
2773 | ||
2774 | memcpy(skb_put(*frag, count), data, count); | |
2775 | ||
2776 | len -= count; | |
2777 | data += count; | |
2778 | ||
2779 | frag = &(*frag)->next; | |
2780 | } | |
2781 | ||
2782 | return skb; | |
2783 | ||
2784 | fail: | |
2785 | kfree_skb(skb); | |
2786 | return NULL; | |
2787 | } | |
2788 | ||
2d792818 GP |
2789 | static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, |
2790 | unsigned long *val) | |
1da177e4 LT |
2791 | { |
2792 | struct l2cap_conf_opt *opt = *ptr; | |
2793 | int len; | |
2794 | ||
2795 | len = L2CAP_CONF_OPT_SIZE + opt->len; | |
2796 | *ptr += len; | |
2797 | ||
2798 | *type = opt->type; | |
2799 | *olen = opt->len; | |
2800 | ||
2801 | switch (opt->len) { | |
2802 | case 1: | |
2803 | *val = *((u8 *) opt->val); | |
2804 | break; | |
2805 | ||
2806 | case 2: | |
bfaaeb3e | 2807 | *val = get_unaligned_le16(opt->val); |
1da177e4 LT |
2808 | break; |
2809 | ||
2810 | case 4: | |
bfaaeb3e | 2811 | *val = get_unaligned_le32(opt->val); |
1da177e4 LT |
2812 | break; |
2813 | ||
2814 | default: | |
2815 | *val = (unsigned long) opt->val; | |
2816 | break; | |
2817 | } | |
2818 | ||
b4400672 | 2819 | BT_DBG("type 0x%2.2x len %u val 0x%lx", *type, opt->len, *val); |
1da177e4 LT |
2820 | return len; |
2821 | } | |
2822 | ||
1da177e4 LT |
2823 | static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) |
2824 | { | |
2825 | struct l2cap_conf_opt *opt = *ptr; | |
2826 | ||
b4400672 | 2827 | BT_DBG("type 0x%2.2x len %u val 0x%lx", type, len, val); |
1da177e4 LT |
2828 | |
2829 | opt->type = type; | |
2830 | opt->len = len; | |
2831 | ||
2832 | switch (len) { | |
2833 | case 1: | |
2834 | *((u8 *) opt->val) = val; | |
2835 | break; | |
2836 | ||
2837 | case 2: | |
4f8b691c | 2838 | put_unaligned_le16(val, opt->val); |
1da177e4 LT |
2839 | break; |
2840 | ||
2841 | case 4: | |
4f8b691c | 2842 | put_unaligned_le32(val, opt->val); |
1da177e4 LT |
2843 | break; |
2844 | ||
2845 | default: | |
2846 | memcpy(opt->val, (void *) val, len); | |
2847 | break; | |
2848 | } | |
2849 | ||
2850 | *ptr += L2CAP_CONF_OPT_SIZE + len; | |
2851 | } | |
2852 | ||
f89cef09 AE |
2853 | static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) |
2854 | { | |
2855 | struct l2cap_conf_efs efs; | |
2856 | ||
1ec918ce | 2857 | switch (chan->mode) { |
f89cef09 AE |
2858 | case L2CAP_MODE_ERTM: |
2859 | efs.id = chan->local_id; | |
2860 | efs.stype = chan->local_stype; | |
2861 | efs.msdu = cpu_to_le16(chan->local_msdu); | |
2862 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); | |
ac73498c | 2863 | efs.acc_lat = __constant_cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); |
8936fa6d | 2864 | efs.flush_to = __constant_cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO); |
f89cef09 AE |
2865 | break; |
2866 | ||
2867 | case L2CAP_MODE_STREAMING: | |
2868 | efs.id = 1; | |
2869 | efs.stype = L2CAP_SERV_BESTEFFORT; | |
2870 | efs.msdu = cpu_to_le16(chan->local_msdu); | |
2871 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); | |
2872 | efs.acc_lat = 0; | |
2873 | efs.flush_to = 0; | |
2874 | break; | |
2875 | ||
2876 | default: | |
2877 | return; | |
2878 | } | |
2879 | ||
2880 | l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), | |
8936fa6d | 2881 | (unsigned long) &efs); |
f89cef09 AE |
2882 | } |
2883 | ||
721c4181 | 2884 | static void l2cap_ack_timeout(struct work_struct *work) |
c1b4f43b | 2885 | { |
721c4181 | 2886 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
0362520b MM |
2887 | ack_timer.work); |
2888 | u16 frames_to_ack; | |
c1b4f43b | 2889 | |
2fb9b3d4 GP |
2890 | BT_DBG("chan %p", chan); |
2891 | ||
6be36555 AE |
2892 | l2cap_chan_lock(chan); |
2893 | ||
0362520b MM |
2894 | frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
2895 | chan->last_acked_seq); | |
6be36555 | 2896 | |
0362520b MM |
2897 | if (frames_to_ack) |
2898 | l2cap_send_rr_or_rnr(chan, 0); | |
09bfb2ee | 2899 | |
0362520b | 2900 | l2cap_chan_unlock(chan); |
09bfb2ee | 2901 | l2cap_chan_put(chan); |
c1b4f43b GP |
2902 | } |
2903 | ||
466f8004 | 2904 | int l2cap_ertm_init(struct l2cap_chan *chan) |
0565c1c2 | 2905 | { |
3c588192 MM |
2906 | int err; |
2907 | ||
105bdf9e MM |
2908 | chan->next_tx_seq = 0; |
2909 | chan->expected_tx_seq = 0; | |
42e5c802 | 2910 | chan->expected_ack_seq = 0; |
6a026610 | 2911 | chan->unacked_frames = 0; |
42e5c802 | 2912 | chan->buffer_seq = 0; |
6a026610 | 2913 | chan->frames_sent = 0; |
105bdf9e MM |
2914 | chan->last_acked_seq = 0; |
2915 | chan->sdu = NULL; | |
2916 | chan->sdu_last_frag = NULL; | |
2917 | chan->sdu_len = 0; | |
2918 | ||
d34c34fb MM |
2919 | skb_queue_head_init(&chan->tx_q); |
2920 | ||
08333283 MM |
2921 | chan->local_amp_id = 0; |
2922 | chan->move_id = 0; | |
2923 | chan->move_state = L2CAP_MOVE_STABLE; | |
2924 | chan->move_role = L2CAP_MOVE_ROLE_NONE; | |
2925 | ||
105bdf9e MM |
2926 | if (chan->mode != L2CAP_MODE_ERTM) |
2927 | return 0; | |
2928 | ||
2929 | chan->rx_state = L2CAP_RX_STATE_RECV; | |
2930 | chan->tx_state = L2CAP_TX_STATE_XMIT; | |
0565c1c2 | 2931 | |
721c4181 GP |
2932 | INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); |
2933 | INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); | |
2934 | INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); | |
0565c1c2 | 2935 | |
f1c6775b | 2936 | skb_queue_head_init(&chan->srej_q); |
1890d36b | 2937 | |
3c588192 MM |
2938 | err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win); |
2939 | if (err < 0) | |
2940 | return err; | |
2941 | ||
9dc9affc MM |
2942 | err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win); |
2943 | if (err < 0) | |
2944 | l2cap_seq_list_free(&chan->srej_list); | |
2945 | ||
2946 | return err; | |
0565c1c2 GP |
2947 | } |
2948 | ||
f2fcfcd6 GP |
2949 | static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) |
2950 | { | |
2951 | switch (mode) { | |
2952 | case L2CAP_MODE_STREAMING: | |
2953 | case L2CAP_MODE_ERTM: | |
2954 | if (l2cap_mode_supported(mode, remote_feat_mask)) | |
2955 | return mode; | |
2956 | /* fall through */ | |
2957 | default: | |
2958 | return L2CAP_MODE_BASIC; | |
2959 | } | |
2960 | } | |
2961 | ||
6327eb98 AE |
2962 | static inline bool __l2cap_ews_supported(struct l2cap_chan *chan) |
2963 | { | |
2964 | return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; | |
2965 | } | |
2966 | ||
f89cef09 AE |
2967 | static inline bool __l2cap_efs_supported(struct l2cap_chan *chan) |
2968 | { | |
2969 | return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW; | |
2970 | } | |
2971 | ||
36c86c85 MM |
2972 | static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan, |
2973 | struct l2cap_conf_rfc *rfc) | |
2974 | { | |
2975 | if (chan->local_amp_id && chan->hs_hcon) { | |
2976 | u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to; | |
2977 | ||
2978 | /* Class 1 devices have must have ERTM timeouts | |
2979 | * exceeding the Link Supervision Timeout. The | |
2980 | * default Link Supervision Timeout for AMP | |
2981 | * controllers is 10 seconds. | |
2982 | * | |
2983 | * Class 1 devices use 0xffffffff for their | |
2984 | * best-effort flush timeout, so the clamping logic | |
2985 | * will result in a timeout that meets the above | |
2986 | * requirement. ERTM timeouts are 16-bit values, so | |
2987 | * the maximum timeout is 65.535 seconds. | |
2988 | */ | |
2989 | ||
2990 | /* Convert timeout to milliseconds and round */ | |
2991 | ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000); | |
2992 | ||
2993 | /* This is the recommended formula for class 2 devices | |
2994 | * that start ERTM timers when packets are sent to the | |
2995 | * controller. | |
2996 | */ | |
2997 | ertm_to = 3 * ertm_to + 500; | |
2998 | ||
2999 | if (ertm_to > 0xffff) | |
3000 | ertm_to = 0xffff; | |
3001 | ||
3002 | rfc->retrans_timeout = cpu_to_le16((u16) ertm_to); | |
3003 | rfc->monitor_timeout = rfc->retrans_timeout; | |
3004 | } else { | |
3005 | rfc->retrans_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); | |
3006 | rfc->monitor_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); | |
3007 | } | |
3008 | } | |
3009 | ||
6327eb98 AE |
3010 | static inline void l2cap_txwin_setup(struct l2cap_chan *chan) |
3011 | { | |
3012 | if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && | |
2d792818 | 3013 | __l2cap_ews_supported(chan)) { |
6327eb98 AE |
3014 | /* use extended control field */ |
3015 | set_bit(FLAG_EXT_CTRL, &chan->flags); | |
836be934 AE |
3016 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
3017 | } else { | |
6327eb98 | 3018 | chan->tx_win = min_t(u16, chan->tx_win, |
2d792818 | 3019 | L2CAP_DEFAULT_TX_WINDOW); |
836be934 AE |
3020 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
3021 | } | |
c20f8e35 | 3022 | chan->ack_win = chan->tx_win; |
6327eb98 AE |
3023 | } |
3024 | ||
710f9b0a | 3025 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) |
1da177e4 | 3026 | { |
1da177e4 | 3027 | struct l2cap_conf_req *req = data; |
0c1bc5c6 | 3028 | struct l2cap_conf_rfc rfc = { .mode = chan->mode }; |
1da177e4 | 3029 | void *ptr = req->data; |
c8f79162 | 3030 | u16 size; |
1da177e4 | 3031 | |
49208c9c | 3032 | BT_DBG("chan %p", chan); |
1da177e4 | 3033 | |
73ffa904 | 3034 | if (chan->num_conf_req || chan->num_conf_rsp) |
f2fcfcd6 GP |
3035 | goto done; |
3036 | ||
0c1bc5c6 | 3037 | switch (chan->mode) { |
f2fcfcd6 GP |
3038 | case L2CAP_MODE_STREAMING: |
3039 | case L2CAP_MODE_ERTM: | |
c1360a1c | 3040 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) |
85eb53c6 | 3041 | break; |
85eb53c6 | 3042 | |
f89cef09 AE |
3043 | if (__l2cap_efs_supported(chan)) |
3044 | set_bit(FLAG_EFS_ENABLE, &chan->flags); | |
3045 | ||
2ba13ed6 | 3046 | /* fall through */ |
f2fcfcd6 | 3047 | default: |
8c1d787b | 3048 | chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); |
f2fcfcd6 GP |
3049 | break; |
3050 | } | |
3051 | ||
3052 | done: | |
0c1bc5c6 GP |
3053 | if (chan->imtu != L2CAP_DEFAULT_MTU) |
3054 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); | |
7990681c | 3055 | |
0c1bc5c6 | 3056 | switch (chan->mode) { |
65c7c491 | 3057 | case L2CAP_MODE_BASIC: |
8c1d787b | 3058 | if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && |
2d792818 | 3059 | !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) |
63406504 GP |
3060 | break; |
3061 | ||
62547752 GP |
3062 | rfc.mode = L2CAP_MODE_BASIC; |
3063 | rfc.txwin_size = 0; | |
3064 | rfc.max_transmit = 0; | |
3065 | rfc.retrans_timeout = 0; | |
3066 | rfc.monitor_timeout = 0; | |
3067 | rfc.max_pdu_size = 0; | |
3068 | ||
63406504 | 3069 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
2d792818 | 3070 | (unsigned long) &rfc); |
65c7c491 MH |
3071 | break; |
3072 | ||
3073 | case L2CAP_MODE_ERTM: | |
3074 | rfc.mode = L2CAP_MODE_ERTM; | |
47d1ec61 | 3075 | rfc.max_transmit = chan->max_tx; |
36c86c85 MM |
3076 | |
3077 | __l2cap_set_ertm_timeouts(chan, &rfc); | |
c8f79162 AE |
3078 | |
3079 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - | |
2d792818 GP |
3080 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
3081 | L2CAP_FCS_SIZE); | |
c8f79162 | 3082 | rfc.max_pdu_size = cpu_to_le16(size); |
f2fcfcd6 | 3083 | |
6327eb98 AE |
3084 | l2cap_txwin_setup(chan); |
3085 | ||
3086 | rfc.txwin_size = min_t(u16, chan->tx_win, | |
2d792818 | 3087 | L2CAP_DEFAULT_TX_WINDOW); |
f2fcfcd6 | 3088 | |
63406504 | 3089 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
2d792818 | 3090 | (unsigned long) &rfc); |
63406504 | 3091 | |
f89cef09 AE |
3092 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
3093 | l2cap_add_opt_efs(&ptr, chan); | |
3094 | ||
8c1d787b | 3095 | if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) |
fcc203c3 GP |
3096 | break; |
3097 | ||
47d1ec61 | 3098 | if (chan->fcs == L2CAP_FCS_NONE || |
2d792818 | 3099 | test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { |
47d1ec61 GP |
3100 | chan->fcs = L2CAP_FCS_NONE; |
3101 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); | |
fcc203c3 | 3102 | } |
6327eb98 AE |
3103 | |
3104 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) | |
3105 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, | |
2d792818 | 3106 | chan->tx_win); |
f2fcfcd6 GP |
3107 | break; |
3108 | ||
3109 | case L2CAP_MODE_STREAMING: | |
273759e2 | 3110 | l2cap_txwin_setup(chan); |
f2fcfcd6 GP |
3111 | rfc.mode = L2CAP_MODE_STREAMING; |
3112 | rfc.txwin_size = 0; | |
3113 | rfc.max_transmit = 0; | |
3114 | rfc.retrans_timeout = 0; | |
3115 | rfc.monitor_timeout = 0; | |
c8f79162 AE |
3116 | |
3117 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - | |
2d792818 GP |
3118 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
3119 | L2CAP_FCS_SIZE); | |
c8f79162 | 3120 | rfc.max_pdu_size = cpu_to_le16(size); |
65c7c491 | 3121 | |
63406504 | 3122 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
2d792818 | 3123 | (unsigned long) &rfc); |
63406504 | 3124 | |
f89cef09 AE |
3125 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
3126 | l2cap_add_opt_efs(&ptr, chan); | |
3127 | ||
8c1d787b | 3128 | if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) |
fcc203c3 GP |
3129 | break; |
3130 | ||
47d1ec61 | 3131 | if (chan->fcs == L2CAP_FCS_NONE || |
2d792818 | 3132 | test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { |
47d1ec61 GP |
3133 | chan->fcs = L2CAP_FCS_NONE; |
3134 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); | |
fcc203c3 | 3135 | } |
65c7c491 MH |
3136 | break; |
3137 | } | |
1da177e4 | 3138 | |
fe4128e0 | 3139 | req->dcid = cpu_to_le16(chan->dcid); |
59e54bd1 | 3140 | req->flags = __constant_cpu_to_le16(0); |
1da177e4 LT |
3141 | |
3142 | return ptr - data; | |
3143 | } | |
3144 | ||
73ffa904 | 3145 | static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) |
1da177e4 | 3146 | { |
5dee9e7c MH |
3147 | struct l2cap_conf_rsp *rsp = data; |
3148 | void *ptr = rsp->data; | |
73ffa904 GP |
3149 | void *req = chan->conf_req; |
3150 | int len = chan->conf_len; | |
5dee9e7c MH |
3151 | int type, hint, olen; |
3152 | unsigned long val; | |
6464f35f | 3153 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
42dceae2 AE |
3154 | struct l2cap_conf_efs efs; |
3155 | u8 remote_efs = 0; | |
861d6882 | 3156 | u16 mtu = L2CAP_DEFAULT_MTU; |
5dee9e7c | 3157 | u16 result = L2CAP_CONF_SUCCESS; |
c8f79162 | 3158 | u16 size; |
1da177e4 | 3159 | |
73ffa904 | 3160 | BT_DBG("chan %p", chan); |
820ae1b8 | 3161 | |
5dee9e7c MH |
3162 | while (len >= L2CAP_CONF_OPT_SIZE) { |
3163 | len -= l2cap_get_conf_opt(&req, &type, &olen, &val); | |
1da177e4 | 3164 | |
589d2746 | 3165 | hint = type & L2CAP_CONF_HINT; |
47ec1dcd | 3166 | type &= L2CAP_CONF_MASK; |
5dee9e7c MH |
3167 | |
3168 | switch (type) { | |
3169 | case L2CAP_CONF_MTU: | |
861d6882 | 3170 | mtu = val; |
5dee9e7c MH |
3171 | break; |
3172 | ||
3173 | case L2CAP_CONF_FLUSH_TO: | |
0c1bc5c6 | 3174 | chan->flush_to = val; |
5dee9e7c MH |
3175 | break; |
3176 | ||
3177 | case L2CAP_CONF_QOS: | |
3178 | break; | |
3179 | ||
6464f35f MH |
3180 | case L2CAP_CONF_RFC: |
3181 | if (olen == sizeof(rfc)) | |
3182 | memcpy(&rfc, (void *) val, olen); | |
3183 | break; | |
3184 | ||
fcc203c3 GP |
3185 | case L2CAP_CONF_FCS: |
3186 | if (val == L2CAP_FCS_NONE) | |
c1360a1c | 3187 | set_bit(CONF_NO_FCS_RECV, &chan->conf_state); |
42dceae2 | 3188 | break; |
fcc203c3 | 3189 | |
42dceae2 AE |
3190 | case L2CAP_CONF_EFS: |
3191 | remote_efs = 1; | |
3192 | if (olen == sizeof(efs)) | |
3193 | memcpy(&efs, (void *) val, olen); | |
fcc203c3 GP |
3194 | break; |
3195 | ||
6327eb98 AE |
3196 | case L2CAP_CONF_EWS: |
3197 | if (!enable_hs) | |
3198 | return -ECONNREFUSED; | |
fcc203c3 | 3199 | |
6327eb98 AE |
3200 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
3201 | set_bit(CONF_EWS_RECV, &chan->conf_state); | |
836be934 | 3202 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
6327eb98 | 3203 | chan->remote_tx_win = val; |
fcc203c3 GP |
3204 | break; |
3205 | ||
5dee9e7c MH |
3206 | default: |
3207 | if (hint) | |
3208 | break; | |
3209 | ||
3210 | result = L2CAP_CONF_UNKNOWN; | |
3211 | *((u8 *) ptr++) = type; | |
3212 | break; | |
3213 | } | |
3214 | } | |
3215 | ||
73ffa904 | 3216 | if (chan->num_conf_rsp || chan->num_conf_req > 1) |
f2fcfcd6 GP |
3217 | goto done; |
3218 | ||
0c1bc5c6 | 3219 | switch (chan->mode) { |
f2fcfcd6 GP |
3220 | case L2CAP_MODE_STREAMING: |
3221 | case L2CAP_MODE_ERTM: | |
c1360a1c | 3222 | if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { |
0c1bc5c6 | 3223 | chan->mode = l2cap_select_mode(rfc.mode, |
2d792818 | 3224 | chan->conn->feat_mask); |
85eb53c6 GP |
3225 | break; |
3226 | } | |
3227 | ||
42dceae2 AE |
3228 | if (remote_efs) { |
3229 | if (__l2cap_efs_supported(chan)) | |
3230 | set_bit(FLAG_EFS_ENABLE, &chan->flags); | |
3231 | else | |
3232 | return -ECONNREFUSED; | |
3233 | } | |
3234 | ||
0c1bc5c6 | 3235 | if (chan->mode != rfc.mode) |
f2fcfcd6 | 3236 | return -ECONNREFUSED; |
742e519b | 3237 | |
f2fcfcd6 | 3238 | break; |
f2fcfcd6 GP |
3239 | } |
3240 | ||
3241 | done: | |
0c1bc5c6 | 3242 | if (chan->mode != rfc.mode) { |
f2fcfcd6 | 3243 | result = L2CAP_CONF_UNACCEPT; |
0c1bc5c6 | 3244 | rfc.mode = chan->mode; |
f2fcfcd6 | 3245 | |
73ffa904 | 3246 | if (chan->num_conf_rsp == 1) |
f2fcfcd6 GP |
3247 | return -ECONNREFUSED; |
3248 | ||
2d792818 GP |
3249 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
3250 | (unsigned long) &rfc); | |
f2fcfcd6 GP |
3251 | } |
3252 | ||
5dee9e7c MH |
3253 | if (result == L2CAP_CONF_SUCCESS) { |
3254 | /* Configure output options and let the other side know | |
3255 | * which ones we don't like. */ | |
3256 | ||
f2fcfcd6 GP |
3257 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
3258 | result = L2CAP_CONF_UNACCEPT; | |
3259 | else { | |
0c1bc5c6 | 3260 | chan->omtu = mtu; |
c1360a1c | 3261 | set_bit(CONF_MTU_DONE, &chan->conf_state); |
f2fcfcd6 | 3262 | } |
0c1bc5c6 | 3263 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); |
6464f35f | 3264 | |
42dceae2 AE |
3265 | if (remote_efs) { |
3266 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && | |
2d792818 GP |
3267 | efs.stype != L2CAP_SERV_NOTRAFIC && |
3268 | efs.stype != chan->local_stype) { | |
42dceae2 AE |
3269 | |
3270 | result = L2CAP_CONF_UNACCEPT; | |
3271 | ||
3272 | if (chan->num_conf_req >= 1) | |
3273 | return -ECONNREFUSED; | |
3274 | ||
3275 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, | |
2d792818 GP |
3276 | sizeof(efs), |
3277 | (unsigned long) &efs); | |
0e8b207e | 3278 | } else { |
3e6b3b95 | 3279 | /* Send PENDING Conf Rsp */ |
0e8b207e AE |
3280 | result = L2CAP_CONF_PENDING; |
3281 | set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); | |
42dceae2 AE |
3282 | } |
3283 | } | |
3284 | ||
f2fcfcd6 GP |
3285 | switch (rfc.mode) { |
3286 | case L2CAP_MODE_BASIC: | |
47d1ec61 | 3287 | chan->fcs = L2CAP_FCS_NONE; |
c1360a1c | 3288 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
f2fcfcd6 GP |
3289 | break; |
3290 | ||
3291 | case L2CAP_MODE_ERTM: | |
6327eb98 AE |
3292 | if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) |
3293 | chan->remote_tx_win = rfc.txwin_size; | |
3294 | else | |
3295 | rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; | |
86b1b263 | 3296 | |
2c03a7a4 | 3297 | chan->remote_max_tx = rfc.max_transmit; |
1c762159 | 3298 | |
c8f79162 | 3299 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
2d792818 GP |
3300 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
3301 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); | |
c8f79162 AE |
3302 | rfc.max_pdu_size = cpu_to_le16(size); |
3303 | chan->remote_mps = size; | |
f2fcfcd6 | 3304 | |
36c86c85 | 3305 | __l2cap_set_ertm_timeouts(chan, &rfc); |
f2fcfcd6 | 3306 | |
c1360a1c | 3307 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
68ae6639 GP |
3308 | |
3309 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, | |
2d792818 | 3310 | sizeof(rfc), (unsigned long) &rfc); |
68ae6639 | 3311 | |
42dceae2 AE |
3312 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
3313 | chan->remote_id = efs.id; | |
3314 | chan->remote_stype = efs.stype; | |
3315 | chan->remote_msdu = le16_to_cpu(efs.msdu); | |
3316 | chan->remote_flush_to = | |
2d792818 | 3317 | le32_to_cpu(efs.flush_to); |
42dceae2 | 3318 | chan->remote_acc_lat = |
2d792818 | 3319 | le32_to_cpu(efs.acc_lat); |
42dceae2 AE |
3320 | chan->remote_sdu_itime = |
3321 | le32_to_cpu(efs.sdu_itime); | |
3322 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, | |
2d792818 GP |
3323 | sizeof(efs), |
3324 | (unsigned long) &efs); | |
42dceae2 | 3325 | } |
f2fcfcd6 GP |
3326 | break; |
3327 | ||
3328 | case L2CAP_MODE_STREAMING: | |
c8f79162 | 3329 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
2d792818 GP |
3330 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
3331 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); | |
c8f79162 AE |
3332 | rfc.max_pdu_size = cpu_to_le16(size); |
3333 | chan->remote_mps = size; | |
f2fcfcd6 | 3334 | |
c1360a1c | 3335 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
68ae6639 | 3336 | |
2d792818 GP |
3337 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
3338 | (unsigned long) &rfc); | |
68ae6639 | 3339 | |
f2fcfcd6 GP |
3340 | break; |
3341 | ||
3342 | default: | |
5dee9e7c | 3343 | result = L2CAP_CONF_UNACCEPT; |
5dee9e7c | 3344 | |
6464f35f | 3345 | memset(&rfc, 0, sizeof(rfc)); |
0c1bc5c6 | 3346 | rfc.mode = chan->mode; |
f2fcfcd6 | 3347 | } |
6464f35f | 3348 | |
f2fcfcd6 | 3349 | if (result == L2CAP_CONF_SUCCESS) |
c1360a1c | 3350 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
f2fcfcd6 | 3351 | } |
fe4128e0 | 3352 | rsp->scid = cpu_to_le16(chan->dcid); |
5dee9e7c | 3353 | rsp->result = cpu_to_le16(result); |
59e54bd1 | 3354 | rsp->flags = __constant_cpu_to_le16(0); |
5dee9e7c MH |
3355 | |
3356 | return ptr - data; | |
1da177e4 LT |
3357 | } |
3358 | ||
2d792818 GP |
3359 | static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, |
3360 | void *data, u16 *result) | |
f2fcfcd6 | 3361 | { |
f2fcfcd6 GP |
3362 | struct l2cap_conf_req *req = data; |
3363 | void *ptr = req->data; | |
3364 | int type, olen; | |
3365 | unsigned long val; | |
36e999a8 | 3366 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
66af7aaf | 3367 | struct l2cap_conf_efs efs; |
f2fcfcd6 | 3368 | |
fe4128e0 | 3369 | BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data); |
f2fcfcd6 GP |
3370 | |
3371 | while (len >= L2CAP_CONF_OPT_SIZE) { | |
3372 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); | |
3373 | ||
3374 | switch (type) { | |
3375 | case L2CAP_CONF_MTU: | |
3376 | if (val < L2CAP_DEFAULT_MIN_MTU) { | |
3377 | *result = L2CAP_CONF_UNACCEPT; | |
0c1bc5c6 | 3378 | chan->imtu = L2CAP_DEFAULT_MIN_MTU; |
f2fcfcd6 | 3379 | } else |
0c1bc5c6 GP |
3380 | chan->imtu = val; |
3381 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); | |
f2fcfcd6 GP |
3382 | break; |
3383 | ||
3384 | case L2CAP_CONF_FLUSH_TO: | |
0c1bc5c6 | 3385 | chan->flush_to = val; |
f2fcfcd6 | 3386 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, |
2d792818 | 3387 | 2, chan->flush_to); |
f2fcfcd6 GP |
3388 | break; |
3389 | ||
3390 | case L2CAP_CONF_RFC: | |
3391 | if (olen == sizeof(rfc)) | |
3392 | memcpy(&rfc, (void *)val, olen); | |
3393 | ||
c1360a1c | 3394 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && |
2d792818 | 3395 | rfc.mode != chan->mode) |
f2fcfcd6 GP |
3396 | return -ECONNREFUSED; |
3397 | ||
47d1ec61 | 3398 | chan->fcs = 0; |
f2fcfcd6 GP |
3399 | |
3400 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, | |
2d792818 | 3401 | sizeof(rfc), (unsigned long) &rfc); |
f2fcfcd6 | 3402 | break; |
6327eb98 AE |
3403 | |
3404 | case L2CAP_CONF_EWS: | |
c20f8e35 | 3405 | chan->ack_win = min_t(u16, val, chan->ack_win); |
3e6b3b95 | 3406 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
c20f8e35 | 3407 | chan->tx_win); |
6327eb98 | 3408 | break; |
66af7aaf AE |
3409 | |
3410 | case L2CAP_CONF_EFS: | |
3411 | if (olen == sizeof(efs)) | |
3412 | memcpy(&efs, (void *)val, olen); | |
3413 | ||
3414 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && | |
2d792818 GP |
3415 | efs.stype != L2CAP_SERV_NOTRAFIC && |
3416 | efs.stype != chan->local_stype) | |
66af7aaf AE |
3417 | return -ECONNREFUSED; |
3418 | ||
2d792818 GP |
3419 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs), |
3420 | (unsigned long) &efs); | |
66af7aaf | 3421 | break; |
f2fcfcd6 GP |
3422 | } |
3423 | } | |
3424 | ||
0c1bc5c6 | 3425 | if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) |
6c2ea7a8 GP |
3426 | return -ECONNREFUSED; |
3427 | ||
0c1bc5c6 | 3428 | chan->mode = rfc.mode; |
6c2ea7a8 | 3429 | |
0e8b207e | 3430 | if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { |
f2fcfcd6 GP |
3431 | switch (rfc.mode) { |
3432 | case L2CAP_MODE_ERTM: | |
47d1ec61 GP |
3433 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
3434 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); | |
3435 | chan->mps = le16_to_cpu(rfc.max_pdu_size); | |
c20f8e35 MM |
3436 | if (!test_bit(FLAG_EXT_CTRL, &chan->flags)) |
3437 | chan->ack_win = min_t(u16, chan->ack_win, | |
3438 | rfc.txwin_size); | |
66af7aaf AE |
3439 | |
3440 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { | |
3441 | chan->local_msdu = le16_to_cpu(efs.msdu); | |
3442 | chan->local_sdu_itime = | |
2d792818 | 3443 | le32_to_cpu(efs.sdu_itime); |
66af7aaf AE |
3444 | chan->local_acc_lat = le32_to_cpu(efs.acc_lat); |
3445 | chan->local_flush_to = | |
2d792818 | 3446 | le32_to_cpu(efs.flush_to); |
66af7aaf | 3447 | } |
f2fcfcd6 | 3448 | break; |
66af7aaf | 3449 | |
f2fcfcd6 | 3450 | case L2CAP_MODE_STREAMING: |
47d1ec61 | 3451 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
f2fcfcd6 GP |
3452 | } |
3453 | } | |
3454 | ||
fe4128e0 | 3455 | req->dcid = cpu_to_le16(chan->dcid); |
59e54bd1 | 3456 | req->flags = __constant_cpu_to_le16(0); |
f2fcfcd6 GP |
3457 | |
3458 | return ptr - data; | |
3459 | } | |
3460 | ||
2d792818 GP |
3461 | static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, |
3462 | u16 result, u16 flags) | |
1da177e4 LT |
3463 | { |
3464 | struct l2cap_conf_rsp *rsp = data; | |
3465 | void *ptr = rsp->data; | |
1da177e4 | 3466 | |
fe4128e0 | 3467 | BT_DBG("chan %p", chan); |
1da177e4 | 3468 | |
fe4128e0 | 3469 | rsp->scid = cpu_to_le16(chan->dcid); |
5dee9e7c | 3470 | rsp->result = cpu_to_le16(result); |
aca3192c | 3471 | rsp->flags = cpu_to_le16(flags); |
1da177e4 LT |
3472 | |
3473 | return ptr - data; | |
3474 | } | |
3475 | ||
8c1d787b | 3476 | void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) |
710f9b0a GP |
3477 | { |
3478 | struct l2cap_conn_rsp rsp; | |
8c1d787b | 3479 | struct l2cap_conn *conn = chan->conn; |
710f9b0a | 3480 | u8 buf[128]; |
439f34ac | 3481 | u8 rsp_code; |
710f9b0a | 3482 | |
fe4128e0 GP |
3483 | rsp.scid = cpu_to_le16(chan->dcid); |
3484 | rsp.dcid = cpu_to_le16(chan->scid); | |
ac73498c AE |
3485 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS); |
3486 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); | |
439f34ac AE |
3487 | |
3488 | if (chan->hs_hcon) | |
3489 | rsp_code = L2CAP_CREATE_CHAN_RSP; | |
3490 | else | |
3491 | rsp_code = L2CAP_CONN_RSP; | |
3492 | ||
3493 | BT_DBG("chan %p rsp_code %u", chan, rsp_code); | |
3494 | ||
3495 | l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp); | |
710f9b0a | 3496 | |
c1360a1c | 3497 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
710f9b0a GP |
3498 | return; |
3499 | ||
710f9b0a | 3500 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
2d792818 | 3501 | l2cap_build_conf_req(chan, buf), buf); |
710f9b0a GP |
3502 | chan->num_conf_req++; |
3503 | } | |
3504 | ||
47d1ec61 | 3505 | static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len) |
7b1c0049 | 3506 | { |
7b1c0049 GP |
3507 | int type, olen; |
3508 | unsigned long val; | |
c20f8e35 MM |
3509 | /* Use sane default values in case a misbehaving remote device |
3510 | * did not send an RFC or extended window size option. | |
3511 | */ | |
3512 | u16 txwin_ext = chan->ack_win; | |
3513 | struct l2cap_conf_rfc rfc = { | |
3514 | .mode = chan->mode, | |
3515 | .retrans_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO), | |
3516 | .monitor_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO), | |
3517 | .max_pdu_size = cpu_to_le16(chan->imtu), | |
3518 | .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW), | |
3519 | }; | |
7b1c0049 | 3520 | |
47d1ec61 | 3521 | BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); |
7b1c0049 | 3522 | |
0c1bc5c6 | 3523 | if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) |
7b1c0049 GP |
3524 | return; |
3525 | ||
3526 | while (len >= L2CAP_CONF_OPT_SIZE) { | |
3527 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); | |
3528 | ||
c20f8e35 MM |
3529 | switch (type) { |
3530 | case L2CAP_CONF_RFC: | |
3531 | if (olen == sizeof(rfc)) | |
3532 | memcpy(&rfc, (void *)val, olen); | |
8f321f85 | 3533 | break; |
c20f8e35 MM |
3534 | case L2CAP_CONF_EWS: |
3535 | txwin_ext = val; | |
3536 | break; | |
3537 | } | |
7b1c0049 GP |
3538 | } |
3539 | ||
7b1c0049 GP |
3540 | switch (rfc.mode) { |
3541 | case L2CAP_MODE_ERTM: | |
47d1ec61 GP |
3542 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
3543 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); | |
c20f8e35 MM |
3544 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
3545 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) | |
3546 | chan->ack_win = min_t(u16, chan->ack_win, txwin_ext); | |
3547 | else | |
3548 | chan->ack_win = min_t(u16, chan->ack_win, | |
3549 | rfc.txwin_size); | |
7b1c0049 GP |
3550 | break; |
3551 | case L2CAP_MODE_STREAMING: | |
47d1ec61 | 3552 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
7b1c0049 GP |
3553 | } |
3554 | } | |
3555 | ||
2d792818 GP |
3556 | static inline int l2cap_command_rej(struct l2cap_conn *conn, |
3557 | struct l2cap_cmd_hdr *cmd, u8 *data) | |
4e8402a3 | 3558 | { |
e2fd318e | 3559 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
4e8402a3 | 3560 | |
e2fd318e | 3561 | if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) |
4e8402a3 MH |
3562 | return 0; |
3563 | ||
3564 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && | |
2d792818 | 3565 | cmd->ident == conn->info_ident) { |
17cd3f37 | 3566 | cancel_delayed_work(&conn->info_timer); |
984947dc MH |
3567 | |
3568 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; | |
e1027a7c | 3569 | conn->info_ident = 0; |
984947dc | 3570 | |
4e8402a3 MH |
3571 | l2cap_conn_start(conn); |
3572 | } | |
3573 | ||
3574 | return 0; | |
3575 | } | |
3576 | ||
1700915f MM |
3577 | static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn, |
3578 | struct l2cap_cmd_hdr *cmd, | |
3579 | u8 *data, u8 rsp_code, u8 amp_id) | |
1da177e4 | 3580 | { |
1da177e4 LT |
3581 | struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; |
3582 | struct l2cap_conn_rsp rsp; | |
23691d75 | 3583 | struct l2cap_chan *chan = NULL, *pchan; |
d793fe8c | 3584 | struct sock *parent, *sk = NULL; |
e7c29cb1 | 3585 | int result, status = L2CAP_CS_NO_INFO; |
1da177e4 LT |
3586 | |
3587 | u16 dcid = 0, scid = __le16_to_cpu(req->scid); | |
e7c29cb1 | 3588 | __le16 psm = req->psm; |
1da177e4 | 3589 | |
097db76c | 3590 | BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid); |
1da177e4 LT |
3591 | |
3592 | /* Check if we have socket listening on psm */ | |
c2287681 | 3593 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src, conn->dst); |
23691d75 | 3594 | if (!pchan) { |
1da177e4 LT |
3595 | result = L2CAP_CR_BAD_PSM; |
3596 | goto sendresp; | |
3597 | } | |
3598 | ||
23691d75 GP |
3599 | parent = pchan->sk; |
3600 | ||
3df91ea2 | 3601 | mutex_lock(&conn->chan_lock); |
aa2ac881 | 3602 | lock_sock(parent); |
e0f0cb56 | 3603 | |
e7c29cb1 | 3604 | /* Check if the ACL is secure enough (if not SDP) */ |
2983fd68 | 3605 | if (psm != __constant_cpu_to_le16(L2CAP_PSM_SDP) && |
2d792818 | 3606 | !hci_conn_check_link_mode(conn->hcon)) { |
9f5a0d7b | 3607 | conn->disc_reason = HCI_ERROR_AUTH_FAILURE; |
e7c29cb1 MH |
3608 | result = L2CAP_CR_SEC_BLOCK; |
3609 | goto response; | |
3610 | } | |
3611 | ||
1da177e4 LT |
3612 | result = L2CAP_CR_NO_MEM; |
3613 | ||
2dfa1003 GP |
3614 | /* Check if we already have channel with that dcid */ |
3615 | if (__l2cap_get_chan_by_dcid(conn, scid)) | |
3616 | goto response; | |
3617 | ||
80b98027 | 3618 | chan = pchan->ops->new_connection(pchan); |
80808e43 | 3619 | if (!chan) |
1da177e4 LT |
3620 | goto response; |
3621 | ||
80808e43 GP |
3622 | sk = chan->sk; |
3623 | ||
1da177e4 LT |
3624 | hci_conn_hold(conn->hcon); |
3625 | ||
1da177e4 LT |
3626 | bacpy(&bt_sk(sk)->src, conn->src); |
3627 | bacpy(&bt_sk(sk)->dst, conn->dst); | |
fe4128e0 GP |
3628 | chan->psm = psm; |
3629 | chan->dcid = scid; | |
1700915f | 3630 | chan->local_amp_id = amp_id; |
1da177e4 | 3631 | |
6be36555 | 3632 | __l2cap_chan_add(conn, chan); |
48454079 | 3633 | |
fe4128e0 | 3634 | dcid = chan->scid; |
1da177e4 | 3635 | |
c9b66675 | 3636 | __set_chan_timer(chan, sk->sk_sndtimeo); |
1da177e4 | 3637 | |
fc7f8a7e | 3638 | chan->ident = cmd->ident; |
1da177e4 | 3639 | |
984947dc | 3640 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
d45fc423 | 3641 | if (l2cap_chan_check_security(chan)) { |
c5daa683 | 3642 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { |
0e587be7 | 3643 | __l2cap_state_change(chan, BT_CONNECT2); |
f66dc81f MH |
3644 | result = L2CAP_CR_PEND; |
3645 | status = L2CAP_CS_AUTHOR_PEND; | |
2dc4e510 | 3646 | chan->ops->defer(chan); |
f66dc81f | 3647 | } else { |
1700915f MM |
3648 | /* Force pending result for AMP controllers. |
3649 | * The connection will succeed after the | |
3650 | * physical link is up. | |
3651 | */ | |
3652 | if (amp_id) { | |
3653 | __l2cap_state_change(chan, BT_CONNECT2); | |
3654 | result = L2CAP_CR_PEND; | |
3655 | } else { | |
3656 | __l2cap_state_change(chan, BT_CONFIG); | |
3657 | result = L2CAP_CR_SUCCESS; | |
3658 | } | |
f66dc81f MH |
3659 | status = L2CAP_CS_NO_INFO; |
3660 | } | |
79d554a6 | 3661 | } else { |
0e587be7 | 3662 | __l2cap_state_change(chan, BT_CONNECT2); |
79d554a6 MH |
3663 | result = L2CAP_CR_PEND; |
3664 | status = L2CAP_CS_AUTHEN_PEND; | |
3665 | } | |
3666 | } else { | |
0e587be7 | 3667 | __l2cap_state_change(chan, BT_CONNECT2); |
79d554a6 MH |
3668 | result = L2CAP_CR_PEND; |
3669 | status = L2CAP_CS_NO_INFO; | |
1da177e4 LT |
3670 | } |
3671 | ||
1da177e4 | 3672 | response: |
aa2ac881 | 3673 | release_sock(parent); |
3df91ea2 | 3674 | mutex_unlock(&conn->chan_lock); |
1da177e4 LT |
3675 | |
3676 | sendresp: | |
aca3192c YH |
3677 | rsp.scid = cpu_to_le16(scid); |
3678 | rsp.dcid = cpu_to_le16(dcid); | |
3679 | rsp.result = cpu_to_le16(result); | |
3680 | rsp.status = cpu_to_le16(status); | |
4c89b6aa | 3681 | l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp); |
79d554a6 MH |
3682 | |
3683 | if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { | |
3684 | struct l2cap_info_req info; | |
ac73498c | 3685 | info.type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK); |
79d554a6 MH |
3686 | |
3687 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; | |
3688 | conn->info_ident = l2cap_get_ident(conn); | |
3689 | ||
ba13ccd9 | 3690 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
79d554a6 | 3691 | |
2d792818 GP |
3692 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
3693 | sizeof(info), &info); | |
79d554a6 MH |
3694 | } |
3695 | ||
c1360a1c | 3696 | if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && |
2d792818 | 3697 | result == L2CAP_CR_SUCCESS) { |
e9aeb2dd | 3698 | u8 buf[128]; |
c1360a1c | 3699 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
e9aeb2dd | 3700 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
2d792818 | 3701 | l2cap_build_conf_req(chan, buf), buf); |
73ffa904 | 3702 | chan->num_conf_req++; |
e9aeb2dd | 3703 | } |
1700915f MM |
3704 | |
3705 | return chan; | |
4c89b6aa | 3706 | } |
e9aeb2dd | 3707 | |
4c89b6aa MM |
3708 | static int l2cap_connect_req(struct l2cap_conn *conn, |
3709 | struct l2cap_cmd_hdr *cmd, u8 *data) | |
3710 | { | |
300229f9 | 3711 | l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0); |
1da177e4 LT |
3712 | return 0; |
3713 | } | |
3714 | ||
5909cf30 | 3715 | static int l2cap_connect_create_rsp(struct l2cap_conn *conn, |
2d792818 | 3716 | struct l2cap_cmd_hdr *cmd, u8 *data) |
1da177e4 LT |
3717 | { |
3718 | struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; | |
3719 | u16 scid, dcid, result, status; | |
48454079 | 3720 | struct l2cap_chan *chan; |
1da177e4 | 3721 | u8 req[128]; |
3df91ea2 | 3722 | int err; |
1da177e4 LT |
3723 | |
3724 | scid = __le16_to_cpu(rsp->scid); | |
3725 | dcid = __le16_to_cpu(rsp->dcid); | |
3726 | result = __le16_to_cpu(rsp->result); | |
3727 | status = __le16_to_cpu(rsp->status); | |
3728 | ||
1b009c98 | 3729 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", |
2d792818 | 3730 | dcid, scid, result, status); |
1da177e4 | 3731 | |
3df91ea2 AE |
3732 | mutex_lock(&conn->chan_lock); |
3733 | ||
1da177e4 | 3734 | if (scid) { |
3df91ea2 AE |
3735 | chan = __l2cap_get_chan_by_scid(conn, scid); |
3736 | if (!chan) { | |
3737 | err = -EFAULT; | |
3738 | goto unlock; | |
3739 | } | |
1da177e4 | 3740 | } else { |
3df91ea2 AE |
3741 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
3742 | if (!chan) { | |
3743 | err = -EFAULT; | |
3744 | goto unlock; | |
3745 | } | |
1da177e4 LT |
3746 | } |
3747 | ||
3df91ea2 AE |
3748 | err = 0; |
3749 | ||
6be36555 | 3750 | l2cap_chan_lock(chan); |
48454079 | 3751 | |
1da177e4 LT |
3752 | switch (result) { |
3753 | case L2CAP_CR_SUCCESS: | |
89bc500e | 3754 | l2cap_state_change(chan, BT_CONFIG); |
fc7f8a7e | 3755 | chan->ident = 0; |
fe4128e0 | 3756 | chan->dcid = dcid; |
c1360a1c | 3757 | clear_bit(CONF_CONNECT_PEND, &chan->conf_state); |
6a8d3010 | 3758 | |
c1360a1c | 3759 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
e9aeb2dd GP |
3760 | break; |
3761 | ||
1da177e4 | 3762 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
2d792818 | 3763 | l2cap_build_conf_req(chan, req), req); |
73ffa904 | 3764 | chan->num_conf_req++; |
1da177e4 LT |
3765 | break; |
3766 | ||
3767 | case L2CAP_CR_PEND: | |
c1360a1c | 3768 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
1da177e4 LT |
3769 | break; |
3770 | ||
3771 | default: | |
48454079 | 3772 | l2cap_chan_del(chan, ECONNREFUSED); |
1da177e4 LT |
3773 | break; |
3774 | } | |
3775 | ||
6be36555 | 3776 | l2cap_chan_unlock(chan); |
3df91ea2 AE |
3777 | |
3778 | unlock: | |
3779 | mutex_unlock(&conn->chan_lock); | |
3780 | ||
3781 | return err; | |
1da177e4 LT |
3782 | } |
3783 | ||
47d1ec61 | 3784 | static inline void set_default_fcs(struct l2cap_chan *chan) |
8c462b60 MM |
3785 | { |
3786 | /* FCS is enabled only in ERTM or streaming mode, if one or both | |
3787 | * sides request it. | |
3788 | */ | |
0c1bc5c6 | 3789 | if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) |
47d1ec61 | 3790 | chan->fcs = L2CAP_FCS_NONE; |
c1360a1c | 3791 | else if (!test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) |
47d1ec61 | 3792 | chan->fcs = L2CAP_FCS_CRC16; |
8c462b60 MM |
3793 | } |
3794 | ||
29d8a590 AE |
3795 | static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data, |
3796 | u8 ident, u16 flags) | |
3797 | { | |
3798 | struct l2cap_conn *conn = chan->conn; | |
3799 | ||
3800 | BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident, | |
3801 | flags); | |
3802 | ||
3803 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); | |
3804 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); | |
3805 | ||
3806 | l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP, | |
3807 | l2cap_build_conf_rsp(chan, data, | |
3808 | L2CAP_CONF_SUCCESS, flags), data); | |
3809 | } | |
3810 | ||
2d792818 GP |
3811 | static inline int l2cap_config_req(struct l2cap_conn *conn, |
3812 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, | |
3813 | u8 *data) | |
1da177e4 LT |
3814 | { |
3815 | struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; | |
3816 | u16 dcid, flags; | |
3817 | u8 rsp[64]; | |
48454079 | 3818 | struct l2cap_chan *chan; |
3c588192 | 3819 | int len, err = 0; |
1da177e4 LT |
3820 | |
3821 | dcid = __le16_to_cpu(req->dcid); | |
3822 | flags = __le16_to_cpu(req->flags); | |
3823 | ||
3824 | BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); | |
3825 | ||
baa7e1fa | 3826 | chan = l2cap_get_chan_by_scid(conn, dcid); |
48454079 | 3827 | if (!chan) |
1da177e4 LT |
3828 | return -ENOENT; |
3829 | ||
033b1142 | 3830 | if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { |
e2fd318e IK |
3831 | struct l2cap_cmd_rej_cid rej; |
3832 | ||
ac73498c | 3833 | rej.reason = __constant_cpu_to_le16(L2CAP_REJ_INVALID_CID); |
e2fd318e IK |
3834 | rej.scid = cpu_to_le16(chan->scid); |
3835 | rej.dcid = cpu_to_le16(chan->dcid); | |
df6bd743 | 3836 | |
df6bd743 | 3837 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
2d792818 | 3838 | sizeof(rej), &rej); |
354f60a9 | 3839 | goto unlock; |
df6bd743 | 3840 | } |
354f60a9 | 3841 | |
5dee9e7c | 3842 | /* Reject if config buffer is too small. */ |
88219a0f | 3843 | len = cmd_len - sizeof(*req); |
7ac28817 | 3844 | if (len < 0 || chan->conf_len + len > sizeof(chan->conf_req)) { |
5dee9e7c | 3845 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
2d792818 GP |
3846 | l2cap_build_conf_rsp(chan, rsp, |
3847 | L2CAP_CONF_REJECT, flags), rsp); | |
5dee9e7c MH |
3848 | goto unlock; |
3849 | } | |
3850 | ||
3851 | /* Store config. */ | |
73ffa904 GP |
3852 | memcpy(chan->conf_req + chan->conf_len, req->data, len); |
3853 | chan->conf_len += len; | |
1da177e4 | 3854 | |
59e54bd1 | 3855 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) { |
1da177e4 LT |
3856 | /* Incomplete config. Send empty response. */ |
3857 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, | |
2d792818 GP |
3858 | l2cap_build_conf_rsp(chan, rsp, |
3859 | L2CAP_CONF_SUCCESS, flags), rsp); | |
1da177e4 LT |
3860 | goto unlock; |
3861 | } | |
3862 | ||
3863 | /* Complete config. */ | |
73ffa904 | 3864 | len = l2cap_parse_conf_req(chan, rsp); |
f2fcfcd6 | 3865 | if (len < 0) { |
e92c8e70 | 3866 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
1da177e4 | 3867 | goto unlock; |
f2fcfcd6 | 3868 | } |
1da177e4 | 3869 | |
1500109b | 3870 | chan->ident = cmd->ident; |
5dee9e7c | 3871 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); |
73ffa904 | 3872 | chan->num_conf_rsp++; |
5dee9e7c | 3873 | |
5dee9e7c | 3874 | /* Reset config buffer. */ |
73ffa904 | 3875 | chan->conf_len = 0; |
5dee9e7c | 3876 | |
c1360a1c | 3877 | if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) |
876d9484 MH |
3878 | goto unlock; |
3879 | ||
c1360a1c | 3880 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
47d1ec61 | 3881 | set_default_fcs(chan); |
fcc203c3 | 3882 | |
105bdf9e MM |
3883 | if (chan->mode == L2CAP_MODE_ERTM || |
3884 | chan->mode == L2CAP_MODE_STREAMING) | |
3c588192 MM |
3885 | err = l2cap_ertm_init(chan); |
3886 | ||
3887 | if (err < 0) | |
3888 | l2cap_send_disconn_req(chan->conn, chan, -err); | |
3889 | else | |
3890 | l2cap_chan_ready(chan); | |
0565c1c2 | 3891 | |
876d9484 MH |
3892 | goto unlock; |
3893 | } | |
3894 | ||
c1360a1c | 3895 | if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { |
79d554a6 | 3896 | u8 buf[64]; |
1da177e4 | 3897 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
2d792818 | 3898 | l2cap_build_conf_req(chan, buf), buf); |
73ffa904 | 3899 | chan->num_conf_req++; |
1da177e4 LT |
3900 | } |
3901 | ||
0e8b207e AE |
3902 | /* Got Conf Rsp PENDING from remote side and asume we sent |
3903 | Conf Rsp PENDING in the code above */ | |
3904 | if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && | |
29d8a590 | 3905 | test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
0e8b207e AE |
3906 | |
3907 | /* check compatibility */ | |
3908 | ||
79de886d AE |
3909 | /* Send rsp for BR/EDR channel */ |
3910 | if (!chan->ctrl_id) | |
3911 | l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags); | |
3912 | else | |
3913 | chan->ident = cmd->ident; | |
0e8b207e AE |
3914 | } |
3915 | ||
1da177e4 | 3916 | unlock: |
6be36555 | 3917 | l2cap_chan_unlock(chan); |
3c588192 | 3918 | return err; |
1da177e4 LT |
3919 | } |
3920 | ||
2d792818 GP |
3921 | static inline int l2cap_config_rsp(struct l2cap_conn *conn, |
3922 | struct l2cap_cmd_hdr *cmd, u8 *data) | |
1da177e4 LT |
3923 | { |
3924 | struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; | |
3925 | u16 scid, flags, result; | |
48454079 | 3926 | struct l2cap_chan *chan; |
61386cba | 3927 | int len = le16_to_cpu(cmd->len) - sizeof(*rsp); |
3c588192 | 3928 | int err = 0; |
1da177e4 LT |
3929 | |
3930 | scid = __le16_to_cpu(rsp->scid); | |
3931 | flags = __le16_to_cpu(rsp->flags); | |
3932 | result = __le16_to_cpu(rsp->result); | |
3933 | ||
61386cba AE |
3934 | BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags, |
3935 | result, len); | |
1da177e4 | 3936 | |
baa7e1fa | 3937 | chan = l2cap_get_chan_by_scid(conn, scid); |
48454079 | 3938 | if (!chan) |
1da177e4 LT |
3939 | return 0; |
3940 | ||
3941 | switch (result) { | |
3942 | case L2CAP_CONF_SUCCESS: | |
47d1ec61 | 3943 | l2cap_conf_rfc_get(chan, rsp->data, len); |
0e8b207e | 3944 | clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
1da177e4 LT |
3945 | break; |
3946 | ||
0e8b207e AE |
3947 | case L2CAP_CONF_PENDING: |
3948 | set_bit(CONF_REM_CONF_PEND, &chan->conf_state); | |
3949 | ||
3950 | if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { | |
3951 | char buf[64]; | |
3952 | ||
3953 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, | |
2d792818 | 3954 | buf, &result); |
0e8b207e AE |
3955 | if (len < 0) { |
3956 | l2cap_send_disconn_req(conn, chan, ECONNRESET); | |
3957 | goto done; | |
3958 | } | |
3959 | ||
3960 | /* check compatibility */ | |
3961 | ||
79de886d AE |
3962 | if (!chan->ctrl_id) |
3963 | l2cap_send_efs_conf_rsp(chan, buf, cmd->ident, | |
3964 | 0); | |
3965 | else | |
3966 | chan->ident = cmd->ident; | |
0e8b207e AE |
3967 | } |
3968 | goto done; | |
3969 | ||
1da177e4 | 3970 | case L2CAP_CONF_UNACCEPT: |
73ffa904 | 3971 | if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { |
f2fcfcd6 GP |
3972 | char req[64]; |
3973 | ||
c2c77ec8 | 3974 | if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { |
e92c8e70 | 3975 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
c2c77ec8 AE |
3976 | goto done; |
3977 | } | |
3978 | ||
f2fcfcd6 GP |
3979 | /* throw out any old stored conf requests */ |
3980 | result = L2CAP_CONF_SUCCESS; | |
b4450035 | 3981 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
2d792818 | 3982 | req, &result); |
f2fcfcd6 | 3983 | if (len < 0) { |
e92c8e70 | 3984 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
f2fcfcd6 GP |
3985 | goto done; |
3986 | } | |
3987 | ||
3988 | l2cap_send_cmd(conn, l2cap_get_ident(conn), | |
2d792818 | 3989 | L2CAP_CONF_REQ, len, req); |
73ffa904 | 3990 | chan->num_conf_req++; |
f2fcfcd6 GP |
3991 | if (result != L2CAP_CONF_SUCCESS) |
3992 | goto done; | |
3993 | break; | |
1da177e4 LT |
3994 | } |
3995 | ||
8e87d142 | 3996 | default: |
6be36555 | 3997 | l2cap_chan_set_err(chan, ECONNRESET); |
2e0052e4 | 3998 | |
ba13ccd9 | 3999 | __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT); |
e92c8e70 | 4000 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
1da177e4 LT |
4001 | goto done; |
4002 | } | |
4003 | ||
59e54bd1 | 4004 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) |
1da177e4 LT |
4005 | goto done; |
4006 | ||
c1360a1c | 4007 | set_bit(CONF_INPUT_DONE, &chan->conf_state); |
1da177e4 | 4008 | |
c1360a1c | 4009 | if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { |
47d1ec61 | 4010 | set_default_fcs(chan); |
fcc203c3 | 4011 | |
105bdf9e MM |
4012 | if (chan->mode == L2CAP_MODE_ERTM || |
4013 | chan->mode == L2CAP_MODE_STREAMING) | |
3c588192 | 4014 | err = l2cap_ertm_init(chan); |
0565c1c2 | 4015 | |
3c588192 MM |
4016 | if (err < 0) |
4017 | l2cap_send_disconn_req(chan->conn, chan, -err); | |
4018 | else | |
4019 | l2cap_chan_ready(chan); | |
1da177e4 LT |
4020 | } |
4021 | ||
4022 | done: | |
6be36555 | 4023 | l2cap_chan_unlock(chan); |
3c588192 | 4024 | return err; |
1da177e4 LT |
4025 | } |
4026 | ||
2d792818 GP |
4027 | static inline int l2cap_disconnect_req(struct l2cap_conn *conn, |
4028 | struct l2cap_cmd_hdr *cmd, u8 *data) | |
1da177e4 LT |
4029 | { |
4030 | struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; | |
4031 | struct l2cap_disconn_rsp rsp; | |
4032 | u16 dcid, scid; | |
48454079 | 4033 | struct l2cap_chan *chan; |
1da177e4 LT |
4034 | struct sock *sk; |
4035 | ||
4036 | scid = __le16_to_cpu(req->scid); | |
4037 | dcid = __le16_to_cpu(req->dcid); | |
4038 | ||
4039 | BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); | |
4040 | ||
3df91ea2 AE |
4041 | mutex_lock(&conn->chan_lock); |
4042 | ||
4043 | chan = __l2cap_get_chan_by_scid(conn, dcid); | |
4044 | if (!chan) { | |
4045 | mutex_unlock(&conn->chan_lock); | |
1da177e4 | 4046 | return 0; |
3df91ea2 | 4047 | } |
1da177e4 | 4048 | |
6be36555 AE |
4049 | l2cap_chan_lock(chan); |
4050 | ||
48454079 GP |
4051 | sk = chan->sk; |
4052 | ||
fe4128e0 GP |
4053 | rsp.dcid = cpu_to_le16(chan->scid); |
4054 | rsp.scid = cpu_to_le16(chan->dcid); | |
1da177e4 LT |
4055 | l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); |
4056 | ||
6be36555 | 4057 | lock_sock(sk); |
1da177e4 | 4058 | sk->sk_shutdown = SHUTDOWN_MASK; |
6be36555 | 4059 | release_sock(sk); |
1da177e4 | 4060 | |
61d6ef3e | 4061 | l2cap_chan_hold(chan); |
48454079 | 4062 | l2cap_chan_del(chan, ECONNRESET); |
6be36555 AE |
4063 | |
4064 | l2cap_chan_unlock(chan); | |
1da177e4 | 4065 | |
80b98027 | 4066 | chan->ops->close(chan); |
61d6ef3e | 4067 | l2cap_chan_put(chan); |
3df91ea2 AE |
4068 | |
4069 | mutex_unlock(&conn->chan_lock); | |
4070 | ||
1da177e4 LT |
4071 | return 0; |
4072 | } | |
4073 | ||
2d792818 GP |
4074 | static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, |
4075 | struct l2cap_cmd_hdr *cmd, u8 *data) | |
1da177e4 LT |
4076 | { |
4077 | struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; | |
4078 | u16 dcid, scid; | |
48454079 | 4079 | struct l2cap_chan *chan; |
1da177e4 LT |
4080 | |
4081 | scid = __le16_to_cpu(rsp->scid); | |
4082 | dcid = __le16_to_cpu(rsp->dcid); | |
4083 | ||
4084 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); | |
4085 | ||
3df91ea2 AE |
4086 | mutex_lock(&conn->chan_lock); |
4087 | ||
4088 | chan = __l2cap_get_chan_by_scid(conn, scid); | |
4089 | if (!chan) { | |
4090 | mutex_unlock(&conn->chan_lock); | |
1da177e4 | 4091 | return 0; |
3df91ea2 | 4092 | } |
1da177e4 | 4093 | |
6be36555 | 4094 | l2cap_chan_lock(chan); |
48454079 | 4095 | |
61d6ef3e | 4096 | l2cap_chan_hold(chan); |
48454079 | 4097 | l2cap_chan_del(chan, 0); |
6be36555 AE |
4098 | |
4099 | l2cap_chan_unlock(chan); | |
1da177e4 | 4100 | |
80b98027 | 4101 | chan->ops->close(chan); |
61d6ef3e | 4102 | l2cap_chan_put(chan); |
3df91ea2 AE |
4103 | |
4104 | mutex_unlock(&conn->chan_lock); | |
4105 | ||
1da177e4 LT |
4106 | return 0; |
4107 | } | |
4108 | ||
2d792818 GP |
4109 | static inline int l2cap_information_req(struct l2cap_conn *conn, |
4110 | struct l2cap_cmd_hdr *cmd, u8 *data) | |
1da177e4 LT |
4111 | { |
4112 | struct l2cap_info_req *req = (struct l2cap_info_req *) data; | |
1da177e4 LT |
4113 | u16 type; |
4114 | ||
4115 | type = __le16_to_cpu(req->type); | |
4116 | ||
4117 | BT_DBG("type 0x%4.4x", type); | |
4118 | ||
f0709e03 MH |
4119 | if (type == L2CAP_IT_FEAT_MASK) { |
4120 | u8 buf[8]; | |
44dd46de | 4121 | u32 feat_mask = l2cap_feat_mask; |
f0709e03 | 4122 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
ac73498c AE |
4123 | rsp->type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK); |
4124 | rsp->result = __constant_cpu_to_le16(L2CAP_IR_SUCCESS); | |
d1c4a17d | 4125 | if (!disable_ertm) |
fcc203c3 | 4126 | feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING |
2d792818 | 4127 | | L2CAP_FEAT_FCS; |
a5fd6f30 | 4128 | if (enable_hs) |
6327eb98 | 4129 | feat_mask |= L2CAP_FEAT_EXT_FLOW |
2d792818 | 4130 | | L2CAP_FEAT_EXT_WINDOW; |
a5fd6f30 | 4131 | |
1b7bf4ed | 4132 | put_unaligned_le32(feat_mask, rsp->data); |
2d792818 GP |
4133 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
4134 | buf); | |
e1027a7c MH |
4135 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
4136 | u8 buf[12]; | |
4137 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; | |
50a147cd MM |
4138 | |
4139 | if (enable_hs) | |
4140 | l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; | |
4141 | else | |
4142 | l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; | |
4143 | ||
ac73498c AE |
4144 | rsp->type = __constant_cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
4145 | rsp->result = __constant_cpu_to_le16(L2CAP_IR_SUCCESS); | |
c6337ea6 | 4146 | memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan)); |
2d792818 GP |
4147 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
4148 | buf); | |
f0709e03 MH |
4149 | } else { |
4150 | struct l2cap_info_rsp rsp; | |
4151 | rsp.type = cpu_to_le16(type); | |
ac73498c | 4152 | rsp.result = __constant_cpu_to_le16(L2CAP_IR_NOTSUPP); |
2d792818 GP |
4153 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp), |
4154 | &rsp); | |
f0709e03 | 4155 | } |
1da177e4 LT |
4156 | |
4157 | return 0; | |
4158 | } | |
4159 | ||
2d792818 GP |
4160 | static inline int l2cap_information_rsp(struct l2cap_conn *conn, |
4161 | struct l2cap_cmd_hdr *cmd, u8 *data) | |
1da177e4 LT |
4162 | { |
4163 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; | |
4164 | u16 type, result; | |
4165 | ||
4166 | type = __le16_to_cpu(rsp->type); | |
4167 | result = __le16_to_cpu(rsp->result); | |
4168 | ||
4169 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); | |
4170 | ||
e90165be AE |
4171 | /* L2CAP Info req/rsp are unbound to channels, add extra checks */ |
4172 | if (cmd->ident != conn->info_ident || | |
2d792818 | 4173 | conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
e90165be AE |
4174 | return 0; |
4175 | ||
17cd3f37 | 4176 | cancel_delayed_work(&conn->info_timer); |
4e8402a3 | 4177 | |
adb08ede VT |
4178 | if (result != L2CAP_IR_SUCCESS) { |
4179 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; | |
4180 | conn->info_ident = 0; | |
4181 | ||
4182 | l2cap_conn_start(conn); | |
4183 | ||
4184 | return 0; | |
4185 | } | |
4186 | ||
978c93b9 AE |
4187 | switch (type) { |
4188 | case L2CAP_IT_FEAT_MASK: | |
83985319 | 4189 | conn->feat_mask = get_unaligned_le32(rsp->data); |
4e8402a3 | 4190 | |
47ec1dcd | 4191 | if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { |
e1027a7c | 4192 | struct l2cap_info_req req; |
ac73498c | 4193 | req.type = __constant_cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
e1027a7c MH |
4194 | |
4195 | conn->info_ident = l2cap_get_ident(conn); | |
4196 | ||
4197 | l2cap_send_cmd(conn, conn->info_ident, | |
2d792818 | 4198 | L2CAP_INFO_REQ, sizeof(req), &req); |
e1027a7c MH |
4199 | } else { |
4200 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; | |
4201 | conn->info_ident = 0; | |
4202 | ||
4203 | l2cap_conn_start(conn); | |
4204 | } | |
978c93b9 AE |
4205 | break; |
4206 | ||
4207 | case L2CAP_IT_FIXED_CHAN: | |
4208 | conn->fixed_chan_mask = rsp->data[0]; | |
984947dc | 4209 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
e1027a7c | 4210 | conn->info_ident = 0; |
984947dc MH |
4211 | |
4212 | l2cap_conn_start(conn); | |
978c93b9 | 4213 | break; |
984947dc | 4214 | } |
4e8402a3 | 4215 | |
1da177e4 LT |
4216 | return 0; |
4217 | } | |
4218 | ||
1700915f MM |
4219 | static int l2cap_create_channel_req(struct l2cap_conn *conn, |
4220 | struct l2cap_cmd_hdr *cmd, | |
4221 | u16 cmd_len, void *data) | |
f94ff6ff MM |
4222 | { |
4223 | struct l2cap_create_chan_req *req = data; | |
1700915f | 4224 | struct l2cap_chan *chan; |
f94ff6ff MM |
4225 | u16 psm, scid; |
4226 | ||
4227 | if (cmd_len != sizeof(*req)) | |
4228 | return -EPROTO; | |
4229 | ||
4230 | if (!enable_hs) | |
4231 | return -EINVAL; | |
4232 | ||
4233 | psm = le16_to_cpu(req->psm); | |
4234 | scid = le16_to_cpu(req->scid); | |
4235 | ||
ad0ac6ca | 4236 | BT_DBG("psm 0x%2.2x, scid 0x%4.4x, amp_id %d", psm, scid, req->amp_id); |
f94ff6ff | 4237 | |
1700915f MM |
4238 | if (req->amp_id) { |
4239 | struct hci_dev *hdev; | |
4240 | ||
4241 | /* Validate AMP controller id */ | |
4242 | hdev = hci_dev_get(req->amp_id); | |
4243 | if (!hdev || hdev->dev_type != HCI_AMP || | |
4244 | !test_bit(HCI_UP, &hdev->flags)) { | |
4245 | struct l2cap_create_chan_rsp rsp; | |
4246 | ||
4247 | rsp.dcid = 0; | |
4248 | rsp.scid = cpu_to_le16(scid); | |
4249 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_BAD_AMP); | |
4250 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); | |
4251 | ||
4252 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, | |
4253 | sizeof(rsp), &rsp); | |
4254 | ||
4255 | if (hdev) | |
4256 | hci_dev_put(hdev); | |
4257 | ||
4258 | return 0; | |
4259 | } | |
4260 | ||
4261 | hci_dev_put(hdev); | |
4262 | } | |
f94ff6ff | 4263 | |
1700915f MM |
4264 | chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
4265 | req->amp_id); | |
f94ff6ff MM |
4266 | |
4267 | return 0; | |
4268 | } | |
4269 | ||
8eb200bd MM |
4270 | static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id) |
4271 | { | |
4272 | struct l2cap_move_chan_req req; | |
4273 | u8 ident; | |
4274 | ||
4275 | BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id); | |
4276 | ||
4277 | ident = l2cap_get_ident(chan->conn); | |
4278 | chan->ident = ident; | |
4279 | ||
4280 | req.icid = cpu_to_le16(chan->scid); | |
4281 | req.dest_amp_id = dest_amp_id; | |
4282 | ||
4283 | l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req), | |
4284 | &req); | |
4285 | ||
4286 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); | |
4287 | } | |
4288 | ||
1500109b | 4289 | static void l2cap_send_move_chan_rsp(struct l2cap_chan *chan, u16 result) |
8d5a04a1 MM |
4290 | { |
4291 | struct l2cap_move_chan_rsp rsp; | |
4292 | ||
1500109b | 4293 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
8d5a04a1 | 4294 | |
1500109b | 4295 | rsp.icid = cpu_to_le16(chan->dcid); |
8d5a04a1 MM |
4296 | rsp.result = cpu_to_le16(result); |
4297 | ||
1500109b MM |
4298 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP, |
4299 | sizeof(rsp), &rsp); | |
8d5a04a1 MM |
4300 | } |
4301 | ||
5b155ef9 | 4302 | static void l2cap_send_move_chan_cfm(struct l2cap_chan *chan, u16 result) |
8d5a04a1 MM |
4303 | { |
4304 | struct l2cap_move_chan_cfm cfm; | |
8d5a04a1 | 4305 | |
5b155ef9 | 4306 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
8d5a04a1 | 4307 | |
5b155ef9 | 4308 | chan->ident = l2cap_get_ident(chan->conn); |
8d5a04a1 | 4309 | |
5b155ef9 | 4310 | cfm.icid = cpu_to_le16(chan->scid); |
8d5a04a1 MM |
4311 | cfm.result = cpu_to_le16(result); |
4312 | ||
5b155ef9 MM |
4313 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM, |
4314 | sizeof(cfm), &cfm); | |
4315 | ||
4316 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); | |
4317 | } | |
4318 | ||
4319 | static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid) | |
4320 | { | |
4321 | struct l2cap_move_chan_cfm cfm; | |
4322 | ||
4323 | BT_DBG("conn %p, icid 0x%4.4x", conn, icid); | |
4324 | ||
4325 | cfm.icid = cpu_to_le16(icid); | |
4326 | cfm.result = __constant_cpu_to_le16(L2CAP_MC_UNCONFIRMED); | |
4327 | ||
4328 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM, | |
4329 | sizeof(cfm), &cfm); | |
8d5a04a1 MM |
4330 | } |
4331 | ||
4332 | static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident, | |
ad0ac6ca | 4333 | u16 icid) |
8d5a04a1 MM |
4334 | { |
4335 | struct l2cap_move_chan_cfm_rsp rsp; | |
4336 | ||
ad0ac6ca | 4337 | BT_DBG("icid 0x%4.4x", icid); |
8d5a04a1 MM |
4338 | |
4339 | rsp.icid = cpu_to_le16(icid); | |
4340 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); | |
4341 | } | |
4342 | ||
5f3847a4 MM |
4343 | static void __release_logical_link(struct l2cap_chan *chan) |
4344 | { | |
4345 | chan->hs_hchan = NULL; | |
4346 | chan->hs_hcon = NULL; | |
4347 | ||
4348 | /* Placeholder - release the logical link */ | |
4349 | } | |
4350 | ||
1500109b MM |
4351 | static void l2cap_logical_fail(struct l2cap_chan *chan) |
4352 | { | |
4353 | /* Logical link setup failed */ | |
4354 | if (chan->state != BT_CONNECTED) { | |
4355 | /* Create channel failure, disconnect */ | |
4356 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); | |
4357 | return; | |
4358 | } | |
4359 | ||
4360 | switch (chan->move_role) { | |
4361 | case L2CAP_MOVE_ROLE_RESPONDER: | |
4362 | l2cap_move_done(chan); | |
4363 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP); | |
4364 | break; | |
4365 | case L2CAP_MOVE_ROLE_INITIATOR: | |
4366 | if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP || | |
4367 | chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) { | |
4368 | /* Remote has only sent pending or | |
4369 | * success responses, clean up | |
4370 | */ | |
4371 | l2cap_move_done(chan); | |
4372 | } | |
4373 | ||
4374 | /* Other amp move states imply that the move | |
4375 | * has already aborted | |
4376 | */ | |
4377 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); | |
4378 | break; | |
4379 | } | |
4380 | } | |
4381 | ||
4382 | static void l2cap_logical_finish_create(struct l2cap_chan *chan, | |
4383 | struct hci_chan *hchan) | |
4384 | { | |
4385 | struct l2cap_conf_rsp rsp; | |
1500109b | 4386 | |
336178a3 | 4387 | chan->hs_hchan = hchan; |
1500109b MM |
4388 | chan->hs_hcon->l2cap_data = chan->conn; |
4389 | ||
35ba9561 | 4390 | l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0); |
1500109b MM |
4391 | |
4392 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { | |
fe79c6fe | 4393 | int err; |
1500109b MM |
4394 | |
4395 | set_default_fcs(chan); | |
4396 | ||
4397 | err = l2cap_ertm_init(chan); | |
4398 | if (err < 0) | |
4399 | l2cap_send_disconn_req(chan->conn, chan, -err); | |
4400 | else | |
4401 | l2cap_chan_ready(chan); | |
4402 | } | |
4403 | } | |
4404 | ||
4405 | static void l2cap_logical_finish_move(struct l2cap_chan *chan, | |
4406 | struct hci_chan *hchan) | |
4407 | { | |
4408 | chan->hs_hcon = hchan->conn; | |
4409 | chan->hs_hcon->l2cap_data = chan->conn; | |
4410 | ||
4411 | BT_DBG("move_state %d", chan->move_state); | |
4412 | ||
4413 | switch (chan->move_state) { | |
4414 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: | |
4415 | /* Move confirm will be sent after a success | |
4416 | * response is received | |
4417 | */ | |
4418 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; | |
4419 | break; | |
4420 | case L2CAP_MOVE_WAIT_LOGICAL_CFM: | |
4421 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { | |
4422 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; | |
4423 | } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { | |
4424 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; | |
4425 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); | |
4426 | } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { | |
4427 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; | |
4428 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); | |
4429 | } | |
4430 | break; | |
4431 | default: | |
4432 | /* Move was not in expected state, free the channel */ | |
4433 | __release_logical_link(chan); | |
4434 | ||
4435 | chan->move_state = L2CAP_MOVE_STABLE; | |
4436 | } | |
4437 | } | |
4438 | ||
4439 | /* Call with chan locked */ | |
27695fb4 AE |
4440 | void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan, |
4441 | u8 status) | |
5b155ef9 | 4442 | { |
1500109b MM |
4443 | BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status); |
4444 | ||
4445 | if (status) { | |
4446 | l2cap_logical_fail(chan); | |
4447 | __release_logical_link(chan); | |
4448 | return; | |
4449 | } | |
4450 | ||
4451 | if (chan->state != BT_CONNECTED) { | |
4452 | /* Ignore logical link if channel is on BR/EDR */ | |
4453 | if (chan->local_amp_id) | |
4454 | l2cap_logical_finish_create(chan, hchan); | |
4455 | } else { | |
4456 | l2cap_logical_finish_move(chan, hchan); | |
4457 | } | |
5b155ef9 MM |
4458 | } |
4459 | ||
3f7a56c4 MM |
4460 | void l2cap_move_start(struct l2cap_chan *chan) |
4461 | { | |
4462 | BT_DBG("chan %p", chan); | |
4463 | ||
4464 | if (chan->local_amp_id == HCI_BREDR_ID) { | |
4465 | if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED) | |
4466 | return; | |
4467 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; | |
4468 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; | |
4469 | /* Placeholder - start physical link setup */ | |
4470 | } else { | |
4471 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; | |
4472 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; | |
4473 | chan->move_id = 0; | |
4474 | l2cap_move_setup(chan); | |
4475 | l2cap_send_move_chan_req(chan, 0); | |
4476 | } | |
4477 | } | |
4478 | ||
8eb200bd MM |
4479 | static void l2cap_do_create(struct l2cap_chan *chan, int result, |
4480 | u8 local_amp_id, u8 remote_amp_id) | |
4481 | { | |
4482 | if (!test_bit(CONF_CONNECT_PEND, &chan->conf_state)) { | |
4483 | struct l2cap_conn_rsp rsp; | |
4484 | char buf[128]; | |
4485 | rsp.scid = cpu_to_le16(chan->dcid); | |
4486 | rsp.dcid = cpu_to_le16(chan->scid); | |
4487 | ||
4488 | /* Incoming channel on AMP */ | |
4489 | if (result == L2CAP_CR_SUCCESS) { | |
4490 | /* Send successful response */ | |
4491 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); | |
4492 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); | |
4493 | } else { | |
4494 | /* Send negative response */ | |
4495 | rsp.result = cpu_to_le16(L2CAP_CR_NO_MEM); | |
4496 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); | |
4497 | } | |
4498 | ||
4499 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP, | |
4500 | sizeof(rsp), &rsp); | |
4501 | ||
4502 | if (result == L2CAP_CR_SUCCESS) { | |
4503 | __l2cap_state_change(chan, BT_CONFIG); | |
4504 | set_bit(CONF_REQ_SENT, &chan->conf_state); | |
4505 | l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn), | |
4506 | L2CAP_CONF_REQ, | |
4507 | l2cap_build_conf_req(chan, buf), buf); | |
4508 | chan->num_conf_req++; | |
4509 | } | |
4510 | } else { | |
4511 | /* Outgoing channel on AMP */ | |
4512 | if (result == L2CAP_CR_SUCCESS) { | |
4513 | chan->local_amp_id = local_amp_id; | |
4514 | l2cap_send_create_chan_req(chan, remote_amp_id); | |
4515 | } else { | |
4516 | /* Revert to BR/EDR connect */ | |
4517 | l2cap_send_conn_req(chan); | |
4518 | } | |
4519 | } | |
4520 | } | |
4521 | ||
4522 | static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id, | |
4523 | u8 remote_amp_id) | |
4524 | { | |
4525 | l2cap_move_setup(chan); | |
4526 | chan->move_id = local_amp_id; | |
4527 | chan->move_state = L2CAP_MOVE_WAIT_RSP; | |
4528 | ||
4529 | l2cap_send_move_chan_req(chan, remote_amp_id); | |
4530 | } | |
4531 | ||
4532 | static void l2cap_do_move_respond(struct l2cap_chan *chan, int result) | |
4533 | { | |
4534 | struct hci_chan *hchan = NULL; | |
4535 | ||
4536 | /* Placeholder - get hci_chan for logical link */ | |
4537 | ||
4538 | if (hchan) { | |
4539 | if (hchan->state == BT_CONNECTED) { | |
4540 | /* Logical link is ready to go */ | |
4541 | chan->hs_hcon = hchan->conn; | |
4542 | chan->hs_hcon->l2cap_data = chan->conn; | |
4543 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; | |
4544 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); | |
4545 | ||
4546 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); | |
4547 | } else { | |
4548 | /* Wait for logical link to be ready */ | |
4549 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; | |
4550 | } | |
4551 | } else { | |
4552 | /* Logical link not available */ | |
4553 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED); | |
4554 | } | |
4555 | } | |
4556 | ||
4557 | static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result) | |
4558 | { | |
4559 | if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { | |
4560 | u8 rsp_result; | |
4561 | if (result == -EINVAL) | |
4562 | rsp_result = L2CAP_MR_BAD_ID; | |
4563 | else | |
4564 | rsp_result = L2CAP_MR_NOT_ALLOWED; | |
4565 | ||
4566 | l2cap_send_move_chan_rsp(chan, rsp_result); | |
4567 | } | |
4568 | ||
4569 | chan->move_role = L2CAP_MOVE_ROLE_NONE; | |
4570 | chan->move_state = L2CAP_MOVE_STABLE; | |
4571 | ||
4572 | /* Restart data transmission */ | |
4573 | l2cap_ertm_send(chan); | |
4574 | } | |
4575 | ||
770bfefa | 4576 | void l2cap_physical_cfm(struct l2cap_chan *chan, int result) |
8eb200bd | 4577 | { |
770bfefa AE |
4578 | u8 local_amp_id = chan->local_amp_id; |
4579 | u8 remote_amp_id = chan->ctrl_id; | |
4580 | ||
8eb200bd MM |
4581 | BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d", |
4582 | chan, result, local_amp_id, remote_amp_id); | |
4583 | ||
4584 | l2cap_chan_lock(chan); | |
4585 | ||
4586 | if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) { | |
4587 | l2cap_chan_unlock(chan); | |
4588 | return; | |
4589 | } | |
4590 | ||
4591 | if (chan->state != BT_CONNECTED) { | |
4592 | l2cap_do_create(chan, result, local_amp_id, remote_amp_id); | |
4593 | } else if (result != L2CAP_MR_SUCCESS) { | |
4594 | l2cap_do_move_cancel(chan, result); | |
4595 | } else { | |
4596 | switch (chan->move_role) { | |
4597 | case L2CAP_MOVE_ROLE_INITIATOR: | |
4598 | l2cap_do_move_initiate(chan, local_amp_id, | |
4599 | remote_amp_id); | |
4600 | break; | |
4601 | case L2CAP_MOVE_ROLE_RESPONDER: | |
4602 | l2cap_do_move_respond(chan, result); | |
4603 | break; | |
4604 | default: | |
4605 | l2cap_do_move_cancel(chan, result); | |
4606 | break; | |
4607 | } | |
4608 | } | |
4609 | ||
4610 | l2cap_chan_unlock(chan); | |
4611 | } | |
4612 | ||
8d5a04a1 | 4613 | static inline int l2cap_move_channel_req(struct l2cap_conn *conn, |
ad0ac6ca AE |
4614 | struct l2cap_cmd_hdr *cmd, |
4615 | u16 cmd_len, void *data) | |
8d5a04a1 MM |
4616 | { |
4617 | struct l2cap_move_chan_req *req = data; | |
1500109b | 4618 | struct l2cap_move_chan_rsp rsp; |
02b0fbb9 | 4619 | struct l2cap_chan *chan; |
8d5a04a1 MM |
4620 | u16 icid = 0; |
4621 | u16 result = L2CAP_MR_NOT_ALLOWED; | |
4622 | ||
4623 | if (cmd_len != sizeof(*req)) | |
4624 | return -EPROTO; | |
4625 | ||
4626 | icid = le16_to_cpu(req->icid); | |
4627 | ||
ad0ac6ca | 4628 | BT_DBG("icid 0x%4.4x, dest_amp_id %d", icid, req->dest_amp_id); |
8d5a04a1 MM |
4629 | |
4630 | if (!enable_hs) | |
4631 | return -EINVAL; | |
4632 | ||
02b0fbb9 MM |
4633 | chan = l2cap_get_chan_by_dcid(conn, icid); |
4634 | if (!chan) { | |
1500109b MM |
4635 | rsp.icid = cpu_to_le16(icid); |
4636 | rsp.result = __constant_cpu_to_le16(L2CAP_MR_NOT_ALLOWED); | |
4637 | l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP, | |
4638 | sizeof(rsp), &rsp); | |
02b0fbb9 MM |
4639 | return 0; |
4640 | } | |
4641 | ||
1500109b MM |
4642 | chan->ident = cmd->ident; |
4643 | ||
02b0fbb9 MM |
4644 | if (chan->scid < L2CAP_CID_DYN_START || |
4645 | chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY || | |
4646 | (chan->mode != L2CAP_MODE_ERTM && | |
4647 | chan->mode != L2CAP_MODE_STREAMING)) { | |
4648 | result = L2CAP_MR_NOT_ALLOWED; | |
4649 | goto send_move_response; | |
4650 | } | |
4651 | ||
4652 | if (chan->local_amp_id == req->dest_amp_id) { | |
4653 | result = L2CAP_MR_SAME_ID; | |
4654 | goto send_move_response; | |
4655 | } | |
4656 | ||
4657 | if (req->dest_amp_id) { | |
4658 | struct hci_dev *hdev; | |
4659 | hdev = hci_dev_get(req->dest_amp_id); | |
4660 | if (!hdev || hdev->dev_type != HCI_AMP || | |
4661 | !test_bit(HCI_UP, &hdev->flags)) { | |
4662 | if (hdev) | |
4663 | hci_dev_put(hdev); | |
4664 | ||
4665 | result = L2CAP_MR_BAD_ID; | |
4666 | goto send_move_response; | |
4667 | } | |
4668 | hci_dev_put(hdev); | |
4669 | } | |
4670 | ||
4671 | /* Detect a move collision. Only send a collision response | |
4672 | * if this side has "lost", otherwise proceed with the move. | |
4673 | * The winner has the larger bd_addr. | |
4674 | */ | |
4675 | if ((__chan_is_moving(chan) || | |
4676 | chan->move_role != L2CAP_MOVE_ROLE_NONE) && | |
4677 | bacmp(conn->src, conn->dst) > 0) { | |
4678 | result = L2CAP_MR_COLLISION; | |
4679 | goto send_move_response; | |
4680 | } | |
4681 | ||
02b0fbb9 MM |
4682 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
4683 | l2cap_move_setup(chan); | |
4684 | chan->move_id = req->dest_amp_id; | |
4685 | icid = chan->dcid; | |
4686 | ||
4687 | if (!req->dest_amp_id) { | |
4688 | /* Moving to BR/EDR */ | |
4689 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { | |
4690 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; | |
4691 | result = L2CAP_MR_PEND; | |
4692 | } else { | |
4693 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; | |
4694 | result = L2CAP_MR_SUCCESS; | |
4695 | } | |
4696 | } else { | |
4697 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; | |
4698 | /* Placeholder - uncomment when amp functions are available */ | |
4699 | /*amp_accept_physical(chan, req->dest_amp_id);*/ | |
4700 | result = L2CAP_MR_PEND; | |
4701 | } | |
4702 | ||
4703 | send_move_response: | |
1500109b | 4704 | l2cap_send_move_chan_rsp(chan, result); |
8d5a04a1 | 4705 | |
02b0fbb9 MM |
4706 | l2cap_chan_unlock(chan); |
4707 | ||
8d5a04a1 MM |
4708 | return 0; |
4709 | } | |
4710 | ||
5b155ef9 MM |
4711 | static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result) |
4712 | { | |
4713 | struct l2cap_chan *chan; | |
4714 | struct hci_chan *hchan = NULL; | |
4715 | ||
4716 | chan = l2cap_get_chan_by_scid(conn, icid); | |
4717 | if (!chan) { | |
4718 | l2cap_send_move_chan_cfm_icid(conn, icid); | |
4719 | return; | |
4720 | } | |
4721 | ||
4722 | __clear_chan_timer(chan); | |
4723 | if (result == L2CAP_MR_PEND) | |
4724 | __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT); | |
4725 | ||
4726 | switch (chan->move_state) { | |
4727 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: | |
4728 | /* Move confirm will be sent when logical link | |
4729 | * is complete. | |
4730 | */ | |
4731 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; | |
4732 | break; | |
4733 | case L2CAP_MOVE_WAIT_RSP_SUCCESS: | |
4734 | if (result == L2CAP_MR_PEND) { | |
4735 | break; | |
4736 | } else if (test_bit(CONN_LOCAL_BUSY, | |
4737 | &chan->conn_state)) { | |
4738 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; | |
4739 | } else { | |
4740 | /* Logical link is up or moving to BR/EDR, | |
4741 | * proceed with move | |
4742 | */ | |
4743 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; | |
4744 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); | |
4745 | } | |
4746 | break; | |
4747 | case L2CAP_MOVE_WAIT_RSP: | |
4748 | /* Moving to AMP */ | |
4749 | if (result == L2CAP_MR_SUCCESS) { | |
4750 | /* Remote is ready, send confirm immediately | |
4751 | * after logical link is ready | |
4752 | */ | |
4753 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; | |
4754 | } else { | |
4755 | /* Both logical link and move success | |
4756 | * are required to confirm | |
4757 | */ | |
4758 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP; | |
4759 | } | |
4760 | ||
4761 | /* Placeholder - get hci_chan for logical link */ | |
4762 | if (!hchan) { | |
4763 | /* Logical link not available */ | |
4764 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); | |
4765 | break; | |
4766 | } | |
4767 | ||
4768 | /* If the logical link is not yet connected, do not | |
4769 | * send confirmation. | |
4770 | */ | |
4771 | if (hchan->state != BT_CONNECTED) | |
4772 | break; | |
4773 | ||
4774 | /* Logical link is already ready to go */ | |
4775 | ||
4776 | chan->hs_hcon = hchan->conn; | |
4777 | chan->hs_hcon->l2cap_data = chan->conn; | |
4778 | ||
4779 | if (result == L2CAP_MR_SUCCESS) { | |
4780 | /* Can confirm now */ | |
4781 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); | |
4782 | } else { | |
4783 | /* Now only need move success | |
4784 | * to confirm | |
4785 | */ | |
4786 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; | |
4787 | } | |
4788 | ||
4789 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); | |
4790 | break; | |
4791 | default: | |
4792 | /* Any other amp move state means the move failed. */ | |
4793 | chan->move_id = chan->local_amp_id; | |
4794 | l2cap_move_done(chan); | |
4795 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); | |
4796 | } | |
4797 | ||
4798 | l2cap_chan_unlock(chan); | |
4799 | } | |
4800 | ||
4801 | static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid, | |
4802 | u16 result) | |
4803 | { | |
4804 | struct l2cap_chan *chan; | |
4805 | ||
4806 | chan = l2cap_get_chan_by_ident(conn, ident); | |
4807 | if (!chan) { | |
4808 | /* Could not locate channel, icid is best guess */ | |
4809 | l2cap_send_move_chan_cfm_icid(conn, icid); | |
4810 | return; | |
4811 | } | |
4812 | ||
4813 | __clear_chan_timer(chan); | |
4814 | ||
4815 | if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { | |
4816 | if (result == L2CAP_MR_COLLISION) { | |
4817 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; | |
4818 | } else { | |
4819 | /* Cleanup - cancel move */ | |
4820 | chan->move_id = chan->local_amp_id; | |
4821 | l2cap_move_done(chan); | |
4822 | } | |
4823 | } | |
4824 | ||
4825 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); | |
4826 | ||
4827 | l2cap_chan_unlock(chan); | |
4828 | } | |
4829 | ||
4830 | static int l2cap_move_channel_rsp(struct l2cap_conn *conn, | |
4831 | struct l2cap_cmd_hdr *cmd, | |
4832 | u16 cmd_len, void *data) | |
8d5a04a1 MM |
4833 | { |
4834 | struct l2cap_move_chan_rsp *rsp = data; | |
4835 | u16 icid, result; | |
4836 | ||
4837 | if (cmd_len != sizeof(*rsp)) | |
4838 | return -EPROTO; | |
4839 | ||
4840 | icid = le16_to_cpu(rsp->icid); | |
4841 | result = le16_to_cpu(rsp->result); | |
4842 | ||
ad0ac6ca | 4843 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
8d5a04a1 | 4844 | |
5b155ef9 MM |
4845 | if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND) |
4846 | l2cap_move_continue(conn, icid, result); | |
4847 | else | |
4848 | l2cap_move_fail(conn, cmd->ident, icid, result); | |
8d5a04a1 MM |
4849 | |
4850 | return 0; | |
4851 | } | |
4852 | ||
5f3847a4 MM |
4853 | static int l2cap_move_channel_confirm(struct l2cap_conn *conn, |
4854 | struct l2cap_cmd_hdr *cmd, | |
4855 | u16 cmd_len, void *data) | |
8d5a04a1 MM |
4856 | { |
4857 | struct l2cap_move_chan_cfm *cfm = data; | |
5f3847a4 | 4858 | struct l2cap_chan *chan; |
8d5a04a1 MM |
4859 | u16 icid, result; |
4860 | ||
4861 | if (cmd_len != sizeof(*cfm)) | |
4862 | return -EPROTO; | |
4863 | ||
4864 | icid = le16_to_cpu(cfm->icid); | |
4865 | result = le16_to_cpu(cfm->result); | |
4866 | ||
ad0ac6ca | 4867 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
8d5a04a1 | 4868 | |
5f3847a4 MM |
4869 | chan = l2cap_get_chan_by_dcid(conn, icid); |
4870 | if (!chan) { | |
4871 | /* Spec requires a response even if the icid was not found */ | |
4872 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); | |
4873 | return 0; | |
4874 | } | |
4875 | ||
4876 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) { | |
4877 | if (result == L2CAP_MC_CONFIRMED) { | |
4878 | chan->local_amp_id = chan->move_id; | |
4879 | if (!chan->local_amp_id) | |
4880 | __release_logical_link(chan); | |
4881 | } else { | |
4882 | chan->move_id = chan->local_amp_id; | |
4883 | } | |
4884 | ||
4885 | l2cap_move_done(chan); | |
4886 | } | |
4887 | ||
8d5a04a1 MM |
4888 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
4889 | ||
5f3847a4 MM |
4890 | l2cap_chan_unlock(chan); |
4891 | ||
8d5a04a1 MM |
4892 | return 0; |
4893 | } | |
4894 | ||
4895 | static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, | |
ad0ac6ca AE |
4896 | struct l2cap_cmd_hdr *cmd, |
4897 | u16 cmd_len, void *data) | |
8d5a04a1 MM |
4898 | { |
4899 | struct l2cap_move_chan_cfm_rsp *rsp = data; | |
3fd71a0a | 4900 | struct l2cap_chan *chan; |
8d5a04a1 MM |
4901 | u16 icid; |
4902 | ||
4903 | if (cmd_len != sizeof(*rsp)) | |
4904 | return -EPROTO; | |
4905 | ||
4906 | icid = le16_to_cpu(rsp->icid); | |
4907 | ||
ad0ac6ca | 4908 | BT_DBG("icid 0x%4.4x", icid); |
8d5a04a1 | 4909 | |
3fd71a0a MM |
4910 | chan = l2cap_get_chan_by_scid(conn, icid); |
4911 | if (!chan) | |
4912 | return 0; | |
4913 | ||
4914 | __clear_chan_timer(chan); | |
4915 | ||
4916 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) { | |
4917 | chan->local_amp_id = chan->move_id; | |
4918 | ||
4919 | if (!chan->local_amp_id && chan->hs_hchan) | |
4920 | __release_logical_link(chan); | |
4921 | ||
4922 | l2cap_move_done(chan); | |
4923 | } | |
4924 | ||
4925 | l2cap_chan_unlock(chan); | |
4926 | ||
8d5a04a1 MM |
4927 | return 0; |
4928 | } | |
4929 | ||
e2174ca4 | 4930 | static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency, |
2d792818 | 4931 | u16 to_multiplier) |
de73115a CT |
4932 | { |
4933 | u16 max_latency; | |
4934 | ||
4935 | if (min > max || min < 6 || max > 3200) | |
4936 | return -EINVAL; | |
4937 | ||
4938 | if (to_multiplier < 10 || to_multiplier > 3200) | |
4939 | return -EINVAL; | |
4940 | ||
4941 | if (max >= to_multiplier * 8) | |
4942 | return -EINVAL; | |
4943 | ||
4944 | max_latency = (to_multiplier * 8 / max) - 1; | |
4945 | if (latency > 499 || latency > max_latency) | |
4946 | return -EINVAL; | |
4947 | ||
4948 | return 0; | |
4949 | } | |
4950 | ||
4951 | static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, | |
2d792818 GP |
4952 | struct l2cap_cmd_hdr *cmd, |
4953 | u8 *data) | |
de73115a CT |
4954 | { |
4955 | struct hci_conn *hcon = conn->hcon; | |
4956 | struct l2cap_conn_param_update_req *req; | |
4957 | struct l2cap_conn_param_update_rsp rsp; | |
4958 | u16 min, max, latency, to_multiplier, cmd_len; | |
2ce603eb | 4959 | int err; |
de73115a CT |
4960 | |
4961 | if (!(hcon->link_mode & HCI_LM_MASTER)) | |
4962 | return -EINVAL; | |
4963 | ||
4964 | cmd_len = __le16_to_cpu(cmd->len); | |
4965 | if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) | |
4966 | return -EPROTO; | |
4967 | ||
4968 | req = (struct l2cap_conn_param_update_req *) data; | |
e2174ca4 GP |
4969 | min = __le16_to_cpu(req->min); |
4970 | max = __le16_to_cpu(req->max); | |
de73115a CT |
4971 | latency = __le16_to_cpu(req->latency); |
4972 | to_multiplier = __le16_to_cpu(req->to_multiplier); | |
4973 | ||
4974 | BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", | |
2d792818 | 4975 | min, max, latency, to_multiplier); |
de73115a CT |
4976 | |
4977 | memset(&rsp, 0, sizeof(rsp)); | |
2ce603eb CT |
4978 | |
4979 | err = l2cap_check_conn_param(min, max, latency, to_multiplier); | |
4980 | if (err) | |
ac73498c | 4981 | rsp.result = __constant_cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); |
de73115a | 4982 | else |
ac73498c | 4983 | rsp.result = __constant_cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); |
de73115a CT |
4984 | |
4985 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, | |
2d792818 | 4986 | sizeof(rsp), &rsp); |
de73115a | 4987 | |
2ce603eb CT |
4988 | if (!err) |
4989 | hci_le_conn_update(hcon, min, max, latency, to_multiplier); | |
4990 | ||
de73115a CT |
4991 | return 0; |
4992 | } | |
4993 | ||
3300d9a9 | 4994 | static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, |
2d792818 GP |
4995 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
4996 | u8 *data) | |
3300d9a9 CT |
4997 | { |
4998 | int err = 0; | |
4999 | ||
5000 | switch (cmd->code) { | |
5001 | case L2CAP_COMMAND_REJ: | |
5002 | l2cap_command_rej(conn, cmd, data); | |
5003 | break; | |
5004 | ||
5005 | case L2CAP_CONN_REQ: | |
5006 | err = l2cap_connect_req(conn, cmd, data); | |
5007 | break; | |
5008 | ||
5009 | case L2CAP_CONN_RSP: | |
f5a2598d | 5010 | case L2CAP_CREATE_CHAN_RSP: |
5909cf30 | 5011 | err = l2cap_connect_create_rsp(conn, cmd, data); |
3300d9a9 CT |
5012 | break; |
5013 | ||
5014 | case L2CAP_CONF_REQ: | |
5015 | err = l2cap_config_req(conn, cmd, cmd_len, data); | |
5016 | break; | |
5017 | ||
5018 | case L2CAP_CONF_RSP: | |
5019 | err = l2cap_config_rsp(conn, cmd, data); | |
5020 | break; | |
5021 | ||
5022 | case L2CAP_DISCONN_REQ: | |
5023 | err = l2cap_disconnect_req(conn, cmd, data); | |
5024 | break; | |
5025 | ||
5026 | case L2CAP_DISCONN_RSP: | |
5027 | err = l2cap_disconnect_rsp(conn, cmd, data); | |
5028 | break; | |
5029 | ||
5030 | case L2CAP_ECHO_REQ: | |
5031 | l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); | |
5032 | break; | |
5033 | ||
5034 | case L2CAP_ECHO_RSP: | |
5035 | break; | |
5036 | ||
5037 | case L2CAP_INFO_REQ: | |
5038 | err = l2cap_information_req(conn, cmd, data); | |
5039 | break; | |
5040 | ||
5041 | case L2CAP_INFO_RSP: | |
5042 | err = l2cap_information_rsp(conn, cmd, data); | |
5043 | break; | |
5044 | ||
f94ff6ff MM |
5045 | case L2CAP_CREATE_CHAN_REQ: |
5046 | err = l2cap_create_channel_req(conn, cmd, cmd_len, data); | |
5047 | break; | |
5048 | ||
8d5a04a1 MM |
5049 | case L2CAP_MOVE_CHAN_REQ: |
5050 | err = l2cap_move_channel_req(conn, cmd, cmd_len, data); | |
5051 | break; | |
5052 | ||
5053 | case L2CAP_MOVE_CHAN_RSP: | |
5054 | err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data); | |
5055 | break; | |
5056 | ||
5057 | case L2CAP_MOVE_CHAN_CFM: | |
5058 | err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); | |
5059 | break; | |
5060 | ||
5061 | case L2CAP_MOVE_CHAN_CFM_RSP: | |
5062 | err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); | |
5063 | break; | |
5064 | ||
3300d9a9 CT |
5065 | default: |
5066 | BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); | |
5067 | err = -EINVAL; | |
5068 | break; | |
5069 | } | |
5070 | ||
5071 | return err; | |
5072 | } | |
5073 | ||
5074 | static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, | |
2d792818 | 5075 | struct l2cap_cmd_hdr *cmd, u8 *data) |
3300d9a9 CT |
5076 | { |
5077 | switch (cmd->code) { | |
5078 | case L2CAP_COMMAND_REJ: | |
5079 | return 0; | |
5080 | ||
5081 | case L2CAP_CONN_PARAM_UPDATE_REQ: | |
de73115a | 5082 | return l2cap_conn_param_update_req(conn, cmd, data); |
3300d9a9 CT |
5083 | |
5084 | case L2CAP_CONN_PARAM_UPDATE_RSP: | |
5085 | return 0; | |
5086 | ||
5087 | default: | |
5088 | BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); | |
5089 | return -EINVAL; | |
5090 | } | |
5091 | } | |
5092 | ||
5093 | static inline void l2cap_sig_channel(struct l2cap_conn *conn, | |
2d792818 | 5094 | struct sk_buff *skb) |
1da177e4 LT |
5095 | { |
5096 | u8 *data = skb->data; | |
5097 | int len = skb->len; | |
5098 | struct l2cap_cmd_hdr cmd; | |
3300d9a9 | 5099 | int err; |
1da177e4 LT |
5100 | |
5101 | l2cap_raw_recv(conn, skb); | |
5102 | ||
5103 | while (len >= L2CAP_CMD_HDR_SIZE) { | |
88219a0f | 5104 | u16 cmd_len; |
1da177e4 LT |
5105 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
5106 | data += L2CAP_CMD_HDR_SIZE; | |
5107 | len -= L2CAP_CMD_HDR_SIZE; | |
5108 | ||
88219a0f | 5109 | cmd_len = le16_to_cpu(cmd.len); |
1da177e4 | 5110 | |
2d792818 GP |
5111 | BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, |
5112 | cmd.ident); | |
1da177e4 | 5113 | |
88219a0f | 5114 | if (cmd_len > len || !cmd.ident) { |
1da177e4 LT |
5115 | BT_DBG("corrupted command"); |
5116 | break; | |
5117 | } | |
5118 | ||
3300d9a9 CT |
5119 | if (conn->hcon->type == LE_LINK) |
5120 | err = l2cap_le_sig_cmd(conn, &cmd, data); | |
5121 | else | |
5122 | err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); | |
1da177e4 LT |
5123 | |
5124 | if (err) { | |
e2fd318e | 5125 | struct l2cap_cmd_rej_unk rej; |
2c6d1a2e GP |
5126 | |
5127 | BT_ERR("Wrong link type (%d)", err); | |
1da177e4 LT |
5128 | |
5129 | /* FIXME: Map err to a valid reason */ | |
ac73498c | 5130 | rej.reason = __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
2d792818 GP |
5131 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, |
5132 | sizeof(rej), &rej); | |
1da177e4 LT |
5133 | } |
5134 | ||
88219a0f AV |
5135 | data += cmd_len; |
5136 | len -= cmd_len; | |
1da177e4 LT |
5137 | } |
5138 | ||
5139 | kfree_skb(skb); | |
5140 | } | |
5141 | ||
47d1ec61 | 5142 | static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb) |
fcc203c3 GP |
5143 | { |
5144 | u16 our_fcs, rcv_fcs; | |
e4ca6d98 AE |
5145 | int hdr_size; |
5146 | ||
5147 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) | |
5148 | hdr_size = L2CAP_EXT_HDR_SIZE; | |
5149 | else | |
5150 | hdr_size = L2CAP_ENH_HDR_SIZE; | |
fcc203c3 | 5151 | |
47d1ec61 | 5152 | if (chan->fcs == L2CAP_FCS_CRC16) { |
03a51213 | 5153 | skb_trim(skb, skb->len - L2CAP_FCS_SIZE); |
fcc203c3 GP |
5154 | rcv_fcs = get_unaligned_le16(skb->data + skb->len); |
5155 | our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); | |
5156 | ||
5157 | if (our_fcs != rcv_fcs) | |
7a560e5c | 5158 | return -EBADMSG; |
fcc203c3 GP |
5159 | } |
5160 | return 0; | |
5161 | } | |
5162 | ||
6ea00485 | 5163 | static void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan) |
d5392c8f | 5164 | { |
e31f7633 | 5165 | struct l2cap_ctrl control; |
d5392c8f | 5166 | |
e31f7633 | 5167 | BT_DBG("chan %p", chan); |
d5392c8f | 5168 | |
e31f7633 MM |
5169 | memset(&control, 0, sizeof(control)); |
5170 | control.sframe = 1; | |
5171 | control.final = 1; | |
5172 | control.reqseq = chan->buffer_seq; | |
5173 | set_bit(CONN_SEND_FBIT, &chan->conn_state); | |
d5392c8f | 5174 | |
e2ab4353 | 5175 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
e31f7633 MM |
5176 | control.super = L2CAP_SUPER_RNR; |
5177 | l2cap_send_sframe(chan, &control); | |
d5392c8f GP |
5178 | } |
5179 | ||
e31f7633 MM |
5180 | if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
5181 | chan->unacked_frames > 0) | |
5182 | __set_retrans_timer(chan); | |
d5392c8f | 5183 | |
e31f7633 | 5184 | /* Send pending iframes */ |
525cd185 | 5185 | l2cap_ertm_send(chan); |
d5392c8f | 5186 | |
e2ab4353 | 5187 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
e31f7633 MM |
5188 | test_bit(CONN_SEND_FBIT, &chan->conn_state)) { |
5189 | /* F-bit wasn't sent in an s-frame or i-frame yet, so | |
5190 | * send it now. | |
5191 | */ | |
5192 | control.super = L2CAP_SUPER_RR; | |
5193 | l2cap_send_sframe(chan, &control); | |
d5392c8f GP |
5194 | } |
5195 | } | |
5196 | ||
2d792818 GP |
5197 | static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag, |
5198 | struct sk_buff **last_frag) | |
18778a63 | 5199 | { |
84084a31 MM |
5200 | /* skb->len reflects data in skb as well as all fragments |
5201 | * skb->data_len reflects only data in fragments | |
5202 | */ | |
5203 | if (!skb_has_frag_list(skb)) | |
5204 | skb_shinfo(skb)->frag_list = new_frag; | |
5205 | ||
5206 | new_frag->next = NULL; | |
5207 | ||
5208 | (*last_frag)->next = new_frag; | |
5209 | *last_frag = new_frag; | |
5210 | ||
5211 | skb->len += new_frag->len; | |
5212 | skb->data_len += new_frag->len; | |
5213 | skb->truesize += new_frag->truesize; | |
5214 | } | |
5215 | ||
4b51dae9 MM |
5216 | static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, |
5217 | struct l2cap_ctrl *control) | |
84084a31 MM |
5218 | { |
5219 | int err = -EINVAL; | |
18778a63 | 5220 | |
4b51dae9 | 5221 | switch (control->sar) { |
7e0ef6ee | 5222 | case L2CAP_SAR_UNSEGMENTED: |
84084a31 MM |
5223 | if (chan->sdu) |
5224 | break; | |
18778a63 | 5225 | |
80b98027 | 5226 | err = chan->ops->recv(chan, skb); |
84084a31 | 5227 | break; |
18778a63 | 5228 | |
7e0ef6ee | 5229 | case L2CAP_SAR_START: |
84084a31 MM |
5230 | if (chan->sdu) |
5231 | break; | |
18778a63 | 5232 | |
6f61fd47 | 5233 | chan->sdu_len = get_unaligned_le16(skb->data); |
03a51213 | 5234 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
18778a63 | 5235 | |
84084a31 MM |
5236 | if (chan->sdu_len > chan->imtu) { |
5237 | err = -EMSGSIZE; | |
5238 | break; | |
5239 | } | |
1890d36b | 5240 | |
84084a31 MM |
5241 | if (skb->len >= chan->sdu_len) |
5242 | break; | |
18778a63 | 5243 | |
84084a31 MM |
5244 | chan->sdu = skb; |
5245 | chan->sdu_last_frag = skb; | |
18778a63 | 5246 | |
84084a31 MM |
5247 | skb = NULL; |
5248 | err = 0; | |
18778a63 GP |
5249 | break; |
5250 | ||
7e0ef6ee | 5251 | case L2CAP_SAR_CONTINUE: |
6f61fd47 | 5252 | if (!chan->sdu) |
84084a31 | 5253 | break; |
18778a63 | 5254 | |
84084a31 MM |
5255 | append_skb_frag(chan->sdu, skb, |
5256 | &chan->sdu_last_frag); | |
5257 | skb = NULL; | |
18778a63 | 5258 | |
84084a31 MM |
5259 | if (chan->sdu->len >= chan->sdu_len) |
5260 | break; | |
4178ba46 | 5261 | |
84084a31 | 5262 | err = 0; |
18778a63 GP |
5263 | break; |
5264 | ||
7e0ef6ee | 5265 | case L2CAP_SAR_END: |
6f61fd47 | 5266 | if (!chan->sdu) |
84084a31 | 5267 | break; |
18778a63 | 5268 | |
84084a31 MM |
5269 | append_skb_frag(chan->sdu, skb, |
5270 | &chan->sdu_last_frag); | |
5271 | skb = NULL; | |
4178ba46 | 5272 | |
84084a31 MM |
5273 | if (chan->sdu->len != chan->sdu_len) |
5274 | break; | |
18778a63 | 5275 | |
80b98027 | 5276 | err = chan->ops->recv(chan, chan->sdu); |
1890d36b | 5277 | |
84084a31 MM |
5278 | if (!err) { |
5279 | /* Reassembly complete */ | |
5280 | chan->sdu = NULL; | |
5281 | chan->sdu_last_frag = NULL; | |
5282 | chan->sdu_len = 0; | |
1890d36b | 5283 | } |
18778a63 GP |
5284 | break; |
5285 | } | |
5286 | ||
84084a31 MM |
5287 | if (err) { |
5288 | kfree_skb(skb); | |
5289 | kfree_skb(chan->sdu); | |
5290 | chan->sdu = NULL; | |
5291 | chan->sdu_last_frag = NULL; | |
5292 | chan->sdu_len = 0; | |
5293 | } | |
18778a63 | 5294 | |
84084a31 | 5295 | return err; |
18778a63 GP |
5296 | } |
5297 | ||
32b32735 MM |
5298 | static int l2cap_resegment(struct l2cap_chan *chan) |
5299 | { | |
5300 | /* Placeholder */ | |
5301 | return 0; | |
5302 | } | |
5303 | ||
61aa4f5b | 5304 | void l2cap_chan_busy(struct l2cap_chan *chan, int busy) |
26f880d2 | 5305 | { |
61aa4f5b | 5306 | u8 event; |
712132eb | 5307 | |
61aa4f5b MM |
5308 | if (chan->mode != L2CAP_MODE_ERTM) |
5309 | return; | |
712132eb | 5310 | |
61aa4f5b | 5311 | event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR; |
401bb1f7 | 5312 | l2cap_tx(chan, NULL, NULL, event); |
1890d36b GP |
5313 | } |
5314 | ||
d2a7ac5d MM |
5315 | static int l2cap_rx_queued_iframes(struct l2cap_chan *chan) |
5316 | { | |
63838725 MM |
5317 | int err = 0; |
5318 | /* Pass sequential frames to l2cap_reassemble_sdu() | |
5319 | * until a gap is encountered. | |
5320 | */ | |
5321 | ||
5322 | BT_DBG("chan %p", chan); | |
5323 | ||
5324 | while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { | |
5325 | struct sk_buff *skb; | |
5326 | BT_DBG("Searching for skb with txseq %d (queue len %d)", | |
5327 | chan->buffer_seq, skb_queue_len(&chan->srej_q)); | |
5328 | ||
5329 | skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq); | |
5330 | ||
5331 | if (!skb) | |
5332 | break; | |
5333 | ||
5334 | skb_unlink(skb, &chan->srej_q); | |
5335 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); | |
5336 | err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->control); | |
5337 | if (err) | |
5338 | break; | |
5339 | } | |
5340 | ||
5341 | if (skb_queue_empty(&chan->srej_q)) { | |
5342 | chan->rx_state = L2CAP_RX_STATE_RECV; | |
5343 | l2cap_send_ack(chan); | |
5344 | } | |
5345 | ||
5346 | return err; | |
d2a7ac5d MM |
5347 | } |
5348 | ||
5349 | static void l2cap_handle_srej(struct l2cap_chan *chan, | |
5350 | struct l2cap_ctrl *control) | |
5351 | { | |
f80842a8 MM |
5352 | struct sk_buff *skb; |
5353 | ||
5354 | BT_DBG("chan %p, control %p", chan, control); | |
5355 | ||
5356 | if (control->reqseq == chan->next_tx_seq) { | |
5357 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); | |
5358 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); | |
5359 | return; | |
5360 | } | |
5361 | ||
5362 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); | |
5363 | ||
5364 | if (skb == NULL) { | |
5365 | BT_DBG("Seq %d not available for retransmission", | |
5366 | control->reqseq); | |
5367 | return; | |
5368 | } | |
5369 | ||
5370 | if (chan->max_tx != 0 && bt_cb(skb)->control.retries >= chan->max_tx) { | |
5371 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); | |
5372 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); | |
5373 | return; | |
5374 | } | |
5375 | ||
5376 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); | |
5377 | ||
5378 | if (control->poll) { | |
5379 | l2cap_pass_to_tx(chan, control); | |
5380 | ||
5381 | set_bit(CONN_SEND_FBIT, &chan->conn_state); | |
5382 | l2cap_retransmit(chan, control); | |
5383 | l2cap_ertm_send(chan); | |
5384 | ||
5385 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { | |
5386 | set_bit(CONN_SREJ_ACT, &chan->conn_state); | |
5387 | chan->srej_save_reqseq = control->reqseq; | |
5388 | } | |
5389 | } else { | |
5390 | l2cap_pass_to_tx_fbit(chan, control); | |
5391 | ||
5392 | if (control->final) { | |
5393 | if (chan->srej_save_reqseq != control->reqseq || | |
5394 | !test_and_clear_bit(CONN_SREJ_ACT, | |
5395 | &chan->conn_state)) | |
5396 | l2cap_retransmit(chan, control); | |
5397 | } else { | |
5398 | l2cap_retransmit(chan, control); | |
5399 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { | |
5400 | set_bit(CONN_SREJ_ACT, &chan->conn_state); | |
5401 | chan->srej_save_reqseq = control->reqseq; | |
5402 | } | |
5403 | } | |
5404 | } | |
d2a7ac5d MM |
5405 | } |
5406 | ||
5407 | static void l2cap_handle_rej(struct l2cap_chan *chan, | |
5408 | struct l2cap_ctrl *control) | |
5409 | { | |
fcd289df MM |
5410 | struct sk_buff *skb; |
5411 | ||
5412 | BT_DBG("chan %p, control %p", chan, control); | |
5413 | ||
5414 | if (control->reqseq == chan->next_tx_seq) { | |
5415 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); | |
5416 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); | |
5417 | return; | |
5418 | } | |
5419 | ||
5420 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); | |
5421 | ||
5422 | if (chan->max_tx && skb && | |
5423 | bt_cb(skb)->control.retries >= chan->max_tx) { | |
5424 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); | |
5425 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); | |
5426 | return; | |
5427 | } | |
5428 | ||
5429 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); | |
5430 | ||
5431 | l2cap_pass_to_tx(chan, control); | |
5432 | ||
5433 | if (control->final) { | |
5434 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) | |
5435 | l2cap_retransmit_all(chan, control); | |
5436 | } else { | |
5437 | l2cap_retransmit_all(chan, control); | |
5438 | l2cap_ertm_send(chan); | |
5439 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) | |
5440 | set_bit(CONN_REJ_ACT, &chan->conn_state); | |
5441 | } | |
d2a7ac5d MM |
5442 | } |
5443 | ||
4b51dae9 MM |
5444 | static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq) |
5445 | { | |
5446 | BT_DBG("chan %p, txseq %d", chan, txseq); | |
5447 | ||
5448 | BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq, | |
5449 | chan->expected_tx_seq); | |
5450 | ||
5451 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { | |
5452 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= | |
2d792818 | 5453 | chan->tx_win) { |
4b51dae9 MM |
5454 | /* See notes below regarding "double poll" and |
5455 | * invalid packets. | |
5456 | */ | |
5457 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { | |
5458 | BT_DBG("Invalid/Ignore - after SREJ"); | |
5459 | return L2CAP_TXSEQ_INVALID_IGNORE; | |
5460 | } else { | |
5461 | BT_DBG("Invalid - in window after SREJ sent"); | |
5462 | return L2CAP_TXSEQ_INVALID; | |
5463 | } | |
5464 | } | |
5465 | ||
5466 | if (chan->srej_list.head == txseq) { | |
5467 | BT_DBG("Expected SREJ"); | |
5468 | return L2CAP_TXSEQ_EXPECTED_SREJ; | |
5469 | } | |
5470 | ||
5471 | if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) { | |
5472 | BT_DBG("Duplicate SREJ - txseq already stored"); | |
5473 | return L2CAP_TXSEQ_DUPLICATE_SREJ; | |
5474 | } | |
5475 | ||
5476 | if (l2cap_seq_list_contains(&chan->srej_list, txseq)) { | |
5477 | BT_DBG("Unexpected SREJ - not requested"); | |
5478 | return L2CAP_TXSEQ_UNEXPECTED_SREJ; | |
5479 | } | |
5480 | } | |
5481 | ||
5482 | if (chan->expected_tx_seq == txseq) { | |
5483 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= | |
5484 | chan->tx_win) { | |
5485 | BT_DBG("Invalid - txseq outside tx window"); | |
5486 | return L2CAP_TXSEQ_INVALID; | |
5487 | } else { | |
5488 | BT_DBG("Expected"); | |
5489 | return L2CAP_TXSEQ_EXPECTED; | |
5490 | } | |
5491 | } | |
5492 | ||
5493 | if (__seq_offset(chan, txseq, chan->last_acked_seq) < | |
2d792818 | 5494 | __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) { |
4b51dae9 MM |
5495 | BT_DBG("Duplicate - expected_tx_seq later than txseq"); |
5496 | return L2CAP_TXSEQ_DUPLICATE; | |
5497 | } | |
5498 | ||
5499 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) { | |
5500 | /* A source of invalid packets is a "double poll" condition, | |
5501 | * where delays cause us to send multiple poll packets. If | |
5502 | * the remote stack receives and processes both polls, | |
5503 | * sequence numbers can wrap around in such a way that a | |
5504 | * resent frame has a sequence number that looks like new data | |
5505 | * with a sequence gap. This would trigger an erroneous SREJ | |
5506 | * request. | |
5507 | * | |
5508 | * Fortunately, this is impossible with a tx window that's | |
5509 | * less than half of the maximum sequence number, which allows | |
5510 | * invalid frames to be safely ignored. | |
5511 | * | |
5512 | * With tx window sizes greater than half of the tx window | |
5513 | * maximum, the frame is invalid and cannot be ignored. This | |
5514 | * causes a disconnect. | |
5515 | */ | |
5516 | ||
5517 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { | |
5518 | BT_DBG("Invalid/Ignore - txseq outside tx window"); | |
5519 | return L2CAP_TXSEQ_INVALID_IGNORE; | |
5520 | } else { | |
5521 | BT_DBG("Invalid - txseq outside tx window"); | |
5522 | return L2CAP_TXSEQ_INVALID; | |
5523 | } | |
5524 | } else { | |
5525 | BT_DBG("Unexpected - txseq indicates missing frames"); | |
5526 | return L2CAP_TXSEQ_UNEXPECTED; | |
5527 | } | |
5528 | } | |
5529 | ||
d2a7ac5d MM |
5530 | static int l2cap_rx_state_recv(struct l2cap_chan *chan, |
5531 | struct l2cap_ctrl *control, | |
5532 | struct sk_buff *skb, u8 event) | |
5533 | { | |
5534 | int err = 0; | |
5535 | bool skb_in_use = 0; | |
5536 | ||
5537 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, | |
5538 | event); | |
5539 | ||
5540 | switch (event) { | |
5541 | case L2CAP_EV_RECV_IFRAME: | |
5542 | switch (l2cap_classify_txseq(chan, control->txseq)) { | |
5543 | case L2CAP_TXSEQ_EXPECTED: | |
5544 | l2cap_pass_to_tx(chan, control); | |
5545 | ||
5546 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { | |
5547 | BT_DBG("Busy, discarding expected seq %d", | |
5548 | control->txseq); | |
5549 | break; | |
5550 | } | |
5551 | ||
5552 | chan->expected_tx_seq = __next_seq(chan, | |
5553 | control->txseq); | |
5554 | ||
5555 | chan->buffer_seq = chan->expected_tx_seq; | |
5556 | skb_in_use = 1; | |
5557 | ||
5558 | err = l2cap_reassemble_sdu(chan, skb, control); | |
5559 | if (err) | |
5560 | break; | |
5561 | ||
5562 | if (control->final) { | |
5563 | if (!test_and_clear_bit(CONN_REJ_ACT, | |
5564 | &chan->conn_state)) { | |
5565 | control->final = 0; | |
5566 | l2cap_retransmit_all(chan, control); | |
5567 | l2cap_ertm_send(chan); | |
5568 | } | |
5569 | } | |
5570 | ||
5571 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) | |
5572 | l2cap_send_ack(chan); | |
5573 | break; | |
5574 | case L2CAP_TXSEQ_UNEXPECTED: | |
5575 | l2cap_pass_to_tx(chan, control); | |
5576 | ||
5577 | /* Can't issue SREJ frames in the local busy state. | |
5578 | * Drop this frame, it will be seen as missing | |
5579 | * when local busy is exited. | |
5580 | */ | |
5581 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { | |
5582 | BT_DBG("Busy, discarding unexpected seq %d", | |
5583 | control->txseq); | |
5584 | break; | |
5585 | } | |
5586 | ||
5587 | /* There was a gap in the sequence, so an SREJ | |
5588 | * must be sent for each missing frame. The | |
5589 | * current frame is stored for later use. | |
5590 | */ | |
5591 | skb_queue_tail(&chan->srej_q, skb); | |
5592 | skb_in_use = 1; | |
5593 | BT_DBG("Queued %p (queue len %d)", skb, | |
5594 | skb_queue_len(&chan->srej_q)); | |
5595 | ||
5596 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); | |
5597 | l2cap_seq_list_clear(&chan->srej_list); | |
5598 | l2cap_send_srej(chan, control->txseq); | |
5599 | ||
5600 | chan->rx_state = L2CAP_RX_STATE_SREJ_SENT; | |
5601 | break; | |
5602 | case L2CAP_TXSEQ_DUPLICATE: | |
5603 | l2cap_pass_to_tx(chan, control); | |
5604 | break; | |
5605 | case L2CAP_TXSEQ_INVALID_IGNORE: | |
5606 | break; | |
5607 | case L2CAP_TXSEQ_INVALID: | |
5608 | default: | |
5609 | l2cap_send_disconn_req(chan->conn, chan, | |
5610 | ECONNRESET); | |
5611 | break; | |
5612 | } | |
5613 | break; | |
5614 | case L2CAP_EV_RECV_RR: | |
5615 | l2cap_pass_to_tx(chan, control); | |
5616 | if (control->final) { | |
5617 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); | |
5618 | ||
e6a3ee6e MM |
5619 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) && |
5620 | !__chan_is_moving(chan)) { | |
d2a7ac5d MM |
5621 | control->final = 0; |
5622 | l2cap_retransmit_all(chan, control); | |
5623 | } | |
5624 | ||
5625 | l2cap_ertm_send(chan); | |
5626 | } else if (control->poll) { | |
5627 | l2cap_send_i_or_rr_or_rnr(chan); | |
5628 | } else { | |
5629 | if (test_and_clear_bit(CONN_REMOTE_BUSY, | |
5630 | &chan->conn_state) && | |
5631 | chan->unacked_frames) | |
5632 | __set_retrans_timer(chan); | |
5633 | ||
5634 | l2cap_ertm_send(chan); | |
5635 | } | |
5636 | break; | |
5637 | case L2CAP_EV_RECV_RNR: | |
5638 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); | |
5639 | l2cap_pass_to_tx(chan, control); | |
5640 | if (control && control->poll) { | |
5641 | set_bit(CONN_SEND_FBIT, &chan->conn_state); | |
5642 | l2cap_send_rr_or_rnr(chan, 0); | |
5643 | } | |
5644 | __clear_retrans_timer(chan); | |
5645 | l2cap_seq_list_clear(&chan->retrans_list); | |
5646 | break; | |
5647 | case L2CAP_EV_RECV_REJ: | |
5648 | l2cap_handle_rej(chan, control); | |
5649 | break; | |
5650 | case L2CAP_EV_RECV_SREJ: | |
5651 | l2cap_handle_srej(chan, control); | |
5652 | break; | |
5653 | default: | |
5654 | break; | |
5655 | } | |
5656 | ||
5657 | if (skb && !skb_in_use) { | |
5658 | BT_DBG("Freeing %p", skb); | |
5659 | kfree_skb(skb); | |
5660 | } | |
5661 | ||
5662 | return err; | |
5663 | } | |
5664 | ||
5665 | static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan, | |
5666 | struct l2cap_ctrl *control, | |
5667 | struct sk_buff *skb, u8 event) | |
5668 | { | |
5669 | int err = 0; | |
5670 | u16 txseq = control->txseq; | |
5671 | bool skb_in_use = 0; | |
5672 | ||
5673 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, | |
5674 | event); | |
5675 | ||
5676 | switch (event) { | |
5677 | case L2CAP_EV_RECV_IFRAME: | |
5678 | switch (l2cap_classify_txseq(chan, txseq)) { | |
5679 | case L2CAP_TXSEQ_EXPECTED: | |
5680 | /* Keep frame for reassembly later */ | |
5681 | l2cap_pass_to_tx(chan, control); | |
5682 | skb_queue_tail(&chan->srej_q, skb); | |
5683 | skb_in_use = 1; | |
5684 | BT_DBG("Queued %p (queue len %d)", skb, | |
5685 | skb_queue_len(&chan->srej_q)); | |
5686 | ||
5687 | chan->expected_tx_seq = __next_seq(chan, txseq); | |
5688 | break; | |
5689 | case L2CAP_TXSEQ_EXPECTED_SREJ: | |
5690 | l2cap_seq_list_pop(&chan->srej_list); | |
5691 | ||
5692 | l2cap_pass_to_tx(chan, control); | |
5693 | skb_queue_tail(&chan->srej_q, skb); | |
5694 | skb_in_use = 1; | |
5695 | BT_DBG("Queued %p (queue len %d)", skb, | |
5696 | skb_queue_len(&chan->srej_q)); | |
5697 | ||
5698 | err = l2cap_rx_queued_iframes(chan); | |
5699 | if (err) | |
5700 | break; | |
5701 | ||
5702 | break; | |
5703 | case L2CAP_TXSEQ_UNEXPECTED: | |
5704 | /* Got a frame that can't be reassembled yet. | |
5705 | * Save it for later, and send SREJs to cover | |
5706 | * the missing frames. | |
5707 | */ | |
5708 | skb_queue_tail(&chan->srej_q, skb); | |
5709 | skb_in_use = 1; | |
5710 | BT_DBG("Queued %p (queue len %d)", skb, | |
5711 | skb_queue_len(&chan->srej_q)); | |
5712 | ||
5713 | l2cap_pass_to_tx(chan, control); | |
5714 | l2cap_send_srej(chan, control->txseq); | |
5715 | break; | |
5716 | case L2CAP_TXSEQ_UNEXPECTED_SREJ: | |
5717 | /* This frame was requested with an SREJ, but | |
5718 | * some expected retransmitted frames are | |
5719 | * missing. Request retransmission of missing | |
5720 | * SREJ'd frames. | |
5721 | */ | |
5722 | skb_queue_tail(&chan->srej_q, skb); | |
5723 | skb_in_use = 1; | |
5724 | BT_DBG("Queued %p (queue len %d)", skb, | |
5725 | skb_queue_len(&chan->srej_q)); | |
5726 | ||
5727 | l2cap_pass_to_tx(chan, control); | |
5728 | l2cap_send_srej_list(chan, control->txseq); | |
5729 | break; | |
5730 | case L2CAP_TXSEQ_DUPLICATE_SREJ: | |
5731 | /* We've already queued this frame. Drop this copy. */ | |
5732 | l2cap_pass_to_tx(chan, control); | |
5733 | break; | |
5734 | case L2CAP_TXSEQ_DUPLICATE: | |
5735 | /* Expecting a later sequence number, so this frame | |
5736 | * was already received. Ignore it completely. | |
5737 | */ | |
5738 | break; | |
5739 | case L2CAP_TXSEQ_INVALID_IGNORE: | |
5740 | break; | |
5741 | case L2CAP_TXSEQ_INVALID: | |
5742 | default: | |
5743 | l2cap_send_disconn_req(chan->conn, chan, | |
5744 | ECONNRESET); | |
5745 | break; | |
5746 | } | |
5747 | break; | |
5748 | case L2CAP_EV_RECV_RR: | |
5749 | l2cap_pass_to_tx(chan, control); | |
5750 | if (control->final) { | |
5751 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); | |
5752 | ||
5753 | if (!test_and_clear_bit(CONN_REJ_ACT, | |
5754 | &chan->conn_state)) { | |
5755 | control->final = 0; | |
5756 | l2cap_retransmit_all(chan, control); | |
5757 | } | |
5758 | ||
5759 | l2cap_ertm_send(chan); | |
5760 | } else if (control->poll) { | |
5761 | if (test_and_clear_bit(CONN_REMOTE_BUSY, | |
5762 | &chan->conn_state) && | |
5763 | chan->unacked_frames) { | |
5764 | __set_retrans_timer(chan); | |
5765 | } | |
5766 | ||
5767 | set_bit(CONN_SEND_FBIT, &chan->conn_state); | |
5768 | l2cap_send_srej_tail(chan); | |
5769 | } else { | |
5770 | if (test_and_clear_bit(CONN_REMOTE_BUSY, | |
5771 | &chan->conn_state) && | |
5772 | chan->unacked_frames) | |
5773 | __set_retrans_timer(chan); | |
5774 | ||
5775 | l2cap_send_ack(chan); | |
5776 | } | |
5777 | break; | |
5778 | case L2CAP_EV_RECV_RNR: | |
5779 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); | |
5780 | l2cap_pass_to_tx(chan, control); | |
5781 | if (control->poll) { | |
5782 | l2cap_send_srej_tail(chan); | |
5783 | } else { | |
5784 | struct l2cap_ctrl rr_control; | |
5785 | memset(&rr_control, 0, sizeof(rr_control)); | |
5786 | rr_control.sframe = 1; | |
5787 | rr_control.super = L2CAP_SUPER_RR; | |
5788 | rr_control.reqseq = chan->buffer_seq; | |
5789 | l2cap_send_sframe(chan, &rr_control); | |
5790 | } | |
5791 | ||
5792 | break; | |
5793 | case L2CAP_EV_RECV_REJ: | |
5794 | l2cap_handle_rej(chan, control); | |
5795 | break; | |
5796 | case L2CAP_EV_RECV_SREJ: | |
5797 | l2cap_handle_srej(chan, control); | |
5798 | break; | |
5799 | } | |
5800 | ||
5801 | if (skb && !skb_in_use) { | |
5802 | BT_DBG("Freeing %p", skb); | |
5803 | kfree_skb(skb); | |
5804 | } | |
5805 | ||
5806 | return err; | |
5807 | } | |
5808 | ||
32b32735 MM |
5809 | static int l2cap_finish_move(struct l2cap_chan *chan) |
5810 | { | |
5811 | BT_DBG("chan %p", chan); | |
5812 | ||
5813 | chan->rx_state = L2CAP_RX_STATE_RECV; | |
5814 | ||
5815 | if (chan->hs_hcon) | |
5816 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; | |
5817 | else | |
5818 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; | |
5819 | ||
5820 | return l2cap_resegment(chan); | |
5821 | } | |
5822 | ||
5823 | static int l2cap_rx_state_wait_p(struct l2cap_chan *chan, | |
5824 | struct l2cap_ctrl *control, | |
5825 | struct sk_buff *skb, u8 event) | |
5826 | { | |
5827 | int err; | |
5828 | ||
5829 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, | |
5830 | event); | |
5831 | ||
5832 | if (!control->poll) | |
5833 | return -EPROTO; | |
5834 | ||
5835 | l2cap_process_reqseq(chan, control->reqseq); | |
5836 | ||
5837 | if (!skb_queue_empty(&chan->tx_q)) | |
5838 | chan->tx_send_head = skb_peek(&chan->tx_q); | |
5839 | else | |
5840 | chan->tx_send_head = NULL; | |
5841 | ||
5842 | /* Rewind next_tx_seq to the point expected | |
5843 | * by the receiver. | |
5844 | */ | |
5845 | chan->next_tx_seq = control->reqseq; | |
5846 | chan->unacked_frames = 0; | |
5847 | ||
5848 | err = l2cap_finish_move(chan); | |
5849 | if (err) | |
5850 | return err; | |
5851 | ||
5852 | set_bit(CONN_SEND_FBIT, &chan->conn_state); | |
5853 | l2cap_send_i_or_rr_or_rnr(chan); | |
5854 | ||
5855 | if (event == L2CAP_EV_RECV_IFRAME) | |
5856 | return -EPROTO; | |
5857 | ||
5858 | return l2cap_rx_state_recv(chan, control, NULL, event); | |
5859 | } | |
5860 | ||
5861 | static int l2cap_rx_state_wait_f(struct l2cap_chan *chan, | |
5862 | struct l2cap_ctrl *control, | |
5863 | struct sk_buff *skb, u8 event) | |
5864 | { | |
5865 | int err; | |
5866 | ||
5867 | if (!control->final) | |
5868 | return -EPROTO; | |
5869 | ||
5870 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); | |
5871 | ||
5872 | chan->rx_state = L2CAP_RX_STATE_RECV; | |
5873 | l2cap_process_reqseq(chan, control->reqseq); | |
5874 | ||
5875 | if (!skb_queue_empty(&chan->tx_q)) | |
5876 | chan->tx_send_head = skb_peek(&chan->tx_q); | |
5877 | else | |
5878 | chan->tx_send_head = NULL; | |
5879 | ||
5880 | /* Rewind next_tx_seq to the point expected | |
5881 | * by the receiver. | |
5882 | */ | |
5883 | chan->next_tx_seq = control->reqseq; | |
5884 | chan->unacked_frames = 0; | |
5885 | ||
5886 | if (chan->hs_hcon) | |
5887 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; | |
5888 | else | |
5889 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; | |
5890 | ||
5891 | err = l2cap_resegment(chan); | |
5892 | ||
5893 | if (!err) | |
5894 | err = l2cap_rx_state_recv(chan, control, skb, event); | |
5895 | ||
5896 | return err; | |
5897 | } | |
5898 | ||
d2a7ac5d MM |
5899 | static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq) |
5900 | { | |
5901 | /* Make sure reqseq is for a packet that has been sent but not acked */ | |
5902 | u16 unacked; | |
5903 | ||
5904 | unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq); | |
5905 | return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked; | |
5906 | } | |
5907 | ||
cec8ab6e MM |
5908 | static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
5909 | struct sk_buff *skb, u8 event) | |
218bb9df | 5910 | { |
d2a7ac5d MM |
5911 | int err = 0; |
5912 | ||
5913 | BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan, | |
5914 | control, skb, event, chan->rx_state); | |
5915 | ||
5916 | if (__valid_reqseq(chan, control->reqseq)) { | |
5917 | switch (chan->rx_state) { | |
5918 | case L2CAP_RX_STATE_RECV: | |
5919 | err = l2cap_rx_state_recv(chan, control, skb, event); | |
5920 | break; | |
5921 | case L2CAP_RX_STATE_SREJ_SENT: | |
5922 | err = l2cap_rx_state_srej_sent(chan, control, skb, | |
5923 | event); | |
5924 | break; | |
32b32735 MM |
5925 | case L2CAP_RX_STATE_WAIT_P: |
5926 | err = l2cap_rx_state_wait_p(chan, control, skb, event); | |
5927 | break; | |
5928 | case L2CAP_RX_STATE_WAIT_F: | |
5929 | err = l2cap_rx_state_wait_f(chan, control, skb, event); | |
5930 | break; | |
d2a7ac5d MM |
5931 | default: |
5932 | /* shut it down */ | |
5933 | break; | |
5934 | } | |
5935 | } else { | |
5936 | BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d", | |
5937 | control->reqseq, chan->next_tx_seq, | |
5938 | chan->expected_ack_seq); | |
5939 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); | |
5940 | } | |
5941 | ||
5942 | return err; | |
cec8ab6e MM |
5943 | } |
5944 | ||
5945 | static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, | |
5946 | struct sk_buff *skb) | |
5947 | { | |
4b51dae9 MM |
5948 | int err = 0; |
5949 | ||
5950 | BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb, | |
5951 | chan->rx_state); | |
5952 | ||
5953 | if (l2cap_classify_txseq(chan, control->txseq) == | |
5954 | L2CAP_TXSEQ_EXPECTED) { | |
5955 | l2cap_pass_to_tx(chan, control); | |
5956 | ||
5957 | BT_DBG("buffer_seq %d->%d", chan->buffer_seq, | |
5958 | __next_seq(chan, chan->buffer_seq)); | |
5959 | ||
5960 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); | |
5961 | ||
5962 | l2cap_reassemble_sdu(chan, skb, control); | |
5963 | } else { | |
5964 | if (chan->sdu) { | |
5965 | kfree_skb(chan->sdu); | |
5966 | chan->sdu = NULL; | |
5967 | } | |
5968 | chan->sdu_last_frag = NULL; | |
5969 | chan->sdu_len = 0; | |
5970 | ||
5971 | if (skb) { | |
5972 | BT_DBG("Freeing %p", skb); | |
5973 | kfree_skb(skb); | |
5974 | } | |
5975 | } | |
5976 | ||
5977 | chan->last_acked_seq = control->txseq; | |
5978 | chan->expected_tx_seq = __next_seq(chan, control->txseq); | |
5979 | ||
5980 | return err; | |
cec8ab6e MM |
5981 | } |
5982 | ||
5983 | static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) | |
5984 | { | |
5985 | struct l2cap_ctrl *control = &bt_cb(skb)->control; | |
5986 | u16 len; | |
5987 | u8 event; | |
218bb9df | 5988 | |
b76bbd66 MM |
5989 | __unpack_control(chan, skb); |
5990 | ||
218bb9df GP |
5991 | len = skb->len; |
5992 | ||
5993 | /* | |
5994 | * We can just drop the corrupted I-frame here. | |
5995 | * Receiver will miss it and start proper recovery | |
cec8ab6e | 5996 | * procedures and ask for retransmission. |
218bb9df | 5997 | */ |
47d1ec61 | 5998 | if (l2cap_check_fcs(chan, skb)) |
218bb9df GP |
5999 | goto drop; |
6000 | ||
cec8ab6e | 6001 | if (!control->sframe && control->sar == L2CAP_SAR_START) |
03a51213 | 6002 | len -= L2CAP_SDULEN_SIZE; |
218bb9df | 6003 | |
47d1ec61 | 6004 | if (chan->fcs == L2CAP_FCS_CRC16) |
03a51213 | 6005 | len -= L2CAP_FCS_SIZE; |
218bb9df | 6006 | |
47d1ec61 | 6007 | if (len > chan->mps) { |
8c1d787b | 6008 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
218bb9df GP |
6009 | goto drop; |
6010 | } | |
6011 | ||
cec8ab6e MM |
6012 | if (!control->sframe) { |
6013 | int err; | |
218bb9df | 6014 | |
cec8ab6e MM |
6015 | BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d", |
6016 | control->sar, control->reqseq, control->final, | |
6017 | control->txseq); | |
218bb9df | 6018 | |
cec8ab6e MM |
6019 | /* Validate F-bit - F=0 always valid, F=1 only |
6020 | * valid in TX WAIT_F | |
6021 | */ | |
6022 | if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F) | |
218bb9df | 6023 | goto drop; |
cec8ab6e MM |
6024 | |
6025 | if (chan->mode != L2CAP_MODE_STREAMING) { | |
6026 | event = L2CAP_EV_RECV_IFRAME; | |
6027 | err = l2cap_rx(chan, control, skb, event); | |
6028 | } else { | |
6029 | err = l2cap_stream_rx(chan, control, skb); | |
218bb9df GP |
6030 | } |
6031 | ||
cec8ab6e MM |
6032 | if (err) |
6033 | l2cap_send_disconn_req(chan->conn, chan, | |
6034 | ECONNRESET); | |
218bb9df | 6035 | } else { |
cec8ab6e MM |
6036 | const u8 rx_func_to_event[4] = { |
6037 | L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ, | |
6038 | L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ | |
6039 | }; | |
6040 | ||
6041 | /* Only I-frames are expected in streaming mode */ | |
6042 | if (chan->mode == L2CAP_MODE_STREAMING) | |
6043 | goto drop; | |
6044 | ||
6045 | BT_DBG("sframe reqseq %d, final %d, poll %d, super %d", | |
6046 | control->reqseq, control->final, control->poll, | |
6047 | control->super); | |
6048 | ||
218bb9df GP |
6049 | if (len != 0) { |
6050 | BT_ERR("%d", len); | |
8c1d787b | 6051 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
218bb9df GP |
6052 | goto drop; |
6053 | } | |
6054 | ||
cec8ab6e MM |
6055 | /* Validate F and P bits */ |
6056 | if (control->final && (control->poll || | |
6057 | chan->tx_state != L2CAP_TX_STATE_WAIT_F)) | |
6058 | goto drop; | |
6059 | ||
6060 | event = rx_func_to_event[control->super]; | |
6061 | if (l2cap_rx(chan, control, skb, event)) | |
6062 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); | |
218bb9df GP |
6063 | } |
6064 | ||
6065 | return 0; | |
6066 | ||
6067 | drop: | |
6068 | kfree_skb(skb); | |
6069 | return 0; | |
6070 | } | |
6071 | ||
13ca56e0 AE |
6072 | static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid, |
6073 | struct sk_buff *skb) | |
1da177e4 | 6074 | { |
48454079 | 6075 | struct l2cap_chan *chan; |
1da177e4 | 6076 | |
baa7e1fa | 6077 | chan = l2cap_get_chan_by_scid(conn, cid); |
48454079 | 6078 | if (!chan) { |
97e8e89d AE |
6079 | if (cid == L2CAP_CID_A2MP) { |
6080 | chan = a2mp_channel_create(conn, skb); | |
6081 | if (!chan) { | |
6082 | kfree_skb(skb); | |
13ca56e0 | 6083 | return; |
97e8e89d AE |
6084 | } |
6085 | ||
6086 | l2cap_chan_lock(chan); | |
6087 | } else { | |
6088 | BT_DBG("unknown cid 0x%4.4x", cid); | |
6089 | /* Drop packet and return */ | |
6090 | kfree_skb(skb); | |
13ca56e0 | 6091 | return; |
97e8e89d | 6092 | } |
1da177e4 LT |
6093 | } |
6094 | ||
49208c9c | 6095 | BT_DBG("chan %p, len %d", chan, skb->len); |
1da177e4 | 6096 | |
89bc500e | 6097 | if (chan->state != BT_CONNECTED) |
1da177e4 LT |
6098 | goto drop; |
6099 | ||
0c1bc5c6 | 6100 | switch (chan->mode) { |
1c2acffb GP |
6101 | case L2CAP_MODE_BASIC: |
6102 | /* If socket recv buffers overflows we drop data here | |
6103 | * which is *bad* because L2CAP has to be reliable. | |
6104 | * But we don't have any other choice. L2CAP doesn't | |
6105 | * provide flow control mechanism. */ | |
1da177e4 | 6106 | |
0c1bc5c6 | 6107 | if (chan->imtu < skb->len) |
1c2acffb | 6108 | goto drop; |
1da177e4 | 6109 | |
80b98027 | 6110 | if (!chan->ops->recv(chan, skb)) |
1c2acffb GP |
6111 | goto done; |
6112 | break; | |
6113 | ||
6114 | case L2CAP_MODE_ERTM: | |
6840ed07 | 6115 | case L2CAP_MODE_STREAMING: |
cec8ab6e | 6116 | l2cap_data_rcv(chan, skb); |
6840ed07 GP |
6117 | goto done; |
6118 | ||
1c2acffb | 6119 | default: |
0c1bc5c6 | 6120 | BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode); |
1c2acffb GP |
6121 | break; |
6122 | } | |
1da177e4 LT |
6123 | |
6124 | drop: | |
6125 | kfree_skb(skb); | |
6126 | ||
6127 | done: | |
6be36555 | 6128 | l2cap_chan_unlock(chan); |
1da177e4 LT |
6129 | } |
6130 | ||
84104b24 AE |
6131 | static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, |
6132 | struct sk_buff *skb) | |
1da177e4 | 6133 | { |
23691d75 | 6134 | struct l2cap_chan *chan; |
1da177e4 | 6135 | |
c2287681 | 6136 | chan = l2cap_global_chan_by_psm(0, psm, conn->src, conn->dst); |
23691d75 | 6137 | if (!chan) |
1da177e4 LT |
6138 | goto drop; |
6139 | ||
5b4cedaa | 6140 | BT_DBG("chan %p, len %d", chan, skb->len); |
1da177e4 | 6141 | |
89bc500e | 6142 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
1da177e4 LT |
6143 | goto drop; |
6144 | ||
e13e21dc | 6145 | if (chan->imtu < skb->len) |
1da177e4 LT |
6146 | goto drop; |
6147 | ||
80b98027 | 6148 | if (!chan->ops->recv(chan, skb)) |
84104b24 | 6149 | return; |
1da177e4 LT |
6150 | |
6151 | drop: | |
6152 | kfree_skb(skb); | |
1da177e4 LT |
6153 | } |
6154 | ||
6810fca7 AE |
6155 | static void l2cap_att_channel(struct l2cap_conn *conn, u16 cid, |
6156 | struct sk_buff *skb) | |
9f69bda6 | 6157 | { |
23691d75 | 6158 | struct l2cap_chan *chan; |
9f69bda6 | 6159 | |
c2287681 | 6160 | chan = l2cap_global_chan_by_scid(0, cid, conn->src, conn->dst); |
23691d75 | 6161 | if (!chan) |
9f69bda6 GP |
6162 | goto drop; |
6163 | ||
5b4cedaa | 6164 | BT_DBG("chan %p, len %d", chan, skb->len); |
9f69bda6 | 6165 | |
89bc500e | 6166 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
9f69bda6 GP |
6167 | goto drop; |
6168 | ||
e13e21dc | 6169 | if (chan->imtu < skb->len) |
9f69bda6 GP |
6170 | goto drop; |
6171 | ||
80b98027 | 6172 | if (!chan->ops->recv(chan, skb)) |
6810fca7 | 6173 | return; |
9f69bda6 GP |
6174 | |
6175 | drop: | |
6176 | kfree_skb(skb); | |
9f69bda6 GP |
6177 | } |
6178 | ||
1da177e4 LT |
6179 | static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) |
6180 | { | |
6181 | struct l2cap_hdr *lh = (void *) skb->data; | |
8e036fc3 AV |
6182 | u16 cid, len; |
6183 | __le16 psm; | |
1da177e4 LT |
6184 | |
6185 | skb_pull(skb, L2CAP_HDR_SIZE); | |
6186 | cid = __le16_to_cpu(lh->cid); | |
6187 | len = __le16_to_cpu(lh->len); | |
6188 | ||
1c2acffb GP |
6189 | if (len != skb->len) { |
6190 | kfree_skb(skb); | |
6191 | return; | |
6192 | } | |
6193 | ||
1da177e4 LT |
6194 | BT_DBG("len %d, cid 0x%4.4x", len, cid); |
6195 | ||
6196 | switch (cid) { | |
3300d9a9 | 6197 | case L2CAP_CID_LE_SIGNALING: |
8db4dc46 | 6198 | case L2CAP_CID_SIGNALING: |
1da177e4 LT |
6199 | l2cap_sig_channel(conn, skb); |
6200 | break; | |
6201 | ||
8db4dc46 | 6202 | case L2CAP_CID_CONN_LESS: |
097db76c | 6203 | psm = get_unaligned((__le16 *) skb->data); |
0181a70f | 6204 | skb_pull(skb, L2CAP_PSMLEN_SIZE); |
1da177e4 LT |
6205 | l2cap_conless_channel(conn, psm, skb); |
6206 | break; | |
6207 | ||
9f69bda6 GP |
6208 | case L2CAP_CID_LE_DATA: |
6209 | l2cap_att_channel(conn, cid, skb); | |
6210 | break; | |
6211 | ||
b501d6a1 AB |
6212 | case L2CAP_CID_SMP: |
6213 | if (smp_sig_channel(conn, skb)) | |
6214 | l2cap_conn_del(conn->hcon, EACCES); | |
6215 | break; | |
6216 | ||
1da177e4 LT |
6217 | default: |
6218 | l2cap_data_channel(conn, cid, skb); | |
6219 | break; | |
6220 | } | |
6221 | } | |
6222 | ||
6223 | /* ---- L2CAP interface with lower layer (HCI) ---- */ | |
6224 | ||
686ebf28 | 6225 | int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) |
1da177e4 LT |
6226 | { |
6227 | int exact = 0, lm1 = 0, lm2 = 0; | |
23691d75 | 6228 | struct l2cap_chan *c; |
1da177e4 | 6229 | |
6ed93dc6 | 6230 | BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr); |
1da177e4 LT |
6231 | |
6232 | /* Find listening sockets and check their link_mode */ | |
23691d75 GP |
6233 | read_lock(&chan_list_lock); |
6234 | list_for_each_entry(c, &chan_list, global_l) { | |
6235 | struct sock *sk = c->sk; | |
4343478f | 6236 | |
89bc500e | 6237 | if (c->state != BT_LISTEN) |
1da177e4 LT |
6238 | continue; |
6239 | ||
6240 | if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { | |
2af6b9d5 | 6241 | lm1 |= HCI_LM_ACCEPT; |
43bd0f32 | 6242 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
2af6b9d5 | 6243 | lm1 |= HCI_LM_MASTER; |
1da177e4 | 6244 | exact++; |
2af6b9d5 MH |
6245 | } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { |
6246 | lm2 |= HCI_LM_ACCEPT; | |
43bd0f32 | 6247 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
2af6b9d5 MH |
6248 | lm2 |= HCI_LM_MASTER; |
6249 | } | |
1da177e4 | 6250 | } |
23691d75 | 6251 | read_unlock(&chan_list_lock); |
1da177e4 LT |
6252 | |
6253 | return exact ? lm1 : lm2; | |
6254 | } | |
6255 | ||
9e664631 | 6256 | void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) |
1da177e4 | 6257 | { |
0139418c MH |
6258 | struct l2cap_conn *conn; |
6259 | ||
6ed93dc6 | 6260 | BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); |
1da177e4 | 6261 | |
1da177e4 | 6262 | if (!status) { |
1da177e4 LT |
6263 | conn = l2cap_conn_add(hcon, status); |
6264 | if (conn) | |
6265 | l2cap_conn_ready(conn); | |
ba6fc317 | 6266 | } else { |
e175072f | 6267 | l2cap_conn_del(hcon, bt_to_errno(status)); |
ba6fc317 | 6268 | } |
1da177e4 LT |
6269 | } |
6270 | ||
686ebf28 | 6271 | int l2cap_disconn_ind(struct hci_conn *hcon) |
2950f21a MH |
6272 | { |
6273 | struct l2cap_conn *conn = hcon->l2cap_data; | |
6274 | ||
6275 | BT_DBG("hcon %p", hcon); | |
6276 | ||
686ebf28 | 6277 | if (!conn) |
9f5a0d7b | 6278 | return HCI_ERROR_REMOTE_USER_TERM; |
2950f21a MH |
6279 | return conn->disc_reason; |
6280 | } | |
6281 | ||
9e664631 | 6282 | void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
1da177e4 LT |
6283 | { |
6284 | BT_DBG("hcon %p reason %d", hcon, reason); | |
6285 | ||
e175072f | 6286 | l2cap_conn_del(hcon, bt_to_errno(reason)); |
1da177e4 LT |
6287 | } |
6288 | ||
4343478f | 6289 | static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) |
f62e4323 | 6290 | { |
715ec005 | 6291 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
255c7601 MH |
6292 | return; |
6293 | ||
f62e4323 | 6294 | if (encrypt == 0x00) { |
4343478f | 6295 | if (chan->sec_level == BT_SECURITY_MEDIUM) { |
ba13ccd9 | 6296 | __set_chan_timer(chan, L2CAP_ENC_TIMEOUT); |
4343478f | 6297 | } else if (chan->sec_level == BT_SECURITY_HIGH) |
0f852724 | 6298 | l2cap_chan_close(chan, ECONNREFUSED); |
f62e4323 | 6299 | } else { |
4343478f | 6300 | if (chan->sec_level == BT_SECURITY_MEDIUM) |
c9b66675 | 6301 | __clear_chan_timer(chan); |
f62e4323 MH |
6302 | } |
6303 | } | |
6304 | ||
686ebf28 | 6305 | int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) |
1da177e4 | 6306 | { |
0139418c | 6307 | struct l2cap_conn *conn = hcon->l2cap_data; |
48454079 | 6308 | struct l2cap_chan *chan; |
1da177e4 | 6309 | |
0139418c | 6310 | if (!conn) |
1da177e4 | 6311 | return 0; |
0139418c | 6312 | |
89d8b407 | 6313 | BT_DBG("conn %p status 0x%2.2x encrypt %u", conn, status, encrypt); |
1da177e4 | 6314 | |
160dc6ac | 6315 | if (hcon->type == LE_LINK) { |
35d4adcc HG |
6316 | if (!status && encrypt) |
6317 | smp_distribute_keys(conn, 0); | |
17cd3f37 | 6318 | cancel_delayed_work(&conn->security_timer); |
160dc6ac VCG |
6319 | } |
6320 | ||
3df91ea2 | 6321 | mutex_lock(&conn->chan_lock); |
1da177e4 | 6322 | |
3df91ea2 | 6323 | list_for_each_entry(chan, &conn->chan_l, list) { |
6be36555 | 6324 | l2cap_chan_lock(chan); |
1da177e4 | 6325 | |
89d8b407 AE |
6326 | BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, |
6327 | state_to_string(chan->state)); | |
f1cb9af5 | 6328 | |
78eb2f98 AE |
6329 | if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) { |
6330 | l2cap_chan_unlock(chan); | |
6331 | continue; | |
6332 | } | |
6333 | ||
f1cb9af5 VCG |
6334 | if (chan->scid == L2CAP_CID_LE_DATA) { |
6335 | if (!status && encrypt) { | |
6336 | chan->sec_level = hcon->sec_level; | |
cf4cd009 | 6337 | l2cap_chan_ready(chan); |
f1cb9af5 VCG |
6338 | } |
6339 | ||
6be36555 | 6340 | l2cap_chan_unlock(chan); |
f1cb9af5 VCG |
6341 | continue; |
6342 | } | |
6343 | ||
c1360a1c | 6344 | if (test_bit(CONF_CONNECT_PEND, &chan->conf_state)) { |
6be36555 | 6345 | l2cap_chan_unlock(chan); |
6a8d3010 MH |
6346 | continue; |
6347 | } | |
6348 | ||
89bc500e | 6349 | if (!status && (chan->state == BT_CONNECTED || |
2d792818 | 6350 | chan->state == BT_CONFIG)) { |
a7d7723a GP |
6351 | struct sock *sk = chan->sk; |
6352 | ||
c5daa683 | 6353 | clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); |
a7d7723a GP |
6354 | sk->sk_state_change(sk); |
6355 | ||
4343478f | 6356 | l2cap_check_encryption(chan, encrypt); |
6be36555 | 6357 | l2cap_chan_unlock(chan); |
9719f8af MH |
6358 | continue; |
6359 | } | |
6360 | ||
89bc500e | 6361 | if (chan->state == BT_CONNECT) { |
b1235d79 | 6362 | if (!status) { |
93c3e8f5 | 6363 | l2cap_start_connection(chan); |
b1235d79 | 6364 | } else { |
ba13ccd9 | 6365 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
b1235d79 | 6366 | } |
89bc500e | 6367 | } else if (chan->state == BT_CONNECT2) { |
6be36555 | 6368 | struct sock *sk = chan->sk; |
b1235d79 | 6369 | struct l2cap_conn_rsp rsp; |
df3c3931 | 6370 | __u16 res, stat; |
1da177e4 | 6371 | |
6be36555 AE |
6372 | lock_sock(sk); |
6373 | ||
b1235d79 | 6374 | if (!status) { |
c5daa683 GP |
6375 | if (test_bit(BT_SK_DEFER_SETUP, |
6376 | &bt_sk(sk)->flags)) { | |
df3c3931 JH |
6377 | res = L2CAP_CR_PEND; |
6378 | stat = L2CAP_CS_AUTHOR_PEND; | |
2dc4e510 | 6379 | chan->ops->defer(chan); |
df3c3931 | 6380 | } else { |
0e587be7 | 6381 | __l2cap_state_change(chan, BT_CONFIG); |
df3c3931 JH |
6382 | res = L2CAP_CR_SUCCESS; |
6383 | stat = L2CAP_CS_NO_INFO; | |
6384 | } | |
b1235d79 | 6385 | } else { |
0e587be7 | 6386 | __l2cap_state_change(chan, BT_DISCONN); |
ba13ccd9 | 6387 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
df3c3931 JH |
6388 | res = L2CAP_CR_SEC_BLOCK; |
6389 | stat = L2CAP_CS_NO_INFO; | |
b1235d79 MH |
6390 | } |
6391 | ||
6be36555 AE |
6392 | release_sock(sk); |
6393 | ||
fe4128e0 GP |
6394 | rsp.scid = cpu_to_le16(chan->dcid); |
6395 | rsp.dcid = cpu_to_le16(chan->scid); | |
df3c3931 JH |
6396 | rsp.result = cpu_to_le16(res); |
6397 | rsp.status = cpu_to_le16(stat); | |
fc7f8a7e | 6398 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
2d792818 | 6399 | sizeof(rsp), &rsp); |
2d369359 MM |
6400 | |
6401 | if (!test_bit(CONF_REQ_SENT, &chan->conf_state) && | |
6402 | res == L2CAP_CR_SUCCESS) { | |
6403 | char buf[128]; | |
6404 | set_bit(CONF_REQ_SENT, &chan->conf_state); | |
6405 | l2cap_send_cmd(conn, l2cap_get_ident(conn), | |
6406 | L2CAP_CONF_REQ, | |
6407 | l2cap_build_conf_req(chan, buf), | |
6408 | buf); | |
6409 | chan->num_conf_req++; | |
6410 | } | |
b1235d79 | 6411 | } |
1da177e4 | 6412 | |
6be36555 | 6413 | l2cap_chan_unlock(chan); |
1da177e4 LT |
6414 | } |
6415 | ||
3df91ea2 | 6416 | mutex_unlock(&conn->chan_lock); |
b1235d79 | 6417 | |
1da177e4 LT |
6418 | return 0; |
6419 | } | |
6420 | ||
686ebf28 | 6421 | int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) |
1da177e4 LT |
6422 | { |
6423 | struct l2cap_conn *conn = hcon->l2cap_data; | |
d73a0988 AE |
6424 | struct l2cap_hdr *hdr; |
6425 | int len; | |
1da177e4 | 6426 | |
1d13a254 AE |
6427 | /* For AMP controller do not create l2cap conn */ |
6428 | if (!conn && hcon->hdev->dev_type != HCI_BREDR) | |
6429 | goto drop; | |
1da177e4 | 6430 | |
5a08ecce AE |
6431 | if (!conn) |
6432 | conn = l2cap_conn_add(hcon, 0); | |
6433 | ||
6434 | if (!conn) | |
1da177e4 LT |
6435 | goto drop; |
6436 | ||
6437 | BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); | |
6438 | ||
d73a0988 AE |
6439 | switch (flags) { |
6440 | case ACL_START: | |
6441 | case ACL_START_NO_FLUSH: | |
6442 | case ACL_COMPLETE: | |
1da177e4 LT |
6443 | if (conn->rx_len) { |
6444 | BT_ERR("Unexpected start frame (len %d)", skb->len); | |
6445 | kfree_skb(conn->rx_skb); | |
6446 | conn->rx_skb = NULL; | |
6447 | conn->rx_len = 0; | |
6448 | l2cap_conn_unreliable(conn, ECOMM); | |
6449 | } | |
6450 | ||
aae7fe22 AE |
6451 | /* Start fragment always begin with Basic L2CAP header */ |
6452 | if (skb->len < L2CAP_HDR_SIZE) { | |
1da177e4 LT |
6453 | BT_ERR("Frame is too short (len %d)", skb->len); |
6454 | l2cap_conn_unreliable(conn, ECOMM); | |
6455 | goto drop; | |
6456 | } | |
6457 | ||
6458 | hdr = (struct l2cap_hdr *) skb->data; | |
6459 | len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; | |
6460 | ||
6461 | if (len == skb->len) { | |
6462 | /* Complete frame received */ | |
6463 | l2cap_recv_frame(conn, skb); | |
6464 | return 0; | |
6465 | } | |
6466 | ||
6467 | BT_DBG("Start: total len %d, frag len %d", len, skb->len); | |
6468 | ||
6469 | if (skb->len > len) { | |
6470 | BT_ERR("Frame is too long (len %d, expected len %d)", | |
2d792818 | 6471 | skb->len, len); |
1da177e4 LT |
6472 | l2cap_conn_unreliable(conn, ECOMM); |
6473 | goto drop; | |
6474 | } | |
6475 | ||
6476 | /* Allocate skb for the complete frame (with header) */ | |
8bcde1f2 | 6477 | conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL); |
af05b30b | 6478 | if (!conn->rx_skb) |
1da177e4 LT |
6479 | goto drop; |
6480 | ||
d626f62b | 6481 | skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), |
2d792818 | 6482 | skb->len); |
1da177e4 | 6483 | conn->rx_len = len - skb->len; |
d73a0988 AE |
6484 | break; |
6485 | ||
6486 | case ACL_CONT: | |
1da177e4 LT |
6487 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
6488 | ||
6489 | if (!conn->rx_len) { | |
6490 | BT_ERR("Unexpected continuation frame (len %d)", skb->len); | |
6491 | l2cap_conn_unreliable(conn, ECOMM); | |
6492 | goto drop; | |
6493 | } | |
6494 | ||
6495 | if (skb->len > conn->rx_len) { | |
6496 | BT_ERR("Fragment is too long (len %d, expected %d)", | |
2d792818 | 6497 | skb->len, conn->rx_len); |
1da177e4 LT |
6498 | kfree_skb(conn->rx_skb); |
6499 | conn->rx_skb = NULL; | |
6500 | conn->rx_len = 0; | |
6501 | l2cap_conn_unreliable(conn, ECOMM); | |
6502 | goto drop; | |
6503 | } | |
6504 | ||
d626f62b | 6505 | skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), |
2d792818 | 6506 | skb->len); |
1da177e4 LT |
6507 | conn->rx_len -= skb->len; |
6508 | ||
6509 | if (!conn->rx_len) { | |
6510 | /* Complete frame received */ | |
6511 | l2cap_recv_frame(conn, conn->rx_skb); | |
6512 | conn->rx_skb = NULL; | |
6513 | } | |
d73a0988 | 6514 | break; |
1da177e4 LT |
6515 | } |
6516 | ||
6517 | drop: | |
6518 | kfree_skb(skb); | |
6519 | return 0; | |
6520 | } | |
6521 | ||
aef7d97c | 6522 | static int l2cap_debugfs_show(struct seq_file *f, void *p) |
1da177e4 | 6523 | { |
23691d75 | 6524 | struct l2cap_chan *c; |
1da177e4 | 6525 | |
333055f2 | 6526 | read_lock(&chan_list_lock); |
1da177e4 | 6527 | |
23691d75 GP |
6528 | list_for_each_entry(c, &chan_list, global_l) { |
6529 | struct sock *sk = c->sk; | |
101545f6 | 6530 | |
fcb73338 AE |
6531 | seq_printf(f, "%pMR %pMR %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", |
6532 | &bt_sk(sk)->src, &bt_sk(sk)->dst, | |
6533 | c->state, __le16_to_cpu(c->psm), | |
6534 | c->scid, c->dcid, c->imtu, c->omtu, | |
6535 | c->sec_level, c->mode); | |
61e1b4b7 | 6536 | } |
1da177e4 | 6537 | |
333055f2 | 6538 | read_unlock(&chan_list_lock); |
1da177e4 | 6539 | |
aef7d97c | 6540 | return 0; |
1da177e4 LT |
6541 | } |
6542 | ||
aef7d97c MH |
6543 | static int l2cap_debugfs_open(struct inode *inode, struct file *file) |
6544 | { | |
6545 | return single_open(file, l2cap_debugfs_show, inode->i_private); | |
6546 | } | |
6547 | ||
6548 | static const struct file_operations l2cap_debugfs_fops = { | |
6549 | .open = l2cap_debugfs_open, | |
6550 | .read = seq_read, | |
6551 | .llseek = seq_lseek, | |
6552 | .release = single_release, | |
6553 | }; | |
6554 | ||
6555 | static struct dentry *l2cap_debugfs; | |
1da177e4 | 6556 | |
64274518 | 6557 | int __init l2cap_init(void) |
1da177e4 LT |
6558 | { |
6559 | int err; | |
be9d1227 | 6560 | |
bb58f747 | 6561 | err = l2cap_init_sockets(); |
1da177e4 LT |
6562 | if (err < 0) |
6563 | return err; | |
6564 | ||
aef7d97c | 6565 | if (bt_debugfs) { |
2d792818 GP |
6566 | l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs, |
6567 | NULL, &l2cap_debugfs_fops); | |
aef7d97c MH |
6568 | if (!l2cap_debugfs) |
6569 | BT_ERR("Failed to create L2CAP debug file"); | |
6570 | } | |
1da177e4 | 6571 | |
1da177e4 | 6572 | return 0; |
1da177e4 LT |
6573 | } |
6574 | ||
64274518 | 6575 | void l2cap_exit(void) |
1da177e4 | 6576 | { |
aef7d97c | 6577 | debugfs_remove(l2cap_debugfs); |
bb58f747 | 6578 | l2cap_cleanup_sockets(); |
1da177e4 LT |
6579 | } |
6580 | ||
d1c4a17d GP |
6581 | module_param(disable_ertm, bool, 0644); |
6582 | MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); |