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 220721c4181SGustavo F. Padovan static void l2cap_chan_timeout(struct work_struct *work) 221ab07801dSGustavo F. Padovan { 222721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 223721c4181SGustavo F. Padovan chan_timer.work); 2243df91ea2SAndrei Emeltchenko struct l2cap_conn *conn = chan->conn; 225ab07801dSGustavo F. Padovan struct sock *sk = chan->sk; 226ab07801dSGustavo F. Padovan int reason; 227ab07801dSGustavo F. Padovan 228e05dcc32SAndrei Emeltchenko BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); 229ab07801dSGustavo F. Padovan 2303df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 231721c4181SGustavo F. Padovan lock_sock(sk); 232ab07801dSGustavo F. Padovan 23389bc500eSGustavo F. Padovan if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) 234ab07801dSGustavo F. Padovan reason = ECONNREFUSED; 23589bc500eSGustavo F. Padovan else if (chan->state == BT_CONNECT && 236ab07801dSGustavo F. Padovan chan->sec_level != BT_SECURITY_SDP) 237ab07801dSGustavo F. Padovan reason = ECONNREFUSED; 238ab07801dSGustavo F. Padovan else 239ab07801dSGustavo F. Padovan reason = ETIMEDOUT; 240ab07801dSGustavo F. Padovan 2410f852724SGustavo F. Padovan l2cap_chan_close(chan, reason); 242ab07801dSGustavo F. Padovan 243721c4181SGustavo F. Padovan release_sock(sk); 244ab07801dSGustavo F. Padovan 245ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 2463df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 2473df91ea2SAndrei Emeltchenko 248371fd835SUlisses Furquim l2cap_chan_put(chan); 249ab07801dSGustavo F. Padovan } 250ab07801dSGustavo F. Padovan 25123691d75SGustavo F. Padovan struct l2cap_chan *l2cap_chan_create(struct sock *sk) 2520a708f8fSGustavo F. Padovan { 25348454079SGustavo F. Padovan struct l2cap_chan *chan; 2540a708f8fSGustavo F. Padovan 25548454079SGustavo F. Padovan chan = kzalloc(sizeof(*chan), GFP_ATOMIC); 25648454079SGustavo F. Padovan if (!chan) 25748454079SGustavo F. Padovan return NULL; 2580a708f8fSGustavo F. Padovan 259c03b355eSAndrei Emeltchenko mutex_init(&chan->lock); 260c03b355eSAndrei Emeltchenko 26148454079SGustavo F. Padovan chan->sk = sk; 26248454079SGustavo F. Padovan 263333055f2SGustavo F. Padovan write_lock(&chan_list_lock); 26423691d75SGustavo F. Padovan list_add(&chan->global_l, &chan_list); 265333055f2SGustavo F. Padovan write_unlock(&chan_list_lock); 26623691d75SGustavo F. Padovan 267721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); 268ab07801dSGustavo F. Padovan 26989bc500eSGustavo F. Padovan chan->state = BT_OPEN; 27089bc500eSGustavo F. Padovan 27171ba0e56SGustavo F. Padovan atomic_set(&chan->refcnt, 1); 27271ba0e56SGustavo F. Padovan 273abc545b8SSzymon Janc BT_DBG("sk %p chan %p", sk, chan); 274abc545b8SSzymon Janc 27548454079SGustavo F. Padovan return chan; 2760a708f8fSGustavo F. Padovan } 2770a708f8fSGustavo F. Padovan 27823691d75SGustavo F. Padovan void l2cap_chan_destroy(struct l2cap_chan *chan) 2796ff5abbfSGustavo F. Padovan { 280333055f2SGustavo F. Padovan write_lock(&chan_list_lock); 28123691d75SGustavo F. Padovan list_del(&chan->global_l); 282333055f2SGustavo F. Padovan write_unlock(&chan_list_lock); 28323691d75SGustavo F. Padovan 284371fd835SUlisses Furquim l2cap_chan_put(chan); 2856ff5abbfSGustavo F. Padovan } 2866ff5abbfSGustavo F. Padovan 2873d57dc68SGustavo F. Padovan static void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 2880a708f8fSGustavo F. Padovan { 2890a708f8fSGustavo F. Padovan BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, 290fe4128e0SGustavo F. Padovan chan->psm, chan->dcid); 2910a708f8fSGustavo F. Padovan 2929f5a0d7bSAndrei Emeltchenko conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; 2930a708f8fSGustavo F. Padovan 2948c1d787bSGustavo F. Padovan chan->conn = conn; 2950a708f8fSGustavo F. Padovan 2965491120eSAndrei Emeltchenko switch (chan->chan_type) { 2975491120eSAndrei Emeltchenko case L2CAP_CHAN_CONN_ORIENTED: 298b62f328bSVille Tervo if (conn->hcon->type == LE_LINK) { 299b62f328bSVille Tervo /* LE connection */ 3000c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_LE_DEFAULT_MTU; 301fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_LE_DATA; 302fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_LE_DATA; 303b62f328bSVille Tervo } else { 3040a708f8fSGustavo F. Padovan /* Alloc CID for connection-oriented socket */ 305fe4128e0SGustavo F. Padovan chan->scid = l2cap_alloc_cid(conn); 3060c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 307b62f328bSVille Tervo } 3085491120eSAndrei Emeltchenko break; 3095491120eSAndrei Emeltchenko 3105491120eSAndrei Emeltchenko case L2CAP_CHAN_CONN_LESS: 3110a708f8fSGustavo F. Padovan /* Connectionless socket */ 312fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_CONN_LESS; 313fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_CONN_LESS; 3140c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 3155491120eSAndrei Emeltchenko break; 3165491120eSAndrei Emeltchenko 3175491120eSAndrei Emeltchenko default: 3180a708f8fSGustavo F. Padovan /* Raw socket can send/recv signalling messages only */ 319fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_SIGNALING; 320fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_SIGNALING; 3210c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 3220a708f8fSGustavo F. Padovan } 3230a708f8fSGustavo F. Padovan 3248f7975b1SAndrei Emeltchenko chan->local_id = L2CAP_BESTEFFORT_ID; 3258f7975b1SAndrei Emeltchenko chan->local_stype = L2CAP_SERV_BESTEFFORT; 3268f7975b1SAndrei Emeltchenko chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; 3278f7975b1SAndrei Emeltchenko chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; 3288f7975b1SAndrei Emeltchenko chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; 3298f7975b1SAndrei Emeltchenko chan->local_flush_to = L2CAP_DEFAULT_FLUSH_TO; 3308f7975b1SAndrei Emeltchenko 331371fd835SUlisses Furquim l2cap_chan_hold(chan); 332baa7e1faSGustavo F. Padovan 3333df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 3343df91ea2SAndrei Emeltchenko list_add(&chan->list, &conn->chan_l); 3353df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 3360a708f8fSGustavo F. Padovan } 3370a708f8fSGustavo F. Padovan 3380a708f8fSGustavo F. Padovan /* Delete channel. 3390a708f8fSGustavo F. Padovan * Must be called on the locked socket. */ 3404519de9aSGustavo F. Padovan static void l2cap_chan_del(struct l2cap_chan *chan, int err) 3410a708f8fSGustavo F. Padovan { 34248454079SGustavo F. Padovan struct sock *sk = chan->sk; 3438c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 3440a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 3450a708f8fSGustavo F. Padovan 346c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 3470a708f8fSGustavo F. Padovan 34849208c9cSGustavo F. Padovan BT_DBG("chan %p, conn %p, err %d", chan, conn, err); 3490a708f8fSGustavo F. Padovan 3500a708f8fSGustavo F. Padovan if (conn) { 351baa7e1faSGustavo F. Padovan /* Delete from channel list */ 3523df91ea2SAndrei Emeltchenko list_del(&chan->list); 3533d57dc68SGustavo F. Padovan 354371fd835SUlisses Furquim l2cap_chan_put(chan); 355baa7e1faSGustavo F. Padovan 3568c1d787bSGustavo F. Padovan chan->conn = NULL; 3570a708f8fSGustavo F. Padovan hci_conn_put(conn->hcon); 3580a708f8fSGustavo F. Padovan } 3590a708f8fSGustavo F. Padovan 3600e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CLOSED); 3610a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 3620a708f8fSGustavo F. Padovan 3630a708f8fSGustavo F. Padovan if (err) 3640a708f8fSGustavo F. Padovan sk->sk_err = err; 3650a708f8fSGustavo F. Padovan 3660a708f8fSGustavo F. Padovan if (parent) { 3670a708f8fSGustavo F. Padovan bt_accept_unlink(sk); 3680a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 3690a708f8fSGustavo F. Padovan } else 3700a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 3710a708f8fSGustavo F. Padovan 372c1360a1cSGustavo F. Padovan if (!(test_bit(CONF_OUTPUT_DONE, &chan->conf_state) && 373c1360a1cSGustavo F. Padovan test_bit(CONF_INPUT_DONE, &chan->conf_state))) 3746ff5abbfSGustavo F. Padovan return; 3752ead70b8SGustavo F. Padovan 37658d35f87SGustavo F. Padovan skb_queue_purge(&chan->tx_q); 3770a708f8fSGustavo F. Padovan 3780c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 3790a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 3800a708f8fSGustavo F. Padovan 3811a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 3821a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 3831a09bcb9SGustavo F. Padovan __clear_ack_timer(chan); 3840a708f8fSGustavo F. Padovan 385f1c6775bSGustavo F. Padovan skb_queue_purge(&chan->srej_q); 3860a708f8fSGustavo F. Padovan 38739d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 3880a708f8fSGustavo F. Padovan list_del(&l->list); 3890a708f8fSGustavo F. Padovan kfree(l); 3900a708f8fSGustavo F. Padovan } 3910a708f8fSGustavo F. Padovan } 3920a708f8fSGustavo F. Padovan } 3930a708f8fSGustavo F. Padovan 3944519de9aSGustavo F. Padovan static void l2cap_chan_cleanup_listen(struct sock *parent) 3954519de9aSGustavo F. Padovan { 3964519de9aSGustavo F. Padovan struct sock *sk; 3974519de9aSGustavo F. Padovan 3984519de9aSGustavo F. Padovan BT_DBG("parent %p", parent); 3994519de9aSGustavo F. Padovan 4004519de9aSGustavo F. Padovan /* Close not yet accepted channels */ 4010f852724SGustavo F. Padovan while ((sk = bt_accept_dequeue(parent, NULL))) { 402ba3bd0eeSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 4033df91ea2SAndrei Emeltchenko 404c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 4050f852724SGustavo F. Padovan lock_sock(sk); 406ba3bd0eeSGustavo F. Padovan l2cap_chan_close(chan, ECONNRESET); 4070f852724SGustavo F. Padovan release_sock(sk); 4083df91ea2SAndrei Emeltchenko 409ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 4100f852724SGustavo F. Padovan } 4114519de9aSGustavo F. Padovan } 4124519de9aSGustavo F. Padovan 4130f852724SGustavo F. Padovan void l2cap_chan_close(struct l2cap_chan *chan, int reason) 4144519de9aSGustavo F. Padovan { 4154519de9aSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 4164519de9aSGustavo F. Padovan struct sock *sk = chan->sk; 4174519de9aSGustavo F. Padovan 418e05dcc32SAndrei Emeltchenko BT_DBG("chan %p state %s sk %p", chan, 419e05dcc32SAndrei Emeltchenko state_to_string(chan->state), sk); 4204519de9aSGustavo F. Padovan 42189bc500eSGustavo F. Padovan switch (chan->state) { 4224519de9aSGustavo F. Padovan case BT_LISTEN: 4234519de9aSGustavo F. Padovan l2cap_chan_cleanup_listen(sk); 42489bc500eSGustavo F. Padovan 4250e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CLOSED); 42689bc500eSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 4274519de9aSGustavo F. Padovan break; 4284519de9aSGustavo F. Padovan 4294519de9aSGustavo F. Padovan case BT_CONNECTED: 4304519de9aSGustavo F. Padovan case BT_CONFIG: 431715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 4324519de9aSGustavo F. Padovan conn->hcon->type == ACL_LINK) { 433c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 434c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 4354519de9aSGustavo F. Padovan l2cap_send_disconn_req(conn, chan, reason); 4364519de9aSGustavo F. Padovan } else 4374519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4384519de9aSGustavo F. Padovan break; 4394519de9aSGustavo F. Padovan 4404519de9aSGustavo F. Padovan case BT_CONNECT2: 441715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 4424519de9aSGustavo F. Padovan conn->hcon->type == ACL_LINK) { 4434519de9aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 4444519de9aSGustavo F. Padovan __u16 result; 4454519de9aSGustavo F. Padovan 4464519de9aSGustavo F. Padovan if (bt_sk(sk)->defer_setup) 4474519de9aSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 4484519de9aSGustavo F. Padovan else 4494519de9aSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 45089bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_DISCONN); 4514519de9aSGustavo F. Padovan 4524519de9aSGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4534519de9aSGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 4544519de9aSGustavo F. Padovan rsp.result = cpu_to_le16(result); 4554519de9aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 4564519de9aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4574519de9aSGustavo F. Padovan sizeof(rsp), &rsp); 4584519de9aSGustavo F. Padovan } 4594519de9aSGustavo F. Padovan 4604519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4614519de9aSGustavo F. Padovan break; 4624519de9aSGustavo F. Padovan 4634519de9aSGustavo F. Padovan case BT_CONNECT: 4644519de9aSGustavo F. Padovan case BT_DISCONN: 4654519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4664519de9aSGustavo F. Padovan break; 4674519de9aSGustavo F. Padovan 4684519de9aSGustavo F. Padovan default: 4694519de9aSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 4704519de9aSGustavo F. Padovan break; 4714519de9aSGustavo F. Padovan } 4724519de9aSGustavo F. Padovan } 4734519de9aSGustavo F. Padovan 4744343478fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) 4750a708f8fSGustavo F. Padovan { 476715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_RAW) { 4774343478fSGustavo F. Padovan switch (chan->sec_level) { 4780a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 4790a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING_MITM; 4800a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 4810a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING; 4820a708f8fSGustavo F. Padovan default: 4830a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 4840a708f8fSGustavo F. Padovan } 485fe4128e0SGustavo F. Padovan } else if (chan->psm == cpu_to_le16(0x0001)) { 4864343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_LOW) 4874343478fSGustavo F. Padovan chan->sec_level = BT_SECURITY_SDP; 4880a708f8fSGustavo F. Padovan 4894343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_HIGH) 4900a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING_MITM; 4910a708f8fSGustavo F. Padovan else 4920a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 4930a708f8fSGustavo F. Padovan } else { 4944343478fSGustavo F. Padovan switch (chan->sec_level) { 4950a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 4960a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING_MITM; 4970a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 4980a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING; 4990a708f8fSGustavo F. Padovan default: 5000a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 5010a708f8fSGustavo F. Padovan } 5020a708f8fSGustavo F. Padovan } 5030a708f8fSGustavo F. Padovan } 5040a708f8fSGustavo F. Padovan 5050a708f8fSGustavo F. Padovan /* Service level security */ 506d45fc423SGustavo F. Padovan int l2cap_chan_check_security(struct l2cap_chan *chan) 5070a708f8fSGustavo F. Padovan { 5088c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 5090a708f8fSGustavo F. Padovan __u8 auth_type; 5100a708f8fSGustavo F. Padovan 5114343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 5120a708f8fSGustavo F. Padovan 5134343478fSGustavo F. Padovan return hci_conn_security(conn->hcon, chan->sec_level, auth_type); 5140a708f8fSGustavo F. Padovan } 5150a708f8fSGustavo F. Padovan 516b5ad8b7fSJohannes Berg static u8 l2cap_get_ident(struct l2cap_conn *conn) 5170a708f8fSGustavo F. Padovan { 5180a708f8fSGustavo F. Padovan u8 id; 5190a708f8fSGustavo F. Padovan 5200a708f8fSGustavo F. Padovan /* Get next available identificator. 5210a708f8fSGustavo F. Padovan * 1 - 128 are used by kernel. 5220a708f8fSGustavo F. Padovan * 129 - 199 are reserved. 5230a708f8fSGustavo F. Padovan * 200 - 254 are used by utilities like l2ping, etc. 5240a708f8fSGustavo F. Padovan */ 5250a708f8fSGustavo F. Padovan 526333055f2SGustavo F. Padovan spin_lock(&conn->lock); 5270a708f8fSGustavo F. Padovan 5280a708f8fSGustavo F. Padovan if (++conn->tx_ident > 128) 5290a708f8fSGustavo F. Padovan conn->tx_ident = 1; 5300a708f8fSGustavo F. Padovan 5310a708f8fSGustavo F. Padovan id = conn->tx_ident; 5320a708f8fSGustavo F. Padovan 533333055f2SGustavo F. Padovan spin_unlock(&conn->lock); 5340a708f8fSGustavo F. Padovan 5350a708f8fSGustavo F. Padovan return id; 5360a708f8fSGustavo F. Padovan } 5370a708f8fSGustavo F. Padovan 5384519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data) 5390a708f8fSGustavo F. Padovan { 5400a708f8fSGustavo F. Padovan struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); 5410a708f8fSGustavo F. Padovan u8 flags; 5420a708f8fSGustavo F. Padovan 5430a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x", code); 5440a708f8fSGustavo F. Padovan 5450a708f8fSGustavo F. Padovan if (!skb) 5460a708f8fSGustavo F. Padovan return; 5470a708f8fSGustavo F. Padovan 5480a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 5490a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 5500a708f8fSGustavo F. Padovan else 5510a708f8fSGustavo F. Padovan flags = ACL_START; 5520a708f8fSGustavo F. Padovan 55314b12d0bSJaikumar Ganesh bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; 5545e59b791SLuiz Augusto von Dentz skb->priority = HCI_PRIO_MAX; 55514b12d0bSJaikumar Ganesh 55673d80debSLuiz Augusto von Dentz hci_send_acl(conn->hchan, skb, flags); 5570a708f8fSGustavo F. Padovan } 5580a708f8fSGustavo F. Padovan 55973d80debSLuiz Augusto von Dentz static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) 56073d80debSLuiz Augusto von Dentz { 56173d80debSLuiz Augusto von Dentz struct hci_conn *hcon = chan->conn->hcon; 56273d80debSLuiz Augusto von Dentz u16 flags; 56373d80debSLuiz Augusto von Dentz 56473d80debSLuiz Augusto von Dentz BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len, 56573d80debSLuiz Augusto von Dentz skb->priority); 56673d80debSLuiz Augusto von Dentz 56773d80debSLuiz Augusto von Dentz if (!test_bit(FLAG_FLUSHABLE, &chan->flags) && 56873d80debSLuiz Augusto von Dentz lmp_no_flush_capable(hcon->hdev)) 56973d80debSLuiz Augusto von Dentz flags = ACL_START_NO_FLUSH; 57073d80debSLuiz Augusto von Dentz else 57173d80debSLuiz Augusto von Dentz flags = ACL_START; 57273d80debSLuiz Augusto von Dentz 57373d80debSLuiz Augusto von Dentz bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); 57473d80debSLuiz Augusto von Dentz hci_send_acl(chan->conn->hchan, skb, flags); 5750a708f8fSGustavo F. Padovan } 5760a708f8fSGustavo F. Padovan 57788843ab0SAndrei Emeltchenko static inline void l2cap_send_sframe(struct l2cap_chan *chan, u32 control) 5780a708f8fSGustavo F. Padovan { 5790a708f8fSGustavo F. Padovan struct sk_buff *skb; 5800a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 5818c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 582e4ca6d98SAndrei Emeltchenko int count, hlen; 5830a708f8fSGustavo F. Padovan 58489bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 5850a708f8fSGustavo F. Padovan return; 5860a708f8fSGustavo F. Padovan 587e4ca6d98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 588e4ca6d98SAndrei Emeltchenko hlen = L2CAP_EXT_HDR_SIZE; 589e4ca6d98SAndrei Emeltchenko else 590e4ca6d98SAndrei Emeltchenko hlen = L2CAP_ENH_HDR_SIZE; 5910a708f8fSGustavo F. Padovan 5920a708f8fSGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 59303a51213SAndrei Emeltchenko hlen += L2CAP_FCS_SIZE; 5940a708f8fSGustavo F. Padovan 59588843ab0SAndrei Emeltchenko BT_DBG("chan %p, control 0x%8.8x", chan, control); 5960a708f8fSGustavo F. Padovan 5970a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, hlen); 598793c2f1cSAndrei Emeltchenko 599793c2f1cSAndrei Emeltchenko control |= __set_sframe(chan); 6000a708f8fSGustavo F. Padovan 601e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 60203f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 6030a708f8fSGustavo F. Padovan 604e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_PBIT, &chan->conn_state)) 605e3781735SAndrei Emeltchenko control |= __set_ctrl_poll(chan); 6060a708f8fSGustavo F. Padovan 6070a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 6080a708f8fSGustavo F. Padovan if (!skb) 6090a708f8fSGustavo F. Padovan return; 6100a708f8fSGustavo F. Padovan 6110a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 6120a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); 613fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 61488843ab0SAndrei Emeltchenko 61588843ab0SAndrei Emeltchenko __put_control(chan, control, skb_put(skb, __ctrl_size(chan))); 6160a708f8fSGustavo F. Padovan 61747d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 61803a51213SAndrei Emeltchenko u16 fcs = crc16(0, (u8 *)lh, count - L2CAP_FCS_SIZE); 61903a51213SAndrei Emeltchenko put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); 6200a708f8fSGustavo F. Padovan } 6210a708f8fSGustavo F. Padovan 62273d80debSLuiz Augusto von Dentz skb->priority = HCI_PRIO_MAX; 62373d80debSLuiz Augusto von Dentz l2cap_do_send(chan, skb); 6240a708f8fSGustavo F. Padovan } 6250a708f8fSGustavo F. Padovan 62688843ab0SAndrei Emeltchenko static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u32 control) 6270a708f8fSGustavo F. Padovan { 628e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 629ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 630e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 6310a708f8fSGustavo F. Padovan } else 632ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 6330a708f8fSGustavo F. Padovan 6340b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 6350a708f8fSGustavo F. Padovan 636525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 6370a708f8fSGustavo F. Padovan } 6380a708f8fSGustavo F. Padovan 639b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) 6400a708f8fSGustavo F. Padovan { 641c1360a1cSGustavo F. Padovan return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); 6420a708f8fSGustavo F. Padovan } 6430a708f8fSGustavo F. Padovan 644fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan) 6450a708f8fSGustavo F. Padovan { 6468c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 6470a708f8fSGustavo F. Padovan 6480a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { 6490a708f8fSGustavo F. Padovan if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) 6500a708f8fSGustavo F. Padovan return; 6510a708f8fSGustavo F. Padovan 652d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan) && 6534343478fSGustavo F. Padovan __l2cap_no_conn_pending(chan)) { 6540a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 655fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 656fe4128e0SGustavo F. Padovan req.psm = chan->psm; 6570a708f8fSGustavo F. Padovan 658fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 659c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 6600a708f8fSGustavo F. Padovan 661fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 662fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 6630a708f8fSGustavo F. Padovan } 6640a708f8fSGustavo F. Padovan } else { 6650a708f8fSGustavo F. Padovan struct l2cap_info_req req; 6660a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 6670a708f8fSGustavo F. Padovan 6680a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 6690a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 6700a708f8fSGustavo F. Padovan 671030013d8SGustavo F. Padovan schedule_delayed_work(&conn->info_timer, 6720a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 6730a708f8fSGustavo F. Padovan 6740a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 6750a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 6760a708f8fSGustavo F. Padovan } 6770a708f8fSGustavo F. Padovan } 6780a708f8fSGustavo F. Padovan 6790a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) 6800a708f8fSGustavo F. Padovan { 6810a708f8fSGustavo F. Padovan u32 local_feat_mask = l2cap_feat_mask; 6820a708f8fSGustavo F. Padovan if (!disable_ertm) 6830a708f8fSGustavo F. Padovan local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; 6840a708f8fSGustavo F. Padovan 6850a708f8fSGustavo F. Padovan switch (mode) { 6860a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 6870a708f8fSGustavo F. Padovan return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; 6880a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 6890a708f8fSGustavo F. Padovan return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; 6900a708f8fSGustavo F. Padovan default: 6910a708f8fSGustavo F. Padovan return 0x00; 6920a708f8fSGustavo F. Padovan } 6930a708f8fSGustavo F. Padovan } 6940a708f8fSGustavo F. Padovan 6954519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err) 6960a708f8fSGustavo F. Padovan { 697e92c8e70SGustavo F. Padovan struct sock *sk; 6980a708f8fSGustavo F. Padovan struct l2cap_disconn_req req; 6990a708f8fSGustavo F. Padovan 7000a708f8fSGustavo F. Padovan if (!conn) 7010a708f8fSGustavo F. Padovan return; 7020a708f8fSGustavo F. Padovan 703e92c8e70SGustavo F. Padovan sk = chan->sk; 704e92c8e70SGustavo F. Padovan 7050c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 7061a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 7071a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 7081a09bcb9SGustavo F. Padovan __clear_ack_timer(chan); 7090a708f8fSGustavo F. Padovan } 7100a708f8fSGustavo F. Padovan 711fe4128e0SGustavo F. Padovan req.dcid = cpu_to_le16(chan->dcid); 712fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 7130a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 7140a708f8fSGustavo F. Padovan L2CAP_DISCONN_REQ, sizeof(req), &req); 7150a708f8fSGustavo F. Padovan 7160e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_DISCONN); 7170a708f8fSGustavo F. Padovan sk->sk_err = err; 7180a708f8fSGustavo F. Padovan } 7190a708f8fSGustavo F. Padovan 7200a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */ 7210a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn) 7220a708f8fSGustavo F. Padovan { 7233df91ea2SAndrei Emeltchenko struct l2cap_chan *chan, *tmp; 7240a708f8fSGustavo F. Padovan 7250a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 7260a708f8fSGustavo F. Padovan 7273df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 7280a708f8fSGustavo F. Padovan 7293df91ea2SAndrei Emeltchenko list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { 73048454079SGustavo F. Padovan struct sock *sk = chan->sk; 731baa7e1faSGustavo F. Padovan 7320a708f8fSGustavo F. Padovan bh_lock_sock(sk); 7330a708f8fSGustavo F. Padovan 734715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 7350a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7360a708f8fSGustavo F. Padovan continue; 7370a708f8fSGustavo F. Padovan } 7380a708f8fSGustavo F. Padovan 73989bc500eSGustavo F. Padovan if (chan->state == BT_CONNECT) { 7400a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 7410a708f8fSGustavo F. Padovan 742d45fc423SGustavo F. Padovan if (!l2cap_chan_check_security(chan) || 743b4450035SGustavo F. Padovan !__l2cap_no_conn_pending(chan)) { 7440a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7450a708f8fSGustavo F. Padovan continue; 7460a708f8fSGustavo F. Padovan } 7470a708f8fSGustavo F. Padovan 748c1360a1cSGustavo F. Padovan if (!l2cap_mode_supported(chan->mode, conn->feat_mask) 749c1360a1cSGustavo F. Padovan && test_bit(CONF_STATE2_DEVICE, 750c1360a1cSGustavo F. Padovan &chan->conf_state)) { 7510f852724SGustavo F. Padovan /* l2cap_chan_close() calls list_del(chan) 752820ffdb3SGustavo F. Padovan * so release the lock */ 7530f852724SGustavo F. Padovan l2cap_chan_close(chan, ECONNRESET); 7540a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7550a708f8fSGustavo F. Padovan continue; 7560a708f8fSGustavo F. Padovan } 7570a708f8fSGustavo F. Padovan 758fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 759fe4128e0SGustavo F. Padovan req.psm = chan->psm; 7600a708f8fSGustavo F. Padovan 761fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 762c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 7630a708f8fSGustavo F. Padovan 764fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 765fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 7660a708f8fSGustavo F. Padovan 76789bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT2) { 7680a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 7690a708f8fSGustavo F. Padovan char buf[128]; 770fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 771fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 7720a708f8fSGustavo F. Padovan 773d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan)) { 7740a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 7750a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 7760a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 7770a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); 77805e9a2f6SIlia Kolomisnky if (parent) 7790a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 7800a708f8fSGustavo F. Padovan 7810a708f8fSGustavo F. Padovan } else { 7820e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONFIG); 7830a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 7840a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 7850a708f8fSGustavo F. Padovan } 7860a708f8fSGustavo F. Padovan } else { 7870a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 7880a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); 7890a708f8fSGustavo F. Padovan } 7900a708f8fSGustavo F. Padovan 791fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 792fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 7930a708f8fSGustavo F. Padovan 794c1360a1cSGustavo F. Padovan if (test_bit(CONF_REQ_SENT, &chan->conf_state) || 7950a708f8fSGustavo F. Padovan rsp.result != L2CAP_CR_SUCCESS) { 7960a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7970a708f8fSGustavo F. Padovan continue; 7980a708f8fSGustavo F. Padovan } 7990a708f8fSGustavo F. Padovan 800c1360a1cSGustavo F. Padovan set_bit(CONF_REQ_SENT, &chan->conf_state); 8010a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 80273ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 80373ffa904SGustavo F. Padovan chan->num_conf_req++; 8040a708f8fSGustavo F. Padovan } 8050a708f8fSGustavo F. Padovan 8060a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 8070a708f8fSGustavo F. Padovan } 8080a708f8fSGustavo F. Padovan 8093df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 8100a708f8fSGustavo F. Padovan } 8110a708f8fSGustavo F. Padovan 812b62f328bSVille Tervo /* Find socket with cid and source bdaddr. 813b62f328bSVille Tervo * Returns closest match, locked. 814b62f328bSVille Tervo */ 81523691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src) 816b62f328bSVille Tervo { 81723691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 818b62f328bSVille Tervo 81923691d75SGustavo F. Padovan read_lock(&chan_list_lock); 820b62f328bSVille Tervo 82123691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 82223691d75SGustavo F. Padovan struct sock *sk = c->sk; 823fe4128e0SGustavo F. Padovan 82489bc500eSGustavo F. Padovan if (state && c->state != state) 825b62f328bSVille Tervo continue; 826b62f328bSVille Tervo 82723691d75SGustavo F. Padovan if (c->scid == cid) { 828b62f328bSVille Tervo /* Exact match. */ 82923691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 83023691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 83123691d75SGustavo F. Padovan return c; 83223691d75SGustavo F. Padovan } 833b62f328bSVille Tervo 834b62f328bSVille Tervo /* Closest match */ 835b62f328bSVille Tervo if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 83623691d75SGustavo F. Padovan c1 = c; 837b62f328bSVille Tervo } 838b62f328bSVille Tervo } 839280f294fSGustavo F. Padovan 84023691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 841b62f328bSVille Tervo 84223691d75SGustavo F. Padovan return c1; 843b62f328bSVille Tervo } 844b62f328bSVille Tervo 845b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn) 846b62f328bSVille Tervo { 847c916fbe4SGustavo F. Padovan struct sock *parent, *sk; 84823691d75SGustavo F. Padovan struct l2cap_chan *chan, *pchan; 849b62f328bSVille Tervo 850b62f328bSVille Tervo BT_DBG(""); 851b62f328bSVille Tervo 852b62f328bSVille Tervo /* Check if we have socket listening on cid */ 85323691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, 854b62f328bSVille Tervo conn->src); 85523691d75SGustavo F. Padovan if (!pchan) 856b62f328bSVille Tervo return; 857b62f328bSVille Tervo 85823691d75SGustavo F. Padovan parent = pchan->sk; 85923691d75SGustavo F. Padovan 860aa2ac881SGustavo F. Padovan lock_sock(parent); 86162f3a2cfSGustavo F. Padovan 862b62f328bSVille Tervo /* Check for backlog size */ 863b62f328bSVille Tervo if (sk_acceptq_is_full(parent)) { 864b62f328bSVille Tervo BT_DBG("backlog full %d", parent->sk_ack_backlog); 865b62f328bSVille Tervo goto clean; 866b62f328bSVille Tervo } 867b62f328bSVille Tervo 86880808e43SGustavo F. Padovan chan = pchan->ops->new_connection(pchan->data); 86980808e43SGustavo F. Padovan if (!chan) 870b62f328bSVille Tervo goto clean; 871b62f328bSVille Tervo 87280808e43SGustavo F. Padovan sk = chan->sk; 8735d41ce1dSGustavo F. Padovan 874b62f328bSVille Tervo hci_conn_hold(conn->hcon); 875b62f328bSVille Tervo 876b62f328bSVille Tervo bacpy(&bt_sk(sk)->src, conn->src); 877b62f328bSVille Tervo bacpy(&bt_sk(sk)->dst, conn->dst); 878b62f328bSVille Tervo 879d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 880d1010240SGustavo F. Padovan 8813d57dc68SGustavo F. Padovan l2cap_chan_add(conn, chan); 88248454079SGustavo F. Padovan 883c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 884b62f328bSVille Tervo 8850e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECTED); 886b62f328bSVille Tervo parent->sk_data_ready(parent, 0); 887b62f328bSVille Tervo 888b62f328bSVille Tervo clean: 889aa2ac881SGustavo F. Padovan release_sock(parent); 890b62f328bSVille Tervo } 891b62f328bSVille Tervo 892cf4cd009SAndrei Emeltchenko static void l2cap_chan_ready(struct l2cap_chan *chan) 893f1cb9af5SVinicius Costa Gomes { 894cf4cd009SAndrei Emeltchenko struct sock *sk = chan->sk; 895f1cb9af5SVinicius Costa Gomes struct sock *parent = bt_sk(sk)->parent; 896f1cb9af5SVinicius Costa Gomes 897f1cb9af5SVinicius Costa Gomes BT_DBG("sk %p, parent %p", sk, parent); 898f1cb9af5SVinicius Costa Gomes 899f1cb9af5SVinicius Costa Gomes chan->conf_state = 0; 900f1cb9af5SVinicius Costa Gomes __clear_chan_timer(chan); 901f1cb9af5SVinicius Costa Gomes 9020e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECTED); 903f1cb9af5SVinicius Costa Gomes sk->sk_state_change(sk); 904f1cb9af5SVinicius Costa Gomes 905f1cb9af5SVinicius Costa Gomes if (parent) 906f1cb9af5SVinicius Costa Gomes parent->sk_data_ready(parent, 0); 907f1cb9af5SVinicius Costa Gomes } 908f1cb9af5SVinicius Costa Gomes 9090a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn) 9100a708f8fSGustavo F. Padovan { 91148454079SGustavo F. Padovan struct l2cap_chan *chan; 9120a708f8fSGustavo F. Padovan 9130a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 9140a708f8fSGustavo F. Padovan 915b62f328bSVille Tervo if (!conn->hcon->out && conn->hcon->type == LE_LINK) 916b62f328bSVille Tervo l2cap_le_conn_ready(conn); 917b62f328bSVille Tervo 918160dc6acSVinicius Costa Gomes if (conn->hcon->out && conn->hcon->type == LE_LINK) 919160dc6acSVinicius Costa Gomes smp_conn_security(conn, conn->hcon->pending_sec_level); 920160dc6acSVinicius Costa Gomes 9213df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 9220a708f8fSGustavo F. Padovan 9233df91ea2SAndrei Emeltchenko list_for_each_entry(chan, &conn->chan_l, list) { 92448454079SGustavo F. Padovan struct sock *sk = chan->sk; 925baa7e1faSGustavo F. Padovan 9260a708f8fSGustavo F. Padovan bh_lock_sock(sk); 9270a708f8fSGustavo F. Padovan 92863128451SVinicius Costa Gomes if (conn->hcon->type == LE_LINK) { 929b501d6a1SAnderson Briglia if (smp_conn_security(conn, chan->sec_level)) 930cf4cd009SAndrei Emeltchenko l2cap_chan_ready(chan); 931acd7d370SVille Tervo 93263128451SVinicius Costa Gomes } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 933c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 9340e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECTED); 9350a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 936b501d6a1SAnderson Briglia 93789bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT) 938fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 9390a708f8fSGustavo F. Padovan 9400a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9410a708f8fSGustavo F. Padovan } 9420a708f8fSGustavo F. Padovan 9433df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 9440a708f8fSGustavo F. Padovan } 9450a708f8fSGustavo F. Padovan 9460a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */ 9470a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) 9480a708f8fSGustavo F. Padovan { 94948454079SGustavo F. Padovan struct l2cap_chan *chan; 9500a708f8fSGustavo F. Padovan 9510a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 9520a708f8fSGustavo F. Padovan 9533df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 9540a708f8fSGustavo F. Padovan 9553df91ea2SAndrei Emeltchenko list_for_each_entry(chan, &conn->chan_l, list) { 95648454079SGustavo F. Padovan struct sock *sk = chan->sk; 957baa7e1faSGustavo F. Padovan 958ecf61bdbSAndrei Emeltchenko if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) 9590a708f8fSGustavo F. Padovan sk->sk_err = err; 9600a708f8fSGustavo F. Padovan } 9610a708f8fSGustavo F. Padovan 9623df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 9630a708f8fSGustavo F. Padovan } 9640a708f8fSGustavo F. Padovan 965f878fcadSGustavo F. Padovan static void l2cap_info_timeout(struct work_struct *work) 9660a708f8fSGustavo F. Padovan { 967f878fcadSGustavo F. Padovan struct l2cap_conn *conn = container_of(work, struct l2cap_conn, 968030013d8SGustavo F. Padovan info_timer.work); 9690a708f8fSGustavo F. Padovan 9700a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 9710a708f8fSGustavo F. Padovan conn->info_ident = 0; 9720a708f8fSGustavo F. Padovan 9730a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 9740a708f8fSGustavo F. Padovan } 9750a708f8fSGustavo F. Padovan 9765d3de7dfSVinicius Costa Gomes static void l2cap_conn_del(struct hci_conn *hcon, int err) 9775d3de7dfSVinicius Costa Gomes { 9785d3de7dfSVinicius Costa Gomes struct l2cap_conn *conn = hcon->l2cap_data; 9795d3de7dfSVinicius Costa Gomes struct l2cap_chan *chan, *l; 9805d3de7dfSVinicius Costa Gomes struct sock *sk; 9815d3de7dfSVinicius Costa Gomes 9825d3de7dfSVinicius Costa Gomes if (!conn) 9835d3de7dfSVinicius Costa Gomes return; 9845d3de7dfSVinicius Costa Gomes 9855d3de7dfSVinicius Costa Gomes BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); 9865d3de7dfSVinicius Costa Gomes 9875d3de7dfSVinicius Costa Gomes kfree_skb(conn->rx_skb); 9885d3de7dfSVinicius Costa Gomes 9893df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 9903df91ea2SAndrei Emeltchenko 9915d3de7dfSVinicius Costa Gomes /* Kill channels */ 9925d3de7dfSVinicius Costa Gomes list_for_each_entry_safe(chan, l, &conn->chan_l, list) { 9935d3de7dfSVinicius Costa Gomes sk = chan->sk; 994aa2ac881SGustavo F. Padovan lock_sock(sk); 9955d3de7dfSVinicius Costa Gomes l2cap_chan_del(chan, err); 996aa2ac881SGustavo F. Padovan release_sock(sk); 9975d3de7dfSVinicius Costa Gomes chan->ops->close(chan->data); 9985d3de7dfSVinicius Costa Gomes } 9995d3de7dfSVinicius Costa Gomes 10003df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 10013df91ea2SAndrei Emeltchenko 100273d80debSLuiz Augusto von Dentz hci_chan_del(conn->hchan); 100373d80debSLuiz Augusto von Dentz 10045d3de7dfSVinicius Costa Gomes if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) 1005127074bfSUlisses Furquim cancel_delayed_work_sync(&conn->info_timer); 10065d3de7dfSVinicius Costa Gomes 100751a8efd7SJohan Hedberg if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) { 1008127074bfSUlisses Furquim cancel_delayed_work_sync(&conn->security_timer); 10098aab4757SVinicius Costa Gomes smp_chan_destroy(conn); 1010d26a2345SVinicius Costa Gomes } 10115d3de7dfSVinicius Costa Gomes 10125d3de7dfSVinicius Costa Gomes hcon->l2cap_data = NULL; 10135d3de7dfSVinicius Costa Gomes kfree(conn); 10145d3de7dfSVinicius Costa Gomes } 10155d3de7dfSVinicius Costa Gomes 10166c9d42a1SGustavo F. Padovan static void security_timeout(struct work_struct *work) 10175d3de7dfSVinicius Costa Gomes { 10186c9d42a1SGustavo F. Padovan struct l2cap_conn *conn = container_of(work, struct l2cap_conn, 10196c9d42a1SGustavo F. Padovan security_timer.work); 10205d3de7dfSVinicius Costa Gomes 10215d3de7dfSVinicius Costa Gomes l2cap_conn_del(conn->hcon, ETIMEDOUT); 10225d3de7dfSVinicius Costa Gomes } 10235d3de7dfSVinicius Costa Gomes 10240a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) 10250a708f8fSGustavo F. Padovan { 10260a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 102773d80debSLuiz Augusto von Dentz struct hci_chan *hchan; 10280a708f8fSGustavo F. Padovan 10290a708f8fSGustavo F. Padovan if (conn || status) 10300a708f8fSGustavo F. Padovan return conn; 10310a708f8fSGustavo F. Padovan 103273d80debSLuiz Augusto von Dentz hchan = hci_chan_create(hcon); 103373d80debSLuiz Augusto von Dentz if (!hchan) 10340a708f8fSGustavo F. Padovan return NULL; 10350a708f8fSGustavo F. Padovan 103673d80debSLuiz Augusto von Dentz conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); 103773d80debSLuiz Augusto von Dentz if (!conn) { 103873d80debSLuiz Augusto von Dentz hci_chan_del(hchan); 103973d80debSLuiz Augusto von Dentz return NULL; 104073d80debSLuiz Augusto von Dentz } 104173d80debSLuiz Augusto von Dentz 10420a708f8fSGustavo F. Padovan hcon->l2cap_data = conn; 10430a708f8fSGustavo F. Padovan conn->hcon = hcon; 104473d80debSLuiz Augusto von Dentz conn->hchan = hchan; 10450a708f8fSGustavo F. Padovan 104673d80debSLuiz Augusto von Dentz BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); 10470a708f8fSGustavo F. Padovan 1048acd7d370SVille Tervo if (hcon->hdev->le_mtu && hcon->type == LE_LINK) 1049acd7d370SVille Tervo conn->mtu = hcon->hdev->le_mtu; 1050acd7d370SVille Tervo else 10510a708f8fSGustavo F. Padovan conn->mtu = hcon->hdev->acl_mtu; 1052acd7d370SVille Tervo 10530a708f8fSGustavo F. Padovan conn->src = &hcon->hdev->bdaddr; 10540a708f8fSGustavo F. Padovan conn->dst = &hcon->dst; 10550a708f8fSGustavo F. Padovan 10560a708f8fSGustavo F. Padovan conn->feat_mask = 0; 10570a708f8fSGustavo F. Padovan 10580a708f8fSGustavo F. Padovan spin_lock_init(&conn->lock); 10593df91ea2SAndrei Emeltchenko mutex_init(&conn->chan_lock); 1060baa7e1faSGustavo F. Padovan 1061baa7e1faSGustavo F. Padovan INIT_LIST_HEAD(&conn->chan_l); 10620a708f8fSGustavo F. Padovan 10635d3de7dfSVinicius Costa Gomes if (hcon->type == LE_LINK) 10646c9d42a1SGustavo F. Padovan INIT_DELAYED_WORK(&conn->security_timer, security_timeout); 10655d3de7dfSVinicius Costa Gomes else 1066030013d8SGustavo F. Padovan INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); 10670a708f8fSGustavo F. Padovan 10689f5a0d7bSAndrei Emeltchenko conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; 10690a708f8fSGustavo F. Padovan 10700a708f8fSGustavo F. Padovan return conn; 10710a708f8fSGustavo F. Padovan } 10720a708f8fSGustavo F. Padovan 10730a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */ 10740a708f8fSGustavo F. Padovan 10750a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr. 10760a708f8fSGustavo F. Padovan * Returns closest match. 10770a708f8fSGustavo F. Padovan */ 107823691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src) 10790a708f8fSGustavo F. Padovan { 108023691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 10810a708f8fSGustavo F. Padovan 108223691d75SGustavo F. Padovan read_lock(&chan_list_lock); 10830a708f8fSGustavo F. Padovan 108423691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 108523691d75SGustavo F. Padovan struct sock *sk = c->sk; 1086fe4128e0SGustavo F. Padovan 108789bc500eSGustavo F. Padovan if (state && c->state != state) 10880a708f8fSGustavo F. Padovan continue; 10890a708f8fSGustavo F. Padovan 109023691d75SGustavo F. Padovan if (c->psm == psm) { 10910a708f8fSGustavo F. Padovan /* Exact match. */ 109223691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 1093a7567b20SJohannes Berg read_unlock(&chan_list_lock); 109423691d75SGustavo F. Padovan return c; 109523691d75SGustavo F. Padovan } 10960a708f8fSGustavo F. Padovan 10970a708f8fSGustavo F. Padovan /* Closest match */ 10980a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 109923691d75SGustavo F. Padovan c1 = c; 11000a708f8fSGustavo F. Padovan } 11010a708f8fSGustavo F. Padovan } 11020a708f8fSGustavo F. Padovan 110323691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 11040a708f8fSGustavo F. Padovan 110523691d75SGustavo F. Padovan return c1; 11060a708f8fSGustavo F. Padovan } 11070a708f8fSGustavo F. Padovan 1108cbe8fed4SJohan Hedberg int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, bdaddr_t *dst) 11090a708f8fSGustavo F. Padovan { 11105d41ce1dSGustavo F. Padovan struct sock *sk = chan->sk; 11110a708f8fSGustavo F. Padovan bdaddr_t *src = &bt_sk(sk)->src; 11120a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 11130a708f8fSGustavo F. Padovan struct hci_conn *hcon; 11140a708f8fSGustavo F. Padovan struct hci_dev *hdev; 11150a708f8fSGustavo F. Padovan __u8 auth_type; 11160a708f8fSGustavo F. Padovan int err; 11170a708f8fSGustavo F. Padovan 11180a708f8fSGustavo F. Padovan BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), 1119fe4128e0SGustavo F. Padovan chan->psm); 11200a708f8fSGustavo F. Padovan 11210a708f8fSGustavo F. Padovan hdev = hci_get_route(dst, src); 11220a708f8fSGustavo F. Padovan if (!hdev) 11230a708f8fSGustavo F. Padovan return -EHOSTUNREACH; 11240a708f8fSGustavo F. Padovan 112509fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 11260a708f8fSGustavo F. Padovan 112703a00194SGustavo F. Padovan lock_sock(sk); 112803a00194SGustavo F. Padovan 112903a00194SGustavo F. Padovan /* PSM must be odd and lsb of upper byte must be 0 */ 113003a00194SGustavo F. Padovan if ((__le16_to_cpu(psm) & 0x0101) != 0x0001 && !cid && 113103a00194SGustavo F. Padovan chan->chan_type != L2CAP_CHAN_RAW) { 113203a00194SGustavo F. Padovan err = -EINVAL; 113303a00194SGustavo F. Padovan goto done; 113403a00194SGustavo F. Padovan } 113503a00194SGustavo F. Padovan 113603a00194SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !(psm || cid)) { 113703a00194SGustavo F. Padovan err = -EINVAL; 113803a00194SGustavo F. Padovan goto done; 113903a00194SGustavo F. Padovan } 114003a00194SGustavo F. Padovan 114103a00194SGustavo F. Padovan switch (chan->mode) { 114203a00194SGustavo F. Padovan case L2CAP_MODE_BASIC: 114303a00194SGustavo F. Padovan break; 114403a00194SGustavo F. Padovan case L2CAP_MODE_ERTM: 114503a00194SGustavo F. Padovan case L2CAP_MODE_STREAMING: 114603a00194SGustavo F. Padovan if (!disable_ertm) 114703a00194SGustavo F. Padovan break; 114803a00194SGustavo F. Padovan /* fall through */ 114903a00194SGustavo F. Padovan default: 115003a00194SGustavo F. Padovan err = -ENOTSUPP; 115103a00194SGustavo F. Padovan goto done; 115203a00194SGustavo F. Padovan } 115303a00194SGustavo F. Padovan 115403a00194SGustavo F. Padovan switch (sk->sk_state) { 115503a00194SGustavo F. Padovan case BT_CONNECT: 115603a00194SGustavo F. Padovan case BT_CONNECT2: 115703a00194SGustavo F. Padovan case BT_CONFIG: 115803a00194SGustavo F. Padovan /* Already connecting */ 115903a00194SGustavo F. Padovan err = 0; 116003a00194SGustavo F. Padovan goto done; 116103a00194SGustavo F. Padovan 116203a00194SGustavo F. Padovan case BT_CONNECTED: 116303a00194SGustavo F. Padovan /* Already connected */ 116403a00194SGustavo F. Padovan err = -EISCONN; 116503a00194SGustavo F. Padovan goto done; 116603a00194SGustavo F. Padovan 116703a00194SGustavo F. Padovan case BT_OPEN: 116803a00194SGustavo F. Padovan case BT_BOUND: 116903a00194SGustavo F. Padovan /* Can connect */ 117003a00194SGustavo F. Padovan break; 117103a00194SGustavo F. Padovan 117203a00194SGustavo F. Padovan default: 117303a00194SGustavo F. Padovan err = -EBADFD; 117403a00194SGustavo F. Padovan goto done; 117503a00194SGustavo F. Padovan } 117603a00194SGustavo F. Padovan 117703a00194SGustavo F. Padovan /* Set destination address and psm */ 11789219b2a0SGustavo F. Padovan bacpy(&bt_sk(sk)->dst, dst); 117903a00194SGustavo F. Padovan chan->psm = psm; 118003a00194SGustavo F. Padovan chan->dcid = cid; 11810a708f8fSGustavo F. Padovan 11824343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 11830a708f8fSGustavo F. Padovan 1184fe4128e0SGustavo F. Padovan if (chan->dcid == L2CAP_CID_LE_DATA) 1185acd7d370SVille Tervo hcon = hci_connect(hdev, LE_LINK, dst, 11864343478fSGustavo F. Padovan chan->sec_level, auth_type); 1187acd7d370SVille Tervo else 11880a708f8fSGustavo F. Padovan hcon = hci_connect(hdev, ACL_LINK, dst, 11894343478fSGustavo F. Padovan chan->sec_level, auth_type); 1190acd7d370SVille Tervo 119130e76272SVille Tervo if (IS_ERR(hcon)) { 119230e76272SVille Tervo err = PTR_ERR(hcon); 11930a708f8fSGustavo F. Padovan goto done; 119430e76272SVille Tervo } 11950a708f8fSGustavo F. Padovan 11960a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 11970a708f8fSGustavo F. Padovan if (!conn) { 11980a708f8fSGustavo F. Padovan hci_conn_put(hcon); 119930e76272SVille Tervo err = -ENOMEM; 12000a708f8fSGustavo F. Padovan goto done; 12010a708f8fSGustavo F. Padovan } 12020a708f8fSGustavo F. Padovan 12030a708f8fSGustavo F. Padovan /* Update source addr of the socket */ 12040a708f8fSGustavo F. Padovan bacpy(src, conn->src); 12050a708f8fSGustavo F. Padovan 120648454079SGustavo F. Padovan l2cap_chan_add(conn, chan); 120748454079SGustavo F. Padovan 12080e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECT); 1209c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 12100a708f8fSGustavo F. Padovan 12110a708f8fSGustavo F. Padovan if (hcon->state == BT_CONNECTED) { 1212715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 1213c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 1214d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan)) 12150e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECTED); 12160a708f8fSGustavo F. Padovan } else 1217fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 12180a708f8fSGustavo F. Padovan } 12190a708f8fSGustavo F. Padovan 122030e76272SVille Tervo err = 0; 122130e76272SVille Tervo 12220a708f8fSGustavo F. Padovan done: 122309fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 12240a708f8fSGustavo F. Padovan hci_dev_put(hdev); 12250a708f8fSGustavo F. Padovan return err; 12260a708f8fSGustavo F. Padovan } 12270a708f8fSGustavo F. Padovan 1228dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk) 12290a708f8fSGustavo F. Padovan { 12308c1d787bSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 12310a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 12320a708f8fSGustavo F. Padovan int err = 0; 12330a708f8fSGustavo F. Padovan int timeo = HZ/5; 12340a708f8fSGustavo F. Padovan 12350a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 12360a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 1237a71a0cf4SPeter Hurley while (chan->unacked_frames > 0 && chan->conn) { 12380a708f8fSGustavo F. Padovan if (!timeo) 12390a708f8fSGustavo F. Padovan timeo = HZ/5; 12400a708f8fSGustavo F. Padovan 12410a708f8fSGustavo F. Padovan if (signal_pending(current)) { 12420a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 12430a708f8fSGustavo F. Padovan break; 12440a708f8fSGustavo F. Padovan } 12450a708f8fSGustavo F. Padovan 12460a708f8fSGustavo F. Padovan release_sock(sk); 12470a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 12480a708f8fSGustavo F. Padovan lock_sock(sk); 1249a71a0cf4SPeter Hurley set_current_state(TASK_INTERRUPTIBLE); 12500a708f8fSGustavo F. Padovan 12510a708f8fSGustavo F. Padovan err = sock_error(sk); 12520a708f8fSGustavo F. Padovan if (err) 12530a708f8fSGustavo F. Padovan break; 12540a708f8fSGustavo F. Padovan } 12550a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 12560a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 12570a708f8fSGustavo F. Padovan return err; 12580a708f8fSGustavo F. Padovan } 12590a708f8fSGustavo F. Padovan 1260721c4181SGustavo F. Padovan static void l2cap_monitor_timeout(struct work_struct *work) 12610a708f8fSGustavo F. Padovan { 1262721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 1263721c4181SGustavo F. Padovan monitor_timer.work); 1264525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 12650a708f8fSGustavo F. Padovan 1266525cd185SGustavo F. Padovan BT_DBG("chan %p", chan); 12670a708f8fSGustavo F. Padovan 1268721c4181SGustavo F. Padovan lock_sock(sk); 12692c03a7a4SGustavo F. Padovan if (chan->retry_count >= chan->remote_max_tx) { 12708c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 1271721c4181SGustavo F. Padovan release_sock(sk); 12720a708f8fSGustavo F. Padovan return; 12730a708f8fSGustavo F. Padovan } 12740a708f8fSGustavo F. Padovan 12756a026610SGustavo F. Padovan chan->retry_count++; 12761a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 12770a708f8fSGustavo F. Padovan 1278525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 1279721c4181SGustavo F. Padovan release_sock(sk); 12800a708f8fSGustavo F. Padovan } 12810a708f8fSGustavo F. Padovan 1282721c4181SGustavo F. Padovan static void l2cap_retrans_timeout(struct work_struct *work) 12830a708f8fSGustavo F. Padovan { 1284721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 1285721c4181SGustavo F. Padovan retrans_timer.work); 1286525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 12870a708f8fSGustavo F. Padovan 128849208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 12890a708f8fSGustavo F. Padovan 1290721c4181SGustavo F. Padovan lock_sock(sk); 12916a026610SGustavo F. Padovan chan->retry_count = 1; 12921a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 12930a708f8fSGustavo F. Padovan 1294e2ab4353SGustavo F. Padovan set_bit(CONN_WAIT_F, &chan->conn_state); 12950a708f8fSGustavo F. Padovan 1296525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 1297721c4181SGustavo F. Padovan release_sock(sk); 12980a708f8fSGustavo F. Padovan } 12990a708f8fSGustavo F. Padovan 130042e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan) 13010a708f8fSGustavo F. Padovan { 13020a708f8fSGustavo F. Padovan struct sk_buff *skb; 13030a708f8fSGustavo F. Padovan 130458d35f87SGustavo F. Padovan while ((skb = skb_peek(&chan->tx_q)) && 13056a026610SGustavo F. Padovan chan->unacked_frames) { 130642e5c802SGustavo F. Padovan if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) 13070a708f8fSGustavo F. Padovan break; 13080a708f8fSGustavo F. Padovan 130958d35f87SGustavo F. Padovan skb = skb_dequeue(&chan->tx_q); 13100a708f8fSGustavo F. Padovan kfree_skb(skb); 13110a708f8fSGustavo F. Padovan 13126a026610SGustavo F. Padovan chan->unacked_frames--; 13130a708f8fSGustavo F. Padovan } 13140a708f8fSGustavo F. Padovan 13156a026610SGustavo F. Padovan if (!chan->unacked_frames) 13161a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 13170a708f8fSGustavo F. Padovan } 13180a708f8fSGustavo F. Padovan 131967c9e840SSzymon Janc static void l2cap_streaming_send(struct l2cap_chan *chan) 13200a708f8fSGustavo F. Padovan { 13210a708f8fSGustavo F. Padovan struct sk_buff *skb; 132288843ab0SAndrei Emeltchenko u32 control; 132388843ab0SAndrei Emeltchenko u16 fcs; 13240a708f8fSGustavo F. Padovan 132558d35f87SGustavo F. Padovan while ((skb = skb_dequeue(&chan->tx_q))) { 132688843ab0SAndrei Emeltchenko control = __get_control(chan, skb->data + L2CAP_HDR_SIZE); 1327fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, chan->next_tx_seq); 132888843ab0SAndrei Emeltchenko __put_control(chan, control, skb->data + L2CAP_HDR_SIZE); 13290a708f8fSGustavo F. Padovan 133047d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 133103a51213SAndrei Emeltchenko fcs = crc16(0, (u8 *)skb->data, 133203a51213SAndrei Emeltchenko skb->len - L2CAP_FCS_SIZE); 133303a51213SAndrei Emeltchenko put_unaligned_le16(fcs, 133403a51213SAndrei Emeltchenko skb->data + skb->len - L2CAP_FCS_SIZE); 13350a708f8fSGustavo F. Padovan } 13360a708f8fSGustavo F. Padovan 13374343478fSGustavo F. Padovan l2cap_do_send(chan, skb); 13380a708f8fSGustavo F. Padovan 1339836be934SAndrei Emeltchenko chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); 13400a708f8fSGustavo F. Padovan } 13410a708f8fSGustavo F. Padovan } 13420a708f8fSGustavo F. Padovan 1343fb45de7dSAndrei Emeltchenko static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u16 tx_seq) 13440a708f8fSGustavo F. Padovan { 13450a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 134688843ab0SAndrei Emeltchenko u16 fcs; 134788843ab0SAndrei Emeltchenko u32 control; 13480a708f8fSGustavo F. Padovan 134958d35f87SGustavo F. Padovan skb = skb_peek(&chan->tx_q); 13500a708f8fSGustavo F. Padovan if (!skb) 13510a708f8fSGustavo F. Padovan return; 13520a708f8fSGustavo F. Padovan 1353d1726b6dSSzymon Janc while (bt_cb(skb)->tx_seq != tx_seq) { 135458d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 13550a708f8fSGustavo F. Padovan return; 13560a708f8fSGustavo F. Padovan 1357d1726b6dSSzymon Janc skb = skb_queue_next(&chan->tx_q, skb); 1358d1726b6dSSzymon Janc } 13590a708f8fSGustavo F. Padovan 13602c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 13612c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 13628c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 13630a708f8fSGustavo F. Padovan return; 13640a708f8fSGustavo F. Padovan } 13650a708f8fSGustavo F. Padovan 13660a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 13670a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 136888843ab0SAndrei Emeltchenko 136988843ab0SAndrei Emeltchenko control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE); 13707e0ef6eeSAndrei Emeltchenko control &= __get_sar_mask(chan); 13710a708f8fSGustavo F. Padovan 1372e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 137303f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 13740a708f8fSGustavo F. Padovan 13750b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 1376fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, tx_seq); 13770a708f8fSGustavo F. Padovan 137888843ab0SAndrei Emeltchenko __put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE); 13790a708f8fSGustavo F. Padovan 138047d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 138103a51213SAndrei Emeltchenko fcs = crc16(0, (u8 *)tx_skb->data, 138203a51213SAndrei Emeltchenko tx_skb->len - L2CAP_FCS_SIZE); 138303a51213SAndrei Emeltchenko put_unaligned_le16(fcs, 138403a51213SAndrei Emeltchenko tx_skb->data + tx_skb->len - L2CAP_FCS_SIZE); 13850a708f8fSGustavo F. Padovan } 13860a708f8fSGustavo F. Padovan 13874343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 13880a708f8fSGustavo F. Padovan } 13890a708f8fSGustavo F. Padovan 139067c9e840SSzymon Janc static int l2cap_ertm_send(struct l2cap_chan *chan) 13910a708f8fSGustavo F. Padovan { 13920a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 139388843ab0SAndrei Emeltchenko u16 fcs; 139488843ab0SAndrei Emeltchenko u32 control; 13950a708f8fSGustavo F. Padovan int nsent = 0; 13960a708f8fSGustavo F. Padovan 139789bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 13980a708f8fSGustavo F. Padovan return -ENOTCONN; 13990a708f8fSGustavo F. Padovan 140058d35f87SGustavo F. Padovan while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) { 14010a708f8fSGustavo F. Padovan 14022c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 14032c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 14048c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 14050a708f8fSGustavo F. Padovan break; 14060a708f8fSGustavo F. Padovan } 14070a708f8fSGustavo F. Padovan 14080a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 14090a708f8fSGustavo F. Padovan 14100a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 14110a708f8fSGustavo F. Padovan 141288843ab0SAndrei Emeltchenko control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE); 14137e0ef6eeSAndrei Emeltchenko control &= __get_sar_mask(chan); 14140a708f8fSGustavo F. Padovan 1415e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 141603f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 1417e2ab4353SGustavo F. Padovan 14180b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 1419fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, chan->next_tx_seq); 14200a708f8fSGustavo F. Padovan 142188843ab0SAndrei Emeltchenko __put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE); 14220a708f8fSGustavo F. Padovan 142347d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 142403a51213SAndrei Emeltchenko fcs = crc16(0, (u8 *)skb->data, 142503a51213SAndrei Emeltchenko tx_skb->len - L2CAP_FCS_SIZE); 142603a51213SAndrei Emeltchenko put_unaligned_le16(fcs, skb->data + 142703a51213SAndrei Emeltchenko tx_skb->len - L2CAP_FCS_SIZE); 14280a708f8fSGustavo F. Padovan } 14290a708f8fSGustavo F. Padovan 14304343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 14310a708f8fSGustavo F. Padovan 14321a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 14330a708f8fSGustavo F. Padovan 143442e5c802SGustavo F. Padovan bt_cb(skb)->tx_seq = chan->next_tx_seq; 1435836be934SAndrei Emeltchenko 1436836be934SAndrei Emeltchenko chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); 14370a708f8fSGustavo F. Padovan 14388ed7a0aeSSzymon Janc if (bt_cb(skb)->retries == 1) { 14396a026610SGustavo F. Padovan chan->unacked_frames++; 1440930fa4aeSSzymon Janc 1441930fa4aeSSzymon Janc if (!nsent++) 1442930fa4aeSSzymon Janc __clear_ack_timer(chan); 14438ed7a0aeSSzymon Janc } 144423e9fde2SSuraj Sumangala 14456a026610SGustavo F. Padovan chan->frames_sent++; 14460a708f8fSGustavo F. Padovan 144758d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 144858d35f87SGustavo F. Padovan chan->tx_send_head = NULL; 14490a708f8fSGustavo F. Padovan else 145058d35f87SGustavo F. Padovan chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); 14510a708f8fSGustavo F. Padovan } 14520a708f8fSGustavo F. Padovan 14530a708f8fSGustavo F. Padovan return nsent; 14540a708f8fSGustavo F. Padovan } 14550a708f8fSGustavo F. Padovan 1456525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan) 14570a708f8fSGustavo F. Padovan { 14580a708f8fSGustavo F. Padovan int ret; 14590a708f8fSGustavo F. Padovan 146058d35f87SGustavo F. Padovan if (!skb_queue_empty(&chan->tx_q)) 146158d35f87SGustavo F. Padovan chan->tx_send_head = chan->tx_q.next; 14620a708f8fSGustavo F. Padovan 146342e5c802SGustavo F. Padovan chan->next_tx_seq = chan->expected_ack_seq; 1464525cd185SGustavo F. Padovan ret = l2cap_ertm_send(chan); 14650a708f8fSGustavo F. Padovan return ret; 14660a708f8fSGustavo F. Padovan } 14670a708f8fSGustavo F. Padovan 1468b17e73bbSSzymon Janc static void __l2cap_send_ack(struct l2cap_chan *chan) 14690a708f8fSGustavo F. Padovan { 147088843ab0SAndrei Emeltchenko u32 control = 0; 14710a708f8fSGustavo F. Padovan 14720b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 14730a708f8fSGustavo F. Padovan 1474e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 1475ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 1476e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 1477525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 14780a708f8fSGustavo F. Padovan return; 14790a708f8fSGustavo F. Padovan } 14800a708f8fSGustavo F. Padovan 1481525cd185SGustavo F. Padovan if (l2cap_ertm_send(chan) > 0) 14820a708f8fSGustavo F. Padovan return; 14830a708f8fSGustavo F. Padovan 1484ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 1485525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 14860a708f8fSGustavo F. Padovan } 14870a708f8fSGustavo F. Padovan 1488b17e73bbSSzymon Janc static void l2cap_send_ack(struct l2cap_chan *chan) 1489b17e73bbSSzymon Janc { 1490b17e73bbSSzymon Janc __clear_ack_timer(chan); 1491b17e73bbSSzymon Janc __l2cap_send_ack(chan); 1492b17e73bbSSzymon Janc } 1493b17e73bbSSzymon Janc 1494525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan) 14950a708f8fSGustavo F. Padovan { 14960a708f8fSGustavo F. Padovan struct srej_list *tail; 149788843ab0SAndrei Emeltchenko u32 control; 14980a708f8fSGustavo F. Padovan 1499ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 150003f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 15010a708f8fSGustavo F. Padovan 150239d5a3eeSGustavo F. Padovan tail = list_entry((&chan->srej_l)->prev, struct srej_list, list); 15030b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, tail->tx_seq); 15040a708f8fSGustavo F. Padovan 1505525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 15060a708f8fSGustavo F. Padovan } 15070a708f8fSGustavo F. Padovan 15080952a57aSAndrei Emeltchenko static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, struct msghdr *msg, int len, int count, struct sk_buff *skb) 15090a708f8fSGustavo F. Padovan { 15100952a57aSAndrei Emeltchenko struct l2cap_conn *conn = chan->conn; 15110a708f8fSGustavo F. Padovan struct sk_buff **frag; 15120a708f8fSGustavo F. Padovan int err, sent = 0; 15130a708f8fSGustavo F. Padovan 15140a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) 15150a708f8fSGustavo F. Padovan return -EFAULT; 15160a708f8fSGustavo F. Padovan 15170a708f8fSGustavo F. Padovan sent += count; 15180a708f8fSGustavo F. Padovan len -= count; 15190a708f8fSGustavo F. Padovan 15200a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 15210a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 15220a708f8fSGustavo F. Padovan while (len) { 15230a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 15240a708f8fSGustavo F. Padovan 15252f7719ceSAndrei Emeltchenko *frag = chan->ops->alloc_skb(chan, count, 15262f7719ceSAndrei Emeltchenko msg->msg_flags & MSG_DONTWAIT, &err); 15272f7719ceSAndrei Emeltchenko 15280a708f8fSGustavo F. Padovan if (!*frag) 15290a708f8fSGustavo F. Padovan return err; 15300a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) 15310a708f8fSGustavo F. Padovan return -EFAULT; 15320a708f8fSGustavo F. Padovan 15335e59b791SLuiz Augusto von Dentz (*frag)->priority = skb->priority; 15345e59b791SLuiz Augusto von Dentz 15350a708f8fSGustavo F. Padovan sent += count; 15360a708f8fSGustavo F. Padovan len -= count; 15370a708f8fSGustavo F. Padovan 15380a708f8fSGustavo F. Padovan frag = &(*frag)->next; 15390a708f8fSGustavo F. Padovan } 15400a708f8fSGustavo F. Padovan 15410a708f8fSGustavo F. Padovan return sent; 15420a708f8fSGustavo F. Padovan } 15430a708f8fSGustavo F. Padovan 15445e59b791SLuiz Augusto von Dentz static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, 15455e59b791SLuiz Augusto von Dentz struct msghdr *msg, size_t len, 15465e59b791SLuiz Augusto von Dentz u32 priority) 15470a708f8fSGustavo F. Padovan { 15488c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 15490a708f8fSGustavo F. Padovan struct sk_buff *skb; 155003a51213SAndrei Emeltchenko int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; 15510a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 15520a708f8fSGustavo F. Padovan 15536d5922b0SAndrei Emeltchenko BT_DBG("chan %p len %d priority %u", chan, (int)len, priority); 15540a708f8fSGustavo F. Padovan 15550a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 15562f7719ceSAndrei Emeltchenko 15572f7719ceSAndrei Emeltchenko skb = chan->ops->alloc_skb(chan, count + hlen, 15580a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 15592f7719ceSAndrei Emeltchenko 15600a708f8fSGustavo F. Padovan if (!skb) 15610a708f8fSGustavo F. Padovan return ERR_PTR(err); 15620a708f8fSGustavo F. Padovan 15635e59b791SLuiz Augusto von Dentz skb->priority = priority; 15645e59b791SLuiz Augusto von Dentz 15650a708f8fSGustavo F. Padovan /* Create L2CAP header */ 15660a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1567fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 15680a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 1569fe4128e0SGustavo F. Padovan put_unaligned_le16(chan->psm, skb_put(skb, 2)); 15700a708f8fSGustavo F. Padovan 15710952a57aSAndrei Emeltchenko err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); 15720a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 15730a708f8fSGustavo F. Padovan kfree_skb(skb); 15740a708f8fSGustavo F. Padovan return ERR_PTR(err); 15750a708f8fSGustavo F. Padovan } 15760a708f8fSGustavo F. Padovan return skb; 15770a708f8fSGustavo F. Padovan } 15780a708f8fSGustavo F. Padovan 15795e59b791SLuiz Augusto von Dentz static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, 15805e59b791SLuiz Augusto von Dentz struct msghdr *msg, size_t len, 15815e59b791SLuiz Augusto von Dentz u32 priority) 15820a708f8fSGustavo F. Padovan { 15838c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 15840a708f8fSGustavo F. Padovan struct sk_buff *skb; 15850a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE; 15860a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 15870a708f8fSGustavo F. Padovan 15886d5922b0SAndrei Emeltchenko BT_DBG("chan %p len %d", chan, (int)len); 15890a708f8fSGustavo F. Padovan 15900a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 15912f7719ceSAndrei Emeltchenko 15922f7719ceSAndrei Emeltchenko skb = chan->ops->alloc_skb(chan, count + hlen, 15930a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 15942f7719ceSAndrei Emeltchenko 15950a708f8fSGustavo F. Padovan if (!skb) 15960a708f8fSGustavo F. Padovan return ERR_PTR(err); 15970a708f8fSGustavo F. Padovan 15985e59b791SLuiz Augusto von Dentz skb->priority = priority; 15995e59b791SLuiz Augusto von Dentz 16000a708f8fSGustavo F. Padovan /* Create L2CAP header */ 16010a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1602fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 16030a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 16040a708f8fSGustavo F. Padovan 16050952a57aSAndrei Emeltchenko err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); 16060a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 16070a708f8fSGustavo F. Padovan kfree_skb(skb); 16080a708f8fSGustavo F. Padovan return ERR_PTR(err); 16090a708f8fSGustavo F. Padovan } 16100a708f8fSGustavo F. Padovan return skb; 16110a708f8fSGustavo F. Padovan } 16120a708f8fSGustavo F. Padovan 1613ab0ff76dSLuiz Augusto von Dentz static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, 1614ab0ff76dSLuiz Augusto von Dentz struct msghdr *msg, size_t len, 161588843ab0SAndrei Emeltchenko u32 control, u16 sdulen) 16160a708f8fSGustavo F. Padovan { 16178c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 16180a708f8fSGustavo F. Padovan struct sk_buff *skb; 1619e4ca6d98SAndrei Emeltchenko int err, count, hlen; 16200a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 16210a708f8fSGustavo F. Padovan 16226d5922b0SAndrei Emeltchenko BT_DBG("chan %p len %d", chan, (int)len); 16230a708f8fSGustavo F. Padovan 16240a708f8fSGustavo F. Padovan if (!conn) 16250a708f8fSGustavo F. Padovan return ERR_PTR(-ENOTCONN); 16260a708f8fSGustavo F. Padovan 1627e4ca6d98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 1628e4ca6d98SAndrei Emeltchenko hlen = L2CAP_EXT_HDR_SIZE; 1629e4ca6d98SAndrei Emeltchenko else 1630e4ca6d98SAndrei Emeltchenko hlen = L2CAP_ENH_HDR_SIZE; 1631e4ca6d98SAndrei Emeltchenko 16320a708f8fSGustavo F. Padovan if (sdulen) 163303a51213SAndrei Emeltchenko hlen += L2CAP_SDULEN_SIZE; 16340a708f8fSGustavo F. Padovan 163547d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 163603a51213SAndrei Emeltchenko hlen += L2CAP_FCS_SIZE; 16370a708f8fSGustavo F. Padovan 16380a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 16392f7719ceSAndrei Emeltchenko 16402f7719ceSAndrei Emeltchenko skb = chan->ops->alloc_skb(chan, count + hlen, 16410a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 16422f7719ceSAndrei Emeltchenko 16430a708f8fSGustavo F. Padovan if (!skb) 16440a708f8fSGustavo F. Padovan return ERR_PTR(err); 16450a708f8fSGustavo F. Padovan 16460a708f8fSGustavo F. Padovan /* Create L2CAP header */ 16470a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1648fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 16490a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 165088843ab0SAndrei Emeltchenko 165188843ab0SAndrei Emeltchenko __put_control(chan, control, skb_put(skb, __ctrl_size(chan))); 165288843ab0SAndrei Emeltchenko 16530a708f8fSGustavo F. Padovan if (sdulen) 165403a51213SAndrei Emeltchenko put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); 16550a708f8fSGustavo F. Padovan 16560952a57aSAndrei Emeltchenko err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); 16570a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 16580a708f8fSGustavo F. Padovan kfree_skb(skb); 16590a708f8fSGustavo F. Padovan return ERR_PTR(err); 16600a708f8fSGustavo F. Padovan } 16610a708f8fSGustavo F. Padovan 166247d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 166303a51213SAndrei Emeltchenko put_unaligned_le16(0, skb_put(skb, L2CAP_FCS_SIZE)); 16640a708f8fSGustavo F. Padovan 16650a708f8fSGustavo F. Padovan bt_cb(skb)->retries = 0; 16660a708f8fSGustavo F. Padovan return skb; 16670a708f8fSGustavo F. Padovan } 16680a708f8fSGustavo F. Padovan 166967c9e840SSzymon Janc static int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 16700a708f8fSGustavo F. Padovan { 16710a708f8fSGustavo F. Padovan struct sk_buff *skb; 16720a708f8fSGustavo F. Padovan struct sk_buff_head sar_queue; 167388843ab0SAndrei Emeltchenko u32 control; 16740a708f8fSGustavo F. Padovan size_t size = 0; 16750a708f8fSGustavo F. Padovan 16760a708f8fSGustavo F. Padovan skb_queue_head_init(&sar_queue); 16777e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_START); 167847d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len); 16790a708f8fSGustavo F. Padovan if (IS_ERR(skb)) 16800a708f8fSGustavo F. Padovan return PTR_ERR(skb); 16810a708f8fSGustavo F. Padovan 16820a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 16832c03a7a4SGustavo F. Padovan len -= chan->remote_mps; 16842c03a7a4SGustavo F. Padovan size += chan->remote_mps; 16850a708f8fSGustavo F. Padovan 16860a708f8fSGustavo F. Padovan while (len > 0) { 16870a708f8fSGustavo F. Padovan size_t buflen; 16880a708f8fSGustavo F. Padovan 16892c03a7a4SGustavo F. Padovan if (len > chan->remote_mps) { 16907e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_CONTINUE); 16912c03a7a4SGustavo F. Padovan buflen = chan->remote_mps; 16920a708f8fSGustavo F. Padovan } else { 16937e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_END); 16940a708f8fSGustavo F. Padovan buflen = len; 16950a708f8fSGustavo F. Padovan } 16960a708f8fSGustavo F. Padovan 169747d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0); 16980a708f8fSGustavo F. Padovan if (IS_ERR(skb)) { 16990a708f8fSGustavo F. Padovan skb_queue_purge(&sar_queue); 17000a708f8fSGustavo F. Padovan return PTR_ERR(skb); 17010a708f8fSGustavo F. Padovan } 17020a708f8fSGustavo F. Padovan 17030a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 17040a708f8fSGustavo F. Padovan len -= buflen; 17050a708f8fSGustavo F. Padovan size += buflen; 17060a708f8fSGustavo F. Padovan } 170758d35f87SGustavo F. Padovan skb_queue_splice_tail(&sar_queue, &chan->tx_q); 170858d35f87SGustavo F. Padovan if (chan->tx_send_head == NULL) 170958d35f87SGustavo F. Padovan chan->tx_send_head = sar_queue.next; 17100a708f8fSGustavo F. Padovan 17110a708f8fSGustavo F. Padovan return size; 17120a708f8fSGustavo F. Padovan } 17130a708f8fSGustavo F. Padovan 17145e59b791SLuiz Augusto von Dentz int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, 17155e59b791SLuiz Augusto von Dentz u32 priority) 17169a91a04aSGustavo F. Padovan { 17179a91a04aSGustavo F. Padovan struct sk_buff *skb; 171888843ab0SAndrei Emeltchenko u32 control; 17199a91a04aSGustavo F. Padovan int err; 17209a91a04aSGustavo F. Padovan 17219a91a04aSGustavo F. Padovan /* Connectionless channel */ 1722715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { 17235e59b791SLuiz Augusto von Dentz skb = l2cap_create_connless_pdu(chan, msg, len, priority); 17249a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 17259a91a04aSGustavo F. Padovan return PTR_ERR(skb); 17269a91a04aSGustavo F. Padovan 17279a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 17289a91a04aSGustavo F. Padovan return len; 17299a91a04aSGustavo F. Padovan } 17309a91a04aSGustavo F. Padovan 17319a91a04aSGustavo F. Padovan switch (chan->mode) { 17329a91a04aSGustavo F. Padovan case L2CAP_MODE_BASIC: 17339a91a04aSGustavo F. Padovan /* Check outgoing MTU */ 17349a91a04aSGustavo F. Padovan if (len > chan->omtu) 17359a91a04aSGustavo F. Padovan return -EMSGSIZE; 17369a91a04aSGustavo F. Padovan 17379a91a04aSGustavo F. Padovan /* Create a basic PDU */ 17385e59b791SLuiz Augusto von Dentz skb = l2cap_create_basic_pdu(chan, msg, len, priority); 17399a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 17409a91a04aSGustavo F. Padovan return PTR_ERR(skb); 17419a91a04aSGustavo F. Padovan 17429a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 17439a91a04aSGustavo F. Padovan err = len; 17449a91a04aSGustavo F. Padovan break; 17459a91a04aSGustavo F. Padovan 17469a91a04aSGustavo F. Padovan case L2CAP_MODE_ERTM: 17479a91a04aSGustavo F. Padovan case L2CAP_MODE_STREAMING: 17489a91a04aSGustavo F. Padovan /* Entire SDU fits into one PDU */ 17499a91a04aSGustavo F. Padovan if (len <= chan->remote_mps) { 17507e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_UNSEGMENTED); 17519a91a04aSGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, len, control, 17529a91a04aSGustavo F. Padovan 0); 17539a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 17549a91a04aSGustavo F. Padovan return PTR_ERR(skb); 17559a91a04aSGustavo F. Padovan 17569a91a04aSGustavo F. Padovan __skb_queue_tail(&chan->tx_q, skb); 17579a91a04aSGustavo F. Padovan 17589a91a04aSGustavo F. Padovan if (chan->tx_send_head == NULL) 17599a91a04aSGustavo F. Padovan chan->tx_send_head = skb; 17609a91a04aSGustavo F. Padovan 17619a91a04aSGustavo F. Padovan } else { 17629a91a04aSGustavo F. Padovan /* Segment SDU into multiples PDUs */ 17639a91a04aSGustavo F. Padovan err = l2cap_sar_segment_sdu(chan, msg, len); 17649a91a04aSGustavo F. Padovan if (err < 0) 17659a91a04aSGustavo F. Padovan return err; 17669a91a04aSGustavo F. Padovan } 17679a91a04aSGustavo F. Padovan 17689a91a04aSGustavo F. Padovan if (chan->mode == L2CAP_MODE_STREAMING) { 17699a91a04aSGustavo F. Padovan l2cap_streaming_send(chan); 17709a91a04aSGustavo F. Padovan err = len; 17719a91a04aSGustavo F. Padovan break; 17729a91a04aSGustavo F. Padovan } 17739a91a04aSGustavo F. Padovan 1774e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 1775e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 17769a91a04aSGustavo F. Padovan err = len; 17779a91a04aSGustavo F. Padovan break; 17789a91a04aSGustavo F. Padovan } 17799a91a04aSGustavo F. Padovan 17809a91a04aSGustavo F. Padovan err = l2cap_ertm_send(chan); 17819a91a04aSGustavo F. Padovan if (err >= 0) 17829a91a04aSGustavo F. Padovan err = len; 17839a91a04aSGustavo F. Padovan 17849a91a04aSGustavo F. Padovan break; 17859a91a04aSGustavo F. Padovan 17869a91a04aSGustavo F. Padovan default: 17879a91a04aSGustavo F. Padovan BT_DBG("bad state %1.1x", chan->mode); 17889a91a04aSGustavo F. Padovan err = -EBADFD; 17899a91a04aSGustavo F. Padovan } 17909a91a04aSGustavo F. Padovan 17919a91a04aSGustavo F. Padovan return err; 17929a91a04aSGustavo F. Padovan } 17939a91a04aSGustavo F. Padovan 17940a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */ 17950a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) 17960a708f8fSGustavo F. Padovan { 17970a708f8fSGustavo F. Padovan struct sk_buff *nskb; 179848454079SGustavo F. Padovan struct l2cap_chan *chan; 17990a708f8fSGustavo F. Padovan 18000a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 18010a708f8fSGustavo F. Padovan 18023df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 18033d57dc68SGustavo F. Padovan 18043df91ea2SAndrei Emeltchenko list_for_each_entry(chan, &conn->chan_l, list) { 180548454079SGustavo F. Padovan struct sock *sk = chan->sk; 1806715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_RAW) 18070a708f8fSGustavo F. Padovan continue; 18080a708f8fSGustavo F. Padovan 18090a708f8fSGustavo F. Padovan /* Don't send frame to the socket it came from */ 18100a708f8fSGustavo F. Padovan if (skb->sk == sk) 18110a708f8fSGustavo F. Padovan continue; 18120a708f8fSGustavo F. Padovan nskb = skb_clone(skb, GFP_ATOMIC); 18130a708f8fSGustavo F. Padovan if (!nskb) 18140a708f8fSGustavo F. Padovan continue; 18150a708f8fSGustavo F. Padovan 181623070494SGustavo F. Padovan if (chan->ops->recv(chan->data, nskb)) 18170a708f8fSGustavo F. Padovan kfree_skb(nskb); 18180a708f8fSGustavo F. Padovan } 18193d57dc68SGustavo F. Padovan 18203df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 18210a708f8fSGustavo F. Padovan } 18220a708f8fSGustavo F. Padovan 18230a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */ 18240a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 18250a708f8fSGustavo F. Padovan u8 code, u8 ident, u16 dlen, void *data) 18260a708f8fSGustavo F. Padovan { 18270a708f8fSGustavo F. Padovan struct sk_buff *skb, **frag; 18280a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr *cmd; 18290a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 18300a708f8fSGustavo F. Padovan int len, count; 18310a708f8fSGustavo F. Padovan 18320a708f8fSGustavo F. Padovan BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", 18330a708f8fSGustavo F. Padovan conn, code, ident, dlen); 18340a708f8fSGustavo F. Padovan 18350a708f8fSGustavo F. Padovan len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; 18360a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 18370a708f8fSGustavo F. Padovan 18380a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 18390a708f8fSGustavo F. Padovan if (!skb) 18400a708f8fSGustavo F. Padovan return NULL; 18410a708f8fSGustavo F. Padovan 18420a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 18430a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); 18443300d9a9SClaudio Takahasi 18453300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 18463300d9a9SClaudio Takahasi lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); 18473300d9a9SClaudio Takahasi else 18480a708f8fSGustavo F. Padovan lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); 18490a708f8fSGustavo F. Padovan 18500a708f8fSGustavo F. Padovan cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); 18510a708f8fSGustavo F. Padovan cmd->code = code; 18520a708f8fSGustavo F. Padovan cmd->ident = ident; 18530a708f8fSGustavo F. Padovan cmd->len = cpu_to_le16(dlen); 18540a708f8fSGustavo F. Padovan 18550a708f8fSGustavo F. Padovan if (dlen) { 18560a708f8fSGustavo F. Padovan count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; 18570a708f8fSGustavo F. Padovan memcpy(skb_put(skb, count), data, count); 18580a708f8fSGustavo F. Padovan data += count; 18590a708f8fSGustavo F. Padovan } 18600a708f8fSGustavo F. Padovan 18610a708f8fSGustavo F. Padovan len -= skb->len; 18620a708f8fSGustavo F. Padovan 18630a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 18640a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 18650a708f8fSGustavo F. Padovan while (len) { 18660a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 18670a708f8fSGustavo F. Padovan 18680a708f8fSGustavo F. Padovan *frag = bt_skb_alloc(count, GFP_ATOMIC); 18690a708f8fSGustavo F. Padovan if (!*frag) 18700a708f8fSGustavo F. Padovan goto fail; 18710a708f8fSGustavo F. Padovan 18720a708f8fSGustavo F. Padovan memcpy(skb_put(*frag, count), data, count); 18730a708f8fSGustavo F. Padovan 18740a708f8fSGustavo F. Padovan len -= count; 18750a708f8fSGustavo F. Padovan data += count; 18760a708f8fSGustavo F. Padovan 18770a708f8fSGustavo F. Padovan frag = &(*frag)->next; 18780a708f8fSGustavo F. Padovan } 18790a708f8fSGustavo F. Padovan 18800a708f8fSGustavo F. Padovan return skb; 18810a708f8fSGustavo F. Padovan 18820a708f8fSGustavo F. Padovan fail: 18830a708f8fSGustavo F. Padovan kfree_skb(skb); 18840a708f8fSGustavo F. Padovan return NULL; 18850a708f8fSGustavo F. Padovan } 18860a708f8fSGustavo F. Padovan 18870a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) 18880a708f8fSGustavo F. Padovan { 18890a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 18900a708f8fSGustavo F. Padovan int len; 18910a708f8fSGustavo F. Padovan 18920a708f8fSGustavo F. Padovan len = L2CAP_CONF_OPT_SIZE + opt->len; 18930a708f8fSGustavo F. Padovan *ptr += len; 18940a708f8fSGustavo F. Padovan 18950a708f8fSGustavo F. Padovan *type = opt->type; 18960a708f8fSGustavo F. Padovan *olen = opt->len; 18970a708f8fSGustavo F. Padovan 18980a708f8fSGustavo F. Padovan switch (opt->len) { 18990a708f8fSGustavo F. Padovan case 1: 19000a708f8fSGustavo F. Padovan *val = *((u8 *) opt->val); 19010a708f8fSGustavo F. Padovan break; 19020a708f8fSGustavo F. Padovan 19030a708f8fSGustavo F. Padovan case 2: 19040a708f8fSGustavo F. Padovan *val = get_unaligned_le16(opt->val); 19050a708f8fSGustavo F. Padovan break; 19060a708f8fSGustavo F. Padovan 19070a708f8fSGustavo F. Padovan case 4: 19080a708f8fSGustavo F. Padovan *val = get_unaligned_le32(opt->val); 19090a708f8fSGustavo F. Padovan break; 19100a708f8fSGustavo F. Padovan 19110a708f8fSGustavo F. Padovan default: 19120a708f8fSGustavo F. Padovan *val = (unsigned long) opt->val; 19130a708f8fSGustavo F. Padovan break; 19140a708f8fSGustavo F. Padovan } 19150a708f8fSGustavo F. Padovan 19160a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); 19170a708f8fSGustavo F. Padovan return len; 19180a708f8fSGustavo F. Padovan } 19190a708f8fSGustavo F. Padovan 19200a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) 19210a708f8fSGustavo F. Padovan { 19220a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 19230a708f8fSGustavo F. Padovan 19240a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); 19250a708f8fSGustavo F. Padovan 19260a708f8fSGustavo F. Padovan opt->type = type; 19270a708f8fSGustavo F. Padovan opt->len = len; 19280a708f8fSGustavo F. Padovan 19290a708f8fSGustavo F. Padovan switch (len) { 19300a708f8fSGustavo F. Padovan case 1: 19310a708f8fSGustavo F. Padovan *((u8 *) opt->val) = val; 19320a708f8fSGustavo F. Padovan break; 19330a708f8fSGustavo F. Padovan 19340a708f8fSGustavo F. Padovan case 2: 19350a708f8fSGustavo F. Padovan put_unaligned_le16(val, opt->val); 19360a708f8fSGustavo F. Padovan break; 19370a708f8fSGustavo F. Padovan 19380a708f8fSGustavo F. Padovan case 4: 19390a708f8fSGustavo F. Padovan put_unaligned_le32(val, opt->val); 19400a708f8fSGustavo F. Padovan break; 19410a708f8fSGustavo F. Padovan 19420a708f8fSGustavo F. Padovan default: 19430a708f8fSGustavo F. Padovan memcpy(opt->val, (void *) val, len); 19440a708f8fSGustavo F. Padovan break; 19450a708f8fSGustavo F. Padovan } 19460a708f8fSGustavo F. Padovan 19470a708f8fSGustavo F. Padovan *ptr += L2CAP_CONF_OPT_SIZE + len; 19480a708f8fSGustavo F. Padovan } 19490a708f8fSGustavo F. Padovan 1950f89cef09SAndrei Emeltchenko static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) 1951f89cef09SAndrei Emeltchenko { 1952f89cef09SAndrei Emeltchenko struct l2cap_conf_efs efs; 1953f89cef09SAndrei Emeltchenko 1954f89cef09SAndrei Emeltchenko switch (chan->mode) { 1955f89cef09SAndrei Emeltchenko case L2CAP_MODE_ERTM: 1956f89cef09SAndrei Emeltchenko efs.id = chan->local_id; 1957f89cef09SAndrei Emeltchenko efs.stype = chan->local_stype; 1958f89cef09SAndrei Emeltchenko efs.msdu = cpu_to_le16(chan->local_msdu); 1959f89cef09SAndrei Emeltchenko efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); 1960f89cef09SAndrei Emeltchenko efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); 1961f89cef09SAndrei Emeltchenko efs.flush_to = cpu_to_le32(L2CAP_DEFAULT_FLUSH_TO); 1962f89cef09SAndrei Emeltchenko break; 1963f89cef09SAndrei Emeltchenko 1964f89cef09SAndrei Emeltchenko case L2CAP_MODE_STREAMING: 1965f89cef09SAndrei Emeltchenko efs.id = 1; 1966f89cef09SAndrei Emeltchenko efs.stype = L2CAP_SERV_BESTEFFORT; 1967f89cef09SAndrei Emeltchenko efs.msdu = cpu_to_le16(chan->local_msdu); 1968f89cef09SAndrei Emeltchenko efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); 1969f89cef09SAndrei Emeltchenko efs.acc_lat = 0; 1970f89cef09SAndrei Emeltchenko efs.flush_to = 0; 1971f89cef09SAndrei Emeltchenko break; 1972f89cef09SAndrei Emeltchenko 1973f89cef09SAndrei Emeltchenko default: 1974f89cef09SAndrei Emeltchenko return; 1975f89cef09SAndrei Emeltchenko } 1976f89cef09SAndrei Emeltchenko 1977f89cef09SAndrei Emeltchenko l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), 1978f89cef09SAndrei Emeltchenko (unsigned long) &efs); 1979f89cef09SAndrei Emeltchenko } 1980f89cef09SAndrei Emeltchenko 1981721c4181SGustavo F. Padovan static void l2cap_ack_timeout(struct work_struct *work) 19820a708f8fSGustavo F. Padovan { 1983721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 1984721c4181SGustavo F. Padovan ack_timer.work); 19850a708f8fSGustavo F. Padovan 19862fb9b3d4SGustavo F. Padovan BT_DBG("chan %p", chan); 19872fb9b3d4SGustavo F. Padovan 1988721c4181SGustavo F. Padovan lock_sock(chan->sk); 1989b17e73bbSSzymon Janc __l2cap_send_ack(chan); 1990721c4181SGustavo F. Padovan release_sock(chan->sk); 199109bfb2eeSSzymon Janc 199209bfb2eeSSzymon Janc l2cap_chan_put(chan); 19930a708f8fSGustavo F. Padovan } 19940a708f8fSGustavo F. Padovan 1995525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan) 19960a708f8fSGustavo F. Padovan { 199742e5c802SGustavo F. Padovan chan->expected_ack_seq = 0; 19986a026610SGustavo F. Padovan chan->unacked_frames = 0; 199942e5c802SGustavo F. Padovan chan->buffer_seq = 0; 20006a026610SGustavo F. Padovan chan->num_acked = 0; 20016a026610SGustavo F. Padovan chan->frames_sent = 0; 20020a708f8fSGustavo F. Padovan 2003721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); 2004721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); 2005721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); 20060a708f8fSGustavo F. Padovan 2007f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->srej_q); 20080a708f8fSGustavo F. Padovan 200939d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 20100a708f8fSGustavo F. Padovan } 20110a708f8fSGustavo F. Padovan 20120a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) 20130a708f8fSGustavo F. Padovan { 20140a708f8fSGustavo F. Padovan switch (mode) { 20150a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 20160a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 20170a708f8fSGustavo F. Padovan if (l2cap_mode_supported(mode, remote_feat_mask)) 20180a708f8fSGustavo F. Padovan return mode; 20190a708f8fSGustavo F. Padovan /* fall through */ 20200a708f8fSGustavo F. Padovan default: 20210a708f8fSGustavo F. Padovan return L2CAP_MODE_BASIC; 20220a708f8fSGustavo F. Padovan } 20230a708f8fSGustavo F. Padovan } 20240a708f8fSGustavo F. Padovan 20256327eb98SAndrei Emeltchenko static inline bool __l2cap_ews_supported(struct l2cap_chan *chan) 20266327eb98SAndrei Emeltchenko { 20276327eb98SAndrei Emeltchenko return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; 20286327eb98SAndrei Emeltchenko } 20296327eb98SAndrei Emeltchenko 2030f89cef09SAndrei Emeltchenko static inline bool __l2cap_efs_supported(struct l2cap_chan *chan) 2031f89cef09SAndrei Emeltchenko { 2032f89cef09SAndrei Emeltchenko return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW; 2033f89cef09SAndrei Emeltchenko } 2034f89cef09SAndrei Emeltchenko 20356327eb98SAndrei Emeltchenko static inline void l2cap_txwin_setup(struct l2cap_chan *chan) 20366327eb98SAndrei Emeltchenko { 20376327eb98SAndrei Emeltchenko if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && 2038836be934SAndrei Emeltchenko __l2cap_ews_supported(chan)) { 20396327eb98SAndrei Emeltchenko /* use extended control field */ 20406327eb98SAndrei Emeltchenko set_bit(FLAG_EXT_CTRL, &chan->flags); 2041836be934SAndrei Emeltchenko chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; 2042836be934SAndrei Emeltchenko } else { 20436327eb98SAndrei Emeltchenko chan->tx_win = min_t(u16, chan->tx_win, 20446327eb98SAndrei Emeltchenko L2CAP_DEFAULT_TX_WINDOW); 2045836be934SAndrei Emeltchenko chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; 2046836be934SAndrei Emeltchenko } 20476327eb98SAndrei Emeltchenko } 20486327eb98SAndrei Emeltchenko 2049710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) 20500a708f8fSGustavo F. Padovan { 20510a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 20520c1bc5c6SGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = chan->mode }; 20530a708f8fSGustavo F. Padovan void *ptr = req->data; 2054c8f79162SAndrei Emeltchenko u16 size; 20550a708f8fSGustavo F. Padovan 205649208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 20570a708f8fSGustavo F. Padovan 205873ffa904SGustavo F. Padovan if (chan->num_conf_req || chan->num_conf_rsp) 20590a708f8fSGustavo F. Padovan goto done; 20600a708f8fSGustavo F. Padovan 20610c1bc5c6SGustavo F. Padovan switch (chan->mode) { 20620a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 20630a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 2064c1360a1cSGustavo F. Padovan if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) 20650a708f8fSGustavo F. Padovan break; 20660a708f8fSGustavo F. Padovan 2067f89cef09SAndrei Emeltchenko if (__l2cap_efs_supported(chan)) 2068f89cef09SAndrei Emeltchenko set_bit(FLAG_EFS_ENABLE, &chan->flags); 2069f89cef09SAndrei Emeltchenko 20700a708f8fSGustavo F. Padovan /* fall through */ 20710a708f8fSGustavo F. Padovan default: 20728c1d787bSGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); 20730a708f8fSGustavo F. Padovan break; 20740a708f8fSGustavo F. Padovan } 20750a708f8fSGustavo F. Padovan 20760a708f8fSGustavo F. Padovan done: 20770c1bc5c6SGustavo F. Padovan if (chan->imtu != L2CAP_DEFAULT_MTU) 20780c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 20790a708f8fSGustavo F. Padovan 20800c1bc5c6SGustavo F. Padovan switch (chan->mode) { 20810a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 20828c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && 20838c1d787bSGustavo F. Padovan !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) 20840a708f8fSGustavo F. Padovan break; 20850a708f8fSGustavo F. Padovan 20860a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_BASIC; 20870a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 20880a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 20890a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 20900a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 20910a708f8fSGustavo F. Padovan rfc.max_pdu_size = 0; 20920a708f8fSGustavo F. Padovan 20930a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 20940a708f8fSGustavo F. Padovan (unsigned long) &rfc); 20950a708f8fSGustavo F. Padovan break; 20960a708f8fSGustavo F. Padovan 20970a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 20980a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_ERTM; 209947d1ec61SGustavo F. Padovan rfc.max_transmit = chan->max_tx; 21000a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 21010a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 2102c8f79162SAndrei Emeltchenko 2103c8f79162SAndrei Emeltchenko size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - 2104c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2105c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2106c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2107c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 21080a708f8fSGustavo F. Padovan 21096327eb98SAndrei Emeltchenko l2cap_txwin_setup(chan); 21106327eb98SAndrei Emeltchenko 21116327eb98SAndrei Emeltchenko rfc.txwin_size = min_t(u16, chan->tx_win, 21126327eb98SAndrei Emeltchenko L2CAP_DEFAULT_TX_WINDOW); 21130a708f8fSGustavo F. Padovan 21140a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 21150a708f8fSGustavo F. Padovan (unsigned long) &rfc); 21160a708f8fSGustavo F. Padovan 2117f89cef09SAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) 2118f89cef09SAndrei Emeltchenko l2cap_add_opt_efs(&ptr, chan); 2119f89cef09SAndrei Emeltchenko 21208c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 21210a708f8fSGustavo F. Padovan break; 21220a708f8fSGustavo F. Padovan 212347d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 2124c1360a1cSGustavo F. Padovan test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { 212547d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 212647d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 21270a708f8fSGustavo F. Padovan } 21286327eb98SAndrei Emeltchenko 21296327eb98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 21306327eb98SAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, 21316327eb98SAndrei Emeltchenko chan->tx_win); 21320a708f8fSGustavo F. Padovan break; 21330a708f8fSGustavo F. Padovan 21340a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 21350a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_STREAMING; 21360a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 21370a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 21380a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 21390a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 2140c8f79162SAndrei Emeltchenko 2141c8f79162SAndrei Emeltchenko size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - 2142c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2143c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2144c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2145c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 21460a708f8fSGustavo F. Padovan 21470a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 21480a708f8fSGustavo F. Padovan (unsigned long) &rfc); 21490a708f8fSGustavo F. Padovan 2150f89cef09SAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) 2151f89cef09SAndrei Emeltchenko l2cap_add_opt_efs(&ptr, chan); 2152f89cef09SAndrei Emeltchenko 21538c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 21540a708f8fSGustavo F. Padovan break; 21550a708f8fSGustavo F. Padovan 215647d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 2157c1360a1cSGustavo F. Padovan test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { 215847d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 215947d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 21600a708f8fSGustavo F. Padovan } 21610a708f8fSGustavo F. Padovan break; 21620a708f8fSGustavo F. Padovan } 21630a708f8fSGustavo F. Padovan 2164fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 21650a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0); 21660a708f8fSGustavo F. Padovan 21670a708f8fSGustavo F. Padovan return ptr - data; 21680a708f8fSGustavo F. Padovan } 21690a708f8fSGustavo F. Padovan 217073ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) 21710a708f8fSGustavo F. Padovan { 21720a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 21730a708f8fSGustavo F. Padovan void *ptr = rsp->data; 217473ffa904SGustavo F. Padovan void *req = chan->conf_req; 217573ffa904SGustavo F. Padovan int len = chan->conf_len; 21760a708f8fSGustavo F. Padovan int type, hint, olen; 21770a708f8fSGustavo F. Padovan unsigned long val; 21780a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 217942dceae2SAndrei Emeltchenko struct l2cap_conf_efs efs; 218042dceae2SAndrei Emeltchenko u8 remote_efs = 0; 21810a708f8fSGustavo F. Padovan u16 mtu = L2CAP_DEFAULT_MTU; 21820a708f8fSGustavo F. Padovan u16 result = L2CAP_CONF_SUCCESS; 2183c8f79162SAndrei Emeltchenko u16 size; 21840a708f8fSGustavo F. Padovan 218573ffa904SGustavo F. Padovan BT_DBG("chan %p", chan); 21860a708f8fSGustavo F. Padovan 21870a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 21880a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&req, &type, &olen, &val); 21890a708f8fSGustavo F. Padovan 21900a708f8fSGustavo F. Padovan hint = type & L2CAP_CONF_HINT; 21910a708f8fSGustavo F. Padovan type &= L2CAP_CONF_MASK; 21920a708f8fSGustavo F. Padovan 21930a708f8fSGustavo F. Padovan switch (type) { 21940a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 21950a708f8fSGustavo F. Padovan mtu = val; 21960a708f8fSGustavo F. Padovan break; 21970a708f8fSGustavo F. Padovan 21980a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 21990c1bc5c6SGustavo F. Padovan chan->flush_to = val; 22000a708f8fSGustavo F. Padovan break; 22010a708f8fSGustavo F. Padovan 22020a708f8fSGustavo F. Padovan case L2CAP_CONF_QOS: 22030a708f8fSGustavo F. Padovan break; 22040a708f8fSGustavo F. Padovan 22050a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 22060a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 22070a708f8fSGustavo F. Padovan memcpy(&rfc, (void *) val, olen); 22080a708f8fSGustavo F. Padovan break; 22090a708f8fSGustavo F. Padovan 22100a708f8fSGustavo F. Padovan case L2CAP_CONF_FCS: 22110a708f8fSGustavo F. Padovan if (val == L2CAP_FCS_NONE) 2212c1360a1cSGustavo F. Padovan set_bit(CONF_NO_FCS_RECV, &chan->conf_state); 221342dceae2SAndrei Emeltchenko break; 22140a708f8fSGustavo F. Padovan 221542dceae2SAndrei Emeltchenko case L2CAP_CONF_EFS: 221642dceae2SAndrei Emeltchenko remote_efs = 1; 221742dceae2SAndrei Emeltchenko if (olen == sizeof(efs)) 221842dceae2SAndrei Emeltchenko memcpy(&efs, (void *) val, olen); 22190a708f8fSGustavo F. Padovan break; 22200a708f8fSGustavo F. Padovan 22216327eb98SAndrei Emeltchenko case L2CAP_CONF_EWS: 22226327eb98SAndrei Emeltchenko if (!enable_hs) 22236327eb98SAndrei Emeltchenko return -ECONNREFUSED; 22246327eb98SAndrei Emeltchenko 22256327eb98SAndrei Emeltchenko set_bit(FLAG_EXT_CTRL, &chan->flags); 22266327eb98SAndrei Emeltchenko set_bit(CONF_EWS_RECV, &chan->conf_state); 2227836be934SAndrei Emeltchenko chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; 22286327eb98SAndrei Emeltchenko chan->remote_tx_win = val; 22290a708f8fSGustavo F. Padovan break; 22300a708f8fSGustavo F. Padovan 22310a708f8fSGustavo F. Padovan default: 22320a708f8fSGustavo F. Padovan if (hint) 22330a708f8fSGustavo F. Padovan break; 22340a708f8fSGustavo F. Padovan 22350a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNKNOWN; 22360a708f8fSGustavo F. Padovan *((u8 *) ptr++) = type; 22370a708f8fSGustavo F. Padovan break; 22380a708f8fSGustavo F. Padovan } 22390a708f8fSGustavo F. Padovan } 22400a708f8fSGustavo F. Padovan 224173ffa904SGustavo F. Padovan if (chan->num_conf_rsp || chan->num_conf_req > 1) 22420a708f8fSGustavo F. Padovan goto done; 22430a708f8fSGustavo F. Padovan 22440c1bc5c6SGustavo F. Padovan switch (chan->mode) { 22450a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 22460a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 2247c1360a1cSGustavo F. Padovan if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { 22480c1bc5c6SGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, 22498c1d787bSGustavo F. Padovan chan->conn->feat_mask); 22500a708f8fSGustavo F. Padovan break; 22510a708f8fSGustavo F. Padovan } 22520a708f8fSGustavo F. Padovan 225342dceae2SAndrei Emeltchenko if (remote_efs) { 225442dceae2SAndrei Emeltchenko if (__l2cap_efs_supported(chan)) 225542dceae2SAndrei Emeltchenko set_bit(FLAG_EFS_ENABLE, &chan->flags); 225642dceae2SAndrei Emeltchenko else 225742dceae2SAndrei Emeltchenko return -ECONNREFUSED; 225842dceae2SAndrei Emeltchenko } 225942dceae2SAndrei Emeltchenko 22600c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) 22610a708f8fSGustavo F. Padovan return -ECONNREFUSED; 22620a708f8fSGustavo F. Padovan 22630a708f8fSGustavo F. Padovan break; 22640a708f8fSGustavo F. Padovan } 22650a708f8fSGustavo F. Padovan 22660a708f8fSGustavo F. Padovan done: 22670c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) { 22680a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 22690c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 22700a708f8fSGustavo F. Padovan 227173ffa904SGustavo F. Padovan if (chan->num_conf_rsp == 1) 22720a708f8fSGustavo F. Padovan return -ECONNREFUSED; 22730a708f8fSGustavo F. Padovan 22740a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 22750a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 22760a708f8fSGustavo F. Padovan } 22770a708f8fSGustavo F. Padovan 22780a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) { 22790a708f8fSGustavo F. Padovan /* Configure output options and let the other side know 22800a708f8fSGustavo F. Padovan * which ones we don't like. */ 22810a708f8fSGustavo F. Padovan 22820a708f8fSGustavo F. Padovan if (mtu < L2CAP_DEFAULT_MIN_MTU) 22830a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 22840a708f8fSGustavo F. Padovan else { 22850c1bc5c6SGustavo F. Padovan chan->omtu = mtu; 2286c1360a1cSGustavo F. Padovan set_bit(CONF_MTU_DONE, &chan->conf_state); 22870a708f8fSGustavo F. Padovan } 22880c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); 22890a708f8fSGustavo F. Padovan 229042dceae2SAndrei Emeltchenko if (remote_efs) { 229142dceae2SAndrei Emeltchenko if (chan->local_stype != L2CAP_SERV_NOTRAFIC && 229242dceae2SAndrei Emeltchenko efs.stype != L2CAP_SERV_NOTRAFIC && 229342dceae2SAndrei Emeltchenko efs.stype != chan->local_stype) { 229442dceae2SAndrei Emeltchenko 229542dceae2SAndrei Emeltchenko result = L2CAP_CONF_UNACCEPT; 229642dceae2SAndrei Emeltchenko 229742dceae2SAndrei Emeltchenko if (chan->num_conf_req >= 1) 229842dceae2SAndrei Emeltchenko return -ECONNREFUSED; 229942dceae2SAndrei Emeltchenko 230042dceae2SAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, 230142dceae2SAndrei Emeltchenko sizeof(efs), 230242dceae2SAndrei Emeltchenko (unsigned long) &efs); 23030e8b207eSAndrei Emeltchenko } else { 23043e6b3b95SGustavo F. Padovan /* Send PENDING Conf Rsp */ 23050e8b207eSAndrei Emeltchenko result = L2CAP_CONF_PENDING; 23060e8b207eSAndrei Emeltchenko set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 230742dceae2SAndrei Emeltchenko } 230842dceae2SAndrei Emeltchenko } 230942dceae2SAndrei Emeltchenko 23100a708f8fSGustavo F. Padovan switch (rfc.mode) { 23110a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 231247d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2313c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 23140a708f8fSGustavo F. Padovan break; 23150a708f8fSGustavo F. Padovan 23160a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 23176327eb98SAndrei Emeltchenko if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) 23182c03a7a4SGustavo F. Padovan chan->remote_tx_win = rfc.txwin_size; 23196327eb98SAndrei Emeltchenko else 23206327eb98SAndrei Emeltchenko rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; 23216327eb98SAndrei Emeltchenko 23222c03a7a4SGustavo F. Padovan chan->remote_max_tx = rfc.max_transmit; 23230a708f8fSGustavo F. Padovan 2324c8f79162SAndrei Emeltchenko size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), 2325c8f79162SAndrei Emeltchenko chan->conn->mtu - 2326c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2327c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2328c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2329c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 2330c8f79162SAndrei Emeltchenko chan->remote_mps = size; 23310a708f8fSGustavo F. Padovan 23320a708f8fSGustavo F. Padovan rfc.retrans_timeout = 23330a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO); 23340a708f8fSGustavo F. Padovan rfc.monitor_timeout = 23350a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO); 23360a708f8fSGustavo F. Padovan 2337c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 23380a708f8fSGustavo F. Padovan 23390a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 23400a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 23410a708f8fSGustavo F. Padovan 234242dceae2SAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { 234342dceae2SAndrei Emeltchenko chan->remote_id = efs.id; 234442dceae2SAndrei Emeltchenko chan->remote_stype = efs.stype; 234542dceae2SAndrei Emeltchenko chan->remote_msdu = le16_to_cpu(efs.msdu); 234642dceae2SAndrei Emeltchenko chan->remote_flush_to = 234742dceae2SAndrei Emeltchenko le32_to_cpu(efs.flush_to); 234842dceae2SAndrei Emeltchenko chan->remote_acc_lat = 234942dceae2SAndrei Emeltchenko le32_to_cpu(efs.acc_lat); 235042dceae2SAndrei Emeltchenko chan->remote_sdu_itime = 235142dceae2SAndrei Emeltchenko le32_to_cpu(efs.sdu_itime); 235242dceae2SAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, 235342dceae2SAndrei Emeltchenko sizeof(efs), (unsigned long) &efs); 235442dceae2SAndrei Emeltchenko } 23550a708f8fSGustavo F. Padovan break; 23560a708f8fSGustavo F. Padovan 23570a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 2358c8f79162SAndrei Emeltchenko size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), 2359c8f79162SAndrei Emeltchenko chan->conn->mtu - 2360c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2361c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2362c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2363c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 2364c8f79162SAndrei Emeltchenko chan->remote_mps = size; 23650a708f8fSGustavo F. Padovan 2366c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 23670a708f8fSGustavo F. Padovan 23680a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 23690a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 23700a708f8fSGustavo F. Padovan 23710a708f8fSGustavo F. Padovan break; 23720a708f8fSGustavo F. Padovan 23730a708f8fSGustavo F. Padovan default: 23740a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 23750a708f8fSGustavo F. Padovan 23760a708f8fSGustavo F. Padovan memset(&rfc, 0, sizeof(rfc)); 23770c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 23780a708f8fSGustavo F. Padovan } 23790a708f8fSGustavo F. Padovan 23800a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) 2381c1360a1cSGustavo F. Padovan set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 23820a708f8fSGustavo F. Padovan } 2383fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 23840a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 23850a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(0x0000); 23860a708f8fSGustavo F. Padovan 23870a708f8fSGustavo F. Padovan return ptr - data; 23880a708f8fSGustavo F. Padovan } 23890a708f8fSGustavo F. Padovan 2390b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result) 23910a708f8fSGustavo F. Padovan { 23920a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 23930a708f8fSGustavo F. Padovan void *ptr = req->data; 23940a708f8fSGustavo F. Padovan int type, olen; 23950a708f8fSGustavo F. Padovan unsigned long val; 239636e999a8SMat Martineau struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 239766af7aafSAndrei Emeltchenko struct l2cap_conf_efs efs; 23980a708f8fSGustavo F. Padovan 2399fe4128e0SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data); 24000a708f8fSGustavo F. Padovan 24010a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 24020a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 24030a708f8fSGustavo F. Padovan 24040a708f8fSGustavo F. Padovan switch (type) { 24050a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 24060a708f8fSGustavo F. Padovan if (val < L2CAP_DEFAULT_MIN_MTU) { 24070a708f8fSGustavo F. Padovan *result = L2CAP_CONF_UNACCEPT; 24080c1bc5c6SGustavo F. Padovan chan->imtu = L2CAP_DEFAULT_MIN_MTU; 24090a708f8fSGustavo F. Padovan } else 24100c1bc5c6SGustavo F. Padovan chan->imtu = val; 24110c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 24120a708f8fSGustavo F. Padovan break; 24130a708f8fSGustavo F. Padovan 24140a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 24150c1bc5c6SGustavo F. Padovan chan->flush_to = val; 24160a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 24170c1bc5c6SGustavo F. Padovan 2, chan->flush_to); 24180a708f8fSGustavo F. Padovan break; 24190a708f8fSGustavo F. Padovan 24200a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 24210a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 24220a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 24230a708f8fSGustavo F. Padovan 2424c1360a1cSGustavo F. Padovan if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && 24250c1bc5c6SGustavo F. Padovan rfc.mode != chan->mode) 24260a708f8fSGustavo F. Padovan return -ECONNREFUSED; 24270a708f8fSGustavo F. Padovan 242847d1ec61SGustavo F. Padovan chan->fcs = 0; 24290a708f8fSGustavo F. Padovan 24300a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 24310a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 24320a708f8fSGustavo F. Padovan break; 24336327eb98SAndrei Emeltchenko 24346327eb98SAndrei Emeltchenko case L2CAP_CONF_EWS: 24356327eb98SAndrei Emeltchenko chan->tx_win = min_t(u16, val, 24366327eb98SAndrei Emeltchenko L2CAP_DEFAULT_EXT_WINDOW); 24373e6b3b95SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, 24383e6b3b95SGustavo F. Padovan chan->tx_win); 24396327eb98SAndrei Emeltchenko break; 244066af7aafSAndrei Emeltchenko 244166af7aafSAndrei Emeltchenko case L2CAP_CONF_EFS: 244266af7aafSAndrei Emeltchenko if (olen == sizeof(efs)) 244366af7aafSAndrei Emeltchenko memcpy(&efs, (void *)val, olen); 244466af7aafSAndrei Emeltchenko 244566af7aafSAndrei Emeltchenko if (chan->local_stype != L2CAP_SERV_NOTRAFIC && 244666af7aafSAndrei Emeltchenko efs.stype != L2CAP_SERV_NOTRAFIC && 244766af7aafSAndrei Emeltchenko efs.stype != chan->local_stype) 244866af7aafSAndrei Emeltchenko return -ECONNREFUSED; 244966af7aafSAndrei Emeltchenko 245066af7aafSAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, 245166af7aafSAndrei Emeltchenko sizeof(efs), (unsigned long) &efs); 245266af7aafSAndrei Emeltchenko break; 24530a708f8fSGustavo F. Padovan } 24540a708f8fSGustavo F. Padovan } 24550a708f8fSGustavo F. Padovan 24560c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) 24570a708f8fSGustavo F. Padovan return -ECONNREFUSED; 24580a708f8fSGustavo F. Padovan 24590c1bc5c6SGustavo F. Padovan chan->mode = rfc.mode; 24600a708f8fSGustavo F. Padovan 24610e8b207eSAndrei Emeltchenko if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { 24620a708f8fSGustavo F. Padovan switch (rfc.mode) { 24630a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 246447d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 246547d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 246647d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 246766af7aafSAndrei Emeltchenko 246866af7aafSAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { 246966af7aafSAndrei Emeltchenko chan->local_msdu = le16_to_cpu(efs.msdu); 247066af7aafSAndrei Emeltchenko chan->local_sdu_itime = 247166af7aafSAndrei Emeltchenko le32_to_cpu(efs.sdu_itime); 247266af7aafSAndrei Emeltchenko chan->local_acc_lat = le32_to_cpu(efs.acc_lat); 247366af7aafSAndrei Emeltchenko chan->local_flush_to = 247466af7aafSAndrei Emeltchenko le32_to_cpu(efs.flush_to); 247566af7aafSAndrei Emeltchenko } 24760a708f8fSGustavo F. Padovan break; 247766af7aafSAndrei Emeltchenko 24780a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 247947d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 24800a708f8fSGustavo F. Padovan } 24810a708f8fSGustavo F. Padovan } 24820a708f8fSGustavo F. Padovan 2483fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 24840a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0x0000); 24850a708f8fSGustavo F. Padovan 24860a708f8fSGustavo F. Padovan return ptr - data; 24870a708f8fSGustavo F. Padovan } 24880a708f8fSGustavo F. Padovan 2489fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags) 24900a708f8fSGustavo F. Padovan { 24910a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 24920a708f8fSGustavo F. Padovan void *ptr = rsp->data; 24930a708f8fSGustavo F. Padovan 2494fe4128e0SGustavo F. Padovan BT_DBG("chan %p", chan); 24950a708f8fSGustavo F. Padovan 2496fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 24970a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 24980a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(flags); 24990a708f8fSGustavo F. Padovan 25000a708f8fSGustavo F. Padovan return ptr - data; 25010a708f8fSGustavo F. Padovan } 25020a708f8fSGustavo F. Padovan 25038c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) 2504710f9b0aSGustavo F. Padovan { 2505710f9b0aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 25068c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 2507710f9b0aSGustavo F. Padovan u8 buf[128]; 2508710f9b0aSGustavo F. Padovan 2509fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 2510fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 2511710f9b0aSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 2512710f9b0aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 2513710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 2514710f9b0aSGustavo F. Padovan L2CAP_CONN_RSP, sizeof(rsp), &rsp); 2515710f9b0aSGustavo F. Padovan 2516c1360a1cSGustavo F. Padovan if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) 2517710f9b0aSGustavo F. Padovan return; 2518710f9b0aSGustavo F. Padovan 2519710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 2520710f9b0aSGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 2521710f9b0aSGustavo F. Padovan chan->num_conf_req++; 2522710f9b0aSGustavo F. Padovan } 2523710f9b0aSGustavo F. Padovan 252447d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len) 25250a708f8fSGustavo F. Padovan { 25260a708f8fSGustavo F. Padovan int type, olen; 25270a708f8fSGustavo F. Padovan unsigned long val; 25280a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 25290a708f8fSGustavo F. Padovan 253047d1ec61SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); 25310a708f8fSGustavo F. Padovan 25320c1bc5c6SGustavo F. Padovan if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) 25330a708f8fSGustavo F. Padovan return; 25340a708f8fSGustavo F. Padovan 25350a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 25360a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 25370a708f8fSGustavo F. Padovan 25380a708f8fSGustavo F. Padovan switch (type) { 25390a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 25400a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 25410a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 25420a708f8fSGustavo F. Padovan goto done; 25430a708f8fSGustavo F. Padovan } 25440a708f8fSGustavo F. Padovan } 25450a708f8fSGustavo F. Padovan 254636e999a8SMat Martineau /* Use sane default values in case a misbehaving remote device 254736e999a8SMat Martineau * did not send an RFC option. 254836e999a8SMat Martineau */ 254936e999a8SMat Martineau rfc.mode = chan->mode; 255036e999a8SMat Martineau rfc.retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); 255136e999a8SMat Martineau rfc.monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); 255236e999a8SMat Martineau rfc.max_pdu_size = cpu_to_le16(chan->imtu); 255336e999a8SMat Martineau 255436e999a8SMat Martineau BT_ERR("Expected RFC option was not found, using defaults"); 255536e999a8SMat Martineau 25560a708f8fSGustavo F. Padovan done: 25570a708f8fSGustavo F. Padovan switch (rfc.mode) { 25580a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 255947d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 256047d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 256147d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 25620a708f8fSGustavo F. Padovan break; 25630a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 256447d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 25650a708f8fSGustavo F. Padovan } 25660a708f8fSGustavo F. Padovan } 25670a708f8fSGustavo F. Padovan 25680a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 25690a708f8fSGustavo F. Padovan { 2570e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; 25710a708f8fSGustavo F. Padovan 2572e2fd318eSIlia Kolomisnky if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) 25730a708f8fSGustavo F. Padovan return 0; 25740a708f8fSGustavo F. Padovan 25750a708f8fSGustavo F. Padovan if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && 25760a708f8fSGustavo F. Padovan cmd->ident == conn->info_ident) { 257717cd3f37SUlisses Furquim cancel_delayed_work(&conn->info_timer); 25780a708f8fSGustavo F. Padovan 25790a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 25800a708f8fSGustavo F. Padovan conn->info_ident = 0; 25810a708f8fSGustavo F. Padovan 25820a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 25830a708f8fSGustavo F. Padovan } 25840a708f8fSGustavo F. Padovan 25850a708f8fSGustavo F. Padovan return 0; 25860a708f8fSGustavo F. Padovan } 25870a708f8fSGustavo F. Padovan 25880a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 25890a708f8fSGustavo F. Padovan { 25900a708f8fSGustavo F. Padovan struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; 25910a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 259223691d75SGustavo F. Padovan struct l2cap_chan *chan = NULL, *pchan; 25930a708f8fSGustavo F. Padovan struct sock *parent, *sk = NULL; 25940a708f8fSGustavo F. Padovan int result, status = L2CAP_CS_NO_INFO; 25950a708f8fSGustavo F. Padovan 25960a708f8fSGustavo F. Padovan u16 dcid = 0, scid = __le16_to_cpu(req->scid); 25970a708f8fSGustavo F. Padovan __le16 psm = req->psm; 25980a708f8fSGustavo F. Padovan 25990a708f8fSGustavo F. Padovan BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid); 26000a708f8fSGustavo F. Padovan 26010a708f8fSGustavo F. Padovan /* Check if we have socket listening on psm */ 260223691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src); 260323691d75SGustavo F. Padovan if (!pchan) { 26040a708f8fSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 26050a708f8fSGustavo F. Padovan goto sendresp; 26060a708f8fSGustavo F. Padovan } 26070a708f8fSGustavo F. Padovan 260823691d75SGustavo F. Padovan parent = pchan->sk; 260923691d75SGustavo F. Padovan 26103df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 2611aa2ac881SGustavo F. Padovan lock_sock(parent); 26120a708f8fSGustavo F. Padovan 26130a708f8fSGustavo F. Padovan /* Check if the ACL is secure enough (if not SDP) */ 26140a708f8fSGustavo F. Padovan if (psm != cpu_to_le16(0x0001) && 26150a708f8fSGustavo F. Padovan !hci_conn_check_link_mode(conn->hcon)) { 26169f5a0d7bSAndrei Emeltchenko conn->disc_reason = HCI_ERROR_AUTH_FAILURE; 26170a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 26180a708f8fSGustavo F. Padovan goto response; 26190a708f8fSGustavo F. Padovan } 26200a708f8fSGustavo F. Padovan 26210a708f8fSGustavo F. Padovan result = L2CAP_CR_NO_MEM; 26220a708f8fSGustavo F. Padovan 26230a708f8fSGustavo F. Padovan /* Check for backlog size */ 26240a708f8fSGustavo F. Padovan if (sk_acceptq_is_full(parent)) { 26250a708f8fSGustavo F. Padovan BT_DBG("backlog full %d", parent->sk_ack_backlog); 26260a708f8fSGustavo F. Padovan goto response; 26270a708f8fSGustavo F. Padovan } 26280a708f8fSGustavo F. Padovan 262980808e43SGustavo F. Padovan chan = pchan->ops->new_connection(pchan->data); 263080808e43SGustavo F. Padovan if (!chan) 26310a708f8fSGustavo F. Padovan goto response; 26320a708f8fSGustavo F. Padovan 263380808e43SGustavo F. Padovan sk = chan->sk; 263480808e43SGustavo F. Padovan 26350a708f8fSGustavo F. Padovan /* Check if we already have channel with that dcid */ 2636baa7e1faSGustavo F. Padovan if (__l2cap_get_chan_by_dcid(conn, scid)) { 26370a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 2638ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 26390a708f8fSGustavo F. Padovan goto response; 26400a708f8fSGustavo F. Padovan } 26410a708f8fSGustavo F. Padovan 26420a708f8fSGustavo F. Padovan hci_conn_hold(conn->hcon); 26430a708f8fSGustavo F. Padovan 26440a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->src, conn->src); 26450a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->dst, conn->dst); 2646fe4128e0SGustavo F. Padovan chan->psm = psm; 2647fe4128e0SGustavo F. Padovan chan->dcid = scid; 26480a708f8fSGustavo F. Padovan 2649d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 2650d1010240SGustavo F. Padovan 26513d57dc68SGustavo F. Padovan l2cap_chan_add(conn, chan); 265248454079SGustavo F. Padovan 2653fe4128e0SGustavo F. Padovan dcid = chan->scid; 26540a708f8fSGustavo F. Padovan 2655c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 26560a708f8fSGustavo F. Padovan 2657fc7f8a7eSGustavo F. Padovan chan->ident = cmd->ident; 26580a708f8fSGustavo F. Padovan 26590a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { 2660d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan)) { 26610a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 26620e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECT2); 26630a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 26640a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHOR_PEND; 26650a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 26660a708f8fSGustavo F. Padovan } else { 26670e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONFIG); 26680a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 26690a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 26700a708f8fSGustavo F. Padovan } 26710a708f8fSGustavo F. Padovan } else { 26720e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECT2); 26730a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 26740a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHEN_PEND; 26750a708f8fSGustavo F. Padovan } 26760a708f8fSGustavo F. Padovan } else { 26770e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONNECT2); 26780a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 26790a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 26800a708f8fSGustavo F. Padovan } 26810a708f8fSGustavo F. Padovan 26820a708f8fSGustavo F. Padovan response: 2683aa2ac881SGustavo F. Padovan release_sock(parent); 26843df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 26850a708f8fSGustavo F. Padovan 26860a708f8fSGustavo F. Padovan sendresp: 26870a708f8fSGustavo F. Padovan rsp.scid = cpu_to_le16(scid); 26880a708f8fSGustavo F. Padovan rsp.dcid = cpu_to_le16(dcid); 26890a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 26900a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(status); 26910a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); 26920a708f8fSGustavo F. Padovan 26930a708f8fSGustavo F. Padovan if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { 26940a708f8fSGustavo F. Padovan struct l2cap_info_req info; 26950a708f8fSGustavo F. Padovan info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 26960a708f8fSGustavo F. Padovan 26970a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 26980a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 26990a708f8fSGustavo F. Padovan 2700030013d8SGustavo F. Padovan schedule_delayed_work(&conn->info_timer, 27010a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 27020a708f8fSGustavo F. Padovan 27030a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 27040a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(info), &info); 27050a708f8fSGustavo F. Padovan } 27060a708f8fSGustavo F. Padovan 2707c1360a1cSGustavo F. Padovan if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && 27080a708f8fSGustavo F. Padovan result == L2CAP_CR_SUCCESS) { 27090a708f8fSGustavo F. Padovan u8 buf[128]; 2710c1360a1cSGustavo F. Padovan set_bit(CONF_REQ_SENT, &chan->conf_state); 27110a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 271273ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 271373ffa904SGustavo F. Padovan chan->num_conf_req++; 27140a708f8fSGustavo F. Padovan } 27150a708f8fSGustavo F. Padovan 27160a708f8fSGustavo F. Padovan return 0; 27170a708f8fSGustavo F. Padovan } 27180a708f8fSGustavo F. Padovan 27190a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 27200a708f8fSGustavo F. Padovan { 27210a708f8fSGustavo F. Padovan struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; 27220a708f8fSGustavo F. Padovan u16 scid, dcid, result, status; 272348454079SGustavo F. Padovan struct l2cap_chan *chan; 27240a708f8fSGustavo F. Padovan struct sock *sk; 27250a708f8fSGustavo F. Padovan u8 req[128]; 27263df91ea2SAndrei Emeltchenko int err; 27270a708f8fSGustavo F. Padovan 27280a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 27290a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 27300a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 27310a708f8fSGustavo F. Padovan status = __le16_to_cpu(rsp->status); 27320a708f8fSGustavo F. Padovan 27331b009c98SAndrei Emeltchenko BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", 27341b009c98SAndrei Emeltchenko dcid, scid, result, status); 27350a708f8fSGustavo F. Padovan 27363df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 27373df91ea2SAndrei Emeltchenko 27380a708f8fSGustavo F. Padovan if (scid) { 27393df91ea2SAndrei Emeltchenko chan = __l2cap_get_chan_by_scid(conn, scid); 27403df91ea2SAndrei Emeltchenko if (!chan) { 27413df91ea2SAndrei Emeltchenko err = -EFAULT; 27423df91ea2SAndrei Emeltchenko goto unlock; 27433df91ea2SAndrei Emeltchenko } 27440a708f8fSGustavo F. Padovan } else { 27453df91ea2SAndrei Emeltchenko chan = __l2cap_get_chan_by_ident(conn, cmd->ident); 27463df91ea2SAndrei Emeltchenko if (!chan) { 27473df91ea2SAndrei Emeltchenko err = -EFAULT; 27483df91ea2SAndrei Emeltchenko goto unlock; 27493df91ea2SAndrei Emeltchenko } 27500a708f8fSGustavo F. Padovan } 27510a708f8fSGustavo F. Padovan 27523df91ea2SAndrei Emeltchenko err = 0; 27533df91ea2SAndrei Emeltchenko 275448454079SGustavo F. Padovan sk = chan->sk; 27553df91ea2SAndrei Emeltchenko lock_sock(sk); 275648454079SGustavo F. Padovan 27570a708f8fSGustavo F. Padovan switch (result) { 27580a708f8fSGustavo F. Padovan case L2CAP_CR_SUCCESS: 275989bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONFIG); 2760fc7f8a7eSGustavo F. Padovan chan->ident = 0; 2761fe4128e0SGustavo F. Padovan chan->dcid = dcid; 2762c1360a1cSGustavo F. Padovan clear_bit(CONF_CONNECT_PEND, &chan->conf_state); 27630a708f8fSGustavo F. Padovan 2764c1360a1cSGustavo F. Padovan if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) 27650a708f8fSGustavo F. Padovan break; 27660a708f8fSGustavo F. Padovan 27670a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 276873ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, req), req); 276973ffa904SGustavo F. Padovan chan->num_conf_req++; 27700a708f8fSGustavo F. Padovan break; 27710a708f8fSGustavo F. Padovan 27720a708f8fSGustavo F. Padovan case L2CAP_CR_PEND: 2773c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 27740a708f8fSGustavo F. Padovan break; 27750a708f8fSGustavo F. Padovan 27760a708f8fSGustavo F. Padovan default: 277748454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNREFUSED); 27780a708f8fSGustavo F. Padovan break; 27790a708f8fSGustavo F. Padovan } 27800a708f8fSGustavo F. Padovan 2781aa2ac881SGustavo F. Padovan release_sock(sk); 27823df91ea2SAndrei Emeltchenko 27833df91ea2SAndrei Emeltchenko unlock: 27843df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 27853df91ea2SAndrei Emeltchenko 27863df91ea2SAndrei Emeltchenko return err; 27870a708f8fSGustavo F. Padovan } 27880a708f8fSGustavo F. Padovan 278947d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan) 27900a708f8fSGustavo F. Padovan { 27910a708f8fSGustavo F. Padovan /* FCS is enabled only in ERTM or streaming mode, if one or both 27920a708f8fSGustavo F. Padovan * sides request it. 27930a708f8fSGustavo F. Padovan */ 27940c1bc5c6SGustavo F. Padovan if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) 279547d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2796c1360a1cSGustavo F. Padovan else if (!test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) 279747d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_CRC16; 27980a708f8fSGustavo F. Padovan } 27990a708f8fSGustavo F. Padovan 28000a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 28010a708f8fSGustavo F. Padovan { 28020a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; 28030a708f8fSGustavo F. Padovan u16 dcid, flags; 28040a708f8fSGustavo F. Padovan u8 rsp[64]; 280548454079SGustavo F. Padovan struct l2cap_chan *chan; 28060a708f8fSGustavo F. Padovan struct sock *sk; 28070a708f8fSGustavo F. Padovan int len; 28080a708f8fSGustavo F. Padovan 28090a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 28100a708f8fSGustavo F. Padovan flags = __le16_to_cpu(req->flags); 28110a708f8fSGustavo F. Padovan 28120a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); 28130a708f8fSGustavo F. Padovan 2814baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 281548454079SGustavo F. Padovan if (!chan) 28160a708f8fSGustavo F. Padovan return -ENOENT; 28170a708f8fSGustavo F. Padovan 281848454079SGustavo F. Padovan sk = chan->sk; 28193df91ea2SAndrei Emeltchenko lock_sock(sk); 282048454079SGustavo F. Padovan 2821033b1142SDavid S. Miller if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { 2822e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_cid rej; 28230a708f8fSGustavo F. Padovan 2824e2fd318eSIlia Kolomisnky rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID); 2825e2fd318eSIlia Kolomisnky rej.scid = cpu_to_le16(chan->scid); 2826e2fd318eSIlia Kolomisnky rej.dcid = cpu_to_le16(chan->dcid); 2827e2fd318eSIlia Kolomisnky 28280a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, 28290a708f8fSGustavo F. Padovan sizeof(rej), &rej); 28300a708f8fSGustavo F. Padovan goto unlock; 28310a708f8fSGustavo F. Padovan } 28320a708f8fSGustavo F. Padovan 28330a708f8fSGustavo F. Padovan /* Reject if config buffer is too small. */ 28340a708f8fSGustavo F. Padovan len = cmd_len - sizeof(*req); 28357ac28817SDan Rosenberg if (len < 0 || chan->conf_len + len > sizeof(chan->conf_req)) { 28360a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2837fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 28380a708f8fSGustavo F. Padovan L2CAP_CONF_REJECT, flags), rsp); 28390a708f8fSGustavo F. Padovan goto unlock; 28400a708f8fSGustavo F. Padovan } 28410a708f8fSGustavo F. Padovan 28420a708f8fSGustavo F. Padovan /* Store config. */ 284373ffa904SGustavo F. Padovan memcpy(chan->conf_req + chan->conf_len, req->data, len); 284473ffa904SGustavo F. Padovan chan->conf_len += len; 28450a708f8fSGustavo F. Padovan 28460a708f8fSGustavo F. Padovan if (flags & 0x0001) { 28470a708f8fSGustavo F. Padovan /* Incomplete config. Send empty response. */ 28480a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2849fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 28500a708f8fSGustavo F. Padovan L2CAP_CONF_SUCCESS, 0x0001), rsp); 28510a708f8fSGustavo F. Padovan goto unlock; 28520a708f8fSGustavo F. Padovan } 28530a708f8fSGustavo F. Padovan 28540a708f8fSGustavo F. Padovan /* Complete config. */ 285573ffa904SGustavo F. Padovan len = l2cap_parse_conf_req(chan, rsp); 28560a708f8fSGustavo F. Padovan if (len < 0) { 2857e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 28580a708f8fSGustavo F. Padovan goto unlock; 28590a708f8fSGustavo F. Padovan } 28600a708f8fSGustavo F. Padovan 28610a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); 286273ffa904SGustavo F. Padovan chan->num_conf_rsp++; 28630a708f8fSGustavo F. Padovan 28640a708f8fSGustavo F. Padovan /* Reset config buffer. */ 286573ffa904SGustavo F. Padovan chan->conf_len = 0; 28660a708f8fSGustavo F. Padovan 2867c1360a1cSGustavo F. Padovan if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) 28680a708f8fSGustavo F. Padovan goto unlock; 28690a708f8fSGustavo F. Padovan 2870c1360a1cSGustavo F. Padovan if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { 287147d1ec61SGustavo F. Padovan set_default_fcs(chan); 28720a708f8fSGustavo F. Padovan 287389bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 28740a708f8fSGustavo F. Padovan 287542e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 287642e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 287758d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 28780c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2879525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 28800a708f8fSGustavo F. Padovan 2881cf4cd009SAndrei Emeltchenko l2cap_chan_ready(chan); 28820a708f8fSGustavo F. Padovan goto unlock; 28830a708f8fSGustavo F. Padovan } 28840a708f8fSGustavo F. Padovan 2885c1360a1cSGustavo F. Padovan if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { 28860a708f8fSGustavo F. Padovan u8 buf[64]; 28870a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 288873ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 288973ffa904SGustavo F. Padovan chan->num_conf_req++; 28900a708f8fSGustavo F. Padovan } 28910a708f8fSGustavo F. Padovan 28920e8b207eSAndrei Emeltchenko /* Got Conf Rsp PENDING from remote side and asume we sent 28930e8b207eSAndrei Emeltchenko Conf Rsp PENDING in the code above */ 28940e8b207eSAndrei Emeltchenko if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && 28950e8b207eSAndrei Emeltchenko test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { 28960e8b207eSAndrei Emeltchenko 28970e8b207eSAndrei Emeltchenko /* check compatibility */ 28980e8b207eSAndrei Emeltchenko 28990e8b207eSAndrei Emeltchenko clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 29000e8b207eSAndrei Emeltchenko set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 29010e8b207eSAndrei Emeltchenko 29020e8b207eSAndrei Emeltchenko l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 29030e8b207eSAndrei Emeltchenko l2cap_build_conf_rsp(chan, rsp, 29040e8b207eSAndrei Emeltchenko L2CAP_CONF_SUCCESS, 0x0000), rsp); 29050e8b207eSAndrei Emeltchenko } 29060e8b207eSAndrei Emeltchenko 29070a708f8fSGustavo F. Padovan unlock: 2908aa2ac881SGustavo F. Padovan release_sock(sk); 29090a708f8fSGustavo F. Padovan return 0; 29100a708f8fSGustavo F. Padovan } 29110a708f8fSGustavo F. Padovan 29120a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 29130a708f8fSGustavo F. Padovan { 29140a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; 29150a708f8fSGustavo F. Padovan u16 scid, flags, result; 291648454079SGustavo F. Padovan struct l2cap_chan *chan; 29170a708f8fSGustavo F. Padovan struct sock *sk; 29180a708f8fSGustavo F. Padovan int len = cmd->len - sizeof(*rsp); 29190a708f8fSGustavo F. Padovan 29200a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 29210a708f8fSGustavo F. Padovan flags = __le16_to_cpu(rsp->flags); 29220a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 29230a708f8fSGustavo F. Padovan 29240a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", 29250a708f8fSGustavo F. Padovan scid, flags, result); 29260a708f8fSGustavo F. Padovan 2927baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 292848454079SGustavo F. Padovan if (!chan) 29290a708f8fSGustavo F. Padovan return 0; 29300a708f8fSGustavo F. Padovan 293148454079SGustavo F. Padovan sk = chan->sk; 29323df91ea2SAndrei Emeltchenko lock_sock(sk); 293348454079SGustavo F. Padovan 29340a708f8fSGustavo F. Padovan switch (result) { 29350a708f8fSGustavo F. Padovan case L2CAP_CONF_SUCCESS: 293647d1ec61SGustavo F. Padovan l2cap_conf_rfc_get(chan, rsp->data, len); 29370e8b207eSAndrei Emeltchenko clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); 29380a708f8fSGustavo F. Padovan break; 29390a708f8fSGustavo F. Padovan 29400e8b207eSAndrei Emeltchenko case L2CAP_CONF_PENDING: 29410e8b207eSAndrei Emeltchenko set_bit(CONF_REM_CONF_PEND, &chan->conf_state); 29420e8b207eSAndrei Emeltchenko 29430e8b207eSAndrei Emeltchenko if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { 29440e8b207eSAndrei Emeltchenko char buf[64]; 29450e8b207eSAndrei Emeltchenko 29460e8b207eSAndrei Emeltchenko len = l2cap_parse_conf_rsp(chan, rsp->data, len, 29470e8b207eSAndrei Emeltchenko buf, &result); 29480e8b207eSAndrei Emeltchenko if (len < 0) { 29490e8b207eSAndrei Emeltchenko l2cap_send_disconn_req(conn, chan, ECONNRESET); 29500e8b207eSAndrei Emeltchenko goto done; 29510e8b207eSAndrei Emeltchenko } 29520e8b207eSAndrei Emeltchenko 29530e8b207eSAndrei Emeltchenko /* check compatibility */ 29540e8b207eSAndrei Emeltchenko 29550e8b207eSAndrei Emeltchenko clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 29560e8b207eSAndrei Emeltchenko set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 29570e8b207eSAndrei Emeltchenko 29580e8b207eSAndrei Emeltchenko l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 29590e8b207eSAndrei Emeltchenko l2cap_build_conf_rsp(chan, buf, 29600e8b207eSAndrei Emeltchenko L2CAP_CONF_SUCCESS, 0x0000), buf); 29610e8b207eSAndrei Emeltchenko } 29620e8b207eSAndrei Emeltchenko goto done; 29630e8b207eSAndrei Emeltchenko 29640a708f8fSGustavo F. Padovan case L2CAP_CONF_UNACCEPT: 296573ffa904SGustavo F. Padovan if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { 29660a708f8fSGustavo F. Padovan char req[64]; 29670a708f8fSGustavo F. Padovan 29680a708f8fSGustavo F. Padovan if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { 2969e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 29700a708f8fSGustavo F. Padovan goto done; 29710a708f8fSGustavo F. Padovan } 29720a708f8fSGustavo F. Padovan 29730a708f8fSGustavo F. Padovan /* throw out any old stored conf requests */ 29740a708f8fSGustavo F. Padovan result = L2CAP_CONF_SUCCESS; 2975b4450035SGustavo F. Padovan len = l2cap_parse_conf_rsp(chan, rsp->data, len, 2976b4450035SGustavo F. Padovan req, &result); 29770a708f8fSGustavo F. Padovan if (len < 0) { 2978e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 29790a708f8fSGustavo F. Padovan goto done; 29800a708f8fSGustavo F. Padovan } 29810a708f8fSGustavo F. Padovan 29820a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 29830a708f8fSGustavo F. Padovan L2CAP_CONF_REQ, len, req); 298473ffa904SGustavo F. Padovan chan->num_conf_req++; 29850a708f8fSGustavo F. Padovan if (result != L2CAP_CONF_SUCCESS) 29860a708f8fSGustavo F. Padovan goto done; 29870a708f8fSGustavo F. Padovan break; 29880a708f8fSGustavo F. Padovan } 29890a708f8fSGustavo F. Padovan 29900a708f8fSGustavo F. Padovan default: 29910a708f8fSGustavo F. Padovan sk->sk_err = ECONNRESET; 2992b83ddfe2SAndrzej Kaczmarek __set_chan_timer(chan, 2993b83ddfe2SAndrzej Kaczmarek msecs_to_jiffies(L2CAP_DISC_REJ_TIMEOUT)); 2994e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 29950a708f8fSGustavo F. Padovan goto done; 29960a708f8fSGustavo F. Padovan } 29970a708f8fSGustavo F. Padovan 29980a708f8fSGustavo F. Padovan if (flags & 0x01) 29990a708f8fSGustavo F. Padovan goto done; 30000a708f8fSGustavo F. Padovan 3001c1360a1cSGustavo F. Padovan set_bit(CONF_INPUT_DONE, &chan->conf_state); 30020a708f8fSGustavo F. Padovan 3003c1360a1cSGustavo F. Padovan if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { 300447d1ec61SGustavo F. Padovan set_default_fcs(chan); 30050a708f8fSGustavo F. Padovan 300689bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 300742e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 300842e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 300958d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 30100c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 3011525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 30120a708f8fSGustavo F. Padovan 3013cf4cd009SAndrei Emeltchenko l2cap_chan_ready(chan); 30140a708f8fSGustavo F. Padovan } 30150a708f8fSGustavo F. Padovan 30160a708f8fSGustavo F. Padovan done: 3017aa2ac881SGustavo F. Padovan release_sock(sk); 30180a708f8fSGustavo F. Padovan return 0; 30190a708f8fSGustavo F. Padovan } 30200a708f8fSGustavo F. Padovan 30210a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 30220a708f8fSGustavo F. Padovan { 30230a708f8fSGustavo F. Padovan struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; 30240a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp rsp; 30250a708f8fSGustavo F. Padovan u16 dcid, scid; 302648454079SGustavo F. Padovan struct l2cap_chan *chan; 30270a708f8fSGustavo F. Padovan struct sock *sk; 30280a708f8fSGustavo F. Padovan 30290a708f8fSGustavo F. Padovan scid = __le16_to_cpu(req->scid); 30300a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 30310a708f8fSGustavo F. Padovan 30320a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); 30330a708f8fSGustavo F. Padovan 30343df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 30353df91ea2SAndrei Emeltchenko 30363df91ea2SAndrei Emeltchenko chan = __l2cap_get_chan_by_scid(conn, dcid); 30373df91ea2SAndrei Emeltchenko if (!chan) { 30383df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 30390a708f8fSGustavo F. Padovan return 0; 30403df91ea2SAndrei Emeltchenko } 30410a708f8fSGustavo F. Padovan 304248454079SGustavo F. Padovan sk = chan->sk; 30433df91ea2SAndrei Emeltchenko lock_sock(sk); 304448454079SGustavo F. Padovan 3045fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 3046fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 30470a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); 30480a708f8fSGustavo F. Padovan 30490a708f8fSGustavo F. Padovan sk->sk_shutdown = SHUTDOWN_MASK; 30500a708f8fSGustavo F. Padovan 305148454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNRESET); 3052aa2ac881SGustavo F. Padovan release_sock(sk); 30530a708f8fSGustavo F. Padovan 3054ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 30553df91ea2SAndrei Emeltchenko 30563df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 30573df91ea2SAndrei Emeltchenko 30580a708f8fSGustavo F. Padovan return 0; 30590a708f8fSGustavo F. Padovan } 30600a708f8fSGustavo F. Padovan 30610a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 30620a708f8fSGustavo F. Padovan { 30630a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; 30640a708f8fSGustavo F. Padovan u16 dcid, scid; 306548454079SGustavo F. Padovan struct l2cap_chan *chan; 30660a708f8fSGustavo F. Padovan struct sock *sk; 30670a708f8fSGustavo F. Padovan 30680a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 30690a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 30700a708f8fSGustavo F. Padovan 30710a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); 30720a708f8fSGustavo F. Padovan 30733df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 30743df91ea2SAndrei Emeltchenko 30753df91ea2SAndrei Emeltchenko chan = __l2cap_get_chan_by_scid(conn, scid); 30763df91ea2SAndrei Emeltchenko if (!chan) { 30773df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 30780a708f8fSGustavo F. Padovan return 0; 30793df91ea2SAndrei Emeltchenko } 30800a708f8fSGustavo F. Padovan 308148454079SGustavo F. Padovan sk = chan->sk; 30823df91ea2SAndrei Emeltchenko lock_sock(sk); 308348454079SGustavo F. Padovan 308448454079SGustavo F. Padovan l2cap_chan_del(chan, 0); 3085aa2ac881SGustavo F. Padovan release_sock(sk); 30860a708f8fSGustavo F. Padovan 3087ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 30883df91ea2SAndrei Emeltchenko 30893df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 30903df91ea2SAndrei Emeltchenko 30910a708f8fSGustavo F. Padovan return 0; 30920a708f8fSGustavo F. Padovan } 30930a708f8fSGustavo F. Padovan 30940a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 30950a708f8fSGustavo F. Padovan { 30960a708f8fSGustavo F. Padovan struct l2cap_info_req *req = (struct l2cap_info_req *) data; 30970a708f8fSGustavo F. Padovan u16 type; 30980a708f8fSGustavo F. Padovan 30990a708f8fSGustavo F. Padovan type = __le16_to_cpu(req->type); 31000a708f8fSGustavo F. Padovan 31010a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x", type); 31020a708f8fSGustavo F. Padovan 31030a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 31040a708f8fSGustavo F. Padovan u8 buf[8]; 31050a708f8fSGustavo F. Padovan u32 feat_mask = l2cap_feat_mask; 31060a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 31070a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 31080a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 31090a708f8fSGustavo F. Padovan if (!disable_ertm) 31100a708f8fSGustavo F. Padovan feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING 31110a708f8fSGustavo F. Padovan | L2CAP_FEAT_FCS; 3112a5fd6f30SAndrei Emeltchenko if (enable_hs) 31136327eb98SAndrei Emeltchenko feat_mask |= L2CAP_FEAT_EXT_FLOW 31146327eb98SAndrei Emeltchenko | L2CAP_FEAT_EXT_WINDOW; 3115a5fd6f30SAndrei Emeltchenko 31160a708f8fSGustavo F. Padovan put_unaligned_le32(feat_mask, rsp->data); 31170a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 31180a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 31190a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 31200a708f8fSGustavo F. Padovan u8 buf[12]; 31210a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 312250a147cdSMat Martineau 312350a147cdSMat Martineau if (enable_hs) 312450a147cdSMat Martineau l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; 312550a147cdSMat Martineau else 312650a147cdSMat Martineau l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; 312750a147cdSMat Martineau 31280a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 31290a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 3130c6337ea6SAndrei Emeltchenko memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan)); 31310a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 31320a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 31330a708f8fSGustavo F. Padovan } else { 31340a708f8fSGustavo F. Padovan struct l2cap_info_rsp rsp; 31350a708f8fSGustavo F. Padovan rsp.type = cpu_to_le16(type); 31360a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); 31370a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 31380a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(rsp), &rsp); 31390a708f8fSGustavo F. Padovan } 31400a708f8fSGustavo F. Padovan 31410a708f8fSGustavo F. Padovan return 0; 31420a708f8fSGustavo F. Padovan } 31430a708f8fSGustavo F. Padovan 31440a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 31450a708f8fSGustavo F. Padovan { 31460a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; 31470a708f8fSGustavo F. Padovan u16 type, result; 31480a708f8fSGustavo F. Padovan 31490a708f8fSGustavo F. Padovan type = __le16_to_cpu(rsp->type); 31500a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 31510a708f8fSGustavo F. Padovan 31520a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); 31530a708f8fSGustavo F. Padovan 3154e90165beSAndrei Emeltchenko /* L2CAP Info req/rsp are unbound to channels, add extra checks */ 3155e90165beSAndrei Emeltchenko if (cmd->ident != conn->info_ident || 3156e90165beSAndrei Emeltchenko conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) 3157e90165beSAndrei Emeltchenko return 0; 3158e90165beSAndrei Emeltchenko 315917cd3f37SUlisses Furquim cancel_delayed_work(&conn->info_timer); 31600a708f8fSGustavo F. Padovan 31610a708f8fSGustavo F. Padovan if (result != L2CAP_IR_SUCCESS) { 31620a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 31630a708f8fSGustavo F. Padovan conn->info_ident = 0; 31640a708f8fSGustavo F. Padovan 31650a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 31660a708f8fSGustavo F. Padovan 31670a708f8fSGustavo F. Padovan return 0; 31680a708f8fSGustavo F. Padovan } 31690a708f8fSGustavo F. Padovan 31700a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 31710a708f8fSGustavo F. Padovan conn->feat_mask = get_unaligned_le32(rsp->data); 31720a708f8fSGustavo F. Padovan 31730a708f8fSGustavo F. Padovan if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { 31740a708f8fSGustavo F. Padovan struct l2cap_info_req req; 31750a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 31760a708f8fSGustavo F. Padovan 31770a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 31780a708f8fSGustavo F. Padovan 31790a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 31800a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 31810a708f8fSGustavo F. Padovan } else { 31820a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 31830a708f8fSGustavo F. Padovan conn->info_ident = 0; 31840a708f8fSGustavo F. Padovan 31850a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 31860a708f8fSGustavo F. Padovan } 31870a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 31880a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 31890a708f8fSGustavo F. Padovan conn->info_ident = 0; 31900a708f8fSGustavo F. Padovan 31910a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 31920a708f8fSGustavo F. Padovan } 31930a708f8fSGustavo F. Padovan 31940a708f8fSGustavo F. Padovan return 0; 31950a708f8fSGustavo F. Padovan } 31960a708f8fSGustavo F. Padovan 3197f94ff6ffSMat Martineau static inline int l2cap_create_channel_req(struct l2cap_conn *conn, 3198f94ff6ffSMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, 3199f94ff6ffSMat Martineau void *data) 3200f94ff6ffSMat Martineau { 3201f94ff6ffSMat Martineau struct l2cap_create_chan_req *req = data; 3202f94ff6ffSMat Martineau struct l2cap_create_chan_rsp rsp; 3203f94ff6ffSMat Martineau u16 psm, scid; 3204f94ff6ffSMat Martineau 3205f94ff6ffSMat Martineau if (cmd_len != sizeof(*req)) 3206f94ff6ffSMat Martineau return -EPROTO; 3207f94ff6ffSMat Martineau 3208f94ff6ffSMat Martineau if (!enable_hs) 3209f94ff6ffSMat Martineau return -EINVAL; 3210f94ff6ffSMat Martineau 3211f94ff6ffSMat Martineau psm = le16_to_cpu(req->psm); 3212f94ff6ffSMat Martineau scid = le16_to_cpu(req->scid); 3213f94ff6ffSMat Martineau 3214f94ff6ffSMat Martineau BT_DBG("psm %d, scid %d, amp_id %d", psm, scid, req->amp_id); 3215f94ff6ffSMat Martineau 3216f94ff6ffSMat Martineau /* Placeholder: Always reject */ 3217f94ff6ffSMat Martineau rsp.dcid = 0; 3218f94ff6ffSMat Martineau rsp.scid = cpu_to_le16(scid); 3219f94ff6ffSMat Martineau rsp.result = L2CAP_CR_NO_MEM; 3220f94ff6ffSMat Martineau rsp.status = L2CAP_CS_NO_INFO; 3221f94ff6ffSMat Martineau 3222f94ff6ffSMat Martineau l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, 3223f94ff6ffSMat Martineau sizeof(rsp), &rsp); 3224f94ff6ffSMat Martineau 3225f94ff6ffSMat Martineau return 0; 3226f94ff6ffSMat Martineau } 3227f94ff6ffSMat Martineau 3228f94ff6ffSMat Martineau static inline int l2cap_create_channel_rsp(struct l2cap_conn *conn, 3229f94ff6ffSMat Martineau struct l2cap_cmd_hdr *cmd, void *data) 3230f94ff6ffSMat Martineau { 3231f94ff6ffSMat Martineau BT_DBG("conn %p", conn); 3232f94ff6ffSMat Martineau 3233f94ff6ffSMat Martineau return l2cap_connect_rsp(conn, cmd, data); 3234f94ff6ffSMat Martineau } 3235f94ff6ffSMat Martineau 32368d5a04a1SMat Martineau static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident, 32378d5a04a1SMat Martineau u16 icid, u16 result) 32388d5a04a1SMat Martineau { 32398d5a04a1SMat Martineau struct l2cap_move_chan_rsp rsp; 32408d5a04a1SMat Martineau 32418d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 32428d5a04a1SMat Martineau 32438d5a04a1SMat Martineau rsp.icid = cpu_to_le16(icid); 32448d5a04a1SMat Martineau rsp.result = cpu_to_le16(result); 32458d5a04a1SMat Martineau 32468d5a04a1SMat Martineau l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_RSP, sizeof(rsp), &rsp); 32478d5a04a1SMat Martineau } 32488d5a04a1SMat Martineau 32498d5a04a1SMat Martineau static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn, 32508d5a04a1SMat Martineau struct l2cap_chan *chan, u16 icid, u16 result) 32518d5a04a1SMat Martineau { 32528d5a04a1SMat Martineau struct l2cap_move_chan_cfm cfm; 32538d5a04a1SMat Martineau u8 ident; 32548d5a04a1SMat Martineau 32558d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 32568d5a04a1SMat Martineau 32578d5a04a1SMat Martineau ident = l2cap_get_ident(conn); 32588d5a04a1SMat Martineau if (chan) 32598d5a04a1SMat Martineau chan->ident = ident; 32608d5a04a1SMat Martineau 32618d5a04a1SMat Martineau cfm.icid = cpu_to_le16(icid); 32628d5a04a1SMat Martineau cfm.result = cpu_to_le16(result); 32638d5a04a1SMat Martineau 32648d5a04a1SMat Martineau l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM, sizeof(cfm), &cfm); 32658d5a04a1SMat Martineau } 32668d5a04a1SMat Martineau 32678d5a04a1SMat Martineau static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident, 32688d5a04a1SMat Martineau u16 icid) 32698d5a04a1SMat Martineau { 32708d5a04a1SMat Martineau struct l2cap_move_chan_cfm_rsp rsp; 32718d5a04a1SMat Martineau 32728d5a04a1SMat Martineau BT_DBG("icid %d", icid); 32738d5a04a1SMat Martineau 32748d5a04a1SMat Martineau rsp.icid = cpu_to_le16(icid); 32758d5a04a1SMat Martineau l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); 32768d5a04a1SMat Martineau } 32778d5a04a1SMat Martineau 32788d5a04a1SMat Martineau static inline int l2cap_move_channel_req(struct l2cap_conn *conn, 32798d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 32808d5a04a1SMat Martineau { 32818d5a04a1SMat Martineau struct l2cap_move_chan_req *req = data; 32828d5a04a1SMat Martineau u16 icid = 0; 32838d5a04a1SMat Martineau u16 result = L2CAP_MR_NOT_ALLOWED; 32848d5a04a1SMat Martineau 32858d5a04a1SMat Martineau if (cmd_len != sizeof(*req)) 32868d5a04a1SMat Martineau return -EPROTO; 32878d5a04a1SMat Martineau 32888d5a04a1SMat Martineau icid = le16_to_cpu(req->icid); 32898d5a04a1SMat Martineau 32908d5a04a1SMat Martineau BT_DBG("icid %d, dest_amp_id %d", icid, req->dest_amp_id); 32918d5a04a1SMat Martineau 32928d5a04a1SMat Martineau if (!enable_hs) 32938d5a04a1SMat Martineau return -EINVAL; 32948d5a04a1SMat Martineau 32958d5a04a1SMat Martineau /* Placeholder: Always refuse */ 32968d5a04a1SMat Martineau l2cap_send_move_chan_rsp(conn, cmd->ident, icid, result); 32978d5a04a1SMat Martineau 32988d5a04a1SMat Martineau return 0; 32998d5a04a1SMat Martineau } 33008d5a04a1SMat Martineau 33018d5a04a1SMat Martineau static inline int l2cap_move_channel_rsp(struct l2cap_conn *conn, 33028d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 33038d5a04a1SMat Martineau { 33048d5a04a1SMat Martineau struct l2cap_move_chan_rsp *rsp = data; 33058d5a04a1SMat Martineau u16 icid, result; 33068d5a04a1SMat Martineau 33078d5a04a1SMat Martineau if (cmd_len != sizeof(*rsp)) 33088d5a04a1SMat Martineau return -EPROTO; 33098d5a04a1SMat Martineau 33108d5a04a1SMat Martineau icid = le16_to_cpu(rsp->icid); 33118d5a04a1SMat Martineau result = le16_to_cpu(rsp->result); 33128d5a04a1SMat Martineau 33138d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 33148d5a04a1SMat Martineau 33158d5a04a1SMat Martineau /* Placeholder: Always unconfirmed */ 33168d5a04a1SMat Martineau l2cap_send_move_chan_cfm(conn, NULL, icid, L2CAP_MC_UNCONFIRMED); 33178d5a04a1SMat Martineau 33188d5a04a1SMat Martineau return 0; 33198d5a04a1SMat Martineau } 33208d5a04a1SMat Martineau 33218d5a04a1SMat Martineau static inline int l2cap_move_channel_confirm(struct l2cap_conn *conn, 33228d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 33238d5a04a1SMat Martineau { 33248d5a04a1SMat Martineau struct l2cap_move_chan_cfm *cfm = data; 33258d5a04a1SMat Martineau u16 icid, result; 33268d5a04a1SMat Martineau 33278d5a04a1SMat Martineau if (cmd_len != sizeof(*cfm)) 33288d5a04a1SMat Martineau return -EPROTO; 33298d5a04a1SMat Martineau 33308d5a04a1SMat Martineau icid = le16_to_cpu(cfm->icid); 33318d5a04a1SMat Martineau result = le16_to_cpu(cfm->result); 33328d5a04a1SMat Martineau 33338d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 33348d5a04a1SMat Martineau 33358d5a04a1SMat Martineau l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); 33368d5a04a1SMat Martineau 33378d5a04a1SMat Martineau return 0; 33388d5a04a1SMat Martineau } 33398d5a04a1SMat Martineau 33408d5a04a1SMat Martineau static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, 33418d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 33428d5a04a1SMat Martineau { 33438d5a04a1SMat Martineau struct l2cap_move_chan_cfm_rsp *rsp = data; 33448d5a04a1SMat Martineau u16 icid; 33458d5a04a1SMat Martineau 33468d5a04a1SMat Martineau if (cmd_len != sizeof(*rsp)) 33478d5a04a1SMat Martineau return -EPROTO; 33488d5a04a1SMat Martineau 33498d5a04a1SMat Martineau icid = le16_to_cpu(rsp->icid); 33508d5a04a1SMat Martineau 33518d5a04a1SMat Martineau BT_DBG("icid %d", icid); 33528d5a04a1SMat Martineau 33538d5a04a1SMat Martineau return 0; 33548d5a04a1SMat Martineau } 33558d5a04a1SMat Martineau 3356e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency, 3357de73115aSClaudio Takahasi u16 to_multiplier) 3358de73115aSClaudio Takahasi { 3359de73115aSClaudio Takahasi u16 max_latency; 3360de73115aSClaudio Takahasi 3361de73115aSClaudio Takahasi if (min > max || min < 6 || max > 3200) 3362de73115aSClaudio Takahasi return -EINVAL; 3363de73115aSClaudio Takahasi 3364de73115aSClaudio Takahasi if (to_multiplier < 10 || to_multiplier > 3200) 3365de73115aSClaudio Takahasi return -EINVAL; 3366de73115aSClaudio Takahasi 3367de73115aSClaudio Takahasi if (max >= to_multiplier * 8) 3368de73115aSClaudio Takahasi return -EINVAL; 3369de73115aSClaudio Takahasi 3370de73115aSClaudio Takahasi max_latency = (to_multiplier * 8 / max) - 1; 3371de73115aSClaudio Takahasi if (latency > 499 || latency > max_latency) 3372de73115aSClaudio Takahasi return -EINVAL; 3373de73115aSClaudio Takahasi 3374de73115aSClaudio Takahasi return 0; 3375de73115aSClaudio Takahasi } 3376de73115aSClaudio Takahasi 3377de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, 3378de73115aSClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 3379de73115aSClaudio Takahasi { 3380de73115aSClaudio Takahasi struct hci_conn *hcon = conn->hcon; 3381de73115aSClaudio Takahasi struct l2cap_conn_param_update_req *req; 3382de73115aSClaudio Takahasi struct l2cap_conn_param_update_rsp rsp; 3383de73115aSClaudio Takahasi u16 min, max, latency, to_multiplier, cmd_len; 33842ce603ebSClaudio Takahasi int err; 3385de73115aSClaudio Takahasi 3386de73115aSClaudio Takahasi if (!(hcon->link_mode & HCI_LM_MASTER)) 3387de73115aSClaudio Takahasi return -EINVAL; 3388de73115aSClaudio Takahasi 3389de73115aSClaudio Takahasi cmd_len = __le16_to_cpu(cmd->len); 3390de73115aSClaudio Takahasi if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) 3391de73115aSClaudio Takahasi return -EPROTO; 3392de73115aSClaudio Takahasi 3393de73115aSClaudio Takahasi req = (struct l2cap_conn_param_update_req *) data; 3394de73115aSClaudio Takahasi min = __le16_to_cpu(req->min); 3395de73115aSClaudio Takahasi max = __le16_to_cpu(req->max); 3396de73115aSClaudio Takahasi latency = __le16_to_cpu(req->latency); 3397de73115aSClaudio Takahasi to_multiplier = __le16_to_cpu(req->to_multiplier); 3398de73115aSClaudio Takahasi 3399de73115aSClaudio Takahasi BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", 3400de73115aSClaudio Takahasi min, max, latency, to_multiplier); 3401de73115aSClaudio Takahasi 3402de73115aSClaudio Takahasi memset(&rsp, 0, sizeof(rsp)); 34032ce603ebSClaudio Takahasi 34042ce603ebSClaudio Takahasi err = l2cap_check_conn_param(min, max, latency, to_multiplier); 34052ce603ebSClaudio Takahasi if (err) 3406de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); 3407de73115aSClaudio Takahasi else 3408de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); 3409de73115aSClaudio Takahasi 3410de73115aSClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, 3411de73115aSClaudio Takahasi sizeof(rsp), &rsp); 3412de73115aSClaudio Takahasi 34132ce603ebSClaudio Takahasi if (!err) 34142ce603ebSClaudio Takahasi hci_le_conn_update(hcon, min, max, latency, to_multiplier); 34152ce603ebSClaudio Takahasi 3416de73115aSClaudio Takahasi return 0; 3417de73115aSClaudio Takahasi } 3418de73115aSClaudio Takahasi 34193300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, 34203300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 34213300d9a9SClaudio Takahasi { 34223300d9a9SClaudio Takahasi int err = 0; 34233300d9a9SClaudio Takahasi 34243300d9a9SClaudio Takahasi switch (cmd->code) { 34253300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 34263300d9a9SClaudio Takahasi l2cap_command_rej(conn, cmd, data); 34273300d9a9SClaudio Takahasi break; 34283300d9a9SClaudio Takahasi 34293300d9a9SClaudio Takahasi case L2CAP_CONN_REQ: 34303300d9a9SClaudio Takahasi err = l2cap_connect_req(conn, cmd, data); 34313300d9a9SClaudio Takahasi break; 34323300d9a9SClaudio Takahasi 34333300d9a9SClaudio Takahasi case L2CAP_CONN_RSP: 34343300d9a9SClaudio Takahasi err = l2cap_connect_rsp(conn, cmd, data); 34353300d9a9SClaudio Takahasi break; 34363300d9a9SClaudio Takahasi 34373300d9a9SClaudio Takahasi case L2CAP_CONF_REQ: 34383300d9a9SClaudio Takahasi err = l2cap_config_req(conn, cmd, cmd_len, data); 34393300d9a9SClaudio Takahasi break; 34403300d9a9SClaudio Takahasi 34413300d9a9SClaudio Takahasi case L2CAP_CONF_RSP: 34423300d9a9SClaudio Takahasi err = l2cap_config_rsp(conn, cmd, data); 34433300d9a9SClaudio Takahasi break; 34443300d9a9SClaudio Takahasi 34453300d9a9SClaudio Takahasi case L2CAP_DISCONN_REQ: 34463300d9a9SClaudio Takahasi err = l2cap_disconnect_req(conn, cmd, data); 34473300d9a9SClaudio Takahasi break; 34483300d9a9SClaudio Takahasi 34493300d9a9SClaudio Takahasi case L2CAP_DISCONN_RSP: 34503300d9a9SClaudio Takahasi err = l2cap_disconnect_rsp(conn, cmd, data); 34513300d9a9SClaudio Takahasi break; 34523300d9a9SClaudio Takahasi 34533300d9a9SClaudio Takahasi case L2CAP_ECHO_REQ: 34543300d9a9SClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); 34553300d9a9SClaudio Takahasi break; 34563300d9a9SClaudio Takahasi 34573300d9a9SClaudio Takahasi case L2CAP_ECHO_RSP: 34583300d9a9SClaudio Takahasi break; 34593300d9a9SClaudio Takahasi 34603300d9a9SClaudio Takahasi case L2CAP_INFO_REQ: 34613300d9a9SClaudio Takahasi err = l2cap_information_req(conn, cmd, data); 34623300d9a9SClaudio Takahasi break; 34633300d9a9SClaudio Takahasi 34643300d9a9SClaudio Takahasi case L2CAP_INFO_RSP: 34653300d9a9SClaudio Takahasi err = l2cap_information_rsp(conn, cmd, data); 34663300d9a9SClaudio Takahasi break; 34673300d9a9SClaudio Takahasi 3468f94ff6ffSMat Martineau case L2CAP_CREATE_CHAN_REQ: 3469f94ff6ffSMat Martineau err = l2cap_create_channel_req(conn, cmd, cmd_len, data); 3470f94ff6ffSMat Martineau break; 3471f94ff6ffSMat Martineau 3472f94ff6ffSMat Martineau case L2CAP_CREATE_CHAN_RSP: 3473f94ff6ffSMat Martineau err = l2cap_create_channel_rsp(conn, cmd, data); 3474f94ff6ffSMat Martineau break; 3475f94ff6ffSMat Martineau 34768d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_REQ: 34778d5a04a1SMat Martineau err = l2cap_move_channel_req(conn, cmd, cmd_len, data); 34788d5a04a1SMat Martineau break; 34798d5a04a1SMat Martineau 34808d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_RSP: 34818d5a04a1SMat Martineau err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data); 34828d5a04a1SMat Martineau break; 34838d5a04a1SMat Martineau 34848d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_CFM: 34858d5a04a1SMat Martineau err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); 34868d5a04a1SMat Martineau break; 34878d5a04a1SMat Martineau 34888d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_CFM_RSP: 34898d5a04a1SMat Martineau err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); 34908d5a04a1SMat Martineau break; 34918d5a04a1SMat Martineau 34923300d9a9SClaudio Takahasi default: 34933300d9a9SClaudio Takahasi BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); 34943300d9a9SClaudio Takahasi err = -EINVAL; 34953300d9a9SClaudio Takahasi break; 34963300d9a9SClaudio Takahasi } 34973300d9a9SClaudio Takahasi 34983300d9a9SClaudio Takahasi return err; 34993300d9a9SClaudio Takahasi } 35003300d9a9SClaudio Takahasi 35013300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, 35023300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 35033300d9a9SClaudio Takahasi { 35043300d9a9SClaudio Takahasi switch (cmd->code) { 35053300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 35063300d9a9SClaudio Takahasi return 0; 35073300d9a9SClaudio Takahasi 35083300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_REQ: 3509de73115aSClaudio Takahasi return l2cap_conn_param_update_req(conn, cmd, data); 35103300d9a9SClaudio Takahasi 35113300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_RSP: 35123300d9a9SClaudio Takahasi return 0; 35133300d9a9SClaudio Takahasi 35143300d9a9SClaudio Takahasi default: 35153300d9a9SClaudio Takahasi BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); 35163300d9a9SClaudio Takahasi return -EINVAL; 35173300d9a9SClaudio Takahasi } 35183300d9a9SClaudio Takahasi } 35193300d9a9SClaudio Takahasi 35203300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn, 35213300d9a9SClaudio Takahasi struct sk_buff *skb) 35220a708f8fSGustavo F. Padovan { 35230a708f8fSGustavo F. Padovan u8 *data = skb->data; 35240a708f8fSGustavo F. Padovan int len = skb->len; 35250a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr cmd; 35263300d9a9SClaudio Takahasi int err; 35270a708f8fSGustavo F. Padovan 35280a708f8fSGustavo F. Padovan l2cap_raw_recv(conn, skb); 35290a708f8fSGustavo F. Padovan 35300a708f8fSGustavo F. Padovan while (len >= L2CAP_CMD_HDR_SIZE) { 35310a708f8fSGustavo F. Padovan u16 cmd_len; 35320a708f8fSGustavo F. Padovan memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); 35330a708f8fSGustavo F. Padovan data += L2CAP_CMD_HDR_SIZE; 35340a708f8fSGustavo F. Padovan len -= L2CAP_CMD_HDR_SIZE; 35350a708f8fSGustavo F. Padovan 35360a708f8fSGustavo F. Padovan cmd_len = le16_to_cpu(cmd.len); 35370a708f8fSGustavo F. Padovan 35380a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident); 35390a708f8fSGustavo F. Padovan 35400a708f8fSGustavo F. Padovan if (cmd_len > len || !cmd.ident) { 35410a708f8fSGustavo F. Padovan BT_DBG("corrupted command"); 35420a708f8fSGustavo F. Padovan break; 35430a708f8fSGustavo F. Padovan } 35440a708f8fSGustavo F. Padovan 35453300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 35463300d9a9SClaudio Takahasi err = l2cap_le_sig_cmd(conn, &cmd, data); 35473300d9a9SClaudio Takahasi else 35483300d9a9SClaudio Takahasi err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); 35490a708f8fSGustavo F. Padovan 35500a708f8fSGustavo F. Padovan if (err) { 3551e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_unk rej; 35522c6d1a2eSGustavo F. Padovan 35532c6d1a2eSGustavo F. Padovan BT_ERR("Wrong link type (%d)", err); 35540a708f8fSGustavo F. Padovan 35550a708f8fSGustavo F. Padovan /* FIXME: Map err to a valid reason */ 3556e2fd318eSIlia Kolomisnky rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); 35570a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); 35580a708f8fSGustavo F. Padovan } 35590a708f8fSGustavo F. Padovan 35600a708f8fSGustavo F. Padovan data += cmd_len; 35610a708f8fSGustavo F. Padovan len -= cmd_len; 35620a708f8fSGustavo F. Padovan } 35630a708f8fSGustavo F. Padovan 35640a708f8fSGustavo F. Padovan kfree_skb(skb); 35650a708f8fSGustavo F. Padovan } 35660a708f8fSGustavo F. Padovan 356747d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb) 35680a708f8fSGustavo F. Padovan { 35690a708f8fSGustavo F. Padovan u16 our_fcs, rcv_fcs; 3570e4ca6d98SAndrei Emeltchenko int hdr_size; 3571e4ca6d98SAndrei Emeltchenko 3572e4ca6d98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 3573e4ca6d98SAndrei Emeltchenko hdr_size = L2CAP_EXT_HDR_SIZE; 3574e4ca6d98SAndrei Emeltchenko else 3575e4ca6d98SAndrei Emeltchenko hdr_size = L2CAP_ENH_HDR_SIZE; 35760a708f8fSGustavo F. Padovan 357747d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 357803a51213SAndrei Emeltchenko skb_trim(skb, skb->len - L2CAP_FCS_SIZE); 35790a708f8fSGustavo F. Padovan rcv_fcs = get_unaligned_le16(skb->data + skb->len); 35800a708f8fSGustavo F. Padovan our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); 35810a708f8fSGustavo F. Padovan 35820a708f8fSGustavo F. Padovan if (our_fcs != rcv_fcs) 35830a708f8fSGustavo F. Padovan return -EBADMSG; 35840a708f8fSGustavo F. Padovan } 35850a708f8fSGustavo F. Padovan return 0; 35860a708f8fSGustavo F. Padovan } 35870a708f8fSGustavo F. Padovan 3588525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan) 35890a708f8fSGustavo F. Padovan { 359088843ab0SAndrei Emeltchenko u32 control = 0; 35910a708f8fSGustavo F. Padovan 35926a026610SGustavo F. Padovan chan->frames_sent = 0; 35930a708f8fSGustavo F. Padovan 35940b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 35950a708f8fSGustavo F. Padovan 3596e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 3597ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 3598525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 3599e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 36000a708f8fSGustavo F. Padovan } 36010a708f8fSGustavo F. Padovan 3602e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) 3603525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 36040a708f8fSGustavo F. Padovan 3605525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 36060a708f8fSGustavo F. Padovan 3607e2ab4353SGustavo F. Padovan if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && 36086a026610SGustavo F. Padovan chan->frames_sent == 0) { 3609ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 3610525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 36110a708f8fSGustavo F. Padovan } 36120a708f8fSGustavo F. Padovan } 36130a708f8fSGustavo F. Padovan 3614fb45de7dSAndrei Emeltchenko static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u16 tx_seq, u8 sar) 36150a708f8fSGustavo F. Padovan { 36160a708f8fSGustavo F. Padovan struct sk_buff *next_skb; 36170a708f8fSGustavo F. Padovan int tx_seq_offset, next_tx_seq_offset; 36180a708f8fSGustavo F. Padovan 36190a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 36200a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 36210a708f8fSGustavo F. Padovan 3622f1c6775bSGustavo F. Padovan next_skb = skb_peek(&chan->srej_q); 36230a708f8fSGustavo F. Padovan 3624836be934SAndrei Emeltchenko tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq); 36250a708f8fSGustavo F. Padovan 3626039d9572SSzymon Janc while (next_skb) { 36270a708f8fSGustavo F. Padovan if (bt_cb(next_skb)->tx_seq == tx_seq) 36280a708f8fSGustavo F. Padovan return -EINVAL; 36290a708f8fSGustavo F. Padovan 3630836be934SAndrei Emeltchenko next_tx_seq_offset = __seq_offset(chan, 3631836be934SAndrei Emeltchenko bt_cb(next_skb)->tx_seq, chan->buffer_seq); 36320a708f8fSGustavo F. Padovan 36330a708f8fSGustavo F. Padovan if (next_tx_seq_offset > tx_seq_offset) { 3634f1c6775bSGustavo F. Padovan __skb_queue_before(&chan->srej_q, next_skb, skb); 36350a708f8fSGustavo F. Padovan return 0; 36360a708f8fSGustavo F. Padovan } 36370a708f8fSGustavo F. Padovan 3638f1c6775bSGustavo F. Padovan if (skb_queue_is_last(&chan->srej_q, next_skb)) 3639039d9572SSzymon Janc next_skb = NULL; 3640039d9572SSzymon Janc else 3641039d9572SSzymon Janc next_skb = skb_queue_next(&chan->srej_q, next_skb); 3642039d9572SSzymon Janc } 36430a708f8fSGustavo F. Padovan 3644f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 36450a708f8fSGustavo F. Padovan 36460a708f8fSGustavo F. Padovan return 0; 36470a708f8fSGustavo F. Padovan } 36480a708f8fSGustavo F. Padovan 364984084a31SMat Martineau static void append_skb_frag(struct sk_buff *skb, 365084084a31SMat Martineau struct sk_buff *new_frag, struct sk_buff **last_frag) 36510a708f8fSGustavo F. Padovan { 365284084a31SMat Martineau /* skb->len reflects data in skb as well as all fragments 365384084a31SMat Martineau * skb->data_len reflects only data in fragments 365484084a31SMat Martineau */ 365584084a31SMat Martineau if (!skb_has_frag_list(skb)) 365684084a31SMat Martineau skb_shinfo(skb)->frag_list = new_frag; 365784084a31SMat Martineau 365884084a31SMat Martineau new_frag->next = NULL; 365984084a31SMat Martineau 366084084a31SMat Martineau (*last_frag)->next = new_frag; 366184084a31SMat Martineau *last_frag = new_frag; 366284084a31SMat Martineau 366384084a31SMat Martineau skb->len += new_frag->len; 366484084a31SMat Martineau skb->data_len += new_frag->len; 366584084a31SMat Martineau skb->truesize += new_frag->truesize; 366684084a31SMat Martineau } 366784084a31SMat Martineau 366888843ab0SAndrei Emeltchenko static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u32 control) 366984084a31SMat Martineau { 367084084a31SMat Martineau int err = -EINVAL; 36710a708f8fSGustavo F. Padovan 36727e0ef6eeSAndrei Emeltchenko switch (__get_ctrl_sar(chan, control)) { 36737e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_UNSEGMENTED: 367484084a31SMat Martineau if (chan->sdu) 367584084a31SMat Martineau break; 36760a708f8fSGustavo F. Padovan 367784084a31SMat Martineau err = chan->ops->recv(chan->data, skb); 367884084a31SMat Martineau break; 36790a708f8fSGustavo F. Padovan 36807e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_START: 368184084a31SMat Martineau if (chan->sdu) 368284084a31SMat Martineau break; 36830a708f8fSGustavo F. Padovan 36846f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 368503a51213SAndrei Emeltchenko skb_pull(skb, L2CAP_SDULEN_SIZE); 36860a708f8fSGustavo F. Padovan 368784084a31SMat Martineau if (chan->sdu_len > chan->imtu) { 368884084a31SMat Martineau err = -EMSGSIZE; 368984084a31SMat Martineau break; 369084084a31SMat Martineau } 36910a708f8fSGustavo F. Padovan 369284084a31SMat Martineau if (skb->len >= chan->sdu_len) 369384084a31SMat Martineau break; 369484084a31SMat Martineau 369584084a31SMat Martineau chan->sdu = skb; 369684084a31SMat Martineau chan->sdu_last_frag = skb; 369784084a31SMat Martineau 369884084a31SMat Martineau skb = NULL; 369984084a31SMat Martineau err = 0; 37000a708f8fSGustavo F. Padovan break; 37010a708f8fSGustavo F. Padovan 37027e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_CONTINUE: 37036f61fd47SGustavo F. Padovan if (!chan->sdu) 370484084a31SMat Martineau break; 37050a708f8fSGustavo F. Padovan 370684084a31SMat Martineau append_skb_frag(chan->sdu, skb, 370784084a31SMat Martineau &chan->sdu_last_frag); 370884084a31SMat Martineau skb = NULL; 37090a708f8fSGustavo F. Padovan 371084084a31SMat Martineau if (chan->sdu->len >= chan->sdu_len) 371184084a31SMat Martineau break; 37120a708f8fSGustavo F. Padovan 371384084a31SMat Martineau err = 0; 37140a708f8fSGustavo F. Padovan break; 37150a708f8fSGustavo F. Padovan 37167e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_END: 37176f61fd47SGustavo F. Padovan if (!chan->sdu) 371884084a31SMat Martineau break; 37190a708f8fSGustavo F. Padovan 372084084a31SMat Martineau append_skb_frag(chan->sdu, skb, 372184084a31SMat Martineau &chan->sdu_last_frag); 372284084a31SMat Martineau skb = NULL; 37230a708f8fSGustavo F. Padovan 372484084a31SMat Martineau if (chan->sdu->len != chan->sdu_len) 372584084a31SMat Martineau break; 37260a708f8fSGustavo F. Padovan 372784084a31SMat Martineau err = chan->ops->recv(chan->data, chan->sdu); 37280a708f8fSGustavo F. Padovan 372984084a31SMat Martineau if (!err) { 373084084a31SMat Martineau /* Reassembly complete */ 373184084a31SMat Martineau chan->sdu = NULL; 373284084a31SMat Martineau chan->sdu_last_frag = NULL; 373384084a31SMat Martineau chan->sdu_len = 0; 37340a708f8fSGustavo F. Padovan } 37350a708f8fSGustavo F. Padovan break; 37360a708f8fSGustavo F. Padovan } 37370a708f8fSGustavo F. Padovan 373884084a31SMat Martineau if (err) { 37390a708f8fSGustavo F. Padovan kfree_skb(skb); 37406f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 37416f61fd47SGustavo F. Padovan chan->sdu = NULL; 374284084a31SMat Martineau chan->sdu_last_frag = NULL; 374384084a31SMat Martineau chan->sdu_len = 0; 374484084a31SMat Martineau } 37450a708f8fSGustavo F. Padovan 374684084a31SMat Martineau return err; 37470a708f8fSGustavo F. Padovan } 37480a708f8fSGustavo F. Padovan 374926f880d2SMat Martineau static void l2cap_ertm_enter_local_busy(struct l2cap_chan *chan) 37500a708f8fSGustavo F. Padovan { 375126f880d2SMat Martineau BT_DBG("chan %p, Enter local busy", chan); 375226f880d2SMat Martineau 375326f880d2SMat Martineau set_bit(CONN_LOCAL_BUSY, &chan->conn_state); 375426f880d2SMat Martineau 375577f918bcSSzymon Janc __set_ack_timer(chan); 37560a708f8fSGustavo F. Padovan } 37570a708f8fSGustavo F. Padovan 375826f880d2SMat Martineau static void l2cap_ertm_exit_local_busy(struct l2cap_chan *chan) 375926f880d2SMat Martineau { 376088843ab0SAndrei Emeltchenko u32 control; 37610a708f8fSGustavo F. Padovan 3762e2ab4353SGustavo F. Padovan if (!test_bit(CONN_RNR_SENT, &chan->conn_state)) 37630a708f8fSGustavo F. Padovan goto done; 37640a708f8fSGustavo F. Padovan 37650b209faeSAndrei Emeltchenko control = __set_reqseq(chan, chan->buffer_seq); 3766e3781735SAndrei Emeltchenko control |= __set_ctrl_poll(chan); 3767ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 3768525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 37696a026610SGustavo F. Padovan chan->retry_count = 1; 37700a708f8fSGustavo F. Padovan 37711a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 37721a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 37730a708f8fSGustavo F. Padovan 3774e2ab4353SGustavo F. Padovan set_bit(CONN_WAIT_F, &chan->conn_state); 37750a708f8fSGustavo F. Padovan 37760a708f8fSGustavo F. Padovan done: 3777e2ab4353SGustavo F. Padovan clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); 3778e2ab4353SGustavo F. Padovan clear_bit(CONN_RNR_SENT, &chan->conn_state); 37790a708f8fSGustavo F. Padovan 378049208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit local busy", chan); 37810a708f8fSGustavo F. Padovan } 37820a708f8fSGustavo F. Padovan 3783e328140fSMat Martineau void l2cap_chan_busy(struct l2cap_chan *chan, int busy) 37840a708f8fSGustavo F. Padovan { 3785e328140fSMat Martineau if (chan->mode == L2CAP_MODE_ERTM) { 3786e328140fSMat Martineau if (busy) 378726f880d2SMat Martineau l2cap_ertm_enter_local_busy(chan); 3788e328140fSMat Martineau else 3789e328140fSMat Martineau l2cap_ertm_exit_local_busy(chan); 37900a708f8fSGustavo F. Padovan } 37910a708f8fSGustavo F. Padovan } 37920a708f8fSGustavo F. Padovan 3793fb45de7dSAndrei Emeltchenko static void l2cap_check_srej_gap(struct l2cap_chan *chan, u16 tx_seq) 37940a708f8fSGustavo F. Padovan { 37950a708f8fSGustavo F. Padovan struct sk_buff *skb; 379688843ab0SAndrei Emeltchenko u32 control; 37970a708f8fSGustavo F. Padovan 3798e328140fSMat Martineau while ((skb = skb_peek(&chan->srej_q)) && 3799e328140fSMat Martineau !test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 3800e328140fSMat Martineau int err; 3801e328140fSMat Martineau 38020a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq != tx_seq) 38030a708f8fSGustavo F. Padovan break; 38040a708f8fSGustavo F. Padovan 3805f1c6775bSGustavo F. Padovan skb = skb_dequeue(&chan->srej_q); 38067e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, bt_cb(skb)->sar); 380784084a31SMat Martineau err = l2cap_reassemble_sdu(chan, skb, control); 3808e328140fSMat Martineau 3809e328140fSMat Martineau if (err < 0) { 3810e328140fSMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 3811e328140fSMat Martineau break; 3812e328140fSMat Martineau } 3813e328140fSMat Martineau 3814836be934SAndrei Emeltchenko chan->buffer_seq_srej = __next_seq(chan, chan->buffer_seq_srej); 3815836be934SAndrei Emeltchenko tx_seq = __next_seq(chan, tx_seq); 38160a708f8fSGustavo F. Padovan } 38170a708f8fSGustavo F. Padovan } 38180a708f8fSGustavo F. Padovan 3819fb45de7dSAndrei Emeltchenko static void l2cap_resend_srejframe(struct l2cap_chan *chan, u16 tx_seq) 38200a708f8fSGustavo F. Padovan { 38210a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 382288843ab0SAndrei Emeltchenko u32 control; 38230a708f8fSGustavo F. Padovan 382439d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 38250a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 38260a708f8fSGustavo F. Padovan list_del(&l->list); 38270a708f8fSGustavo F. Padovan kfree(l); 38280a708f8fSGustavo F. Padovan return; 38290a708f8fSGustavo F. Padovan } 3830ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 38310b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, l->tx_seq); 3832525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 38330a708f8fSGustavo F. Padovan list_del(&l->list); 383439d5a3eeSGustavo F. Padovan list_add_tail(&l->list, &chan->srej_l); 38350a708f8fSGustavo F. Padovan } 38360a708f8fSGustavo F. Padovan } 38370a708f8fSGustavo F. Padovan 3838aef89f21SSzymon Janc static int l2cap_send_srejframe(struct l2cap_chan *chan, u16 tx_seq) 38390a708f8fSGustavo F. Padovan { 38400a708f8fSGustavo F. Padovan struct srej_list *new; 384188843ab0SAndrei Emeltchenko u32 control; 38420a708f8fSGustavo F. Padovan 384342e5c802SGustavo F. Padovan while (tx_seq != chan->expected_tx_seq) { 3844ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 38450b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->expected_tx_seq); 3846525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 38470a708f8fSGustavo F. Padovan 38480a708f8fSGustavo F. Padovan new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); 3849aef89f21SSzymon Janc if (!new) 3850aef89f21SSzymon Janc return -ENOMEM; 3851aef89f21SSzymon Janc 385242e5c802SGustavo F. Padovan new->tx_seq = chan->expected_tx_seq; 3853836be934SAndrei Emeltchenko 3854836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); 3855836be934SAndrei Emeltchenko 385639d5a3eeSGustavo F. Padovan list_add_tail(&new->list, &chan->srej_l); 38570a708f8fSGustavo F. Padovan } 3858836be934SAndrei Emeltchenko 3859836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); 3860aef89f21SSzymon Janc 3861aef89f21SSzymon Janc return 0; 38620a708f8fSGustavo F. Padovan } 38630a708f8fSGustavo F. Padovan 386488843ab0SAndrei Emeltchenko static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb) 38650a708f8fSGustavo F. Padovan { 3866fb45de7dSAndrei Emeltchenko u16 tx_seq = __get_txseq(chan, rx_control); 38670b209faeSAndrei Emeltchenko u16 req_seq = __get_reqseq(chan, rx_control); 38687e0ef6eeSAndrei Emeltchenko u8 sar = __get_ctrl_sar(chan, rx_control); 38690a708f8fSGustavo F. Padovan int tx_seq_offset, expected_tx_seq_offset; 387047d1ec61SGustavo F. Padovan int num_to_ack = (chan->tx_win/6) + 1; 38710a708f8fSGustavo F. Padovan int err = 0; 38720a708f8fSGustavo F. Padovan 387388843ab0SAndrei Emeltchenko BT_DBG("chan %p len %d tx_seq %d rx_control 0x%8.8x", chan, skb->len, 3874525cd185SGustavo F. Padovan tx_seq, rx_control); 38750a708f8fSGustavo F. Padovan 387603f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control) && 3877e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 38781a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 38796a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 38801a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 3881e2ab4353SGustavo F. Padovan clear_bit(CONN_WAIT_F, &chan->conn_state); 38820a708f8fSGustavo F. Padovan } 38830a708f8fSGustavo F. Padovan 388442e5c802SGustavo F. Padovan chan->expected_ack_seq = req_seq; 388542e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 38860a708f8fSGustavo F. Padovan 3887836be934SAndrei Emeltchenko tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq); 38880a708f8fSGustavo F. Padovan 38890a708f8fSGustavo F. Padovan /* invalid tx_seq */ 389047d1ec61SGustavo F. Padovan if (tx_seq_offset >= chan->tx_win) { 38918c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38920a708f8fSGustavo F. Padovan goto drop; 38930a708f8fSGustavo F. Padovan } 38940a708f8fSGustavo F. Padovan 389577f918bcSSzymon Janc if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 389677f918bcSSzymon Janc if (!test_bit(CONN_RNR_SENT, &chan->conn_state)) 389777f918bcSSzymon Janc l2cap_send_ack(chan); 38980a708f8fSGustavo F. Padovan goto drop; 389977f918bcSSzymon Janc } 39000a708f8fSGustavo F. Padovan 390102f1b641SMat Martineau if (tx_seq == chan->expected_tx_seq) 390202f1b641SMat Martineau goto expected; 390302f1b641SMat Martineau 3904e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 39050a708f8fSGustavo F. Padovan struct srej_list *first; 39060a708f8fSGustavo F. Padovan 390739d5a3eeSGustavo F. Padovan first = list_first_entry(&chan->srej_l, 39080a708f8fSGustavo F. Padovan struct srej_list, list); 39090a708f8fSGustavo F. Padovan if (tx_seq == first->tx_seq) { 391042e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 3911525cd185SGustavo F. Padovan l2cap_check_srej_gap(chan, tx_seq); 39120a708f8fSGustavo F. Padovan 39130a708f8fSGustavo F. Padovan list_del(&first->list); 39140a708f8fSGustavo F. Padovan kfree(first); 39150a708f8fSGustavo F. Padovan 391639d5a3eeSGustavo F. Padovan if (list_empty(&chan->srej_l)) { 391742e5c802SGustavo F. Padovan chan->buffer_seq = chan->buffer_seq_srej; 3918e2ab4353SGustavo F. Padovan clear_bit(CONN_SREJ_SENT, &chan->conn_state); 3919525cd185SGustavo F. Padovan l2cap_send_ack(chan); 392049208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit SREJ_SENT", chan); 39210a708f8fSGustavo F. Padovan } 39220a708f8fSGustavo F. Padovan } else { 39230a708f8fSGustavo F. Padovan struct srej_list *l; 39240a708f8fSGustavo F. Padovan 39250a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 392642e5c802SGustavo F. Padovan if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0) 39270a708f8fSGustavo F. Padovan goto drop; 39280a708f8fSGustavo F. Padovan 392939d5a3eeSGustavo F. Padovan list_for_each_entry(l, &chan->srej_l, list) { 39300a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 3931525cd185SGustavo F. Padovan l2cap_resend_srejframe(chan, tx_seq); 39320a708f8fSGustavo F. Padovan return 0; 39330a708f8fSGustavo F. Padovan } 39340a708f8fSGustavo F. Padovan } 3935aef89f21SSzymon Janc 3936aef89f21SSzymon Janc err = l2cap_send_srejframe(chan, tx_seq); 3937aef89f21SSzymon Janc if (err < 0) { 3938aef89f21SSzymon Janc l2cap_send_disconn_req(chan->conn, chan, -err); 3939aef89f21SSzymon Janc return err; 3940aef89f21SSzymon Janc } 39410a708f8fSGustavo F. Padovan } 39420a708f8fSGustavo F. Padovan } else { 3943836be934SAndrei Emeltchenko expected_tx_seq_offset = __seq_offset(chan, 3944836be934SAndrei Emeltchenko chan->expected_tx_seq, chan->buffer_seq); 39450a708f8fSGustavo F. Padovan 39460a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 39470a708f8fSGustavo F. Padovan if (tx_seq_offset < expected_tx_seq_offset) 39480a708f8fSGustavo F. Padovan goto drop; 39490a708f8fSGustavo F. Padovan 3950e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_SENT, &chan->conn_state); 39510a708f8fSGustavo F. Padovan 395249208c9cSGustavo F. Padovan BT_DBG("chan %p, Enter SREJ", chan); 39530a708f8fSGustavo F. Padovan 395439d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 395542e5c802SGustavo F. Padovan chan->buffer_seq_srej = chan->buffer_seq; 39560a708f8fSGustavo F. Padovan 3957f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->srej_q); 395842e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 39590a708f8fSGustavo F. Padovan 39600ef3ef0fSSzymon Janc /* Set P-bit only if there are some I-frames to ack. */ 39610ef3ef0fSSzymon Janc if (__clear_ack_timer(chan)) 3962e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_PBIT, &chan->conn_state); 39630a708f8fSGustavo F. Padovan 3964aef89f21SSzymon Janc err = l2cap_send_srejframe(chan, tx_seq); 3965aef89f21SSzymon Janc if (err < 0) { 3966aef89f21SSzymon Janc l2cap_send_disconn_req(chan->conn, chan, -err); 3967aef89f21SSzymon Janc return err; 3968aef89f21SSzymon Janc } 39690a708f8fSGustavo F. Padovan } 39700a708f8fSGustavo F. Padovan return 0; 39710a708f8fSGustavo F. Padovan 39720a708f8fSGustavo F. Padovan expected: 3973836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); 39740a708f8fSGustavo F. Padovan 3975e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 39760a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 39770a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 3978f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 39790a708f8fSGustavo F. Padovan return 0; 39800a708f8fSGustavo F. Padovan } 39810a708f8fSGustavo F. Padovan 398284084a31SMat Martineau err = l2cap_reassemble_sdu(chan, skb, rx_control); 3983836be934SAndrei Emeltchenko chan->buffer_seq = __next_seq(chan, chan->buffer_seq); 3984836be934SAndrei Emeltchenko 3985e328140fSMat Martineau if (err < 0) { 3986e328140fSMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 3987e328140fSMat Martineau return err; 3988e328140fSMat Martineau } 39890a708f8fSGustavo F. Padovan 399003f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control)) { 3991e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 3992525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 39930a708f8fSGustavo F. Padovan } 39940a708f8fSGustavo F. Padovan 39950a708f8fSGustavo F. Padovan 39966a026610SGustavo F. Padovan chan->num_acked = (chan->num_acked + 1) % num_to_ack; 39976a026610SGustavo F. Padovan if (chan->num_acked == num_to_ack - 1) 3998525cd185SGustavo F. Padovan l2cap_send_ack(chan); 39994d611e4dSGustavo F. Padovan else 40004d611e4dSGustavo F. Padovan __set_ack_timer(chan); 40010a708f8fSGustavo F. Padovan 40020a708f8fSGustavo F. Padovan return 0; 40030a708f8fSGustavo F. Padovan 40040a708f8fSGustavo F. Padovan drop: 40050a708f8fSGustavo F. Padovan kfree_skb(skb); 40060a708f8fSGustavo F. Padovan return 0; 40070a708f8fSGustavo F. Padovan } 40080a708f8fSGustavo F. Padovan 400988843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u32 rx_control) 40100a708f8fSGustavo F. Padovan { 401188843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, 40120b209faeSAndrei Emeltchenko __get_reqseq(chan, rx_control), rx_control); 40130a708f8fSGustavo F. Padovan 40140b209faeSAndrei Emeltchenko chan->expected_ack_seq = __get_reqseq(chan, rx_control); 401542e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 40160a708f8fSGustavo F. Padovan 4017e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) { 4018e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 4019e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 4020e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 40216a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 40221a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 40230a708f8fSGustavo F. Padovan 4024e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 4025525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 40260a708f8fSGustavo F. Padovan } else { 4027525cd185SGustavo F. Padovan l2cap_send_i_or_rr_or_rnr(chan); 40280a708f8fSGustavo F. Padovan } 40290a708f8fSGustavo F. Padovan 403003f6715dSAndrei Emeltchenko } else if (__is_ctrl_final(chan, rx_control)) { 4031e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 40320a708f8fSGustavo F. Padovan 4033e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 4034525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 40350a708f8fSGustavo F. Padovan 40360a708f8fSGustavo F. Padovan } else { 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); 4042e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) 4043525cd185SGustavo F. Padovan l2cap_send_ack(chan); 40440a708f8fSGustavo F. Padovan else 4045525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 40460a708f8fSGustavo F. Padovan } 40470a708f8fSGustavo F. Padovan } 40480a708f8fSGustavo F. Padovan 404988843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u32 rx_control) 40500a708f8fSGustavo F. Padovan { 40510b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 40520a708f8fSGustavo F. Padovan 405388843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); 40540a708f8fSGustavo F. Padovan 4055e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 40560a708f8fSGustavo F. Padovan 405742e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 405842e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 40590a708f8fSGustavo F. Padovan 406003f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control)) { 4061e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 4062525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 40630a708f8fSGustavo F. Padovan } else { 4064525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 40650a708f8fSGustavo F. Padovan 4066e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) 4067e2ab4353SGustavo F. Padovan set_bit(CONN_REJ_ACT, &chan->conn_state); 40680a708f8fSGustavo F. Padovan } 40690a708f8fSGustavo F. Padovan } 407088843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u32 rx_control) 40710a708f8fSGustavo F. Padovan { 40720b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 40730a708f8fSGustavo F. Padovan 407488843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); 40750a708f8fSGustavo F. Padovan 4076e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 40770a708f8fSGustavo F. Padovan 4078e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) { 407942e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 408042e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 40810a708f8fSGustavo F. Padovan 4082e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 4083525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 40840a708f8fSGustavo F. Padovan 4085525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 40860a708f8fSGustavo F. Padovan 4087e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) { 40886a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 4089e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_ACT, &chan->conn_state); 40900a708f8fSGustavo F. Padovan } 409103f6715dSAndrei Emeltchenko } else if (__is_ctrl_final(chan, rx_control)) { 4092e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_ACT, &chan->conn_state) && 40936a026610SGustavo F. Padovan chan->srej_save_reqseq == tx_seq) 4094e2ab4353SGustavo F. Padovan clear_bit(CONN_SREJ_ACT, &chan->conn_state); 40950a708f8fSGustavo F. Padovan else 4096525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 40970a708f8fSGustavo F. Padovan } else { 4098525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 4099e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) { 41006a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 4101e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_ACT, &chan->conn_state); 41020a708f8fSGustavo F. Padovan } 41030a708f8fSGustavo F. Padovan } 41040a708f8fSGustavo F. Padovan } 41050a708f8fSGustavo F. Padovan 410688843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u32 rx_control) 41070a708f8fSGustavo F. Padovan { 41080b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 41090a708f8fSGustavo F. Padovan 411088843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); 41110a708f8fSGustavo F. Padovan 4112e2ab4353SGustavo F. Padovan set_bit(CONN_REMOTE_BUSY, &chan->conn_state); 411342e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 411442e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 41150a708f8fSGustavo F. Padovan 4116e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) 4117e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 41180a708f8fSGustavo F. Padovan 4119e2ab4353SGustavo F. Padovan if (!test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 41201a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 4121e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) 4122525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); 41230a708f8fSGustavo F. Padovan return; 41240a708f8fSGustavo F. Padovan } 41250a708f8fSGustavo F. Padovan 4126e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) { 4127525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 4128ab784b73SAndrei Emeltchenko } else { 4129ab784b73SAndrei Emeltchenko rx_control = __set_ctrl_super(chan, L2CAP_SUPER_RR); 4130ab784b73SAndrei Emeltchenko l2cap_send_sframe(chan, rx_control); 4131ab784b73SAndrei Emeltchenko } 41320a708f8fSGustavo F. Padovan } 41330a708f8fSGustavo F. Padovan 413488843ab0SAndrei Emeltchenko static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb) 41350a708f8fSGustavo F. Padovan { 413688843ab0SAndrei Emeltchenko BT_DBG("chan %p rx_control 0x%8.8x len %d", chan, rx_control, skb->len); 41370a708f8fSGustavo F. Padovan 413803f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control) && 4139e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 41401a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 41416a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 41421a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 4143e2ab4353SGustavo F. Padovan clear_bit(CONN_WAIT_F, &chan->conn_state); 41440a708f8fSGustavo F. Padovan } 41450a708f8fSGustavo F. Padovan 4146ab784b73SAndrei Emeltchenko switch (__get_ctrl_super(chan, rx_control)) { 4147ab784b73SAndrei Emeltchenko case L2CAP_SUPER_RR: 4148525cd185SGustavo F. Padovan l2cap_data_channel_rrframe(chan, rx_control); 41490a708f8fSGustavo F. Padovan break; 41500a708f8fSGustavo F. Padovan 4151ab784b73SAndrei Emeltchenko case L2CAP_SUPER_REJ: 4152525cd185SGustavo F. Padovan l2cap_data_channel_rejframe(chan, rx_control); 41530a708f8fSGustavo F. Padovan break; 41540a708f8fSGustavo F. Padovan 4155ab784b73SAndrei Emeltchenko case L2CAP_SUPER_SREJ: 4156525cd185SGustavo F. Padovan l2cap_data_channel_srejframe(chan, rx_control); 41570a708f8fSGustavo F. Padovan break; 41580a708f8fSGustavo F. Padovan 4159ab784b73SAndrei Emeltchenko case L2CAP_SUPER_RNR: 4160525cd185SGustavo F. Padovan l2cap_data_channel_rnrframe(chan, rx_control); 41610a708f8fSGustavo F. Padovan break; 41620a708f8fSGustavo F. Padovan } 41630a708f8fSGustavo F. Padovan 41640a708f8fSGustavo F. Padovan kfree_skb(skb); 41650a708f8fSGustavo F. Padovan return 0; 41660a708f8fSGustavo F. Padovan } 41670a708f8fSGustavo F. Padovan 4168cad8f1d0SSzymon Janc static int l2cap_ertm_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) 41690a708f8fSGustavo F. Padovan { 417088843ab0SAndrei Emeltchenko u32 control; 41710b209faeSAndrei Emeltchenko u16 req_seq; 41720a708f8fSGustavo F. Padovan int len, next_tx_seq_offset, req_seq_offset; 41730a708f8fSGustavo F. Padovan 417488843ab0SAndrei Emeltchenko control = __get_control(chan, skb->data); 417588843ab0SAndrei Emeltchenko skb_pull(skb, __ctrl_size(chan)); 41760a708f8fSGustavo F. Padovan len = skb->len; 41770a708f8fSGustavo F. Padovan 41780a708f8fSGustavo F. Padovan /* 41790a708f8fSGustavo F. Padovan * We can just drop the corrupted I-frame here. 41800a708f8fSGustavo F. Padovan * Receiver will miss it and start proper recovery 41810a708f8fSGustavo F. Padovan * procedures and ask retransmission. 41820a708f8fSGustavo F. Padovan */ 418347d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 41840a708f8fSGustavo F. Padovan goto drop; 41850a708f8fSGustavo F. Padovan 4186793c2f1cSAndrei Emeltchenko if (__is_sar_start(chan, control) && !__is_sframe(chan, control)) 418703a51213SAndrei Emeltchenko len -= L2CAP_SDULEN_SIZE; 41880a708f8fSGustavo F. Padovan 418947d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 419003a51213SAndrei Emeltchenko len -= L2CAP_FCS_SIZE; 41910a708f8fSGustavo F. Padovan 419247d1ec61SGustavo F. Padovan if (len > chan->mps) { 41938c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 41940a708f8fSGustavo F. Padovan goto drop; 41950a708f8fSGustavo F. Padovan } 41960a708f8fSGustavo F. Padovan 41970b209faeSAndrei Emeltchenko req_seq = __get_reqseq(chan, control); 41980a708f8fSGustavo F. Padovan 4199836be934SAndrei Emeltchenko req_seq_offset = __seq_offset(chan, req_seq, chan->expected_ack_seq); 4200836be934SAndrei Emeltchenko 4201836be934SAndrei Emeltchenko next_tx_seq_offset = __seq_offset(chan, chan->next_tx_seq, 4202836be934SAndrei Emeltchenko chan->expected_ack_seq); 42030a708f8fSGustavo F. Padovan 42040a708f8fSGustavo F. Padovan /* check for invalid req-seq */ 42050a708f8fSGustavo F. Padovan if (req_seq_offset > next_tx_seq_offset) { 42068c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 42070a708f8fSGustavo F. Padovan goto drop; 42080a708f8fSGustavo F. Padovan } 42090a708f8fSGustavo F. Padovan 4210793c2f1cSAndrei Emeltchenko if (!__is_sframe(chan, control)) { 42110a708f8fSGustavo F. Padovan if (len < 0) { 42128c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 42130a708f8fSGustavo F. Padovan goto drop; 42140a708f8fSGustavo F. Padovan } 42150a708f8fSGustavo F. Padovan 4216525cd185SGustavo F. Padovan l2cap_data_channel_iframe(chan, control, skb); 42170a708f8fSGustavo F. Padovan } else { 42180a708f8fSGustavo F. Padovan if (len != 0) { 42190a708f8fSGustavo F. Padovan BT_ERR("%d", len); 42208c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 42210a708f8fSGustavo F. Padovan goto drop; 42220a708f8fSGustavo F. Padovan } 42230a708f8fSGustavo F. Padovan 4224525cd185SGustavo F. Padovan l2cap_data_channel_sframe(chan, control, skb); 42250a708f8fSGustavo F. Padovan } 42260a708f8fSGustavo F. Padovan 42270a708f8fSGustavo F. Padovan return 0; 42280a708f8fSGustavo F. Padovan 42290a708f8fSGustavo F. Padovan drop: 42300a708f8fSGustavo F. Padovan kfree_skb(skb); 42310a708f8fSGustavo F. Padovan return 0; 42320a708f8fSGustavo F. Padovan } 42330a708f8fSGustavo F. Padovan 42340a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) 42350a708f8fSGustavo F. Padovan { 423648454079SGustavo F. Padovan struct l2cap_chan *chan; 4237bf734843SDavid S. Miller struct sock *sk = NULL; 423888843ab0SAndrei Emeltchenko u32 control; 4239fb45de7dSAndrei Emeltchenko u16 tx_seq; 42400a708f8fSGustavo F. Padovan int len; 42410a708f8fSGustavo F. Padovan 4242baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 424348454079SGustavo F. Padovan if (!chan) { 42440a708f8fSGustavo F. Padovan BT_DBG("unknown cid 0x%4.4x", cid); 42450a708f8fSGustavo F. Padovan goto drop; 42460a708f8fSGustavo F. Padovan } 42470a708f8fSGustavo F. Padovan 424848454079SGustavo F. Padovan sk = chan->sk; 42493df91ea2SAndrei Emeltchenko lock_sock(sk); 42500a708f8fSGustavo F. Padovan 425149208c9cSGustavo F. Padovan BT_DBG("chan %p, len %d", chan, skb->len); 42520a708f8fSGustavo F. Padovan 425389bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 42540a708f8fSGustavo F. Padovan goto drop; 42550a708f8fSGustavo F. Padovan 42560c1bc5c6SGustavo F. Padovan switch (chan->mode) { 42570a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 42580a708f8fSGustavo F. Padovan /* If socket recv buffers overflows we drop data here 42590a708f8fSGustavo F. Padovan * which is *bad* because L2CAP has to be reliable. 42600a708f8fSGustavo F. Padovan * But we don't have any other choice. L2CAP doesn't 42610a708f8fSGustavo F. Padovan * provide flow control mechanism. */ 42620a708f8fSGustavo F. Padovan 42630c1bc5c6SGustavo F. Padovan if (chan->imtu < skb->len) 42640a708f8fSGustavo F. Padovan goto drop; 42650a708f8fSGustavo F. Padovan 426623070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 42670a708f8fSGustavo F. Padovan goto done; 42680a708f8fSGustavo F. Padovan break; 42690a708f8fSGustavo F. Padovan 42700a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 42715ef8cb9eSAndrei Emeltchenko l2cap_ertm_data_rcv(chan, skb); 42720a708f8fSGustavo F. Padovan 42730a708f8fSGustavo F. Padovan goto done; 42740a708f8fSGustavo F. Padovan 42750a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 427688843ab0SAndrei Emeltchenko control = __get_control(chan, skb->data); 427788843ab0SAndrei Emeltchenko skb_pull(skb, __ctrl_size(chan)); 42780a708f8fSGustavo F. Padovan len = skb->len; 42790a708f8fSGustavo F. Padovan 428047d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 42810a708f8fSGustavo F. Padovan goto drop; 42820a708f8fSGustavo F. Padovan 42837e0ef6eeSAndrei Emeltchenko if (__is_sar_start(chan, control)) 428403a51213SAndrei Emeltchenko len -= L2CAP_SDULEN_SIZE; 42850a708f8fSGustavo F. Padovan 428647d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 428703a51213SAndrei Emeltchenko len -= L2CAP_FCS_SIZE; 42880a708f8fSGustavo F. Padovan 4289793c2f1cSAndrei Emeltchenko if (len > chan->mps || len < 0 || __is_sframe(chan, control)) 42900a708f8fSGustavo F. Padovan goto drop; 42910a708f8fSGustavo F. Padovan 4292fb45de7dSAndrei Emeltchenko tx_seq = __get_txseq(chan, control); 42930a708f8fSGustavo F. Padovan 429484084a31SMat Martineau if (chan->expected_tx_seq != tx_seq) { 429584084a31SMat Martineau /* Frame(s) missing - must discard partial SDU */ 429684084a31SMat Martineau kfree_skb(chan->sdu); 429784084a31SMat Martineau chan->sdu = NULL; 429884084a31SMat Martineau chan->sdu_last_frag = NULL; 429984084a31SMat Martineau chan->sdu_len = 0; 430084084a31SMat Martineau 430184084a31SMat Martineau /* TODO: Notify userland of missing data */ 430284084a31SMat Martineau } 430384084a31SMat Martineau 4304836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, tx_seq); 43050a708f8fSGustavo F. Padovan 430684084a31SMat Martineau if (l2cap_reassemble_sdu(chan, skb, control) == -EMSGSIZE) 430784084a31SMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 43080a708f8fSGustavo F. Padovan 43090a708f8fSGustavo F. Padovan goto done; 43100a708f8fSGustavo F. Padovan 43110a708f8fSGustavo F. Padovan default: 43120c1bc5c6SGustavo F. Padovan BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode); 43130a708f8fSGustavo F. Padovan break; 43140a708f8fSGustavo F. Padovan } 43150a708f8fSGustavo F. Padovan 43160a708f8fSGustavo F. Padovan drop: 43170a708f8fSGustavo F. Padovan kfree_skb(skb); 43180a708f8fSGustavo F. Padovan 43190a708f8fSGustavo F. Padovan done: 43200a708f8fSGustavo F. Padovan if (sk) 4321aa2ac881SGustavo F. Padovan release_sock(sk); 43220a708f8fSGustavo F. Padovan 43230a708f8fSGustavo F. Padovan return 0; 43240a708f8fSGustavo F. Padovan } 43250a708f8fSGustavo F. Padovan 43260a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb) 43270a708f8fSGustavo F. Padovan { 43286dcae1eaSDavid S. Miller struct sock *sk = NULL; 432923691d75SGustavo F. Padovan struct l2cap_chan *chan; 43300a708f8fSGustavo F. Padovan 433123691d75SGustavo F. Padovan chan = l2cap_global_chan_by_psm(0, psm, conn->src); 433223691d75SGustavo F. Padovan if (!chan) 43330a708f8fSGustavo F. Padovan goto drop; 43340a708f8fSGustavo F. Padovan 433523691d75SGustavo F. Padovan sk = chan->sk; 433623691d75SGustavo F. Padovan 4337aa2ac881SGustavo F. Padovan lock_sock(sk); 43380a708f8fSGustavo F. Padovan 43390a708f8fSGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 43400a708f8fSGustavo F. Padovan 434189bc500eSGustavo F. Padovan if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) 43420a708f8fSGustavo F. Padovan goto drop; 43430a708f8fSGustavo F. Padovan 4344e13e21dcSVinicius Costa Gomes if (chan->imtu < skb->len) 43450a708f8fSGustavo F. Padovan goto drop; 43460a708f8fSGustavo F. Padovan 434723070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 43480a708f8fSGustavo F. Padovan goto done; 43490a708f8fSGustavo F. Padovan 43500a708f8fSGustavo F. Padovan drop: 43510a708f8fSGustavo F. Padovan kfree_skb(skb); 43520a708f8fSGustavo F. Padovan 43530a708f8fSGustavo F. Padovan done: 43540a708f8fSGustavo F. Padovan if (sk) 4355aa2ac881SGustavo F. Padovan release_sock(sk); 43560a708f8fSGustavo F. Padovan return 0; 43570a708f8fSGustavo F. Padovan } 43580a708f8fSGustavo F. Padovan 43599f69bda6SGustavo F. Padovan static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb) 43609f69bda6SGustavo F. Padovan { 43616dcae1eaSDavid S. Miller struct sock *sk = NULL; 436223691d75SGustavo F. Padovan struct l2cap_chan *chan; 43639f69bda6SGustavo F. Padovan 436423691d75SGustavo F. Padovan chan = l2cap_global_chan_by_scid(0, cid, conn->src); 436523691d75SGustavo F. Padovan if (!chan) 43669f69bda6SGustavo F. Padovan goto drop; 43679f69bda6SGustavo F. Padovan 436823691d75SGustavo F. Padovan sk = chan->sk; 436923691d75SGustavo F. Padovan 4370aa2ac881SGustavo F. Padovan lock_sock(sk); 43719f69bda6SGustavo F. Padovan 43729f69bda6SGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 43739f69bda6SGustavo F. Padovan 437489bc500eSGustavo F. Padovan if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) 43759f69bda6SGustavo F. Padovan goto drop; 43769f69bda6SGustavo F. Padovan 4377e13e21dcSVinicius Costa Gomes if (chan->imtu < skb->len) 43789f69bda6SGustavo F. Padovan goto drop; 43799f69bda6SGustavo F. Padovan 438023070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 43819f69bda6SGustavo F. Padovan goto done; 43829f69bda6SGustavo F. Padovan 43839f69bda6SGustavo F. Padovan drop: 43849f69bda6SGustavo F. Padovan kfree_skb(skb); 43859f69bda6SGustavo F. Padovan 43869f69bda6SGustavo F. Padovan done: 43879f69bda6SGustavo F. Padovan if (sk) 4388aa2ac881SGustavo F. Padovan release_sock(sk); 43899f69bda6SGustavo F. Padovan return 0; 43909f69bda6SGustavo F. Padovan } 43919f69bda6SGustavo F. Padovan 43920a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) 43930a708f8fSGustavo F. Padovan { 43940a708f8fSGustavo F. Padovan struct l2cap_hdr *lh = (void *) skb->data; 43950a708f8fSGustavo F. Padovan u16 cid, len; 43960a708f8fSGustavo F. Padovan __le16 psm; 43970a708f8fSGustavo F. Padovan 43980a708f8fSGustavo F. Padovan skb_pull(skb, L2CAP_HDR_SIZE); 43990a708f8fSGustavo F. Padovan cid = __le16_to_cpu(lh->cid); 44000a708f8fSGustavo F. Padovan len = __le16_to_cpu(lh->len); 44010a708f8fSGustavo F. Padovan 44020a708f8fSGustavo F. Padovan if (len != skb->len) { 44030a708f8fSGustavo F. Padovan kfree_skb(skb); 44040a708f8fSGustavo F. Padovan return; 44050a708f8fSGustavo F. Padovan } 44060a708f8fSGustavo F. Padovan 44070a708f8fSGustavo F. Padovan BT_DBG("len %d, cid 0x%4.4x", len, cid); 44080a708f8fSGustavo F. Padovan 44090a708f8fSGustavo F. Padovan switch (cid) { 44103300d9a9SClaudio Takahasi case L2CAP_CID_LE_SIGNALING: 44110a708f8fSGustavo F. Padovan case L2CAP_CID_SIGNALING: 44120a708f8fSGustavo F. Padovan l2cap_sig_channel(conn, skb); 44130a708f8fSGustavo F. Padovan break; 44140a708f8fSGustavo F. Padovan 44150a708f8fSGustavo F. Padovan case L2CAP_CID_CONN_LESS: 44160a708f8fSGustavo F. Padovan psm = get_unaligned_le16(skb->data); 44170a708f8fSGustavo F. Padovan skb_pull(skb, 2); 44180a708f8fSGustavo F. Padovan l2cap_conless_channel(conn, psm, skb); 44190a708f8fSGustavo F. Padovan break; 44200a708f8fSGustavo F. Padovan 44219f69bda6SGustavo F. Padovan case L2CAP_CID_LE_DATA: 44229f69bda6SGustavo F. Padovan l2cap_att_channel(conn, cid, skb); 44239f69bda6SGustavo F. Padovan break; 44249f69bda6SGustavo F. Padovan 4425b501d6a1SAnderson Briglia case L2CAP_CID_SMP: 4426b501d6a1SAnderson Briglia if (smp_sig_channel(conn, skb)) 4427b501d6a1SAnderson Briglia l2cap_conn_del(conn->hcon, EACCES); 4428b501d6a1SAnderson Briglia break; 4429b501d6a1SAnderson Briglia 44300a708f8fSGustavo F. Padovan default: 44310a708f8fSGustavo F. Padovan l2cap_data_channel(conn, cid, skb); 44320a708f8fSGustavo F. Padovan break; 44330a708f8fSGustavo F. Padovan } 44340a708f8fSGustavo F. Padovan } 44350a708f8fSGustavo F. Padovan 44360a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */ 44370a708f8fSGustavo F. Padovan 4438686ebf28SUlisses Furquim int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) 44390a708f8fSGustavo F. Padovan { 44400a708f8fSGustavo F. Padovan int exact = 0, lm1 = 0, lm2 = 0; 444123691d75SGustavo F. Padovan struct l2cap_chan *c; 44420a708f8fSGustavo F. Padovan 44430a708f8fSGustavo F. Padovan BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); 44440a708f8fSGustavo F. Padovan 44450a708f8fSGustavo F. Padovan /* Find listening sockets and check their link_mode */ 444623691d75SGustavo F. Padovan read_lock(&chan_list_lock); 444723691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 444823691d75SGustavo F. Padovan struct sock *sk = c->sk; 44494343478fSGustavo F. Padovan 445089bc500eSGustavo F. Padovan if (c->state != BT_LISTEN) 44510a708f8fSGustavo F. Padovan continue; 44520a708f8fSGustavo F. Padovan 44530a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { 44540a708f8fSGustavo F. Padovan lm1 |= HCI_LM_ACCEPT; 445543bd0f32SAndrei Emeltchenko if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 44560a708f8fSGustavo F. Padovan lm1 |= HCI_LM_MASTER; 44570a708f8fSGustavo F. Padovan exact++; 44580a708f8fSGustavo F. Padovan } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { 44590a708f8fSGustavo F. Padovan lm2 |= HCI_LM_ACCEPT; 446043bd0f32SAndrei Emeltchenko if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 44610a708f8fSGustavo F. Padovan lm2 |= HCI_LM_MASTER; 44620a708f8fSGustavo F. Padovan } 44630a708f8fSGustavo F. Padovan } 446423691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 44650a708f8fSGustavo F. Padovan 44660a708f8fSGustavo F. Padovan return exact ? lm1 : lm2; 44670a708f8fSGustavo F. Padovan } 44680a708f8fSGustavo F. Padovan 4469686ebf28SUlisses Furquim int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) 44700a708f8fSGustavo F. Padovan { 44710a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 44720a708f8fSGustavo F. Padovan 44730a708f8fSGustavo F. Padovan BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); 44740a708f8fSGustavo F. Padovan 44750a708f8fSGustavo F. Padovan if (!status) { 44760a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, status); 44770a708f8fSGustavo F. Padovan if (conn) 44780a708f8fSGustavo F. Padovan l2cap_conn_ready(conn); 44790a708f8fSGustavo F. Padovan } else 4480e175072fSJoe Perches l2cap_conn_del(hcon, bt_to_errno(status)); 44810a708f8fSGustavo F. Padovan 44820a708f8fSGustavo F. Padovan return 0; 44830a708f8fSGustavo F. Padovan } 44840a708f8fSGustavo F. Padovan 4485686ebf28SUlisses Furquim int l2cap_disconn_ind(struct hci_conn *hcon) 44860a708f8fSGustavo F. Padovan { 44870a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 44880a708f8fSGustavo F. Padovan 44890a708f8fSGustavo F. Padovan BT_DBG("hcon %p", hcon); 44900a708f8fSGustavo F. Padovan 4491686ebf28SUlisses Furquim if (!conn) 44929f5a0d7bSAndrei Emeltchenko return HCI_ERROR_REMOTE_USER_TERM; 44930a708f8fSGustavo F. Padovan return conn->disc_reason; 44940a708f8fSGustavo F. Padovan } 44950a708f8fSGustavo F. Padovan 4496686ebf28SUlisses Furquim int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) 44970a708f8fSGustavo F. Padovan { 44980a708f8fSGustavo F. Padovan BT_DBG("hcon %p reason %d", hcon, reason); 44990a708f8fSGustavo F. Padovan 4500e175072fSJoe Perches l2cap_conn_del(hcon, bt_to_errno(reason)); 45010a708f8fSGustavo F. Padovan return 0; 45020a708f8fSGustavo F. Padovan } 45030a708f8fSGustavo F. Padovan 45044343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) 45050a708f8fSGustavo F. Padovan { 4506715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) 45070a708f8fSGustavo F. Padovan return; 45080a708f8fSGustavo F. Padovan 45090a708f8fSGustavo F. Padovan if (encrypt == 0x00) { 45104343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) { 4511c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 4512b83ddfe2SAndrzej Kaczmarek __set_chan_timer(chan, 4513b83ddfe2SAndrzej Kaczmarek msecs_to_jiffies(L2CAP_ENC_TIMEOUT)); 45144343478fSGustavo F. Padovan } else if (chan->sec_level == BT_SECURITY_HIGH) 45150f852724SGustavo F. Padovan l2cap_chan_close(chan, ECONNREFUSED); 45160a708f8fSGustavo F. Padovan } else { 45174343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) 4518c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 45190a708f8fSGustavo F. Padovan } 45200a708f8fSGustavo F. Padovan } 45210a708f8fSGustavo F. Padovan 4522686ebf28SUlisses Furquim int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) 45230a708f8fSGustavo F. Padovan { 45240a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 452548454079SGustavo F. Padovan struct l2cap_chan *chan; 45260a708f8fSGustavo F. Padovan 45270a708f8fSGustavo F. Padovan if (!conn) 45280a708f8fSGustavo F. Padovan return 0; 45290a708f8fSGustavo F. Padovan 45300a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 45310a708f8fSGustavo F. Padovan 4532160dc6acSVinicius Costa Gomes if (hcon->type == LE_LINK) { 4533160dc6acSVinicius Costa Gomes smp_distribute_keys(conn, 0); 453417cd3f37SUlisses Furquim cancel_delayed_work(&conn->security_timer); 4535160dc6acSVinicius Costa Gomes } 4536160dc6acSVinicius Costa Gomes 45373df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 45380a708f8fSGustavo F. Padovan 45393df91ea2SAndrei Emeltchenko list_for_each_entry(chan, &conn->chan_l, list) { 454048454079SGustavo F. Padovan struct sock *sk = chan->sk; 4541baa7e1faSGustavo F. Padovan 45420a708f8fSGustavo F. Padovan bh_lock_sock(sk); 45430a708f8fSGustavo F. Padovan 4544f1cb9af5SVinicius Costa Gomes BT_DBG("chan->scid %d", chan->scid); 4545f1cb9af5SVinicius Costa Gomes 4546f1cb9af5SVinicius Costa Gomes if (chan->scid == L2CAP_CID_LE_DATA) { 4547f1cb9af5SVinicius Costa Gomes if (!status && encrypt) { 4548f1cb9af5SVinicius Costa Gomes chan->sec_level = hcon->sec_level; 4549cf4cd009SAndrei Emeltchenko l2cap_chan_ready(chan); 4550f1cb9af5SVinicius Costa Gomes } 4551f1cb9af5SVinicius Costa Gomes 4552f1cb9af5SVinicius Costa Gomes bh_unlock_sock(sk); 4553f1cb9af5SVinicius Costa Gomes continue; 4554f1cb9af5SVinicius Costa Gomes } 4555f1cb9af5SVinicius Costa Gomes 4556c1360a1cSGustavo F. Padovan if (test_bit(CONF_CONNECT_PEND, &chan->conf_state)) { 45570a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 45580a708f8fSGustavo F. Padovan continue; 45590a708f8fSGustavo F. Padovan } 45600a708f8fSGustavo F. Padovan 456189bc500eSGustavo F. Padovan if (!status && (chan->state == BT_CONNECTED || 456289bc500eSGustavo F. Padovan chan->state == BT_CONFIG)) { 45634343478fSGustavo F. Padovan l2cap_check_encryption(chan, encrypt); 45640a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 45650a708f8fSGustavo F. Padovan continue; 45660a708f8fSGustavo F. Padovan } 45670a708f8fSGustavo F. Padovan 456889bc500eSGustavo F. Padovan if (chan->state == BT_CONNECT) { 45690a708f8fSGustavo F. Padovan if (!status) { 45700a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 4571fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 4572fe4128e0SGustavo F. Padovan req.psm = chan->psm; 45730a708f8fSGustavo F. Padovan 4574fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 4575c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 45760a708f8fSGustavo F. Padovan 4577fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 45780a708f8fSGustavo F. Padovan L2CAP_CONN_REQ, sizeof(req), &req); 45790a708f8fSGustavo F. Padovan } else { 4580c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 4581b83ddfe2SAndrzej Kaczmarek __set_chan_timer(chan, 4582b83ddfe2SAndrzej Kaczmarek msecs_to_jiffies(L2CAP_DISC_TIMEOUT)); 45830a708f8fSGustavo F. Padovan } 458489bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT2) { 45850a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 4586df3c3931SJohan Hedberg __u16 res, stat; 45870a708f8fSGustavo F. Padovan 45880a708f8fSGustavo F. Padovan if (!status) { 4589df3c3931SJohan Hedberg if (bt_sk(sk)->defer_setup) { 4590df3c3931SJohan Hedberg struct sock *parent = bt_sk(sk)->parent; 4591df3c3931SJohan Hedberg res = L2CAP_CR_PEND; 4592df3c3931SJohan Hedberg stat = L2CAP_CS_AUTHOR_PEND; 459305e9a2f6SIlia Kolomisnky if (parent) 4594df3c3931SJohan Hedberg parent->sk_data_ready(parent, 0); 4595df3c3931SJohan Hedberg } else { 45960e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_CONFIG); 4597df3c3931SJohan Hedberg res = L2CAP_CR_SUCCESS; 4598df3c3931SJohan Hedberg stat = L2CAP_CS_NO_INFO; 4599df3c3931SJohan Hedberg } 46000a708f8fSGustavo F. Padovan } else { 46010e587be7SAndrei Emeltchenko __l2cap_state_change(chan, BT_DISCONN); 4602b83ddfe2SAndrzej Kaczmarek __set_chan_timer(chan, 4603b83ddfe2SAndrzej Kaczmarek msecs_to_jiffies(L2CAP_DISC_TIMEOUT)); 4604df3c3931SJohan Hedberg res = L2CAP_CR_SEC_BLOCK; 4605df3c3931SJohan Hedberg stat = L2CAP_CS_NO_INFO; 46060a708f8fSGustavo F. Padovan } 46070a708f8fSGustavo F. Padovan 4608fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4609fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 4610df3c3931SJohan Hedberg rsp.result = cpu_to_le16(res); 4611df3c3931SJohan Hedberg rsp.status = cpu_to_le16(stat); 4612fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4613fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 46140a708f8fSGustavo F. Padovan } 46150a708f8fSGustavo F. Padovan 46160a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 46170a708f8fSGustavo F. Padovan } 46180a708f8fSGustavo F. Padovan 46193df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 46200a708f8fSGustavo F. Padovan 46210a708f8fSGustavo F. Padovan return 0; 46220a708f8fSGustavo F. Padovan } 46230a708f8fSGustavo F. Padovan 4624686ebf28SUlisses Furquim int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) 46250a708f8fSGustavo F. Padovan { 46260a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 46270a708f8fSGustavo F. Padovan 46280a708f8fSGustavo F. Padovan if (!conn) 46290a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 46300a708f8fSGustavo F. Padovan 46310a708f8fSGustavo F. Padovan if (!conn) 46320a708f8fSGustavo F. Padovan goto drop; 46330a708f8fSGustavo F. Padovan 46340a708f8fSGustavo F. Padovan BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); 46350a708f8fSGustavo F. Padovan 46360a708f8fSGustavo F. Padovan if (!(flags & ACL_CONT)) { 46370a708f8fSGustavo F. Padovan struct l2cap_hdr *hdr; 463848454079SGustavo F. Padovan struct l2cap_chan *chan; 46390a708f8fSGustavo F. Padovan u16 cid; 46400a708f8fSGustavo F. Padovan int len; 46410a708f8fSGustavo F. Padovan 46420a708f8fSGustavo F. Padovan if (conn->rx_len) { 46430a708f8fSGustavo F. Padovan BT_ERR("Unexpected start frame (len %d)", skb->len); 46440a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 46450a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 46460a708f8fSGustavo F. Padovan conn->rx_len = 0; 46470a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46480a708f8fSGustavo F. Padovan } 46490a708f8fSGustavo F. Padovan 46500a708f8fSGustavo F. Padovan /* Start fragment always begin with Basic L2CAP header */ 46510a708f8fSGustavo F. Padovan if (skb->len < L2CAP_HDR_SIZE) { 46520a708f8fSGustavo F. Padovan BT_ERR("Frame is too short (len %d)", skb->len); 46530a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46540a708f8fSGustavo F. Padovan goto drop; 46550a708f8fSGustavo F. Padovan } 46560a708f8fSGustavo F. Padovan 46570a708f8fSGustavo F. Padovan hdr = (struct l2cap_hdr *) skb->data; 46580a708f8fSGustavo F. Padovan len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; 46590a708f8fSGustavo F. Padovan cid = __le16_to_cpu(hdr->cid); 46600a708f8fSGustavo F. Padovan 46610a708f8fSGustavo F. Padovan if (len == skb->len) { 46620a708f8fSGustavo F. Padovan /* Complete frame received */ 46630a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, skb); 46640a708f8fSGustavo F. Padovan return 0; 46650a708f8fSGustavo F. Padovan } 46660a708f8fSGustavo F. Padovan 46670a708f8fSGustavo F. Padovan BT_DBG("Start: total len %d, frag len %d", len, skb->len); 46680a708f8fSGustavo F. Padovan 46690a708f8fSGustavo F. Padovan if (skb->len > len) { 46700a708f8fSGustavo F. Padovan BT_ERR("Frame is too long (len %d, expected len %d)", 46710a708f8fSGustavo F. Padovan skb->len, len); 46720a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46730a708f8fSGustavo F. Padovan goto drop; 46740a708f8fSGustavo F. Padovan } 46750a708f8fSGustavo F. Padovan 4676baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 46770a708f8fSGustavo F. Padovan 467848454079SGustavo F. Padovan if (chan && chan->sk) { 467948454079SGustavo F. Padovan struct sock *sk = chan->sk; 46803df91ea2SAndrei Emeltchenko lock_sock(sk); 468148454079SGustavo F. Padovan 46820c1bc5c6SGustavo F. Padovan if (chan->imtu < len - L2CAP_HDR_SIZE) { 468348454079SGustavo F. Padovan BT_ERR("Frame exceeding recv MTU (len %d, " 468448454079SGustavo F. Padovan "MTU %d)", len, 46850c1bc5c6SGustavo F. Padovan chan->imtu); 4686aa2ac881SGustavo F. Padovan release_sock(sk); 46870a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46880a708f8fSGustavo F. Padovan goto drop; 46890a708f8fSGustavo F. Padovan } 4690aa2ac881SGustavo F. Padovan release_sock(sk); 469148454079SGustavo F. Padovan } 46920a708f8fSGustavo F. Padovan 46930a708f8fSGustavo F. Padovan /* Allocate skb for the complete frame (with header) */ 46940a708f8fSGustavo F. Padovan conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); 46950a708f8fSGustavo F. Padovan if (!conn->rx_skb) 46960a708f8fSGustavo F. Padovan goto drop; 46970a708f8fSGustavo F. Padovan 46980a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 46990a708f8fSGustavo F. Padovan skb->len); 47000a708f8fSGustavo F. Padovan conn->rx_len = len - skb->len; 47010a708f8fSGustavo F. Padovan } else { 47020a708f8fSGustavo F. Padovan BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); 47030a708f8fSGustavo F. Padovan 47040a708f8fSGustavo F. Padovan if (!conn->rx_len) { 47050a708f8fSGustavo F. Padovan BT_ERR("Unexpected continuation frame (len %d)", skb->len); 47060a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 47070a708f8fSGustavo F. Padovan goto drop; 47080a708f8fSGustavo F. Padovan } 47090a708f8fSGustavo F. Padovan 47100a708f8fSGustavo F. Padovan if (skb->len > conn->rx_len) { 47110a708f8fSGustavo F. Padovan BT_ERR("Fragment is too long (len %d, expected %d)", 47120a708f8fSGustavo F. Padovan skb->len, conn->rx_len); 47130a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 47140a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 47150a708f8fSGustavo F. Padovan conn->rx_len = 0; 47160a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 47170a708f8fSGustavo F. Padovan goto drop; 47180a708f8fSGustavo F. Padovan } 47190a708f8fSGustavo F. Padovan 47200a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 47210a708f8fSGustavo F. Padovan skb->len); 47220a708f8fSGustavo F. Padovan conn->rx_len -= skb->len; 47230a708f8fSGustavo F. Padovan 47240a708f8fSGustavo F. Padovan if (!conn->rx_len) { 47250a708f8fSGustavo F. Padovan /* Complete frame received */ 47260a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, conn->rx_skb); 47270a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 47280a708f8fSGustavo F. Padovan } 47290a708f8fSGustavo F. Padovan } 47300a708f8fSGustavo F. Padovan 47310a708f8fSGustavo F. Padovan drop: 47320a708f8fSGustavo F. Padovan kfree_skb(skb); 47330a708f8fSGustavo F. Padovan return 0; 47340a708f8fSGustavo F. Padovan } 47350a708f8fSGustavo F. Padovan 47360a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p) 47370a708f8fSGustavo F. Padovan { 473823691d75SGustavo F. Padovan struct l2cap_chan *c; 47390a708f8fSGustavo F. Padovan 4740333055f2SGustavo F. Padovan read_lock(&chan_list_lock); 47410a708f8fSGustavo F. Padovan 474223691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 474323691d75SGustavo F. Padovan struct sock *sk = c->sk; 47440a708f8fSGustavo F. Padovan 4745903d343eSGustavo F. Padovan seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", 47460a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->src), 47470a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->dst), 474889bc500eSGustavo F. Padovan c->state, __le16_to_cpu(c->psm), 474923691d75SGustavo F. Padovan c->scid, c->dcid, c->imtu, c->omtu, 475023691d75SGustavo F. Padovan c->sec_level, c->mode); 47510a708f8fSGustavo F. Padovan } 47520a708f8fSGustavo F. Padovan 4753333055f2SGustavo F. Padovan read_unlock(&chan_list_lock); 47540a708f8fSGustavo F. Padovan 47550a708f8fSGustavo F. Padovan return 0; 47560a708f8fSGustavo F. Padovan } 47570a708f8fSGustavo F. Padovan 47580a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file) 47590a708f8fSGustavo F. Padovan { 47600a708f8fSGustavo F. Padovan return single_open(file, l2cap_debugfs_show, inode->i_private); 47610a708f8fSGustavo F. Padovan } 47620a708f8fSGustavo F. Padovan 47630a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = { 47640a708f8fSGustavo F. Padovan .open = l2cap_debugfs_open, 47650a708f8fSGustavo F. Padovan .read = seq_read, 47660a708f8fSGustavo F. Padovan .llseek = seq_lseek, 47670a708f8fSGustavo F. Padovan .release = single_release, 47680a708f8fSGustavo F. Padovan }; 47690a708f8fSGustavo F. Padovan 47700a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs; 47710a708f8fSGustavo F. Padovan 477264274518SGustavo F. Padovan int __init l2cap_init(void) 47730a708f8fSGustavo F. Padovan { 47740a708f8fSGustavo F. Padovan int err; 47750a708f8fSGustavo F. Padovan 4776bb58f747SGustavo F. Padovan err = l2cap_init_sockets(); 47770a708f8fSGustavo F. Padovan if (err < 0) 47780a708f8fSGustavo F. Padovan return err; 47790a708f8fSGustavo F. Padovan 47800a708f8fSGustavo F. Padovan if (bt_debugfs) { 47810a708f8fSGustavo F. Padovan l2cap_debugfs = debugfs_create_file("l2cap", 0444, 47820a708f8fSGustavo F. Padovan bt_debugfs, NULL, &l2cap_debugfs_fops); 47830a708f8fSGustavo F. Padovan if (!l2cap_debugfs) 47840a708f8fSGustavo F. Padovan BT_ERR("Failed to create L2CAP debug file"); 47850a708f8fSGustavo F. Padovan } 47860a708f8fSGustavo F. Padovan 47870a708f8fSGustavo F. Padovan return 0; 47880a708f8fSGustavo F. Padovan } 47890a708f8fSGustavo F. Padovan 479064274518SGustavo F. Padovan void l2cap_exit(void) 47910a708f8fSGustavo F. Padovan { 47920a708f8fSGustavo F. Padovan debugfs_remove(l2cap_debugfs); 4793bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 47940a708f8fSGustavo F. Padovan } 47950a708f8fSGustavo F. Padovan 47960a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644); 47970a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); 4798