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 20289bc500eSGustavo F. Padovan 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 211721c4181SGustavo F. Padovan static void l2cap_chan_timeout(struct work_struct *work) 212ab07801dSGustavo F. Padovan { 213721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 214721c4181SGustavo F. Padovan chan_timer.work); 2153df91ea2SAndrei Emeltchenko struct l2cap_conn *conn = chan->conn; 216ab07801dSGustavo F. Padovan struct sock *sk = chan->sk; 217ab07801dSGustavo F. Padovan int reason; 218ab07801dSGustavo F. Padovan 219e05dcc32SAndrei Emeltchenko BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); 220ab07801dSGustavo F. Padovan 2213df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 222721c4181SGustavo F. Padovan lock_sock(sk); 223ab07801dSGustavo F. Padovan 22489bc500eSGustavo F. Padovan if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) 225ab07801dSGustavo F. Padovan reason = ECONNREFUSED; 22689bc500eSGustavo F. Padovan else if (chan->state == BT_CONNECT && 227ab07801dSGustavo F. Padovan chan->sec_level != BT_SECURITY_SDP) 228ab07801dSGustavo F. Padovan reason = ECONNREFUSED; 229ab07801dSGustavo F. Padovan else 230ab07801dSGustavo F. Padovan reason = ETIMEDOUT; 231ab07801dSGustavo F. Padovan 2320f852724SGustavo F. Padovan l2cap_chan_close(chan, reason); 233ab07801dSGustavo F. Padovan 234721c4181SGustavo F. Padovan release_sock(sk); 235ab07801dSGustavo F. Padovan 236ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 2373df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 2383df91ea2SAndrei Emeltchenko 239371fd835SUlisses Furquim l2cap_chan_put(chan); 240ab07801dSGustavo F. Padovan } 241ab07801dSGustavo F. Padovan 24223691d75SGustavo F. Padovan struct l2cap_chan *l2cap_chan_create(struct sock *sk) 2430a708f8fSGustavo F. Padovan { 24448454079SGustavo F. Padovan struct l2cap_chan *chan; 2450a708f8fSGustavo F. Padovan 24648454079SGustavo F. Padovan chan = kzalloc(sizeof(*chan), GFP_ATOMIC); 24748454079SGustavo F. Padovan if (!chan) 24848454079SGustavo F. Padovan return NULL; 2490a708f8fSGustavo F. Padovan 250c03b355eSAndrei Emeltchenko mutex_init(&chan->lock); 251c03b355eSAndrei Emeltchenko 25248454079SGustavo F. Padovan chan->sk = sk; 25348454079SGustavo F. Padovan 254333055f2SGustavo F. Padovan write_lock(&chan_list_lock); 25523691d75SGustavo F. Padovan list_add(&chan->global_l, &chan_list); 256333055f2SGustavo F. Padovan write_unlock(&chan_list_lock); 25723691d75SGustavo F. Padovan 258721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); 259ab07801dSGustavo F. Padovan 26089bc500eSGustavo F. Padovan chan->state = BT_OPEN; 26189bc500eSGustavo F. Padovan 26271ba0e56SGustavo F. Padovan atomic_set(&chan->refcnt, 1); 26371ba0e56SGustavo F. Padovan 264abc545b8SSzymon Janc BT_DBG("sk %p chan %p", sk, chan); 265abc545b8SSzymon Janc 26648454079SGustavo F. Padovan return chan; 2670a708f8fSGustavo F. Padovan } 2680a708f8fSGustavo F. Padovan 26923691d75SGustavo F. Padovan void l2cap_chan_destroy(struct l2cap_chan *chan) 2706ff5abbfSGustavo F. Padovan { 271333055f2SGustavo F. Padovan write_lock(&chan_list_lock); 27223691d75SGustavo F. Padovan list_del(&chan->global_l); 273333055f2SGustavo F. Padovan write_unlock(&chan_list_lock); 27423691d75SGustavo F. Padovan 275371fd835SUlisses Furquim l2cap_chan_put(chan); 2766ff5abbfSGustavo F. Padovan } 2776ff5abbfSGustavo F. Padovan 2783d57dc68SGustavo F. Padovan static void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 2790a708f8fSGustavo F. Padovan { 2800a708f8fSGustavo F. Padovan BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, 281fe4128e0SGustavo F. Padovan chan->psm, chan->dcid); 2820a708f8fSGustavo F. Padovan 2839f5a0d7bSAndrei Emeltchenko conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; 2840a708f8fSGustavo F. Padovan 2858c1d787bSGustavo F. Padovan chan->conn = conn; 2860a708f8fSGustavo F. Padovan 2875491120eSAndrei Emeltchenko switch (chan->chan_type) { 2885491120eSAndrei Emeltchenko case L2CAP_CHAN_CONN_ORIENTED: 289b62f328bSVille Tervo if (conn->hcon->type == LE_LINK) { 290b62f328bSVille Tervo /* LE connection */ 2910c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_LE_DEFAULT_MTU; 292fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_LE_DATA; 293fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_LE_DATA; 294b62f328bSVille Tervo } else { 2950a708f8fSGustavo F. Padovan /* Alloc CID for connection-oriented socket */ 296fe4128e0SGustavo F. Padovan chan->scid = l2cap_alloc_cid(conn); 2970c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 298b62f328bSVille Tervo } 2995491120eSAndrei Emeltchenko break; 3005491120eSAndrei Emeltchenko 3015491120eSAndrei Emeltchenko case L2CAP_CHAN_CONN_LESS: 3020a708f8fSGustavo F. Padovan /* Connectionless socket */ 303fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_CONN_LESS; 304fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_CONN_LESS; 3050c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 3065491120eSAndrei Emeltchenko break; 3075491120eSAndrei Emeltchenko 3085491120eSAndrei Emeltchenko default: 3090a708f8fSGustavo F. Padovan /* Raw socket can send/recv signalling messages only */ 310fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_SIGNALING; 311fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_SIGNALING; 3120c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 3130a708f8fSGustavo F. Padovan } 3140a708f8fSGustavo F. Padovan 3158f7975b1SAndrei Emeltchenko chan->local_id = L2CAP_BESTEFFORT_ID; 3168f7975b1SAndrei Emeltchenko chan->local_stype = L2CAP_SERV_BESTEFFORT; 3178f7975b1SAndrei Emeltchenko chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; 3188f7975b1SAndrei Emeltchenko chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; 3198f7975b1SAndrei Emeltchenko chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; 3208f7975b1SAndrei Emeltchenko chan->local_flush_to = L2CAP_DEFAULT_FLUSH_TO; 3218f7975b1SAndrei Emeltchenko 322371fd835SUlisses Furquim l2cap_chan_hold(chan); 323baa7e1faSGustavo F. Padovan 3243df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 3253df91ea2SAndrei Emeltchenko list_add(&chan->list, &conn->chan_l); 3263df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 3270a708f8fSGustavo F. Padovan } 3280a708f8fSGustavo F. Padovan 3290a708f8fSGustavo F. Padovan /* Delete channel. 3300a708f8fSGustavo F. Padovan * Must be called on the locked socket. */ 3314519de9aSGustavo F. Padovan static void l2cap_chan_del(struct l2cap_chan *chan, int err) 3320a708f8fSGustavo F. Padovan { 33348454079SGustavo F. Padovan struct sock *sk = chan->sk; 3348c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 3350a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 3360a708f8fSGustavo F. Padovan 337c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 3380a708f8fSGustavo F. Padovan 33949208c9cSGustavo F. Padovan BT_DBG("chan %p, conn %p, err %d", chan, conn, err); 3400a708f8fSGustavo F. Padovan 3410a708f8fSGustavo F. Padovan if (conn) { 342baa7e1faSGustavo F. Padovan /* Delete from channel list */ 3433df91ea2SAndrei Emeltchenko list_del(&chan->list); 3443d57dc68SGustavo F. Padovan 345371fd835SUlisses Furquim l2cap_chan_put(chan); 346baa7e1faSGustavo F. Padovan 3478c1d787bSGustavo F. Padovan chan->conn = NULL; 3480a708f8fSGustavo F. Padovan hci_conn_put(conn->hcon); 3490a708f8fSGustavo F. Padovan } 3500a708f8fSGustavo F. Padovan 35189bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CLOSED); 3520a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 3530a708f8fSGustavo F. Padovan 3540a708f8fSGustavo F. Padovan if (err) 3550a708f8fSGustavo F. Padovan sk->sk_err = err; 3560a708f8fSGustavo F. Padovan 3570a708f8fSGustavo F. Padovan if (parent) { 3580a708f8fSGustavo F. Padovan bt_accept_unlink(sk); 3590a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 3600a708f8fSGustavo F. Padovan } else 3610a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 3620a708f8fSGustavo F. Padovan 363c1360a1cSGustavo F. Padovan if (!(test_bit(CONF_OUTPUT_DONE, &chan->conf_state) && 364c1360a1cSGustavo F. Padovan test_bit(CONF_INPUT_DONE, &chan->conf_state))) 3656ff5abbfSGustavo F. Padovan return; 3662ead70b8SGustavo F. Padovan 36758d35f87SGustavo F. Padovan skb_queue_purge(&chan->tx_q); 3680a708f8fSGustavo F. Padovan 3690c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 3700a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 3710a708f8fSGustavo F. Padovan 3721a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 3731a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 3741a09bcb9SGustavo F. Padovan __clear_ack_timer(chan); 3750a708f8fSGustavo F. Padovan 376f1c6775bSGustavo F. Padovan skb_queue_purge(&chan->srej_q); 3770a708f8fSGustavo F. Padovan 37839d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 3790a708f8fSGustavo F. Padovan list_del(&l->list); 3800a708f8fSGustavo F. Padovan kfree(l); 3810a708f8fSGustavo F. Padovan } 3820a708f8fSGustavo F. Padovan } 3830a708f8fSGustavo F. Padovan } 3840a708f8fSGustavo F. Padovan 3854519de9aSGustavo F. Padovan static void l2cap_chan_cleanup_listen(struct sock *parent) 3864519de9aSGustavo F. Padovan { 3874519de9aSGustavo F. Padovan struct sock *sk; 3884519de9aSGustavo F. Padovan 3894519de9aSGustavo F. Padovan BT_DBG("parent %p", parent); 3904519de9aSGustavo F. Padovan 3914519de9aSGustavo F. Padovan /* Close not yet accepted channels */ 3920f852724SGustavo F. Padovan while ((sk = bt_accept_dequeue(parent, NULL))) { 393ba3bd0eeSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 3943df91ea2SAndrei Emeltchenko 395c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 3960f852724SGustavo F. Padovan lock_sock(sk); 397ba3bd0eeSGustavo F. Padovan l2cap_chan_close(chan, ECONNRESET); 3980f852724SGustavo F. Padovan release_sock(sk); 3993df91ea2SAndrei Emeltchenko 400ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 4010f852724SGustavo F. Padovan } 4024519de9aSGustavo F. Padovan } 4034519de9aSGustavo F. Padovan 4040f852724SGustavo F. Padovan void l2cap_chan_close(struct l2cap_chan *chan, int reason) 4054519de9aSGustavo F. Padovan { 4064519de9aSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 4074519de9aSGustavo F. Padovan struct sock *sk = chan->sk; 4084519de9aSGustavo F. Padovan 409e05dcc32SAndrei Emeltchenko BT_DBG("chan %p state %s sk %p", chan, 410e05dcc32SAndrei Emeltchenko state_to_string(chan->state), sk); 4114519de9aSGustavo F. Padovan 41289bc500eSGustavo F. Padovan switch (chan->state) { 4134519de9aSGustavo F. Padovan case BT_LISTEN: 4144519de9aSGustavo F. Padovan l2cap_chan_cleanup_listen(sk); 41589bc500eSGustavo F. Padovan 41689bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CLOSED); 41789bc500eSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 4184519de9aSGustavo F. Padovan break; 4194519de9aSGustavo F. Padovan 4204519de9aSGustavo F. Padovan case BT_CONNECTED: 4214519de9aSGustavo F. Padovan case BT_CONFIG: 422715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 4234519de9aSGustavo F. Padovan conn->hcon->type == ACL_LINK) { 424c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 425c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 4264519de9aSGustavo F. Padovan l2cap_send_disconn_req(conn, chan, reason); 4274519de9aSGustavo F. Padovan } else 4284519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4294519de9aSGustavo F. Padovan break; 4304519de9aSGustavo F. Padovan 4314519de9aSGustavo F. Padovan case BT_CONNECT2: 432715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 4334519de9aSGustavo F. Padovan conn->hcon->type == ACL_LINK) { 4344519de9aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 4354519de9aSGustavo F. Padovan __u16 result; 4364519de9aSGustavo F. Padovan 4374519de9aSGustavo F. Padovan if (bt_sk(sk)->defer_setup) 4384519de9aSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 4394519de9aSGustavo F. Padovan else 4404519de9aSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 44189bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_DISCONN); 4424519de9aSGustavo F. Padovan 4434519de9aSGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4444519de9aSGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 4454519de9aSGustavo F. Padovan rsp.result = cpu_to_le16(result); 4464519de9aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 4474519de9aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4484519de9aSGustavo F. Padovan sizeof(rsp), &rsp); 4494519de9aSGustavo F. Padovan } 4504519de9aSGustavo F. Padovan 4514519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4524519de9aSGustavo F. Padovan break; 4534519de9aSGustavo F. Padovan 4544519de9aSGustavo F. Padovan case BT_CONNECT: 4554519de9aSGustavo F. Padovan case BT_DISCONN: 4564519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4574519de9aSGustavo F. Padovan break; 4584519de9aSGustavo F. Padovan 4594519de9aSGustavo F. Padovan default: 4604519de9aSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 4614519de9aSGustavo F. Padovan break; 4624519de9aSGustavo F. Padovan } 4634519de9aSGustavo F. Padovan } 4644519de9aSGustavo F. Padovan 4654343478fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) 4660a708f8fSGustavo F. Padovan { 467715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_RAW) { 4684343478fSGustavo F. Padovan switch (chan->sec_level) { 4690a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 4700a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING_MITM; 4710a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 4720a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING; 4730a708f8fSGustavo F. Padovan default: 4740a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 4750a708f8fSGustavo F. Padovan } 476fe4128e0SGustavo F. Padovan } else if (chan->psm == cpu_to_le16(0x0001)) { 4774343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_LOW) 4784343478fSGustavo F. Padovan chan->sec_level = BT_SECURITY_SDP; 4790a708f8fSGustavo F. Padovan 4804343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_HIGH) 4810a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING_MITM; 4820a708f8fSGustavo F. Padovan else 4830a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 4840a708f8fSGustavo F. Padovan } else { 4854343478fSGustavo F. Padovan switch (chan->sec_level) { 4860a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 4870a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING_MITM; 4880a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 4890a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING; 4900a708f8fSGustavo F. Padovan default: 4910a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 4920a708f8fSGustavo F. Padovan } 4930a708f8fSGustavo F. Padovan } 4940a708f8fSGustavo F. Padovan } 4950a708f8fSGustavo F. Padovan 4960a708f8fSGustavo F. Padovan /* Service level security */ 497d45fc423SGustavo F. Padovan int l2cap_chan_check_security(struct l2cap_chan *chan) 4980a708f8fSGustavo F. Padovan { 4998c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 5000a708f8fSGustavo F. Padovan __u8 auth_type; 5010a708f8fSGustavo F. Padovan 5024343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 5030a708f8fSGustavo F. Padovan 5044343478fSGustavo F. Padovan return hci_conn_security(conn->hcon, chan->sec_level, auth_type); 5050a708f8fSGustavo F. Padovan } 5060a708f8fSGustavo F. Padovan 507b5ad8b7fSJohannes Berg static u8 l2cap_get_ident(struct l2cap_conn *conn) 5080a708f8fSGustavo F. Padovan { 5090a708f8fSGustavo F. Padovan u8 id; 5100a708f8fSGustavo F. Padovan 5110a708f8fSGustavo F. Padovan /* Get next available identificator. 5120a708f8fSGustavo F. Padovan * 1 - 128 are used by kernel. 5130a708f8fSGustavo F. Padovan * 129 - 199 are reserved. 5140a708f8fSGustavo F. Padovan * 200 - 254 are used by utilities like l2ping, etc. 5150a708f8fSGustavo F. Padovan */ 5160a708f8fSGustavo F. Padovan 517333055f2SGustavo F. Padovan spin_lock(&conn->lock); 5180a708f8fSGustavo F. Padovan 5190a708f8fSGustavo F. Padovan if (++conn->tx_ident > 128) 5200a708f8fSGustavo F. Padovan conn->tx_ident = 1; 5210a708f8fSGustavo F. Padovan 5220a708f8fSGustavo F. Padovan id = conn->tx_ident; 5230a708f8fSGustavo F. Padovan 524333055f2SGustavo F. Padovan spin_unlock(&conn->lock); 5250a708f8fSGustavo F. Padovan 5260a708f8fSGustavo F. Padovan return id; 5270a708f8fSGustavo F. Padovan } 5280a708f8fSGustavo F. Padovan 5294519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data) 5300a708f8fSGustavo F. Padovan { 5310a708f8fSGustavo F. Padovan struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); 5320a708f8fSGustavo F. Padovan u8 flags; 5330a708f8fSGustavo F. Padovan 5340a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x", code); 5350a708f8fSGustavo F. Padovan 5360a708f8fSGustavo F. Padovan if (!skb) 5370a708f8fSGustavo F. Padovan return; 5380a708f8fSGustavo F. Padovan 5390a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 5400a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 5410a708f8fSGustavo F. Padovan else 5420a708f8fSGustavo F. Padovan flags = ACL_START; 5430a708f8fSGustavo F. Padovan 54414b12d0bSJaikumar Ganesh bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; 5455e59b791SLuiz Augusto von Dentz skb->priority = HCI_PRIO_MAX; 54614b12d0bSJaikumar Ganesh 54773d80debSLuiz Augusto von Dentz hci_send_acl(conn->hchan, skb, flags); 5480a708f8fSGustavo F. Padovan } 5490a708f8fSGustavo F. Padovan 55073d80debSLuiz Augusto von Dentz static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) 55173d80debSLuiz Augusto von Dentz { 55273d80debSLuiz Augusto von Dentz struct hci_conn *hcon = chan->conn->hcon; 55373d80debSLuiz Augusto von Dentz u16 flags; 55473d80debSLuiz Augusto von Dentz 55573d80debSLuiz Augusto von Dentz BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len, 55673d80debSLuiz Augusto von Dentz skb->priority); 55773d80debSLuiz Augusto von Dentz 55873d80debSLuiz Augusto von Dentz if (!test_bit(FLAG_FLUSHABLE, &chan->flags) && 55973d80debSLuiz Augusto von Dentz lmp_no_flush_capable(hcon->hdev)) 56073d80debSLuiz Augusto von Dentz flags = ACL_START_NO_FLUSH; 56173d80debSLuiz Augusto von Dentz else 56273d80debSLuiz Augusto von Dentz flags = ACL_START; 56373d80debSLuiz Augusto von Dentz 56473d80debSLuiz Augusto von Dentz bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); 56573d80debSLuiz Augusto von Dentz hci_send_acl(chan->conn->hchan, skb, flags); 5660a708f8fSGustavo F. Padovan } 5670a708f8fSGustavo F. Padovan 56888843ab0SAndrei Emeltchenko static inline void l2cap_send_sframe(struct l2cap_chan *chan, u32 control) 5690a708f8fSGustavo F. Padovan { 5700a708f8fSGustavo F. Padovan struct sk_buff *skb; 5710a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 5728c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 573e4ca6d98SAndrei Emeltchenko int count, hlen; 5740a708f8fSGustavo F. Padovan 57589bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 5760a708f8fSGustavo F. Padovan return; 5770a708f8fSGustavo F. Padovan 578e4ca6d98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 579e4ca6d98SAndrei Emeltchenko hlen = L2CAP_EXT_HDR_SIZE; 580e4ca6d98SAndrei Emeltchenko else 581e4ca6d98SAndrei Emeltchenko hlen = L2CAP_ENH_HDR_SIZE; 5820a708f8fSGustavo F. Padovan 5830a708f8fSGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 58403a51213SAndrei Emeltchenko hlen += L2CAP_FCS_SIZE; 5850a708f8fSGustavo F. Padovan 58688843ab0SAndrei Emeltchenko BT_DBG("chan %p, control 0x%8.8x", chan, control); 5870a708f8fSGustavo F. Padovan 5880a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, hlen); 589793c2f1cSAndrei Emeltchenko 590793c2f1cSAndrei Emeltchenko control |= __set_sframe(chan); 5910a708f8fSGustavo F. Padovan 592e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 59303f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 5940a708f8fSGustavo F. Padovan 595e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_PBIT, &chan->conn_state)) 596e3781735SAndrei Emeltchenko control |= __set_ctrl_poll(chan); 5970a708f8fSGustavo F. Padovan 5980a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 5990a708f8fSGustavo F. Padovan if (!skb) 6000a708f8fSGustavo F. Padovan return; 6010a708f8fSGustavo F. Padovan 6020a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 6030a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); 604fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 60588843ab0SAndrei Emeltchenko 60688843ab0SAndrei Emeltchenko __put_control(chan, control, skb_put(skb, __ctrl_size(chan))); 6070a708f8fSGustavo F. Padovan 60847d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 60903a51213SAndrei Emeltchenko u16 fcs = crc16(0, (u8 *)lh, count - L2CAP_FCS_SIZE); 61003a51213SAndrei Emeltchenko put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); 6110a708f8fSGustavo F. Padovan } 6120a708f8fSGustavo F. Padovan 61373d80debSLuiz Augusto von Dentz skb->priority = HCI_PRIO_MAX; 61473d80debSLuiz Augusto von Dentz l2cap_do_send(chan, skb); 6150a708f8fSGustavo F. Padovan } 6160a708f8fSGustavo F. Padovan 61788843ab0SAndrei Emeltchenko static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u32 control) 6180a708f8fSGustavo F. Padovan { 619e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 620ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 621e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 6220a708f8fSGustavo F. Padovan } else 623ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 6240a708f8fSGustavo F. Padovan 6250b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 6260a708f8fSGustavo F. Padovan 627525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 6280a708f8fSGustavo F. Padovan } 6290a708f8fSGustavo F. Padovan 630b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) 6310a708f8fSGustavo F. Padovan { 632c1360a1cSGustavo F. Padovan return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); 6330a708f8fSGustavo F. Padovan } 6340a708f8fSGustavo F. Padovan 635fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan) 6360a708f8fSGustavo F. Padovan { 6378c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 6380a708f8fSGustavo F. Padovan 6390a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { 6400a708f8fSGustavo F. Padovan if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) 6410a708f8fSGustavo F. Padovan return; 6420a708f8fSGustavo F. Padovan 643d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan) && 6444343478fSGustavo F. Padovan __l2cap_no_conn_pending(chan)) { 6450a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 646fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 647fe4128e0SGustavo F. Padovan req.psm = chan->psm; 6480a708f8fSGustavo F. Padovan 649fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 650c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 6510a708f8fSGustavo F. Padovan 652fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 653fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 6540a708f8fSGustavo F. Padovan } 6550a708f8fSGustavo F. Padovan } else { 6560a708f8fSGustavo F. Padovan struct l2cap_info_req req; 6570a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 6580a708f8fSGustavo F. Padovan 6590a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 6600a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 6610a708f8fSGustavo F. Padovan 662030013d8SGustavo F. Padovan schedule_delayed_work(&conn->info_timer, 6630a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 6640a708f8fSGustavo F. Padovan 6650a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 6660a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 6670a708f8fSGustavo F. Padovan } 6680a708f8fSGustavo F. Padovan } 6690a708f8fSGustavo F. Padovan 6700a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) 6710a708f8fSGustavo F. Padovan { 6720a708f8fSGustavo F. Padovan u32 local_feat_mask = l2cap_feat_mask; 6730a708f8fSGustavo F. Padovan if (!disable_ertm) 6740a708f8fSGustavo F. Padovan local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; 6750a708f8fSGustavo F. Padovan 6760a708f8fSGustavo F. Padovan switch (mode) { 6770a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 6780a708f8fSGustavo F. Padovan return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; 6790a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 6800a708f8fSGustavo F. Padovan return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; 6810a708f8fSGustavo F. Padovan default: 6820a708f8fSGustavo F. Padovan return 0x00; 6830a708f8fSGustavo F. Padovan } 6840a708f8fSGustavo F. Padovan } 6850a708f8fSGustavo F. Padovan 6864519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err) 6870a708f8fSGustavo F. Padovan { 688e92c8e70SGustavo F. Padovan struct sock *sk; 6890a708f8fSGustavo F. Padovan struct l2cap_disconn_req req; 6900a708f8fSGustavo F. Padovan 6910a708f8fSGustavo F. Padovan if (!conn) 6920a708f8fSGustavo F. Padovan return; 6930a708f8fSGustavo F. Padovan 694e92c8e70SGustavo F. Padovan sk = chan->sk; 695e92c8e70SGustavo F. Padovan 6960c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 6971a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 6981a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 6991a09bcb9SGustavo F. Padovan __clear_ack_timer(chan); 7000a708f8fSGustavo F. Padovan } 7010a708f8fSGustavo F. Padovan 702fe4128e0SGustavo F. Padovan req.dcid = cpu_to_le16(chan->dcid); 703fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 7040a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 7050a708f8fSGustavo F. Padovan L2CAP_DISCONN_REQ, sizeof(req), &req); 7060a708f8fSGustavo F. Padovan 70789bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_DISCONN); 7080a708f8fSGustavo F. Padovan sk->sk_err = err; 7090a708f8fSGustavo F. Padovan } 7100a708f8fSGustavo F. Padovan 7110a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */ 7120a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn) 7130a708f8fSGustavo F. Padovan { 7143df91ea2SAndrei Emeltchenko struct l2cap_chan *chan, *tmp; 7150a708f8fSGustavo F. Padovan 7160a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 7170a708f8fSGustavo F. Padovan 7183df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 7190a708f8fSGustavo F. Padovan 7203df91ea2SAndrei Emeltchenko list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { 72148454079SGustavo F. Padovan struct sock *sk = chan->sk; 722baa7e1faSGustavo F. Padovan 7230a708f8fSGustavo F. Padovan bh_lock_sock(sk); 7240a708f8fSGustavo F. Padovan 725715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 7260a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7270a708f8fSGustavo F. Padovan continue; 7280a708f8fSGustavo F. Padovan } 7290a708f8fSGustavo F. Padovan 73089bc500eSGustavo F. Padovan if (chan->state == BT_CONNECT) { 7310a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 7320a708f8fSGustavo F. Padovan 733d45fc423SGustavo F. Padovan if (!l2cap_chan_check_security(chan) || 734b4450035SGustavo F. Padovan !__l2cap_no_conn_pending(chan)) { 7350a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7360a708f8fSGustavo F. Padovan continue; 7370a708f8fSGustavo F. Padovan } 7380a708f8fSGustavo F. Padovan 739c1360a1cSGustavo F. Padovan if (!l2cap_mode_supported(chan->mode, conn->feat_mask) 740c1360a1cSGustavo F. Padovan && test_bit(CONF_STATE2_DEVICE, 741c1360a1cSGustavo F. Padovan &chan->conf_state)) { 7420f852724SGustavo F. Padovan /* l2cap_chan_close() calls list_del(chan) 743820ffdb3SGustavo F. Padovan * so release the lock */ 7440f852724SGustavo F. Padovan l2cap_chan_close(chan, ECONNRESET); 7450a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7460a708f8fSGustavo F. Padovan continue; 7470a708f8fSGustavo F. Padovan } 7480a708f8fSGustavo F. Padovan 749fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 750fe4128e0SGustavo F. Padovan req.psm = chan->psm; 7510a708f8fSGustavo F. Padovan 752fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 753c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 7540a708f8fSGustavo F. Padovan 755fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 756fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 7570a708f8fSGustavo F. Padovan 75889bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT2) { 7590a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 7600a708f8fSGustavo F. Padovan char buf[128]; 761fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 762fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 7630a708f8fSGustavo F. Padovan 764d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan)) { 7650a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 7660a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 7670a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 7680a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); 76905e9a2f6SIlia Kolomisnky if (parent) 7700a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 7710a708f8fSGustavo F. Padovan 7720a708f8fSGustavo F. Padovan } else { 77389bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONFIG); 7740a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 7750a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 7760a708f8fSGustavo F. Padovan } 7770a708f8fSGustavo F. Padovan } else { 7780a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 7790a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); 7800a708f8fSGustavo F. Padovan } 7810a708f8fSGustavo F. Padovan 782fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 783fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 7840a708f8fSGustavo F. Padovan 785c1360a1cSGustavo F. Padovan if (test_bit(CONF_REQ_SENT, &chan->conf_state) || 7860a708f8fSGustavo F. Padovan rsp.result != L2CAP_CR_SUCCESS) { 7870a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7880a708f8fSGustavo F. Padovan continue; 7890a708f8fSGustavo F. Padovan } 7900a708f8fSGustavo F. Padovan 791c1360a1cSGustavo F. Padovan set_bit(CONF_REQ_SENT, &chan->conf_state); 7920a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 79373ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 79473ffa904SGustavo F. Padovan chan->num_conf_req++; 7950a708f8fSGustavo F. Padovan } 7960a708f8fSGustavo F. Padovan 7970a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7980a708f8fSGustavo F. Padovan } 7990a708f8fSGustavo F. Padovan 8003df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 8010a708f8fSGustavo F. Padovan } 8020a708f8fSGustavo F. Padovan 803b62f328bSVille Tervo /* Find socket with cid and source bdaddr. 804b62f328bSVille Tervo * Returns closest match, locked. 805b62f328bSVille Tervo */ 80623691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src) 807b62f328bSVille Tervo { 80823691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 809b62f328bSVille Tervo 81023691d75SGustavo F. Padovan read_lock(&chan_list_lock); 811b62f328bSVille Tervo 81223691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 81323691d75SGustavo F. Padovan struct sock *sk = c->sk; 814fe4128e0SGustavo F. Padovan 81589bc500eSGustavo F. Padovan if (state && c->state != state) 816b62f328bSVille Tervo continue; 817b62f328bSVille Tervo 81823691d75SGustavo F. Padovan if (c->scid == cid) { 819b62f328bSVille Tervo /* Exact match. */ 82023691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 82123691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 82223691d75SGustavo F. Padovan return c; 82323691d75SGustavo F. Padovan } 824b62f328bSVille Tervo 825b62f328bSVille Tervo /* Closest match */ 826b62f328bSVille Tervo if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 82723691d75SGustavo F. Padovan c1 = c; 828b62f328bSVille Tervo } 829b62f328bSVille Tervo } 830280f294fSGustavo F. Padovan 83123691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 832b62f328bSVille Tervo 83323691d75SGustavo F. Padovan return c1; 834b62f328bSVille Tervo } 835b62f328bSVille Tervo 836b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn) 837b62f328bSVille Tervo { 838c916fbe4SGustavo F. Padovan struct sock *parent, *sk; 83923691d75SGustavo F. Padovan struct l2cap_chan *chan, *pchan; 840b62f328bSVille Tervo 841b62f328bSVille Tervo BT_DBG(""); 842b62f328bSVille Tervo 843b62f328bSVille Tervo /* Check if we have socket listening on cid */ 84423691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, 845b62f328bSVille Tervo conn->src); 84623691d75SGustavo F. Padovan if (!pchan) 847b62f328bSVille Tervo return; 848b62f328bSVille Tervo 84923691d75SGustavo F. Padovan parent = pchan->sk; 85023691d75SGustavo F. Padovan 851aa2ac881SGustavo F. Padovan lock_sock(parent); 85262f3a2cfSGustavo F. Padovan 853b62f328bSVille Tervo /* Check for backlog size */ 854b62f328bSVille Tervo if (sk_acceptq_is_full(parent)) { 855b62f328bSVille Tervo BT_DBG("backlog full %d", parent->sk_ack_backlog); 856b62f328bSVille Tervo goto clean; 857b62f328bSVille Tervo } 858b62f328bSVille Tervo 85980808e43SGustavo F. Padovan chan = pchan->ops->new_connection(pchan->data); 86080808e43SGustavo F. Padovan if (!chan) 861b62f328bSVille Tervo goto clean; 862b62f328bSVille Tervo 86380808e43SGustavo F. Padovan sk = chan->sk; 8645d41ce1dSGustavo F. Padovan 865b62f328bSVille Tervo hci_conn_hold(conn->hcon); 866b62f328bSVille Tervo 867b62f328bSVille Tervo bacpy(&bt_sk(sk)->src, conn->src); 868b62f328bSVille Tervo bacpy(&bt_sk(sk)->dst, conn->dst); 869b62f328bSVille Tervo 870d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 871d1010240SGustavo F. Padovan 8723d57dc68SGustavo F. Padovan l2cap_chan_add(conn, chan); 87348454079SGustavo F. Padovan 874c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 875b62f328bSVille Tervo 87689bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 877b62f328bSVille Tervo parent->sk_data_ready(parent, 0); 878b62f328bSVille Tervo 879b62f328bSVille Tervo clean: 880aa2ac881SGustavo F. Padovan release_sock(parent); 881b62f328bSVille Tervo } 882b62f328bSVille Tervo 883cf4cd009SAndrei Emeltchenko static void l2cap_chan_ready(struct l2cap_chan *chan) 884f1cb9af5SVinicius Costa Gomes { 885cf4cd009SAndrei Emeltchenko struct sock *sk = chan->sk; 886f1cb9af5SVinicius Costa Gomes struct sock *parent = bt_sk(sk)->parent; 887f1cb9af5SVinicius Costa Gomes 888f1cb9af5SVinicius Costa Gomes BT_DBG("sk %p, parent %p", sk, parent); 889f1cb9af5SVinicius Costa Gomes 890f1cb9af5SVinicius Costa Gomes chan->conf_state = 0; 891f1cb9af5SVinicius Costa Gomes __clear_chan_timer(chan); 892f1cb9af5SVinicius Costa Gomes 89343f3dc41SVinicius Costa Gomes l2cap_state_change(chan, BT_CONNECTED); 894f1cb9af5SVinicius Costa Gomes sk->sk_state_change(sk); 895f1cb9af5SVinicius Costa Gomes 896f1cb9af5SVinicius Costa Gomes if (parent) 897f1cb9af5SVinicius Costa Gomes parent->sk_data_ready(parent, 0); 898f1cb9af5SVinicius Costa Gomes } 899f1cb9af5SVinicius Costa Gomes 9000a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn) 9010a708f8fSGustavo F. Padovan { 90248454079SGustavo F. Padovan struct l2cap_chan *chan; 9030a708f8fSGustavo F. Padovan 9040a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 9050a708f8fSGustavo F. Padovan 906b62f328bSVille Tervo if (!conn->hcon->out && conn->hcon->type == LE_LINK) 907b62f328bSVille Tervo l2cap_le_conn_ready(conn); 908b62f328bSVille Tervo 909160dc6acSVinicius Costa Gomes if (conn->hcon->out && conn->hcon->type == LE_LINK) 910160dc6acSVinicius Costa Gomes smp_conn_security(conn, conn->hcon->pending_sec_level); 911160dc6acSVinicius Costa Gomes 9123df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 9130a708f8fSGustavo F. Padovan 9143df91ea2SAndrei Emeltchenko list_for_each_entry(chan, &conn->chan_l, list) { 91548454079SGustavo F. Padovan struct sock *sk = chan->sk; 916baa7e1faSGustavo F. Padovan 9170a708f8fSGustavo F. Padovan bh_lock_sock(sk); 9180a708f8fSGustavo F. Padovan 91963128451SVinicius Costa Gomes if (conn->hcon->type == LE_LINK) { 920b501d6a1SAnderson Briglia if (smp_conn_security(conn, chan->sec_level)) 921cf4cd009SAndrei Emeltchenko l2cap_chan_ready(chan); 922acd7d370SVille Tervo 92363128451SVinicius Costa Gomes } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 924c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 92589bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 9260a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 927b501d6a1SAnderson Briglia 92889bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT) 929fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 9300a708f8fSGustavo F. Padovan 9310a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9320a708f8fSGustavo F. Padovan } 9330a708f8fSGustavo F. Padovan 9343df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 9350a708f8fSGustavo F. Padovan } 9360a708f8fSGustavo F. Padovan 9370a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */ 9380a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) 9390a708f8fSGustavo F. Padovan { 94048454079SGustavo F. Padovan struct l2cap_chan *chan; 9410a708f8fSGustavo F. Padovan 9420a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 9430a708f8fSGustavo F. Padovan 9443df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 9450a708f8fSGustavo F. Padovan 9463df91ea2SAndrei Emeltchenko list_for_each_entry(chan, &conn->chan_l, list) { 94748454079SGustavo F. Padovan struct sock *sk = chan->sk; 948baa7e1faSGustavo F. Padovan 949ecf61bdbSAndrei Emeltchenko if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) 9500a708f8fSGustavo F. Padovan sk->sk_err = err; 9510a708f8fSGustavo F. Padovan } 9520a708f8fSGustavo F. Padovan 9533df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 9540a708f8fSGustavo F. Padovan } 9550a708f8fSGustavo F. Padovan 956f878fcadSGustavo F. Padovan static void l2cap_info_timeout(struct work_struct *work) 9570a708f8fSGustavo F. Padovan { 958f878fcadSGustavo F. Padovan struct l2cap_conn *conn = container_of(work, struct l2cap_conn, 959030013d8SGustavo F. Padovan info_timer.work); 9600a708f8fSGustavo F. Padovan 9610a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 9620a708f8fSGustavo F. Padovan conn->info_ident = 0; 9630a708f8fSGustavo F. Padovan 9640a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 9650a708f8fSGustavo F. Padovan } 9660a708f8fSGustavo F. Padovan 9675d3de7dfSVinicius Costa Gomes static void l2cap_conn_del(struct hci_conn *hcon, int err) 9685d3de7dfSVinicius Costa Gomes { 9695d3de7dfSVinicius Costa Gomes struct l2cap_conn *conn = hcon->l2cap_data; 9705d3de7dfSVinicius Costa Gomes struct l2cap_chan *chan, *l; 9715d3de7dfSVinicius Costa Gomes struct sock *sk; 9725d3de7dfSVinicius Costa Gomes 9735d3de7dfSVinicius Costa Gomes if (!conn) 9745d3de7dfSVinicius Costa Gomes return; 9755d3de7dfSVinicius Costa Gomes 9765d3de7dfSVinicius Costa Gomes BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); 9775d3de7dfSVinicius Costa Gomes 9785d3de7dfSVinicius Costa Gomes kfree_skb(conn->rx_skb); 9795d3de7dfSVinicius Costa Gomes 9803df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 9813df91ea2SAndrei Emeltchenko 9825d3de7dfSVinicius Costa Gomes /* Kill channels */ 9835d3de7dfSVinicius Costa Gomes list_for_each_entry_safe(chan, l, &conn->chan_l, list) { 9845d3de7dfSVinicius Costa Gomes sk = chan->sk; 985aa2ac881SGustavo F. Padovan lock_sock(sk); 9865d3de7dfSVinicius Costa Gomes l2cap_chan_del(chan, err); 987aa2ac881SGustavo F. Padovan release_sock(sk); 9885d3de7dfSVinicius Costa Gomes chan->ops->close(chan->data); 9895d3de7dfSVinicius Costa Gomes } 9905d3de7dfSVinicius Costa Gomes 9913df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 9923df91ea2SAndrei Emeltchenko 99373d80debSLuiz Augusto von Dentz hci_chan_del(conn->hchan); 99473d80debSLuiz Augusto von Dentz 9955d3de7dfSVinicius Costa Gomes if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) 996127074bfSUlisses Furquim cancel_delayed_work_sync(&conn->info_timer); 9975d3de7dfSVinicius Costa Gomes 99851a8efd7SJohan Hedberg if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) { 999127074bfSUlisses Furquim cancel_delayed_work_sync(&conn->security_timer); 10008aab4757SVinicius Costa Gomes smp_chan_destroy(conn); 1001d26a2345SVinicius Costa Gomes } 10025d3de7dfSVinicius Costa Gomes 10035d3de7dfSVinicius Costa Gomes hcon->l2cap_data = NULL; 10045d3de7dfSVinicius Costa Gomes kfree(conn); 10055d3de7dfSVinicius Costa Gomes } 10065d3de7dfSVinicius Costa Gomes 10076c9d42a1SGustavo F. Padovan static void security_timeout(struct work_struct *work) 10085d3de7dfSVinicius Costa Gomes { 10096c9d42a1SGustavo F. Padovan struct l2cap_conn *conn = container_of(work, struct l2cap_conn, 10106c9d42a1SGustavo F. Padovan security_timer.work); 10115d3de7dfSVinicius Costa Gomes 10125d3de7dfSVinicius Costa Gomes l2cap_conn_del(conn->hcon, ETIMEDOUT); 10135d3de7dfSVinicius Costa Gomes } 10145d3de7dfSVinicius Costa Gomes 10150a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) 10160a708f8fSGustavo F. Padovan { 10170a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 101873d80debSLuiz Augusto von Dentz struct hci_chan *hchan; 10190a708f8fSGustavo F. Padovan 10200a708f8fSGustavo F. Padovan if (conn || status) 10210a708f8fSGustavo F. Padovan return conn; 10220a708f8fSGustavo F. Padovan 102373d80debSLuiz Augusto von Dentz hchan = hci_chan_create(hcon); 102473d80debSLuiz Augusto von Dentz if (!hchan) 10250a708f8fSGustavo F. Padovan return NULL; 10260a708f8fSGustavo F. Padovan 102773d80debSLuiz Augusto von Dentz conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); 102873d80debSLuiz Augusto von Dentz if (!conn) { 102973d80debSLuiz Augusto von Dentz hci_chan_del(hchan); 103073d80debSLuiz Augusto von Dentz return NULL; 103173d80debSLuiz Augusto von Dentz } 103273d80debSLuiz Augusto von Dentz 10330a708f8fSGustavo F. Padovan hcon->l2cap_data = conn; 10340a708f8fSGustavo F. Padovan conn->hcon = hcon; 103573d80debSLuiz Augusto von Dentz conn->hchan = hchan; 10360a708f8fSGustavo F. Padovan 103773d80debSLuiz Augusto von Dentz BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); 10380a708f8fSGustavo F. Padovan 1039acd7d370SVille Tervo if (hcon->hdev->le_mtu && hcon->type == LE_LINK) 1040acd7d370SVille Tervo conn->mtu = hcon->hdev->le_mtu; 1041acd7d370SVille Tervo else 10420a708f8fSGustavo F. Padovan conn->mtu = hcon->hdev->acl_mtu; 1043acd7d370SVille Tervo 10440a708f8fSGustavo F. Padovan conn->src = &hcon->hdev->bdaddr; 10450a708f8fSGustavo F. Padovan conn->dst = &hcon->dst; 10460a708f8fSGustavo F. Padovan 10470a708f8fSGustavo F. Padovan conn->feat_mask = 0; 10480a708f8fSGustavo F. Padovan 10490a708f8fSGustavo F. Padovan spin_lock_init(&conn->lock); 10503df91ea2SAndrei Emeltchenko mutex_init(&conn->chan_lock); 1051baa7e1faSGustavo F. Padovan 1052baa7e1faSGustavo F. Padovan INIT_LIST_HEAD(&conn->chan_l); 10530a708f8fSGustavo F. Padovan 10545d3de7dfSVinicius Costa Gomes if (hcon->type == LE_LINK) 10556c9d42a1SGustavo F. Padovan INIT_DELAYED_WORK(&conn->security_timer, security_timeout); 10565d3de7dfSVinicius Costa Gomes else 1057030013d8SGustavo F. Padovan INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); 10580a708f8fSGustavo F. Padovan 10599f5a0d7bSAndrei Emeltchenko conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; 10600a708f8fSGustavo F. Padovan 10610a708f8fSGustavo F. Padovan return conn; 10620a708f8fSGustavo F. Padovan } 10630a708f8fSGustavo F. Padovan 10640a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */ 10650a708f8fSGustavo F. Padovan 10660a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr. 10670a708f8fSGustavo F. Padovan * Returns closest match. 10680a708f8fSGustavo F. Padovan */ 106923691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src) 10700a708f8fSGustavo F. Padovan { 107123691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 10720a708f8fSGustavo F. Padovan 107323691d75SGustavo F. Padovan read_lock(&chan_list_lock); 10740a708f8fSGustavo F. Padovan 107523691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 107623691d75SGustavo F. Padovan struct sock *sk = c->sk; 1077fe4128e0SGustavo F. Padovan 107889bc500eSGustavo F. Padovan if (state && c->state != state) 10790a708f8fSGustavo F. Padovan continue; 10800a708f8fSGustavo F. Padovan 108123691d75SGustavo F. Padovan if (c->psm == psm) { 10820a708f8fSGustavo F. Padovan /* Exact match. */ 108323691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 1084a7567b20SJohannes Berg read_unlock(&chan_list_lock); 108523691d75SGustavo F. Padovan return c; 108623691d75SGustavo F. Padovan } 10870a708f8fSGustavo F. Padovan 10880a708f8fSGustavo F. Padovan /* Closest match */ 10890a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 109023691d75SGustavo F. Padovan c1 = c; 10910a708f8fSGustavo F. Padovan } 10920a708f8fSGustavo F. Padovan } 10930a708f8fSGustavo F. Padovan 109423691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 10950a708f8fSGustavo F. Padovan 109623691d75SGustavo F. Padovan return c1; 10970a708f8fSGustavo F. Padovan } 10980a708f8fSGustavo F. Padovan 1099cbe8fed4SJohan Hedberg int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, bdaddr_t *dst) 11000a708f8fSGustavo F. Padovan { 11015d41ce1dSGustavo F. Padovan struct sock *sk = chan->sk; 11020a708f8fSGustavo F. Padovan bdaddr_t *src = &bt_sk(sk)->src; 11030a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 11040a708f8fSGustavo F. Padovan struct hci_conn *hcon; 11050a708f8fSGustavo F. Padovan struct hci_dev *hdev; 11060a708f8fSGustavo F. Padovan __u8 auth_type; 11070a708f8fSGustavo F. Padovan int err; 11080a708f8fSGustavo F. Padovan 11090a708f8fSGustavo F. Padovan BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), 1110fe4128e0SGustavo F. Padovan chan->psm); 11110a708f8fSGustavo F. Padovan 11120a708f8fSGustavo F. Padovan hdev = hci_get_route(dst, src); 11130a708f8fSGustavo F. Padovan if (!hdev) 11140a708f8fSGustavo F. Padovan return -EHOSTUNREACH; 11150a708f8fSGustavo F. Padovan 111609fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 11170a708f8fSGustavo F. Padovan 111803a00194SGustavo F. Padovan lock_sock(sk); 111903a00194SGustavo F. Padovan 112003a00194SGustavo F. Padovan /* PSM must be odd and lsb of upper byte must be 0 */ 112103a00194SGustavo F. Padovan if ((__le16_to_cpu(psm) & 0x0101) != 0x0001 && !cid && 112203a00194SGustavo F. Padovan chan->chan_type != L2CAP_CHAN_RAW) { 112303a00194SGustavo F. Padovan err = -EINVAL; 112403a00194SGustavo F. Padovan goto done; 112503a00194SGustavo F. Padovan } 112603a00194SGustavo F. Padovan 112703a00194SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !(psm || cid)) { 112803a00194SGustavo F. Padovan err = -EINVAL; 112903a00194SGustavo F. Padovan goto done; 113003a00194SGustavo F. Padovan } 113103a00194SGustavo F. Padovan 113203a00194SGustavo F. Padovan switch (chan->mode) { 113303a00194SGustavo F. Padovan case L2CAP_MODE_BASIC: 113403a00194SGustavo F. Padovan break; 113503a00194SGustavo F. Padovan case L2CAP_MODE_ERTM: 113603a00194SGustavo F. Padovan case L2CAP_MODE_STREAMING: 113703a00194SGustavo F. Padovan if (!disable_ertm) 113803a00194SGustavo F. Padovan break; 113903a00194SGustavo F. Padovan /* fall through */ 114003a00194SGustavo F. Padovan default: 114103a00194SGustavo F. Padovan err = -ENOTSUPP; 114203a00194SGustavo F. Padovan goto done; 114303a00194SGustavo F. Padovan } 114403a00194SGustavo F. Padovan 114503a00194SGustavo F. Padovan switch (sk->sk_state) { 114603a00194SGustavo F. Padovan case BT_CONNECT: 114703a00194SGustavo F. Padovan case BT_CONNECT2: 114803a00194SGustavo F. Padovan case BT_CONFIG: 114903a00194SGustavo F. Padovan /* Already connecting */ 115003a00194SGustavo F. Padovan err = 0; 115103a00194SGustavo F. Padovan goto done; 115203a00194SGustavo F. Padovan 115303a00194SGustavo F. Padovan case BT_CONNECTED: 115403a00194SGustavo F. Padovan /* Already connected */ 115503a00194SGustavo F. Padovan err = -EISCONN; 115603a00194SGustavo F. Padovan goto done; 115703a00194SGustavo F. Padovan 115803a00194SGustavo F. Padovan case BT_OPEN: 115903a00194SGustavo F. Padovan case BT_BOUND: 116003a00194SGustavo F. Padovan /* Can connect */ 116103a00194SGustavo F. Padovan break; 116203a00194SGustavo F. Padovan 116303a00194SGustavo F. Padovan default: 116403a00194SGustavo F. Padovan err = -EBADFD; 116503a00194SGustavo F. Padovan goto done; 116603a00194SGustavo F. Padovan } 116703a00194SGustavo F. Padovan 116803a00194SGustavo F. Padovan /* Set destination address and psm */ 11699219b2a0SGustavo F. Padovan bacpy(&bt_sk(sk)->dst, dst); 117003a00194SGustavo F. Padovan chan->psm = psm; 117103a00194SGustavo F. Padovan chan->dcid = cid; 11720a708f8fSGustavo F. Padovan 11734343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 11740a708f8fSGustavo F. Padovan 1175fe4128e0SGustavo F. Padovan if (chan->dcid == L2CAP_CID_LE_DATA) 1176acd7d370SVille Tervo hcon = hci_connect(hdev, LE_LINK, dst, 11774343478fSGustavo F. Padovan chan->sec_level, auth_type); 1178acd7d370SVille Tervo else 11790a708f8fSGustavo F. Padovan hcon = hci_connect(hdev, ACL_LINK, dst, 11804343478fSGustavo F. Padovan chan->sec_level, auth_type); 1181acd7d370SVille Tervo 118230e76272SVille Tervo if (IS_ERR(hcon)) { 118330e76272SVille Tervo err = PTR_ERR(hcon); 11840a708f8fSGustavo F. Padovan goto done; 118530e76272SVille Tervo } 11860a708f8fSGustavo F. Padovan 11870a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 11880a708f8fSGustavo F. Padovan if (!conn) { 11890a708f8fSGustavo F. Padovan hci_conn_put(hcon); 119030e76272SVille Tervo err = -ENOMEM; 11910a708f8fSGustavo F. Padovan goto done; 11920a708f8fSGustavo F. Padovan } 11930a708f8fSGustavo F. Padovan 11940a708f8fSGustavo F. Padovan /* Update source addr of the socket */ 11950a708f8fSGustavo F. Padovan bacpy(src, conn->src); 11960a708f8fSGustavo F. Padovan 119748454079SGustavo F. Padovan l2cap_chan_add(conn, chan); 119848454079SGustavo F. Padovan 119989bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECT); 1200c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 12010a708f8fSGustavo F. Padovan 12020a708f8fSGustavo F. Padovan if (hcon->state == BT_CONNECTED) { 1203715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 1204c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 1205d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan)) 120689bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 12070a708f8fSGustavo F. Padovan } else 1208fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 12090a708f8fSGustavo F. Padovan } 12100a708f8fSGustavo F. Padovan 121130e76272SVille Tervo err = 0; 121230e76272SVille Tervo 12130a708f8fSGustavo F. Padovan done: 121409fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 12150a708f8fSGustavo F. Padovan hci_dev_put(hdev); 12160a708f8fSGustavo F. Padovan return err; 12170a708f8fSGustavo F. Padovan } 12180a708f8fSGustavo F. Padovan 1219dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk) 12200a708f8fSGustavo F. Padovan { 12218c1d787bSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 12220a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 12230a708f8fSGustavo F. Padovan int err = 0; 12240a708f8fSGustavo F. Padovan int timeo = HZ/5; 12250a708f8fSGustavo F. Padovan 12260a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 12270a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 1228a71a0cf4SPeter Hurley while (chan->unacked_frames > 0 && chan->conn) { 12290a708f8fSGustavo F. Padovan if (!timeo) 12300a708f8fSGustavo F. Padovan timeo = HZ/5; 12310a708f8fSGustavo F. Padovan 12320a708f8fSGustavo F. Padovan if (signal_pending(current)) { 12330a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 12340a708f8fSGustavo F. Padovan break; 12350a708f8fSGustavo F. Padovan } 12360a708f8fSGustavo F. Padovan 12370a708f8fSGustavo F. Padovan release_sock(sk); 12380a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 12390a708f8fSGustavo F. Padovan lock_sock(sk); 1240a71a0cf4SPeter Hurley set_current_state(TASK_INTERRUPTIBLE); 12410a708f8fSGustavo F. Padovan 12420a708f8fSGustavo F. Padovan err = sock_error(sk); 12430a708f8fSGustavo F. Padovan if (err) 12440a708f8fSGustavo F. Padovan break; 12450a708f8fSGustavo F. Padovan } 12460a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 12470a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 12480a708f8fSGustavo F. Padovan return err; 12490a708f8fSGustavo F. Padovan } 12500a708f8fSGustavo F. Padovan 1251721c4181SGustavo F. Padovan static void l2cap_monitor_timeout(struct work_struct *work) 12520a708f8fSGustavo F. Padovan { 1253721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 1254721c4181SGustavo F. Padovan monitor_timer.work); 1255525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 12560a708f8fSGustavo F. Padovan 1257525cd185SGustavo F. Padovan BT_DBG("chan %p", chan); 12580a708f8fSGustavo F. Padovan 1259721c4181SGustavo F. Padovan lock_sock(sk); 12602c03a7a4SGustavo F. Padovan if (chan->retry_count >= chan->remote_max_tx) { 12618c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 1262721c4181SGustavo F. Padovan release_sock(sk); 12630a708f8fSGustavo F. Padovan return; 12640a708f8fSGustavo F. Padovan } 12650a708f8fSGustavo F. Padovan 12666a026610SGustavo F. Padovan chan->retry_count++; 12671a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 12680a708f8fSGustavo F. Padovan 1269525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 1270721c4181SGustavo F. Padovan release_sock(sk); 12710a708f8fSGustavo F. Padovan } 12720a708f8fSGustavo F. Padovan 1273721c4181SGustavo F. Padovan static void l2cap_retrans_timeout(struct work_struct *work) 12740a708f8fSGustavo F. Padovan { 1275721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 1276721c4181SGustavo F. Padovan retrans_timer.work); 1277525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 12780a708f8fSGustavo F. Padovan 127949208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 12800a708f8fSGustavo F. Padovan 1281721c4181SGustavo F. Padovan lock_sock(sk); 12826a026610SGustavo F. Padovan chan->retry_count = 1; 12831a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 12840a708f8fSGustavo F. Padovan 1285e2ab4353SGustavo F. Padovan set_bit(CONN_WAIT_F, &chan->conn_state); 12860a708f8fSGustavo F. Padovan 1287525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 1288721c4181SGustavo F. Padovan release_sock(sk); 12890a708f8fSGustavo F. Padovan } 12900a708f8fSGustavo F. Padovan 129142e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan) 12920a708f8fSGustavo F. Padovan { 12930a708f8fSGustavo F. Padovan struct sk_buff *skb; 12940a708f8fSGustavo F. Padovan 129558d35f87SGustavo F. Padovan while ((skb = skb_peek(&chan->tx_q)) && 12966a026610SGustavo F. Padovan chan->unacked_frames) { 129742e5c802SGustavo F. Padovan if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) 12980a708f8fSGustavo F. Padovan break; 12990a708f8fSGustavo F. Padovan 130058d35f87SGustavo F. Padovan skb = skb_dequeue(&chan->tx_q); 13010a708f8fSGustavo F. Padovan kfree_skb(skb); 13020a708f8fSGustavo F. Padovan 13036a026610SGustavo F. Padovan chan->unacked_frames--; 13040a708f8fSGustavo F. Padovan } 13050a708f8fSGustavo F. Padovan 13066a026610SGustavo F. Padovan if (!chan->unacked_frames) 13071a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 13080a708f8fSGustavo F. Padovan } 13090a708f8fSGustavo F. Padovan 131067c9e840SSzymon Janc static void l2cap_streaming_send(struct l2cap_chan *chan) 13110a708f8fSGustavo F. Padovan { 13120a708f8fSGustavo F. Padovan struct sk_buff *skb; 131388843ab0SAndrei Emeltchenko u32 control; 131488843ab0SAndrei Emeltchenko u16 fcs; 13150a708f8fSGustavo F. Padovan 131658d35f87SGustavo F. Padovan while ((skb = skb_dequeue(&chan->tx_q))) { 131788843ab0SAndrei Emeltchenko control = __get_control(chan, skb->data + L2CAP_HDR_SIZE); 1318fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, chan->next_tx_seq); 131988843ab0SAndrei Emeltchenko __put_control(chan, control, skb->data + L2CAP_HDR_SIZE); 13200a708f8fSGustavo F. Padovan 132147d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 132203a51213SAndrei Emeltchenko fcs = crc16(0, (u8 *)skb->data, 132303a51213SAndrei Emeltchenko skb->len - L2CAP_FCS_SIZE); 132403a51213SAndrei Emeltchenko put_unaligned_le16(fcs, 132503a51213SAndrei Emeltchenko skb->data + skb->len - L2CAP_FCS_SIZE); 13260a708f8fSGustavo F. Padovan } 13270a708f8fSGustavo F. Padovan 13284343478fSGustavo F. Padovan l2cap_do_send(chan, skb); 13290a708f8fSGustavo F. Padovan 1330836be934SAndrei Emeltchenko chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); 13310a708f8fSGustavo F. Padovan } 13320a708f8fSGustavo F. Padovan } 13330a708f8fSGustavo F. Padovan 1334fb45de7dSAndrei Emeltchenko static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u16 tx_seq) 13350a708f8fSGustavo F. Padovan { 13360a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 133788843ab0SAndrei Emeltchenko u16 fcs; 133888843ab0SAndrei Emeltchenko u32 control; 13390a708f8fSGustavo F. Padovan 134058d35f87SGustavo F. Padovan skb = skb_peek(&chan->tx_q); 13410a708f8fSGustavo F. Padovan if (!skb) 13420a708f8fSGustavo F. Padovan return; 13430a708f8fSGustavo F. Padovan 1344d1726b6dSSzymon Janc while (bt_cb(skb)->tx_seq != tx_seq) { 134558d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 13460a708f8fSGustavo F. Padovan return; 13470a708f8fSGustavo F. Padovan 1348d1726b6dSSzymon Janc skb = skb_queue_next(&chan->tx_q, skb); 1349d1726b6dSSzymon Janc } 13500a708f8fSGustavo F. Padovan 13512c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 13522c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 13538c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 13540a708f8fSGustavo F. Padovan return; 13550a708f8fSGustavo F. Padovan } 13560a708f8fSGustavo F. Padovan 13570a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 13580a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 135988843ab0SAndrei Emeltchenko 136088843ab0SAndrei Emeltchenko control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE); 13617e0ef6eeSAndrei Emeltchenko control &= __get_sar_mask(chan); 13620a708f8fSGustavo F. Padovan 1363e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 136403f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 13650a708f8fSGustavo F. Padovan 13660b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 1367fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, tx_seq); 13680a708f8fSGustavo F. Padovan 136988843ab0SAndrei Emeltchenko __put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE); 13700a708f8fSGustavo F. Padovan 137147d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 137203a51213SAndrei Emeltchenko fcs = crc16(0, (u8 *)tx_skb->data, 137303a51213SAndrei Emeltchenko tx_skb->len - L2CAP_FCS_SIZE); 137403a51213SAndrei Emeltchenko put_unaligned_le16(fcs, 137503a51213SAndrei Emeltchenko tx_skb->data + tx_skb->len - L2CAP_FCS_SIZE); 13760a708f8fSGustavo F. Padovan } 13770a708f8fSGustavo F. Padovan 13784343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 13790a708f8fSGustavo F. Padovan } 13800a708f8fSGustavo F. Padovan 138167c9e840SSzymon Janc static int l2cap_ertm_send(struct l2cap_chan *chan) 13820a708f8fSGustavo F. Padovan { 13830a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 138488843ab0SAndrei Emeltchenko u16 fcs; 138588843ab0SAndrei Emeltchenko u32 control; 13860a708f8fSGustavo F. Padovan int nsent = 0; 13870a708f8fSGustavo F. Padovan 138889bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 13890a708f8fSGustavo F. Padovan return -ENOTCONN; 13900a708f8fSGustavo F. Padovan 139158d35f87SGustavo F. Padovan while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) { 13920a708f8fSGustavo F. Padovan 13932c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 13942c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 13958c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 13960a708f8fSGustavo F. Padovan break; 13970a708f8fSGustavo F. Padovan } 13980a708f8fSGustavo F. Padovan 13990a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 14000a708f8fSGustavo F. Padovan 14010a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 14020a708f8fSGustavo F. Padovan 140388843ab0SAndrei Emeltchenko control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE); 14047e0ef6eeSAndrei Emeltchenko control &= __get_sar_mask(chan); 14050a708f8fSGustavo F. Padovan 1406e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 140703f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 1408e2ab4353SGustavo F. Padovan 14090b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 1410fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, chan->next_tx_seq); 14110a708f8fSGustavo F. Padovan 141288843ab0SAndrei Emeltchenko __put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE); 14130a708f8fSGustavo F. Padovan 141447d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 141503a51213SAndrei Emeltchenko fcs = crc16(0, (u8 *)skb->data, 141603a51213SAndrei Emeltchenko tx_skb->len - L2CAP_FCS_SIZE); 141703a51213SAndrei Emeltchenko put_unaligned_le16(fcs, skb->data + 141803a51213SAndrei Emeltchenko tx_skb->len - L2CAP_FCS_SIZE); 14190a708f8fSGustavo F. Padovan } 14200a708f8fSGustavo F. Padovan 14214343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 14220a708f8fSGustavo F. Padovan 14231a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 14240a708f8fSGustavo F. Padovan 142542e5c802SGustavo F. Padovan bt_cb(skb)->tx_seq = chan->next_tx_seq; 1426836be934SAndrei Emeltchenko 1427836be934SAndrei Emeltchenko chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); 14280a708f8fSGustavo F. Padovan 14298ed7a0aeSSzymon Janc if (bt_cb(skb)->retries == 1) { 14306a026610SGustavo F. Padovan chan->unacked_frames++; 1431930fa4aeSSzymon Janc 1432930fa4aeSSzymon Janc if (!nsent++) 1433930fa4aeSSzymon Janc __clear_ack_timer(chan); 14348ed7a0aeSSzymon Janc } 143523e9fde2SSuraj Sumangala 14366a026610SGustavo F. Padovan chan->frames_sent++; 14370a708f8fSGustavo F. Padovan 143858d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 143958d35f87SGustavo F. Padovan chan->tx_send_head = NULL; 14400a708f8fSGustavo F. Padovan else 144158d35f87SGustavo F. Padovan chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); 14420a708f8fSGustavo F. Padovan } 14430a708f8fSGustavo F. Padovan 14440a708f8fSGustavo F. Padovan return nsent; 14450a708f8fSGustavo F. Padovan } 14460a708f8fSGustavo F. Padovan 1447525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan) 14480a708f8fSGustavo F. Padovan { 14490a708f8fSGustavo F. Padovan int ret; 14500a708f8fSGustavo F. Padovan 145158d35f87SGustavo F. Padovan if (!skb_queue_empty(&chan->tx_q)) 145258d35f87SGustavo F. Padovan chan->tx_send_head = chan->tx_q.next; 14530a708f8fSGustavo F. Padovan 145442e5c802SGustavo F. Padovan chan->next_tx_seq = chan->expected_ack_seq; 1455525cd185SGustavo F. Padovan ret = l2cap_ertm_send(chan); 14560a708f8fSGustavo F. Padovan return ret; 14570a708f8fSGustavo F. Padovan } 14580a708f8fSGustavo F. Padovan 1459b17e73bbSSzymon Janc static void __l2cap_send_ack(struct l2cap_chan *chan) 14600a708f8fSGustavo F. Padovan { 146188843ab0SAndrei Emeltchenko u32 control = 0; 14620a708f8fSGustavo F. Padovan 14630b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 14640a708f8fSGustavo F. Padovan 1465e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 1466ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 1467e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 1468525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 14690a708f8fSGustavo F. Padovan return; 14700a708f8fSGustavo F. Padovan } 14710a708f8fSGustavo F. Padovan 1472525cd185SGustavo F. Padovan if (l2cap_ertm_send(chan) > 0) 14730a708f8fSGustavo F. Padovan return; 14740a708f8fSGustavo F. Padovan 1475ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 1476525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 14770a708f8fSGustavo F. Padovan } 14780a708f8fSGustavo F. Padovan 1479b17e73bbSSzymon Janc static void l2cap_send_ack(struct l2cap_chan *chan) 1480b17e73bbSSzymon Janc { 1481b17e73bbSSzymon Janc __clear_ack_timer(chan); 1482b17e73bbSSzymon Janc __l2cap_send_ack(chan); 1483b17e73bbSSzymon Janc } 1484b17e73bbSSzymon Janc 1485525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan) 14860a708f8fSGustavo F. Padovan { 14870a708f8fSGustavo F. Padovan struct srej_list *tail; 148888843ab0SAndrei Emeltchenko u32 control; 14890a708f8fSGustavo F. Padovan 1490ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 149103f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 14920a708f8fSGustavo F. Padovan 149339d5a3eeSGustavo F. Padovan tail = list_entry((&chan->srej_l)->prev, struct srej_list, list); 14940b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, tail->tx_seq); 14950a708f8fSGustavo F. Padovan 1496525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 14970a708f8fSGustavo F. Padovan } 14980a708f8fSGustavo F. Padovan 14990952a57aSAndrei Emeltchenko static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, struct msghdr *msg, int len, int count, struct sk_buff *skb) 15000a708f8fSGustavo F. Padovan { 15010952a57aSAndrei Emeltchenko struct l2cap_conn *conn = chan->conn; 15020a708f8fSGustavo F. Padovan struct sk_buff **frag; 15030a708f8fSGustavo F. Padovan int err, sent = 0; 15040a708f8fSGustavo F. Padovan 15050a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) 15060a708f8fSGustavo F. Padovan return -EFAULT; 15070a708f8fSGustavo F. Padovan 15080a708f8fSGustavo F. Padovan sent += count; 15090a708f8fSGustavo F. Padovan len -= count; 15100a708f8fSGustavo F. Padovan 15110a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 15120a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 15130a708f8fSGustavo F. Padovan while (len) { 15140a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 15150a708f8fSGustavo F. Padovan 15162f7719ceSAndrei Emeltchenko *frag = chan->ops->alloc_skb(chan, count, 15172f7719ceSAndrei Emeltchenko msg->msg_flags & MSG_DONTWAIT, &err); 15182f7719ceSAndrei Emeltchenko 15190a708f8fSGustavo F. Padovan if (!*frag) 15200a708f8fSGustavo F. Padovan return err; 15210a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) 15220a708f8fSGustavo F. Padovan return -EFAULT; 15230a708f8fSGustavo F. Padovan 15245e59b791SLuiz Augusto von Dentz (*frag)->priority = skb->priority; 15255e59b791SLuiz Augusto von Dentz 15260a708f8fSGustavo F. Padovan sent += count; 15270a708f8fSGustavo F. Padovan len -= count; 15280a708f8fSGustavo F. Padovan 15290a708f8fSGustavo F. Padovan frag = &(*frag)->next; 15300a708f8fSGustavo F. Padovan } 15310a708f8fSGustavo F. Padovan 15320a708f8fSGustavo F. Padovan return sent; 15330a708f8fSGustavo F. Padovan } 15340a708f8fSGustavo F. Padovan 15355e59b791SLuiz Augusto von Dentz static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, 15365e59b791SLuiz Augusto von Dentz struct msghdr *msg, size_t len, 15375e59b791SLuiz Augusto von Dentz u32 priority) 15380a708f8fSGustavo F. Padovan { 15398c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 15400a708f8fSGustavo F. Padovan struct sk_buff *skb; 154103a51213SAndrei Emeltchenko int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; 15420a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 15430a708f8fSGustavo F. Padovan 15446d5922b0SAndrei Emeltchenko BT_DBG("chan %p len %d priority %u", chan, (int)len, priority); 15450a708f8fSGustavo F. Padovan 15460a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 15472f7719ceSAndrei Emeltchenko 15482f7719ceSAndrei Emeltchenko skb = chan->ops->alloc_skb(chan, count + hlen, 15490a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 15502f7719ceSAndrei Emeltchenko 15510a708f8fSGustavo F. Padovan if (!skb) 15520a708f8fSGustavo F. Padovan return ERR_PTR(err); 15530a708f8fSGustavo F. Padovan 15545e59b791SLuiz Augusto von Dentz skb->priority = priority; 15555e59b791SLuiz Augusto von Dentz 15560a708f8fSGustavo F. Padovan /* Create L2CAP header */ 15570a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1558fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 15590a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 1560fe4128e0SGustavo F. Padovan put_unaligned_le16(chan->psm, skb_put(skb, 2)); 15610a708f8fSGustavo F. Padovan 15620952a57aSAndrei Emeltchenko err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); 15630a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 15640a708f8fSGustavo F. Padovan kfree_skb(skb); 15650a708f8fSGustavo F. Padovan return ERR_PTR(err); 15660a708f8fSGustavo F. Padovan } 15670a708f8fSGustavo F. Padovan return skb; 15680a708f8fSGustavo F. Padovan } 15690a708f8fSGustavo F. Padovan 15705e59b791SLuiz Augusto von Dentz static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, 15715e59b791SLuiz Augusto von Dentz struct msghdr *msg, size_t len, 15725e59b791SLuiz Augusto von Dentz u32 priority) 15730a708f8fSGustavo F. Padovan { 15748c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 15750a708f8fSGustavo F. Padovan struct sk_buff *skb; 15760a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE; 15770a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 15780a708f8fSGustavo F. Padovan 15796d5922b0SAndrei Emeltchenko BT_DBG("chan %p len %d", chan, (int)len); 15800a708f8fSGustavo F. Padovan 15810a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 15822f7719ceSAndrei Emeltchenko 15832f7719ceSAndrei Emeltchenko skb = chan->ops->alloc_skb(chan, count + hlen, 15840a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 15852f7719ceSAndrei Emeltchenko 15860a708f8fSGustavo F. Padovan if (!skb) 15870a708f8fSGustavo F. Padovan return ERR_PTR(err); 15880a708f8fSGustavo F. Padovan 15895e59b791SLuiz Augusto von Dentz skb->priority = priority; 15905e59b791SLuiz Augusto von Dentz 15910a708f8fSGustavo F. Padovan /* Create L2CAP header */ 15920a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1593fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 15940a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 15950a708f8fSGustavo F. Padovan 15960952a57aSAndrei Emeltchenko err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); 15970a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 15980a708f8fSGustavo F. Padovan kfree_skb(skb); 15990a708f8fSGustavo F. Padovan return ERR_PTR(err); 16000a708f8fSGustavo F. Padovan } 16010a708f8fSGustavo F. Padovan return skb; 16020a708f8fSGustavo F. Padovan } 16030a708f8fSGustavo F. Padovan 1604ab0ff76dSLuiz Augusto von Dentz static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, 1605ab0ff76dSLuiz Augusto von Dentz struct msghdr *msg, size_t len, 160688843ab0SAndrei Emeltchenko u32 control, u16 sdulen) 16070a708f8fSGustavo F. Padovan { 16088c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 16090a708f8fSGustavo F. Padovan struct sk_buff *skb; 1610e4ca6d98SAndrei Emeltchenko int err, count, hlen; 16110a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 16120a708f8fSGustavo F. Padovan 16136d5922b0SAndrei Emeltchenko BT_DBG("chan %p len %d", chan, (int)len); 16140a708f8fSGustavo F. Padovan 16150a708f8fSGustavo F. Padovan if (!conn) 16160a708f8fSGustavo F. Padovan return ERR_PTR(-ENOTCONN); 16170a708f8fSGustavo F. Padovan 1618e4ca6d98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 1619e4ca6d98SAndrei Emeltchenko hlen = L2CAP_EXT_HDR_SIZE; 1620e4ca6d98SAndrei Emeltchenko else 1621e4ca6d98SAndrei Emeltchenko hlen = L2CAP_ENH_HDR_SIZE; 1622e4ca6d98SAndrei Emeltchenko 16230a708f8fSGustavo F. Padovan if (sdulen) 162403a51213SAndrei Emeltchenko hlen += L2CAP_SDULEN_SIZE; 16250a708f8fSGustavo F. Padovan 162647d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 162703a51213SAndrei Emeltchenko hlen += L2CAP_FCS_SIZE; 16280a708f8fSGustavo F. Padovan 16290a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 16302f7719ceSAndrei Emeltchenko 16312f7719ceSAndrei Emeltchenko skb = chan->ops->alloc_skb(chan, count + hlen, 16320a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 16332f7719ceSAndrei Emeltchenko 16340a708f8fSGustavo F. Padovan if (!skb) 16350a708f8fSGustavo F. Padovan return ERR_PTR(err); 16360a708f8fSGustavo F. Padovan 16370a708f8fSGustavo F. Padovan /* Create L2CAP header */ 16380a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1639fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 16400a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 164188843ab0SAndrei Emeltchenko 164288843ab0SAndrei Emeltchenko __put_control(chan, control, skb_put(skb, __ctrl_size(chan))); 164388843ab0SAndrei Emeltchenko 16440a708f8fSGustavo F. Padovan if (sdulen) 164503a51213SAndrei Emeltchenko put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); 16460a708f8fSGustavo F. Padovan 16470952a57aSAndrei Emeltchenko err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); 16480a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 16490a708f8fSGustavo F. Padovan kfree_skb(skb); 16500a708f8fSGustavo F. Padovan return ERR_PTR(err); 16510a708f8fSGustavo F. Padovan } 16520a708f8fSGustavo F. Padovan 165347d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 165403a51213SAndrei Emeltchenko put_unaligned_le16(0, skb_put(skb, L2CAP_FCS_SIZE)); 16550a708f8fSGustavo F. Padovan 16560a708f8fSGustavo F. Padovan bt_cb(skb)->retries = 0; 16570a708f8fSGustavo F. Padovan return skb; 16580a708f8fSGustavo F. Padovan } 16590a708f8fSGustavo F. Padovan 166067c9e840SSzymon Janc static int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 16610a708f8fSGustavo F. Padovan { 16620a708f8fSGustavo F. Padovan struct sk_buff *skb; 16630a708f8fSGustavo F. Padovan struct sk_buff_head sar_queue; 166488843ab0SAndrei Emeltchenko u32 control; 16650a708f8fSGustavo F. Padovan size_t size = 0; 16660a708f8fSGustavo F. Padovan 16670a708f8fSGustavo F. Padovan skb_queue_head_init(&sar_queue); 16687e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_START); 166947d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len); 16700a708f8fSGustavo F. Padovan if (IS_ERR(skb)) 16710a708f8fSGustavo F. Padovan return PTR_ERR(skb); 16720a708f8fSGustavo F. Padovan 16730a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 16742c03a7a4SGustavo F. Padovan len -= chan->remote_mps; 16752c03a7a4SGustavo F. Padovan size += chan->remote_mps; 16760a708f8fSGustavo F. Padovan 16770a708f8fSGustavo F. Padovan while (len > 0) { 16780a708f8fSGustavo F. Padovan size_t buflen; 16790a708f8fSGustavo F. Padovan 16802c03a7a4SGustavo F. Padovan if (len > chan->remote_mps) { 16817e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_CONTINUE); 16822c03a7a4SGustavo F. Padovan buflen = chan->remote_mps; 16830a708f8fSGustavo F. Padovan } else { 16847e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_END); 16850a708f8fSGustavo F. Padovan buflen = len; 16860a708f8fSGustavo F. Padovan } 16870a708f8fSGustavo F. Padovan 168847d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0); 16890a708f8fSGustavo F. Padovan if (IS_ERR(skb)) { 16900a708f8fSGustavo F. Padovan skb_queue_purge(&sar_queue); 16910a708f8fSGustavo F. Padovan return PTR_ERR(skb); 16920a708f8fSGustavo F. Padovan } 16930a708f8fSGustavo F. Padovan 16940a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 16950a708f8fSGustavo F. Padovan len -= buflen; 16960a708f8fSGustavo F. Padovan size += buflen; 16970a708f8fSGustavo F. Padovan } 169858d35f87SGustavo F. Padovan skb_queue_splice_tail(&sar_queue, &chan->tx_q); 169958d35f87SGustavo F. Padovan if (chan->tx_send_head == NULL) 170058d35f87SGustavo F. Padovan chan->tx_send_head = sar_queue.next; 17010a708f8fSGustavo F. Padovan 17020a708f8fSGustavo F. Padovan return size; 17030a708f8fSGustavo F. Padovan } 17040a708f8fSGustavo F. Padovan 17055e59b791SLuiz Augusto von Dentz int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, 17065e59b791SLuiz Augusto von Dentz u32 priority) 17079a91a04aSGustavo F. Padovan { 17089a91a04aSGustavo F. Padovan struct sk_buff *skb; 170988843ab0SAndrei Emeltchenko u32 control; 17109a91a04aSGustavo F. Padovan int err; 17119a91a04aSGustavo F. Padovan 17129a91a04aSGustavo F. Padovan /* Connectionless channel */ 1713715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { 17145e59b791SLuiz Augusto von Dentz skb = l2cap_create_connless_pdu(chan, msg, len, priority); 17159a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 17169a91a04aSGustavo F. Padovan return PTR_ERR(skb); 17179a91a04aSGustavo F. Padovan 17189a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 17199a91a04aSGustavo F. Padovan return len; 17209a91a04aSGustavo F. Padovan } 17219a91a04aSGustavo F. Padovan 17229a91a04aSGustavo F. Padovan switch (chan->mode) { 17239a91a04aSGustavo F. Padovan case L2CAP_MODE_BASIC: 17249a91a04aSGustavo F. Padovan /* Check outgoing MTU */ 17259a91a04aSGustavo F. Padovan if (len > chan->omtu) 17269a91a04aSGustavo F. Padovan return -EMSGSIZE; 17279a91a04aSGustavo F. Padovan 17289a91a04aSGustavo F. Padovan /* Create a basic PDU */ 17295e59b791SLuiz Augusto von Dentz skb = l2cap_create_basic_pdu(chan, msg, len, priority); 17309a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 17319a91a04aSGustavo F. Padovan return PTR_ERR(skb); 17329a91a04aSGustavo F. Padovan 17339a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 17349a91a04aSGustavo F. Padovan err = len; 17359a91a04aSGustavo F. Padovan break; 17369a91a04aSGustavo F. Padovan 17379a91a04aSGustavo F. Padovan case L2CAP_MODE_ERTM: 17389a91a04aSGustavo F. Padovan case L2CAP_MODE_STREAMING: 17399a91a04aSGustavo F. Padovan /* Entire SDU fits into one PDU */ 17409a91a04aSGustavo F. Padovan if (len <= chan->remote_mps) { 17417e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_UNSEGMENTED); 17429a91a04aSGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, len, control, 17439a91a04aSGustavo F. Padovan 0); 17449a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 17459a91a04aSGustavo F. Padovan return PTR_ERR(skb); 17469a91a04aSGustavo F. Padovan 17479a91a04aSGustavo F. Padovan __skb_queue_tail(&chan->tx_q, skb); 17489a91a04aSGustavo F. Padovan 17499a91a04aSGustavo F. Padovan if (chan->tx_send_head == NULL) 17509a91a04aSGustavo F. Padovan chan->tx_send_head = skb; 17519a91a04aSGustavo F. Padovan 17529a91a04aSGustavo F. Padovan } else { 17539a91a04aSGustavo F. Padovan /* Segment SDU into multiples PDUs */ 17549a91a04aSGustavo F. Padovan err = l2cap_sar_segment_sdu(chan, msg, len); 17559a91a04aSGustavo F. Padovan if (err < 0) 17569a91a04aSGustavo F. Padovan return err; 17579a91a04aSGustavo F. Padovan } 17589a91a04aSGustavo F. Padovan 17599a91a04aSGustavo F. Padovan if (chan->mode == L2CAP_MODE_STREAMING) { 17609a91a04aSGustavo F. Padovan l2cap_streaming_send(chan); 17619a91a04aSGustavo F. Padovan err = len; 17629a91a04aSGustavo F. Padovan break; 17639a91a04aSGustavo F. Padovan } 17649a91a04aSGustavo F. Padovan 1765e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 1766e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 17679a91a04aSGustavo F. Padovan err = len; 17689a91a04aSGustavo F. Padovan break; 17699a91a04aSGustavo F. Padovan } 17709a91a04aSGustavo F. Padovan 17719a91a04aSGustavo F. Padovan err = l2cap_ertm_send(chan); 17729a91a04aSGustavo F. Padovan if (err >= 0) 17739a91a04aSGustavo F. Padovan err = len; 17749a91a04aSGustavo F. Padovan 17759a91a04aSGustavo F. Padovan break; 17769a91a04aSGustavo F. Padovan 17779a91a04aSGustavo F. Padovan default: 17789a91a04aSGustavo F. Padovan BT_DBG("bad state %1.1x", chan->mode); 17799a91a04aSGustavo F. Padovan err = -EBADFD; 17809a91a04aSGustavo F. Padovan } 17819a91a04aSGustavo F. Padovan 17829a91a04aSGustavo F. Padovan return err; 17839a91a04aSGustavo F. Padovan } 17849a91a04aSGustavo F. Padovan 17850a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */ 17860a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) 17870a708f8fSGustavo F. Padovan { 17880a708f8fSGustavo F. Padovan struct sk_buff *nskb; 178948454079SGustavo F. Padovan struct l2cap_chan *chan; 17900a708f8fSGustavo F. Padovan 17910a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 17920a708f8fSGustavo F. Padovan 17933df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 17943d57dc68SGustavo F. Padovan 17953df91ea2SAndrei Emeltchenko list_for_each_entry(chan, &conn->chan_l, list) { 179648454079SGustavo F. Padovan struct sock *sk = chan->sk; 1797715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_RAW) 17980a708f8fSGustavo F. Padovan continue; 17990a708f8fSGustavo F. Padovan 18000a708f8fSGustavo F. Padovan /* Don't send frame to the socket it came from */ 18010a708f8fSGustavo F. Padovan if (skb->sk == sk) 18020a708f8fSGustavo F. Padovan continue; 18030a708f8fSGustavo F. Padovan nskb = skb_clone(skb, GFP_ATOMIC); 18040a708f8fSGustavo F. Padovan if (!nskb) 18050a708f8fSGustavo F. Padovan continue; 18060a708f8fSGustavo F. Padovan 180723070494SGustavo F. Padovan if (chan->ops->recv(chan->data, nskb)) 18080a708f8fSGustavo F. Padovan kfree_skb(nskb); 18090a708f8fSGustavo F. Padovan } 18103d57dc68SGustavo F. Padovan 18113df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 18120a708f8fSGustavo F. Padovan } 18130a708f8fSGustavo F. Padovan 18140a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */ 18150a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 18160a708f8fSGustavo F. Padovan u8 code, u8 ident, u16 dlen, void *data) 18170a708f8fSGustavo F. Padovan { 18180a708f8fSGustavo F. Padovan struct sk_buff *skb, **frag; 18190a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr *cmd; 18200a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 18210a708f8fSGustavo F. Padovan int len, count; 18220a708f8fSGustavo F. Padovan 18230a708f8fSGustavo F. Padovan BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", 18240a708f8fSGustavo F. Padovan conn, code, ident, dlen); 18250a708f8fSGustavo F. Padovan 18260a708f8fSGustavo F. Padovan len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; 18270a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 18280a708f8fSGustavo F. Padovan 18290a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 18300a708f8fSGustavo F. Padovan if (!skb) 18310a708f8fSGustavo F. Padovan return NULL; 18320a708f8fSGustavo F. Padovan 18330a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 18340a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); 18353300d9a9SClaudio Takahasi 18363300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 18373300d9a9SClaudio Takahasi lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); 18383300d9a9SClaudio Takahasi else 18390a708f8fSGustavo F. Padovan lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); 18400a708f8fSGustavo F. Padovan 18410a708f8fSGustavo F. Padovan cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); 18420a708f8fSGustavo F. Padovan cmd->code = code; 18430a708f8fSGustavo F. Padovan cmd->ident = ident; 18440a708f8fSGustavo F. Padovan cmd->len = cpu_to_le16(dlen); 18450a708f8fSGustavo F. Padovan 18460a708f8fSGustavo F. Padovan if (dlen) { 18470a708f8fSGustavo F. Padovan count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; 18480a708f8fSGustavo F. Padovan memcpy(skb_put(skb, count), data, count); 18490a708f8fSGustavo F. Padovan data += count; 18500a708f8fSGustavo F. Padovan } 18510a708f8fSGustavo F. Padovan 18520a708f8fSGustavo F. Padovan len -= skb->len; 18530a708f8fSGustavo F. Padovan 18540a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 18550a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 18560a708f8fSGustavo F. Padovan while (len) { 18570a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 18580a708f8fSGustavo F. Padovan 18590a708f8fSGustavo F. Padovan *frag = bt_skb_alloc(count, GFP_ATOMIC); 18600a708f8fSGustavo F. Padovan if (!*frag) 18610a708f8fSGustavo F. Padovan goto fail; 18620a708f8fSGustavo F. Padovan 18630a708f8fSGustavo F. Padovan memcpy(skb_put(*frag, count), data, count); 18640a708f8fSGustavo F. Padovan 18650a708f8fSGustavo F. Padovan len -= count; 18660a708f8fSGustavo F. Padovan data += count; 18670a708f8fSGustavo F. Padovan 18680a708f8fSGustavo F. Padovan frag = &(*frag)->next; 18690a708f8fSGustavo F. Padovan } 18700a708f8fSGustavo F. Padovan 18710a708f8fSGustavo F. Padovan return skb; 18720a708f8fSGustavo F. Padovan 18730a708f8fSGustavo F. Padovan fail: 18740a708f8fSGustavo F. Padovan kfree_skb(skb); 18750a708f8fSGustavo F. Padovan return NULL; 18760a708f8fSGustavo F. Padovan } 18770a708f8fSGustavo F. Padovan 18780a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) 18790a708f8fSGustavo F. Padovan { 18800a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 18810a708f8fSGustavo F. Padovan int len; 18820a708f8fSGustavo F. Padovan 18830a708f8fSGustavo F. Padovan len = L2CAP_CONF_OPT_SIZE + opt->len; 18840a708f8fSGustavo F. Padovan *ptr += len; 18850a708f8fSGustavo F. Padovan 18860a708f8fSGustavo F. Padovan *type = opt->type; 18870a708f8fSGustavo F. Padovan *olen = opt->len; 18880a708f8fSGustavo F. Padovan 18890a708f8fSGustavo F. Padovan switch (opt->len) { 18900a708f8fSGustavo F. Padovan case 1: 18910a708f8fSGustavo F. Padovan *val = *((u8 *) opt->val); 18920a708f8fSGustavo F. Padovan break; 18930a708f8fSGustavo F. Padovan 18940a708f8fSGustavo F. Padovan case 2: 18950a708f8fSGustavo F. Padovan *val = get_unaligned_le16(opt->val); 18960a708f8fSGustavo F. Padovan break; 18970a708f8fSGustavo F. Padovan 18980a708f8fSGustavo F. Padovan case 4: 18990a708f8fSGustavo F. Padovan *val = get_unaligned_le32(opt->val); 19000a708f8fSGustavo F. Padovan break; 19010a708f8fSGustavo F. Padovan 19020a708f8fSGustavo F. Padovan default: 19030a708f8fSGustavo F. Padovan *val = (unsigned long) opt->val; 19040a708f8fSGustavo F. Padovan break; 19050a708f8fSGustavo F. Padovan } 19060a708f8fSGustavo F. Padovan 19070a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); 19080a708f8fSGustavo F. Padovan return len; 19090a708f8fSGustavo F. Padovan } 19100a708f8fSGustavo F. Padovan 19110a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) 19120a708f8fSGustavo F. Padovan { 19130a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 19140a708f8fSGustavo F. Padovan 19150a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); 19160a708f8fSGustavo F. Padovan 19170a708f8fSGustavo F. Padovan opt->type = type; 19180a708f8fSGustavo F. Padovan opt->len = len; 19190a708f8fSGustavo F. Padovan 19200a708f8fSGustavo F. Padovan switch (len) { 19210a708f8fSGustavo F. Padovan case 1: 19220a708f8fSGustavo F. Padovan *((u8 *) opt->val) = val; 19230a708f8fSGustavo F. Padovan break; 19240a708f8fSGustavo F. Padovan 19250a708f8fSGustavo F. Padovan case 2: 19260a708f8fSGustavo F. Padovan put_unaligned_le16(val, opt->val); 19270a708f8fSGustavo F. Padovan break; 19280a708f8fSGustavo F. Padovan 19290a708f8fSGustavo F. Padovan case 4: 19300a708f8fSGustavo F. Padovan put_unaligned_le32(val, opt->val); 19310a708f8fSGustavo F. Padovan break; 19320a708f8fSGustavo F. Padovan 19330a708f8fSGustavo F. Padovan default: 19340a708f8fSGustavo F. Padovan memcpy(opt->val, (void *) val, len); 19350a708f8fSGustavo F. Padovan break; 19360a708f8fSGustavo F. Padovan } 19370a708f8fSGustavo F. Padovan 19380a708f8fSGustavo F. Padovan *ptr += L2CAP_CONF_OPT_SIZE + len; 19390a708f8fSGustavo F. Padovan } 19400a708f8fSGustavo F. Padovan 1941f89cef09SAndrei Emeltchenko static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) 1942f89cef09SAndrei Emeltchenko { 1943f89cef09SAndrei Emeltchenko struct l2cap_conf_efs efs; 1944f89cef09SAndrei Emeltchenko 1945f89cef09SAndrei Emeltchenko switch (chan->mode) { 1946f89cef09SAndrei Emeltchenko case L2CAP_MODE_ERTM: 1947f89cef09SAndrei Emeltchenko efs.id = chan->local_id; 1948f89cef09SAndrei Emeltchenko efs.stype = chan->local_stype; 1949f89cef09SAndrei Emeltchenko efs.msdu = cpu_to_le16(chan->local_msdu); 1950f89cef09SAndrei Emeltchenko efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); 1951f89cef09SAndrei Emeltchenko efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); 1952f89cef09SAndrei Emeltchenko efs.flush_to = cpu_to_le32(L2CAP_DEFAULT_FLUSH_TO); 1953f89cef09SAndrei Emeltchenko break; 1954f89cef09SAndrei Emeltchenko 1955f89cef09SAndrei Emeltchenko case L2CAP_MODE_STREAMING: 1956f89cef09SAndrei Emeltchenko efs.id = 1; 1957f89cef09SAndrei Emeltchenko efs.stype = L2CAP_SERV_BESTEFFORT; 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 = 0; 1961f89cef09SAndrei Emeltchenko efs.flush_to = 0; 1962f89cef09SAndrei Emeltchenko break; 1963f89cef09SAndrei Emeltchenko 1964f89cef09SAndrei Emeltchenko default: 1965f89cef09SAndrei Emeltchenko return; 1966f89cef09SAndrei Emeltchenko } 1967f89cef09SAndrei Emeltchenko 1968f89cef09SAndrei Emeltchenko l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), 1969f89cef09SAndrei Emeltchenko (unsigned long) &efs); 1970f89cef09SAndrei Emeltchenko } 1971f89cef09SAndrei Emeltchenko 1972721c4181SGustavo F. Padovan static void l2cap_ack_timeout(struct work_struct *work) 19730a708f8fSGustavo F. Padovan { 1974721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 1975721c4181SGustavo F. Padovan ack_timer.work); 19760a708f8fSGustavo F. Padovan 19772fb9b3d4SGustavo F. Padovan BT_DBG("chan %p", chan); 19782fb9b3d4SGustavo F. Padovan 1979721c4181SGustavo F. Padovan lock_sock(chan->sk); 1980b17e73bbSSzymon Janc __l2cap_send_ack(chan); 1981721c4181SGustavo F. Padovan release_sock(chan->sk); 198209bfb2eeSSzymon Janc 198309bfb2eeSSzymon Janc l2cap_chan_put(chan); 19840a708f8fSGustavo F. Padovan } 19850a708f8fSGustavo F. Padovan 1986525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan) 19870a708f8fSGustavo F. Padovan { 198842e5c802SGustavo F. Padovan chan->expected_ack_seq = 0; 19896a026610SGustavo F. Padovan chan->unacked_frames = 0; 199042e5c802SGustavo F. Padovan chan->buffer_seq = 0; 19916a026610SGustavo F. Padovan chan->num_acked = 0; 19926a026610SGustavo F. Padovan chan->frames_sent = 0; 19930a708f8fSGustavo F. Padovan 1994721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); 1995721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); 1996721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); 19970a708f8fSGustavo F. Padovan 1998f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->srej_q); 19990a708f8fSGustavo F. Padovan 200039d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 20010a708f8fSGustavo F. Padovan } 20020a708f8fSGustavo F. Padovan 20030a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) 20040a708f8fSGustavo F. Padovan { 20050a708f8fSGustavo F. Padovan switch (mode) { 20060a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 20070a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 20080a708f8fSGustavo F. Padovan if (l2cap_mode_supported(mode, remote_feat_mask)) 20090a708f8fSGustavo F. Padovan return mode; 20100a708f8fSGustavo F. Padovan /* fall through */ 20110a708f8fSGustavo F. Padovan default: 20120a708f8fSGustavo F. Padovan return L2CAP_MODE_BASIC; 20130a708f8fSGustavo F. Padovan } 20140a708f8fSGustavo F. Padovan } 20150a708f8fSGustavo F. Padovan 20166327eb98SAndrei Emeltchenko static inline bool __l2cap_ews_supported(struct l2cap_chan *chan) 20176327eb98SAndrei Emeltchenko { 20186327eb98SAndrei Emeltchenko return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; 20196327eb98SAndrei Emeltchenko } 20206327eb98SAndrei Emeltchenko 2021f89cef09SAndrei Emeltchenko static inline bool __l2cap_efs_supported(struct l2cap_chan *chan) 2022f89cef09SAndrei Emeltchenko { 2023f89cef09SAndrei Emeltchenko return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW; 2024f89cef09SAndrei Emeltchenko } 2025f89cef09SAndrei Emeltchenko 20266327eb98SAndrei Emeltchenko static inline void l2cap_txwin_setup(struct l2cap_chan *chan) 20276327eb98SAndrei Emeltchenko { 20286327eb98SAndrei Emeltchenko if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && 2029836be934SAndrei Emeltchenko __l2cap_ews_supported(chan)) { 20306327eb98SAndrei Emeltchenko /* use extended control field */ 20316327eb98SAndrei Emeltchenko set_bit(FLAG_EXT_CTRL, &chan->flags); 2032836be934SAndrei Emeltchenko chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; 2033836be934SAndrei Emeltchenko } else { 20346327eb98SAndrei Emeltchenko chan->tx_win = min_t(u16, chan->tx_win, 20356327eb98SAndrei Emeltchenko L2CAP_DEFAULT_TX_WINDOW); 2036836be934SAndrei Emeltchenko chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; 2037836be934SAndrei Emeltchenko } 20386327eb98SAndrei Emeltchenko } 20396327eb98SAndrei Emeltchenko 2040710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) 20410a708f8fSGustavo F. Padovan { 20420a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 20430c1bc5c6SGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = chan->mode }; 20440a708f8fSGustavo F. Padovan void *ptr = req->data; 2045c8f79162SAndrei Emeltchenko u16 size; 20460a708f8fSGustavo F. Padovan 204749208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 20480a708f8fSGustavo F. Padovan 204973ffa904SGustavo F. Padovan if (chan->num_conf_req || chan->num_conf_rsp) 20500a708f8fSGustavo F. Padovan goto done; 20510a708f8fSGustavo F. Padovan 20520c1bc5c6SGustavo F. Padovan switch (chan->mode) { 20530a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 20540a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 2055c1360a1cSGustavo F. Padovan if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) 20560a708f8fSGustavo F. Padovan break; 20570a708f8fSGustavo F. Padovan 2058f89cef09SAndrei Emeltchenko if (__l2cap_efs_supported(chan)) 2059f89cef09SAndrei Emeltchenko set_bit(FLAG_EFS_ENABLE, &chan->flags); 2060f89cef09SAndrei Emeltchenko 20610a708f8fSGustavo F. Padovan /* fall through */ 20620a708f8fSGustavo F. Padovan default: 20638c1d787bSGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); 20640a708f8fSGustavo F. Padovan break; 20650a708f8fSGustavo F. Padovan } 20660a708f8fSGustavo F. Padovan 20670a708f8fSGustavo F. Padovan done: 20680c1bc5c6SGustavo F. Padovan if (chan->imtu != L2CAP_DEFAULT_MTU) 20690c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 20700a708f8fSGustavo F. Padovan 20710c1bc5c6SGustavo F. Padovan switch (chan->mode) { 20720a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 20738c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && 20748c1d787bSGustavo F. Padovan !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) 20750a708f8fSGustavo F. Padovan break; 20760a708f8fSGustavo F. Padovan 20770a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_BASIC; 20780a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 20790a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 20800a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 20810a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 20820a708f8fSGustavo F. Padovan rfc.max_pdu_size = 0; 20830a708f8fSGustavo F. Padovan 20840a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 20850a708f8fSGustavo F. Padovan (unsigned long) &rfc); 20860a708f8fSGustavo F. Padovan break; 20870a708f8fSGustavo F. Padovan 20880a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 20890a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_ERTM; 209047d1ec61SGustavo F. Padovan rfc.max_transmit = chan->max_tx; 20910a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 20920a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 2093c8f79162SAndrei Emeltchenko 2094c8f79162SAndrei Emeltchenko size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - 2095c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2096c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2097c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2098c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 20990a708f8fSGustavo F. Padovan 21006327eb98SAndrei Emeltchenko l2cap_txwin_setup(chan); 21016327eb98SAndrei Emeltchenko 21026327eb98SAndrei Emeltchenko rfc.txwin_size = min_t(u16, chan->tx_win, 21036327eb98SAndrei Emeltchenko L2CAP_DEFAULT_TX_WINDOW); 21040a708f8fSGustavo F. Padovan 21050a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 21060a708f8fSGustavo F. Padovan (unsigned long) &rfc); 21070a708f8fSGustavo F. Padovan 2108f89cef09SAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) 2109f89cef09SAndrei Emeltchenko l2cap_add_opt_efs(&ptr, chan); 2110f89cef09SAndrei Emeltchenko 21118c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 21120a708f8fSGustavo F. Padovan break; 21130a708f8fSGustavo F. Padovan 211447d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 2115c1360a1cSGustavo F. Padovan test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { 211647d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 211747d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 21180a708f8fSGustavo F. Padovan } 21196327eb98SAndrei Emeltchenko 21206327eb98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 21216327eb98SAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, 21226327eb98SAndrei Emeltchenko chan->tx_win); 21230a708f8fSGustavo F. Padovan break; 21240a708f8fSGustavo F. Padovan 21250a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 21260a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_STREAMING; 21270a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 21280a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 21290a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 21300a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 2131c8f79162SAndrei Emeltchenko 2132c8f79162SAndrei Emeltchenko size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - 2133c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2134c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2135c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2136c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 21370a708f8fSGustavo F. Padovan 21380a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 21390a708f8fSGustavo F. Padovan (unsigned long) &rfc); 21400a708f8fSGustavo F. Padovan 2141f89cef09SAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) 2142f89cef09SAndrei Emeltchenko l2cap_add_opt_efs(&ptr, chan); 2143f89cef09SAndrei Emeltchenko 21448c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 21450a708f8fSGustavo F. Padovan break; 21460a708f8fSGustavo F. Padovan 214747d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 2148c1360a1cSGustavo F. Padovan test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { 214947d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 215047d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 21510a708f8fSGustavo F. Padovan } 21520a708f8fSGustavo F. Padovan break; 21530a708f8fSGustavo F. Padovan } 21540a708f8fSGustavo F. Padovan 2155fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 21560a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0); 21570a708f8fSGustavo F. Padovan 21580a708f8fSGustavo F. Padovan return ptr - data; 21590a708f8fSGustavo F. Padovan } 21600a708f8fSGustavo F. Padovan 216173ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) 21620a708f8fSGustavo F. Padovan { 21630a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 21640a708f8fSGustavo F. Padovan void *ptr = rsp->data; 216573ffa904SGustavo F. Padovan void *req = chan->conf_req; 216673ffa904SGustavo F. Padovan int len = chan->conf_len; 21670a708f8fSGustavo F. Padovan int type, hint, olen; 21680a708f8fSGustavo F. Padovan unsigned long val; 21690a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 217042dceae2SAndrei Emeltchenko struct l2cap_conf_efs efs; 217142dceae2SAndrei Emeltchenko u8 remote_efs = 0; 21720a708f8fSGustavo F. Padovan u16 mtu = L2CAP_DEFAULT_MTU; 21730a708f8fSGustavo F. Padovan u16 result = L2CAP_CONF_SUCCESS; 2174c8f79162SAndrei Emeltchenko u16 size; 21750a708f8fSGustavo F. Padovan 217673ffa904SGustavo F. Padovan BT_DBG("chan %p", chan); 21770a708f8fSGustavo F. Padovan 21780a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 21790a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&req, &type, &olen, &val); 21800a708f8fSGustavo F. Padovan 21810a708f8fSGustavo F. Padovan hint = type & L2CAP_CONF_HINT; 21820a708f8fSGustavo F. Padovan type &= L2CAP_CONF_MASK; 21830a708f8fSGustavo F. Padovan 21840a708f8fSGustavo F. Padovan switch (type) { 21850a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 21860a708f8fSGustavo F. Padovan mtu = val; 21870a708f8fSGustavo F. Padovan break; 21880a708f8fSGustavo F. Padovan 21890a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 21900c1bc5c6SGustavo F. Padovan chan->flush_to = val; 21910a708f8fSGustavo F. Padovan break; 21920a708f8fSGustavo F. Padovan 21930a708f8fSGustavo F. Padovan case L2CAP_CONF_QOS: 21940a708f8fSGustavo F. Padovan break; 21950a708f8fSGustavo F. Padovan 21960a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 21970a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 21980a708f8fSGustavo F. Padovan memcpy(&rfc, (void *) val, olen); 21990a708f8fSGustavo F. Padovan break; 22000a708f8fSGustavo F. Padovan 22010a708f8fSGustavo F. Padovan case L2CAP_CONF_FCS: 22020a708f8fSGustavo F. Padovan if (val == L2CAP_FCS_NONE) 2203c1360a1cSGustavo F. Padovan set_bit(CONF_NO_FCS_RECV, &chan->conf_state); 220442dceae2SAndrei Emeltchenko break; 22050a708f8fSGustavo F. Padovan 220642dceae2SAndrei Emeltchenko case L2CAP_CONF_EFS: 220742dceae2SAndrei Emeltchenko remote_efs = 1; 220842dceae2SAndrei Emeltchenko if (olen == sizeof(efs)) 220942dceae2SAndrei Emeltchenko memcpy(&efs, (void *) val, olen); 22100a708f8fSGustavo F. Padovan break; 22110a708f8fSGustavo F. Padovan 22126327eb98SAndrei Emeltchenko case L2CAP_CONF_EWS: 22136327eb98SAndrei Emeltchenko if (!enable_hs) 22146327eb98SAndrei Emeltchenko return -ECONNREFUSED; 22156327eb98SAndrei Emeltchenko 22166327eb98SAndrei Emeltchenko set_bit(FLAG_EXT_CTRL, &chan->flags); 22176327eb98SAndrei Emeltchenko set_bit(CONF_EWS_RECV, &chan->conf_state); 2218836be934SAndrei Emeltchenko chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; 22196327eb98SAndrei Emeltchenko chan->remote_tx_win = val; 22200a708f8fSGustavo F. Padovan break; 22210a708f8fSGustavo F. Padovan 22220a708f8fSGustavo F. Padovan default: 22230a708f8fSGustavo F. Padovan if (hint) 22240a708f8fSGustavo F. Padovan break; 22250a708f8fSGustavo F. Padovan 22260a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNKNOWN; 22270a708f8fSGustavo F. Padovan *((u8 *) ptr++) = type; 22280a708f8fSGustavo F. Padovan break; 22290a708f8fSGustavo F. Padovan } 22300a708f8fSGustavo F. Padovan } 22310a708f8fSGustavo F. Padovan 223273ffa904SGustavo F. Padovan if (chan->num_conf_rsp || chan->num_conf_req > 1) 22330a708f8fSGustavo F. Padovan goto done; 22340a708f8fSGustavo F. Padovan 22350c1bc5c6SGustavo F. Padovan switch (chan->mode) { 22360a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 22370a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 2238c1360a1cSGustavo F. Padovan if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { 22390c1bc5c6SGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, 22408c1d787bSGustavo F. Padovan chan->conn->feat_mask); 22410a708f8fSGustavo F. Padovan break; 22420a708f8fSGustavo F. Padovan } 22430a708f8fSGustavo F. Padovan 224442dceae2SAndrei Emeltchenko if (remote_efs) { 224542dceae2SAndrei Emeltchenko if (__l2cap_efs_supported(chan)) 224642dceae2SAndrei Emeltchenko set_bit(FLAG_EFS_ENABLE, &chan->flags); 224742dceae2SAndrei Emeltchenko else 224842dceae2SAndrei Emeltchenko return -ECONNREFUSED; 224942dceae2SAndrei Emeltchenko } 225042dceae2SAndrei Emeltchenko 22510c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) 22520a708f8fSGustavo F. Padovan return -ECONNREFUSED; 22530a708f8fSGustavo F. Padovan 22540a708f8fSGustavo F. Padovan break; 22550a708f8fSGustavo F. Padovan } 22560a708f8fSGustavo F. Padovan 22570a708f8fSGustavo F. Padovan done: 22580c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) { 22590a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 22600c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 22610a708f8fSGustavo F. Padovan 226273ffa904SGustavo F. Padovan if (chan->num_conf_rsp == 1) 22630a708f8fSGustavo F. Padovan return -ECONNREFUSED; 22640a708f8fSGustavo F. Padovan 22650a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 22660a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 22670a708f8fSGustavo F. Padovan } 22680a708f8fSGustavo F. Padovan 22690a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) { 22700a708f8fSGustavo F. Padovan /* Configure output options and let the other side know 22710a708f8fSGustavo F. Padovan * which ones we don't like. */ 22720a708f8fSGustavo F. Padovan 22730a708f8fSGustavo F. Padovan if (mtu < L2CAP_DEFAULT_MIN_MTU) 22740a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 22750a708f8fSGustavo F. Padovan else { 22760c1bc5c6SGustavo F. Padovan chan->omtu = mtu; 2277c1360a1cSGustavo F. Padovan set_bit(CONF_MTU_DONE, &chan->conf_state); 22780a708f8fSGustavo F. Padovan } 22790c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); 22800a708f8fSGustavo F. Padovan 228142dceae2SAndrei Emeltchenko if (remote_efs) { 228242dceae2SAndrei Emeltchenko if (chan->local_stype != L2CAP_SERV_NOTRAFIC && 228342dceae2SAndrei Emeltchenko efs.stype != L2CAP_SERV_NOTRAFIC && 228442dceae2SAndrei Emeltchenko efs.stype != chan->local_stype) { 228542dceae2SAndrei Emeltchenko 228642dceae2SAndrei Emeltchenko result = L2CAP_CONF_UNACCEPT; 228742dceae2SAndrei Emeltchenko 228842dceae2SAndrei Emeltchenko if (chan->num_conf_req >= 1) 228942dceae2SAndrei Emeltchenko return -ECONNREFUSED; 229042dceae2SAndrei Emeltchenko 229142dceae2SAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, 229242dceae2SAndrei Emeltchenko sizeof(efs), 229342dceae2SAndrei Emeltchenko (unsigned long) &efs); 22940e8b207eSAndrei Emeltchenko } else { 22953e6b3b95SGustavo F. Padovan /* Send PENDING Conf Rsp */ 22960e8b207eSAndrei Emeltchenko result = L2CAP_CONF_PENDING; 22970e8b207eSAndrei Emeltchenko set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 229842dceae2SAndrei Emeltchenko } 229942dceae2SAndrei Emeltchenko } 230042dceae2SAndrei Emeltchenko 23010a708f8fSGustavo F. Padovan switch (rfc.mode) { 23020a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 230347d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2304c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 23050a708f8fSGustavo F. Padovan break; 23060a708f8fSGustavo F. Padovan 23070a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 23086327eb98SAndrei Emeltchenko if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) 23092c03a7a4SGustavo F. Padovan chan->remote_tx_win = rfc.txwin_size; 23106327eb98SAndrei Emeltchenko else 23116327eb98SAndrei Emeltchenko rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; 23126327eb98SAndrei Emeltchenko 23132c03a7a4SGustavo F. Padovan chan->remote_max_tx = rfc.max_transmit; 23140a708f8fSGustavo F. Padovan 2315c8f79162SAndrei Emeltchenko size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), 2316c8f79162SAndrei Emeltchenko chan->conn->mtu - 2317c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2318c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2319c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2320c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 2321c8f79162SAndrei Emeltchenko chan->remote_mps = size; 23220a708f8fSGustavo F. Padovan 23230a708f8fSGustavo F. Padovan rfc.retrans_timeout = 23240a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO); 23250a708f8fSGustavo F. Padovan rfc.monitor_timeout = 23260a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO); 23270a708f8fSGustavo F. Padovan 2328c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 23290a708f8fSGustavo F. Padovan 23300a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 23310a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 23320a708f8fSGustavo F. Padovan 233342dceae2SAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { 233442dceae2SAndrei Emeltchenko chan->remote_id = efs.id; 233542dceae2SAndrei Emeltchenko chan->remote_stype = efs.stype; 233642dceae2SAndrei Emeltchenko chan->remote_msdu = le16_to_cpu(efs.msdu); 233742dceae2SAndrei Emeltchenko chan->remote_flush_to = 233842dceae2SAndrei Emeltchenko le32_to_cpu(efs.flush_to); 233942dceae2SAndrei Emeltchenko chan->remote_acc_lat = 234042dceae2SAndrei Emeltchenko le32_to_cpu(efs.acc_lat); 234142dceae2SAndrei Emeltchenko chan->remote_sdu_itime = 234242dceae2SAndrei Emeltchenko le32_to_cpu(efs.sdu_itime); 234342dceae2SAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, 234442dceae2SAndrei Emeltchenko sizeof(efs), (unsigned long) &efs); 234542dceae2SAndrei Emeltchenko } 23460a708f8fSGustavo F. Padovan break; 23470a708f8fSGustavo F. Padovan 23480a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 2349c8f79162SAndrei Emeltchenko size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), 2350c8f79162SAndrei Emeltchenko chan->conn->mtu - 2351c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2352c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2353c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2354c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 2355c8f79162SAndrei Emeltchenko chan->remote_mps = size; 23560a708f8fSGustavo F. Padovan 2357c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 23580a708f8fSGustavo F. Padovan 23590a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 23600a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 23610a708f8fSGustavo F. Padovan 23620a708f8fSGustavo F. Padovan break; 23630a708f8fSGustavo F. Padovan 23640a708f8fSGustavo F. Padovan default: 23650a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 23660a708f8fSGustavo F. Padovan 23670a708f8fSGustavo F. Padovan memset(&rfc, 0, sizeof(rfc)); 23680c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 23690a708f8fSGustavo F. Padovan } 23700a708f8fSGustavo F. Padovan 23710a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) 2372c1360a1cSGustavo F. Padovan set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 23730a708f8fSGustavo F. Padovan } 2374fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 23750a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 23760a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(0x0000); 23770a708f8fSGustavo F. Padovan 23780a708f8fSGustavo F. Padovan return ptr - data; 23790a708f8fSGustavo F. Padovan } 23800a708f8fSGustavo F. Padovan 2381b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result) 23820a708f8fSGustavo F. Padovan { 23830a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 23840a708f8fSGustavo F. Padovan void *ptr = req->data; 23850a708f8fSGustavo F. Padovan int type, olen; 23860a708f8fSGustavo F. Padovan unsigned long val; 238736e999a8SMat Martineau struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 238866af7aafSAndrei Emeltchenko struct l2cap_conf_efs efs; 23890a708f8fSGustavo F. Padovan 2390fe4128e0SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data); 23910a708f8fSGustavo F. Padovan 23920a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 23930a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 23940a708f8fSGustavo F. Padovan 23950a708f8fSGustavo F. Padovan switch (type) { 23960a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 23970a708f8fSGustavo F. Padovan if (val < L2CAP_DEFAULT_MIN_MTU) { 23980a708f8fSGustavo F. Padovan *result = L2CAP_CONF_UNACCEPT; 23990c1bc5c6SGustavo F. Padovan chan->imtu = L2CAP_DEFAULT_MIN_MTU; 24000a708f8fSGustavo F. Padovan } else 24010c1bc5c6SGustavo F. Padovan chan->imtu = val; 24020c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 24030a708f8fSGustavo F. Padovan break; 24040a708f8fSGustavo F. Padovan 24050a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 24060c1bc5c6SGustavo F. Padovan chan->flush_to = val; 24070a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 24080c1bc5c6SGustavo F. Padovan 2, chan->flush_to); 24090a708f8fSGustavo F. Padovan break; 24100a708f8fSGustavo F. Padovan 24110a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 24120a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 24130a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 24140a708f8fSGustavo F. Padovan 2415c1360a1cSGustavo F. Padovan if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && 24160c1bc5c6SGustavo F. Padovan rfc.mode != chan->mode) 24170a708f8fSGustavo F. Padovan return -ECONNREFUSED; 24180a708f8fSGustavo F. Padovan 241947d1ec61SGustavo F. Padovan chan->fcs = 0; 24200a708f8fSGustavo F. Padovan 24210a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 24220a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 24230a708f8fSGustavo F. Padovan break; 24246327eb98SAndrei Emeltchenko 24256327eb98SAndrei Emeltchenko case L2CAP_CONF_EWS: 24266327eb98SAndrei Emeltchenko chan->tx_win = min_t(u16, val, 24276327eb98SAndrei Emeltchenko L2CAP_DEFAULT_EXT_WINDOW); 24283e6b3b95SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, 24293e6b3b95SGustavo F. Padovan chan->tx_win); 24306327eb98SAndrei Emeltchenko break; 243166af7aafSAndrei Emeltchenko 243266af7aafSAndrei Emeltchenko case L2CAP_CONF_EFS: 243366af7aafSAndrei Emeltchenko if (olen == sizeof(efs)) 243466af7aafSAndrei Emeltchenko memcpy(&efs, (void *)val, olen); 243566af7aafSAndrei Emeltchenko 243666af7aafSAndrei Emeltchenko if (chan->local_stype != L2CAP_SERV_NOTRAFIC && 243766af7aafSAndrei Emeltchenko efs.stype != L2CAP_SERV_NOTRAFIC && 243866af7aafSAndrei Emeltchenko efs.stype != chan->local_stype) 243966af7aafSAndrei Emeltchenko return -ECONNREFUSED; 244066af7aafSAndrei Emeltchenko 244166af7aafSAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, 244266af7aafSAndrei Emeltchenko sizeof(efs), (unsigned long) &efs); 244366af7aafSAndrei Emeltchenko break; 24440a708f8fSGustavo F. Padovan } 24450a708f8fSGustavo F. Padovan } 24460a708f8fSGustavo F. Padovan 24470c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) 24480a708f8fSGustavo F. Padovan return -ECONNREFUSED; 24490a708f8fSGustavo F. Padovan 24500c1bc5c6SGustavo F. Padovan chan->mode = rfc.mode; 24510a708f8fSGustavo F. Padovan 24520e8b207eSAndrei Emeltchenko if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { 24530a708f8fSGustavo F. Padovan switch (rfc.mode) { 24540a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 245547d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 245647d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 245747d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 245866af7aafSAndrei Emeltchenko 245966af7aafSAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { 246066af7aafSAndrei Emeltchenko chan->local_msdu = le16_to_cpu(efs.msdu); 246166af7aafSAndrei Emeltchenko chan->local_sdu_itime = 246266af7aafSAndrei Emeltchenko le32_to_cpu(efs.sdu_itime); 246366af7aafSAndrei Emeltchenko chan->local_acc_lat = le32_to_cpu(efs.acc_lat); 246466af7aafSAndrei Emeltchenko chan->local_flush_to = 246566af7aafSAndrei Emeltchenko le32_to_cpu(efs.flush_to); 246666af7aafSAndrei Emeltchenko } 24670a708f8fSGustavo F. Padovan break; 246866af7aafSAndrei Emeltchenko 24690a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 247047d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 24710a708f8fSGustavo F. Padovan } 24720a708f8fSGustavo F. Padovan } 24730a708f8fSGustavo F. Padovan 2474fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 24750a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0x0000); 24760a708f8fSGustavo F. Padovan 24770a708f8fSGustavo F. Padovan return ptr - data; 24780a708f8fSGustavo F. Padovan } 24790a708f8fSGustavo F. Padovan 2480fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags) 24810a708f8fSGustavo F. Padovan { 24820a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 24830a708f8fSGustavo F. Padovan void *ptr = rsp->data; 24840a708f8fSGustavo F. Padovan 2485fe4128e0SGustavo F. Padovan BT_DBG("chan %p", chan); 24860a708f8fSGustavo F. Padovan 2487fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 24880a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 24890a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(flags); 24900a708f8fSGustavo F. Padovan 24910a708f8fSGustavo F. Padovan return ptr - data; 24920a708f8fSGustavo F. Padovan } 24930a708f8fSGustavo F. Padovan 24948c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) 2495710f9b0aSGustavo F. Padovan { 2496710f9b0aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 24978c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 2498710f9b0aSGustavo F. Padovan u8 buf[128]; 2499710f9b0aSGustavo F. Padovan 2500fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 2501fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 2502710f9b0aSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 2503710f9b0aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 2504710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 2505710f9b0aSGustavo F. Padovan L2CAP_CONN_RSP, sizeof(rsp), &rsp); 2506710f9b0aSGustavo F. Padovan 2507c1360a1cSGustavo F. Padovan if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) 2508710f9b0aSGustavo F. Padovan return; 2509710f9b0aSGustavo F. Padovan 2510710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 2511710f9b0aSGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 2512710f9b0aSGustavo F. Padovan chan->num_conf_req++; 2513710f9b0aSGustavo F. Padovan } 2514710f9b0aSGustavo F. Padovan 251547d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len) 25160a708f8fSGustavo F. Padovan { 25170a708f8fSGustavo F. Padovan int type, olen; 25180a708f8fSGustavo F. Padovan unsigned long val; 25190a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 25200a708f8fSGustavo F. Padovan 252147d1ec61SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); 25220a708f8fSGustavo F. Padovan 25230c1bc5c6SGustavo F. Padovan if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) 25240a708f8fSGustavo F. Padovan return; 25250a708f8fSGustavo F. Padovan 25260a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 25270a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 25280a708f8fSGustavo F. Padovan 25290a708f8fSGustavo F. Padovan switch (type) { 25300a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 25310a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 25320a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 25330a708f8fSGustavo F. Padovan goto done; 25340a708f8fSGustavo F. Padovan } 25350a708f8fSGustavo F. Padovan } 25360a708f8fSGustavo F. Padovan 253736e999a8SMat Martineau /* Use sane default values in case a misbehaving remote device 253836e999a8SMat Martineau * did not send an RFC option. 253936e999a8SMat Martineau */ 254036e999a8SMat Martineau rfc.mode = chan->mode; 254136e999a8SMat Martineau rfc.retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); 254236e999a8SMat Martineau rfc.monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); 254336e999a8SMat Martineau rfc.max_pdu_size = cpu_to_le16(chan->imtu); 254436e999a8SMat Martineau 254536e999a8SMat Martineau BT_ERR("Expected RFC option was not found, using defaults"); 254636e999a8SMat Martineau 25470a708f8fSGustavo F. Padovan done: 25480a708f8fSGustavo F. Padovan switch (rfc.mode) { 25490a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 255047d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 255147d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 255247d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 25530a708f8fSGustavo F. Padovan break; 25540a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 255547d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 25560a708f8fSGustavo F. Padovan } 25570a708f8fSGustavo F. Padovan } 25580a708f8fSGustavo F. Padovan 25590a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 25600a708f8fSGustavo F. Padovan { 2561e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; 25620a708f8fSGustavo F. Padovan 2563e2fd318eSIlia Kolomisnky if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) 25640a708f8fSGustavo F. Padovan return 0; 25650a708f8fSGustavo F. Padovan 25660a708f8fSGustavo F. Padovan if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && 25670a708f8fSGustavo F. Padovan cmd->ident == conn->info_ident) { 256817cd3f37SUlisses Furquim cancel_delayed_work(&conn->info_timer); 25690a708f8fSGustavo F. Padovan 25700a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 25710a708f8fSGustavo F. Padovan conn->info_ident = 0; 25720a708f8fSGustavo F. Padovan 25730a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 25740a708f8fSGustavo F. Padovan } 25750a708f8fSGustavo F. Padovan 25760a708f8fSGustavo F. Padovan return 0; 25770a708f8fSGustavo F. Padovan } 25780a708f8fSGustavo F. Padovan 25790a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 25800a708f8fSGustavo F. Padovan { 25810a708f8fSGustavo F. Padovan struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; 25820a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 258323691d75SGustavo F. Padovan struct l2cap_chan *chan = NULL, *pchan; 25840a708f8fSGustavo F. Padovan struct sock *parent, *sk = NULL; 25850a708f8fSGustavo F. Padovan int result, status = L2CAP_CS_NO_INFO; 25860a708f8fSGustavo F. Padovan 25870a708f8fSGustavo F. Padovan u16 dcid = 0, scid = __le16_to_cpu(req->scid); 25880a708f8fSGustavo F. Padovan __le16 psm = req->psm; 25890a708f8fSGustavo F. Padovan 25900a708f8fSGustavo F. Padovan BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid); 25910a708f8fSGustavo F. Padovan 25920a708f8fSGustavo F. Padovan /* Check if we have socket listening on psm */ 259323691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src); 259423691d75SGustavo F. Padovan if (!pchan) { 25950a708f8fSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 25960a708f8fSGustavo F. Padovan goto sendresp; 25970a708f8fSGustavo F. Padovan } 25980a708f8fSGustavo F. Padovan 259923691d75SGustavo F. Padovan parent = pchan->sk; 260023691d75SGustavo F. Padovan 26013df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 2602aa2ac881SGustavo F. Padovan lock_sock(parent); 26030a708f8fSGustavo F. Padovan 26040a708f8fSGustavo F. Padovan /* Check if the ACL is secure enough (if not SDP) */ 26050a708f8fSGustavo F. Padovan if (psm != cpu_to_le16(0x0001) && 26060a708f8fSGustavo F. Padovan !hci_conn_check_link_mode(conn->hcon)) { 26079f5a0d7bSAndrei Emeltchenko conn->disc_reason = HCI_ERROR_AUTH_FAILURE; 26080a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 26090a708f8fSGustavo F. Padovan goto response; 26100a708f8fSGustavo F. Padovan } 26110a708f8fSGustavo F. Padovan 26120a708f8fSGustavo F. Padovan result = L2CAP_CR_NO_MEM; 26130a708f8fSGustavo F. Padovan 26140a708f8fSGustavo F. Padovan /* Check for backlog size */ 26150a708f8fSGustavo F. Padovan if (sk_acceptq_is_full(parent)) { 26160a708f8fSGustavo F. Padovan BT_DBG("backlog full %d", parent->sk_ack_backlog); 26170a708f8fSGustavo F. Padovan goto response; 26180a708f8fSGustavo F. Padovan } 26190a708f8fSGustavo F. Padovan 262080808e43SGustavo F. Padovan chan = pchan->ops->new_connection(pchan->data); 262180808e43SGustavo F. Padovan if (!chan) 26220a708f8fSGustavo F. Padovan goto response; 26230a708f8fSGustavo F. Padovan 262480808e43SGustavo F. Padovan sk = chan->sk; 262580808e43SGustavo F. Padovan 26260a708f8fSGustavo F. Padovan /* Check if we already have channel with that dcid */ 2627baa7e1faSGustavo F. Padovan if (__l2cap_get_chan_by_dcid(conn, scid)) { 26280a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 2629ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 26300a708f8fSGustavo F. Padovan goto response; 26310a708f8fSGustavo F. Padovan } 26320a708f8fSGustavo F. Padovan 26330a708f8fSGustavo F. Padovan hci_conn_hold(conn->hcon); 26340a708f8fSGustavo F. Padovan 26350a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->src, conn->src); 26360a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->dst, conn->dst); 2637fe4128e0SGustavo F. Padovan chan->psm = psm; 2638fe4128e0SGustavo F. Padovan chan->dcid = scid; 26390a708f8fSGustavo F. Padovan 2640d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 2641d1010240SGustavo F. Padovan 26423d57dc68SGustavo F. Padovan l2cap_chan_add(conn, chan); 264348454079SGustavo F. Padovan 2644fe4128e0SGustavo F. Padovan dcid = chan->scid; 26450a708f8fSGustavo F. Padovan 2646c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 26470a708f8fSGustavo F. Padovan 2648fc7f8a7eSGustavo F. Padovan chan->ident = cmd->ident; 26490a708f8fSGustavo F. Padovan 26500a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { 2651d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan)) { 26520a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 265389bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECT2); 26540a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 26550a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHOR_PEND; 26560a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 26570a708f8fSGustavo F. Padovan } else { 265889bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONFIG); 26590a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 26600a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 26610a708f8fSGustavo F. Padovan } 26620a708f8fSGustavo F. Padovan } else { 266389bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECT2); 26640a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 26650a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHEN_PEND; 26660a708f8fSGustavo F. Padovan } 26670a708f8fSGustavo F. Padovan } else { 266889bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECT2); 26690a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 26700a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 26710a708f8fSGustavo F. Padovan } 26720a708f8fSGustavo F. Padovan 26730a708f8fSGustavo F. Padovan response: 2674aa2ac881SGustavo F. Padovan release_sock(parent); 26753df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 26760a708f8fSGustavo F. Padovan 26770a708f8fSGustavo F. Padovan sendresp: 26780a708f8fSGustavo F. Padovan rsp.scid = cpu_to_le16(scid); 26790a708f8fSGustavo F. Padovan rsp.dcid = cpu_to_le16(dcid); 26800a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 26810a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(status); 26820a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); 26830a708f8fSGustavo F. Padovan 26840a708f8fSGustavo F. Padovan if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { 26850a708f8fSGustavo F. Padovan struct l2cap_info_req info; 26860a708f8fSGustavo F. Padovan info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 26870a708f8fSGustavo F. Padovan 26880a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 26890a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 26900a708f8fSGustavo F. Padovan 2691030013d8SGustavo F. Padovan schedule_delayed_work(&conn->info_timer, 26920a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 26930a708f8fSGustavo F. Padovan 26940a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 26950a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(info), &info); 26960a708f8fSGustavo F. Padovan } 26970a708f8fSGustavo F. Padovan 2698c1360a1cSGustavo F. Padovan if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && 26990a708f8fSGustavo F. Padovan result == L2CAP_CR_SUCCESS) { 27000a708f8fSGustavo F. Padovan u8 buf[128]; 2701c1360a1cSGustavo F. Padovan set_bit(CONF_REQ_SENT, &chan->conf_state); 27020a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 270373ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 270473ffa904SGustavo F. Padovan chan->num_conf_req++; 27050a708f8fSGustavo F. Padovan } 27060a708f8fSGustavo F. Padovan 27070a708f8fSGustavo F. Padovan return 0; 27080a708f8fSGustavo F. Padovan } 27090a708f8fSGustavo F. Padovan 27100a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 27110a708f8fSGustavo F. Padovan { 27120a708f8fSGustavo F. Padovan struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; 27130a708f8fSGustavo F. Padovan u16 scid, dcid, result, status; 271448454079SGustavo F. Padovan struct l2cap_chan *chan; 27150a708f8fSGustavo F. Padovan struct sock *sk; 27160a708f8fSGustavo F. Padovan u8 req[128]; 27173df91ea2SAndrei Emeltchenko int err; 27180a708f8fSGustavo F. Padovan 27190a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 27200a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 27210a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 27220a708f8fSGustavo F. Padovan status = __le16_to_cpu(rsp->status); 27230a708f8fSGustavo F. Padovan 27241b009c98SAndrei Emeltchenko BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", 27251b009c98SAndrei Emeltchenko dcid, scid, result, status); 27260a708f8fSGustavo F. Padovan 27273df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 27283df91ea2SAndrei Emeltchenko 27290a708f8fSGustavo F. Padovan if (scid) { 27303df91ea2SAndrei Emeltchenko chan = __l2cap_get_chan_by_scid(conn, scid); 27313df91ea2SAndrei Emeltchenko if (!chan) { 27323df91ea2SAndrei Emeltchenko err = -EFAULT; 27333df91ea2SAndrei Emeltchenko goto unlock; 27343df91ea2SAndrei Emeltchenko } 27350a708f8fSGustavo F. Padovan } else { 27363df91ea2SAndrei Emeltchenko chan = __l2cap_get_chan_by_ident(conn, cmd->ident); 27373df91ea2SAndrei Emeltchenko if (!chan) { 27383df91ea2SAndrei Emeltchenko err = -EFAULT; 27393df91ea2SAndrei Emeltchenko goto unlock; 27403df91ea2SAndrei Emeltchenko } 27410a708f8fSGustavo F. Padovan } 27420a708f8fSGustavo F. Padovan 27433df91ea2SAndrei Emeltchenko err = 0; 27443df91ea2SAndrei Emeltchenko 274548454079SGustavo F. Padovan sk = chan->sk; 27463df91ea2SAndrei Emeltchenko lock_sock(sk); 274748454079SGustavo F. Padovan 27480a708f8fSGustavo F. Padovan switch (result) { 27490a708f8fSGustavo F. Padovan case L2CAP_CR_SUCCESS: 275089bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONFIG); 2751fc7f8a7eSGustavo F. Padovan chan->ident = 0; 2752fe4128e0SGustavo F. Padovan chan->dcid = dcid; 2753c1360a1cSGustavo F. Padovan clear_bit(CONF_CONNECT_PEND, &chan->conf_state); 27540a708f8fSGustavo F. Padovan 2755c1360a1cSGustavo F. Padovan if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) 27560a708f8fSGustavo F. Padovan break; 27570a708f8fSGustavo F. Padovan 27580a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 275973ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, req), req); 276073ffa904SGustavo F. Padovan chan->num_conf_req++; 27610a708f8fSGustavo F. Padovan break; 27620a708f8fSGustavo F. Padovan 27630a708f8fSGustavo F. Padovan case L2CAP_CR_PEND: 2764c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 27650a708f8fSGustavo F. Padovan break; 27660a708f8fSGustavo F. Padovan 27670a708f8fSGustavo F. Padovan default: 276848454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNREFUSED); 27690a708f8fSGustavo F. Padovan break; 27700a708f8fSGustavo F. Padovan } 27710a708f8fSGustavo F. Padovan 2772aa2ac881SGustavo F. Padovan release_sock(sk); 27733df91ea2SAndrei Emeltchenko 27743df91ea2SAndrei Emeltchenko unlock: 27753df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 27763df91ea2SAndrei Emeltchenko 27773df91ea2SAndrei Emeltchenko return err; 27780a708f8fSGustavo F. Padovan } 27790a708f8fSGustavo F. Padovan 278047d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan) 27810a708f8fSGustavo F. Padovan { 27820a708f8fSGustavo F. Padovan /* FCS is enabled only in ERTM or streaming mode, if one or both 27830a708f8fSGustavo F. Padovan * sides request it. 27840a708f8fSGustavo F. Padovan */ 27850c1bc5c6SGustavo F. Padovan if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) 278647d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2787c1360a1cSGustavo F. Padovan else if (!test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) 278847d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_CRC16; 27890a708f8fSGustavo F. Padovan } 27900a708f8fSGustavo F. Padovan 27910a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 27920a708f8fSGustavo F. Padovan { 27930a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; 27940a708f8fSGustavo F. Padovan u16 dcid, flags; 27950a708f8fSGustavo F. Padovan u8 rsp[64]; 279648454079SGustavo F. Padovan struct l2cap_chan *chan; 27970a708f8fSGustavo F. Padovan struct sock *sk; 27980a708f8fSGustavo F. Padovan int len; 27990a708f8fSGustavo F. Padovan 28000a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 28010a708f8fSGustavo F. Padovan flags = __le16_to_cpu(req->flags); 28020a708f8fSGustavo F. Padovan 28030a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); 28040a708f8fSGustavo F. Padovan 2805baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 280648454079SGustavo F. Padovan if (!chan) 28070a708f8fSGustavo F. Padovan return -ENOENT; 28080a708f8fSGustavo F. Padovan 280948454079SGustavo F. Padovan sk = chan->sk; 28103df91ea2SAndrei Emeltchenko lock_sock(sk); 281148454079SGustavo F. Padovan 2812033b1142SDavid S. Miller if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { 2813e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_cid rej; 28140a708f8fSGustavo F. Padovan 2815e2fd318eSIlia Kolomisnky rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID); 2816e2fd318eSIlia Kolomisnky rej.scid = cpu_to_le16(chan->scid); 2817e2fd318eSIlia Kolomisnky rej.dcid = cpu_to_le16(chan->dcid); 2818e2fd318eSIlia Kolomisnky 28190a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, 28200a708f8fSGustavo F. Padovan sizeof(rej), &rej); 28210a708f8fSGustavo F. Padovan goto unlock; 28220a708f8fSGustavo F. Padovan } 28230a708f8fSGustavo F. Padovan 28240a708f8fSGustavo F. Padovan /* Reject if config buffer is too small. */ 28250a708f8fSGustavo F. Padovan len = cmd_len - sizeof(*req); 28267ac28817SDan Rosenberg if (len < 0 || chan->conf_len + len > sizeof(chan->conf_req)) { 28270a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2828fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 28290a708f8fSGustavo F. Padovan L2CAP_CONF_REJECT, flags), rsp); 28300a708f8fSGustavo F. Padovan goto unlock; 28310a708f8fSGustavo F. Padovan } 28320a708f8fSGustavo F. Padovan 28330a708f8fSGustavo F. Padovan /* Store config. */ 283473ffa904SGustavo F. Padovan memcpy(chan->conf_req + chan->conf_len, req->data, len); 283573ffa904SGustavo F. Padovan chan->conf_len += len; 28360a708f8fSGustavo F. Padovan 28370a708f8fSGustavo F. Padovan if (flags & 0x0001) { 28380a708f8fSGustavo F. Padovan /* Incomplete config. Send empty response. */ 28390a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2840fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 28410a708f8fSGustavo F. Padovan L2CAP_CONF_SUCCESS, 0x0001), rsp); 28420a708f8fSGustavo F. Padovan goto unlock; 28430a708f8fSGustavo F. Padovan } 28440a708f8fSGustavo F. Padovan 28450a708f8fSGustavo F. Padovan /* Complete config. */ 284673ffa904SGustavo F. Padovan len = l2cap_parse_conf_req(chan, rsp); 28470a708f8fSGustavo F. Padovan if (len < 0) { 2848e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 28490a708f8fSGustavo F. Padovan goto unlock; 28500a708f8fSGustavo F. Padovan } 28510a708f8fSGustavo F. Padovan 28520a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); 285373ffa904SGustavo F. Padovan chan->num_conf_rsp++; 28540a708f8fSGustavo F. Padovan 28550a708f8fSGustavo F. Padovan /* Reset config buffer. */ 285673ffa904SGustavo F. Padovan chan->conf_len = 0; 28570a708f8fSGustavo F. Padovan 2858c1360a1cSGustavo F. Padovan if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) 28590a708f8fSGustavo F. Padovan goto unlock; 28600a708f8fSGustavo F. Padovan 2861c1360a1cSGustavo F. Padovan if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { 286247d1ec61SGustavo F. Padovan set_default_fcs(chan); 28630a708f8fSGustavo F. Padovan 286489bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 28650a708f8fSGustavo F. Padovan 286642e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 286742e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 286858d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 28690c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2870525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 28710a708f8fSGustavo F. Padovan 2872cf4cd009SAndrei Emeltchenko l2cap_chan_ready(chan); 28730a708f8fSGustavo F. Padovan goto unlock; 28740a708f8fSGustavo F. Padovan } 28750a708f8fSGustavo F. Padovan 2876c1360a1cSGustavo F. Padovan if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { 28770a708f8fSGustavo F. Padovan u8 buf[64]; 28780a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 287973ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 288073ffa904SGustavo F. Padovan chan->num_conf_req++; 28810a708f8fSGustavo F. Padovan } 28820a708f8fSGustavo F. Padovan 28830e8b207eSAndrei Emeltchenko /* Got Conf Rsp PENDING from remote side and asume we sent 28840e8b207eSAndrei Emeltchenko Conf Rsp PENDING in the code above */ 28850e8b207eSAndrei Emeltchenko if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && 28860e8b207eSAndrei Emeltchenko test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { 28870e8b207eSAndrei Emeltchenko 28880e8b207eSAndrei Emeltchenko /* check compatibility */ 28890e8b207eSAndrei Emeltchenko 28900e8b207eSAndrei Emeltchenko clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 28910e8b207eSAndrei Emeltchenko set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 28920e8b207eSAndrei Emeltchenko 28930e8b207eSAndrei Emeltchenko l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 28940e8b207eSAndrei Emeltchenko l2cap_build_conf_rsp(chan, rsp, 28950e8b207eSAndrei Emeltchenko L2CAP_CONF_SUCCESS, 0x0000), rsp); 28960e8b207eSAndrei Emeltchenko } 28970e8b207eSAndrei Emeltchenko 28980a708f8fSGustavo F. Padovan unlock: 2899aa2ac881SGustavo F. Padovan release_sock(sk); 29000a708f8fSGustavo F. Padovan return 0; 29010a708f8fSGustavo F. Padovan } 29020a708f8fSGustavo F. Padovan 29030a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 29040a708f8fSGustavo F. Padovan { 29050a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; 29060a708f8fSGustavo F. Padovan u16 scid, flags, result; 290748454079SGustavo F. Padovan struct l2cap_chan *chan; 29080a708f8fSGustavo F. Padovan struct sock *sk; 29090a708f8fSGustavo F. Padovan int len = cmd->len - sizeof(*rsp); 29100a708f8fSGustavo F. Padovan 29110a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 29120a708f8fSGustavo F. Padovan flags = __le16_to_cpu(rsp->flags); 29130a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 29140a708f8fSGustavo F. Padovan 29150a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", 29160a708f8fSGustavo F. Padovan scid, flags, result); 29170a708f8fSGustavo F. Padovan 2918baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 291948454079SGustavo F. Padovan if (!chan) 29200a708f8fSGustavo F. Padovan return 0; 29210a708f8fSGustavo F. Padovan 292248454079SGustavo F. Padovan sk = chan->sk; 29233df91ea2SAndrei Emeltchenko lock_sock(sk); 292448454079SGustavo F. Padovan 29250a708f8fSGustavo F. Padovan switch (result) { 29260a708f8fSGustavo F. Padovan case L2CAP_CONF_SUCCESS: 292747d1ec61SGustavo F. Padovan l2cap_conf_rfc_get(chan, rsp->data, len); 29280e8b207eSAndrei Emeltchenko clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); 29290a708f8fSGustavo F. Padovan break; 29300a708f8fSGustavo F. Padovan 29310e8b207eSAndrei Emeltchenko case L2CAP_CONF_PENDING: 29320e8b207eSAndrei Emeltchenko set_bit(CONF_REM_CONF_PEND, &chan->conf_state); 29330e8b207eSAndrei Emeltchenko 29340e8b207eSAndrei Emeltchenko if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { 29350e8b207eSAndrei Emeltchenko char buf[64]; 29360e8b207eSAndrei Emeltchenko 29370e8b207eSAndrei Emeltchenko len = l2cap_parse_conf_rsp(chan, rsp->data, len, 29380e8b207eSAndrei Emeltchenko buf, &result); 29390e8b207eSAndrei Emeltchenko if (len < 0) { 29400e8b207eSAndrei Emeltchenko l2cap_send_disconn_req(conn, chan, ECONNRESET); 29410e8b207eSAndrei Emeltchenko goto done; 29420e8b207eSAndrei Emeltchenko } 29430e8b207eSAndrei Emeltchenko 29440e8b207eSAndrei Emeltchenko /* check compatibility */ 29450e8b207eSAndrei Emeltchenko 29460e8b207eSAndrei Emeltchenko clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 29470e8b207eSAndrei Emeltchenko set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 29480e8b207eSAndrei Emeltchenko 29490e8b207eSAndrei Emeltchenko l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 29500e8b207eSAndrei Emeltchenko l2cap_build_conf_rsp(chan, buf, 29510e8b207eSAndrei Emeltchenko L2CAP_CONF_SUCCESS, 0x0000), buf); 29520e8b207eSAndrei Emeltchenko } 29530e8b207eSAndrei Emeltchenko goto done; 29540e8b207eSAndrei Emeltchenko 29550a708f8fSGustavo F. Padovan case L2CAP_CONF_UNACCEPT: 295673ffa904SGustavo F. Padovan if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { 29570a708f8fSGustavo F. Padovan char req[64]; 29580a708f8fSGustavo F. Padovan 29590a708f8fSGustavo F. Padovan if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { 2960e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 29610a708f8fSGustavo F. Padovan goto done; 29620a708f8fSGustavo F. Padovan } 29630a708f8fSGustavo F. Padovan 29640a708f8fSGustavo F. Padovan /* throw out any old stored conf requests */ 29650a708f8fSGustavo F. Padovan result = L2CAP_CONF_SUCCESS; 2966b4450035SGustavo F. Padovan len = l2cap_parse_conf_rsp(chan, rsp->data, len, 2967b4450035SGustavo F. Padovan req, &result); 29680a708f8fSGustavo F. Padovan if (len < 0) { 2969e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 29700a708f8fSGustavo F. Padovan goto done; 29710a708f8fSGustavo F. Padovan } 29720a708f8fSGustavo F. Padovan 29730a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 29740a708f8fSGustavo F. Padovan L2CAP_CONF_REQ, len, req); 297573ffa904SGustavo F. Padovan chan->num_conf_req++; 29760a708f8fSGustavo F. Padovan if (result != L2CAP_CONF_SUCCESS) 29770a708f8fSGustavo F. Padovan goto done; 29780a708f8fSGustavo F. Padovan break; 29790a708f8fSGustavo F. Padovan } 29800a708f8fSGustavo F. Padovan 29810a708f8fSGustavo F. Padovan default: 29820a708f8fSGustavo F. Padovan sk->sk_err = ECONNRESET; 2983b83ddfe2SAndrzej Kaczmarek __set_chan_timer(chan, 2984b83ddfe2SAndrzej Kaczmarek msecs_to_jiffies(L2CAP_DISC_REJ_TIMEOUT)); 2985e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 29860a708f8fSGustavo F. Padovan goto done; 29870a708f8fSGustavo F. Padovan } 29880a708f8fSGustavo F. Padovan 29890a708f8fSGustavo F. Padovan if (flags & 0x01) 29900a708f8fSGustavo F. Padovan goto done; 29910a708f8fSGustavo F. Padovan 2992c1360a1cSGustavo F. Padovan set_bit(CONF_INPUT_DONE, &chan->conf_state); 29930a708f8fSGustavo F. Padovan 2994c1360a1cSGustavo F. Padovan if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { 299547d1ec61SGustavo F. Padovan set_default_fcs(chan); 29960a708f8fSGustavo F. Padovan 299789bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 299842e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 299942e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 300058d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 30010c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 3002525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 30030a708f8fSGustavo F. Padovan 3004cf4cd009SAndrei Emeltchenko l2cap_chan_ready(chan); 30050a708f8fSGustavo F. Padovan } 30060a708f8fSGustavo F. Padovan 30070a708f8fSGustavo F. Padovan done: 3008aa2ac881SGustavo F. Padovan release_sock(sk); 30090a708f8fSGustavo F. Padovan return 0; 30100a708f8fSGustavo F. Padovan } 30110a708f8fSGustavo F. Padovan 30120a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 30130a708f8fSGustavo F. Padovan { 30140a708f8fSGustavo F. Padovan struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; 30150a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp rsp; 30160a708f8fSGustavo F. Padovan u16 dcid, scid; 301748454079SGustavo F. Padovan struct l2cap_chan *chan; 30180a708f8fSGustavo F. Padovan struct sock *sk; 30190a708f8fSGustavo F. Padovan 30200a708f8fSGustavo F. Padovan scid = __le16_to_cpu(req->scid); 30210a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 30220a708f8fSGustavo F. Padovan 30230a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); 30240a708f8fSGustavo F. Padovan 30253df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 30263df91ea2SAndrei Emeltchenko 30273df91ea2SAndrei Emeltchenko chan = __l2cap_get_chan_by_scid(conn, dcid); 30283df91ea2SAndrei Emeltchenko if (!chan) { 30293df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 30300a708f8fSGustavo F. Padovan return 0; 30313df91ea2SAndrei Emeltchenko } 30320a708f8fSGustavo F. Padovan 303348454079SGustavo F. Padovan sk = chan->sk; 30343df91ea2SAndrei Emeltchenko lock_sock(sk); 303548454079SGustavo F. Padovan 3036fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 3037fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 30380a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); 30390a708f8fSGustavo F. Padovan 30400a708f8fSGustavo F. Padovan sk->sk_shutdown = SHUTDOWN_MASK; 30410a708f8fSGustavo F. Padovan 304248454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNRESET); 3043aa2ac881SGustavo F. Padovan release_sock(sk); 30440a708f8fSGustavo F. Padovan 3045ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 30463df91ea2SAndrei Emeltchenko 30473df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 30483df91ea2SAndrei Emeltchenko 30490a708f8fSGustavo F. Padovan return 0; 30500a708f8fSGustavo F. Padovan } 30510a708f8fSGustavo F. Padovan 30520a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 30530a708f8fSGustavo F. Padovan { 30540a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; 30550a708f8fSGustavo F. Padovan u16 dcid, scid; 305648454079SGustavo F. Padovan struct l2cap_chan *chan; 30570a708f8fSGustavo F. Padovan struct sock *sk; 30580a708f8fSGustavo F. Padovan 30590a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 30600a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 30610a708f8fSGustavo F. Padovan 30620a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); 30630a708f8fSGustavo F. Padovan 30643df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 30653df91ea2SAndrei Emeltchenko 30663df91ea2SAndrei Emeltchenko chan = __l2cap_get_chan_by_scid(conn, scid); 30673df91ea2SAndrei Emeltchenko if (!chan) { 30683df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 30690a708f8fSGustavo F. Padovan return 0; 30703df91ea2SAndrei Emeltchenko } 30710a708f8fSGustavo F. Padovan 307248454079SGustavo F. Padovan sk = chan->sk; 30733df91ea2SAndrei Emeltchenko lock_sock(sk); 307448454079SGustavo F. Padovan 307548454079SGustavo F. Padovan l2cap_chan_del(chan, 0); 3076aa2ac881SGustavo F. Padovan release_sock(sk); 30770a708f8fSGustavo F. Padovan 3078ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 30793df91ea2SAndrei Emeltchenko 30803df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 30813df91ea2SAndrei Emeltchenko 30820a708f8fSGustavo F. Padovan return 0; 30830a708f8fSGustavo F. Padovan } 30840a708f8fSGustavo F. Padovan 30850a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 30860a708f8fSGustavo F. Padovan { 30870a708f8fSGustavo F. Padovan struct l2cap_info_req *req = (struct l2cap_info_req *) data; 30880a708f8fSGustavo F. Padovan u16 type; 30890a708f8fSGustavo F. Padovan 30900a708f8fSGustavo F. Padovan type = __le16_to_cpu(req->type); 30910a708f8fSGustavo F. Padovan 30920a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x", type); 30930a708f8fSGustavo F. Padovan 30940a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 30950a708f8fSGustavo F. Padovan u8 buf[8]; 30960a708f8fSGustavo F. Padovan u32 feat_mask = l2cap_feat_mask; 30970a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 30980a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 30990a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 31000a708f8fSGustavo F. Padovan if (!disable_ertm) 31010a708f8fSGustavo F. Padovan feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING 31020a708f8fSGustavo F. Padovan | L2CAP_FEAT_FCS; 3103a5fd6f30SAndrei Emeltchenko if (enable_hs) 31046327eb98SAndrei Emeltchenko feat_mask |= L2CAP_FEAT_EXT_FLOW 31056327eb98SAndrei Emeltchenko | L2CAP_FEAT_EXT_WINDOW; 3106a5fd6f30SAndrei Emeltchenko 31070a708f8fSGustavo F. Padovan put_unaligned_le32(feat_mask, rsp->data); 31080a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 31090a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 31100a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 31110a708f8fSGustavo F. Padovan u8 buf[12]; 31120a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 311350a147cdSMat Martineau 311450a147cdSMat Martineau if (enable_hs) 311550a147cdSMat Martineau l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; 311650a147cdSMat Martineau else 311750a147cdSMat Martineau l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; 311850a147cdSMat Martineau 31190a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 31200a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 3121c6337ea6SAndrei Emeltchenko memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan)); 31220a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 31230a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 31240a708f8fSGustavo F. Padovan } else { 31250a708f8fSGustavo F. Padovan struct l2cap_info_rsp rsp; 31260a708f8fSGustavo F. Padovan rsp.type = cpu_to_le16(type); 31270a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); 31280a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 31290a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(rsp), &rsp); 31300a708f8fSGustavo F. Padovan } 31310a708f8fSGustavo F. Padovan 31320a708f8fSGustavo F. Padovan return 0; 31330a708f8fSGustavo F. Padovan } 31340a708f8fSGustavo F. Padovan 31350a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 31360a708f8fSGustavo F. Padovan { 31370a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; 31380a708f8fSGustavo F. Padovan u16 type, result; 31390a708f8fSGustavo F. Padovan 31400a708f8fSGustavo F. Padovan type = __le16_to_cpu(rsp->type); 31410a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 31420a708f8fSGustavo F. Padovan 31430a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); 31440a708f8fSGustavo F. Padovan 3145e90165beSAndrei Emeltchenko /* L2CAP Info req/rsp are unbound to channels, add extra checks */ 3146e90165beSAndrei Emeltchenko if (cmd->ident != conn->info_ident || 3147e90165beSAndrei Emeltchenko conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) 3148e90165beSAndrei Emeltchenko return 0; 3149e90165beSAndrei Emeltchenko 315017cd3f37SUlisses Furquim cancel_delayed_work(&conn->info_timer); 31510a708f8fSGustavo F. Padovan 31520a708f8fSGustavo F. Padovan if (result != L2CAP_IR_SUCCESS) { 31530a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 31540a708f8fSGustavo F. Padovan conn->info_ident = 0; 31550a708f8fSGustavo F. Padovan 31560a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 31570a708f8fSGustavo F. Padovan 31580a708f8fSGustavo F. Padovan return 0; 31590a708f8fSGustavo F. Padovan } 31600a708f8fSGustavo F. Padovan 31610a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 31620a708f8fSGustavo F. Padovan conn->feat_mask = get_unaligned_le32(rsp->data); 31630a708f8fSGustavo F. Padovan 31640a708f8fSGustavo F. Padovan if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { 31650a708f8fSGustavo F. Padovan struct l2cap_info_req req; 31660a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 31670a708f8fSGustavo F. Padovan 31680a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 31690a708f8fSGustavo F. Padovan 31700a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 31710a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 31720a708f8fSGustavo F. Padovan } else { 31730a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 31740a708f8fSGustavo F. Padovan conn->info_ident = 0; 31750a708f8fSGustavo F. Padovan 31760a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 31770a708f8fSGustavo F. Padovan } 31780a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 31790a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 31800a708f8fSGustavo F. Padovan conn->info_ident = 0; 31810a708f8fSGustavo F. Padovan 31820a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 31830a708f8fSGustavo F. Padovan } 31840a708f8fSGustavo F. Padovan 31850a708f8fSGustavo F. Padovan return 0; 31860a708f8fSGustavo F. Padovan } 31870a708f8fSGustavo F. Padovan 3188f94ff6ffSMat Martineau static inline int l2cap_create_channel_req(struct l2cap_conn *conn, 3189f94ff6ffSMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, 3190f94ff6ffSMat Martineau void *data) 3191f94ff6ffSMat Martineau { 3192f94ff6ffSMat Martineau struct l2cap_create_chan_req *req = data; 3193f94ff6ffSMat Martineau struct l2cap_create_chan_rsp rsp; 3194f94ff6ffSMat Martineau u16 psm, scid; 3195f94ff6ffSMat Martineau 3196f94ff6ffSMat Martineau if (cmd_len != sizeof(*req)) 3197f94ff6ffSMat Martineau return -EPROTO; 3198f94ff6ffSMat Martineau 3199f94ff6ffSMat Martineau if (!enable_hs) 3200f94ff6ffSMat Martineau return -EINVAL; 3201f94ff6ffSMat Martineau 3202f94ff6ffSMat Martineau psm = le16_to_cpu(req->psm); 3203f94ff6ffSMat Martineau scid = le16_to_cpu(req->scid); 3204f94ff6ffSMat Martineau 3205f94ff6ffSMat Martineau BT_DBG("psm %d, scid %d, amp_id %d", psm, scid, req->amp_id); 3206f94ff6ffSMat Martineau 3207f94ff6ffSMat Martineau /* Placeholder: Always reject */ 3208f94ff6ffSMat Martineau rsp.dcid = 0; 3209f94ff6ffSMat Martineau rsp.scid = cpu_to_le16(scid); 3210f94ff6ffSMat Martineau rsp.result = L2CAP_CR_NO_MEM; 3211f94ff6ffSMat Martineau rsp.status = L2CAP_CS_NO_INFO; 3212f94ff6ffSMat Martineau 3213f94ff6ffSMat Martineau l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, 3214f94ff6ffSMat Martineau sizeof(rsp), &rsp); 3215f94ff6ffSMat Martineau 3216f94ff6ffSMat Martineau return 0; 3217f94ff6ffSMat Martineau } 3218f94ff6ffSMat Martineau 3219f94ff6ffSMat Martineau static inline int l2cap_create_channel_rsp(struct l2cap_conn *conn, 3220f94ff6ffSMat Martineau struct l2cap_cmd_hdr *cmd, void *data) 3221f94ff6ffSMat Martineau { 3222f94ff6ffSMat Martineau BT_DBG("conn %p", conn); 3223f94ff6ffSMat Martineau 3224f94ff6ffSMat Martineau return l2cap_connect_rsp(conn, cmd, data); 3225f94ff6ffSMat Martineau } 3226f94ff6ffSMat Martineau 32278d5a04a1SMat Martineau static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident, 32288d5a04a1SMat Martineau u16 icid, u16 result) 32298d5a04a1SMat Martineau { 32308d5a04a1SMat Martineau struct l2cap_move_chan_rsp rsp; 32318d5a04a1SMat Martineau 32328d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 32338d5a04a1SMat Martineau 32348d5a04a1SMat Martineau rsp.icid = cpu_to_le16(icid); 32358d5a04a1SMat Martineau rsp.result = cpu_to_le16(result); 32368d5a04a1SMat Martineau 32378d5a04a1SMat Martineau l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_RSP, sizeof(rsp), &rsp); 32388d5a04a1SMat Martineau } 32398d5a04a1SMat Martineau 32408d5a04a1SMat Martineau static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn, 32418d5a04a1SMat Martineau struct l2cap_chan *chan, u16 icid, u16 result) 32428d5a04a1SMat Martineau { 32438d5a04a1SMat Martineau struct l2cap_move_chan_cfm cfm; 32448d5a04a1SMat Martineau u8 ident; 32458d5a04a1SMat Martineau 32468d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 32478d5a04a1SMat Martineau 32488d5a04a1SMat Martineau ident = l2cap_get_ident(conn); 32498d5a04a1SMat Martineau if (chan) 32508d5a04a1SMat Martineau chan->ident = ident; 32518d5a04a1SMat Martineau 32528d5a04a1SMat Martineau cfm.icid = cpu_to_le16(icid); 32538d5a04a1SMat Martineau cfm.result = cpu_to_le16(result); 32548d5a04a1SMat Martineau 32558d5a04a1SMat Martineau l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM, sizeof(cfm), &cfm); 32568d5a04a1SMat Martineau } 32578d5a04a1SMat Martineau 32588d5a04a1SMat Martineau static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident, 32598d5a04a1SMat Martineau u16 icid) 32608d5a04a1SMat Martineau { 32618d5a04a1SMat Martineau struct l2cap_move_chan_cfm_rsp rsp; 32628d5a04a1SMat Martineau 32638d5a04a1SMat Martineau BT_DBG("icid %d", icid); 32648d5a04a1SMat Martineau 32658d5a04a1SMat Martineau rsp.icid = cpu_to_le16(icid); 32668d5a04a1SMat Martineau l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); 32678d5a04a1SMat Martineau } 32688d5a04a1SMat Martineau 32698d5a04a1SMat Martineau static inline int l2cap_move_channel_req(struct l2cap_conn *conn, 32708d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 32718d5a04a1SMat Martineau { 32728d5a04a1SMat Martineau struct l2cap_move_chan_req *req = data; 32738d5a04a1SMat Martineau u16 icid = 0; 32748d5a04a1SMat Martineau u16 result = L2CAP_MR_NOT_ALLOWED; 32758d5a04a1SMat Martineau 32768d5a04a1SMat Martineau if (cmd_len != sizeof(*req)) 32778d5a04a1SMat Martineau return -EPROTO; 32788d5a04a1SMat Martineau 32798d5a04a1SMat Martineau icid = le16_to_cpu(req->icid); 32808d5a04a1SMat Martineau 32818d5a04a1SMat Martineau BT_DBG("icid %d, dest_amp_id %d", icid, req->dest_amp_id); 32828d5a04a1SMat Martineau 32838d5a04a1SMat Martineau if (!enable_hs) 32848d5a04a1SMat Martineau return -EINVAL; 32858d5a04a1SMat Martineau 32868d5a04a1SMat Martineau /* Placeholder: Always refuse */ 32878d5a04a1SMat Martineau l2cap_send_move_chan_rsp(conn, cmd->ident, icid, result); 32888d5a04a1SMat Martineau 32898d5a04a1SMat Martineau return 0; 32908d5a04a1SMat Martineau } 32918d5a04a1SMat Martineau 32928d5a04a1SMat Martineau static inline int l2cap_move_channel_rsp(struct l2cap_conn *conn, 32938d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 32948d5a04a1SMat Martineau { 32958d5a04a1SMat Martineau struct l2cap_move_chan_rsp *rsp = data; 32968d5a04a1SMat Martineau u16 icid, result; 32978d5a04a1SMat Martineau 32988d5a04a1SMat Martineau if (cmd_len != sizeof(*rsp)) 32998d5a04a1SMat Martineau return -EPROTO; 33008d5a04a1SMat Martineau 33018d5a04a1SMat Martineau icid = le16_to_cpu(rsp->icid); 33028d5a04a1SMat Martineau result = le16_to_cpu(rsp->result); 33038d5a04a1SMat Martineau 33048d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 33058d5a04a1SMat Martineau 33068d5a04a1SMat Martineau /* Placeholder: Always unconfirmed */ 33078d5a04a1SMat Martineau l2cap_send_move_chan_cfm(conn, NULL, icid, L2CAP_MC_UNCONFIRMED); 33088d5a04a1SMat Martineau 33098d5a04a1SMat Martineau return 0; 33108d5a04a1SMat Martineau } 33118d5a04a1SMat Martineau 33128d5a04a1SMat Martineau static inline int l2cap_move_channel_confirm(struct l2cap_conn *conn, 33138d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 33148d5a04a1SMat Martineau { 33158d5a04a1SMat Martineau struct l2cap_move_chan_cfm *cfm = data; 33168d5a04a1SMat Martineau u16 icid, result; 33178d5a04a1SMat Martineau 33188d5a04a1SMat Martineau if (cmd_len != sizeof(*cfm)) 33198d5a04a1SMat Martineau return -EPROTO; 33208d5a04a1SMat Martineau 33218d5a04a1SMat Martineau icid = le16_to_cpu(cfm->icid); 33228d5a04a1SMat Martineau result = le16_to_cpu(cfm->result); 33238d5a04a1SMat Martineau 33248d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 33258d5a04a1SMat Martineau 33268d5a04a1SMat Martineau l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); 33278d5a04a1SMat Martineau 33288d5a04a1SMat Martineau return 0; 33298d5a04a1SMat Martineau } 33308d5a04a1SMat Martineau 33318d5a04a1SMat Martineau static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, 33328d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 33338d5a04a1SMat Martineau { 33348d5a04a1SMat Martineau struct l2cap_move_chan_cfm_rsp *rsp = data; 33358d5a04a1SMat Martineau u16 icid; 33368d5a04a1SMat Martineau 33378d5a04a1SMat Martineau if (cmd_len != sizeof(*rsp)) 33388d5a04a1SMat Martineau return -EPROTO; 33398d5a04a1SMat Martineau 33408d5a04a1SMat Martineau icid = le16_to_cpu(rsp->icid); 33418d5a04a1SMat Martineau 33428d5a04a1SMat Martineau BT_DBG("icid %d", icid); 33438d5a04a1SMat Martineau 33448d5a04a1SMat Martineau return 0; 33458d5a04a1SMat Martineau } 33468d5a04a1SMat Martineau 3347e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency, 3348de73115aSClaudio Takahasi u16 to_multiplier) 3349de73115aSClaudio Takahasi { 3350de73115aSClaudio Takahasi u16 max_latency; 3351de73115aSClaudio Takahasi 3352de73115aSClaudio Takahasi if (min > max || min < 6 || max > 3200) 3353de73115aSClaudio Takahasi return -EINVAL; 3354de73115aSClaudio Takahasi 3355de73115aSClaudio Takahasi if (to_multiplier < 10 || to_multiplier > 3200) 3356de73115aSClaudio Takahasi return -EINVAL; 3357de73115aSClaudio Takahasi 3358de73115aSClaudio Takahasi if (max >= to_multiplier * 8) 3359de73115aSClaudio Takahasi return -EINVAL; 3360de73115aSClaudio Takahasi 3361de73115aSClaudio Takahasi max_latency = (to_multiplier * 8 / max) - 1; 3362de73115aSClaudio Takahasi if (latency > 499 || latency > max_latency) 3363de73115aSClaudio Takahasi return -EINVAL; 3364de73115aSClaudio Takahasi 3365de73115aSClaudio Takahasi return 0; 3366de73115aSClaudio Takahasi } 3367de73115aSClaudio Takahasi 3368de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, 3369de73115aSClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 3370de73115aSClaudio Takahasi { 3371de73115aSClaudio Takahasi struct hci_conn *hcon = conn->hcon; 3372de73115aSClaudio Takahasi struct l2cap_conn_param_update_req *req; 3373de73115aSClaudio Takahasi struct l2cap_conn_param_update_rsp rsp; 3374de73115aSClaudio Takahasi u16 min, max, latency, to_multiplier, cmd_len; 33752ce603ebSClaudio Takahasi int err; 3376de73115aSClaudio Takahasi 3377de73115aSClaudio Takahasi if (!(hcon->link_mode & HCI_LM_MASTER)) 3378de73115aSClaudio Takahasi return -EINVAL; 3379de73115aSClaudio Takahasi 3380de73115aSClaudio Takahasi cmd_len = __le16_to_cpu(cmd->len); 3381de73115aSClaudio Takahasi if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) 3382de73115aSClaudio Takahasi return -EPROTO; 3383de73115aSClaudio Takahasi 3384de73115aSClaudio Takahasi req = (struct l2cap_conn_param_update_req *) data; 3385de73115aSClaudio Takahasi min = __le16_to_cpu(req->min); 3386de73115aSClaudio Takahasi max = __le16_to_cpu(req->max); 3387de73115aSClaudio Takahasi latency = __le16_to_cpu(req->latency); 3388de73115aSClaudio Takahasi to_multiplier = __le16_to_cpu(req->to_multiplier); 3389de73115aSClaudio Takahasi 3390de73115aSClaudio Takahasi BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", 3391de73115aSClaudio Takahasi min, max, latency, to_multiplier); 3392de73115aSClaudio Takahasi 3393de73115aSClaudio Takahasi memset(&rsp, 0, sizeof(rsp)); 33942ce603ebSClaudio Takahasi 33952ce603ebSClaudio Takahasi err = l2cap_check_conn_param(min, max, latency, to_multiplier); 33962ce603ebSClaudio Takahasi if (err) 3397de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); 3398de73115aSClaudio Takahasi else 3399de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); 3400de73115aSClaudio Takahasi 3401de73115aSClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, 3402de73115aSClaudio Takahasi sizeof(rsp), &rsp); 3403de73115aSClaudio Takahasi 34042ce603ebSClaudio Takahasi if (!err) 34052ce603ebSClaudio Takahasi hci_le_conn_update(hcon, min, max, latency, to_multiplier); 34062ce603ebSClaudio Takahasi 3407de73115aSClaudio Takahasi return 0; 3408de73115aSClaudio Takahasi } 3409de73115aSClaudio Takahasi 34103300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, 34113300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 34123300d9a9SClaudio Takahasi { 34133300d9a9SClaudio Takahasi int err = 0; 34143300d9a9SClaudio Takahasi 34153300d9a9SClaudio Takahasi switch (cmd->code) { 34163300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 34173300d9a9SClaudio Takahasi l2cap_command_rej(conn, cmd, data); 34183300d9a9SClaudio Takahasi break; 34193300d9a9SClaudio Takahasi 34203300d9a9SClaudio Takahasi case L2CAP_CONN_REQ: 34213300d9a9SClaudio Takahasi err = l2cap_connect_req(conn, cmd, data); 34223300d9a9SClaudio Takahasi break; 34233300d9a9SClaudio Takahasi 34243300d9a9SClaudio Takahasi case L2CAP_CONN_RSP: 34253300d9a9SClaudio Takahasi err = l2cap_connect_rsp(conn, cmd, data); 34263300d9a9SClaudio Takahasi break; 34273300d9a9SClaudio Takahasi 34283300d9a9SClaudio Takahasi case L2CAP_CONF_REQ: 34293300d9a9SClaudio Takahasi err = l2cap_config_req(conn, cmd, cmd_len, data); 34303300d9a9SClaudio Takahasi break; 34313300d9a9SClaudio Takahasi 34323300d9a9SClaudio Takahasi case L2CAP_CONF_RSP: 34333300d9a9SClaudio Takahasi err = l2cap_config_rsp(conn, cmd, data); 34343300d9a9SClaudio Takahasi break; 34353300d9a9SClaudio Takahasi 34363300d9a9SClaudio Takahasi case L2CAP_DISCONN_REQ: 34373300d9a9SClaudio Takahasi err = l2cap_disconnect_req(conn, cmd, data); 34383300d9a9SClaudio Takahasi break; 34393300d9a9SClaudio Takahasi 34403300d9a9SClaudio Takahasi case L2CAP_DISCONN_RSP: 34413300d9a9SClaudio Takahasi err = l2cap_disconnect_rsp(conn, cmd, data); 34423300d9a9SClaudio Takahasi break; 34433300d9a9SClaudio Takahasi 34443300d9a9SClaudio Takahasi case L2CAP_ECHO_REQ: 34453300d9a9SClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); 34463300d9a9SClaudio Takahasi break; 34473300d9a9SClaudio Takahasi 34483300d9a9SClaudio Takahasi case L2CAP_ECHO_RSP: 34493300d9a9SClaudio Takahasi break; 34503300d9a9SClaudio Takahasi 34513300d9a9SClaudio Takahasi case L2CAP_INFO_REQ: 34523300d9a9SClaudio Takahasi err = l2cap_information_req(conn, cmd, data); 34533300d9a9SClaudio Takahasi break; 34543300d9a9SClaudio Takahasi 34553300d9a9SClaudio Takahasi case L2CAP_INFO_RSP: 34563300d9a9SClaudio Takahasi err = l2cap_information_rsp(conn, cmd, data); 34573300d9a9SClaudio Takahasi break; 34583300d9a9SClaudio Takahasi 3459f94ff6ffSMat Martineau case L2CAP_CREATE_CHAN_REQ: 3460f94ff6ffSMat Martineau err = l2cap_create_channel_req(conn, cmd, cmd_len, data); 3461f94ff6ffSMat Martineau break; 3462f94ff6ffSMat Martineau 3463f94ff6ffSMat Martineau case L2CAP_CREATE_CHAN_RSP: 3464f94ff6ffSMat Martineau err = l2cap_create_channel_rsp(conn, cmd, data); 3465f94ff6ffSMat Martineau break; 3466f94ff6ffSMat Martineau 34678d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_REQ: 34688d5a04a1SMat Martineau err = l2cap_move_channel_req(conn, cmd, cmd_len, data); 34698d5a04a1SMat Martineau break; 34708d5a04a1SMat Martineau 34718d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_RSP: 34728d5a04a1SMat Martineau err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data); 34738d5a04a1SMat Martineau break; 34748d5a04a1SMat Martineau 34758d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_CFM: 34768d5a04a1SMat Martineau err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); 34778d5a04a1SMat Martineau break; 34788d5a04a1SMat Martineau 34798d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_CFM_RSP: 34808d5a04a1SMat Martineau err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); 34818d5a04a1SMat Martineau break; 34828d5a04a1SMat Martineau 34833300d9a9SClaudio Takahasi default: 34843300d9a9SClaudio Takahasi BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); 34853300d9a9SClaudio Takahasi err = -EINVAL; 34863300d9a9SClaudio Takahasi break; 34873300d9a9SClaudio Takahasi } 34883300d9a9SClaudio Takahasi 34893300d9a9SClaudio Takahasi return err; 34903300d9a9SClaudio Takahasi } 34913300d9a9SClaudio Takahasi 34923300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, 34933300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 34943300d9a9SClaudio Takahasi { 34953300d9a9SClaudio Takahasi switch (cmd->code) { 34963300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 34973300d9a9SClaudio Takahasi return 0; 34983300d9a9SClaudio Takahasi 34993300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_REQ: 3500de73115aSClaudio Takahasi return l2cap_conn_param_update_req(conn, cmd, data); 35013300d9a9SClaudio Takahasi 35023300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_RSP: 35033300d9a9SClaudio Takahasi return 0; 35043300d9a9SClaudio Takahasi 35053300d9a9SClaudio Takahasi default: 35063300d9a9SClaudio Takahasi BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); 35073300d9a9SClaudio Takahasi return -EINVAL; 35083300d9a9SClaudio Takahasi } 35093300d9a9SClaudio Takahasi } 35103300d9a9SClaudio Takahasi 35113300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn, 35123300d9a9SClaudio Takahasi struct sk_buff *skb) 35130a708f8fSGustavo F. Padovan { 35140a708f8fSGustavo F. Padovan u8 *data = skb->data; 35150a708f8fSGustavo F. Padovan int len = skb->len; 35160a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr cmd; 35173300d9a9SClaudio Takahasi int err; 35180a708f8fSGustavo F. Padovan 35190a708f8fSGustavo F. Padovan l2cap_raw_recv(conn, skb); 35200a708f8fSGustavo F. Padovan 35210a708f8fSGustavo F. Padovan while (len >= L2CAP_CMD_HDR_SIZE) { 35220a708f8fSGustavo F. Padovan u16 cmd_len; 35230a708f8fSGustavo F. Padovan memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); 35240a708f8fSGustavo F. Padovan data += L2CAP_CMD_HDR_SIZE; 35250a708f8fSGustavo F. Padovan len -= L2CAP_CMD_HDR_SIZE; 35260a708f8fSGustavo F. Padovan 35270a708f8fSGustavo F. Padovan cmd_len = le16_to_cpu(cmd.len); 35280a708f8fSGustavo F. Padovan 35290a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident); 35300a708f8fSGustavo F. Padovan 35310a708f8fSGustavo F. Padovan if (cmd_len > len || !cmd.ident) { 35320a708f8fSGustavo F. Padovan BT_DBG("corrupted command"); 35330a708f8fSGustavo F. Padovan break; 35340a708f8fSGustavo F. Padovan } 35350a708f8fSGustavo F. Padovan 35363300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 35373300d9a9SClaudio Takahasi err = l2cap_le_sig_cmd(conn, &cmd, data); 35383300d9a9SClaudio Takahasi else 35393300d9a9SClaudio Takahasi err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); 35400a708f8fSGustavo F. Padovan 35410a708f8fSGustavo F. Padovan if (err) { 3542e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_unk rej; 35432c6d1a2eSGustavo F. Padovan 35442c6d1a2eSGustavo F. Padovan BT_ERR("Wrong link type (%d)", err); 35450a708f8fSGustavo F. Padovan 35460a708f8fSGustavo F. Padovan /* FIXME: Map err to a valid reason */ 3547e2fd318eSIlia Kolomisnky rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); 35480a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); 35490a708f8fSGustavo F. Padovan } 35500a708f8fSGustavo F. Padovan 35510a708f8fSGustavo F. Padovan data += cmd_len; 35520a708f8fSGustavo F. Padovan len -= cmd_len; 35530a708f8fSGustavo F. Padovan } 35540a708f8fSGustavo F. Padovan 35550a708f8fSGustavo F. Padovan kfree_skb(skb); 35560a708f8fSGustavo F. Padovan } 35570a708f8fSGustavo F. Padovan 355847d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb) 35590a708f8fSGustavo F. Padovan { 35600a708f8fSGustavo F. Padovan u16 our_fcs, rcv_fcs; 3561e4ca6d98SAndrei Emeltchenko int hdr_size; 3562e4ca6d98SAndrei Emeltchenko 3563e4ca6d98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 3564e4ca6d98SAndrei Emeltchenko hdr_size = L2CAP_EXT_HDR_SIZE; 3565e4ca6d98SAndrei Emeltchenko else 3566e4ca6d98SAndrei Emeltchenko hdr_size = L2CAP_ENH_HDR_SIZE; 35670a708f8fSGustavo F. Padovan 356847d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 356903a51213SAndrei Emeltchenko skb_trim(skb, skb->len - L2CAP_FCS_SIZE); 35700a708f8fSGustavo F. Padovan rcv_fcs = get_unaligned_le16(skb->data + skb->len); 35710a708f8fSGustavo F. Padovan our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); 35720a708f8fSGustavo F. Padovan 35730a708f8fSGustavo F. Padovan if (our_fcs != rcv_fcs) 35740a708f8fSGustavo F. Padovan return -EBADMSG; 35750a708f8fSGustavo F. Padovan } 35760a708f8fSGustavo F. Padovan return 0; 35770a708f8fSGustavo F. Padovan } 35780a708f8fSGustavo F. Padovan 3579525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan) 35800a708f8fSGustavo F. Padovan { 358188843ab0SAndrei Emeltchenko u32 control = 0; 35820a708f8fSGustavo F. Padovan 35836a026610SGustavo F. Padovan chan->frames_sent = 0; 35840a708f8fSGustavo F. Padovan 35850b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 35860a708f8fSGustavo F. Padovan 3587e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 3588ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 3589525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 3590e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 35910a708f8fSGustavo F. Padovan } 35920a708f8fSGustavo F. Padovan 3593e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) 3594525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 35950a708f8fSGustavo F. Padovan 3596525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 35970a708f8fSGustavo F. Padovan 3598e2ab4353SGustavo F. Padovan if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && 35996a026610SGustavo F. Padovan chan->frames_sent == 0) { 3600ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 3601525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 36020a708f8fSGustavo F. Padovan } 36030a708f8fSGustavo F. Padovan } 36040a708f8fSGustavo F. Padovan 3605fb45de7dSAndrei Emeltchenko static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u16 tx_seq, u8 sar) 36060a708f8fSGustavo F. Padovan { 36070a708f8fSGustavo F. Padovan struct sk_buff *next_skb; 36080a708f8fSGustavo F. Padovan int tx_seq_offset, next_tx_seq_offset; 36090a708f8fSGustavo F. Padovan 36100a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 36110a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 36120a708f8fSGustavo F. Padovan 3613f1c6775bSGustavo F. Padovan next_skb = skb_peek(&chan->srej_q); 36140a708f8fSGustavo F. Padovan 3615836be934SAndrei Emeltchenko tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq); 36160a708f8fSGustavo F. Padovan 3617039d9572SSzymon Janc while (next_skb) { 36180a708f8fSGustavo F. Padovan if (bt_cb(next_skb)->tx_seq == tx_seq) 36190a708f8fSGustavo F. Padovan return -EINVAL; 36200a708f8fSGustavo F. Padovan 3621836be934SAndrei Emeltchenko next_tx_seq_offset = __seq_offset(chan, 3622836be934SAndrei Emeltchenko bt_cb(next_skb)->tx_seq, chan->buffer_seq); 36230a708f8fSGustavo F. Padovan 36240a708f8fSGustavo F. Padovan if (next_tx_seq_offset > tx_seq_offset) { 3625f1c6775bSGustavo F. Padovan __skb_queue_before(&chan->srej_q, next_skb, skb); 36260a708f8fSGustavo F. Padovan return 0; 36270a708f8fSGustavo F. Padovan } 36280a708f8fSGustavo F. Padovan 3629f1c6775bSGustavo F. Padovan if (skb_queue_is_last(&chan->srej_q, next_skb)) 3630039d9572SSzymon Janc next_skb = NULL; 3631039d9572SSzymon Janc else 3632039d9572SSzymon Janc next_skb = skb_queue_next(&chan->srej_q, next_skb); 3633039d9572SSzymon Janc } 36340a708f8fSGustavo F. Padovan 3635f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 36360a708f8fSGustavo F. Padovan 36370a708f8fSGustavo F. Padovan return 0; 36380a708f8fSGustavo F. Padovan } 36390a708f8fSGustavo F. Padovan 364084084a31SMat Martineau static void append_skb_frag(struct sk_buff *skb, 364184084a31SMat Martineau struct sk_buff *new_frag, struct sk_buff **last_frag) 36420a708f8fSGustavo F. Padovan { 364384084a31SMat Martineau /* skb->len reflects data in skb as well as all fragments 364484084a31SMat Martineau * skb->data_len reflects only data in fragments 364584084a31SMat Martineau */ 364684084a31SMat Martineau if (!skb_has_frag_list(skb)) 364784084a31SMat Martineau skb_shinfo(skb)->frag_list = new_frag; 364884084a31SMat Martineau 364984084a31SMat Martineau new_frag->next = NULL; 365084084a31SMat Martineau 365184084a31SMat Martineau (*last_frag)->next = new_frag; 365284084a31SMat Martineau *last_frag = new_frag; 365384084a31SMat Martineau 365484084a31SMat Martineau skb->len += new_frag->len; 365584084a31SMat Martineau skb->data_len += new_frag->len; 365684084a31SMat Martineau skb->truesize += new_frag->truesize; 365784084a31SMat Martineau } 365884084a31SMat Martineau 365988843ab0SAndrei Emeltchenko static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u32 control) 366084084a31SMat Martineau { 366184084a31SMat Martineau int err = -EINVAL; 36620a708f8fSGustavo F. Padovan 36637e0ef6eeSAndrei Emeltchenko switch (__get_ctrl_sar(chan, control)) { 36647e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_UNSEGMENTED: 366584084a31SMat Martineau if (chan->sdu) 366684084a31SMat Martineau break; 36670a708f8fSGustavo F. Padovan 366884084a31SMat Martineau err = chan->ops->recv(chan->data, skb); 366984084a31SMat Martineau break; 36700a708f8fSGustavo F. Padovan 36717e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_START: 367284084a31SMat Martineau if (chan->sdu) 367384084a31SMat Martineau break; 36740a708f8fSGustavo F. Padovan 36756f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 367603a51213SAndrei Emeltchenko skb_pull(skb, L2CAP_SDULEN_SIZE); 36770a708f8fSGustavo F. Padovan 367884084a31SMat Martineau if (chan->sdu_len > chan->imtu) { 367984084a31SMat Martineau err = -EMSGSIZE; 368084084a31SMat Martineau break; 368184084a31SMat Martineau } 36820a708f8fSGustavo F. Padovan 368384084a31SMat Martineau if (skb->len >= chan->sdu_len) 368484084a31SMat Martineau break; 368584084a31SMat Martineau 368684084a31SMat Martineau chan->sdu = skb; 368784084a31SMat Martineau chan->sdu_last_frag = skb; 368884084a31SMat Martineau 368984084a31SMat Martineau skb = NULL; 369084084a31SMat Martineau err = 0; 36910a708f8fSGustavo F. Padovan break; 36920a708f8fSGustavo F. Padovan 36937e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_CONTINUE: 36946f61fd47SGustavo F. Padovan if (!chan->sdu) 369584084a31SMat Martineau break; 36960a708f8fSGustavo F. Padovan 369784084a31SMat Martineau append_skb_frag(chan->sdu, skb, 369884084a31SMat Martineau &chan->sdu_last_frag); 369984084a31SMat Martineau skb = NULL; 37000a708f8fSGustavo F. Padovan 370184084a31SMat Martineau if (chan->sdu->len >= chan->sdu_len) 370284084a31SMat Martineau break; 37030a708f8fSGustavo F. Padovan 370484084a31SMat Martineau err = 0; 37050a708f8fSGustavo F. Padovan break; 37060a708f8fSGustavo F. Padovan 37077e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_END: 37086f61fd47SGustavo F. Padovan if (!chan->sdu) 370984084a31SMat Martineau break; 37100a708f8fSGustavo F. Padovan 371184084a31SMat Martineau append_skb_frag(chan->sdu, skb, 371284084a31SMat Martineau &chan->sdu_last_frag); 371384084a31SMat Martineau skb = NULL; 37140a708f8fSGustavo F. Padovan 371584084a31SMat Martineau if (chan->sdu->len != chan->sdu_len) 371684084a31SMat Martineau break; 37170a708f8fSGustavo F. Padovan 371884084a31SMat Martineau err = chan->ops->recv(chan->data, chan->sdu); 37190a708f8fSGustavo F. Padovan 372084084a31SMat Martineau if (!err) { 372184084a31SMat Martineau /* Reassembly complete */ 372284084a31SMat Martineau chan->sdu = NULL; 372384084a31SMat Martineau chan->sdu_last_frag = NULL; 372484084a31SMat Martineau chan->sdu_len = 0; 37250a708f8fSGustavo F. Padovan } 37260a708f8fSGustavo F. Padovan break; 37270a708f8fSGustavo F. Padovan } 37280a708f8fSGustavo F. Padovan 372984084a31SMat Martineau if (err) { 37300a708f8fSGustavo F. Padovan kfree_skb(skb); 37316f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 37326f61fd47SGustavo F. Padovan chan->sdu = NULL; 373384084a31SMat Martineau chan->sdu_last_frag = NULL; 373484084a31SMat Martineau chan->sdu_len = 0; 373584084a31SMat Martineau } 37360a708f8fSGustavo F. Padovan 373784084a31SMat Martineau return err; 37380a708f8fSGustavo F. Padovan } 37390a708f8fSGustavo F. Padovan 374026f880d2SMat Martineau static void l2cap_ertm_enter_local_busy(struct l2cap_chan *chan) 37410a708f8fSGustavo F. Padovan { 374226f880d2SMat Martineau BT_DBG("chan %p, Enter local busy", chan); 374326f880d2SMat Martineau 374426f880d2SMat Martineau set_bit(CONN_LOCAL_BUSY, &chan->conn_state); 374526f880d2SMat Martineau 374677f918bcSSzymon Janc __set_ack_timer(chan); 37470a708f8fSGustavo F. Padovan } 37480a708f8fSGustavo F. Padovan 374926f880d2SMat Martineau static void l2cap_ertm_exit_local_busy(struct l2cap_chan *chan) 375026f880d2SMat Martineau { 375188843ab0SAndrei Emeltchenko u32 control; 37520a708f8fSGustavo F. Padovan 3753e2ab4353SGustavo F. Padovan if (!test_bit(CONN_RNR_SENT, &chan->conn_state)) 37540a708f8fSGustavo F. Padovan goto done; 37550a708f8fSGustavo F. Padovan 37560b209faeSAndrei Emeltchenko control = __set_reqseq(chan, chan->buffer_seq); 3757e3781735SAndrei Emeltchenko control |= __set_ctrl_poll(chan); 3758ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 3759525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 37606a026610SGustavo F. Padovan chan->retry_count = 1; 37610a708f8fSGustavo F. Padovan 37621a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 37631a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 37640a708f8fSGustavo F. Padovan 3765e2ab4353SGustavo F. Padovan set_bit(CONN_WAIT_F, &chan->conn_state); 37660a708f8fSGustavo F. Padovan 37670a708f8fSGustavo F. Padovan done: 3768e2ab4353SGustavo F. Padovan clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); 3769e2ab4353SGustavo F. Padovan clear_bit(CONN_RNR_SENT, &chan->conn_state); 37700a708f8fSGustavo F. Padovan 377149208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit local busy", chan); 37720a708f8fSGustavo F. Padovan } 37730a708f8fSGustavo F. Padovan 3774e328140fSMat Martineau void l2cap_chan_busy(struct l2cap_chan *chan, int busy) 37750a708f8fSGustavo F. Padovan { 3776e328140fSMat Martineau if (chan->mode == L2CAP_MODE_ERTM) { 3777e328140fSMat Martineau if (busy) 377826f880d2SMat Martineau l2cap_ertm_enter_local_busy(chan); 3779e328140fSMat Martineau else 3780e328140fSMat Martineau l2cap_ertm_exit_local_busy(chan); 37810a708f8fSGustavo F. Padovan } 37820a708f8fSGustavo F. Padovan } 37830a708f8fSGustavo F. Padovan 3784fb45de7dSAndrei Emeltchenko static void l2cap_check_srej_gap(struct l2cap_chan *chan, u16 tx_seq) 37850a708f8fSGustavo F. Padovan { 37860a708f8fSGustavo F. Padovan struct sk_buff *skb; 378788843ab0SAndrei Emeltchenko u32 control; 37880a708f8fSGustavo F. Padovan 3789e328140fSMat Martineau while ((skb = skb_peek(&chan->srej_q)) && 3790e328140fSMat Martineau !test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 3791e328140fSMat Martineau int err; 3792e328140fSMat Martineau 37930a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq != tx_seq) 37940a708f8fSGustavo F. Padovan break; 37950a708f8fSGustavo F. Padovan 3796f1c6775bSGustavo F. Padovan skb = skb_dequeue(&chan->srej_q); 37977e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, bt_cb(skb)->sar); 379884084a31SMat Martineau err = l2cap_reassemble_sdu(chan, skb, control); 3799e328140fSMat Martineau 3800e328140fSMat Martineau if (err < 0) { 3801e328140fSMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 3802e328140fSMat Martineau break; 3803e328140fSMat Martineau } 3804e328140fSMat Martineau 3805836be934SAndrei Emeltchenko chan->buffer_seq_srej = __next_seq(chan, chan->buffer_seq_srej); 3806836be934SAndrei Emeltchenko tx_seq = __next_seq(chan, tx_seq); 38070a708f8fSGustavo F. Padovan } 38080a708f8fSGustavo F. Padovan } 38090a708f8fSGustavo F. Padovan 3810fb45de7dSAndrei Emeltchenko static void l2cap_resend_srejframe(struct l2cap_chan *chan, u16 tx_seq) 38110a708f8fSGustavo F. Padovan { 38120a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 381388843ab0SAndrei Emeltchenko u32 control; 38140a708f8fSGustavo F. Padovan 381539d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 38160a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 38170a708f8fSGustavo F. Padovan list_del(&l->list); 38180a708f8fSGustavo F. Padovan kfree(l); 38190a708f8fSGustavo F. Padovan return; 38200a708f8fSGustavo F. Padovan } 3821ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 38220b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, l->tx_seq); 3823525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 38240a708f8fSGustavo F. Padovan list_del(&l->list); 382539d5a3eeSGustavo F. Padovan list_add_tail(&l->list, &chan->srej_l); 38260a708f8fSGustavo F. Padovan } 38270a708f8fSGustavo F. Padovan } 38280a708f8fSGustavo F. Padovan 3829aef89f21SSzymon Janc static int l2cap_send_srejframe(struct l2cap_chan *chan, u16 tx_seq) 38300a708f8fSGustavo F. Padovan { 38310a708f8fSGustavo F. Padovan struct srej_list *new; 383288843ab0SAndrei Emeltchenko u32 control; 38330a708f8fSGustavo F. Padovan 383442e5c802SGustavo F. Padovan while (tx_seq != chan->expected_tx_seq) { 3835ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 38360b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->expected_tx_seq); 3837525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 38380a708f8fSGustavo F. Padovan 38390a708f8fSGustavo F. Padovan new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); 3840aef89f21SSzymon Janc if (!new) 3841aef89f21SSzymon Janc return -ENOMEM; 3842aef89f21SSzymon Janc 384342e5c802SGustavo F. Padovan new->tx_seq = chan->expected_tx_seq; 3844836be934SAndrei Emeltchenko 3845836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); 3846836be934SAndrei Emeltchenko 384739d5a3eeSGustavo F. Padovan list_add_tail(&new->list, &chan->srej_l); 38480a708f8fSGustavo F. Padovan } 3849836be934SAndrei Emeltchenko 3850836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); 3851aef89f21SSzymon Janc 3852aef89f21SSzymon Janc return 0; 38530a708f8fSGustavo F. Padovan } 38540a708f8fSGustavo F. Padovan 385588843ab0SAndrei Emeltchenko static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb) 38560a708f8fSGustavo F. Padovan { 3857fb45de7dSAndrei Emeltchenko u16 tx_seq = __get_txseq(chan, rx_control); 38580b209faeSAndrei Emeltchenko u16 req_seq = __get_reqseq(chan, rx_control); 38597e0ef6eeSAndrei Emeltchenko u8 sar = __get_ctrl_sar(chan, rx_control); 38600a708f8fSGustavo F. Padovan int tx_seq_offset, expected_tx_seq_offset; 386147d1ec61SGustavo F. Padovan int num_to_ack = (chan->tx_win/6) + 1; 38620a708f8fSGustavo F. Padovan int err = 0; 38630a708f8fSGustavo F. Padovan 386488843ab0SAndrei Emeltchenko BT_DBG("chan %p len %d tx_seq %d rx_control 0x%8.8x", chan, skb->len, 3865525cd185SGustavo F. Padovan tx_seq, rx_control); 38660a708f8fSGustavo F. Padovan 386703f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control) && 3868e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 38691a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 38706a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 38711a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 3872e2ab4353SGustavo F. Padovan clear_bit(CONN_WAIT_F, &chan->conn_state); 38730a708f8fSGustavo F. Padovan } 38740a708f8fSGustavo F. Padovan 387542e5c802SGustavo F. Padovan chan->expected_ack_seq = req_seq; 387642e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 38770a708f8fSGustavo F. Padovan 3878836be934SAndrei Emeltchenko tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq); 38790a708f8fSGustavo F. Padovan 38800a708f8fSGustavo F. Padovan /* invalid tx_seq */ 388147d1ec61SGustavo F. Padovan if (tx_seq_offset >= chan->tx_win) { 38828c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38830a708f8fSGustavo F. Padovan goto drop; 38840a708f8fSGustavo F. Padovan } 38850a708f8fSGustavo F. Padovan 388677f918bcSSzymon Janc if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 388777f918bcSSzymon Janc if (!test_bit(CONN_RNR_SENT, &chan->conn_state)) 388877f918bcSSzymon Janc l2cap_send_ack(chan); 38890a708f8fSGustavo F. Padovan goto drop; 389077f918bcSSzymon Janc } 38910a708f8fSGustavo F. Padovan 389202f1b641SMat Martineau if (tx_seq == chan->expected_tx_seq) 389302f1b641SMat Martineau goto expected; 389402f1b641SMat Martineau 3895e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 38960a708f8fSGustavo F. Padovan struct srej_list *first; 38970a708f8fSGustavo F. Padovan 389839d5a3eeSGustavo F. Padovan first = list_first_entry(&chan->srej_l, 38990a708f8fSGustavo F. Padovan struct srej_list, list); 39000a708f8fSGustavo F. Padovan if (tx_seq == first->tx_seq) { 390142e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 3902525cd185SGustavo F. Padovan l2cap_check_srej_gap(chan, tx_seq); 39030a708f8fSGustavo F. Padovan 39040a708f8fSGustavo F. Padovan list_del(&first->list); 39050a708f8fSGustavo F. Padovan kfree(first); 39060a708f8fSGustavo F. Padovan 390739d5a3eeSGustavo F. Padovan if (list_empty(&chan->srej_l)) { 390842e5c802SGustavo F. Padovan chan->buffer_seq = chan->buffer_seq_srej; 3909e2ab4353SGustavo F. Padovan clear_bit(CONN_SREJ_SENT, &chan->conn_state); 3910525cd185SGustavo F. Padovan l2cap_send_ack(chan); 391149208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit SREJ_SENT", chan); 39120a708f8fSGustavo F. Padovan } 39130a708f8fSGustavo F. Padovan } else { 39140a708f8fSGustavo F. Padovan struct srej_list *l; 39150a708f8fSGustavo F. Padovan 39160a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 391742e5c802SGustavo F. Padovan if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0) 39180a708f8fSGustavo F. Padovan goto drop; 39190a708f8fSGustavo F. Padovan 392039d5a3eeSGustavo F. Padovan list_for_each_entry(l, &chan->srej_l, list) { 39210a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 3922525cd185SGustavo F. Padovan l2cap_resend_srejframe(chan, tx_seq); 39230a708f8fSGustavo F. Padovan return 0; 39240a708f8fSGustavo F. Padovan } 39250a708f8fSGustavo F. Padovan } 3926aef89f21SSzymon Janc 3927aef89f21SSzymon Janc err = l2cap_send_srejframe(chan, tx_seq); 3928aef89f21SSzymon Janc if (err < 0) { 3929aef89f21SSzymon Janc l2cap_send_disconn_req(chan->conn, chan, -err); 3930aef89f21SSzymon Janc return err; 3931aef89f21SSzymon Janc } 39320a708f8fSGustavo F. Padovan } 39330a708f8fSGustavo F. Padovan } else { 3934836be934SAndrei Emeltchenko expected_tx_seq_offset = __seq_offset(chan, 3935836be934SAndrei Emeltchenko chan->expected_tx_seq, chan->buffer_seq); 39360a708f8fSGustavo F. Padovan 39370a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 39380a708f8fSGustavo F. Padovan if (tx_seq_offset < expected_tx_seq_offset) 39390a708f8fSGustavo F. Padovan goto drop; 39400a708f8fSGustavo F. Padovan 3941e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_SENT, &chan->conn_state); 39420a708f8fSGustavo F. Padovan 394349208c9cSGustavo F. Padovan BT_DBG("chan %p, Enter SREJ", chan); 39440a708f8fSGustavo F. Padovan 394539d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 394642e5c802SGustavo F. Padovan chan->buffer_seq_srej = chan->buffer_seq; 39470a708f8fSGustavo F. Padovan 3948f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->srej_q); 394942e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 39500a708f8fSGustavo F. Padovan 39510ef3ef0fSSzymon Janc /* Set P-bit only if there are some I-frames to ack. */ 39520ef3ef0fSSzymon Janc if (__clear_ack_timer(chan)) 3953e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_PBIT, &chan->conn_state); 39540a708f8fSGustavo F. Padovan 3955aef89f21SSzymon Janc err = l2cap_send_srejframe(chan, tx_seq); 3956aef89f21SSzymon Janc if (err < 0) { 3957aef89f21SSzymon Janc l2cap_send_disconn_req(chan->conn, chan, -err); 3958aef89f21SSzymon Janc return err; 3959aef89f21SSzymon Janc } 39600a708f8fSGustavo F. Padovan } 39610a708f8fSGustavo F. Padovan return 0; 39620a708f8fSGustavo F. Padovan 39630a708f8fSGustavo F. Padovan expected: 3964836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); 39650a708f8fSGustavo F. Padovan 3966e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 39670a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 39680a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 3969f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 39700a708f8fSGustavo F. Padovan return 0; 39710a708f8fSGustavo F. Padovan } 39720a708f8fSGustavo F. Padovan 397384084a31SMat Martineau err = l2cap_reassemble_sdu(chan, skb, rx_control); 3974836be934SAndrei Emeltchenko chan->buffer_seq = __next_seq(chan, chan->buffer_seq); 3975836be934SAndrei Emeltchenko 3976e328140fSMat Martineau if (err < 0) { 3977e328140fSMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 3978e328140fSMat Martineau return err; 3979e328140fSMat Martineau } 39800a708f8fSGustavo F. Padovan 398103f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control)) { 3982e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 3983525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 39840a708f8fSGustavo F. Padovan } 39850a708f8fSGustavo F. Padovan 39860a708f8fSGustavo F. Padovan 39876a026610SGustavo F. Padovan chan->num_acked = (chan->num_acked + 1) % num_to_ack; 39886a026610SGustavo F. Padovan if (chan->num_acked == num_to_ack - 1) 3989525cd185SGustavo F. Padovan l2cap_send_ack(chan); 39904d611e4dSGustavo F. Padovan else 39914d611e4dSGustavo F. Padovan __set_ack_timer(chan); 39920a708f8fSGustavo F. Padovan 39930a708f8fSGustavo F. Padovan return 0; 39940a708f8fSGustavo F. Padovan 39950a708f8fSGustavo F. Padovan drop: 39960a708f8fSGustavo F. Padovan kfree_skb(skb); 39970a708f8fSGustavo F. Padovan return 0; 39980a708f8fSGustavo F. Padovan } 39990a708f8fSGustavo F. Padovan 400088843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u32 rx_control) 40010a708f8fSGustavo F. Padovan { 400288843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, 40030b209faeSAndrei Emeltchenko __get_reqseq(chan, rx_control), rx_control); 40040a708f8fSGustavo F. Padovan 40050b209faeSAndrei Emeltchenko chan->expected_ack_seq = __get_reqseq(chan, rx_control); 400642e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 40070a708f8fSGustavo F. Padovan 4008e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) { 4009e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 4010e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 4011e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 40126a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 40131a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 40140a708f8fSGustavo F. Padovan 4015e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 4016525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 40170a708f8fSGustavo F. Padovan } else { 4018525cd185SGustavo F. Padovan l2cap_send_i_or_rr_or_rnr(chan); 40190a708f8fSGustavo F. Padovan } 40200a708f8fSGustavo F. Padovan 402103f6715dSAndrei Emeltchenko } else if (__is_ctrl_final(chan, rx_control)) { 4022e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 40230a708f8fSGustavo F. Padovan 4024e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 4025525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 40260a708f8fSGustavo F. Padovan 40270a708f8fSGustavo F. Padovan } else { 4028e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 40296a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 40301a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 40310a708f8fSGustavo F. Padovan 4032e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 4033e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) 4034525cd185SGustavo F. Padovan l2cap_send_ack(chan); 40350a708f8fSGustavo F. Padovan else 4036525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 40370a708f8fSGustavo F. Padovan } 40380a708f8fSGustavo F. Padovan } 40390a708f8fSGustavo F. Padovan 404088843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u32 rx_control) 40410a708f8fSGustavo F. Padovan { 40420b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 40430a708f8fSGustavo F. Padovan 404488843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); 40450a708f8fSGustavo F. Padovan 4046e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 40470a708f8fSGustavo F. Padovan 404842e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 404942e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 40500a708f8fSGustavo F. Padovan 405103f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control)) { 4052e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 4053525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 40540a708f8fSGustavo F. Padovan } else { 4055525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 40560a708f8fSGustavo F. Padovan 4057e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) 4058e2ab4353SGustavo F. Padovan set_bit(CONN_REJ_ACT, &chan->conn_state); 40590a708f8fSGustavo F. Padovan } 40600a708f8fSGustavo F. Padovan } 406188843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u32 rx_control) 40620a708f8fSGustavo F. Padovan { 40630b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 40640a708f8fSGustavo F. Padovan 406588843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); 40660a708f8fSGustavo F. Padovan 4067e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 40680a708f8fSGustavo F. Padovan 4069e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) { 407042e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 407142e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 40720a708f8fSGustavo F. Padovan 4073e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 4074525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 40750a708f8fSGustavo F. Padovan 4076525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 40770a708f8fSGustavo F. Padovan 4078e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) { 40796a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 4080e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_ACT, &chan->conn_state); 40810a708f8fSGustavo F. Padovan } 408203f6715dSAndrei Emeltchenko } else if (__is_ctrl_final(chan, rx_control)) { 4083e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_ACT, &chan->conn_state) && 40846a026610SGustavo F. Padovan chan->srej_save_reqseq == tx_seq) 4085e2ab4353SGustavo F. Padovan clear_bit(CONN_SREJ_ACT, &chan->conn_state); 40860a708f8fSGustavo F. Padovan else 4087525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 40880a708f8fSGustavo F. Padovan } else { 4089525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 4090e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) { 40916a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 4092e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_ACT, &chan->conn_state); 40930a708f8fSGustavo F. Padovan } 40940a708f8fSGustavo F. Padovan } 40950a708f8fSGustavo F. Padovan } 40960a708f8fSGustavo F. Padovan 409788843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u32 rx_control) 40980a708f8fSGustavo F. Padovan { 40990b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 41000a708f8fSGustavo F. Padovan 410188843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); 41020a708f8fSGustavo F. Padovan 4103e2ab4353SGustavo F. Padovan set_bit(CONN_REMOTE_BUSY, &chan->conn_state); 410442e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 410542e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 41060a708f8fSGustavo F. Padovan 4107e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) 4108e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 41090a708f8fSGustavo F. Padovan 4110e2ab4353SGustavo F. Padovan if (!test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 41111a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 4112e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) 4113525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); 41140a708f8fSGustavo F. Padovan return; 41150a708f8fSGustavo F. Padovan } 41160a708f8fSGustavo F. Padovan 4117e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) { 4118525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 4119ab784b73SAndrei Emeltchenko } else { 4120ab784b73SAndrei Emeltchenko rx_control = __set_ctrl_super(chan, L2CAP_SUPER_RR); 4121ab784b73SAndrei Emeltchenko l2cap_send_sframe(chan, rx_control); 4122ab784b73SAndrei Emeltchenko } 41230a708f8fSGustavo F. Padovan } 41240a708f8fSGustavo F. Padovan 412588843ab0SAndrei Emeltchenko static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb) 41260a708f8fSGustavo F. Padovan { 412788843ab0SAndrei Emeltchenko BT_DBG("chan %p rx_control 0x%8.8x len %d", chan, rx_control, skb->len); 41280a708f8fSGustavo F. Padovan 412903f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control) && 4130e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 41311a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 41326a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 41331a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 4134e2ab4353SGustavo F. Padovan clear_bit(CONN_WAIT_F, &chan->conn_state); 41350a708f8fSGustavo F. Padovan } 41360a708f8fSGustavo F. Padovan 4137ab784b73SAndrei Emeltchenko switch (__get_ctrl_super(chan, rx_control)) { 4138ab784b73SAndrei Emeltchenko case L2CAP_SUPER_RR: 4139525cd185SGustavo F. Padovan l2cap_data_channel_rrframe(chan, rx_control); 41400a708f8fSGustavo F. Padovan break; 41410a708f8fSGustavo F. Padovan 4142ab784b73SAndrei Emeltchenko case L2CAP_SUPER_REJ: 4143525cd185SGustavo F. Padovan l2cap_data_channel_rejframe(chan, rx_control); 41440a708f8fSGustavo F. Padovan break; 41450a708f8fSGustavo F. Padovan 4146ab784b73SAndrei Emeltchenko case L2CAP_SUPER_SREJ: 4147525cd185SGustavo F. Padovan l2cap_data_channel_srejframe(chan, rx_control); 41480a708f8fSGustavo F. Padovan break; 41490a708f8fSGustavo F. Padovan 4150ab784b73SAndrei Emeltchenko case L2CAP_SUPER_RNR: 4151525cd185SGustavo F. Padovan l2cap_data_channel_rnrframe(chan, rx_control); 41520a708f8fSGustavo F. Padovan break; 41530a708f8fSGustavo F. Padovan } 41540a708f8fSGustavo F. Padovan 41550a708f8fSGustavo F. Padovan kfree_skb(skb); 41560a708f8fSGustavo F. Padovan return 0; 41570a708f8fSGustavo F. Padovan } 41580a708f8fSGustavo F. Padovan 4159cad8f1d0SSzymon Janc static int l2cap_ertm_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) 41600a708f8fSGustavo F. Padovan { 416188843ab0SAndrei Emeltchenko u32 control; 41620b209faeSAndrei Emeltchenko u16 req_seq; 41630a708f8fSGustavo F. Padovan int len, next_tx_seq_offset, req_seq_offset; 41640a708f8fSGustavo F. Padovan 416588843ab0SAndrei Emeltchenko control = __get_control(chan, skb->data); 416688843ab0SAndrei Emeltchenko skb_pull(skb, __ctrl_size(chan)); 41670a708f8fSGustavo F. Padovan len = skb->len; 41680a708f8fSGustavo F. Padovan 41690a708f8fSGustavo F. Padovan /* 41700a708f8fSGustavo F. Padovan * We can just drop the corrupted I-frame here. 41710a708f8fSGustavo F. Padovan * Receiver will miss it and start proper recovery 41720a708f8fSGustavo F. Padovan * procedures and ask retransmission. 41730a708f8fSGustavo F. Padovan */ 417447d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 41750a708f8fSGustavo F. Padovan goto drop; 41760a708f8fSGustavo F. Padovan 4177793c2f1cSAndrei Emeltchenko if (__is_sar_start(chan, control) && !__is_sframe(chan, control)) 417803a51213SAndrei Emeltchenko len -= L2CAP_SDULEN_SIZE; 41790a708f8fSGustavo F. Padovan 418047d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 418103a51213SAndrei Emeltchenko len -= L2CAP_FCS_SIZE; 41820a708f8fSGustavo F. Padovan 418347d1ec61SGustavo F. Padovan if (len > chan->mps) { 41848c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 41850a708f8fSGustavo F. Padovan goto drop; 41860a708f8fSGustavo F. Padovan } 41870a708f8fSGustavo F. Padovan 41880b209faeSAndrei Emeltchenko req_seq = __get_reqseq(chan, control); 41890a708f8fSGustavo F. Padovan 4190836be934SAndrei Emeltchenko req_seq_offset = __seq_offset(chan, req_seq, chan->expected_ack_seq); 4191836be934SAndrei Emeltchenko 4192836be934SAndrei Emeltchenko next_tx_seq_offset = __seq_offset(chan, chan->next_tx_seq, 4193836be934SAndrei Emeltchenko chan->expected_ack_seq); 41940a708f8fSGustavo F. Padovan 41950a708f8fSGustavo F. Padovan /* check for invalid req-seq */ 41960a708f8fSGustavo F. Padovan if (req_seq_offset > next_tx_seq_offset) { 41978c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 41980a708f8fSGustavo F. Padovan goto drop; 41990a708f8fSGustavo F. Padovan } 42000a708f8fSGustavo F. Padovan 4201793c2f1cSAndrei Emeltchenko if (!__is_sframe(chan, control)) { 42020a708f8fSGustavo F. Padovan if (len < 0) { 42038c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 42040a708f8fSGustavo F. Padovan goto drop; 42050a708f8fSGustavo F. Padovan } 42060a708f8fSGustavo F. Padovan 4207525cd185SGustavo F. Padovan l2cap_data_channel_iframe(chan, control, skb); 42080a708f8fSGustavo F. Padovan } else { 42090a708f8fSGustavo F. Padovan if (len != 0) { 42100a708f8fSGustavo F. Padovan BT_ERR("%d", len); 42118c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 42120a708f8fSGustavo F. Padovan goto drop; 42130a708f8fSGustavo F. Padovan } 42140a708f8fSGustavo F. Padovan 4215525cd185SGustavo F. Padovan l2cap_data_channel_sframe(chan, control, skb); 42160a708f8fSGustavo F. Padovan } 42170a708f8fSGustavo F. Padovan 42180a708f8fSGustavo F. Padovan return 0; 42190a708f8fSGustavo F. Padovan 42200a708f8fSGustavo F. Padovan drop: 42210a708f8fSGustavo F. Padovan kfree_skb(skb); 42220a708f8fSGustavo F. Padovan return 0; 42230a708f8fSGustavo F. Padovan } 42240a708f8fSGustavo F. Padovan 42250a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) 42260a708f8fSGustavo F. Padovan { 422748454079SGustavo F. Padovan struct l2cap_chan *chan; 4228bf734843SDavid S. Miller struct sock *sk = NULL; 422988843ab0SAndrei Emeltchenko u32 control; 4230fb45de7dSAndrei Emeltchenko u16 tx_seq; 42310a708f8fSGustavo F. Padovan int len; 42320a708f8fSGustavo F. Padovan 4233baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 423448454079SGustavo F. Padovan if (!chan) { 42350a708f8fSGustavo F. Padovan BT_DBG("unknown cid 0x%4.4x", cid); 42360a708f8fSGustavo F. Padovan goto drop; 42370a708f8fSGustavo F. Padovan } 42380a708f8fSGustavo F. Padovan 423948454079SGustavo F. Padovan sk = chan->sk; 42403df91ea2SAndrei Emeltchenko lock_sock(sk); 42410a708f8fSGustavo F. Padovan 424249208c9cSGustavo F. Padovan BT_DBG("chan %p, len %d", chan, skb->len); 42430a708f8fSGustavo F. Padovan 424489bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 42450a708f8fSGustavo F. Padovan goto drop; 42460a708f8fSGustavo F. Padovan 42470c1bc5c6SGustavo F. Padovan switch (chan->mode) { 42480a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 42490a708f8fSGustavo F. Padovan /* If socket recv buffers overflows we drop data here 42500a708f8fSGustavo F. Padovan * which is *bad* because L2CAP has to be reliable. 42510a708f8fSGustavo F. Padovan * But we don't have any other choice. L2CAP doesn't 42520a708f8fSGustavo F. Padovan * provide flow control mechanism. */ 42530a708f8fSGustavo F. Padovan 42540c1bc5c6SGustavo F. Padovan if (chan->imtu < skb->len) 42550a708f8fSGustavo F. Padovan goto drop; 42560a708f8fSGustavo F. Padovan 425723070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 42580a708f8fSGustavo F. Padovan goto done; 42590a708f8fSGustavo F. Padovan break; 42600a708f8fSGustavo F. Padovan 42610a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 42625ef8cb9eSAndrei Emeltchenko l2cap_ertm_data_rcv(chan, skb); 42630a708f8fSGustavo F. Padovan 42640a708f8fSGustavo F. Padovan goto done; 42650a708f8fSGustavo F. Padovan 42660a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 426788843ab0SAndrei Emeltchenko control = __get_control(chan, skb->data); 426888843ab0SAndrei Emeltchenko skb_pull(skb, __ctrl_size(chan)); 42690a708f8fSGustavo F. Padovan len = skb->len; 42700a708f8fSGustavo F. Padovan 427147d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 42720a708f8fSGustavo F. Padovan goto drop; 42730a708f8fSGustavo F. Padovan 42747e0ef6eeSAndrei Emeltchenko if (__is_sar_start(chan, control)) 427503a51213SAndrei Emeltchenko len -= L2CAP_SDULEN_SIZE; 42760a708f8fSGustavo F. Padovan 427747d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 427803a51213SAndrei Emeltchenko len -= L2CAP_FCS_SIZE; 42790a708f8fSGustavo F. Padovan 4280793c2f1cSAndrei Emeltchenko if (len > chan->mps || len < 0 || __is_sframe(chan, control)) 42810a708f8fSGustavo F. Padovan goto drop; 42820a708f8fSGustavo F. Padovan 4283fb45de7dSAndrei Emeltchenko tx_seq = __get_txseq(chan, control); 42840a708f8fSGustavo F. Padovan 428584084a31SMat Martineau if (chan->expected_tx_seq != tx_seq) { 428684084a31SMat Martineau /* Frame(s) missing - must discard partial SDU */ 428784084a31SMat Martineau kfree_skb(chan->sdu); 428884084a31SMat Martineau chan->sdu = NULL; 428984084a31SMat Martineau chan->sdu_last_frag = NULL; 429084084a31SMat Martineau chan->sdu_len = 0; 429184084a31SMat Martineau 429284084a31SMat Martineau /* TODO: Notify userland of missing data */ 429384084a31SMat Martineau } 429484084a31SMat Martineau 4295836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, tx_seq); 42960a708f8fSGustavo F. Padovan 429784084a31SMat Martineau if (l2cap_reassemble_sdu(chan, skb, control) == -EMSGSIZE) 429884084a31SMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 42990a708f8fSGustavo F. Padovan 43000a708f8fSGustavo F. Padovan goto done; 43010a708f8fSGustavo F. Padovan 43020a708f8fSGustavo F. Padovan default: 43030c1bc5c6SGustavo F. Padovan BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode); 43040a708f8fSGustavo F. Padovan break; 43050a708f8fSGustavo F. Padovan } 43060a708f8fSGustavo F. Padovan 43070a708f8fSGustavo F. Padovan drop: 43080a708f8fSGustavo F. Padovan kfree_skb(skb); 43090a708f8fSGustavo F. Padovan 43100a708f8fSGustavo F. Padovan done: 43110a708f8fSGustavo F. Padovan if (sk) 4312aa2ac881SGustavo F. Padovan release_sock(sk); 43130a708f8fSGustavo F. Padovan 43140a708f8fSGustavo F. Padovan return 0; 43150a708f8fSGustavo F. Padovan } 43160a708f8fSGustavo F. Padovan 43170a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb) 43180a708f8fSGustavo F. Padovan { 43196dcae1eaSDavid S. Miller struct sock *sk = NULL; 432023691d75SGustavo F. Padovan struct l2cap_chan *chan; 43210a708f8fSGustavo F. Padovan 432223691d75SGustavo F. Padovan chan = l2cap_global_chan_by_psm(0, psm, conn->src); 432323691d75SGustavo F. Padovan if (!chan) 43240a708f8fSGustavo F. Padovan goto drop; 43250a708f8fSGustavo F. Padovan 432623691d75SGustavo F. Padovan sk = chan->sk; 432723691d75SGustavo F. Padovan 4328aa2ac881SGustavo F. Padovan lock_sock(sk); 43290a708f8fSGustavo F. Padovan 43300a708f8fSGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 43310a708f8fSGustavo F. Padovan 433289bc500eSGustavo F. Padovan if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) 43330a708f8fSGustavo F. Padovan goto drop; 43340a708f8fSGustavo F. Padovan 4335e13e21dcSVinicius Costa Gomes if (chan->imtu < skb->len) 43360a708f8fSGustavo F. Padovan goto drop; 43370a708f8fSGustavo F. Padovan 433823070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 43390a708f8fSGustavo F. Padovan goto done; 43400a708f8fSGustavo F. Padovan 43410a708f8fSGustavo F. Padovan drop: 43420a708f8fSGustavo F. Padovan kfree_skb(skb); 43430a708f8fSGustavo F. Padovan 43440a708f8fSGustavo F. Padovan done: 43450a708f8fSGustavo F. Padovan if (sk) 4346aa2ac881SGustavo F. Padovan release_sock(sk); 43470a708f8fSGustavo F. Padovan return 0; 43480a708f8fSGustavo F. Padovan } 43490a708f8fSGustavo F. Padovan 43509f69bda6SGustavo F. Padovan static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb) 43519f69bda6SGustavo F. Padovan { 43526dcae1eaSDavid S. Miller struct sock *sk = NULL; 435323691d75SGustavo F. Padovan struct l2cap_chan *chan; 43549f69bda6SGustavo F. Padovan 435523691d75SGustavo F. Padovan chan = l2cap_global_chan_by_scid(0, cid, conn->src); 435623691d75SGustavo F. Padovan if (!chan) 43579f69bda6SGustavo F. Padovan goto drop; 43589f69bda6SGustavo F. Padovan 435923691d75SGustavo F. Padovan sk = chan->sk; 436023691d75SGustavo F. Padovan 4361aa2ac881SGustavo F. Padovan lock_sock(sk); 43629f69bda6SGustavo F. Padovan 43639f69bda6SGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 43649f69bda6SGustavo F. Padovan 436589bc500eSGustavo F. Padovan if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) 43669f69bda6SGustavo F. Padovan goto drop; 43679f69bda6SGustavo F. Padovan 4368e13e21dcSVinicius Costa Gomes if (chan->imtu < skb->len) 43699f69bda6SGustavo F. Padovan goto drop; 43709f69bda6SGustavo F. Padovan 437123070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 43729f69bda6SGustavo F. Padovan goto done; 43739f69bda6SGustavo F. Padovan 43749f69bda6SGustavo F. Padovan drop: 43759f69bda6SGustavo F. Padovan kfree_skb(skb); 43769f69bda6SGustavo F. Padovan 43779f69bda6SGustavo F. Padovan done: 43789f69bda6SGustavo F. Padovan if (sk) 4379aa2ac881SGustavo F. Padovan release_sock(sk); 43809f69bda6SGustavo F. Padovan return 0; 43819f69bda6SGustavo F. Padovan } 43829f69bda6SGustavo F. Padovan 43830a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) 43840a708f8fSGustavo F. Padovan { 43850a708f8fSGustavo F. Padovan struct l2cap_hdr *lh = (void *) skb->data; 43860a708f8fSGustavo F. Padovan u16 cid, len; 43870a708f8fSGustavo F. Padovan __le16 psm; 43880a708f8fSGustavo F. Padovan 43890a708f8fSGustavo F. Padovan skb_pull(skb, L2CAP_HDR_SIZE); 43900a708f8fSGustavo F. Padovan cid = __le16_to_cpu(lh->cid); 43910a708f8fSGustavo F. Padovan len = __le16_to_cpu(lh->len); 43920a708f8fSGustavo F. Padovan 43930a708f8fSGustavo F. Padovan if (len != skb->len) { 43940a708f8fSGustavo F. Padovan kfree_skb(skb); 43950a708f8fSGustavo F. Padovan return; 43960a708f8fSGustavo F. Padovan } 43970a708f8fSGustavo F. Padovan 43980a708f8fSGustavo F. Padovan BT_DBG("len %d, cid 0x%4.4x", len, cid); 43990a708f8fSGustavo F. Padovan 44000a708f8fSGustavo F. Padovan switch (cid) { 44013300d9a9SClaudio Takahasi case L2CAP_CID_LE_SIGNALING: 44020a708f8fSGustavo F. Padovan case L2CAP_CID_SIGNALING: 44030a708f8fSGustavo F. Padovan l2cap_sig_channel(conn, skb); 44040a708f8fSGustavo F. Padovan break; 44050a708f8fSGustavo F. Padovan 44060a708f8fSGustavo F. Padovan case L2CAP_CID_CONN_LESS: 44070a708f8fSGustavo F. Padovan psm = get_unaligned_le16(skb->data); 44080a708f8fSGustavo F. Padovan skb_pull(skb, 2); 44090a708f8fSGustavo F. Padovan l2cap_conless_channel(conn, psm, skb); 44100a708f8fSGustavo F. Padovan break; 44110a708f8fSGustavo F. Padovan 44129f69bda6SGustavo F. Padovan case L2CAP_CID_LE_DATA: 44139f69bda6SGustavo F. Padovan l2cap_att_channel(conn, cid, skb); 44149f69bda6SGustavo F. Padovan break; 44159f69bda6SGustavo F. Padovan 4416b501d6a1SAnderson Briglia case L2CAP_CID_SMP: 4417b501d6a1SAnderson Briglia if (smp_sig_channel(conn, skb)) 4418b501d6a1SAnderson Briglia l2cap_conn_del(conn->hcon, EACCES); 4419b501d6a1SAnderson Briglia break; 4420b501d6a1SAnderson Briglia 44210a708f8fSGustavo F. Padovan default: 44220a708f8fSGustavo F. Padovan l2cap_data_channel(conn, cid, skb); 44230a708f8fSGustavo F. Padovan break; 44240a708f8fSGustavo F. Padovan } 44250a708f8fSGustavo F. Padovan } 44260a708f8fSGustavo F. Padovan 44270a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */ 44280a708f8fSGustavo F. Padovan 4429686ebf28SUlisses Furquim int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) 44300a708f8fSGustavo F. Padovan { 44310a708f8fSGustavo F. Padovan int exact = 0, lm1 = 0, lm2 = 0; 443223691d75SGustavo F. Padovan struct l2cap_chan *c; 44330a708f8fSGustavo F. Padovan 44340a708f8fSGustavo F. Padovan BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); 44350a708f8fSGustavo F. Padovan 44360a708f8fSGustavo F. Padovan /* Find listening sockets and check their link_mode */ 443723691d75SGustavo F. Padovan read_lock(&chan_list_lock); 443823691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 443923691d75SGustavo F. Padovan struct sock *sk = c->sk; 44404343478fSGustavo F. Padovan 444189bc500eSGustavo F. Padovan if (c->state != BT_LISTEN) 44420a708f8fSGustavo F. Padovan continue; 44430a708f8fSGustavo F. Padovan 44440a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { 44450a708f8fSGustavo F. Padovan lm1 |= HCI_LM_ACCEPT; 444643bd0f32SAndrei Emeltchenko if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 44470a708f8fSGustavo F. Padovan lm1 |= HCI_LM_MASTER; 44480a708f8fSGustavo F. Padovan exact++; 44490a708f8fSGustavo F. Padovan } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { 44500a708f8fSGustavo F. Padovan lm2 |= HCI_LM_ACCEPT; 445143bd0f32SAndrei Emeltchenko if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 44520a708f8fSGustavo F. Padovan lm2 |= HCI_LM_MASTER; 44530a708f8fSGustavo F. Padovan } 44540a708f8fSGustavo F. Padovan } 445523691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 44560a708f8fSGustavo F. Padovan 44570a708f8fSGustavo F. Padovan return exact ? lm1 : lm2; 44580a708f8fSGustavo F. Padovan } 44590a708f8fSGustavo F. Padovan 4460686ebf28SUlisses Furquim int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) 44610a708f8fSGustavo F. Padovan { 44620a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 44630a708f8fSGustavo F. Padovan 44640a708f8fSGustavo F. Padovan BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); 44650a708f8fSGustavo F. Padovan 44660a708f8fSGustavo F. Padovan if (!status) { 44670a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, status); 44680a708f8fSGustavo F. Padovan if (conn) 44690a708f8fSGustavo F. Padovan l2cap_conn_ready(conn); 44700a708f8fSGustavo F. Padovan } else 4471e175072fSJoe Perches l2cap_conn_del(hcon, bt_to_errno(status)); 44720a708f8fSGustavo F. Padovan 44730a708f8fSGustavo F. Padovan return 0; 44740a708f8fSGustavo F. Padovan } 44750a708f8fSGustavo F. Padovan 4476686ebf28SUlisses Furquim int l2cap_disconn_ind(struct hci_conn *hcon) 44770a708f8fSGustavo F. Padovan { 44780a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 44790a708f8fSGustavo F. Padovan 44800a708f8fSGustavo F. Padovan BT_DBG("hcon %p", hcon); 44810a708f8fSGustavo F. Padovan 4482686ebf28SUlisses Furquim if (!conn) 44839f5a0d7bSAndrei Emeltchenko return HCI_ERROR_REMOTE_USER_TERM; 44840a708f8fSGustavo F. Padovan return conn->disc_reason; 44850a708f8fSGustavo F. Padovan } 44860a708f8fSGustavo F. Padovan 4487686ebf28SUlisses Furquim int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) 44880a708f8fSGustavo F. Padovan { 44890a708f8fSGustavo F. Padovan BT_DBG("hcon %p reason %d", hcon, reason); 44900a708f8fSGustavo F. Padovan 4491e175072fSJoe Perches l2cap_conn_del(hcon, bt_to_errno(reason)); 44920a708f8fSGustavo F. Padovan return 0; 44930a708f8fSGustavo F. Padovan } 44940a708f8fSGustavo F. Padovan 44954343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) 44960a708f8fSGustavo F. Padovan { 4497715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) 44980a708f8fSGustavo F. Padovan return; 44990a708f8fSGustavo F. Padovan 45000a708f8fSGustavo F. Padovan if (encrypt == 0x00) { 45014343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) { 4502c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 4503b83ddfe2SAndrzej Kaczmarek __set_chan_timer(chan, 4504b83ddfe2SAndrzej Kaczmarek msecs_to_jiffies(L2CAP_ENC_TIMEOUT)); 45054343478fSGustavo F. Padovan } else if (chan->sec_level == BT_SECURITY_HIGH) 45060f852724SGustavo F. Padovan l2cap_chan_close(chan, ECONNREFUSED); 45070a708f8fSGustavo F. Padovan } else { 45084343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) 4509c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 45100a708f8fSGustavo F. Padovan } 45110a708f8fSGustavo F. Padovan } 45120a708f8fSGustavo F. Padovan 4513686ebf28SUlisses Furquim int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) 45140a708f8fSGustavo F. Padovan { 45150a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 451648454079SGustavo F. Padovan struct l2cap_chan *chan; 45170a708f8fSGustavo F. Padovan 45180a708f8fSGustavo F. Padovan if (!conn) 45190a708f8fSGustavo F. Padovan return 0; 45200a708f8fSGustavo F. Padovan 45210a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 45220a708f8fSGustavo F. Padovan 4523160dc6acSVinicius Costa Gomes if (hcon->type == LE_LINK) { 4524160dc6acSVinicius Costa Gomes smp_distribute_keys(conn, 0); 452517cd3f37SUlisses Furquim cancel_delayed_work(&conn->security_timer); 4526160dc6acSVinicius Costa Gomes } 4527160dc6acSVinicius Costa Gomes 45283df91ea2SAndrei Emeltchenko mutex_lock(&conn->chan_lock); 45290a708f8fSGustavo F. Padovan 45303df91ea2SAndrei Emeltchenko list_for_each_entry(chan, &conn->chan_l, list) { 453148454079SGustavo F. Padovan struct sock *sk = chan->sk; 4532baa7e1faSGustavo F. Padovan 45330a708f8fSGustavo F. Padovan bh_lock_sock(sk); 45340a708f8fSGustavo F. Padovan 4535f1cb9af5SVinicius Costa Gomes BT_DBG("chan->scid %d", chan->scid); 4536f1cb9af5SVinicius Costa Gomes 4537f1cb9af5SVinicius Costa Gomes if (chan->scid == L2CAP_CID_LE_DATA) { 4538f1cb9af5SVinicius Costa Gomes if (!status && encrypt) { 4539f1cb9af5SVinicius Costa Gomes chan->sec_level = hcon->sec_level; 4540cf4cd009SAndrei Emeltchenko l2cap_chan_ready(chan); 4541f1cb9af5SVinicius Costa Gomes } 4542f1cb9af5SVinicius Costa Gomes 4543f1cb9af5SVinicius Costa Gomes bh_unlock_sock(sk); 4544f1cb9af5SVinicius Costa Gomes continue; 4545f1cb9af5SVinicius Costa Gomes } 4546f1cb9af5SVinicius Costa Gomes 4547c1360a1cSGustavo F. Padovan if (test_bit(CONF_CONNECT_PEND, &chan->conf_state)) { 45480a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 45490a708f8fSGustavo F. Padovan continue; 45500a708f8fSGustavo F. Padovan } 45510a708f8fSGustavo F. Padovan 455289bc500eSGustavo F. Padovan if (!status && (chan->state == BT_CONNECTED || 455389bc500eSGustavo F. Padovan chan->state == BT_CONFIG)) { 45544343478fSGustavo F. Padovan l2cap_check_encryption(chan, encrypt); 45550a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 45560a708f8fSGustavo F. Padovan continue; 45570a708f8fSGustavo F. Padovan } 45580a708f8fSGustavo F. Padovan 455989bc500eSGustavo F. Padovan if (chan->state == BT_CONNECT) { 45600a708f8fSGustavo F. Padovan if (!status) { 45610a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 4562fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 4563fe4128e0SGustavo F. Padovan req.psm = chan->psm; 45640a708f8fSGustavo F. Padovan 4565fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 4566c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 45670a708f8fSGustavo F. Padovan 4568fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 45690a708f8fSGustavo F. Padovan L2CAP_CONN_REQ, sizeof(req), &req); 45700a708f8fSGustavo F. Padovan } else { 4571c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 4572b83ddfe2SAndrzej Kaczmarek __set_chan_timer(chan, 4573b83ddfe2SAndrzej Kaczmarek msecs_to_jiffies(L2CAP_DISC_TIMEOUT)); 45740a708f8fSGustavo F. Padovan } 457589bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT2) { 45760a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 4577df3c3931SJohan Hedberg __u16 res, stat; 45780a708f8fSGustavo F. Padovan 45790a708f8fSGustavo F. Padovan if (!status) { 4580df3c3931SJohan Hedberg if (bt_sk(sk)->defer_setup) { 4581df3c3931SJohan Hedberg struct sock *parent = bt_sk(sk)->parent; 4582df3c3931SJohan Hedberg res = L2CAP_CR_PEND; 4583df3c3931SJohan Hedberg stat = L2CAP_CS_AUTHOR_PEND; 458405e9a2f6SIlia Kolomisnky if (parent) 4585df3c3931SJohan Hedberg parent->sk_data_ready(parent, 0); 4586df3c3931SJohan Hedberg } else { 458789bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONFIG); 4588df3c3931SJohan Hedberg res = L2CAP_CR_SUCCESS; 4589df3c3931SJohan Hedberg stat = L2CAP_CS_NO_INFO; 4590df3c3931SJohan Hedberg } 45910a708f8fSGustavo F. Padovan } else { 459289bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_DISCONN); 4593b83ddfe2SAndrzej Kaczmarek __set_chan_timer(chan, 4594b83ddfe2SAndrzej Kaczmarek msecs_to_jiffies(L2CAP_DISC_TIMEOUT)); 4595df3c3931SJohan Hedberg res = L2CAP_CR_SEC_BLOCK; 4596df3c3931SJohan Hedberg stat = L2CAP_CS_NO_INFO; 45970a708f8fSGustavo F. Padovan } 45980a708f8fSGustavo F. Padovan 4599fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4600fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 4601df3c3931SJohan Hedberg rsp.result = cpu_to_le16(res); 4602df3c3931SJohan Hedberg rsp.status = cpu_to_le16(stat); 4603fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4604fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 46050a708f8fSGustavo F. Padovan } 46060a708f8fSGustavo F. Padovan 46070a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 46080a708f8fSGustavo F. Padovan } 46090a708f8fSGustavo F. Padovan 46103df91ea2SAndrei Emeltchenko mutex_unlock(&conn->chan_lock); 46110a708f8fSGustavo F. Padovan 46120a708f8fSGustavo F. Padovan return 0; 46130a708f8fSGustavo F. Padovan } 46140a708f8fSGustavo F. Padovan 4615686ebf28SUlisses Furquim int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) 46160a708f8fSGustavo F. Padovan { 46170a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 46180a708f8fSGustavo F. Padovan 46190a708f8fSGustavo F. Padovan if (!conn) 46200a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 46210a708f8fSGustavo F. Padovan 46220a708f8fSGustavo F. Padovan if (!conn) 46230a708f8fSGustavo F. Padovan goto drop; 46240a708f8fSGustavo F. Padovan 46250a708f8fSGustavo F. Padovan BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); 46260a708f8fSGustavo F. Padovan 46270a708f8fSGustavo F. Padovan if (!(flags & ACL_CONT)) { 46280a708f8fSGustavo F. Padovan struct l2cap_hdr *hdr; 462948454079SGustavo F. Padovan struct l2cap_chan *chan; 46300a708f8fSGustavo F. Padovan u16 cid; 46310a708f8fSGustavo F. Padovan int len; 46320a708f8fSGustavo F. Padovan 46330a708f8fSGustavo F. Padovan if (conn->rx_len) { 46340a708f8fSGustavo F. Padovan BT_ERR("Unexpected start frame (len %d)", skb->len); 46350a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 46360a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 46370a708f8fSGustavo F. Padovan conn->rx_len = 0; 46380a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46390a708f8fSGustavo F. Padovan } 46400a708f8fSGustavo F. Padovan 46410a708f8fSGustavo F. Padovan /* Start fragment always begin with Basic L2CAP header */ 46420a708f8fSGustavo F. Padovan if (skb->len < L2CAP_HDR_SIZE) { 46430a708f8fSGustavo F. Padovan BT_ERR("Frame is too short (len %d)", skb->len); 46440a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46450a708f8fSGustavo F. Padovan goto drop; 46460a708f8fSGustavo F. Padovan } 46470a708f8fSGustavo F. Padovan 46480a708f8fSGustavo F. Padovan hdr = (struct l2cap_hdr *) skb->data; 46490a708f8fSGustavo F. Padovan len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; 46500a708f8fSGustavo F. Padovan cid = __le16_to_cpu(hdr->cid); 46510a708f8fSGustavo F. Padovan 46520a708f8fSGustavo F. Padovan if (len == skb->len) { 46530a708f8fSGustavo F. Padovan /* Complete frame received */ 46540a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, skb); 46550a708f8fSGustavo F. Padovan return 0; 46560a708f8fSGustavo F. Padovan } 46570a708f8fSGustavo F. Padovan 46580a708f8fSGustavo F. Padovan BT_DBG("Start: total len %d, frag len %d", len, skb->len); 46590a708f8fSGustavo F. Padovan 46600a708f8fSGustavo F. Padovan if (skb->len > len) { 46610a708f8fSGustavo F. Padovan BT_ERR("Frame is too long (len %d, expected len %d)", 46620a708f8fSGustavo F. Padovan skb->len, len); 46630a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46640a708f8fSGustavo F. Padovan goto drop; 46650a708f8fSGustavo F. Padovan } 46660a708f8fSGustavo F. Padovan 4667baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 46680a708f8fSGustavo F. Padovan 466948454079SGustavo F. Padovan if (chan && chan->sk) { 467048454079SGustavo F. Padovan struct sock *sk = chan->sk; 46713df91ea2SAndrei Emeltchenko lock_sock(sk); 467248454079SGustavo F. Padovan 46730c1bc5c6SGustavo F. Padovan if (chan->imtu < len - L2CAP_HDR_SIZE) { 467448454079SGustavo F. Padovan BT_ERR("Frame exceeding recv MTU (len %d, " 467548454079SGustavo F. Padovan "MTU %d)", len, 46760c1bc5c6SGustavo F. Padovan chan->imtu); 4677aa2ac881SGustavo F. Padovan release_sock(sk); 46780a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46790a708f8fSGustavo F. Padovan goto drop; 46800a708f8fSGustavo F. Padovan } 4681aa2ac881SGustavo F. Padovan release_sock(sk); 468248454079SGustavo F. Padovan } 46830a708f8fSGustavo F. Padovan 46840a708f8fSGustavo F. Padovan /* Allocate skb for the complete frame (with header) */ 46850a708f8fSGustavo F. Padovan conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); 46860a708f8fSGustavo F. Padovan if (!conn->rx_skb) 46870a708f8fSGustavo F. Padovan goto drop; 46880a708f8fSGustavo F. Padovan 46890a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 46900a708f8fSGustavo F. Padovan skb->len); 46910a708f8fSGustavo F. Padovan conn->rx_len = len - skb->len; 46920a708f8fSGustavo F. Padovan } else { 46930a708f8fSGustavo F. Padovan BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); 46940a708f8fSGustavo F. Padovan 46950a708f8fSGustavo F. Padovan if (!conn->rx_len) { 46960a708f8fSGustavo F. Padovan BT_ERR("Unexpected continuation frame (len %d)", skb->len); 46970a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46980a708f8fSGustavo F. Padovan goto drop; 46990a708f8fSGustavo F. Padovan } 47000a708f8fSGustavo F. Padovan 47010a708f8fSGustavo F. Padovan if (skb->len > conn->rx_len) { 47020a708f8fSGustavo F. Padovan BT_ERR("Fragment is too long (len %d, expected %d)", 47030a708f8fSGustavo F. Padovan skb->len, conn->rx_len); 47040a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 47050a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 47060a708f8fSGustavo F. Padovan conn->rx_len = 0; 47070a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 47080a708f8fSGustavo F. Padovan goto drop; 47090a708f8fSGustavo F. Padovan } 47100a708f8fSGustavo F. Padovan 47110a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 47120a708f8fSGustavo F. Padovan skb->len); 47130a708f8fSGustavo F. Padovan conn->rx_len -= skb->len; 47140a708f8fSGustavo F. Padovan 47150a708f8fSGustavo F. Padovan if (!conn->rx_len) { 47160a708f8fSGustavo F. Padovan /* Complete frame received */ 47170a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, conn->rx_skb); 47180a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 47190a708f8fSGustavo F. Padovan } 47200a708f8fSGustavo F. Padovan } 47210a708f8fSGustavo F. Padovan 47220a708f8fSGustavo F. Padovan drop: 47230a708f8fSGustavo F. Padovan kfree_skb(skb); 47240a708f8fSGustavo F. Padovan return 0; 47250a708f8fSGustavo F. Padovan } 47260a708f8fSGustavo F. Padovan 47270a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p) 47280a708f8fSGustavo F. Padovan { 472923691d75SGustavo F. Padovan struct l2cap_chan *c; 47300a708f8fSGustavo F. Padovan 4731333055f2SGustavo F. Padovan read_lock(&chan_list_lock); 47320a708f8fSGustavo F. Padovan 473323691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 473423691d75SGustavo F. Padovan struct sock *sk = c->sk; 47350a708f8fSGustavo F. Padovan 4736903d343eSGustavo F. Padovan seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", 47370a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->src), 47380a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->dst), 473989bc500eSGustavo F. Padovan c->state, __le16_to_cpu(c->psm), 474023691d75SGustavo F. Padovan c->scid, c->dcid, c->imtu, c->omtu, 474123691d75SGustavo F. Padovan c->sec_level, c->mode); 47420a708f8fSGustavo F. Padovan } 47430a708f8fSGustavo F. Padovan 4744333055f2SGustavo F. Padovan read_unlock(&chan_list_lock); 47450a708f8fSGustavo F. Padovan 47460a708f8fSGustavo F. Padovan return 0; 47470a708f8fSGustavo F. Padovan } 47480a708f8fSGustavo F. Padovan 47490a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file) 47500a708f8fSGustavo F. Padovan { 47510a708f8fSGustavo F. Padovan return single_open(file, l2cap_debugfs_show, inode->i_private); 47520a708f8fSGustavo F. Padovan } 47530a708f8fSGustavo F. Padovan 47540a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = { 47550a708f8fSGustavo F. Padovan .open = l2cap_debugfs_open, 47560a708f8fSGustavo F. Padovan .read = seq_read, 47570a708f8fSGustavo F. Padovan .llseek = seq_lseek, 47580a708f8fSGustavo F. Padovan .release = single_release, 47590a708f8fSGustavo F. Padovan }; 47600a708f8fSGustavo F. Padovan 47610a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs; 47620a708f8fSGustavo F. Padovan 476364274518SGustavo F. Padovan int __init l2cap_init(void) 47640a708f8fSGustavo F. Padovan { 47650a708f8fSGustavo F. Padovan int err; 47660a708f8fSGustavo F. Padovan 4767bb58f747SGustavo F. Padovan err = l2cap_init_sockets(); 47680a708f8fSGustavo F. Padovan if (err < 0) 47690a708f8fSGustavo F. Padovan return err; 47700a708f8fSGustavo F. Padovan 47710a708f8fSGustavo F. Padovan if (bt_debugfs) { 47720a708f8fSGustavo F. Padovan l2cap_debugfs = debugfs_create_file("l2cap", 0444, 47730a708f8fSGustavo F. Padovan bt_debugfs, NULL, &l2cap_debugfs_fops); 47740a708f8fSGustavo F. Padovan if (!l2cap_debugfs) 47750a708f8fSGustavo F. Padovan BT_ERR("Failed to create L2CAP debug file"); 47760a708f8fSGustavo F. Padovan } 47770a708f8fSGustavo F. Padovan 47780a708f8fSGustavo F. Padovan return 0; 47790a708f8fSGustavo F. Padovan } 47800a708f8fSGustavo F. Padovan 478164274518SGustavo F. Padovan void l2cap_exit(void) 47820a708f8fSGustavo F. Padovan { 47830a708f8fSGustavo F. Padovan debugfs_remove(l2cap_debugfs); 4784bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 47850a708f8fSGustavo F. Padovan } 47860a708f8fSGustavo F. Padovan 47870a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644); 47880a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); 4789