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. 60a708f8fSGustavo F. Padovan 70a708f8fSGustavo F. Padovan Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 80a708f8fSGustavo F. Padovan 90a708f8fSGustavo F. Padovan This program is free software; you can redistribute it and/or modify 100a708f8fSGustavo F. Padovan it under the terms of the GNU General Public License version 2 as 110a708f8fSGustavo F. Padovan published by the Free Software Foundation; 120a708f8fSGustavo F. Padovan 130a708f8fSGustavo F. Padovan THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 140a708f8fSGustavo F. Padovan OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 150a708f8fSGustavo F. Padovan FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 160a708f8fSGustavo F. Padovan IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 170a708f8fSGustavo F. Padovan CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 180a708f8fSGustavo F. Padovan WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 190a708f8fSGustavo F. Padovan ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 200a708f8fSGustavo F. Padovan OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 210a708f8fSGustavo F. Padovan 220a708f8fSGustavo F. Padovan ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 230a708f8fSGustavo F. Padovan COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 240a708f8fSGustavo F. Padovan SOFTWARE IS DISCLAIMED. 250a708f8fSGustavo F. Padovan */ 260a708f8fSGustavo F. Padovan 27bb58f747SGustavo F. Padovan /* Bluetooth L2CAP core. */ 280a708f8fSGustavo F. Padovan 290a708f8fSGustavo F. Padovan #include <linux/module.h> 300a708f8fSGustavo F. Padovan 310a708f8fSGustavo F. Padovan #include <linux/types.h> 320a708f8fSGustavo F. Padovan #include <linux/capability.h> 330a708f8fSGustavo F. Padovan #include <linux/errno.h> 340a708f8fSGustavo F. Padovan #include <linux/kernel.h> 350a708f8fSGustavo F. Padovan #include <linux/sched.h> 360a708f8fSGustavo F. Padovan #include <linux/slab.h> 370a708f8fSGustavo F. Padovan #include <linux/poll.h> 380a708f8fSGustavo F. Padovan #include <linux/fcntl.h> 390a708f8fSGustavo F. Padovan #include <linux/init.h> 400a708f8fSGustavo F. Padovan #include <linux/interrupt.h> 410a708f8fSGustavo F. Padovan #include <linux/socket.h> 420a708f8fSGustavo F. Padovan #include <linux/skbuff.h> 430a708f8fSGustavo F. Padovan #include <linux/list.h> 440a708f8fSGustavo F. Padovan #include <linux/device.h> 450a708f8fSGustavo F. Padovan #include <linux/debugfs.h> 460a708f8fSGustavo F. Padovan #include <linux/seq_file.h> 470a708f8fSGustavo F. Padovan #include <linux/uaccess.h> 480a708f8fSGustavo F. Padovan #include <linux/crc16.h> 490a708f8fSGustavo F. Padovan #include <net/sock.h> 500a708f8fSGustavo F. Padovan 510a708f8fSGustavo F. Padovan #include <asm/system.h> 520a708f8fSGustavo F. Padovan #include <asm/unaligned.h> 530a708f8fSGustavo F. Padovan 540a708f8fSGustavo F. Padovan #include <net/bluetooth/bluetooth.h> 550a708f8fSGustavo F. Padovan #include <net/bluetooth/hci_core.h> 560a708f8fSGustavo F. Padovan #include <net/bluetooth/l2cap.h> 57b501d6a1SAnderson Briglia #include <net/bluetooth/smp.h> 580a708f8fSGustavo F. Padovan 59bb58f747SGustavo F. Padovan int disable_ertm; 60a5fd6f30SAndrei Emeltchenko int enable_hs; 610a708f8fSGustavo F. Padovan 620a708f8fSGustavo F. Padovan static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN; 630a708f8fSGustavo F. Padovan static u8 l2cap_fixed_chan[8] = { 0x02, }; 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 static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb); 770a708f8fSGustavo F. Padovan 780a708f8fSGustavo F. Padovan /* ---- L2CAP channels ---- */ 7971ba0e56SGustavo F. Padovan 8071ba0e56SGustavo F. Padovan static inline void chan_hold(struct l2cap_chan *c) 8171ba0e56SGustavo F. Padovan { 8271ba0e56SGustavo F. Padovan atomic_inc(&c->refcnt); 8371ba0e56SGustavo F. Padovan } 8471ba0e56SGustavo F. Padovan 8571ba0e56SGustavo F. Padovan static inline void chan_put(struct l2cap_chan *c) 8671ba0e56SGustavo F. Padovan { 8771ba0e56SGustavo F. Padovan if (atomic_dec_and_test(&c->refcnt)) 8871ba0e56SGustavo F. Padovan kfree(c); 8971ba0e56SGustavo F. Padovan } 9071ba0e56SGustavo F. Padovan 91baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid) 920a708f8fSGustavo F. Padovan { 9348454079SGustavo F. Padovan struct l2cap_chan *c; 94baa7e1faSGustavo F. Padovan 95baa7e1faSGustavo F. Padovan list_for_each_entry(c, &conn->chan_l, list) { 96fe4128e0SGustavo F. Padovan if (c->dcid == cid) 9748454079SGustavo F. Padovan return c; 980a708f8fSGustavo F. Padovan } 99baa7e1faSGustavo F. Padovan return NULL; 100baa7e1faSGustavo F. Padovan 101baa7e1faSGustavo F. Padovan } 1020a708f8fSGustavo F. Padovan 103baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) 1040a708f8fSGustavo F. Padovan { 10548454079SGustavo F. Padovan struct l2cap_chan *c; 106baa7e1faSGustavo F. Padovan 107baa7e1faSGustavo F. Padovan list_for_each_entry(c, &conn->chan_l, list) { 108fe4128e0SGustavo F. Padovan if (c->scid == cid) 10948454079SGustavo F. Padovan return c; 1100a708f8fSGustavo F. Padovan } 111baa7e1faSGustavo F. Padovan return NULL; 112baa7e1faSGustavo F. Padovan } 1130a708f8fSGustavo F. Padovan 1140a708f8fSGustavo F. Padovan /* Find channel with given SCID. 1150a708f8fSGustavo F. Padovan * Returns locked socket */ 116baa7e1faSGustavo F. Padovan static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) 1170a708f8fSGustavo F. Padovan { 11848454079SGustavo F. Padovan struct l2cap_chan *c; 119baa7e1faSGustavo F. Padovan 120baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 121baa7e1faSGustavo F. Padovan c = __l2cap_get_chan_by_scid(conn, cid); 12248454079SGustavo F. Padovan if (c) 12348454079SGustavo F. Padovan bh_lock_sock(c->sk); 124baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 12548454079SGustavo F. Padovan return c; 1260a708f8fSGustavo F. Padovan } 1270a708f8fSGustavo F. Padovan 128baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) 1290a708f8fSGustavo F. Padovan { 13048454079SGustavo F. Padovan struct l2cap_chan *c; 131baa7e1faSGustavo F. Padovan 132baa7e1faSGustavo F. Padovan list_for_each_entry(c, &conn->chan_l, list) { 133fc7f8a7eSGustavo F. Padovan if (c->ident == ident) 13448454079SGustavo F. Padovan return c; 1350a708f8fSGustavo F. Padovan } 136baa7e1faSGustavo F. Padovan return NULL; 137baa7e1faSGustavo F. Padovan } 1380a708f8fSGustavo F. Padovan 139baa7e1faSGustavo F. Padovan static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) 1400a708f8fSGustavo F. Padovan { 14148454079SGustavo F. Padovan struct l2cap_chan *c; 142baa7e1faSGustavo F. Padovan 143baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 144baa7e1faSGustavo F. Padovan c = __l2cap_get_chan_by_ident(conn, ident); 14548454079SGustavo F. Padovan if (c) 14648454079SGustavo F. Padovan bh_lock_sock(c->sk); 147baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 14848454079SGustavo F. Padovan return c; 1490a708f8fSGustavo F. Padovan } 1500a708f8fSGustavo F. Padovan 15123691d75SGustavo F. Padovan static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src) 1529e4425ffSGustavo F. Padovan { 15323691d75SGustavo F. Padovan struct l2cap_chan *c; 1549e4425ffSGustavo F. Padovan 15523691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 15623691d75SGustavo F. Padovan if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src)) 1579e4425ffSGustavo F. Padovan goto found; 1589e4425ffSGustavo F. Padovan } 1599e4425ffSGustavo F. Padovan 16023691d75SGustavo F. Padovan c = NULL; 1619e4425ffSGustavo F. Padovan found: 16223691d75SGustavo F. Padovan return c; 1639e4425ffSGustavo F. Padovan } 1649e4425ffSGustavo F. Padovan 1659e4425ffSGustavo F. Padovan int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) 1669e4425ffSGustavo F. Padovan { 16773b2ec18SGustavo F. Padovan int err; 16873b2ec18SGustavo F. Padovan 16923691d75SGustavo F. Padovan write_lock_bh(&chan_list_lock); 1709e4425ffSGustavo F. Padovan 17123691d75SGustavo F. Padovan if (psm && __l2cap_global_chan_by_addr(psm, src)) { 17273b2ec18SGustavo F. Padovan err = -EADDRINUSE; 17373b2ec18SGustavo F. Padovan goto done; 1749e4425ffSGustavo F. Padovan } 1759e4425ffSGustavo F. Padovan 17673b2ec18SGustavo F. Padovan if (psm) { 1779e4425ffSGustavo F. Padovan chan->psm = psm; 1789e4425ffSGustavo F. Padovan chan->sport = psm; 17973b2ec18SGustavo F. Padovan err = 0; 18073b2ec18SGustavo F. Padovan } else { 18173b2ec18SGustavo F. Padovan u16 p; 1829e4425ffSGustavo F. Padovan 18373b2ec18SGustavo F. Padovan err = -EINVAL; 18473b2ec18SGustavo F. Padovan for (p = 0x1001; p < 0x1100; p += 2) 18523691d75SGustavo F. Padovan if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { 18673b2ec18SGustavo F. Padovan chan->psm = cpu_to_le16(p); 18773b2ec18SGustavo F. Padovan chan->sport = cpu_to_le16(p); 18873b2ec18SGustavo F. Padovan err = 0; 18973b2ec18SGustavo F. Padovan break; 19073b2ec18SGustavo F. Padovan } 19173b2ec18SGustavo F. Padovan } 19273b2ec18SGustavo F. Padovan 19373b2ec18SGustavo F. Padovan done: 19423691d75SGustavo F. Padovan write_unlock_bh(&chan_list_lock); 19573b2ec18SGustavo F. Padovan return err; 1969e4425ffSGustavo F. Padovan } 1979e4425ffSGustavo F. Padovan 1989e4425ffSGustavo F. Padovan int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) 1999e4425ffSGustavo F. Padovan { 20023691d75SGustavo F. Padovan write_lock_bh(&chan_list_lock); 2019e4425ffSGustavo F. Padovan 2029e4425ffSGustavo F. Padovan chan->scid = scid; 2039e4425ffSGustavo F. Padovan 20423691d75SGustavo F. Padovan write_unlock_bh(&chan_list_lock); 2059e4425ffSGustavo F. Padovan 2069e4425ffSGustavo F. Padovan return 0; 2079e4425ffSGustavo F. Padovan } 2089e4425ffSGustavo F. Padovan 209baa7e1faSGustavo F. Padovan static u16 l2cap_alloc_cid(struct l2cap_conn *conn) 2100a708f8fSGustavo F. Padovan { 2110a708f8fSGustavo F. Padovan u16 cid = L2CAP_CID_DYN_START; 2120a708f8fSGustavo F. Padovan 2130a708f8fSGustavo F. Padovan for (; cid < L2CAP_CID_DYN_END; cid++) { 214baa7e1faSGustavo F. Padovan if (!__l2cap_get_chan_by_scid(conn, cid)) 2150a708f8fSGustavo F. Padovan return cid; 2160a708f8fSGustavo F. Padovan } 2170a708f8fSGustavo F. Padovan 2180a708f8fSGustavo F. Padovan return 0; 2190a708f8fSGustavo F. Padovan } 2200a708f8fSGustavo F. Padovan 221c9b66675SGustavo F. Padovan static void l2cap_set_timer(struct l2cap_chan *chan, struct timer_list *timer, long timeout) 222ab07801dSGustavo F. Padovan { 22389bc500eSGustavo F. Padovan BT_DBG("chan %p state %d timeout %ld", chan->sk, chan->state, timeout); 22489bc500eSGustavo F. Padovan 225942ecc9cSMat Martineau if (!mod_timer(timer, jiffies + msecs_to_jiffies(timeout))) 22671ba0e56SGustavo F. Padovan chan_hold(chan); 227ab07801dSGustavo F. Padovan } 228ab07801dSGustavo F. Padovan 229c9b66675SGustavo F. Padovan static void l2cap_clear_timer(struct l2cap_chan *chan, struct timer_list *timer) 230ab07801dSGustavo F. Padovan { 23189bc500eSGustavo F. Padovan BT_DBG("chan %p state %d", chan, chan->state); 232ab07801dSGustavo F. Padovan 233c9b66675SGustavo F. Padovan if (timer_pending(timer) && del_timer(timer)) 23471ba0e56SGustavo F. Padovan chan_put(chan); 235ab07801dSGustavo F. Padovan } 236ab07801dSGustavo F. Padovan 23789bc500eSGustavo F. Padovan static void l2cap_state_change(struct l2cap_chan *chan, int state) 23889bc500eSGustavo F. Padovan { 23989bc500eSGustavo F. Padovan chan->state = state; 24089bc500eSGustavo F. Padovan chan->ops->state_change(chan->data, state); 24189bc500eSGustavo F. Padovan } 24289bc500eSGustavo F. Padovan 243ab07801dSGustavo F. Padovan static void l2cap_chan_timeout(unsigned long arg) 244ab07801dSGustavo F. Padovan { 245ab07801dSGustavo F. Padovan struct l2cap_chan *chan = (struct l2cap_chan *) arg; 246ab07801dSGustavo F. Padovan struct sock *sk = chan->sk; 247ab07801dSGustavo F. Padovan int reason; 248ab07801dSGustavo F. Padovan 24989bc500eSGustavo F. Padovan BT_DBG("chan %p state %d", chan, chan->state); 250ab07801dSGustavo F. Padovan 251ab07801dSGustavo F. Padovan bh_lock_sock(sk); 252ab07801dSGustavo F. Padovan 253ab07801dSGustavo F. Padovan if (sock_owned_by_user(sk)) { 254ab07801dSGustavo F. Padovan /* sk is owned by user. Try again later */ 255c9b66675SGustavo F. Padovan __set_chan_timer(chan, HZ / 5); 256ab07801dSGustavo F. Padovan bh_unlock_sock(sk); 25771ba0e56SGustavo F. Padovan chan_put(chan); 258ab07801dSGustavo F. Padovan return; 259ab07801dSGustavo F. Padovan } 260ab07801dSGustavo F. Padovan 26189bc500eSGustavo F. Padovan if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) 262ab07801dSGustavo F. Padovan reason = ECONNREFUSED; 26389bc500eSGustavo F. Padovan else if (chan->state == BT_CONNECT && 264ab07801dSGustavo F. Padovan chan->sec_level != BT_SECURITY_SDP) 265ab07801dSGustavo F. Padovan reason = ECONNREFUSED; 266ab07801dSGustavo F. Padovan else 267ab07801dSGustavo F. Padovan reason = ETIMEDOUT; 268ab07801dSGustavo F. Padovan 2690f852724SGustavo F. Padovan l2cap_chan_close(chan, reason); 270ab07801dSGustavo F. Padovan 271ab07801dSGustavo F. Padovan bh_unlock_sock(sk); 272ab07801dSGustavo F. Padovan 273ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 27471ba0e56SGustavo F. Padovan chan_put(chan); 275ab07801dSGustavo F. Padovan } 276ab07801dSGustavo F. Padovan 27723691d75SGustavo F. Padovan struct l2cap_chan *l2cap_chan_create(struct sock *sk) 2780a708f8fSGustavo F. Padovan { 27948454079SGustavo F. Padovan struct l2cap_chan *chan; 2800a708f8fSGustavo F. Padovan 28148454079SGustavo F. Padovan chan = kzalloc(sizeof(*chan), GFP_ATOMIC); 28248454079SGustavo F. Padovan if (!chan) 28348454079SGustavo F. Padovan return NULL; 2840a708f8fSGustavo F. Padovan 28548454079SGustavo F. Padovan chan->sk = sk; 28648454079SGustavo F. Padovan 28723691d75SGustavo F. Padovan write_lock_bh(&chan_list_lock); 28823691d75SGustavo F. Padovan list_add(&chan->global_l, &chan_list); 28923691d75SGustavo F. Padovan write_unlock_bh(&chan_list_lock); 29023691d75SGustavo F. Padovan 291ab07801dSGustavo F. Padovan setup_timer(&chan->chan_timer, l2cap_chan_timeout, (unsigned long) chan); 292ab07801dSGustavo F. Padovan 29389bc500eSGustavo F. Padovan chan->state = BT_OPEN; 29489bc500eSGustavo F. Padovan 29571ba0e56SGustavo F. Padovan atomic_set(&chan->refcnt, 1); 29671ba0e56SGustavo F. Padovan 29748454079SGustavo F. Padovan return chan; 2980a708f8fSGustavo F. Padovan } 2990a708f8fSGustavo F. Padovan 30023691d75SGustavo F. Padovan void l2cap_chan_destroy(struct l2cap_chan *chan) 3016ff5abbfSGustavo F. Padovan { 30223691d75SGustavo F. Padovan write_lock_bh(&chan_list_lock); 30323691d75SGustavo F. Padovan list_del(&chan->global_l); 30423691d75SGustavo F. Padovan write_unlock_bh(&chan_list_lock); 30523691d75SGustavo F. Padovan 30671ba0e56SGustavo F. Padovan chan_put(chan); 3076ff5abbfSGustavo F. Padovan } 3086ff5abbfSGustavo F. Padovan 30948454079SGustavo F. Padovan static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 3100a708f8fSGustavo F. Padovan { 3110a708f8fSGustavo F. Padovan BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, 312fe4128e0SGustavo F. Padovan chan->psm, chan->dcid); 3130a708f8fSGustavo F. Padovan 3140a708f8fSGustavo F. Padovan conn->disc_reason = 0x13; 3150a708f8fSGustavo F. Padovan 3168c1d787bSGustavo F. Padovan chan->conn = conn; 3170a708f8fSGustavo F. Padovan 318715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { 319b62f328bSVille Tervo if (conn->hcon->type == LE_LINK) { 320b62f328bSVille Tervo /* LE connection */ 3210c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_LE_DEFAULT_MTU; 322fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_LE_DATA; 323fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_LE_DATA; 324b62f328bSVille Tervo } else { 3250a708f8fSGustavo F. Padovan /* Alloc CID for connection-oriented socket */ 326fe4128e0SGustavo F. Padovan chan->scid = l2cap_alloc_cid(conn); 3270c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 328b62f328bSVille Tervo } 329715ec005SGustavo F. Padovan } else if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { 3300a708f8fSGustavo F. Padovan /* Connectionless socket */ 331fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_CONN_LESS; 332fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_CONN_LESS; 3330c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 3340a708f8fSGustavo F. Padovan } else { 3350a708f8fSGustavo F. Padovan /* Raw socket can send/recv signalling messages only */ 336fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_SIGNALING; 337fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_SIGNALING; 3380c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 3390a708f8fSGustavo F. Padovan } 3400a708f8fSGustavo F. Padovan 34171ba0e56SGustavo F. Padovan chan_hold(chan); 342baa7e1faSGustavo F. Padovan 343baa7e1faSGustavo F. Padovan list_add(&chan->list, &conn->chan_l); 3440a708f8fSGustavo F. Padovan } 3450a708f8fSGustavo F. Padovan 3460a708f8fSGustavo F. Padovan /* Delete channel. 3470a708f8fSGustavo F. Padovan * Must be called on the locked socket. */ 3484519de9aSGustavo F. Padovan static void l2cap_chan_del(struct l2cap_chan *chan, int err) 3490a708f8fSGustavo F. Padovan { 35048454079SGustavo F. Padovan struct sock *sk = chan->sk; 3518c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 3520a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 3530a708f8fSGustavo F. Padovan 354c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 3550a708f8fSGustavo F. Padovan 35649208c9cSGustavo F. Padovan BT_DBG("chan %p, conn %p, err %d", chan, conn, err); 3570a708f8fSGustavo F. Padovan 3580a708f8fSGustavo F. Padovan if (conn) { 359baa7e1faSGustavo F. Padovan /* Delete from channel list */ 360baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 361baa7e1faSGustavo F. Padovan list_del(&chan->list); 362baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 36371ba0e56SGustavo F. Padovan chan_put(chan); 364baa7e1faSGustavo F. Padovan 3658c1d787bSGustavo F. Padovan chan->conn = NULL; 3660a708f8fSGustavo F. Padovan hci_conn_put(conn->hcon); 3670a708f8fSGustavo F. Padovan } 3680a708f8fSGustavo F. Padovan 36989bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CLOSED); 3700a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 3710a708f8fSGustavo F. Padovan 3720a708f8fSGustavo F. Padovan if (err) 3730a708f8fSGustavo F. Padovan sk->sk_err = err; 3740a708f8fSGustavo F. Padovan 3750a708f8fSGustavo F. Padovan if (parent) { 3760a708f8fSGustavo F. Padovan bt_accept_unlink(sk); 3770a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 3780a708f8fSGustavo F. Padovan } else 3790a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 3800a708f8fSGustavo F. Padovan 381c1360a1cSGustavo F. Padovan if (!(test_bit(CONF_OUTPUT_DONE, &chan->conf_state) && 382c1360a1cSGustavo F. Padovan test_bit(CONF_INPUT_DONE, &chan->conf_state))) 3836ff5abbfSGustavo F. Padovan return; 3842ead70b8SGustavo F. Padovan 38558d35f87SGustavo F. Padovan skb_queue_purge(&chan->tx_q); 3860a708f8fSGustavo F. Padovan 3870c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 3880a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 3890a708f8fSGustavo F. Padovan 3901a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 3911a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 3921a09bcb9SGustavo F. Padovan __clear_ack_timer(chan); 3930a708f8fSGustavo F. Padovan 394f1c6775bSGustavo F. Padovan skb_queue_purge(&chan->srej_q); 3950a708f8fSGustavo F. Padovan 39639d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 3970a708f8fSGustavo F. Padovan list_del(&l->list); 3980a708f8fSGustavo F. Padovan kfree(l); 3990a708f8fSGustavo F. Padovan } 4000a708f8fSGustavo F. Padovan } 4010a708f8fSGustavo F. Padovan } 4020a708f8fSGustavo F. Padovan 4034519de9aSGustavo F. Padovan static void l2cap_chan_cleanup_listen(struct sock *parent) 4044519de9aSGustavo F. Padovan { 4054519de9aSGustavo F. Padovan struct sock *sk; 4064519de9aSGustavo F. Padovan 4074519de9aSGustavo F. Padovan BT_DBG("parent %p", parent); 4084519de9aSGustavo F. Padovan 4094519de9aSGustavo F. Padovan /* Close not yet accepted channels */ 4100f852724SGustavo F. Padovan while ((sk = bt_accept_dequeue(parent, NULL))) { 411ba3bd0eeSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 412c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 4130f852724SGustavo F. Padovan lock_sock(sk); 414ba3bd0eeSGustavo F. Padovan l2cap_chan_close(chan, ECONNRESET); 4150f852724SGustavo F. Padovan release_sock(sk); 416ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 4170f852724SGustavo F. Padovan } 4184519de9aSGustavo F. Padovan } 4194519de9aSGustavo F. Padovan 4200f852724SGustavo F. Padovan void l2cap_chan_close(struct l2cap_chan *chan, int reason) 4214519de9aSGustavo F. Padovan { 4224519de9aSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 4234519de9aSGustavo F. Padovan struct sock *sk = chan->sk; 4244519de9aSGustavo F. Padovan 42589bc500eSGustavo F. Padovan BT_DBG("chan %p state %d socket %p", chan, chan->state, sk->sk_socket); 4264519de9aSGustavo F. Padovan 42789bc500eSGustavo F. Padovan switch (chan->state) { 4284519de9aSGustavo F. Padovan case BT_LISTEN: 4294519de9aSGustavo F. Padovan l2cap_chan_cleanup_listen(sk); 43089bc500eSGustavo F. Padovan 43189bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CLOSED); 43289bc500eSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 4334519de9aSGustavo F. Padovan break; 4344519de9aSGustavo F. Padovan 4354519de9aSGustavo F. Padovan case BT_CONNECTED: 4364519de9aSGustavo F. Padovan case BT_CONFIG: 437715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 4384519de9aSGustavo F. Padovan conn->hcon->type == ACL_LINK) { 439c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 440c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 4414519de9aSGustavo F. Padovan l2cap_send_disconn_req(conn, chan, reason); 4424519de9aSGustavo F. Padovan } else 4434519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4444519de9aSGustavo F. Padovan break; 4454519de9aSGustavo F. Padovan 4464519de9aSGustavo F. Padovan case BT_CONNECT2: 447715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 4484519de9aSGustavo F. Padovan conn->hcon->type == ACL_LINK) { 4494519de9aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 4504519de9aSGustavo F. Padovan __u16 result; 4514519de9aSGustavo F. Padovan 4524519de9aSGustavo F. Padovan if (bt_sk(sk)->defer_setup) 4534519de9aSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 4544519de9aSGustavo F. Padovan else 4554519de9aSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 45689bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_DISCONN); 4574519de9aSGustavo F. Padovan 4584519de9aSGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4594519de9aSGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 4604519de9aSGustavo F. Padovan rsp.result = cpu_to_le16(result); 4614519de9aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 4624519de9aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4634519de9aSGustavo F. Padovan sizeof(rsp), &rsp); 4644519de9aSGustavo F. Padovan } 4654519de9aSGustavo F. Padovan 4664519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4674519de9aSGustavo F. Padovan break; 4684519de9aSGustavo F. Padovan 4694519de9aSGustavo F. Padovan case BT_CONNECT: 4704519de9aSGustavo F. Padovan case BT_DISCONN: 4714519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4724519de9aSGustavo F. Padovan break; 4734519de9aSGustavo F. Padovan 4744519de9aSGustavo F. Padovan default: 4754519de9aSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 4764519de9aSGustavo F. Padovan break; 4774519de9aSGustavo F. Padovan } 4784519de9aSGustavo F. Padovan } 4794519de9aSGustavo F. Padovan 4804343478fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) 4810a708f8fSGustavo F. Padovan { 482715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_RAW) { 4834343478fSGustavo F. Padovan switch (chan->sec_level) { 4840a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 4850a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING_MITM; 4860a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 4870a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING; 4880a708f8fSGustavo F. Padovan default: 4890a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 4900a708f8fSGustavo F. Padovan } 491fe4128e0SGustavo F. Padovan } else if (chan->psm == cpu_to_le16(0x0001)) { 4924343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_LOW) 4934343478fSGustavo F. Padovan chan->sec_level = BT_SECURITY_SDP; 4940a708f8fSGustavo F. Padovan 4954343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_HIGH) 4960a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING_MITM; 4970a708f8fSGustavo F. Padovan else 4980a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 4990a708f8fSGustavo F. Padovan } else { 5004343478fSGustavo F. Padovan switch (chan->sec_level) { 5010a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 5020a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING_MITM; 5030a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 5040a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING; 5050a708f8fSGustavo F. Padovan default: 5060a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 5070a708f8fSGustavo F. Padovan } 5080a708f8fSGustavo F. Padovan } 5090a708f8fSGustavo F. Padovan } 5100a708f8fSGustavo F. Padovan 5110a708f8fSGustavo F. Padovan /* Service level security */ 5124343478fSGustavo F. Padovan static inline int l2cap_check_security(struct l2cap_chan *chan) 5130a708f8fSGustavo F. Padovan { 5148c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 5150a708f8fSGustavo F. Padovan __u8 auth_type; 5160a708f8fSGustavo F. Padovan 5174343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 5180a708f8fSGustavo F. Padovan 5194343478fSGustavo F. Padovan return hci_conn_security(conn->hcon, chan->sec_level, auth_type); 5200a708f8fSGustavo F. Padovan } 5210a708f8fSGustavo F. Padovan 522b5ad8b7fSJohannes Berg static u8 l2cap_get_ident(struct l2cap_conn *conn) 5230a708f8fSGustavo F. Padovan { 5240a708f8fSGustavo F. Padovan u8 id; 5250a708f8fSGustavo F. Padovan 5260a708f8fSGustavo F. Padovan /* Get next available identificator. 5270a708f8fSGustavo F. Padovan * 1 - 128 are used by kernel. 5280a708f8fSGustavo F. Padovan * 129 - 199 are reserved. 5290a708f8fSGustavo F. Padovan * 200 - 254 are used by utilities like l2ping, etc. 5300a708f8fSGustavo F. Padovan */ 5310a708f8fSGustavo F. Padovan 5320a708f8fSGustavo F. Padovan spin_lock_bh(&conn->lock); 5330a708f8fSGustavo F. Padovan 5340a708f8fSGustavo F. Padovan if (++conn->tx_ident > 128) 5350a708f8fSGustavo F. Padovan conn->tx_ident = 1; 5360a708f8fSGustavo F. Padovan 5370a708f8fSGustavo F. Padovan id = conn->tx_ident; 5380a708f8fSGustavo F. Padovan 5390a708f8fSGustavo F. Padovan spin_unlock_bh(&conn->lock); 5400a708f8fSGustavo F. Padovan 5410a708f8fSGustavo F. Padovan return id; 5420a708f8fSGustavo F. Padovan } 5430a708f8fSGustavo F. Padovan 5444519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data) 5450a708f8fSGustavo F. Padovan { 5460a708f8fSGustavo F. Padovan struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); 5470a708f8fSGustavo F. Padovan u8 flags; 5480a708f8fSGustavo F. Padovan 5490a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x", code); 5500a708f8fSGustavo F. Padovan 5510a708f8fSGustavo F. Padovan if (!skb) 5520a708f8fSGustavo F. Padovan return; 5530a708f8fSGustavo F. Padovan 5540a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 5550a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 5560a708f8fSGustavo F. Padovan else 5570a708f8fSGustavo F. Padovan flags = ACL_START; 5580a708f8fSGustavo F. Padovan 55914b12d0bSJaikumar Ganesh bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; 56014b12d0bSJaikumar Ganesh 5610a708f8fSGustavo F. Padovan hci_send_acl(conn->hcon, skb, flags); 5620a708f8fSGustavo F. Padovan } 5630a708f8fSGustavo F. Padovan 564525cd185SGustavo F. Padovan static inline void l2cap_send_sframe(struct l2cap_chan *chan, u16 control) 5650a708f8fSGustavo F. Padovan { 5660a708f8fSGustavo F. Padovan struct sk_buff *skb; 5670a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 5688c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 5690a708f8fSGustavo F. Padovan int count, hlen = L2CAP_HDR_SIZE + 2; 5700a708f8fSGustavo F. Padovan u8 flags; 5710a708f8fSGustavo F. Padovan 57289bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 5730a708f8fSGustavo F. Padovan return; 5740a708f8fSGustavo F. Padovan 57547d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 5760a708f8fSGustavo F. Padovan hlen += 2; 5770a708f8fSGustavo F. Padovan 57849208c9cSGustavo F. Padovan BT_DBG("chan %p, control 0x%2.2x", chan, control); 5790a708f8fSGustavo F. Padovan 5800a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, hlen); 581793c2f1cSAndrei Emeltchenko 582793c2f1cSAndrei Emeltchenko control |= __set_sframe(chan); 5830a708f8fSGustavo F. Padovan 584e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 585*03f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 5860a708f8fSGustavo F. Padovan 587e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_PBIT, &chan->conn_state)) 5880a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_POLL; 5890a708f8fSGustavo F. Padovan 5900a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 5910a708f8fSGustavo F. Padovan if (!skb) 5920a708f8fSGustavo F. Padovan return; 5930a708f8fSGustavo F. Padovan 5940a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 5950a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); 596fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 5970a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb_put(skb, 2)); 5980a708f8fSGustavo F. Padovan 59947d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 6000a708f8fSGustavo F. Padovan u16 fcs = crc16(0, (u8 *)lh, count - 2); 6010a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb_put(skb, 2)); 6020a708f8fSGustavo F. Padovan } 6030a708f8fSGustavo F. Padovan 6040a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 6050a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 6060a708f8fSGustavo F. Padovan else 6070a708f8fSGustavo F. Padovan flags = ACL_START; 6080a708f8fSGustavo F. Padovan 60915770b1aSAndrei Emeltchenko bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); 61014b12d0bSJaikumar Ganesh 6118c1d787bSGustavo F. Padovan hci_send_acl(chan->conn->hcon, skb, flags); 6120a708f8fSGustavo F. Padovan } 6130a708f8fSGustavo F. Padovan 614525cd185SGustavo F. Padovan static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u16 control) 6150a708f8fSGustavo F. Padovan { 616e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 617ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 618e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 6190a708f8fSGustavo F. Padovan } else 620ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 6210a708f8fSGustavo F. Padovan 6220b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 6230a708f8fSGustavo F. Padovan 624525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 6250a708f8fSGustavo F. Padovan } 6260a708f8fSGustavo F. Padovan 627b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) 6280a708f8fSGustavo F. Padovan { 629c1360a1cSGustavo F. Padovan return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); 6300a708f8fSGustavo F. Padovan } 6310a708f8fSGustavo F. Padovan 632fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan) 6330a708f8fSGustavo F. Padovan { 6348c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 6350a708f8fSGustavo F. Padovan 6360a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { 6370a708f8fSGustavo F. Padovan if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) 6380a708f8fSGustavo F. Padovan return; 6390a708f8fSGustavo F. Padovan 6404343478fSGustavo F. Padovan if (l2cap_check_security(chan) && 6414343478fSGustavo F. Padovan __l2cap_no_conn_pending(chan)) { 6420a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 643fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 644fe4128e0SGustavo F. Padovan req.psm = chan->psm; 6450a708f8fSGustavo F. Padovan 646fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 647c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 6480a708f8fSGustavo F. Padovan 649fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 650fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 6510a708f8fSGustavo F. Padovan } 6520a708f8fSGustavo F. Padovan } else { 6530a708f8fSGustavo F. Padovan struct l2cap_info_req req; 6540a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 6550a708f8fSGustavo F. Padovan 6560a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 6570a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 6580a708f8fSGustavo F. Padovan 6590a708f8fSGustavo F. Padovan mod_timer(&conn->info_timer, jiffies + 6600a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 6610a708f8fSGustavo F. Padovan 6620a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 6630a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 6640a708f8fSGustavo F. Padovan } 6650a708f8fSGustavo F. Padovan } 6660a708f8fSGustavo F. Padovan 6670a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) 6680a708f8fSGustavo F. Padovan { 6690a708f8fSGustavo F. Padovan u32 local_feat_mask = l2cap_feat_mask; 6700a708f8fSGustavo F. Padovan if (!disable_ertm) 6710a708f8fSGustavo F. Padovan local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; 6720a708f8fSGustavo F. Padovan 6730a708f8fSGustavo F. Padovan switch (mode) { 6740a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 6750a708f8fSGustavo F. Padovan return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; 6760a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 6770a708f8fSGustavo F. Padovan return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; 6780a708f8fSGustavo F. Padovan default: 6790a708f8fSGustavo F. Padovan return 0x00; 6800a708f8fSGustavo F. Padovan } 6810a708f8fSGustavo F. Padovan } 6820a708f8fSGustavo F. Padovan 6834519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err) 6840a708f8fSGustavo F. Padovan { 685e92c8e70SGustavo F. Padovan struct sock *sk; 6860a708f8fSGustavo F. Padovan struct l2cap_disconn_req req; 6870a708f8fSGustavo F. Padovan 6880a708f8fSGustavo F. Padovan if (!conn) 6890a708f8fSGustavo F. Padovan return; 6900a708f8fSGustavo F. Padovan 691e92c8e70SGustavo F. Padovan sk = chan->sk; 692e92c8e70SGustavo F. Padovan 6930c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 6941a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 6951a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 6961a09bcb9SGustavo F. Padovan __clear_ack_timer(chan); 6970a708f8fSGustavo F. Padovan } 6980a708f8fSGustavo F. Padovan 699fe4128e0SGustavo F. Padovan req.dcid = cpu_to_le16(chan->dcid); 700fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 7010a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 7020a708f8fSGustavo F. Padovan L2CAP_DISCONN_REQ, sizeof(req), &req); 7030a708f8fSGustavo F. Padovan 70489bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_DISCONN); 7050a708f8fSGustavo F. Padovan sk->sk_err = err; 7060a708f8fSGustavo F. Padovan } 7070a708f8fSGustavo F. Padovan 7080a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */ 7090a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn) 7100a708f8fSGustavo F. Padovan { 711820ffdb3SGustavo F. Padovan struct l2cap_chan *chan, *tmp; 7120a708f8fSGustavo F. Padovan 7130a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 7140a708f8fSGustavo F. Padovan 715baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 7160a708f8fSGustavo F. Padovan 717820ffdb3SGustavo F. Padovan list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { 71848454079SGustavo F. Padovan struct sock *sk = chan->sk; 719baa7e1faSGustavo F. Padovan 7200a708f8fSGustavo F. Padovan bh_lock_sock(sk); 7210a708f8fSGustavo F. Padovan 722715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 7230a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7240a708f8fSGustavo F. Padovan continue; 7250a708f8fSGustavo F. Padovan } 7260a708f8fSGustavo F. Padovan 72789bc500eSGustavo F. Padovan if (chan->state == BT_CONNECT) { 7280a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 7290a708f8fSGustavo F. Padovan 7304343478fSGustavo F. Padovan if (!l2cap_check_security(chan) || 731b4450035SGustavo F. Padovan !__l2cap_no_conn_pending(chan)) { 7320a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7330a708f8fSGustavo F. Padovan continue; 7340a708f8fSGustavo F. Padovan } 7350a708f8fSGustavo F. Padovan 736c1360a1cSGustavo F. Padovan if (!l2cap_mode_supported(chan->mode, conn->feat_mask) 737c1360a1cSGustavo F. Padovan && test_bit(CONF_STATE2_DEVICE, 738c1360a1cSGustavo F. Padovan &chan->conf_state)) { 7390f852724SGustavo F. Padovan /* l2cap_chan_close() calls list_del(chan) 740820ffdb3SGustavo F. Padovan * so release the lock */ 7412461daacSGustavo F. Padovan read_unlock(&conn->chan_lock); 7420f852724SGustavo F. Padovan l2cap_chan_close(chan, ECONNRESET); 7432461daacSGustavo F. Padovan read_lock(&conn->chan_lock); 7440a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7450a708f8fSGustavo F. Padovan continue; 7460a708f8fSGustavo F. Padovan } 7470a708f8fSGustavo F. Padovan 748fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 749fe4128e0SGustavo F. Padovan req.psm = chan->psm; 7500a708f8fSGustavo F. Padovan 751fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 752c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 7530a708f8fSGustavo F. Padovan 754fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 755fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 7560a708f8fSGustavo F. Padovan 75789bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT2) { 7580a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 7590a708f8fSGustavo F. Padovan char buf[128]; 760fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 761fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 7620a708f8fSGustavo F. Padovan 7634343478fSGustavo F. Padovan if (l2cap_check_security(chan)) { 7640a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 7650a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 7660a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 7670a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); 76805e9a2f6SIlia Kolomisnky if (parent) 7690a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 7700a708f8fSGustavo F. Padovan 7710a708f8fSGustavo F. Padovan } else { 77289bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONFIG); 7730a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 7740a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 7750a708f8fSGustavo F. Padovan } 7760a708f8fSGustavo F. Padovan } else { 7770a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 7780a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); 7790a708f8fSGustavo F. Padovan } 7800a708f8fSGustavo F. Padovan 781fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 782fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 7830a708f8fSGustavo F. Padovan 784c1360a1cSGustavo F. Padovan if (test_bit(CONF_REQ_SENT, &chan->conf_state) || 7850a708f8fSGustavo F. Padovan rsp.result != L2CAP_CR_SUCCESS) { 7860a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7870a708f8fSGustavo F. Padovan continue; 7880a708f8fSGustavo F. Padovan } 7890a708f8fSGustavo F. Padovan 790c1360a1cSGustavo F. Padovan set_bit(CONF_REQ_SENT, &chan->conf_state); 7910a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 79273ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 79373ffa904SGustavo F. Padovan chan->num_conf_req++; 7940a708f8fSGustavo F. Padovan } 7950a708f8fSGustavo F. Padovan 7960a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7970a708f8fSGustavo F. Padovan } 7980a708f8fSGustavo F. Padovan 799baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 8000a708f8fSGustavo F. Padovan } 8010a708f8fSGustavo F. Padovan 802b62f328bSVille Tervo /* Find socket with cid and source bdaddr. 803b62f328bSVille Tervo * Returns closest match, locked. 804b62f328bSVille Tervo */ 80523691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src) 806b62f328bSVille Tervo { 80723691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 808b62f328bSVille Tervo 80923691d75SGustavo F. Padovan read_lock(&chan_list_lock); 810b62f328bSVille Tervo 81123691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 81223691d75SGustavo F. Padovan struct sock *sk = c->sk; 813fe4128e0SGustavo F. Padovan 81489bc500eSGustavo F. Padovan if (state && c->state != state) 815b62f328bSVille Tervo continue; 816b62f328bSVille Tervo 81723691d75SGustavo F. Padovan if (c->scid == cid) { 818b62f328bSVille Tervo /* Exact match. */ 81923691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 82023691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 82123691d75SGustavo F. Padovan return c; 82223691d75SGustavo F. Padovan } 823b62f328bSVille Tervo 824b62f328bSVille Tervo /* Closest match */ 825b62f328bSVille Tervo if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 82623691d75SGustavo F. Padovan c1 = c; 827b62f328bSVille Tervo } 828b62f328bSVille Tervo } 829280f294fSGustavo F. Padovan 83023691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 831b62f328bSVille Tervo 83223691d75SGustavo F. Padovan return c1; 833b62f328bSVille Tervo } 834b62f328bSVille Tervo 835b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn) 836b62f328bSVille Tervo { 837c916fbe4SGustavo F. Padovan struct sock *parent, *sk; 83823691d75SGustavo F. Padovan struct l2cap_chan *chan, *pchan; 839b62f328bSVille Tervo 840b62f328bSVille Tervo BT_DBG(""); 841b62f328bSVille Tervo 842b62f328bSVille Tervo /* Check if we have socket listening on cid */ 84323691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, 844b62f328bSVille Tervo conn->src); 84523691d75SGustavo F. Padovan if (!pchan) 846b62f328bSVille Tervo return; 847b62f328bSVille Tervo 84823691d75SGustavo F. Padovan parent = pchan->sk; 84923691d75SGustavo F. Padovan 85062f3a2cfSGustavo F. Padovan bh_lock_sock(parent); 85162f3a2cfSGustavo F. Padovan 852b62f328bSVille Tervo /* Check for backlog size */ 853b62f328bSVille Tervo if (sk_acceptq_is_full(parent)) { 854b62f328bSVille Tervo BT_DBG("backlog full %d", parent->sk_ack_backlog); 855b62f328bSVille Tervo goto clean; 856b62f328bSVille Tervo } 857b62f328bSVille Tervo 85880808e43SGustavo F. Padovan chan = pchan->ops->new_connection(pchan->data); 85980808e43SGustavo F. Padovan if (!chan) 860b62f328bSVille Tervo goto clean; 861b62f328bSVille Tervo 86280808e43SGustavo F. Padovan sk = chan->sk; 8635d41ce1dSGustavo F. Padovan 864baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 865b62f328bSVille Tervo 866b62f328bSVille Tervo hci_conn_hold(conn->hcon); 867b62f328bSVille Tervo 868b62f328bSVille Tervo bacpy(&bt_sk(sk)->src, conn->src); 869b62f328bSVille Tervo bacpy(&bt_sk(sk)->dst, conn->dst); 870b62f328bSVille Tervo 871d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 872d1010240SGustavo F. Padovan 87348454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 87448454079SGustavo F. Padovan 875c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 876b62f328bSVille Tervo 87789bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 878b62f328bSVille Tervo parent->sk_data_ready(parent, 0); 879b62f328bSVille Tervo 880baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 881b62f328bSVille Tervo 882b62f328bSVille Tervo clean: 883b62f328bSVille Tervo bh_unlock_sock(parent); 884b62f328bSVille Tervo } 885b62f328bSVille Tervo 886f1cb9af5SVinicius Costa Gomes static void l2cap_chan_ready(struct sock *sk) 887f1cb9af5SVinicius Costa Gomes { 888f1cb9af5SVinicius Costa Gomes struct l2cap_chan *chan = l2cap_pi(sk)->chan; 889f1cb9af5SVinicius Costa Gomes struct sock *parent = bt_sk(sk)->parent; 890f1cb9af5SVinicius Costa Gomes 891f1cb9af5SVinicius Costa Gomes BT_DBG("sk %p, parent %p", sk, parent); 892f1cb9af5SVinicius Costa Gomes 893f1cb9af5SVinicius Costa Gomes chan->conf_state = 0; 894f1cb9af5SVinicius Costa Gomes __clear_chan_timer(chan); 895f1cb9af5SVinicius Costa Gomes 89643f3dc41SVinicius Costa Gomes l2cap_state_change(chan, BT_CONNECTED); 897f1cb9af5SVinicius Costa Gomes sk->sk_state_change(sk); 898f1cb9af5SVinicius Costa Gomes 899f1cb9af5SVinicius Costa Gomes if (parent) 900f1cb9af5SVinicius Costa Gomes parent->sk_data_ready(parent, 0); 901f1cb9af5SVinicius Costa Gomes } 902f1cb9af5SVinicius Costa Gomes 9030a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn) 9040a708f8fSGustavo F. Padovan { 90548454079SGustavo F. Padovan struct l2cap_chan *chan; 9060a708f8fSGustavo F. Padovan 9070a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 9080a708f8fSGustavo F. Padovan 909b62f328bSVille Tervo if (!conn->hcon->out && conn->hcon->type == LE_LINK) 910b62f328bSVille Tervo l2cap_le_conn_ready(conn); 911b62f328bSVille Tervo 912160dc6acSVinicius Costa Gomes if (conn->hcon->out && conn->hcon->type == LE_LINK) 913160dc6acSVinicius Costa Gomes smp_conn_security(conn, conn->hcon->pending_sec_level); 914160dc6acSVinicius Costa Gomes 915baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 9160a708f8fSGustavo F. Padovan 917baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 91848454079SGustavo F. Padovan struct sock *sk = chan->sk; 919baa7e1faSGustavo F. Padovan 9200a708f8fSGustavo F. Padovan bh_lock_sock(sk); 9210a708f8fSGustavo F. Padovan 92263128451SVinicius Costa Gomes if (conn->hcon->type == LE_LINK) { 923b501d6a1SAnderson Briglia if (smp_conn_security(conn, chan->sec_level)) 924f1cb9af5SVinicius Costa Gomes l2cap_chan_ready(sk); 925acd7d370SVille Tervo 92663128451SVinicius Costa Gomes } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 927c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 92889bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 9290a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 930b501d6a1SAnderson Briglia 93189bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT) 932fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 9330a708f8fSGustavo F. Padovan 9340a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9350a708f8fSGustavo F. Padovan } 9360a708f8fSGustavo F. Padovan 937baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 9380a708f8fSGustavo F. Padovan } 9390a708f8fSGustavo F. Padovan 9400a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */ 9410a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) 9420a708f8fSGustavo F. Padovan { 94348454079SGustavo F. Padovan struct l2cap_chan *chan; 9440a708f8fSGustavo F. Padovan 9450a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 9460a708f8fSGustavo F. Padovan 947baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 9480a708f8fSGustavo F. Padovan 949baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 95048454079SGustavo F. Padovan struct sock *sk = chan->sk; 951baa7e1faSGustavo F. Padovan 952ecf61bdbSAndrei Emeltchenko if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) 9530a708f8fSGustavo F. Padovan sk->sk_err = err; 9540a708f8fSGustavo F. Padovan } 9550a708f8fSGustavo F. Padovan 956baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 9570a708f8fSGustavo F. Padovan } 9580a708f8fSGustavo F. Padovan 9590a708f8fSGustavo F. Padovan static void l2cap_info_timeout(unsigned long arg) 9600a708f8fSGustavo F. Padovan { 9610a708f8fSGustavo F. Padovan struct l2cap_conn *conn = (void *) arg; 9620a708f8fSGustavo F. Padovan 9630a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 9640a708f8fSGustavo F. Padovan conn->info_ident = 0; 9650a708f8fSGustavo F. Padovan 9660a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 9670a708f8fSGustavo F. Padovan } 9680a708f8fSGustavo F. Padovan 9695d3de7dfSVinicius Costa Gomes static void l2cap_conn_del(struct hci_conn *hcon, int err) 9705d3de7dfSVinicius Costa Gomes { 9715d3de7dfSVinicius Costa Gomes struct l2cap_conn *conn = hcon->l2cap_data; 9725d3de7dfSVinicius Costa Gomes struct l2cap_chan *chan, *l; 9735d3de7dfSVinicius Costa Gomes struct sock *sk; 9745d3de7dfSVinicius Costa Gomes 9755d3de7dfSVinicius Costa Gomes if (!conn) 9765d3de7dfSVinicius Costa Gomes return; 9775d3de7dfSVinicius Costa Gomes 9785d3de7dfSVinicius Costa Gomes BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); 9795d3de7dfSVinicius Costa Gomes 9805d3de7dfSVinicius Costa Gomes kfree_skb(conn->rx_skb); 9815d3de7dfSVinicius Costa Gomes 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; 9855d3de7dfSVinicius Costa Gomes bh_lock_sock(sk); 9865d3de7dfSVinicius Costa Gomes l2cap_chan_del(chan, err); 9875d3de7dfSVinicius Costa Gomes bh_unlock_sock(sk); 9885d3de7dfSVinicius Costa Gomes chan->ops->close(chan->data); 9895d3de7dfSVinicius Costa Gomes } 9905d3de7dfSVinicius Costa Gomes 9915d3de7dfSVinicius Costa Gomes if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) 9925d3de7dfSVinicius Costa Gomes del_timer_sync(&conn->info_timer); 9935d3de7dfSVinicius Costa Gomes 994d26a2345SVinicius Costa Gomes if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->pend)) { 9955d3de7dfSVinicius Costa Gomes del_timer(&conn->security_timer); 9968aab4757SVinicius Costa Gomes smp_chan_destroy(conn); 997d26a2345SVinicius Costa Gomes } 9985d3de7dfSVinicius Costa Gomes 9995d3de7dfSVinicius Costa Gomes hcon->l2cap_data = NULL; 10005d3de7dfSVinicius Costa Gomes kfree(conn); 10015d3de7dfSVinicius Costa Gomes } 10025d3de7dfSVinicius Costa Gomes 10035d3de7dfSVinicius Costa Gomes static void security_timeout(unsigned long arg) 10045d3de7dfSVinicius Costa Gomes { 10055d3de7dfSVinicius Costa Gomes struct l2cap_conn *conn = (void *) arg; 10065d3de7dfSVinicius Costa Gomes 10075d3de7dfSVinicius Costa Gomes l2cap_conn_del(conn->hcon, ETIMEDOUT); 10085d3de7dfSVinicius Costa Gomes } 10095d3de7dfSVinicius Costa Gomes 10100a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) 10110a708f8fSGustavo F. Padovan { 10120a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 10130a708f8fSGustavo F. Padovan 10140a708f8fSGustavo F. Padovan if (conn || status) 10150a708f8fSGustavo F. Padovan return conn; 10160a708f8fSGustavo F. Padovan 10170a708f8fSGustavo F. Padovan conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); 10180a708f8fSGustavo F. Padovan if (!conn) 10190a708f8fSGustavo F. Padovan return NULL; 10200a708f8fSGustavo F. Padovan 10210a708f8fSGustavo F. Padovan hcon->l2cap_data = conn; 10220a708f8fSGustavo F. Padovan conn->hcon = hcon; 10230a708f8fSGustavo F. Padovan 10240a708f8fSGustavo F. Padovan BT_DBG("hcon %p conn %p", hcon, conn); 10250a708f8fSGustavo F. Padovan 1026acd7d370SVille Tervo if (hcon->hdev->le_mtu && hcon->type == LE_LINK) 1027acd7d370SVille Tervo conn->mtu = hcon->hdev->le_mtu; 1028acd7d370SVille Tervo else 10290a708f8fSGustavo F. Padovan conn->mtu = hcon->hdev->acl_mtu; 1030acd7d370SVille Tervo 10310a708f8fSGustavo F. Padovan conn->src = &hcon->hdev->bdaddr; 10320a708f8fSGustavo F. Padovan conn->dst = &hcon->dst; 10330a708f8fSGustavo F. Padovan 10340a708f8fSGustavo F. Padovan conn->feat_mask = 0; 10350a708f8fSGustavo F. Padovan 10360a708f8fSGustavo F. Padovan spin_lock_init(&conn->lock); 1037baa7e1faSGustavo F. Padovan rwlock_init(&conn->chan_lock); 1038baa7e1faSGustavo F. Padovan 1039baa7e1faSGustavo F. Padovan INIT_LIST_HEAD(&conn->chan_l); 10400a708f8fSGustavo F. Padovan 10415d3de7dfSVinicius Costa Gomes if (hcon->type == LE_LINK) 10425d3de7dfSVinicius Costa Gomes setup_timer(&conn->security_timer, security_timeout, 10435d3de7dfSVinicius Costa Gomes (unsigned long) conn); 10445d3de7dfSVinicius Costa Gomes else 10450a708f8fSGustavo F. Padovan setup_timer(&conn->info_timer, l2cap_info_timeout, 10460a708f8fSGustavo F. Padovan (unsigned long) conn); 10470a708f8fSGustavo F. Padovan 10480a708f8fSGustavo F. Padovan conn->disc_reason = 0x13; 10490a708f8fSGustavo F. Padovan 10500a708f8fSGustavo F. Padovan return conn; 10510a708f8fSGustavo F. Padovan } 10520a708f8fSGustavo F. Padovan 105348454079SGustavo F. Padovan static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 10540a708f8fSGustavo F. Padovan { 1055baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 105648454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 1057baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 10580a708f8fSGustavo F. Padovan } 10590a708f8fSGustavo F. Padovan 10600a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */ 10610a708f8fSGustavo F. Padovan 10620a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr. 10630a708f8fSGustavo F. Padovan * Returns closest match. 10640a708f8fSGustavo F. Padovan */ 106523691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src) 10660a708f8fSGustavo F. Padovan { 106723691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 10680a708f8fSGustavo F. Padovan 106923691d75SGustavo F. Padovan read_lock(&chan_list_lock); 10700a708f8fSGustavo F. Padovan 107123691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 107223691d75SGustavo F. Padovan struct sock *sk = c->sk; 1073fe4128e0SGustavo F. Padovan 107489bc500eSGustavo F. Padovan if (state && c->state != state) 10750a708f8fSGustavo F. Padovan continue; 10760a708f8fSGustavo F. Padovan 107723691d75SGustavo F. Padovan if (c->psm == psm) { 10780a708f8fSGustavo F. Padovan /* Exact match. */ 107923691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 1080a7567b20SJohannes Berg read_unlock(&chan_list_lock); 108123691d75SGustavo F. Padovan return c; 108223691d75SGustavo F. Padovan } 10830a708f8fSGustavo F. Padovan 10840a708f8fSGustavo F. Padovan /* Closest match */ 10850a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 108623691d75SGustavo F. Padovan c1 = c; 10870a708f8fSGustavo F. Padovan } 10880a708f8fSGustavo F. Padovan } 10890a708f8fSGustavo F. Padovan 109023691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 10910a708f8fSGustavo F. Padovan 109223691d75SGustavo F. Padovan return c1; 10930a708f8fSGustavo F. Padovan } 10940a708f8fSGustavo F. Padovan 109577a74c7eSGustavo F. Padovan int l2cap_chan_connect(struct l2cap_chan *chan) 10960a708f8fSGustavo F. Padovan { 10975d41ce1dSGustavo F. Padovan struct sock *sk = chan->sk; 10980a708f8fSGustavo F. Padovan bdaddr_t *src = &bt_sk(sk)->src; 10990a708f8fSGustavo F. Padovan bdaddr_t *dst = &bt_sk(sk)->dst; 11000a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 11010a708f8fSGustavo F. Padovan struct hci_conn *hcon; 11020a708f8fSGustavo F. Padovan struct hci_dev *hdev; 11030a708f8fSGustavo F. Padovan __u8 auth_type; 11040a708f8fSGustavo F. Padovan int err; 11050a708f8fSGustavo F. Padovan 11060a708f8fSGustavo F. Padovan BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), 1107fe4128e0SGustavo F. Padovan chan->psm); 11080a708f8fSGustavo F. Padovan 11090a708f8fSGustavo F. Padovan hdev = hci_get_route(dst, src); 11100a708f8fSGustavo F. Padovan if (!hdev) 11110a708f8fSGustavo F. Padovan return -EHOSTUNREACH; 11120a708f8fSGustavo F. Padovan 11130a708f8fSGustavo F. Padovan hci_dev_lock_bh(hdev); 11140a708f8fSGustavo F. Padovan 11154343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 11160a708f8fSGustavo F. Padovan 1117fe4128e0SGustavo F. Padovan if (chan->dcid == L2CAP_CID_LE_DATA) 1118acd7d370SVille Tervo hcon = hci_connect(hdev, LE_LINK, dst, 11194343478fSGustavo F. Padovan chan->sec_level, auth_type); 1120acd7d370SVille Tervo else 11210a708f8fSGustavo F. Padovan hcon = hci_connect(hdev, ACL_LINK, dst, 11224343478fSGustavo F. Padovan chan->sec_level, auth_type); 1123acd7d370SVille Tervo 112430e76272SVille Tervo if (IS_ERR(hcon)) { 112530e76272SVille Tervo err = PTR_ERR(hcon); 11260a708f8fSGustavo F. Padovan goto done; 112730e76272SVille Tervo } 11280a708f8fSGustavo F. Padovan 11290a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 11300a708f8fSGustavo F. Padovan if (!conn) { 11310a708f8fSGustavo F. Padovan hci_conn_put(hcon); 113230e76272SVille Tervo err = -ENOMEM; 11330a708f8fSGustavo F. Padovan goto done; 11340a708f8fSGustavo F. Padovan } 11350a708f8fSGustavo F. Padovan 11360a708f8fSGustavo F. Padovan /* Update source addr of the socket */ 11370a708f8fSGustavo F. Padovan bacpy(src, conn->src); 11380a708f8fSGustavo F. Padovan 113948454079SGustavo F. Padovan l2cap_chan_add(conn, chan); 114048454079SGustavo F. Padovan 114189bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECT); 1142c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 11430a708f8fSGustavo F. Padovan 11440a708f8fSGustavo F. Padovan if (hcon->state == BT_CONNECTED) { 1145715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 1146c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 11474343478fSGustavo F. Padovan if (l2cap_check_security(chan)) 114889bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 11490a708f8fSGustavo F. Padovan } else 1150fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 11510a708f8fSGustavo F. Padovan } 11520a708f8fSGustavo F. Padovan 115330e76272SVille Tervo err = 0; 115430e76272SVille Tervo 11550a708f8fSGustavo F. Padovan done: 11560a708f8fSGustavo F. Padovan hci_dev_unlock_bh(hdev); 11570a708f8fSGustavo F. Padovan hci_dev_put(hdev); 11580a708f8fSGustavo F. Padovan return err; 11590a708f8fSGustavo F. Padovan } 11600a708f8fSGustavo F. Padovan 1161dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk) 11620a708f8fSGustavo F. Padovan { 11638c1d787bSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 11640a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 11650a708f8fSGustavo F. Padovan int err = 0; 11660a708f8fSGustavo F. Padovan int timeo = HZ/5; 11670a708f8fSGustavo F. Padovan 11680a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 11690a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 1170a71a0cf4SPeter Hurley while (chan->unacked_frames > 0 && chan->conn) { 11710a708f8fSGustavo F. Padovan if (!timeo) 11720a708f8fSGustavo F. Padovan timeo = HZ/5; 11730a708f8fSGustavo F. Padovan 11740a708f8fSGustavo F. Padovan if (signal_pending(current)) { 11750a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 11760a708f8fSGustavo F. Padovan break; 11770a708f8fSGustavo F. Padovan } 11780a708f8fSGustavo F. Padovan 11790a708f8fSGustavo F. Padovan release_sock(sk); 11800a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 11810a708f8fSGustavo F. Padovan lock_sock(sk); 1182a71a0cf4SPeter Hurley set_current_state(TASK_INTERRUPTIBLE); 11830a708f8fSGustavo F. Padovan 11840a708f8fSGustavo F. Padovan err = sock_error(sk); 11850a708f8fSGustavo F. Padovan if (err) 11860a708f8fSGustavo F. Padovan break; 11870a708f8fSGustavo F. Padovan } 11880a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 11890a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 11900a708f8fSGustavo F. Padovan return err; 11910a708f8fSGustavo F. Padovan } 11920a708f8fSGustavo F. Padovan 11930a708f8fSGustavo F. Padovan static void l2cap_monitor_timeout(unsigned long arg) 11940a708f8fSGustavo F. Padovan { 1195525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 1196525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 11970a708f8fSGustavo F. Padovan 1198525cd185SGustavo F. Padovan BT_DBG("chan %p", chan); 11990a708f8fSGustavo F. Padovan 12000a708f8fSGustavo F. Padovan bh_lock_sock(sk); 12012c03a7a4SGustavo F. Padovan if (chan->retry_count >= chan->remote_max_tx) { 12028c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 12030a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 12040a708f8fSGustavo F. Padovan return; 12050a708f8fSGustavo F. Padovan } 12060a708f8fSGustavo F. Padovan 12076a026610SGustavo F. Padovan chan->retry_count++; 12081a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 12090a708f8fSGustavo F. Padovan 1210525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 12110a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 12120a708f8fSGustavo F. Padovan } 12130a708f8fSGustavo F. Padovan 12140a708f8fSGustavo F. Padovan static void l2cap_retrans_timeout(unsigned long arg) 12150a708f8fSGustavo F. Padovan { 1216525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 1217525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 12180a708f8fSGustavo F. Padovan 121949208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 12200a708f8fSGustavo F. Padovan 12210a708f8fSGustavo F. Padovan bh_lock_sock(sk); 12226a026610SGustavo F. Padovan chan->retry_count = 1; 12231a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 12240a708f8fSGustavo F. Padovan 1225e2ab4353SGustavo F. Padovan set_bit(CONN_WAIT_F, &chan->conn_state); 12260a708f8fSGustavo F. Padovan 1227525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 12280a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 12290a708f8fSGustavo F. Padovan } 12300a708f8fSGustavo F. Padovan 123142e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan) 12320a708f8fSGustavo F. Padovan { 12330a708f8fSGustavo F. Padovan struct sk_buff *skb; 12340a708f8fSGustavo F. Padovan 123558d35f87SGustavo F. Padovan while ((skb = skb_peek(&chan->tx_q)) && 12366a026610SGustavo F. Padovan chan->unacked_frames) { 123742e5c802SGustavo F. Padovan if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) 12380a708f8fSGustavo F. Padovan break; 12390a708f8fSGustavo F. Padovan 124058d35f87SGustavo F. Padovan skb = skb_dequeue(&chan->tx_q); 12410a708f8fSGustavo F. Padovan kfree_skb(skb); 12420a708f8fSGustavo F. Padovan 12436a026610SGustavo F. Padovan chan->unacked_frames--; 12440a708f8fSGustavo F. Padovan } 12450a708f8fSGustavo F. Padovan 12466a026610SGustavo F. Padovan if (!chan->unacked_frames) 12471a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 12480a708f8fSGustavo F. Padovan } 12490a708f8fSGustavo F. Padovan 125067c9e840SSzymon Janc static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) 12510a708f8fSGustavo F. Padovan { 12528c1d787bSGustavo F. Padovan struct hci_conn *hcon = chan->conn->hcon; 12530a708f8fSGustavo F. Padovan u16 flags; 12540a708f8fSGustavo F. Padovan 12554343478fSGustavo F. Padovan BT_DBG("chan %p, skb %p len %d", chan, skb, skb->len); 12560a708f8fSGustavo F. Padovan 1257d57b0e8bSAndrei Emeltchenko if (!test_bit(FLAG_FLUSHABLE, &chan->flags) && 1258d57b0e8bSAndrei Emeltchenko lmp_no_flush_capable(hcon->hdev)) 12590a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 12600a708f8fSGustavo F. Padovan else 12610a708f8fSGustavo F. Padovan flags = ACL_START; 12620a708f8fSGustavo F. Padovan 126315770b1aSAndrei Emeltchenko bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); 12640a708f8fSGustavo F. Padovan hci_send_acl(hcon, skb, flags); 12650a708f8fSGustavo F. Padovan } 12660a708f8fSGustavo F. Padovan 126767c9e840SSzymon Janc static void l2cap_streaming_send(struct l2cap_chan *chan) 12680a708f8fSGustavo F. Padovan { 12690a708f8fSGustavo F. Padovan struct sk_buff *skb; 12700a708f8fSGustavo F. Padovan u16 control, fcs; 12710a708f8fSGustavo F. Padovan 127258d35f87SGustavo F. Padovan while ((skb = skb_dequeue(&chan->tx_q))) { 12730a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data + L2CAP_HDR_SIZE); 1274fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, chan->next_tx_seq); 12750a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb->data + L2CAP_HDR_SIZE); 12760a708f8fSGustavo F. Padovan 127747d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 12780a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)skb->data, skb->len - 2); 12790a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb->data + skb->len - 2); 12800a708f8fSGustavo F. Padovan } 12810a708f8fSGustavo F. Padovan 12824343478fSGustavo F. Padovan l2cap_do_send(chan, skb); 12830a708f8fSGustavo F. Padovan 128442e5c802SGustavo F. Padovan chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; 12850a708f8fSGustavo F. Padovan } 12860a708f8fSGustavo F. Padovan } 12870a708f8fSGustavo F. Padovan 1288fb45de7dSAndrei Emeltchenko static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u16 tx_seq) 12890a708f8fSGustavo F. Padovan { 12900a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 12910a708f8fSGustavo F. Padovan u16 control, fcs; 12920a708f8fSGustavo F. Padovan 129358d35f87SGustavo F. Padovan skb = skb_peek(&chan->tx_q); 12940a708f8fSGustavo F. Padovan if (!skb) 12950a708f8fSGustavo F. Padovan return; 12960a708f8fSGustavo F. Padovan 12970a708f8fSGustavo F. Padovan do { 12980a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq == tx_seq) 12990a708f8fSGustavo F. Padovan break; 13000a708f8fSGustavo F. Padovan 130158d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 13020a708f8fSGustavo F. Padovan return; 13030a708f8fSGustavo F. Padovan 130458d35f87SGustavo F. Padovan } while ((skb = skb_queue_next(&chan->tx_q, skb))); 13050a708f8fSGustavo F. Padovan 13062c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 13072c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 13088c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 13090a708f8fSGustavo F. Padovan return; 13100a708f8fSGustavo F. Padovan } 13110a708f8fSGustavo F. Padovan 13120a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 13130a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 13140a708f8fSGustavo F. Padovan control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); 13157e0ef6eeSAndrei Emeltchenko control &= __get_sar_mask(chan); 13160a708f8fSGustavo F. Padovan 1317e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 1318*03f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 13190a708f8fSGustavo F. Padovan 13200b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 1321fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, tx_seq); 13220a708f8fSGustavo F. Padovan 13230a708f8fSGustavo F. Padovan put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); 13240a708f8fSGustavo F. Padovan 132547d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 13260a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)tx_skb->data, tx_skb->len - 2); 13270a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, tx_skb->data + tx_skb->len - 2); 13280a708f8fSGustavo F. Padovan } 13290a708f8fSGustavo F. Padovan 13304343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 13310a708f8fSGustavo F. Padovan } 13320a708f8fSGustavo F. Padovan 133367c9e840SSzymon Janc static int l2cap_ertm_send(struct l2cap_chan *chan) 13340a708f8fSGustavo F. Padovan { 13350a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 13360a708f8fSGustavo F. Padovan u16 control, fcs; 13370a708f8fSGustavo F. Padovan int nsent = 0; 13380a708f8fSGustavo F. Padovan 133989bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 13400a708f8fSGustavo F. Padovan return -ENOTCONN; 13410a708f8fSGustavo F. Padovan 134258d35f87SGustavo F. Padovan while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) { 13430a708f8fSGustavo F. Padovan 13442c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 13452c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 13468c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 13470a708f8fSGustavo F. Padovan break; 13480a708f8fSGustavo F. Padovan } 13490a708f8fSGustavo F. Padovan 13500a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 13510a708f8fSGustavo F. Padovan 13520a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 13530a708f8fSGustavo F. Padovan 13540a708f8fSGustavo F. Padovan control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); 13557e0ef6eeSAndrei Emeltchenko control &= __get_sar_mask(chan); 13560a708f8fSGustavo F. Padovan 1357e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 1358*03f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 1359e2ab4353SGustavo F. Padovan 13600b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 1361fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, chan->next_tx_seq); 13620a708f8fSGustavo F. Padovan put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); 13630a708f8fSGustavo F. Padovan 13640a708f8fSGustavo F. Padovan 136547d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 13660a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2); 13670a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb->data + tx_skb->len - 2); 13680a708f8fSGustavo F. Padovan } 13690a708f8fSGustavo F. Padovan 13704343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 13710a708f8fSGustavo F. Padovan 13721a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 13730a708f8fSGustavo F. Padovan 137442e5c802SGustavo F. Padovan bt_cb(skb)->tx_seq = chan->next_tx_seq; 137542e5c802SGustavo F. Padovan chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; 13760a708f8fSGustavo F. Padovan 137723e9fde2SSuraj Sumangala if (bt_cb(skb)->retries == 1) 13786a026610SGustavo F. Padovan chan->unacked_frames++; 137923e9fde2SSuraj Sumangala 13806a026610SGustavo F. Padovan chan->frames_sent++; 13810a708f8fSGustavo F. Padovan 138258d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 138358d35f87SGustavo F. Padovan chan->tx_send_head = NULL; 13840a708f8fSGustavo F. Padovan else 138558d35f87SGustavo F. Padovan chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); 13860a708f8fSGustavo F. Padovan 13870a708f8fSGustavo F. Padovan nsent++; 13880a708f8fSGustavo F. Padovan } 13890a708f8fSGustavo F. Padovan 13900a708f8fSGustavo F. Padovan return nsent; 13910a708f8fSGustavo F. Padovan } 13920a708f8fSGustavo F. Padovan 1393525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan) 13940a708f8fSGustavo F. Padovan { 13950a708f8fSGustavo F. Padovan int ret; 13960a708f8fSGustavo F. Padovan 139758d35f87SGustavo F. Padovan if (!skb_queue_empty(&chan->tx_q)) 139858d35f87SGustavo F. Padovan chan->tx_send_head = chan->tx_q.next; 13990a708f8fSGustavo F. Padovan 140042e5c802SGustavo F. Padovan chan->next_tx_seq = chan->expected_ack_seq; 1401525cd185SGustavo F. Padovan ret = l2cap_ertm_send(chan); 14020a708f8fSGustavo F. Padovan return ret; 14030a708f8fSGustavo F. Padovan } 14040a708f8fSGustavo F. Padovan 1405525cd185SGustavo F. Padovan static void l2cap_send_ack(struct l2cap_chan *chan) 14060a708f8fSGustavo F. Padovan { 14070a708f8fSGustavo F. Padovan u16 control = 0; 14080a708f8fSGustavo F. Padovan 14090b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 14100a708f8fSGustavo F. Padovan 1411e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 1412ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 1413e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 1414525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 14150a708f8fSGustavo F. Padovan return; 14160a708f8fSGustavo F. Padovan } 14170a708f8fSGustavo F. Padovan 1418525cd185SGustavo F. Padovan if (l2cap_ertm_send(chan) > 0) 14190a708f8fSGustavo F. Padovan return; 14200a708f8fSGustavo F. Padovan 1421ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 1422525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 14230a708f8fSGustavo F. Padovan } 14240a708f8fSGustavo F. Padovan 1425525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan) 14260a708f8fSGustavo F. Padovan { 14270a708f8fSGustavo F. Padovan struct srej_list *tail; 14280a708f8fSGustavo F. Padovan u16 control; 14290a708f8fSGustavo F. Padovan 1430ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 1431*03f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 14320a708f8fSGustavo F. Padovan 143339d5a3eeSGustavo F. Padovan tail = list_entry((&chan->srej_l)->prev, struct srej_list, list); 14340b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, tail->tx_seq); 14350a708f8fSGustavo F. Padovan 1436525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 14370a708f8fSGustavo F. Padovan } 14380a708f8fSGustavo F. Padovan 14390a708f8fSGustavo F. Padovan static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb) 14400a708f8fSGustavo F. Padovan { 14418c1d787bSGustavo F. Padovan struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn; 14420a708f8fSGustavo F. Padovan struct sk_buff **frag; 14430a708f8fSGustavo F. Padovan int err, sent = 0; 14440a708f8fSGustavo F. Padovan 14450a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) 14460a708f8fSGustavo F. Padovan return -EFAULT; 14470a708f8fSGustavo F. Padovan 14480a708f8fSGustavo F. Padovan sent += count; 14490a708f8fSGustavo F. Padovan len -= count; 14500a708f8fSGustavo F. Padovan 14510a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 14520a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 14530a708f8fSGustavo F. Padovan while (len) { 14540a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 14550a708f8fSGustavo F. Padovan 14560a708f8fSGustavo F. Padovan *frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err); 14570a708f8fSGustavo F. Padovan if (!*frag) 14580a708f8fSGustavo F. Padovan return err; 14590a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) 14600a708f8fSGustavo F. Padovan return -EFAULT; 14610a708f8fSGustavo F. Padovan 14620a708f8fSGustavo F. Padovan sent += count; 14630a708f8fSGustavo F. Padovan len -= count; 14640a708f8fSGustavo F. Padovan 14650a708f8fSGustavo F. Padovan frag = &(*frag)->next; 14660a708f8fSGustavo F. Padovan } 14670a708f8fSGustavo F. Padovan 14680a708f8fSGustavo F. Padovan return sent; 14690a708f8fSGustavo F. Padovan } 14700a708f8fSGustavo F. Padovan 147167c9e840SSzymon Janc static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 14720a708f8fSGustavo F. Padovan { 1473fe4128e0SGustavo F. Padovan struct sock *sk = chan->sk; 14748c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 14750a708f8fSGustavo F. Padovan struct sk_buff *skb; 14760a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE + 2; 14770a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 14780a708f8fSGustavo F. Padovan 14790a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 14800a708f8fSGustavo F. Padovan 14810a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 14820a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 14830a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 14840a708f8fSGustavo F. Padovan if (!skb) 14850a708f8fSGustavo F. Padovan return ERR_PTR(err); 14860a708f8fSGustavo F. Padovan 14870a708f8fSGustavo F. Padovan /* Create L2CAP header */ 14880a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1489fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 14900a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 1491fe4128e0SGustavo F. Padovan put_unaligned_le16(chan->psm, skb_put(skb, 2)); 14920a708f8fSGustavo F. Padovan 14930a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 14940a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 14950a708f8fSGustavo F. Padovan kfree_skb(skb); 14960a708f8fSGustavo F. Padovan return ERR_PTR(err); 14970a708f8fSGustavo F. Padovan } 14980a708f8fSGustavo F. Padovan return skb; 14990a708f8fSGustavo F. Padovan } 15000a708f8fSGustavo F. Padovan 150167c9e840SSzymon Janc static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 15020a708f8fSGustavo F. Padovan { 1503fe4128e0SGustavo F. Padovan struct sock *sk = chan->sk; 15048c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 15050a708f8fSGustavo F. Padovan struct sk_buff *skb; 15060a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE; 15070a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 15080a708f8fSGustavo F. Padovan 15090a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 15100a708f8fSGustavo F. Padovan 15110a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 15120a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 15130a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 15140a708f8fSGustavo F. Padovan if (!skb) 15150a708f8fSGustavo F. Padovan return ERR_PTR(err); 15160a708f8fSGustavo F. Padovan 15170a708f8fSGustavo F. Padovan /* Create L2CAP header */ 15180a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1519fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 15200a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 15210a708f8fSGustavo F. Padovan 15220a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 15230a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 15240a708f8fSGustavo F. Padovan kfree_skb(skb); 15250a708f8fSGustavo F. Padovan return ERR_PTR(err); 15260a708f8fSGustavo F. Padovan } 15270a708f8fSGustavo F. Padovan return skb; 15280a708f8fSGustavo F. Padovan } 15290a708f8fSGustavo F. Padovan 1530ab0ff76dSLuiz Augusto von Dentz static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, 1531ab0ff76dSLuiz Augusto von Dentz struct msghdr *msg, size_t len, 1532ab0ff76dSLuiz Augusto von Dentz u16 control, u16 sdulen) 15330a708f8fSGustavo F. Padovan { 153447d1ec61SGustavo F. Padovan struct sock *sk = chan->sk; 15358c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 15360a708f8fSGustavo F. Padovan struct sk_buff *skb; 15370a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE + 2; 15380a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 15390a708f8fSGustavo F. Padovan 15400a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 15410a708f8fSGustavo F. Padovan 15420a708f8fSGustavo F. Padovan if (!conn) 15430a708f8fSGustavo F. Padovan return ERR_PTR(-ENOTCONN); 15440a708f8fSGustavo F. Padovan 15450a708f8fSGustavo F. Padovan if (sdulen) 15460a708f8fSGustavo F. Padovan hlen += 2; 15470a708f8fSGustavo F. Padovan 154847d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 15490a708f8fSGustavo F. Padovan hlen += 2; 15500a708f8fSGustavo F. Padovan 15510a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 15520a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 15530a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 15540a708f8fSGustavo F. Padovan if (!skb) 15550a708f8fSGustavo F. Padovan return ERR_PTR(err); 15560a708f8fSGustavo F. Padovan 15570a708f8fSGustavo F. Padovan /* Create L2CAP header */ 15580a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1559fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 15600a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 15610a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb_put(skb, 2)); 15620a708f8fSGustavo F. Padovan if (sdulen) 15630a708f8fSGustavo F. Padovan put_unaligned_le16(sdulen, skb_put(skb, 2)); 15640a708f8fSGustavo F. Padovan 15650a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 15660a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 15670a708f8fSGustavo F. Padovan kfree_skb(skb); 15680a708f8fSGustavo F. Padovan return ERR_PTR(err); 15690a708f8fSGustavo F. Padovan } 15700a708f8fSGustavo F. Padovan 157147d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 15720a708f8fSGustavo F. Padovan put_unaligned_le16(0, skb_put(skb, 2)); 15730a708f8fSGustavo F. Padovan 15740a708f8fSGustavo F. Padovan bt_cb(skb)->retries = 0; 15750a708f8fSGustavo F. Padovan return skb; 15760a708f8fSGustavo F. Padovan } 15770a708f8fSGustavo F. Padovan 157867c9e840SSzymon Janc static int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 15790a708f8fSGustavo F. Padovan { 15800a708f8fSGustavo F. Padovan struct sk_buff *skb; 15810a708f8fSGustavo F. Padovan struct sk_buff_head sar_queue; 15820a708f8fSGustavo F. Padovan u16 control; 15830a708f8fSGustavo F. Padovan size_t size = 0; 15840a708f8fSGustavo F. Padovan 15850a708f8fSGustavo F. Padovan skb_queue_head_init(&sar_queue); 15867e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_START); 158747d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len); 15880a708f8fSGustavo F. Padovan if (IS_ERR(skb)) 15890a708f8fSGustavo F. Padovan return PTR_ERR(skb); 15900a708f8fSGustavo F. Padovan 15910a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 15922c03a7a4SGustavo F. Padovan len -= chan->remote_mps; 15932c03a7a4SGustavo F. Padovan size += chan->remote_mps; 15940a708f8fSGustavo F. Padovan 15950a708f8fSGustavo F. Padovan while (len > 0) { 15960a708f8fSGustavo F. Padovan size_t buflen; 15970a708f8fSGustavo F. Padovan 15982c03a7a4SGustavo F. Padovan if (len > chan->remote_mps) { 15997e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_CONTINUE); 16002c03a7a4SGustavo F. Padovan buflen = chan->remote_mps; 16010a708f8fSGustavo F. Padovan } else { 16027e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_END); 16030a708f8fSGustavo F. Padovan buflen = len; 16040a708f8fSGustavo F. Padovan } 16050a708f8fSGustavo F. Padovan 160647d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0); 16070a708f8fSGustavo F. Padovan if (IS_ERR(skb)) { 16080a708f8fSGustavo F. Padovan skb_queue_purge(&sar_queue); 16090a708f8fSGustavo F. Padovan return PTR_ERR(skb); 16100a708f8fSGustavo F. Padovan } 16110a708f8fSGustavo F. Padovan 16120a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 16130a708f8fSGustavo F. Padovan len -= buflen; 16140a708f8fSGustavo F. Padovan size += buflen; 16150a708f8fSGustavo F. Padovan } 161658d35f87SGustavo F. Padovan skb_queue_splice_tail(&sar_queue, &chan->tx_q); 161758d35f87SGustavo F. Padovan if (chan->tx_send_head == NULL) 161858d35f87SGustavo F. Padovan chan->tx_send_head = sar_queue.next; 16190a708f8fSGustavo F. Padovan 16200a708f8fSGustavo F. Padovan return size; 16210a708f8fSGustavo F. Padovan } 16220a708f8fSGustavo F. Padovan 16239a91a04aSGustavo F. Padovan int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 16249a91a04aSGustavo F. Padovan { 16259a91a04aSGustavo F. Padovan struct sk_buff *skb; 16269a91a04aSGustavo F. Padovan u16 control; 16279a91a04aSGustavo F. Padovan int err; 16289a91a04aSGustavo F. Padovan 16299a91a04aSGustavo F. Padovan /* Connectionless channel */ 1630715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { 16319a91a04aSGustavo F. Padovan skb = l2cap_create_connless_pdu(chan, msg, len); 16329a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 16339a91a04aSGustavo F. Padovan return PTR_ERR(skb); 16349a91a04aSGustavo F. Padovan 16359a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 16369a91a04aSGustavo F. Padovan return len; 16379a91a04aSGustavo F. Padovan } 16389a91a04aSGustavo F. Padovan 16399a91a04aSGustavo F. Padovan switch (chan->mode) { 16409a91a04aSGustavo F. Padovan case L2CAP_MODE_BASIC: 16419a91a04aSGustavo F. Padovan /* Check outgoing MTU */ 16429a91a04aSGustavo F. Padovan if (len > chan->omtu) 16439a91a04aSGustavo F. Padovan return -EMSGSIZE; 16449a91a04aSGustavo F. Padovan 16459a91a04aSGustavo F. Padovan /* Create a basic PDU */ 16469a91a04aSGustavo F. Padovan skb = l2cap_create_basic_pdu(chan, msg, len); 16479a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 16489a91a04aSGustavo F. Padovan return PTR_ERR(skb); 16499a91a04aSGustavo F. Padovan 16509a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 16519a91a04aSGustavo F. Padovan err = len; 16529a91a04aSGustavo F. Padovan break; 16539a91a04aSGustavo F. Padovan 16549a91a04aSGustavo F. Padovan case L2CAP_MODE_ERTM: 16559a91a04aSGustavo F. Padovan case L2CAP_MODE_STREAMING: 16569a91a04aSGustavo F. Padovan /* Entire SDU fits into one PDU */ 16579a91a04aSGustavo F. Padovan if (len <= chan->remote_mps) { 16587e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_UNSEGMENTED); 16599a91a04aSGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, len, control, 16609a91a04aSGustavo F. Padovan 0); 16619a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 16629a91a04aSGustavo F. Padovan return PTR_ERR(skb); 16639a91a04aSGustavo F. Padovan 16649a91a04aSGustavo F. Padovan __skb_queue_tail(&chan->tx_q, skb); 16659a91a04aSGustavo F. Padovan 16669a91a04aSGustavo F. Padovan if (chan->tx_send_head == NULL) 16679a91a04aSGustavo F. Padovan chan->tx_send_head = skb; 16689a91a04aSGustavo F. Padovan 16699a91a04aSGustavo F. Padovan } else { 16709a91a04aSGustavo F. Padovan /* Segment SDU into multiples PDUs */ 16719a91a04aSGustavo F. Padovan err = l2cap_sar_segment_sdu(chan, msg, len); 16729a91a04aSGustavo F. Padovan if (err < 0) 16739a91a04aSGustavo F. Padovan return err; 16749a91a04aSGustavo F. Padovan } 16759a91a04aSGustavo F. Padovan 16769a91a04aSGustavo F. Padovan if (chan->mode == L2CAP_MODE_STREAMING) { 16779a91a04aSGustavo F. Padovan l2cap_streaming_send(chan); 16789a91a04aSGustavo F. Padovan err = len; 16799a91a04aSGustavo F. Padovan break; 16809a91a04aSGustavo F. Padovan } 16819a91a04aSGustavo F. Padovan 1682e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 1683e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 16849a91a04aSGustavo F. Padovan err = len; 16859a91a04aSGustavo F. Padovan break; 16869a91a04aSGustavo F. Padovan } 16879a91a04aSGustavo F. Padovan 16889a91a04aSGustavo F. Padovan err = l2cap_ertm_send(chan); 16899a91a04aSGustavo F. Padovan if (err >= 0) 16909a91a04aSGustavo F. Padovan err = len; 16919a91a04aSGustavo F. Padovan 16929a91a04aSGustavo F. Padovan break; 16939a91a04aSGustavo F. Padovan 16949a91a04aSGustavo F. Padovan default: 16959a91a04aSGustavo F. Padovan BT_DBG("bad state %1.1x", chan->mode); 16969a91a04aSGustavo F. Padovan err = -EBADFD; 16979a91a04aSGustavo F. Padovan } 16989a91a04aSGustavo F. Padovan 16999a91a04aSGustavo F. Padovan return err; 17009a91a04aSGustavo F. Padovan } 17019a91a04aSGustavo F. Padovan 17020a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */ 17030a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) 17040a708f8fSGustavo F. Padovan { 17050a708f8fSGustavo F. Padovan struct sk_buff *nskb; 170648454079SGustavo F. Padovan struct l2cap_chan *chan; 17070a708f8fSGustavo F. Padovan 17080a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 17090a708f8fSGustavo F. Padovan 1710baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 1711baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 171248454079SGustavo F. Padovan struct sock *sk = chan->sk; 1713715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_RAW) 17140a708f8fSGustavo F. Padovan continue; 17150a708f8fSGustavo F. Padovan 17160a708f8fSGustavo F. Padovan /* Don't send frame to the socket it came from */ 17170a708f8fSGustavo F. Padovan if (skb->sk == sk) 17180a708f8fSGustavo F. Padovan continue; 17190a708f8fSGustavo F. Padovan nskb = skb_clone(skb, GFP_ATOMIC); 17200a708f8fSGustavo F. Padovan if (!nskb) 17210a708f8fSGustavo F. Padovan continue; 17220a708f8fSGustavo F. Padovan 172323070494SGustavo F. Padovan if (chan->ops->recv(chan->data, nskb)) 17240a708f8fSGustavo F. Padovan kfree_skb(nskb); 17250a708f8fSGustavo F. Padovan } 1726baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 17270a708f8fSGustavo F. Padovan } 17280a708f8fSGustavo F. Padovan 17290a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */ 17300a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 17310a708f8fSGustavo F. Padovan u8 code, u8 ident, u16 dlen, void *data) 17320a708f8fSGustavo F. Padovan { 17330a708f8fSGustavo F. Padovan struct sk_buff *skb, **frag; 17340a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr *cmd; 17350a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 17360a708f8fSGustavo F. Padovan int len, count; 17370a708f8fSGustavo F. Padovan 17380a708f8fSGustavo F. Padovan BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", 17390a708f8fSGustavo F. Padovan conn, code, ident, dlen); 17400a708f8fSGustavo F. Padovan 17410a708f8fSGustavo F. Padovan len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; 17420a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 17430a708f8fSGustavo F. Padovan 17440a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 17450a708f8fSGustavo F. Padovan if (!skb) 17460a708f8fSGustavo F. Padovan return NULL; 17470a708f8fSGustavo F. Padovan 17480a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 17490a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); 17503300d9a9SClaudio Takahasi 17513300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 17523300d9a9SClaudio Takahasi lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); 17533300d9a9SClaudio Takahasi else 17540a708f8fSGustavo F. Padovan lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); 17550a708f8fSGustavo F. Padovan 17560a708f8fSGustavo F. Padovan cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); 17570a708f8fSGustavo F. Padovan cmd->code = code; 17580a708f8fSGustavo F. Padovan cmd->ident = ident; 17590a708f8fSGustavo F. Padovan cmd->len = cpu_to_le16(dlen); 17600a708f8fSGustavo F. Padovan 17610a708f8fSGustavo F. Padovan if (dlen) { 17620a708f8fSGustavo F. Padovan count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; 17630a708f8fSGustavo F. Padovan memcpy(skb_put(skb, count), data, count); 17640a708f8fSGustavo F. Padovan data += count; 17650a708f8fSGustavo F. Padovan } 17660a708f8fSGustavo F. Padovan 17670a708f8fSGustavo F. Padovan len -= skb->len; 17680a708f8fSGustavo F. Padovan 17690a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 17700a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 17710a708f8fSGustavo F. Padovan while (len) { 17720a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 17730a708f8fSGustavo F. Padovan 17740a708f8fSGustavo F. Padovan *frag = bt_skb_alloc(count, GFP_ATOMIC); 17750a708f8fSGustavo F. Padovan if (!*frag) 17760a708f8fSGustavo F. Padovan goto fail; 17770a708f8fSGustavo F. Padovan 17780a708f8fSGustavo F. Padovan memcpy(skb_put(*frag, count), data, count); 17790a708f8fSGustavo F. Padovan 17800a708f8fSGustavo F. Padovan len -= count; 17810a708f8fSGustavo F. Padovan data += count; 17820a708f8fSGustavo F. Padovan 17830a708f8fSGustavo F. Padovan frag = &(*frag)->next; 17840a708f8fSGustavo F. Padovan } 17850a708f8fSGustavo F. Padovan 17860a708f8fSGustavo F. Padovan return skb; 17870a708f8fSGustavo F. Padovan 17880a708f8fSGustavo F. Padovan fail: 17890a708f8fSGustavo F. Padovan kfree_skb(skb); 17900a708f8fSGustavo F. Padovan return NULL; 17910a708f8fSGustavo F. Padovan } 17920a708f8fSGustavo F. Padovan 17930a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) 17940a708f8fSGustavo F. Padovan { 17950a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 17960a708f8fSGustavo F. Padovan int len; 17970a708f8fSGustavo F. Padovan 17980a708f8fSGustavo F. Padovan len = L2CAP_CONF_OPT_SIZE + opt->len; 17990a708f8fSGustavo F. Padovan *ptr += len; 18000a708f8fSGustavo F. Padovan 18010a708f8fSGustavo F. Padovan *type = opt->type; 18020a708f8fSGustavo F. Padovan *olen = opt->len; 18030a708f8fSGustavo F. Padovan 18040a708f8fSGustavo F. Padovan switch (opt->len) { 18050a708f8fSGustavo F. Padovan case 1: 18060a708f8fSGustavo F. Padovan *val = *((u8 *) opt->val); 18070a708f8fSGustavo F. Padovan break; 18080a708f8fSGustavo F. Padovan 18090a708f8fSGustavo F. Padovan case 2: 18100a708f8fSGustavo F. Padovan *val = get_unaligned_le16(opt->val); 18110a708f8fSGustavo F. Padovan break; 18120a708f8fSGustavo F. Padovan 18130a708f8fSGustavo F. Padovan case 4: 18140a708f8fSGustavo F. Padovan *val = get_unaligned_le32(opt->val); 18150a708f8fSGustavo F. Padovan break; 18160a708f8fSGustavo F. Padovan 18170a708f8fSGustavo F. Padovan default: 18180a708f8fSGustavo F. Padovan *val = (unsigned long) opt->val; 18190a708f8fSGustavo F. Padovan break; 18200a708f8fSGustavo F. Padovan } 18210a708f8fSGustavo F. Padovan 18220a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); 18230a708f8fSGustavo F. Padovan return len; 18240a708f8fSGustavo F. Padovan } 18250a708f8fSGustavo F. Padovan 18260a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) 18270a708f8fSGustavo F. Padovan { 18280a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 18290a708f8fSGustavo F. Padovan 18300a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); 18310a708f8fSGustavo F. Padovan 18320a708f8fSGustavo F. Padovan opt->type = type; 18330a708f8fSGustavo F. Padovan opt->len = len; 18340a708f8fSGustavo F. Padovan 18350a708f8fSGustavo F. Padovan switch (len) { 18360a708f8fSGustavo F. Padovan case 1: 18370a708f8fSGustavo F. Padovan *((u8 *) opt->val) = val; 18380a708f8fSGustavo F. Padovan break; 18390a708f8fSGustavo F. Padovan 18400a708f8fSGustavo F. Padovan case 2: 18410a708f8fSGustavo F. Padovan put_unaligned_le16(val, opt->val); 18420a708f8fSGustavo F. Padovan break; 18430a708f8fSGustavo F. Padovan 18440a708f8fSGustavo F. Padovan case 4: 18450a708f8fSGustavo F. Padovan put_unaligned_le32(val, opt->val); 18460a708f8fSGustavo F. Padovan break; 18470a708f8fSGustavo F. Padovan 18480a708f8fSGustavo F. Padovan default: 18490a708f8fSGustavo F. Padovan memcpy(opt->val, (void *) val, len); 18500a708f8fSGustavo F. Padovan break; 18510a708f8fSGustavo F. Padovan } 18520a708f8fSGustavo F. Padovan 18530a708f8fSGustavo F. Padovan *ptr += L2CAP_CONF_OPT_SIZE + len; 18540a708f8fSGustavo F. Padovan } 18550a708f8fSGustavo F. Padovan 18560a708f8fSGustavo F. Padovan static void l2cap_ack_timeout(unsigned long arg) 18570a708f8fSGustavo F. Padovan { 1858525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 18590a708f8fSGustavo F. Padovan 1860525cd185SGustavo F. Padovan bh_lock_sock(chan->sk); 1861525cd185SGustavo F. Padovan l2cap_send_ack(chan); 1862525cd185SGustavo F. Padovan bh_unlock_sock(chan->sk); 18630a708f8fSGustavo F. Padovan } 18640a708f8fSGustavo F. Padovan 1865525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan) 18660a708f8fSGustavo F. Padovan { 1867525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 1868525cd185SGustavo F. Padovan 186942e5c802SGustavo F. Padovan chan->expected_ack_seq = 0; 18706a026610SGustavo F. Padovan chan->unacked_frames = 0; 187142e5c802SGustavo F. Padovan chan->buffer_seq = 0; 18726a026610SGustavo F. Padovan chan->num_acked = 0; 18736a026610SGustavo F. Padovan chan->frames_sent = 0; 18740a708f8fSGustavo F. Padovan 1875e92c8e70SGustavo F. Padovan setup_timer(&chan->retrans_timer, l2cap_retrans_timeout, 1876e92c8e70SGustavo F. Padovan (unsigned long) chan); 1877e92c8e70SGustavo F. Padovan setup_timer(&chan->monitor_timer, l2cap_monitor_timeout, 1878e92c8e70SGustavo F. Padovan (unsigned long) chan); 1879e92c8e70SGustavo F. Padovan setup_timer(&chan->ack_timer, l2cap_ack_timeout, (unsigned long) chan); 18800a708f8fSGustavo F. Padovan 1881f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->srej_q); 18820a708f8fSGustavo F. Padovan 188339d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 188439d5a3eeSGustavo F. Padovan 18850a708f8fSGustavo F. Padovan 18860a708f8fSGustavo F. Padovan sk->sk_backlog_rcv = l2cap_ertm_data_rcv; 18870a708f8fSGustavo F. Padovan } 18880a708f8fSGustavo F. Padovan 18890a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) 18900a708f8fSGustavo F. Padovan { 18910a708f8fSGustavo F. Padovan switch (mode) { 18920a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 18930a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 18940a708f8fSGustavo F. Padovan if (l2cap_mode_supported(mode, remote_feat_mask)) 18950a708f8fSGustavo F. Padovan return mode; 18960a708f8fSGustavo F. Padovan /* fall through */ 18970a708f8fSGustavo F. Padovan default: 18980a708f8fSGustavo F. Padovan return L2CAP_MODE_BASIC; 18990a708f8fSGustavo F. Padovan } 19000a708f8fSGustavo F. Padovan } 19010a708f8fSGustavo F. Padovan 19026327eb98SAndrei Emeltchenko static inline bool __l2cap_ews_supported(struct l2cap_chan *chan) 19036327eb98SAndrei Emeltchenko { 19046327eb98SAndrei Emeltchenko return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; 19056327eb98SAndrei Emeltchenko } 19066327eb98SAndrei Emeltchenko 19076327eb98SAndrei Emeltchenko static inline void l2cap_txwin_setup(struct l2cap_chan *chan) 19086327eb98SAndrei Emeltchenko { 19096327eb98SAndrei Emeltchenko if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && 19106327eb98SAndrei Emeltchenko __l2cap_ews_supported(chan)) 19116327eb98SAndrei Emeltchenko /* use extended control field */ 19126327eb98SAndrei Emeltchenko set_bit(FLAG_EXT_CTRL, &chan->flags); 19136327eb98SAndrei Emeltchenko else 19146327eb98SAndrei Emeltchenko chan->tx_win = min_t(u16, chan->tx_win, 19156327eb98SAndrei Emeltchenko L2CAP_DEFAULT_TX_WINDOW); 19166327eb98SAndrei Emeltchenko } 19176327eb98SAndrei Emeltchenko 1918710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) 19190a708f8fSGustavo F. Padovan { 19200a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 19210c1bc5c6SGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = chan->mode }; 19220a708f8fSGustavo F. Padovan void *ptr = req->data; 19230a708f8fSGustavo F. Padovan 192449208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 19250a708f8fSGustavo F. Padovan 192673ffa904SGustavo F. Padovan if (chan->num_conf_req || chan->num_conf_rsp) 19270a708f8fSGustavo F. Padovan goto done; 19280a708f8fSGustavo F. Padovan 19290c1bc5c6SGustavo F. Padovan switch (chan->mode) { 19300a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 19310a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 1932c1360a1cSGustavo F. Padovan if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) 19330a708f8fSGustavo F. Padovan break; 19340a708f8fSGustavo F. Padovan 19350a708f8fSGustavo F. Padovan /* fall through */ 19360a708f8fSGustavo F. Padovan default: 19378c1d787bSGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); 19380a708f8fSGustavo F. Padovan break; 19390a708f8fSGustavo F. Padovan } 19400a708f8fSGustavo F. Padovan 19410a708f8fSGustavo F. Padovan done: 19420c1bc5c6SGustavo F. Padovan if (chan->imtu != L2CAP_DEFAULT_MTU) 19430c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 19440a708f8fSGustavo F. Padovan 19450c1bc5c6SGustavo F. Padovan switch (chan->mode) { 19460a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 19478c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && 19488c1d787bSGustavo F. Padovan !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) 19490a708f8fSGustavo F. Padovan break; 19500a708f8fSGustavo F. Padovan 19510a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_BASIC; 19520a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 19530a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 19540a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 19550a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 19560a708f8fSGustavo F. Padovan rfc.max_pdu_size = 0; 19570a708f8fSGustavo F. Padovan 19580a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 19590a708f8fSGustavo F. Padovan (unsigned long) &rfc); 19600a708f8fSGustavo F. Padovan break; 19610a708f8fSGustavo F. Padovan 19620a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 19630a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_ERTM; 196447d1ec61SGustavo F. Padovan rfc.max_transmit = chan->max_tx; 19650a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 19660a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 19670a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); 19688c1d787bSGustavo F. Padovan if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10) 19698c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 19700a708f8fSGustavo F. Padovan 19716327eb98SAndrei Emeltchenko l2cap_txwin_setup(chan); 19726327eb98SAndrei Emeltchenko 19736327eb98SAndrei Emeltchenko rfc.txwin_size = min_t(u16, chan->tx_win, 19746327eb98SAndrei Emeltchenko L2CAP_DEFAULT_TX_WINDOW); 19756327eb98SAndrei Emeltchenko 19760a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 19770a708f8fSGustavo F. Padovan (unsigned long) &rfc); 19780a708f8fSGustavo F. Padovan 19798c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 19800a708f8fSGustavo F. Padovan break; 19810a708f8fSGustavo F. Padovan 198247d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 1983c1360a1cSGustavo F. Padovan test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { 198447d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 198547d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 19860a708f8fSGustavo F. Padovan } 19876327eb98SAndrei Emeltchenko 19886327eb98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 19896327eb98SAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, 19906327eb98SAndrei Emeltchenko chan->tx_win); 19910a708f8fSGustavo F. Padovan break; 19920a708f8fSGustavo F. Padovan 19930a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 19940a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_STREAMING; 19950a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 19960a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 19970a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 19980a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 19990a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); 20008c1d787bSGustavo F. Padovan if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10) 20018c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 20020a708f8fSGustavo F. Padovan 20030a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 20040a708f8fSGustavo F. Padovan (unsigned long) &rfc); 20050a708f8fSGustavo F. Padovan 20068c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 20070a708f8fSGustavo F. Padovan break; 20080a708f8fSGustavo F. Padovan 200947d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 2010c1360a1cSGustavo F. Padovan test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { 201147d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 201247d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 20130a708f8fSGustavo F. Padovan } 20140a708f8fSGustavo F. Padovan break; 20150a708f8fSGustavo F. Padovan } 20160a708f8fSGustavo F. Padovan 2017fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 20180a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0); 20190a708f8fSGustavo F. Padovan 20200a708f8fSGustavo F. Padovan return ptr - data; 20210a708f8fSGustavo F. Padovan } 20220a708f8fSGustavo F. Padovan 202373ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) 20240a708f8fSGustavo F. Padovan { 20250a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 20260a708f8fSGustavo F. Padovan void *ptr = rsp->data; 202773ffa904SGustavo F. Padovan void *req = chan->conf_req; 202873ffa904SGustavo F. Padovan int len = chan->conf_len; 20290a708f8fSGustavo F. Padovan int type, hint, olen; 20300a708f8fSGustavo F. Padovan unsigned long val; 20310a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 20320a708f8fSGustavo F. Padovan u16 mtu = L2CAP_DEFAULT_MTU; 20330a708f8fSGustavo F. Padovan u16 result = L2CAP_CONF_SUCCESS; 20340a708f8fSGustavo F. Padovan 203573ffa904SGustavo F. Padovan BT_DBG("chan %p", chan); 20360a708f8fSGustavo F. Padovan 20370a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 20380a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&req, &type, &olen, &val); 20390a708f8fSGustavo F. Padovan 20400a708f8fSGustavo F. Padovan hint = type & L2CAP_CONF_HINT; 20410a708f8fSGustavo F. Padovan type &= L2CAP_CONF_MASK; 20420a708f8fSGustavo F. Padovan 20430a708f8fSGustavo F. Padovan switch (type) { 20440a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 20450a708f8fSGustavo F. Padovan mtu = val; 20460a708f8fSGustavo F. Padovan break; 20470a708f8fSGustavo F. Padovan 20480a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 20490c1bc5c6SGustavo F. Padovan chan->flush_to = val; 20500a708f8fSGustavo F. Padovan break; 20510a708f8fSGustavo F. Padovan 20520a708f8fSGustavo F. Padovan case L2CAP_CONF_QOS: 20530a708f8fSGustavo F. Padovan break; 20540a708f8fSGustavo F. Padovan 20550a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 20560a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 20570a708f8fSGustavo F. Padovan memcpy(&rfc, (void *) val, olen); 20580a708f8fSGustavo F. Padovan break; 20590a708f8fSGustavo F. Padovan 20600a708f8fSGustavo F. Padovan case L2CAP_CONF_FCS: 20610a708f8fSGustavo F. Padovan if (val == L2CAP_FCS_NONE) 2062c1360a1cSGustavo F. Padovan set_bit(CONF_NO_FCS_RECV, &chan->conf_state); 20630a708f8fSGustavo F. Padovan 20640a708f8fSGustavo F. Padovan break; 20650a708f8fSGustavo F. Padovan 20666327eb98SAndrei Emeltchenko case L2CAP_CONF_EWS: 20676327eb98SAndrei Emeltchenko if (!enable_hs) 20686327eb98SAndrei Emeltchenko return -ECONNREFUSED; 20696327eb98SAndrei Emeltchenko 20706327eb98SAndrei Emeltchenko set_bit(FLAG_EXT_CTRL, &chan->flags); 20716327eb98SAndrei Emeltchenko set_bit(CONF_EWS_RECV, &chan->conf_state); 20726327eb98SAndrei Emeltchenko chan->remote_tx_win = val; 20736327eb98SAndrei Emeltchenko break; 20746327eb98SAndrei Emeltchenko 20750a708f8fSGustavo F. Padovan default: 20760a708f8fSGustavo F. Padovan if (hint) 20770a708f8fSGustavo F. Padovan break; 20780a708f8fSGustavo F. Padovan 20790a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNKNOWN; 20800a708f8fSGustavo F. Padovan *((u8 *) ptr++) = type; 20810a708f8fSGustavo F. Padovan break; 20820a708f8fSGustavo F. Padovan } 20830a708f8fSGustavo F. Padovan } 20840a708f8fSGustavo F. Padovan 208573ffa904SGustavo F. Padovan if (chan->num_conf_rsp || chan->num_conf_req > 1) 20860a708f8fSGustavo F. Padovan goto done; 20870a708f8fSGustavo F. Padovan 20880c1bc5c6SGustavo F. Padovan switch (chan->mode) { 20890a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 20900a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 2091c1360a1cSGustavo F. Padovan if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { 20920c1bc5c6SGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, 20938c1d787bSGustavo F. Padovan chan->conn->feat_mask); 20940a708f8fSGustavo F. Padovan break; 20950a708f8fSGustavo F. Padovan } 20960a708f8fSGustavo F. Padovan 20970c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) 20980a708f8fSGustavo F. Padovan return -ECONNREFUSED; 20990a708f8fSGustavo F. Padovan 21000a708f8fSGustavo F. Padovan break; 21010a708f8fSGustavo F. Padovan } 21020a708f8fSGustavo F. Padovan 21030a708f8fSGustavo F. Padovan done: 21040c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) { 21050a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 21060c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 21070a708f8fSGustavo F. Padovan 210873ffa904SGustavo F. Padovan if (chan->num_conf_rsp == 1) 21090a708f8fSGustavo F. Padovan return -ECONNREFUSED; 21100a708f8fSGustavo F. Padovan 21110a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 21120a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 21130a708f8fSGustavo F. Padovan } 21140a708f8fSGustavo F. Padovan 21150a708f8fSGustavo F. Padovan 21160a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) { 21170a708f8fSGustavo F. Padovan /* Configure output options and let the other side know 21180a708f8fSGustavo F. Padovan * which ones we don't like. */ 21190a708f8fSGustavo F. Padovan 21200a708f8fSGustavo F. Padovan if (mtu < L2CAP_DEFAULT_MIN_MTU) 21210a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 21220a708f8fSGustavo F. Padovan else { 21230c1bc5c6SGustavo F. Padovan chan->omtu = mtu; 2124c1360a1cSGustavo F. Padovan set_bit(CONF_MTU_DONE, &chan->conf_state); 21250a708f8fSGustavo F. Padovan } 21260c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); 21270a708f8fSGustavo F. Padovan 21280a708f8fSGustavo F. Padovan switch (rfc.mode) { 21290a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 213047d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2131c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 21320a708f8fSGustavo F. Padovan break; 21330a708f8fSGustavo F. Padovan 21340a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 21356327eb98SAndrei Emeltchenko if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) 21362c03a7a4SGustavo F. Padovan chan->remote_tx_win = rfc.txwin_size; 21376327eb98SAndrei Emeltchenko else 21386327eb98SAndrei Emeltchenko rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; 21396327eb98SAndrei Emeltchenko 21402c03a7a4SGustavo F. Padovan chan->remote_max_tx = rfc.max_transmit; 21410a708f8fSGustavo F. Padovan 21428c1d787bSGustavo F. Padovan if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10) 21438c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 21440a708f8fSGustavo F. Padovan 21452c03a7a4SGustavo F. Padovan chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); 21460a708f8fSGustavo F. Padovan 21470a708f8fSGustavo F. Padovan rfc.retrans_timeout = 21480a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO); 21490a708f8fSGustavo F. Padovan rfc.monitor_timeout = 21500a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO); 21510a708f8fSGustavo F. Padovan 2152c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 21530a708f8fSGustavo F. Padovan 21540a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 21550a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 21560a708f8fSGustavo F. Padovan 21570a708f8fSGustavo F. Padovan break; 21580a708f8fSGustavo F. Padovan 21590a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 21608c1d787bSGustavo F. Padovan if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10) 21618c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 21620a708f8fSGustavo F. Padovan 21632c03a7a4SGustavo F. Padovan chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); 21640a708f8fSGustavo F. Padovan 2165c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 21660a708f8fSGustavo F. Padovan 21670a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 21680a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 21690a708f8fSGustavo F. Padovan 21700a708f8fSGustavo F. Padovan break; 21710a708f8fSGustavo F. Padovan 21720a708f8fSGustavo F. Padovan default: 21730a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 21740a708f8fSGustavo F. Padovan 21750a708f8fSGustavo F. Padovan memset(&rfc, 0, sizeof(rfc)); 21760c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 21770a708f8fSGustavo F. Padovan } 21780a708f8fSGustavo F. Padovan 21790a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) 2180c1360a1cSGustavo F. Padovan set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 21810a708f8fSGustavo F. Padovan } 2182fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 21830a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 21840a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(0x0000); 21850a708f8fSGustavo F. Padovan 21860a708f8fSGustavo F. Padovan return ptr - data; 21870a708f8fSGustavo F. Padovan } 21880a708f8fSGustavo F. Padovan 2189b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result) 21900a708f8fSGustavo F. Padovan { 21910a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 21920a708f8fSGustavo F. Padovan void *ptr = req->data; 21930a708f8fSGustavo F. Padovan int type, olen; 21940a708f8fSGustavo F. Padovan unsigned long val; 21950a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 21960a708f8fSGustavo F. Padovan 2197fe4128e0SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data); 21980a708f8fSGustavo F. Padovan 21990a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 22000a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 22010a708f8fSGustavo F. Padovan 22020a708f8fSGustavo F. Padovan switch (type) { 22030a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 22040a708f8fSGustavo F. Padovan if (val < L2CAP_DEFAULT_MIN_MTU) { 22050a708f8fSGustavo F. Padovan *result = L2CAP_CONF_UNACCEPT; 22060c1bc5c6SGustavo F. Padovan chan->imtu = L2CAP_DEFAULT_MIN_MTU; 22070a708f8fSGustavo F. Padovan } else 22080c1bc5c6SGustavo F. Padovan chan->imtu = val; 22090c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 22100a708f8fSGustavo F. Padovan break; 22110a708f8fSGustavo F. Padovan 22120a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 22130c1bc5c6SGustavo F. Padovan chan->flush_to = val; 22140a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 22150c1bc5c6SGustavo F. Padovan 2, chan->flush_to); 22160a708f8fSGustavo F. Padovan break; 22170a708f8fSGustavo F. Padovan 22180a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 22190a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 22200a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 22210a708f8fSGustavo F. Padovan 2222c1360a1cSGustavo F. Padovan if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && 22230c1bc5c6SGustavo F. Padovan rfc.mode != chan->mode) 22240a708f8fSGustavo F. Padovan return -ECONNREFUSED; 22250a708f8fSGustavo F. Padovan 222647d1ec61SGustavo F. Padovan chan->fcs = 0; 22270a708f8fSGustavo F. Padovan 22280a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 22290a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 22300a708f8fSGustavo F. Padovan break; 22316327eb98SAndrei Emeltchenko 22326327eb98SAndrei Emeltchenko case L2CAP_CONF_EWS: 22336327eb98SAndrei Emeltchenko chan->tx_win = min_t(u16, val, 22346327eb98SAndrei Emeltchenko L2CAP_DEFAULT_EXT_WINDOW); 22356327eb98SAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 22366327eb98SAndrei Emeltchenko 2, chan->tx_win); 22376327eb98SAndrei Emeltchenko break; 22380a708f8fSGustavo F. Padovan } 22390a708f8fSGustavo F. Padovan } 22400a708f8fSGustavo F. Padovan 22410c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) 22420a708f8fSGustavo F. Padovan return -ECONNREFUSED; 22430a708f8fSGustavo F. Padovan 22440c1bc5c6SGustavo F. Padovan chan->mode = rfc.mode; 22450a708f8fSGustavo F. Padovan 22460a708f8fSGustavo F. Padovan if (*result == L2CAP_CONF_SUCCESS) { 22470a708f8fSGustavo F. Padovan switch (rfc.mode) { 22480a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 224947d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 225047d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 225147d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 22520a708f8fSGustavo F. Padovan break; 22530a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 225447d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 22550a708f8fSGustavo F. Padovan } 22560a708f8fSGustavo F. Padovan } 22570a708f8fSGustavo F. Padovan 2258fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 22590a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0x0000); 22600a708f8fSGustavo F. Padovan 22610a708f8fSGustavo F. Padovan return ptr - data; 22620a708f8fSGustavo F. Padovan } 22630a708f8fSGustavo F. Padovan 2264fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags) 22650a708f8fSGustavo F. Padovan { 22660a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 22670a708f8fSGustavo F. Padovan void *ptr = rsp->data; 22680a708f8fSGustavo F. Padovan 2269fe4128e0SGustavo F. Padovan BT_DBG("chan %p", chan); 22700a708f8fSGustavo F. Padovan 2271fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 22720a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 22730a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(flags); 22740a708f8fSGustavo F. Padovan 22750a708f8fSGustavo F. Padovan return ptr - data; 22760a708f8fSGustavo F. Padovan } 22770a708f8fSGustavo F. Padovan 22788c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) 2279710f9b0aSGustavo F. Padovan { 2280710f9b0aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 22818c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 2282710f9b0aSGustavo F. Padovan u8 buf[128]; 2283710f9b0aSGustavo F. Padovan 2284fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 2285fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 2286710f9b0aSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 2287710f9b0aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 2288710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 2289710f9b0aSGustavo F. Padovan L2CAP_CONN_RSP, sizeof(rsp), &rsp); 2290710f9b0aSGustavo F. Padovan 2291c1360a1cSGustavo F. Padovan if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) 2292710f9b0aSGustavo F. Padovan return; 2293710f9b0aSGustavo F. Padovan 2294710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 2295710f9b0aSGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 2296710f9b0aSGustavo F. Padovan chan->num_conf_req++; 2297710f9b0aSGustavo F. Padovan } 2298710f9b0aSGustavo F. Padovan 229947d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len) 23000a708f8fSGustavo F. Padovan { 23010a708f8fSGustavo F. Padovan int type, olen; 23020a708f8fSGustavo F. Padovan unsigned long val; 23030a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 23040a708f8fSGustavo F. Padovan 230547d1ec61SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); 23060a708f8fSGustavo F. Padovan 23070c1bc5c6SGustavo F. Padovan if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) 23080a708f8fSGustavo F. Padovan return; 23090a708f8fSGustavo F. Padovan 23100a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 23110a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 23120a708f8fSGustavo F. Padovan 23130a708f8fSGustavo F. Padovan switch (type) { 23140a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 23150a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 23160a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 23170a708f8fSGustavo F. Padovan goto done; 23180a708f8fSGustavo F. Padovan } 23190a708f8fSGustavo F. Padovan } 23200a708f8fSGustavo F. Padovan 23210a708f8fSGustavo F. Padovan done: 23220a708f8fSGustavo F. Padovan switch (rfc.mode) { 23230a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 232447d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 232547d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 232647d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 23270a708f8fSGustavo F. Padovan break; 23280a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 232947d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 23300a708f8fSGustavo F. Padovan } 23310a708f8fSGustavo F. Padovan } 23320a708f8fSGustavo F. Padovan 23330a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 23340a708f8fSGustavo F. Padovan { 2335e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; 23360a708f8fSGustavo F. Padovan 2337e2fd318eSIlia Kolomisnky if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) 23380a708f8fSGustavo F. Padovan return 0; 23390a708f8fSGustavo F. Padovan 23400a708f8fSGustavo F. Padovan if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && 23410a708f8fSGustavo F. Padovan cmd->ident == conn->info_ident) { 23420a708f8fSGustavo F. Padovan del_timer(&conn->info_timer); 23430a708f8fSGustavo F. Padovan 23440a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 23450a708f8fSGustavo F. Padovan conn->info_ident = 0; 23460a708f8fSGustavo F. Padovan 23470a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 23480a708f8fSGustavo F. Padovan } 23490a708f8fSGustavo F. Padovan 23500a708f8fSGustavo F. Padovan return 0; 23510a708f8fSGustavo F. Padovan } 23520a708f8fSGustavo F. Padovan 23530a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 23540a708f8fSGustavo F. Padovan { 23550a708f8fSGustavo F. Padovan struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; 23560a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 235723691d75SGustavo F. Padovan struct l2cap_chan *chan = NULL, *pchan; 23580a708f8fSGustavo F. Padovan struct sock *parent, *sk = NULL; 23590a708f8fSGustavo F. Padovan int result, status = L2CAP_CS_NO_INFO; 23600a708f8fSGustavo F. Padovan 23610a708f8fSGustavo F. Padovan u16 dcid = 0, scid = __le16_to_cpu(req->scid); 23620a708f8fSGustavo F. Padovan __le16 psm = req->psm; 23630a708f8fSGustavo F. Padovan 23640a708f8fSGustavo F. Padovan BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid); 23650a708f8fSGustavo F. Padovan 23660a708f8fSGustavo F. Padovan /* Check if we have socket listening on psm */ 236723691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src); 236823691d75SGustavo F. Padovan if (!pchan) { 23690a708f8fSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 23700a708f8fSGustavo F. Padovan goto sendresp; 23710a708f8fSGustavo F. Padovan } 23720a708f8fSGustavo F. Padovan 237323691d75SGustavo F. Padovan parent = pchan->sk; 237423691d75SGustavo F. Padovan 23750a708f8fSGustavo F. Padovan bh_lock_sock(parent); 23760a708f8fSGustavo F. Padovan 23770a708f8fSGustavo F. Padovan /* Check if the ACL is secure enough (if not SDP) */ 23780a708f8fSGustavo F. Padovan if (psm != cpu_to_le16(0x0001) && 23790a708f8fSGustavo F. Padovan !hci_conn_check_link_mode(conn->hcon)) { 23800a708f8fSGustavo F. Padovan conn->disc_reason = 0x05; 23810a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 23820a708f8fSGustavo F. Padovan goto response; 23830a708f8fSGustavo F. Padovan } 23840a708f8fSGustavo F. Padovan 23850a708f8fSGustavo F. Padovan result = L2CAP_CR_NO_MEM; 23860a708f8fSGustavo F. Padovan 23870a708f8fSGustavo F. Padovan /* Check for backlog size */ 23880a708f8fSGustavo F. Padovan if (sk_acceptq_is_full(parent)) { 23890a708f8fSGustavo F. Padovan BT_DBG("backlog full %d", parent->sk_ack_backlog); 23900a708f8fSGustavo F. Padovan goto response; 23910a708f8fSGustavo F. Padovan } 23920a708f8fSGustavo F. Padovan 239380808e43SGustavo F. Padovan chan = pchan->ops->new_connection(pchan->data); 239480808e43SGustavo F. Padovan if (!chan) 23950a708f8fSGustavo F. Padovan goto response; 23960a708f8fSGustavo F. Padovan 239780808e43SGustavo F. Padovan sk = chan->sk; 239880808e43SGustavo F. Padovan 2399baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 24000a708f8fSGustavo F. Padovan 24010a708f8fSGustavo F. Padovan /* Check if we already have channel with that dcid */ 2402baa7e1faSGustavo F. Padovan if (__l2cap_get_chan_by_dcid(conn, scid)) { 2403baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 24040a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 2405ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 24060a708f8fSGustavo F. Padovan goto response; 24070a708f8fSGustavo F. Padovan } 24080a708f8fSGustavo F. Padovan 24090a708f8fSGustavo F. Padovan hci_conn_hold(conn->hcon); 24100a708f8fSGustavo F. Padovan 24110a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->src, conn->src); 24120a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->dst, conn->dst); 2413fe4128e0SGustavo F. Padovan chan->psm = psm; 2414fe4128e0SGustavo F. Padovan chan->dcid = scid; 24150a708f8fSGustavo F. Padovan 2416d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 2417d1010240SGustavo F. Padovan 241848454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 241948454079SGustavo F. Padovan 2420fe4128e0SGustavo F. Padovan dcid = chan->scid; 24210a708f8fSGustavo F. Padovan 2422c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 24230a708f8fSGustavo F. Padovan 2424fc7f8a7eSGustavo F. Padovan chan->ident = cmd->ident; 24250a708f8fSGustavo F. Padovan 24260a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { 24274343478fSGustavo F. Padovan if (l2cap_check_security(chan)) { 24280a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 242989bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECT2); 24300a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 24310a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHOR_PEND; 24320a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 24330a708f8fSGustavo F. Padovan } else { 243489bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONFIG); 24350a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 24360a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 24370a708f8fSGustavo F. Padovan } 24380a708f8fSGustavo F. Padovan } else { 243989bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECT2); 24400a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 24410a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHEN_PEND; 24420a708f8fSGustavo F. Padovan } 24430a708f8fSGustavo F. Padovan } else { 244489bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECT2); 24450a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 24460a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 24470a708f8fSGustavo F. Padovan } 24480a708f8fSGustavo F. Padovan 2449baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 24500a708f8fSGustavo F. Padovan 24510a708f8fSGustavo F. Padovan response: 24520a708f8fSGustavo F. Padovan bh_unlock_sock(parent); 24530a708f8fSGustavo F. Padovan 24540a708f8fSGustavo F. Padovan sendresp: 24550a708f8fSGustavo F. Padovan rsp.scid = cpu_to_le16(scid); 24560a708f8fSGustavo F. Padovan rsp.dcid = cpu_to_le16(dcid); 24570a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 24580a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(status); 24590a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); 24600a708f8fSGustavo F. Padovan 24610a708f8fSGustavo F. Padovan if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { 24620a708f8fSGustavo F. Padovan struct l2cap_info_req info; 24630a708f8fSGustavo F. Padovan info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 24640a708f8fSGustavo F. Padovan 24650a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 24660a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 24670a708f8fSGustavo F. Padovan 24680a708f8fSGustavo F. Padovan mod_timer(&conn->info_timer, jiffies + 24690a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 24700a708f8fSGustavo F. Padovan 24710a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 24720a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(info), &info); 24730a708f8fSGustavo F. Padovan } 24740a708f8fSGustavo F. Padovan 2475c1360a1cSGustavo F. Padovan if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && 24760a708f8fSGustavo F. Padovan result == L2CAP_CR_SUCCESS) { 24770a708f8fSGustavo F. Padovan u8 buf[128]; 2478c1360a1cSGustavo F. Padovan set_bit(CONF_REQ_SENT, &chan->conf_state); 24790a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 248073ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 248173ffa904SGustavo F. Padovan chan->num_conf_req++; 24820a708f8fSGustavo F. Padovan } 24830a708f8fSGustavo F. Padovan 24840a708f8fSGustavo F. Padovan return 0; 24850a708f8fSGustavo F. Padovan } 24860a708f8fSGustavo F. Padovan 24870a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 24880a708f8fSGustavo F. Padovan { 24890a708f8fSGustavo F. Padovan struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; 24900a708f8fSGustavo F. Padovan u16 scid, dcid, result, status; 249148454079SGustavo F. Padovan struct l2cap_chan *chan; 24920a708f8fSGustavo F. Padovan struct sock *sk; 24930a708f8fSGustavo F. Padovan u8 req[128]; 24940a708f8fSGustavo F. Padovan 24950a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 24960a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 24970a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 24980a708f8fSGustavo F. Padovan status = __le16_to_cpu(rsp->status); 24990a708f8fSGustavo F. Padovan 25000a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status); 25010a708f8fSGustavo F. Padovan 25020a708f8fSGustavo F. Padovan if (scid) { 2503baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 250448454079SGustavo F. Padovan if (!chan) 25050a708f8fSGustavo F. Padovan return -EFAULT; 25060a708f8fSGustavo F. Padovan } else { 2507baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_ident(conn, cmd->ident); 250848454079SGustavo F. Padovan if (!chan) 25090a708f8fSGustavo F. Padovan return -EFAULT; 25100a708f8fSGustavo F. Padovan } 25110a708f8fSGustavo F. Padovan 251248454079SGustavo F. Padovan sk = chan->sk; 251348454079SGustavo F. Padovan 25140a708f8fSGustavo F. Padovan switch (result) { 25150a708f8fSGustavo F. Padovan case L2CAP_CR_SUCCESS: 251689bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONFIG); 2517fc7f8a7eSGustavo F. Padovan chan->ident = 0; 2518fe4128e0SGustavo F. Padovan chan->dcid = dcid; 2519c1360a1cSGustavo F. Padovan clear_bit(CONF_CONNECT_PEND, &chan->conf_state); 25200a708f8fSGustavo F. Padovan 2521c1360a1cSGustavo F. Padovan if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) 25220a708f8fSGustavo F. Padovan break; 25230a708f8fSGustavo F. Padovan 25240a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 252573ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, req), req); 252673ffa904SGustavo F. Padovan chan->num_conf_req++; 25270a708f8fSGustavo F. Padovan break; 25280a708f8fSGustavo F. Padovan 25290a708f8fSGustavo F. Padovan case L2CAP_CR_PEND: 2530c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 25310a708f8fSGustavo F. Padovan break; 25320a708f8fSGustavo F. Padovan 25330a708f8fSGustavo F. Padovan default: 25340a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 25350a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 253689bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_DISCONN); 2537c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 2538c9b66675SGustavo F. Padovan __set_chan_timer(chan, HZ / 5); 25390a708f8fSGustavo F. Padovan break; 25400a708f8fSGustavo F. Padovan } 25410a708f8fSGustavo F. Padovan 254248454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNREFUSED); 25430a708f8fSGustavo F. Padovan break; 25440a708f8fSGustavo F. Padovan } 25450a708f8fSGustavo F. Padovan 25460a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 25470a708f8fSGustavo F. Padovan return 0; 25480a708f8fSGustavo F. Padovan } 25490a708f8fSGustavo F. Padovan 255047d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan) 25510a708f8fSGustavo F. Padovan { 25520a708f8fSGustavo F. Padovan /* FCS is enabled only in ERTM or streaming mode, if one or both 25530a708f8fSGustavo F. Padovan * sides request it. 25540a708f8fSGustavo F. Padovan */ 25550c1bc5c6SGustavo F. Padovan if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) 255647d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2557c1360a1cSGustavo F. Padovan else if (!test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) 255847d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_CRC16; 25590a708f8fSGustavo F. Padovan } 25600a708f8fSGustavo F. Padovan 25610a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 25620a708f8fSGustavo F. Padovan { 25630a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; 25640a708f8fSGustavo F. Padovan u16 dcid, flags; 25650a708f8fSGustavo F. Padovan u8 rsp[64]; 256648454079SGustavo F. Padovan struct l2cap_chan *chan; 25670a708f8fSGustavo F. Padovan struct sock *sk; 25680a708f8fSGustavo F. Padovan int len; 25690a708f8fSGustavo F. Padovan 25700a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 25710a708f8fSGustavo F. Padovan flags = __le16_to_cpu(req->flags); 25720a708f8fSGustavo F. Padovan 25730a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); 25740a708f8fSGustavo F. Padovan 2575baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 257648454079SGustavo F. Padovan if (!chan) 25770a708f8fSGustavo F. Padovan return -ENOENT; 25780a708f8fSGustavo F. Padovan 257948454079SGustavo F. Padovan sk = chan->sk; 258048454079SGustavo F. Padovan 2581033b1142SDavid S. Miller if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { 2582e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_cid rej; 25830a708f8fSGustavo F. Padovan 2584e2fd318eSIlia Kolomisnky rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID); 2585e2fd318eSIlia Kolomisnky rej.scid = cpu_to_le16(chan->scid); 2586e2fd318eSIlia Kolomisnky rej.dcid = cpu_to_le16(chan->dcid); 2587e2fd318eSIlia Kolomisnky 25880a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, 25890a708f8fSGustavo F. Padovan sizeof(rej), &rej); 25900a708f8fSGustavo F. Padovan goto unlock; 25910a708f8fSGustavo F. Padovan } 25920a708f8fSGustavo F. Padovan 25930a708f8fSGustavo F. Padovan /* Reject if config buffer is too small. */ 25940a708f8fSGustavo F. Padovan len = cmd_len - sizeof(*req); 25957ac28817SDan Rosenberg if (len < 0 || chan->conf_len + len > sizeof(chan->conf_req)) { 25960a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2597fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 25980a708f8fSGustavo F. Padovan L2CAP_CONF_REJECT, flags), rsp); 25990a708f8fSGustavo F. Padovan goto unlock; 26000a708f8fSGustavo F. Padovan } 26010a708f8fSGustavo F. Padovan 26020a708f8fSGustavo F. Padovan /* Store config. */ 260373ffa904SGustavo F. Padovan memcpy(chan->conf_req + chan->conf_len, req->data, len); 260473ffa904SGustavo F. Padovan chan->conf_len += len; 26050a708f8fSGustavo F. Padovan 26060a708f8fSGustavo F. Padovan if (flags & 0x0001) { 26070a708f8fSGustavo F. Padovan /* Incomplete config. Send empty response. */ 26080a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2609fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 26100a708f8fSGustavo F. Padovan L2CAP_CONF_SUCCESS, 0x0001), rsp); 26110a708f8fSGustavo F. Padovan goto unlock; 26120a708f8fSGustavo F. Padovan } 26130a708f8fSGustavo F. Padovan 26140a708f8fSGustavo F. Padovan /* Complete config. */ 261573ffa904SGustavo F. Padovan len = l2cap_parse_conf_req(chan, rsp); 26160a708f8fSGustavo F. Padovan if (len < 0) { 2617e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 26180a708f8fSGustavo F. Padovan goto unlock; 26190a708f8fSGustavo F. Padovan } 26200a708f8fSGustavo F. Padovan 26210a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); 262273ffa904SGustavo F. Padovan chan->num_conf_rsp++; 26230a708f8fSGustavo F. Padovan 26240a708f8fSGustavo F. Padovan /* Reset config buffer. */ 262573ffa904SGustavo F. Padovan chan->conf_len = 0; 26260a708f8fSGustavo F. Padovan 2627c1360a1cSGustavo F. Padovan if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) 26280a708f8fSGustavo F. Padovan goto unlock; 26290a708f8fSGustavo F. Padovan 2630c1360a1cSGustavo F. Padovan if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { 263147d1ec61SGustavo F. Padovan set_default_fcs(chan); 26320a708f8fSGustavo F. Padovan 263389bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 26340a708f8fSGustavo F. Padovan 263542e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 263642e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 263758d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 26380c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2639525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 26400a708f8fSGustavo F. Padovan 26410a708f8fSGustavo F. Padovan l2cap_chan_ready(sk); 26420a708f8fSGustavo F. Padovan goto unlock; 26430a708f8fSGustavo F. Padovan } 26440a708f8fSGustavo F. Padovan 2645c1360a1cSGustavo F. Padovan if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { 26460a708f8fSGustavo F. Padovan u8 buf[64]; 26470a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 264873ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 264973ffa904SGustavo F. Padovan chan->num_conf_req++; 26500a708f8fSGustavo F. Padovan } 26510a708f8fSGustavo F. Padovan 26520a708f8fSGustavo F. Padovan unlock: 26530a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 26540a708f8fSGustavo F. Padovan return 0; 26550a708f8fSGustavo F. Padovan } 26560a708f8fSGustavo F. Padovan 26570a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 26580a708f8fSGustavo F. Padovan { 26590a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; 26600a708f8fSGustavo F. Padovan u16 scid, flags, result; 266148454079SGustavo F. Padovan struct l2cap_chan *chan; 26620a708f8fSGustavo F. Padovan struct sock *sk; 26630a708f8fSGustavo F. Padovan int len = cmd->len - sizeof(*rsp); 26640a708f8fSGustavo F. Padovan 26650a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 26660a708f8fSGustavo F. Padovan flags = __le16_to_cpu(rsp->flags); 26670a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 26680a708f8fSGustavo F. Padovan 26690a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", 26700a708f8fSGustavo F. Padovan scid, flags, result); 26710a708f8fSGustavo F. Padovan 2672baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 267348454079SGustavo F. Padovan if (!chan) 26740a708f8fSGustavo F. Padovan return 0; 26750a708f8fSGustavo F. Padovan 267648454079SGustavo F. Padovan sk = chan->sk; 267748454079SGustavo F. Padovan 26780a708f8fSGustavo F. Padovan switch (result) { 26790a708f8fSGustavo F. Padovan case L2CAP_CONF_SUCCESS: 268047d1ec61SGustavo F. Padovan l2cap_conf_rfc_get(chan, rsp->data, len); 26810a708f8fSGustavo F. Padovan break; 26820a708f8fSGustavo F. Padovan 26830a708f8fSGustavo F. Padovan case L2CAP_CONF_UNACCEPT: 268473ffa904SGustavo F. Padovan if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { 26850a708f8fSGustavo F. Padovan char req[64]; 26860a708f8fSGustavo F. Padovan 26870a708f8fSGustavo F. Padovan if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { 2688e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 26890a708f8fSGustavo F. Padovan goto done; 26900a708f8fSGustavo F. Padovan } 26910a708f8fSGustavo F. Padovan 26920a708f8fSGustavo F. Padovan /* throw out any old stored conf requests */ 26930a708f8fSGustavo F. Padovan result = L2CAP_CONF_SUCCESS; 2694b4450035SGustavo F. Padovan len = l2cap_parse_conf_rsp(chan, rsp->data, len, 2695b4450035SGustavo F. Padovan req, &result); 26960a708f8fSGustavo F. Padovan if (len < 0) { 2697e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 26980a708f8fSGustavo F. Padovan goto done; 26990a708f8fSGustavo F. Padovan } 27000a708f8fSGustavo F. Padovan 27010a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 27020a708f8fSGustavo F. Padovan L2CAP_CONF_REQ, len, req); 270373ffa904SGustavo F. Padovan chan->num_conf_req++; 27040a708f8fSGustavo F. Padovan if (result != L2CAP_CONF_SUCCESS) 27050a708f8fSGustavo F. Padovan goto done; 27060a708f8fSGustavo F. Padovan break; 27070a708f8fSGustavo F. Padovan } 27080a708f8fSGustavo F. Padovan 27090a708f8fSGustavo F. Padovan default: 27100a708f8fSGustavo F. Padovan sk->sk_err = ECONNRESET; 2711c9b66675SGustavo F. Padovan __set_chan_timer(chan, HZ * 5); 2712e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 27130a708f8fSGustavo F. Padovan goto done; 27140a708f8fSGustavo F. Padovan } 27150a708f8fSGustavo F. Padovan 27160a708f8fSGustavo F. Padovan if (flags & 0x01) 27170a708f8fSGustavo F. Padovan goto done; 27180a708f8fSGustavo F. Padovan 2719c1360a1cSGustavo F. Padovan set_bit(CONF_INPUT_DONE, &chan->conf_state); 27200a708f8fSGustavo F. Padovan 2721c1360a1cSGustavo F. Padovan if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { 272247d1ec61SGustavo F. Padovan set_default_fcs(chan); 27230a708f8fSGustavo F. Padovan 272489bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 272542e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 272642e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 272758d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 27280c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2729525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 27300a708f8fSGustavo F. Padovan 27310a708f8fSGustavo F. Padovan l2cap_chan_ready(sk); 27320a708f8fSGustavo F. Padovan } 27330a708f8fSGustavo F. Padovan 27340a708f8fSGustavo F. Padovan done: 27350a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27360a708f8fSGustavo F. Padovan return 0; 27370a708f8fSGustavo F. Padovan } 27380a708f8fSGustavo F. Padovan 27390a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 27400a708f8fSGustavo F. Padovan { 27410a708f8fSGustavo F. Padovan struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; 27420a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp rsp; 27430a708f8fSGustavo F. Padovan u16 dcid, scid; 274448454079SGustavo F. Padovan struct l2cap_chan *chan; 27450a708f8fSGustavo F. Padovan struct sock *sk; 27460a708f8fSGustavo F. Padovan 27470a708f8fSGustavo F. Padovan scid = __le16_to_cpu(req->scid); 27480a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 27490a708f8fSGustavo F. Padovan 27500a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); 27510a708f8fSGustavo F. Padovan 2752baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 275348454079SGustavo F. Padovan if (!chan) 27540a708f8fSGustavo F. Padovan return 0; 27550a708f8fSGustavo F. Padovan 275648454079SGustavo F. Padovan sk = chan->sk; 275748454079SGustavo F. Padovan 2758fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 2759fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 27600a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); 27610a708f8fSGustavo F. Padovan 27620a708f8fSGustavo F. Padovan sk->sk_shutdown = SHUTDOWN_MASK; 27630a708f8fSGustavo F. Padovan 27640a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 27650a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 276689bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_DISCONN); 2767c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 2768c9b66675SGustavo F. Padovan __set_chan_timer(chan, HZ / 5); 27690a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27700a708f8fSGustavo F. Padovan return 0; 27710a708f8fSGustavo F. Padovan } 27720a708f8fSGustavo F. Padovan 277348454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNRESET); 27740a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27750a708f8fSGustavo F. Padovan 2776ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 27770a708f8fSGustavo F. Padovan return 0; 27780a708f8fSGustavo F. Padovan } 27790a708f8fSGustavo F. Padovan 27800a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 27810a708f8fSGustavo F. Padovan { 27820a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; 27830a708f8fSGustavo F. Padovan u16 dcid, scid; 278448454079SGustavo F. Padovan struct l2cap_chan *chan; 27850a708f8fSGustavo F. Padovan struct sock *sk; 27860a708f8fSGustavo F. Padovan 27870a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 27880a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 27890a708f8fSGustavo F. Padovan 27900a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); 27910a708f8fSGustavo F. Padovan 2792baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 279348454079SGustavo F. Padovan if (!chan) 27940a708f8fSGustavo F. Padovan return 0; 27950a708f8fSGustavo F. Padovan 279648454079SGustavo F. Padovan sk = chan->sk; 279748454079SGustavo F. Padovan 27980a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 27990a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 280089bc500eSGustavo F. Padovan l2cap_state_change(chan,BT_DISCONN); 2801c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 2802c9b66675SGustavo F. Padovan __set_chan_timer(chan, HZ / 5); 28030a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 28040a708f8fSGustavo F. Padovan return 0; 28050a708f8fSGustavo F. Padovan } 28060a708f8fSGustavo F. Padovan 280748454079SGustavo F. Padovan l2cap_chan_del(chan, 0); 28080a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 28090a708f8fSGustavo F. Padovan 2810ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 28110a708f8fSGustavo F. Padovan return 0; 28120a708f8fSGustavo F. Padovan } 28130a708f8fSGustavo F. Padovan 28140a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 28150a708f8fSGustavo F. Padovan { 28160a708f8fSGustavo F. Padovan struct l2cap_info_req *req = (struct l2cap_info_req *) data; 28170a708f8fSGustavo F. Padovan u16 type; 28180a708f8fSGustavo F. Padovan 28190a708f8fSGustavo F. Padovan type = __le16_to_cpu(req->type); 28200a708f8fSGustavo F. Padovan 28210a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x", type); 28220a708f8fSGustavo F. Padovan 28230a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 28240a708f8fSGustavo F. Padovan u8 buf[8]; 28250a708f8fSGustavo F. Padovan u32 feat_mask = l2cap_feat_mask; 28260a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 28270a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 28280a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 28290a708f8fSGustavo F. Padovan if (!disable_ertm) 28300a708f8fSGustavo F. Padovan feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING 28310a708f8fSGustavo F. Padovan | L2CAP_FEAT_FCS; 2832a5fd6f30SAndrei Emeltchenko if (enable_hs) 28336327eb98SAndrei Emeltchenko feat_mask |= L2CAP_FEAT_EXT_FLOW 28346327eb98SAndrei Emeltchenko | L2CAP_FEAT_EXT_WINDOW; 2835a5fd6f30SAndrei Emeltchenko 28360a708f8fSGustavo F. Padovan put_unaligned_le32(feat_mask, rsp->data); 28370a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 28380a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 28390a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 28400a708f8fSGustavo F. Padovan u8 buf[12]; 28410a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 28420a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 28430a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 28440a708f8fSGustavo F. Padovan memcpy(buf + 4, l2cap_fixed_chan, 8); 28450a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 28460a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 28470a708f8fSGustavo F. Padovan } else { 28480a708f8fSGustavo F. Padovan struct l2cap_info_rsp rsp; 28490a708f8fSGustavo F. Padovan rsp.type = cpu_to_le16(type); 28500a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); 28510a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 28520a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(rsp), &rsp); 28530a708f8fSGustavo F. Padovan } 28540a708f8fSGustavo F. Padovan 28550a708f8fSGustavo F. Padovan return 0; 28560a708f8fSGustavo F. Padovan } 28570a708f8fSGustavo F. Padovan 28580a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 28590a708f8fSGustavo F. Padovan { 28600a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; 28610a708f8fSGustavo F. Padovan u16 type, result; 28620a708f8fSGustavo F. Padovan 28630a708f8fSGustavo F. Padovan type = __le16_to_cpu(rsp->type); 28640a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 28650a708f8fSGustavo F. Padovan 28660a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); 28670a708f8fSGustavo F. Padovan 2868e90165beSAndrei Emeltchenko /* L2CAP Info req/rsp are unbound to channels, add extra checks */ 2869e90165beSAndrei Emeltchenko if (cmd->ident != conn->info_ident || 2870e90165beSAndrei Emeltchenko conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) 2871e90165beSAndrei Emeltchenko return 0; 2872e90165beSAndrei Emeltchenko 28730a708f8fSGustavo F. Padovan del_timer(&conn->info_timer); 28740a708f8fSGustavo F. Padovan 28750a708f8fSGustavo F. Padovan if (result != L2CAP_IR_SUCCESS) { 28760a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 28770a708f8fSGustavo F. Padovan conn->info_ident = 0; 28780a708f8fSGustavo F. Padovan 28790a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 28800a708f8fSGustavo F. Padovan 28810a708f8fSGustavo F. Padovan return 0; 28820a708f8fSGustavo F. Padovan } 28830a708f8fSGustavo F. Padovan 28840a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 28850a708f8fSGustavo F. Padovan conn->feat_mask = get_unaligned_le32(rsp->data); 28860a708f8fSGustavo F. Padovan 28870a708f8fSGustavo F. Padovan if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { 28880a708f8fSGustavo F. Padovan struct l2cap_info_req req; 28890a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 28900a708f8fSGustavo F. Padovan 28910a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 28920a708f8fSGustavo F. Padovan 28930a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 28940a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 28950a708f8fSGustavo F. Padovan } else { 28960a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 28970a708f8fSGustavo F. Padovan conn->info_ident = 0; 28980a708f8fSGustavo F. Padovan 28990a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 29000a708f8fSGustavo F. Padovan } 29010a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 29020a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 29030a708f8fSGustavo F. Padovan conn->info_ident = 0; 29040a708f8fSGustavo F. Padovan 29050a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 29060a708f8fSGustavo F. Padovan } 29070a708f8fSGustavo F. Padovan 29080a708f8fSGustavo F. Padovan return 0; 29090a708f8fSGustavo F. Padovan } 29100a708f8fSGustavo F. Padovan 2911e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency, 2912de73115aSClaudio Takahasi u16 to_multiplier) 2913de73115aSClaudio Takahasi { 2914de73115aSClaudio Takahasi u16 max_latency; 2915de73115aSClaudio Takahasi 2916de73115aSClaudio Takahasi if (min > max || min < 6 || max > 3200) 2917de73115aSClaudio Takahasi return -EINVAL; 2918de73115aSClaudio Takahasi 2919de73115aSClaudio Takahasi if (to_multiplier < 10 || to_multiplier > 3200) 2920de73115aSClaudio Takahasi return -EINVAL; 2921de73115aSClaudio Takahasi 2922de73115aSClaudio Takahasi if (max >= to_multiplier * 8) 2923de73115aSClaudio Takahasi return -EINVAL; 2924de73115aSClaudio Takahasi 2925de73115aSClaudio Takahasi max_latency = (to_multiplier * 8 / max) - 1; 2926de73115aSClaudio Takahasi if (latency > 499 || latency > max_latency) 2927de73115aSClaudio Takahasi return -EINVAL; 2928de73115aSClaudio Takahasi 2929de73115aSClaudio Takahasi return 0; 2930de73115aSClaudio Takahasi } 2931de73115aSClaudio Takahasi 2932de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, 2933de73115aSClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 2934de73115aSClaudio Takahasi { 2935de73115aSClaudio Takahasi struct hci_conn *hcon = conn->hcon; 2936de73115aSClaudio Takahasi struct l2cap_conn_param_update_req *req; 2937de73115aSClaudio Takahasi struct l2cap_conn_param_update_rsp rsp; 2938de73115aSClaudio Takahasi u16 min, max, latency, to_multiplier, cmd_len; 29392ce603ebSClaudio Takahasi int err; 2940de73115aSClaudio Takahasi 2941de73115aSClaudio Takahasi if (!(hcon->link_mode & HCI_LM_MASTER)) 2942de73115aSClaudio Takahasi return -EINVAL; 2943de73115aSClaudio Takahasi 2944de73115aSClaudio Takahasi cmd_len = __le16_to_cpu(cmd->len); 2945de73115aSClaudio Takahasi if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) 2946de73115aSClaudio Takahasi return -EPROTO; 2947de73115aSClaudio Takahasi 2948de73115aSClaudio Takahasi req = (struct l2cap_conn_param_update_req *) data; 2949de73115aSClaudio Takahasi min = __le16_to_cpu(req->min); 2950de73115aSClaudio Takahasi max = __le16_to_cpu(req->max); 2951de73115aSClaudio Takahasi latency = __le16_to_cpu(req->latency); 2952de73115aSClaudio Takahasi to_multiplier = __le16_to_cpu(req->to_multiplier); 2953de73115aSClaudio Takahasi 2954de73115aSClaudio Takahasi BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", 2955de73115aSClaudio Takahasi min, max, latency, to_multiplier); 2956de73115aSClaudio Takahasi 2957de73115aSClaudio Takahasi memset(&rsp, 0, sizeof(rsp)); 29582ce603ebSClaudio Takahasi 29592ce603ebSClaudio Takahasi err = l2cap_check_conn_param(min, max, latency, to_multiplier); 29602ce603ebSClaudio Takahasi if (err) 2961de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); 2962de73115aSClaudio Takahasi else 2963de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); 2964de73115aSClaudio Takahasi 2965de73115aSClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, 2966de73115aSClaudio Takahasi sizeof(rsp), &rsp); 2967de73115aSClaudio Takahasi 29682ce603ebSClaudio Takahasi if (!err) 29692ce603ebSClaudio Takahasi hci_le_conn_update(hcon, min, max, latency, to_multiplier); 29702ce603ebSClaudio Takahasi 2971de73115aSClaudio Takahasi return 0; 2972de73115aSClaudio Takahasi } 2973de73115aSClaudio Takahasi 29743300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, 29753300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 29763300d9a9SClaudio Takahasi { 29773300d9a9SClaudio Takahasi int err = 0; 29783300d9a9SClaudio Takahasi 29793300d9a9SClaudio Takahasi switch (cmd->code) { 29803300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 29813300d9a9SClaudio Takahasi l2cap_command_rej(conn, cmd, data); 29823300d9a9SClaudio Takahasi break; 29833300d9a9SClaudio Takahasi 29843300d9a9SClaudio Takahasi case L2CAP_CONN_REQ: 29853300d9a9SClaudio Takahasi err = l2cap_connect_req(conn, cmd, data); 29863300d9a9SClaudio Takahasi break; 29873300d9a9SClaudio Takahasi 29883300d9a9SClaudio Takahasi case L2CAP_CONN_RSP: 29893300d9a9SClaudio Takahasi err = l2cap_connect_rsp(conn, cmd, data); 29903300d9a9SClaudio Takahasi break; 29913300d9a9SClaudio Takahasi 29923300d9a9SClaudio Takahasi case L2CAP_CONF_REQ: 29933300d9a9SClaudio Takahasi err = l2cap_config_req(conn, cmd, cmd_len, data); 29943300d9a9SClaudio Takahasi break; 29953300d9a9SClaudio Takahasi 29963300d9a9SClaudio Takahasi case L2CAP_CONF_RSP: 29973300d9a9SClaudio Takahasi err = l2cap_config_rsp(conn, cmd, data); 29983300d9a9SClaudio Takahasi break; 29993300d9a9SClaudio Takahasi 30003300d9a9SClaudio Takahasi case L2CAP_DISCONN_REQ: 30013300d9a9SClaudio Takahasi err = l2cap_disconnect_req(conn, cmd, data); 30023300d9a9SClaudio Takahasi break; 30033300d9a9SClaudio Takahasi 30043300d9a9SClaudio Takahasi case L2CAP_DISCONN_RSP: 30053300d9a9SClaudio Takahasi err = l2cap_disconnect_rsp(conn, cmd, data); 30063300d9a9SClaudio Takahasi break; 30073300d9a9SClaudio Takahasi 30083300d9a9SClaudio Takahasi case L2CAP_ECHO_REQ: 30093300d9a9SClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); 30103300d9a9SClaudio Takahasi break; 30113300d9a9SClaudio Takahasi 30123300d9a9SClaudio Takahasi case L2CAP_ECHO_RSP: 30133300d9a9SClaudio Takahasi break; 30143300d9a9SClaudio Takahasi 30153300d9a9SClaudio Takahasi case L2CAP_INFO_REQ: 30163300d9a9SClaudio Takahasi err = l2cap_information_req(conn, cmd, data); 30173300d9a9SClaudio Takahasi break; 30183300d9a9SClaudio Takahasi 30193300d9a9SClaudio Takahasi case L2CAP_INFO_RSP: 30203300d9a9SClaudio Takahasi err = l2cap_information_rsp(conn, cmd, data); 30213300d9a9SClaudio Takahasi break; 30223300d9a9SClaudio Takahasi 30233300d9a9SClaudio Takahasi default: 30243300d9a9SClaudio Takahasi BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); 30253300d9a9SClaudio Takahasi err = -EINVAL; 30263300d9a9SClaudio Takahasi break; 30273300d9a9SClaudio Takahasi } 30283300d9a9SClaudio Takahasi 30293300d9a9SClaudio Takahasi return err; 30303300d9a9SClaudio Takahasi } 30313300d9a9SClaudio Takahasi 30323300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, 30333300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 30343300d9a9SClaudio Takahasi { 30353300d9a9SClaudio Takahasi switch (cmd->code) { 30363300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 30373300d9a9SClaudio Takahasi return 0; 30383300d9a9SClaudio Takahasi 30393300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_REQ: 3040de73115aSClaudio Takahasi return l2cap_conn_param_update_req(conn, cmd, data); 30413300d9a9SClaudio Takahasi 30423300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_RSP: 30433300d9a9SClaudio Takahasi return 0; 30443300d9a9SClaudio Takahasi 30453300d9a9SClaudio Takahasi default: 30463300d9a9SClaudio Takahasi BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); 30473300d9a9SClaudio Takahasi return -EINVAL; 30483300d9a9SClaudio Takahasi } 30493300d9a9SClaudio Takahasi } 30503300d9a9SClaudio Takahasi 30513300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn, 30523300d9a9SClaudio Takahasi struct sk_buff *skb) 30530a708f8fSGustavo F. Padovan { 30540a708f8fSGustavo F. Padovan u8 *data = skb->data; 30550a708f8fSGustavo F. Padovan int len = skb->len; 30560a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr cmd; 30573300d9a9SClaudio Takahasi int err; 30580a708f8fSGustavo F. Padovan 30590a708f8fSGustavo F. Padovan l2cap_raw_recv(conn, skb); 30600a708f8fSGustavo F. Padovan 30610a708f8fSGustavo F. Padovan while (len >= L2CAP_CMD_HDR_SIZE) { 30620a708f8fSGustavo F. Padovan u16 cmd_len; 30630a708f8fSGustavo F. Padovan memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); 30640a708f8fSGustavo F. Padovan data += L2CAP_CMD_HDR_SIZE; 30650a708f8fSGustavo F. Padovan len -= L2CAP_CMD_HDR_SIZE; 30660a708f8fSGustavo F. Padovan 30670a708f8fSGustavo F. Padovan cmd_len = le16_to_cpu(cmd.len); 30680a708f8fSGustavo F. Padovan 30690a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident); 30700a708f8fSGustavo F. Padovan 30710a708f8fSGustavo F. Padovan if (cmd_len > len || !cmd.ident) { 30720a708f8fSGustavo F. Padovan BT_DBG("corrupted command"); 30730a708f8fSGustavo F. Padovan break; 30740a708f8fSGustavo F. Padovan } 30750a708f8fSGustavo F. Padovan 30763300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 30773300d9a9SClaudio Takahasi err = l2cap_le_sig_cmd(conn, &cmd, data); 30783300d9a9SClaudio Takahasi else 30793300d9a9SClaudio Takahasi err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); 30800a708f8fSGustavo F. Padovan 30810a708f8fSGustavo F. Padovan if (err) { 3082e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_unk rej; 30832c6d1a2eSGustavo F. Padovan 30842c6d1a2eSGustavo F. Padovan BT_ERR("Wrong link type (%d)", err); 30850a708f8fSGustavo F. Padovan 30860a708f8fSGustavo F. Padovan /* FIXME: Map err to a valid reason */ 3087e2fd318eSIlia Kolomisnky rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); 30880a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); 30890a708f8fSGustavo F. Padovan } 30900a708f8fSGustavo F. Padovan 30910a708f8fSGustavo F. Padovan data += cmd_len; 30920a708f8fSGustavo F. Padovan len -= cmd_len; 30930a708f8fSGustavo F. Padovan } 30940a708f8fSGustavo F. Padovan 30950a708f8fSGustavo F. Padovan kfree_skb(skb); 30960a708f8fSGustavo F. Padovan } 30970a708f8fSGustavo F. Padovan 309847d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb) 30990a708f8fSGustavo F. Padovan { 31000a708f8fSGustavo F. Padovan u16 our_fcs, rcv_fcs; 31010a708f8fSGustavo F. Padovan int hdr_size = L2CAP_HDR_SIZE + 2; 31020a708f8fSGustavo F. Padovan 310347d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 31040a708f8fSGustavo F. Padovan skb_trim(skb, skb->len - 2); 31050a708f8fSGustavo F. Padovan rcv_fcs = get_unaligned_le16(skb->data + skb->len); 31060a708f8fSGustavo F. Padovan our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); 31070a708f8fSGustavo F. Padovan 31080a708f8fSGustavo F. Padovan if (our_fcs != rcv_fcs) 31090a708f8fSGustavo F. Padovan return -EBADMSG; 31100a708f8fSGustavo F. Padovan } 31110a708f8fSGustavo F. Padovan return 0; 31120a708f8fSGustavo F. Padovan } 31130a708f8fSGustavo F. Padovan 3114525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan) 31150a708f8fSGustavo F. Padovan { 31160a708f8fSGustavo F. Padovan u16 control = 0; 31170a708f8fSGustavo F. Padovan 31186a026610SGustavo F. Padovan chan->frames_sent = 0; 31190a708f8fSGustavo F. Padovan 31200b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 31210a708f8fSGustavo F. Padovan 3122e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 3123ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 3124525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 3125e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 31260a708f8fSGustavo F. Padovan } 31270a708f8fSGustavo F. Padovan 3128e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) 3129525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 31300a708f8fSGustavo F. Padovan 3131525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 31320a708f8fSGustavo F. Padovan 3133e2ab4353SGustavo F. Padovan if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && 31346a026610SGustavo F. Padovan chan->frames_sent == 0) { 3135ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 3136525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 31370a708f8fSGustavo F. Padovan } 31380a708f8fSGustavo F. Padovan } 31390a708f8fSGustavo F. Padovan 3140fb45de7dSAndrei Emeltchenko static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u16 tx_seq, u8 sar) 31410a708f8fSGustavo F. Padovan { 31420a708f8fSGustavo F. Padovan struct sk_buff *next_skb; 31430a708f8fSGustavo F. Padovan int tx_seq_offset, next_tx_seq_offset; 31440a708f8fSGustavo F. Padovan 31450a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 31460a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 31470a708f8fSGustavo F. Padovan 3148f1c6775bSGustavo F. Padovan next_skb = skb_peek(&chan->srej_q); 31490a708f8fSGustavo F. Padovan if (!next_skb) { 3150f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 31510a708f8fSGustavo F. Padovan return 0; 31520a708f8fSGustavo F. Padovan } 31530a708f8fSGustavo F. Padovan 315442e5c802SGustavo F. Padovan tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; 31550a708f8fSGustavo F. Padovan if (tx_seq_offset < 0) 31560a708f8fSGustavo F. Padovan tx_seq_offset += 64; 31570a708f8fSGustavo F. Padovan 31580a708f8fSGustavo F. Padovan do { 31590a708f8fSGustavo F. Padovan if (bt_cb(next_skb)->tx_seq == tx_seq) 31600a708f8fSGustavo F. Padovan return -EINVAL; 31610a708f8fSGustavo F. Padovan 31620a708f8fSGustavo F. Padovan next_tx_seq_offset = (bt_cb(next_skb)->tx_seq - 316342e5c802SGustavo F. Padovan chan->buffer_seq) % 64; 31640a708f8fSGustavo F. Padovan if (next_tx_seq_offset < 0) 31650a708f8fSGustavo F. Padovan next_tx_seq_offset += 64; 31660a708f8fSGustavo F. Padovan 31670a708f8fSGustavo F. Padovan if (next_tx_seq_offset > tx_seq_offset) { 3168f1c6775bSGustavo F. Padovan __skb_queue_before(&chan->srej_q, next_skb, skb); 31690a708f8fSGustavo F. Padovan return 0; 31700a708f8fSGustavo F. Padovan } 31710a708f8fSGustavo F. Padovan 3172f1c6775bSGustavo F. Padovan if (skb_queue_is_last(&chan->srej_q, next_skb)) 31730a708f8fSGustavo F. Padovan break; 31740a708f8fSGustavo F. Padovan 3175f1c6775bSGustavo F. Padovan } while ((next_skb = skb_queue_next(&chan->srej_q, next_skb))); 31760a708f8fSGustavo F. Padovan 3177f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 31780a708f8fSGustavo F. Padovan 31790a708f8fSGustavo F. Padovan return 0; 31800a708f8fSGustavo F. Padovan } 31810a708f8fSGustavo F. Padovan 318284084a31SMat Martineau static void append_skb_frag(struct sk_buff *skb, 318384084a31SMat Martineau struct sk_buff *new_frag, struct sk_buff **last_frag) 31840a708f8fSGustavo F. Padovan { 318584084a31SMat Martineau /* skb->len reflects data in skb as well as all fragments 318684084a31SMat Martineau * skb->data_len reflects only data in fragments 318784084a31SMat Martineau */ 318884084a31SMat Martineau if (!skb_has_frag_list(skb)) 318984084a31SMat Martineau skb_shinfo(skb)->frag_list = new_frag; 319084084a31SMat Martineau 319184084a31SMat Martineau new_frag->next = NULL; 319284084a31SMat Martineau 319384084a31SMat Martineau (*last_frag)->next = new_frag; 319484084a31SMat Martineau *last_frag = new_frag; 319584084a31SMat Martineau 319684084a31SMat Martineau skb->len += new_frag->len; 319784084a31SMat Martineau skb->data_len += new_frag->len; 319884084a31SMat Martineau skb->truesize += new_frag->truesize; 319984084a31SMat Martineau } 320084084a31SMat Martineau 320184084a31SMat Martineau static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 320284084a31SMat Martineau { 320384084a31SMat Martineau int err = -EINVAL; 32040a708f8fSGustavo F. Padovan 32057e0ef6eeSAndrei Emeltchenko switch (__get_ctrl_sar(chan, control)) { 32067e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_UNSEGMENTED: 320784084a31SMat Martineau if (chan->sdu) 320884084a31SMat Martineau break; 32090a708f8fSGustavo F. Padovan 321084084a31SMat Martineau err = chan->ops->recv(chan->data, skb); 321184084a31SMat Martineau break; 32120a708f8fSGustavo F. Padovan 32137e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_START: 321484084a31SMat Martineau if (chan->sdu) 321584084a31SMat Martineau break; 32160a708f8fSGustavo F. Padovan 32176f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 32180a708f8fSGustavo F. Padovan skb_pull(skb, 2); 32190a708f8fSGustavo F. Padovan 322084084a31SMat Martineau if (chan->sdu_len > chan->imtu) { 322184084a31SMat Martineau err = -EMSGSIZE; 322284084a31SMat Martineau break; 322384084a31SMat Martineau } 32240a708f8fSGustavo F. Padovan 322584084a31SMat Martineau if (skb->len >= chan->sdu_len) 322684084a31SMat Martineau break; 322784084a31SMat Martineau 322884084a31SMat Martineau chan->sdu = skb; 322984084a31SMat Martineau chan->sdu_last_frag = skb; 323084084a31SMat Martineau 323184084a31SMat Martineau skb = NULL; 323284084a31SMat Martineau err = 0; 32330a708f8fSGustavo F. Padovan break; 32340a708f8fSGustavo F. Padovan 32357e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_CONTINUE: 32366f61fd47SGustavo F. Padovan if (!chan->sdu) 323784084a31SMat Martineau break; 32380a708f8fSGustavo F. Padovan 323984084a31SMat Martineau append_skb_frag(chan->sdu, skb, 324084084a31SMat Martineau &chan->sdu_last_frag); 324184084a31SMat Martineau skb = NULL; 32420a708f8fSGustavo F. Padovan 324384084a31SMat Martineau if (chan->sdu->len >= chan->sdu_len) 324484084a31SMat Martineau break; 32450a708f8fSGustavo F. Padovan 324684084a31SMat Martineau err = 0; 32470a708f8fSGustavo F. Padovan break; 32480a708f8fSGustavo F. Padovan 32497e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_END: 32506f61fd47SGustavo F. Padovan if (!chan->sdu) 325184084a31SMat Martineau break; 32520a708f8fSGustavo F. Padovan 325384084a31SMat Martineau append_skb_frag(chan->sdu, skb, 325484084a31SMat Martineau &chan->sdu_last_frag); 325584084a31SMat Martineau skb = NULL; 32560a708f8fSGustavo F. Padovan 325784084a31SMat Martineau if (chan->sdu->len != chan->sdu_len) 325884084a31SMat Martineau break; 32590a708f8fSGustavo F. Padovan 326084084a31SMat Martineau err = chan->ops->recv(chan->data, chan->sdu); 32610a708f8fSGustavo F. Padovan 326284084a31SMat Martineau if (!err) { 326384084a31SMat Martineau /* Reassembly complete */ 326484084a31SMat Martineau chan->sdu = NULL; 326584084a31SMat Martineau chan->sdu_last_frag = NULL; 326684084a31SMat Martineau chan->sdu_len = 0; 32670a708f8fSGustavo F. Padovan } 32680a708f8fSGustavo F. Padovan break; 32690a708f8fSGustavo F. Padovan } 32700a708f8fSGustavo F. Padovan 327184084a31SMat Martineau if (err) { 32720a708f8fSGustavo F. Padovan kfree_skb(skb); 32736f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 32746f61fd47SGustavo F. Padovan chan->sdu = NULL; 327584084a31SMat Martineau chan->sdu_last_frag = NULL; 327684084a31SMat Martineau chan->sdu_len = 0; 327784084a31SMat Martineau } 32780a708f8fSGustavo F. Padovan 327984084a31SMat Martineau return err; 32800a708f8fSGustavo F. Padovan } 32810a708f8fSGustavo F. Padovan 328226f880d2SMat Martineau static void l2cap_ertm_enter_local_busy(struct l2cap_chan *chan) 32830a708f8fSGustavo F. Padovan { 32840a708f8fSGustavo F. Padovan u16 control; 32850a708f8fSGustavo F. Padovan 328626f880d2SMat Martineau BT_DBG("chan %p, Enter local busy", chan); 328726f880d2SMat Martineau 328826f880d2SMat Martineau set_bit(CONN_LOCAL_BUSY, &chan->conn_state); 328926f880d2SMat Martineau 32900b209faeSAndrei Emeltchenko control = __set_reqseq(chan, chan->buffer_seq); 3291ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 329226f880d2SMat Martineau l2cap_send_sframe(chan, control); 329326f880d2SMat Martineau 329426f880d2SMat Martineau set_bit(CONN_RNR_SENT, &chan->conn_state); 329526f880d2SMat Martineau 329626f880d2SMat Martineau __clear_ack_timer(chan); 32970a708f8fSGustavo F. Padovan } 32980a708f8fSGustavo F. Padovan 329926f880d2SMat Martineau static void l2cap_ertm_exit_local_busy(struct l2cap_chan *chan) 330026f880d2SMat Martineau { 330126f880d2SMat Martineau u16 control; 33020a708f8fSGustavo F. Padovan 3303e2ab4353SGustavo F. Padovan if (!test_bit(CONN_RNR_SENT, &chan->conn_state)) 33040a708f8fSGustavo F. Padovan goto done; 33050a708f8fSGustavo F. Padovan 33060b209faeSAndrei Emeltchenko control = __set_reqseq(chan, chan->buffer_seq); 3307ab784b73SAndrei Emeltchenko control |= L2CAP_CTRL_POLL; 3308ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 3309525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 33106a026610SGustavo F. Padovan chan->retry_count = 1; 33110a708f8fSGustavo F. Padovan 33121a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 33131a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 33140a708f8fSGustavo F. Padovan 3315e2ab4353SGustavo F. Padovan set_bit(CONN_WAIT_F, &chan->conn_state); 33160a708f8fSGustavo F. Padovan 33170a708f8fSGustavo F. Padovan done: 3318e2ab4353SGustavo F. Padovan clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); 3319e2ab4353SGustavo F. Padovan clear_bit(CONN_RNR_SENT, &chan->conn_state); 33200a708f8fSGustavo F. Padovan 332149208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit local busy", chan); 33220a708f8fSGustavo F. Padovan } 33230a708f8fSGustavo F. Padovan 3324e328140fSMat Martineau void l2cap_chan_busy(struct l2cap_chan *chan, int busy) 33250a708f8fSGustavo F. Padovan { 3326e328140fSMat Martineau if (chan->mode == L2CAP_MODE_ERTM) { 3327e328140fSMat Martineau if (busy) 332826f880d2SMat Martineau l2cap_ertm_enter_local_busy(chan); 3329e328140fSMat Martineau else 3330e328140fSMat Martineau l2cap_ertm_exit_local_busy(chan); 33310a708f8fSGustavo F. Padovan } 33320a708f8fSGustavo F. Padovan } 33330a708f8fSGustavo F. Padovan 3334fb45de7dSAndrei Emeltchenko static void l2cap_check_srej_gap(struct l2cap_chan *chan, u16 tx_seq) 33350a708f8fSGustavo F. Padovan { 33360a708f8fSGustavo F. Padovan struct sk_buff *skb; 33370a708f8fSGustavo F. Padovan u16 control; 33380a708f8fSGustavo F. Padovan 3339e328140fSMat Martineau while ((skb = skb_peek(&chan->srej_q)) && 3340e328140fSMat Martineau !test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 3341e328140fSMat Martineau int err; 3342e328140fSMat Martineau 33430a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq != tx_seq) 33440a708f8fSGustavo F. Padovan break; 33450a708f8fSGustavo F. Padovan 3346f1c6775bSGustavo F. Padovan skb = skb_dequeue(&chan->srej_q); 33477e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, bt_cb(skb)->sar); 334884084a31SMat Martineau err = l2cap_reassemble_sdu(chan, skb, control); 3349e328140fSMat Martineau 3350e328140fSMat Martineau if (err < 0) { 3351e328140fSMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 3352e328140fSMat Martineau break; 3353e328140fSMat Martineau } 3354e328140fSMat Martineau 335542e5c802SGustavo F. Padovan chan->buffer_seq_srej = 335642e5c802SGustavo F. Padovan (chan->buffer_seq_srej + 1) % 64; 33570a708f8fSGustavo F. Padovan tx_seq = (tx_seq + 1) % 64; 33580a708f8fSGustavo F. Padovan } 33590a708f8fSGustavo F. Padovan } 33600a708f8fSGustavo F. Padovan 3361fb45de7dSAndrei Emeltchenko static void l2cap_resend_srejframe(struct l2cap_chan *chan, u16 tx_seq) 33620a708f8fSGustavo F. Padovan { 33630a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 33640a708f8fSGustavo F. Padovan u16 control; 33650a708f8fSGustavo F. Padovan 336639d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 33670a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 33680a708f8fSGustavo F. Padovan list_del(&l->list); 33690a708f8fSGustavo F. Padovan kfree(l); 33700a708f8fSGustavo F. Padovan return; 33710a708f8fSGustavo F. Padovan } 3372ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 33730b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, l->tx_seq); 3374525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 33750a708f8fSGustavo F. Padovan list_del(&l->list); 337639d5a3eeSGustavo F. Padovan list_add_tail(&l->list, &chan->srej_l); 33770a708f8fSGustavo F. Padovan } 33780a708f8fSGustavo F. Padovan } 33790a708f8fSGustavo F. Padovan 3380fb45de7dSAndrei Emeltchenko static void l2cap_send_srejframe(struct l2cap_chan *chan, u16 tx_seq) 33810a708f8fSGustavo F. Padovan { 33820a708f8fSGustavo F. Padovan struct srej_list *new; 33830a708f8fSGustavo F. Padovan u16 control; 33840a708f8fSGustavo F. Padovan 338542e5c802SGustavo F. Padovan while (tx_seq != chan->expected_tx_seq) { 3386ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 33870b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->expected_tx_seq); 3388525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 33890a708f8fSGustavo F. Padovan 33900a708f8fSGustavo F. Padovan new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); 339142e5c802SGustavo F. Padovan new->tx_seq = chan->expected_tx_seq; 339242e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 339339d5a3eeSGustavo F. Padovan list_add_tail(&new->list, &chan->srej_l); 33940a708f8fSGustavo F. Padovan } 339542e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 33960a708f8fSGustavo F. Padovan } 33970a708f8fSGustavo F. Padovan 3398525cd185SGustavo F. Padovan static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) 33990a708f8fSGustavo F. Padovan { 3400fb45de7dSAndrei Emeltchenko u16 tx_seq = __get_txseq(chan, rx_control); 34010b209faeSAndrei Emeltchenko u16 req_seq = __get_reqseq(chan, rx_control); 34027e0ef6eeSAndrei Emeltchenko u8 sar = __get_ctrl_sar(chan, rx_control); 34030a708f8fSGustavo F. Padovan int tx_seq_offset, expected_tx_seq_offset; 340447d1ec61SGustavo F. Padovan int num_to_ack = (chan->tx_win/6) + 1; 34050a708f8fSGustavo F. Padovan int err = 0; 34060a708f8fSGustavo F. Padovan 3407525cd185SGustavo F. Padovan BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len, 3408525cd185SGustavo F. Padovan tx_seq, rx_control); 34090a708f8fSGustavo F. Padovan 3410*03f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control) && 3411e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 34121a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 34136a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 34141a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 3415e2ab4353SGustavo F. Padovan clear_bit(CONN_WAIT_F, &chan->conn_state); 34160a708f8fSGustavo F. Padovan } 34170a708f8fSGustavo F. Padovan 341842e5c802SGustavo F. Padovan chan->expected_ack_seq = req_seq; 341942e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 34200a708f8fSGustavo F. Padovan 342142e5c802SGustavo F. Padovan tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; 34220a708f8fSGustavo F. Padovan if (tx_seq_offset < 0) 34230a708f8fSGustavo F. Padovan tx_seq_offset += 64; 34240a708f8fSGustavo F. Padovan 34250a708f8fSGustavo F. Padovan /* invalid tx_seq */ 342647d1ec61SGustavo F. Padovan if (tx_seq_offset >= chan->tx_win) { 34278c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 34280a708f8fSGustavo F. Padovan goto drop; 34290a708f8fSGustavo F. Padovan } 34300a708f8fSGustavo F. Padovan 3431e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) 34320a708f8fSGustavo F. Padovan goto drop; 34330a708f8fSGustavo F. Padovan 343402f1b641SMat Martineau if (tx_seq == chan->expected_tx_seq) 343502f1b641SMat Martineau goto expected; 343602f1b641SMat Martineau 3437e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 34380a708f8fSGustavo F. Padovan struct srej_list *first; 34390a708f8fSGustavo F. Padovan 344039d5a3eeSGustavo F. Padovan first = list_first_entry(&chan->srej_l, 34410a708f8fSGustavo F. Padovan struct srej_list, list); 34420a708f8fSGustavo F. Padovan if (tx_seq == first->tx_seq) { 344342e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 3444525cd185SGustavo F. Padovan l2cap_check_srej_gap(chan, tx_seq); 34450a708f8fSGustavo F. Padovan 34460a708f8fSGustavo F. Padovan list_del(&first->list); 34470a708f8fSGustavo F. Padovan kfree(first); 34480a708f8fSGustavo F. Padovan 344939d5a3eeSGustavo F. Padovan if (list_empty(&chan->srej_l)) { 345042e5c802SGustavo F. Padovan chan->buffer_seq = chan->buffer_seq_srej; 3451e2ab4353SGustavo F. Padovan clear_bit(CONN_SREJ_SENT, &chan->conn_state); 3452525cd185SGustavo F. Padovan l2cap_send_ack(chan); 345349208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit SREJ_SENT", chan); 34540a708f8fSGustavo F. Padovan } 34550a708f8fSGustavo F. Padovan } else { 34560a708f8fSGustavo F. Padovan struct srej_list *l; 34570a708f8fSGustavo F. Padovan 34580a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 345942e5c802SGustavo F. Padovan if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0) 34600a708f8fSGustavo F. Padovan goto drop; 34610a708f8fSGustavo F. Padovan 346239d5a3eeSGustavo F. Padovan list_for_each_entry(l, &chan->srej_l, list) { 34630a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 3464525cd185SGustavo F. Padovan l2cap_resend_srejframe(chan, tx_seq); 34650a708f8fSGustavo F. Padovan return 0; 34660a708f8fSGustavo F. Padovan } 34670a708f8fSGustavo F. Padovan } 3468525cd185SGustavo F. Padovan l2cap_send_srejframe(chan, tx_seq); 34690a708f8fSGustavo F. Padovan } 34700a708f8fSGustavo F. Padovan } else { 34710a708f8fSGustavo F. Padovan expected_tx_seq_offset = 347242e5c802SGustavo F. Padovan (chan->expected_tx_seq - chan->buffer_seq) % 64; 34730a708f8fSGustavo F. Padovan if (expected_tx_seq_offset < 0) 34740a708f8fSGustavo F. Padovan expected_tx_seq_offset += 64; 34750a708f8fSGustavo F. Padovan 34760a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 34770a708f8fSGustavo F. Padovan if (tx_seq_offset < expected_tx_seq_offset) 34780a708f8fSGustavo F. Padovan goto drop; 34790a708f8fSGustavo F. Padovan 3480e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_SENT, &chan->conn_state); 34810a708f8fSGustavo F. Padovan 348249208c9cSGustavo F. Padovan BT_DBG("chan %p, Enter SREJ", chan); 34830a708f8fSGustavo F. Padovan 348439d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 348542e5c802SGustavo F. Padovan chan->buffer_seq_srej = chan->buffer_seq; 34860a708f8fSGustavo F. Padovan 3487f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->srej_q); 348842e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 34890a708f8fSGustavo F. Padovan 3490e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_PBIT, &chan->conn_state); 34910a708f8fSGustavo F. Padovan 3492525cd185SGustavo F. Padovan l2cap_send_srejframe(chan, tx_seq); 34930a708f8fSGustavo F. Padovan 34941a09bcb9SGustavo F. Padovan __clear_ack_timer(chan); 34950a708f8fSGustavo F. Padovan } 34960a708f8fSGustavo F. Padovan return 0; 34970a708f8fSGustavo F. Padovan 34980a708f8fSGustavo F. Padovan expected: 349942e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 35000a708f8fSGustavo F. Padovan 3501e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 35020a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 35030a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 3504f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 35050a708f8fSGustavo F. Padovan return 0; 35060a708f8fSGustavo F. Padovan } 35070a708f8fSGustavo F. Padovan 350884084a31SMat Martineau err = l2cap_reassemble_sdu(chan, skb, rx_control); 3509fadd192eSMat Martineau chan->buffer_seq = (chan->buffer_seq + 1) % 64; 3510e328140fSMat Martineau if (err < 0) { 3511e328140fSMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 3512e328140fSMat Martineau return err; 3513e328140fSMat Martineau } 35140a708f8fSGustavo F. Padovan 3515*03f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control)) { 3516e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 3517525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 35180a708f8fSGustavo F. Padovan } 35190a708f8fSGustavo F. Padovan 35201a09bcb9SGustavo F. Padovan __set_ack_timer(chan); 35210a708f8fSGustavo F. Padovan 35226a026610SGustavo F. Padovan chan->num_acked = (chan->num_acked + 1) % num_to_ack; 35236a026610SGustavo F. Padovan if (chan->num_acked == num_to_ack - 1) 3524525cd185SGustavo F. Padovan l2cap_send_ack(chan); 35250a708f8fSGustavo F. Padovan 35260a708f8fSGustavo F. Padovan return 0; 35270a708f8fSGustavo F. Padovan 35280a708f8fSGustavo F. Padovan drop: 35290a708f8fSGustavo F. Padovan kfree_skb(skb); 35300a708f8fSGustavo F. Padovan return 0; 35310a708f8fSGustavo F. Padovan } 35320a708f8fSGustavo F. Padovan 3533525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control) 35340a708f8fSGustavo F. Padovan { 35350b209faeSAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, 35360b209faeSAndrei Emeltchenko __get_reqseq(chan, rx_control), rx_control); 35370a708f8fSGustavo F. Padovan 35380b209faeSAndrei Emeltchenko chan->expected_ack_seq = __get_reqseq(chan, rx_control); 353942e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 35400a708f8fSGustavo F. Padovan 35410a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) { 3542e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 3543e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 3544e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 35456a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 35461a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 35470a708f8fSGustavo F. Padovan 3548e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 3549525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 35500a708f8fSGustavo F. Padovan } else { 3551525cd185SGustavo F. Padovan l2cap_send_i_or_rr_or_rnr(chan); 35520a708f8fSGustavo F. Padovan } 35530a708f8fSGustavo F. Padovan 3554*03f6715dSAndrei Emeltchenko } else if (__is_ctrl_final(chan, rx_control)) { 3555e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 35560a708f8fSGustavo F. Padovan 3557e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 3558525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 35590a708f8fSGustavo F. Padovan 35600a708f8fSGustavo F. Padovan } else { 3561e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 35626a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 35631a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 35640a708f8fSGustavo F. Padovan 3565e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 3566e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) 3567525cd185SGustavo F. Padovan l2cap_send_ack(chan); 35680a708f8fSGustavo F. Padovan else 3569525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 35700a708f8fSGustavo F. Padovan } 35710a708f8fSGustavo F. Padovan } 35720a708f8fSGustavo F. Padovan 3573525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control) 35740a708f8fSGustavo F. Padovan { 35750b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 35760a708f8fSGustavo F. Padovan 3577525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 35780a708f8fSGustavo F. Padovan 3579e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 35800a708f8fSGustavo F. Padovan 358142e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 358242e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 35830a708f8fSGustavo F. Padovan 3584*03f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control)) { 3585e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 3586525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 35870a708f8fSGustavo F. Padovan } else { 3588525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 35890a708f8fSGustavo F. Padovan 3590e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) 3591e2ab4353SGustavo F. Padovan set_bit(CONN_REJ_ACT, &chan->conn_state); 35920a708f8fSGustavo F. Padovan } 35930a708f8fSGustavo F. Padovan } 3594525cd185SGustavo F. Padovan static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control) 35950a708f8fSGustavo F. Padovan { 35960b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 35970a708f8fSGustavo F. Padovan 3598525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 35990a708f8fSGustavo F. Padovan 3600e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 36010a708f8fSGustavo F. Padovan 36020a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) { 360342e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 360442e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 36050a708f8fSGustavo F. Padovan 3606e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 3607525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 36080a708f8fSGustavo F. Padovan 3609525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 36100a708f8fSGustavo F. Padovan 3611e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) { 36126a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 3613e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_ACT, &chan->conn_state); 36140a708f8fSGustavo F. Padovan } 3615*03f6715dSAndrei Emeltchenko } else if (__is_ctrl_final(chan, rx_control)) { 3616e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_ACT, &chan->conn_state) && 36176a026610SGustavo F. Padovan chan->srej_save_reqseq == tx_seq) 3618e2ab4353SGustavo F. Padovan clear_bit(CONN_SREJ_ACT, &chan->conn_state); 36190a708f8fSGustavo F. Padovan else 3620525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 36210a708f8fSGustavo F. Padovan } else { 3622525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 3623e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) { 36246a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 3625e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_ACT, &chan->conn_state); 36260a708f8fSGustavo F. Padovan } 36270a708f8fSGustavo F. Padovan } 36280a708f8fSGustavo F. Padovan } 36290a708f8fSGustavo F. Padovan 3630525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control) 36310a708f8fSGustavo F. Padovan { 36320b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 36330a708f8fSGustavo F. Padovan 3634525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 36350a708f8fSGustavo F. Padovan 3636e2ab4353SGustavo F. Padovan set_bit(CONN_REMOTE_BUSY, &chan->conn_state); 363742e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 363842e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 36390a708f8fSGustavo F. Padovan 36400a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3641e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 36420a708f8fSGustavo F. Padovan 3643e2ab4353SGustavo F. Padovan if (!test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 36441a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 36450a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3646525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); 36470a708f8fSGustavo F. Padovan return; 36480a708f8fSGustavo F. Padovan } 36490a708f8fSGustavo F. Padovan 3650ab784b73SAndrei Emeltchenko if (rx_control & L2CAP_CTRL_POLL) { 3651525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 3652ab784b73SAndrei Emeltchenko } else { 3653ab784b73SAndrei Emeltchenko rx_control = __set_ctrl_super(chan, L2CAP_SUPER_RR); 3654ab784b73SAndrei Emeltchenko l2cap_send_sframe(chan, rx_control); 3655ab784b73SAndrei Emeltchenko } 36560a708f8fSGustavo F. Padovan } 36570a708f8fSGustavo F. Padovan 3658525cd185SGustavo F. Padovan static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) 36590a708f8fSGustavo F. Padovan { 3660525cd185SGustavo F. Padovan BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len); 36610a708f8fSGustavo F. Padovan 3662*03f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control) && 3663e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 36641a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 36656a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 36661a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 3667e2ab4353SGustavo F. Padovan clear_bit(CONN_WAIT_F, &chan->conn_state); 36680a708f8fSGustavo F. Padovan } 36690a708f8fSGustavo F. Padovan 3670ab784b73SAndrei Emeltchenko switch (__get_ctrl_super(chan, rx_control)) { 3671ab784b73SAndrei Emeltchenko case L2CAP_SUPER_RR: 3672525cd185SGustavo F. Padovan l2cap_data_channel_rrframe(chan, rx_control); 36730a708f8fSGustavo F. Padovan break; 36740a708f8fSGustavo F. Padovan 3675ab784b73SAndrei Emeltchenko case L2CAP_SUPER_REJ: 3676525cd185SGustavo F. Padovan l2cap_data_channel_rejframe(chan, rx_control); 36770a708f8fSGustavo F. Padovan break; 36780a708f8fSGustavo F. Padovan 3679ab784b73SAndrei Emeltchenko case L2CAP_SUPER_SREJ: 3680525cd185SGustavo F. Padovan l2cap_data_channel_srejframe(chan, rx_control); 36810a708f8fSGustavo F. Padovan break; 36820a708f8fSGustavo F. Padovan 3683ab784b73SAndrei Emeltchenko case L2CAP_SUPER_RNR: 3684525cd185SGustavo F. Padovan l2cap_data_channel_rnrframe(chan, rx_control); 36850a708f8fSGustavo F. Padovan break; 36860a708f8fSGustavo F. Padovan } 36870a708f8fSGustavo F. Padovan 36880a708f8fSGustavo F. Padovan kfree_skb(skb); 36890a708f8fSGustavo F. Padovan return 0; 36900a708f8fSGustavo F. Padovan } 36910a708f8fSGustavo F. Padovan 36920a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb) 36930a708f8fSGustavo F. Padovan { 3694525cd185SGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 36950a708f8fSGustavo F. Padovan u16 control; 36960b209faeSAndrei Emeltchenko u16 req_seq; 36970a708f8fSGustavo F. Padovan int len, next_tx_seq_offset, req_seq_offset; 36980a708f8fSGustavo F. Padovan 36990a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data); 37000a708f8fSGustavo F. Padovan skb_pull(skb, 2); 37010a708f8fSGustavo F. Padovan len = skb->len; 37020a708f8fSGustavo F. Padovan 37030a708f8fSGustavo F. Padovan /* 37040a708f8fSGustavo F. Padovan * We can just drop the corrupted I-frame here. 37050a708f8fSGustavo F. Padovan * Receiver will miss it and start proper recovery 37060a708f8fSGustavo F. Padovan * procedures and ask retransmission. 37070a708f8fSGustavo F. Padovan */ 370847d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 37090a708f8fSGustavo F. Padovan goto drop; 37100a708f8fSGustavo F. Padovan 3711793c2f1cSAndrei Emeltchenko if (__is_sar_start(chan, control) && !__is_sframe(chan, control)) 37120a708f8fSGustavo F. Padovan len -= 2; 37130a708f8fSGustavo F. Padovan 371447d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 37150a708f8fSGustavo F. Padovan len -= 2; 37160a708f8fSGustavo F. Padovan 371747d1ec61SGustavo F. Padovan if (len > chan->mps) { 37188c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 37190a708f8fSGustavo F. Padovan goto drop; 37200a708f8fSGustavo F. Padovan } 37210a708f8fSGustavo F. Padovan 37220b209faeSAndrei Emeltchenko req_seq = __get_reqseq(chan, control); 372342e5c802SGustavo F. Padovan req_seq_offset = (req_seq - chan->expected_ack_seq) % 64; 37240a708f8fSGustavo F. Padovan if (req_seq_offset < 0) 37250a708f8fSGustavo F. Padovan req_seq_offset += 64; 37260a708f8fSGustavo F. Padovan 37270a708f8fSGustavo F. Padovan next_tx_seq_offset = 372842e5c802SGustavo F. Padovan (chan->next_tx_seq - chan->expected_ack_seq) % 64; 37290a708f8fSGustavo F. Padovan if (next_tx_seq_offset < 0) 37300a708f8fSGustavo F. Padovan next_tx_seq_offset += 64; 37310a708f8fSGustavo F. Padovan 37320a708f8fSGustavo F. Padovan /* check for invalid req-seq */ 37330a708f8fSGustavo F. Padovan if (req_seq_offset > next_tx_seq_offset) { 37348c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 37350a708f8fSGustavo F. Padovan goto drop; 37360a708f8fSGustavo F. Padovan } 37370a708f8fSGustavo F. Padovan 3738793c2f1cSAndrei Emeltchenko if (!__is_sframe(chan, control)) { 37390a708f8fSGustavo F. Padovan if (len < 0) { 37408c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 37410a708f8fSGustavo F. Padovan goto drop; 37420a708f8fSGustavo F. Padovan } 37430a708f8fSGustavo F. Padovan 3744525cd185SGustavo F. Padovan l2cap_data_channel_iframe(chan, control, skb); 37450a708f8fSGustavo F. Padovan } else { 37460a708f8fSGustavo F. Padovan if (len != 0) { 37470a708f8fSGustavo F. Padovan BT_ERR("%d", len); 37488c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 37490a708f8fSGustavo F. Padovan goto drop; 37500a708f8fSGustavo F. Padovan } 37510a708f8fSGustavo F. Padovan 3752525cd185SGustavo F. Padovan l2cap_data_channel_sframe(chan, control, skb); 37530a708f8fSGustavo F. Padovan } 37540a708f8fSGustavo F. Padovan 37550a708f8fSGustavo F. Padovan return 0; 37560a708f8fSGustavo F. Padovan 37570a708f8fSGustavo F. Padovan drop: 37580a708f8fSGustavo F. Padovan kfree_skb(skb); 37590a708f8fSGustavo F. Padovan return 0; 37600a708f8fSGustavo F. Padovan } 37610a708f8fSGustavo F. Padovan 37620a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) 37630a708f8fSGustavo F. Padovan { 376448454079SGustavo F. Padovan struct l2cap_chan *chan; 3765bf734843SDavid S. Miller struct sock *sk = NULL; 37660a708f8fSGustavo F. Padovan u16 control; 3767fb45de7dSAndrei Emeltchenko u16 tx_seq; 37680a708f8fSGustavo F. Padovan int len; 37690a708f8fSGustavo F. Padovan 3770baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 377148454079SGustavo F. Padovan if (!chan) { 37720a708f8fSGustavo F. Padovan BT_DBG("unknown cid 0x%4.4x", cid); 37730a708f8fSGustavo F. Padovan goto drop; 37740a708f8fSGustavo F. Padovan } 37750a708f8fSGustavo F. Padovan 377648454079SGustavo F. Padovan sk = chan->sk; 37770a708f8fSGustavo F. Padovan 377849208c9cSGustavo F. Padovan BT_DBG("chan %p, len %d", chan, skb->len); 37790a708f8fSGustavo F. Padovan 378089bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 37810a708f8fSGustavo F. Padovan goto drop; 37820a708f8fSGustavo F. Padovan 37830c1bc5c6SGustavo F. Padovan switch (chan->mode) { 37840a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 37850a708f8fSGustavo F. Padovan /* If socket recv buffers overflows we drop data here 37860a708f8fSGustavo F. Padovan * which is *bad* because L2CAP has to be reliable. 37870a708f8fSGustavo F. Padovan * But we don't have any other choice. L2CAP doesn't 37880a708f8fSGustavo F. Padovan * provide flow control mechanism. */ 37890a708f8fSGustavo F. Padovan 37900c1bc5c6SGustavo F. Padovan if (chan->imtu < skb->len) 37910a708f8fSGustavo F. Padovan goto drop; 37920a708f8fSGustavo F. Padovan 379323070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 37940a708f8fSGustavo F. Padovan goto done; 37950a708f8fSGustavo F. Padovan break; 37960a708f8fSGustavo F. Padovan 37970a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 37980a708f8fSGustavo F. Padovan if (!sock_owned_by_user(sk)) { 37990a708f8fSGustavo F. Padovan l2cap_ertm_data_rcv(sk, skb); 38000a708f8fSGustavo F. Padovan } else { 38010a708f8fSGustavo F. Padovan if (sk_add_backlog(sk, skb)) 38020a708f8fSGustavo F. Padovan goto drop; 38030a708f8fSGustavo F. Padovan } 38040a708f8fSGustavo F. Padovan 38050a708f8fSGustavo F. Padovan goto done; 38060a708f8fSGustavo F. Padovan 38070a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 38080a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data); 38090a708f8fSGustavo F. Padovan skb_pull(skb, 2); 38100a708f8fSGustavo F. Padovan len = skb->len; 38110a708f8fSGustavo F. Padovan 381247d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 38130a708f8fSGustavo F. Padovan goto drop; 38140a708f8fSGustavo F. Padovan 38157e0ef6eeSAndrei Emeltchenko if (__is_sar_start(chan, control)) 38160a708f8fSGustavo F. Padovan len -= 2; 38170a708f8fSGustavo F. Padovan 381847d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 38190a708f8fSGustavo F. Padovan len -= 2; 38200a708f8fSGustavo F. Padovan 3821793c2f1cSAndrei Emeltchenko if (len > chan->mps || len < 0 || __is_sframe(chan, control)) 38220a708f8fSGustavo F. Padovan goto drop; 38230a708f8fSGustavo F. Padovan 3824fb45de7dSAndrei Emeltchenko tx_seq = __get_txseq(chan, control); 38250a708f8fSGustavo F. Padovan 382684084a31SMat Martineau if (chan->expected_tx_seq != tx_seq) { 382784084a31SMat Martineau /* Frame(s) missing - must discard partial SDU */ 382884084a31SMat Martineau kfree_skb(chan->sdu); 382984084a31SMat Martineau chan->sdu = NULL; 383084084a31SMat Martineau chan->sdu_last_frag = NULL; 383184084a31SMat Martineau chan->sdu_len = 0; 383284084a31SMat Martineau 383384084a31SMat Martineau /* TODO: Notify userland of missing data */ 383484084a31SMat Martineau } 383584084a31SMat Martineau 383642e5c802SGustavo F. Padovan chan->expected_tx_seq = (tx_seq + 1) % 64; 38370a708f8fSGustavo F. Padovan 383884084a31SMat Martineau if (l2cap_reassemble_sdu(chan, skb, control) == -EMSGSIZE) 383984084a31SMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38400a708f8fSGustavo F. Padovan 38410a708f8fSGustavo F. Padovan goto done; 38420a708f8fSGustavo F. Padovan 38430a708f8fSGustavo F. Padovan default: 38440c1bc5c6SGustavo F. Padovan BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode); 38450a708f8fSGustavo F. Padovan break; 38460a708f8fSGustavo F. Padovan } 38470a708f8fSGustavo F. Padovan 38480a708f8fSGustavo F. Padovan drop: 38490a708f8fSGustavo F. Padovan kfree_skb(skb); 38500a708f8fSGustavo F. Padovan 38510a708f8fSGustavo F. Padovan done: 38520a708f8fSGustavo F. Padovan if (sk) 38530a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 38540a708f8fSGustavo F. Padovan 38550a708f8fSGustavo F. Padovan return 0; 38560a708f8fSGustavo F. Padovan } 38570a708f8fSGustavo F. Padovan 38580a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb) 38590a708f8fSGustavo F. Padovan { 38606dcae1eaSDavid S. Miller struct sock *sk = NULL; 386123691d75SGustavo F. Padovan struct l2cap_chan *chan; 38620a708f8fSGustavo F. Padovan 386323691d75SGustavo F. Padovan chan = l2cap_global_chan_by_psm(0, psm, conn->src); 386423691d75SGustavo F. Padovan if (!chan) 38650a708f8fSGustavo F. Padovan goto drop; 38660a708f8fSGustavo F. Padovan 386723691d75SGustavo F. Padovan sk = chan->sk; 386823691d75SGustavo F. Padovan 38690a708f8fSGustavo F. Padovan bh_lock_sock(sk); 38700a708f8fSGustavo F. Padovan 38710a708f8fSGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 38720a708f8fSGustavo F. Padovan 387389bc500eSGustavo F. Padovan if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) 38740a708f8fSGustavo F. Padovan goto drop; 38750a708f8fSGustavo F. Padovan 3876e13e21dcSVinicius Costa Gomes if (chan->imtu < skb->len) 38770a708f8fSGustavo F. Padovan goto drop; 38780a708f8fSGustavo F. Padovan 387923070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 38800a708f8fSGustavo F. Padovan goto done; 38810a708f8fSGustavo F. Padovan 38820a708f8fSGustavo F. Padovan drop: 38830a708f8fSGustavo F. Padovan kfree_skb(skb); 38840a708f8fSGustavo F. Padovan 38850a708f8fSGustavo F. Padovan done: 38860a708f8fSGustavo F. Padovan if (sk) 38870a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 38880a708f8fSGustavo F. Padovan return 0; 38890a708f8fSGustavo F. Padovan } 38900a708f8fSGustavo F. Padovan 38919f69bda6SGustavo F. Padovan static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb) 38929f69bda6SGustavo F. Padovan { 38936dcae1eaSDavid S. Miller struct sock *sk = NULL; 389423691d75SGustavo F. Padovan struct l2cap_chan *chan; 38959f69bda6SGustavo F. Padovan 389623691d75SGustavo F. Padovan chan = l2cap_global_chan_by_scid(0, cid, conn->src); 389723691d75SGustavo F. Padovan if (!chan) 38989f69bda6SGustavo F. Padovan goto drop; 38999f69bda6SGustavo F. Padovan 390023691d75SGustavo F. Padovan sk = chan->sk; 390123691d75SGustavo F. Padovan 39029f69bda6SGustavo F. Padovan bh_lock_sock(sk); 39039f69bda6SGustavo F. Padovan 39049f69bda6SGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 39059f69bda6SGustavo F. Padovan 390689bc500eSGustavo F. Padovan if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) 39079f69bda6SGustavo F. Padovan goto drop; 39089f69bda6SGustavo F. Padovan 3909e13e21dcSVinicius Costa Gomes if (chan->imtu < skb->len) 39109f69bda6SGustavo F. Padovan goto drop; 39119f69bda6SGustavo F. Padovan 391223070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 39139f69bda6SGustavo F. Padovan goto done; 39149f69bda6SGustavo F. Padovan 39159f69bda6SGustavo F. Padovan drop: 39169f69bda6SGustavo F. Padovan kfree_skb(skb); 39179f69bda6SGustavo F. Padovan 39189f69bda6SGustavo F. Padovan done: 39199f69bda6SGustavo F. Padovan if (sk) 39209f69bda6SGustavo F. Padovan bh_unlock_sock(sk); 39219f69bda6SGustavo F. Padovan return 0; 39229f69bda6SGustavo F. Padovan } 39239f69bda6SGustavo F. Padovan 39240a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) 39250a708f8fSGustavo F. Padovan { 39260a708f8fSGustavo F. Padovan struct l2cap_hdr *lh = (void *) skb->data; 39270a708f8fSGustavo F. Padovan u16 cid, len; 39280a708f8fSGustavo F. Padovan __le16 psm; 39290a708f8fSGustavo F. Padovan 39300a708f8fSGustavo F. Padovan skb_pull(skb, L2CAP_HDR_SIZE); 39310a708f8fSGustavo F. Padovan cid = __le16_to_cpu(lh->cid); 39320a708f8fSGustavo F. Padovan len = __le16_to_cpu(lh->len); 39330a708f8fSGustavo F. Padovan 39340a708f8fSGustavo F. Padovan if (len != skb->len) { 39350a708f8fSGustavo F. Padovan kfree_skb(skb); 39360a708f8fSGustavo F. Padovan return; 39370a708f8fSGustavo F. Padovan } 39380a708f8fSGustavo F. Padovan 39390a708f8fSGustavo F. Padovan BT_DBG("len %d, cid 0x%4.4x", len, cid); 39400a708f8fSGustavo F. Padovan 39410a708f8fSGustavo F. Padovan switch (cid) { 39423300d9a9SClaudio Takahasi case L2CAP_CID_LE_SIGNALING: 39430a708f8fSGustavo F. Padovan case L2CAP_CID_SIGNALING: 39440a708f8fSGustavo F. Padovan l2cap_sig_channel(conn, skb); 39450a708f8fSGustavo F. Padovan break; 39460a708f8fSGustavo F. Padovan 39470a708f8fSGustavo F. Padovan case L2CAP_CID_CONN_LESS: 39480a708f8fSGustavo F. Padovan psm = get_unaligned_le16(skb->data); 39490a708f8fSGustavo F. Padovan skb_pull(skb, 2); 39500a708f8fSGustavo F. Padovan l2cap_conless_channel(conn, psm, skb); 39510a708f8fSGustavo F. Padovan break; 39520a708f8fSGustavo F. Padovan 39539f69bda6SGustavo F. Padovan case L2CAP_CID_LE_DATA: 39549f69bda6SGustavo F. Padovan l2cap_att_channel(conn, cid, skb); 39559f69bda6SGustavo F. Padovan break; 39569f69bda6SGustavo F. Padovan 3957b501d6a1SAnderson Briglia case L2CAP_CID_SMP: 3958b501d6a1SAnderson Briglia if (smp_sig_channel(conn, skb)) 3959b501d6a1SAnderson Briglia l2cap_conn_del(conn->hcon, EACCES); 3960b501d6a1SAnderson Briglia break; 3961b501d6a1SAnderson Briglia 39620a708f8fSGustavo F. Padovan default: 39630a708f8fSGustavo F. Padovan l2cap_data_channel(conn, cid, skb); 39640a708f8fSGustavo F. Padovan break; 39650a708f8fSGustavo F. Padovan } 39660a708f8fSGustavo F. Padovan } 39670a708f8fSGustavo F. Padovan 39680a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */ 39690a708f8fSGustavo F. Padovan 39700a708f8fSGustavo F. Padovan static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) 39710a708f8fSGustavo F. Padovan { 39720a708f8fSGustavo F. Padovan int exact = 0, lm1 = 0, lm2 = 0; 397323691d75SGustavo F. Padovan struct l2cap_chan *c; 39740a708f8fSGustavo F. Padovan 39750a708f8fSGustavo F. Padovan if (type != ACL_LINK) 39760a708f8fSGustavo F. Padovan return -EINVAL; 39770a708f8fSGustavo F. Padovan 39780a708f8fSGustavo F. Padovan BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); 39790a708f8fSGustavo F. Padovan 39800a708f8fSGustavo F. Padovan /* Find listening sockets and check their link_mode */ 398123691d75SGustavo F. Padovan read_lock(&chan_list_lock); 398223691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 398323691d75SGustavo F. Padovan struct sock *sk = c->sk; 39844343478fSGustavo F. Padovan 398589bc500eSGustavo F. Padovan if (c->state != BT_LISTEN) 39860a708f8fSGustavo F. Padovan continue; 39870a708f8fSGustavo F. Padovan 39880a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { 39890a708f8fSGustavo F. Padovan lm1 |= HCI_LM_ACCEPT; 399043bd0f32SAndrei Emeltchenko if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 39910a708f8fSGustavo F. Padovan lm1 |= HCI_LM_MASTER; 39920a708f8fSGustavo F. Padovan exact++; 39930a708f8fSGustavo F. Padovan } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { 39940a708f8fSGustavo F. Padovan lm2 |= HCI_LM_ACCEPT; 399543bd0f32SAndrei Emeltchenko if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 39960a708f8fSGustavo F. Padovan lm2 |= HCI_LM_MASTER; 39970a708f8fSGustavo F. Padovan } 39980a708f8fSGustavo F. Padovan } 399923691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 40000a708f8fSGustavo F. Padovan 40010a708f8fSGustavo F. Padovan return exact ? lm1 : lm2; 40020a708f8fSGustavo F. Padovan } 40030a708f8fSGustavo F. Padovan 40040a708f8fSGustavo F. Padovan static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) 40050a708f8fSGustavo F. Padovan { 40060a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 40070a708f8fSGustavo F. Padovan 40080a708f8fSGustavo F. Padovan BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); 40090a708f8fSGustavo F. Padovan 4010acd7d370SVille Tervo if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) 40110a708f8fSGustavo F. Padovan return -EINVAL; 40120a708f8fSGustavo F. Padovan 40130a708f8fSGustavo F. Padovan if (!status) { 40140a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, status); 40150a708f8fSGustavo F. Padovan if (conn) 40160a708f8fSGustavo F. Padovan l2cap_conn_ready(conn); 40170a708f8fSGustavo F. Padovan } else 4018e175072fSJoe Perches l2cap_conn_del(hcon, bt_to_errno(status)); 40190a708f8fSGustavo F. Padovan 40200a708f8fSGustavo F. Padovan return 0; 40210a708f8fSGustavo F. Padovan } 40220a708f8fSGustavo F. Padovan 40230a708f8fSGustavo F. Padovan static int l2cap_disconn_ind(struct hci_conn *hcon) 40240a708f8fSGustavo F. Padovan { 40250a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 40260a708f8fSGustavo F. Padovan 40270a708f8fSGustavo F. Padovan BT_DBG("hcon %p", hcon); 40280a708f8fSGustavo F. Padovan 4029b5694506SGustavo F. Padovan if ((hcon->type != ACL_LINK && hcon->type != LE_LINK) || !conn) 40300a708f8fSGustavo F. Padovan return 0x13; 40310a708f8fSGustavo F. Padovan 40320a708f8fSGustavo F. Padovan return conn->disc_reason; 40330a708f8fSGustavo F. Padovan } 40340a708f8fSGustavo F. Padovan 40350a708f8fSGustavo F. Padovan static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) 40360a708f8fSGustavo F. Padovan { 40370a708f8fSGustavo F. Padovan BT_DBG("hcon %p reason %d", hcon, reason); 40380a708f8fSGustavo F. Padovan 4039acd7d370SVille Tervo if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) 40400a708f8fSGustavo F. Padovan return -EINVAL; 40410a708f8fSGustavo F. Padovan 4042e175072fSJoe Perches l2cap_conn_del(hcon, bt_to_errno(reason)); 40430a708f8fSGustavo F. Padovan 40440a708f8fSGustavo F. Padovan return 0; 40450a708f8fSGustavo F. Padovan } 40460a708f8fSGustavo F. Padovan 40474343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) 40480a708f8fSGustavo F. Padovan { 4049715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) 40500a708f8fSGustavo F. Padovan return; 40510a708f8fSGustavo F. Padovan 40520a708f8fSGustavo F. Padovan if (encrypt == 0x00) { 40534343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) { 4054c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 4055c9b66675SGustavo F. Padovan __set_chan_timer(chan, HZ * 5); 40564343478fSGustavo F. Padovan } else if (chan->sec_level == BT_SECURITY_HIGH) 40570f852724SGustavo F. Padovan l2cap_chan_close(chan, ECONNREFUSED); 40580a708f8fSGustavo F. Padovan } else { 40594343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) 4060c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 40610a708f8fSGustavo F. Padovan } 40620a708f8fSGustavo F. Padovan } 40630a708f8fSGustavo F. Padovan 40640a708f8fSGustavo F. Padovan static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) 40650a708f8fSGustavo F. Padovan { 40660a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 406748454079SGustavo F. Padovan struct l2cap_chan *chan; 40680a708f8fSGustavo F. Padovan 40690a708f8fSGustavo F. Padovan if (!conn) 40700a708f8fSGustavo F. Padovan return 0; 40710a708f8fSGustavo F. Padovan 40720a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 40730a708f8fSGustavo F. Padovan 4074160dc6acSVinicius Costa Gomes if (hcon->type == LE_LINK) { 4075160dc6acSVinicius Costa Gomes smp_distribute_keys(conn, 0); 4076160dc6acSVinicius Costa Gomes del_timer(&conn->security_timer); 4077160dc6acSVinicius Costa Gomes } 4078160dc6acSVinicius Costa Gomes 4079baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 40800a708f8fSGustavo F. Padovan 4081baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 408248454079SGustavo F. Padovan struct sock *sk = chan->sk; 4083baa7e1faSGustavo F. Padovan 40840a708f8fSGustavo F. Padovan bh_lock_sock(sk); 40850a708f8fSGustavo F. Padovan 4086f1cb9af5SVinicius Costa Gomes BT_DBG("chan->scid %d", chan->scid); 4087f1cb9af5SVinicius Costa Gomes 4088f1cb9af5SVinicius Costa Gomes if (chan->scid == L2CAP_CID_LE_DATA) { 4089f1cb9af5SVinicius Costa Gomes if (!status && encrypt) { 4090f1cb9af5SVinicius Costa Gomes chan->sec_level = hcon->sec_level; 4091f1cb9af5SVinicius Costa Gomes l2cap_chan_ready(sk); 4092f1cb9af5SVinicius Costa Gomes } 4093f1cb9af5SVinicius Costa Gomes 4094f1cb9af5SVinicius Costa Gomes bh_unlock_sock(sk); 4095f1cb9af5SVinicius Costa Gomes continue; 4096f1cb9af5SVinicius Costa Gomes } 4097f1cb9af5SVinicius Costa Gomes 4098c1360a1cSGustavo F. Padovan if (test_bit(CONF_CONNECT_PEND, &chan->conf_state)) { 40990a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 41000a708f8fSGustavo F. Padovan continue; 41010a708f8fSGustavo F. Padovan } 41020a708f8fSGustavo F. Padovan 410389bc500eSGustavo F. Padovan if (!status && (chan->state == BT_CONNECTED || 410489bc500eSGustavo F. Padovan chan->state == BT_CONFIG)) { 41054343478fSGustavo F. Padovan l2cap_check_encryption(chan, encrypt); 41060a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 41070a708f8fSGustavo F. Padovan continue; 41080a708f8fSGustavo F. Padovan } 41090a708f8fSGustavo F. Padovan 411089bc500eSGustavo F. Padovan if (chan->state == BT_CONNECT) { 41110a708f8fSGustavo F. Padovan if (!status) { 41120a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 4113fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 4114fe4128e0SGustavo F. Padovan req.psm = chan->psm; 41150a708f8fSGustavo F. Padovan 4116fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 4117c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 41180a708f8fSGustavo F. Padovan 4119fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 41200a708f8fSGustavo F. Padovan L2CAP_CONN_REQ, sizeof(req), &req); 41210a708f8fSGustavo F. Padovan } else { 4122c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 4123c9b66675SGustavo F. Padovan __set_chan_timer(chan, HZ / 10); 41240a708f8fSGustavo F. Padovan } 412589bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT2) { 41260a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 4127df3c3931SJohan Hedberg __u16 res, stat; 41280a708f8fSGustavo F. Padovan 41290a708f8fSGustavo F. Padovan if (!status) { 4130df3c3931SJohan Hedberg if (bt_sk(sk)->defer_setup) { 4131df3c3931SJohan Hedberg struct sock *parent = bt_sk(sk)->parent; 4132df3c3931SJohan Hedberg res = L2CAP_CR_PEND; 4133df3c3931SJohan Hedberg stat = L2CAP_CS_AUTHOR_PEND; 413405e9a2f6SIlia Kolomisnky if (parent) 4135df3c3931SJohan Hedberg parent->sk_data_ready(parent, 0); 4136df3c3931SJohan Hedberg } else { 413789bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONFIG); 4138df3c3931SJohan Hedberg res = L2CAP_CR_SUCCESS; 4139df3c3931SJohan Hedberg stat = L2CAP_CS_NO_INFO; 4140df3c3931SJohan Hedberg } 41410a708f8fSGustavo F. Padovan } else { 414289bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_DISCONN); 4143c9b66675SGustavo F. Padovan __set_chan_timer(chan, HZ / 10); 4144df3c3931SJohan Hedberg res = L2CAP_CR_SEC_BLOCK; 4145df3c3931SJohan Hedberg stat = L2CAP_CS_NO_INFO; 41460a708f8fSGustavo F. Padovan } 41470a708f8fSGustavo F. Padovan 4148fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4149fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 4150df3c3931SJohan Hedberg rsp.result = cpu_to_le16(res); 4151df3c3931SJohan Hedberg rsp.status = cpu_to_le16(stat); 4152fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4153fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 41540a708f8fSGustavo F. Padovan } 41550a708f8fSGustavo F. Padovan 41560a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 41570a708f8fSGustavo F. Padovan } 41580a708f8fSGustavo F. Padovan 4159baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 41600a708f8fSGustavo F. Padovan 41610a708f8fSGustavo F. Padovan return 0; 41620a708f8fSGustavo F. Padovan } 41630a708f8fSGustavo F. Padovan 41640a708f8fSGustavo F. Padovan static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) 41650a708f8fSGustavo F. Padovan { 41660a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 41670a708f8fSGustavo F. Padovan 41680a708f8fSGustavo F. Padovan if (!conn) 41690a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 41700a708f8fSGustavo F. Padovan 41710a708f8fSGustavo F. Padovan if (!conn) 41720a708f8fSGustavo F. Padovan goto drop; 41730a708f8fSGustavo F. Padovan 41740a708f8fSGustavo F. Padovan BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); 41750a708f8fSGustavo F. Padovan 41760a708f8fSGustavo F. Padovan if (!(flags & ACL_CONT)) { 41770a708f8fSGustavo F. Padovan struct l2cap_hdr *hdr; 417848454079SGustavo F. Padovan struct l2cap_chan *chan; 41790a708f8fSGustavo F. Padovan u16 cid; 41800a708f8fSGustavo F. Padovan int len; 41810a708f8fSGustavo F. Padovan 41820a708f8fSGustavo F. Padovan if (conn->rx_len) { 41830a708f8fSGustavo F. Padovan BT_ERR("Unexpected start frame (len %d)", skb->len); 41840a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 41850a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 41860a708f8fSGustavo F. Padovan conn->rx_len = 0; 41870a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 41880a708f8fSGustavo F. Padovan } 41890a708f8fSGustavo F. Padovan 41900a708f8fSGustavo F. Padovan /* Start fragment always begin with Basic L2CAP header */ 41910a708f8fSGustavo F. Padovan if (skb->len < L2CAP_HDR_SIZE) { 41920a708f8fSGustavo F. Padovan BT_ERR("Frame is too short (len %d)", skb->len); 41930a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 41940a708f8fSGustavo F. Padovan goto drop; 41950a708f8fSGustavo F. Padovan } 41960a708f8fSGustavo F. Padovan 41970a708f8fSGustavo F. Padovan hdr = (struct l2cap_hdr *) skb->data; 41980a708f8fSGustavo F. Padovan len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; 41990a708f8fSGustavo F. Padovan cid = __le16_to_cpu(hdr->cid); 42000a708f8fSGustavo F. Padovan 42010a708f8fSGustavo F. Padovan if (len == skb->len) { 42020a708f8fSGustavo F. Padovan /* Complete frame received */ 42030a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, skb); 42040a708f8fSGustavo F. Padovan return 0; 42050a708f8fSGustavo F. Padovan } 42060a708f8fSGustavo F. Padovan 42070a708f8fSGustavo F. Padovan BT_DBG("Start: total len %d, frag len %d", len, skb->len); 42080a708f8fSGustavo F. Padovan 42090a708f8fSGustavo F. Padovan if (skb->len > len) { 42100a708f8fSGustavo F. Padovan BT_ERR("Frame is too long (len %d, expected len %d)", 42110a708f8fSGustavo F. Padovan skb->len, len); 42120a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42130a708f8fSGustavo F. Padovan goto drop; 42140a708f8fSGustavo F. Padovan } 42150a708f8fSGustavo F. Padovan 4216baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 42170a708f8fSGustavo F. Padovan 421848454079SGustavo F. Padovan if (chan && chan->sk) { 421948454079SGustavo F. Padovan struct sock *sk = chan->sk; 422048454079SGustavo F. Padovan 42210c1bc5c6SGustavo F. Padovan if (chan->imtu < len - L2CAP_HDR_SIZE) { 422248454079SGustavo F. Padovan BT_ERR("Frame exceeding recv MTU (len %d, " 422348454079SGustavo F. Padovan "MTU %d)", len, 42240c1bc5c6SGustavo F. Padovan chan->imtu); 42250a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 42260a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42270a708f8fSGustavo F. Padovan goto drop; 42280a708f8fSGustavo F. Padovan } 42290a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 423048454079SGustavo F. Padovan } 42310a708f8fSGustavo F. Padovan 42320a708f8fSGustavo F. Padovan /* Allocate skb for the complete frame (with header) */ 42330a708f8fSGustavo F. Padovan conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); 42340a708f8fSGustavo F. Padovan if (!conn->rx_skb) 42350a708f8fSGustavo F. Padovan goto drop; 42360a708f8fSGustavo F. Padovan 42370a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 42380a708f8fSGustavo F. Padovan skb->len); 42390a708f8fSGustavo F. Padovan conn->rx_len = len - skb->len; 42400a708f8fSGustavo F. Padovan } else { 42410a708f8fSGustavo F. Padovan BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); 42420a708f8fSGustavo F. Padovan 42430a708f8fSGustavo F. Padovan if (!conn->rx_len) { 42440a708f8fSGustavo F. Padovan BT_ERR("Unexpected continuation frame (len %d)", skb->len); 42450a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42460a708f8fSGustavo F. Padovan goto drop; 42470a708f8fSGustavo F. Padovan } 42480a708f8fSGustavo F. Padovan 42490a708f8fSGustavo F. Padovan if (skb->len > conn->rx_len) { 42500a708f8fSGustavo F. Padovan BT_ERR("Fragment is too long (len %d, expected %d)", 42510a708f8fSGustavo F. Padovan skb->len, conn->rx_len); 42520a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 42530a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 42540a708f8fSGustavo F. Padovan conn->rx_len = 0; 42550a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42560a708f8fSGustavo F. Padovan goto drop; 42570a708f8fSGustavo F. Padovan } 42580a708f8fSGustavo F. Padovan 42590a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 42600a708f8fSGustavo F. Padovan skb->len); 42610a708f8fSGustavo F. Padovan conn->rx_len -= skb->len; 42620a708f8fSGustavo F. Padovan 42630a708f8fSGustavo F. Padovan if (!conn->rx_len) { 42640a708f8fSGustavo F. Padovan /* Complete frame received */ 42650a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, conn->rx_skb); 42660a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 42670a708f8fSGustavo F. Padovan } 42680a708f8fSGustavo F. Padovan } 42690a708f8fSGustavo F. Padovan 42700a708f8fSGustavo F. Padovan drop: 42710a708f8fSGustavo F. Padovan kfree_skb(skb); 42720a708f8fSGustavo F. Padovan return 0; 42730a708f8fSGustavo F. Padovan } 42740a708f8fSGustavo F. Padovan 42750a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p) 42760a708f8fSGustavo F. Padovan { 427723691d75SGustavo F. Padovan struct l2cap_chan *c; 42780a708f8fSGustavo F. Padovan 427923691d75SGustavo F. Padovan read_lock_bh(&chan_list_lock); 42800a708f8fSGustavo F. Padovan 428123691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 428223691d75SGustavo F. Padovan struct sock *sk = c->sk; 42830a708f8fSGustavo F. Padovan 4284903d343eSGustavo F. Padovan seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", 42850a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->src), 42860a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->dst), 428789bc500eSGustavo F. Padovan c->state, __le16_to_cpu(c->psm), 428823691d75SGustavo F. Padovan c->scid, c->dcid, c->imtu, c->omtu, 428923691d75SGustavo F. Padovan c->sec_level, c->mode); 42900a708f8fSGustavo F. Padovan } 42910a708f8fSGustavo F. Padovan 429223691d75SGustavo F. Padovan read_unlock_bh(&chan_list_lock); 42930a708f8fSGustavo F. Padovan 42940a708f8fSGustavo F. Padovan return 0; 42950a708f8fSGustavo F. Padovan } 42960a708f8fSGustavo F. Padovan 42970a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file) 42980a708f8fSGustavo F. Padovan { 42990a708f8fSGustavo F. Padovan return single_open(file, l2cap_debugfs_show, inode->i_private); 43000a708f8fSGustavo F. Padovan } 43010a708f8fSGustavo F. Padovan 43020a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = { 43030a708f8fSGustavo F. Padovan .open = l2cap_debugfs_open, 43040a708f8fSGustavo F. Padovan .read = seq_read, 43050a708f8fSGustavo F. Padovan .llseek = seq_lseek, 43060a708f8fSGustavo F. Padovan .release = single_release, 43070a708f8fSGustavo F. Padovan }; 43080a708f8fSGustavo F. Padovan 43090a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs; 43100a708f8fSGustavo F. Padovan 43110a708f8fSGustavo F. Padovan static struct hci_proto l2cap_hci_proto = { 43120a708f8fSGustavo F. Padovan .name = "L2CAP", 43130a708f8fSGustavo F. Padovan .id = HCI_PROTO_L2CAP, 43140a708f8fSGustavo F. Padovan .connect_ind = l2cap_connect_ind, 43150a708f8fSGustavo F. Padovan .connect_cfm = l2cap_connect_cfm, 43160a708f8fSGustavo F. Padovan .disconn_ind = l2cap_disconn_ind, 43170a708f8fSGustavo F. Padovan .disconn_cfm = l2cap_disconn_cfm, 43180a708f8fSGustavo F. Padovan .security_cfm = l2cap_security_cfm, 43190a708f8fSGustavo F. Padovan .recv_acldata = l2cap_recv_acldata 43200a708f8fSGustavo F. Padovan }; 43210a708f8fSGustavo F. Padovan 432264274518SGustavo F. Padovan int __init l2cap_init(void) 43230a708f8fSGustavo F. Padovan { 43240a708f8fSGustavo F. Padovan int err; 43250a708f8fSGustavo F. Padovan 4326bb58f747SGustavo F. Padovan err = l2cap_init_sockets(); 43270a708f8fSGustavo F. Padovan if (err < 0) 43280a708f8fSGustavo F. Padovan return err; 43290a708f8fSGustavo F. Padovan 43300a708f8fSGustavo F. Padovan err = hci_register_proto(&l2cap_hci_proto); 43310a708f8fSGustavo F. Padovan if (err < 0) { 43320a708f8fSGustavo F. Padovan BT_ERR("L2CAP protocol registration failed"); 43330a708f8fSGustavo F. Padovan bt_sock_unregister(BTPROTO_L2CAP); 43340a708f8fSGustavo F. Padovan goto error; 43350a708f8fSGustavo F. Padovan } 43360a708f8fSGustavo F. Padovan 43370a708f8fSGustavo F. Padovan if (bt_debugfs) { 43380a708f8fSGustavo F. Padovan l2cap_debugfs = debugfs_create_file("l2cap", 0444, 43390a708f8fSGustavo F. Padovan bt_debugfs, NULL, &l2cap_debugfs_fops); 43400a708f8fSGustavo F. Padovan if (!l2cap_debugfs) 43410a708f8fSGustavo F. Padovan BT_ERR("Failed to create L2CAP debug file"); 43420a708f8fSGustavo F. Padovan } 43430a708f8fSGustavo F. Padovan 43440a708f8fSGustavo F. Padovan return 0; 43450a708f8fSGustavo F. Padovan 43460a708f8fSGustavo F. Padovan error: 4347bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 43480a708f8fSGustavo F. Padovan return err; 43490a708f8fSGustavo F. Padovan } 43500a708f8fSGustavo F. Padovan 435164274518SGustavo F. Padovan void l2cap_exit(void) 43520a708f8fSGustavo F. Padovan { 43530a708f8fSGustavo F. Padovan debugfs_remove(l2cap_debugfs); 43540a708f8fSGustavo F. Padovan 43550a708f8fSGustavo F. Padovan if (hci_unregister_proto(&l2cap_hci_proto) < 0) 43560a708f8fSGustavo F. Padovan BT_ERR("L2CAP protocol unregistration failed"); 43570a708f8fSGustavo F. Padovan 4358bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 43590a708f8fSGustavo F. Padovan } 43600a708f8fSGustavo F. Padovan 43610a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644); 43620a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); 4363a5fd6f30SAndrei Emeltchenko 4364a5fd6f30SAndrei Emeltchenko module_param(enable_hs, bool, 0644); 4365a5fd6f30SAndrei Emeltchenko MODULE_PARM_DESC(enable_hs, "Enable High Speed"); 4366