10a708f8fSGustavo F. Padovan /* 20a708f8fSGustavo F. Padovan BlueZ - Bluetooth protocol stack for Linux 30a708f8fSGustavo F. Padovan Copyright (C) 2000-2001 Qualcomm Incorporated 40a708f8fSGustavo F. Padovan Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org> 50a708f8fSGustavo F. Padovan Copyright (C) 2010 Google Inc. 6590051deSGustavo F. Padovan Copyright (C) 2011 ProFUSION Embedded Systems 70a708f8fSGustavo F. Padovan 80a708f8fSGustavo F. Padovan Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 90a708f8fSGustavo F. Padovan 100a708f8fSGustavo F. Padovan This program is free software; you can redistribute it and/or modify 110a708f8fSGustavo F. Padovan it under the terms of the GNU General Public License version 2 as 120a708f8fSGustavo F. Padovan published by the Free Software Foundation; 130a708f8fSGustavo F. Padovan 140a708f8fSGustavo F. Padovan THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 150a708f8fSGustavo F. Padovan OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 160a708f8fSGustavo F. Padovan FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 170a708f8fSGustavo F. Padovan IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 180a708f8fSGustavo F. Padovan CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 190a708f8fSGustavo F. Padovan WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 200a708f8fSGustavo F. Padovan ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 210a708f8fSGustavo F. Padovan OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 220a708f8fSGustavo F. Padovan 230a708f8fSGustavo F. Padovan ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 240a708f8fSGustavo F. Padovan COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 250a708f8fSGustavo F. Padovan SOFTWARE IS DISCLAIMED. 260a708f8fSGustavo F. Padovan */ 270a708f8fSGustavo F. Padovan 28bb58f747SGustavo F. Padovan /* Bluetooth L2CAP core. */ 290a708f8fSGustavo F. Padovan 300a708f8fSGustavo F. Padovan #include <linux/module.h> 310a708f8fSGustavo F. Padovan 320a708f8fSGustavo F. Padovan #include <linux/types.h> 330a708f8fSGustavo F. Padovan #include <linux/capability.h> 340a708f8fSGustavo F. Padovan #include <linux/errno.h> 350a708f8fSGustavo F. Padovan #include <linux/kernel.h> 360a708f8fSGustavo F. Padovan #include <linux/sched.h> 370a708f8fSGustavo F. Padovan #include <linux/slab.h> 380a708f8fSGustavo F. Padovan #include <linux/poll.h> 390a708f8fSGustavo F. Padovan #include <linux/fcntl.h> 400a708f8fSGustavo F. Padovan #include <linux/init.h> 410a708f8fSGustavo F. Padovan #include <linux/interrupt.h> 420a708f8fSGustavo F. Padovan #include <linux/socket.h> 430a708f8fSGustavo F. Padovan #include <linux/skbuff.h> 440a708f8fSGustavo F. Padovan #include <linux/list.h> 450a708f8fSGustavo F. Padovan #include <linux/device.h> 460a708f8fSGustavo F. Padovan #include <linux/debugfs.h> 470a708f8fSGustavo F. Padovan #include <linux/seq_file.h> 480a708f8fSGustavo F. Padovan #include <linux/uaccess.h> 490a708f8fSGustavo F. Padovan #include <linux/crc16.h> 500a708f8fSGustavo F. Padovan #include <net/sock.h> 510a708f8fSGustavo F. Padovan 520a708f8fSGustavo F. Padovan #include <asm/system.h> 530a708f8fSGustavo F. Padovan #include <asm/unaligned.h> 540a708f8fSGustavo F. Padovan 550a708f8fSGustavo F. Padovan #include <net/bluetooth/bluetooth.h> 560a708f8fSGustavo F. Padovan #include <net/bluetooth/hci_core.h> 570a708f8fSGustavo F. Padovan #include <net/bluetooth/l2cap.h> 58b501d6a1SAnderson Briglia #include <net/bluetooth/smp.h> 590a708f8fSGustavo F. Padovan 60eb939922SRusty Russell bool disable_ertm; 610a708f8fSGustavo F. Padovan 620a708f8fSGustavo F. Padovan static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN; 6350a147cdSMat Martineau static u8 l2cap_fixed_chan[8] = { L2CAP_FC_L2CAP, }; 640a708f8fSGustavo F. Padovan 65b5ad8b7fSJohannes Berg static LIST_HEAD(chan_list); 66b5ad8b7fSJohannes Berg static DEFINE_RWLOCK(chan_list_lock); 670a708f8fSGustavo F. Padovan 680a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 690a708f8fSGustavo F. Padovan u8 code, u8 ident, u16 dlen, void *data); 704519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, 714519de9aSGustavo F. Padovan void *data); 72710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data); 734519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, 744519de9aSGustavo F. Padovan struct l2cap_chan *chan, int err); 750a708f8fSGustavo F. Padovan 760a708f8fSGustavo F. Padovan /* ---- L2CAP channels ---- */ 7771ba0e56SGustavo F. Padovan 78baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid) 790a708f8fSGustavo F. Padovan { 803df91ea2SAndrei Emeltchenko struct l2cap_chan *c; 81baa7e1faSGustavo F. Padovan 823df91ea2SAndrei Emeltchenko list_for_each_entry(c, &conn->chan_l, list) { 833df91ea2SAndrei Emeltchenko if (c->dcid == cid) 843df91ea2SAndrei Emeltchenko return c; 850a708f8fSGustavo F. Padovan } 863df91ea2SAndrei Emeltchenko return NULL; 87baa7e1faSGustavo F. Padovan } 880a708f8fSGustavo F. Padovan 89baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) 900a708f8fSGustavo F. Padovan { 913df91ea2SAndrei Emeltchenko struct l2cap_chan *c; 92baa7e1faSGustavo F. Padovan 933df91ea2SAndrei Emeltchenko list_for_each_entry(c, &conn->chan_l, list) { 943df91ea2SAndrei Emeltchenko if (c->scid == cid) 953df91ea2SAndrei Emeltchenko return c; 960a708f8fSGustavo F. Padovan } 973df91ea2SAndrei Emeltchenko return NULL; 98baa7e1faSGustavo F. Padovan } 990a708f8fSGustavo F. Padovan 1000a708f8fSGustavo F. Padovan /* Find channel with given SCID. 1010a708f8fSGustavo F. Padovan * Returns locked socket */ 102baa7e1faSGustavo F. Padovan static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) 1030a708f8fSGustavo F. Padovan { 10448454079SGustavo F. Padovan struct l2cap_chan *c; 105baa7e1faSGustavo F. Padovan 1063df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 107baa7e1faSGustavo F. Padovan c = __l2cap_get_chan_by_scid(conn, cid); 1083df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 1093df91ea2SAndrei Emeltchenko 11048454079SGustavo F. Padovan return c; 1110a708f8fSGustavo F. Padovan } 1120a708f8fSGustavo F. Padovan 113baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) 1140a708f8fSGustavo F. Padovan { 1153df91ea2SAndrei Emeltchenko struct l2cap_chan *c; 116baa7e1faSGustavo F. Padovan 1173df91ea2SAndrei Emeltchenko list_for_each_entry(c, &conn->chan_l, list) { 1183df91ea2SAndrei Emeltchenko if (c->ident == ident) 1193df91ea2SAndrei Emeltchenko return c; 1200a708f8fSGustavo F. Padovan } 1213df91ea2SAndrei Emeltchenko return NULL; 122baa7e1faSGustavo F. Padovan } 1230a708f8fSGustavo F. Padovan 124baa7e1faSGustavo F. Padovan static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) 1250a708f8fSGustavo F. Padovan { 12648454079SGustavo F. Padovan struct l2cap_chan *c; 127baa7e1faSGustavo F. Padovan 1283df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 129baa7e1faSGustavo F. Padovan c = __l2cap_get_chan_by_ident(conn, ident); 1303df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 1313df91ea2SAndrei Emeltchenko 13248454079SGustavo F. Padovan return c; 1330a708f8fSGustavo F. Padovan } 1340a708f8fSGustavo F. Padovan 13523691d75SGustavo F. Padovan static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src) 1369e4425ffSGustavo F. Padovan { 13723691d75SGustavo F. Padovan struct l2cap_chan *c; 1389e4425ffSGustavo F. Padovan 13923691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 14023691d75SGustavo F. Padovan if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src)) 14123691d75SGustavo F. Padovan return c; 1429e4425ffSGustavo F. Padovan } 143250938cbSSzymon Janc return NULL; 144250938cbSSzymon Janc } 1459e4425ffSGustavo F. Padovan 1469e4425ffSGustavo F. Padovan int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) 1479e4425ffSGustavo F. Padovan { 14873b2ec18SGustavo F. Padovan int err; 14973b2ec18SGustavo F. Padovan 150333055f2SGustavo F. Padovan write_lock(&chan_list_lock); 1519e4425ffSGustavo F. Padovan 15223691d75SGustavo F. Padovan if (psm && __l2cap_global_chan_by_addr(psm, src)) { 15373b2ec18SGustavo F. Padovan err = -EADDRINUSE; 15473b2ec18SGustavo F. Padovan goto done; 1559e4425ffSGustavo F. Padovan } 1569e4425ffSGustavo F. Padovan 15773b2ec18SGustavo F. Padovan if (psm) { 1589e4425ffSGustavo F. Padovan chan->psm = psm; 1599e4425ffSGustavo F. Padovan chan->sport = psm; 16073b2ec18SGustavo F. Padovan err = 0; 16173b2ec18SGustavo F. Padovan } else { 16273b2ec18SGustavo F. Padovan u16 p; 1639e4425ffSGustavo F. Padovan 16473b2ec18SGustavo F. Padovan err = -EINVAL; 16573b2ec18SGustavo F. Padovan for (p = 0x1001; p < 0x1100; p += 2) 16623691d75SGustavo F. Padovan if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { 16773b2ec18SGustavo F. Padovan chan->psm = cpu_to_le16(p); 16873b2ec18SGustavo F. Padovan chan->sport = cpu_to_le16(p); 16973b2ec18SGustavo F. Padovan err = 0; 17073b2ec18SGustavo F. Padovan break; 17173b2ec18SGustavo F. Padovan } 17273b2ec18SGustavo F. Padovan } 17373b2ec18SGustavo F. Padovan 17473b2ec18SGustavo F. Padovan done: 175333055f2SGustavo F. Padovan write_unlock(&chan_list_lock); 17673b2ec18SGustavo F. Padovan return err; 1779e4425ffSGustavo F. Padovan } 1789e4425ffSGustavo F. Padovan 1799e4425ffSGustavo F. Padovan int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) 1809e4425ffSGustavo F. Padovan { 181333055f2SGustavo F. Padovan write_lock(&chan_list_lock); 1829e4425ffSGustavo F. Padovan 1839e4425ffSGustavo F. Padovan chan->scid = scid; 1849e4425ffSGustavo F. Padovan 185333055f2SGustavo F. Padovan write_unlock(&chan_list_lock); 1869e4425ffSGustavo F. Padovan 1879e4425ffSGustavo F. Padovan return 0; 1889e4425ffSGustavo F. Padovan } 1899e4425ffSGustavo F. Padovan 190baa7e1faSGustavo F. Padovan static u16 l2cap_alloc_cid(struct l2cap_conn *conn) 1910a708f8fSGustavo F. Padovan { 1920a708f8fSGustavo F. Padovan u16 cid = L2CAP_CID_DYN_START; 1930a708f8fSGustavo F. Padovan 1940a708f8fSGustavo F. Padovan for (; cid < L2CAP_CID_DYN_END; cid++) { 195baa7e1faSGustavo F. Padovan if (!__l2cap_get_chan_by_scid(conn, cid)) 1960a708f8fSGustavo F. Padovan return cid; 1970a708f8fSGustavo F. Padovan } 1980a708f8fSGustavo F. Padovan 1990a708f8fSGustavo F. Padovan return 0; 2000a708f8fSGustavo F. Padovan } 2010a708f8fSGustavo F. Padovan 2020e587be7SAndrei Emeltchenko static void __l2cap_state_change(struct l2cap_chan *chan, int state) 20389bc500eSGustavo F. Padovan { 20442d2d87cSAndrei Emeltchenko BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state), 205badaaa00SGustavo F. Padovan state_to_string(state)); 206badaaa00SGustavo F. Padovan 20789bc500eSGustavo F. Padovan chan->state = state; 20889bc500eSGustavo F. Padovan chan->ops->state_change(chan->data, state); 20989bc500eSGustavo F. Padovan } 21089bc500eSGustavo F. Padovan 2110e587be7SAndrei Emeltchenko static void l2cap_state_change(struct l2cap_chan *chan, int state) 2120e587be7SAndrei Emeltchenko { 2130e587be7SAndrei Emeltchenko struct sock *sk = chan->sk; 2140e587be7SAndrei Emeltchenko 2150e587be7SAndrei Emeltchenko lock_sock(sk); 2160e587be7SAndrei Emeltchenko __l2cap_state_change(chan, state); 2170e587be7SAndrei Emeltchenko release_sock(sk); 2180e587be7SAndrei Emeltchenko } 2190e587be7SAndrei Emeltchenko 2202e0052e4SAndrei Emeltchenko static inline void __l2cap_chan_set_err(struct l2cap_chan *chan, int err) 2212e0052e4SAndrei Emeltchenko { 2222e0052e4SAndrei Emeltchenko struct sock *sk = chan->sk; 2232e0052e4SAndrei Emeltchenko 2242e0052e4SAndrei Emeltchenko sk->sk_err = err; 2252e0052e4SAndrei Emeltchenko } 2262e0052e4SAndrei Emeltchenko 2272e0052e4SAndrei Emeltchenko static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err) 2282e0052e4SAndrei Emeltchenko { 2292e0052e4SAndrei Emeltchenko struct sock *sk = chan->sk; 2302e0052e4SAndrei Emeltchenko 2312e0052e4SAndrei Emeltchenko lock_sock(sk); 2322e0052e4SAndrei Emeltchenko __l2cap_chan_set_err(chan, err); 2332e0052e4SAndrei Emeltchenko release_sock(sk); 2342e0052e4SAndrei Emeltchenko } 2352e0052e4SAndrei Emeltchenko 236721c4181SGustavo F. Padovan static void l2cap_chan_timeout(struct work_struct *work) 237ab07801dSGustavo F. Padovan { 238721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 239721c4181SGustavo F. Padovan chan_timer.work); 2403df91ea2SAndrei Emeltchenko struct l2cap_conn *conn = chan->conn; 241ab07801dSGustavo F. Padovan struct sock *sk = chan->sk; 242ab07801dSGustavo F. Padovan int reason; 243ab07801dSGustavo F. Padovan 244e05dcc32SAndrei Emeltchenko BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); 245ab07801dSGustavo F. Padovan 2463df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 247721c4181SGustavo F. Padovan lock_sock(sk); 248ab07801dSGustavo F. Padovan 24989bc500eSGustavo F. Padovan if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) 250ab07801dSGustavo F. Padovan reason = ECONNREFUSED; 25189bc500eSGustavo F. Padovan else if (chan->state == BT_CONNECT && 252ab07801dSGustavo F. Padovan chan->sec_level != BT_SECURITY_SDP) 253ab07801dSGustavo F. Padovan reason = ECONNREFUSED; 254ab07801dSGustavo F. Padovan else 255ab07801dSGustavo F. Padovan reason = ETIMEDOUT; 256ab07801dSGustavo F. Padovan 2570f852724SGustavo F. Padovan l2cap_chan_close(chan, reason); 258ab07801dSGustavo F. Padovan 259721c4181SGustavo F. Padovan release_sock(sk); 260ab07801dSGustavo F. Padovan 261ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 2623df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 2633df91ea2SAndrei Emeltchenko 264371fd835SUlisses Furquim l2cap_chan_put(chan); 265ab07801dSGustavo F. Padovan } 266ab07801dSGustavo F. Padovan 26723691d75SGustavo F. Padovan struct l2cap_chan *l2cap_chan_create(struct sock *sk) 2680a708f8fSGustavo F. Padovan { 26948454079SGustavo F. Padovan struct l2cap_chan *chan; 2700a708f8fSGustavo F. Padovan 27148454079SGustavo F. Padovan chan = kzalloc(sizeof(*chan), GFP_ATOMIC); 27248454079SGustavo F. Padovan if (!chan) 27348454079SGustavo F. Padovan return NULL; 2740a708f8fSGustavo F. Padovan 275c03b355eSAndrei Emeltchenko mutex_init(&chan->lock); 276c03b355eSAndrei Emeltchenko 27748454079SGustavo F. Padovan chan->sk = sk; 27848454079SGustavo F. Padovan 279333055f2SGustavo F. Padovan write_lock(&chan_list_lock); 28023691d75SGustavo F. Padovan list_add(&chan->global_l, &chan_list); 281333055f2SGustavo F. Padovan write_unlock(&chan_list_lock); 28223691d75SGustavo F. Padovan 283721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); 284ab07801dSGustavo F. Padovan 28589bc500eSGustavo F. Padovan chan->state = BT_OPEN; 28689bc500eSGustavo F. Padovan 28771ba0e56SGustavo F. Padovan atomic_set(&chan->refcnt, 1); 28871ba0e56SGustavo F. Padovan 289abc545b8SSzymon Janc BT_DBG("sk %p chan %p", sk, chan); 290abc545b8SSzymon Janc 29148454079SGustavo F. Padovan return chan; 2920a708f8fSGustavo F. Padovan } 2930a708f8fSGustavo F. Padovan 29423691d75SGustavo F. Padovan void l2cap_chan_destroy(struct l2cap_chan *chan) 2956ff5abbfSGustavo F. Padovan { 296333055f2SGustavo F. Padovan write_lock(&chan_list_lock); 29723691d75SGustavo F. Padovan list_del(&chan->global_l); 298333055f2SGustavo F. Padovan write_unlock(&chan_list_lock); 29923691d75SGustavo F. Padovan 300371fd835SUlisses Furquim l2cap_chan_put(chan); 3016ff5abbfSGustavo F. Padovan } 3026ff5abbfSGustavo F. Padovan 303643162a8SAndrei Emeltchenko void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 3040a708f8fSGustavo F. Padovan { 3050a708f8fSGustavo F. Padovan BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, 306fe4128e0SGustavo F. Padovan chan->psm, chan->dcid); 3070a708f8fSGustavo F. Padovan 3089f5a0d7bSAndrei Emeltchenko conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; 3090a708f8fSGustavo F. Padovan 3108c1d787bSGustavo F. Padovan chan->conn = conn; 3110a708f8fSGustavo F. Padovan 3125491120eSAndrei Emeltchenko switch (chan->chan_type) { 3135491120eSAndrei Emeltchenko case L2CAP_CHAN_CONN_ORIENTED: 314b62f328bSVille Tervo if (conn->hcon->type == LE_LINK) { 315b62f328bSVille Tervo /* LE connection */ 3160c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_LE_DEFAULT_MTU; 317fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_LE_DATA; 318fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_LE_DATA; 319b62f328bSVille Tervo } else { 3200a708f8fSGustavo F. Padovan /* Alloc CID for connection-oriented socket */ 321fe4128e0SGustavo F. Padovan chan->scid = l2cap_alloc_cid(conn); 3220c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 323b62f328bSVille Tervo } 3245491120eSAndrei Emeltchenko break; 3255491120eSAndrei Emeltchenko 3265491120eSAndrei Emeltchenko case L2CAP_CHAN_CONN_LESS: 3270a708f8fSGustavo F. Padovan /* Connectionless socket */ 328fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_CONN_LESS; 329fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_CONN_LESS; 3300c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 3315491120eSAndrei Emeltchenko break; 3325491120eSAndrei Emeltchenko 3335491120eSAndrei Emeltchenko default: 3340a708f8fSGustavo F. Padovan /* Raw socket can send/recv signalling messages only */ 335fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_SIGNALING; 336fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_SIGNALING; 3370c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 3380a708f8fSGustavo F. Padovan } 3390a708f8fSGustavo F. Padovan 3408f7975b1SAndrei Emeltchenko chan->local_id = L2CAP_BESTEFFORT_ID; 3418f7975b1SAndrei Emeltchenko chan->local_stype = L2CAP_SERV_BESTEFFORT; 3428f7975b1SAndrei Emeltchenko chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; 3438f7975b1SAndrei Emeltchenko chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; 3448f7975b1SAndrei Emeltchenko chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; 3458f7975b1SAndrei Emeltchenko chan->local_flush_to = L2CAP_DEFAULT_FLUSH_TO; 3468f7975b1SAndrei Emeltchenko 347371fd835SUlisses Furquim l2cap_chan_hold(chan); 348baa7e1faSGustavo F. Padovan 3493df91ea2SAndrei Emeltchenko list_add(&chan->list, &conn->chan_l); 350643162a8SAndrei Emeltchenko } 351643162a8SAndrei Emeltchenko 352643162a8SAndrei Emeltchenko void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 353643162a8SAndrei Emeltchenko { 354643162a8SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 355643162a8SAndrei Emeltchenko __l2cap_chan_add(conn, chan); 3563df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 3570a708f8fSGustavo F. Padovan } 3580a708f8fSGustavo F. Padovan 3590a708f8fSGustavo F. Padovan /* Delete channel. 3600a708f8fSGustavo F. Padovan * Must be called on the locked socket. */ 3614519de9aSGustavo F. Padovan static void l2cap_chan_del(struct l2cap_chan *chan, int err) 3620a708f8fSGustavo F. Padovan { 36348454079SGustavo F. Padovan struct sock *sk = chan->sk; 3648c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 3650a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 3660a708f8fSGustavo F. Padovan 367c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 3680a708f8fSGustavo F. Padovan 36949208c9cSGustavo F. Padovan BT_DBG("chan %p, conn %p, err %d", chan, conn, err); 3700a708f8fSGustavo F. Padovan 3710a708f8fSGustavo F. Padovan if (conn) { 372baa7e1faSGustavo F. Padovan /* Delete from channel list */ 3733df91ea2SAndrei Emeltchenko list_del(&chan->list); 3743d57dc68SGustavo F. Padovan 375371fd835SUlisses Furquim l2cap_chan_put(chan); 376baa7e1faSGustavo F. Padovan 3778c1d787bSGustavo F. Padovan chan->conn = NULL; 3780a708f8fSGustavo F. Padovan hci_conn_put(conn->hcon); 3790a708f8fSGustavo F. Padovan } 3800a708f8fSGustavo F. Padovan 3810e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CLOSED); 3820a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 3830a708f8fSGustavo F. Padovan 3840a708f8fSGustavo F. Padovan if (err) 3852e0052e4SAndrei Emeltchenko __l2cap_chan_set_err(chan, err); 3860a708f8fSGustavo F. Padovan 3870a708f8fSGustavo F. Padovan if (parent) { 3880a708f8fSGustavo F. Padovan bt_accept_unlink(sk); 3890a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 3900a708f8fSGustavo F. Padovan } else 3910a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 3920a708f8fSGustavo F. Padovan 393c1360a1cSGustavo F. Padovan if (!(test_bit(CONF_OUTPUT_DONE, &chan->conf_state) && 394c1360a1cSGustavo F. Padovan test_bit(CONF_INPUT_DONE, &chan->conf_state))) 3956ff5abbfSGustavo F. Padovan return; 3962ead70b8SGustavo F. Padovan 39758d35f87SGustavo F. Padovan skb_queue_purge(&chan->tx_q); 3980a708f8fSGustavo F. Padovan 3990c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 4000a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 4010a708f8fSGustavo F. Padovan 4021a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 4031a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 4041a09bcb9SGustavo F. Padovan __clear_ack_timer(chan); 4050a708f8fSGustavo F. Padovan 406f1c6775bSGustavo F. Padovan skb_queue_purge(&chan->srej_q); 4070a708f8fSGustavo F. Padovan 40839d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 4090a708f8fSGustavo F. Padovan list_del(&l->list); 4100a708f8fSGustavo F. Padovan kfree(l); 4110a708f8fSGustavo F. Padovan } 4120a708f8fSGustavo F. Padovan } 4130a708f8fSGustavo F. Padovan } 4140a708f8fSGustavo F. Padovan 4154519de9aSGustavo F. Padovan static void l2cap_chan_cleanup_listen(struct sock *parent) 4164519de9aSGustavo F. Padovan { 4174519de9aSGustavo F. Padovan struct sock *sk; 4184519de9aSGustavo F. Padovan 4194519de9aSGustavo F. Padovan BT_DBG("parent %p", parent); 4204519de9aSGustavo F. Padovan 4214519de9aSGustavo F. Padovan /* Close not yet accepted channels */ 4220f852724SGustavo F. Padovan while ((sk = bt_accept_dequeue(parent, NULL))) { 423ba3bd0eeSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 4243df91ea2SAndrei Emeltchenko 425c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 4260f852724SGustavo F. Padovan lock_sock(sk); 427ba3bd0eeSGustavo F. Padovan l2cap_chan_close(chan, ECONNRESET); 4280f852724SGustavo F. Padovan release_sock(sk); 4293df91ea2SAndrei Emeltchenko 430ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 4310f852724SGustavo F. Padovan } 4324519de9aSGustavo F. Padovan } 4334519de9aSGustavo F. Padovan 4340f852724SGustavo F. Padovan void l2cap_chan_close(struct l2cap_chan *chan, int reason) 4354519de9aSGustavo F. Padovan { 4364519de9aSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 4374519de9aSGustavo F. Padovan struct sock *sk = chan->sk; 4384519de9aSGustavo F. Padovan 439e05dcc32SAndrei Emeltchenko BT_DBG("chan %p state %s sk %p", chan, 440e05dcc32SAndrei Emeltchenko state_to_string(chan->state), sk); 4414519de9aSGustavo F. Padovan 44289bc500eSGustavo F. Padovan switch (chan->state) { 4434519de9aSGustavo F. Padovan case BT_LISTEN: 4444519de9aSGustavo F. Padovan l2cap_chan_cleanup_listen(sk); 44589bc500eSGustavo F. Padovan 4460e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CLOSED); 44789bc500eSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 4484519de9aSGustavo F. Padovan break; 4494519de9aSGustavo F. Padovan 4504519de9aSGustavo F. Padovan case BT_CONNECTED: 4514519de9aSGustavo F. Padovan case BT_CONFIG: 452715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 4534519de9aSGustavo F. Padovan conn->hcon->type == ACL_LINK) { 454c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 455c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 4564519de9aSGustavo F. Padovan l2cap_send_disconn_req(conn, chan, reason); 4574519de9aSGustavo F. Padovan } else 4584519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4594519de9aSGustavo F. Padovan break; 4604519de9aSGustavo F. Padovan 4614519de9aSGustavo F. Padovan case BT_CONNECT2: 462715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 4634519de9aSGustavo F. Padovan conn->hcon->type == ACL_LINK) { 4644519de9aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 4654519de9aSGustavo F. Padovan __u16 result; 4664519de9aSGustavo F. Padovan 4674519de9aSGustavo F. Padovan if (bt_sk(sk)->defer_setup) 4684519de9aSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 4694519de9aSGustavo F. Padovan else 4704519de9aSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 47189bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_DISCONN); 4724519de9aSGustavo F. Padovan 4734519de9aSGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4744519de9aSGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 4754519de9aSGustavo F. Padovan rsp.result = cpu_to_le16(result); 4764519de9aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 4774519de9aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4784519de9aSGustavo F. Padovan sizeof(rsp), &rsp); 4794519de9aSGustavo F. Padovan } 4804519de9aSGustavo F. Padovan 4814519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4824519de9aSGustavo F. Padovan break; 4834519de9aSGustavo F. Padovan 4844519de9aSGustavo F. Padovan case BT_CONNECT: 4854519de9aSGustavo F. Padovan case BT_DISCONN: 4864519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4874519de9aSGustavo F. Padovan break; 4884519de9aSGustavo F. Padovan 4894519de9aSGustavo F. Padovan default: 4904519de9aSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 4914519de9aSGustavo F. Padovan break; 4924519de9aSGustavo F. Padovan } 4934519de9aSGustavo F. Padovan } 4944519de9aSGustavo F. Padovan 4954343478fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) 4960a708f8fSGustavo F. Padovan { 497715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_RAW) { 4984343478fSGustavo F. Padovan switch (chan->sec_level) { 4990a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 5000a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING_MITM; 5010a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 5020a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING; 5030a708f8fSGustavo F. Padovan default: 5040a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 5050a708f8fSGustavo F. Padovan } 506fe4128e0SGustavo F. Padovan } else if (chan->psm == cpu_to_le16(0x0001)) { 5074343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_LOW) 5084343478fSGustavo F. Padovan chan->sec_level = BT_SECURITY_SDP; 5090a708f8fSGustavo F. Padovan 5104343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_HIGH) 5110a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING_MITM; 5120a708f8fSGustavo F. Padovan else 5130a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 5140a708f8fSGustavo F. Padovan } else { 5154343478fSGustavo F. Padovan switch (chan->sec_level) { 5160a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 5170a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING_MITM; 5180a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 5190a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING; 5200a708f8fSGustavo F. Padovan default: 5210a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 5220a708f8fSGustavo F. Padovan } 5230a708f8fSGustavo F. Padovan } 5240a708f8fSGustavo F. Padovan } 5250a708f8fSGustavo F. Padovan 5260a708f8fSGustavo F. Padovan /* Service level security */ 527d45fc423SGustavo F. Padovan int l2cap_chan_check_security(struct l2cap_chan *chan) 5280a708f8fSGustavo F. Padovan { 5298c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 5300a708f8fSGustavo F. Padovan __u8 auth_type; 5310a708f8fSGustavo F. Padovan 5324343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 5330a708f8fSGustavo F. Padovan 5344343478fSGustavo F. Padovan return hci_conn_security(conn->hcon, chan->sec_level, auth_type); 5350a708f8fSGustavo F. Padovan } 5360a708f8fSGustavo F. Padovan 537b5ad8b7fSJohannes Berg static u8 l2cap_get_ident(struct l2cap_conn *conn) 5380a708f8fSGustavo F. Padovan { 5390a708f8fSGustavo F. Padovan u8 id; 5400a708f8fSGustavo F. Padovan 5410a708f8fSGustavo F. Padovan /* Get next available identificator. 5420a708f8fSGustavo F. Padovan * 1 - 128 are used by kernel. 5430a708f8fSGustavo F. Padovan * 129 - 199 are reserved. 5440a708f8fSGustavo F. Padovan * 200 - 254 are used by utilities like l2ping, etc. 5450a708f8fSGustavo F. Padovan */ 5460a708f8fSGustavo F. Padovan 547333055f2SGustavo F. Padovan spin_lock(&conn->lock); 5480a708f8fSGustavo F. Padovan 5490a708f8fSGustavo F. Padovan if (++conn->tx_ident > 128) 5500a708f8fSGustavo F. Padovan conn->tx_ident = 1; 5510a708f8fSGustavo F. Padovan 5520a708f8fSGustavo F. Padovan id = conn->tx_ident; 5530a708f8fSGustavo F. Padovan 554333055f2SGustavo F. Padovan spin_unlock(&conn->lock); 5550a708f8fSGustavo F. Padovan 5560a708f8fSGustavo F. Padovan return id; 5570a708f8fSGustavo F. Padovan } 5580a708f8fSGustavo F. Padovan 5594519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data) 5600a708f8fSGustavo F. Padovan { 5610a708f8fSGustavo F. Padovan struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); 5620a708f8fSGustavo F. Padovan u8 flags; 5630a708f8fSGustavo F. Padovan 5640a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x", code); 5650a708f8fSGustavo F. Padovan 5660a708f8fSGustavo F. Padovan if (!skb) 5670a708f8fSGustavo F. Padovan return; 5680a708f8fSGustavo F. Padovan 5690a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 5700a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 5710a708f8fSGustavo F. Padovan else 5720a708f8fSGustavo F. Padovan flags = ACL_START; 5730a708f8fSGustavo F. Padovan 57414b12d0bSJaikumar Ganesh bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; 5755e59b791SLuiz Augusto von Dentz skb->priority = HCI_PRIO_MAX; 57614b12d0bSJaikumar Ganesh 57773d80debSLuiz Augusto von Dentz hci_send_acl(conn->hchan, skb, flags); 5780a708f8fSGustavo F. Padovan } 5790a708f8fSGustavo F. Padovan 58073d80debSLuiz Augusto von Dentz static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) 58173d80debSLuiz Augusto von Dentz { 58273d80debSLuiz Augusto von Dentz struct hci_conn *hcon = chan->conn->hcon; 58373d80debSLuiz Augusto von Dentz u16 flags; 58473d80debSLuiz Augusto von Dentz 58573d80debSLuiz Augusto von Dentz BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len, 58673d80debSLuiz Augusto von Dentz skb->priority); 58773d80debSLuiz Augusto von Dentz 58873d80debSLuiz Augusto von Dentz if (!test_bit(FLAG_FLUSHABLE, &chan->flags) && 58973d80debSLuiz Augusto von Dentz lmp_no_flush_capable(hcon->hdev)) 59073d80debSLuiz Augusto von Dentz flags = ACL_START_NO_FLUSH; 59173d80debSLuiz Augusto von Dentz else 59273d80debSLuiz Augusto von Dentz flags = ACL_START; 59373d80debSLuiz Augusto von Dentz 59473d80debSLuiz Augusto von Dentz bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); 59573d80debSLuiz Augusto von Dentz hci_send_acl(chan->conn->hchan, skb, flags); 5960a708f8fSGustavo F. Padovan } 5970a708f8fSGustavo F. Padovan 59888843ab0SAndrei Emeltchenko static inline void l2cap_send_sframe(struct l2cap_chan *chan, u32 control) 5990a708f8fSGustavo F. Padovan { 6000a708f8fSGustavo F. Padovan struct sk_buff *skb; 6010a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 6028c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 603e4ca6d98SAndrei Emeltchenko int count, hlen; 6040a708f8fSGustavo F. Padovan 60589bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 6060a708f8fSGustavo F. Padovan return; 6070a708f8fSGustavo F. Padovan 608e4ca6d98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 609e4ca6d98SAndrei Emeltchenko hlen = L2CAP_EXT_HDR_SIZE; 610e4ca6d98SAndrei Emeltchenko else 611e4ca6d98SAndrei Emeltchenko hlen = L2CAP_ENH_HDR_SIZE; 6120a708f8fSGustavo F. Padovan 6130a708f8fSGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 61403a51213SAndrei Emeltchenko hlen += L2CAP_FCS_SIZE; 6150a708f8fSGustavo F. Padovan 61688843ab0SAndrei Emeltchenko BT_DBG("chan %p, control 0x%8.8x", chan, control); 6170a708f8fSGustavo F. Padovan 6180a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, hlen); 619793c2f1cSAndrei Emeltchenko 620793c2f1cSAndrei Emeltchenko control |= __set_sframe(chan); 6210a708f8fSGustavo F. Padovan 622e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 62303f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 6240a708f8fSGustavo F. Padovan 625e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_PBIT, &chan->conn_state)) 626e3781735SAndrei Emeltchenko control |= __set_ctrl_poll(chan); 6270a708f8fSGustavo F. Padovan 6280a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 6290a708f8fSGustavo F. Padovan if (!skb) 6300a708f8fSGustavo F. Padovan return; 6310a708f8fSGustavo F. Padovan 6320a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 6330a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); 634fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 63588843ab0SAndrei Emeltchenko 63688843ab0SAndrei Emeltchenko __put_control(chan, control, skb_put(skb, __ctrl_size(chan))); 6370a708f8fSGustavo F. Padovan 63847d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 63903a51213SAndrei Emeltchenko u16 fcs = crc16(0, (u8 *)lh, count - L2CAP_FCS_SIZE); 64003a51213SAndrei Emeltchenko put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); 6410a708f8fSGustavo F. Padovan } 6420a708f8fSGustavo F. Padovan 64373d80debSLuiz Augusto von Dentz skb->priority = HCI_PRIO_MAX; 64473d80debSLuiz Augusto von Dentz l2cap_do_send(chan, skb); 6450a708f8fSGustavo F. Padovan } 6460a708f8fSGustavo F. Padovan 64788843ab0SAndrei Emeltchenko static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u32 control) 6480a708f8fSGustavo F. Padovan { 649e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 650ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 651e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 6520a708f8fSGustavo F. Padovan } else 653ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 6540a708f8fSGustavo F. Padovan 6550b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 6560a708f8fSGustavo F. Padovan 657525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 6580a708f8fSGustavo F. Padovan } 6590a708f8fSGustavo F. Padovan 660b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) 6610a708f8fSGustavo F. Padovan { 662c1360a1cSGustavo F. Padovan return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); 6630a708f8fSGustavo F. Padovan } 6640a708f8fSGustavo F. Padovan 665fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan) 6660a708f8fSGustavo F. Padovan { 6678c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 6680a708f8fSGustavo F. Padovan 6690a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { 6700a708f8fSGustavo F. Padovan if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) 6710a708f8fSGustavo F. Padovan return; 6720a708f8fSGustavo F. Padovan 673d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan) && 6744343478fSGustavo F. Padovan __l2cap_no_conn_pending(chan)) { 6750a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 676fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 677fe4128e0SGustavo F. Padovan req.psm = chan->psm; 6780a708f8fSGustavo F. Padovan 679fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 680c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 6810a708f8fSGustavo F. Padovan 682fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 683fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 6840a708f8fSGustavo F. Padovan } 6850a708f8fSGustavo F. Padovan } else { 6860a708f8fSGustavo F. Padovan struct l2cap_info_req req; 6870a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 6880a708f8fSGustavo F. Padovan 6890a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 6900a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 6910a708f8fSGustavo F. Padovan 692030013d8SGustavo F. Padovan schedule_delayed_work(&conn->info_timer, 6930a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 6940a708f8fSGustavo F. Padovan 6950a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 6960a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 6970a708f8fSGustavo F. Padovan } 6980a708f8fSGustavo F. Padovan } 6990a708f8fSGustavo F. Padovan 7000a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) 7010a708f8fSGustavo F. Padovan { 7020a708f8fSGustavo F. Padovan u32 local_feat_mask = l2cap_feat_mask; 7030a708f8fSGustavo F. Padovan if (!disable_ertm) 7040a708f8fSGustavo F. Padovan local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; 7050a708f8fSGustavo F. Padovan 7060a708f8fSGustavo F. Padovan switch (mode) { 7070a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 7080a708f8fSGustavo F. Padovan return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; 7090a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 7100a708f8fSGustavo F. Padovan return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; 7110a708f8fSGustavo F. Padovan default: 7120a708f8fSGustavo F. Padovan return 0x00; 7130a708f8fSGustavo F. Padovan } 7140a708f8fSGustavo F. Padovan } 7150a708f8fSGustavo F. Padovan 7164519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err) 7170a708f8fSGustavo F. Padovan { 7180a708f8fSGustavo F. Padovan struct l2cap_disconn_req req; 7190a708f8fSGustavo F. Padovan 7200a708f8fSGustavo F. Padovan if (!conn) 7210a708f8fSGustavo F. Padovan return; 7220a708f8fSGustavo F. Padovan 7230c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 7241a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 7251a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 7261a09bcb9SGustavo F. Padovan __clear_ack_timer(chan); 7270a708f8fSGustavo F. Padovan } 7280a708f8fSGustavo F. Padovan 729fe4128e0SGustavo F. Padovan req.dcid = cpu_to_le16(chan->dcid); 730fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 7310a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 7320a708f8fSGustavo F. Padovan L2CAP_DISCONN_REQ, sizeof(req), &req); 7330a708f8fSGustavo F. Padovan 7340e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_DISCONN); 7352e0052e4SAndrei Emeltchenko __l2cap_chan_set_err(chan, err); 7360a708f8fSGustavo F. Padovan } 7370a708f8fSGustavo F. Padovan 7380a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */ 7390a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn) 7400a708f8fSGustavo F. Padovan { 7413df91ea2SAndrei Emeltchenko struct l2cap_chan *chan, *tmp; 7420a708f8fSGustavo F. Padovan 7430a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 7440a708f8fSGustavo F. Padovan 7453df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 7460a708f8fSGustavo F. Padovan 7473df91ea2SAndrei Emeltchenko list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { 74848454079SGustavo F. Padovan struct sock *sk = chan->sk; 749baa7e1faSGustavo F. Padovan 7500a708f8fSGustavo F. Padovan bh_lock_sock(sk); 7510a708f8fSGustavo F. Padovan 752715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 7530a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7540a708f8fSGustavo F. Padovan continue; 7550a708f8fSGustavo F. Padovan } 7560a708f8fSGustavo F. Padovan 75789bc500eSGustavo F. Padovan if (chan->state == BT_CONNECT) { 7580a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 7590a708f8fSGustavo F. Padovan 760d45fc423SGustavo F. Padovan if (!l2cap_chan_check_security(chan) || 761b4450035SGustavo F. Padovan !__l2cap_no_conn_pending(chan)) { 7620a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7630a708f8fSGustavo F. Padovan continue; 7640a708f8fSGustavo F. Padovan } 7650a708f8fSGustavo F. Padovan 766c1360a1cSGustavo F. Padovan if (!l2cap_mode_supported(chan->mode, conn->feat_mask) 767c1360a1cSGustavo F. Padovan && test_bit(CONF_STATE2_DEVICE, 768c1360a1cSGustavo F. Padovan &chan->conf_state)) { 7690f852724SGustavo F. Padovan /* l2cap_chan_close() calls list_del(chan) 770820ffdb3SGustavo F. Padovan * so release the lock */ 7710f852724SGustavo F. Padovan l2cap_chan_close(chan, ECONNRESET); 7720a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7730a708f8fSGustavo F. Padovan continue; 7740a708f8fSGustavo F. Padovan } 7750a708f8fSGustavo F. Padovan 776fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 777fe4128e0SGustavo F. Padovan req.psm = chan->psm; 7780a708f8fSGustavo F. Padovan 779fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 780c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 7810a708f8fSGustavo F. Padovan 782fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 783fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 7840a708f8fSGustavo F. Padovan 78589bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT2) { 7860a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 7870a708f8fSGustavo F. Padovan char buf[128]; 788fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 789fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 7900a708f8fSGustavo F. Padovan 791d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan)) { 7920a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 7930a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 7940a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 7950a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); 79605e9a2f6SIlia Kolomisnky if (parent) 7970a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 7980a708f8fSGustavo F. Padovan 7990a708f8fSGustavo F. Padovan } else { 8000e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONFIG); 8010a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 8020a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 8030a708f8fSGustavo F. Padovan } 8040a708f8fSGustavo F. Padovan } else { 8050a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 8060a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); 8070a708f8fSGustavo F. Padovan } 8080a708f8fSGustavo F. Padovan 809fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 810fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 8110a708f8fSGustavo F. Padovan 812c1360a1cSGustavo F. Padovan if (test_bit(CONF_REQ_SENT, &chan->conf_state) || 8130a708f8fSGustavo F. Padovan rsp.result != L2CAP_CR_SUCCESS) { 8140a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 8150a708f8fSGustavo F. Padovan continue; 8160a708f8fSGustavo F. Padovan } 8170a708f8fSGustavo F. Padovan 818c1360a1cSGustavo F. Padovan set_bit(CONF_REQ_SENT, &chan->conf_state); 8190a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 82073ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 82173ffa904SGustavo F. Padovan chan->num_conf_req++; 8220a708f8fSGustavo F. Padovan } 8230a708f8fSGustavo F. Padovan 8240a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 8250a708f8fSGustavo F. Padovan } 8260a708f8fSGustavo F. Padovan 8273df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 8280a708f8fSGustavo F. Padovan } 8290a708f8fSGustavo F. Padovan 830b62f328bSVille Tervo /* Find socket with cid and source bdaddr. 831b62f328bSVille Tervo * Returns closest match, locked. 832b62f328bSVille Tervo */ 83323691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src) 834b62f328bSVille Tervo { 83523691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 836b62f328bSVille Tervo 83723691d75SGustavo F. Padovan read_lock(&chan_list_lock); 838b62f328bSVille Tervo 83923691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 84023691d75SGustavo F. Padovan struct sock *sk = c->sk; 841fe4128e0SGustavo F. Padovan 84289bc500eSGustavo F. Padovan if (state && c->state != state) 843b62f328bSVille Tervo continue; 844b62f328bSVille Tervo 84523691d75SGustavo F. Padovan if (c->scid == cid) { 846b62f328bSVille Tervo /* Exact match. */ 84723691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 84823691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 84923691d75SGustavo F. Padovan return c; 85023691d75SGustavo F. Padovan } 851b62f328bSVille Tervo 852b62f328bSVille Tervo /* Closest match */ 853b62f328bSVille Tervo if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 85423691d75SGustavo F. Padovan c1 = c; 855b62f328bSVille Tervo } 856b62f328bSVille Tervo } 857280f294fSGustavo F. Padovan 85823691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 859b62f328bSVille Tervo 86023691d75SGustavo F. Padovan return c1; 861b62f328bSVille Tervo } 862b62f328bSVille Tervo 863b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn) 864b62f328bSVille Tervo { 865c916fbe4SGustavo F. Padovan struct sock *parent, *sk; 86623691d75SGustavo F. Padovan struct l2cap_chan *chan, *pchan; 867b62f328bSVille Tervo 868b62f328bSVille Tervo BT_DBG(""); 869b62f328bSVille Tervo 870b62f328bSVille Tervo /* Check if we have socket listening on cid */ 87123691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, 872b62f328bSVille Tervo conn->src); 87323691d75SGustavo F. Padovan if (!pchan) 874b62f328bSVille Tervo return; 875b62f328bSVille Tervo 87623691d75SGustavo F. Padovan parent = pchan->sk; 87723691d75SGustavo F. Padovan 878aa2ac881SGustavo F. Padovan lock_sock(parent); 87962f3a2cfSGustavo F. Padovan 880b62f328bSVille Tervo /* Check for backlog size */ 881b62f328bSVille Tervo if (sk_acceptq_is_full(parent)) { 882b62f328bSVille Tervo BT_DBG("backlog full %d", parent->sk_ack_backlog); 883b62f328bSVille Tervo goto clean; 884b62f328bSVille Tervo } 885b62f328bSVille Tervo 88680808e43SGustavo F. Padovan chan = pchan->ops->new_connection(pchan->data); 88780808e43SGustavo F. Padovan if (!chan) 888b62f328bSVille Tervo goto clean; 889b62f328bSVille Tervo 89080808e43SGustavo F. Padovan sk = chan->sk; 8915d41ce1dSGustavo F. Padovan 892b62f328bSVille Tervo hci_conn_hold(conn->hcon); 893b62f328bSVille Tervo 894b62f328bSVille Tervo bacpy(&bt_sk(sk)->src, conn->src); 895b62f328bSVille Tervo bacpy(&bt_sk(sk)->dst, conn->dst); 896b62f328bSVille Tervo 897d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 898d1010240SGustavo F. Padovan 8993d57dc68SGustavo F. Padovan l2cap_chan_add(conn, chan); 90048454079SGustavo F. Padovan 901c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 902b62f328bSVille Tervo 9030e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECTED); 904b62f328bSVille Tervo parent->sk_data_ready(parent, 0); 905b62f328bSVille Tervo 906b62f328bSVille Tervo clean: 907aa2ac881SGustavo F. Padovan release_sock(parent); 908b62f328bSVille Tervo } 909b62f328bSVille Tervo 910cf4cd009SAndrei Emeltchenko static void l2cap_chan_ready(struct l2cap_chan *chan) 911f1cb9af5SVinicius Costa Gomes { 912cf4cd009SAndrei Emeltchenko struct sock *sk = chan->sk; 913f1cb9af5SVinicius Costa Gomes struct sock *parent = bt_sk(sk)->parent; 914f1cb9af5SVinicius Costa Gomes 915f1cb9af5SVinicius Costa Gomes BT_DBG("sk %p, parent %p", sk, parent); 916f1cb9af5SVinicius Costa Gomes 917f1cb9af5SVinicius Costa Gomes chan->conf_state = 0; 918f1cb9af5SVinicius Costa Gomes __clear_chan_timer(chan); 919f1cb9af5SVinicius Costa Gomes 9200e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECTED); 921f1cb9af5SVinicius Costa Gomes sk->sk_state_change(sk); 922f1cb9af5SVinicius Costa Gomes 923f1cb9af5SVinicius Costa Gomes if (parent) 924f1cb9af5SVinicius Costa Gomes parent->sk_data_ready(parent, 0); 925f1cb9af5SVinicius Costa Gomes } 926f1cb9af5SVinicius Costa Gomes 9270a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn) 9280a708f8fSGustavo F. Padovan { 92948454079SGustavo F. Padovan struct l2cap_chan *chan; 9300a708f8fSGustavo F. Padovan 9310a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 9320a708f8fSGustavo F. Padovan 933b62f328bSVille Tervo if (!conn->hcon->out && conn->hcon->type == LE_LINK) 934b62f328bSVille Tervo l2cap_le_conn_ready(conn); 935b62f328bSVille Tervo 936160dc6acSVinicius Costa Gomes if (conn->hcon->out && conn->hcon->type == LE_LINK) 937160dc6acSVinicius Costa Gomes smp_conn_security(conn, conn->hcon->pending_sec_level); 938160dc6acSVinicius Costa Gomes 9393df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 9400a708f8fSGustavo F. Padovan 9413df91ea2SAndrei Emeltchenko list_for_each_entry(chan, &conn->chan_l, list) { 94248454079SGustavo F. Padovan struct sock *sk = chan->sk; 943baa7e1faSGustavo F. Padovan 9440a708f8fSGustavo F. Padovan bh_lock_sock(sk); 9450a708f8fSGustavo F. Padovan 94663128451SVinicius Costa Gomes if (conn->hcon->type == LE_LINK) { 947b501d6a1SAnderson Briglia if (smp_conn_security(conn, chan->sec_level)) 948cf4cd009SAndrei Emeltchenko l2cap_chan_ready(chan); 949acd7d370SVille Tervo 95063128451SVinicius Costa Gomes } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 951c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 9520e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECTED); 9530a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 954b501d6a1SAnderson Briglia 95589bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT) 956fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 9570a708f8fSGustavo F. Padovan 9580a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9590a708f8fSGustavo F. Padovan } 9600a708f8fSGustavo F. Padovan 9613df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 9620a708f8fSGustavo F. Padovan } 9630a708f8fSGustavo F. Padovan 9640a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */ 9650a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) 9660a708f8fSGustavo F. Padovan { 96748454079SGustavo F. Padovan struct l2cap_chan *chan; 9680a708f8fSGustavo F. Padovan 9690a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 9700a708f8fSGustavo F. Padovan 9713df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 9720a708f8fSGustavo F. Padovan 9733df91ea2SAndrei Emeltchenko list_for_each_entry(chan, &conn->chan_l, list) { 974ecf61bdbSAndrei Emeltchenko if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) 9752e0052e4SAndrei Emeltchenko __l2cap_chan_set_err(chan, err); 9760a708f8fSGustavo F. Padovan } 9770a708f8fSGustavo F. Padovan 9783df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 9790a708f8fSGustavo F. Padovan } 9800a708f8fSGustavo F. Padovan 981f878fcadSGustavo F. Padovan static void l2cap_info_timeout(struct work_struct *work) 9820a708f8fSGustavo F. Padovan { 983f878fcadSGustavo F. Padovan struct l2cap_conn *conn = container_of(work, struct l2cap_conn, 984030013d8SGustavo F. Padovan info_timer.work); 9850a708f8fSGustavo F. Padovan 9860a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 9870a708f8fSGustavo F. Padovan conn->info_ident = 0; 9880a708f8fSGustavo F. Padovan 9890a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 9900a708f8fSGustavo F. Padovan } 9910a708f8fSGustavo F. Padovan 9925d3de7dfSVinicius Costa Gomes static void l2cap_conn_del(struct hci_conn *hcon, int err) 9935d3de7dfSVinicius Costa Gomes { 9945d3de7dfSVinicius Costa Gomes struct l2cap_conn *conn = hcon->l2cap_data; 9955d3de7dfSVinicius Costa Gomes struct l2cap_chan *chan, *l; 9965d3de7dfSVinicius Costa Gomes struct sock *sk; 9975d3de7dfSVinicius Costa Gomes 9985d3de7dfSVinicius Costa Gomes if (!conn) 9995d3de7dfSVinicius Costa Gomes return; 10005d3de7dfSVinicius Costa Gomes 10015d3de7dfSVinicius Costa Gomes BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); 10025d3de7dfSVinicius Costa Gomes 10035d3de7dfSVinicius Costa Gomes kfree_skb(conn->rx_skb); 10045d3de7dfSVinicius Costa Gomes 10053df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 10063df91ea2SAndrei Emeltchenko 10075d3de7dfSVinicius Costa Gomes /* Kill channels */ 10085d3de7dfSVinicius Costa Gomes list_for_each_entry_safe(chan, l, &conn->chan_l, list) { 10095d3de7dfSVinicius Costa Gomes sk = chan->sk; 1010aa2ac881SGustavo F. Padovan lock_sock(sk); 10115d3de7dfSVinicius Costa Gomes l2cap_chan_del(chan, err); 1012aa2ac881SGustavo F. Padovan release_sock(sk); 10135d3de7dfSVinicius Costa Gomes chan->ops->close(chan->data); 10145d3de7dfSVinicius Costa Gomes } 10155d3de7dfSVinicius Costa Gomes 10163df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 10173df91ea2SAndrei Emeltchenko 101873d80debSLuiz Augusto von Dentz hci_chan_del(conn->hchan); 101973d80debSLuiz Augusto von Dentz 10205d3de7dfSVinicius Costa Gomes if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) 1021127074bfSUlisses Furquim cancel_delayed_work_sync(&conn->info_timer); 10225d3de7dfSVinicius Costa Gomes 102351a8efd7SJohan Hedberg if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) { 1024127074bfSUlisses Furquim cancel_delayed_work_sync(&conn->security_timer); 10258aab4757SVinicius Costa Gomes smp_chan_destroy(conn); 1026d26a2345SVinicius Costa Gomes } 10275d3de7dfSVinicius Costa Gomes 10285d3de7dfSVinicius Costa Gomes hcon->l2cap_data = NULL; 10295d3de7dfSVinicius Costa Gomes kfree(conn); 10305d3de7dfSVinicius Costa Gomes } 10315d3de7dfSVinicius Costa Gomes 10326c9d42a1SGustavo F. Padovan static void security_timeout(struct work_struct *work) 10335d3de7dfSVinicius Costa Gomes { 10346c9d42a1SGustavo F. Padovan struct l2cap_conn *conn = container_of(work, struct l2cap_conn, 10356c9d42a1SGustavo F. Padovan security_timer.work); 10365d3de7dfSVinicius Costa Gomes 10375d3de7dfSVinicius Costa Gomes l2cap_conn_del(conn->hcon, ETIMEDOUT); 10385d3de7dfSVinicius Costa Gomes } 10395d3de7dfSVinicius Costa Gomes 10400a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) 10410a708f8fSGustavo F. Padovan { 10420a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 104373d80debSLuiz Augusto von Dentz struct hci_chan *hchan; 10440a708f8fSGustavo F. Padovan 10450a708f8fSGustavo F. Padovan if (conn || status) 10460a708f8fSGustavo F. Padovan return conn; 10470a708f8fSGustavo F. Padovan 104873d80debSLuiz Augusto von Dentz hchan = hci_chan_create(hcon); 104973d80debSLuiz Augusto von Dentz if (!hchan) 10500a708f8fSGustavo F. Padovan return NULL; 10510a708f8fSGustavo F. Padovan 105273d80debSLuiz Augusto von Dentz conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); 105373d80debSLuiz Augusto von Dentz if (!conn) { 105473d80debSLuiz Augusto von Dentz hci_chan_del(hchan); 105573d80debSLuiz Augusto von Dentz return NULL; 105673d80debSLuiz Augusto von Dentz } 105773d80debSLuiz Augusto von Dentz 10580a708f8fSGustavo F. Padovan hcon->l2cap_data = conn; 10590a708f8fSGustavo F. Padovan conn->hcon = hcon; 106073d80debSLuiz Augusto von Dentz conn->hchan = hchan; 10610a708f8fSGustavo F. Padovan 106273d80debSLuiz Augusto von Dentz BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); 10630a708f8fSGustavo F. Padovan 1064acd7d370SVille Tervo if (hcon->hdev->le_mtu && hcon->type == LE_LINK) 1065acd7d370SVille Tervo conn->mtu = hcon->hdev->le_mtu; 1066acd7d370SVille Tervo else 10670a708f8fSGustavo F. Padovan conn->mtu = hcon->hdev->acl_mtu; 1068acd7d370SVille Tervo 10690a708f8fSGustavo F. Padovan conn->src = &hcon->hdev->bdaddr; 10700a708f8fSGustavo F. Padovan conn->dst = &hcon->dst; 10710a708f8fSGustavo F. Padovan 10720a708f8fSGustavo F. Padovan conn->feat_mask = 0; 10730a708f8fSGustavo F. Padovan 10740a708f8fSGustavo F. Padovan spin_lock_init(&conn->lock); 10753df91ea2SAndrei Emeltchenko mutex_init(&conn->chan_lock); 1076baa7e1faSGustavo F. Padovan 1077baa7e1faSGustavo F. Padovan INIT_LIST_HEAD(&conn->chan_l); 10780a708f8fSGustavo F. Padovan 10795d3de7dfSVinicius Costa Gomes if (hcon->type == LE_LINK) 10806c9d42a1SGustavo F. Padovan INIT_DELAYED_WORK(&conn->security_timer, security_timeout); 10815d3de7dfSVinicius Costa Gomes else 1082030013d8SGustavo F. Padovan INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); 10830a708f8fSGustavo F. Padovan 10849f5a0d7bSAndrei Emeltchenko conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; 10850a708f8fSGustavo F. Padovan 10860a708f8fSGustavo F. Padovan return conn; 10870a708f8fSGustavo F. Padovan } 10880a708f8fSGustavo F. Padovan 10890a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */ 10900a708f8fSGustavo F. Padovan 10910a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr. 10920a708f8fSGustavo F. Padovan * Returns closest match. 10930a708f8fSGustavo F. Padovan */ 109423691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src) 10950a708f8fSGustavo F. Padovan { 109623691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 10970a708f8fSGustavo F. Padovan 109823691d75SGustavo F. Padovan read_lock(&chan_list_lock); 10990a708f8fSGustavo F. Padovan 110023691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 110123691d75SGustavo F. Padovan struct sock *sk = c->sk; 1102fe4128e0SGustavo F. Padovan 110389bc500eSGustavo F. Padovan if (state && c->state != state) 11040a708f8fSGustavo F. Padovan continue; 11050a708f8fSGustavo F. Padovan 110623691d75SGustavo F. Padovan if (c->psm == psm) { 11070a708f8fSGustavo F. Padovan /* Exact match. */ 110823691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 1109a7567b20SJohannes Berg read_unlock(&chan_list_lock); 111023691d75SGustavo F. Padovan return c; 111123691d75SGustavo F. Padovan } 11120a708f8fSGustavo F. Padovan 11130a708f8fSGustavo F. Padovan /* Closest match */ 11140a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 111523691d75SGustavo F. Padovan c1 = c; 11160a708f8fSGustavo F. Padovan } 11170a708f8fSGustavo F. Padovan } 11180a708f8fSGustavo F. Padovan 111923691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 11200a708f8fSGustavo F. Padovan 112123691d75SGustavo F. Padovan return c1; 11220a708f8fSGustavo F. Padovan } 11230a708f8fSGustavo F. Padovan 1124cbe8fed4SJohan Hedberg int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, bdaddr_t *dst) 11250a708f8fSGustavo F. Padovan { 11265d41ce1dSGustavo F. Padovan struct sock *sk = chan->sk; 11270a708f8fSGustavo F. Padovan bdaddr_t *src = &bt_sk(sk)->src; 11280a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 11290a708f8fSGustavo F. Padovan struct hci_conn *hcon; 11300a708f8fSGustavo F. Padovan struct hci_dev *hdev; 11310a708f8fSGustavo F. Padovan __u8 auth_type; 11320a708f8fSGustavo F. Padovan int err; 11330a708f8fSGustavo F. Padovan 11340a708f8fSGustavo F. Padovan BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), 1135fe4128e0SGustavo F. Padovan chan->psm); 11360a708f8fSGustavo F. Padovan 11370a708f8fSGustavo F. Padovan hdev = hci_get_route(dst, src); 11380a708f8fSGustavo F. Padovan if (!hdev) 11390a708f8fSGustavo F. Padovan return -EHOSTUNREACH; 11400a708f8fSGustavo F. Padovan 114109fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 11420a708f8fSGustavo F. Padovan 114303a00194SGustavo F. Padovan lock_sock(sk); 114403a00194SGustavo F. Padovan 114503a00194SGustavo F. Padovan /* PSM must be odd and lsb of upper byte must be 0 */ 114603a00194SGustavo F. Padovan if ((__le16_to_cpu(psm) & 0x0101) != 0x0001 && !cid && 114703a00194SGustavo F. Padovan chan->chan_type != L2CAP_CHAN_RAW) { 114803a00194SGustavo F. Padovan err = -EINVAL; 114903a00194SGustavo F. Padovan goto done; 115003a00194SGustavo F. Padovan } 115103a00194SGustavo F. Padovan 115203a00194SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !(psm || cid)) { 115303a00194SGustavo F. Padovan err = -EINVAL; 115403a00194SGustavo F. Padovan goto done; 115503a00194SGustavo F. Padovan } 115603a00194SGustavo F. Padovan 115703a00194SGustavo F. Padovan switch (chan->mode) { 115803a00194SGustavo F. Padovan case L2CAP_MODE_BASIC: 115903a00194SGustavo F. Padovan break; 116003a00194SGustavo F. Padovan case L2CAP_MODE_ERTM: 116103a00194SGustavo F. Padovan case L2CAP_MODE_STREAMING: 116203a00194SGustavo F. Padovan if (!disable_ertm) 116303a00194SGustavo F. Padovan break; 116403a00194SGustavo F. Padovan /* fall through */ 116503a00194SGustavo F. Padovan default: 116603a00194SGustavo F. Padovan err = -ENOTSUPP; 116703a00194SGustavo F. Padovan goto done; 116803a00194SGustavo F. Padovan } 116903a00194SGustavo F. Padovan 117003a00194SGustavo F. Padovan switch (sk->sk_state) { 117103a00194SGustavo F. Padovan case BT_CONNECT: 117203a00194SGustavo F. Padovan case BT_CONNECT2: 117303a00194SGustavo F. Padovan case BT_CONFIG: 117403a00194SGustavo F. Padovan /* Already connecting */ 117503a00194SGustavo F. Padovan err = 0; 117603a00194SGustavo F. Padovan goto done; 117703a00194SGustavo F. Padovan 117803a00194SGustavo F. Padovan case BT_CONNECTED: 117903a00194SGustavo F. Padovan /* Already connected */ 118003a00194SGustavo F. Padovan err = -EISCONN; 118103a00194SGustavo F. Padovan goto done; 118203a00194SGustavo F. Padovan 118303a00194SGustavo F. Padovan case BT_OPEN: 118403a00194SGustavo F. Padovan case BT_BOUND: 118503a00194SGustavo F. Padovan /* Can connect */ 118603a00194SGustavo F. Padovan break; 118703a00194SGustavo F. Padovan 118803a00194SGustavo F. Padovan default: 118903a00194SGustavo F. Padovan err = -EBADFD; 119003a00194SGustavo F. Padovan goto done; 119103a00194SGustavo F. Padovan } 119203a00194SGustavo F. Padovan 119303a00194SGustavo F. Padovan /* Set destination address and psm */ 11949219b2a0SGustavo F. Padovan bacpy(&bt_sk(sk)->dst, dst); 119503a00194SGustavo F. Padovan chan->psm = psm; 119603a00194SGustavo F. Padovan chan->dcid = cid; 11970a708f8fSGustavo F. Padovan 11984343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 11990a708f8fSGustavo F. Padovan 1200fe4128e0SGustavo F. Padovan if (chan->dcid == L2CAP_CID_LE_DATA) 1201acd7d370SVille Tervo hcon = hci_connect(hdev, LE_LINK, dst, 12024343478fSGustavo F. Padovan chan->sec_level, auth_type); 1203acd7d370SVille Tervo else 12040a708f8fSGustavo F. Padovan hcon = hci_connect(hdev, ACL_LINK, dst, 12054343478fSGustavo F. Padovan chan->sec_level, auth_type); 1206acd7d370SVille Tervo 120730e76272SVille Tervo if (IS_ERR(hcon)) { 120830e76272SVille Tervo err = PTR_ERR(hcon); 12090a708f8fSGustavo F. Padovan goto done; 121030e76272SVille Tervo } 12110a708f8fSGustavo F. Padovan 12120a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 12130a708f8fSGustavo F. Padovan if (!conn) { 12140a708f8fSGustavo F. Padovan hci_conn_put(hcon); 121530e76272SVille Tervo err = -ENOMEM; 12160a708f8fSGustavo F. Padovan goto done; 12170a708f8fSGustavo F. Padovan } 12180a708f8fSGustavo F. Padovan 12190a708f8fSGustavo F. Padovan /* Update source addr of the socket */ 12200a708f8fSGustavo F. Padovan bacpy(src, conn->src); 12210a708f8fSGustavo F. Padovan 122248454079SGustavo F. Padovan l2cap_chan_add(conn, chan); 122348454079SGustavo F. Padovan 12240e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECT); 1225c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 12260a708f8fSGustavo F. Padovan 12270a708f8fSGustavo F. Padovan if (hcon->state == BT_CONNECTED) { 1228715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 1229c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 1230d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan)) 12310e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECTED); 12320a708f8fSGustavo F. Padovan } else 1233fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 12340a708f8fSGustavo F. Padovan } 12350a708f8fSGustavo F. Padovan 123630e76272SVille Tervo err = 0; 123730e76272SVille Tervo 12380a708f8fSGustavo F. Padovan done: 123909fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 12400a708f8fSGustavo F. Padovan hci_dev_put(hdev); 12410a708f8fSGustavo F. Padovan return err; 12420a708f8fSGustavo F. Padovan } 12430a708f8fSGustavo F. Padovan 1244dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk) 12450a708f8fSGustavo F. Padovan { 12468c1d787bSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 12470a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 12480a708f8fSGustavo F. Padovan int err = 0; 12490a708f8fSGustavo F. Padovan int timeo = HZ/5; 12500a708f8fSGustavo F. Padovan 12510a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 12520a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 1253a71a0cf4SPeter Hurley while (chan->unacked_frames > 0 && chan->conn) { 12540a708f8fSGustavo F. Padovan if (!timeo) 12550a708f8fSGustavo F. Padovan timeo = HZ/5; 12560a708f8fSGustavo F. Padovan 12570a708f8fSGustavo F. Padovan if (signal_pending(current)) { 12580a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 12590a708f8fSGustavo F. Padovan break; 12600a708f8fSGustavo F. Padovan } 12610a708f8fSGustavo F. Padovan 12620a708f8fSGustavo F. Padovan release_sock(sk); 12630a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 12640a708f8fSGustavo F. Padovan lock_sock(sk); 1265a71a0cf4SPeter Hurley set_current_state(TASK_INTERRUPTIBLE); 12660a708f8fSGustavo F. Padovan 12670a708f8fSGustavo F. Padovan err = sock_error(sk); 12680a708f8fSGustavo F. Padovan if (err) 12690a708f8fSGustavo F. Padovan break; 12700a708f8fSGustavo F. Padovan } 12710a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 12720a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 12730a708f8fSGustavo F. Padovan return err; 12740a708f8fSGustavo F. Padovan } 12750a708f8fSGustavo F. Padovan 1276721c4181SGustavo F. Padovan static void l2cap_monitor_timeout(struct work_struct *work) 12770a708f8fSGustavo F. Padovan { 1278721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 1279721c4181SGustavo F. Padovan monitor_timer.work); 1280525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 12810a708f8fSGustavo F. Padovan 1282525cd185SGustavo F. Padovan BT_DBG("chan %p", chan); 12830a708f8fSGustavo F. Padovan 1284721c4181SGustavo F. Padovan lock_sock(sk); 12852c03a7a4SGustavo F. Padovan if (chan->retry_count >= chan->remote_max_tx) { 12868c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 1287721c4181SGustavo F. Padovan release_sock(sk); 12880a708f8fSGustavo F. Padovan return; 12890a708f8fSGustavo F. Padovan } 12900a708f8fSGustavo F. Padovan 12916a026610SGustavo F. Padovan chan->retry_count++; 12921a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 12930a708f8fSGustavo F. Padovan 1294525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 1295721c4181SGustavo F. Padovan release_sock(sk); 12960a708f8fSGustavo F. Padovan } 12970a708f8fSGustavo F. Padovan 1298721c4181SGustavo F. Padovan static void l2cap_retrans_timeout(struct work_struct *work) 12990a708f8fSGustavo F. Padovan { 1300721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 1301721c4181SGustavo F. Padovan retrans_timer.work); 1302525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 13030a708f8fSGustavo F. Padovan 130449208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 13050a708f8fSGustavo F. Padovan 1306721c4181SGustavo F. Padovan lock_sock(sk); 13076a026610SGustavo F. Padovan chan->retry_count = 1; 13081a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 13090a708f8fSGustavo F. Padovan 1310e2ab4353SGustavo F. Padovan set_bit(CONN_WAIT_F, &chan->conn_state); 13110a708f8fSGustavo F. Padovan 1312525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 1313721c4181SGustavo F. Padovan release_sock(sk); 13140a708f8fSGustavo F. Padovan } 13150a708f8fSGustavo F. Padovan 131642e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan) 13170a708f8fSGustavo F. Padovan { 13180a708f8fSGustavo F. Padovan struct sk_buff *skb; 13190a708f8fSGustavo F. Padovan 132058d35f87SGustavo F. Padovan while ((skb = skb_peek(&chan->tx_q)) && 13216a026610SGustavo F. Padovan chan->unacked_frames) { 132242e5c802SGustavo F. Padovan if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) 13230a708f8fSGustavo F. Padovan break; 13240a708f8fSGustavo F. Padovan 132558d35f87SGustavo F. Padovan skb = skb_dequeue(&chan->tx_q); 13260a708f8fSGustavo F. Padovan kfree_skb(skb); 13270a708f8fSGustavo F. Padovan 13286a026610SGustavo F. Padovan chan->unacked_frames--; 13290a708f8fSGustavo F. Padovan } 13300a708f8fSGustavo F. Padovan 13316a026610SGustavo F. Padovan if (!chan->unacked_frames) 13321a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 13330a708f8fSGustavo F. Padovan } 13340a708f8fSGustavo F. Padovan 133567c9e840SSzymon Janc static void l2cap_streaming_send(struct l2cap_chan *chan) 13360a708f8fSGustavo F. Padovan { 13370a708f8fSGustavo F. Padovan struct sk_buff *skb; 133888843ab0SAndrei Emeltchenko u32 control; 133988843ab0SAndrei Emeltchenko u16 fcs; 13400a708f8fSGustavo F. Padovan 134158d35f87SGustavo F. Padovan while ((skb = skb_dequeue(&chan->tx_q))) { 134288843ab0SAndrei Emeltchenko control = __get_control(chan, skb->data + L2CAP_HDR_SIZE); 1343fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, chan->next_tx_seq); 134488843ab0SAndrei Emeltchenko __put_control(chan, control, skb->data + L2CAP_HDR_SIZE); 13450a708f8fSGustavo F. Padovan 134647d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 134703a51213SAndrei Emeltchenko fcs = crc16(0, (u8 *)skb->data, 134803a51213SAndrei Emeltchenko skb->len - L2CAP_FCS_SIZE); 134903a51213SAndrei Emeltchenko put_unaligned_le16(fcs, 135003a51213SAndrei Emeltchenko skb->data + skb->len - L2CAP_FCS_SIZE); 13510a708f8fSGustavo F. Padovan } 13520a708f8fSGustavo F. Padovan 13534343478fSGustavo F. Padovan l2cap_do_send(chan, skb); 13540a708f8fSGustavo F. Padovan 1355836be934SAndrei Emeltchenko chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); 13560a708f8fSGustavo F. Padovan } 13570a708f8fSGustavo F. Padovan } 13580a708f8fSGustavo F. Padovan 1359fb45de7dSAndrei Emeltchenko static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u16 tx_seq) 13600a708f8fSGustavo F. Padovan { 13610a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 136288843ab0SAndrei Emeltchenko u16 fcs; 136388843ab0SAndrei Emeltchenko u32 control; 13640a708f8fSGustavo F. Padovan 136558d35f87SGustavo F. Padovan skb = skb_peek(&chan->tx_q); 13660a708f8fSGustavo F. Padovan if (!skb) 13670a708f8fSGustavo F. Padovan return; 13680a708f8fSGustavo F. Padovan 1369d1726b6dSSzymon Janc while (bt_cb(skb)->tx_seq != tx_seq) { 137058d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 13710a708f8fSGustavo F. Padovan return; 13720a708f8fSGustavo F. Padovan 1373d1726b6dSSzymon Janc skb = skb_queue_next(&chan->tx_q, skb); 1374d1726b6dSSzymon Janc } 13750a708f8fSGustavo F. Padovan 13762c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 13772c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 13788c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 13790a708f8fSGustavo F. Padovan return; 13800a708f8fSGustavo F. Padovan } 13810a708f8fSGustavo F. Padovan 13820a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 13830a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 138488843ab0SAndrei Emeltchenko 138588843ab0SAndrei Emeltchenko control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE); 13867e0ef6eeSAndrei Emeltchenko control &= __get_sar_mask(chan); 13870a708f8fSGustavo F. Padovan 1388e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 138903f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 13900a708f8fSGustavo F. Padovan 13910b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 1392fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, tx_seq); 13930a708f8fSGustavo F. Padovan 139488843ab0SAndrei Emeltchenko __put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE); 13950a708f8fSGustavo F. Padovan 139647d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 139703a51213SAndrei Emeltchenko fcs = crc16(0, (u8 *)tx_skb->data, 139803a51213SAndrei Emeltchenko tx_skb->len - L2CAP_FCS_SIZE); 139903a51213SAndrei Emeltchenko put_unaligned_le16(fcs, 140003a51213SAndrei Emeltchenko tx_skb->data + tx_skb->len - L2CAP_FCS_SIZE); 14010a708f8fSGustavo F. Padovan } 14020a708f8fSGustavo F. Padovan 14034343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 14040a708f8fSGustavo F. Padovan } 14050a708f8fSGustavo F. Padovan 140667c9e840SSzymon Janc static int l2cap_ertm_send(struct l2cap_chan *chan) 14070a708f8fSGustavo F. Padovan { 14080a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 140988843ab0SAndrei Emeltchenko u16 fcs; 141088843ab0SAndrei Emeltchenko u32 control; 14110a708f8fSGustavo F. Padovan int nsent = 0; 14120a708f8fSGustavo F. Padovan 141389bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 14140a708f8fSGustavo F. Padovan return -ENOTCONN; 14150a708f8fSGustavo F. Padovan 141658d35f87SGustavo F. Padovan while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) { 14170a708f8fSGustavo F. Padovan 14182c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 14192c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 14208c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 14210a708f8fSGustavo F. Padovan break; 14220a708f8fSGustavo F. Padovan } 14230a708f8fSGustavo F. Padovan 14240a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 14250a708f8fSGustavo F. Padovan 14260a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 14270a708f8fSGustavo F. Padovan 142888843ab0SAndrei Emeltchenko control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE); 14297e0ef6eeSAndrei Emeltchenko control &= __get_sar_mask(chan); 14300a708f8fSGustavo F. Padovan 1431e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 143203f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 1433e2ab4353SGustavo F. Padovan 14340b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 1435fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, chan->next_tx_seq); 14360a708f8fSGustavo F. Padovan 143788843ab0SAndrei Emeltchenko __put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE); 14380a708f8fSGustavo F. Padovan 143947d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 144003a51213SAndrei Emeltchenko fcs = crc16(0, (u8 *)skb->data, 144103a51213SAndrei Emeltchenko tx_skb->len - L2CAP_FCS_SIZE); 144203a51213SAndrei Emeltchenko put_unaligned_le16(fcs, skb->data + 144303a51213SAndrei Emeltchenko tx_skb->len - L2CAP_FCS_SIZE); 14440a708f8fSGustavo F. Padovan } 14450a708f8fSGustavo F. Padovan 14464343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 14470a708f8fSGustavo F. Padovan 14481a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 14490a708f8fSGustavo F. Padovan 145042e5c802SGustavo F. Padovan bt_cb(skb)->tx_seq = chan->next_tx_seq; 1451836be934SAndrei Emeltchenko 1452836be934SAndrei Emeltchenko chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); 14530a708f8fSGustavo F. Padovan 14548ed7a0aeSSzymon Janc if (bt_cb(skb)->retries == 1) { 14556a026610SGustavo F. Padovan chan->unacked_frames++; 1456930fa4aeSSzymon Janc 1457930fa4aeSSzymon Janc if (!nsent++) 1458930fa4aeSSzymon Janc __clear_ack_timer(chan); 14598ed7a0aeSSzymon Janc } 146023e9fde2SSuraj Sumangala 14616a026610SGustavo F. Padovan chan->frames_sent++; 14620a708f8fSGustavo F. Padovan 146358d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 146458d35f87SGustavo F. Padovan chan->tx_send_head = NULL; 14650a708f8fSGustavo F. Padovan else 146658d35f87SGustavo F. Padovan chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); 14670a708f8fSGustavo F. Padovan } 14680a708f8fSGustavo F. Padovan 14690a708f8fSGustavo F. Padovan return nsent; 14700a708f8fSGustavo F. Padovan } 14710a708f8fSGustavo F. Padovan 1472525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan) 14730a708f8fSGustavo F. Padovan { 14740a708f8fSGustavo F. Padovan int ret; 14750a708f8fSGustavo F. Padovan 147658d35f87SGustavo F. Padovan if (!skb_queue_empty(&chan->tx_q)) 147758d35f87SGustavo F. Padovan chan->tx_send_head = chan->tx_q.next; 14780a708f8fSGustavo F. Padovan 147942e5c802SGustavo F. Padovan chan->next_tx_seq = chan->expected_ack_seq; 1480525cd185SGustavo F. Padovan ret = l2cap_ertm_send(chan); 14810a708f8fSGustavo F. Padovan return ret; 14820a708f8fSGustavo F. Padovan } 14830a708f8fSGustavo F. Padovan 1484b17e73bbSSzymon Janc static void __l2cap_send_ack(struct l2cap_chan *chan) 14850a708f8fSGustavo F. Padovan { 148688843ab0SAndrei Emeltchenko u32 control = 0; 14870a708f8fSGustavo F. Padovan 14880b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 14890a708f8fSGustavo F. Padovan 1490e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 1491ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 1492e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 1493525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 14940a708f8fSGustavo F. Padovan return; 14950a708f8fSGustavo F. Padovan } 14960a708f8fSGustavo F. Padovan 1497525cd185SGustavo F. Padovan if (l2cap_ertm_send(chan) > 0) 14980a708f8fSGustavo F. Padovan return; 14990a708f8fSGustavo F. Padovan 1500ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 1501525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 15020a708f8fSGustavo F. Padovan } 15030a708f8fSGustavo F. Padovan 1504b17e73bbSSzymon Janc static void l2cap_send_ack(struct l2cap_chan *chan) 1505b17e73bbSSzymon Janc { 1506b17e73bbSSzymon Janc __clear_ack_timer(chan); 1507b17e73bbSSzymon Janc __l2cap_send_ack(chan); 1508b17e73bbSSzymon Janc } 1509b17e73bbSSzymon Janc 1510525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan) 15110a708f8fSGustavo F. Padovan { 15120a708f8fSGustavo F. Padovan struct srej_list *tail; 151388843ab0SAndrei Emeltchenko u32 control; 15140a708f8fSGustavo F. Padovan 1515ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 151603f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 15170a708f8fSGustavo F. Padovan 151839d5a3eeSGustavo F. Padovan tail = list_entry((&chan->srej_l)->prev, struct srej_list, list); 15190b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, tail->tx_seq); 15200a708f8fSGustavo F. Padovan 1521525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 15220a708f8fSGustavo F. Padovan } 15230a708f8fSGustavo F. Padovan 15240952a57aSAndrei Emeltchenko static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, struct msghdr *msg, int len, int count, struct sk_buff *skb) 15250a708f8fSGustavo F. Padovan { 15260952a57aSAndrei Emeltchenko struct l2cap_conn *conn = chan->conn; 15270a708f8fSGustavo F. Padovan struct sk_buff **frag; 15280a708f8fSGustavo F. Padovan int err, sent = 0; 15290a708f8fSGustavo F. Padovan 15300a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) 15310a708f8fSGustavo F. Padovan return -EFAULT; 15320a708f8fSGustavo F. Padovan 15330a708f8fSGustavo F. Padovan sent += count; 15340a708f8fSGustavo F. Padovan len -= count; 15350a708f8fSGustavo F. Padovan 15360a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 15370a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 15380a708f8fSGustavo F. Padovan while (len) { 15390a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 15400a708f8fSGustavo F. Padovan 15412f7719ceSAndrei Emeltchenko *frag = chan->ops->alloc_skb(chan, count, 15422f7719ceSAndrei Emeltchenko msg->msg_flags & MSG_DONTWAIT, &err); 15432f7719ceSAndrei Emeltchenko 15440a708f8fSGustavo F. Padovan if (!*frag) 15450a708f8fSGustavo F. Padovan return err; 15460a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) 15470a708f8fSGustavo F. Padovan return -EFAULT; 15480a708f8fSGustavo F. Padovan 15495e59b791SLuiz Augusto von Dentz (*frag)->priority = skb->priority; 15505e59b791SLuiz Augusto von Dentz 15510a708f8fSGustavo F. Padovan sent += count; 15520a708f8fSGustavo F. Padovan len -= count; 15530a708f8fSGustavo F. Padovan 15540a708f8fSGustavo F. Padovan frag = &(*frag)->next; 15550a708f8fSGustavo F. Padovan } 15560a708f8fSGustavo F. Padovan 15570a708f8fSGustavo F. Padovan return sent; 15580a708f8fSGustavo F. Padovan } 15590a708f8fSGustavo F. Padovan 15605e59b791SLuiz Augusto von Dentz static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, 15615e59b791SLuiz Augusto von Dentz struct msghdr *msg, size_t len, 15625e59b791SLuiz Augusto von Dentz u32 priority) 15630a708f8fSGustavo F. Padovan { 15648c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 15650a708f8fSGustavo F. Padovan struct sk_buff *skb; 156603a51213SAndrei Emeltchenko int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; 15670a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 15680a708f8fSGustavo F. Padovan 15696d5922b0SAndrei Emeltchenko BT_DBG("chan %p len %d priority %u", chan, (int)len, priority); 15700a708f8fSGustavo F. Padovan 15710a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 15722f7719ceSAndrei Emeltchenko 15732f7719ceSAndrei Emeltchenko skb = chan->ops->alloc_skb(chan, count + hlen, 15740a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 15752f7719ceSAndrei Emeltchenko 15760a708f8fSGustavo F. Padovan if (!skb) 15770a708f8fSGustavo F. Padovan return ERR_PTR(err); 15780a708f8fSGustavo F. Padovan 15795e59b791SLuiz Augusto von Dentz skb->priority = priority; 15805e59b791SLuiz Augusto von Dentz 15810a708f8fSGustavo F. Padovan /* Create L2CAP header */ 15820a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1583fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 15840a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 1585fe4128e0SGustavo F. Padovan put_unaligned_le16(chan->psm, skb_put(skb, 2)); 15860a708f8fSGustavo F. Padovan 15870952a57aSAndrei Emeltchenko err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); 15880a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 15890a708f8fSGustavo F. Padovan kfree_skb(skb); 15900a708f8fSGustavo F. Padovan return ERR_PTR(err); 15910a708f8fSGustavo F. Padovan } 15920a708f8fSGustavo F. Padovan return skb; 15930a708f8fSGustavo F. Padovan } 15940a708f8fSGustavo F. Padovan 15955e59b791SLuiz Augusto von Dentz static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, 15965e59b791SLuiz Augusto von Dentz struct msghdr *msg, size_t len, 15975e59b791SLuiz Augusto von Dentz u32 priority) 15980a708f8fSGustavo F. Padovan { 15998c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 16000a708f8fSGustavo F. Padovan struct sk_buff *skb; 16010a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE; 16020a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 16030a708f8fSGustavo F. Padovan 16046d5922b0SAndrei Emeltchenko BT_DBG("chan %p len %d", chan, (int)len); 16050a708f8fSGustavo F. Padovan 16060a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 16072f7719ceSAndrei Emeltchenko 16082f7719ceSAndrei Emeltchenko skb = chan->ops->alloc_skb(chan, count + hlen, 16090a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 16102f7719ceSAndrei Emeltchenko 16110a708f8fSGustavo F. Padovan if (!skb) 16120a708f8fSGustavo F. Padovan return ERR_PTR(err); 16130a708f8fSGustavo F. Padovan 16145e59b791SLuiz Augusto von Dentz skb->priority = priority; 16155e59b791SLuiz Augusto von Dentz 16160a708f8fSGustavo F. Padovan /* Create L2CAP header */ 16170a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1618fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 16190a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 16200a708f8fSGustavo F. Padovan 16210952a57aSAndrei Emeltchenko err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); 16220a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 16230a708f8fSGustavo F. Padovan kfree_skb(skb); 16240a708f8fSGustavo F. Padovan return ERR_PTR(err); 16250a708f8fSGustavo F. Padovan } 16260a708f8fSGustavo F. Padovan return skb; 16270a708f8fSGustavo F. Padovan } 16280a708f8fSGustavo F. Padovan 1629ab0ff76dSLuiz Augusto von Dentz static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, 1630ab0ff76dSLuiz Augusto von Dentz struct msghdr *msg, size_t len, 163188843ab0SAndrei Emeltchenko u32 control, u16 sdulen) 16320a708f8fSGustavo F. Padovan { 16338c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 16340a708f8fSGustavo F. Padovan struct sk_buff *skb; 1635e4ca6d98SAndrei Emeltchenko int err, count, hlen; 16360a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 16370a708f8fSGustavo F. Padovan 16386d5922b0SAndrei Emeltchenko BT_DBG("chan %p len %d", chan, (int)len); 16390a708f8fSGustavo F. Padovan 16400a708f8fSGustavo F. Padovan if (!conn) 16410a708f8fSGustavo F. Padovan return ERR_PTR(-ENOTCONN); 16420a708f8fSGustavo F. Padovan 1643e4ca6d98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 1644e4ca6d98SAndrei Emeltchenko hlen = L2CAP_EXT_HDR_SIZE; 1645e4ca6d98SAndrei Emeltchenko else 1646e4ca6d98SAndrei Emeltchenko hlen = L2CAP_ENH_HDR_SIZE; 1647e4ca6d98SAndrei Emeltchenko 16480a708f8fSGustavo F. Padovan if (sdulen) 164903a51213SAndrei Emeltchenko hlen += L2CAP_SDULEN_SIZE; 16500a708f8fSGustavo F. Padovan 165147d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 165203a51213SAndrei Emeltchenko hlen += L2CAP_FCS_SIZE; 16530a708f8fSGustavo F. Padovan 16540a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 16552f7719ceSAndrei Emeltchenko 16562f7719ceSAndrei Emeltchenko skb = chan->ops->alloc_skb(chan, count + hlen, 16570a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 16582f7719ceSAndrei Emeltchenko 16590a708f8fSGustavo F. Padovan if (!skb) 16600a708f8fSGustavo F. Padovan return ERR_PTR(err); 16610a708f8fSGustavo F. Padovan 16620a708f8fSGustavo F. Padovan /* Create L2CAP header */ 16630a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1664fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 16650a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 166688843ab0SAndrei Emeltchenko 166788843ab0SAndrei Emeltchenko __put_control(chan, control, skb_put(skb, __ctrl_size(chan))); 166888843ab0SAndrei Emeltchenko 16690a708f8fSGustavo F. Padovan if (sdulen) 167003a51213SAndrei Emeltchenko put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); 16710a708f8fSGustavo F. Padovan 16720952a57aSAndrei Emeltchenko err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); 16730a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 16740a708f8fSGustavo F. Padovan kfree_skb(skb); 16750a708f8fSGustavo F. Padovan return ERR_PTR(err); 16760a708f8fSGustavo F. Padovan } 16770a708f8fSGustavo F. Padovan 167847d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 167903a51213SAndrei Emeltchenko put_unaligned_le16(0, skb_put(skb, L2CAP_FCS_SIZE)); 16800a708f8fSGustavo F. Padovan 16810a708f8fSGustavo F. Padovan bt_cb(skb)->retries = 0; 16820a708f8fSGustavo F. Padovan return skb; 16830a708f8fSGustavo F. Padovan } 16840a708f8fSGustavo F. Padovan 168567c9e840SSzymon Janc static int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 16860a708f8fSGustavo F. Padovan { 16870a708f8fSGustavo F. Padovan struct sk_buff *skb; 16880a708f8fSGustavo F. Padovan struct sk_buff_head sar_queue; 168988843ab0SAndrei Emeltchenko u32 control; 16900a708f8fSGustavo F. Padovan size_t size = 0; 16910a708f8fSGustavo F. Padovan 16920a708f8fSGustavo F. Padovan skb_queue_head_init(&sar_queue); 16937e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_START); 169447d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len); 16950a708f8fSGustavo F. Padovan if (IS_ERR(skb)) 16960a708f8fSGustavo F. Padovan return PTR_ERR(skb); 16970a708f8fSGustavo F. Padovan 16980a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 16992c03a7a4SGustavo F. Padovan len -= chan->remote_mps; 17002c03a7a4SGustavo F. Padovan size += chan->remote_mps; 17010a708f8fSGustavo F. Padovan 17020a708f8fSGustavo F. Padovan while (len > 0) { 17030a708f8fSGustavo F. Padovan size_t buflen; 17040a708f8fSGustavo F. Padovan 17052c03a7a4SGustavo F. Padovan if (len > chan->remote_mps) { 17067e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_CONTINUE); 17072c03a7a4SGustavo F. Padovan buflen = chan->remote_mps; 17080a708f8fSGustavo F. Padovan } else { 17097e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_END); 17100a708f8fSGustavo F. Padovan buflen = len; 17110a708f8fSGustavo F. Padovan } 17120a708f8fSGustavo F. Padovan 171347d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0); 17140a708f8fSGustavo F. Padovan if (IS_ERR(skb)) { 17150a708f8fSGustavo F. Padovan skb_queue_purge(&sar_queue); 17160a708f8fSGustavo F. Padovan return PTR_ERR(skb); 17170a708f8fSGustavo F. Padovan } 17180a708f8fSGustavo F. Padovan 17190a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 17200a708f8fSGustavo F. Padovan len -= buflen; 17210a708f8fSGustavo F. Padovan size += buflen; 17220a708f8fSGustavo F. Padovan } 172358d35f87SGustavo F. Padovan skb_queue_splice_tail(&sar_queue, &chan->tx_q); 172458d35f87SGustavo F. Padovan if (chan->tx_send_head == NULL) 172558d35f87SGustavo F. Padovan chan->tx_send_head = sar_queue.next; 17260a708f8fSGustavo F. Padovan 17270a708f8fSGustavo F. Padovan return size; 17280a708f8fSGustavo F. Padovan } 17290a708f8fSGustavo F. Padovan 17305e59b791SLuiz Augusto von Dentz int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, 17315e59b791SLuiz Augusto von Dentz u32 priority) 17329a91a04aSGustavo F. Padovan { 17339a91a04aSGustavo F. Padovan struct sk_buff *skb; 173488843ab0SAndrei Emeltchenko u32 control; 17359a91a04aSGustavo F. Padovan int err; 17369a91a04aSGustavo F. Padovan 17379a91a04aSGustavo F. Padovan /* Connectionless channel */ 1738715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { 17395e59b791SLuiz Augusto von Dentz skb = l2cap_create_connless_pdu(chan, msg, len, priority); 17409a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 17419a91a04aSGustavo F. Padovan return PTR_ERR(skb); 17429a91a04aSGustavo F. Padovan 17439a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 17449a91a04aSGustavo F. Padovan return len; 17459a91a04aSGustavo F. Padovan } 17469a91a04aSGustavo F. Padovan 17479a91a04aSGustavo F. Padovan switch (chan->mode) { 17489a91a04aSGustavo F. Padovan case L2CAP_MODE_BASIC: 17499a91a04aSGustavo F. Padovan /* Check outgoing MTU */ 17509a91a04aSGustavo F. Padovan if (len > chan->omtu) 17519a91a04aSGustavo F. Padovan return -EMSGSIZE; 17529a91a04aSGustavo F. Padovan 17539a91a04aSGustavo F. Padovan /* Create a basic PDU */ 17545e59b791SLuiz Augusto von Dentz skb = l2cap_create_basic_pdu(chan, msg, len, priority); 17559a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 17569a91a04aSGustavo F. Padovan return PTR_ERR(skb); 17579a91a04aSGustavo F. Padovan 17589a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 17599a91a04aSGustavo F. Padovan err = len; 17609a91a04aSGustavo F. Padovan break; 17619a91a04aSGustavo F. Padovan 17629a91a04aSGustavo F. Padovan case L2CAP_MODE_ERTM: 17639a91a04aSGustavo F. Padovan case L2CAP_MODE_STREAMING: 17649a91a04aSGustavo F. Padovan /* Entire SDU fits into one PDU */ 17659a91a04aSGustavo F. Padovan if (len <= chan->remote_mps) { 17667e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_UNSEGMENTED); 17679a91a04aSGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, len, control, 17689a91a04aSGustavo F. Padovan 0); 17699a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 17709a91a04aSGustavo F. Padovan return PTR_ERR(skb); 17719a91a04aSGustavo F. Padovan 17729a91a04aSGustavo F. Padovan __skb_queue_tail(&chan->tx_q, skb); 17739a91a04aSGustavo F. Padovan 17749a91a04aSGustavo F. Padovan if (chan->tx_send_head == NULL) 17759a91a04aSGustavo F. Padovan chan->tx_send_head = skb; 17769a91a04aSGustavo F. Padovan 17779a91a04aSGustavo F. Padovan } else { 17789a91a04aSGustavo F. Padovan /* Segment SDU into multiples PDUs */ 17799a91a04aSGustavo F. Padovan err = l2cap_sar_segment_sdu(chan, msg, len); 17809a91a04aSGustavo F. Padovan if (err < 0) 17819a91a04aSGustavo F. Padovan return err; 17829a91a04aSGustavo F. Padovan } 17839a91a04aSGustavo F. Padovan 17849a91a04aSGustavo F. Padovan if (chan->mode == L2CAP_MODE_STREAMING) { 17859a91a04aSGustavo F. Padovan l2cap_streaming_send(chan); 17869a91a04aSGustavo F. Padovan err = len; 17879a91a04aSGustavo F. Padovan break; 17889a91a04aSGustavo F. Padovan } 17899a91a04aSGustavo F. Padovan 1790e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 1791e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 17929a91a04aSGustavo F. Padovan err = len; 17939a91a04aSGustavo F. Padovan break; 17949a91a04aSGustavo F. Padovan } 17959a91a04aSGustavo F. Padovan 17969a91a04aSGustavo F. Padovan err = l2cap_ertm_send(chan); 17979a91a04aSGustavo F. Padovan if (err >= 0) 17989a91a04aSGustavo F. Padovan err = len; 17999a91a04aSGustavo F. Padovan 18009a91a04aSGustavo F. Padovan break; 18019a91a04aSGustavo F. Padovan 18029a91a04aSGustavo F. Padovan default: 18039a91a04aSGustavo F. Padovan BT_DBG("bad state %1.1x", chan->mode); 18049a91a04aSGustavo F. Padovan err = -EBADFD; 18059a91a04aSGustavo F. Padovan } 18069a91a04aSGustavo F. Padovan 18079a91a04aSGustavo F. Padovan return err; 18089a91a04aSGustavo F. Padovan } 18099a91a04aSGustavo F. Padovan 18100a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */ 18110a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) 18120a708f8fSGustavo F. Padovan { 18130a708f8fSGustavo F. Padovan struct sk_buff *nskb; 181448454079SGustavo F. Padovan struct l2cap_chan *chan; 18150a708f8fSGustavo F. Padovan 18160a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 18170a708f8fSGustavo F. Padovan 18183df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 18193d57dc68SGustavo F. Padovan 18203df91ea2SAndrei Emeltchenko list_for_each_entry(chan, &conn->chan_l, list) { 182148454079SGustavo F. Padovan struct sock *sk = chan->sk; 1822715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_RAW) 18230a708f8fSGustavo F. Padovan continue; 18240a708f8fSGustavo F. Padovan 18250a708f8fSGustavo F. Padovan /* Don't send frame to the socket it came from */ 18260a708f8fSGustavo F. Padovan if (skb->sk == sk) 18270a708f8fSGustavo F. Padovan continue; 18280a708f8fSGustavo F. Padovan nskb = skb_clone(skb, GFP_ATOMIC); 18290a708f8fSGustavo F. Padovan if (!nskb) 18300a708f8fSGustavo F. Padovan continue; 18310a708f8fSGustavo F. Padovan 183223070494SGustavo F. Padovan if (chan->ops->recv(chan->data, nskb)) 18330a708f8fSGustavo F. Padovan kfree_skb(nskb); 18340a708f8fSGustavo F. Padovan } 18353d57dc68SGustavo F. Padovan 18363df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 18370a708f8fSGustavo F. Padovan } 18380a708f8fSGustavo F. Padovan 18390a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */ 18400a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 18410a708f8fSGustavo F. Padovan u8 code, u8 ident, u16 dlen, void *data) 18420a708f8fSGustavo F. Padovan { 18430a708f8fSGustavo F. Padovan struct sk_buff *skb, **frag; 18440a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr *cmd; 18450a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 18460a708f8fSGustavo F. Padovan int len, count; 18470a708f8fSGustavo F. Padovan 18480a708f8fSGustavo F. Padovan BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", 18490a708f8fSGustavo F. Padovan conn, code, ident, dlen); 18500a708f8fSGustavo F. Padovan 18510a708f8fSGustavo F. Padovan len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; 18520a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 18530a708f8fSGustavo F. Padovan 18540a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 18550a708f8fSGustavo F. Padovan if (!skb) 18560a708f8fSGustavo F. Padovan return NULL; 18570a708f8fSGustavo F. Padovan 18580a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 18590a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); 18603300d9a9SClaudio Takahasi 18613300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 18623300d9a9SClaudio Takahasi lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); 18633300d9a9SClaudio Takahasi else 18640a708f8fSGustavo F. Padovan lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); 18650a708f8fSGustavo F. Padovan 18660a708f8fSGustavo F. Padovan cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); 18670a708f8fSGustavo F. Padovan cmd->code = code; 18680a708f8fSGustavo F. Padovan cmd->ident = ident; 18690a708f8fSGustavo F. Padovan cmd->len = cpu_to_le16(dlen); 18700a708f8fSGustavo F. Padovan 18710a708f8fSGustavo F. Padovan if (dlen) { 18720a708f8fSGustavo F. Padovan count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; 18730a708f8fSGustavo F. Padovan memcpy(skb_put(skb, count), data, count); 18740a708f8fSGustavo F. Padovan data += count; 18750a708f8fSGustavo F. Padovan } 18760a708f8fSGustavo F. Padovan 18770a708f8fSGustavo F. Padovan len -= skb->len; 18780a708f8fSGustavo F. Padovan 18790a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 18800a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 18810a708f8fSGustavo F. Padovan while (len) { 18820a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 18830a708f8fSGustavo F. Padovan 18840a708f8fSGustavo F. Padovan *frag = bt_skb_alloc(count, GFP_ATOMIC); 18850a708f8fSGustavo F. Padovan if (!*frag) 18860a708f8fSGustavo F. Padovan goto fail; 18870a708f8fSGustavo F. Padovan 18880a708f8fSGustavo F. Padovan memcpy(skb_put(*frag, count), data, count); 18890a708f8fSGustavo F. Padovan 18900a708f8fSGustavo F. Padovan len -= count; 18910a708f8fSGustavo F. Padovan data += count; 18920a708f8fSGustavo F. Padovan 18930a708f8fSGustavo F. Padovan frag = &(*frag)->next; 18940a708f8fSGustavo F. Padovan } 18950a708f8fSGustavo F. Padovan 18960a708f8fSGustavo F. Padovan return skb; 18970a708f8fSGustavo F. Padovan 18980a708f8fSGustavo F. Padovan fail: 18990a708f8fSGustavo F. Padovan kfree_skb(skb); 19000a708f8fSGustavo F. Padovan return NULL; 19010a708f8fSGustavo F. Padovan } 19020a708f8fSGustavo F. Padovan 19030a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) 19040a708f8fSGustavo F. Padovan { 19050a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 19060a708f8fSGustavo F. Padovan int len; 19070a708f8fSGustavo F. Padovan 19080a708f8fSGustavo F. Padovan len = L2CAP_CONF_OPT_SIZE + opt->len; 19090a708f8fSGustavo F. Padovan *ptr += len; 19100a708f8fSGustavo F. Padovan 19110a708f8fSGustavo F. Padovan *type = opt->type; 19120a708f8fSGustavo F. Padovan *olen = opt->len; 19130a708f8fSGustavo F. Padovan 19140a708f8fSGustavo F. Padovan switch (opt->len) { 19150a708f8fSGustavo F. Padovan case 1: 19160a708f8fSGustavo F. Padovan *val = *((u8 *) opt->val); 19170a708f8fSGustavo F. Padovan break; 19180a708f8fSGustavo F. Padovan 19190a708f8fSGustavo F. Padovan case 2: 19200a708f8fSGustavo F. Padovan *val = get_unaligned_le16(opt->val); 19210a708f8fSGustavo F. Padovan break; 19220a708f8fSGustavo F. Padovan 19230a708f8fSGustavo F. Padovan case 4: 19240a708f8fSGustavo F. Padovan *val = get_unaligned_le32(opt->val); 19250a708f8fSGustavo F. Padovan break; 19260a708f8fSGustavo F. Padovan 19270a708f8fSGustavo F. Padovan default: 19280a708f8fSGustavo F. Padovan *val = (unsigned long) opt->val; 19290a708f8fSGustavo F. Padovan break; 19300a708f8fSGustavo F. Padovan } 19310a708f8fSGustavo F. Padovan 19320a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); 19330a708f8fSGustavo F. Padovan return len; 19340a708f8fSGustavo F. Padovan } 19350a708f8fSGustavo F. Padovan 19360a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) 19370a708f8fSGustavo F. Padovan { 19380a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 19390a708f8fSGustavo F. Padovan 19400a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); 19410a708f8fSGustavo F. Padovan 19420a708f8fSGustavo F. Padovan opt->type = type; 19430a708f8fSGustavo F. Padovan opt->len = len; 19440a708f8fSGustavo F. Padovan 19450a708f8fSGustavo F. Padovan switch (len) { 19460a708f8fSGustavo F. Padovan case 1: 19470a708f8fSGustavo F. Padovan *((u8 *) opt->val) = val; 19480a708f8fSGustavo F. Padovan break; 19490a708f8fSGustavo F. Padovan 19500a708f8fSGustavo F. Padovan case 2: 19510a708f8fSGustavo F. Padovan put_unaligned_le16(val, opt->val); 19520a708f8fSGustavo F. Padovan break; 19530a708f8fSGustavo F. Padovan 19540a708f8fSGustavo F. Padovan case 4: 19550a708f8fSGustavo F. Padovan put_unaligned_le32(val, opt->val); 19560a708f8fSGustavo F. Padovan break; 19570a708f8fSGustavo F. Padovan 19580a708f8fSGustavo F. Padovan default: 19590a708f8fSGustavo F. Padovan memcpy(opt->val, (void *) val, len); 19600a708f8fSGustavo F. Padovan break; 19610a708f8fSGustavo F. Padovan } 19620a708f8fSGustavo F. Padovan 19630a708f8fSGustavo F. Padovan *ptr += L2CAP_CONF_OPT_SIZE + len; 19640a708f8fSGustavo F. Padovan } 19650a708f8fSGustavo F. Padovan 1966f89cef09SAndrei Emeltchenko static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) 1967f89cef09SAndrei Emeltchenko { 1968f89cef09SAndrei Emeltchenko struct l2cap_conf_efs efs; 1969f89cef09SAndrei Emeltchenko 1970f89cef09SAndrei Emeltchenko switch (chan->mode) { 1971f89cef09SAndrei Emeltchenko case L2CAP_MODE_ERTM: 1972f89cef09SAndrei Emeltchenko efs.id = chan->local_id; 1973f89cef09SAndrei Emeltchenko efs.stype = chan->local_stype; 1974f89cef09SAndrei Emeltchenko efs.msdu = cpu_to_le16(chan->local_msdu); 1975f89cef09SAndrei Emeltchenko efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); 1976f89cef09SAndrei Emeltchenko efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); 1977f89cef09SAndrei Emeltchenko efs.flush_to = cpu_to_le32(L2CAP_DEFAULT_FLUSH_TO); 1978f89cef09SAndrei Emeltchenko break; 1979f89cef09SAndrei Emeltchenko 1980f89cef09SAndrei Emeltchenko case L2CAP_MODE_STREAMING: 1981f89cef09SAndrei Emeltchenko efs.id = 1; 1982f89cef09SAndrei Emeltchenko efs.stype = L2CAP_SERV_BESTEFFORT; 1983f89cef09SAndrei Emeltchenko efs.msdu = cpu_to_le16(chan->local_msdu); 1984f89cef09SAndrei Emeltchenko efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); 1985f89cef09SAndrei Emeltchenko efs.acc_lat = 0; 1986f89cef09SAndrei Emeltchenko efs.flush_to = 0; 1987f89cef09SAndrei Emeltchenko break; 1988f89cef09SAndrei Emeltchenko 1989f89cef09SAndrei Emeltchenko default: 1990f89cef09SAndrei Emeltchenko return; 1991f89cef09SAndrei Emeltchenko } 1992f89cef09SAndrei Emeltchenko 1993f89cef09SAndrei Emeltchenko l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), 1994f89cef09SAndrei Emeltchenko (unsigned long) &efs); 1995f89cef09SAndrei Emeltchenko } 1996f89cef09SAndrei Emeltchenko 1997721c4181SGustavo F. Padovan static void l2cap_ack_timeout(struct work_struct *work) 19980a708f8fSGustavo F. Padovan { 1999721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 2000721c4181SGustavo F. Padovan ack_timer.work); 20010a708f8fSGustavo F. Padovan 20022fb9b3d4SGustavo F. Padovan BT_DBG("chan %p", chan); 20032fb9b3d4SGustavo F. Padovan 2004721c4181SGustavo F. Padovan lock_sock(chan->sk); 2005b17e73bbSSzymon Janc __l2cap_send_ack(chan); 2006721c4181SGustavo F. Padovan release_sock(chan->sk); 200709bfb2eeSSzymon Janc 200809bfb2eeSSzymon Janc l2cap_chan_put(chan); 20090a708f8fSGustavo F. Padovan } 20100a708f8fSGustavo F. Padovan 2011525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan) 20120a708f8fSGustavo F. Padovan { 201342e5c802SGustavo F. Padovan chan->expected_ack_seq = 0; 20146a026610SGustavo F. Padovan chan->unacked_frames = 0; 201542e5c802SGustavo F. Padovan chan->buffer_seq = 0; 20166a026610SGustavo F. Padovan chan->num_acked = 0; 20176a026610SGustavo F. Padovan chan->frames_sent = 0; 20180a708f8fSGustavo F. Padovan 2019721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); 2020721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); 2021721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); 20220a708f8fSGustavo F. Padovan 2023f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->srej_q); 20240a708f8fSGustavo F. Padovan 202539d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 20260a708f8fSGustavo F. Padovan } 20270a708f8fSGustavo F. Padovan 20280a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) 20290a708f8fSGustavo F. Padovan { 20300a708f8fSGustavo F. Padovan switch (mode) { 20310a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 20320a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 20330a708f8fSGustavo F. Padovan if (l2cap_mode_supported(mode, remote_feat_mask)) 20340a708f8fSGustavo F. Padovan return mode; 20350a708f8fSGustavo F. Padovan /* fall through */ 20360a708f8fSGustavo F. Padovan default: 20370a708f8fSGustavo F. Padovan return L2CAP_MODE_BASIC; 20380a708f8fSGustavo F. Padovan } 20390a708f8fSGustavo F. Padovan } 20400a708f8fSGustavo F. Padovan 20416327eb98SAndrei Emeltchenko static inline bool __l2cap_ews_supported(struct l2cap_chan *chan) 20426327eb98SAndrei Emeltchenko { 20436327eb98SAndrei Emeltchenko return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; 20446327eb98SAndrei Emeltchenko } 20456327eb98SAndrei Emeltchenko 2046f89cef09SAndrei Emeltchenko static inline bool __l2cap_efs_supported(struct l2cap_chan *chan) 2047f89cef09SAndrei Emeltchenko { 2048f89cef09SAndrei Emeltchenko return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW; 2049f89cef09SAndrei Emeltchenko } 2050f89cef09SAndrei Emeltchenko 20516327eb98SAndrei Emeltchenko static inline void l2cap_txwin_setup(struct l2cap_chan *chan) 20526327eb98SAndrei Emeltchenko { 20536327eb98SAndrei Emeltchenko if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && 2054836be934SAndrei Emeltchenko __l2cap_ews_supported(chan)) { 20556327eb98SAndrei Emeltchenko /* use extended control field */ 20566327eb98SAndrei Emeltchenko set_bit(FLAG_EXT_CTRL, &chan->flags); 2057836be934SAndrei Emeltchenko chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; 2058836be934SAndrei Emeltchenko } else { 20596327eb98SAndrei Emeltchenko chan->tx_win = min_t(u16, chan->tx_win, 20606327eb98SAndrei Emeltchenko L2CAP_DEFAULT_TX_WINDOW); 2061836be934SAndrei Emeltchenko chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; 2062836be934SAndrei Emeltchenko } 20636327eb98SAndrei Emeltchenko } 20646327eb98SAndrei Emeltchenko 2065710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) 20660a708f8fSGustavo F. Padovan { 20670a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 20680c1bc5c6SGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = chan->mode }; 20690a708f8fSGustavo F. Padovan void *ptr = req->data; 2070c8f79162SAndrei Emeltchenko u16 size; 20710a708f8fSGustavo F. Padovan 207249208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 20730a708f8fSGustavo F. Padovan 207473ffa904SGustavo F. Padovan if (chan->num_conf_req || chan->num_conf_rsp) 20750a708f8fSGustavo F. Padovan goto done; 20760a708f8fSGustavo F. Padovan 20770c1bc5c6SGustavo F. Padovan switch (chan->mode) { 20780a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 20790a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 2080c1360a1cSGustavo F. Padovan if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) 20810a708f8fSGustavo F. Padovan break; 20820a708f8fSGustavo F. Padovan 2083f89cef09SAndrei Emeltchenko if (__l2cap_efs_supported(chan)) 2084f89cef09SAndrei Emeltchenko set_bit(FLAG_EFS_ENABLE, &chan->flags); 2085f89cef09SAndrei Emeltchenko 20860a708f8fSGustavo F. Padovan /* fall through */ 20870a708f8fSGustavo F. Padovan default: 20888c1d787bSGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); 20890a708f8fSGustavo F. Padovan break; 20900a708f8fSGustavo F. Padovan } 20910a708f8fSGustavo F. Padovan 20920a708f8fSGustavo F. Padovan done: 20930c1bc5c6SGustavo F. Padovan if (chan->imtu != L2CAP_DEFAULT_MTU) 20940c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 20950a708f8fSGustavo F. Padovan 20960c1bc5c6SGustavo F. Padovan switch (chan->mode) { 20970a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 20988c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && 20998c1d787bSGustavo F. Padovan !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) 21000a708f8fSGustavo F. Padovan break; 21010a708f8fSGustavo F. Padovan 21020a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_BASIC; 21030a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 21040a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 21050a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 21060a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 21070a708f8fSGustavo F. Padovan rfc.max_pdu_size = 0; 21080a708f8fSGustavo F. Padovan 21090a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 21100a708f8fSGustavo F. Padovan (unsigned long) &rfc); 21110a708f8fSGustavo F. Padovan break; 21120a708f8fSGustavo F. Padovan 21130a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 21140a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_ERTM; 211547d1ec61SGustavo F. Padovan rfc.max_transmit = chan->max_tx; 21160a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 21170a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 2118c8f79162SAndrei Emeltchenko 2119c8f79162SAndrei Emeltchenko size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - 2120c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2121c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2122c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2123c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 21240a708f8fSGustavo F. Padovan 21256327eb98SAndrei Emeltchenko l2cap_txwin_setup(chan); 21266327eb98SAndrei Emeltchenko 21276327eb98SAndrei Emeltchenko rfc.txwin_size = min_t(u16, chan->tx_win, 21286327eb98SAndrei Emeltchenko L2CAP_DEFAULT_TX_WINDOW); 21290a708f8fSGustavo F. Padovan 21300a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 21310a708f8fSGustavo F. Padovan (unsigned long) &rfc); 21320a708f8fSGustavo F. Padovan 2133f89cef09SAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) 2134f89cef09SAndrei Emeltchenko l2cap_add_opt_efs(&ptr, chan); 2135f89cef09SAndrei Emeltchenko 21368c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 21370a708f8fSGustavo F. Padovan break; 21380a708f8fSGustavo F. Padovan 213947d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 2140c1360a1cSGustavo F. Padovan test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { 214147d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 214247d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 21430a708f8fSGustavo F. Padovan } 21446327eb98SAndrei Emeltchenko 21456327eb98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 21466327eb98SAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, 21476327eb98SAndrei Emeltchenko chan->tx_win); 21480a708f8fSGustavo F. Padovan break; 21490a708f8fSGustavo F. Padovan 21500a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 21510a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_STREAMING; 21520a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 21530a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 21540a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 21550a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 2156c8f79162SAndrei Emeltchenko 2157c8f79162SAndrei Emeltchenko size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - 2158c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2159c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2160c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2161c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 21620a708f8fSGustavo F. Padovan 21630a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 21640a708f8fSGustavo F. Padovan (unsigned long) &rfc); 21650a708f8fSGustavo F. Padovan 2166f89cef09SAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) 2167f89cef09SAndrei Emeltchenko l2cap_add_opt_efs(&ptr, chan); 2168f89cef09SAndrei Emeltchenko 21698c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 21700a708f8fSGustavo F. Padovan break; 21710a708f8fSGustavo F. Padovan 217247d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 2173c1360a1cSGustavo F. Padovan test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { 217447d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 217547d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 21760a708f8fSGustavo F. Padovan } 21770a708f8fSGustavo F. Padovan break; 21780a708f8fSGustavo F. Padovan } 21790a708f8fSGustavo F. Padovan 2180fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 21810a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0); 21820a708f8fSGustavo F. Padovan 21830a708f8fSGustavo F. Padovan return ptr - data; 21840a708f8fSGustavo F. Padovan } 21850a708f8fSGustavo F. Padovan 218673ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) 21870a708f8fSGustavo F. Padovan { 21880a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 21890a708f8fSGustavo F. Padovan void *ptr = rsp->data; 219073ffa904SGustavo F. Padovan void *req = chan->conf_req; 219173ffa904SGustavo F. Padovan int len = chan->conf_len; 21920a708f8fSGustavo F. Padovan int type, hint, olen; 21930a708f8fSGustavo F. Padovan unsigned long val; 21940a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 219542dceae2SAndrei Emeltchenko struct l2cap_conf_efs efs; 219642dceae2SAndrei Emeltchenko u8 remote_efs = 0; 21970a708f8fSGustavo F. Padovan u16 mtu = L2CAP_DEFAULT_MTU; 21980a708f8fSGustavo F. Padovan u16 result = L2CAP_CONF_SUCCESS; 2199c8f79162SAndrei Emeltchenko u16 size; 22000a708f8fSGustavo F. Padovan 220173ffa904SGustavo F. Padovan BT_DBG("chan %p", chan); 22020a708f8fSGustavo F. Padovan 22030a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 22040a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&req, &type, &olen, &val); 22050a708f8fSGustavo F. Padovan 22060a708f8fSGustavo F. Padovan hint = type & L2CAP_CONF_HINT; 22070a708f8fSGustavo F. Padovan type &= L2CAP_CONF_MASK; 22080a708f8fSGustavo F. Padovan 22090a708f8fSGustavo F. Padovan switch (type) { 22100a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 22110a708f8fSGustavo F. Padovan mtu = val; 22120a708f8fSGustavo F. Padovan break; 22130a708f8fSGustavo F. Padovan 22140a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 22150c1bc5c6SGustavo F. Padovan chan->flush_to = val; 22160a708f8fSGustavo F. Padovan break; 22170a708f8fSGustavo F. Padovan 22180a708f8fSGustavo F. Padovan case L2CAP_CONF_QOS: 22190a708f8fSGustavo F. Padovan break; 22200a708f8fSGustavo F. Padovan 22210a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 22220a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 22230a708f8fSGustavo F. Padovan memcpy(&rfc, (void *) val, olen); 22240a708f8fSGustavo F. Padovan break; 22250a708f8fSGustavo F. Padovan 22260a708f8fSGustavo F. Padovan case L2CAP_CONF_FCS: 22270a708f8fSGustavo F. Padovan if (val == L2CAP_FCS_NONE) 2228c1360a1cSGustavo F. Padovan set_bit(CONF_NO_FCS_RECV, &chan->conf_state); 222942dceae2SAndrei Emeltchenko break; 22300a708f8fSGustavo F. Padovan 223142dceae2SAndrei Emeltchenko case L2CAP_CONF_EFS: 223242dceae2SAndrei Emeltchenko remote_efs = 1; 223342dceae2SAndrei Emeltchenko if (olen == sizeof(efs)) 223442dceae2SAndrei Emeltchenko memcpy(&efs, (void *) val, olen); 22350a708f8fSGustavo F. Padovan break; 22360a708f8fSGustavo F. Padovan 22376327eb98SAndrei Emeltchenko case L2CAP_CONF_EWS: 22386327eb98SAndrei Emeltchenko if (!enable_hs) 22396327eb98SAndrei Emeltchenko return -ECONNREFUSED; 22406327eb98SAndrei Emeltchenko 22416327eb98SAndrei Emeltchenko set_bit(FLAG_EXT_CTRL, &chan->flags); 22426327eb98SAndrei Emeltchenko set_bit(CONF_EWS_RECV, &chan->conf_state); 2243836be934SAndrei Emeltchenko chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; 22446327eb98SAndrei Emeltchenko chan->remote_tx_win = val; 22450a708f8fSGustavo F. Padovan break; 22460a708f8fSGustavo F. Padovan 22470a708f8fSGustavo F. Padovan default: 22480a708f8fSGustavo F. Padovan if (hint) 22490a708f8fSGustavo F. Padovan break; 22500a708f8fSGustavo F. Padovan 22510a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNKNOWN; 22520a708f8fSGustavo F. Padovan *((u8 *) ptr++) = type; 22530a708f8fSGustavo F. Padovan break; 22540a708f8fSGustavo F. Padovan } 22550a708f8fSGustavo F. Padovan } 22560a708f8fSGustavo F. Padovan 225773ffa904SGustavo F. Padovan if (chan->num_conf_rsp || chan->num_conf_req > 1) 22580a708f8fSGustavo F. Padovan goto done; 22590a708f8fSGustavo F. Padovan 22600c1bc5c6SGustavo F. Padovan switch (chan->mode) { 22610a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 22620a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 2263c1360a1cSGustavo F. Padovan if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { 22640c1bc5c6SGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, 22658c1d787bSGustavo F. Padovan chan->conn->feat_mask); 22660a708f8fSGustavo F. Padovan break; 22670a708f8fSGustavo F. Padovan } 22680a708f8fSGustavo F. Padovan 226942dceae2SAndrei Emeltchenko if (remote_efs) { 227042dceae2SAndrei Emeltchenko if (__l2cap_efs_supported(chan)) 227142dceae2SAndrei Emeltchenko set_bit(FLAG_EFS_ENABLE, &chan->flags); 227242dceae2SAndrei Emeltchenko else 227342dceae2SAndrei Emeltchenko return -ECONNREFUSED; 227442dceae2SAndrei Emeltchenko } 227542dceae2SAndrei Emeltchenko 22760c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) 22770a708f8fSGustavo F. Padovan return -ECONNREFUSED; 22780a708f8fSGustavo F. Padovan 22790a708f8fSGustavo F. Padovan break; 22800a708f8fSGustavo F. Padovan } 22810a708f8fSGustavo F. Padovan 22820a708f8fSGustavo F. Padovan done: 22830c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) { 22840a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 22850c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 22860a708f8fSGustavo F. Padovan 228773ffa904SGustavo F. Padovan if (chan->num_conf_rsp == 1) 22880a708f8fSGustavo F. Padovan return -ECONNREFUSED; 22890a708f8fSGustavo F. Padovan 22900a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 22910a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 22920a708f8fSGustavo F. Padovan } 22930a708f8fSGustavo F. Padovan 22940a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) { 22950a708f8fSGustavo F. Padovan /* Configure output options and let the other side know 22960a708f8fSGustavo F. Padovan * which ones we don't like. */ 22970a708f8fSGustavo F. Padovan 22980a708f8fSGustavo F. Padovan if (mtu < L2CAP_DEFAULT_MIN_MTU) 22990a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 23000a708f8fSGustavo F. Padovan else { 23010c1bc5c6SGustavo F. Padovan chan->omtu = mtu; 2302c1360a1cSGustavo F. Padovan set_bit(CONF_MTU_DONE, &chan->conf_state); 23030a708f8fSGustavo F. Padovan } 23040c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); 23050a708f8fSGustavo F. Padovan 230642dceae2SAndrei Emeltchenko if (remote_efs) { 230742dceae2SAndrei Emeltchenko if (chan->local_stype != L2CAP_SERV_NOTRAFIC && 230842dceae2SAndrei Emeltchenko efs.stype != L2CAP_SERV_NOTRAFIC && 230942dceae2SAndrei Emeltchenko efs.stype != chan->local_stype) { 231042dceae2SAndrei Emeltchenko 231142dceae2SAndrei Emeltchenko result = L2CAP_CONF_UNACCEPT; 231242dceae2SAndrei Emeltchenko 231342dceae2SAndrei Emeltchenko if (chan->num_conf_req >= 1) 231442dceae2SAndrei Emeltchenko return -ECONNREFUSED; 231542dceae2SAndrei Emeltchenko 231642dceae2SAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, 231742dceae2SAndrei Emeltchenko sizeof(efs), 231842dceae2SAndrei Emeltchenko (unsigned long) &efs); 23190e8b207eSAndrei Emeltchenko } else { 23203e6b3b95SGustavo F. Padovan /* Send PENDING Conf Rsp */ 23210e8b207eSAndrei Emeltchenko result = L2CAP_CONF_PENDING; 23220e8b207eSAndrei Emeltchenko set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 232342dceae2SAndrei Emeltchenko } 232442dceae2SAndrei Emeltchenko } 232542dceae2SAndrei Emeltchenko 23260a708f8fSGustavo F. Padovan switch (rfc.mode) { 23270a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 232847d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2329c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 23300a708f8fSGustavo F. Padovan break; 23310a708f8fSGustavo F. Padovan 23320a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 23336327eb98SAndrei Emeltchenko if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) 23342c03a7a4SGustavo F. Padovan chan->remote_tx_win = rfc.txwin_size; 23356327eb98SAndrei Emeltchenko else 23366327eb98SAndrei Emeltchenko rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; 23376327eb98SAndrei Emeltchenko 23382c03a7a4SGustavo F. Padovan chan->remote_max_tx = rfc.max_transmit; 23390a708f8fSGustavo F. Padovan 2340c8f79162SAndrei Emeltchenko size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), 2341c8f79162SAndrei Emeltchenko chan->conn->mtu - 2342c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2343c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2344c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2345c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 2346c8f79162SAndrei Emeltchenko chan->remote_mps = size; 23470a708f8fSGustavo F. Padovan 23480a708f8fSGustavo F. Padovan rfc.retrans_timeout = 23490a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO); 23500a708f8fSGustavo F. Padovan rfc.monitor_timeout = 23510a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO); 23520a708f8fSGustavo F. Padovan 2353c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 23540a708f8fSGustavo F. Padovan 23550a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 23560a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 23570a708f8fSGustavo F. Padovan 235842dceae2SAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { 235942dceae2SAndrei Emeltchenko chan->remote_id = efs.id; 236042dceae2SAndrei Emeltchenko chan->remote_stype = efs.stype; 236142dceae2SAndrei Emeltchenko chan->remote_msdu = le16_to_cpu(efs.msdu); 236242dceae2SAndrei Emeltchenko chan->remote_flush_to = 236342dceae2SAndrei Emeltchenko le32_to_cpu(efs.flush_to); 236442dceae2SAndrei Emeltchenko chan->remote_acc_lat = 236542dceae2SAndrei Emeltchenko le32_to_cpu(efs.acc_lat); 236642dceae2SAndrei Emeltchenko chan->remote_sdu_itime = 236742dceae2SAndrei Emeltchenko le32_to_cpu(efs.sdu_itime); 236842dceae2SAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, 236942dceae2SAndrei Emeltchenko sizeof(efs), (unsigned long) &efs); 237042dceae2SAndrei Emeltchenko } 23710a708f8fSGustavo F. Padovan break; 23720a708f8fSGustavo F. Padovan 23730a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 2374c8f79162SAndrei Emeltchenko size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), 2375c8f79162SAndrei Emeltchenko chan->conn->mtu - 2376c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2377c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2378c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2379c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 2380c8f79162SAndrei Emeltchenko chan->remote_mps = size; 23810a708f8fSGustavo F. Padovan 2382c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 23830a708f8fSGustavo F. Padovan 23840a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 23850a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 23860a708f8fSGustavo F. Padovan 23870a708f8fSGustavo F. Padovan break; 23880a708f8fSGustavo F. Padovan 23890a708f8fSGustavo F. Padovan default: 23900a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 23910a708f8fSGustavo F. Padovan 23920a708f8fSGustavo F. Padovan memset(&rfc, 0, sizeof(rfc)); 23930c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 23940a708f8fSGustavo F. Padovan } 23950a708f8fSGustavo F. Padovan 23960a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) 2397c1360a1cSGustavo F. Padovan set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 23980a708f8fSGustavo F. Padovan } 2399fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 24000a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 24010a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(0x0000); 24020a708f8fSGustavo F. Padovan 24030a708f8fSGustavo F. Padovan return ptr - data; 24040a708f8fSGustavo F. Padovan } 24050a708f8fSGustavo F. Padovan 2406b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result) 24070a708f8fSGustavo F. Padovan { 24080a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 24090a708f8fSGustavo F. Padovan void *ptr = req->data; 24100a708f8fSGustavo F. Padovan int type, olen; 24110a708f8fSGustavo F. Padovan unsigned long val; 241236e999a8SMat Martineau struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 241366af7aafSAndrei Emeltchenko struct l2cap_conf_efs efs; 24140a708f8fSGustavo F. Padovan 2415fe4128e0SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data); 24160a708f8fSGustavo F. Padovan 24170a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 24180a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 24190a708f8fSGustavo F. Padovan 24200a708f8fSGustavo F. Padovan switch (type) { 24210a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 24220a708f8fSGustavo F. Padovan if (val < L2CAP_DEFAULT_MIN_MTU) { 24230a708f8fSGustavo F. Padovan *result = L2CAP_CONF_UNACCEPT; 24240c1bc5c6SGustavo F. Padovan chan->imtu = L2CAP_DEFAULT_MIN_MTU; 24250a708f8fSGustavo F. Padovan } else 24260c1bc5c6SGustavo F. Padovan chan->imtu = val; 24270c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 24280a708f8fSGustavo F. Padovan break; 24290a708f8fSGustavo F. Padovan 24300a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 24310c1bc5c6SGustavo F. Padovan chan->flush_to = val; 24320a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 24330c1bc5c6SGustavo F. Padovan 2, chan->flush_to); 24340a708f8fSGustavo F. Padovan break; 24350a708f8fSGustavo F. Padovan 24360a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 24370a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 24380a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 24390a708f8fSGustavo F. Padovan 2440c1360a1cSGustavo F. Padovan if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && 24410c1bc5c6SGustavo F. Padovan rfc.mode != chan->mode) 24420a708f8fSGustavo F. Padovan return -ECONNREFUSED; 24430a708f8fSGustavo F. Padovan 244447d1ec61SGustavo F. Padovan chan->fcs = 0; 24450a708f8fSGustavo F. Padovan 24460a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 24470a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 24480a708f8fSGustavo F. Padovan break; 24496327eb98SAndrei Emeltchenko 24506327eb98SAndrei Emeltchenko case L2CAP_CONF_EWS: 24516327eb98SAndrei Emeltchenko chan->tx_win = min_t(u16, val, 24526327eb98SAndrei Emeltchenko L2CAP_DEFAULT_EXT_WINDOW); 24533e6b3b95SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, 24543e6b3b95SGustavo F. Padovan chan->tx_win); 24556327eb98SAndrei Emeltchenko break; 245666af7aafSAndrei Emeltchenko 245766af7aafSAndrei Emeltchenko case L2CAP_CONF_EFS: 245866af7aafSAndrei Emeltchenko if (olen == sizeof(efs)) 245966af7aafSAndrei Emeltchenko memcpy(&efs, (void *)val, olen); 246066af7aafSAndrei Emeltchenko 246166af7aafSAndrei Emeltchenko if (chan->local_stype != L2CAP_SERV_NOTRAFIC && 246266af7aafSAndrei Emeltchenko efs.stype != L2CAP_SERV_NOTRAFIC && 246366af7aafSAndrei Emeltchenko efs.stype != chan->local_stype) 246466af7aafSAndrei Emeltchenko return -ECONNREFUSED; 246566af7aafSAndrei Emeltchenko 246666af7aafSAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, 246766af7aafSAndrei Emeltchenko sizeof(efs), (unsigned long) &efs); 246866af7aafSAndrei Emeltchenko break; 24690a708f8fSGustavo F. Padovan } 24700a708f8fSGustavo F. Padovan } 24710a708f8fSGustavo F. Padovan 24720c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) 24730a708f8fSGustavo F. Padovan return -ECONNREFUSED; 24740a708f8fSGustavo F. Padovan 24750c1bc5c6SGustavo F. Padovan chan->mode = rfc.mode; 24760a708f8fSGustavo F. Padovan 24770e8b207eSAndrei Emeltchenko if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { 24780a708f8fSGustavo F. Padovan switch (rfc.mode) { 24790a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 248047d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 248147d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 248247d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 248366af7aafSAndrei Emeltchenko 248466af7aafSAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { 248566af7aafSAndrei Emeltchenko chan->local_msdu = le16_to_cpu(efs.msdu); 248666af7aafSAndrei Emeltchenko chan->local_sdu_itime = 248766af7aafSAndrei Emeltchenko le32_to_cpu(efs.sdu_itime); 248866af7aafSAndrei Emeltchenko chan->local_acc_lat = le32_to_cpu(efs.acc_lat); 248966af7aafSAndrei Emeltchenko chan->local_flush_to = 249066af7aafSAndrei Emeltchenko le32_to_cpu(efs.flush_to); 249166af7aafSAndrei Emeltchenko } 24920a708f8fSGustavo F. Padovan break; 249366af7aafSAndrei Emeltchenko 24940a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 249547d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 24960a708f8fSGustavo F. Padovan } 24970a708f8fSGustavo F. Padovan } 24980a708f8fSGustavo F. Padovan 2499fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 25000a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0x0000); 25010a708f8fSGustavo F. Padovan 25020a708f8fSGustavo F. Padovan return ptr - data; 25030a708f8fSGustavo F. Padovan } 25040a708f8fSGustavo F. Padovan 2505fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags) 25060a708f8fSGustavo F. Padovan { 25070a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 25080a708f8fSGustavo F. Padovan void *ptr = rsp->data; 25090a708f8fSGustavo F. Padovan 2510fe4128e0SGustavo F. Padovan BT_DBG("chan %p", chan); 25110a708f8fSGustavo F. Padovan 2512fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 25130a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 25140a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(flags); 25150a708f8fSGustavo F. Padovan 25160a708f8fSGustavo F. Padovan return ptr - data; 25170a708f8fSGustavo F. Padovan } 25180a708f8fSGustavo F. Padovan 25198c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) 2520710f9b0aSGustavo F. Padovan { 2521710f9b0aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 25228c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 2523710f9b0aSGustavo F. Padovan u8 buf[128]; 2524710f9b0aSGustavo F. Padovan 2525fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 2526fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 2527710f9b0aSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 2528710f9b0aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 2529710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 2530710f9b0aSGustavo F. Padovan L2CAP_CONN_RSP, sizeof(rsp), &rsp); 2531710f9b0aSGustavo F. Padovan 2532c1360a1cSGustavo F. Padovan if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) 2533710f9b0aSGustavo F. Padovan return; 2534710f9b0aSGustavo F. Padovan 2535710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 2536710f9b0aSGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 2537710f9b0aSGustavo F. Padovan chan->num_conf_req++; 2538710f9b0aSGustavo F. Padovan } 2539710f9b0aSGustavo F. Padovan 254047d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len) 25410a708f8fSGustavo F. Padovan { 25420a708f8fSGustavo F. Padovan int type, olen; 25430a708f8fSGustavo F. Padovan unsigned long val; 25440a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 25450a708f8fSGustavo F. Padovan 254647d1ec61SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); 25470a708f8fSGustavo F. Padovan 25480c1bc5c6SGustavo F. Padovan if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) 25490a708f8fSGustavo F. Padovan return; 25500a708f8fSGustavo F. Padovan 25510a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 25520a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 25530a708f8fSGustavo F. Padovan 25540a708f8fSGustavo F. Padovan switch (type) { 25550a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 25560a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 25570a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 25580a708f8fSGustavo F. Padovan goto done; 25590a708f8fSGustavo F. Padovan } 25600a708f8fSGustavo F. Padovan } 25610a708f8fSGustavo F. Padovan 256236e999a8SMat Martineau /* Use sane default values in case a misbehaving remote device 256336e999a8SMat Martineau * did not send an RFC option. 256436e999a8SMat Martineau */ 256536e999a8SMat Martineau rfc.mode = chan->mode; 256636e999a8SMat Martineau rfc.retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); 256736e999a8SMat Martineau rfc.monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); 256836e999a8SMat Martineau rfc.max_pdu_size = cpu_to_le16(chan->imtu); 256936e999a8SMat Martineau 257036e999a8SMat Martineau BT_ERR("Expected RFC option was not found, using defaults"); 257136e999a8SMat Martineau 25720a708f8fSGustavo F. Padovan done: 25730a708f8fSGustavo F. Padovan switch (rfc.mode) { 25740a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 257547d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 257647d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 257747d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 25780a708f8fSGustavo F. Padovan break; 25790a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 258047d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 25810a708f8fSGustavo F. Padovan } 25820a708f8fSGustavo F. Padovan } 25830a708f8fSGustavo F. Padovan 25840a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 25850a708f8fSGustavo F. Padovan { 2586e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; 25870a708f8fSGustavo F. Padovan 2588e2fd318eSIlia Kolomisnky if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) 25890a708f8fSGustavo F. Padovan return 0; 25900a708f8fSGustavo F. Padovan 25910a708f8fSGustavo F. Padovan if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && 25920a708f8fSGustavo F. Padovan cmd->ident == conn->info_ident) { 259317cd3f37SUlisses Furquim cancel_delayed_work(&conn->info_timer); 25940a708f8fSGustavo F. Padovan 25950a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 25960a708f8fSGustavo F. Padovan conn->info_ident = 0; 25970a708f8fSGustavo F. Padovan 25980a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 25990a708f8fSGustavo F. Padovan } 26000a708f8fSGustavo F. Padovan 26010a708f8fSGustavo F. Padovan return 0; 26020a708f8fSGustavo F. Padovan } 26030a708f8fSGustavo F. Padovan 26040a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 26050a708f8fSGustavo F. Padovan { 26060a708f8fSGustavo F. Padovan struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; 26070a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 260823691d75SGustavo F. Padovan struct l2cap_chan *chan = NULL, *pchan; 26090a708f8fSGustavo F. Padovan struct sock *parent, *sk = NULL; 26100a708f8fSGustavo F. Padovan int result, status = L2CAP_CS_NO_INFO; 26110a708f8fSGustavo F. Padovan 26120a708f8fSGustavo F. Padovan u16 dcid = 0, scid = __le16_to_cpu(req->scid); 26130a708f8fSGustavo F. Padovan __le16 psm = req->psm; 26140a708f8fSGustavo F. Padovan 26150a708f8fSGustavo F. Padovan BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid); 26160a708f8fSGustavo F. Padovan 26170a708f8fSGustavo F. Padovan /* Check if we have socket listening on psm */ 261823691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src); 261923691d75SGustavo F. Padovan if (!pchan) { 26200a708f8fSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 26210a708f8fSGustavo F. Padovan goto sendresp; 26220a708f8fSGustavo F. Padovan } 26230a708f8fSGustavo F. Padovan 262423691d75SGustavo F. Padovan parent = pchan->sk; 262523691d75SGustavo F. Padovan 26263df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 2627aa2ac881SGustavo F. Padovan lock_sock(parent); 26280a708f8fSGustavo F. Padovan 26290a708f8fSGustavo F. Padovan /* Check if the ACL is secure enough (if not SDP) */ 26300a708f8fSGustavo F. Padovan if (psm != cpu_to_le16(0x0001) && 26310a708f8fSGustavo F. Padovan !hci_conn_check_link_mode(conn->hcon)) { 26329f5a0d7bSAndrei Emeltchenko conn->disc_reason = HCI_ERROR_AUTH_FAILURE; 26330a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 26340a708f8fSGustavo F. Padovan goto response; 26350a708f8fSGustavo F. Padovan } 26360a708f8fSGustavo F. Padovan 26370a708f8fSGustavo F. Padovan result = L2CAP_CR_NO_MEM; 26380a708f8fSGustavo F. Padovan 26390a708f8fSGustavo F. Padovan /* Check for backlog size */ 26400a708f8fSGustavo F. Padovan if (sk_acceptq_is_full(parent)) { 26410a708f8fSGustavo F. Padovan BT_DBG("backlog full %d", parent->sk_ack_backlog); 26420a708f8fSGustavo F. Padovan goto response; 26430a708f8fSGustavo F. Padovan } 26440a708f8fSGustavo F. Padovan 264580808e43SGustavo F. Padovan chan = pchan->ops->new_connection(pchan->data); 264680808e43SGustavo F. Padovan if (!chan) 26470a708f8fSGustavo F. Padovan goto response; 26480a708f8fSGustavo F. Padovan 264980808e43SGustavo F. Padovan sk = chan->sk; 265080808e43SGustavo F. Padovan 26510a708f8fSGustavo F. Padovan /* Check if we already have channel with that dcid */ 2652baa7e1faSGustavo F. Padovan if (__l2cap_get_chan_by_dcid(conn, scid)) { 26530a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 2654ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 26550a708f8fSGustavo F. Padovan goto response; 26560a708f8fSGustavo F. Padovan } 26570a708f8fSGustavo F. Padovan 26580a708f8fSGustavo F. Padovan hci_conn_hold(conn->hcon); 26590a708f8fSGustavo F. Padovan 26600a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->src, conn->src); 26610a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->dst, conn->dst); 2662fe4128e0SGustavo F. Padovan chan->psm = psm; 2663fe4128e0SGustavo F. Padovan chan->dcid = scid; 26640a708f8fSGustavo F. Padovan 2665d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 2666d1010240SGustavo F. Padovan 26673d57dc68SGustavo F. Padovan l2cap_chan_add(conn, chan); 266848454079SGustavo F. Padovan 2669fe4128e0SGustavo F. Padovan dcid = chan->scid; 26700a708f8fSGustavo F. Padovan 2671c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 26720a708f8fSGustavo F. Padovan 2673fc7f8a7eSGustavo F. Padovan chan->ident = cmd->ident; 26740a708f8fSGustavo F. Padovan 26750a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { 2676d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan)) { 26770a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 26780e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECT2); 26790a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 26800a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHOR_PEND; 26810a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 26820a708f8fSGustavo F. Padovan } else { 26830e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONFIG); 26840a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 26850a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 26860a708f8fSGustavo F. Padovan } 26870a708f8fSGustavo F. Padovan } else { 26880e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECT2); 26890a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 26900a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHEN_PEND; 26910a708f8fSGustavo F. Padovan } 26920a708f8fSGustavo F. Padovan } else { 26930e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECT2); 26940a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 26950a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 26960a708f8fSGustavo F. Padovan } 26970a708f8fSGustavo F. Padovan 26980a708f8fSGustavo F. Padovan response: 2699aa2ac881SGustavo F. Padovan release_sock(parent); 27003df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 27010a708f8fSGustavo F. Padovan 27020a708f8fSGustavo F. Padovan sendresp: 27030a708f8fSGustavo F. Padovan rsp.scid = cpu_to_le16(scid); 27040a708f8fSGustavo F. Padovan rsp.dcid = cpu_to_le16(dcid); 27050a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 27060a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(status); 27070a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); 27080a708f8fSGustavo F. Padovan 27090a708f8fSGustavo F. Padovan if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { 27100a708f8fSGustavo F. Padovan struct l2cap_info_req info; 27110a708f8fSGustavo F. Padovan info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 27120a708f8fSGustavo F. Padovan 27130a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 27140a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 27150a708f8fSGustavo F. Padovan 2716030013d8SGustavo F. Padovan schedule_delayed_work(&conn->info_timer, 27170a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 27180a708f8fSGustavo F. Padovan 27190a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 27200a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(info), &info); 27210a708f8fSGustavo F. Padovan } 27220a708f8fSGustavo F. Padovan 2723c1360a1cSGustavo F. Padovan if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && 27240a708f8fSGustavo F. Padovan result == L2CAP_CR_SUCCESS) { 27250a708f8fSGustavo F. Padovan u8 buf[128]; 2726c1360a1cSGustavo F. Padovan set_bit(CONF_REQ_SENT, &chan->conf_state); 27270a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 272873ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 272973ffa904SGustavo F. Padovan chan->num_conf_req++; 27300a708f8fSGustavo F. Padovan } 27310a708f8fSGustavo F. Padovan 27320a708f8fSGustavo F. Padovan return 0; 27330a708f8fSGustavo F. Padovan } 27340a708f8fSGustavo F. Padovan 27350a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 27360a708f8fSGustavo F. Padovan { 27370a708f8fSGustavo F. Padovan struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; 27380a708f8fSGustavo F. Padovan u16 scid, dcid, result, status; 273948454079SGustavo F. Padovan struct l2cap_chan *chan; 27400a708f8fSGustavo F. Padovan struct sock *sk; 27410a708f8fSGustavo F. Padovan u8 req[128]; 27423df91ea2SAndrei Emeltchenko int err; 27430a708f8fSGustavo F. Padovan 27440a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 27450a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 27460a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 27470a708f8fSGustavo F. Padovan status = __le16_to_cpu(rsp->status); 27480a708f8fSGustavo F. Padovan 27491b009c98SAndrei Emeltchenko BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", 27501b009c98SAndrei Emeltchenko dcid, scid, result, status); 27510a708f8fSGustavo F. Padovan 27523df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 27533df91ea2SAndrei Emeltchenko 27540a708f8fSGustavo F. Padovan if (scid) { 27553df91ea2SAndrei Emeltchenko chan = __l2cap_get_chan_by_scid(conn, scid); 27563df91ea2SAndrei Emeltchenko if (!chan) { 27573df91ea2SAndrei Emeltchenko err = -EFAULT; 27583df91ea2SAndrei Emeltchenko goto unlock; 27593df91ea2SAndrei Emeltchenko } 27600a708f8fSGustavo F. Padovan } else { 27613df91ea2SAndrei Emeltchenko chan = __l2cap_get_chan_by_ident(conn, cmd->ident); 27623df91ea2SAndrei Emeltchenko if (!chan) { 27633df91ea2SAndrei Emeltchenko err = -EFAULT; 27643df91ea2SAndrei Emeltchenko goto unlock; 27653df91ea2SAndrei Emeltchenko } 27660a708f8fSGustavo F. Padovan } 27670a708f8fSGustavo F. Padovan 27683df91ea2SAndrei Emeltchenko err = 0; 27693df91ea2SAndrei Emeltchenko 277048454079SGustavo F. Padovan sk = chan->sk; 27713df91ea2SAndrei Emeltchenko lock_sock(sk); 277248454079SGustavo F. Padovan 27730a708f8fSGustavo F. Padovan switch (result) { 27740a708f8fSGustavo F. Padovan case L2CAP_CR_SUCCESS: 277589bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONFIG); 2776fc7f8a7eSGustavo F. Padovan chan->ident = 0; 2777fe4128e0SGustavo F. Padovan chan->dcid = dcid; 2778c1360a1cSGustavo F. Padovan clear_bit(CONF_CONNECT_PEND, &chan->conf_state); 27790a708f8fSGustavo F. Padovan 2780c1360a1cSGustavo F. Padovan if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) 27810a708f8fSGustavo F. Padovan break; 27820a708f8fSGustavo F. Padovan 27830a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 278473ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, req), req); 278573ffa904SGustavo F. Padovan chan->num_conf_req++; 27860a708f8fSGustavo F. Padovan break; 27870a708f8fSGustavo F. Padovan 27880a708f8fSGustavo F. Padovan case L2CAP_CR_PEND: 2789c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 27900a708f8fSGustavo F. Padovan break; 27910a708f8fSGustavo F. Padovan 27920a708f8fSGustavo F. Padovan default: 279348454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNREFUSED); 27940a708f8fSGustavo F. Padovan break; 27950a708f8fSGustavo F. Padovan } 27960a708f8fSGustavo F. Padovan 2797aa2ac881SGustavo F. Padovan release_sock(sk); 27983df91ea2SAndrei Emeltchenko 27993df91ea2SAndrei Emeltchenko unlock: 28003df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 28013df91ea2SAndrei Emeltchenko 28023df91ea2SAndrei Emeltchenko return err; 28030a708f8fSGustavo F. Padovan } 28040a708f8fSGustavo F. Padovan 280547d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan) 28060a708f8fSGustavo F. Padovan { 28070a708f8fSGustavo F. Padovan /* FCS is enabled only in ERTM or streaming mode, if one or both 28080a708f8fSGustavo F. Padovan * sides request it. 28090a708f8fSGustavo F. Padovan */ 28100c1bc5c6SGustavo F. Padovan if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) 281147d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2812c1360a1cSGustavo F. Padovan else if (!test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) 281347d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_CRC16; 28140a708f8fSGustavo F. Padovan } 28150a708f8fSGustavo F. Padovan 28160a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 28170a708f8fSGustavo F. Padovan { 28180a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; 28190a708f8fSGustavo F. Padovan u16 dcid, flags; 28200a708f8fSGustavo F. Padovan u8 rsp[64]; 282148454079SGustavo F. Padovan struct l2cap_chan *chan; 28220a708f8fSGustavo F. Padovan struct sock *sk; 28230a708f8fSGustavo F. Padovan int len; 28240a708f8fSGustavo F. Padovan 28250a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 28260a708f8fSGustavo F. Padovan flags = __le16_to_cpu(req->flags); 28270a708f8fSGustavo F. Padovan 28280a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); 28290a708f8fSGustavo F. Padovan 2830baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 283148454079SGustavo F. Padovan if (!chan) 28320a708f8fSGustavo F. Padovan return -ENOENT; 28330a708f8fSGustavo F. Padovan 283448454079SGustavo F. Padovan sk = chan->sk; 28353df91ea2SAndrei Emeltchenko lock_sock(sk); 283648454079SGustavo F. Padovan 2837033b1142SDavid S. Miller if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { 2838e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_cid rej; 28390a708f8fSGustavo F. Padovan 2840e2fd318eSIlia Kolomisnky rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID); 2841e2fd318eSIlia Kolomisnky rej.scid = cpu_to_le16(chan->scid); 2842e2fd318eSIlia Kolomisnky rej.dcid = cpu_to_le16(chan->dcid); 2843e2fd318eSIlia Kolomisnky 28440a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, 28450a708f8fSGustavo F. Padovan sizeof(rej), &rej); 28460a708f8fSGustavo F. Padovan goto unlock; 28470a708f8fSGustavo F. Padovan } 28480a708f8fSGustavo F. Padovan 28490a708f8fSGustavo F. Padovan /* Reject if config buffer is too small. */ 28500a708f8fSGustavo F. Padovan len = cmd_len - sizeof(*req); 28517ac28817SDan Rosenberg if (len < 0 || chan->conf_len + len > sizeof(chan->conf_req)) { 28520a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2853fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 28540a708f8fSGustavo F. Padovan L2CAP_CONF_REJECT, flags), rsp); 28550a708f8fSGustavo F. Padovan goto unlock; 28560a708f8fSGustavo F. Padovan } 28570a708f8fSGustavo F. Padovan 28580a708f8fSGustavo F. Padovan /* Store config. */ 285973ffa904SGustavo F. Padovan memcpy(chan->conf_req + chan->conf_len, req->data, len); 286073ffa904SGustavo F. Padovan chan->conf_len += len; 28610a708f8fSGustavo F. Padovan 28620a708f8fSGustavo F. Padovan if (flags & 0x0001) { 28630a708f8fSGustavo F. Padovan /* Incomplete config. Send empty response. */ 28640a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2865fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 28660a708f8fSGustavo F. Padovan L2CAP_CONF_SUCCESS, 0x0001), rsp); 28670a708f8fSGustavo F. Padovan goto unlock; 28680a708f8fSGustavo F. Padovan } 28690a708f8fSGustavo F. Padovan 28700a708f8fSGustavo F. Padovan /* Complete config. */ 287173ffa904SGustavo F. Padovan len = l2cap_parse_conf_req(chan, rsp); 28720a708f8fSGustavo F. Padovan if (len < 0) { 2873e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 28740a708f8fSGustavo F. Padovan goto unlock; 28750a708f8fSGustavo F. Padovan } 28760a708f8fSGustavo F. Padovan 28770a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); 287873ffa904SGustavo F. Padovan chan->num_conf_rsp++; 28790a708f8fSGustavo F. Padovan 28800a708f8fSGustavo F. Padovan /* Reset config buffer. */ 288173ffa904SGustavo F. Padovan chan->conf_len = 0; 28820a708f8fSGustavo F. Padovan 2883c1360a1cSGustavo F. Padovan if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) 28840a708f8fSGustavo F. Padovan goto unlock; 28850a708f8fSGustavo F. Padovan 2886c1360a1cSGustavo F. Padovan if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { 288747d1ec61SGustavo F. Padovan set_default_fcs(chan); 28880a708f8fSGustavo F. Padovan 288989bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 28900a708f8fSGustavo F. Padovan 289142e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 289242e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 289358d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 28940c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2895525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 28960a708f8fSGustavo F. Padovan 2897cf4cd009SAndrei Emeltchenko l2cap_chan_ready(chan); 28980a708f8fSGustavo F. Padovan goto unlock; 28990a708f8fSGustavo F. Padovan } 29000a708f8fSGustavo F. Padovan 2901c1360a1cSGustavo F. Padovan if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { 29020a708f8fSGustavo F. Padovan u8 buf[64]; 29030a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 290473ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 290573ffa904SGustavo F. Padovan chan->num_conf_req++; 29060a708f8fSGustavo F. Padovan } 29070a708f8fSGustavo F. Padovan 29080e8b207eSAndrei Emeltchenko /* Got Conf Rsp PENDING from remote side and asume we sent 29090e8b207eSAndrei Emeltchenko Conf Rsp PENDING in the code above */ 29100e8b207eSAndrei Emeltchenko if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && 29110e8b207eSAndrei Emeltchenko test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { 29120e8b207eSAndrei Emeltchenko 29130e8b207eSAndrei Emeltchenko /* check compatibility */ 29140e8b207eSAndrei Emeltchenko 29150e8b207eSAndrei Emeltchenko clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 29160e8b207eSAndrei Emeltchenko set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 29170e8b207eSAndrei Emeltchenko 29180e8b207eSAndrei Emeltchenko l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 29190e8b207eSAndrei Emeltchenko l2cap_build_conf_rsp(chan, rsp, 29200e8b207eSAndrei Emeltchenko L2CAP_CONF_SUCCESS, 0x0000), rsp); 29210e8b207eSAndrei Emeltchenko } 29220e8b207eSAndrei Emeltchenko 29230a708f8fSGustavo F. Padovan unlock: 2924aa2ac881SGustavo F. Padovan release_sock(sk); 29250a708f8fSGustavo F. Padovan return 0; 29260a708f8fSGustavo F. Padovan } 29270a708f8fSGustavo F. Padovan 29280a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 29290a708f8fSGustavo F. Padovan { 29300a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; 29310a708f8fSGustavo F. Padovan u16 scid, flags, result; 293248454079SGustavo F. Padovan struct l2cap_chan *chan; 29330a708f8fSGustavo F. Padovan struct sock *sk; 29340a708f8fSGustavo F. Padovan int len = cmd->len - sizeof(*rsp); 29350a708f8fSGustavo F. Padovan 29360a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 29370a708f8fSGustavo F. Padovan flags = __le16_to_cpu(rsp->flags); 29380a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 29390a708f8fSGustavo F. Padovan 29400a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", 29410a708f8fSGustavo F. Padovan scid, flags, result); 29420a708f8fSGustavo F. Padovan 2943baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 294448454079SGustavo F. Padovan if (!chan) 29450a708f8fSGustavo F. Padovan return 0; 29460a708f8fSGustavo F. Padovan 294748454079SGustavo F. Padovan sk = chan->sk; 29483df91ea2SAndrei Emeltchenko lock_sock(sk); 294948454079SGustavo F. Padovan 29500a708f8fSGustavo F. Padovan switch (result) { 29510a708f8fSGustavo F. Padovan case L2CAP_CONF_SUCCESS: 295247d1ec61SGustavo F. Padovan l2cap_conf_rfc_get(chan, rsp->data, len); 29530e8b207eSAndrei Emeltchenko clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); 29540a708f8fSGustavo F. Padovan break; 29550a708f8fSGustavo F. Padovan 29560e8b207eSAndrei Emeltchenko case L2CAP_CONF_PENDING: 29570e8b207eSAndrei Emeltchenko set_bit(CONF_REM_CONF_PEND, &chan->conf_state); 29580e8b207eSAndrei Emeltchenko 29590e8b207eSAndrei Emeltchenko if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { 29600e8b207eSAndrei Emeltchenko char buf[64]; 29610e8b207eSAndrei Emeltchenko 29620e8b207eSAndrei Emeltchenko len = l2cap_parse_conf_rsp(chan, rsp->data, len, 29630e8b207eSAndrei Emeltchenko buf, &result); 29640e8b207eSAndrei Emeltchenko if (len < 0) { 29650e8b207eSAndrei Emeltchenko l2cap_send_disconn_req(conn, chan, ECONNRESET); 29660e8b207eSAndrei Emeltchenko goto done; 29670e8b207eSAndrei Emeltchenko } 29680e8b207eSAndrei Emeltchenko 29690e8b207eSAndrei Emeltchenko /* check compatibility */ 29700e8b207eSAndrei Emeltchenko 29710e8b207eSAndrei Emeltchenko clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 29720e8b207eSAndrei Emeltchenko set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 29730e8b207eSAndrei Emeltchenko 29740e8b207eSAndrei Emeltchenko l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 29750e8b207eSAndrei Emeltchenko l2cap_build_conf_rsp(chan, buf, 29760e8b207eSAndrei Emeltchenko L2CAP_CONF_SUCCESS, 0x0000), buf); 29770e8b207eSAndrei Emeltchenko } 29780e8b207eSAndrei Emeltchenko goto done; 29790e8b207eSAndrei Emeltchenko 29800a708f8fSGustavo F. Padovan case L2CAP_CONF_UNACCEPT: 298173ffa904SGustavo F. Padovan if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { 29820a708f8fSGustavo F. Padovan char req[64]; 29830a708f8fSGustavo F. Padovan 29840a708f8fSGustavo F. Padovan if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { 2985e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 29860a708f8fSGustavo F. Padovan goto done; 29870a708f8fSGustavo F. Padovan } 29880a708f8fSGustavo F. Padovan 29890a708f8fSGustavo F. Padovan /* throw out any old stored conf requests */ 29900a708f8fSGustavo F. Padovan result = L2CAP_CONF_SUCCESS; 2991b4450035SGustavo F. Padovan len = l2cap_parse_conf_rsp(chan, rsp->data, len, 2992b4450035SGustavo F. Padovan req, &result); 29930a708f8fSGustavo F. Padovan if (len < 0) { 2994e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 29950a708f8fSGustavo F. Padovan goto done; 29960a708f8fSGustavo F. Padovan } 29970a708f8fSGustavo F. Padovan 29980a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 29990a708f8fSGustavo F. Padovan L2CAP_CONF_REQ, len, req); 300073ffa904SGustavo F. Padovan chan->num_conf_req++; 30010a708f8fSGustavo F. Padovan if (result != L2CAP_CONF_SUCCESS) 30020a708f8fSGustavo F. Padovan goto done; 30030a708f8fSGustavo F. Padovan break; 30040a708f8fSGustavo F. Padovan } 30050a708f8fSGustavo F. Padovan 30060a708f8fSGustavo F. Padovan default: 30072e0052e4SAndrei Emeltchenko __l2cap_chan_set_err(chan, ECONNRESET); 30082e0052e4SAndrei Emeltchenko 3009b83ddfe2SAndrzej Kaczmarek __set_chan_timer(chan, 3010b83ddfe2SAndrzej Kaczmarek msecs_to_jiffies(L2CAP_DISC_REJ_TIMEOUT)); 3011e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 30120a708f8fSGustavo F. Padovan goto done; 30130a708f8fSGustavo F. Padovan } 30140a708f8fSGustavo F. Padovan 30150a708f8fSGustavo F. Padovan if (flags & 0x01) 30160a708f8fSGustavo F. Padovan goto done; 30170a708f8fSGustavo F. Padovan 3018c1360a1cSGustavo F. Padovan set_bit(CONF_INPUT_DONE, &chan->conf_state); 30190a708f8fSGustavo F. Padovan 3020c1360a1cSGustavo F. Padovan if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { 302147d1ec61SGustavo F. Padovan set_default_fcs(chan); 30220a708f8fSGustavo F. Padovan 302389bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 302442e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 302542e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 302658d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 30270c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 3028525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 30290a708f8fSGustavo F. Padovan 3030cf4cd009SAndrei Emeltchenko l2cap_chan_ready(chan); 30310a708f8fSGustavo F. Padovan } 30320a708f8fSGustavo F. Padovan 30330a708f8fSGustavo F. Padovan done: 3034aa2ac881SGustavo F. Padovan release_sock(sk); 30350a708f8fSGustavo F. Padovan return 0; 30360a708f8fSGustavo F. Padovan } 30370a708f8fSGustavo F. Padovan 30380a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 30390a708f8fSGustavo F. Padovan { 30400a708f8fSGustavo F. Padovan struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; 30410a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp rsp; 30420a708f8fSGustavo F. Padovan u16 dcid, scid; 304348454079SGustavo F. Padovan struct l2cap_chan *chan; 30440a708f8fSGustavo F. Padovan struct sock *sk; 30450a708f8fSGustavo F. Padovan 30460a708f8fSGustavo F. Padovan scid = __le16_to_cpu(req->scid); 30470a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 30480a708f8fSGustavo F. Padovan 30490a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); 30500a708f8fSGustavo F. Padovan 30513df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 30523df91ea2SAndrei Emeltchenko 30533df91ea2SAndrei Emeltchenko chan = __l2cap_get_chan_by_scid(conn, dcid); 30543df91ea2SAndrei Emeltchenko if (!chan) { 30553df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 30560a708f8fSGustavo F. Padovan return 0; 30573df91ea2SAndrei Emeltchenko } 30580a708f8fSGustavo F. Padovan 305948454079SGustavo F. Padovan sk = chan->sk; 30603df91ea2SAndrei Emeltchenko lock_sock(sk); 306148454079SGustavo F. Padovan 3062fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 3063fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 30640a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); 30650a708f8fSGustavo F. Padovan 30660a708f8fSGustavo F. Padovan sk->sk_shutdown = SHUTDOWN_MASK; 30670a708f8fSGustavo F. Padovan 306848454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNRESET); 3069aa2ac881SGustavo F. Padovan release_sock(sk); 30700a708f8fSGustavo F. Padovan 3071ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 30723df91ea2SAndrei Emeltchenko 30733df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 30743df91ea2SAndrei Emeltchenko 30750a708f8fSGustavo F. Padovan return 0; 30760a708f8fSGustavo F. Padovan } 30770a708f8fSGustavo F. Padovan 30780a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 30790a708f8fSGustavo F. Padovan { 30800a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; 30810a708f8fSGustavo F. Padovan u16 dcid, scid; 308248454079SGustavo F. Padovan struct l2cap_chan *chan; 30830a708f8fSGustavo F. Padovan struct sock *sk; 30840a708f8fSGustavo F. Padovan 30850a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 30860a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 30870a708f8fSGustavo F. Padovan 30880a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); 30890a708f8fSGustavo F. Padovan 30903df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 30913df91ea2SAndrei Emeltchenko 30923df91ea2SAndrei Emeltchenko chan = __l2cap_get_chan_by_scid(conn, scid); 30933df91ea2SAndrei Emeltchenko if (!chan) { 30943df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 30950a708f8fSGustavo F. Padovan return 0; 30963df91ea2SAndrei Emeltchenko } 30970a708f8fSGustavo F. Padovan 309848454079SGustavo F. Padovan sk = chan->sk; 30993df91ea2SAndrei Emeltchenko lock_sock(sk); 310048454079SGustavo F. Padovan 310148454079SGustavo F. Padovan l2cap_chan_del(chan, 0); 3102aa2ac881SGustavo F. Padovan release_sock(sk); 31030a708f8fSGustavo F. Padovan 3104ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 31053df91ea2SAndrei Emeltchenko 31063df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 31073df91ea2SAndrei Emeltchenko 31080a708f8fSGustavo F. Padovan return 0; 31090a708f8fSGustavo F. Padovan } 31100a708f8fSGustavo F. Padovan 31110a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 31120a708f8fSGustavo F. Padovan { 31130a708f8fSGustavo F. Padovan struct l2cap_info_req *req = (struct l2cap_info_req *) data; 31140a708f8fSGustavo F. Padovan u16 type; 31150a708f8fSGustavo F. Padovan 31160a708f8fSGustavo F. Padovan type = __le16_to_cpu(req->type); 31170a708f8fSGustavo F. Padovan 31180a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x", type); 31190a708f8fSGustavo F. Padovan 31200a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 31210a708f8fSGustavo F. Padovan u8 buf[8]; 31220a708f8fSGustavo F. Padovan u32 feat_mask = l2cap_feat_mask; 31230a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 31240a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 31250a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 31260a708f8fSGustavo F. Padovan if (!disable_ertm) 31270a708f8fSGustavo F. Padovan feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING 31280a708f8fSGustavo F. Padovan | L2CAP_FEAT_FCS; 3129a5fd6f30SAndrei Emeltchenko if (enable_hs) 31306327eb98SAndrei Emeltchenko feat_mask |= L2CAP_FEAT_EXT_FLOW 31316327eb98SAndrei Emeltchenko | L2CAP_FEAT_EXT_WINDOW; 3132a5fd6f30SAndrei Emeltchenko 31330a708f8fSGustavo F. Padovan put_unaligned_le32(feat_mask, rsp->data); 31340a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 31350a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 31360a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 31370a708f8fSGustavo F. Padovan u8 buf[12]; 31380a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 313950a147cdSMat Martineau 314050a147cdSMat Martineau if (enable_hs) 314150a147cdSMat Martineau l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; 314250a147cdSMat Martineau else 314350a147cdSMat Martineau l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; 314450a147cdSMat Martineau 31450a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 31460a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 3147c6337ea6SAndrei Emeltchenko memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan)); 31480a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 31490a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 31500a708f8fSGustavo F. Padovan } else { 31510a708f8fSGustavo F. Padovan struct l2cap_info_rsp rsp; 31520a708f8fSGustavo F. Padovan rsp.type = cpu_to_le16(type); 31530a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); 31540a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 31550a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(rsp), &rsp); 31560a708f8fSGustavo F. Padovan } 31570a708f8fSGustavo F. Padovan 31580a708f8fSGustavo F. Padovan return 0; 31590a708f8fSGustavo F. Padovan } 31600a708f8fSGustavo F. Padovan 31610a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 31620a708f8fSGustavo F. Padovan { 31630a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; 31640a708f8fSGustavo F. Padovan u16 type, result; 31650a708f8fSGustavo F. Padovan 31660a708f8fSGustavo F. Padovan type = __le16_to_cpu(rsp->type); 31670a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 31680a708f8fSGustavo F. Padovan 31690a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); 31700a708f8fSGustavo F. Padovan 3171e90165beSAndrei Emeltchenko /* L2CAP Info req/rsp are unbound to channels, add extra checks */ 3172e90165beSAndrei Emeltchenko if (cmd->ident != conn->info_ident || 3173e90165beSAndrei Emeltchenko conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) 3174e90165beSAndrei Emeltchenko return 0; 3175e90165beSAndrei Emeltchenko 317617cd3f37SUlisses Furquim cancel_delayed_work(&conn->info_timer); 31770a708f8fSGustavo F. Padovan 31780a708f8fSGustavo F. Padovan if (result != L2CAP_IR_SUCCESS) { 31790a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 31800a708f8fSGustavo F. Padovan conn->info_ident = 0; 31810a708f8fSGustavo F. Padovan 31820a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 31830a708f8fSGustavo F. Padovan 31840a708f8fSGustavo F. Padovan return 0; 31850a708f8fSGustavo F. Padovan } 31860a708f8fSGustavo F. Padovan 31870a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 31880a708f8fSGustavo F. Padovan conn->feat_mask = get_unaligned_le32(rsp->data); 31890a708f8fSGustavo F. Padovan 31900a708f8fSGustavo F. Padovan if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { 31910a708f8fSGustavo F. Padovan struct l2cap_info_req req; 31920a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 31930a708f8fSGustavo F. Padovan 31940a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 31950a708f8fSGustavo F. Padovan 31960a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 31970a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 31980a708f8fSGustavo F. Padovan } else { 31990a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 32000a708f8fSGustavo F. Padovan conn->info_ident = 0; 32010a708f8fSGustavo F. Padovan 32020a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 32030a708f8fSGustavo F. Padovan } 32040a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 32050a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 32060a708f8fSGustavo F. Padovan conn->info_ident = 0; 32070a708f8fSGustavo F. Padovan 32080a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 32090a708f8fSGustavo F. Padovan } 32100a708f8fSGustavo F. Padovan 32110a708f8fSGustavo F. Padovan return 0; 32120a708f8fSGustavo F. Padovan } 32130a708f8fSGustavo F. Padovan 3214f94ff6ffSMat Martineau static inline int l2cap_create_channel_req(struct l2cap_conn *conn, 3215f94ff6ffSMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, 3216f94ff6ffSMat Martineau void *data) 3217f94ff6ffSMat Martineau { 3218f94ff6ffSMat Martineau struct l2cap_create_chan_req *req = data; 3219f94ff6ffSMat Martineau struct l2cap_create_chan_rsp rsp; 3220f94ff6ffSMat Martineau u16 psm, scid; 3221f94ff6ffSMat Martineau 3222f94ff6ffSMat Martineau if (cmd_len != sizeof(*req)) 3223f94ff6ffSMat Martineau return -EPROTO; 3224f94ff6ffSMat Martineau 3225f94ff6ffSMat Martineau if (!enable_hs) 3226f94ff6ffSMat Martineau return -EINVAL; 3227f94ff6ffSMat Martineau 3228f94ff6ffSMat Martineau psm = le16_to_cpu(req->psm); 3229f94ff6ffSMat Martineau scid = le16_to_cpu(req->scid); 3230f94ff6ffSMat Martineau 3231f94ff6ffSMat Martineau BT_DBG("psm %d, scid %d, amp_id %d", psm, scid, req->amp_id); 3232f94ff6ffSMat Martineau 3233f94ff6ffSMat Martineau /* Placeholder: Always reject */ 3234f94ff6ffSMat Martineau rsp.dcid = 0; 3235f94ff6ffSMat Martineau rsp.scid = cpu_to_le16(scid); 3236f94ff6ffSMat Martineau rsp.result = L2CAP_CR_NO_MEM; 3237f94ff6ffSMat Martineau rsp.status = L2CAP_CS_NO_INFO; 3238f94ff6ffSMat Martineau 3239f94ff6ffSMat Martineau l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, 3240f94ff6ffSMat Martineau sizeof(rsp), &rsp); 3241f94ff6ffSMat Martineau 3242f94ff6ffSMat Martineau return 0; 3243f94ff6ffSMat Martineau } 3244f94ff6ffSMat Martineau 3245f94ff6ffSMat Martineau static inline int l2cap_create_channel_rsp(struct l2cap_conn *conn, 3246f94ff6ffSMat Martineau struct l2cap_cmd_hdr *cmd, void *data) 3247f94ff6ffSMat Martineau { 3248f94ff6ffSMat Martineau BT_DBG("conn %p", conn); 3249f94ff6ffSMat Martineau 3250f94ff6ffSMat Martineau return l2cap_connect_rsp(conn, cmd, data); 3251f94ff6ffSMat Martineau } 3252f94ff6ffSMat Martineau 32538d5a04a1SMat Martineau static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident, 32548d5a04a1SMat Martineau u16 icid, u16 result) 32558d5a04a1SMat Martineau { 32568d5a04a1SMat Martineau struct l2cap_move_chan_rsp rsp; 32578d5a04a1SMat Martineau 32588d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 32598d5a04a1SMat Martineau 32608d5a04a1SMat Martineau rsp.icid = cpu_to_le16(icid); 32618d5a04a1SMat Martineau rsp.result = cpu_to_le16(result); 32628d5a04a1SMat Martineau 32638d5a04a1SMat Martineau l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_RSP, sizeof(rsp), &rsp); 32648d5a04a1SMat Martineau } 32658d5a04a1SMat Martineau 32668d5a04a1SMat Martineau static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn, 32678d5a04a1SMat Martineau struct l2cap_chan *chan, u16 icid, u16 result) 32688d5a04a1SMat Martineau { 32698d5a04a1SMat Martineau struct l2cap_move_chan_cfm cfm; 32708d5a04a1SMat Martineau u8 ident; 32718d5a04a1SMat Martineau 32728d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 32738d5a04a1SMat Martineau 32748d5a04a1SMat Martineau ident = l2cap_get_ident(conn); 32758d5a04a1SMat Martineau if (chan) 32768d5a04a1SMat Martineau chan->ident = ident; 32778d5a04a1SMat Martineau 32788d5a04a1SMat Martineau cfm.icid = cpu_to_le16(icid); 32798d5a04a1SMat Martineau cfm.result = cpu_to_le16(result); 32808d5a04a1SMat Martineau 32818d5a04a1SMat Martineau l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM, sizeof(cfm), &cfm); 32828d5a04a1SMat Martineau } 32838d5a04a1SMat Martineau 32848d5a04a1SMat Martineau static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident, 32858d5a04a1SMat Martineau u16 icid) 32868d5a04a1SMat Martineau { 32878d5a04a1SMat Martineau struct l2cap_move_chan_cfm_rsp rsp; 32888d5a04a1SMat Martineau 32898d5a04a1SMat Martineau BT_DBG("icid %d", icid); 32908d5a04a1SMat Martineau 32918d5a04a1SMat Martineau rsp.icid = cpu_to_le16(icid); 32928d5a04a1SMat Martineau l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); 32938d5a04a1SMat Martineau } 32948d5a04a1SMat Martineau 32958d5a04a1SMat Martineau static inline int l2cap_move_channel_req(struct l2cap_conn *conn, 32968d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 32978d5a04a1SMat Martineau { 32988d5a04a1SMat Martineau struct l2cap_move_chan_req *req = data; 32998d5a04a1SMat Martineau u16 icid = 0; 33008d5a04a1SMat Martineau u16 result = L2CAP_MR_NOT_ALLOWED; 33018d5a04a1SMat Martineau 33028d5a04a1SMat Martineau if (cmd_len != sizeof(*req)) 33038d5a04a1SMat Martineau return -EPROTO; 33048d5a04a1SMat Martineau 33058d5a04a1SMat Martineau icid = le16_to_cpu(req->icid); 33068d5a04a1SMat Martineau 33078d5a04a1SMat Martineau BT_DBG("icid %d, dest_amp_id %d", icid, req->dest_amp_id); 33088d5a04a1SMat Martineau 33098d5a04a1SMat Martineau if (!enable_hs) 33108d5a04a1SMat Martineau return -EINVAL; 33118d5a04a1SMat Martineau 33128d5a04a1SMat Martineau /* Placeholder: Always refuse */ 33138d5a04a1SMat Martineau l2cap_send_move_chan_rsp(conn, cmd->ident, icid, result); 33148d5a04a1SMat Martineau 33158d5a04a1SMat Martineau return 0; 33168d5a04a1SMat Martineau } 33178d5a04a1SMat Martineau 33188d5a04a1SMat Martineau static inline int l2cap_move_channel_rsp(struct l2cap_conn *conn, 33198d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 33208d5a04a1SMat Martineau { 33218d5a04a1SMat Martineau struct l2cap_move_chan_rsp *rsp = data; 33228d5a04a1SMat Martineau u16 icid, result; 33238d5a04a1SMat Martineau 33248d5a04a1SMat Martineau if (cmd_len != sizeof(*rsp)) 33258d5a04a1SMat Martineau return -EPROTO; 33268d5a04a1SMat Martineau 33278d5a04a1SMat Martineau icid = le16_to_cpu(rsp->icid); 33288d5a04a1SMat Martineau result = le16_to_cpu(rsp->result); 33298d5a04a1SMat Martineau 33308d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 33318d5a04a1SMat Martineau 33328d5a04a1SMat Martineau /* Placeholder: Always unconfirmed */ 33338d5a04a1SMat Martineau l2cap_send_move_chan_cfm(conn, NULL, icid, L2CAP_MC_UNCONFIRMED); 33348d5a04a1SMat Martineau 33358d5a04a1SMat Martineau return 0; 33368d5a04a1SMat Martineau } 33378d5a04a1SMat Martineau 33388d5a04a1SMat Martineau static inline int l2cap_move_channel_confirm(struct l2cap_conn *conn, 33398d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 33408d5a04a1SMat Martineau { 33418d5a04a1SMat Martineau struct l2cap_move_chan_cfm *cfm = data; 33428d5a04a1SMat Martineau u16 icid, result; 33438d5a04a1SMat Martineau 33448d5a04a1SMat Martineau if (cmd_len != sizeof(*cfm)) 33458d5a04a1SMat Martineau return -EPROTO; 33468d5a04a1SMat Martineau 33478d5a04a1SMat Martineau icid = le16_to_cpu(cfm->icid); 33488d5a04a1SMat Martineau result = le16_to_cpu(cfm->result); 33498d5a04a1SMat Martineau 33508d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 33518d5a04a1SMat Martineau 33528d5a04a1SMat Martineau l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); 33538d5a04a1SMat Martineau 33548d5a04a1SMat Martineau return 0; 33558d5a04a1SMat Martineau } 33568d5a04a1SMat Martineau 33578d5a04a1SMat Martineau static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, 33588d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 33598d5a04a1SMat Martineau { 33608d5a04a1SMat Martineau struct l2cap_move_chan_cfm_rsp *rsp = data; 33618d5a04a1SMat Martineau u16 icid; 33628d5a04a1SMat Martineau 33638d5a04a1SMat Martineau if (cmd_len != sizeof(*rsp)) 33648d5a04a1SMat Martineau return -EPROTO; 33658d5a04a1SMat Martineau 33668d5a04a1SMat Martineau icid = le16_to_cpu(rsp->icid); 33678d5a04a1SMat Martineau 33688d5a04a1SMat Martineau BT_DBG("icid %d", icid); 33698d5a04a1SMat Martineau 33708d5a04a1SMat Martineau return 0; 33718d5a04a1SMat Martineau } 33728d5a04a1SMat Martineau 3373e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency, 3374de73115aSClaudio Takahasi u16 to_multiplier) 3375de73115aSClaudio Takahasi { 3376de73115aSClaudio Takahasi u16 max_latency; 3377de73115aSClaudio Takahasi 3378de73115aSClaudio Takahasi if (min > max || min < 6 || max > 3200) 3379de73115aSClaudio Takahasi return -EINVAL; 3380de73115aSClaudio Takahasi 3381de73115aSClaudio Takahasi if (to_multiplier < 10 || to_multiplier > 3200) 3382de73115aSClaudio Takahasi return -EINVAL; 3383de73115aSClaudio Takahasi 3384de73115aSClaudio Takahasi if (max >= to_multiplier * 8) 3385de73115aSClaudio Takahasi return -EINVAL; 3386de73115aSClaudio Takahasi 3387de73115aSClaudio Takahasi max_latency = (to_multiplier * 8 / max) - 1; 3388de73115aSClaudio Takahasi if (latency > 499 || latency > max_latency) 3389de73115aSClaudio Takahasi return -EINVAL; 3390de73115aSClaudio Takahasi 3391de73115aSClaudio Takahasi return 0; 3392de73115aSClaudio Takahasi } 3393de73115aSClaudio Takahasi 3394de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, 3395de73115aSClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 3396de73115aSClaudio Takahasi { 3397de73115aSClaudio Takahasi struct hci_conn *hcon = conn->hcon; 3398de73115aSClaudio Takahasi struct l2cap_conn_param_update_req *req; 3399de73115aSClaudio Takahasi struct l2cap_conn_param_update_rsp rsp; 3400de73115aSClaudio Takahasi u16 min, max, latency, to_multiplier, cmd_len; 34012ce603ebSClaudio Takahasi int err; 3402de73115aSClaudio Takahasi 3403de73115aSClaudio Takahasi if (!(hcon->link_mode & HCI_LM_MASTER)) 3404de73115aSClaudio Takahasi return -EINVAL; 3405de73115aSClaudio Takahasi 3406de73115aSClaudio Takahasi cmd_len = __le16_to_cpu(cmd->len); 3407de73115aSClaudio Takahasi if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) 3408de73115aSClaudio Takahasi return -EPROTO; 3409de73115aSClaudio Takahasi 3410de73115aSClaudio Takahasi req = (struct l2cap_conn_param_update_req *) data; 3411de73115aSClaudio Takahasi min = __le16_to_cpu(req->min); 3412de73115aSClaudio Takahasi max = __le16_to_cpu(req->max); 3413de73115aSClaudio Takahasi latency = __le16_to_cpu(req->latency); 3414de73115aSClaudio Takahasi to_multiplier = __le16_to_cpu(req->to_multiplier); 3415de73115aSClaudio Takahasi 3416de73115aSClaudio Takahasi BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", 3417de73115aSClaudio Takahasi min, max, latency, to_multiplier); 3418de73115aSClaudio Takahasi 3419de73115aSClaudio Takahasi memset(&rsp, 0, sizeof(rsp)); 34202ce603ebSClaudio Takahasi 34212ce603ebSClaudio Takahasi err = l2cap_check_conn_param(min, max, latency, to_multiplier); 34222ce603ebSClaudio Takahasi if (err) 3423de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); 3424de73115aSClaudio Takahasi else 3425de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); 3426de73115aSClaudio Takahasi 3427de73115aSClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, 3428de73115aSClaudio Takahasi sizeof(rsp), &rsp); 3429de73115aSClaudio Takahasi 34302ce603ebSClaudio Takahasi if (!err) 34312ce603ebSClaudio Takahasi hci_le_conn_update(hcon, min, max, latency, to_multiplier); 34322ce603ebSClaudio Takahasi 3433de73115aSClaudio Takahasi return 0; 3434de73115aSClaudio Takahasi } 3435de73115aSClaudio Takahasi 34363300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, 34373300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 34383300d9a9SClaudio Takahasi { 34393300d9a9SClaudio Takahasi int err = 0; 34403300d9a9SClaudio Takahasi 34413300d9a9SClaudio Takahasi switch (cmd->code) { 34423300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 34433300d9a9SClaudio Takahasi l2cap_command_rej(conn, cmd, data); 34443300d9a9SClaudio Takahasi break; 34453300d9a9SClaudio Takahasi 34463300d9a9SClaudio Takahasi case L2CAP_CONN_REQ: 34473300d9a9SClaudio Takahasi err = l2cap_connect_req(conn, cmd, data); 34483300d9a9SClaudio Takahasi break; 34493300d9a9SClaudio Takahasi 34503300d9a9SClaudio Takahasi case L2CAP_CONN_RSP: 34513300d9a9SClaudio Takahasi err = l2cap_connect_rsp(conn, cmd, data); 34523300d9a9SClaudio Takahasi break; 34533300d9a9SClaudio Takahasi 34543300d9a9SClaudio Takahasi case L2CAP_CONF_REQ: 34553300d9a9SClaudio Takahasi err = l2cap_config_req(conn, cmd, cmd_len, data); 34563300d9a9SClaudio Takahasi break; 34573300d9a9SClaudio Takahasi 34583300d9a9SClaudio Takahasi case L2CAP_CONF_RSP: 34593300d9a9SClaudio Takahasi err = l2cap_config_rsp(conn, cmd, data); 34603300d9a9SClaudio Takahasi break; 34613300d9a9SClaudio Takahasi 34623300d9a9SClaudio Takahasi case L2CAP_DISCONN_REQ: 34633300d9a9SClaudio Takahasi err = l2cap_disconnect_req(conn, cmd, data); 34643300d9a9SClaudio Takahasi break; 34653300d9a9SClaudio Takahasi 34663300d9a9SClaudio Takahasi case L2CAP_DISCONN_RSP: 34673300d9a9SClaudio Takahasi err = l2cap_disconnect_rsp(conn, cmd, data); 34683300d9a9SClaudio Takahasi break; 34693300d9a9SClaudio Takahasi 34703300d9a9SClaudio Takahasi case L2CAP_ECHO_REQ: 34713300d9a9SClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); 34723300d9a9SClaudio Takahasi break; 34733300d9a9SClaudio Takahasi 34743300d9a9SClaudio Takahasi case L2CAP_ECHO_RSP: 34753300d9a9SClaudio Takahasi break; 34763300d9a9SClaudio Takahasi 34773300d9a9SClaudio Takahasi case L2CAP_INFO_REQ: 34783300d9a9SClaudio Takahasi err = l2cap_information_req(conn, cmd, data); 34793300d9a9SClaudio Takahasi break; 34803300d9a9SClaudio Takahasi 34813300d9a9SClaudio Takahasi case L2CAP_INFO_RSP: 34823300d9a9SClaudio Takahasi err = l2cap_information_rsp(conn, cmd, data); 34833300d9a9SClaudio Takahasi break; 34843300d9a9SClaudio Takahasi 3485f94ff6ffSMat Martineau case L2CAP_CREATE_CHAN_REQ: 3486f94ff6ffSMat Martineau err = l2cap_create_channel_req(conn, cmd, cmd_len, data); 3487f94ff6ffSMat Martineau break; 3488f94ff6ffSMat Martineau 3489f94ff6ffSMat Martineau case L2CAP_CREATE_CHAN_RSP: 3490f94ff6ffSMat Martineau err = l2cap_create_channel_rsp(conn, cmd, data); 3491f94ff6ffSMat Martineau break; 3492f94ff6ffSMat Martineau 34938d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_REQ: 34948d5a04a1SMat Martineau err = l2cap_move_channel_req(conn, cmd, cmd_len, data); 34958d5a04a1SMat Martineau break; 34968d5a04a1SMat Martineau 34978d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_RSP: 34988d5a04a1SMat Martineau err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data); 34998d5a04a1SMat Martineau break; 35008d5a04a1SMat Martineau 35018d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_CFM: 35028d5a04a1SMat Martineau err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); 35038d5a04a1SMat Martineau break; 35048d5a04a1SMat Martineau 35058d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_CFM_RSP: 35068d5a04a1SMat Martineau err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); 35078d5a04a1SMat Martineau break; 35088d5a04a1SMat Martineau 35093300d9a9SClaudio Takahasi default: 35103300d9a9SClaudio Takahasi BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); 35113300d9a9SClaudio Takahasi err = -EINVAL; 35123300d9a9SClaudio Takahasi break; 35133300d9a9SClaudio Takahasi } 35143300d9a9SClaudio Takahasi 35153300d9a9SClaudio Takahasi return err; 35163300d9a9SClaudio Takahasi } 35173300d9a9SClaudio Takahasi 35183300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, 35193300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 35203300d9a9SClaudio Takahasi { 35213300d9a9SClaudio Takahasi switch (cmd->code) { 35223300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 35233300d9a9SClaudio Takahasi return 0; 35243300d9a9SClaudio Takahasi 35253300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_REQ: 3526de73115aSClaudio Takahasi return l2cap_conn_param_update_req(conn, cmd, data); 35273300d9a9SClaudio Takahasi 35283300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_RSP: 35293300d9a9SClaudio Takahasi return 0; 35303300d9a9SClaudio Takahasi 35313300d9a9SClaudio Takahasi default: 35323300d9a9SClaudio Takahasi BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); 35333300d9a9SClaudio Takahasi return -EINVAL; 35343300d9a9SClaudio Takahasi } 35353300d9a9SClaudio Takahasi } 35363300d9a9SClaudio Takahasi 35373300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn, 35383300d9a9SClaudio Takahasi struct sk_buff *skb) 35390a708f8fSGustavo F. Padovan { 35400a708f8fSGustavo F. Padovan u8 *data = skb->data; 35410a708f8fSGustavo F. Padovan int len = skb->len; 35420a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr cmd; 35433300d9a9SClaudio Takahasi int err; 35440a708f8fSGustavo F. Padovan 35450a708f8fSGustavo F. Padovan l2cap_raw_recv(conn, skb); 35460a708f8fSGustavo F. Padovan 35470a708f8fSGustavo F. Padovan while (len >= L2CAP_CMD_HDR_SIZE) { 35480a708f8fSGustavo F. Padovan u16 cmd_len; 35490a708f8fSGustavo F. Padovan memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); 35500a708f8fSGustavo F. Padovan data += L2CAP_CMD_HDR_SIZE; 35510a708f8fSGustavo F. Padovan len -= L2CAP_CMD_HDR_SIZE; 35520a708f8fSGustavo F. Padovan 35530a708f8fSGustavo F. Padovan cmd_len = le16_to_cpu(cmd.len); 35540a708f8fSGustavo F. Padovan 35550a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident); 35560a708f8fSGustavo F. Padovan 35570a708f8fSGustavo F. Padovan if (cmd_len > len || !cmd.ident) { 35580a708f8fSGustavo F. Padovan BT_DBG("corrupted command"); 35590a708f8fSGustavo F. Padovan break; 35600a708f8fSGustavo F. Padovan } 35610a708f8fSGustavo F. Padovan 35623300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 35633300d9a9SClaudio Takahasi err = l2cap_le_sig_cmd(conn, &cmd, data); 35643300d9a9SClaudio Takahasi else 35653300d9a9SClaudio Takahasi err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); 35660a708f8fSGustavo F. Padovan 35670a708f8fSGustavo F. Padovan if (err) { 3568e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_unk rej; 35692c6d1a2eSGustavo F. Padovan 35702c6d1a2eSGustavo F. Padovan BT_ERR("Wrong link type (%d)", err); 35710a708f8fSGustavo F. Padovan 35720a708f8fSGustavo F. Padovan /* FIXME: Map err to a valid reason */ 3573e2fd318eSIlia Kolomisnky rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); 35740a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); 35750a708f8fSGustavo F. Padovan } 35760a708f8fSGustavo F. Padovan 35770a708f8fSGustavo F. Padovan data += cmd_len; 35780a708f8fSGustavo F. Padovan len -= cmd_len; 35790a708f8fSGustavo F. Padovan } 35800a708f8fSGustavo F. Padovan 35810a708f8fSGustavo F. Padovan kfree_skb(skb); 35820a708f8fSGustavo F. Padovan } 35830a708f8fSGustavo F. Padovan 358447d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb) 35850a708f8fSGustavo F. Padovan { 35860a708f8fSGustavo F. Padovan u16 our_fcs, rcv_fcs; 3587e4ca6d98SAndrei Emeltchenko int hdr_size; 3588e4ca6d98SAndrei Emeltchenko 3589e4ca6d98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 3590e4ca6d98SAndrei Emeltchenko hdr_size = L2CAP_EXT_HDR_SIZE; 3591e4ca6d98SAndrei Emeltchenko else 3592e4ca6d98SAndrei Emeltchenko hdr_size = L2CAP_ENH_HDR_SIZE; 35930a708f8fSGustavo F. Padovan 359447d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 359503a51213SAndrei Emeltchenko skb_trim(skb, skb->len - L2CAP_FCS_SIZE); 35960a708f8fSGustavo F. Padovan rcv_fcs = get_unaligned_le16(skb->data + skb->len); 35970a708f8fSGustavo F. Padovan our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); 35980a708f8fSGustavo F. Padovan 35990a708f8fSGustavo F. Padovan if (our_fcs != rcv_fcs) 36000a708f8fSGustavo F. Padovan return -EBADMSG; 36010a708f8fSGustavo F. Padovan } 36020a708f8fSGustavo F. Padovan return 0; 36030a708f8fSGustavo F. Padovan } 36040a708f8fSGustavo F. Padovan 3605525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan) 36060a708f8fSGustavo F. Padovan { 360788843ab0SAndrei Emeltchenko u32 control = 0; 36080a708f8fSGustavo F. Padovan 36096a026610SGustavo F. Padovan chan->frames_sent = 0; 36100a708f8fSGustavo F. Padovan 36110b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 36120a708f8fSGustavo F. Padovan 3613e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 3614ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 3615525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 3616e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 36170a708f8fSGustavo F. Padovan } 36180a708f8fSGustavo F. Padovan 3619e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) 3620525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 36210a708f8fSGustavo F. Padovan 3622525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 36230a708f8fSGustavo F. Padovan 3624e2ab4353SGustavo F. Padovan if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && 36256a026610SGustavo F. Padovan chan->frames_sent == 0) { 3626ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 3627525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 36280a708f8fSGustavo F. Padovan } 36290a708f8fSGustavo F. Padovan } 36300a708f8fSGustavo F. Padovan 3631fb45de7dSAndrei Emeltchenko static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u16 tx_seq, u8 sar) 36320a708f8fSGustavo F. Padovan { 36330a708f8fSGustavo F. Padovan struct sk_buff *next_skb; 36340a708f8fSGustavo F. Padovan int tx_seq_offset, next_tx_seq_offset; 36350a708f8fSGustavo F. Padovan 36360a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 36370a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 36380a708f8fSGustavo F. Padovan 3639f1c6775bSGustavo F. Padovan next_skb = skb_peek(&chan->srej_q); 36400a708f8fSGustavo F. Padovan 3641836be934SAndrei Emeltchenko tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq); 36420a708f8fSGustavo F. Padovan 3643039d9572SSzymon Janc while (next_skb) { 36440a708f8fSGustavo F. Padovan if (bt_cb(next_skb)->tx_seq == tx_seq) 36450a708f8fSGustavo F. Padovan return -EINVAL; 36460a708f8fSGustavo F. Padovan 3647836be934SAndrei Emeltchenko next_tx_seq_offset = __seq_offset(chan, 3648836be934SAndrei Emeltchenko bt_cb(next_skb)->tx_seq, chan->buffer_seq); 36490a708f8fSGustavo F. Padovan 36500a708f8fSGustavo F. Padovan if (next_tx_seq_offset > tx_seq_offset) { 3651f1c6775bSGustavo F. Padovan __skb_queue_before(&chan->srej_q, next_skb, skb); 36520a708f8fSGustavo F. Padovan return 0; 36530a708f8fSGustavo F. Padovan } 36540a708f8fSGustavo F. Padovan 3655f1c6775bSGustavo F. Padovan if (skb_queue_is_last(&chan->srej_q, next_skb)) 3656039d9572SSzymon Janc next_skb = NULL; 3657039d9572SSzymon Janc else 3658039d9572SSzymon Janc next_skb = skb_queue_next(&chan->srej_q, next_skb); 3659039d9572SSzymon Janc } 36600a708f8fSGustavo F. Padovan 3661f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 36620a708f8fSGustavo F. Padovan 36630a708f8fSGustavo F. Padovan return 0; 36640a708f8fSGustavo F. Padovan } 36650a708f8fSGustavo F. Padovan 366684084a31SMat Martineau static void append_skb_frag(struct sk_buff *skb, 366784084a31SMat Martineau struct sk_buff *new_frag, struct sk_buff **last_frag) 36680a708f8fSGustavo F. Padovan { 366984084a31SMat Martineau /* skb->len reflects data in skb as well as all fragments 367084084a31SMat Martineau * skb->data_len reflects only data in fragments 367184084a31SMat Martineau */ 367284084a31SMat Martineau if (!skb_has_frag_list(skb)) 367384084a31SMat Martineau skb_shinfo(skb)->frag_list = new_frag; 367484084a31SMat Martineau 367584084a31SMat Martineau new_frag->next = NULL; 367684084a31SMat Martineau 367784084a31SMat Martineau (*last_frag)->next = new_frag; 367884084a31SMat Martineau *last_frag = new_frag; 367984084a31SMat Martineau 368084084a31SMat Martineau skb->len += new_frag->len; 368184084a31SMat Martineau skb->data_len += new_frag->len; 368284084a31SMat Martineau skb->truesize += new_frag->truesize; 368384084a31SMat Martineau } 368484084a31SMat Martineau 368588843ab0SAndrei Emeltchenko static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u32 control) 368684084a31SMat Martineau { 368784084a31SMat Martineau int err = -EINVAL; 36880a708f8fSGustavo F. Padovan 36897e0ef6eeSAndrei Emeltchenko switch (__get_ctrl_sar(chan, control)) { 36907e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_UNSEGMENTED: 369184084a31SMat Martineau if (chan->sdu) 369284084a31SMat Martineau break; 36930a708f8fSGustavo F. Padovan 369484084a31SMat Martineau err = chan->ops->recv(chan->data, skb); 369584084a31SMat Martineau break; 36960a708f8fSGustavo F. Padovan 36977e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_START: 369884084a31SMat Martineau if (chan->sdu) 369984084a31SMat Martineau break; 37000a708f8fSGustavo F. Padovan 37016f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 370203a51213SAndrei Emeltchenko skb_pull(skb, L2CAP_SDULEN_SIZE); 37030a708f8fSGustavo F. Padovan 370484084a31SMat Martineau if (chan->sdu_len > chan->imtu) { 370584084a31SMat Martineau err = -EMSGSIZE; 370684084a31SMat Martineau break; 370784084a31SMat Martineau } 37080a708f8fSGustavo F. Padovan 370984084a31SMat Martineau if (skb->len >= chan->sdu_len) 371084084a31SMat Martineau break; 371184084a31SMat Martineau 371284084a31SMat Martineau chan->sdu = skb; 371384084a31SMat Martineau chan->sdu_last_frag = skb; 371484084a31SMat Martineau 371584084a31SMat Martineau skb = NULL; 371684084a31SMat Martineau err = 0; 37170a708f8fSGustavo F. Padovan break; 37180a708f8fSGustavo F. Padovan 37197e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_CONTINUE: 37206f61fd47SGustavo F. Padovan if (!chan->sdu) 372184084a31SMat Martineau break; 37220a708f8fSGustavo F. Padovan 372384084a31SMat Martineau append_skb_frag(chan->sdu, skb, 372484084a31SMat Martineau &chan->sdu_last_frag); 372584084a31SMat Martineau skb = NULL; 37260a708f8fSGustavo F. Padovan 372784084a31SMat Martineau if (chan->sdu->len >= chan->sdu_len) 372884084a31SMat Martineau break; 37290a708f8fSGustavo F. Padovan 373084084a31SMat Martineau err = 0; 37310a708f8fSGustavo F. Padovan break; 37320a708f8fSGustavo F. Padovan 37337e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_END: 37346f61fd47SGustavo F. Padovan if (!chan->sdu) 373584084a31SMat Martineau break; 37360a708f8fSGustavo F. Padovan 373784084a31SMat Martineau append_skb_frag(chan->sdu, skb, 373884084a31SMat Martineau &chan->sdu_last_frag); 373984084a31SMat Martineau skb = NULL; 37400a708f8fSGustavo F. Padovan 374184084a31SMat Martineau if (chan->sdu->len != chan->sdu_len) 374284084a31SMat Martineau break; 37430a708f8fSGustavo F. Padovan 374484084a31SMat Martineau err = chan->ops->recv(chan->data, chan->sdu); 37450a708f8fSGustavo F. Padovan 374684084a31SMat Martineau if (!err) { 374784084a31SMat Martineau /* Reassembly complete */ 374884084a31SMat Martineau chan->sdu = NULL; 374984084a31SMat Martineau chan->sdu_last_frag = NULL; 375084084a31SMat Martineau chan->sdu_len = 0; 37510a708f8fSGustavo F. Padovan } 37520a708f8fSGustavo F. Padovan break; 37530a708f8fSGustavo F. Padovan } 37540a708f8fSGustavo F. Padovan 375584084a31SMat Martineau if (err) { 37560a708f8fSGustavo F. Padovan kfree_skb(skb); 37576f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 37586f61fd47SGustavo F. Padovan chan->sdu = NULL; 375984084a31SMat Martineau chan->sdu_last_frag = NULL; 376084084a31SMat Martineau chan->sdu_len = 0; 376184084a31SMat Martineau } 37620a708f8fSGustavo F. Padovan 376384084a31SMat Martineau return err; 37640a708f8fSGustavo F. Padovan } 37650a708f8fSGustavo F. Padovan 376626f880d2SMat Martineau static void l2cap_ertm_enter_local_busy(struct l2cap_chan *chan) 37670a708f8fSGustavo F. Padovan { 376826f880d2SMat Martineau BT_DBG("chan %p, Enter local busy", chan); 376926f880d2SMat Martineau 377026f880d2SMat Martineau set_bit(CONN_LOCAL_BUSY, &chan->conn_state); 377126f880d2SMat Martineau 377277f918bcSSzymon Janc __set_ack_timer(chan); 37730a708f8fSGustavo F. Padovan } 37740a708f8fSGustavo F. Padovan 377526f880d2SMat Martineau static void l2cap_ertm_exit_local_busy(struct l2cap_chan *chan) 377626f880d2SMat Martineau { 377788843ab0SAndrei Emeltchenko u32 control; 37780a708f8fSGustavo F. Padovan 3779e2ab4353SGustavo F. Padovan if (!test_bit(CONN_RNR_SENT, &chan->conn_state)) 37800a708f8fSGustavo F. Padovan goto done; 37810a708f8fSGustavo F. Padovan 37820b209faeSAndrei Emeltchenko control = __set_reqseq(chan, chan->buffer_seq); 3783e3781735SAndrei Emeltchenko control |= __set_ctrl_poll(chan); 3784ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 3785525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 37866a026610SGustavo F. Padovan chan->retry_count = 1; 37870a708f8fSGustavo F. Padovan 37881a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 37891a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 37900a708f8fSGustavo F. Padovan 3791e2ab4353SGustavo F. Padovan set_bit(CONN_WAIT_F, &chan->conn_state); 37920a708f8fSGustavo F. Padovan 37930a708f8fSGustavo F. Padovan done: 3794e2ab4353SGustavo F. Padovan clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); 3795e2ab4353SGustavo F. Padovan clear_bit(CONN_RNR_SENT, &chan->conn_state); 37960a708f8fSGustavo F. Padovan 379749208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit local busy", chan); 37980a708f8fSGustavo F. Padovan } 37990a708f8fSGustavo F. Padovan 3800e328140fSMat Martineau void l2cap_chan_busy(struct l2cap_chan *chan, int busy) 38010a708f8fSGustavo F. Padovan { 3802e328140fSMat Martineau if (chan->mode == L2CAP_MODE_ERTM) { 3803e328140fSMat Martineau if (busy) 380426f880d2SMat Martineau l2cap_ertm_enter_local_busy(chan); 3805e328140fSMat Martineau else 3806e328140fSMat Martineau l2cap_ertm_exit_local_busy(chan); 38070a708f8fSGustavo F. Padovan } 38080a708f8fSGustavo F. Padovan } 38090a708f8fSGustavo F. Padovan 3810fb45de7dSAndrei Emeltchenko static void l2cap_check_srej_gap(struct l2cap_chan *chan, u16 tx_seq) 38110a708f8fSGustavo F. Padovan { 38120a708f8fSGustavo F. Padovan struct sk_buff *skb; 381388843ab0SAndrei Emeltchenko u32 control; 38140a708f8fSGustavo F. Padovan 3815e328140fSMat Martineau while ((skb = skb_peek(&chan->srej_q)) && 3816e328140fSMat Martineau !test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 3817e328140fSMat Martineau int err; 3818e328140fSMat Martineau 38190a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq != tx_seq) 38200a708f8fSGustavo F. Padovan break; 38210a708f8fSGustavo F. Padovan 3822f1c6775bSGustavo F. Padovan skb = skb_dequeue(&chan->srej_q); 38237e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, bt_cb(skb)->sar); 382484084a31SMat Martineau err = l2cap_reassemble_sdu(chan, skb, control); 3825e328140fSMat Martineau 3826e328140fSMat Martineau if (err < 0) { 3827e328140fSMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 3828e328140fSMat Martineau break; 3829e328140fSMat Martineau } 3830e328140fSMat Martineau 3831836be934SAndrei Emeltchenko chan->buffer_seq_srej = __next_seq(chan, chan->buffer_seq_srej); 3832836be934SAndrei Emeltchenko tx_seq = __next_seq(chan, tx_seq); 38330a708f8fSGustavo F. Padovan } 38340a708f8fSGustavo F. Padovan } 38350a708f8fSGustavo F. Padovan 3836fb45de7dSAndrei Emeltchenko static void l2cap_resend_srejframe(struct l2cap_chan *chan, u16 tx_seq) 38370a708f8fSGustavo F. Padovan { 38380a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 383988843ab0SAndrei Emeltchenko u32 control; 38400a708f8fSGustavo F. Padovan 384139d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 38420a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 38430a708f8fSGustavo F. Padovan list_del(&l->list); 38440a708f8fSGustavo F. Padovan kfree(l); 38450a708f8fSGustavo F. Padovan return; 38460a708f8fSGustavo F. Padovan } 3847ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 38480b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, l->tx_seq); 3849525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 38500a708f8fSGustavo F. Padovan list_del(&l->list); 385139d5a3eeSGustavo F. Padovan list_add_tail(&l->list, &chan->srej_l); 38520a708f8fSGustavo F. Padovan } 38530a708f8fSGustavo F. Padovan } 38540a708f8fSGustavo F. Padovan 3855aef89f21SSzymon Janc static int l2cap_send_srejframe(struct l2cap_chan *chan, u16 tx_seq) 38560a708f8fSGustavo F. Padovan { 38570a708f8fSGustavo F. Padovan struct srej_list *new; 385888843ab0SAndrei Emeltchenko u32 control; 38590a708f8fSGustavo F. Padovan 386042e5c802SGustavo F. Padovan while (tx_seq != chan->expected_tx_seq) { 3861ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 38620b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->expected_tx_seq); 3863525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 38640a708f8fSGustavo F. Padovan 38650a708f8fSGustavo F. Padovan new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); 3866aef89f21SSzymon Janc if (!new) 3867aef89f21SSzymon Janc return -ENOMEM; 3868aef89f21SSzymon Janc 386942e5c802SGustavo F. Padovan new->tx_seq = chan->expected_tx_seq; 3870836be934SAndrei Emeltchenko 3871836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); 3872836be934SAndrei Emeltchenko 387339d5a3eeSGustavo F. Padovan list_add_tail(&new->list, &chan->srej_l); 38740a708f8fSGustavo F. Padovan } 3875836be934SAndrei Emeltchenko 3876836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); 3877aef89f21SSzymon Janc 3878aef89f21SSzymon Janc return 0; 38790a708f8fSGustavo F. Padovan } 38800a708f8fSGustavo F. Padovan 388188843ab0SAndrei Emeltchenko static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb) 38820a708f8fSGustavo F. Padovan { 3883fb45de7dSAndrei Emeltchenko u16 tx_seq = __get_txseq(chan, rx_control); 38840b209faeSAndrei Emeltchenko u16 req_seq = __get_reqseq(chan, rx_control); 38857e0ef6eeSAndrei Emeltchenko u8 sar = __get_ctrl_sar(chan, rx_control); 38860a708f8fSGustavo F. Padovan int tx_seq_offset, expected_tx_seq_offset; 388747d1ec61SGustavo F. Padovan int num_to_ack = (chan->tx_win/6) + 1; 38880a708f8fSGustavo F. Padovan int err = 0; 38890a708f8fSGustavo F. Padovan 389088843ab0SAndrei Emeltchenko BT_DBG("chan %p len %d tx_seq %d rx_control 0x%8.8x", chan, skb->len, 3891525cd185SGustavo F. Padovan tx_seq, rx_control); 38920a708f8fSGustavo F. Padovan 389303f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control) && 3894e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 38951a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 38966a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 38971a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 3898e2ab4353SGustavo F. Padovan clear_bit(CONN_WAIT_F, &chan->conn_state); 38990a708f8fSGustavo F. Padovan } 39000a708f8fSGustavo F. Padovan 390142e5c802SGustavo F. Padovan chan->expected_ack_seq = req_seq; 390242e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 39030a708f8fSGustavo F. Padovan 3904836be934SAndrei Emeltchenko tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq); 39050a708f8fSGustavo F. Padovan 39060a708f8fSGustavo F. Padovan /* invalid tx_seq */ 390747d1ec61SGustavo F. Padovan if (tx_seq_offset >= chan->tx_win) { 39088c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 39090a708f8fSGustavo F. Padovan goto drop; 39100a708f8fSGustavo F. Padovan } 39110a708f8fSGustavo F. Padovan 391277f918bcSSzymon Janc if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 391377f918bcSSzymon Janc if (!test_bit(CONN_RNR_SENT, &chan->conn_state)) 391477f918bcSSzymon Janc l2cap_send_ack(chan); 39150a708f8fSGustavo F. Padovan goto drop; 391677f918bcSSzymon Janc } 39170a708f8fSGustavo F. Padovan 391802f1b641SMat Martineau if (tx_seq == chan->expected_tx_seq) 391902f1b641SMat Martineau goto expected; 392002f1b641SMat Martineau 3921e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 39220a708f8fSGustavo F. Padovan struct srej_list *first; 39230a708f8fSGustavo F. Padovan 392439d5a3eeSGustavo F. Padovan first = list_first_entry(&chan->srej_l, 39250a708f8fSGustavo F. Padovan struct srej_list, list); 39260a708f8fSGustavo F. Padovan if (tx_seq == first->tx_seq) { 392742e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 3928525cd185SGustavo F. Padovan l2cap_check_srej_gap(chan, tx_seq); 39290a708f8fSGustavo F. Padovan 39300a708f8fSGustavo F. Padovan list_del(&first->list); 39310a708f8fSGustavo F. Padovan kfree(first); 39320a708f8fSGustavo F. Padovan 393339d5a3eeSGustavo F. Padovan if (list_empty(&chan->srej_l)) { 393442e5c802SGustavo F. Padovan chan->buffer_seq = chan->buffer_seq_srej; 3935e2ab4353SGustavo F. Padovan clear_bit(CONN_SREJ_SENT, &chan->conn_state); 3936525cd185SGustavo F. Padovan l2cap_send_ack(chan); 393749208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit SREJ_SENT", chan); 39380a708f8fSGustavo F. Padovan } 39390a708f8fSGustavo F. Padovan } else { 39400a708f8fSGustavo F. Padovan struct srej_list *l; 39410a708f8fSGustavo F. Padovan 39420a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 394342e5c802SGustavo F. Padovan if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0) 39440a708f8fSGustavo F. Padovan goto drop; 39450a708f8fSGustavo F. Padovan 394639d5a3eeSGustavo F. Padovan list_for_each_entry(l, &chan->srej_l, list) { 39470a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 3948525cd185SGustavo F. Padovan l2cap_resend_srejframe(chan, tx_seq); 39490a708f8fSGustavo F. Padovan return 0; 39500a708f8fSGustavo F. Padovan } 39510a708f8fSGustavo F. Padovan } 3952aef89f21SSzymon Janc 3953aef89f21SSzymon Janc err = l2cap_send_srejframe(chan, tx_seq); 3954aef89f21SSzymon Janc if (err < 0) { 3955aef89f21SSzymon Janc l2cap_send_disconn_req(chan->conn, chan, -err); 3956aef89f21SSzymon Janc return err; 3957aef89f21SSzymon Janc } 39580a708f8fSGustavo F. Padovan } 39590a708f8fSGustavo F. Padovan } else { 3960836be934SAndrei Emeltchenko expected_tx_seq_offset = __seq_offset(chan, 3961836be934SAndrei Emeltchenko chan->expected_tx_seq, chan->buffer_seq); 39620a708f8fSGustavo F. Padovan 39630a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 39640a708f8fSGustavo F. Padovan if (tx_seq_offset < expected_tx_seq_offset) 39650a708f8fSGustavo F. Padovan goto drop; 39660a708f8fSGustavo F. Padovan 3967e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_SENT, &chan->conn_state); 39680a708f8fSGustavo F. Padovan 396949208c9cSGustavo F. Padovan BT_DBG("chan %p, Enter SREJ", chan); 39700a708f8fSGustavo F. Padovan 397139d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 397242e5c802SGustavo F. Padovan chan->buffer_seq_srej = chan->buffer_seq; 39730a708f8fSGustavo F. Padovan 3974f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->srej_q); 397542e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 39760a708f8fSGustavo F. Padovan 39770ef3ef0fSSzymon Janc /* Set P-bit only if there are some I-frames to ack. */ 39780ef3ef0fSSzymon Janc if (__clear_ack_timer(chan)) 3979e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_PBIT, &chan->conn_state); 39800a708f8fSGustavo F. Padovan 3981aef89f21SSzymon Janc err = l2cap_send_srejframe(chan, tx_seq); 3982aef89f21SSzymon Janc if (err < 0) { 3983aef89f21SSzymon Janc l2cap_send_disconn_req(chan->conn, chan, -err); 3984aef89f21SSzymon Janc return err; 3985aef89f21SSzymon Janc } 39860a708f8fSGustavo F. Padovan } 39870a708f8fSGustavo F. Padovan return 0; 39880a708f8fSGustavo F. Padovan 39890a708f8fSGustavo F. Padovan expected: 3990836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); 39910a708f8fSGustavo F. Padovan 3992e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 39930a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 39940a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 3995f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 39960a708f8fSGustavo F. Padovan return 0; 39970a708f8fSGustavo F. Padovan } 39980a708f8fSGustavo F. Padovan 399984084a31SMat Martineau err = l2cap_reassemble_sdu(chan, skb, rx_control); 4000836be934SAndrei Emeltchenko chan->buffer_seq = __next_seq(chan, chan->buffer_seq); 4001836be934SAndrei Emeltchenko 4002e328140fSMat Martineau if (err < 0) { 4003e328140fSMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 4004e328140fSMat Martineau return err; 4005e328140fSMat Martineau } 40060a708f8fSGustavo F. Padovan 400703f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control)) { 4008e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 4009525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 40100a708f8fSGustavo F. Padovan } 40110a708f8fSGustavo F. Padovan 40120a708f8fSGustavo F. Padovan 40136a026610SGustavo F. Padovan chan->num_acked = (chan->num_acked + 1) % num_to_ack; 40146a026610SGustavo F. Padovan if (chan->num_acked == num_to_ack - 1) 4015525cd185SGustavo F. Padovan l2cap_send_ack(chan); 40164d611e4dSGustavo F. Padovan else 40174d611e4dSGustavo F. Padovan __set_ack_timer(chan); 40180a708f8fSGustavo F. Padovan 40190a708f8fSGustavo F. Padovan return 0; 40200a708f8fSGustavo F. Padovan 40210a708f8fSGustavo F. Padovan drop: 40220a708f8fSGustavo F. Padovan kfree_skb(skb); 40230a708f8fSGustavo F. Padovan return 0; 40240a708f8fSGustavo F. Padovan } 40250a708f8fSGustavo F. Padovan 402688843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u32 rx_control) 40270a708f8fSGustavo F. Padovan { 402888843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, 40290b209faeSAndrei Emeltchenko __get_reqseq(chan, rx_control), rx_control); 40300a708f8fSGustavo F. Padovan 40310b209faeSAndrei Emeltchenko chan->expected_ack_seq = __get_reqseq(chan, rx_control); 403242e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 40330a708f8fSGustavo F. Padovan 4034e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) { 4035e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 4036e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 4037e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 40386a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 40391a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 40400a708f8fSGustavo F. Padovan 4041e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 4042525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 40430a708f8fSGustavo F. Padovan } else { 4044525cd185SGustavo F. Padovan l2cap_send_i_or_rr_or_rnr(chan); 40450a708f8fSGustavo F. Padovan } 40460a708f8fSGustavo F. Padovan 404703f6715dSAndrei Emeltchenko } else if (__is_ctrl_final(chan, rx_control)) { 4048e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 40490a708f8fSGustavo F. Padovan 4050e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 4051525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 40520a708f8fSGustavo F. Padovan 40530a708f8fSGustavo F. Padovan } else { 4054e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 40556a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 40561a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 40570a708f8fSGustavo F. Padovan 4058e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 4059e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) 4060525cd185SGustavo F. Padovan l2cap_send_ack(chan); 40610a708f8fSGustavo F. Padovan else 4062525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 40630a708f8fSGustavo F. Padovan } 40640a708f8fSGustavo F. Padovan } 40650a708f8fSGustavo F. Padovan 406688843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u32 rx_control) 40670a708f8fSGustavo F. Padovan { 40680b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 40690a708f8fSGustavo F. Padovan 407088843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); 40710a708f8fSGustavo F. Padovan 4072e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 40730a708f8fSGustavo F. Padovan 407442e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 407542e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 40760a708f8fSGustavo F. Padovan 407703f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control)) { 4078e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 4079525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 40800a708f8fSGustavo F. Padovan } else { 4081525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 40820a708f8fSGustavo F. Padovan 4083e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) 4084e2ab4353SGustavo F. Padovan set_bit(CONN_REJ_ACT, &chan->conn_state); 40850a708f8fSGustavo F. Padovan } 40860a708f8fSGustavo F. Padovan } 408788843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u32 rx_control) 40880a708f8fSGustavo F. Padovan { 40890b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 40900a708f8fSGustavo F. Padovan 409188843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); 40920a708f8fSGustavo F. Padovan 4093e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 40940a708f8fSGustavo F. Padovan 4095e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) { 409642e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 409742e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 40980a708f8fSGustavo F. Padovan 4099e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 4100525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 41010a708f8fSGustavo F. Padovan 4102525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 41030a708f8fSGustavo F. Padovan 4104e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) { 41056a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 4106e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_ACT, &chan->conn_state); 41070a708f8fSGustavo F. Padovan } 410803f6715dSAndrei Emeltchenko } else if (__is_ctrl_final(chan, rx_control)) { 4109e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_ACT, &chan->conn_state) && 41106a026610SGustavo F. Padovan chan->srej_save_reqseq == tx_seq) 4111e2ab4353SGustavo F. Padovan clear_bit(CONN_SREJ_ACT, &chan->conn_state); 41120a708f8fSGustavo F. Padovan else 4113525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 41140a708f8fSGustavo F. Padovan } else { 4115525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 4116e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) { 41176a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 4118e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_ACT, &chan->conn_state); 41190a708f8fSGustavo F. Padovan } 41200a708f8fSGustavo F. Padovan } 41210a708f8fSGustavo F. Padovan } 41220a708f8fSGustavo F. Padovan 412388843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u32 rx_control) 41240a708f8fSGustavo F. Padovan { 41250b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 41260a708f8fSGustavo F. Padovan 412788843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); 41280a708f8fSGustavo F. Padovan 4129e2ab4353SGustavo F. Padovan set_bit(CONN_REMOTE_BUSY, &chan->conn_state); 413042e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 413142e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 41320a708f8fSGustavo F. Padovan 4133e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) 4134e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 41350a708f8fSGustavo F. Padovan 4136e2ab4353SGustavo F. Padovan if (!test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 41371a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 4138e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) 4139525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); 41400a708f8fSGustavo F. Padovan return; 41410a708f8fSGustavo F. Padovan } 41420a708f8fSGustavo F. Padovan 4143e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) { 4144525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 4145ab784b73SAndrei Emeltchenko } else { 4146ab784b73SAndrei Emeltchenko rx_control = __set_ctrl_super(chan, L2CAP_SUPER_RR); 4147ab784b73SAndrei Emeltchenko l2cap_send_sframe(chan, rx_control); 4148ab784b73SAndrei Emeltchenko } 41490a708f8fSGustavo F. Padovan } 41500a708f8fSGustavo F. Padovan 415188843ab0SAndrei Emeltchenko static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb) 41520a708f8fSGustavo F. Padovan { 415388843ab0SAndrei Emeltchenko BT_DBG("chan %p rx_control 0x%8.8x len %d", chan, rx_control, skb->len); 41540a708f8fSGustavo F. Padovan 415503f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control) && 4156e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 41571a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 41586a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 41591a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 4160e2ab4353SGustavo F. Padovan clear_bit(CONN_WAIT_F, &chan->conn_state); 41610a708f8fSGustavo F. Padovan } 41620a708f8fSGustavo F. Padovan 4163ab784b73SAndrei Emeltchenko switch (__get_ctrl_super(chan, rx_control)) { 4164ab784b73SAndrei Emeltchenko case L2CAP_SUPER_RR: 4165525cd185SGustavo F. Padovan l2cap_data_channel_rrframe(chan, rx_control); 41660a708f8fSGustavo F. Padovan break; 41670a708f8fSGustavo F. Padovan 4168ab784b73SAndrei Emeltchenko case L2CAP_SUPER_REJ: 4169525cd185SGustavo F. Padovan l2cap_data_channel_rejframe(chan, rx_control); 41700a708f8fSGustavo F. Padovan break; 41710a708f8fSGustavo F. Padovan 4172ab784b73SAndrei Emeltchenko case L2CAP_SUPER_SREJ: 4173525cd185SGustavo F. Padovan l2cap_data_channel_srejframe(chan, rx_control); 41740a708f8fSGustavo F. Padovan break; 41750a708f8fSGustavo F. Padovan 4176ab784b73SAndrei Emeltchenko case L2CAP_SUPER_RNR: 4177525cd185SGustavo F. Padovan l2cap_data_channel_rnrframe(chan, rx_control); 41780a708f8fSGustavo F. Padovan break; 41790a708f8fSGustavo F. Padovan } 41800a708f8fSGustavo F. Padovan 41810a708f8fSGustavo F. Padovan kfree_skb(skb); 41820a708f8fSGustavo F. Padovan return 0; 41830a708f8fSGustavo F. Padovan } 41840a708f8fSGustavo F. Padovan 4185cad8f1d0SSzymon Janc static int l2cap_ertm_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) 41860a708f8fSGustavo F. Padovan { 418788843ab0SAndrei Emeltchenko u32 control; 41880b209faeSAndrei Emeltchenko u16 req_seq; 41890a708f8fSGustavo F. Padovan int len, next_tx_seq_offset, req_seq_offset; 41900a708f8fSGustavo F. Padovan 419188843ab0SAndrei Emeltchenko control = __get_control(chan, skb->data); 419288843ab0SAndrei Emeltchenko skb_pull(skb, __ctrl_size(chan)); 41930a708f8fSGustavo F. Padovan len = skb->len; 41940a708f8fSGustavo F. Padovan 41950a708f8fSGustavo F. Padovan /* 41960a708f8fSGustavo F. Padovan * We can just drop the corrupted I-frame here. 41970a708f8fSGustavo F. Padovan * Receiver will miss it and start proper recovery 41980a708f8fSGustavo F. Padovan * procedures and ask retransmission. 41990a708f8fSGustavo F. Padovan */ 420047d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 42010a708f8fSGustavo F. Padovan goto drop; 42020a708f8fSGustavo F. Padovan 4203793c2f1cSAndrei Emeltchenko if (__is_sar_start(chan, control) && !__is_sframe(chan, control)) 420403a51213SAndrei Emeltchenko len -= L2CAP_SDULEN_SIZE; 42050a708f8fSGustavo F. Padovan 420647d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 420703a51213SAndrei Emeltchenko len -= L2CAP_FCS_SIZE; 42080a708f8fSGustavo F. Padovan 420947d1ec61SGustavo F. Padovan if (len > chan->mps) { 42108c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 42110a708f8fSGustavo F. Padovan goto drop; 42120a708f8fSGustavo F. Padovan } 42130a708f8fSGustavo F. Padovan 42140b209faeSAndrei Emeltchenko req_seq = __get_reqseq(chan, control); 42150a708f8fSGustavo F. Padovan 4216836be934SAndrei Emeltchenko req_seq_offset = __seq_offset(chan, req_seq, chan->expected_ack_seq); 4217836be934SAndrei Emeltchenko 4218836be934SAndrei Emeltchenko next_tx_seq_offset = __seq_offset(chan, chan->next_tx_seq, 4219836be934SAndrei Emeltchenko chan->expected_ack_seq); 42200a708f8fSGustavo F. Padovan 42210a708f8fSGustavo F. Padovan /* check for invalid req-seq */ 42220a708f8fSGustavo F. Padovan if (req_seq_offset > next_tx_seq_offset) { 42238c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 42240a708f8fSGustavo F. Padovan goto drop; 42250a708f8fSGustavo F. Padovan } 42260a708f8fSGustavo F. Padovan 4227793c2f1cSAndrei Emeltchenko if (!__is_sframe(chan, control)) { 42280a708f8fSGustavo F. Padovan if (len < 0) { 42298c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 42300a708f8fSGustavo F. Padovan goto drop; 42310a708f8fSGustavo F. Padovan } 42320a708f8fSGustavo F. Padovan 4233525cd185SGustavo F. Padovan l2cap_data_channel_iframe(chan, control, skb); 42340a708f8fSGustavo F. Padovan } else { 42350a708f8fSGustavo F. Padovan if (len != 0) { 42360a708f8fSGustavo F. Padovan BT_ERR("%d", len); 42378c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 42380a708f8fSGustavo F. Padovan goto drop; 42390a708f8fSGustavo F. Padovan } 42400a708f8fSGustavo F. Padovan 4241525cd185SGustavo F. Padovan l2cap_data_channel_sframe(chan, control, skb); 42420a708f8fSGustavo F. Padovan } 42430a708f8fSGustavo F. Padovan 42440a708f8fSGustavo F. Padovan return 0; 42450a708f8fSGustavo F. Padovan 42460a708f8fSGustavo F. Padovan drop: 42470a708f8fSGustavo F. Padovan kfree_skb(skb); 42480a708f8fSGustavo F. Padovan return 0; 42490a708f8fSGustavo F. Padovan } 42500a708f8fSGustavo F. Padovan 42510a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) 42520a708f8fSGustavo F. Padovan { 425348454079SGustavo F. Padovan struct l2cap_chan *chan; 4254bf734843SDavid S. Miller struct sock *sk = NULL; 425588843ab0SAndrei Emeltchenko u32 control; 4256fb45de7dSAndrei Emeltchenko u16 tx_seq; 42570a708f8fSGustavo F. Padovan int len; 42580a708f8fSGustavo F. Padovan 4259baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 426048454079SGustavo F. Padovan if (!chan) { 42610a708f8fSGustavo F. Padovan BT_DBG("unknown cid 0x%4.4x", cid); 42620a708f8fSGustavo F. Padovan goto drop; 42630a708f8fSGustavo F. Padovan } 42640a708f8fSGustavo F. Padovan 426548454079SGustavo F. Padovan sk = chan->sk; 42663df91ea2SAndrei Emeltchenko lock_sock(sk); 42670a708f8fSGustavo F. Padovan 426849208c9cSGustavo F. Padovan BT_DBG("chan %p, len %d", chan, skb->len); 42690a708f8fSGustavo F. Padovan 427089bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 42710a708f8fSGustavo F. Padovan goto drop; 42720a708f8fSGustavo F. Padovan 42730c1bc5c6SGustavo F. Padovan switch (chan->mode) { 42740a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 42750a708f8fSGustavo F. Padovan /* If socket recv buffers overflows we drop data here 42760a708f8fSGustavo F. Padovan * which is *bad* because L2CAP has to be reliable. 42770a708f8fSGustavo F. Padovan * But we don't have any other choice. L2CAP doesn't 42780a708f8fSGustavo F. Padovan * provide flow control mechanism. */ 42790a708f8fSGustavo F. Padovan 42800c1bc5c6SGustavo F. Padovan if (chan->imtu < skb->len) 42810a708f8fSGustavo F. Padovan goto drop; 42820a708f8fSGustavo F. Padovan 428323070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 42840a708f8fSGustavo F. Padovan goto done; 42850a708f8fSGustavo F. Padovan break; 42860a708f8fSGustavo F. Padovan 42870a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 42885ef8cb9eSAndrei Emeltchenko l2cap_ertm_data_rcv(chan, skb); 42890a708f8fSGustavo F. Padovan 42900a708f8fSGustavo F. Padovan goto done; 42910a708f8fSGustavo F. Padovan 42920a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 429388843ab0SAndrei Emeltchenko control = __get_control(chan, skb->data); 429488843ab0SAndrei Emeltchenko skb_pull(skb, __ctrl_size(chan)); 42950a708f8fSGustavo F. Padovan len = skb->len; 42960a708f8fSGustavo F. Padovan 429747d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 42980a708f8fSGustavo F. Padovan goto drop; 42990a708f8fSGustavo F. Padovan 43007e0ef6eeSAndrei Emeltchenko if (__is_sar_start(chan, control)) 430103a51213SAndrei Emeltchenko len -= L2CAP_SDULEN_SIZE; 43020a708f8fSGustavo F. Padovan 430347d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 430403a51213SAndrei Emeltchenko len -= L2CAP_FCS_SIZE; 43050a708f8fSGustavo F. Padovan 4306793c2f1cSAndrei Emeltchenko if (len > chan->mps || len < 0 || __is_sframe(chan, control)) 43070a708f8fSGustavo F. Padovan goto drop; 43080a708f8fSGustavo F. Padovan 4309fb45de7dSAndrei Emeltchenko tx_seq = __get_txseq(chan, control); 43100a708f8fSGustavo F. Padovan 431184084a31SMat Martineau if (chan->expected_tx_seq != tx_seq) { 431284084a31SMat Martineau /* Frame(s) missing - must discard partial SDU */ 431384084a31SMat Martineau kfree_skb(chan->sdu); 431484084a31SMat Martineau chan->sdu = NULL; 431584084a31SMat Martineau chan->sdu_last_frag = NULL; 431684084a31SMat Martineau chan->sdu_len = 0; 431784084a31SMat Martineau 431884084a31SMat Martineau /* TODO: Notify userland of missing data */ 431984084a31SMat Martineau } 432084084a31SMat Martineau 4321836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, tx_seq); 43220a708f8fSGustavo F. Padovan 432384084a31SMat Martineau if (l2cap_reassemble_sdu(chan, skb, control) == -EMSGSIZE) 432484084a31SMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 43250a708f8fSGustavo F. Padovan 43260a708f8fSGustavo F. Padovan goto done; 43270a708f8fSGustavo F. Padovan 43280a708f8fSGustavo F. Padovan default: 43290c1bc5c6SGustavo F. Padovan BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode); 43300a708f8fSGustavo F. Padovan break; 43310a708f8fSGustavo F. Padovan } 43320a708f8fSGustavo F. Padovan 43330a708f8fSGustavo F. Padovan drop: 43340a708f8fSGustavo F. Padovan kfree_skb(skb); 43350a708f8fSGustavo F. Padovan 43360a708f8fSGustavo F. Padovan done: 43370a708f8fSGustavo F. Padovan if (sk) 4338aa2ac881SGustavo F. Padovan release_sock(sk); 43390a708f8fSGustavo F. Padovan 43400a708f8fSGustavo F. Padovan return 0; 43410a708f8fSGustavo F. Padovan } 43420a708f8fSGustavo F. Padovan 43430a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb) 43440a708f8fSGustavo F. Padovan { 43456dcae1eaSDavid S. Miller struct sock *sk = NULL; 434623691d75SGustavo F. Padovan struct l2cap_chan *chan; 43470a708f8fSGustavo F. Padovan 434823691d75SGustavo F. Padovan chan = l2cap_global_chan_by_psm(0, psm, conn->src); 434923691d75SGustavo F. Padovan if (!chan) 43500a708f8fSGustavo F. Padovan goto drop; 43510a708f8fSGustavo F. Padovan 435223691d75SGustavo F. Padovan sk = chan->sk; 435323691d75SGustavo F. Padovan 4354aa2ac881SGustavo F. Padovan lock_sock(sk); 43550a708f8fSGustavo F. Padovan 43560a708f8fSGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 43570a708f8fSGustavo F. Padovan 435889bc500eSGustavo F. Padovan if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) 43590a708f8fSGustavo F. Padovan goto drop; 43600a708f8fSGustavo F. Padovan 4361e13e21dcSVinicius Costa Gomes if (chan->imtu < skb->len) 43620a708f8fSGustavo F. Padovan goto drop; 43630a708f8fSGustavo F. Padovan 436423070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 43650a708f8fSGustavo F. Padovan goto done; 43660a708f8fSGustavo F. Padovan 43670a708f8fSGustavo F. Padovan drop: 43680a708f8fSGustavo F. Padovan kfree_skb(skb); 43690a708f8fSGustavo F. Padovan 43700a708f8fSGustavo F. Padovan done: 43710a708f8fSGustavo F. Padovan if (sk) 4372aa2ac881SGustavo F. Padovan release_sock(sk); 43730a708f8fSGustavo F. Padovan return 0; 43740a708f8fSGustavo F. Padovan } 43750a708f8fSGustavo F. Padovan 43769f69bda6SGustavo F. Padovan static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb) 43779f69bda6SGustavo F. Padovan { 43786dcae1eaSDavid S. Miller struct sock *sk = NULL; 437923691d75SGustavo F. Padovan struct l2cap_chan *chan; 43809f69bda6SGustavo F. Padovan 438123691d75SGustavo F. Padovan chan = l2cap_global_chan_by_scid(0, cid, conn->src); 438223691d75SGustavo F. Padovan if (!chan) 43839f69bda6SGustavo F. Padovan goto drop; 43849f69bda6SGustavo F. Padovan 438523691d75SGustavo F. Padovan sk = chan->sk; 438623691d75SGustavo F. Padovan 4387aa2ac881SGustavo F. Padovan lock_sock(sk); 43889f69bda6SGustavo F. Padovan 43899f69bda6SGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 43909f69bda6SGustavo F. Padovan 439189bc500eSGustavo F. Padovan if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) 43929f69bda6SGustavo F. Padovan goto drop; 43939f69bda6SGustavo F. Padovan 4394e13e21dcSVinicius Costa Gomes if (chan->imtu < skb->len) 43959f69bda6SGustavo F. Padovan goto drop; 43969f69bda6SGustavo F. Padovan 439723070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 43989f69bda6SGustavo F. Padovan goto done; 43999f69bda6SGustavo F. Padovan 44009f69bda6SGustavo F. Padovan drop: 44019f69bda6SGustavo F. Padovan kfree_skb(skb); 44029f69bda6SGustavo F. Padovan 44039f69bda6SGustavo F. Padovan done: 44049f69bda6SGustavo F. Padovan if (sk) 4405aa2ac881SGustavo F. Padovan release_sock(sk); 44069f69bda6SGustavo F. Padovan return 0; 44079f69bda6SGustavo F. Padovan } 44089f69bda6SGustavo F. Padovan 44090a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) 44100a708f8fSGustavo F. Padovan { 44110a708f8fSGustavo F. Padovan struct l2cap_hdr *lh = (void *) skb->data; 44120a708f8fSGustavo F. Padovan u16 cid, len; 44130a708f8fSGustavo F. Padovan __le16 psm; 44140a708f8fSGustavo F. Padovan 44150a708f8fSGustavo F. Padovan skb_pull(skb, L2CAP_HDR_SIZE); 44160a708f8fSGustavo F. Padovan cid = __le16_to_cpu(lh->cid); 44170a708f8fSGustavo F. Padovan len = __le16_to_cpu(lh->len); 44180a708f8fSGustavo F. Padovan 44190a708f8fSGustavo F. Padovan if (len != skb->len) { 44200a708f8fSGustavo F. Padovan kfree_skb(skb); 44210a708f8fSGustavo F. Padovan return; 44220a708f8fSGustavo F. Padovan } 44230a708f8fSGustavo F. Padovan 44240a708f8fSGustavo F. Padovan BT_DBG("len %d, cid 0x%4.4x", len, cid); 44250a708f8fSGustavo F. Padovan 44260a708f8fSGustavo F. Padovan switch (cid) { 44273300d9a9SClaudio Takahasi case L2CAP_CID_LE_SIGNALING: 44280a708f8fSGustavo F. Padovan case L2CAP_CID_SIGNALING: 44290a708f8fSGustavo F. Padovan l2cap_sig_channel(conn, skb); 44300a708f8fSGustavo F. Padovan break; 44310a708f8fSGustavo F. Padovan 44320a708f8fSGustavo F. Padovan case L2CAP_CID_CONN_LESS: 44330a708f8fSGustavo F. Padovan psm = get_unaligned_le16(skb->data); 44340a708f8fSGustavo F. Padovan skb_pull(skb, 2); 44350a708f8fSGustavo F. Padovan l2cap_conless_channel(conn, psm, skb); 44360a708f8fSGustavo F. Padovan break; 44370a708f8fSGustavo F. Padovan 44389f69bda6SGustavo F. Padovan case L2CAP_CID_LE_DATA: 44399f69bda6SGustavo F. Padovan l2cap_att_channel(conn, cid, skb); 44409f69bda6SGustavo F. Padovan break; 44419f69bda6SGustavo F. Padovan 4442b501d6a1SAnderson Briglia case L2CAP_CID_SMP: 4443b501d6a1SAnderson Briglia if (smp_sig_channel(conn, skb)) 4444b501d6a1SAnderson Briglia l2cap_conn_del(conn->hcon, EACCES); 4445b501d6a1SAnderson Briglia break; 4446b501d6a1SAnderson Briglia 44470a708f8fSGustavo F. Padovan default: 44480a708f8fSGustavo F. Padovan l2cap_data_channel(conn, cid, skb); 44490a708f8fSGustavo F. Padovan break; 44500a708f8fSGustavo F. Padovan } 44510a708f8fSGustavo F. Padovan } 44520a708f8fSGustavo F. Padovan 44530a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */ 44540a708f8fSGustavo F. Padovan 4455686ebf28SUlisses Furquim int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) 44560a708f8fSGustavo F. Padovan { 44570a708f8fSGustavo F. Padovan int exact = 0, lm1 = 0, lm2 = 0; 445823691d75SGustavo F. Padovan struct l2cap_chan *c; 44590a708f8fSGustavo F. Padovan 44600a708f8fSGustavo F. Padovan BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); 44610a708f8fSGustavo F. Padovan 44620a708f8fSGustavo F. Padovan /* Find listening sockets and check their link_mode */ 446323691d75SGustavo F. Padovan read_lock(&chan_list_lock); 446423691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 446523691d75SGustavo F. Padovan struct sock *sk = c->sk; 44664343478fSGustavo F. Padovan 446789bc500eSGustavo F. Padovan if (c->state != BT_LISTEN) 44680a708f8fSGustavo F. Padovan continue; 44690a708f8fSGustavo F. Padovan 44700a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { 44710a708f8fSGustavo F. Padovan lm1 |= HCI_LM_ACCEPT; 447243bd0f32SAndrei Emeltchenko if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 44730a708f8fSGustavo F. Padovan lm1 |= HCI_LM_MASTER; 44740a708f8fSGustavo F. Padovan exact++; 44750a708f8fSGustavo F. Padovan } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { 44760a708f8fSGustavo F. Padovan lm2 |= HCI_LM_ACCEPT; 447743bd0f32SAndrei Emeltchenko if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 44780a708f8fSGustavo F. Padovan lm2 |= HCI_LM_MASTER; 44790a708f8fSGustavo F. Padovan } 44800a708f8fSGustavo F. Padovan } 448123691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 44820a708f8fSGustavo F. Padovan 44830a708f8fSGustavo F. Padovan return exact ? lm1 : lm2; 44840a708f8fSGustavo F. Padovan } 44850a708f8fSGustavo F. Padovan 4486686ebf28SUlisses Furquim int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) 44870a708f8fSGustavo F. Padovan { 44880a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 44890a708f8fSGustavo F. Padovan 44900a708f8fSGustavo F. Padovan BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); 44910a708f8fSGustavo F. Padovan 44920a708f8fSGustavo F. Padovan if (!status) { 44930a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, status); 44940a708f8fSGustavo F. Padovan if (conn) 44950a708f8fSGustavo F. Padovan l2cap_conn_ready(conn); 44960a708f8fSGustavo F. Padovan } else 4497e175072fSJoe Perches l2cap_conn_del(hcon, bt_to_errno(status)); 44980a708f8fSGustavo F. Padovan 44990a708f8fSGustavo F. Padovan return 0; 45000a708f8fSGustavo F. Padovan } 45010a708f8fSGustavo F. Padovan 4502686ebf28SUlisses Furquim int l2cap_disconn_ind(struct hci_conn *hcon) 45030a708f8fSGustavo F. Padovan { 45040a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 45050a708f8fSGustavo F. Padovan 45060a708f8fSGustavo F. Padovan BT_DBG("hcon %p", hcon); 45070a708f8fSGustavo F. Padovan 4508686ebf28SUlisses Furquim if (!conn) 45099f5a0d7bSAndrei Emeltchenko return HCI_ERROR_REMOTE_USER_TERM; 45100a708f8fSGustavo F. Padovan return conn->disc_reason; 45110a708f8fSGustavo F. Padovan } 45120a708f8fSGustavo F. Padovan 4513686ebf28SUlisses Furquim int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) 45140a708f8fSGustavo F. Padovan { 45150a708f8fSGustavo F. Padovan BT_DBG("hcon %p reason %d", hcon, reason); 45160a708f8fSGustavo F. Padovan 4517e175072fSJoe Perches l2cap_conn_del(hcon, bt_to_errno(reason)); 45180a708f8fSGustavo F. Padovan return 0; 45190a708f8fSGustavo F. Padovan } 45200a708f8fSGustavo F. Padovan 45214343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) 45220a708f8fSGustavo F. Padovan { 4523715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) 45240a708f8fSGustavo F. Padovan return; 45250a708f8fSGustavo F. Padovan 45260a708f8fSGustavo F. Padovan if (encrypt == 0x00) { 45274343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) { 4528c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 4529b83ddfe2SAndrzej Kaczmarek __set_chan_timer(chan, 4530b83ddfe2SAndrzej Kaczmarek msecs_to_jiffies(L2CAP_ENC_TIMEOUT)); 45314343478fSGustavo F. Padovan } else if (chan->sec_level == BT_SECURITY_HIGH) 45320f852724SGustavo F. Padovan l2cap_chan_close(chan, ECONNREFUSED); 45330a708f8fSGustavo F. Padovan } else { 45344343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) 4535c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 45360a708f8fSGustavo F. Padovan } 45370a708f8fSGustavo F. Padovan } 45380a708f8fSGustavo F. Padovan 4539686ebf28SUlisses Furquim int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) 45400a708f8fSGustavo F. Padovan { 45410a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 454248454079SGustavo F. Padovan struct l2cap_chan *chan; 45430a708f8fSGustavo F. Padovan 45440a708f8fSGustavo F. Padovan if (!conn) 45450a708f8fSGustavo F. Padovan return 0; 45460a708f8fSGustavo F. Padovan 45470a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 45480a708f8fSGustavo F. Padovan 4549160dc6acSVinicius Costa Gomes if (hcon->type == LE_LINK) { 4550160dc6acSVinicius Costa Gomes smp_distribute_keys(conn, 0); 455117cd3f37SUlisses Furquim cancel_delayed_work(&conn->security_timer); 4552160dc6acSVinicius Costa Gomes } 4553160dc6acSVinicius Costa Gomes 45543df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 45550a708f8fSGustavo F. Padovan 45563df91ea2SAndrei Emeltchenko list_for_each_entry(chan, &conn->chan_l, list) { 455748454079SGustavo F. Padovan struct sock *sk = chan->sk; 4558baa7e1faSGustavo F. Padovan 45590a708f8fSGustavo F. Padovan bh_lock_sock(sk); 45600a708f8fSGustavo F. Padovan 4561f1cb9af5SVinicius Costa Gomes BT_DBG("chan->scid %d", chan->scid); 4562f1cb9af5SVinicius Costa Gomes 4563f1cb9af5SVinicius Costa Gomes if (chan->scid == L2CAP_CID_LE_DATA) { 4564f1cb9af5SVinicius Costa Gomes if (!status && encrypt) { 4565f1cb9af5SVinicius Costa Gomes chan->sec_level = hcon->sec_level; 4566cf4cd009SAndrei Emeltchenko l2cap_chan_ready(chan); 4567f1cb9af5SVinicius Costa Gomes } 4568f1cb9af5SVinicius Costa Gomes 4569f1cb9af5SVinicius Costa Gomes bh_unlock_sock(sk); 4570f1cb9af5SVinicius Costa Gomes continue; 4571f1cb9af5SVinicius Costa Gomes } 4572f1cb9af5SVinicius Costa Gomes 4573c1360a1cSGustavo F. Padovan if (test_bit(CONF_CONNECT_PEND, &chan->conf_state)) { 45740a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 45750a708f8fSGustavo F. Padovan continue; 45760a708f8fSGustavo F. Padovan } 45770a708f8fSGustavo F. Padovan 457889bc500eSGustavo F. Padovan if (!status && (chan->state == BT_CONNECTED || 457989bc500eSGustavo F. Padovan chan->state == BT_CONFIG)) { 45804343478fSGustavo F. Padovan l2cap_check_encryption(chan, encrypt); 45810a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 45820a708f8fSGustavo F. Padovan continue; 45830a708f8fSGustavo F. Padovan } 45840a708f8fSGustavo F. Padovan 458589bc500eSGustavo F. Padovan if (chan->state == BT_CONNECT) { 45860a708f8fSGustavo F. Padovan if (!status) { 45870a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 4588fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 4589fe4128e0SGustavo F. Padovan req.psm = chan->psm; 45900a708f8fSGustavo F. Padovan 4591fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 4592c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 45930a708f8fSGustavo F. Padovan 4594fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 45950a708f8fSGustavo F. Padovan L2CAP_CONN_REQ, sizeof(req), &req); 45960a708f8fSGustavo F. Padovan } else { 4597c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 4598b83ddfe2SAndrzej Kaczmarek __set_chan_timer(chan, 4599b83ddfe2SAndrzej Kaczmarek msecs_to_jiffies(L2CAP_DISC_TIMEOUT)); 46000a708f8fSGustavo F. Padovan } 460189bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT2) { 46020a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 4603df3c3931SJohan Hedberg __u16 res, stat; 46040a708f8fSGustavo F. Padovan 46050a708f8fSGustavo F. Padovan if (!status) { 4606df3c3931SJohan Hedberg if (bt_sk(sk)->defer_setup) { 4607df3c3931SJohan Hedberg struct sock *parent = bt_sk(sk)->parent; 4608df3c3931SJohan Hedberg res = L2CAP_CR_PEND; 4609df3c3931SJohan Hedberg stat = L2CAP_CS_AUTHOR_PEND; 461005e9a2f6SIlia Kolomisnky if (parent) 4611df3c3931SJohan Hedberg parent->sk_data_ready(parent, 0); 4612df3c3931SJohan Hedberg } else { 46130e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONFIG); 4614df3c3931SJohan Hedberg res = L2CAP_CR_SUCCESS; 4615df3c3931SJohan Hedberg stat = L2CAP_CS_NO_INFO; 4616df3c3931SJohan Hedberg } 46170a708f8fSGustavo F. Padovan } else { 46180e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_DISCONN); 4619b83ddfe2SAndrzej Kaczmarek __set_chan_timer(chan, 4620b83ddfe2SAndrzej Kaczmarek msecs_to_jiffies(L2CAP_DISC_TIMEOUT)); 4621df3c3931SJohan Hedberg res = L2CAP_CR_SEC_BLOCK; 4622df3c3931SJohan Hedberg stat = L2CAP_CS_NO_INFO; 46230a708f8fSGustavo F. Padovan } 46240a708f8fSGustavo F. Padovan 4625fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4626fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 4627df3c3931SJohan Hedberg rsp.result = cpu_to_le16(res); 4628df3c3931SJohan Hedberg rsp.status = cpu_to_le16(stat); 4629fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4630fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 46310a708f8fSGustavo F. Padovan } 46320a708f8fSGustavo F. Padovan 46330a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 46340a708f8fSGustavo F. Padovan } 46350a708f8fSGustavo F. Padovan 46363df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 46370a708f8fSGustavo F. Padovan 46380a708f8fSGustavo F. Padovan return 0; 46390a708f8fSGustavo F. Padovan } 46400a708f8fSGustavo F. Padovan 4641686ebf28SUlisses Furquim int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) 46420a708f8fSGustavo F. Padovan { 46430a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 46440a708f8fSGustavo F. Padovan 46450a708f8fSGustavo F. Padovan if (!conn) 46460a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 46470a708f8fSGustavo F. Padovan 46480a708f8fSGustavo F. Padovan if (!conn) 46490a708f8fSGustavo F. Padovan goto drop; 46500a708f8fSGustavo F. Padovan 46510a708f8fSGustavo F. Padovan BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); 46520a708f8fSGustavo F. Padovan 46530a708f8fSGustavo F. Padovan if (!(flags & ACL_CONT)) { 46540a708f8fSGustavo F. Padovan struct l2cap_hdr *hdr; 465548454079SGustavo F. Padovan struct l2cap_chan *chan; 46560a708f8fSGustavo F. Padovan u16 cid; 46570a708f8fSGustavo F. Padovan int len; 46580a708f8fSGustavo F. Padovan 46590a708f8fSGustavo F. Padovan if (conn->rx_len) { 46600a708f8fSGustavo F. Padovan BT_ERR("Unexpected start frame (len %d)", skb->len); 46610a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 46620a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 46630a708f8fSGustavo F. Padovan conn->rx_len = 0; 46640a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46650a708f8fSGustavo F. Padovan } 46660a708f8fSGustavo F. Padovan 46670a708f8fSGustavo F. Padovan /* Start fragment always begin with Basic L2CAP header */ 46680a708f8fSGustavo F. Padovan if (skb->len < L2CAP_HDR_SIZE) { 46690a708f8fSGustavo F. Padovan BT_ERR("Frame is too short (len %d)", skb->len); 46700a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46710a708f8fSGustavo F. Padovan goto drop; 46720a708f8fSGustavo F. Padovan } 46730a708f8fSGustavo F. Padovan 46740a708f8fSGustavo F. Padovan hdr = (struct l2cap_hdr *) skb->data; 46750a708f8fSGustavo F. Padovan len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; 46760a708f8fSGustavo F. Padovan cid = __le16_to_cpu(hdr->cid); 46770a708f8fSGustavo F. Padovan 46780a708f8fSGustavo F. Padovan if (len == skb->len) { 46790a708f8fSGustavo F. Padovan /* Complete frame received */ 46800a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, skb); 46810a708f8fSGustavo F. Padovan return 0; 46820a708f8fSGustavo F. Padovan } 46830a708f8fSGustavo F. Padovan 46840a708f8fSGustavo F. Padovan BT_DBG("Start: total len %d, frag len %d", len, skb->len); 46850a708f8fSGustavo F. Padovan 46860a708f8fSGustavo F. Padovan if (skb->len > len) { 46870a708f8fSGustavo F. Padovan BT_ERR("Frame is too long (len %d, expected len %d)", 46880a708f8fSGustavo F. Padovan skb->len, len); 46890a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46900a708f8fSGustavo F. Padovan goto drop; 46910a708f8fSGustavo F. Padovan } 46920a708f8fSGustavo F. Padovan 4693baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 46940a708f8fSGustavo F. Padovan 469548454079SGustavo F. Padovan if (chan && chan->sk) { 469648454079SGustavo F. Padovan struct sock *sk = chan->sk; 46973df91ea2SAndrei Emeltchenko lock_sock(sk); 469848454079SGustavo F. Padovan 46990c1bc5c6SGustavo F. Padovan if (chan->imtu < len - L2CAP_HDR_SIZE) { 470048454079SGustavo F. Padovan BT_ERR("Frame exceeding recv MTU (len %d, " 470148454079SGustavo F. Padovan "MTU %d)", len, 47020c1bc5c6SGustavo F. Padovan chan->imtu); 4703aa2ac881SGustavo F. Padovan release_sock(sk); 47040a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 47050a708f8fSGustavo F. Padovan goto drop; 47060a708f8fSGustavo F. Padovan } 4707aa2ac881SGustavo F. Padovan release_sock(sk); 470848454079SGustavo F. Padovan } 47090a708f8fSGustavo F. Padovan 47100a708f8fSGustavo F. Padovan /* Allocate skb for the complete frame (with header) */ 47110a708f8fSGustavo F. Padovan conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); 47120a708f8fSGustavo F. Padovan if (!conn->rx_skb) 47130a708f8fSGustavo F. Padovan goto drop; 47140a708f8fSGustavo F. Padovan 47150a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 47160a708f8fSGustavo F. Padovan skb->len); 47170a708f8fSGustavo F. Padovan conn->rx_len = len - skb->len; 47180a708f8fSGustavo F. Padovan } else { 47190a708f8fSGustavo F. Padovan BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); 47200a708f8fSGustavo F. Padovan 47210a708f8fSGustavo F. Padovan if (!conn->rx_len) { 47220a708f8fSGustavo F. Padovan BT_ERR("Unexpected continuation frame (len %d)", skb->len); 47230a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 47240a708f8fSGustavo F. Padovan goto drop; 47250a708f8fSGustavo F. Padovan } 47260a708f8fSGustavo F. Padovan 47270a708f8fSGustavo F. Padovan if (skb->len > conn->rx_len) { 47280a708f8fSGustavo F. Padovan BT_ERR("Fragment is too long (len %d, expected %d)", 47290a708f8fSGustavo F. Padovan skb->len, conn->rx_len); 47300a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 47310a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 47320a708f8fSGustavo F. Padovan conn->rx_len = 0; 47330a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 47340a708f8fSGustavo F. Padovan goto drop; 47350a708f8fSGustavo F. Padovan } 47360a708f8fSGustavo F. Padovan 47370a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 47380a708f8fSGustavo F. Padovan skb->len); 47390a708f8fSGustavo F. Padovan conn->rx_len -= skb->len; 47400a708f8fSGustavo F. Padovan 47410a708f8fSGustavo F. Padovan if (!conn->rx_len) { 47420a708f8fSGustavo F. Padovan /* Complete frame received */ 47430a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, conn->rx_skb); 47440a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 47450a708f8fSGustavo F. Padovan } 47460a708f8fSGustavo F. Padovan } 47470a708f8fSGustavo F. Padovan 47480a708f8fSGustavo F. Padovan drop: 47490a708f8fSGustavo F. Padovan kfree_skb(skb); 47500a708f8fSGustavo F. Padovan return 0; 47510a708f8fSGustavo F. Padovan } 47520a708f8fSGustavo F. Padovan 47530a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p) 47540a708f8fSGustavo F. Padovan { 475523691d75SGustavo F. Padovan struct l2cap_chan *c; 47560a708f8fSGustavo F. Padovan 4757333055f2SGustavo F. Padovan read_lock(&chan_list_lock); 47580a708f8fSGustavo F. Padovan 475923691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 476023691d75SGustavo F. Padovan struct sock *sk = c->sk; 47610a708f8fSGustavo F. Padovan 4762903d343eSGustavo F. Padovan seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", 47630a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->src), 47640a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->dst), 476589bc500eSGustavo F. Padovan c->state, __le16_to_cpu(c->psm), 476623691d75SGustavo F. Padovan c->scid, c->dcid, c->imtu, c->omtu, 476723691d75SGustavo F. Padovan c->sec_level, c->mode); 47680a708f8fSGustavo F. Padovan } 47690a708f8fSGustavo F. Padovan 4770333055f2SGustavo F. Padovan read_unlock(&chan_list_lock); 47710a708f8fSGustavo F. Padovan 47720a708f8fSGustavo F. Padovan return 0; 47730a708f8fSGustavo F. Padovan } 47740a708f8fSGustavo F. Padovan 47750a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file) 47760a708f8fSGustavo F. Padovan { 47770a708f8fSGustavo F. Padovan return single_open(file, l2cap_debugfs_show, inode->i_private); 47780a708f8fSGustavo F. Padovan } 47790a708f8fSGustavo F. Padovan 47800a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = { 47810a708f8fSGustavo F. Padovan .open = l2cap_debugfs_open, 47820a708f8fSGustavo F. Padovan .read = seq_read, 47830a708f8fSGustavo F. Padovan .llseek = seq_lseek, 47840a708f8fSGustavo F. Padovan .release = single_release, 47850a708f8fSGustavo F. Padovan }; 47860a708f8fSGustavo F. Padovan 47870a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs; 47880a708f8fSGustavo F. Padovan 478964274518SGustavo F. Padovan int __init l2cap_init(void) 47900a708f8fSGustavo F. Padovan { 47910a708f8fSGustavo F. Padovan int err; 47920a708f8fSGustavo F. Padovan 4793bb58f747SGustavo F. Padovan err = l2cap_init_sockets(); 47940a708f8fSGustavo F. Padovan if (err < 0) 47950a708f8fSGustavo F. Padovan return err; 47960a708f8fSGustavo F. Padovan 47970a708f8fSGustavo F. Padovan if (bt_debugfs) { 47980a708f8fSGustavo F. Padovan l2cap_debugfs = debugfs_create_file("l2cap", 0444, 47990a708f8fSGustavo F. Padovan bt_debugfs, NULL, &l2cap_debugfs_fops); 48000a708f8fSGustavo F. Padovan if (!l2cap_debugfs) 48010a708f8fSGustavo F. Padovan BT_ERR("Failed to create L2CAP debug file"); 48020a708f8fSGustavo F. Padovan } 48030a708f8fSGustavo F. Padovan 48040a708f8fSGustavo F. Padovan return 0; 48050a708f8fSGustavo F. Padovan } 48060a708f8fSGustavo F. Padovan 480764274518SGustavo F. Padovan void l2cap_exit(void) 48080a708f8fSGustavo F. Padovan { 48090a708f8fSGustavo F. Padovan debugfs_remove(l2cap_debugfs); 4810bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 48110a708f8fSGustavo F. Padovan } 48120a708f8fSGustavo F. Padovan 48130a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644); 48140a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); 4815