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