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> 570a708f8fSGustavo F. Padovan 58bb58f747SGustavo F. Padovan int disable_ertm; 590a708f8fSGustavo F. Padovan 600a708f8fSGustavo F. Padovan static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN; 610a708f8fSGustavo F. Padovan static u8 l2cap_fixed_chan[8] = { 0x02, }; 620a708f8fSGustavo F. Padovan 630a708f8fSGustavo F. Padovan static struct workqueue_struct *_busy_wq; 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 void l2cap_busy_work(struct work_struct *work); 690a708f8fSGustavo F. Padovan 700a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 710a708f8fSGustavo F. Padovan u8 code, u8 ident, u16 dlen, void *data); 724519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, 734519de9aSGustavo F. Padovan void *data); 74710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data); 754519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, 764519de9aSGustavo F. Padovan struct l2cap_chan *chan, int err); 770a708f8fSGustavo F. Padovan 780a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb); 790a708f8fSGustavo F. Padovan 800a708f8fSGustavo F. Padovan /* ---- L2CAP channels ---- */ 81baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid) 820a708f8fSGustavo F. Padovan { 8348454079SGustavo F. Padovan struct l2cap_chan *c; 84baa7e1faSGustavo F. Padovan 85baa7e1faSGustavo F. Padovan list_for_each_entry(c, &conn->chan_l, list) { 86fe4128e0SGustavo F. Padovan if (c->dcid == cid) 8748454079SGustavo F. Padovan return c; 880a708f8fSGustavo F. Padovan } 89baa7e1faSGustavo F. Padovan return NULL; 90baa7e1faSGustavo F. Padovan 91baa7e1faSGustavo F. Padovan } 920a708f8fSGustavo F. Padovan 93baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) 940a708f8fSGustavo F. Padovan { 9548454079SGustavo F. Padovan struct l2cap_chan *c; 96baa7e1faSGustavo F. Padovan 97baa7e1faSGustavo F. Padovan list_for_each_entry(c, &conn->chan_l, list) { 98fe4128e0SGustavo F. Padovan if (c->scid == cid) 9948454079SGustavo F. Padovan return c; 1000a708f8fSGustavo F. Padovan } 101baa7e1faSGustavo F. Padovan return NULL; 102baa7e1faSGustavo F. Padovan } 1030a708f8fSGustavo F. Padovan 1040a708f8fSGustavo F. Padovan /* Find channel with given SCID. 1050a708f8fSGustavo F. Padovan * Returns locked socket */ 106baa7e1faSGustavo F. Padovan static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) 1070a708f8fSGustavo F. Padovan { 10848454079SGustavo F. Padovan struct l2cap_chan *c; 109baa7e1faSGustavo F. Padovan 110baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 111baa7e1faSGustavo F. Padovan c = __l2cap_get_chan_by_scid(conn, cid); 11248454079SGustavo F. Padovan if (c) 11348454079SGustavo F. Padovan bh_lock_sock(c->sk); 114baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 11548454079SGustavo F. Padovan return c; 1160a708f8fSGustavo F. Padovan } 1170a708f8fSGustavo F. Padovan 118baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) 1190a708f8fSGustavo F. Padovan { 12048454079SGustavo F. Padovan struct l2cap_chan *c; 121baa7e1faSGustavo F. Padovan 122baa7e1faSGustavo F. Padovan list_for_each_entry(c, &conn->chan_l, list) { 123fc7f8a7eSGustavo F. Padovan if (c->ident == ident) 12448454079SGustavo F. Padovan return c; 1250a708f8fSGustavo F. Padovan } 126baa7e1faSGustavo F. Padovan return NULL; 127baa7e1faSGustavo F. Padovan } 1280a708f8fSGustavo F. Padovan 129baa7e1faSGustavo F. Padovan static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) 1300a708f8fSGustavo F. Padovan { 13148454079SGustavo F. Padovan struct l2cap_chan *c; 132baa7e1faSGustavo F. Padovan 133baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 134baa7e1faSGustavo F. Padovan c = __l2cap_get_chan_by_ident(conn, ident); 13548454079SGustavo F. Padovan if (c) 13648454079SGustavo F. Padovan bh_lock_sock(c->sk); 137baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 13848454079SGustavo F. Padovan return c; 1390a708f8fSGustavo F. Padovan } 1400a708f8fSGustavo F. Padovan 14123691d75SGustavo F. Padovan static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src) 1429e4425ffSGustavo F. Padovan { 14323691d75SGustavo F. Padovan struct l2cap_chan *c; 1449e4425ffSGustavo F. Padovan 14523691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 14623691d75SGustavo F. Padovan if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src)) 1479e4425ffSGustavo F. Padovan goto found; 1489e4425ffSGustavo F. Padovan } 1499e4425ffSGustavo F. Padovan 15023691d75SGustavo F. Padovan c = NULL; 1519e4425ffSGustavo F. Padovan found: 15223691d75SGustavo F. Padovan return c; 1539e4425ffSGustavo F. Padovan } 1549e4425ffSGustavo F. Padovan 1559e4425ffSGustavo F. Padovan int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) 1569e4425ffSGustavo F. Padovan { 15773b2ec18SGustavo F. Padovan int err; 15873b2ec18SGustavo F. Padovan 15923691d75SGustavo F. Padovan write_lock_bh(&chan_list_lock); 1609e4425ffSGustavo F. Padovan 16123691d75SGustavo F. Padovan if (psm && __l2cap_global_chan_by_addr(psm, src)) { 16273b2ec18SGustavo F. Padovan err = -EADDRINUSE; 16373b2ec18SGustavo F. Padovan goto done; 1649e4425ffSGustavo F. Padovan } 1659e4425ffSGustavo F. Padovan 16673b2ec18SGustavo F. Padovan if (psm) { 1679e4425ffSGustavo F. Padovan chan->psm = psm; 1689e4425ffSGustavo F. Padovan chan->sport = psm; 16973b2ec18SGustavo F. Padovan err = 0; 17073b2ec18SGustavo F. Padovan } else { 17173b2ec18SGustavo F. Padovan u16 p; 1729e4425ffSGustavo F. Padovan 17373b2ec18SGustavo F. Padovan err = -EINVAL; 17473b2ec18SGustavo F. Padovan for (p = 0x1001; p < 0x1100; p += 2) 17523691d75SGustavo F. Padovan if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { 17673b2ec18SGustavo F. Padovan chan->psm = cpu_to_le16(p); 17773b2ec18SGustavo F. Padovan chan->sport = cpu_to_le16(p); 17873b2ec18SGustavo F. Padovan err = 0; 17973b2ec18SGustavo F. Padovan break; 18073b2ec18SGustavo F. Padovan } 18173b2ec18SGustavo F. Padovan } 18273b2ec18SGustavo F. Padovan 18373b2ec18SGustavo F. Padovan done: 18423691d75SGustavo F. Padovan write_unlock_bh(&chan_list_lock); 18573b2ec18SGustavo F. Padovan return err; 1869e4425ffSGustavo F. Padovan } 1879e4425ffSGustavo F. Padovan 1889e4425ffSGustavo F. Padovan int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) 1899e4425ffSGustavo F. Padovan { 19023691d75SGustavo F. Padovan write_lock_bh(&chan_list_lock); 1919e4425ffSGustavo F. Padovan 1929e4425ffSGustavo F. Padovan chan->scid = scid; 1939e4425ffSGustavo F. Padovan 19423691d75SGustavo F. Padovan write_unlock_bh(&chan_list_lock); 1959e4425ffSGustavo F. Padovan 1969e4425ffSGustavo F. Padovan return 0; 1979e4425ffSGustavo F. Padovan } 1989e4425ffSGustavo F. Padovan 199baa7e1faSGustavo F. Padovan static u16 l2cap_alloc_cid(struct l2cap_conn *conn) 2000a708f8fSGustavo F. Padovan { 2010a708f8fSGustavo F. Padovan u16 cid = L2CAP_CID_DYN_START; 2020a708f8fSGustavo F. Padovan 2030a708f8fSGustavo F. Padovan for (; cid < L2CAP_CID_DYN_END; cid++) { 204baa7e1faSGustavo F. Padovan if (!__l2cap_get_chan_by_scid(conn, cid)) 2050a708f8fSGustavo F. Padovan return cid; 2060a708f8fSGustavo F. Padovan } 2070a708f8fSGustavo F. Padovan 2080a708f8fSGustavo F. Padovan return 0; 2090a708f8fSGustavo F. Padovan } 2100a708f8fSGustavo F. Padovan 211ab07801dSGustavo F. Padovan static void l2cap_chan_set_timer(struct l2cap_chan *chan, long timeout) 212ab07801dSGustavo F. Padovan { 213ab07801dSGustavo F. Padovan BT_DBG("chan %p state %d timeout %ld", chan->sk, chan->sk->sk_state, 214ab07801dSGustavo F. Padovan timeout); 215ab07801dSGustavo F. Padovan if (!mod_timer(&chan->chan_timer, jiffies + timeout)) 216ab07801dSGustavo F. Padovan sock_hold(chan->sk); 217ab07801dSGustavo F. Padovan } 218ab07801dSGustavo F. Padovan 219500698d3SGustavo F. Padovan static void l2cap_chan_clear_timer(struct l2cap_chan *chan) 220ab07801dSGustavo F. Padovan { 221ab07801dSGustavo F. Padovan BT_DBG("chan %p state %d", chan, chan->sk->sk_state); 222ab07801dSGustavo F. Padovan 223ab07801dSGustavo F. Padovan if (timer_pending(&chan->chan_timer) && del_timer(&chan->chan_timer)) 224ab07801dSGustavo F. Padovan __sock_put(chan->sk); 225ab07801dSGustavo F. Padovan } 226ab07801dSGustavo F. Padovan 227ab07801dSGustavo F. Padovan static void l2cap_chan_timeout(unsigned long arg) 228ab07801dSGustavo F. Padovan { 229ab07801dSGustavo F. Padovan struct l2cap_chan *chan = (struct l2cap_chan *) arg; 230ab07801dSGustavo F. Padovan struct sock *sk = chan->sk; 231ab07801dSGustavo F. Padovan int reason; 232ab07801dSGustavo F. Padovan 233ab07801dSGustavo F. Padovan BT_DBG("chan %p state %d", chan, sk->sk_state); 234ab07801dSGustavo F. Padovan 235ab07801dSGustavo F. Padovan bh_lock_sock(sk); 236ab07801dSGustavo F. Padovan 237ab07801dSGustavo F. Padovan if (sock_owned_by_user(sk)) { 238ab07801dSGustavo F. Padovan /* sk is owned by user. Try again later */ 239ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 5); 240ab07801dSGustavo F. Padovan bh_unlock_sock(sk); 241ab07801dSGustavo F. Padovan sock_put(sk); 242ab07801dSGustavo F. Padovan return; 243ab07801dSGustavo F. Padovan } 244ab07801dSGustavo F. Padovan 245ab07801dSGustavo F. Padovan if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONFIG) 246ab07801dSGustavo F. Padovan reason = ECONNREFUSED; 247ab07801dSGustavo F. Padovan else if (sk->sk_state == BT_CONNECT && 248ab07801dSGustavo F. Padovan chan->sec_level != BT_SECURITY_SDP) 249ab07801dSGustavo F. Padovan reason = ECONNREFUSED; 250ab07801dSGustavo F. Padovan else 251ab07801dSGustavo F. Padovan reason = ETIMEDOUT; 252ab07801dSGustavo F. Padovan 2530f852724SGustavo F. Padovan l2cap_chan_close(chan, reason); 254ab07801dSGustavo F. Padovan 255ab07801dSGustavo F. Padovan bh_unlock_sock(sk); 256ab07801dSGustavo F. Padovan 257ab07801dSGustavo F. Padovan l2cap_sock_kill(sk); 258ab07801dSGustavo F. Padovan sock_put(sk); 259ab07801dSGustavo F. Padovan } 260ab07801dSGustavo F. Padovan 26123691d75SGustavo F. Padovan struct l2cap_chan *l2cap_chan_create(struct sock *sk) 2620a708f8fSGustavo F. Padovan { 26348454079SGustavo F. Padovan struct l2cap_chan *chan; 2640a708f8fSGustavo F. Padovan 26548454079SGustavo F. Padovan chan = kzalloc(sizeof(*chan), GFP_ATOMIC); 26648454079SGustavo F. Padovan if (!chan) 26748454079SGustavo F. Padovan return NULL; 2680a708f8fSGustavo F. Padovan 26948454079SGustavo F. Padovan chan->sk = sk; 27048454079SGustavo F. Padovan 27123691d75SGustavo F. Padovan write_lock_bh(&chan_list_lock); 27223691d75SGustavo F. Padovan list_add(&chan->global_l, &chan_list); 27323691d75SGustavo F. Padovan write_unlock_bh(&chan_list_lock); 27423691d75SGustavo F. Padovan 275ab07801dSGustavo F. Padovan setup_timer(&chan->chan_timer, l2cap_chan_timeout, (unsigned long) chan); 276ab07801dSGustavo F. Padovan 27748454079SGustavo F. Padovan return chan; 2780a708f8fSGustavo F. Padovan } 2790a708f8fSGustavo F. Padovan 28023691d75SGustavo F. Padovan void l2cap_chan_destroy(struct l2cap_chan *chan) 2816ff5abbfSGustavo F. Padovan { 28223691d75SGustavo F. Padovan write_lock_bh(&chan_list_lock); 28323691d75SGustavo F. Padovan list_del(&chan->global_l); 28423691d75SGustavo F. Padovan write_unlock_bh(&chan_list_lock); 28523691d75SGustavo F. Padovan 2866ff5abbfSGustavo F. Padovan kfree(chan); 2876ff5abbfSGustavo F. Padovan } 2886ff5abbfSGustavo F. Padovan 28948454079SGustavo F. Padovan static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 2900a708f8fSGustavo F. Padovan { 29148454079SGustavo F. Padovan struct sock *sk = chan->sk; 2920a708f8fSGustavo F. Padovan 2930a708f8fSGustavo F. Padovan BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, 294fe4128e0SGustavo F. Padovan chan->psm, chan->dcid); 2950a708f8fSGustavo F. Padovan 2960a708f8fSGustavo F. Padovan conn->disc_reason = 0x13; 2970a708f8fSGustavo F. Padovan 2988c1d787bSGustavo F. Padovan chan->conn = conn; 2990a708f8fSGustavo F. Padovan 300715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { 301b62f328bSVille Tervo if (conn->hcon->type == LE_LINK) { 302b62f328bSVille Tervo /* LE connection */ 3030c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_LE_DEFAULT_MTU; 304fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_LE_DATA; 305fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_LE_DATA; 306b62f328bSVille Tervo } else { 3070a708f8fSGustavo F. Padovan /* Alloc CID for connection-oriented socket */ 308fe4128e0SGustavo F. Padovan chan->scid = l2cap_alloc_cid(conn); 3090c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 310b62f328bSVille Tervo } 311715ec005SGustavo F. Padovan } else if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { 3120a708f8fSGustavo F. Padovan /* Connectionless socket */ 313fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_CONN_LESS; 314fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_CONN_LESS; 3150c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 3160a708f8fSGustavo F. Padovan } else { 3170a708f8fSGustavo F. Padovan /* Raw socket can send/recv signalling messages only */ 318fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_SIGNALING; 319fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_SIGNALING; 3200c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 3210a708f8fSGustavo F. Padovan } 3220a708f8fSGustavo F. Padovan 323baa7e1faSGustavo F. Padovan sock_hold(sk); 324baa7e1faSGustavo F. Padovan 325baa7e1faSGustavo F. Padovan list_add(&chan->list, &conn->chan_l); 3260a708f8fSGustavo F. Padovan } 3270a708f8fSGustavo F. Padovan 3280a708f8fSGustavo F. Padovan /* Delete channel. 3290a708f8fSGustavo F. Padovan * Must be called on the locked socket. */ 3304519de9aSGustavo F. Padovan static void l2cap_chan_del(struct l2cap_chan *chan, int err) 3310a708f8fSGustavo F. Padovan { 33248454079SGustavo F. Padovan struct sock *sk = chan->sk; 3338c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 3340a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 3350a708f8fSGustavo F. Padovan 336ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 3370a708f8fSGustavo F. Padovan 33849208c9cSGustavo F. Padovan BT_DBG("chan %p, conn %p, err %d", chan, conn, err); 3390a708f8fSGustavo F. Padovan 3400a708f8fSGustavo F. Padovan if (conn) { 341baa7e1faSGustavo F. Padovan /* Delete from channel list */ 342baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 343baa7e1faSGustavo F. Padovan list_del(&chan->list); 344baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 345baa7e1faSGustavo F. Padovan __sock_put(sk); 346baa7e1faSGustavo F. Padovan 3478c1d787bSGustavo F. Padovan chan->conn = NULL; 3480a708f8fSGustavo F. Padovan hci_conn_put(conn->hcon); 3490a708f8fSGustavo F. Padovan } 3500a708f8fSGustavo F. Padovan 3510a708f8fSGustavo F. Padovan sk->sk_state = BT_CLOSED; 3520a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 3530a708f8fSGustavo F. Padovan 3540a708f8fSGustavo F. Padovan if (err) 3550a708f8fSGustavo F. Padovan sk->sk_err = err; 3560a708f8fSGustavo F. Padovan 3570a708f8fSGustavo F. Padovan if (parent) { 3580a708f8fSGustavo F. Padovan bt_accept_unlink(sk); 3590a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 3600a708f8fSGustavo F. Padovan } else 3610a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 3620a708f8fSGustavo F. Padovan 363b4450035SGustavo F. Padovan if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE && 364b4450035SGustavo F. Padovan chan->conf_state & L2CAP_CONF_INPUT_DONE)) 3656ff5abbfSGustavo F. Padovan return; 3662ead70b8SGustavo F. Padovan 36758d35f87SGustavo F. Padovan skb_queue_purge(&chan->tx_q); 3680a708f8fSGustavo F. Padovan 3690c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 3700a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 3710a708f8fSGustavo F. Padovan 372e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 373e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 374e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 3750a708f8fSGustavo F. Padovan 376f1c6775bSGustavo F. Padovan skb_queue_purge(&chan->srej_q); 377f1c6775bSGustavo F. Padovan skb_queue_purge(&chan->busy_q); 3780a708f8fSGustavo F. Padovan 37939d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 3800a708f8fSGustavo F. Padovan list_del(&l->list); 3810a708f8fSGustavo F. Padovan kfree(l); 3820a708f8fSGustavo F. Padovan } 3830a708f8fSGustavo F. Padovan } 3840a708f8fSGustavo F. Padovan } 3850a708f8fSGustavo F. Padovan 3864519de9aSGustavo F. Padovan static void l2cap_chan_cleanup_listen(struct sock *parent) 3874519de9aSGustavo F. Padovan { 3884519de9aSGustavo F. Padovan struct sock *sk; 3894519de9aSGustavo F. Padovan 3904519de9aSGustavo F. Padovan BT_DBG("parent %p", parent); 3914519de9aSGustavo F. Padovan 3924519de9aSGustavo F. Padovan /* Close not yet accepted channels */ 3930f852724SGustavo F. Padovan while ((sk = bt_accept_dequeue(parent, NULL))) { 3940f852724SGustavo F. Padovan l2cap_chan_clear_timer(l2cap_pi(sk)->chan); 3950f852724SGustavo F. Padovan lock_sock(sk); 3960f852724SGustavo F. Padovan l2cap_chan_close(l2cap_pi(sk)->chan, ECONNRESET); 3970f852724SGustavo F. Padovan release_sock(sk); 3980f852724SGustavo F. Padovan l2cap_sock_kill(sk); 3990f852724SGustavo F. Padovan } 4004519de9aSGustavo F. Padovan 4014519de9aSGustavo F. Padovan parent->sk_state = BT_CLOSED; 4024519de9aSGustavo F. Padovan sock_set_flag(parent, SOCK_ZAPPED); 4034519de9aSGustavo F. Padovan } 4044519de9aSGustavo F. Padovan 4050f852724SGustavo F. Padovan void l2cap_chan_close(struct l2cap_chan *chan, int reason) 4064519de9aSGustavo F. Padovan { 4074519de9aSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 4084519de9aSGustavo F. Padovan struct sock *sk = chan->sk; 4094519de9aSGustavo F. Padovan 4104519de9aSGustavo F. Padovan BT_DBG("chan %p state %d socket %p", chan, sk->sk_state, sk->sk_socket); 4114519de9aSGustavo F. Padovan 4124519de9aSGustavo F. Padovan switch (sk->sk_state) { 4134519de9aSGustavo F. Padovan case BT_LISTEN: 4144519de9aSGustavo F. Padovan l2cap_chan_cleanup_listen(sk); 4154519de9aSGustavo F. Padovan break; 4164519de9aSGustavo F. Padovan 4174519de9aSGustavo F. Padovan case BT_CONNECTED: 4184519de9aSGustavo F. Padovan case BT_CONFIG: 419715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 4204519de9aSGustavo F. Padovan conn->hcon->type == ACL_LINK) { 421500698d3SGustavo F. Padovan l2cap_chan_clear_timer(chan); 422ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, sk->sk_sndtimeo); 4234519de9aSGustavo F. Padovan l2cap_send_disconn_req(conn, chan, reason); 4244519de9aSGustavo F. Padovan } else 4254519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4264519de9aSGustavo F. Padovan break; 4274519de9aSGustavo F. Padovan 4284519de9aSGustavo F. Padovan case BT_CONNECT2: 429715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 4304519de9aSGustavo F. Padovan conn->hcon->type == ACL_LINK) { 4314519de9aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 4324519de9aSGustavo F. Padovan __u16 result; 4334519de9aSGustavo F. Padovan 4344519de9aSGustavo F. Padovan if (bt_sk(sk)->defer_setup) 4354519de9aSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 4364519de9aSGustavo F. Padovan else 4374519de9aSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 4384519de9aSGustavo F. Padovan 4394519de9aSGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4404519de9aSGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 4414519de9aSGustavo F. Padovan rsp.result = cpu_to_le16(result); 4424519de9aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 4434519de9aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4444519de9aSGustavo F. Padovan sizeof(rsp), &rsp); 4454519de9aSGustavo F. Padovan } 4464519de9aSGustavo F. Padovan 4474519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4484519de9aSGustavo F. Padovan break; 4494519de9aSGustavo F. Padovan 4504519de9aSGustavo F. Padovan case BT_CONNECT: 4514519de9aSGustavo F. Padovan case BT_DISCONN: 4524519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4534519de9aSGustavo F. Padovan break; 4544519de9aSGustavo F. Padovan 4554519de9aSGustavo F. Padovan default: 4564519de9aSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 4574519de9aSGustavo F. Padovan break; 4584519de9aSGustavo F. Padovan } 4594519de9aSGustavo F. Padovan } 4604519de9aSGustavo F. Padovan 4614343478fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) 4620a708f8fSGustavo F. Padovan { 463715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_RAW) { 4644343478fSGustavo F. Padovan switch (chan->sec_level) { 4650a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 4660a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING_MITM; 4670a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 4680a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING; 4690a708f8fSGustavo F. Padovan default: 4700a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 4710a708f8fSGustavo F. Padovan } 472fe4128e0SGustavo F. Padovan } else if (chan->psm == cpu_to_le16(0x0001)) { 4734343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_LOW) 4744343478fSGustavo F. Padovan chan->sec_level = BT_SECURITY_SDP; 4750a708f8fSGustavo F. Padovan 4764343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_HIGH) 4770a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING_MITM; 4780a708f8fSGustavo F. Padovan else 4790a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 4800a708f8fSGustavo F. Padovan } else { 4814343478fSGustavo F. Padovan switch (chan->sec_level) { 4820a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 4830a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING_MITM; 4840a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 4850a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING; 4860a708f8fSGustavo F. Padovan default: 4870a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 4880a708f8fSGustavo F. Padovan } 4890a708f8fSGustavo F. Padovan } 4900a708f8fSGustavo F. Padovan } 4910a708f8fSGustavo F. Padovan 4920a708f8fSGustavo F. Padovan /* Service level security */ 4934343478fSGustavo F. Padovan static inline int l2cap_check_security(struct l2cap_chan *chan) 4940a708f8fSGustavo F. Padovan { 4958c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 4960a708f8fSGustavo F. Padovan __u8 auth_type; 4970a708f8fSGustavo F. Padovan 4984343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 4990a708f8fSGustavo F. Padovan 5004343478fSGustavo F. Padovan return hci_conn_security(conn->hcon, chan->sec_level, auth_type); 5010a708f8fSGustavo F. Padovan } 5020a708f8fSGustavo F. Padovan 503b5ad8b7fSJohannes Berg static u8 l2cap_get_ident(struct l2cap_conn *conn) 5040a708f8fSGustavo F. Padovan { 5050a708f8fSGustavo F. Padovan u8 id; 5060a708f8fSGustavo F. Padovan 5070a708f8fSGustavo F. Padovan /* Get next available identificator. 5080a708f8fSGustavo F. Padovan * 1 - 128 are used by kernel. 5090a708f8fSGustavo F. Padovan * 129 - 199 are reserved. 5100a708f8fSGustavo F. Padovan * 200 - 254 are used by utilities like l2ping, etc. 5110a708f8fSGustavo F. Padovan */ 5120a708f8fSGustavo F. Padovan 5130a708f8fSGustavo F. Padovan spin_lock_bh(&conn->lock); 5140a708f8fSGustavo F. Padovan 5150a708f8fSGustavo F. Padovan if (++conn->tx_ident > 128) 5160a708f8fSGustavo F. Padovan conn->tx_ident = 1; 5170a708f8fSGustavo F. Padovan 5180a708f8fSGustavo F. Padovan id = conn->tx_ident; 5190a708f8fSGustavo F. Padovan 5200a708f8fSGustavo F. Padovan spin_unlock_bh(&conn->lock); 5210a708f8fSGustavo F. Padovan 5220a708f8fSGustavo F. Padovan return id; 5230a708f8fSGustavo F. Padovan } 5240a708f8fSGustavo F. Padovan 5254519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data) 5260a708f8fSGustavo F. Padovan { 5270a708f8fSGustavo F. Padovan struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); 5280a708f8fSGustavo F. Padovan u8 flags; 5290a708f8fSGustavo F. Padovan 5300a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x", code); 5310a708f8fSGustavo F. Padovan 5320a708f8fSGustavo F. Padovan if (!skb) 5330a708f8fSGustavo F. Padovan return; 5340a708f8fSGustavo F. Padovan 5350a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 5360a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 5370a708f8fSGustavo F. Padovan else 5380a708f8fSGustavo F. Padovan flags = ACL_START; 5390a708f8fSGustavo F. Padovan 54014b12d0bSJaikumar Ganesh bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; 54114b12d0bSJaikumar Ganesh 5420a708f8fSGustavo F. Padovan hci_send_acl(conn->hcon, skb, flags); 5430a708f8fSGustavo F. Padovan } 5440a708f8fSGustavo F. Padovan 545525cd185SGustavo F. Padovan static inline void l2cap_send_sframe(struct l2cap_chan *chan, u16 control) 5460a708f8fSGustavo F. Padovan { 5470a708f8fSGustavo F. Padovan struct sk_buff *skb; 5480a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 549525cd185SGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(chan->sk); 5508c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 5510a708f8fSGustavo F. Padovan struct sock *sk = (struct sock *)pi; 5520a708f8fSGustavo F. Padovan int count, hlen = L2CAP_HDR_SIZE + 2; 5530a708f8fSGustavo F. Padovan u8 flags; 5540a708f8fSGustavo F. Padovan 5550a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 5560a708f8fSGustavo F. Padovan return; 5570a708f8fSGustavo F. Padovan 55847d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 5590a708f8fSGustavo F. Padovan hlen += 2; 5600a708f8fSGustavo F. Padovan 56149208c9cSGustavo F. Padovan BT_DBG("chan %p, control 0x%2.2x", chan, control); 5620a708f8fSGustavo F. Padovan 5630a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, hlen); 5640a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FRAME_TYPE; 5650a708f8fSGustavo F. Padovan 566525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 5670a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 568525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 5690a708f8fSGustavo F. Padovan } 5700a708f8fSGustavo F. Padovan 571525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_PBIT) { 5720a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_POLL; 573525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_PBIT; 5740a708f8fSGustavo F. Padovan } 5750a708f8fSGustavo F. Padovan 5760a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 5770a708f8fSGustavo F. Padovan if (!skb) 5780a708f8fSGustavo F. Padovan return; 5790a708f8fSGustavo F. Padovan 5800a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 5810a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); 582fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 5830a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb_put(skb, 2)); 5840a708f8fSGustavo F. Padovan 58547d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 5860a708f8fSGustavo F. Padovan u16 fcs = crc16(0, (u8 *)lh, count - 2); 5870a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb_put(skb, 2)); 5880a708f8fSGustavo F. Padovan } 5890a708f8fSGustavo F. Padovan 5900a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 5910a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 5920a708f8fSGustavo F. Padovan else 5930a708f8fSGustavo F. Padovan flags = ACL_START; 5940a708f8fSGustavo F. Padovan 59514b12d0bSJaikumar Ganesh bt_cb(skb)->force_active = chan->force_active; 59614b12d0bSJaikumar Ganesh 5978c1d787bSGustavo F. Padovan hci_send_acl(chan->conn->hcon, skb, flags); 5980a708f8fSGustavo F. Padovan } 5990a708f8fSGustavo F. Padovan 600525cd185SGustavo F. Padovan static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u16 control) 6010a708f8fSGustavo F. Padovan { 602525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 6030a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 604525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 6050a708f8fSGustavo F. Padovan } else 6060a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 6070a708f8fSGustavo F. Padovan 60842e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 6090a708f8fSGustavo F. Padovan 610525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 6110a708f8fSGustavo F. Padovan } 6120a708f8fSGustavo F. Padovan 613b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) 6140a708f8fSGustavo F. Padovan { 615b4450035SGustavo F. Padovan return !(chan->conf_state & L2CAP_CONF_CONNECT_PEND); 6160a708f8fSGustavo F. Padovan } 6170a708f8fSGustavo F. Padovan 618fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan) 6190a708f8fSGustavo F. Padovan { 6208c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 6210a708f8fSGustavo F. Padovan 6220a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { 6230a708f8fSGustavo F. Padovan if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) 6240a708f8fSGustavo F. Padovan return; 6250a708f8fSGustavo F. Padovan 6264343478fSGustavo F. Padovan if (l2cap_check_security(chan) && 6274343478fSGustavo F. Padovan __l2cap_no_conn_pending(chan)) { 6280a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 629fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 630fe4128e0SGustavo F. Padovan req.psm = chan->psm; 6310a708f8fSGustavo F. Padovan 632fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 633b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 6340a708f8fSGustavo F. Padovan 635fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 636fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 6370a708f8fSGustavo F. Padovan } 6380a708f8fSGustavo F. Padovan } else { 6390a708f8fSGustavo F. Padovan struct l2cap_info_req req; 6400a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 6410a708f8fSGustavo F. Padovan 6420a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 6430a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 6440a708f8fSGustavo F. Padovan 6450a708f8fSGustavo F. Padovan mod_timer(&conn->info_timer, jiffies + 6460a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 6470a708f8fSGustavo F. Padovan 6480a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 6490a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 6500a708f8fSGustavo F. Padovan } 6510a708f8fSGustavo F. Padovan } 6520a708f8fSGustavo F. Padovan 6530a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) 6540a708f8fSGustavo F. Padovan { 6550a708f8fSGustavo F. Padovan u32 local_feat_mask = l2cap_feat_mask; 6560a708f8fSGustavo F. Padovan if (!disable_ertm) 6570a708f8fSGustavo F. Padovan local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; 6580a708f8fSGustavo F. Padovan 6590a708f8fSGustavo F. Padovan switch (mode) { 6600a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 6610a708f8fSGustavo F. Padovan return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; 6620a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 6630a708f8fSGustavo F. Padovan return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; 6640a708f8fSGustavo F. Padovan default: 6650a708f8fSGustavo F. Padovan return 0x00; 6660a708f8fSGustavo F. Padovan } 6670a708f8fSGustavo F. Padovan } 6680a708f8fSGustavo F. Padovan 6694519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err) 6700a708f8fSGustavo F. Padovan { 671e92c8e70SGustavo F. Padovan struct sock *sk; 6720a708f8fSGustavo F. Padovan struct l2cap_disconn_req req; 6730a708f8fSGustavo F. Padovan 6740a708f8fSGustavo F. Padovan if (!conn) 6750a708f8fSGustavo F. Padovan return; 6760a708f8fSGustavo F. Padovan 677e92c8e70SGustavo F. Padovan sk = chan->sk; 678e92c8e70SGustavo F. Padovan 6790c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 680e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 681e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 682e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 6830a708f8fSGustavo F. Padovan } 6840a708f8fSGustavo F. Padovan 685fe4128e0SGustavo F. Padovan req.dcid = cpu_to_le16(chan->dcid); 686fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 6870a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 6880a708f8fSGustavo F. Padovan L2CAP_DISCONN_REQ, sizeof(req), &req); 6890a708f8fSGustavo F. Padovan 6900a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 6910a708f8fSGustavo F. Padovan sk->sk_err = err; 6920a708f8fSGustavo F. Padovan } 6930a708f8fSGustavo F. Padovan 6940a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */ 6950a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn) 6960a708f8fSGustavo F. Padovan { 697820ffdb3SGustavo F. Padovan struct l2cap_chan *chan, *tmp; 6980a708f8fSGustavo F. Padovan 6990a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 7000a708f8fSGustavo F. Padovan 701baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 7020a708f8fSGustavo F. Padovan 703820ffdb3SGustavo F. Padovan list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { 70448454079SGustavo F. Padovan struct sock *sk = chan->sk; 705baa7e1faSGustavo F. Padovan 7060a708f8fSGustavo F. Padovan bh_lock_sock(sk); 7070a708f8fSGustavo F. Padovan 708715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 7090a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7100a708f8fSGustavo F. Padovan continue; 7110a708f8fSGustavo F. Padovan } 7120a708f8fSGustavo F. Padovan 7130a708f8fSGustavo F. Padovan if (sk->sk_state == BT_CONNECT) { 7140a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 7150a708f8fSGustavo F. Padovan 7164343478fSGustavo F. Padovan if (!l2cap_check_security(chan) || 717b4450035SGustavo F. Padovan !__l2cap_no_conn_pending(chan)) { 7180a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7190a708f8fSGustavo F. Padovan continue; 7200a708f8fSGustavo F. Padovan } 7210a708f8fSGustavo F. Padovan 7220c1bc5c6SGustavo F. Padovan if (!l2cap_mode_supported(chan->mode, 7230a708f8fSGustavo F. Padovan conn->feat_mask) 724b4450035SGustavo F. Padovan && chan->conf_state & 7250a708f8fSGustavo F. Padovan L2CAP_CONF_STATE2_DEVICE) { 7260f852724SGustavo F. Padovan /* l2cap_chan_close() calls list_del(chan) 727820ffdb3SGustavo F. Padovan * so release the lock */ 728820ffdb3SGustavo F. Padovan read_unlock_bh(&conn->chan_lock); 7290f852724SGustavo F. Padovan l2cap_chan_close(chan, ECONNRESET); 730820ffdb3SGustavo F. Padovan read_lock_bh(&conn->chan_lock); 7310a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7320a708f8fSGustavo F. Padovan continue; 7330a708f8fSGustavo F. Padovan } 7340a708f8fSGustavo F. Padovan 735fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 736fe4128e0SGustavo F. Padovan req.psm = chan->psm; 7370a708f8fSGustavo F. Padovan 738fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 739b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 7400a708f8fSGustavo F. Padovan 741fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 742fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 7430a708f8fSGustavo F. Padovan 7440a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT2) { 7450a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 7460a708f8fSGustavo F. Padovan char buf[128]; 747fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 748fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 7490a708f8fSGustavo F. Padovan 7504343478fSGustavo F. Padovan if (l2cap_check_security(chan)) { 7510a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 7520a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 7530a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 7540a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); 7550a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 7560a708f8fSGustavo F. Padovan 7570a708f8fSGustavo F. Padovan } else { 7580a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 7590a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 7600a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 7610a708f8fSGustavo F. Padovan } 7620a708f8fSGustavo F. Padovan } else { 7630a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 7640a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); 7650a708f8fSGustavo F. Padovan } 7660a708f8fSGustavo F. Padovan 767fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 768fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 7690a708f8fSGustavo F. Padovan 770b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_REQ_SENT || 7710a708f8fSGustavo F. Padovan rsp.result != L2CAP_CR_SUCCESS) { 7720a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7730a708f8fSGustavo F. Padovan continue; 7740a708f8fSGustavo F. Padovan } 7750a708f8fSGustavo F. Padovan 776b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 7770a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 77873ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 77973ffa904SGustavo F. Padovan chan->num_conf_req++; 7800a708f8fSGustavo F. Padovan } 7810a708f8fSGustavo F. Padovan 7820a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7830a708f8fSGustavo F. Padovan } 7840a708f8fSGustavo F. Padovan 785baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 7860a708f8fSGustavo F. Padovan } 7870a708f8fSGustavo F. Padovan 788b62f328bSVille Tervo /* Find socket with cid and source bdaddr. 789b62f328bSVille Tervo * Returns closest match, locked. 790b62f328bSVille Tervo */ 79123691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src) 792b62f328bSVille Tervo { 79323691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 794b62f328bSVille Tervo 79523691d75SGustavo F. Padovan read_lock(&chan_list_lock); 796b62f328bSVille Tervo 79723691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 79823691d75SGustavo F. Padovan struct sock *sk = c->sk; 799fe4128e0SGustavo F. Padovan 800b62f328bSVille Tervo if (state && sk->sk_state != state) 801b62f328bSVille Tervo continue; 802b62f328bSVille Tervo 80323691d75SGustavo F. Padovan if (c->scid == cid) { 804b62f328bSVille Tervo /* Exact match. */ 80523691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 80623691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 80723691d75SGustavo F. Padovan return c; 80823691d75SGustavo F. Padovan } 809b62f328bSVille Tervo 810b62f328bSVille Tervo /* Closest match */ 811b62f328bSVille Tervo if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 81223691d75SGustavo F. Padovan c1 = c; 813b62f328bSVille Tervo } 814b62f328bSVille Tervo } 815280f294fSGustavo F. Padovan 81623691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 817b62f328bSVille Tervo 81823691d75SGustavo F. Padovan return c1; 819b62f328bSVille Tervo } 820b62f328bSVille Tervo 821b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn) 822b62f328bSVille Tervo { 823c916fbe4SGustavo F. Padovan struct sock *parent, *sk; 82423691d75SGustavo F. Padovan struct l2cap_chan *chan, *pchan; 825b62f328bSVille Tervo 826b62f328bSVille Tervo BT_DBG(""); 827b62f328bSVille Tervo 828b62f328bSVille Tervo /* Check if we have socket listening on cid */ 82923691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, 830b62f328bSVille Tervo conn->src); 83123691d75SGustavo F. Padovan if (!pchan) 832b62f328bSVille Tervo return; 833b62f328bSVille Tervo 83423691d75SGustavo F. Padovan parent = pchan->sk; 83523691d75SGustavo F. Padovan 83662f3a2cfSGustavo F. Padovan bh_lock_sock(parent); 83762f3a2cfSGustavo F. Padovan 838b62f328bSVille Tervo /* Check for backlog size */ 839b62f328bSVille Tervo if (sk_acceptq_is_full(parent)) { 840b62f328bSVille Tervo BT_DBG("backlog full %d", parent->sk_ack_backlog); 841b62f328bSVille Tervo goto clean; 842b62f328bSVille Tervo } 843b62f328bSVille Tervo 844b62f328bSVille Tervo sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC); 845b62f328bSVille Tervo if (!sk) 846b62f328bSVille Tervo goto clean; 847b62f328bSVille Tervo 84823691d75SGustavo F. Padovan chan = l2cap_chan_create(sk); 84948454079SGustavo F. Padovan if (!chan) { 85048454079SGustavo F. Padovan l2cap_sock_kill(sk); 85148454079SGustavo F. Padovan goto clean; 85248454079SGustavo F. Padovan } 85348454079SGustavo F. Padovan 8545d41ce1dSGustavo F. Padovan l2cap_pi(sk)->chan = chan; 8555d41ce1dSGustavo F. Padovan 856baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 857b62f328bSVille Tervo 858b62f328bSVille Tervo hci_conn_hold(conn->hcon); 859b62f328bSVille Tervo 860b62f328bSVille Tervo l2cap_sock_init(sk, parent); 861baa7e1faSGustavo F. Padovan 862b62f328bSVille Tervo bacpy(&bt_sk(sk)->src, conn->src); 863b62f328bSVille Tervo bacpy(&bt_sk(sk)->dst, conn->dst); 864b62f328bSVille Tervo 865d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 866d1010240SGustavo F. Padovan 86748454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 86848454079SGustavo F. Padovan 869ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, sk->sk_sndtimeo); 870b62f328bSVille Tervo 871b62f328bSVille Tervo sk->sk_state = BT_CONNECTED; 872b62f328bSVille Tervo parent->sk_data_ready(parent, 0); 873b62f328bSVille Tervo 874baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 875b62f328bSVille Tervo 876b62f328bSVille Tervo clean: 877b62f328bSVille Tervo bh_unlock_sock(parent); 878b62f328bSVille Tervo } 879b62f328bSVille Tervo 8800a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn) 8810a708f8fSGustavo F. Padovan { 88248454079SGustavo F. Padovan struct l2cap_chan *chan; 8830a708f8fSGustavo F. Padovan 8840a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 8850a708f8fSGustavo F. Padovan 886b62f328bSVille Tervo if (!conn->hcon->out && conn->hcon->type == LE_LINK) 887b62f328bSVille Tervo l2cap_le_conn_ready(conn); 888b62f328bSVille Tervo 889baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 8900a708f8fSGustavo F. Padovan 891baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 89248454079SGustavo F. Padovan struct sock *sk = chan->sk; 893baa7e1faSGustavo F. Padovan 8940a708f8fSGustavo F. Padovan bh_lock_sock(sk); 8950a708f8fSGustavo F. Padovan 896acd7d370SVille Tervo if (conn->hcon->type == LE_LINK) { 897ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 898acd7d370SVille Tervo sk->sk_state = BT_CONNECTED; 899acd7d370SVille Tervo sk->sk_state_change(sk); 900acd7d370SVille Tervo } 901acd7d370SVille Tervo 902715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 903ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 9040a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 9050a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 9060a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT) 907fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 9080a708f8fSGustavo F. Padovan 9090a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9100a708f8fSGustavo F. Padovan } 9110a708f8fSGustavo F. Padovan 912baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 9130a708f8fSGustavo F. Padovan } 9140a708f8fSGustavo F. Padovan 9150a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */ 9160a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) 9170a708f8fSGustavo F. Padovan { 91848454079SGustavo F. Padovan struct l2cap_chan *chan; 9190a708f8fSGustavo F. Padovan 9200a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 9210a708f8fSGustavo F. Padovan 922baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 9230a708f8fSGustavo F. Padovan 924baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 92548454079SGustavo F. Padovan struct sock *sk = chan->sk; 926baa7e1faSGustavo F. Padovan 9274343478fSGustavo F. Padovan if (chan->force_reliable) 9280a708f8fSGustavo F. Padovan sk->sk_err = err; 9290a708f8fSGustavo F. Padovan } 9300a708f8fSGustavo F. Padovan 931baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 9320a708f8fSGustavo F. Padovan } 9330a708f8fSGustavo F. Padovan 9340a708f8fSGustavo F. Padovan static void l2cap_info_timeout(unsigned long arg) 9350a708f8fSGustavo F. Padovan { 9360a708f8fSGustavo F. Padovan struct l2cap_conn *conn = (void *) arg; 9370a708f8fSGustavo F. Padovan 9380a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 9390a708f8fSGustavo F. Padovan conn->info_ident = 0; 9400a708f8fSGustavo F. Padovan 9410a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 9420a708f8fSGustavo F. Padovan } 9430a708f8fSGustavo F. Padovan 9440a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) 9450a708f8fSGustavo F. Padovan { 9460a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 9470a708f8fSGustavo F. Padovan 9480a708f8fSGustavo F. Padovan if (conn || status) 9490a708f8fSGustavo F. Padovan return conn; 9500a708f8fSGustavo F. Padovan 9510a708f8fSGustavo F. Padovan conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); 9520a708f8fSGustavo F. Padovan if (!conn) 9530a708f8fSGustavo F. Padovan return NULL; 9540a708f8fSGustavo F. Padovan 9550a708f8fSGustavo F. Padovan hcon->l2cap_data = conn; 9560a708f8fSGustavo F. Padovan conn->hcon = hcon; 9570a708f8fSGustavo F. Padovan 9580a708f8fSGustavo F. Padovan BT_DBG("hcon %p conn %p", hcon, conn); 9590a708f8fSGustavo F. Padovan 960acd7d370SVille Tervo if (hcon->hdev->le_mtu && hcon->type == LE_LINK) 961acd7d370SVille Tervo conn->mtu = hcon->hdev->le_mtu; 962acd7d370SVille Tervo else 9630a708f8fSGustavo F. Padovan conn->mtu = hcon->hdev->acl_mtu; 964acd7d370SVille Tervo 9650a708f8fSGustavo F. Padovan conn->src = &hcon->hdev->bdaddr; 9660a708f8fSGustavo F. Padovan conn->dst = &hcon->dst; 9670a708f8fSGustavo F. Padovan 9680a708f8fSGustavo F. Padovan conn->feat_mask = 0; 9690a708f8fSGustavo F. Padovan 9700a708f8fSGustavo F. Padovan spin_lock_init(&conn->lock); 971baa7e1faSGustavo F. Padovan rwlock_init(&conn->chan_lock); 972baa7e1faSGustavo F. Padovan 973baa7e1faSGustavo F. Padovan INIT_LIST_HEAD(&conn->chan_l); 9740a708f8fSGustavo F. Padovan 975b62f328bSVille Tervo if (hcon->type != LE_LINK) 9760a708f8fSGustavo F. Padovan setup_timer(&conn->info_timer, l2cap_info_timeout, 9770a708f8fSGustavo F. Padovan (unsigned long) conn); 9780a708f8fSGustavo F. Padovan 9790a708f8fSGustavo F. Padovan conn->disc_reason = 0x13; 9800a708f8fSGustavo F. Padovan 9810a708f8fSGustavo F. Padovan return conn; 9820a708f8fSGustavo F. Padovan } 9830a708f8fSGustavo F. Padovan 9840a708f8fSGustavo F. Padovan static void l2cap_conn_del(struct hci_conn *hcon, int err) 9850a708f8fSGustavo F. Padovan { 9860a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 987baa7e1faSGustavo F. Padovan struct l2cap_chan *chan, *l; 9880a708f8fSGustavo F. Padovan struct sock *sk; 9890a708f8fSGustavo F. Padovan 9900a708f8fSGustavo F. Padovan if (!conn) 9910a708f8fSGustavo F. Padovan return; 9920a708f8fSGustavo F. Padovan 9930a708f8fSGustavo F. Padovan BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); 9940a708f8fSGustavo F. Padovan 9950a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 9960a708f8fSGustavo F. Padovan 9970a708f8fSGustavo F. Padovan /* Kill channels */ 998baa7e1faSGustavo F. Padovan list_for_each_entry_safe(chan, l, &conn->chan_l, list) { 99948454079SGustavo F. Padovan sk = chan->sk; 10000a708f8fSGustavo F. Padovan bh_lock_sock(sk); 100148454079SGustavo F. Padovan l2cap_chan_del(chan, err); 10020a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 10030a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 10040a708f8fSGustavo F. Padovan } 10050a708f8fSGustavo F. Padovan 10060a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) 10070a708f8fSGustavo F. Padovan del_timer_sync(&conn->info_timer); 10080a708f8fSGustavo F. Padovan 10090a708f8fSGustavo F. Padovan hcon->l2cap_data = NULL; 10100a708f8fSGustavo F. Padovan kfree(conn); 10110a708f8fSGustavo F. Padovan } 10120a708f8fSGustavo F. Padovan 101348454079SGustavo F. Padovan static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 10140a708f8fSGustavo F. Padovan { 1015baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 101648454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 1017baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 10180a708f8fSGustavo F. Padovan } 10190a708f8fSGustavo F. Padovan 10200a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */ 10210a708f8fSGustavo F. Padovan 10220a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr. 10230a708f8fSGustavo F. Padovan * Returns closest match. 10240a708f8fSGustavo F. Padovan */ 102523691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src) 10260a708f8fSGustavo F. Padovan { 102723691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 10280a708f8fSGustavo F. Padovan 102923691d75SGustavo F. Padovan read_lock(&chan_list_lock); 10300a708f8fSGustavo F. Padovan 103123691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 103223691d75SGustavo F. Padovan struct sock *sk = c->sk; 1033fe4128e0SGustavo F. Padovan 10340a708f8fSGustavo F. Padovan if (state && sk->sk_state != state) 10350a708f8fSGustavo F. Padovan continue; 10360a708f8fSGustavo F. Padovan 103723691d75SGustavo F. Padovan if (c->psm == psm) { 10380a708f8fSGustavo F. Padovan /* Exact match. */ 103923691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 1040a7567b20SJohannes Berg read_unlock(&chan_list_lock); 104123691d75SGustavo F. Padovan return c; 104223691d75SGustavo F. Padovan } 10430a708f8fSGustavo F. Padovan 10440a708f8fSGustavo F. Padovan /* Closest match */ 10450a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 104623691d75SGustavo F. Padovan c1 = c; 10470a708f8fSGustavo F. Padovan } 10480a708f8fSGustavo F. Padovan } 10490a708f8fSGustavo F. Padovan 105023691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 10510a708f8fSGustavo F. Padovan 105223691d75SGustavo F. Padovan return c1; 10530a708f8fSGustavo F. Padovan } 10540a708f8fSGustavo F. Padovan 105577a74c7eSGustavo F. Padovan int l2cap_chan_connect(struct l2cap_chan *chan) 10560a708f8fSGustavo F. Padovan { 10575d41ce1dSGustavo F. Padovan struct sock *sk = chan->sk; 10580a708f8fSGustavo F. Padovan bdaddr_t *src = &bt_sk(sk)->src; 10590a708f8fSGustavo F. Padovan bdaddr_t *dst = &bt_sk(sk)->dst; 10600a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 10610a708f8fSGustavo F. Padovan struct hci_conn *hcon; 10620a708f8fSGustavo F. Padovan struct hci_dev *hdev; 10630a708f8fSGustavo F. Padovan __u8 auth_type; 10640a708f8fSGustavo F. Padovan int err; 10650a708f8fSGustavo F. Padovan 10660a708f8fSGustavo F. Padovan BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), 1067fe4128e0SGustavo F. Padovan chan->psm); 10680a708f8fSGustavo F. Padovan 10690a708f8fSGustavo F. Padovan hdev = hci_get_route(dst, src); 10700a708f8fSGustavo F. Padovan if (!hdev) 10710a708f8fSGustavo F. Padovan return -EHOSTUNREACH; 10720a708f8fSGustavo F. Padovan 10730a708f8fSGustavo F. Padovan hci_dev_lock_bh(hdev); 10740a708f8fSGustavo F. Padovan 10754343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 10760a708f8fSGustavo F. Padovan 1077fe4128e0SGustavo F. Padovan if (chan->dcid == L2CAP_CID_LE_DATA) 1078acd7d370SVille Tervo hcon = hci_connect(hdev, LE_LINK, dst, 10794343478fSGustavo F. Padovan chan->sec_level, auth_type); 1080acd7d370SVille Tervo else 10810a708f8fSGustavo F. Padovan hcon = hci_connect(hdev, ACL_LINK, dst, 10824343478fSGustavo F. Padovan chan->sec_level, auth_type); 1083acd7d370SVille Tervo 108430e76272SVille Tervo if (IS_ERR(hcon)) { 108530e76272SVille Tervo err = PTR_ERR(hcon); 10860a708f8fSGustavo F. Padovan goto done; 108730e76272SVille Tervo } 10880a708f8fSGustavo F. Padovan 10890a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 10900a708f8fSGustavo F. Padovan if (!conn) { 10910a708f8fSGustavo F. Padovan hci_conn_put(hcon); 109230e76272SVille Tervo err = -ENOMEM; 10930a708f8fSGustavo F. Padovan goto done; 10940a708f8fSGustavo F. Padovan } 10950a708f8fSGustavo F. Padovan 10960a708f8fSGustavo F. Padovan /* Update source addr of the socket */ 10970a708f8fSGustavo F. Padovan bacpy(src, conn->src); 10980a708f8fSGustavo F. Padovan 109948454079SGustavo F. Padovan l2cap_chan_add(conn, chan); 110048454079SGustavo F. Padovan 11010a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT; 1102ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, sk->sk_sndtimeo); 11030a708f8fSGustavo F. Padovan 11040a708f8fSGustavo F. Padovan if (hcon->state == BT_CONNECTED) { 1105715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 1106ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 11074343478fSGustavo F. Padovan if (l2cap_check_security(chan)) 11080a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 11090a708f8fSGustavo F. Padovan } else 1110fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 11110a708f8fSGustavo F. Padovan } 11120a708f8fSGustavo F. Padovan 111330e76272SVille Tervo err = 0; 111430e76272SVille Tervo 11150a708f8fSGustavo F. Padovan done: 11160a708f8fSGustavo F. Padovan hci_dev_unlock_bh(hdev); 11170a708f8fSGustavo F. Padovan hci_dev_put(hdev); 11180a708f8fSGustavo F. Padovan return err; 11190a708f8fSGustavo F. Padovan } 11200a708f8fSGustavo F. Padovan 1121dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk) 11220a708f8fSGustavo F. Padovan { 11238c1d787bSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 11240a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 11250a708f8fSGustavo F. Padovan int err = 0; 11260a708f8fSGustavo F. Padovan int timeo = HZ/5; 11270a708f8fSGustavo F. Padovan 11280a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 11298c1d787bSGustavo F. Padovan while ((chan->unacked_frames > 0 && chan->conn)) { 11300a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 11310a708f8fSGustavo F. Padovan 11320a708f8fSGustavo F. Padovan if (!timeo) 11330a708f8fSGustavo F. Padovan timeo = HZ/5; 11340a708f8fSGustavo F. Padovan 11350a708f8fSGustavo F. Padovan if (signal_pending(current)) { 11360a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 11370a708f8fSGustavo F. Padovan break; 11380a708f8fSGustavo F. Padovan } 11390a708f8fSGustavo F. Padovan 11400a708f8fSGustavo F. Padovan release_sock(sk); 11410a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 11420a708f8fSGustavo F. Padovan lock_sock(sk); 11430a708f8fSGustavo F. Padovan 11440a708f8fSGustavo F. Padovan err = sock_error(sk); 11450a708f8fSGustavo F. Padovan if (err) 11460a708f8fSGustavo F. Padovan break; 11470a708f8fSGustavo F. Padovan } 11480a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 11490a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 11500a708f8fSGustavo F. Padovan return err; 11510a708f8fSGustavo F. Padovan } 11520a708f8fSGustavo F. Padovan 11530a708f8fSGustavo F. Padovan static void l2cap_monitor_timeout(unsigned long arg) 11540a708f8fSGustavo F. Padovan { 1155525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 1156525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 11570a708f8fSGustavo F. Padovan 1158525cd185SGustavo F. Padovan BT_DBG("chan %p", chan); 11590a708f8fSGustavo F. Padovan 11600a708f8fSGustavo F. Padovan bh_lock_sock(sk); 11612c03a7a4SGustavo F. Padovan if (chan->retry_count >= chan->remote_max_tx) { 11628c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 11630a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 11640a708f8fSGustavo F. Padovan return; 11650a708f8fSGustavo F. Padovan } 11660a708f8fSGustavo F. Padovan 11676a026610SGustavo F. Padovan chan->retry_count++; 11680a708f8fSGustavo F. Padovan __mod_monitor_timer(); 11690a708f8fSGustavo F. Padovan 1170525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 11710a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 11720a708f8fSGustavo F. Padovan } 11730a708f8fSGustavo F. Padovan 11740a708f8fSGustavo F. Padovan static void l2cap_retrans_timeout(unsigned long arg) 11750a708f8fSGustavo F. Padovan { 1176525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 1177525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 11780a708f8fSGustavo F. Padovan 117949208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 11800a708f8fSGustavo F. Padovan 11810a708f8fSGustavo F. Padovan bh_lock_sock(sk); 11826a026610SGustavo F. Padovan chan->retry_count = 1; 11830a708f8fSGustavo F. Padovan __mod_monitor_timer(); 11840a708f8fSGustavo F. Padovan 1185525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_WAIT_F; 11860a708f8fSGustavo F. Padovan 1187525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 11880a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 11890a708f8fSGustavo F. Padovan } 11900a708f8fSGustavo F. Padovan 119142e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan) 11920a708f8fSGustavo F. Padovan { 11930a708f8fSGustavo F. Padovan struct sk_buff *skb; 11940a708f8fSGustavo F. Padovan 119558d35f87SGustavo F. Padovan while ((skb = skb_peek(&chan->tx_q)) && 11966a026610SGustavo F. Padovan chan->unacked_frames) { 119742e5c802SGustavo F. Padovan if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) 11980a708f8fSGustavo F. Padovan break; 11990a708f8fSGustavo F. Padovan 120058d35f87SGustavo F. Padovan skb = skb_dequeue(&chan->tx_q); 12010a708f8fSGustavo F. Padovan kfree_skb(skb); 12020a708f8fSGustavo F. Padovan 12036a026610SGustavo F. Padovan chan->unacked_frames--; 12040a708f8fSGustavo F. Padovan } 12050a708f8fSGustavo F. Padovan 12066a026610SGustavo F. Padovan if (!chan->unacked_frames) 1207e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 12080a708f8fSGustavo F. Padovan } 12090a708f8fSGustavo F. Padovan 12104343478fSGustavo F. Padovan void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) 12110a708f8fSGustavo F. Padovan { 12128c1d787bSGustavo F. Padovan struct hci_conn *hcon = chan->conn->hcon; 12130a708f8fSGustavo F. Padovan u16 flags; 12140a708f8fSGustavo F. Padovan 12154343478fSGustavo F. Padovan BT_DBG("chan %p, skb %p len %d", chan, skb, skb->len); 12160a708f8fSGustavo F. Padovan 12174343478fSGustavo F. Padovan if (!chan->flushable && lmp_no_flush_capable(hcon->hdev)) 12180a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 12190a708f8fSGustavo F. Padovan else 12200a708f8fSGustavo F. Padovan flags = ACL_START; 12210a708f8fSGustavo F. Padovan 122214b12d0bSJaikumar Ganesh bt_cb(skb)->force_active = chan->force_active; 12230a708f8fSGustavo F. Padovan hci_send_acl(hcon, skb, flags); 12240a708f8fSGustavo F. Padovan } 12250a708f8fSGustavo F. Padovan 122642e5c802SGustavo F. Padovan void l2cap_streaming_send(struct l2cap_chan *chan) 12270a708f8fSGustavo F. Padovan { 12280a708f8fSGustavo F. Padovan struct sk_buff *skb; 12290a708f8fSGustavo F. Padovan u16 control, fcs; 12300a708f8fSGustavo F. Padovan 123158d35f87SGustavo F. Padovan while ((skb = skb_dequeue(&chan->tx_q))) { 12320a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data + L2CAP_HDR_SIZE); 123342e5c802SGustavo F. Padovan control |= chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT; 12340a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb->data + L2CAP_HDR_SIZE); 12350a708f8fSGustavo F. Padovan 123647d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 12370a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)skb->data, skb->len - 2); 12380a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb->data + skb->len - 2); 12390a708f8fSGustavo F. Padovan } 12400a708f8fSGustavo F. Padovan 12414343478fSGustavo F. Padovan l2cap_do_send(chan, skb); 12420a708f8fSGustavo F. Padovan 124342e5c802SGustavo F. Padovan chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; 12440a708f8fSGustavo F. Padovan } 12450a708f8fSGustavo F. Padovan } 12460a708f8fSGustavo F. Padovan 1247525cd185SGustavo F. Padovan static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq) 12480a708f8fSGustavo F. Padovan { 12490a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 12500a708f8fSGustavo F. Padovan u16 control, fcs; 12510a708f8fSGustavo F. Padovan 125258d35f87SGustavo F. Padovan skb = skb_peek(&chan->tx_q); 12530a708f8fSGustavo F. Padovan if (!skb) 12540a708f8fSGustavo F. Padovan return; 12550a708f8fSGustavo F. Padovan 12560a708f8fSGustavo F. Padovan do { 12570a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq == tx_seq) 12580a708f8fSGustavo F. Padovan break; 12590a708f8fSGustavo F. Padovan 126058d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 12610a708f8fSGustavo F. Padovan return; 12620a708f8fSGustavo F. Padovan 126358d35f87SGustavo F. Padovan } while ((skb = skb_queue_next(&chan->tx_q, skb))); 12640a708f8fSGustavo F. Padovan 12652c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 12662c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 12678c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 12680a708f8fSGustavo F. Padovan return; 12690a708f8fSGustavo F. Padovan } 12700a708f8fSGustavo F. Padovan 12710a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 12720a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 12730a708f8fSGustavo F. Padovan control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); 1274a429b519SRuiyi Zhang control &= L2CAP_CTRL_SAR; 12750a708f8fSGustavo F. Padovan 1276525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 12770a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 1278525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 12790a708f8fSGustavo F. Padovan } 12800a708f8fSGustavo F. Padovan 128142e5c802SGustavo F. Padovan control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT) 12820a708f8fSGustavo F. Padovan | (tx_seq << L2CAP_CTRL_TXSEQ_SHIFT); 12830a708f8fSGustavo F. Padovan 12840a708f8fSGustavo F. Padovan put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); 12850a708f8fSGustavo F. Padovan 128647d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 12870a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)tx_skb->data, tx_skb->len - 2); 12880a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, tx_skb->data + tx_skb->len - 2); 12890a708f8fSGustavo F. Padovan } 12900a708f8fSGustavo F. Padovan 12914343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 12920a708f8fSGustavo F. Padovan } 12930a708f8fSGustavo F. Padovan 1294525cd185SGustavo F. Padovan int l2cap_ertm_send(struct l2cap_chan *chan) 12950a708f8fSGustavo F. Padovan { 12960a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 1297525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 12980a708f8fSGustavo F. Padovan u16 control, fcs; 12990a708f8fSGustavo F. Padovan int nsent = 0; 13000a708f8fSGustavo F. Padovan 13010a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 13020a708f8fSGustavo F. Padovan return -ENOTCONN; 13030a708f8fSGustavo F. Padovan 130458d35f87SGustavo F. Padovan while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) { 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 break; 13100a708f8fSGustavo F. Padovan } 13110a708f8fSGustavo F. Padovan 13120a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 13130a708f8fSGustavo F. Padovan 13140a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 13150a708f8fSGustavo F. Padovan 13160a708f8fSGustavo F. Padovan control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); 13170a708f8fSGustavo F. Padovan control &= L2CAP_CTRL_SAR; 13180a708f8fSGustavo F. Padovan 1319525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 13200a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 1321525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 13220a708f8fSGustavo F. Padovan } 132342e5c802SGustavo F. Padovan control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT) 132442e5c802SGustavo F. Padovan | (chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT); 13250a708f8fSGustavo F. Padovan put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); 13260a708f8fSGustavo F. Padovan 13270a708f8fSGustavo F. Padovan 132847d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 13290a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2); 13300a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb->data + tx_skb->len - 2); 13310a708f8fSGustavo F. Padovan } 13320a708f8fSGustavo F. Padovan 13334343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 13340a708f8fSGustavo F. Padovan 13350a708f8fSGustavo F. Padovan __mod_retrans_timer(); 13360a708f8fSGustavo F. Padovan 133742e5c802SGustavo F. Padovan bt_cb(skb)->tx_seq = chan->next_tx_seq; 133842e5c802SGustavo F. Padovan chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; 13390a708f8fSGustavo F. Padovan 134023e9fde2SSuraj Sumangala if (bt_cb(skb)->retries == 1) 13416a026610SGustavo F. Padovan chan->unacked_frames++; 134223e9fde2SSuraj Sumangala 13436a026610SGustavo F. Padovan chan->frames_sent++; 13440a708f8fSGustavo F. Padovan 134558d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 134658d35f87SGustavo F. Padovan chan->tx_send_head = NULL; 13470a708f8fSGustavo F. Padovan else 134858d35f87SGustavo F. Padovan chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); 13490a708f8fSGustavo F. Padovan 13500a708f8fSGustavo F. Padovan nsent++; 13510a708f8fSGustavo F. Padovan } 13520a708f8fSGustavo F. Padovan 13530a708f8fSGustavo F. Padovan return nsent; 13540a708f8fSGustavo F. Padovan } 13550a708f8fSGustavo F. Padovan 1356525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan) 13570a708f8fSGustavo F. Padovan { 13580a708f8fSGustavo F. Padovan int ret; 13590a708f8fSGustavo F. Padovan 136058d35f87SGustavo F. Padovan if (!skb_queue_empty(&chan->tx_q)) 136158d35f87SGustavo F. Padovan chan->tx_send_head = chan->tx_q.next; 13620a708f8fSGustavo F. Padovan 136342e5c802SGustavo F. Padovan chan->next_tx_seq = chan->expected_ack_seq; 1364525cd185SGustavo F. Padovan ret = l2cap_ertm_send(chan); 13650a708f8fSGustavo F. Padovan return ret; 13660a708f8fSGustavo F. Padovan } 13670a708f8fSGustavo F. Padovan 1368525cd185SGustavo F. Padovan static void l2cap_send_ack(struct l2cap_chan *chan) 13690a708f8fSGustavo F. Padovan { 13700a708f8fSGustavo F. Padovan u16 control = 0; 13710a708f8fSGustavo F. Padovan 137242e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 13730a708f8fSGustavo F. Padovan 1374525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 13750a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 1376525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 1377525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 13780a708f8fSGustavo F. Padovan return; 13790a708f8fSGustavo F. Padovan } 13800a708f8fSGustavo F. Padovan 1381525cd185SGustavo F. Padovan if (l2cap_ertm_send(chan) > 0) 13820a708f8fSGustavo F. Padovan return; 13830a708f8fSGustavo F. Padovan 13840a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 1385525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 13860a708f8fSGustavo F. Padovan } 13870a708f8fSGustavo F. Padovan 1388525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan) 13890a708f8fSGustavo F. Padovan { 13900a708f8fSGustavo F. Padovan struct srej_list *tail; 13910a708f8fSGustavo F. Padovan u16 control; 13920a708f8fSGustavo F. Padovan 13930a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 13940a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 13950a708f8fSGustavo F. Padovan 139639d5a3eeSGustavo F. Padovan tail = list_entry((&chan->srej_l)->prev, struct srej_list, list); 13970a708f8fSGustavo F. Padovan control |= tail->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 13980a708f8fSGustavo F. Padovan 1399525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 14000a708f8fSGustavo F. Padovan } 14010a708f8fSGustavo F. Padovan 14020a708f8fSGustavo F. Padovan static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb) 14030a708f8fSGustavo F. Padovan { 14048c1d787bSGustavo F. Padovan struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn; 14050a708f8fSGustavo F. Padovan struct sk_buff **frag; 14060a708f8fSGustavo F. Padovan int err, sent = 0; 14070a708f8fSGustavo F. Padovan 14080a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) 14090a708f8fSGustavo F. Padovan return -EFAULT; 14100a708f8fSGustavo F. Padovan 14110a708f8fSGustavo F. Padovan sent += count; 14120a708f8fSGustavo F. Padovan len -= count; 14130a708f8fSGustavo F. Padovan 14140a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 14150a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 14160a708f8fSGustavo F. Padovan while (len) { 14170a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 14180a708f8fSGustavo F. Padovan 14190a708f8fSGustavo F. Padovan *frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err); 14200a708f8fSGustavo F. Padovan if (!*frag) 14210a708f8fSGustavo F. Padovan return err; 14220a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) 14230a708f8fSGustavo F. Padovan return -EFAULT; 14240a708f8fSGustavo F. Padovan 14250a708f8fSGustavo F. Padovan sent += count; 14260a708f8fSGustavo F. Padovan len -= count; 14270a708f8fSGustavo F. Padovan 14280a708f8fSGustavo F. Padovan frag = &(*frag)->next; 14290a708f8fSGustavo F. Padovan } 14300a708f8fSGustavo F. Padovan 14310a708f8fSGustavo F. Padovan return sent; 14320a708f8fSGustavo F. Padovan } 14330a708f8fSGustavo F. Padovan 1434fe4128e0SGustavo F. Padovan struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 14350a708f8fSGustavo F. Padovan { 1436fe4128e0SGustavo F. Padovan struct sock *sk = chan->sk; 14378c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 14380a708f8fSGustavo F. Padovan struct sk_buff *skb; 14390a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE + 2; 14400a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 14410a708f8fSGustavo F. Padovan 14420a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 14430a708f8fSGustavo F. Padovan 14440a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 14450a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 14460a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 14470a708f8fSGustavo F. Padovan if (!skb) 14480a708f8fSGustavo F. Padovan return ERR_PTR(err); 14490a708f8fSGustavo F. Padovan 14500a708f8fSGustavo F. Padovan /* Create L2CAP header */ 14510a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1452fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 14530a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 1454fe4128e0SGustavo F. Padovan put_unaligned_le16(chan->psm, skb_put(skb, 2)); 14550a708f8fSGustavo F. Padovan 14560a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 14570a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 14580a708f8fSGustavo F. Padovan kfree_skb(skb); 14590a708f8fSGustavo F. Padovan return ERR_PTR(err); 14600a708f8fSGustavo F. Padovan } 14610a708f8fSGustavo F. Padovan return skb; 14620a708f8fSGustavo F. Padovan } 14630a708f8fSGustavo F. Padovan 1464fe4128e0SGustavo F. Padovan struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 14650a708f8fSGustavo F. Padovan { 1466fe4128e0SGustavo F. Padovan struct sock *sk = chan->sk; 14678c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 14680a708f8fSGustavo F. Padovan struct sk_buff *skb; 14690a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE; 14700a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 14710a708f8fSGustavo F. Padovan 14720a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 14730a708f8fSGustavo F. Padovan 14740a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 14750a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 14760a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 14770a708f8fSGustavo F. Padovan if (!skb) 14780a708f8fSGustavo F. Padovan return ERR_PTR(err); 14790a708f8fSGustavo F. Padovan 14800a708f8fSGustavo F. Padovan /* Create L2CAP header */ 14810a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1482fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 14830a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 14840a708f8fSGustavo F. Padovan 14850a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 14860a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 14870a708f8fSGustavo F. Padovan kfree_skb(skb); 14880a708f8fSGustavo F. Padovan return ERR_PTR(err); 14890a708f8fSGustavo F. Padovan } 14900a708f8fSGustavo F. Padovan return skb; 14910a708f8fSGustavo F. Padovan } 14920a708f8fSGustavo F. Padovan 149347d1ec61SGustavo F. Padovan struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len, u16 control, u16 sdulen) 14940a708f8fSGustavo F. Padovan { 149547d1ec61SGustavo F. Padovan struct sock *sk = chan->sk; 14968c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 14970a708f8fSGustavo F. Padovan struct sk_buff *skb; 14980a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE + 2; 14990a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 15000a708f8fSGustavo F. Padovan 15010a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 15020a708f8fSGustavo F. Padovan 15030a708f8fSGustavo F. Padovan if (!conn) 15040a708f8fSGustavo F. Padovan return ERR_PTR(-ENOTCONN); 15050a708f8fSGustavo F. Padovan 15060a708f8fSGustavo F. Padovan if (sdulen) 15070a708f8fSGustavo F. Padovan hlen += 2; 15080a708f8fSGustavo F. Padovan 150947d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 15100a708f8fSGustavo F. Padovan hlen += 2; 15110a708f8fSGustavo F. Padovan 15120a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 15130a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 15140a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 15150a708f8fSGustavo F. Padovan if (!skb) 15160a708f8fSGustavo F. Padovan return ERR_PTR(err); 15170a708f8fSGustavo F. Padovan 15180a708f8fSGustavo F. Padovan /* Create L2CAP header */ 15190a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1520fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 15210a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 15220a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb_put(skb, 2)); 15230a708f8fSGustavo F. Padovan if (sdulen) 15240a708f8fSGustavo F. Padovan put_unaligned_le16(sdulen, skb_put(skb, 2)); 15250a708f8fSGustavo F. Padovan 15260a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 15270a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 15280a708f8fSGustavo F. Padovan kfree_skb(skb); 15290a708f8fSGustavo F. Padovan return ERR_PTR(err); 15300a708f8fSGustavo F. Padovan } 15310a708f8fSGustavo F. Padovan 153247d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 15330a708f8fSGustavo F. Padovan put_unaligned_le16(0, skb_put(skb, 2)); 15340a708f8fSGustavo F. Padovan 15350a708f8fSGustavo F. Padovan bt_cb(skb)->retries = 0; 15360a708f8fSGustavo F. Padovan return skb; 15370a708f8fSGustavo F. Padovan } 15380a708f8fSGustavo F. Padovan 15392c03a7a4SGustavo F. Padovan int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 15400a708f8fSGustavo F. Padovan { 15410a708f8fSGustavo F. Padovan struct sk_buff *skb; 15420a708f8fSGustavo F. Padovan struct sk_buff_head sar_queue; 15430a708f8fSGustavo F. Padovan u16 control; 15440a708f8fSGustavo F. Padovan size_t size = 0; 15450a708f8fSGustavo F. Padovan 15460a708f8fSGustavo F. Padovan skb_queue_head_init(&sar_queue); 15470a708f8fSGustavo F. Padovan control = L2CAP_SDU_START; 154847d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len); 15490a708f8fSGustavo F. Padovan if (IS_ERR(skb)) 15500a708f8fSGustavo F. Padovan return PTR_ERR(skb); 15510a708f8fSGustavo F. Padovan 15520a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 15532c03a7a4SGustavo F. Padovan len -= chan->remote_mps; 15542c03a7a4SGustavo F. Padovan size += chan->remote_mps; 15550a708f8fSGustavo F. Padovan 15560a708f8fSGustavo F. Padovan while (len > 0) { 15570a708f8fSGustavo F. Padovan size_t buflen; 15580a708f8fSGustavo F. Padovan 15592c03a7a4SGustavo F. Padovan if (len > chan->remote_mps) { 15600a708f8fSGustavo F. Padovan control = L2CAP_SDU_CONTINUE; 15612c03a7a4SGustavo F. Padovan buflen = chan->remote_mps; 15620a708f8fSGustavo F. Padovan } else { 15630a708f8fSGustavo F. Padovan control = L2CAP_SDU_END; 15640a708f8fSGustavo F. Padovan buflen = len; 15650a708f8fSGustavo F. Padovan } 15660a708f8fSGustavo F. Padovan 156747d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0); 15680a708f8fSGustavo F. Padovan if (IS_ERR(skb)) { 15690a708f8fSGustavo F. Padovan skb_queue_purge(&sar_queue); 15700a708f8fSGustavo F. Padovan return PTR_ERR(skb); 15710a708f8fSGustavo F. Padovan } 15720a708f8fSGustavo F. Padovan 15730a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 15740a708f8fSGustavo F. Padovan len -= buflen; 15750a708f8fSGustavo F. Padovan size += buflen; 15760a708f8fSGustavo F. Padovan } 157758d35f87SGustavo F. Padovan skb_queue_splice_tail(&sar_queue, &chan->tx_q); 157858d35f87SGustavo F. Padovan if (chan->tx_send_head == NULL) 157958d35f87SGustavo F. Padovan chan->tx_send_head = sar_queue.next; 15800a708f8fSGustavo F. Padovan 15810a708f8fSGustavo F. Padovan return size; 15820a708f8fSGustavo F. Padovan } 15830a708f8fSGustavo F. Padovan 15849a91a04aSGustavo F. Padovan int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 15859a91a04aSGustavo F. Padovan { 15869a91a04aSGustavo F. Padovan struct sk_buff *skb; 15879a91a04aSGustavo F. Padovan u16 control; 15889a91a04aSGustavo F. Padovan int err; 15899a91a04aSGustavo F. Padovan 15909a91a04aSGustavo F. Padovan /* Connectionless channel */ 1591715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { 15929a91a04aSGustavo F. Padovan skb = l2cap_create_connless_pdu(chan, msg, len); 15939a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 15949a91a04aSGustavo F. Padovan return PTR_ERR(skb); 15959a91a04aSGustavo F. Padovan 15969a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 15979a91a04aSGustavo F. Padovan return len; 15989a91a04aSGustavo F. Padovan } 15999a91a04aSGustavo F. Padovan 16009a91a04aSGustavo F. Padovan switch (chan->mode) { 16019a91a04aSGustavo F. Padovan case L2CAP_MODE_BASIC: 16029a91a04aSGustavo F. Padovan /* Check outgoing MTU */ 16039a91a04aSGustavo F. Padovan if (len > chan->omtu) 16049a91a04aSGustavo F. Padovan return -EMSGSIZE; 16059a91a04aSGustavo F. Padovan 16069a91a04aSGustavo F. Padovan /* Create a basic PDU */ 16079a91a04aSGustavo F. Padovan skb = l2cap_create_basic_pdu(chan, msg, len); 16089a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 16099a91a04aSGustavo F. Padovan return PTR_ERR(skb); 16109a91a04aSGustavo F. Padovan 16119a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 16129a91a04aSGustavo F. Padovan err = len; 16139a91a04aSGustavo F. Padovan break; 16149a91a04aSGustavo F. Padovan 16159a91a04aSGustavo F. Padovan case L2CAP_MODE_ERTM: 16169a91a04aSGustavo F. Padovan case L2CAP_MODE_STREAMING: 16179a91a04aSGustavo F. Padovan /* Entire SDU fits into one PDU */ 16189a91a04aSGustavo F. Padovan if (len <= chan->remote_mps) { 16199a91a04aSGustavo F. Padovan control = L2CAP_SDU_UNSEGMENTED; 16209a91a04aSGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, len, control, 16219a91a04aSGustavo F. Padovan 0); 16229a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 16239a91a04aSGustavo F. Padovan return PTR_ERR(skb); 16249a91a04aSGustavo F. Padovan 16259a91a04aSGustavo F. Padovan __skb_queue_tail(&chan->tx_q, skb); 16269a91a04aSGustavo F. Padovan 16279a91a04aSGustavo F. Padovan if (chan->tx_send_head == NULL) 16289a91a04aSGustavo F. Padovan chan->tx_send_head = skb; 16299a91a04aSGustavo F. Padovan 16309a91a04aSGustavo F. Padovan } else { 16319a91a04aSGustavo F. Padovan /* Segment SDU into multiples PDUs */ 16329a91a04aSGustavo F. Padovan err = l2cap_sar_segment_sdu(chan, msg, len); 16339a91a04aSGustavo F. Padovan if (err < 0) 16349a91a04aSGustavo F. Padovan return err; 16359a91a04aSGustavo F. Padovan } 16369a91a04aSGustavo F. Padovan 16379a91a04aSGustavo F. Padovan if (chan->mode == L2CAP_MODE_STREAMING) { 16389a91a04aSGustavo F. Padovan l2cap_streaming_send(chan); 16399a91a04aSGustavo F. Padovan err = len; 16409a91a04aSGustavo F. Padovan break; 16419a91a04aSGustavo F. Padovan } 16429a91a04aSGustavo F. Padovan 16439a91a04aSGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && 16449a91a04aSGustavo F. Padovan (chan->conn_state & L2CAP_CONN_WAIT_F)) { 16459a91a04aSGustavo F. Padovan err = len; 16469a91a04aSGustavo F. Padovan break; 16479a91a04aSGustavo F. Padovan } 16489a91a04aSGustavo F. Padovan 16499a91a04aSGustavo F. Padovan err = l2cap_ertm_send(chan); 16509a91a04aSGustavo F. Padovan if (err >= 0) 16519a91a04aSGustavo F. Padovan err = len; 16529a91a04aSGustavo F. Padovan 16539a91a04aSGustavo F. Padovan break; 16549a91a04aSGustavo F. Padovan 16559a91a04aSGustavo F. Padovan default: 16569a91a04aSGustavo F. Padovan BT_DBG("bad state %1.1x", chan->mode); 16579a91a04aSGustavo F. Padovan err = -EBADFD; 16589a91a04aSGustavo F. Padovan } 16599a91a04aSGustavo F. Padovan 16609a91a04aSGustavo F. Padovan return err; 16619a91a04aSGustavo F. Padovan } 16629a91a04aSGustavo F. Padovan 16630a708f8fSGustavo F. Padovan static void l2cap_chan_ready(struct sock *sk) 16640a708f8fSGustavo F. Padovan { 16650a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 1666b4450035SGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 16670a708f8fSGustavo F. Padovan 16680a708f8fSGustavo F. Padovan BT_DBG("sk %p, parent %p", sk, parent); 16690a708f8fSGustavo F. Padovan 1670b4450035SGustavo F. Padovan chan->conf_state = 0; 1671ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 16720a708f8fSGustavo F. Padovan 16730a708f8fSGustavo F. Padovan if (!parent) { 16740a708f8fSGustavo F. Padovan /* Outgoing channel. 16750a708f8fSGustavo F. Padovan * Wake up socket sleeping on connect. 16760a708f8fSGustavo F. Padovan */ 16770a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 16780a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 16790a708f8fSGustavo F. Padovan } else { 16800a708f8fSGustavo F. Padovan /* Incoming channel. 16810a708f8fSGustavo F. Padovan * Wake up socket sleeping on accept. 16820a708f8fSGustavo F. Padovan */ 16830a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 16840a708f8fSGustavo F. Padovan } 16850a708f8fSGustavo F. Padovan } 16860a708f8fSGustavo F. Padovan 16870a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */ 16880a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) 16890a708f8fSGustavo F. Padovan { 16900a708f8fSGustavo F. Padovan struct sk_buff *nskb; 169148454079SGustavo F. Padovan struct l2cap_chan *chan; 16920a708f8fSGustavo F. Padovan 16930a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 16940a708f8fSGustavo F. Padovan 1695baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 1696baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 169748454079SGustavo F. Padovan struct sock *sk = chan->sk; 1698715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_RAW) 16990a708f8fSGustavo F. Padovan continue; 17000a708f8fSGustavo F. Padovan 17010a708f8fSGustavo F. Padovan /* Don't send frame to the socket it came from */ 17020a708f8fSGustavo F. Padovan if (skb->sk == sk) 17030a708f8fSGustavo F. Padovan continue; 17040a708f8fSGustavo F. Padovan nskb = skb_clone(skb, GFP_ATOMIC); 17050a708f8fSGustavo F. Padovan if (!nskb) 17060a708f8fSGustavo F. Padovan continue; 17070a708f8fSGustavo F. Padovan 17080a708f8fSGustavo F. Padovan if (sock_queue_rcv_skb(sk, nskb)) 17090a708f8fSGustavo F. Padovan kfree_skb(nskb); 17100a708f8fSGustavo F. Padovan } 1711baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 17120a708f8fSGustavo F. Padovan } 17130a708f8fSGustavo F. Padovan 17140a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */ 17150a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 17160a708f8fSGustavo F. Padovan u8 code, u8 ident, u16 dlen, void *data) 17170a708f8fSGustavo F. Padovan { 17180a708f8fSGustavo F. Padovan struct sk_buff *skb, **frag; 17190a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr *cmd; 17200a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 17210a708f8fSGustavo F. Padovan int len, count; 17220a708f8fSGustavo F. Padovan 17230a708f8fSGustavo F. Padovan BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", 17240a708f8fSGustavo F. Padovan conn, code, ident, dlen); 17250a708f8fSGustavo F. Padovan 17260a708f8fSGustavo F. Padovan len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; 17270a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 17280a708f8fSGustavo F. Padovan 17290a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 17300a708f8fSGustavo F. Padovan if (!skb) 17310a708f8fSGustavo F. Padovan return NULL; 17320a708f8fSGustavo F. Padovan 17330a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 17340a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); 17353300d9a9SClaudio Takahasi 17363300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 17373300d9a9SClaudio Takahasi lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); 17383300d9a9SClaudio Takahasi else 17390a708f8fSGustavo F. Padovan lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); 17400a708f8fSGustavo F. Padovan 17410a708f8fSGustavo F. Padovan cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); 17420a708f8fSGustavo F. Padovan cmd->code = code; 17430a708f8fSGustavo F. Padovan cmd->ident = ident; 17440a708f8fSGustavo F. Padovan cmd->len = cpu_to_le16(dlen); 17450a708f8fSGustavo F. Padovan 17460a708f8fSGustavo F. Padovan if (dlen) { 17470a708f8fSGustavo F. Padovan count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; 17480a708f8fSGustavo F. Padovan memcpy(skb_put(skb, count), data, count); 17490a708f8fSGustavo F. Padovan data += count; 17500a708f8fSGustavo F. Padovan } 17510a708f8fSGustavo F. Padovan 17520a708f8fSGustavo F. Padovan len -= skb->len; 17530a708f8fSGustavo F. Padovan 17540a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 17550a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 17560a708f8fSGustavo F. Padovan while (len) { 17570a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 17580a708f8fSGustavo F. Padovan 17590a708f8fSGustavo F. Padovan *frag = bt_skb_alloc(count, GFP_ATOMIC); 17600a708f8fSGustavo F. Padovan if (!*frag) 17610a708f8fSGustavo F. Padovan goto fail; 17620a708f8fSGustavo F. Padovan 17630a708f8fSGustavo F. Padovan memcpy(skb_put(*frag, count), data, count); 17640a708f8fSGustavo F. Padovan 17650a708f8fSGustavo F. Padovan len -= count; 17660a708f8fSGustavo F. Padovan data += count; 17670a708f8fSGustavo F. Padovan 17680a708f8fSGustavo F. Padovan frag = &(*frag)->next; 17690a708f8fSGustavo F. Padovan } 17700a708f8fSGustavo F. Padovan 17710a708f8fSGustavo F. Padovan return skb; 17720a708f8fSGustavo F. Padovan 17730a708f8fSGustavo F. Padovan fail: 17740a708f8fSGustavo F. Padovan kfree_skb(skb); 17750a708f8fSGustavo F. Padovan return NULL; 17760a708f8fSGustavo F. Padovan } 17770a708f8fSGustavo F. Padovan 17780a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) 17790a708f8fSGustavo F. Padovan { 17800a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 17810a708f8fSGustavo F. Padovan int len; 17820a708f8fSGustavo F. Padovan 17830a708f8fSGustavo F. Padovan len = L2CAP_CONF_OPT_SIZE + opt->len; 17840a708f8fSGustavo F. Padovan *ptr += len; 17850a708f8fSGustavo F. Padovan 17860a708f8fSGustavo F. Padovan *type = opt->type; 17870a708f8fSGustavo F. Padovan *olen = opt->len; 17880a708f8fSGustavo F. Padovan 17890a708f8fSGustavo F. Padovan switch (opt->len) { 17900a708f8fSGustavo F. Padovan case 1: 17910a708f8fSGustavo F. Padovan *val = *((u8 *) opt->val); 17920a708f8fSGustavo F. Padovan break; 17930a708f8fSGustavo F. Padovan 17940a708f8fSGustavo F. Padovan case 2: 17950a708f8fSGustavo F. Padovan *val = get_unaligned_le16(opt->val); 17960a708f8fSGustavo F. Padovan break; 17970a708f8fSGustavo F. Padovan 17980a708f8fSGustavo F. Padovan case 4: 17990a708f8fSGustavo F. Padovan *val = get_unaligned_le32(opt->val); 18000a708f8fSGustavo F. Padovan break; 18010a708f8fSGustavo F. Padovan 18020a708f8fSGustavo F. Padovan default: 18030a708f8fSGustavo F. Padovan *val = (unsigned long) opt->val; 18040a708f8fSGustavo F. Padovan break; 18050a708f8fSGustavo F. Padovan } 18060a708f8fSGustavo F. Padovan 18070a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); 18080a708f8fSGustavo F. Padovan return len; 18090a708f8fSGustavo F. Padovan } 18100a708f8fSGustavo F. Padovan 18110a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) 18120a708f8fSGustavo F. Padovan { 18130a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 18140a708f8fSGustavo F. Padovan 18150a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); 18160a708f8fSGustavo F. Padovan 18170a708f8fSGustavo F. Padovan opt->type = type; 18180a708f8fSGustavo F. Padovan opt->len = len; 18190a708f8fSGustavo F. Padovan 18200a708f8fSGustavo F. Padovan switch (len) { 18210a708f8fSGustavo F. Padovan case 1: 18220a708f8fSGustavo F. Padovan *((u8 *) opt->val) = val; 18230a708f8fSGustavo F. Padovan break; 18240a708f8fSGustavo F. Padovan 18250a708f8fSGustavo F. Padovan case 2: 18260a708f8fSGustavo F. Padovan put_unaligned_le16(val, opt->val); 18270a708f8fSGustavo F. Padovan break; 18280a708f8fSGustavo F. Padovan 18290a708f8fSGustavo F. Padovan case 4: 18300a708f8fSGustavo F. Padovan put_unaligned_le32(val, opt->val); 18310a708f8fSGustavo F. Padovan break; 18320a708f8fSGustavo F. Padovan 18330a708f8fSGustavo F. Padovan default: 18340a708f8fSGustavo F. Padovan memcpy(opt->val, (void *) val, len); 18350a708f8fSGustavo F. Padovan break; 18360a708f8fSGustavo F. Padovan } 18370a708f8fSGustavo F. Padovan 18380a708f8fSGustavo F. Padovan *ptr += L2CAP_CONF_OPT_SIZE + len; 18390a708f8fSGustavo F. Padovan } 18400a708f8fSGustavo F. Padovan 18410a708f8fSGustavo F. Padovan static void l2cap_ack_timeout(unsigned long arg) 18420a708f8fSGustavo F. Padovan { 1843525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 18440a708f8fSGustavo F. Padovan 1845525cd185SGustavo F. Padovan bh_lock_sock(chan->sk); 1846525cd185SGustavo F. Padovan l2cap_send_ack(chan); 1847525cd185SGustavo F. Padovan bh_unlock_sock(chan->sk); 18480a708f8fSGustavo F. Padovan } 18490a708f8fSGustavo F. Padovan 1850525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan) 18510a708f8fSGustavo F. Padovan { 1852525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 1853525cd185SGustavo F. Padovan 185442e5c802SGustavo F. Padovan chan->expected_ack_seq = 0; 18556a026610SGustavo F. Padovan chan->unacked_frames = 0; 185642e5c802SGustavo F. Padovan chan->buffer_seq = 0; 18576a026610SGustavo F. Padovan chan->num_acked = 0; 18586a026610SGustavo F. Padovan chan->frames_sent = 0; 18590a708f8fSGustavo F. Padovan 1860e92c8e70SGustavo F. Padovan setup_timer(&chan->retrans_timer, l2cap_retrans_timeout, 1861e92c8e70SGustavo F. Padovan (unsigned long) chan); 1862e92c8e70SGustavo F. Padovan setup_timer(&chan->monitor_timer, l2cap_monitor_timeout, 1863e92c8e70SGustavo F. Padovan (unsigned long) chan); 1864e92c8e70SGustavo F. Padovan setup_timer(&chan->ack_timer, l2cap_ack_timeout, (unsigned long) chan); 18650a708f8fSGustavo F. Padovan 1866f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->srej_q); 1867f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->busy_q); 18680a708f8fSGustavo F. Padovan 186939d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 187039d5a3eeSGustavo F. Padovan 1871311bb895SGustavo F. Padovan INIT_WORK(&chan->busy_work, l2cap_busy_work); 18720a708f8fSGustavo F. Padovan 18730a708f8fSGustavo F. Padovan sk->sk_backlog_rcv = l2cap_ertm_data_rcv; 18740a708f8fSGustavo F. Padovan } 18750a708f8fSGustavo F. Padovan 18760a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) 18770a708f8fSGustavo F. Padovan { 18780a708f8fSGustavo F. Padovan switch (mode) { 18790a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 18800a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 18810a708f8fSGustavo F. Padovan if (l2cap_mode_supported(mode, remote_feat_mask)) 18820a708f8fSGustavo F. Padovan return mode; 18830a708f8fSGustavo F. Padovan /* fall through */ 18840a708f8fSGustavo F. Padovan default: 18850a708f8fSGustavo F. Padovan return L2CAP_MODE_BASIC; 18860a708f8fSGustavo F. Padovan } 18870a708f8fSGustavo F. Padovan } 18880a708f8fSGustavo F. Padovan 1889710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) 18900a708f8fSGustavo F. Padovan { 18910a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 18920c1bc5c6SGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = chan->mode }; 18930a708f8fSGustavo F. Padovan void *ptr = req->data; 18940a708f8fSGustavo F. Padovan 189549208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 18960a708f8fSGustavo F. Padovan 189773ffa904SGustavo F. Padovan if (chan->num_conf_req || chan->num_conf_rsp) 18980a708f8fSGustavo F. Padovan goto done; 18990a708f8fSGustavo F. Padovan 19000c1bc5c6SGustavo F. Padovan switch (chan->mode) { 19010a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 19020a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 1903b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_STATE2_DEVICE) 19040a708f8fSGustavo F. Padovan break; 19050a708f8fSGustavo F. Padovan 19060a708f8fSGustavo F. Padovan /* fall through */ 19070a708f8fSGustavo F. Padovan default: 19088c1d787bSGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); 19090a708f8fSGustavo F. Padovan break; 19100a708f8fSGustavo F. Padovan } 19110a708f8fSGustavo F. Padovan 19120a708f8fSGustavo F. Padovan done: 19130c1bc5c6SGustavo F. Padovan if (chan->imtu != L2CAP_DEFAULT_MTU) 19140c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 19150a708f8fSGustavo F. Padovan 19160c1bc5c6SGustavo F. Padovan switch (chan->mode) { 19170a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 19188c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && 19198c1d787bSGustavo F. Padovan !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) 19200a708f8fSGustavo F. Padovan break; 19210a708f8fSGustavo F. Padovan 19220a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_BASIC; 19230a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 19240a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 19250a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 19260a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 19270a708f8fSGustavo F. Padovan rfc.max_pdu_size = 0; 19280a708f8fSGustavo F. Padovan 19290a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 19300a708f8fSGustavo F. Padovan (unsigned long) &rfc); 19310a708f8fSGustavo F. Padovan break; 19320a708f8fSGustavo F. Padovan 19330a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 19340a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_ERTM; 193547d1ec61SGustavo F. Padovan rfc.txwin_size = chan->tx_win; 193647d1ec61SGustavo F. Padovan rfc.max_transmit = chan->max_tx; 19370a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 19380a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 19390a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); 19408c1d787bSGustavo F. Padovan if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10) 19418c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 19420a708f8fSGustavo F. Padovan 19430a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 19440a708f8fSGustavo F. Padovan (unsigned long) &rfc); 19450a708f8fSGustavo F. Padovan 19468c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 19470a708f8fSGustavo F. Padovan break; 19480a708f8fSGustavo F. Padovan 194947d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 1950b4450035SGustavo F. Padovan chan->conf_state & L2CAP_CONF_NO_FCS_RECV) { 195147d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 195247d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 19530a708f8fSGustavo F. Padovan } 19540a708f8fSGustavo F. Padovan break; 19550a708f8fSGustavo F. Padovan 19560a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 19570a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_STREAMING; 19580a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 19590a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 19600a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 19610a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 19620a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); 19638c1d787bSGustavo F. Padovan if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10) 19648c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 19650a708f8fSGustavo F. Padovan 19660a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 19670a708f8fSGustavo F. Padovan (unsigned long) &rfc); 19680a708f8fSGustavo F. Padovan 19698c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 19700a708f8fSGustavo F. Padovan break; 19710a708f8fSGustavo F. Padovan 197247d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 1973b4450035SGustavo F. Padovan chan->conf_state & L2CAP_CONF_NO_FCS_RECV) { 197447d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 197547d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 19760a708f8fSGustavo F. Padovan } 19770a708f8fSGustavo F. Padovan break; 19780a708f8fSGustavo F. Padovan } 19790a708f8fSGustavo F. Padovan 1980fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 19810a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0); 19820a708f8fSGustavo F. Padovan 19830a708f8fSGustavo F. Padovan return ptr - data; 19840a708f8fSGustavo F. Padovan } 19850a708f8fSGustavo F. Padovan 198673ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) 19870a708f8fSGustavo F. Padovan { 19880a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 19890a708f8fSGustavo F. Padovan void *ptr = rsp->data; 199073ffa904SGustavo F. Padovan void *req = chan->conf_req; 199173ffa904SGustavo F. Padovan int len = chan->conf_len; 19920a708f8fSGustavo F. Padovan int type, hint, olen; 19930a708f8fSGustavo F. Padovan unsigned long val; 19940a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 19950a708f8fSGustavo F. Padovan u16 mtu = L2CAP_DEFAULT_MTU; 19960a708f8fSGustavo F. Padovan u16 result = L2CAP_CONF_SUCCESS; 19970a708f8fSGustavo F. Padovan 199873ffa904SGustavo F. Padovan BT_DBG("chan %p", chan); 19990a708f8fSGustavo F. Padovan 20000a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 20010a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&req, &type, &olen, &val); 20020a708f8fSGustavo F. Padovan 20030a708f8fSGustavo F. Padovan hint = type & L2CAP_CONF_HINT; 20040a708f8fSGustavo F. Padovan type &= L2CAP_CONF_MASK; 20050a708f8fSGustavo F. Padovan 20060a708f8fSGustavo F. Padovan switch (type) { 20070a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 20080a708f8fSGustavo F. Padovan mtu = val; 20090a708f8fSGustavo F. Padovan break; 20100a708f8fSGustavo F. Padovan 20110a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 20120c1bc5c6SGustavo F. Padovan chan->flush_to = val; 20130a708f8fSGustavo F. Padovan break; 20140a708f8fSGustavo F. Padovan 20150a708f8fSGustavo F. Padovan case L2CAP_CONF_QOS: 20160a708f8fSGustavo F. Padovan break; 20170a708f8fSGustavo F. Padovan 20180a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 20190a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 20200a708f8fSGustavo F. Padovan memcpy(&rfc, (void *) val, olen); 20210a708f8fSGustavo F. Padovan break; 20220a708f8fSGustavo F. Padovan 20230a708f8fSGustavo F. Padovan case L2CAP_CONF_FCS: 20240a708f8fSGustavo F. Padovan if (val == L2CAP_FCS_NONE) 2025b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_NO_FCS_RECV; 20260a708f8fSGustavo F. Padovan 20270a708f8fSGustavo F. Padovan break; 20280a708f8fSGustavo F. Padovan 20290a708f8fSGustavo F. Padovan default: 20300a708f8fSGustavo F. Padovan if (hint) 20310a708f8fSGustavo F. Padovan break; 20320a708f8fSGustavo F. Padovan 20330a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNKNOWN; 20340a708f8fSGustavo F. Padovan *((u8 *) ptr++) = type; 20350a708f8fSGustavo F. Padovan break; 20360a708f8fSGustavo F. Padovan } 20370a708f8fSGustavo F. Padovan } 20380a708f8fSGustavo F. Padovan 203973ffa904SGustavo F. Padovan if (chan->num_conf_rsp || chan->num_conf_req > 1) 20400a708f8fSGustavo F. Padovan goto done; 20410a708f8fSGustavo F. Padovan 20420c1bc5c6SGustavo F. Padovan switch (chan->mode) { 20430a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 20440a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 2045b4450035SGustavo F. Padovan if (!(chan->conf_state & L2CAP_CONF_STATE2_DEVICE)) { 20460c1bc5c6SGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, 20478c1d787bSGustavo F. Padovan chan->conn->feat_mask); 20480a708f8fSGustavo F. Padovan break; 20490a708f8fSGustavo F. Padovan } 20500a708f8fSGustavo F. Padovan 20510c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) 20520a708f8fSGustavo F. Padovan return -ECONNREFUSED; 20530a708f8fSGustavo F. Padovan 20540a708f8fSGustavo F. Padovan break; 20550a708f8fSGustavo F. Padovan } 20560a708f8fSGustavo F. Padovan 20570a708f8fSGustavo F. Padovan done: 20580c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) { 20590a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 20600c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 20610a708f8fSGustavo F. Padovan 206273ffa904SGustavo F. Padovan if (chan->num_conf_rsp == 1) 20630a708f8fSGustavo F. Padovan return -ECONNREFUSED; 20640a708f8fSGustavo F. Padovan 20650a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 20660a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 20670a708f8fSGustavo F. Padovan } 20680a708f8fSGustavo F. Padovan 20690a708f8fSGustavo F. Padovan 20700a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) { 20710a708f8fSGustavo F. Padovan /* Configure output options and let the other side know 20720a708f8fSGustavo F. Padovan * which ones we don't like. */ 20730a708f8fSGustavo F. Padovan 20740a708f8fSGustavo F. Padovan if (mtu < L2CAP_DEFAULT_MIN_MTU) 20750a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 20760a708f8fSGustavo F. Padovan else { 20770c1bc5c6SGustavo F. Padovan chan->omtu = mtu; 2078b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MTU_DONE; 20790a708f8fSGustavo F. Padovan } 20800c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); 20810a708f8fSGustavo F. Padovan 20820a708f8fSGustavo F. Padovan switch (rfc.mode) { 20830a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 208447d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2085b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MODE_DONE; 20860a708f8fSGustavo F. Padovan break; 20870a708f8fSGustavo F. Padovan 20880a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 20892c03a7a4SGustavo F. Padovan chan->remote_tx_win = rfc.txwin_size; 20902c03a7a4SGustavo F. Padovan chan->remote_max_tx = rfc.max_transmit; 20910a708f8fSGustavo F. Padovan 20928c1d787bSGustavo F. Padovan if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10) 20938c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 20940a708f8fSGustavo F. Padovan 20952c03a7a4SGustavo F. Padovan chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); 20960a708f8fSGustavo F. Padovan 20970a708f8fSGustavo F. Padovan rfc.retrans_timeout = 20980a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO); 20990a708f8fSGustavo F. Padovan rfc.monitor_timeout = 21000a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO); 21010a708f8fSGustavo F. Padovan 2102b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MODE_DONE; 21030a708f8fSGustavo F. Padovan 21040a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 21050a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 21060a708f8fSGustavo F. Padovan 21070a708f8fSGustavo F. Padovan break; 21080a708f8fSGustavo F. Padovan 21090a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 21108c1d787bSGustavo F. Padovan if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10) 21118c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 21120a708f8fSGustavo F. Padovan 21132c03a7a4SGustavo F. Padovan chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); 21140a708f8fSGustavo F. Padovan 2115b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MODE_DONE; 21160a708f8fSGustavo F. Padovan 21170a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 21180a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 21190a708f8fSGustavo F. Padovan 21200a708f8fSGustavo F. Padovan break; 21210a708f8fSGustavo F. Padovan 21220a708f8fSGustavo F. Padovan default: 21230a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 21240a708f8fSGustavo F. Padovan 21250a708f8fSGustavo F. Padovan memset(&rfc, 0, sizeof(rfc)); 21260c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 21270a708f8fSGustavo F. Padovan } 21280a708f8fSGustavo F. Padovan 21290a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) 2130b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_OUTPUT_DONE; 21310a708f8fSGustavo F. Padovan } 2132fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 21330a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 21340a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(0x0000); 21350a708f8fSGustavo F. Padovan 21360a708f8fSGustavo F. Padovan return ptr - data; 21370a708f8fSGustavo F. Padovan } 21380a708f8fSGustavo F. Padovan 2139b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result) 21400a708f8fSGustavo F. Padovan { 21410a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 21420a708f8fSGustavo F. Padovan void *ptr = req->data; 21430a708f8fSGustavo F. Padovan int type, olen; 21440a708f8fSGustavo F. Padovan unsigned long val; 21450a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 21460a708f8fSGustavo F. Padovan 2147fe4128e0SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data); 21480a708f8fSGustavo F. Padovan 21490a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 21500a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 21510a708f8fSGustavo F. Padovan 21520a708f8fSGustavo F. Padovan switch (type) { 21530a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 21540a708f8fSGustavo F. Padovan if (val < L2CAP_DEFAULT_MIN_MTU) { 21550a708f8fSGustavo F. Padovan *result = L2CAP_CONF_UNACCEPT; 21560c1bc5c6SGustavo F. Padovan chan->imtu = L2CAP_DEFAULT_MIN_MTU; 21570a708f8fSGustavo F. Padovan } else 21580c1bc5c6SGustavo F. Padovan chan->imtu = val; 21590c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 21600a708f8fSGustavo F. Padovan break; 21610a708f8fSGustavo F. Padovan 21620a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 21630c1bc5c6SGustavo F. Padovan chan->flush_to = val; 21640a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 21650c1bc5c6SGustavo F. Padovan 2, chan->flush_to); 21660a708f8fSGustavo F. Padovan break; 21670a708f8fSGustavo F. Padovan 21680a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 21690a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 21700a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 21710a708f8fSGustavo F. Padovan 2172b4450035SGustavo F. Padovan if ((chan->conf_state & L2CAP_CONF_STATE2_DEVICE) && 21730c1bc5c6SGustavo F. Padovan rfc.mode != chan->mode) 21740a708f8fSGustavo F. Padovan return -ECONNREFUSED; 21750a708f8fSGustavo F. Padovan 217647d1ec61SGustavo F. Padovan chan->fcs = 0; 21770a708f8fSGustavo F. Padovan 21780a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 21790a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 21800a708f8fSGustavo F. Padovan break; 21810a708f8fSGustavo F. Padovan } 21820a708f8fSGustavo F. Padovan } 21830a708f8fSGustavo F. Padovan 21840c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) 21850a708f8fSGustavo F. Padovan return -ECONNREFUSED; 21860a708f8fSGustavo F. Padovan 21870c1bc5c6SGustavo F. Padovan chan->mode = rfc.mode; 21880a708f8fSGustavo F. Padovan 21890a708f8fSGustavo F. Padovan if (*result == L2CAP_CONF_SUCCESS) { 21900a708f8fSGustavo F. Padovan switch (rfc.mode) { 21910a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 219247d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 219347d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 219447d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 21950a708f8fSGustavo F. Padovan break; 21960a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 219747d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 21980a708f8fSGustavo F. Padovan } 21990a708f8fSGustavo F. Padovan } 22000a708f8fSGustavo F. Padovan 2201fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 22020a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0x0000); 22030a708f8fSGustavo F. Padovan 22040a708f8fSGustavo F. Padovan return ptr - data; 22050a708f8fSGustavo F. Padovan } 22060a708f8fSGustavo F. Padovan 2207fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags) 22080a708f8fSGustavo F. Padovan { 22090a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 22100a708f8fSGustavo F. Padovan void *ptr = rsp->data; 22110a708f8fSGustavo F. Padovan 2212fe4128e0SGustavo F. Padovan BT_DBG("chan %p", chan); 22130a708f8fSGustavo F. Padovan 2214fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 22150a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 22160a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(flags); 22170a708f8fSGustavo F. Padovan 22180a708f8fSGustavo F. Padovan return ptr - data; 22190a708f8fSGustavo F. Padovan } 22200a708f8fSGustavo F. Padovan 22218c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) 2222710f9b0aSGustavo F. Padovan { 2223710f9b0aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 22248c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 2225710f9b0aSGustavo F. Padovan u8 buf[128]; 2226710f9b0aSGustavo F. Padovan 2227fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 2228fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 2229710f9b0aSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 2230710f9b0aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 2231710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 2232710f9b0aSGustavo F. Padovan L2CAP_CONN_RSP, sizeof(rsp), &rsp); 2233710f9b0aSGustavo F. Padovan 2234b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_REQ_SENT) 2235710f9b0aSGustavo F. Padovan return; 2236710f9b0aSGustavo F. Padovan 2237b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 2238710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 2239710f9b0aSGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 2240710f9b0aSGustavo F. Padovan chan->num_conf_req++; 2241710f9b0aSGustavo F. Padovan } 2242710f9b0aSGustavo F. Padovan 224347d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len) 22440a708f8fSGustavo F. Padovan { 22450a708f8fSGustavo F. Padovan int type, olen; 22460a708f8fSGustavo F. Padovan unsigned long val; 22470a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 22480a708f8fSGustavo F. Padovan 224947d1ec61SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); 22500a708f8fSGustavo F. Padovan 22510c1bc5c6SGustavo F. Padovan if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) 22520a708f8fSGustavo F. Padovan return; 22530a708f8fSGustavo F. Padovan 22540a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 22550a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 22560a708f8fSGustavo F. Padovan 22570a708f8fSGustavo F. Padovan switch (type) { 22580a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 22590a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 22600a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 22610a708f8fSGustavo F. Padovan goto done; 22620a708f8fSGustavo F. Padovan } 22630a708f8fSGustavo F. Padovan } 22640a708f8fSGustavo F. Padovan 22650a708f8fSGustavo F. Padovan done: 22660a708f8fSGustavo F. Padovan switch (rfc.mode) { 22670a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 226847d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 226947d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 227047d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 22710a708f8fSGustavo F. Padovan break; 22720a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 227347d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 22740a708f8fSGustavo F. Padovan } 22750a708f8fSGustavo F. Padovan } 22760a708f8fSGustavo F. Padovan 22770a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 22780a708f8fSGustavo F. Padovan { 22790a708f8fSGustavo F. Padovan struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data; 22800a708f8fSGustavo F. Padovan 22810a708f8fSGustavo F. Padovan if (rej->reason != 0x0000) 22820a708f8fSGustavo F. Padovan return 0; 22830a708f8fSGustavo F. Padovan 22840a708f8fSGustavo F. Padovan if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && 22850a708f8fSGustavo F. Padovan cmd->ident == conn->info_ident) { 22860a708f8fSGustavo F. Padovan del_timer(&conn->info_timer); 22870a708f8fSGustavo F. Padovan 22880a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 22890a708f8fSGustavo F. Padovan conn->info_ident = 0; 22900a708f8fSGustavo F. Padovan 22910a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 22920a708f8fSGustavo F. Padovan } 22930a708f8fSGustavo F. Padovan 22940a708f8fSGustavo F. Padovan return 0; 22950a708f8fSGustavo F. Padovan } 22960a708f8fSGustavo F. Padovan 22970a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 22980a708f8fSGustavo F. Padovan { 22990a708f8fSGustavo F. Padovan struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; 23000a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 230123691d75SGustavo F. Padovan struct l2cap_chan *chan = NULL, *pchan; 23020a708f8fSGustavo F. Padovan struct sock *parent, *sk = NULL; 23030a708f8fSGustavo F. Padovan int result, status = L2CAP_CS_NO_INFO; 23040a708f8fSGustavo F. Padovan 23050a708f8fSGustavo F. Padovan u16 dcid = 0, scid = __le16_to_cpu(req->scid); 23060a708f8fSGustavo F. Padovan __le16 psm = req->psm; 23070a708f8fSGustavo F. Padovan 23080a708f8fSGustavo F. Padovan BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid); 23090a708f8fSGustavo F. Padovan 23100a708f8fSGustavo F. Padovan /* Check if we have socket listening on psm */ 231123691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src); 231223691d75SGustavo F. Padovan if (!pchan) { 23130a708f8fSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 23140a708f8fSGustavo F. Padovan goto sendresp; 23150a708f8fSGustavo F. Padovan } 23160a708f8fSGustavo F. Padovan 231723691d75SGustavo F. Padovan parent = pchan->sk; 231823691d75SGustavo F. Padovan 23190a708f8fSGustavo F. Padovan bh_lock_sock(parent); 23200a708f8fSGustavo F. Padovan 23210a708f8fSGustavo F. Padovan /* Check if the ACL is secure enough (if not SDP) */ 23220a708f8fSGustavo F. Padovan if (psm != cpu_to_le16(0x0001) && 23230a708f8fSGustavo F. Padovan !hci_conn_check_link_mode(conn->hcon)) { 23240a708f8fSGustavo F. Padovan conn->disc_reason = 0x05; 23250a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 23260a708f8fSGustavo F. Padovan goto response; 23270a708f8fSGustavo F. Padovan } 23280a708f8fSGustavo F. Padovan 23290a708f8fSGustavo F. Padovan result = L2CAP_CR_NO_MEM; 23300a708f8fSGustavo F. Padovan 23310a708f8fSGustavo F. Padovan /* Check for backlog size */ 23320a708f8fSGustavo F. Padovan if (sk_acceptq_is_full(parent)) { 23330a708f8fSGustavo F. Padovan BT_DBG("backlog full %d", parent->sk_ack_backlog); 23340a708f8fSGustavo F. Padovan goto response; 23350a708f8fSGustavo F. Padovan } 23360a708f8fSGustavo F. Padovan 23370a708f8fSGustavo F. Padovan sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC); 23380a708f8fSGustavo F. Padovan if (!sk) 23390a708f8fSGustavo F. Padovan goto response; 23400a708f8fSGustavo F. Padovan 234123691d75SGustavo F. Padovan chan = l2cap_chan_create(sk); 234248454079SGustavo F. Padovan if (!chan) { 234348454079SGustavo F. Padovan l2cap_sock_kill(sk); 234448454079SGustavo F. Padovan goto response; 234548454079SGustavo F. Padovan } 234648454079SGustavo F. Padovan 23475d41ce1dSGustavo F. Padovan l2cap_pi(sk)->chan = chan; 23485d41ce1dSGustavo F. Padovan 2349baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 23500a708f8fSGustavo F. Padovan 23510a708f8fSGustavo F. Padovan /* Check if we already have channel with that dcid */ 2352baa7e1faSGustavo F. Padovan if (__l2cap_get_chan_by_dcid(conn, scid)) { 2353baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 23540a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 23550a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 23560a708f8fSGustavo F. Padovan goto response; 23570a708f8fSGustavo F. Padovan } 23580a708f8fSGustavo F. Padovan 23590a708f8fSGustavo F. Padovan hci_conn_hold(conn->hcon); 23600a708f8fSGustavo F. Padovan 23610a708f8fSGustavo F. Padovan l2cap_sock_init(sk, parent); 23620a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->src, conn->src); 23630a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->dst, conn->dst); 2364fe4128e0SGustavo F. Padovan chan->psm = psm; 2365fe4128e0SGustavo F. Padovan chan->dcid = scid; 23660a708f8fSGustavo F. Padovan 2367d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 2368d1010240SGustavo F. Padovan 236948454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 237048454079SGustavo F. Padovan 2371fe4128e0SGustavo F. Padovan dcid = chan->scid; 23720a708f8fSGustavo F. Padovan 2373ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, sk->sk_sndtimeo); 23740a708f8fSGustavo F. Padovan 2375fc7f8a7eSGustavo F. Padovan chan->ident = cmd->ident; 23760a708f8fSGustavo F. Padovan 23770a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { 23784343478fSGustavo F. Padovan if (l2cap_check_security(chan)) { 23790a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 23800a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT2; 23810a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 23820a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHOR_PEND; 23830a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 23840a708f8fSGustavo F. Padovan } else { 23850a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 23860a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 23870a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 23880a708f8fSGustavo F. Padovan } 23890a708f8fSGustavo F. Padovan } else { 23900a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT2; 23910a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 23920a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHEN_PEND; 23930a708f8fSGustavo F. Padovan } 23940a708f8fSGustavo F. Padovan } else { 23950a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT2; 23960a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 23970a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 23980a708f8fSGustavo F. Padovan } 23990a708f8fSGustavo F. Padovan 2400baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 24010a708f8fSGustavo F. Padovan 24020a708f8fSGustavo F. Padovan response: 24030a708f8fSGustavo F. Padovan bh_unlock_sock(parent); 24040a708f8fSGustavo F. Padovan 24050a708f8fSGustavo F. Padovan sendresp: 24060a708f8fSGustavo F. Padovan rsp.scid = cpu_to_le16(scid); 24070a708f8fSGustavo F. Padovan rsp.dcid = cpu_to_le16(dcid); 24080a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 24090a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(status); 24100a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); 24110a708f8fSGustavo F. Padovan 24120a708f8fSGustavo F. Padovan if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { 24130a708f8fSGustavo F. Padovan struct l2cap_info_req info; 24140a708f8fSGustavo F. Padovan info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 24150a708f8fSGustavo F. Padovan 24160a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 24170a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 24180a708f8fSGustavo F. Padovan 24190a708f8fSGustavo F. Padovan mod_timer(&conn->info_timer, jiffies + 24200a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 24210a708f8fSGustavo F. Padovan 24220a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 24230a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(info), &info); 24240a708f8fSGustavo F. Padovan } 24250a708f8fSGustavo F. Padovan 2426b4450035SGustavo F. Padovan if (chan && !(chan->conf_state & L2CAP_CONF_REQ_SENT) && 24270a708f8fSGustavo F. Padovan result == L2CAP_CR_SUCCESS) { 24280a708f8fSGustavo F. Padovan u8 buf[128]; 2429b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 24300a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 243173ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 243273ffa904SGustavo F. Padovan chan->num_conf_req++; 24330a708f8fSGustavo F. Padovan } 24340a708f8fSGustavo F. Padovan 24350a708f8fSGustavo F. Padovan return 0; 24360a708f8fSGustavo F. Padovan } 24370a708f8fSGustavo F. Padovan 24380a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 24390a708f8fSGustavo F. Padovan { 24400a708f8fSGustavo F. Padovan struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; 24410a708f8fSGustavo F. Padovan u16 scid, dcid, result, status; 244248454079SGustavo F. Padovan struct l2cap_chan *chan; 24430a708f8fSGustavo F. Padovan struct sock *sk; 24440a708f8fSGustavo F. Padovan u8 req[128]; 24450a708f8fSGustavo F. Padovan 24460a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 24470a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 24480a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 24490a708f8fSGustavo F. Padovan status = __le16_to_cpu(rsp->status); 24500a708f8fSGustavo F. Padovan 24510a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status); 24520a708f8fSGustavo F. Padovan 24530a708f8fSGustavo F. Padovan if (scid) { 2454baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 245548454079SGustavo F. Padovan if (!chan) 24560a708f8fSGustavo F. Padovan return -EFAULT; 24570a708f8fSGustavo F. Padovan } else { 2458baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_ident(conn, cmd->ident); 245948454079SGustavo F. Padovan if (!chan) 24600a708f8fSGustavo F. Padovan return -EFAULT; 24610a708f8fSGustavo F. Padovan } 24620a708f8fSGustavo F. Padovan 246348454079SGustavo F. Padovan sk = chan->sk; 246448454079SGustavo F. Padovan 24650a708f8fSGustavo F. Padovan switch (result) { 24660a708f8fSGustavo F. Padovan case L2CAP_CR_SUCCESS: 24670a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 2468fc7f8a7eSGustavo F. Padovan chan->ident = 0; 2469fe4128e0SGustavo F. Padovan chan->dcid = dcid; 2470b4450035SGustavo F. Padovan chan->conf_state &= ~L2CAP_CONF_CONNECT_PEND; 24710a708f8fSGustavo F. Padovan 2472b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_REQ_SENT) 24730a708f8fSGustavo F. Padovan break; 24740a708f8fSGustavo F. Padovan 2475b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 24760a708f8fSGustavo F. Padovan 24770a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 247873ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, req), req); 247973ffa904SGustavo F. Padovan chan->num_conf_req++; 24800a708f8fSGustavo F. Padovan break; 24810a708f8fSGustavo F. Padovan 24820a708f8fSGustavo F. Padovan case L2CAP_CR_PEND: 2483b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 24840a708f8fSGustavo F. Padovan break; 24850a708f8fSGustavo F. Padovan 24860a708f8fSGustavo F. Padovan default: 24870a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 24880a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 24890a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 2490ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 2491ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 5); 24920a708f8fSGustavo F. Padovan break; 24930a708f8fSGustavo F. Padovan } 24940a708f8fSGustavo F. Padovan 249548454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNREFUSED); 24960a708f8fSGustavo F. Padovan break; 24970a708f8fSGustavo F. Padovan } 24980a708f8fSGustavo F. Padovan 24990a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 25000a708f8fSGustavo F. Padovan return 0; 25010a708f8fSGustavo F. Padovan } 25020a708f8fSGustavo F. Padovan 250347d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan) 25040a708f8fSGustavo F. Padovan { 250547d1ec61SGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(chan->sk); 250647d1ec61SGustavo F. Padovan 25070a708f8fSGustavo F. Padovan /* FCS is enabled only in ERTM or streaming mode, if one or both 25080a708f8fSGustavo F. Padovan * sides request it. 25090a708f8fSGustavo F. Padovan */ 25100c1bc5c6SGustavo F. Padovan if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) 251147d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2512b4450035SGustavo F. Padovan else if (!(pi->chan->conf_state & L2CAP_CONF_NO_FCS_RECV)) 251347d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_CRC16; 25140a708f8fSGustavo F. Padovan } 25150a708f8fSGustavo F. Padovan 25160a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 25170a708f8fSGustavo F. Padovan { 25180a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; 25190a708f8fSGustavo F. Padovan u16 dcid, flags; 25200a708f8fSGustavo F. Padovan u8 rsp[64]; 252148454079SGustavo F. Padovan struct l2cap_chan *chan; 25220a708f8fSGustavo F. Padovan struct sock *sk; 25230a708f8fSGustavo F. Padovan int len; 25240a708f8fSGustavo F. Padovan 25250a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 25260a708f8fSGustavo F. Padovan flags = __le16_to_cpu(req->flags); 25270a708f8fSGustavo F. Padovan 25280a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); 25290a708f8fSGustavo F. Padovan 2530baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 253148454079SGustavo F. Padovan if (!chan) 25320a708f8fSGustavo F. Padovan return -ENOENT; 25330a708f8fSGustavo F. Padovan 253448454079SGustavo F. Padovan sk = chan->sk; 253548454079SGustavo F. Padovan 25360a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONFIG) { 25370a708f8fSGustavo F. Padovan struct l2cap_cmd_rej rej; 25380a708f8fSGustavo F. Padovan 25390a708f8fSGustavo F. Padovan rej.reason = cpu_to_le16(0x0002); 25400a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, 25410a708f8fSGustavo F. Padovan sizeof(rej), &rej); 25420a708f8fSGustavo F. Padovan goto unlock; 25430a708f8fSGustavo F. Padovan } 25440a708f8fSGustavo F. Padovan 25450a708f8fSGustavo F. Padovan /* Reject if config buffer is too small. */ 25460a708f8fSGustavo F. Padovan len = cmd_len - sizeof(*req); 254773ffa904SGustavo F. Padovan if (chan->conf_len + len > sizeof(chan->conf_req)) { 25480a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2549fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 25500a708f8fSGustavo F. Padovan L2CAP_CONF_REJECT, flags), rsp); 25510a708f8fSGustavo F. Padovan goto unlock; 25520a708f8fSGustavo F. Padovan } 25530a708f8fSGustavo F. Padovan 25540a708f8fSGustavo F. Padovan /* Store config. */ 255573ffa904SGustavo F. Padovan memcpy(chan->conf_req + chan->conf_len, req->data, len); 255673ffa904SGustavo F. Padovan chan->conf_len += len; 25570a708f8fSGustavo F. Padovan 25580a708f8fSGustavo F. Padovan if (flags & 0x0001) { 25590a708f8fSGustavo F. Padovan /* Incomplete config. Send empty response. */ 25600a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2561fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 25620a708f8fSGustavo F. Padovan L2CAP_CONF_SUCCESS, 0x0001), rsp); 25630a708f8fSGustavo F. Padovan goto unlock; 25640a708f8fSGustavo F. Padovan } 25650a708f8fSGustavo F. Padovan 25660a708f8fSGustavo F. Padovan /* Complete config. */ 256773ffa904SGustavo F. Padovan len = l2cap_parse_conf_req(chan, rsp); 25680a708f8fSGustavo F. Padovan if (len < 0) { 2569e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 25700a708f8fSGustavo F. Padovan goto unlock; 25710a708f8fSGustavo F. Padovan } 25720a708f8fSGustavo F. Padovan 25730a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); 257473ffa904SGustavo F. Padovan chan->num_conf_rsp++; 25750a708f8fSGustavo F. Padovan 25760a708f8fSGustavo F. Padovan /* Reset config buffer. */ 257773ffa904SGustavo F. Padovan chan->conf_len = 0; 25780a708f8fSGustavo F. Padovan 2579b4450035SGustavo F. Padovan if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE)) 25800a708f8fSGustavo F. Padovan goto unlock; 25810a708f8fSGustavo F. Padovan 2582b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_INPUT_DONE) { 258347d1ec61SGustavo F. Padovan set_default_fcs(chan); 25840a708f8fSGustavo F. Padovan 25850a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 25860a708f8fSGustavo F. Padovan 258742e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 258842e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 258958d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 25900c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2591525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 25920a708f8fSGustavo F. Padovan 25930a708f8fSGustavo F. Padovan l2cap_chan_ready(sk); 25940a708f8fSGustavo F. Padovan goto unlock; 25950a708f8fSGustavo F. Padovan } 25960a708f8fSGustavo F. Padovan 2597b4450035SGustavo F. Padovan if (!(chan->conf_state & L2CAP_CONF_REQ_SENT)) { 25980a708f8fSGustavo F. Padovan u8 buf[64]; 2599b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 26000a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 260173ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 260273ffa904SGustavo F. Padovan chan->num_conf_req++; 26030a708f8fSGustavo F. Padovan } 26040a708f8fSGustavo F. Padovan 26050a708f8fSGustavo F. Padovan unlock: 26060a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 26070a708f8fSGustavo F. Padovan return 0; 26080a708f8fSGustavo F. Padovan } 26090a708f8fSGustavo F. Padovan 26100a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 26110a708f8fSGustavo F. Padovan { 26120a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; 26130a708f8fSGustavo F. Padovan u16 scid, flags, result; 261448454079SGustavo F. Padovan struct l2cap_chan *chan; 26150a708f8fSGustavo F. Padovan struct sock *sk; 26160a708f8fSGustavo F. Padovan int len = cmd->len - sizeof(*rsp); 26170a708f8fSGustavo F. Padovan 26180a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 26190a708f8fSGustavo F. Padovan flags = __le16_to_cpu(rsp->flags); 26200a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 26210a708f8fSGustavo F. Padovan 26220a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", 26230a708f8fSGustavo F. Padovan scid, flags, result); 26240a708f8fSGustavo F. Padovan 2625baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 262648454079SGustavo F. Padovan if (!chan) 26270a708f8fSGustavo F. Padovan return 0; 26280a708f8fSGustavo F. Padovan 262948454079SGustavo F. Padovan sk = chan->sk; 263048454079SGustavo F. Padovan 26310a708f8fSGustavo F. Padovan switch (result) { 26320a708f8fSGustavo F. Padovan case L2CAP_CONF_SUCCESS: 263347d1ec61SGustavo F. Padovan l2cap_conf_rfc_get(chan, rsp->data, len); 26340a708f8fSGustavo F. Padovan break; 26350a708f8fSGustavo F. Padovan 26360a708f8fSGustavo F. Padovan case L2CAP_CONF_UNACCEPT: 263773ffa904SGustavo F. Padovan if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { 26380a708f8fSGustavo F. Padovan char req[64]; 26390a708f8fSGustavo F. Padovan 26400a708f8fSGustavo F. Padovan if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { 2641e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 26420a708f8fSGustavo F. Padovan goto done; 26430a708f8fSGustavo F. Padovan } 26440a708f8fSGustavo F. Padovan 26450a708f8fSGustavo F. Padovan /* throw out any old stored conf requests */ 26460a708f8fSGustavo F. Padovan result = L2CAP_CONF_SUCCESS; 2647b4450035SGustavo F. Padovan len = l2cap_parse_conf_rsp(chan, rsp->data, len, 2648b4450035SGustavo F. Padovan req, &result); 26490a708f8fSGustavo F. Padovan if (len < 0) { 2650e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 26510a708f8fSGustavo F. Padovan goto done; 26520a708f8fSGustavo F. Padovan } 26530a708f8fSGustavo F. Padovan 26540a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 26550a708f8fSGustavo F. Padovan L2CAP_CONF_REQ, len, req); 265673ffa904SGustavo F. Padovan chan->num_conf_req++; 26570a708f8fSGustavo F. Padovan if (result != L2CAP_CONF_SUCCESS) 26580a708f8fSGustavo F. Padovan goto done; 26590a708f8fSGustavo F. Padovan break; 26600a708f8fSGustavo F. Padovan } 26610a708f8fSGustavo F. Padovan 26620a708f8fSGustavo F. Padovan default: 26630a708f8fSGustavo F. Padovan sk->sk_err = ECONNRESET; 2664ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ * 5); 2665e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 26660a708f8fSGustavo F. Padovan goto done; 26670a708f8fSGustavo F. Padovan } 26680a708f8fSGustavo F. Padovan 26690a708f8fSGustavo F. Padovan if (flags & 0x01) 26700a708f8fSGustavo F. Padovan goto done; 26710a708f8fSGustavo F. Padovan 2672b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_INPUT_DONE; 26730a708f8fSGustavo F. Padovan 2674b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_OUTPUT_DONE) { 267547d1ec61SGustavo F. Padovan set_default_fcs(chan); 26760a708f8fSGustavo F. Padovan 26770a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 267842e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 267942e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 268058d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 26810c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2682525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 26830a708f8fSGustavo F. Padovan 26840a708f8fSGustavo F. Padovan l2cap_chan_ready(sk); 26850a708f8fSGustavo F. Padovan } 26860a708f8fSGustavo F. Padovan 26870a708f8fSGustavo F. Padovan done: 26880a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 26890a708f8fSGustavo F. Padovan return 0; 26900a708f8fSGustavo F. Padovan } 26910a708f8fSGustavo F. Padovan 26920a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 26930a708f8fSGustavo F. Padovan { 26940a708f8fSGustavo F. Padovan struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; 26950a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp rsp; 26960a708f8fSGustavo F. Padovan u16 dcid, scid; 269748454079SGustavo F. Padovan struct l2cap_chan *chan; 26980a708f8fSGustavo F. Padovan struct sock *sk; 26990a708f8fSGustavo F. Padovan 27000a708f8fSGustavo F. Padovan scid = __le16_to_cpu(req->scid); 27010a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 27020a708f8fSGustavo F. Padovan 27030a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); 27040a708f8fSGustavo F. Padovan 2705baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 270648454079SGustavo F. Padovan if (!chan) 27070a708f8fSGustavo F. Padovan return 0; 27080a708f8fSGustavo F. Padovan 270948454079SGustavo F. Padovan sk = chan->sk; 271048454079SGustavo F. Padovan 2711fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 2712fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 27130a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); 27140a708f8fSGustavo F. Padovan 27150a708f8fSGustavo F. Padovan sk->sk_shutdown = SHUTDOWN_MASK; 27160a708f8fSGustavo F. Padovan 27170a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 27180a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 27190a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 2720ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 2721ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 5); 27220a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27230a708f8fSGustavo F. Padovan return 0; 27240a708f8fSGustavo F. Padovan } 27250a708f8fSGustavo F. Padovan 272648454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNRESET); 27270a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27280a708f8fSGustavo F. Padovan 27290a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 27300a708f8fSGustavo F. Padovan return 0; 27310a708f8fSGustavo F. Padovan } 27320a708f8fSGustavo F. Padovan 27330a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 27340a708f8fSGustavo F. Padovan { 27350a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; 27360a708f8fSGustavo F. Padovan u16 dcid, scid; 273748454079SGustavo F. Padovan struct l2cap_chan *chan; 27380a708f8fSGustavo F. Padovan struct sock *sk; 27390a708f8fSGustavo F. Padovan 27400a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 27410a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 27420a708f8fSGustavo F. Padovan 27430a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); 27440a708f8fSGustavo F. Padovan 2745baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 274648454079SGustavo F. Padovan if (!chan) 27470a708f8fSGustavo F. Padovan return 0; 27480a708f8fSGustavo F. Padovan 274948454079SGustavo F. Padovan sk = chan->sk; 275048454079SGustavo F. Padovan 27510a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 27520a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 27530a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 2754ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 2755ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 5); 27560a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27570a708f8fSGustavo F. Padovan return 0; 27580a708f8fSGustavo F. Padovan } 27590a708f8fSGustavo F. Padovan 276048454079SGustavo F. Padovan l2cap_chan_del(chan, 0); 27610a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27620a708f8fSGustavo F. Padovan 27630a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 27640a708f8fSGustavo F. Padovan return 0; 27650a708f8fSGustavo F. Padovan } 27660a708f8fSGustavo F. Padovan 27670a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 27680a708f8fSGustavo F. Padovan { 27690a708f8fSGustavo F. Padovan struct l2cap_info_req *req = (struct l2cap_info_req *) data; 27700a708f8fSGustavo F. Padovan u16 type; 27710a708f8fSGustavo F. Padovan 27720a708f8fSGustavo F. Padovan type = __le16_to_cpu(req->type); 27730a708f8fSGustavo F. Padovan 27740a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x", type); 27750a708f8fSGustavo F. Padovan 27760a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 27770a708f8fSGustavo F. Padovan u8 buf[8]; 27780a708f8fSGustavo F. Padovan u32 feat_mask = l2cap_feat_mask; 27790a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 27800a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 27810a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 27820a708f8fSGustavo F. Padovan if (!disable_ertm) 27830a708f8fSGustavo F. Padovan feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING 27840a708f8fSGustavo F. Padovan | L2CAP_FEAT_FCS; 27850a708f8fSGustavo F. Padovan put_unaligned_le32(feat_mask, rsp->data); 27860a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 27870a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 27880a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 27890a708f8fSGustavo F. Padovan u8 buf[12]; 27900a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 27910a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 27920a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 27930a708f8fSGustavo F. Padovan memcpy(buf + 4, l2cap_fixed_chan, 8); 27940a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 27950a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 27960a708f8fSGustavo F. Padovan } else { 27970a708f8fSGustavo F. Padovan struct l2cap_info_rsp rsp; 27980a708f8fSGustavo F. Padovan rsp.type = cpu_to_le16(type); 27990a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); 28000a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 28010a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(rsp), &rsp); 28020a708f8fSGustavo F. Padovan } 28030a708f8fSGustavo F. Padovan 28040a708f8fSGustavo F. Padovan return 0; 28050a708f8fSGustavo F. Padovan } 28060a708f8fSGustavo F. Padovan 28070a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 28080a708f8fSGustavo F. Padovan { 28090a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; 28100a708f8fSGustavo F. Padovan u16 type, result; 28110a708f8fSGustavo F. Padovan 28120a708f8fSGustavo F. Padovan type = __le16_to_cpu(rsp->type); 28130a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 28140a708f8fSGustavo F. Padovan 28150a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); 28160a708f8fSGustavo F. Padovan 2817e90165beSAndrei Emeltchenko /* L2CAP Info req/rsp are unbound to channels, add extra checks */ 2818e90165beSAndrei Emeltchenko if (cmd->ident != conn->info_ident || 2819e90165beSAndrei Emeltchenko conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) 2820e90165beSAndrei Emeltchenko return 0; 2821e90165beSAndrei Emeltchenko 28220a708f8fSGustavo F. Padovan del_timer(&conn->info_timer); 28230a708f8fSGustavo F. Padovan 28240a708f8fSGustavo F. Padovan if (result != L2CAP_IR_SUCCESS) { 28250a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 28260a708f8fSGustavo F. Padovan conn->info_ident = 0; 28270a708f8fSGustavo F. Padovan 28280a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 28290a708f8fSGustavo F. Padovan 28300a708f8fSGustavo F. Padovan return 0; 28310a708f8fSGustavo F. Padovan } 28320a708f8fSGustavo F. Padovan 28330a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 28340a708f8fSGustavo F. Padovan conn->feat_mask = get_unaligned_le32(rsp->data); 28350a708f8fSGustavo F. Padovan 28360a708f8fSGustavo F. Padovan if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { 28370a708f8fSGustavo F. Padovan struct l2cap_info_req req; 28380a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 28390a708f8fSGustavo F. Padovan 28400a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 28410a708f8fSGustavo F. Padovan 28420a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 28430a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 28440a708f8fSGustavo F. Padovan } else { 28450a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 28460a708f8fSGustavo F. Padovan conn->info_ident = 0; 28470a708f8fSGustavo F. Padovan 28480a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 28490a708f8fSGustavo F. Padovan } 28500a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 28510a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 28520a708f8fSGustavo F. Padovan conn->info_ident = 0; 28530a708f8fSGustavo F. Padovan 28540a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 28550a708f8fSGustavo F. Padovan } 28560a708f8fSGustavo F. Padovan 28570a708f8fSGustavo F. Padovan return 0; 28580a708f8fSGustavo F. Padovan } 28590a708f8fSGustavo F. Padovan 2860e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency, 2861de73115aSClaudio Takahasi u16 to_multiplier) 2862de73115aSClaudio Takahasi { 2863de73115aSClaudio Takahasi u16 max_latency; 2864de73115aSClaudio Takahasi 2865de73115aSClaudio Takahasi if (min > max || min < 6 || max > 3200) 2866de73115aSClaudio Takahasi return -EINVAL; 2867de73115aSClaudio Takahasi 2868de73115aSClaudio Takahasi if (to_multiplier < 10 || to_multiplier > 3200) 2869de73115aSClaudio Takahasi return -EINVAL; 2870de73115aSClaudio Takahasi 2871de73115aSClaudio Takahasi if (max >= to_multiplier * 8) 2872de73115aSClaudio Takahasi return -EINVAL; 2873de73115aSClaudio Takahasi 2874de73115aSClaudio Takahasi max_latency = (to_multiplier * 8 / max) - 1; 2875de73115aSClaudio Takahasi if (latency > 499 || latency > max_latency) 2876de73115aSClaudio Takahasi return -EINVAL; 2877de73115aSClaudio Takahasi 2878de73115aSClaudio Takahasi return 0; 2879de73115aSClaudio Takahasi } 2880de73115aSClaudio Takahasi 2881de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, 2882de73115aSClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 2883de73115aSClaudio Takahasi { 2884de73115aSClaudio Takahasi struct hci_conn *hcon = conn->hcon; 2885de73115aSClaudio Takahasi struct l2cap_conn_param_update_req *req; 2886de73115aSClaudio Takahasi struct l2cap_conn_param_update_rsp rsp; 2887de73115aSClaudio Takahasi u16 min, max, latency, to_multiplier, cmd_len; 28882ce603ebSClaudio Takahasi int err; 2889de73115aSClaudio Takahasi 2890de73115aSClaudio Takahasi if (!(hcon->link_mode & HCI_LM_MASTER)) 2891de73115aSClaudio Takahasi return -EINVAL; 2892de73115aSClaudio Takahasi 2893de73115aSClaudio Takahasi cmd_len = __le16_to_cpu(cmd->len); 2894de73115aSClaudio Takahasi if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) 2895de73115aSClaudio Takahasi return -EPROTO; 2896de73115aSClaudio Takahasi 2897de73115aSClaudio Takahasi req = (struct l2cap_conn_param_update_req *) data; 2898de73115aSClaudio Takahasi min = __le16_to_cpu(req->min); 2899de73115aSClaudio Takahasi max = __le16_to_cpu(req->max); 2900de73115aSClaudio Takahasi latency = __le16_to_cpu(req->latency); 2901de73115aSClaudio Takahasi to_multiplier = __le16_to_cpu(req->to_multiplier); 2902de73115aSClaudio Takahasi 2903de73115aSClaudio Takahasi BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", 2904de73115aSClaudio Takahasi min, max, latency, to_multiplier); 2905de73115aSClaudio Takahasi 2906de73115aSClaudio Takahasi memset(&rsp, 0, sizeof(rsp)); 29072ce603ebSClaudio Takahasi 29082ce603ebSClaudio Takahasi err = l2cap_check_conn_param(min, max, latency, to_multiplier); 29092ce603ebSClaudio Takahasi if (err) 2910de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); 2911de73115aSClaudio Takahasi else 2912de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); 2913de73115aSClaudio Takahasi 2914de73115aSClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, 2915de73115aSClaudio Takahasi sizeof(rsp), &rsp); 2916de73115aSClaudio Takahasi 29172ce603ebSClaudio Takahasi if (!err) 29182ce603ebSClaudio Takahasi hci_le_conn_update(hcon, min, max, latency, to_multiplier); 29192ce603ebSClaudio Takahasi 2920de73115aSClaudio Takahasi return 0; 2921de73115aSClaudio Takahasi } 2922de73115aSClaudio Takahasi 29233300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, 29243300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 29253300d9a9SClaudio Takahasi { 29263300d9a9SClaudio Takahasi int err = 0; 29273300d9a9SClaudio Takahasi 29283300d9a9SClaudio Takahasi switch (cmd->code) { 29293300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 29303300d9a9SClaudio Takahasi l2cap_command_rej(conn, cmd, data); 29313300d9a9SClaudio Takahasi break; 29323300d9a9SClaudio Takahasi 29333300d9a9SClaudio Takahasi case L2CAP_CONN_REQ: 29343300d9a9SClaudio Takahasi err = l2cap_connect_req(conn, cmd, data); 29353300d9a9SClaudio Takahasi break; 29363300d9a9SClaudio Takahasi 29373300d9a9SClaudio Takahasi case L2CAP_CONN_RSP: 29383300d9a9SClaudio Takahasi err = l2cap_connect_rsp(conn, cmd, data); 29393300d9a9SClaudio Takahasi break; 29403300d9a9SClaudio Takahasi 29413300d9a9SClaudio Takahasi case L2CAP_CONF_REQ: 29423300d9a9SClaudio Takahasi err = l2cap_config_req(conn, cmd, cmd_len, data); 29433300d9a9SClaudio Takahasi break; 29443300d9a9SClaudio Takahasi 29453300d9a9SClaudio Takahasi case L2CAP_CONF_RSP: 29463300d9a9SClaudio Takahasi err = l2cap_config_rsp(conn, cmd, data); 29473300d9a9SClaudio Takahasi break; 29483300d9a9SClaudio Takahasi 29493300d9a9SClaudio Takahasi case L2CAP_DISCONN_REQ: 29503300d9a9SClaudio Takahasi err = l2cap_disconnect_req(conn, cmd, data); 29513300d9a9SClaudio Takahasi break; 29523300d9a9SClaudio Takahasi 29533300d9a9SClaudio Takahasi case L2CAP_DISCONN_RSP: 29543300d9a9SClaudio Takahasi err = l2cap_disconnect_rsp(conn, cmd, data); 29553300d9a9SClaudio Takahasi break; 29563300d9a9SClaudio Takahasi 29573300d9a9SClaudio Takahasi case L2CAP_ECHO_REQ: 29583300d9a9SClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); 29593300d9a9SClaudio Takahasi break; 29603300d9a9SClaudio Takahasi 29613300d9a9SClaudio Takahasi case L2CAP_ECHO_RSP: 29623300d9a9SClaudio Takahasi break; 29633300d9a9SClaudio Takahasi 29643300d9a9SClaudio Takahasi case L2CAP_INFO_REQ: 29653300d9a9SClaudio Takahasi err = l2cap_information_req(conn, cmd, data); 29663300d9a9SClaudio Takahasi break; 29673300d9a9SClaudio Takahasi 29683300d9a9SClaudio Takahasi case L2CAP_INFO_RSP: 29693300d9a9SClaudio Takahasi err = l2cap_information_rsp(conn, cmd, data); 29703300d9a9SClaudio Takahasi break; 29713300d9a9SClaudio Takahasi 29723300d9a9SClaudio Takahasi default: 29733300d9a9SClaudio Takahasi BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); 29743300d9a9SClaudio Takahasi err = -EINVAL; 29753300d9a9SClaudio Takahasi break; 29763300d9a9SClaudio Takahasi } 29773300d9a9SClaudio Takahasi 29783300d9a9SClaudio Takahasi return err; 29793300d9a9SClaudio Takahasi } 29803300d9a9SClaudio Takahasi 29813300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, 29823300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 29833300d9a9SClaudio Takahasi { 29843300d9a9SClaudio Takahasi switch (cmd->code) { 29853300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 29863300d9a9SClaudio Takahasi return 0; 29873300d9a9SClaudio Takahasi 29883300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_REQ: 2989de73115aSClaudio Takahasi return l2cap_conn_param_update_req(conn, cmd, data); 29903300d9a9SClaudio Takahasi 29913300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_RSP: 29923300d9a9SClaudio Takahasi return 0; 29933300d9a9SClaudio Takahasi 29943300d9a9SClaudio Takahasi default: 29953300d9a9SClaudio Takahasi BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); 29963300d9a9SClaudio Takahasi return -EINVAL; 29973300d9a9SClaudio Takahasi } 29983300d9a9SClaudio Takahasi } 29993300d9a9SClaudio Takahasi 30003300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn, 30013300d9a9SClaudio Takahasi struct sk_buff *skb) 30020a708f8fSGustavo F. Padovan { 30030a708f8fSGustavo F. Padovan u8 *data = skb->data; 30040a708f8fSGustavo F. Padovan int len = skb->len; 30050a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr cmd; 30063300d9a9SClaudio Takahasi int err; 30070a708f8fSGustavo F. Padovan 30080a708f8fSGustavo F. Padovan l2cap_raw_recv(conn, skb); 30090a708f8fSGustavo F. Padovan 30100a708f8fSGustavo F. Padovan while (len >= L2CAP_CMD_HDR_SIZE) { 30110a708f8fSGustavo F. Padovan u16 cmd_len; 30120a708f8fSGustavo F. Padovan memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); 30130a708f8fSGustavo F. Padovan data += L2CAP_CMD_HDR_SIZE; 30140a708f8fSGustavo F. Padovan len -= L2CAP_CMD_HDR_SIZE; 30150a708f8fSGustavo F. Padovan 30160a708f8fSGustavo F. Padovan cmd_len = le16_to_cpu(cmd.len); 30170a708f8fSGustavo F. Padovan 30180a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident); 30190a708f8fSGustavo F. Padovan 30200a708f8fSGustavo F. Padovan if (cmd_len > len || !cmd.ident) { 30210a708f8fSGustavo F. Padovan BT_DBG("corrupted command"); 30220a708f8fSGustavo F. Padovan break; 30230a708f8fSGustavo F. Padovan } 30240a708f8fSGustavo F. Padovan 30253300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 30263300d9a9SClaudio Takahasi err = l2cap_le_sig_cmd(conn, &cmd, data); 30273300d9a9SClaudio Takahasi else 30283300d9a9SClaudio Takahasi err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); 30290a708f8fSGustavo F. Padovan 30300a708f8fSGustavo F. Padovan if (err) { 30310a708f8fSGustavo F. Padovan struct l2cap_cmd_rej rej; 30322c6d1a2eSGustavo F. Padovan 30332c6d1a2eSGustavo F. Padovan BT_ERR("Wrong link type (%d)", err); 30340a708f8fSGustavo F. Padovan 30350a708f8fSGustavo F. Padovan /* FIXME: Map err to a valid reason */ 30360a708f8fSGustavo F. Padovan rej.reason = cpu_to_le16(0); 30370a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); 30380a708f8fSGustavo F. Padovan } 30390a708f8fSGustavo F. Padovan 30400a708f8fSGustavo F. Padovan data += cmd_len; 30410a708f8fSGustavo F. Padovan len -= cmd_len; 30420a708f8fSGustavo F. Padovan } 30430a708f8fSGustavo F. Padovan 30440a708f8fSGustavo F. Padovan kfree_skb(skb); 30450a708f8fSGustavo F. Padovan } 30460a708f8fSGustavo F. Padovan 304747d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb) 30480a708f8fSGustavo F. Padovan { 30490a708f8fSGustavo F. Padovan u16 our_fcs, rcv_fcs; 30500a708f8fSGustavo F. Padovan int hdr_size = L2CAP_HDR_SIZE + 2; 30510a708f8fSGustavo F. Padovan 305247d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 30530a708f8fSGustavo F. Padovan skb_trim(skb, skb->len - 2); 30540a708f8fSGustavo F. Padovan rcv_fcs = get_unaligned_le16(skb->data + skb->len); 30550a708f8fSGustavo F. Padovan our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); 30560a708f8fSGustavo F. Padovan 30570a708f8fSGustavo F. Padovan if (our_fcs != rcv_fcs) 30580a708f8fSGustavo F. Padovan return -EBADMSG; 30590a708f8fSGustavo F. Padovan } 30600a708f8fSGustavo F. Padovan return 0; 30610a708f8fSGustavo F. Padovan } 30620a708f8fSGustavo F. Padovan 3063525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan) 30640a708f8fSGustavo F. Padovan { 30650a708f8fSGustavo F. Padovan u16 control = 0; 30660a708f8fSGustavo F. Padovan 30676a026610SGustavo F. Padovan chan->frames_sent = 0; 30680a708f8fSGustavo F. Padovan 306942e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 30700a708f8fSGustavo F. Padovan 3071525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 30720a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 3073525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 3074525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 30750a708f8fSGustavo F. Padovan } 30760a708f8fSGustavo F. Padovan 3077525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REMOTE_BUSY) 3078525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 30790a708f8fSGustavo F. Padovan 3080525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 30810a708f8fSGustavo F. Padovan 3082525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_LOCAL_BUSY) && 30836a026610SGustavo F. Padovan chan->frames_sent == 0) { 30840a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 3085525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 30860a708f8fSGustavo F. Padovan } 30870a708f8fSGustavo F. Padovan } 30880a708f8fSGustavo F. Padovan 308942e5c802SGustavo F. Padovan static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u8 tx_seq, u8 sar) 30900a708f8fSGustavo F. Padovan { 30910a708f8fSGustavo F. Padovan struct sk_buff *next_skb; 30920a708f8fSGustavo F. Padovan int tx_seq_offset, next_tx_seq_offset; 30930a708f8fSGustavo F. Padovan 30940a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 30950a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 30960a708f8fSGustavo F. Padovan 3097f1c6775bSGustavo F. Padovan next_skb = skb_peek(&chan->srej_q); 30980a708f8fSGustavo F. Padovan if (!next_skb) { 3099f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 31000a708f8fSGustavo F. Padovan return 0; 31010a708f8fSGustavo F. Padovan } 31020a708f8fSGustavo F. Padovan 310342e5c802SGustavo F. Padovan tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; 31040a708f8fSGustavo F. Padovan if (tx_seq_offset < 0) 31050a708f8fSGustavo F. Padovan tx_seq_offset += 64; 31060a708f8fSGustavo F. Padovan 31070a708f8fSGustavo F. Padovan do { 31080a708f8fSGustavo F. Padovan if (bt_cb(next_skb)->tx_seq == tx_seq) 31090a708f8fSGustavo F. Padovan return -EINVAL; 31100a708f8fSGustavo F. Padovan 31110a708f8fSGustavo F. Padovan next_tx_seq_offset = (bt_cb(next_skb)->tx_seq - 311242e5c802SGustavo F. Padovan chan->buffer_seq) % 64; 31130a708f8fSGustavo F. Padovan if (next_tx_seq_offset < 0) 31140a708f8fSGustavo F. Padovan next_tx_seq_offset += 64; 31150a708f8fSGustavo F. Padovan 31160a708f8fSGustavo F. Padovan if (next_tx_seq_offset > tx_seq_offset) { 3117f1c6775bSGustavo F. Padovan __skb_queue_before(&chan->srej_q, next_skb, skb); 31180a708f8fSGustavo F. Padovan return 0; 31190a708f8fSGustavo F. Padovan } 31200a708f8fSGustavo F. Padovan 3121f1c6775bSGustavo F. Padovan if (skb_queue_is_last(&chan->srej_q, next_skb)) 31220a708f8fSGustavo F. Padovan break; 31230a708f8fSGustavo F. Padovan 3124f1c6775bSGustavo F. Padovan } while ((next_skb = skb_queue_next(&chan->srej_q, next_skb))); 31250a708f8fSGustavo F. Padovan 3126f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 31270a708f8fSGustavo F. Padovan 31280a708f8fSGustavo F. Padovan return 0; 31290a708f8fSGustavo F. Padovan } 31300a708f8fSGustavo F. Padovan 3131525cd185SGustavo F. Padovan static int l2cap_ertm_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 31320a708f8fSGustavo F. Padovan { 31330a708f8fSGustavo F. Padovan struct sk_buff *_skb; 31340a708f8fSGustavo F. Padovan int err; 31350a708f8fSGustavo F. Padovan 31360a708f8fSGustavo F. Padovan switch (control & L2CAP_CTRL_SAR) { 31370a708f8fSGustavo F. Padovan case L2CAP_SDU_UNSEGMENTED: 3138525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) 31390a708f8fSGustavo F. Padovan goto drop; 31400a708f8fSGustavo F. Padovan 3141224f8af0SRuiyi Zhang return sock_queue_rcv_skb(chan->sk, skb); 31420a708f8fSGustavo F. Padovan 31430a708f8fSGustavo F. Padovan case L2CAP_SDU_START: 3144525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) 31450a708f8fSGustavo F. Padovan goto drop; 31460a708f8fSGustavo F. Padovan 31476f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 31480a708f8fSGustavo F. Padovan 31490c1bc5c6SGustavo F. Padovan if (chan->sdu_len > chan->imtu) 31500a708f8fSGustavo F. Padovan goto disconnect; 31510a708f8fSGustavo F. Padovan 31526f61fd47SGustavo F. Padovan chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC); 31536f61fd47SGustavo F. Padovan if (!chan->sdu) 31540a708f8fSGustavo F. Padovan return -ENOMEM; 31550a708f8fSGustavo F. Padovan 31560a708f8fSGustavo F. Padovan /* pull sdu_len bytes only after alloc, because of Local Busy 31570a708f8fSGustavo F. Padovan * condition we have to be sure that this will be executed 31580a708f8fSGustavo F. Padovan * only once, i.e., when alloc does not fail */ 31590a708f8fSGustavo F. Padovan skb_pull(skb, 2); 31600a708f8fSGustavo F. Padovan 31616f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31620a708f8fSGustavo F. Padovan 3163525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_SDU; 31646f61fd47SGustavo F. Padovan chan->partial_sdu_len = skb->len; 31650a708f8fSGustavo F. Padovan break; 31660a708f8fSGustavo F. Padovan 31670a708f8fSGustavo F. Padovan case L2CAP_SDU_CONTINUE: 3168525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 31690a708f8fSGustavo F. Padovan goto disconnect; 31700a708f8fSGustavo F. Padovan 31716f61fd47SGustavo F. Padovan if (!chan->sdu) 31720a708f8fSGustavo F. Padovan goto disconnect; 31730a708f8fSGustavo F. Padovan 31746f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 31756f61fd47SGustavo F. Padovan if (chan->partial_sdu_len > chan->sdu_len) 31760a708f8fSGustavo F. Padovan goto drop; 31770a708f8fSGustavo F. Padovan 31786f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31790a708f8fSGustavo F. Padovan 31800a708f8fSGustavo F. Padovan break; 31810a708f8fSGustavo F. Padovan 31820a708f8fSGustavo F. Padovan case L2CAP_SDU_END: 3183525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 31840a708f8fSGustavo F. Padovan goto disconnect; 31850a708f8fSGustavo F. Padovan 31866f61fd47SGustavo F. Padovan if (!chan->sdu) 31870a708f8fSGustavo F. Padovan goto disconnect; 31880a708f8fSGustavo F. Padovan 3189525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_RETRY)) { 31906f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 31910a708f8fSGustavo F. Padovan 31920c1bc5c6SGustavo F. Padovan if (chan->partial_sdu_len > chan->imtu) 31930a708f8fSGustavo F. Padovan goto drop; 31940a708f8fSGustavo F. Padovan 31956f61fd47SGustavo F. Padovan if (chan->partial_sdu_len != chan->sdu_len) 31960a708f8fSGustavo F. Padovan goto drop; 31970a708f8fSGustavo F. Padovan 31986f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31990a708f8fSGustavo F. Padovan } 32000a708f8fSGustavo F. Padovan 32016f61fd47SGustavo F. Padovan _skb = skb_clone(chan->sdu, GFP_ATOMIC); 32020a708f8fSGustavo F. Padovan if (!_skb) { 3203525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_RETRY; 32040a708f8fSGustavo F. Padovan return -ENOMEM; 32050a708f8fSGustavo F. Padovan } 32060a708f8fSGustavo F. Padovan 3207525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, _skb); 32080a708f8fSGustavo F. Padovan if (err < 0) { 32090a708f8fSGustavo F. Padovan kfree_skb(_skb); 3210525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_RETRY; 32110a708f8fSGustavo F. Padovan return err; 32120a708f8fSGustavo F. Padovan } 32130a708f8fSGustavo F. Padovan 3214525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_RETRY; 3215525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_SDU; 32160a708f8fSGustavo F. Padovan 32176f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 32180a708f8fSGustavo F. Padovan break; 32190a708f8fSGustavo F. Padovan } 32200a708f8fSGustavo F. Padovan 32210a708f8fSGustavo F. Padovan kfree_skb(skb); 32220a708f8fSGustavo F. Padovan return 0; 32230a708f8fSGustavo F. Padovan 32240a708f8fSGustavo F. Padovan drop: 32256f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 32266f61fd47SGustavo F. Padovan chan->sdu = NULL; 32270a708f8fSGustavo F. Padovan 32280a708f8fSGustavo F. Padovan disconnect: 32298c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 32300a708f8fSGustavo F. Padovan kfree_skb(skb); 32310a708f8fSGustavo F. Padovan return 0; 32320a708f8fSGustavo F. Padovan } 32330a708f8fSGustavo F. Padovan 3234525cd185SGustavo F. Padovan static int l2cap_try_push_rx_skb(struct l2cap_chan *chan) 32350a708f8fSGustavo F. Padovan { 32360a708f8fSGustavo F. Padovan struct sk_buff *skb; 32370a708f8fSGustavo F. Padovan u16 control; 32380a708f8fSGustavo F. Padovan int err; 32390a708f8fSGustavo F. Padovan 3240f1c6775bSGustavo F. Padovan while ((skb = skb_dequeue(&chan->busy_q))) { 32410a708f8fSGustavo F. Padovan control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT; 3242525cd185SGustavo F. Padovan err = l2cap_ertm_reassembly_sdu(chan, skb, control); 32430a708f8fSGustavo F. Padovan if (err < 0) { 3244f1c6775bSGustavo F. Padovan skb_queue_head(&chan->busy_q, skb); 32450a708f8fSGustavo F. Padovan return -EBUSY; 32460a708f8fSGustavo F. Padovan } 32470a708f8fSGustavo F. Padovan 324842e5c802SGustavo F. Padovan chan->buffer_seq = (chan->buffer_seq + 1) % 64; 32490a708f8fSGustavo F. Padovan } 32500a708f8fSGustavo F. Padovan 3251525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_RNR_SENT)) 32520a708f8fSGustavo F. Padovan goto done; 32530a708f8fSGustavo F. Padovan 325442e5c802SGustavo F. Padovan control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 32550a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY | L2CAP_CTRL_POLL; 3256525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 32576a026610SGustavo F. Padovan chan->retry_count = 1; 32580a708f8fSGustavo F. Padovan 3259e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 32600a708f8fSGustavo F. Padovan __mod_monitor_timer(); 32610a708f8fSGustavo F. Padovan 3262525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_WAIT_F; 32630a708f8fSGustavo F. Padovan 32640a708f8fSGustavo F. Padovan done: 3265525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_LOCAL_BUSY; 3266525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_RNR_SENT; 32670a708f8fSGustavo F. Padovan 326849208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit local busy", chan); 32690a708f8fSGustavo F. Padovan 32700a708f8fSGustavo F. Padovan return 0; 32710a708f8fSGustavo F. Padovan } 32720a708f8fSGustavo F. Padovan 32730a708f8fSGustavo F. Padovan static void l2cap_busy_work(struct work_struct *work) 32740a708f8fSGustavo F. Padovan { 32750a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 3276311bb895SGustavo F. Padovan struct l2cap_chan *chan = 3277311bb895SGustavo F. Padovan container_of(work, struct l2cap_chan, busy_work); 3278311bb895SGustavo F. Padovan struct sock *sk = chan->sk; 32790a708f8fSGustavo F. Padovan int n_tries = 0, timeo = HZ/5, err; 32800a708f8fSGustavo F. Padovan struct sk_buff *skb; 32810a708f8fSGustavo F. Padovan 32820a708f8fSGustavo F. Padovan lock_sock(sk); 32830a708f8fSGustavo F. Padovan 32840a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 3285311bb895SGustavo F. Padovan while ((skb = skb_peek(&chan->busy_q))) { 32860a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 32870a708f8fSGustavo F. Padovan 32880a708f8fSGustavo F. Padovan if (n_tries++ > L2CAP_LOCAL_BUSY_TRIES) { 32890a708f8fSGustavo F. Padovan err = -EBUSY; 32908c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, EBUSY); 32910a708f8fSGustavo F. Padovan break; 32920a708f8fSGustavo F. Padovan } 32930a708f8fSGustavo F. Padovan 32940a708f8fSGustavo F. Padovan if (!timeo) 32950a708f8fSGustavo F. Padovan timeo = HZ/5; 32960a708f8fSGustavo F. Padovan 32970a708f8fSGustavo F. Padovan if (signal_pending(current)) { 32980a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 32990a708f8fSGustavo F. Padovan break; 33000a708f8fSGustavo F. Padovan } 33010a708f8fSGustavo F. Padovan 33020a708f8fSGustavo F. Padovan release_sock(sk); 33030a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 33040a708f8fSGustavo F. Padovan lock_sock(sk); 33050a708f8fSGustavo F. Padovan 33060a708f8fSGustavo F. Padovan err = sock_error(sk); 33070a708f8fSGustavo F. Padovan if (err) 33080a708f8fSGustavo F. Padovan break; 33090a708f8fSGustavo F. Padovan 3310311bb895SGustavo F. Padovan if (l2cap_try_push_rx_skb(chan) == 0) 33110a708f8fSGustavo F. Padovan break; 33120a708f8fSGustavo F. Padovan } 33130a708f8fSGustavo F. Padovan 33140a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 33150a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 33160a708f8fSGustavo F. Padovan 33170a708f8fSGustavo F. Padovan release_sock(sk); 33180a708f8fSGustavo F. Padovan } 33190a708f8fSGustavo F. Padovan 3320525cd185SGustavo F. Padovan static int l2cap_push_rx_skb(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 33210a708f8fSGustavo F. Padovan { 33220a708f8fSGustavo F. Padovan int sctrl, err; 33230a708f8fSGustavo F. Padovan 3324525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 33250a708f8fSGustavo F. Padovan bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT; 3326f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->busy_q, skb); 3327525cd185SGustavo F. Padovan return l2cap_try_push_rx_skb(chan); 33280a708f8fSGustavo F. Padovan 33290a708f8fSGustavo F. Padovan 33300a708f8fSGustavo F. Padovan } 33310a708f8fSGustavo F. Padovan 3332525cd185SGustavo F. Padovan err = l2cap_ertm_reassembly_sdu(chan, skb, control); 33330a708f8fSGustavo F. Padovan if (err >= 0) { 333442e5c802SGustavo F. Padovan chan->buffer_seq = (chan->buffer_seq + 1) % 64; 33350a708f8fSGustavo F. Padovan return err; 33360a708f8fSGustavo F. Padovan } 33370a708f8fSGustavo F. Padovan 33380a708f8fSGustavo F. Padovan /* Busy Condition */ 3339311bb895SGustavo F. Padovan BT_DBG("chan %p, Enter local busy", chan); 33400a708f8fSGustavo F. Padovan 3341525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_LOCAL_BUSY; 33420a708f8fSGustavo F. Padovan bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT; 3343f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->busy_q, skb); 33440a708f8fSGustavo F. Padovan 334542e5c802SGustavo F. Padovan sctrl = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 33460a708f8fSGustavo F. Padovan sctrl |= L2CAP_SUPER_RCV_NOT_READY; 3347525cd185SGustavo F. Padovan l2cap_send_sframe(chan, sctrl); 33480a708f8fSGustavo F. Padovan 3349525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 33500a708f8fSGustavo F. Padovan 3351e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 33520a708f8fSGustavo F. Padovan 3353311bb895SGustavo F. Padovan queue_work(_busy_wq, &chan->busy_work); 33540a708f8fSGustavo F. Padovan 33550a708f8fSGustavo F. Padovan return err; 33560a708f8fSGustavo F. Padovan } 33570a708f8fSGustavo F. Padovan 3358525cd185SGustavo F. Padovan static int l2cap_streaming_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 33590a708f8fSGustavo F. Padovan { 33600a708f8fSGustavo F. Padovan struct sk_buff *_skb; 33610a708f8fSGustavo F. Padovan int err = -EINVAL; 33620a708f8fSGustavo F. Padovan 33630a708f8fSGustavo F. Padovan /* 33640a708f8fSGustavo F. Padovan * TODO: We have to notify the userland if some data is lost with the 33650a708f8fSGustavo F. Padovan * Streaming Mode. 33660a708f8fSGustavo F. Padovan */ 33670a708f8fSGustavo F. Padovan 33680a708f8fSGustavo F. Padovan switch (control & L2CAP_CTRL_SAR) { 33690a708f8fSGustavo F. Padovan case L2CAP_SDU_UNSEGMENTED: 3370525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) { 33716f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 33720a708f8fSGustavo F. Padovan break; 33730a708f8fSGustavo F. Padovan } 33740a708f8fSGustavo F. Padovan 3375525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, skb); 33760a708f8fSGustavo F. Padovan if (!err) 33770a708f8fSGustavo F. Padovan return 0; 33780a708f8fSGustavo F. Padovan 33790a708f8fSGustavo F. Padovan break; 33800a708f8fSGustavo F. Padovan 33810a708f8fSGustavo F. Padovan case L2CAP_SDU_START: 3382525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) { 33836f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 33840a708f8fSGustavo F. Padovan break; 33850a708f8fSGustavo F. Padovan } 33860a708f8fSGustavo F. Padovan 33876f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 33880a708f8fSGustavo F. Padovan skb_pull(skb, 2); 33890a708f8fSGustavo F. Padovan 33900c1bc5c6SGustavo F. Padovan if (chan->sdu_len > chan->imtu) { 33910a708f8fSGustavo F. Padovan err = -EMSGSIZE; 33920a708f8fSGustavo F. Padovan break; 33930a708f8fSGustavo F. Padovan } 33940a708f8fSGustavo F. Padovan 33956f61fd47SGustavo F. Padovan chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC); 33966f61fd47SGustavo F. Padovan if (!chan->sdu) { 33970a708f8fSGustavo F. Padovan err = -ENOMEM; 33980a708f8fSGustavo F. Padovan break; 33990a708f8fSGustavo F. Padovan } 34000a708f8fSGustavo F. Padovan 34016f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 34020a708f8fSGustavo F. Padovan 3403525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_SDU; 34046f61fd47SGustavo F. Padovan chan->partial_sdu_len = skb->len; 34050a708f8fSGustavo F. Padovan err = 0; 34060a708f8fSGustavo F. Padovan break; 34070a708f8fSGustavo F. Padovan 34080a708f8fSGustavo F. Padovan case L2CAP_SDU_CONTINUE: 3409525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 34100a708f8fSGustavo F. Padovan break; 34110a708f8fSGustavo F. Padovan 34126f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 34130a708f8fSGustavo F. Padovan 34146f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 34156f61fd47SGustavo F. Padovan if (chan->partial_sdu_len > chan->sdu_len) 34166f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 34170a708f8fSGustavo F. Padovan else 34180a708f8fSGustavo F. Padovan err = 0; 34190a708f8fSGustavo F. Padovan 34200a708f8fSGustavo F. Padovan break; 34210a708f8fSGustavo F. Padovan 34220a708f8fSGustavo F. Padovan case L2CAP_SDU_END: 3423525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 34240a708f8fSGustavo F. Padovan break; 34250a708f8fSGustavo F. Padovan 34266f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 34270a708f8fSGustavo F. Padovan 3428525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_SDU; 34296f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 34300a708f8fSGustavo F. Padovan 34310c1bc5c6SGustavo F. Padovan if (chan->partial_sdu_len > chan->imtu) 34320a708f8fSGustavo F. Padovan goto drop; 34330a708f8fSGustavo F. Padovan 34346f61fd47SGustavo F. Padovan if (chan->partial_sdu_len == chan->sdu_len) { 34356f61fd47SGustavo F. Padovan _skb = skb_clone(chan->sdu, GFP_ATOMIC); 3436525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, _skb); 34370a708f8fSGustavo F. Padovan if (err < 0) 34380a708f8fSGustavo F. Padovan kfree_skb(_skb); 34390a708f8fSGustavo F. Padovan } 34400a708f8fSGustavo F. Padovan err = 0; 34410a708f8fSGustavo F. Padovan 34420a708f8fSGustavo F. Padovan drop: 34436f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 34440a708f8fSGustavo F. Padovan break; 34450a708f8fSGustavo F. Padovan } 34460a708f8fSGustavo F. Padovan 34470a708f8fSGustavo F. Padovan kfree_skb(skb); 34480a708f8fSGustavo F. Padovan return err; 34490a708f8fSGustavo F. Padovan } 34500a708f8fSGustavo F. Padovan 3451525cd185SGustavo F. Padovan static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq) 34520a708f8fSGustavo F. Padovan { 34530a708f8fSGustavo F. Padovan struct sk_buff *skb; 34540a708f8fSGustavo F. Padovan u16 control; 34550a708f8fSGustavo F. Padovan 3456f1c6775bSGustavo F. Padovan while ((skb = skb_peek(&chan->srej_q))) { 34570a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq != tx_seq) 34580a708f8fSGustavo F. Padovan break; 34590a708f8fSGustavo F. Padovan 3460f1c6775bSGustavo F. Padovan skb = skb_dequeue(&chan->srej_q); 34610a708f8fSGustavo F. Padovan control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT; 3462525cd185SGustavo F. Padovan l2cap_ertm_reassembly_sdu(chan, skb, control); 346342e5c802SGustavo F. Padovan chan->buffer_seq_srej = 346442e5c802SGustavo F. Padovan (chan->buffer_seq_srej + 1) % 64; 34650a708f8fSGustavo F. Padovan tx_seq = (tx_seq + 1) % 64; 34660a708f8fSGustavo F. Padovan } 34670a708f8fSGustavo F. Padovan } 34680a708f8fSGustavo F. Padovan 3469525cd185SGustavo F. Padovan static void l2cap_resend_srejframe(struct l2cap_chan *chan, u8 tx_seq) 34700a708f8fSGustavo F. Padovan { 34710a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 34720a708f8fSGustavo F. Padovan u16 control; 34730a708f8fSGustavo F. Padovan 347439d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 34750a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 34760a708f8fSGustavo F. Padovan list_del(&l->list); 34770a708f8fSGustavo F. Padovan kfree(l); 34780a708f8fSGustavo F. Padovan return; 34790a708f8fSGustavo F. Padovan } 34800a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 34810a708f8fSGustavo F. Padovan control |= l->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 3482525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 34830a708f8fSGustavo F. Padovan list_del(&l->list); 348439d5a3eeSGustavo F. Padovan list_add_tail(&l->list, &chan->srej_l); 34850a708f8fSGustavo F. Padovan } 34860a708f8fSGustavo F. Padovan } 34870a708f8fSGustavo F. Padovan 3488525cd185SGustavo F. Padovan static void l2cap_send_srejframe(struct l2cap_chan *chan, u8 tx_seq) 34890a708f8fSGustavo F. Padovan { 34900a708f8fSGustavo F. Padovan struct srej_list *new; 34910a708f8fSGustavo F. Padovan u16 control; 34920a708f8fSGustavo F. Padovan 349342e5c802SGustavo F. Padovan while (tx_seq != chan->expected_tx_seq) { 34940a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 349542e5c802SGustavo F. Padovan control |= chan->expected_tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 3496525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 34970a708f8fSGustavo F. Padovan 34980a708f8fSGustavo F. Padovan new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); 349942e5c802SGustavo F. Padovan new->tx_seq = chan->expected_tx_seq; 350042e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 350139d5a3eeSGustavo F. Padovan list_add_tail(&new->list, &chan->srej_l); 35020a708f8fSGustavo F. Padovan } 350342e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 35040a708f8fSGustavo F. Padovan } 35050a708f8fSGustavo F. Padovan 3506525cd185SGustavo F. Padovan static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) 35070a708f8fSGustavo F. Padovan { 35080a708f8fSGustavo F. Padovan u8 tx_seq = __get_txseq(rx_control); 35090a708f8fSGustavo F. Padovan u8 req_seq = __get_reqseq(rx_control); 35100a708f8fSGustavo F. Padovan u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT; 35110a708f8fSGustavo F. Padovan int tx_seq_offset, expected_tx_seq_offset; 351247d1ec61SGustavo F. Padovan int num_to_ack = (chan->tx_win/6) + 1; 35130a708f8fSGustavo F. Padovan int err = 0; 35140a708f8fSGustavo F. Padovan 3515525cd185SGustavo F. Padovan BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len, 3516525cd185SGustavo F. Padovan tx_seq, rx_control); 35170a708f8fSGustavo F. Padovan 35180a708f8fSGustavo F. Padovan if (L2CAP_CTRL_FINAL & rx_control && 3519525cd185SGustavo F. Padovan chan->conn_state & L2CAP_CONN_WAIT_F) { 3520e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 35216a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 35220a708f8fSGustavo F. Padovan __mod_retrans_timer(); 3523525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_WAIT_F; 35240a708f8fSGustavo F. Padovan } 35250a708f8fSGustavo F. Padovan 352642e5c802SGustavo F. Padovan chan->expected_ack_seq = req_seq; 352742e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 35280a708f8fSGustavo F. Padovan 352942e5c802SGustavo F. Padovan if (tx_seq == chan->expected_tx_seq) 35300a708f8fSGustavo F. Padovan goto expected; 35310a708f8fSGustavo F. Padovan 353242e5c802SGustavo F. Padovan tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; 35330a708f8fSGustavo F. Padovan if (tx_seq_offset < 0) 35340a708f8fSGustavo F. Padovan tx_seq_offset += 64; 35350a708f8fSGustavo F. Padovan 35360a708f8fSGustavo F. Padovan /* invalid tx_seq */ 353747d1ec61SGustavo F. Padovan if (tx_seq_offset >= chan->tx_win) { 35388c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 35390a708f8fSGustavo F. Padovan goto drop; 35400a708f8fSGustavo F. Padovan } 35410a708f8fSGustavo F. Padovan 3542525cd185SGustavo F. Padovan if (chan->conn_state == L2CAP_CONN_LOCAL_BUSY) 35430a708f8fSGustavo F. Padovan goto drop; 35440a708f8fSGustavo F. Padovan 3545525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 35460a708f8fSGustavo F. Padovan struct srej_list *first; 35470a708f8fSGustavo F. Padovan 354839d5a3eeSGustavo F. Padovan first = list_first_entry(&chan->srej_l, 35490a708f8fSGustavo F. Padovan struct srej_list, list); 35500a708f8fSGustavo F. Padovan if (tx_seq == first->tx_seq) { 355142e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 3552525cd185SGustavo F. Padovan l2cap_check_srej_gap(chan, tx_seq); 35530a708f8fSGustavo F. Padovan 35540a708f8fSGustavo F. Padovan list_del(&first->list); 35550a708f8fSGustavo F. Padovan kfree(first); 35560a708f8fSGustavo F. Padovan 355739d5a3eeSGustavo F. Padovan if (list_empty(&chan->srej_l)) { 355842e5c802SGustavo F. Padovan chan->buffer_seq = chan->buffer_seq_srej; 3559525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SREJ_SENT; 3560525cd185SGustavo F. Padovan l2cap_send_ack(chan); 356149208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit SREJ_SENT", chan); 35620a708f8fSGustavo F. Padovan } 35630a708f8fSGustavo F. Padovan } else { 35640a708f8fSGustavo F. Padovan struct srej_list *l; 35650a708f8fSGustavo F. Padovan 35660a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 356742e5c802SGustavo F. Padovan if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0) 35680a708f8fSGustavo F. Padovan goto drop; 35690a708f8fSGustavo F. Padovan 357039d5a3eeSGustavo F. Padovan list_for_each_entry(l, &chan->srej_l, list) { 35710a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 3572525cd185SGustavo F. Padovan l2cap_resend_srejframe(chan, tx_seq); 35730a708f8fSGustavo F. Padovan return 0; 35740a708f8fSGustavo F. Padovan } 35750a708f8fSGustavo F. Padovan } 3576525cd185SGustavo F. Padovan l2cap_send_srejframe(chan, tx_seq); 35770a708f8fSGustavo F. Padovan } 35780a708f8fSGustavo F. Padovan } else { 35790a708f8fSGustavo F. Padovan expected_tx_seq_offset = 358042e5c802SGustavo F. Padovan (chan->expected_tx_seq - chan->buffer_seq) % 64; 35810a708f8fSGustavo F. Padovan if (expected_tx_seq_offset < 0) 35820a708f8fSGustavo F. Padovan expected_tx_seq_offset += 64; 35830a708f8fSGustavo F. Padovan 35840a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 35850a708f8fSGustavo F. Padovan if (tx_seq_offset < expected_tx_seq_offset) 35860a708f8fSGustavo F. Padovan goto drop; 35870a708f8fSGustavo F. Padovan 3588525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_SENT; 35890a708f8fSGustavo F. Padovan 359049208c9cSGustavo F. Padovan BT_DBG("chan %p, Enter SREJ", chan); 35910a708f8fSGustavo F. Padovan 359239d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 359342e5c802SGustavo F. Padovan chan->buffer_seq_srej = chan->buffer_seq; 35940a708f8fSGustavo F. Padovan 3595f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->srej_q); 3596f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->busy_q); 359742e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 35980a708f8fSGustavo F. Padovan 3599525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_PBIT; 36000a708f8fSGustavo F. Padovan 3601525cd185SGustavo F. Padovan l2cap_send_srejframe(chan, tx_seq); 36020a708f8fSGustavo F. Padovan 3603e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 36040a708f8fSGustavo F. Padovan } 36050a708f8fSGustavo F. Padovan return 0; 36060a708f8fSGustavo F. Padovan 36070a708f8fSGustavo F. Padovan expected: 360842e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 36090a708f8fSGustavo F. Padovan 3610525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 36110a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 36120a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 3613f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 36140a708f8fSGustavo F. Padovan return 0; 36150a708f8fSGustavo F. Padovan } 36160a708f8fSGustavo F. Padovan 3617525cd185SGustavo F. Padovan err = l2cap_push_rx_skb(chan, skb, rx_control); 36180a708f8fSGustavo F. Padovan if (err < 0) 36190a708f8fSGustavo F. Padovan return 0; 36200a708f8fSGustavo F. Padovan 36210a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_FINAL) { 3622525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3623525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 36240a708f8fSGustavo F. Padovan else 3625525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 36260a708f8fSGustavo F. Padovan } 36270a708f8fSGustavo F. Padovan 36280a708f8fSGustavo F. Padovan __mod_ack_timer(); 36290a708f8fSGustavo F. Padovan 36306a026610SGustavo F. Padovan chan->num_acked = (chan->num_acked + 1) % num_to_ack; 36316a026610SGustavo F. Padovan if (chan->num_acked == num_to_ack - 1) 3632525cd185SGustavo F. Padovan l2cap_send_ack(chan); 36330a708f8fSGustavo F. Padovan 36340a708f8fSGustavo F. Padovan return 0; 36350a708f8fSGustavo F. Padovan 36360a708f8fSGustavo F. Padovan drop: 36370a708f8fSGustavo F. Padovan kfree_skb(skb); 36380a708f8fSGustavo F. Padovan return 0; 36390a708f8fSGustavo F. Padovan } 36400a708f8fSGustavo F. Padovan 3641525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control) 36420a708f8fSGustavo F. Padovan { 364349208c9cSGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, __get_reqseq(rx_control), 36440a708f8fSGustavo F. Padovan rx_control); 36450a708f8fSGustavo F. Padovan 364642e5c802SGustavo F. Padovan chan->expected_ack_seq = __get_reqseq(rx_control); 364742e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 36480a708f8fSGustavo F. Padovan 36490a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) { 3650525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 3651525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 3652525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && 36536a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 36540a708f8fSGustavo F. Padovan __mod_retrans_timer(); 36550a708f8fSGustavo F. Padovan 3656525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 3657525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 36580a708f8fSGustavo F. Padovan } else { 3659525cd185SGustavo F. Padovan l2cap_send_i_or_rr_or_rnr(chan); 36600a708f8fSGustavo F. Padovan } 36610a708f8fSGustavo F. Padovan 36620a708f8fSGustavo F. Padovan } else if (rx_control & L2CAP_CTRL_FINAL) { 3663525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 36640a708f8fSGustavo F. Padovan 3665525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3666525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 36670a708f8fSGustavo F. Padovan else 3668525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 36690a708f8fSGustavo F. Padovan 36700a708f8fSGustavo F. Padovan } else { 3671525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && 36726a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 36730a708f8fSGustavo F. Padovan __mod_retrans_timer(); 36740a708f8fSGustavo F. Padovan 3675525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 3676525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) 3677525cd185SGustavo F. Padovan l2cap_send_ack(chan); 36780a708f8fSGustavo F. Padovan else 3679525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 36800a708f8fSGustavo F. Padovan } 36810a708f8fSGustavo F. Padovan } 36820a708f8fSGustavo F. Padovan 3683525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control) 36840a708f8fSGustavo F. Padovan { 36850a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 36860a708f8fSGustavo F. Padovan 3687525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 36880a708f8fSGustavo F. Padovan 3689525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 36900a708f8fSGustavo F. Padovan 369142e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 369242e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 36930a708f8fSGustavo F. Padovan 36940a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_FINAL) { 3695525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3696525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 36970a708f8fSGustavo F. Padovan else 3698525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 36990a708f8fSGustavo F. Padovan } else { 3700525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 37010a708f8fSGustavo F. Padovan 3702525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) 3703525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_REJ_ACT; 37040a708f8fSGustavo F. Padovan } 37050a708f8fSGustavo F. Padovan } 3706525cd185SGustavo F. Padovan static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control) 37070a708f8fSGustavo F. Padovan { 37080a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 37090a708f8fSGustavo F. Padovan 3710525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 37110a708f8fSGustavo F. Padovan 3712525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 37130a708f8fSGustavo F. Padovan 37140a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) { 371542e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 371642e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 37170a708f8fSGustavo F. Padovan 3718525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 3719525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 37200a708f8fSGustavo F. Padovan 3721525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 37220a708f8fSGustavo F. Padovan 3723525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) { 37246a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 3725525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_ACT; 37260a708f8fSGustavo F. Padovan } 37270a708f8fSGustavo F. Padovan } else if (rx_control & L2CAP_CTRL_FINAL) { 3728525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_SREJ_ACT) && 37296a026610SGustavo F. Padovan chan->srej_save_reqseq == tx_seq) 3730525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SREJ_ACT; 37310a708f8fSGustavo F. Padovan else 3732525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 37330a708f8fSGustavo F. Padovan } else { 3734525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 3735525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) { 37366a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 3737525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_ACT; 37380a708f8fSGustavo F. Padovan } 37390a708f8fSGustavo F. Padovan } 37400a708f8fSGustavo F. Padovan } 37410a708f8fSGustavo F. Padovan 3742525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control) 37430a708f8fSGustavo F. Padovan { 37440a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 37450a708f8fSGustavo F. Padovan 3746525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 37470a708f8fSGustavo F. Padovan 3748525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_REMOTE_BUSY; 374942e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 375042e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 37510a708f8fSGustavo F. Padovan 37520a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3753525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 37540a708f8fSGustavo F. Padovan 3755525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SREJ_SENT)) { 3756e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 37570a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3758525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); 37590a708f8fSGustavo F. Padovan return; 37600a708f8fSGustavo F. Padovan } 37610a708f8fSGustavo F. Padovan 37620a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3763525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 37640a708f8fSGustavo F. Padovan else 3765525cd185SGustavo F. Padovan l2cap_send_sframe(chan, L2CAP_SUPER_RCV_READY); 37660a708f8fSGustavo F. Padovan } 37670a708f8fSGustavo F. Padovan 3768525cd185SGustavo F. Padovan static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) 37690a708f8fSGustavo F. Padovan { 3770525cd185SGustavo F. Padovan BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len); 37710a708f8fSGustavo F. Padovan 37720a708f8fSGustavo F. Padovan if (L2CAP_CTRL_FINAL & rx_control && 3773525cd185SGustavo F. Padovan chan->conn_state & L2CAP_CONN_WAIT_F) { 3774e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 37756a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 37760a708f8fSGustavo F. Padovan __mod_retrans_timer(); 3777525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_WAIT_F; 37780a708f8fSGustavo F. Padovan } 37790a708f8fSGustavo F. Padovan 37800a708f8fSGustavo F. Padovan switch (rx_control & L2CAP_CTRL_SUPERVISE) { 37810a708f8fSGustavo F. Padovan case L2CAP_SUPER_RCV_READY: 3782525cd185SGustavo F. Padovan l2cap_data_channel_rrframe(chan, rx_control); 37830a708f8fSGustavo F. Padovan break; 37840a708f8fSGustavo F. Padovan 37850a708f8fSGustavo F. Padovan case L2CAP_SUPER_REJECT: 3786525cd185SGustavo F. Padovan l2cap_data_channel_rejframe(chan, rx_control); 37870a708f8fSGustavo F. Padovan break; 37880a708f8fSGustavo F. Padovan 37890a708f8fSGustavo F. Padovan case L2CAP_SUPER_SELECT_REJECT: 3790525cd185SGustavo F. Padovan l2cap_data_channel_srejframe(chan, rx_control); 37910a708f8fSGustavo F. Padovan break; 37920a708f8fSGustavo F. Padovan 37930a708f8fSGustavo F. Padovan case L2CAP_SUPER_RCV_NOT_READY: 3794525cd185SGustavo F. Padovan l2cap_data_channel_rnrframe(chan, rx_control); 37950a708f8fSGustavo F. Padovan break; 37960a708f8fSGustavo F. Padovan } 37970a708f8fSGustavo F. Padovan 37980a708f8fSGustavo F. Padovan kfree_skb(skb); 37990a708f8fSGustavo F. Padovan return 0; 38000a708f8fSGustavo F. Padovan } 38010a708f8fSGustavo F. Padovan 38020a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb) 38030a708f8fSGustavo F. Padovan { 3804525cd185SGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 38050a708f8fSGustavo F. Padovan u16 control; 38060a708f8fSGustavo F. Padovan u8 req_seq; 38070a708f8fSGustavo F. Padovan int len, next_tx_seq_offset, req_seq_offset; 38080a708f8fSGustavo F. Padovan 38090a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data); 38100a708f8fSGustavo F. Padovan skb_pull(skb, 2); 38110a708f8fSGustavo F. Padovan len = skb->len; 38120a708f8fSGustavo F. Padovan 38130a708f8fSGustavo F. Padovan /* 38140a708f8fSGustavo F. Padovan * We can just drop the corrupted I-frame here. 38150a708f8fSGustavo F. Padovan * Receiver will miss it and start proper recovery 38160a708f8fSGustavo F. Padovan * procedures and ask retransmission. 38170a708f8fSGustavo F. Padovan */ 381847d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 38190a708f8fSGustavo F. Padovan goto drop; 38200a708f8fSGustavo F. Padovan 38210a708f8fSGustavo F. Padovan if (__is_sar_start(control) && __is_iframe(control)) 38220a708f8fSGustavo F. Padovan len -= 2; 38230a708f8fSGustavo F. Padovan 382447d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 38250a708f8fSGustavo F. Padovan len -= 2; 38260a708f8fSGustavo F. Padovan 382747d1ec61SGustavo F. Padovan if (len > chan->mps) { 38288c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38290a708f8fSGustavo F. Padovan goto drop; 38300a708f8fSGustavo F. Padovan } 38310a708f8fSGustavo F. Padovan 38320a708f8fSGustavo F. Padovan req_seq = __get_reqseq(control); 383342e5c802SGustavo F. Padovan req_seq_offset = (req_seq - chan->expected_ack_seq) % 64; 38340a708f8fSGustavo F. Padovan if (req_seq_offset < 0) 38350a708f8fSGustavo F. Padovan req_seq_offset += 64; 38360a708f8fSGustavo F. Padovan 38370a708f8fSGustavo F. Padovan next_tx_seq_offset = 383842e5c802SGustavo F. Padovan (chan->next_tx_seq - chan->expected_ack_seq) % 64; 38390a708f8fSGustavo F. Padovan if (next_tx_seq_offset < 0) 38400a708f8fSGustavo F. Padovan next_tx_seq_offset += 64; 38410a708f8fSGustavo F. Padovan 38420a708f8fSGustavo F. Padovan /* check for invalid req-seq */ 38430a708f8fSGustavo F. Padovan if (req_seq_offset > next_tx_seq_offset) { 38448c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38450a708f8fSGustavo F. Padovan goto drop; 38460a708f8fSGustavo F. Padovan } 38470a708f8fSGustavo F. Padovan 38480a708f8fSGustavo F. Padovan if (__is_iframe(control)) { 38490a708f8fSGustavo F. Padovan if (len < 0) { 38508c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38510a708f8fSGustavo F. Padovan goto drop; 38520a708f8fSGustavo F. Padovan } 38530a708f8fSGustavo F. Padovan 3854525cd185SGustavo F. Padovan l2cap_data_channel_iframe(chan, control, skb); 38550a708f8fSGustavo F. Padovan } else { 38560a708f8fSGustavo F. Padovan if (len != 0) { 38570a708f8fSGustavo F. Padovan BT_ERR("%d", len); 38588c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38590a708f8fSGustavo F. Padovan goto drop; 38600a708f8fSGustavo F. Padovan } 38610a708f8fSGustavo F. Padovan 3862525cd185SGustavo F. Padovan l2cap_data_channel_sframe(chan, control, skb); 38630a708f8fSGustavo F. Padovan } 38640a708f8fSGustavo F. Padovan 38650a708f8fSGustavo F. Padovan return 0; 38660a708f8fSGustavo F. Padovan 38670a708f8fSGustavo F. Padovan drop: 38680a708f8fSGustavo F. Padovan kfree_skb(skb); 38690a708f8fSGustavo F. Padovan return 0; 38700a708f8fSGustavo F. Padovan } 38710a708f8fSGustavo F. Padovan 38720a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) 38730a708f8fSGustavo F. Padovan { 387448454079SGustavo F. Padovan struct l2cap_chan *chan; 3875bf734843SDavid S. Miller struct sock *sk = NULL; 38760a708f8fSGustavo F. Padovan u16 control; 38770a708f8fSGustavo F. Padovan u8 tx_seq; 38780a708f8fSGustavo F. Padovan int len; 38790a708f8fSGustavo F. Padovan 3880baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 388148454079SGustavo F. Padovan if (!chan) { 38820a708f8fSGustavo F. Padovan BT_DBG("unknown cid 0x%4.4x", cid); 38830a708f8fSGustavo F. Padovan goto drop; 38840a708f8fSGustavo F. Padovan } 38850a708f8fSGustavo F. Padovan 388648454079SGustavo F. Padovan sk = chan->sk; 38870a708f8fSGustavo F. Padovan 388849208c9cSGustavo F. Padovan BT_DBG("chan %p, len %d", chan, skb->len); 38890a708f8fSGustavo F. Padovan 38900a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 38910a708f8fSGustavo F. Padovan goto drop; 38920a708f8fSGustavo F. Padovan 38930c1bc5c6SGustavo F. Padovan switch (chan->mode) { 38940a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 38950a708f8fSGustavo F. Padovan /* If socket recv buffers overflows we drop data here 38960a708f8fSGustavo F. Padovan * which is *bad* because L2CAP has to be reliable. 38970a708f8fSGustavo F. Padovan * But we don't have any other choice. L2CAP doesn't 38980a708f8fSGustavo F. Padovan * provide flow control mechanism. */ 38990a708f8fSGustavo F. Padovan 39000c1bc5c6SGustavo F. Padovan if (chan->imtu < skb->len) 39010a708f8fSGustavo F. Padovan goto drop; 39020a708f8fSGustavo F. Padovan 39030a708f8fSGustavo F. Padovan if (!sock_queue_rcv_skb(sk, skb)) 39040a708f8fSGustavo F. Padovan goto done; 39050a708f8fSGustavo F. Padovan break; 39060a708f8fSGustavo F. Padovan 39070a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 39080a708f8fSGustavo F. Padovan if (!sock_owned_by_user(sk)) { 39090a708f8fSGustavo F. Padovan l2cap_ertm_data_rcv(sk, skb); 39100a708f8fSGustavo F. Padovan } else { 39110a708f8fSGustavo F. Padovan if (sk_add_backlog(sk, skb)) 39120a708f8fSGustavo F. Padovan goto drop; 39130a708f8fSGustavo F. Padovan } 39140a708f8fSGustavo F. Padovan 39150a708f8fSGustavo F. Padovan goto done; 39160a708f8fSGustavo F. Padovan 39170a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 39180a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data); 39190a708f8fSGustavo F. Padovan skb_pull(skb, 2); 39200a708f8fSGustavo F. Padovan len = skb->len; 39210a708f8fSGustavo F. Padovan 392247d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 39230a708f8fSGustavo F. Padovan goto drop; 39240a708f8fSGustavo F. Padovan 39250a708f8fSGustavo F. Padovan if (__is_sar_start(control)) 39260a708f8fSGustavo F. Padovan len -= 2; 39270a708f8fSGustavo F. Padovan 392847d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 39290a708f8fSGustavo F. Padovan len -= 2; 39300a708f8fSGustavo F. Padovan 393147d1ec61SGustavo F. Padovan if (len > chan->mps || len < 0 || __is_sframe(control)) 39320a708f8fSGustavo F. Padovan goto drop; 39330a708f8fSGustavo F. Padovan 39340a708f8fSGustavo F. Padovan tx_seq = __get_txseq(control); 39350a708f8fSGustavo F. Padovan 393642e5c802SGustavo F. Padovan if (chan->expected_tx_seq == tx_seq) 393742e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 39380a708f8fSGustavo F. Padovan else 393942e5c802SGustavo F. Padovan chan->expected_tx_seq = (tx_seq + 1) % 64; 39400a708f8fSGustavo F. Padovan 3941525cd185SGustavo F. Padovan l2cap_streaming_reassembly_sdu(chan, skb, control); 39420a708f8fSGustavo F. Padovan 39430a708f8fSGustavo F. Padovan goto done; 39440a708f8fSGustavo F. Padovan 39450a708f8fSGustavo F. Padovan default: 39460c1bc5c6SGustavo F. Padovan BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode); 39470a708f8fSGustavo F. Padovan break; 39480a708f8fSGustavo F. Padovan } 39490a708f8fSGustavo F. Padovan 39500a708f8fSGustavo F. Padovan drop: 39510a708f8fSGustavo F. Padovan kfree_skb(skb); 39520a708f8fSGustavo F. Padovan 39530a708f8fSGustavo F. Padovan done: 39540a708f8fSGustavo F. Padovan if (sk) 39550a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 39560a708f8fSGustavo F. Padovan 39570a708f8fSGustavo F. Padovan return 0; 39580a708f8fSGustavo F. Padovan } 39590a708f8fSGustavo F. Padovan 39600a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb) 39610a708f8fSGustavo F. Padovan { 39626dcae1eaSDavid S. Miller struct sock *sk = NULL; 396323691d75SGustavo F. Padovan struct l2cap_chan *chan; 39640a708f8fSGustavo F. Padovan 396523691d75SGustavo F. Padovan chan = l2cap_global_chan_by_psm(0, psm, conn->src); 396623691d75SGustavo F. Padovan if (!chan) 39670a708f8fSGustavo F. Padovan goto drop; 39680a708f8fSGustavo F. Padovan 396923691d75SGustavo F. Padovan sk = chan->sk; 397023691d75SGustavo F. Padovan 39710a708f8fSGustavo F. Padovan bh_lock_sock(sk); 39720a708f8fSGustavo F. Padovan 39730a708f8fSGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 39740a708f8fSGustavo F. Padovan 39750a708f8fSGustavo F. Padovan if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) 39760a708f8fSGustavo F. Padovan goto drop; 39770a708f8fSGustavo F. Padovan 39780c1bc5c6SGustavo F. Padovan if (l2cap_pi(sk)->chan->imtu < skb->len) 39790a708f8fSGustavo F. Padovan goto drop; 39800a708f8fSGustavo F. Padovan 39810a708f8fSGustavo F. Padovan if (!sock_queue_rcv_skb(sk, skb)) 39820a708f8fSGustavo F. Padovan goto done; 39830a708f8fSGustavo F. Padovan 39840a708f8fSGustavo F. Padovan drop: 39850a708f8fSGustavo F. Padovan kfree_skb(skb); 39860a708f8fSGustavo F. Padovan 39870a708f8fSGustavo F. Padovan done: 39880a708f8fSGustavo F. Padovan if (sk) 39890a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 39900a708f8fSGustavo F. Padovan return 0; 39910a708f8fSGustavo F. Padovan } 39920a708f8fSGustavo F. Padovan 39939f69bda6SGustavo F. Padovan static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb) 39949f69bda6SGustavo F. Padovan { 39956dcae1eaSDavid S. Miller struct sock *sk = NULL; 399623691d75SGustavo F. Padovan struct l2cap_chan *chan; 39979f69bda6SGustavo F. Padovan 399823691d75SGustavo F. Padovan chan = l2cap_global_chan_by_scid(0, cid, conn->src); 399923691d75SGustavo F. Padovan if (!chan) 40009f69bda6SGustavo F. Padovan goto drop; 40019f69bda6SGustavo F. Padovan 400223691d75SGustavo F. Padovan sk = chan->sk; 400323691d75SGustavo F. Padovan 40049f69bda6SGustavo F. Padovan bh_lock_sock(sk); 40059f69bda6SGustavo F. Padovan 40069f69bda6SGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 40079f69bda6SGustavo F. Padovan 40089f69bda6SGustavo F. Padovan if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) 40099f69bda6SGustavo F. Padovan goto drop; 40109f69bda6SGustavo F. Padovan 40110c1bc5c6SGustavo F. Padovan if (l2cap_pi(sk)->chan->imtu < skb->len) 40129f69bda6SGustavo F. Padovan goto drop; 40139f69bda6SGustavo F. Padovan 40149f69bda6SGustavo F. Padovan if (!sock_queue_rcv_skb(sk, skb)) 40159f69bda6SGustavo F. Padovan goto done; 40169f69bda6SGustavo F. Padovan 40179f69bda6SGustavo F. Padovan drop: 40189f69bda6SGustavo F. Padovan kfree_skb(skb); 40199f69bda6SGustavo F. Padovan 40209f69bda6SGustavo F. Padovan done: 40219f69bda6SGustavo F. Padovan if (sk) 40229f69bda6SGustavo F. Padovan bh_unlock_sock(sk); 40239f69bda6SGustavo F. Padovan return 0; 40249f69bda6SGustavo F. Padovan } 40259f69bda6SGustavo F. Padovan 40260a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) 40270a708f8fSGustavo F. Padovan { 40280a708f8fSGustavo F. Padovan struct l2cap_hdr *lh = (void *) skb->data; 40290a708f8fSGustavo F. Padovan u16 cid, len; 40300a708f8fSGustavo F. Padovan __le16 psm; 40310a708f8fSGustavo F. Padovan 40320a708f8fSGustavo F. Padovan skb_pull(skb, L2CAP_HDR_SIZE); 40330a708f8fSGustavo F. Padovan cid = __le16_to_cpu(lh->cid); 40340a708f8fSGustavo F. Padovan len = __le16_to_cpu(lh->len); 40350a708f8fSGustavo F. Padovan 40360a708f8fSGustavo F. Padovan if (len != skb->len) { 40370a708f8fSGustavo F. Padovan kfree_skb(skb); 40380a708f8fSGustavo F. Padovan return; 40390a708f8fSGustavo F. Padovan } 40400a708f8fSGustavo F. Padovan 40410a708f8fSGustavo F. Padovan BT_DBG("len %d, cid 0x%4.4x", len, cid); 40420a708f8fSGustavo F. Padovan 40430a708f8fSGustavo F. Padovan switch (cid) { 40443300d9a9SClaudio Takahasi case L2CAP_CID_LE_SIGNALING: 40450a708f8fSGustavo F. Padovan case L2CAP_CID_SIGNALING: 40460a708f8fSGustavo F. Padovan l2cap_sig_channel(conn, skb); 40470a708f8fSGustavo F. Padovan break; 40480a708f8fSGustavo F. Padovan 40490a708f8fSGustavo F. Padovan case L2CAP_CID_CONN_LESS: 40500a708f8fSGustavo F. Padovan psm = get_unaligned_le16(skb->data); 40510a708f8fSGustavo F. Padovan skb_pull(skb, 2); 40520a708f8fSGustavo F. Padovan l2cap_conless_channel(conn, psm, skb); 40530a708f8fSGustavo F. Padovan break; 40540a708f8fSGustavo F. Padovan 40559f69bda6SGustavo F. Padovan case L2CAP_CID_LE_DATA: 40569f69bda6SGustavo F. Padovan l2cap_att_channel(conn, cid, skb); 40579f69bda6SGustavo F. Padovan break; 40589f69bda6SGustavo F. Padovan 40590a708f8fSGustavo F. Padovan default: 40600a708f8fSGustavo F. Padovan l2cap_data_channel(conn, cid, skb); 40610a708f8fSGustavo F. Padovan break; 40620a708f8fSGustavo F. Padovan } 40630a708f8fSGustavo F. Padovan } 40640a708f8fSGustavo F. Padovan 40650a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */ 40660a708f8fSGustavo F. Padovan 40670a708f8fSGustavo F. Padovan static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) 40680a708f8fSGustavo F. Padovan { 40690a708f8fSGustavo F. Padovan int exact = 0, lm1 = 0, lm2 = 0; 407023691d75SGustavo F. Padovan struct l2cap_chan *c; 40710a708f8fSGustavo F. Padovan 40720a708f8fSGustavo F. Padovan if (type != ACL_LINK) 40730a708f8fSGustavo F. Padovan return -EINVAL; 40740a708f8fSGustavo F. Padovan 40750a708f8fSGustavo F. Padovan BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); 40760a708f8fSGustavo F. Padovan 40770a708f8fSGustavo F. Padovan /* Find listening sockets and check their link_mode */ 407823691d75SGustavo F. Padovan read_lock(&chan_list_lock); 407923691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 408023691d75SGustavo F. Padovan struct sock *sk = c->sk; 40814343478fSGustavo F. Padovan 40820a708f8fSGustavo F. Padovan if (sk->sk_state != BT_LISTEN) 40830a708f8fSGustavo F. Padovan continue; 40840a708f8fSGustavo F. Padovan 40850a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { 40860a708f8fSGustavo F. Padovan lm1 |= HCI_LM_ACCEPT; 408723691d75SGustavo F. Padovan if (c->role_switch) 40880a708f8fSGustavo F. Padovan lm1 |= HCI_LM_MASTER; 40890a708f8fSGustavo F. Padovan exact++; 40900a708f8fSGustavo F. Padovan } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { 40910a708f8fSGustavo F. Padovan lm2 |= HCI_LM_ACCEPT; 409223691d75SGustavo F. Padovan if (c->role_switch) 40930a708f8fSGustavo F. Padovan lm2 |= HCI_LM_MASTER; 40940a708f8fSGustavo F. Padovan } 40950a708f8fSGustavo F. Padovan } 409623691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 40970a708f8fSGustavo F. Padovan 40980a708f8fSGustavo F. Padovan return exact ? lm1 : lm2; 40990a708f8fSGustavo F. Padovan } 41000a708f8fSGustavo F. Padovan 41010a708f8fSGustavo F. Padovan static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) 41020a708f8fSGustavo F. Padovan { 41030a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 41040a708f8fSGustavo F. Padovan 41050a708f8fSGustavo F. Padovan BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); 41060a708f8fSGustavo F. Padovan 4107acd7d370SVille Tervo if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) 41080a708f8fSGustavo F. Padovan return -EINVAL; 41090a708f8fSGustavo F. Padovan 41100a708f8fSGustavo F. Padovan if (!status) { 41110a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, status); 41120a708f8fSGustavo F. Padovan if (conn) 41130a708f8fSGustavo F. Padovan l2cap_conn_ready(conn); 41140a708f8fSGustavo F. Padovan } else 41150a708f8fSGustavo F. Padovan l2cap_conn_del(hcon, bt_err(status)); 41160a708f8fSGustavo F. Padovan 41170a708f8fSGustavo F. Padovan return 0; 41180a708f8fSGustavo F. Padovan } 41190a708f8fSGustavo F. Padovan 41200a708f8fSGustavo F. Padovan static int l2cap_disconn_ind(struct hci_conn *hcon) 41210a708f8fSGustavo F. Padovan { 41220a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 41230a708f8fSGustavo F. Padovan 41240a708f8fSGustavo F. Padovan BT_DBG("hcon %p", hcon); 41250a708f8fSGustavo F. Padovan 41260a708f8fSGustavo F. Padovan if (hcon->type != ACL_LINK || !conn) 41270a708f8fSGustavo F. Padovan return 0x13; 41280a708f8fSGustavo F. Padovan 41290a708f8fSGustavo F. Padovan return conn->disc_reason; 41300a708f8fSGustavo F. Padovan } 41310a708f8fSGustavo F. Padovan 41320a708f8fSGustavo F. Padovan static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) 41330a708f8fSGustavo F. Padovan { 41340a708f8fSGustavo F. Padovan BT_DBG("hcon %p reason %d", hcon, reason); 41350a708f8fSGustavo F. Padovan 4136acd7d370SVille Tervo if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) 41370a708f8fSGustavo F. Padovan return -EINVAL; 41380a708f8fSGustavo F. Padovan 41390a708f8fSGustavo F. Padovan l2cap_conn_del(hcon, bt_err(reason)); 41400a708f8fSGustavo F. Padovan 41410a708f8fSGustavo F. Padovan return 0; 41420a708f8fSGustavo F. Padovan } 41430a708f8fSGustavo F. Padovan 41444343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) 41450a708f8fSGustavo F. Padovan { 4146715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) 41470a708f8fSGustavo F. Padovan return; 41480a708f8fSGustavo F. Padovan 41490a708f8fSGustavo F. Padovan if (encrypt == 0x00) { 41504343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) { 4151ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 4152ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ * 5); 41534343478fSGustavo F. Padovan } else if (chan->sec_level == BT_SECURITY_HIGH) 41540f852724SGustavo F. Padovan l2cap_chan_close(chan, ECONNREFUSED); 41550a708f8fSGustavo F. Padovan } else { 41564343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) 4157ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 41580a708f8fSGustavo F. Padovan } 41590a708f8fSGustavo F. Padovan } 41600a708f8fSGustavo F. Padovan 41610a708f8fSGustavo F. Padovan static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) 41620a708f8fSGustavo F. Padovan { 41630a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 416448454079SGustavo F. Padovan struct l2cap_chan *chan; 41650a708f8fSGustavo F. Padovan 41660a708f8fSGustavo F. Padovan if (!conn) 41670a708f8fSGustavo F. Padovan return 0; 41680a708f8fSGustavo F. Padovan 41690a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 41700a708f8fSGustavo F. Padovan 4171baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 41720a708f8fSGustavo F. Padovan 4173baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 417448454079SGustavo F. Padovan struct sock *sk = chan->sk; 4175baa7e1faSGustavo F. Padovan 41760a708f8fSGustavo F. Padovan bh_lock_sock(sk); 41770a708f8fSGustavo F. Padovan 4178b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_CONNECT_PEND) { 41790a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 41800a708f8fSGustavo F. Padovan continue; 41810a708f8fSGustavo F. Padovan } 41820a708f8fSGustavo F. Padovan 41830a708f8fSGustavo F. Padovan if (!status && (sk->sk_state == BT_CONNECTED || 41840a708f8fSGustavo F. Padovan sk->sk_state == BT_CONFIG)) { 41854343478fSGustavo F. Padovan l2cap_check_encryption(chan, encrypt); 41860a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 41870a708f8fSGustavo F. Padovan continue; 41880a708f8fSGustavo F. Padovan } 41890a708f8fSGustavo F. Padovan 41900a708f8fSGustavo F. Padovan if (sk->sk_state == BT_CONNECT) { 41910a708f8fSGustavo F. Padovan if (!status) { 41920a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 4193fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 4194fe4128e0SGustavo F. Padovan req.psm = chan->psm; 41950a708f8fSGustavo F. Padovan 4196fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 4197b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 41980a708f8fSGustavo F. Padovan 4199fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 42000a708f8fSGustavo F. Padovan L2CAP_CONN_REQ, sizeof(req), &req); 42010a708f8fSGustavo F. Padovan } else { 4202ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 4203ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 10); 42040a708f8fSGustavo F. Padovan } 42050a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT2) { 42060a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 42070a708f8fSGustavo F. Padovan __u16 result; 42080a708f8fSGustavo F. Padovan 42090a708f8fSGustavo F. Padovan if (!status) { 42100a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 42110a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 42120a708f8fSGustavo F. Padovan } else { 42130a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 4214ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 10); 42150a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 42160a708f8fSGustavo F. Padovan } 42170a708f8fSGustavo F. Padovan 4218fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4219fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 42200a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 42210a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 4222fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4223fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 42240a708f8fSGustavo F. Padovan } 42250a708f8fSGustavo F. Padovan 42260a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 42270a708f8fSGustavo F. Padovan } 42280a708f8fSGustavo F. Padovan 4229baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 42300a708f8fSGustavo F. Padovan 42310a708f8fSGustavo F. Padovan return 0; 42320a708f8fSGustavo F. Padovan } 42330a708f8fSGustavo F. Padovan 42340a708f8fSGustavo F. Padovan static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) 42350a708f8fSGustavo F. Padovan { 42360a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 42370a708f8fSGustavo F. Padovan 42380a708f8fSGustavo F. Padovan if (!conn) 42390a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 42400a708f8fSGustavo F. Padovan 42410a708f8fSGustavo F. Padovan if (!conn) 42420a708f8fSGustavo F. Padovan goto drop; 42430a708f8fSGustavo F. Padovan 42440a708f8fSGustavo F. Padovan BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); 42450a708f8fSGustavo F. Padovan 42460a708f8fSGustavo F. Padovan if (!(flags & ACL_CONT)) { 42470a708f8fSGustavo F. Padovan struct l2cap_hdr *hdr; 424848454079SGustavo F. Padovan struct l2cap_chan *chan; 42490a708f8fSGustavo F. Padovan u16 cid; 42500a708f8fSGustavo F. Padovan int len; 42510a708f8fSGustavo F. Padovan 42520a708f8fSGustavo F. Padovan if (conn->rx_len) { 42530a708f8fSGustavo F. Padovan BT_ERR("Unexpected start frame (len %d)", skb->len); 42540a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 42550a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 42560a708f8fSGustavo F. Padovan conn->rx_len = 0; 42570a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42580a708f8fSGustavo F. Padovan } 42590a708f8fSGustavo F. Padovan 42600a708f8fSGustavo F. Padovan /* Start fragment always begin with Basic L2CAP header */ 42610a708f8fSGustavo F. Padovan if (skb->len < L2CAP_HDR_SIZE) { 42620a708f8fSGustavo F. Padovan BT_ERR("Frame is too short (len %d)", skb->len); 42630a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42640a708f8fSGustavo F. Padovan goto drop; 42650a708f8fSGustavo F. Padovan } 42660a708f8fSGustavo F. Padovan 42670a708f8fSGustavo F. Padovan hdr = (struct l2cap_hdr *) skb->data; 42680a708f8fSGustavo F. Padovan len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; 42690a708f8fSGustavo F. Padovan cid = __le16_to_cpu(hdr->cid); 42700a708f8fSGustavo F. Padovan 42710a708f8fSGustavo F. Padovan if (len == skb->len) { 42720a708f8fSGustavo F. Padovan /* Complete frame received */ 42730a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, skb); 42740a708f8fSGustavo F. Padovan return 0; 42750a708f8fSGustavo F. Padovan } 42760a708f8fSGustavo F. Padovan 42770a708f8fSGustavo F. Padovan BT_DBG("Start: total len %d, frag len %d", len, skb->len); 42780a708f8fSGustavo F. Padovan 42790a708f8fSGustavo F. Padovan if (skb->len > len) { 42800a708f8fSGustavo F. Padovan BT_ERR("Frame is too long (len %d, expected len %d)", 42810a708f8fSGustavo F. Padovan skb->len, len); 42820a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42830a708f8fSGustavo F. Padovan goto drop; 42840a708f8fSGustavo F. Padovan } 42850a708f8fSGustavo F. Padovan 4286baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 42870a708f8fSGustavo F. Padovan 428848454079SGustavo F. Padovan if (chan && chan->sk) { 428948454079SGustavo F. Padovan struct sock *sk = chan->sk; 429048454079SGustavo F. Padovan 42910c1bc5c6SGustavo F. Padovan if (chan->imtu < len - L2CAP_HDR_SIZE) { 429248454079SGustavo F. Padovan BT_ERR("Frame exceeding recv MTU (len %d, " 429348454079SGustavo F. Padovan "MTU %d)", len, 42940c1bc5c6SGustavo F. Padovan chan->imtu); 42950a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 42960a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42970a708f8fSGustavo F. Padovan goto drop; 42980a708f8fSGustavo F. Padovan } 42990a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 430048454079SGustavo F. Padovan } 43010a708f8fSGustavo F. Padovan 43020a708f8fSGustavo F. Padovan /* Allocate skb for the complete frame (with header) */ 43030a708f8fSGustavo F. Padovan conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); 43040a708f8fSGustavo F. Padovan if (!conn->rx_skb) 43050a708f8fSGustavo F. Padovan goto drop; 43060a708f8fSGustavo F. Padovan 43070a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 43080a708f8fSGustavo F. Padovan skb->len); 43090a708f8fSGustavo F. Padovan conn->rx_len = len - skb->len; 43100a708f8fSGustavo F. Padovan } else { 43110a708f8fSGustavo F. Padovan BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); 43120a708f8fSGustavo F. Padovan 43130a708f8fSGustavo F. Padovan if (!conn->rx_len) { 43140a708f8fSGustavo F. Padovan BT_ERR("Unexpected continuation frame (len %d)", skb->len); 43150a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 43160a708f8fSGustavo F. Padovan goto drop; 43170a708f8fSGustavo F. Padovan } 43180a708f8fSGustavo F. Padovan 43190a708f8fSGustavo F. Padovan if (skb->len > conn->rx_len) { 43200a708f8fSGustavo F. Padovan BT_ERR("Fragment is too long (len %d, expected %d)", 43210a708f8fSGustavo F. Padovan skb->len, conn->rx_len); 43220a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 43230a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 43240a708f8fSGustavo F. Padovan conn->rx_len = 0; 43250a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 43260a708f8fSGustavo F. Padovan goto drop; 43270a708f8fSGustavo F. Padovan } 43280a708f8fSGustavo F. Padovan 43290a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 43300a708f8fSGustavo F. Padovan skb->len); 43310a708f8fSGustavo F. Padovan conn->rx_len -= skb->len; 43320a708f8fSGustavo F. Padovan 43330a708f8fSGustavo F. Padovan if (!conn->rx_len) { 43340a708f8fSGustavo F. Padovan /* Complete frame received */ 43350a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, conn->rx_skb); 43360a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 43370a708f8fSGustavo F. Padovan } 43380a708f8fSGustavo F. Padovan } 43390a708f8fSGustavo F. Padovan 43400a708f8fSGustavo F. Padovan drop: 43410a708f8fSGustavo F. Padovan kfree_skb(skb); 43420a708f8fSGustavo F. Padovan return 0; 43430a708f8fSGustavo F. Padovan } 43440a708f8fSGustavo F. Padovan 43450a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p) 43460a708f8fSGustavo F. Padovan { 434723691d75SGustavo F. Padovan struct l2cap_chan *c; 43480a708f8fSGustavo F. Padovan 434923691d75SGustavo F. Padovan read_lock_bh(&chan_list_lock); 43500a708f8fSGustavo F. Padovan 435123691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 435223691d75SGustavo F. Padovan struct sock *sk = c->sk; 43530a708f8fSGustavo F. Padovan 4354903d343eSGustavo F. Padovan seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", 43550a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->src), 43560a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->dst), 435723691d75SGustavo F. Padovan sk->sk_state, __le16_to_cpu(c->psm), 435823691d75SGustavo F. Padovan c->scid, c->dcid, c->imtu, c->omtu, 435923691d75SGustavo F. Padovan c->sec_level, c->mode); 43600a708f8fSGustavo F. Padovan } 43610a708f8fSGustavo F. Padovan 436223691d75SGustavo F. Padovan read_unlock_bh(&chan_list_lock); 43630a708f8fSGustavo F. Padovan 43640a708f8fSGustavo F. Padovan return 0; 43650a708f8fSGustavo F. Padovan } 43660a708f8fSGustavo F. Padovan 43670a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file) 43680a708f8fSGustavo F. Padovan { 43690a708f8fSGustavo F. Padovan return single_open(file, l2cap_debugfs_show, inode->i_private); 43700a708f8fSGustavo F. Padovan } 43710a708f8fSGustavo F. Padovan 43720a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = { 43730a708f8fSGustavo F. Padovan .open = l2cap_debugfs_open, 43740a708f8fSGustavo F. Padovan .read = seq_read, 43750a708f8fSGustavo F. Padovan .llseek = seq_lseek, 43760a708f8fSGustavo F. Padovan .release = single_release, 43770a708f8fSGustavo F. Padovan }; 43780a708f8fSGustavo F. Padovan 43790a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs; 43800a708f8fSGustavo F. Padovan 43810a708f8fSGustavo F. Padovan static struct hci_proto l2cap_hci_proto = { 43820a708f8fSGustavo F. Padovan .name = "L2CAP", 43830a708f8fSGustavo F. Padovan .id = HCI_PROTO_L2CAP, 43840a708f8fSGustavo F. Padovan .connect_ind = l2cap_connect_ind, 43850a708f8fSGustavo F. Padovan .connect_cfm = l2cap_connect_cfm, 43860a708f8fSGustavo F. Padovan .disconn_ind = l2cap_disconn_ind, 43870a708f8fSGustavo F. Padovan .disconn_cfm = l2cap_disconn_cfm, 43880a708f8fSGustavo F. Padovan .security_cfm = l2cap_security_cfm, 43890a708f8fSGustavo F. Padovan .recv_acldata = l2cap_recv_acldata 43900a708f8fSGustavo F. Padovan }; 43910a708f8fSGustavo F. Padovan 439264274518SGustavo F. Padovan int __init l2cap_init(void) 43930a708f8fSGustavo F. Padovan { 43940a708f8fSGustavo F. Padovan int err; 43950a708f8fSGustavo F. Padovan 4396bb58f747SGustavo F. Padovan err = l2cap_init_sockets(); 43970a708f8fSGustavo F. Padovan if (err < 0) 43980a708f8fSGustavo F. Padovan return err; 43990a708f8fSGustavo F. Padovan 44000a708f8fSGustavo F. Padovan _busy_wq = create_singlethread_workqueue("l2cap"); 44010a708f8fSGustavo F. Padovan if (!_busy_wq) { 4402bb58f747SGustavo F. Padovan err = -ENOMEM; 44030a708f8fSGustavo F. Padovan goto error; 44040a708f8fSGustavo F. Padovan } 44050a708f8fSGustavo F. Padovan 44060a708f8fSGustavo F. Padovan err = hci_register_proto(&l2cap_hci_proto); 44070a708f8fSGustavo F. Padovan if (err < 0) { 44080a708f8fSGustavo F. Padovan BT_ERR("L2CAP protocol registration failed"); 44090a708f8fSGustavo F. Padovan bt_sock_unregister(BTPROTO_L2CAP); 44100a708f8fSGustavo F. Padovan goto error; 44110a708f8fSGustavo F. Padovan } 44120a708f8fSGustavo F. Padovan 44130a708f8fSGustavo F. Padovan if (bt_debugfs) { 44140a708f8fSGustavo F. Padovan l2cap_debugfs = debugfs_create_file("l2cap", 0444, 44150a708f8fSGustavo F. Padovan bt_debugfs, NULL, &l2cap_debugfs_fops); 44160a708f8fSGustavo F. Padovan if (!l2cap_debugfs) 44170a708f8fSGustavo F. Padovan BT_ERR("Failed to create L2CAP debug file"); 44180a708f8fSGustavo F. Padovan } 44190a708f8fSGustavo F. Padovan 44200a708f8fSGustavo F. Padovan return 0; 44210a708f8fSGustavo F. Padovan 44220a708f8fSGustavo F. Padovan error: 44230a708f8fSGustavo F. Padovan destroy_workqueue(_busy_wq); 4424bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 44250a708f8fSGustavo F. Padovan return err; 44260a708f8fSGustavo F. Padovan } 44270a708f8fSGustavo F. Padovan 442864274518SGustavo F. Padovan void l2cap_exit(void) 44290a708f8fSGustavo F. Padovan { 44300a708f8fSGustavo F. Padovan debugfs_remove(l2cap_debugfs); 44310a708f8fSGustavo F. Padovan 44320a708f8fSGustavo F. Padovan flush_workqueue(_busy_wq); 44330a708f8fSGustavo F. Padovan destroy_workqueue(_busy_wq); 44340a708f8fSGustavo F. Padovan 44350a708f8fSGustavo F. Padovan if (hci_unregister_proto(&l2cap_hci_proto) < 0) 44360a708f8fSGustavo F. Padovan BT_ERR("L2CAP protocol unregistration failed"); 44370a708f8fSGustavo F. Padovan 4438bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 44390a708f8fSGustavo F. Padovan } 44400a708f8fSGustavo F. Padovan 44410a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644); 44420a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); 4443