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 6523691d75SGustavo F. Padovan LIST_HEAD(chan_list); 6623691d75SGustavo F. Padovan 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 50368983259SGustavo F. Padovan 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 5400a708f8fSGustavo F. Padovan hci_send_acl(conn->hcon, skb, flags); 5410a708f8fSGustavo F. Padovan } 5420a708f8fSGustavo F. Padovan 543525cd185SGustavo F. Padovan static inline void l2cap_send_sframe(struct l2cap_chan *chan, u16 control) 5440a708f8fSGustavo F. Padovan { 5450a708f8fSGustavo F. Padovan struct sk_buff *skb; 5460a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 547525cd185SGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(chan->sk); 5488c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 5490a708f8fSGustavo F. Padovan struct sock *sk = (struct sock *)pi; 5500a708f8fSGustavo F. Padovan int count, hlen = L2CAP_HDR_SIZE + 2; 5510a708f8fSGustavo F. Padovan u8 flags; 5520a708f8fSGustavo F. Padovan 5530a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 5540a708f8fSGustavo F. Padovan return; 5550a708f8fSGustavo F. Padovan 55647d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 5570a708f8fSGustavo F. Padovan hlen += 2; 5580a708f8fSGustavo F. Padovan 55949208c9cSGustavo F. Padovan BT_DBG("chan %p, control 0x%2.2x", chan, control); 5600a708f8fSGustavo F. Padovan 5610a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, hlen); 5620a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FRAME_TYPE; 5630a708f8fSGustavo F. Padovan 564525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 5650a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 566525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 5670a708f8fSGustavo F. Padovan } 5680a708f8fSGustavo F. Padovan 569525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_PBIT) { 5700a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_POLL; 571525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_PBIT; 5720a708f8fSGustavo F. Padovan } 5730a708f8fSGustavo F. Padovan 5740a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 5750a708f8fSGustavo F. Padovan if (!skb) 5760a708f8fSGustavo F. Padovan return; 5770a708f8fSGustavo F. Padovan 5780a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 5790a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); 580fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 5810a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb_put(skb, 2)); 5820a708f8fSGustavo F. Padovan 58347d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 5840a708f8fSGustavo F. Padovan u16 fcs = crc16(0, (u8 *)lh, count - 2); 5850a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb_put(skb, 2)); 5860a708f8fSGustavo F. Padovan } 5870a708f8fSGustavo F. Padovan 5880a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 5890a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 5900a708f8fSGustavo F. Padovan else 5910a708f8fSGustavo F. Padovan flags = ACL_START; 5920a708f8fSGustavo F. Padovan 5938c1d787bSGustavo F. Padovan hci_send_acl(chan->conn->hcon, skb, flags); 5940a708f8fSGustavo F. Padovan } 5950a708f8fSGustavo F. Padovan 596525cd185SGustavo F. Padovan static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u16 control) 5970a708f8fSGustavo F. Padovan { 598525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 5990a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 600525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 6010a708f8fSGustavo F. Padovan } else 6020a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 6030a708f8fSGustavo F. Padovan 60442e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 6050a708f8fSGustavo F. Padovan 606525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 6070a708f8fSGustavo F. Padovan } 6080a708f8fSGustavo F. Padovan 609b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) 6100a708f8fSGustavo F. Padovan { 611b4450035SGustavo F. Padovan return !(chan->conf_state & L2CAP_CONF_CONNECT_PEND); 6120a708f8fSGustavo F. Padovan } 6130a708f8fSGustavo F. Padovan 614fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan) 6150a708f8fSGustavo F. Padovan { 6168c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 6170a708f8fSGustavo F. Padovan 6180a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { 6190a708f8fSGustavo F. Padovan if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) 6200a708f8fSGustavo F. Padovan return; 6210a708f8fSGustavo F. Padovan 6224343478fSGustavo F. Padovan if (l2cap_check_security(chan) && 6234343478fSGustavo F. Padovan __l2cap_no_conn_pending(chan)) { 6240a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 625fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 626fe4128e0SGustavo F. Padovan req.psm = chan->psm; 6270a708f8fSGustavo F. Padovan 628fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 629b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 6300a708f8fSGustavo F. Padovan 631fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 632fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 6330a708f8fSGustavo F. Padovan } 6340a708f8fSGustavo F. Padovan } else { 6350a708f8fSGustavo F. Padovan struct l2cap_info_req req; 6360a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 6370a708f8fSGustavo F. Padovan 6380a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 6390a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 6400a708f8fSGustavo F. Padovan 6410a708f8fSGustavo F. Padovan mod_timer(&conn->info_timer, jiffies + 6420a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 6430a708f8fSGustavo F. Padovan 6440a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 6450a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 6460a708f8fSGustavo F. Padovan } 6470a708f8fSGustavo F. Padovan } 6480a708f8fSGustavo F. Padovan 6490a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) 6500a708f8fSGustavo F. Padovan { 6510a708f8fSGustavo F. Padovan u32 local_feat_mask = l2cap_feat_mask; 6520a708f8fSGustavo F. Padovan if (!disable_ertm) 6530a708f8fSGustavo F. Padovan local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; 6540a708f8fSGustavo F. Padovan 6550a708f8fSGustavo F. Padovan switch (mode) { 6560a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 6570a708f8fSGustavo F. Padovan return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; 6580a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 6590a708f8fSGustavo F. Padovan return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; 6600a708f8fSGustavo F. Padovan default: 6610a708f8fSGustavo F. Padovan return 0x00; 6620a708f8fSGustavo F. Padovan } 6630a708f8fSGustavo F. Padovan } 6640a708f8fSGustavo F. Padovan 6654519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err) 6660a708f8fSGustavo F. Padovan { 667e92c8e70SGustavo F. Padovan struct sock *sk; 6680a708f8fSGustavo F. Padovan struct l2cap_disconn_req req; 6690a708f8fSGustavo F. Padovan 6700a708f8fSGustavo F. Padovan if (!conn) 6710a708f8fSGustavo F. Padovan return; 6720a708f8fSGustavo F. Padovan 673e92c8e70SGustavo F. Padovan sk = chan->sk; 674e92c8e70SGustavo F. Padovan 6750c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 676e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 677e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 678e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 6790a708f8fSGustavo F. Padovan } 6800a708f8fSGustavo F. Padovan 681fe4128e0SGustavo F. Padovan req.dcid = cpu_to_le16(chan->dcid); 682fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 6830a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 6840a708f8fSGustavo F. Padovan L2CAP_DISCONN_REQ, sizeof(req), &req); 6850a708f8fSGustavo F. Padovan 6860a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 6870a708f8fSGustavo F. Padovan sk->sk_err = err; 6880a708f8fSGustavo F. Padovan } 6890a708f8fSGustavo F. Padovan 6900a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */ 6910a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn) 6920a708f8fSGustavo F. Padovan { 693820ffdb3SGustavo F. Padovan struct l2cap_chan *chan, *tmp; 6940a708f8fSGustavo F. Padovan 6950a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 6960a708f8fSGustavo F. Padovan 697baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 6980a708f8fSGustavo F. Padovan 699820ffdb3SGustavo F. Padovan list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { 70048454079SGustavo F. Padovan struct sock *sk = chan->sk; 701baa7e1faSGustavo F. Padovan 7020a708f8fSGustavo F. Padovan bh_lock_sock(sk); 7030a708f8fSGustavo F. Padovan 704715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 7050a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7060a708f8fSGustavo F. Padovan continue; 7070a708f8fSGustavo F. Padovan } 7080a708f8fSGustavo F. Padovan 7090a708f8fSGustavo F. Padovan if (sk->sk_state == BT_CONNECT) { 7100a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 7110a708f8fSGustavo F. Padovan 7124343478fSGustavo F. Padovan if (!l2cap_check_security(chan) || 713b4450035SGustavo F. Padovan !__l2cap_no_conn_pending(chan)) { 7140a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7150a708f8fSGustavo F. Padovan continue; 7160a708f8fSGustavo F. Padovan } 7170a708f8fSGustavo F. Padovan 7180c1bc5c6SGustavo F. Padovan if (!l2cap_mode_supported(chan->mode, 7190a708f8fSGustavo F. Padovan conn->feat_mask) 720b4450035SGustavo F. Padovan && chan->conf_state & 7210a708f8fSGustavo F. Padovan L2CAP_CONF_STATE2_DEVICE) { 7220f852724SGustavo F. Padovan /* l2cap_chan_close() calls list_del(chan) 723820ffdb3SGustavo F. Padovan * so release the lock */ 724820ffdb3SGustavo F. Padovan read_unlock_bh(&conn->chan_lock); 7250f852724SGustavo F. Padovan l2cap_chan_close(chan, ECONNRESET); 726820ffdb3SGustavo F. Padovan read_lock_bh(&conn->chan_lock); 7270a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7280a708f8fSGustavo F. Padovan continue; 7290a708f8fSGustavo F. Padovan } 7300a708f8fSGustavo F. Padovan 731fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 732fe4128e0SGustavo F. Padovan req.psm = chan->psm; 7330a708f8fSGustavo F. Padovan 734fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 735b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 7360a708f8fSGustavo F. Padovan 737fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 738fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 7390a708f8fSGustavo F. Padovan 7400a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT2) { 7410a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 7420a708f8fSGustavo F. Padovan char buf[128]; 743fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 744fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 7450a708f8fSGustavo F. Padovan 7464343478fSGustavo F. Padovan if (l2cap_check_security(chan)) { 7470a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 7480a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 7490a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 7500a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); 7510a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 7520a708f8fSGustavo F. Padovan 7530a708f8fSGustavo F. Padovan } else { 7540a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 7550a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 7560a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 7570a708f8fSGustavo F. Padovan } 7580a708f8fSGustavo F. Padovan } else { 7590a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 7600a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); 7610a708f8fSGustavo F. Padovan } 7620a708f8fSGustavo F. Padovan 763fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 764fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 7650a708f8fSGustavo F. Padovan 766b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_REQ_SENT || 7670a708f8fSGustavo F. Padovan rsp.result != L2CAP_CR_SUCCESS) { 7680a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7690a708f8fSGustavo F. Padovan continue; 7700a708f8fSGustavo F. Padovan } 7710a708f8fSGustavo F. Padovan 772b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 7730a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 77473ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 77573ffa904SGustavo F. Padovan chan->num_conf_req++; 7760a708f8fSGustavo F. Padovan } 7770a708f8fSGustavo F. Padovan 7780a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7790a708f8fSGustavo F. Padovan } 7800a708f8fSGustavo F. Padovan 781baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 7820a708f8fSGustavo F. Padovan } 7830a708f8fSGustavo F. Padovan 784b62f328bSVille Tervo /* Find socket with cid and source bdaddr. 785b62f328bSVille Tervo * Returns closest match, locked. 786b62f328bSVille Tervo */ 78723691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src) 788b62f328bSVille Tervo { 78923691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 790b62f328bSVille Tervo 79123691d75SGustavo F. Padovan read_lock(&chan_list_lock); 792b62f328bSVille Tervo 79323691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 79423691d75SGustavo F. Padovan struct sock *sk = c->sk; 795fe4128e0SGustavo F. Padovan 796b62f328bSVille Tervo if (state && sk->sk_state != state) 797b62f328bSVille Tervo continue; 798b62f328bSVille Tervo 79923691d75SGustavo F. Padovan if (c->scid == cid) { 800b62f328bSVille Tervo /* Exact match. */ 80123691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 80223691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 80323691d75SGustavo F. Padovan return c; 80423691d75SGustavo F. Padovan } 805b62f328bSVille Tervo 806b62f328bSVille Tervo /* Closest match */ 807b62f328bSVille Tervo if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 80823691d75SGustavo F. Padovan c1 = c; 809b62f328bSVille Tervo } 810b62f328bSVille Tervo } 811280f294fSGustavo F. Padovan 81223691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 813b62f328bSVille Tervo 81423691d75SGustavo F. Padovan return c1; 815b62f328bSVille Tervo } 816b62f328bSVille Tervo 817b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn) 818b62f328bSVille Tervo { 819c916fbe4SGustavo F. Padovan struct sock *parent, *sk; 82023691d75SGustavo F. Padovan struct l2cap_chan *chan, *pchan; 821b62f328bSVille Tervo 822b62f328bSVille Tervo BT_DBG(""); 823b62f328bSVille Tervo 824b62f328bSVille Tervo /* Check if we have socket listening on cid */ 82523691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, 826b62f328bSVille Tervo conn->src); 82723691d75SGustavo F. Padovan if (!pchan) 828b62f328bSVille Tervo return; 829b62f328bSVille Tervo 83023691d75SGustavo F. Padovan parent = pchan->sk; 83123691d75SGustavo F. Padovan 83262f3a2cfSGustavo F. Padovan bh_lock_sock(parent); 83362f3a2cfSGustavo F. Padovan 834b62f328bSVille Tervo /* Check for backlog size */ 835b62f328bSVille Tervo if (sk_acceptq_is_full(parent)) { 836b62f328bSVille Tervo BT_DBG("backlog full %d", parent->sk_ack_backlog); 837b62f328bSVille Tervo goto clean; 838b62f328bSVille Tervo } 839b62f328bSVille Tervo 840b62f328bSVille Tervo sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC); 841b62f328bSVille Tervo if (!sk) 842b62f328bSVille Tervo goto clean; 843b62f328bSVille Tervo 84423691d75SGustavo F. Padovan chan = l2cap_chan_create(sk); 84548454079SGustavo F. Padovan if (!chan) { 84648454079SGustavo F. Padovan l2cap_sock_kill(sk); 84748454079SGustavo F. Padovan goto clean; 84848454079SGustavo F. Padovan } 84948454079SGustavo F. Padovan 8505d41ce1dSGustavo F. Padovan l2cap_pi(sk)->chan = chan; 8515d41ce1dSGustavo F. Padovan 852baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 853b62f328bSVille Tervo 854b62f328bSVille Tervo hci_conn_hold(conn->hcon); 855b62f328bSVille Tervo 856b62f328bSVille Tervo l2cap_sock_init(sk, parent); 857baa7e1faSGustavo F. Padovan 858b62f328bSVille Tervo bacpy(&bt_sk(sk)->src, conn->src); 859b62f328bSVille Tervo bacpy(&bt_sk(sk)->dst, conn->dst); 860b62f328bSVille Tervo 861d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 862d1010240SGustavo F. Padovan 86348454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 86448454079SGustavo F. Padovan 865ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, sk->sk_sndtimeo); 866b62f328bSVille Tervo 867b62f328bSVille Tervo sk->sk_state = BT_CONNECTED; 868b62f328bSVille Tervo parent->sk_data_ready(parent, 0); 869b62f328bSVille Tervo 870baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 871b62f328bSVille Tervo 872b62f328bSVille Tervo clean: 873b62f328bSVille Tervo bh_unlock_sock(parent); 874b62f328bSVille Tervo } 875b62f328bSVille Tervo 8760a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn) 8770a708f8fSGustavo F. Padovan { 87848454079SGustavo F. Padovan struct l2cap_chan *chan; 8790a708f8fSGustavo F. Padovan 8800a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 8810a708f8fSGustavo F. Padovan 882b62f328bSVille Tervo if (!conn->hcon->out && conn->hcon->type == LE_LINK) 883b62f328bSVille Tervo l2cap_le_conn_ready(conn); 884b62f328bSVille Tervo 885baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 8860a708f8fSGustavo F. Padovan 887baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 88848454079SGustavo F. Padovan struct sock *sk = chan->sk; 889baa7e1faSGustavo F. Padovan 8900a708f8fSGustavo F. Padovan bh_lock_sock(sk); 8910a708f8fSGustavo F. Padovan 892acd7d370SVille Tervo if (conn->hcon->type == LE_LINK) { 893ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 894acd7d370SVille Tervo sk->sk_state = BT_CONNECTED; 895acd7d370SVille Tervo sk->sk_state_change(sk); 896acd7d370SVille Tervo } 897acd7d370SVille Tervo 898715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 899ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 9000a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 9010a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 9020a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT) 903fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 9040a708f8fSGustavo F. Padovan 9050a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9060a708f8fSGustavo F. Padovan } 9070a708f8fSGustavo F. Padovan 908baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 9090a708f8fSGustavo F. Padovan } 9100a708f8fSGustavo F. Padovan 9110a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */ 9120a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) 9130a708f8fSGustavo F. Padovan { 91448454079SGustavo F. Padovan struct l2cap_chan *chan; 9150a708f8fSGustavo F. Padovan 9160a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 9170a708f8fSGustavo F. Padovan 918baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 9190a708f8fSGustavo F. Padovan 920baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 92148454079SGustavo F. Padovan struct sock *sk = chan->sk; 922baa7e1faSGustavo F. Padovan 9234343478fSGustavo F. Padovan if (chan->force_reliable) 9240a708f8fSGustavo F. Padovan sk->sk_err = err; 9250a708f8fSGustavo F. Padovan } 9260a708f8fSGustavo F. Padovan 927baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 9280a708f8fSGustavo F. Padovan } 9290a708f8fSGustavo F. Padovan 9300a708f8fSGustavo F. Padovan static void l2cap_info_timeout(unsigned long arg) 9310a708f8fSGustavo F. Padovan { 9320a708f8fSGustavo F. Padovan struct l2cap_conn *conn = (void *) arg; 9330a708f8fSGustavo F. Padovan 9340a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 9350a708f8fSGustavo F. Padovan conn->info_ident = 0; 9360a708f8fSGustavo F. Padovan 9370a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 9380a708f8fSGustavo F. Padovan } 9390a708f8fSGustavo F. Padovan 9400a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) 9410a708f8fSGustavo F. Padovan { 9420a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 9430a708f8fSGustavo F. Padovan 9440a708f8fSGustavo F. Padovan if (conn || status) 9450a708f8fSGustavo F. Padovan return conn; 9460a708f8fSGustavo F. Padovan 9470a708f8fSGustavo F. Padovan conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); 9480a708f8fSGustavo F. Padovan if (!conn) 9490a708f8fSGustavo F. Padovan return NULL; 9500a708f8fSGustavo F. Padovan 9510a708f8fSGustavo F. Padovan hcon->l2cap_data = conn; 9520a708f8fSGustavo F. Padovan conn->hcon = hcon; 9530a708f8fSGustavo F. Padovan 9540a708f8fSGustavo F. Padovan BT_DBG("hcon %p conn %p", hcon, conn); 9550a708f8fSGustavo F. Padovan 956acd7d370SVille Tervo if (hcon->hdev->le_mtu && hcon->type == LE_LINK) 957acd7d370SVille Tervo conn->mtu = hcon->hdev->le_mtu; 958acd7d370SVille Tervo else 9590a708f8fSGustavo F. Padovan conn->mtu = hcon->hdev->acl_mtu; 960acd7d370SVille Tervo 9610a708f8fSGustavo F. Padovan conn->src = &hcon->hdev->bdaddr; 9620a708f8fSGustavo F. Padovan conn->dst = &hcon->dst; 9630a708f8fSGustavo F. Padovan 9640a708f8fSGustavo F. Padovan conn->feat_mask = 0; 9650a708f8fSGustavo F. Padovan 9660a708f8fSGustavo F. Padovan spin_lock_init(&conn->lock); 967baa7e1faSGustavo F. Padovan rwlock_init(&conn->chan_lock); 968baa7e1faSGustavo F. Padovan 969baa7e1faSGustavo F. Padovan INIT_LIST_HEAD(&conn->chan_l); 9700a708f8fSGustavo F. Padovan 971b62f328bSVille Tervo if (hcon->type != LE_LINK) 9720a708f8fSGustavo F. Padovan setup_timer(&conn->info_timer, l2cap_info_timeout, 9730a708f8fSGustavo F. Padovan (unsigned long) conn); 9740a708f8fSGustavo F. Padovan 9750a708f8fSGustavo F. Padovan conn->disc_reason = 0x13; 9760a708f8fSGustavo F. Padovan 9770a708f8fSGustavo F. Padovan return conn; 9780a708f8fSGustavo F. Padovan } 9790a708f8fSGustavo F. Padovan 9800a708f8fSGustavo F. Padovan static void l2cap_conn_del(struct hci_conn *hcon, int err) 9810a708f8fSGustavo F. Padovan { 9820a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 983baa7e1faSGustavo F. Padovan struct l2cap_chan *chan, *l; 9840a708f8fSGustavo F. Padovan struct sock *sk; 9850a708f8fSGustavo F. Padovan 9860a708f8fSGustavo F. Padovan if (!conn) 9870a708f8fSGustavo F. Padovan return; 9880a708f8fSGustavo F. Padovan 9890a708f8fSGustavo F. Padovan BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); 9900a708f8fSGustavo F. Padovan 9910a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 9920a708f8fSGustavo F. Padovan 9930a708f8fSGustavo F. Padovan /* Kill channels */ 994baa7e1faSGustavo F. Padovan list_for_each_entry_safe(chan, l, &conn->chan_l, list) { 99548454079SGustavo F. Padovan sk = chan->sk; 9960a708f8fSGustavo F. Padovan bh_lock_sock(sk); 99748454079SGustavo F. Padovan l2cap_chan_del(chan, err); 9980a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9990a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 10000a708f8fSGustavo F. Padovan } 10010a708f8fSGustavo F. Padovan 10020a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) 10030a708f8fSGustavo F. Padovan del_timer_sync(&conn->info_timer); 10040a708f8fSGustavo F. Padovan 10050a708f8fSGustavo F. Padovan hcon->l2cap_data = NULL; 10060a708f8fSGustavo F. Padovan kfree(conn); 10070a708f8fSGustavo F. Padovan } 10080a708f8fSGustavo F. Padovan 100948454079SGustavo F. Padovan static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 10100a708f8fSGustavo F. Padovan { 1011baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 101248454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 1013baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 10140a708f8fSGustavo F. Padovan } 10150a708f8fSGustavo F. Padovan 10160a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */ 10170a708f8fSGustavo F. Padovan 10180a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr. 10190a708f8fSGustavo F. Padovan * Returns closest match. 10200a708f8fSGustavo F. Padovan */ 102123691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src) 10220a708f8fSGustavo F. Padovan { 102323691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 10240a708f8fSGustavo F. Padovan 102523691d75SGustavo F. Padovan read_lock(&chan_list_lock); 10260a708f8fSGustavo F. Padovan 102723691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 102823691d75SGustavo F. Padovan struct sock *sk = c->sk; 1029fe4128e0SGustavo F. Padovan 10300a708f8fSGustavo F. Padovan if (state && sk->sk_state != state) 10310a708f8fSGustavo F. Padovan continue; 10320a708f8fSGustavo F. Padovan 103323691d75SGustavo F. Padovan if (c->psm == psm) { 10340a708f8fSGustavo F. Padovan /* Exact match. */ 103523691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 1036a7567b20SJohannes Berg read_unlock(&chan_list_lock); 103723691d75SGustavo F. Padovan return c; 103823691d75SGustavo F. Padovan } 10390a708f8fSGustavo F. Padovan 10400a708f8fSGustavo F. Padovan /* Closest match */ 10410a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 104223691d75SGustavo F. Padovan c1 = c; 10430a708f8fSGustavo F. Padovan } 10440a708f8fSGustavo F. Padovan } 10450a708f8fSGustavo F. Padovan 104623691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 10470a708f8fSGustavo F. Padovan 104823691d75SGustavo F. Padovan return c1; 10490a708f8fSGustavo F. Padovan } 10500a708f8fSGustavo F. Padovan 105177a74c7eSGustavo F. Padovan int l2cap_chan_connect(struct l2cap_chan *chan) 10520a708f8fSGustavo F. Padovan { 10535d41ce1dSGustavo F. Padovan struct sock *sk = chan->sk; 10540a708f8fSGustavo F. Padovan bdaddr_t *src = &bt_sk(sk)->src; 10550a708f8fSGustavo F. Padovan bdaddr_t *dst = &bt_sk(sk)->dst; 10560a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 10570a708f8fSGustavo F. Padovan struct hci_conn *hcon; 10580a708f8fSGustavo F. Padovan struct hci_dev *hdev; 10590a708f8fSGustavo F. Padovan __u8 auth_type; 10600a708f8fSGustavo F. Padovan int err; 10610a708f8fSGustavo F. Padovan 10620a708f8fSGustavo F. Padovan BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), 1063fe4128e0SGustavo F. Padovan chan->psm); 10640a708f8fSGustavo F. Padovan 10650a708f8fSGustavo F. Padovan hdev = hci_get_route(dst, src); 10660a708f8fSGustavo F. Padovan if (!hdev) 10670a708f8fSGustavo F. Padovan return -EHOSTUNREACH; 10680a708f8fSGustavo F. Padovan 10690a708f8fSGustavo F. Padovan hci_dev_lock_bh(hdev); 10700a708f8fSGustavo F. Padovan 10714343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 10720a708f8fSGustavo F. Padovan 1073fe4128e0SGustavo F. Padovan if (chan->dcid == L2CAP_CID_LE_DATA) 1074acd7d370SVille Tervo hcon = hci_connect(hdev, LE_LINK, dst, 10754343478fSGustavo F. Padovan chan->sec_level, auth_type); 1076acd7d370SVille Tervo else 10770a708f8fSGustavo F. Padovan hcon = hci_connect(hdev, ACL_LINK, dst, 10784343478fSGustavo F. Padovan chan->sec_level, auth_type); 1079acd7d370SVille Tervo 108030e76272SVille Tervo if (IS_ERR(hcon)) { 108130e76272SVille Tervo err = PTR_ERR(hcon); 10820a708f8fSGustavo F. Padovan goto done; 108330e76272SVille Tervo } 10840a708f8fSGustavo F. Padovan 10850a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 10860a708f8fSGustavo F. Padovan if (!conn) { 10870a708f8fSGustavo F. Padovan hci_conn_put(hcon); 108830e76272SVille Tervo err = -ENOMEM; 10890a708f8fSGustavo F. Padovan goto done; 10900a708f8fSGustavo F. Padovan } 10910a708f8fSGustavo F. Padovan 10920a708f8fSGustavo F. Padovan /* Update source addr of the socket */ 10930a708f8fSGustavo F. Padovan bacpy(src, conn->src); 10940a708f8fSGustavo F. Padovan 109548454079SGustavo F. Padovan l2cap_chan_add(conn, chan); 109648454079SGustavo F. Padovan 10970a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT; 1098ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, sk->sk_sndtimeo); 10990a708f8fSGustavo F. Padovan 11000a708f8fSGustavo F. Padovan if (hcon->state == BT_CONNECTED) { 1101715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 1102ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 11034343478fSGustavo F. Padovan if (l2cap_check_security(chan)) 11040a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 11050a708f8fSGustavo F. Padovan } else 1106fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 11070a708f8fSGustavo F. Padovan } 11080a708f8fSGustavo F. Padovan 110930e76272SVille Tervo err = 0; 111030e76272SVille Tervo 11110a708f8fSGustavo F. Padovan done: 11120a708f8fSGustavo F. Padovan hci_dev_unlock_bh(hdev); 11130a708f8fSGustavo F. Padovan hci_dev_put(hdev); 11140a708f8fSGustavo F. Padovan return err; 11150a708f8fSGustavo F. Padovan } 11160a708f8fSGustavo F. Padovan 1117dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk) 11180a708f8fSGustavo F. Padovan { 11198c1d787bSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 11200a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 11210a708f8fSGustavo F. Padovan int err = 0; 11220a708f8fSGustavo F. Padovan int timeo = HZ/5; 11230a708f8fSGustavo F. Padovan 11240a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 11258c1d787bSGustavo F. Padovan while ((chan->unacked_frames > 0 && chan->conn)) { 11260a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 11270a708f8fSGustavo F. Padovan 11280a708f8fSGustavo F. Padovan if (!timeo) 11290a708f8fSGustavo F. Padovan timeo = HZ/5; 11300a708f8fSGustavo F. Padovan 11310a708f8fSGustavo F. Padovan if (signal_pending(current)) { 11320a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 11330a708f8fSGustavo F. Padovan break; 11340a708f8fSGustavo F. Padovan } 11350a708f8fSGustavo F. Padovan 11360a708f8fSGustavo F. Padovan release_sock(sk); 11370a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 11380a708f8fSGustavo F. Padovan lock_sock(sk); 11390a708f8fSGustavo F. Padovan 11400a708f8fSGustavo F. Padovan err = sock_error(sk); 11410a708f8fSGustavo F. Padovan if (err) 11420a708f8fSGustavo F. Padovan break; 11430a708f8fSGustavo F. Padovan } 11440a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 11450a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 11460a708f8fSGustavo F. Padovan return err; 11470a708f8fSGustavo F. Padovan } 11480a708f8fSGustavo F. Padovan 11490a708f8fSGustavo F. Padovan static void l2cap_monitor_timeout(unsigned long arg) 11500a708f8fSGustavo F. Padovan { 1151525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 1152525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 11530a708f8fSGustavo F. Padovan 1154525cd185SGustavo F. Padovan BT_DBG("chan %p", chan); 11550a708f8fSGustavo F. Padovan 11560a708f8fSGustavo F. Padovan bh_lock_sock(sk); 11572c03a7a4SGustavo F. Padovan if (chan->retry_count >= chan->remote_max_tx) { 11588c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 11590a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 11600a708f8fSGustavo F. Padovan return; 11610a708f8fSGustavo F. Padovan } 11620a708f8fSGustavo F. Padovan 11636a026610SGustavo F. Padovan chan->retry_count++; 11640a708f8fSGustavo F. Padovan __mod_monitor_timer(); 11650a708f8fSGustavo F. Padovan 1166525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 11670a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 11680a708f8fSGustavo F. Padovan } 11690a708f8fSGustavo F. Padovan 11700a708f8fSGustavo F. Padovan static void l2cap_retrans_timeout(unsigned long arg) 11710a708f8fSGustavo F. Padovan { 1172525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 1173525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 11740a708f8fSGustavo F. Padovan 117549208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 11760a708f8fSGustavo F. Padovan 11770a708f8fSGustavo F. Padovan bh_lock_sock(sk); 11786a026610SGustavo F. Padovan chan->retry_count = 1; 11790a708f8fSGustavo F. Padovan __mod_monitor_timer(); 11800a708f8fSGustavo F. Padovan 1181525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_WAIT_F; 11820a708f8fSGustavo F. Padovan 1183525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 11840a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 11850a708f8fSGustavo F. Padovan } 11860a708f8fSGustavo F. Padovan 118742e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan) 11880a708f8fSGustavo F. Padovan { 11890a708f8fSGustavo F. Padovan struct sk_buff *skb; 11900a708f8fSGustavo F. Padovan 119158d35f87SGustavo F. Padovan while ((skb = skb_peek(&chan->tx_q)) && 11926a026610SGustavo F. Padovan chan->unacked_frames) { 119342e5c802SGustavo F. Padovan if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) 11940a708f8fSGustavo F. Padovan break; 11950a708f8fSGustavo F. Padovan 119658d35f87SGustavo F. Padovan skb = skb_dequeue(&chan->tx_q); 11970a708f8fSGustavo F. Padovan kfree_skb(skb); 11980a708f8fSGustavo F. Padovan 11996a026610SGustavo F. Padovan chan->unacked_frames--; 12000a708f8fSGustavo F. Padovan } 12010a708f8fSGustavo F. Padovan 12026a026610SGustavo F. Padovan if (!chan->unacked_frames) 1203e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 12040a708f8fSGustavo F. Padovan } 12050a708f8fSGustavo F. Padovan 12064343478fSGustavo F. Padovan void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) 12070a708f8fSGustavo F. Padovan { 12088c1d787bSGustavo F. Padovan struct hci_conn *hcon = chan->conn->hcon; 12090a708f8fSGustavo F. Padovan u16 flags; 12100a708f8fSGustavo F. Padovan 12114343478fSGustavo F. Padovan BT_DBG("chan %p, skb %p len %d", chan, skb, skb->len); 12120a708f8fSGustavo F. Padovan 12134343478fSGustavo F. Padovan if (!chan->flushable && lmp_no_flush_capable(hcon->hdev)) 12140a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 12150a708f8fSGustavo F. Padovan else 12160a708f8fSGustavo F. Padovan flags = ACL_START; 12170a708f8fSGustavo F. Padovan 12180a708f8fSGustavo F. Padovan hci_send_acl(hcon, skb, flags); 12190a708f8fSGustavo F. Padovan } 12200a708f8fSGustavo F. Padovan 122142e5c802SGustavo F. Padovan void l2cap_streaming_send(struct l2cap_chan *chan) 12220a708f8fSGustavo F. Padovan { 12230a708f8fSGustavo F. Padovan struct sk_buff *skb; 12240a708f8fSGustavo F. Padovan u16 control, fcs; 12250a708f8fSGustavo F. Padovan 122658d35f87SGustavo F. Padovan while ((skb = skb_dequeue(&chan->tx_q))) { 12270a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data + L2CAP_HDR_SIZE); 122842e5c802SGustavo F. Padovan control |= chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT; 12290a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb->data + L2CAP_HDR_SIZE); 12300a708f8fSGustavo F. Padovan 123147d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 12320a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)skb->data, skb->len - 2); 12330a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb->data + skb->len - 2); 12340a708f8fSGustavo F. Padovan } 12350a708f8fSGustavo F. Padovan 12364343478fSGustavo F. Padovan l2cap_do_send(chan, skb); 12370a708f8fSGustavo F. Padovan 123842e5c802SGustavo F. Padovan chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; 12390a708f8fSGustavo F. Padovan } 12400a708f8fSGustavo F. Padovan } 12410a708f8fSGustavo F. Padovan 1242525cd185SGustavo F. Padovan static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq) 12430a708f8fSGustavo F. Padovan { 12440a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 12450a708f8fSGustavo F. Padovan u16 control, fcs; 12460a708f8fSGustavo F. Padovan 124758d35f87SGustavo F. Padovan skb = skb_peek(&chan->tx_q); 12480a708f8fSGustavo F. Padovan if (!skb) 12490a708f8fSGustavo F. Padovan return; 12500a708f8fSGustavo F. Padovan 12510a708f8fSGustavo F. Padovan do { 12520a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq == tx_seq) 12530a708f8fSGustavo F. Padovan break; 12540a708f8fSGustavo F. Padovan 125558d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 12560a708f8fSGustavo F. Padovan return; 12570a708f8fSGustavo F. Padovan 125858d35f87SGustavo F. Padovan } while ((skb = skb_queue_next(&chan->tx_q, skb))); 12590a708f8fSGustavo F. Padovan 12602c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 12612c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 12628c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 12630a708f8fSGustavo F. Padovan return; 12640a708f8fSGustavo F. Padovan } 12650a708f8fSGustavo F. Padovan 12660a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 12670a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 12680a708f8fSGustavo F. Padovan control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); 1269a429b519SRuiyi Zhang control &= L2CAP_CTRL_SAR; 12700a708f8fSGustavo F. Padovan 1271525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 12720a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 1273525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 12740a708f8fSGustavo F. Padovan } 12750a708f8fSGustavo F. Padovan 127642e5c802SGustavo F. Padovan control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT) 12770a708f8fSGustavo F. Padovan | (tx_seq << L2CAP_CTRL_TXSEQ_SHIFT); 12780a708f8fSGustavo F. Padovan 12790a708f8fSGustavo F. Padovan put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); 12800a708f8fSGustavo F. Padovan 128147d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 12820a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)tx_skb->data, tx_skb->len - 2); 12830a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, tx_skb->data + tx_skb->len - 2); 12840a708f8fSGustavo F. Padovan } 12850a708f8fSGustavo F. Padovan 12864343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 12870a708f8fSGustavo F. Padovan } 12880a708f8fSGustavo F. Padovan 1289525cd185SGustavo F. Padovan int l2cap_ertm_send(struct l2cap_chan *chan) 12900a708f8fSGustavo F. Padovan { 12910a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 1292525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 12930a708f8fSGustavo F. Padovan u16 control, fcs; 12940a708f8fSGustavo F. Padovan int nsent = 0; 12950a708f8fSGustavo F. Padovan 12960a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 12970a708f8fSGustavo F. Padovan return -ENOTCONN; 12980a708f8fSGustavo F. Padovan 129958d35f87SGustavo F. Padovan while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) { 13000a708f8fSGustavo F. Padovan 13012c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 13022c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 13038c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 13040a708f8fSGustavo F. Padovan break; 13050a708f8fSGustavo F. Padovan } 13060a708f8fSGustavo F. Padovan 13070a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 13080a708f8fSGustavo F. Padovan 13090a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 13100a708f8fSGustavo F. Padovan 13110a708f8fSGustavo F. Padovan control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); 13120a708f8fSGustavo F. Padovan control &= L2CAP_CTRL_SAR; 13130a708f8fSGustavo F. Padovan 1314525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 13150a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 1316525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 13170a708f8fSGustavo F. Padovan } 131842e5c802SGustavo F. Padovan control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT) 131942e5c802SGustavo F. Padovan | (chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT); 13200a708f8fSGustavo F. Padovan put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); 13210a708f8fSGustavo F. Padovan 13220a708f8fSGustavo F. Padovan 132347d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 13240a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2); 13250a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb->data + tx_skb->len - 2); 13260a708f8fSGustavo F. Padovan } 13270a708f8fSGustavo F. Padovan 13284343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 13290a708f8fSGustavo F. Padovan 13300a708f8fSGustavo F. Padovan __mod_retrans_timer(); 13310a708f8fSGustavo F. Padovan 133242e5c802SGustavo F. Padovan bt_cb(skb)->tx_seq = chan->next_tx_seq; 133342e5c802SGustavo F. Padovan chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; 13340a708f8fSGustavo F. Padovan 133523e9fde2SSuraj Sumangala if (bt_cb(skb)->retries == 1) 13366a026610SGustavo F. Padovan chan->unacked_frames++; 133723e9fde2SSuraj Sumangala 13386a026610SGustavo F. Padovan chan->frames_sent++; 13390a708f8fSGustavo F. Padovan 134058d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 134158d35f87SGustavo F. Padovan chan->tx_send_head = NULL; 13420a708f8fSGustavo F. Padovan else 134358d35f87SGustavo F. Padovan chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); 13440a708f8fSGustavo F. Padovan 13450a708f8fSGustavo F. Padovan nsent++; 13460a708f8fSGustavo F. Padovan } 13470a708f8fSGustavo F. Padovan 13480a708f8fSGustavo F. Padovan return nsent; 13490a708f8fSGustavo F. Padovan } 13500a708f8fSGustavo F. Padovan 1351525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan) 13520a708f8fSGustavo F. Padovan { 13530a708f8fSGustavo F. Padovan int ret; 13540a708f8fSGustavo F. Padovan 135558d35f87SGustavo F. Padovan if (!skb_queue_empty(&chan->tx_q)) 135658d35f87SGustavo F. Padovan chan->tx_send_head = chan->tx_q.next; 13570a708f8fSGustavo F. Padovan 135842e5c802SGustavo F. Padovan chan->next_tx_seq = chan->expected_ack_seq; 1359525cd185SGustavo F. Padovan ret = l2cap_ertm_send(chan); 13600a708f8fSGustavo F. Padovan return ret; 13610a708f8fSGustavo F. Padovan } 13620a708f8fSGustavo F. Padovan 1363525cd185SGustavo F. Padovan static void l2cap_send_ack(struct l2cap_chan *chan) 13640a708f8fSGustavo F. Padovan { 13650a708f8fSGustavo F. Padovan u16 control = 0; 13660a708f8fSGustavo F. Padovan 136742e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 13680a708f8fSGustavo F. Padovan 1369525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 13700a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 1371525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 1372525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 13730a708f8fSGustavo F. Padovan return; 13740a708f8fSGustavo F. Padovan } 13750a708f8fSGustavo F. Padovan 1376525cd185SGustavo F. Padovan if (l2cap_ertm_send(chan) > 0) 13770a708f8fSGustavo F. Padovan return; 13780a708f8fSGustavo F. Padovan 13790a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 1380525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 13810a708f8fSGustavo F. Padovan } 13820a708f8fSGustavo F. Padovan 1383525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan) 13840a708f8fSGustavo F. Padovan { 13850a708f8fSGustavo F. Padovan struct srej_list *tail; 13860a708f8fSGustavo F. Padovan u16 control; 13870a708f8fSGustavo F. Padovan 13880a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 13890a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 13900a708f8fSGustavo F. Padovan 139139d5a3eeSGustavo F. Padovan tail = list_entry((&chan->srej_l)->prev, struct srej_list, list); 13920a708f8fSGustavo F. Padovan control |= tail->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 13930a708f8fSGustavo F. Padovan 1394525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 13950a708f8fSGustavo F. Padovan } 13960a708f8fSGustavo F. Padovan 13970a708f8fSGustavo F. Padovan static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb) 13980a708f8fSGustavo F. Padovan { 13998c1d787bSGustavo F. Padovan struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn; 14000a708f8fSGustavo F. Padovan struct sk_buff **frag; 14010a708f8fSGustavo F. Padovan int err, sent = 0; 14020a708f8fSGustavo F. Padovan 14030a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) 14040a708f8fSGustavo F. Padovan return -EFAULT; 14050a708f8fSGustavo F. Padovan 14060a708f8fSGustavo F. Padovan sent += count; 14070a708f8fSGustavo F. Padovan len -= count; 14080a708f8fSGustavo F. Padovan 14090a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 14100a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 14110a708f8fSGustavo F. Padovan while (len) { 14120a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 14130a708f8fSGustavo F. Padovan 14140a708f8fSGustavo F. Padovan *frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err); 14150a708f8fSGustavo F. Padovan if (!*frag) 14160a708f8fSGustavo F. Padovan return err; 14170a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) 14180a708f8fSGustavo F. Padovan return -EFAULT; 14190a708f8fSGustavo F. Padovan 14200a708f8fSGustavo F. Padovan sent += count; 14210a708f8fSGustavo F. Padovan len -= count; 14220a708f8fSGustavo F. Padovan 14230a708f8fSGustavo F. Padovan frag = &(*frag)->next; 14240a708f8fSGustavo F. Padovan } 14250a708f8fSGustavo F. Padovan 14260a708f8fSGustavo F. Padovan return sent; 14270a708f8fSGustavo F. Padovan } 14280a708f8fSGustavo F. Padovan 1429fe4128e0SGustavo F. Padovan struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 14300a708f8fSGustavo F. Padovan { 1431fe4128e0SGustavo F. Padovan struct sock *sk = chan->sk; 14328c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 14330a708f8fSGustavo F. Padovan struct sk_buff *skb; 14340a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE + 2; 14350a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 14360a708f8fSGustavo F. Padovan 14370a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 14380a708f8fSGustavo F. Padovan 14390a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 14400a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 14410a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 14420a708f8fSGustavo F. Padovan if (!skb) 14430a708f8fSGustavo F. Padovan return ERR_PTR(err); 14440a708f8fSGustavo F. Padovan 14450a708f8fSGustavo F. Padovan /* Create L2CAP header */ 14460a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1447fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 14480a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 1449fe4128e0SGustavo F. Padovan put_unaligned_le16(chan->psm, skb_put(skb, 2)); 14500a708f8fSGustavo F. Padovan 14510a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 14520a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 14530a708f8fSGustavo F. Padovan kfree_skb(skb); 14540a708f8fSGustavo F. Padovan return ERR_PTR(err); 14550a708f8fSGustavo F. Padovan } 14560a708f8fSGustavo F. Padovan return skb; 14570a708f8fSGustavo F. Padovan } 14580a708f8fSGustavo F. Padovan 1459fe4128e0SGustavo F. Padovan struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 14600a708f8fSGustavo F. Padovan { 1461fe4128e0SGustavo F. Padovan struct sock *sk = chan->sk; 14628c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 14630a708f8fSGustavo F. Padovan struct sk_buff *skb; 14640a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE; 14650a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 14660a708f8fSGustavo F. Padovan 14670a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 14680a708f8fSGustavo F. Padovan 14690a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 14700a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 14710a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 14720a708f8fSGustavo F. Padovan if (!skb) 14730a708f8fSGustavo F. Padovan return ERR_PTR(err); 14740a708f8fSGustavo F. Padovan 14750a708f8fSGustavo F. Padovan /* Create L2CAP header */ 14760a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1477fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 14780a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 14790a708f8fSGustavo F. Padovan 14800a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 14810a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 14820a708f8fSGustavo F. Padovan kfree_skb(skb); 14830a708f8fSGustavo F. Padovan return ERR_PTR(err); 14840a708f8fSGustavo F. Padovan } 14850a708f8fSGustavo F. Padovan return skb; 14860a708f8fSGustavo F. Padovan } 14870a708f8fSGustavo F. Padovan 148847d1ec61SGustavo F. Padovan struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len, u16 control, u16 sdulen) 14890a708f8fSGustavo F. Padovan { 149047d1ec61SGustavo F. Padovan struct sock *sk = chan->sk; 14918c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 14920a708f8fSGustavo F. Padovan struct sk_buff *skb; 14930a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE + 2; 14940a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 14950a708f8fSGustavo F. Padovan 14960a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 14970a708f8fSGustavo F. Padovan 14980a708f8fSGustavo F. Padovan if (!conn) 14990a708f8fSGustavo F. Padovan return ERR_PTR(-ENOTCONN); 15000a708f8fSGustavo F. Padovan 15010a708f8fSGustavo F. Padovan if (sdulen) 15020a708f8fSGustavo F. Padovan hlen += 2; 15030a708f8fSGustavo F. Padovan 150447d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 15050a708f8fSGustavo F. Padovan hlen += 2; 15060a708f8fSGustavo F. Padovan 15070a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 15080a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 15090a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 15100a708f8fSGustavo F. Padovan if (!skb) 15110a708f8fSGustavo F. Padovan return ERR_PTR(err); 15120a708f8fSGustavo F. Padovan 15130a708f8fSGustavo F. Padovan /* Create L2CAP header */ 15140a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1515fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 15160a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 15170a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb_put(skb, 2)); 15180a708f8fSGustavo F. Padovan if (sdulen) 15190a708f8fSGustavo F. Padovan put_unaligned_le16(sdulen, skb_put(skb, 2)); 15200a708f8fSGustavo F. Padovan 15210a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 15220a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 15230a708f8fSGustavo F. Padovan kfree_skb(skb); 15240a708f8fSGustavo F. Padovan return ERR_PTR(err); 15250a708f8fSGustavo F. Padovan } 15260a708f8fSGustavo F. Padovan 152747d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 15280a708f8fSGustavo F. Padovan put_unaligned_le16(0, skb_put(skb, 2)); 15290a708f8fSGustavo F. Padovan 15300a708f8fSGustavo F. Padovan bt_cb(skb)->retries = 0; 15310a708f8fSGustavo F. Padovan return skb; 15320a708f8fSGustavo F. Padovan } 15330a708f8fSGustavo F. Padovan 15342c03a7a4SGustavo F. Padovan int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 15350a708f8fSGustavo F. Padovan { 15360a708f8fSGustavo F. Padovan struct sk_buff *skb; 15370a708f8fSGustavo F. Padovan struct sk_buff_head sar_queue; 15380a708f8fSGustavo F. Padovan u16 control; 15390a708f8fSGustavo F. Padovan size_t size = 0; 15400a708f8fSGustavo F. Padovan 15410a708f8fSGustavo F. Padovan skb_queue_head_init(&sar_queue); 15420a708f8fSGustavo F. Padovan control = L2CAP_SDU_START; 154347d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len); 15440a708f8fSGustavo F. Padovan if (IS_ERR(skb)) 15450a708f8fSGustavo F. Padovan return PTR_ERR(skb); 15460a708f8fSGustavo F. Padovan 15470a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 15482c03a7a4SGustavo F. Padovan len -= chan->remote_mps; 15492c03a7a4SGustavo F. Padovan size += chan->remote_mps; 15500a708f8fSGustavo F. Padovan 15510a708f8fSGustavo F. Padovan while (len > 0) { 15520a708f8fSGustavo F. Padovan size_t buflen; 15530a708f8fSGustavo F. Padovan 15542c03a7a4SGustavo F. Padovan if (len > chan->remote_mps) { 15550a708f8fSGustavo F. Padovan control = L2CAP_SDU_CONTINUE; 15562c03a7a4SGustavo F. Padovan buflen = chan->remote_mps; 15570a708f8fSGustavo F. Padovan } else { 15580a708f8fSGustavo F. Padovan control = L2CAP_SDU_END; 15590a708f8fSGustavo F. Padovan buflen = len; 15600a708f8fSGustavo F. Padovan } 15610a708f8fSGustavo F. Padovan 156247d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0); 15630a708f8fSGustavo F. Padovan if (IS_ERR(skb)) { 15640a708f8fSGustavo F. Padovan skb_queue_purge(&sar_queue); 15650a708f8fSGustavo F. Padovan return PTR_ERR(skb); 15660a708f8fSGustavo F. Padovan } 15670a708f8fSGustavo F. Padovan 15680a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 15690a708f8fSGustavo F. Padovan len -= buflen; 15700a708f8fSGustavo F. Padovan size += buflen; 15710a708f8fSGustavo F. Padovan } 157258d35f87SGustavo F. Padovan skb_queue_splice_tail(&sar_queue, &chan->tx_q); 157358d35f87SGustavo F. Padovan if (chan->tx_send_head == NULL) 157458d35f87SGustavo F. Padovan chan->tx_send_head = sar_queue.next; 15750a708f8fSGustavo F. Padovan 15760a708f8fSGustavo F. Padovan return size; 15770a708f8fSGustavo F. Padovan } 15780a708f8fSGustavo F. Padovan 15799a91a04aSGustavo F. Padovan int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 15809a91a04aSGustavo F. Padovan { 15819a91a04aSGustavo F. Padovan struct sk_buff *skb; 15829a91a04aSGustavo F. Padovan u16 control; 15839a91a04aSGustavo F. Padovan int err; 15849a91a04aSGustavo F. Padovan 15859a91a04aSGustavo F. Padovan /* Connectionless channel */ 1586715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { 15879a91a04aSGustavo F. Padovan skb = l2cap_create_connless_pdu(chan, msg, len); 15889a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 15899a91a04aSGustavo F. Padovan return PTR_ERR(skb); 15909a91a04aSGustavo F. Padovan 15919a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 15929a91a04aSGustavo F. Padovan return len; 15939a91a04aSGustavo F. Padovan } 15949a91a04aSGustavo F. Padovan 15959a91a04aSGustavo F. Padovan switch (chan->mode) { 15969a91a04aSGustavo F. Padovan case L2CAP_MODE_BASIC: 15979a91a04aSGustavo F. Padovan /* Check outgoing MTU */ 15989a91a04aSGustavo F. Padovan if (len > chan->omtu) 15999a91a04aSGustavo F. Padovan return -EMSGSIZE; 16009a91a04aSGustavo F. Padovan 16019a91a04aSGustavo F. Padovan /* Create a basic PDU */ 16029a91a04aSGustavo F. Padovan skb = l2cap_create_basic_pdu(chan, msg, len); 16039a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 16049a91a04aSGustavo F. Padovan return PTR_ERR(skb); 16059a91a04aSGustavo F. Padovan 16069a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 16079a91a04aSGustavo F. Padovan err = len; 16089a91a04aSGustavo F. Padovan break; 16099a91a04aSGustavo F. Padovan 16109a91a04aSGustavo F. Padovan case L2CAP_MODE_ERTM: 16119a91a04aSGustavo F. Padovan case L2CAP_MODE_STREAMING: 16129a91a04aSGustavo F. Padovan /* Entire SDU fits into one PDU */ 16139a91a04aSGustavo F. Padovan if (len <= chan->remote_mps) { 16149a91a04aSGustavo F. Padovan control = L2CAP_SDU_UNSEGMENTED; 16159a91a04aSGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, len, control, 16169a91a04aSGustavo F. Padovan 0); 16179a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 16189a91a04aSGustavo F. Padovan return PTR_ERR(skb); 16199a91a04aSGustavo F. Padovan 16209a91a04aSGustavo F. Padovan __skb_queue_tail(&chan->tx_q, skb); 16219a91a04aSGustavo F. Padovan 16229a91a04aSGustavo F. Padovan if (chan->tx_send_head == NULL) 16239a91a04aSGustavo F. Padovan chan->tx_send_head = skb; 16249a91a04aSGustavo F. Padovan 16259a91a04aSGustavo F. Padovan } else { 16269a91a04aSGustavo F. Padovan /* Segment SDU into multiples PDUs */ 16279a91a04aSGustavo F. Padovan err = l2cap_sar_segment_sdu(chan, msg, len); 16289a91a04aSGustavo F. Padovan if (err < 0) 16299a91a04aSGustavo F. Padovan return err; 16309a91a04aSGustavo F. Padovan } 16319a91a04aSGustavo F. Padovan 16329a91a04aSGustavo F. Padovan if (chan->mode == L2CAP_MODE_STREAMING) { 16339a91a04aSGustavo F. Padovan l2cap_streaming_send(chan); 16349a91a04aSGustavo F. Padovan err = len; 16359a91a04aSGustavo F. Padovan break; 16369a91a04aSGustavo F. Padovan } 16379a91a04aSGustavo F. Padovan 16389a91a04aSGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && 16399a91a04aSGustavo F. Padovan (chan->conn_state & L2CAP_CONN_WAIT_F)) { 16409a91a04aSGustavo F. Padovan err = len; 16419a91a04aSGustavo F. Padovan break; 16429a91a04aSGustavo F. Padovan } 16439a91a04aSGustavo F. Padovan 16449a91a04aSGustavo F. Padovan err = l2cap_ertm_send(chan); 16459a91a04aSGustavo F. Padovan if (err >= 0) 16469a91a04aSGustavo F. Padovan err = len; 16479a91a04aSGustavo F. Padovan 16489a91a04aSGustavo F. Padovan break; 16499a91a04aSGustavo F. Padovan 16509a91a04aSGustavo F. Padovan default: 16519a91a04aSGustavo F. Padovan BT_DBG("bad state %1.1x", chan->mode); 16529a91a04aSGustavo F. Padovan err = -EBADFD; 16539a91a04aSGustavo F. Padovan } 16549a91a04aSGustavo F. Padovan 16559a91a04aSGustavo F. Padovan return err; 16569a91a04aSGustavo F. Padovan } 16579a91a04aSGustavo F. Padovan 16580a708f8fSGustavo F. Padovan static void l2cap_chan_ready(struct sock *sk) 16590a708f8fSGustavo F. Padovan { 16600a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 1661b4450035SGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 16620a708f8fSGustavo F. Padovan 16630a708f8fSGustavo F. Padovan BT_DBG("sk %p, parent %p", sk, parent); 16640a708f8fSGustavo F. Padovan 1665b4450035SGustavo F. Padovan chan->conf_state = 0; 1666ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 16670a708f8fSGustavo F. Padovan 16680a708f8fSGustavo F. Padovan if (!parent) { 16690a708f8fSGustavo F. Padovan /* Outgoing channel. 16700a708f8fSGustavo F. Padovan * Wake up socket sleeping on connect. 16710a708f8fSGustavo F. Padovan */ 16720a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 16730a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 16740a708f8fSGustavo F. Padovan } else { 16750a708f8fSGustavo F. Padovan /* Incoming channel. 16760a708f8fSGustavo F. Padovan * Wake up socket sleeping on accept. 16770a708f8fSGustavo F. Padovan */ 16780a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 16790a708f8fSGustavo F. Padovan } 16800a708f8fSGustavo F. Padovan } 16810a708f8fSGustavo F. Padovan 16820a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */ 16830a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) 16840a708f8fSGustavo F. Padovan { 16850a708f8fSGustavo F. Padovan struct sk_buff *nskb; 168648454079SGustavo F. Padovan struct l2cap_chan *chan; 16870a708f8fSGustavo F. Padovan 16880a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 16890a708f8fSGustavo F. Padovan 1690baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 1691baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 169248454079SGustavo F. Padovan struct sock *sk = chan->sk; 1693715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_RAW) 16940a708f8fSGustavo F. Padovan continue; 16950a708f8fSGustavo F. Padovan 16960a708f8fSGustavo F. Padovan /* Don't send frame to the socket it came from */ 16970a708f8fSGustavo F. Padovan if (skb->sk == sk) 16980a708f8fSGustavo F. Padovan continue; 16990a708f8fSGustavo F. Padovan nskb = skb_clone(skb, GFP_ATOMIC); 17000a708f8fSGustavo F. Padovan if (!nskb) 17010a708f8fSGustavo F. Padovan continue; 17020a708f8fSGustavo F. Padovan 17030a708f8fSGustavo F. Padovan if (sock_queue_rcv_skb(sk, nskb)) 17040a708f8fSGustavo F. Padovan kfree_skb(nskb); 17050a708f8fSGustavo F. Padovan } 1706baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 17070a708f8fSGustavo F. Padovan } 17080a708f8fSGustavo F. Padovan 17090a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */ 17100a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 17110a708f8fSGustavo F. Padovan u8 code, u8 ident, u16 dlen, void *data) 17120a708f8fSGustavo F. Padovan { 17130a708f8fSGustavo F. Padovan struct sk_buff *skb, **frag; 17140a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr *cmd; 17150a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 17160a708f8fSGustavo F. Padovan int len, count; 17170a708f8fSGustavo F. Padovan 17180a708f8fSGustavo F. Padovan BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", 17190a708f8fSGustavo F. Padovan conn, code, ident, dlen); 17200a708f8fSGustavo F. Padovan 17210a708f8fSGustavo F. Padovan len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; 17220a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 17230a708f8fSGustavo F. Padovan 17240a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 17250a708f8fSGustavo F. Padovan if (!skb) 17260a708f8fSGustavo F. Padovan return NULL; 17270a708f8fSGustavo F. Padovan 17280a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 17290a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); 17303300d9a9SClaudio Takahasi 17313300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 17323300d9a9SClaudio Takahasi lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); 17333300d9a9SClaudio Takahasi else 17340a708f8fSGustavo F. Padovan lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); 17350a708f8fSGustavo F. Padovan 17360a708f8fSGustavo F. Padovan cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); 17370a708f8fSGustavo F. Padovan cmd->code = code; 17380a708f8fSGustavo F. Padovan cmd->ident = ident; 17390a708f8fSGustavo F. Padovan cmd->len = cpu_to_le16(dlen); 17400a708f8fSGustavo F. Padovan 17410a708f8fSGustavo F. Padovan if (dlen) { 17420a708f8fSGustavo F. Padovan count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; 17430a708f8fSGustavo F. Padovan memcpy(skb_put(skb, count), data, count); 17440a708f8fSGustavo F. Padovan data += count; 17450a708f8fSGustavo F. Padovan } 17460a708f8fSGustavo F. Padovan 17470a708f8fSGustavo F. Padovan len -= skb->len; 17480a708f8fSGustavo F. Padovan 17490a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 17500a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 17510a708f8fSGustavo F. Padovan while (len) { 17520a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 17530a708f8fSGustavo F. Padovan 17540a708f8fSGustavo F. Padovan *frag = bt_skb_alloc(count, GFP_ATOMIC); 17550a708f8fSGustavo F. Padovan if (!*frag) 17560a708f8fSGustavo F. Padovan goto fail; 17570a708f8fSGustavo F. Padovan 17580a708f8fSGustavo F. Padovan memcpy(skb_put(*frag, count), data, count); 17590a708f8fSGustavo F. Padovan 17600a708f8fSGustavo F. Padovan len -= count; 17610a708f8fSGustavo F. Padovan data += count; 17620a708f8fSGustavo F. Padovan 17630a708f8fSGustavo F. Padovan frag = &(*frag)->next; 17640a708f8fSGustavo F. Padovan } 17650a708f8fSGustavo F. Padovan 17660a708f8fSGustavo F. Padovan return skb; 17670a708f8fSGustavo F. Padovan 17680a708f8fSGustavo F. Padovan fail: 17690a708f8fSGustavo F. Padovan kfree_skb(skb); 17700a708f8fSGustavo F. Padovan return NULL; 17710a708f8fSGustavo F. Padovan } 17720a708f8fSGustavo F. Padovan 17730a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) 17740a708f8fSGustavo F. Padovan { 17750a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 17760a708f8fSGustavo F. Padovan int len; 17770a708f8fSGustavo F. Padovan 17780a708f8fSGustavo F. Padovan len = L2CAP_CONF_OPT_SIZE + opt->len; 17790a708f8fSGustavo F. Padovan *ptr += len; 17800a708f8fSGustavo F. Padovan 17810a708f8fSGustavo F. Padovan *type = opt->type; 17820a708f8fSGustavo F. Padovan *olen = opt->len; 17830a708f8fSGustavo F. Padovan 17840a708f8fSGustavo F. Padovan switch (opt->len) { 17850a708f8fSGustavo F. Padovan case 1: 17860a708f8fSGustavo F. Padovan *val = *((u8 *) opt->val); 17870a708f8fSGustavo F. Padovan break; 17880a708f8fSGustavo F. Padovan 17890a708f8fSGustavo F. Padovan case 2: 17900a708f8fSGustavo F. Padovan *val = get_unaligned_le16(opt->val); 17910a708f8fSGustavo F. Padovan break; 17920a708f8fSGustavo F. Padovan 17930a708f8fSGustavo F. Padovan case 4: 17940a708f8fSGustavo F. Padovan *val = get_unaligned_le32(opt->val); 17950a708f8fSGustavo F. Padovan break; 17960a708f8fSGustavo F. Padovan 17970a708f8fSGustavo F. Padovan default: 17980a708f8fSGustavo F. Padovan *val = (unsigned long) opt->val; 17990a708f8fSGustavo F. Padovan break; 18000a708f8fSGustavo F. Padovan } 18010a708f8fSGustavo F. Padovan 18020a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); 18030a708f8fSGustavo F. Padovan return len; 18040a708f8fSGustavo F. Padovan } 18050a708f8fSGustavo F. Padovan 18060a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) 18070a708f8fSGustavo F. Padovan { 18080a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 18090a708f8fSGustavo F. Padovan 18100a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); 18110a708f8fSGustavo F. Padovan 18120a708f8fSGustavo F. Padovan opt->type = type; 18130a708f8fSGustavo F. Padovan opt->len = len; 18140a708f8fSGustavo F. Padovan 18150a708f8fSGustavo F. Padovan switch (len) { 18160a708f8fSGustavo F. Padovan case 1: 18170a708f8fSGustavo F. Padovan *((u8 *) opt->val) = val; 18180a708f8fSGustavo F. Padovan break; 18190a708f8fSGustavo F. Padovan 18200a708f8fSGustavo F. Padovan case 2: 18210a708f8fSGustavo F. Padovan put_unaligned_le16(val, opt->val); 18220a708f8fSGustavo F. Padovan break; 18230a708f8fSGustavo F. Padovan 18240a708f8fSGustavo F. Padovan case 4: 18250a708f8fSGustavo F. Padovan put_unaligned_le32(val, opt->val); 18260a708f8fSGustavo F. Padovan break; 18270a708f8fSGustavo F. Padovan 18280a708f8fSGustavo F. Padovan default: 18290a708f8fSGustavo F. Padovan memcpy(opt->val, (void *) val, len); 18300a708f8fSGustavo F. Padovan break; 18310a708f8fSGustavo F. Padovan } 18320a708f8fSGustavo F. Padovan 18330a708f8fSGustavo F. Padovan *ptr += L2CAP_CONF_OPT_SIZE + len; 18340a708f8fSGustavo F. Padovan } 18350a708f8fSGustavo F. Padovan 18360a708f8fSGustavo F. Padovan static void l2cap_ack_timeout(unsigned long arg) 18370a708f8fSGustavo F. Padovan { 1838525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 18390a708f8fSGustavo F. Padovan 1840525cd185SGustavo F. Padovan bh_lock_sock(chan->sk); 1841525cd185SGustavo F. Padovan l2cap_send_ack(chan); 1842525cd185SGustavo F. Padovan bh_unlock_sock(chan->sk); 18430a708f8fSGustavo F. Padovan } 18440a708f8fSGustavo F. Padovan 1845525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan) 18460a708f8fSGustavo F. Padovan { 1847525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 1848525cd185SGustavo F. Padovan 184942e5c802SGustavo F. Padovan chan->expected_ack_seq = 0; 18506a026610SGustavo F. Padovan chan->unacked_frames = 0; 185142e5c802SGustavo F. Padovan chan->buffer_seq = 0; 18526a026610SGustavo F. Padovan chan->num_acked = 0; 18536a026610SGustavo F. Padovan chan->frames_sent = 0; 18540a708f8fSGustavo F. Padovan 1855e92c8e70SGustavo F. Padovan setup_timer(&chan->retrans_timer, l2cap_retrans_timeout, 1856e92c8e70SGustavo F. Padovan (unsigned long) chan); 1857e92c8e70SGustavo F. Padovan setup_timer(&chan->monitor_timer, l2cap_monitor_timeout, 1858e92c8e70SGustavo F. Padovan (unsigned long) chan); 1859e92c8e70SGustavo F. Padovan setup_timer(&chan->ack_timer, l2cap_ack_timeout, (unsigned long) chan); 18600a708f8fSGustavo F. Padovan 1861f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->srej_q); 1862f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->busy_q); 18630a708f8fSGustavo F. Padovan 186439d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 186539d5a3eeSGustavo F. Padovan 1866311bb895SGustavo F. Padovan INIT_WORK(&chan->busy_work, l2cap_busy_work); 18670a708f8fSGustavo F. Padovan 18680a708f8fSGustavo F. Padovan sk->sk_backlog_rcv = l2cap_ertm_data_rcv; 18690a708f8fSGustavo F. Padovan } 18700a708f8fSGustavo F. Padovan 18710a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) 18720a708f8fSGustavo F. Padovan { 18730a708f8fSGustavo F. Padovan switch (mode) { 18740a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 18750a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 18760a708f8fSGustavo F. Padovan if (l2cap_mode_supported(mode, remote_feat_mask)) 18770a708f8fSGustavo F. Padovan return mode; 18780a708f8fSGustavo F. Padovan /* fall through */ 18790a708f8fSGustavo F. Padovan default: 18800a708f8fSGustavo F. Padovan return L2CAP_MODE_BASIC; 18810a708f8fSGustavo F. Padovan } 18820a708f8fSGustavo F. Padovan } 18830a708f8fSGustavo F. Padovan 1884710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) 18850a708f8fSGustavo F. Padovan { 18860a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 18870c1bc5c6SGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = chan->mode }; 18880a708f8fSGustavo F. Padovan void *ptr = req->data; 18890a708f8fSGustavo F. Padovan 189049208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 18910a708f8fSGustavo F. Padovan 189273ffa904SGustavo F. Padovan if (chan->num_conf_req || chan->num_conf_rsp) 18930a708f8fSGustavo F. Padovan goto done; 18940a708f8fSGustavo F. Padovan 18950c1bc5c6SGustavo F. Padovan switch (chan->mode) { 18960a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 18970a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 1898b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_STATE2_DEVICE) 18990a708f8fSGustavo F. Padovan break; 19000a708f8fSGustavo F. Padovan 19010a708f8fSGustavo F. Padovan /* fall through */ 19020a708f8fSGustavo F. Padovan default: 19038c1d787bSGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); 19040a708f8fSGustavo F. Padovan break; 19050a708f8fSGustavo F. Padovan } 19060a708f8fSGustavo F. Padovan 19070a708f8fSGustavo F. Padovan done: 19080c1bc5c6SGustavo F. Padovan if (chan->imtu != L2CAP_DEFAULT_MTU) 19090c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 19100a708f8fSGustavo F. Padovan 19110c1bc5c6SGustavo F. Padovan switch (chan->mode) { 19120a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 19138c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && 19148c1d787bSGustavo F. Padovan !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) 19150a708f8fSGustavo F. Padovan break; 19160a708f8fSGustavo F. Padovan 19170a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_BASIC; 19180a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 19190a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 19200a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 19210a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 19220a708f8fSGustavo F. Padovan rfc.max_pdu_size = 0; 19230a708f8fSGustavo F. Padovan 19240a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 19250a708f8fSGustavo F. Padovan (unsigned long) &rfc); 19260a708f8fSGustavo F. Padovan break; 19270a708f8fSGustavo F. Padovan 19280a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 19290a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_ERTM; 193047d1ec61SGustavo F. Padovan rfc.txwin_size = chan->tx_win; 193147d1ec61SGustavo F. Padovan rfc.max_transmit = chan->max_tx; 19320a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 19330a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 19340a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); 19358c1d787bSGustavo F. Padovan if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10) 19368c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 19370a708f8fSGustavo F. Padovan 19380a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 19390a708f8fSGustavo F. Padovan (unsigned long) &rfc); 19400a708f8fSGustavo F. Padovan 19418c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 19420a708f8fSGustavo F. Padovan break; 19430a708f8fSGustavo F. Padovan 194447d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 1945b4450035SGustavo F. Padovan chan->conf_state & L2CAP_CONF_NO_FCS_RECV) { 194647d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 194747d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 19480a708f8fSGustavo F. Padovan } 19490a708f8fSGustavo F. Padovan break; 19500a708f8fSGustavo F. Padovan 19510a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 19520a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_STREAMING; 19530a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 19540a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 19550a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 19560a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 19570a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); 19588c1d787bSGustavo F. Padovan if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10) 19598c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 19600a708f8fSGustavo F. Padovan 19610a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 19620a708f8fSGustavo F. Padovan (unsigned long) &rfc); 19630a708f8fSGustavo F. Padovan 19648c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 19650a708f8fSGustavo F. Padovan break; 19660a708f8fSGustavo F. Padovan 196747d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 1968b4450035SGustavo F. Padovan chan->conf_state & L2CAP_CONF_NO_FCS_RECV) { 196947d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 197047d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 19710a708f8fSGustavo F. Padovan } 19720a708f8fSGustavo F. Padovan break; 19730a708f8fSGustavo F. Padovan } 19740a708f8fSGustavo F. Padovan 1975fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 19760a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0); 19770a708f8fSGustavo F. Padovan 19780a708f8fSGustavo F. Padovan return ptr - data; 19790a708f8fSGustavo F. Padovan } 19800a708f8fSGustavo F. Padovan 198173ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) 19820a708f8fSGustavo F. Padovan { 19830a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 19840a708f8fSGustavo F. Padovan void *ptr = rsp->data; 198573ffa904SGustavo F. Padovan void *req = chan->conf_req; 198673ffa904SGustavo F. Padovan int len = chan->conf_len; 19870a708f8fSGustavo F. Padovan int type, hint, olen; 19880a708f8fSGustavo F. Padovan unsigned long val; 19890a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 19900a708f8fSGustavo F. Padovan u16 mtu = L2CAP_DEFAULT_MTU; 19910a708f8fSGustavo F. Padovan u16 result = L2CAP_CONF_SUCCESS; 19920a708f8fSGustavo F. Padovan 199373ffa904SGustavo F. Padovan BT_DBG("chan %p", chan); 19940a708f8fSGustavo F. Padovan 19950a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 19960a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&req, &type, &olen, &val); 19970a708f8fSGustavo F. Padovan 19980a708f8fSGustavo F. Padovan hint = type & L2CAP_CONF_HINT; 19990a708f8fSGustavo F. Padovan type &= L2CAP_CONF_MASK; 20000a708f8fSGustavo F. Padovan 20010a708f8fSGustavo F. Padovan switch (type) { 20020a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 20030a708f8fSGustavo F. Padovan mtu = val; 20040a708f8fSGustavo F. Padovan break; 20050a708f8fSGustavo F. Padovan 20060a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 20070c1bc5c6SGustavo F. Padovan chan->flush_to = val; 20080a708f8fSGustavo F. Padovan break; 20090a708f8fSGustavo F. Padovan 20100a708f8fSGustavo F. Padovan case L2CAP_CONF_QOS: 20110a708f8fSGustavo F. Padovan break; 20120a708f8fSGustavo F. Padovan 20130a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 20140a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 20150a708f8fSGustavo F. Padovan memcpy(&rfc, (void *) val, olen); 20160a708f8fSGustavo F. Padovan break; 20170a708f8fSGustavo F. Padovan 20180a708f8fSGustavo F. Padovan case L2CAP_CONF_FCS: 20190a708f8fSGustavo F. Padovan if (val == L2CAP_FCS_NONE) 2020b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_NO_FCS_RECV; 20210a708f8fSGustavo F. Padovan 20220a708f8fSGustavo F. Padovan break; 20230a708f8fSGustavo F. Padovan 20240a708f8fSGustavo F. Padovan default: 20250a708f8fSGustavo F. Padovan if (hint) 20260a708f8fSGustavo F. Padovan break; 20270a708f8fSGustavo F. Padovan 20280a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNKNOWN; 20290a708f8fSGustavo F. Padovan *((u8 *) ptr++) = type; 20300a708f8fSGustavo F. Padovan break; 20310a708f8fSGustavo F. Padovan } 20320a708f8fSGustavo F. Padovan } 20330a708f8fSGustavo F. Padovan 203473ffa904SGustavo F. Padovan if (chan->num_conf_rsp || chan->num_conf_req > 1) 20350a708f8fSGustavo F. Padovan goto done; 20360a708f8fSGustavo F. Padovan 20370c1bc5c6SGustavo F. Padovan switch (chan->mode) { 20380a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 20390a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 2040b4450035SGustavo F. Padovan if (!(chan->conf_state & L2CAP_CONF_STATE2_DEVICE)) { 20410c1bc5c6SGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, 20428c1d787bSGustavo F. Padovan chan->conn->feat_mask); 20430a708f8fSGustavo F. Padovan break; 20440a708f8fSGustavo F. Padovan } 20450a708f8fSGustavo F. Padovan 20460c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) 20470a708f8fSGustavo F. Padovan return -ECONNREFUSED; 20480a708f8fSGustavo F. Padovan 20490a708f8fSGustavo F. Padovan break; 20500a708f8fSGustavo F. Padovan } 20510a708f8fSGustavo F. Padovan 20520a708f8fSGustavo F. Padovan done: 20530c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) { 20540a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 20550c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 20560a708f8fSGustavo F. Padovan 205773ffa904SGustavo F. Padovan if (chan->num_conf_rsp == 1) 20580a708f8fSGustavo F. Padovan return -ECONNREFUSED; 20590a708f8fSGustavo F. Padovan 20600a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 20610a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 20620a708f8fSGustavo F. Padovan } 20630a708f8fSGustavo F. Padovan 20640a708f8fSGustavo F. Padovan 20650a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) { 20660a708f8fSGustavo F. Padovan /* Configure output options and let the other side know 20670a708f8fSGustavo F. Padovan * which ones we don't like. */ 20680a708f8fSGustavo F. Padovan 20690a708f8fSGustavo F. Padovan if (mtu < L2CAP_DEFAULT_MIN_MTU) 20700a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 20710a708f8fSGustavo F. Padovan else { 20720c1bc5c6SGustavo F. Padovan chan->omtu = mtu; 2073b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MTU_DONE; 20740a708f8fSGustavo F. Padovan } 20750c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); 20760a708f8fSGustavo F. Padovan 20770a708f8fSGustavo F. Padovan switch (rfc.mode) { 20780a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 207947d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2080b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MODE_DONE; 20810a708f8fSGustavo F. Padovan break; 20820a708f8fSGustavo F. Padovan 20830a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 20842c03a7a4SGustavo F. Padovan chan->remote_tx_win = rfc.txwin_size; 20852c03a7a4SGustavo F. Padovan chan->remote_max_tx = rfc.max_transmit; 20860a708f8fSGustavo F. Padovan 20878c1d787bSGustavo F. Padovan if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10) 20888c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 20890a708f8fSGustavo F. Padovan 20902c03a7a4SGustavo F. Padovan chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); 20910a708f8fSGustavo F. Padovan 20920a708f8fSGustavo F. Padovan rfc.retrans_timeout = 20930a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO); 20940a708f8fSGustavo F. Padovan rfc.monitor_timeout = 20950a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO); 20960a708f8fSGustavo F. Padovan 2097b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MODE_DONE; 20980a708f8fSGustavo F. Padovan 20990a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 21000a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 21010a708f8fSGustavo F. Padovan 21020a708f8fSGustavo F. Padovan break; 21030a708f8fSGustavo F. Padovan 21040a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 21058c1d787bSGustavo F. Padovan if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10) 21068c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 21070a708f8fSGustavo F. Padovan 21082c03a7a4SGustavo F. Padovan chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); 21090a708f8fSGustavo F. Padovan 2110b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MODE_DONE; 21110a708f8fSGustavo F. Padovan 21120a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 21130a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 21140a708f8fSGustavo F. Padovan 21150a708f8fSGustavo F. Padovan break; 21160a708f8fSGustavo F. Padovan 21170a708f8fSGustavo F. Padovan default: 21180a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 21190a708f8fSGustavo F. Padovan 21200a708f8fSGustavo F. Padovan memset(&rfc, 0, sizeof(rfc)); 21210c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 21220a708f8fSGustavo F. Padovan } 21230a708f8fSGustavo F. Padovan 21240a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) 2125b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_OUTPUT_DONE; 21260a708f8fSGustavo F. Padovan } 2127fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 21280a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 21290a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(0x0000); 21300a708f8fSGustavo F. Padovan 21310a708f8fSGustavo F. Padovan return ptr - data; 21320a708f8fSGustavo F. Padovan } 21330a708f8fSGustavo F. Padovan 2134b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result) 21350a708f8fSGustavo F. Padovan { 21360a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 21370a708f8fSGustavo F. Padovan void *ptr = req->data; 21380a708f8fSGustavo F. Padovan int type, olen; 21390a708f8fSGustavo F. Padovan unsigned long val; 21400a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 21410a708f8fSGustavo F. Padovan 2142fe4128e0SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data); 21430a708f8fSGustavo F. Padovan 21440a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 21450a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 21460a708f8fSGustavo F. Padovan 21470a708f8fSGustavo F. Padovan switch (type) { 21480a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 21490a708f8fSGustavo F. Padovan if (val < L2CAP_DEFAULT_MIN_MTU) { 21500a708f8fSGustavo F. Padovan *result = L2CAP_CONF_UNACCEPT; 21510c1bc5c6SGustavo F. Padovan chan->imtu = L2CAP_DEFAULT_MIN_MTU; 21520a708f8fSGustavo F. Padovan } else 21530c1bc5c6SGustavo F. Padovan chan->imtu = val; 21540c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 21550a708f8fSGustavo F. Padovan break; 21560a708f8fSGustavo F. Padovan 21570a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 21580c1bc5c6SGustavo F. Padovan chan->flush_to = val; 21590a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 21600c1bc5c6SGustavo F. Padovan 2, chan->flush_to); 21610a708f8fSGustavo F. Padovan break; 21620a708f8fSGustavo F. Padovan 21630a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 21640a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 21650a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 21660a708f8fSGustavo F. Padovan 2167b4450035SGustavo F. Padovan if ((chan->conf_state & L2CAP_CONF_STATE2_DEVICE) && 21680c1bc5c6SGustavo F. Padovan rfc.mode != chan->mode) 21690a708f8fSGustavo F. Padovan return -ECONNREFUSED; 21700a708f8fSGustavo F. Padovan 217147d1ec61SGustavo F. Padovan chan->fcs = 0; 21720a708f8fSGustavo F. Padovan 21730a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 21740a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 21750a708f8fSGustavo F. Padovan break; 21760a708f8fSGustavo F. Padovan } 21770a708f8fSGustavo F. Padovan } 21780a708f8fSGustavo F. Padovan 21790c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) 21800a708f8fSGustavo F. Padovan return -ECONNREFUSED; 21810a708f8fSGustavo F. Padovan 21820c1bc5c6SGustavo F. Padovan chan->mode = rfc.mode; 21830a708f8fSGustavo F. Padovan 21840a708f8fSGustavo F. Padovan if (*result == L2CAP_CONF_SUCCESS) { 21850a708f8fSGustavo F. Padovan switch (rfc.mode) { 21860a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 218747d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 218847d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 218947d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 21900a708f8fSGustavo F. Padovan break; 21910a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 219247d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 21930a708f8fSGustavo F. Padovan } 21940a708f8fSGustavo F. Padovan } 21950a708f8fSGustavo F. Padovan 2196fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 21970a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0x0000); 21980a708f8fSGustavo F. Padovan 21990a708f8fSGustavo F. Padovan return ptr - data; 22000a708f8fSGustavo F. Padovan } 22010a708f8fSGustavo F. Padovan 2202fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags) 22030a708f8fSGustavo F. Padovan { 22040a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 22050a708f8fSGustavo F. Padovan void *ptr = rsp->data; 22060a708f8fSGustavo F. Padovan 2207fe4128e0SGustavo F. Padovan BT_DBG("chan %p", chan); 22080a708f8fSGustavo F. Padovan 2209fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 22100a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 22110a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(flags); 22120a708f8fSGustavo F. Padovan 22130a708f8fSGustavo F. Padovan return ptr - data; 22140a708f8fSGustavo F. Padovan } 22150a708f8fSGustavo F. Padovan 22168c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) 2217710f9b0aSGustavo F. Padovan { 2218710f9b0aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 22198c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 2220710f9b0aSGustavo F. Padovan u8 buf[128]; 2221710f9b0aSGustavo F. Padovan 2222fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 2223fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 2224710f9b0aSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 2225710f9b0aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 2226710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 2227710f9b0aSGustavo F. Padovan L2CAP_CONN_RSP, sizeof(rsp), &rsp); 2228710f9b0aSGustavo F. Padovan 2229b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_REQ_SENT) 2230710f9b0aSGustavo F. Padovan return; 2231710f9b0aSGustavo F. Padovan 2232b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 2233710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 2234710f9b0aSGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 2235710f9b0aSGustavo F. Padovan chan->num_conf_req++; 2236710f9b0aSGustavo F. Padovan } 2237710f9b0aSGustavo F. Padovan 223847d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len) 22390a708f8fSGustavo F. Padovan { 22400a708f8fSGustavo F. Padovan int type, olen; 22410a708f8fSGustavo F. Padovan unsigned long val; 22420a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 22430a708f8fSGustavo F. Padovan 224447d1ec61SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); 22450a708f8fSGustavo F. Padovan 22460c1bc5c6SGustavo F. Padovan if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) 22470a708f8fSGustavo F. Padovan return; 22480a708f8fSGustavo F. Padovan 22490a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 22500a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 22510a708f8fSGustavo F. Padovan 22520a708f8fSGustavo F. Padovan switch (type) { 22530a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 22540a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 22550a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 22560a708f8fSGustavo F. Padovan goto done; 22570a708f8fSGustavo F. Padovan } 22580a708f8fSGustavo F. Padovan } 22590a708f8fSGustavo F. Padovan 22600a708f8fSGustavo F. Padovan done: 22610a708f8fSGustavo F. Padovan switch (rfc.mode) { 22620a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 226347d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 226447d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 226547d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 22660a708f8fSGustavo F. Padovan break; 22670a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 226847d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 22690a708f8fSGustavo F. Padovan } 22700a708f8fSGustavo F. Padovan } 22710a708f8fSGustavo F. Padovan 22720a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 22730a708f8fSGustavo F. Padovan { 22740a708f8fSGustavo F. Padovan struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data; 22750a708f8fSGustavo F. Padovan 22760a708f8fSGustavo F. Padovan if (rej->reason != 0x0000) 22770a708f8fSGustavo F. Padovan return 0; 22780a708f8fSGustavo F. Padovan 22790a708f8fSGustavo F. Padovan if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && 22800a708f8fSGustavo F. Padovan cmd->ident == conn->info_ident) { 22810a708f8fSGustavo F. Padovan del_timer(&conn->info_timer); 22820a708f8fSGustavo F. Padovan 22830a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 22840a708f8fSGustavo F. Padovan conn->info_ident = 0; 22850a708f8fSGustavo F. Padovan 22860a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 22870a708f8fSGustavo F. Padovan } 22880a708f8fSGustavo F. Padovan 22890a708f8fSGustavo F. Padovan return 0; 22900a708f8fSGustavo F. Padovan } 22910a708f8fSGustavo F. Padovan 22920a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 22930a708f8fSGustavo F. Padovan { 22940a708f8fSGustavo F. Padovan struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; 22950a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 229623691d75SGustavo F. Padovan struct l2cap_chan *chan = NULL, *pchan; 22970a708f8fSGustavo F. Padovan struct sock *parent, *sk = NULL; 22980a708f8fSGustavo F. Padovan int result, status = L2CAP_CS_NO_INFO; 22990a708f8fSGustavo F. Padovan 23000a708f8fSGustavo F. Padovan u16 dcid = 0, scid = __le16_to_cpu(req->scid); 23010a708f8fSGustavo F. Padovan __le16 psm = req->psm; 23020a708f8fSGustavo F. Padovan 23030a708f8fSGustavo F. Padovan BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid); 23040a708f8fSGustavo F. Padovan 23050a708f8fSGustavo F. Padovan /* Check if we have socket listening on psm */ 230623691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src); 230723691d75SGustavo F. Padovan if (!pchan) { 23080a708f8fSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 23090a708f8fSGustavo F. Padovan goto sendresp; 23100a708f8fSGustavo F. Padovan } 23110a708f8fSGustavo F. Padovan 231223691d75SGustavo F. Padovan parent = pchan->sk; 231323691d75SGustavo F. Padovan 23140a708f8fSGustavo F. Padovan bh_lock_sock(parent); 23150a708f8fSGustavo F. Padovan 23160a708f8fSGustavo F. Padovan /* Check if the ACL is secure enough (if not SDP) */ 23170a708f8fSGustavo F. Padovan if (psm != cpu_to_le16(0x0001) && 23180a708f8fSGustavo F. Padovan !hci_conn_check_link_mode(conn->hcon)) { 23190a708f8fSGustavo F. Padovan conn->disc_reason = 0x05; 23200a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 23210a708f8fSGustavo F. Padovan goto response; 23220a708f8fSGustavo F. Padovan } 23230a708f8fSGustavo F. Padovan 23240a708f8fSGustavo F. Padovan result = L2CAP_CR_NO_MEM; 23250a708f8fSGustavo F. Padovan 23260a708f8fSGustavo F. Padovan /* Check for backlog size */ 23270a708f8fSGustavo F. Padovan if (sk_acceptq_is_full(parent)) { 23280a708f8fSGustavo F. Padovan BT_DBG("backlog full %d", parent->sk_ack_backlog); 23290a708f8fSGustavo F. Padovan goto response; 23300a708f8fSGustavo F. Padovan } 23310a708f8fSGustavo F. Padovan 23320a708f8fSGustavo F. Padovan sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC); 23330a708f8fSGustavo F. Padovan if (!sk) 23340a708f8fSGustavo F. Padovan goto response; 23350a708f8fSGustavo F. Padovan 233623691d75SGustavo F. Padovan chan = l2cap_chan_create(sk); 233748454079SGustavo F. Padovan if (!chan) { 233848454079SGustavo F. Padovan l2cap_sock_kill(sk); 233948454079SGustavo F. Padovan goto response; 234048454079SGustavo F. Padovan } 234148454079SGustavo F. Padovan 23425d41ce1dSGustavo F. Padovan l2cap_pi(sk)->chan = chan; 23435d41ce1dSGustavo F. Padovan 2344baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 23450a708f8fSGustavo F. Padovan 23460a708f8fSGustavo F. Padovan /* Check if we already have channel with that dcid */ 2347baa7e1faSGustavo F. Padovan if (__l2cap_get_chan_by_dcid(conn, scid)) { 2348baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 23490a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 23500a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 23510a708f8fSGustavo F. Padovan goto response; 23520a708f8fSGustavo F. Padovan } 23530a708f8fSGustavo F. Padovan 23540a708f8fSGustavo F. Padovan hci_conn_hold(conn->hcon); 23550a708f8fSGustavo F. Padovan 23560a708f8fSGustavo F. Padovan l2cap_sock_init(sk, parent); 23570a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->src, conn->src); 23580a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->dst, conn->dst); 2359fe4128e0SGustavo F. Padovan chan->psm = psm; 2360fe4128e0SGustavo F. Padovan chan->dcid = scid; 23610a708f8fSGustavo F. Padovan 2362d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 2363d1010240SGustavo F. Padovan 236448454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 236548454079SGustavo F. Padovan 2366fe4128e0SGustavo F. Padovan dcid = chan->scid; 23670a708f8fSGustavo F. Padovan 2368ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, sk->sk_sndtimeo); 23690a708f8fSGustavo F. Padovan 2370fc7f8a7eSGustavo F. Padovan chan->ident = cmd->ident; 23710a708f8fSGustavo F. Padovan 23720a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { 23734343478fSGustavo F. Padovan if (l2cap_check_security(chan)) { 23740a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 23750a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT2; 23760a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 23770a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHOR_PEND; 23780a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 23790a708f8fSGustavo F. Padovan } else { 23800a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 23810a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 23820a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 23830a708f8fSGustavo F. Padovan } 23840a708f8fSGustavo F. Padovan } else { 23850a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT2; 23860a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 23870a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHEN_PEND; 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_NO_INFO; 23930a708f8fSGustavo F. Padovan } 23940a708f8fSGustavo F. Padovan 2395baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 23960a708f8fSGustavo F. Padovan 23970a708f8fSGustavo F. Padovan response: 23980a708f8fSGustavo F. Padovan bh_unlock_sock(parent); 23990a708f8fSGustavo F. Padovan 24000a708f8fSGustavo F. Padovan sendresp: 24010a708f8fSGustavo F. Padovan rsp.scid = cpu_to_le16(scid); 24020a708f8fSGustavo F. Padovan rsp.dcid = cpu_to_le16(dcid); 24030a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 24040a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(status); 24050a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); 24060a708f8fSGustavo F. Padovan 24070a708f8fSGustavo F. Padovan if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { 24080a708f8fSGustavo F. Padovan struct l2cap_info_req info; 24090a708f8fSGustavo F. Padovan info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 24100a708f8fSGustavo F. Padovan 24110a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 24120a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 24130a708f8fSGustavo F. Padovan 24140a708f8fSGustavo F. Padovan mod_timer(&conn->info_timer, jiffies + 24150a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 24160a708f8fSGustavo F. Padovan 24170a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 24180a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(info), &info); 24190a708f8fSGustavo F. Padovan } 24200a708f8fSGustavo F. Padovan 2421b4450035SGustavo F. Padovan if (chan && !(chan->conf_state & L2CAP_CONF_REQ_SENT) && 24220a708f8fSGustavo F. Padovan result == L2CAP_CR_SUCCESS) { 24230a708f8fSGustavo F. Padovan u8 buf[128]; 2424b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 24250a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 242673ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 242773ffa904SGustavo F. Padovan chan->num_conf_req++; 24280a708f8fSGustavo F. Padovan } 24290a708f8fSGustavo F. Padovan 24300a708f8fSGustavo F. Padovan return 0; 24310a708f8fSGustavo F. Padovan } 24320a708f8fSGustavo F. Padovan 24330a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 24340a708f8fSGustavo F. Padovan { 24350a708f8fSGustavo F. Padovan struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; 24360a708f8fSGustavo F. Padovan u16 scid, dcid, result, status; 243748454079SGustavo F. Padovan struct l2cap_chan *chan; 24380a708f8fSGustavo F. Padovan struct sock *sk; 24390a708f8fSGustavo F. Padovan u8 req[128]; 24400a708f8fSGustavo F. Padovan 24410a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 24420a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 24430a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 24440a708f8fSGustavo F. Padovan status = __le16_to_cpu(rsp->status); 24450a708f8fSGustavo F. Padovan 24460a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status); 24470a708f8fSGustavo F. Padovan 24480a708f8fSGustavo F. Padovan if (scid) { 2449baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 245048454079SGustavo F. Padovan if (!chan) 24510a708f8fSGustavo F. Padovan return -EFAULT; 24520a708f8fSGustavo F. Padovan } else { 2453baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_ident(conn, cmd->ident); 245448454079SGustavo F. Padovan if (!chan) 24550a708f8fSGustavo F. Padovan return -EFAULT; 24560a708f8fSGustavo F. Padovan } 24570a708f8fSGustavo F. Padovan 245848454079SGustavo F. Padovan sk = chan->sk; 245948454079SGustavo F. Padovan 24600a708f8fSGustavo F. Padovan switch (result) { 24610a708f8fSGustavo F. Padovan case L2CAP_CR_SUCCESS: 24620a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 2463fc7f8a7eSGustavo F. Padovan chan->ident = 0; 2464fe4128e0SGustavo F. Padovan chan->dcid = dcid; 2465b4450035SGustavo F. Padovan chan->conf_state &= ~L2CAP_CONF_CONNECT_PEND; 24660a708f8fSGustavo F. Padovan 2467b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_REQ_SENT) 24680a708f8fSGustavo F. Padovan break; 24690a708f8fSGustavo F. Padovan 2470b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 24710a708f8fSGustavo F. Padovan 24720a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 247373ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, req), req); 247473ffa904SGustavo F. Padovan chan->num_conf_req++; 24750a708f8fSGustavo F. Padovan break; 24760a708f8fSGustavo F. Padovan 24770a708f8fSGustavo F. Padovan case L2CAP_CR_PEND: 2478b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 24790a708f8fSGustavo F. Padovan break; 24800a708f8fSGustavo F. Padovan 24810a708f8fSGustavo F. Padovan default: 24820a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 24830a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 24840a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 2485ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 2486ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 5); 24870a708f8fSGustavo F. Padovan break; 24880a708f8fSGustavo F. Padovan } 24890a708f8fSGustavo F. Padovan 249048454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNREFUSED); 24910a708f8fSGustavo F. Padovan break; 24920a708f8fSGustavo F. Padovan } 24930a708f8fSGustavo F. Padovan 24940a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 24950a708f8fSGustavo F. Padovan return 0; 24960a708f8fSGustavo F. Padovan } 24970a708f8fSGustavo F. Padovan 249847d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan) 24990a708f8fSGustavo F. Padovan { 250047d1ec61SGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(chan->sk); 250147d1ec61SGustavo F. Padovan 25020a708f8fSGustavo F. Padovan /* FCS is enabled only in ERTM or streaming mode, if one or both 25030a708f8fSGustavo F. Padovan * sides request it. 25040a708f8fSGustavo F. Padovan */ 25050c1bc5c6SGustavo F. Padovan if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) 250647d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2507b4450035SGustavo F. Padovan else if (!(pi->chan->conf_state & L2CAP_CONF_NO_FCS_RECV)) 250847d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_CRC16; 25090a708f8fSGustavo F. Padovan } 25100a708f8fSGustavo F. Padovan 25110a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 25120a708f8fSGustavo F. Padovan { 25130a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; 25140a708f8fSGustavo F. Padovan u16 dcid, flags; 25150a708f8fSGustavo F. Padovan u8 rsp[64]; 251648454079SGustavo F. Padovan struct l2cap_chan *chan; 25170a708f8fSGustavo F. Padovan struct sock *sk; 25180a708f8fSGustavo F. Padovan int len; 25190a708f8fSGustavo F. Padovan 25200a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 25210a708f8fSGustavo F. Padovan flags = __le16_to_cpu(req->flags); 25220a708f8fSGustavo F. Padovan 25230a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); 25240a708f8fSGustavo F. Padovan 2525baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 252648454079SGustavo F. Padovan if (!chan) 25270a708f8fSGustavo F. Padovan return -ENOENT; 25280a708f8fSGustavo F. Padovan 252948454079SGustavo F. Padovan sk = chan->sk; 253048454079SGustavo F. Padovan 25310a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONFIG) { 25320a708f8fSGustavo F. Padovan struct l2cap_cmd_rej rej; 25330a708f8fSGustavo F. Padovan 25340a708f8fSGustavo F. Padovan rej.reason = cpu_to_le16(0x0002); 25350a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, 25360a708f8fSGustavo F. Padovan sizeof(rej), &rej); 25370a708f8fSGustavo F. Padovan goto unlock; 25380a708f8fSGustavo F. Padovan } 25390a708f8fSGustavo F. Padovan 25400a708f8fSGustavo F. Padovan /* Reject if config buffer is too small. */ 25410a708f8fSGustavo F. Padovan len = cmd_len - sizeof(*req); 254273ffa904SGustavo F. Padovan if (chan->conf_len + len > sizeof(chan->conf_req)) { 25430a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2544fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 25450a708f8fSGustavo F. Padovan L2CAP_CONF_REJECT, flags), rsp); 25460a708f8fSGustavo F. Padovan goto unlock; 25470a708f8fSGustavo F. Padovan } 25480a708f8fSGustavo F. Padovan 25490a708f8fSGustavo F. Padovan /* Store config. */ 255073ffa904SGustavo F. Padovan memcpy(chan->conf_req + chan->conf_len, req->data, len); 255173ffa904SGustavo F. Padovan chan->conf_len += len; 25520a708f8fSGustavo F. Padovan 25530a708f8fSGustavo F. Padovan if (flags & 0x0001) { 25540a708f8fSGustavo F. Padovan /* Incomplete config. Send empty response. */ 25550a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2556fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 25570a708f8fSGustavo F. Padovan L2CAP_CONF_SUCCESS, 0x0001), rsp); 25580a708f8fSGustavo F. Padovan goto unlock; 25590a708f8fSGustavo F. Padovan } 25600a708f8fSGustavo F. Padovan 25610a708f8fSGustavo F. Padovan /* Complete config. */ 256273ffa904SGustavo F. Padovan len = l2cap_parse_conf_req(chan, rsp); 25630a708f8fSGustavo F. Padovan if (len < 0) { 2564e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 25650a708f8fSGustavo F. Padovan goto unlock; 25660a708f8fSGustavo F. Padovan } 25670a708f8fSGustavo F. Padovan 25680a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); 256973ffa904SGustavo F. Padovan chan->num_conf_rsp++; 25700a708f8fSGustavo F. Padovan 25710a708f8fSGustavo F. Padovan /* Reset config buffer. */ 257273ffa904SGustavo F. Padovan chan->conf_len = 0; 25730a708f8fSGustavo F. Padovan 2574b4450035SGustavo F. Padovan if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE)) 25750a708f8fSGustavo F. Padovan goto unlock; 25760a708f8fSGustavo F. Padovan 2577b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_INPUT_DONE) { 257847d1ec61SGustavo F. Padovan set_default_fcs(chan); 25790a708f8fSGustavo F. Padovan 25800a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 25810a708f8fSGustavo F. Padovan 258242e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 258342e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 258458d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 25850c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2586525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 25870a708f8fSGustavo F. Padovan 25880a708f8fSGustavo F. Padovan l2cap_chan_ready(sk); 25890a708f8fSGustavo F. Padovan goto unlock; 25900a708f8fSGustavo F. Padovan } 25910a708f8fSGustavo F. Padovan 2592b4450035SGustavo F. Padovan if (!(chan->conf_state & L2CAP_CONF_REQ_SENT)) { 25930a708f8fSGustavo F. Padovan u8 buf[64]; 2594b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 25950a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 259673ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 259773ffa904SGustavo F. Padovan chan->num_conf_req++; 25980a708f8fSGustavo F. Padovan } 25990a708f8fSGustavo F. Padovan 26000a708f8fSGustavo F. Padovan unlock: 26010a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 26020a708f8fSGustavo F. Padovan return 0; 26030a708f8fSGustavo F. Padovan } 26040a708f8fSGustavo F. Padovan 26050a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 26060a708f8fSGustavo F. Padovan { 26070a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; 26080a708f8fSGustavo F. Padovan u16 scid, flags, result; 260948454079SGustavo F. Padovan struct l2cap_chan *chan; 26100a708f8fSGustavo F. Padovan struct sock *sk; 26110a708f8fSGustavo F. Padovan int len = cmd->len - sizeof(*rsp); 26120a708f8fSGustavo F. Padovan 26130a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 26140a708f8fSGustavo F. Padovan flags = __le16_to_cpu(rsp->flags); 26150a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 26160a708f8fSGustavo F. Padovan 26170a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", 26180a708f8fSGustavo F. Padovan scid, flags, result); 26190a708f8fSGustavo F. Padovan 2620baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 262148454079SGustavo F. Padovan if (!chan) 26220a708f8fSGustavo F. Padovan return 0; 26230a708f8fSGustavo F. Padovan 262448454079SGustavo F. Padovan sk = chan->sk; 262548454079SGustavo F. Padovan 26260a708f8fSGustavo F. Padovan switch (result) { 26270a708f8fSGustavo F. Padovan case L2CAP_CONF_SUCCESS: 262847d1ec61SGustavo F. Padovan l2cap_conf_rfc_get(chan, rsp->data, len); 26290a708f8fSGustavo F. Padovan break; 26300a708f8fSGustavo F. Padovan 26310a708f8fSGustavo F. Padovan case L2CAP_CONF_UNACCEPT: 263273ffa904SGustavo F. Padovan if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { 26330a708f8fSGustavo F. Padovan char req[64]; 26340a708f8fSGustavo F. Padovan 26350a708f8fSGustavo F. Padovan if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { 2636e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 26370a708f8fSGustavo F. Padovan goto done; 26380a708f8fSGustavo F. Padovan } 26390a708f8fSGustavo F. Padovan 26400a708f8fSGustavo F. Padovan /* throw out any old stored conf requests */ 26410a708f8fSGustavo F. Padovan result = L2CAP_CONF_SUCCESS; 2642b4450035SGustavo F. Padovan len = l2cap_parse_conf_rsp(chan, rsp->data, len, 2643b4450035SGustavo F. Padovan req, &result); 26440a708f8fSGustavo F. Padovan if (len < 0) { 2645e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 26460a708f8fSGustavo F. Padovan goto done; 26470a708f8fSGustavo F. Padovan } 26480a708f8fSGustavo F. Padovan 26490a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 26500a708f8fSGustavo F. Padovan L2CAP_CONF_REQ, len, req); 265173ffa904SGustavo F. Padovan chan->num_conf_req++; 26520a708f8fSGustavo F. Padovan if (result != L2CAP_CONF_SUCCESS) 26530a708f8fSGustavo F. Padovan goto done; 26540a708f8fSGustavo F. Padovan break; 26550a708f8fSGustavo F. Padovan } 26560a708f8fSGustavo F. Padovan 26570a708f8fSGustavo F. Padovan default: 26580a708f8fSGustavo F. Padovan sk->sk_err = ECONNRESET; 2659ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ * 5); 2660e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 26610a708f8fSGustavo F. Padovan goto done; 26620a708f8fSGustavo F. Padovan } 26630a708f8fSGustavo F. Padovan 26640a708f8fSGustavo F. Padovan if (flags & 0x01) 26650a708f8fSGustavo F. Padovan goto done; 26660a708f8fSGustavo F. Padovan 2667b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_INPUT_DONE; 26680a708f8fSGustavo F. Padovan 2669b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_OUTPUT_DONE) { 267047d1ec61SGustavo F. Padovan set_default_fcs(chan); 26710a708f8fSGustavo F. Padovan 26720a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 267342e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 267442e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 267558d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 26760c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2677525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 26780a708f8fSGustavo F. Padovan 26790a708f8fSGustavo F. Padovan l2cap_chan_ready(sk); 26800a708f8fSGustavo F. Padovan } 26810a708f8fSGustavo F. Padovan 26820a708f8fSGustavo F. Padovan done: 26830a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 26840a708f8fSGustavo F. Padovan return 0; 26850a708f8fSGustavo F. Padovan } 26860a708f8fSGustavo F. Padovan 26870a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 26880a708f8fSGustavo F. Padovan { 26890a708f8fSGustavo F. Padovan struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; 26900a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp rsp; 26910a708f8fSGustavo F. Padovan u16 dcid, scid; 269248454079SGustavo F. Padovan struct l2cap_chan *chan; 26930a708f8fSGustavo F. Padovan struct sock *sk; 26940a708f8fSGustavo F. Padovan 26950a708f8fSGustavo F. Padovan scid = __le16_to_cpu(req->scid); 26960a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 26970a708f8fSGustavo F. Padovan 26980a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); 26990a708f8fSGustavo F. Padovan 2700baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 270148454079SGustavo F. Padovan if (!chan) 27020a708f8fSGustavo F. Padovan return 0; 27030a708f8fSGustavo F. Padovan 270448454079SGustavo F. Padovan sk = chan->sk; 270548454079SGustavo F. Padovan 2706fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 2707fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 27080a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); 27090a708f8fSGustavo F. Padovan 27100a708f8fSGustavo F. Padovan sk->sk_shutdown = SHUTDOWN_MASK; 27110a708f8fSGustavo F. Padovan 27120a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 27130a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 27140a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 2715ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 2716ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 5); 27170a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27180a708f8fSGustavo F. Padovan return 0; 27190a708f8fSGustavo F. Padovan } 27200a708f8fSGustavo F. Padovan 272148454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNRESET); 27220a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27230a708f8fSGustavo F. Padovan 27240a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 27250a708f8fSGustavo F. Padovan return 0; 27260a708f8fSGustavo F. Padovan } 27270a708f8fSGustavo F. Padovan 27280a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 27290a708f8fSGustavo F. Padovan { 27300a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; 27310a708f8fSGustavo F. Padovan u16 dcid, scid; 273248454079SGustavo F. Padovan struct l2cap_chan *chan; 27330a708f8fSGustavo F. Padovan struct sock *sk; 27340a708f8fSGustavo F. Padovan 27350a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 27360a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 27370a708f8fSGustavo F. Padovan 27380a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); 27390a708f8fSGustavo F. Padovan 2740baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 274148454079SGustavo F. Padovan if (!chan) 27420a708f8fSGustavo F. Padovan return 0; 27430a708f8fSGustavo F. Padovan 274448454079SGustavo F. Padovan sk = chan->sk; 274548454079SGustavo F. Padovan 27460a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 27470a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 27480a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 2749ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 2750ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 5); 27510a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27520a708f8fSGustavo F. Padovan return 0; 27530a708f8fSGustavo F. Padovan } 27540a708f8fSGustavo F. Padovan 275548454079SGustavo F. Padovan l2cap_chan_del(chan, 0); 27560a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27570a708f8fSGustavo F. Padovan 27580a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 27590a708f8fSGustavo F. Padovan return 0; 27600a708f8fSGustavo F. Padovan } 27610a708f8fSGustavo F. Padovan 27620a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 27630a708f8fSGustavo F. Padovan { 27640a708f8fSGustavo F. Padovan struct l2cap_info_req *req = (struct l2cap_info_req *) data; 27650a708f8fSGustavo F. Padovan u16 type; 27660a708f8fSGustavo F. Padovan 27670a708f8fSGustavo F. Padovan type = __le16_to_cpu(req->type); 27680a708f8fSGustavo F. Padovan 27690a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x", type); 27700a708f8fSGustavo F. Padovan 27710a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 27720a708f8fSGustavo F. Padovan u8 buf[8]; 27730a708f8fSGustavo F. Padovan u32 feat_mask = l2cap_feat_mask; 27740a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 27750a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 27760a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 27770a708f8fSGustavo F. Padovan if (!disable_ertm) 27780a708f8fSGustavo F. Padovan feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING 27790a708f8fSGustavo F. Padovan | L2CAP_FEAT_FCS; 27800a708f8fSGustavo F. Padovan put_unaligned_le32(feat_mask, rsp->data); 27810a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 27820a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 27830a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 27840a708f8fSGustavo F. Padovan u8 buf[12]; 27850a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 27860a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 27870a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 27880a708f8fSGustavo F. Padovan memcpy(buf + 4, l2cap_fixed_chan, 8); 27890a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 27900a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 27910a708f8fSGustavo F. Padovan } else { 27920a708f8fSGustavo F. Padovan struct l2cap_info_rsp rsp; 27930a708f8fSGustavo F. Padovan rsp.type = cpu_to_le16(type); 27940a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); 27950a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 27960a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(rsp), &rsp); 27970a708f8fSGustavo F. Padovan } 27980a708f8fSGustavo F. Padovan 27990a708f8fSGustavo F. Padovan return 0; 28000a708f8fSGustavo F. Padovan } 28010a708f8fSGustavo F. Padovan 28020a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 28030a708f8fSGustavo F. Padovan { 28040a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; 28050a708f8fSGustavo F. Padovan u16 type, result; 28060a708f8fSGustavo F. Padovan 28070a708f8fSGustavo F. Padovan type = __le16_to_cpu(rsp->type); 28080a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 28090a708f8fSGustavo F. Padovan 28100a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); 28110a708f8fSGustavo F. Padovan 2812e90165beSAndrei Emeltchenko /* L2CAP Info req/rsp are unbound to channels, add extra checks */ 2813e90165beSAndrei Emeltchenko if (cmd->ident != conn->info_ident || 2814e90165beSAndrei Emeltchenko conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) 2815e90165beSAndrei Emeltchenko return 0; 2816e90165beSAndrei Emeltchenko 28170a708f8fSGustavo F. Padovan del_timer(&conn->info_timer); 28180a708f8fSGustavo F. Padovan 28190a708f8fSGustavo F. Padovan if (result != L2CAP_IR_SUCCESS) { 28200a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 28210a708f8fSGustavo F. Padovan conn->info_ident = 0; 28220a708f8fSGustavo F. Padovan 28230a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 28240a708f8fSGustavo F. Padovan 28250a708f8fSGustavo F. Padovan return 0; 28260a708f8fSGustavo F. Padovan } 28270a708f8fSGustavo F. Padovan 28280a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 28290a708f8fSGustavo F. Padovan conn->feat_mask = get_unaligned_le32(rsp->data); 28300a708f8fSGustavo F. Padovan 28310a708f8fSGustavo F. Padovan if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { 28320a708f8fSGustavo F. Padovan struct l2cap_info_req req; 28330a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 28340a708f8fSGustavo F. Padovan 28350a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 28360a708f8fSGustavo F. Padovan 28370a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 28380a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 28390a708f8fSGustavo F. Padovan } else { 28400a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 28410a708f8fSGustavo F. Padovan conn->info_ident = 0; 28420a708f8fSGustavo F. Padovan 28430a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 28440a708f8fSGustavo F. Padovan } 28450a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 28460a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 28470a708f8fSGustavo F. Padovan conn->info_ident = 0; 28480a708f8fSGustavo F. Padovan 28490a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 28500a708f8fSGustavo F. Padovan } 28510a708f8fSGustavo F. Padovan 28520a708f8fSGustavo F. Padovan return 0; 28530a708f8fSGustavo F. Padovan } 28540a708f8fSGustavo F. Padovan 2855e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency, 2856de73115aSClaudio Takahasi u16 to_multiplier) 2857de73115aSClaudio Takahasi { 2858de73115aSClaudio Takahasi u16 max_latency; 2859de73115aSClaudio Takahasi 2860de73115aSClaudio Takahasi if (min > max || min < 6 || max > 3200) 2861de73115aSClaudio Takahasi return -EINVAL; 2862de73115aSClaudio Takahasi 2863de73115aSClaudio Takahasi if (to_multiplier < 10 || to_multiplier > 3200) 2864de73115aSClaudio Takahasi return -EINVAL; 2865de73115aSClaudio Takahasi 2866de73115aSClaudio Takahasi if (max >= to_multiplier * 8) 2867de73115aSClaudio Takahasi return -EINVAL; 2868de73115aSClaudio Takahasi 2869de73115aSClaudio Takahasi max_latency = (to_multiplier * 8 / max) - 1; 2870de73115aSClaudio Takahasi if (latency > 499 || latency > max_latency) 2871de73115aSClaudio Takahasi return -EINVAL; 2872de73115aSClaudio Takahasi 2873de73115aSClaudio Takahasi return 0; 2874de73115aSClaudio Takahasi } 2875de73115aSClaudio Takahasi 2876de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, 2877de73115aSClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 2878de73115aSClaudio Takahasi { 2879de73115aSClaudio Takahasi struct hci_conn *hcon = conn->hcon; 2880de73115aSClaudio Takahasi struct l2cap_conn_param_update_req *req; 2881de73115aSClaudio Takahasi struct l2cap_conn_param_update_rsp rsp; 2882de73115aSClaudio Takahasi u16 min, max, latency, to_multiplier, cmd_len; 28832ce603ebSClaudio Takahasi int err; 2884de73115aSClaudio Takahasi 2885de73115aSClaudio Takahasi if (!(hcon->link_mode & HCI_LM_MASTER)) 2886de73115aSClaudio Takahasi return -EINVAL; 2887de73115aSClaudio Takahasi 2888de73115aSClaudio Takahasi cmd_len = __le16_to_cpu(cmd->len); 2889de73115aSClaudio Takahasi if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) 2890de73115aSClaudio Takahasi return -EPROTO; 2891de73115aSClaudio Takahasi 2892de73115aSClaudio Takahasi req = (struct l2cap_conn_param_update_req *) data; 2893de73115aSClaudio Takahasi min = __le16_to_cpu(req->min); 2894de73115aSClaudio Takahasi max = __le16_to_cpu(req->max); 2895de73115aSClaudio Takahasi latency = __le16_to_cpu(req->latency); 2896de73115aSClaudio Takahasi to_multiplier = __le16_to_cpu(req->to_multiplier); 2897de73115aSClaudio Takahasi 2898de73115aSClaudio Takahasi BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", 2899de73115aSClaudio Takahasi min, max, latency, to_multiplier); 2900de73115aSClaudio Takahasi 2901de73115aSClaudio Takahasi memset(&rsp, 0, sizeof(rsp)); 29022ce603ebSClaudio Takahasi 29032ce603ebSClaudio Takahasi err = l2cap_check_conn_param(min, max, latency, to_multiplier); 29042ce603ebSClaudio Takahasi if (err) 2905de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); 2906de73115aSClaudio Takahasi else 2907de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); 2908de73115aSClaudio Takahasi 2909de73115aSClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, 2910de73115aSClaudio Takahasi sizeof(rsp), &rsp); 2911de73115aSClaudio Takahasi 29122ce603ebSClaudio Takahasi if (!err) 29132ce603ebSClaudio Takahasi hci_le_conn_update(hcon, min, max, latency, to_multiplier); 29142ce603ebSClaudio Takahasi 2915de73115aSClaudio Takahasi return 0; 2916de73115aSClaudio Takahasi } 2917de73115aSClaudio Takahasi 29183300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, 29193300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 29203300d9a9SClaudio Takahasi { 29213300d9a9SClaudio Takahasi int err = 0; 29223300d9a9SClaudio Takahasi 29233300d9a9SClaudio Takahasi switch (cmd->code) { 29243300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 29253300d9a9SClaudio Takahasi l2cap_command_rej(conn, cmd, data); 29263300d9a9SClaudio Takahasi break; 29273300d9a9SClaudio Takahasi 29283300d9a9SClaudio Takahasi case L2CAP_CONN_REQ: 29293300d9a9SClaudio Takahasi err = l2cap_connect_req(conn, cmd, data); 29303300d9a9SClaudio Takahasi break; 29313300d9a9SClaudio Takahasi 29323300d9a9SClaudio Takahasi case L2CAP_CONN_RSP: 29333300d9a9SClaudio Takahasi err = l2cap_connect_rsp(conn, cmd, data); 29343300d9a9SClaudio Takahasi break; 29353300d9a9SClaudio Takahasi 29363300d9a9SClaudio Takahasi case L2CAP_CONF_REQ: 29373300d9a9SClaudio Takahasi err = l2cap_config_req(conn, cmd, cmd_len, data); 29383300d9a9SClaudio Takahasi break; 29393300d9a9SClaudio Takahasi 29403300d9a9SClaudio Takahasi case L2CAP_CONF_RSP: 29413300d9a9SClaudio Takahasi err = l2cap_config_rsp(conn, cmd, data); 29423300d9a9SClaudio Takahasi break; 29433300d9a9SClaudio Takahasi 29443300d9a9SClaudio Takahasi case L2CAP_DISCONN_REQ: 29453300d9a9SClaudio Takahasi err = l2cap_disconnect_req(conn, cmd, data); 29463300d9a9SClaudio Takahasi break; 29473300d9a9SClaudio Takahasi 29483300d9a9SClaudio Takahasi case L2CAP_DISCONN_RSP: 29493300d9a9SClaudio Takahasi err = l2cap_disconnect_rsp(conn, cmd, data); 29503300d9a9SClaudio Takahasi break; 29513300d9a9SClaudio Takahasi 29523300d9a9SClaudio Takahasi case L2CAP_ECHO_REQ: 29533300d9a9SClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); 29543300d9a9SClaudio Takahasi break; 29553300d9a9SClaudio Takahasi 29563300d9a9SClaudio Takahasi case L2CAP_ECHO_RSP: 29573300d9a9SClaudio Takahasi break; 29583300d9a9SClaudio Takahasi 29593300d9a9SClaudio Takahasi case L2CAP_INFO_REQ: 29603300d9a9SClaudio Takahasi err = l2cap_information_req(conn, cmd, data); 29613300d9a9SClaudio Takahasi break; 29623300d9a9SClaudio Takahasi 29633300d9a9SClaudio Takahasi case L2CAP_INFO_RSP: 29643300d9a9SClaudio Takahasi err = l2cap_information_rsp(conn, cmd, data); 29653300d9a9SClaudio Takahasi break; 29663300d9a9SClaudio Takahasi 29673300d9a9SClaudio Takahasi default: 29683300d9a9SClaudio Takahasi BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); 29693300d9a9SClaudio Takahasi err = -EINVAL; 29703300d9a9SClaudio Takahasi break; 29713300d9a9SClaudio Takahasi } 29723300d9a9SClaudio Takahasi 29733300d9a9SClaudio Takahasi return err; 29743300d9a9SClaudio Takahasi } 29753300d9a9SClaudio Takahasi 29763300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, 29773300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 29783300d9a9SClaudio Takahasi { 29793300d9a9SClaudio Takahasi switch (cmd->code) { 29803300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 29813300d9a9SClaudio Takahasi return 0; 29823300d9a9SClaudio Takahasi 29833300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_REQ: 2984de73115aSClaudio Takahasi return l2cap_conn_param_update_req(conn, cmd, data); 29853300d9a9SClaudio Takahasi 29863300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_RSP: 29873300d9a9SClaudio Takahasi return 0; 29883300d9a9SClaudio Takahasi 29893300d9a9SClaudio Takahasi default: 29903300d9a9SClaudio Takahasi BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); 29913300d9a9SClaudio Takahasi return -EINVAL; 29923300d9a9SClaudio Takahasi } 29933300d9a9SClaudio Takahasi } 29943300d9a9SClaudio Takahasi 29953300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn, 29963300d9a9SClaudio Takahasi struct sk_buff *skb) 29970a708f8fSGustavo F. Padovan { 29980a708f8fSGustavo F. Padovan u8 *data = skb->data; 29990a708f8fSGustavo F. Padovan int len = skb->len; 30000a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr cmd; 30013300d9a9SClaudio Takahasi int err; 30020a708f8fSGustavo F. Padovan 30030a708f8fSGustavo F. Padovan l2cap_raw_recv(conn, skb); 30040a708f8fSGustavo F. Padovan 30050a708f8fSGustavo F. Padovan while (len >= L2CAP_CMD_HDR_SIZE) { 30060a708f8fSGustavo F. Padovan u16 cmd_len; 30070a708f8fSGustavo F. Padovan memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); 30080a708f8fSGustavo F. Padovan data += L2CAP_CMD_HDR_SIZE; 30090a708f8fSGustavo F. Padovan len -= L2CAP_CMD_HDR_SIZE; 30100a708f8fSGustavo F. Padovan 30110a708f8fSGustavo F. Padovan cmd_len = le16_to_cpu(cmd.len); 30120a708f8fSGustavo F. Padovan 30130a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident); 30140a708f8fSGustavo F. Padovan 30150a708f8fSGustavo F. Padovan if (cmd_len > len || !cmd.ident) { 30160a708f8fSGustavo F. Padovan BT_DBG("corrupted command"); 30170a708f8fSGustavo F. Padovan break; 30180a708f8fSGustavo F. Padovan } 30190a708f8fSGustavo F. Padovan 30203300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 30213300d9a9SClaudio Takahasi err = l2cap_le_sig_cmd(conn, &cmd, data); 30223300d9a9SClaudio Takahasi else 30233300d9a9SClaudio Takahasi err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); 30240a708f8fSGustavo F. Padovan 30250a708f8fSGustavo F. Padovan if (err) { 30260a708f8fSGustavo F. Padovan struct l2cap_cmd_rej rej; 30272c6d1a2eSGustavo F. Padovan 30282c6d1a2eSGustavo F. Padovan BT_ERR("Wrong link type (%d)", err); 30290a708f8fSGustavo F. Padovan 30300a708f8fSGustavo F. Padovan /* FIXME: Map err to a valid reason */ 30310a708f8fSGustavo F. Padovan rej.reason = cpu_to_le16(0); 30320a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); 30330a708f8fSGustavo F. Padovan } 30340a708f8fSGustavo F. Padovan 30350a708f8fSGustavo F. Padovan data += cmd_len; 30360a708f8fSGustavo F. Padovan len -= cmd_len; 30370a708f8fSGustavo F. Padovan } 30380a708f8fSGustavo F. Padovan 30390a708f8fSGustavo F. Padovan kfree_skb(skb); 30400a708f8fSGustavo F. Padovan } 30410a708f8fSGustavo F. Padovan 304247d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb) 30430a708f8fSGustavo F. Padovan { 30440a708f8fSGustavo F. Padovan u16 our_fcs, rcv_fcs; 30450a708f8fSGustavo F. Padovan int hdr_size = L2CAP_HDR_SIZE + 2; 30460a708f8fSGustavo F. Padovan 304747d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 30480a708f8fSGustavo F. Padovan skb_trim(skb, skb->len - 2); 30490a708f8fSGustavo F. Padovan rcv_fcs = get_unaligned_le16(skb->data + skb->len); 30500a708f8fSGustavo F. Padovan our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); 30510a708f8fSGustavo F. Padovan 30520a708f8fSGustavo F. Padovan if (our_fcs != rcv_fcs) 30530a708f8fSGustavo F. Padovan return -EBADMSG; 30540a708f8fSGustavo F. Padovan } 30550a708f8fSGustavo F. Padovan return 0; 30560a708f8fSGustavo F. Padovan } 30570a708f8fSGustavo F. Padovan 3058525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan) 30590a708f8fSGustavo F. Padovan { 30600a708f8fSGustavo F. Padovan u16 control = 0; 30610a708f8fSGustavo F. Padovan 30626a026610SGustavo F. Padovan chan->frames_sent = 0; 30630a708f8fSGustavo F. Padovan 306442e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 30650a708f8fSGustavo F. Padovan 3066525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 30670a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 3068525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 3069525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 30700a708f8fSGustavo F. Padovan } 30710a708f8fSGustavo F. Padovan 3072525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REMOTE_BUSY) 3073525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 30740a708f8fSGustavo F. Padovan 3075525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 30760a708f8fSGustavo F. Padovan 3077525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_LOCAL_BUSY) && 30786a026610SGustavo F. Padovan chan->frames_sent == 0) { 30790a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 3080525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 30810a708f8fSGustavo F. Padovan } 30820a708f8fSGustavo F. Padovan } 30830a708f8fSGustavo F. Padovan 308442e5c802SGustavo F. Padovan static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u8 tx_seq, u8 sar) 30850a708f8fSGustavo F. Padovan { 30860a708f8fSGustavo F. Padovan struct sk_buff *next_skb; 30870a708f8fSGustavo F. Padovan int tx_seq_offset, next_tx_seq_offset; 30880a708f8fSGustavo F. Padovan 30890a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 30900a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 30910a708f8fSGustavo F. Padovan 3092f1c6775bSGustavo F. Padovan next_skb = skb_peek(&chan->srej_q); 30930a708f8fSGustavo F. Padovan if (!next_skb) { 3094f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 30950a708f8fSGustavo F. Padovan return 0; 30960a708f8fSGustavo F. Padovan } 30970a708f8fSGustavo F. Padovan 309842e5c802SGustavo F. Padovan tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; 30990a708f8fSGustavo F. Padovan if (tx_seq_offset < 0) 31000a708f8fSGustavo F. Padovan tx_seq_offset += 64; 31010a708f8fSGustavo F. Padovan 31020a708f8fSGustavo F. Padovan do { 31030a708f8fSGustavo F. Padovan if (bt_cb(next_skb)->tx_seq == tx_seq) 31040a708f8fSGustavo F. Padovan return -EINVAL; 31050a708f8fSGustavo F. Padovan 31060a708f8fSGustavo F. Padovan next_tx_seq_offset = (bt_cb(next_skb)->tx_seq - 310742e5c802SGustavo F. Padovan chan->buffer_seq) % 64; 31080a708f8fSGustavo F. Padovan if (next_tx_seq_offset < 0) 31090a708f8fSGustavo F. Padovan next_tx_seq_offset += 64; 31100a708f8fSGustavo F. Padovan 31110a708f8fSGustavo F. Padovan if (next_tx_seq_offset > tx_seq_offset) { 3112f1c6775bSGustavo F. Padovan __skb_queue_before(&chan->srej_q, next_skb, skb); 31130a708f8fSGustavo F. Padovan return 0; 31140a708f8fSGustavo F. Padovan } 31150a708f8fSGustavo F. Padovan 3116f1c6775bSGustavo F. Padovan if (skb_queue_is_last(&chan->srej_q, next_skb)) 31170a708f8fSGustavo F. Padovan break; 31180a708f8fSGustavo F. Padovan 3119f1c6775bSGustavo F. Padovan } while ((next_skb = skb_queue_next(&chan->srej_q, next_skb))); 31200a708f8fSGustavo F. Padovan 3121f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 31220a708f8fSGustavo F. Padovan 31230a708f8fSGustavo F. Padovan return 0; 31240a708f8fSGustavo F. Padovan } 31250a708f8fSGustavo F. Padovan 3126525cd185SGustavo F. Padovan static int l2cap_ertm_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 31270a708f8fSGustavo F. Padovan { 31280a708f8fSGustavo F. Padovan struct sk_buff *_skb; 31290a708f8fSGustavo F. Padovan int err; 31300a708f8fSGustavo F. Padovan 31310a708f8fSGustavo F. Padovan switch (control & L2CAP_CTRL_SAR) { 31320a708f8fSGustavo F. Padovan case L2CAP_SDU_UNSEGMENTED: 3133525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) 31340a708f8fSGustavo F. Padovan goto drop; 31350a708f8fSGustavo F. Padovan 3136224f8af0SRuiyi Zhang return sock_queue_rcv_skb(chan->sk, skb); 31370a708f8fSGustavo F. Padovan 31380a708f8fSGustavo F. Padovan case L2CAP_SDU_START: 3139525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) 31400a708f8fSGustavo F. Padovan goto drop; 31410a708f8fSGustavo F. Padovan 31426f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 31430a708f8fSGustavo F. Padovan 31440c1bc5c6SGustavo F. Padovan if (chan->sdu_len > chan->imtu) 31450a708f8fSGustavo F. Padovan goto disconnect; 31460a708f8fSGustavo F. Padovan 31476f61fd47SGustavo F. Padovan chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC); 31486f61fd47SGustavo F. Padovan if (!chan->sdu) 31490a708f8fSGustavo F. Padovan return -ENOMEM; 31500a708f8fSGustavo F. Padovan 31510a708f8fSGustavo F. Padovan /* pull sdu_len bytes only after alloc, because of Local Busy 31520a708f8fSGustavo F. Padovan * condition we have to be sure that this will be executed 31530a708f8fSGustavo F. Padovan * only once, i.e., when alloc does not fail */ 31540a708f8fSGustavo F. Padovan skb_pull(skb, 2); 31550a708f8fSGustavo F. Padovan 31566f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31570a708f8fSGustavo F. Padovan 3158525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_SDU; 31596f61fd47SGustavo F. Padovan chan->partial_sdu_len = skb->len; 31600a708f8fSGustavo F. Padovan break; 31610a708f8fSGustavo F. Padovan 31620a708f8fSGustavo F. Padovan case L2CAP_SDU_CONTINUE: 3163525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 31640a708f8fSGustavo F. Padovan goto disconnect; 31650a708f8fSGustavo F. Padovan 31666f61fd47SGustavo F. Padovan if (!chan->sdu) 31670a708f8fSGustavo F. Padovan goto disconnect; 31680a708f8fSGustavo F. Padovan 31696f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 31706f61fd47SGustavo F. Padovan if (chan->partial_sdu_len > chan->sdu_len) 31710a708f8fSGustavo F. Padovan goto drop; 31720a708f8fSGustavo F. Padovan 31736f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31740a708f8fSGustavo F. Padovan 31750a708f8fSGustavo F. Padovan break; 31760a708f8fSGustavo F. Padovan 31770a708f8fSGustavo F. Padovan case L2CAP_SDU_END: 3178525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 31790a708f8fSGustavo F. Padovan goto disconnect; 31800a708f8fSGustavo F. Padovan 31816f61fd47SGustavo F. Padovan if (!chan->sdu) 31820a708f8fSGustavo F. Padovan goto disconnect; 31830a708f8fSGustavo F. Padovan 3184525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_RETRY)) { 31856f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 31860a708f8fSGustavo F. Padovan 31870c1bc5c6SGustavo F. Padovan if (chan->partial_sdu_len > chan->imtu) 31880a708f8fSGustavo F. Padovan goto drop; 31890a708f8fSGustavo F. Padovan 31906f61fd47SGustavo F. Padovan if (chan->partial_sdu_len != chan->sdu_len) 31910a708f8fSGustavo F. Padovan goto drop; 31920a708f8fSGustavo F. Padovan 31936f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31940a708f8fSGustavo F. Padovan } 31950a708f8fSGustavo F. Padovan 31966f61fd47SGustavo F. Padovan _skb = skb_clone(chan->sdu, GFP_ATOMIC); 31970a708f8fSGustavo F. Padovan if (!_skb) { 3198525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_RETRY; 31990a708f8fSGustavo F. Padovan return -ENOMEM; 32000a708f8fSGustavo F. Padovan } 32010a708f8fSGustavo F. Padovan 3202525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, _skb); 32030a708f8fSGustavo F. Padovan if (err < 0) { 32040a708f8fSGustavo F. Padovan kfree_skb(_skb); 3205525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_RETRY; 32060a708f8fSGustavo F. Padovan return err; 32070a708f8fSGustavo F. Padovan } 32080a708f8fSGustavo F. Padovan 3209525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_RETRY; 3210525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_SDU; 32110a708f8fSGustavo F. Padovan 32126f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 32130a708f8fSGustavo F. Padovan break; 32140a708f8fSGustavo F. Padovan } 32150a708f8fSGustavo F. Padovan 32160a708f8fSGustavo F. Padovan kfree_skb(skb); 32170a708f8fSGustavo F. Padovan return 0; 32180a708f8fSGustavo F. Padovan 32190a708f8fSGustavo F. Padovan drop: 32206f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 32216f61fd47SGustavo F. Padovan chan->sdu = NULL; 32220a708f8fSGustavo F. Padovan 32230a708f8fSGustavo F. Padovan disconnect: 32248c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 32250a708f8fSGustavo F. Padovan kfree_skb(skb); 32260a708f8fSGustavo F. Padovan return 0; 32270a708f8fSGustavo F. Padovan } 32280a708f8fSGustavo F. Padovan 3229525cd185SGustavo F. Padovan static int l2cap_try_push_rx_skb(struct l2cap_chan *chan) 32300a708f8fSGustavo F. Padovan { 32310a708f8fSGustavo F. Padovan struct sk_buff *skb; 32320a708f8fSGustavo F. Padovan u16 control; 32330a708f8fSGustavo F. Padovan int err; 32340a708f8fSGustavo F. Padovan 3235f1c6775bSGustavo F. Padovan while ((skb = skb_dequeue(&chan->busy_q))) { 32360a708f8fSGustavo F. Padovan control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT; 3237525cd185SGustavo F. Padovan err = l2cap_ertm_reassembly_sdu(chan, skb, control); 32380a708f8fSGustavo F. Padovan if (err < 0) { 3239f1c6775bSGustavo F. Padovan skb_queue_head(&chan->busy_q, skb); 32400a708f8fSGustavo F. Padovan return -EBUSY; 32410a708f8fSGustavo F. Padovan } 32420a708f8fSGustavo F. Padovan 324342e5c802SGustavo F. Padovan chan->buffer_seq = (chan->buffer_seq + 1) % 64; 32440a708f8fSGustavo F. Padovan } 32450a708f8fSGustavo F. Padovan 3246525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_RNR_SENT)) 32470a708f8fSGustavo F. Padovan goto done; 32480a708f8fSGustavo F. Padovan 324942e5c802SGustavo F. Padovan control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 32500a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY | L2CAP_CTRL_POLL; 3251525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 32526a026610SGustavo F. Padovan chan->retry_count = 1; 32530a708f8fSGustavo F. Padovan 3254e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 32550a708f8fSGustavo F. Padovan __mod_monitor_timer(); 32560a708f8fSGustavo F. Padovan 3257525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_WAIT_F; 32580a708f8fSGustavo F. Padovan 32590a708f8fSGustavo F. Padovan done: 3260525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_LOCAL_BUSY; 3261525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_RNR_SENT; 32620a708f8fSGustavo F. Padovan 326349208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit local busy", chan); 32640a708f8fSGustavo F. Padovan 32650a708f8fSGustavo F. Padovan return 0; 32660a708f8fSGustavo F. Padovan } 32670a708f8fSGustavo F. Padovan 32680a708f8fSGustavo F. Padovan static void l2cap_busy_work(struct work_struct *work) 32690a708f8fSGustavo F. Padovan { 32700a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 3271311bb895SGustavo F. Padovan struct l2cap_chan *chan = 3272311bb895SGustavo F. Padovan container_of(work, struct l2cap_chan, busy_work); 3273311bb895SGustavo F. Padovan struct sock *sk = chan->sk; 32740a708f8fSGustavo F. Padovan int n_tries = 0, timeo = HZ/5, err; 32750a708f8fSGustavo F. Padovan struct sk_buff *skb; 32760a708f8fSGustavo F. Padovan 32770a708f8fSGustavo F. Padovan lock_sock(sk); 32780a708f8fSGustavo F. Padovan 32790a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 3280311bb895SGustavo F. Padovan while ((skb = skb_peek(&chan->busy_q))) { 32810a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 32820a708f8fSGustavo F. Padovan 32830a708f8fSGustavo F. Padovan if (n_tries++ > L2CAP_LOCAL_BUSY_TRIES) { 32840a708f8fSGustavo F. Padovan err = -EBUSY; 32858c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, EBUSY); 32860a708f8fSGustavo F. Padovan break; 32870a708f8fSGustavo F. Padovan } 32880a708f8fSGustavo F. Padovan 32890a708f8fSGustavo F. Padovan if (!timeo) 32900a708f8fSGustavo F. Padovan timeo = HZ/5; 32910a708f8fSGustavo F. Padovan 32920a708f8fSGustavo F. Padovan if (signal_pending(current)) { 32930a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 32940a708f8fSGustavo F. Padovan break; 32950a708f8fSGustavo F. Padovan } 32960a708f8fSGustavo F. Padovan 32970a708f8fSGustavo F. Padovan release_sock(sk); 32980a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 32990a708f8fSGustavo F. Padovan lock_sock(sk); 33000a708f8fSGustavo F. Padovan 33010a708f8fSGustavo F. Padovan err = sock_error(sk); 33020a708f8fSGustavo F. Padovan if (err) 33030a708f8fSGustavo F. Padovan break; 33040a708f8fSGustavo F. Padovan 3305311bb895SGustavo F. Padovan if (l2cap_try_push_rx_skb(chan) == 0) 33060a708f8fSGustavo F. Padovan break; 33070a708f8fSGustavo F. Padovan } 33080a708f8fSGustavo F. Padovan 33090a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 33100a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 33110a708f8fSGustavo F. Padovan 33120a708f8fSGustavo F. Padovan release_sock(sk); 33130a708f8fSGustavo F. Padovan } 33140a708f8fSGustavo F. Padovan 3315525cd185SGustavo F. Padovan static int l2cap_push_rx_skb(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 33160a708f8fSGustavo F. Padovan { 33170a708f8fSGustavo F. Padovan int sctrl, err; 33180a708f8fSGustavo F. Padovan 3319525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 33200a708f8fSGustavo F. Padovan bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT; 3321f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->busy_q, skb); 3322525cd185SGustavo F. Padovan return l2cap_try_push_rx_skb(chan); 33230a708f8fSGustavo F. Padovan 33240a708f8fSGustavo F. Padovan 33250a708f8fSGustavo F. Padovan } 33260a708f8fSGustavo F. Padovan 3327525cd185SGustavo F. Padovan err = l2cap_ertm_reassembly_sdu(chan, skb, control); 33280a708f8fSGustavo F. Padovan if (err >= 0) { 332942e5c802SGustavo F. Padovan chan->buffer_seq = (chan->buffer_seq + 1) % 64; 33300a708f8fSGustavo F. Padovan return err; 33310a708f8fSGustavo F. Padovan } 33320a708f8fSGustavo F. Padovan 33330a708f8fSGustavo F. Padovan /* Busy Condition */ 3334311bb895SGustavo F. Padovan BT_DBG("chan %p, Enter local busy", chan); 33350a708f8fSGustavo F. Padovan 3336525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_LOCAL_BUSY; 33370a708f8fSGustavo F. Padovan bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT; 3338f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->busy_q, skb); 33390a708f8fSGustavo F. Padovan 334042e5c802SGustavo F. Padovan sctrl = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 33410a708f8fSGustavo F. Padovan sctrl |= L2CAP_SUPER_RCV_NOT_READY; 3342525cd185SGustavo F. Padovan l2cap_send_sframe(chan, sctrl); 33430a708f8fSGustavo F. Padovan 3344525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 33450a708f8fSGustavo F. Padovan 3346e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 33470a708f8fSGustavo F. Padovan 3348311bb895SGustavo F. Padovan queue_work(_busy_wq, &chan->busy_work); 33490a708f8fSGustavo F. Padovan 33500a708f8fSGustavo F. Padovan return err; 33510a708f8fSGustavo F. Padovan } 33520a708f8fSGustavo F. Padovan 3353525cd185SGustavo F. Padovan static int l2cap_streaming_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 33540a708f8fSGustavo F. Padovan { 33550a708f8fSGustavo F. Padovan struct sk_buff *_skb; 33560a708f8fSGustavo F. Padovan int err = -EINVAL; 33570a708f8fSGustavo F. Padovan 33580a708f8fSGustavo F. Padovan /* 33590a708f8fSGustavo F. Padovan * TODO: We have to notify the userland if some data is lost with the 33600a708f8fSGustavo F. Padovan * Streaming Mode. 33610a708f8fSGustavo F. Padovan */ 33620a708f8fSGustavo F. Padovan 33630a708f8fSGustavo F. Padovan switch (control & L2CAP_CTRL_SAR) { 33640a708f8fSGustavo F. Padovan case L2CAP_SDU_UNSEGMENTED: 3365525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) { 33666f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 33670a708f8fSGustavo F. Padovan break; 33680a708f8fSGustavo F. Padovan } 33690a708f8fSGustavo F. Padovan 3370525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, skb); 33710a708f8fSGustavo F. Padovan if (!err) 33720a708f8fSGustavo F. Padovan return 0; 33730a708f8fSGustavo F. Padovan 33740a708f8fSGustavo F. Padovan break; 33750a708f8fSGustavo F. Padovan 33760a708f8fSGustavo F. Padovan case L2CAP_SDU_START: 3377525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) { 33786f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 33790a708f8fSGustavo F. Padovan break; 33800a708f8fSGustavo F. Padovan } 33810a708f8fSGustavo F. Padovan 33826f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 33830a708f8fSGustavo F. Padovan skb_pull(skb, 2); 33840a708f8fSGustavo F. Padovan 33850c1bc5c6SGustavo F. Padovan if (chan->sdu_len > chan->imtu) { 33860a708f8fSGustavo F. Padovan err = -EMSGSIZE; 33870a708f8fSGustavo F. Padovan break; 33880a708f8fSGustavo F. Padovan } 33890a708f8fSGustavo F. Padovan 33906f61fd47SGustavo F. Padovan chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC); 33916f61fd47SGustavo F. Padovan if (!chan->sdu) { 33920a708f8fSGustavo F. Padovan err = -ENOMEM; 33930a708f8fSGustavo F. Padovan break; 33940a708f8fSGustavo F. Padovan } 33950a708f8fSGustavo F. Padovan 33966f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 33970a708f8fSGustavo F. Padovan 3398525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_SDU; 33996f61fd47SGustavo F. Padovan chan->partial_sdu_len = skb->len; 34000a708f8fSGustavo F. Padovan err = 0; 34010a708f8fSGustavo F. Padovan break; 34020a708f8fSGustavo F. Padovan 34030a708f8fSGustavo F. Padovan case L2CAP_SDU_CONTINUE: 3404525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 34050a708f8fSGustavo F. Padovan break; 34060a708f8fSGustavo F. Padovan 34076f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 34080a708f8fSGustavo F. Padovan 34096f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 34106f61fd47SGustavo F. Padovan if (chan->partial_sdu_len > chan->sdu_len) 34116f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 34120a708f8fSGustavo F. Padovan else 34130a708f8fSGustavo F. Padovan err = 0; 34140a708f8fSGustavo F. Padovan 34150a708f8fSGustavo F. Padovan break; 34160a708f8fSGustavo F. Padovan 34170a708f8fSGustavo F. Padovan case L2CAP_SDU_END: 3418525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 34190a708f8fSGustavo F. Padovan break; 34200a708f8fSGustavo F. Padovan 34216f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 34220a708f8fSGustavo F. Padovan 3423525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_SDU; 34246f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 34250a708f8fSGustavo F. Padovan 34260c1bc5c6SGustavo F. Padovan if (chan->partial_sdu_len > chan->imtu) 34270a708f8fSGustavo F. Padovan goto drop; 34280a708f8fSGustavo F. Padovan 34296f61fd47SGustavo F. Padovan if (chan->partial_sdu_len == chan->sdu_len) { 34306f61fd47SGustavo F. Padovan _skb = skb_clone(chan->sdu, GFP_ATOMIC); 3431525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, _skb); 34320a708f8fSGustavo F. Padovan if (err < 0) 34330a708f8fSGustavo F. Padovan kfree_skb(_skb); 34340a708f8fSGustavo F. Padovan } 34350a708f8fSGustavo F. Padovan err = 0; 34360a708f8fSGustavo F. Padovan 34370a708f8fSGustavo F. Padovan drop: 34386f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 34390a708f8fSGustavo F. Padovan break; 34400a708f8fSGustavo F. Padovan } 34410a708f8fSGustavo F. Padovan 34420a708f8fSGustavo F. Padovan kfree_skb(skb); 34430a708f8fSGustavo F. Padovan return err; 34440a708f8fSGustavo F. Padovan } 34450a708f8fSGustavo F. Padovan 3446525cd185SGustavo F. Padovan static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq) 34470a708f8fSGustavo F. Padovan { 34480a708f8fSGustavo F. Padovan struct sk_buff *skb; 34490a708f8fSGustavo F. Padovan u16 control; 34500a708f8fSGustavo F. Padovan 3451f1c6775bSGustavo F. Padovan while ((skb = skb_peek(&chan->srej_q))) { 34520a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq != tx_seq) 34530a708f8fSGustavo F. Padovan break; 34540a708f8fSGustavo F. Padovan 3455f1c6775bSGustavo F. Padovan skb = skb_dequeue(&chan->srej_q); 34560a708f8fSGustavo F. Padovan control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT; 3457525cd185SGustavo F. Padovan l2cap_ertm_reassembly_sdu(chan, skb, control); 345842e5c802SGustavo F. Padovan chan->buffer_seq_srej = 345942e5c802SGustavo F. Padovan (chan->buffer_seq_srej + 1) % 64; 34600a708f8fSGustavo F. Padovan tx_seq = (tx_seq + 1) % 64; 34610a708f8fSGustavo F. Padovan } 34620a708f8fSGustavo F. Padovan } 34630a708f8fSGustavo F. Padovan 3464525cd185SGustavo F. Padovan static void l2cap_resend_srejframe(struct l2cap_chan *chan, u8 tx_seq) 34650a708f8fSGustavo F. Padovan { 34660a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 34670a708f8fSGustavo F. Padovan u16 control; 34680a708f8fSGustavo F. Padovan 346939d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 34700a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 34710a708f8fSGustavo F. Padovan list_del(&l->list); 34720a708f8fSGustavo F. Padovan kfree(l); 34730a708f8fSGustavo F. Padovan return; 34740a708f8fSGustavo F. Padovan } 34750a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 34760a708f8fSGustavo F. Padovan control |= l->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 3477525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 34780a708f8fSGustavo F. Padovan list_del(&l->list); 347939d5a3eeSGustavo F. Padovan list_add_tail(&l->list, &chan->srej_l); 34800a708f8fSGustavo F. Padovan } 34810a708f8fSGustavo F. Padovan } 34820a708f8fSGustavo F. Padovan 3483525cd185SGustavo F. Padovan static void l2cap_send_srejframe(struct l2cap_chan *chan, u8 tx_seq) 34840a708f8fSGustavo F. Padovan { 34850a708f8fSGustavo F. Padovan struct srej_list *new; 34860a708f8fSGustavo F. Padovan u16 control; 34870a708f8fSGustavo F. Padovan 348842e5c802SGustavo F. Padovan while (tx_seq != chan->expected_tx_seq) { 34890a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 349042e5c802SGustavo F. Padovan control |= chan->expected_tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 3491525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 34920a708f8fSGustavo F. Padovan 34930a708f8fSGustavo F. Padovan new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); 349442e5c802SGustavo F. Padovan new->tx_seq = chan->expected_tx_seq; 349542e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 349639d5a3eeSGustavo F. Padovan list_add_tail(&new->list, &chan->srej_l); 34970a708f8fSGustavo F. Padovan } 349842e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 34990a708f8fSGustavo F. Padovan } 35000a708f8fSGustavo F. Padovan 3501525cd185SGustavo F. Padovan static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) 35020a708f8fSGustavo F. Padovan { 35030a708f8fSGustavo F. Padovan u8 tx_seq = __get_txseq(rx_control); 35040a708f8fSGustavo F. Padovan u8 req_seq = __get_reqseq(rx_control); 35050a708f8fSGustavo F. Padovan u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT; 35060a708f8fSGustavo F. Padovan int tx_seq_offset, expected_tx_seq_offset; 350747d1ec61SGustavo F. Padovan int num_to_ack = (chan->tx_win/6) + 1; 35080a708f8fSGustavo F. Padovan int err = 0; 35090a708f8fSGustavo F. Padovan 3510525cd185SGustavo F. Padovan BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len, 3511525cd185SGustavo F. Padovan tx_seq, rx_control); 35120a708f8fSGustavo F. Padovan 35130a708f8fSGustavo F. Padovan if (L2CAP_CTRL_FINAL & rx_control && 3514525cd185SGustavo F. Padovan chan->conn_state & L2CAP_CONN_WAIT_F) { 3515e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 35166a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 35170a708f8fSGustavo F. Padovan __mod_retrans_timer(); 3518525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_WAIT_F; 35190a708f8fSGustavo F. Padovan } 35200a708f8fSGustavo F. Padovan 352142e5c802SGustavo F. Padovan chan->expected_ack_seq = req_seq; 352242e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 35230a708f8fSGustavo F. Padovan 352442e5c802SGustavo F. Padovan if (tx_seq == chan->expected_tx_seq) 35250a708f8fSGustavo F. Padovan goto expected; 35260a708f8fSGustavo F. Padovan 352742e5c802SGustavo F. Padovan tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; 35280a708f8fSGustavo F. Padovan if (tx_seq_offset < 0) 35290a708f8fSGustavo F. Padovan tx_seq_offset += 64; 35300a708f8fSGustavo F. Padovan 35310a708f8fSGustavo F. Padovan /* invalid tx_seq */ 353247d1ec61SGustavo F. Padovan if (tx_seq_offset >= chan->tx_win) { 35338c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 35340a708f8fSGustavo F. Padovan goto drop; 35350a708f8fSGustavo F. Padovan } 35360a708f8fSGustavo F. Padovan 3537525cd185SGustavo F. Padovan if (chan->conn_state == L2CAP_CONN_LOCAL_BUSY) 35380a708f8fSGustavo F. Padovan goto drop; 35390a708f8fSGustavo F. Padovan 3540525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 35410a708f8fSGustavo F. Padovan struct srej_list *first; 35420a708f8fSGustavo F. Padovan 354339d5a3eeSGustavo F. Padovan first = list_first_entry(&chan->srej_l, 35440a708f8fSGustavo F. Padovan struct srej_list, list); 35450a708f8fSGustavo F. Padovan if (tx_seq == first->tx_seq) { 354642e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 3547525cd185SGustavo F. Padovan l2cap_check_srej_gap(chan, tx_seq); 35480a708f8fSGustavo F. Padovan 35490a708f8fSGustavo F. Padovan list_del(&first->list); 35500a708f8fSGustavo F. Padovan kfree(first); 35510a708f8fSGustavo F. Padovan 355239d5a3eeSGustavo F. Padovan if (list_empty(&chan->srej_l)) { 355342e5c802SGustavo F. Padovan chan->buffer_seq = chan->buffer_seq_srej; 3554525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SREJ_SENT; 3555525cd185SGustavo F. Padovan l2cap_send_ack(chan); 355649208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit SREJ_SENT", chan); 35570a708f8fSGustavo F. Padovan } 35580a708f8fSGustavo F. Padovan } else { 35590a708f8fSGustavo F. Padovan struct srej_list *l; 35600a708f8fSGustavo F. Padovan 35610a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 356242e5c802SGustavo F. Padovan if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0) 35630a708f8fSGustavo F. Padovan goto drop; 35640a708f8fSGustavo F. Padovan 356539d5a3eeSGustavo F. Padovan list_for_each_entry(l, &chan->srej_l, list) { 35660a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 3567525cd185SGustavo F. Padovan l2cap_resend_srejframe(chan, tx_seq); 35680a708f8fSGustavo F. Padovan return 0; 35690a708f8fSGustavo F. Padovan } 35700a708f8fSGustavo F. Padovan } 3571525cd185SGustavo F. Padovan l2cap_send_srejframe(chan, tx_seq); 35720a708f8fSGustavo F. Padovan } 35730a708f8fSGustavo F. Padovan } else { 35740a708f8fSGustavo F. Padovan expected_tx_seq_offset = 357542e5c802SGustavo F. Padovan (chan->expected_tx_seq - chan->buffer_seq) % 64; 35760a708f8fSGustavo F. Padovan if (expected_tx_seq_offset < 0) 35770a708f8fSGustavo F. Padovan expected_tx_seq_offset += 64; 35780a708f8fSGustavo F. Padovan 35790a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 35800a708f8fSGustavo F. Padovan if (tx_seq_offset < expected_tx_seq_offset) 35810a708f8fSGustavo F. Padovan goto drop; 35820a708f8fSGustavo F. Padovan 3583525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_SENT; 35840a708f8fSGustavo F. Padovan 358549208c9cSGustavo F. Padovan BT_DBG("chan %p, Enter SREJ", chan); 35860a708f8fSGustavo F. Padovan 358739d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 358842e5c802SGustavo F. Padovan chan->buffer_seq_srej = chan->buffer_seq; 35890a708f8fSGustavo F. Padovan 3590f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->srej_q); 3591f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->busy_q); 359242e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 35930a708f8fSGustavo F. Padovan 3594525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_PBIT; 35950a708f8fSGustavo F. Padovan 3596525cd185SGustavo F. Padovan l2cap_send_srejframe(chan, tx_seq); 35970a708f8fSGustavo F. Padovan 3598e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 35990a708f8fSGustavo F. Padovan } 36000a708f8fSGustavo F. Padovan return 0; 36010a708f8fSGustavo F. Padovan 36020a708f8fSGustavo F. Padovan expected: 360342e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 36040a708f8fSGustavo F. Padovan 3605525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 36060a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 36070a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 3608f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 36090a708f8fSGustavo F. Padovan return 0; 36100a708f8fSGustavo F. Padovan } 36110a708f8fSGustavo F. Padovan 3612525cd185SGustavo F. Padovan err = l2cap_push_rx_skb(chan, skb, rx_control); 36130a708f8fSGustavo F. Padovan if (err < 0) 36140a708f8fSGustavo F. Padovan return 0; 36150a708f8fSGustavo F. Padovan 36160a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_FINAL) { 3617525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3618525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 36190a708f8fSGustavo F. Padovan else 3620525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 36210a708f8fSGustavo F. Padovan } 36220a708f8fSGustavo F. Padovan 36230a708f8fSGustavo F. Padovan __mod_ack_timer(); 36240a708f8fSGustavo F. Padovan 36256a026610SGustavo F. Padovan chan->num_acked = (chan->num_acked + 1) % num_to_ack; 36266a026610SGustavo F. Padovan if (chan->num_acked == num_to_ack - 1) 3627525cd185SGustavo F. Padovan l2cap_send_ack(chan); 36280a708f8fSGustavo F. Padovan 36290a708f8fSGustavo F. Padovan return 0; 36300a708f8fSGustavo F. Padovan 36310a708f8fSGustavo F. Padovan drop: 36320a708f8fSGustavo F. Padovan kfree_skb(skb); 36330a708f8fSGustavo F. Padovan return 0; 36340a708f8fSGustavo F. Padovan } 36350a708f8fSGustavo F. Padovan 3636525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control) 36370a708f8fSGustavo F. Padovan { 363849208c9cSGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, __get_reqseq(rx_control), 36390a708f8fSGustavo F. Padovan rx_control); 36400a708f8fSGustavo F. Padovan 364142e5c802SGustavo F. Padovan chan->expected_ack_seq = __get_reqseq(rx_control); 364242e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 36430a708f8fSGustavo F. Padovan 36440a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) { 3645525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 3646525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 3647525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && 36486a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 36490a708f8fSGustavo F. Padovan __mod_retrans_timer(); 36500a708f8fSGustavo F. Padovan 3651525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 3652525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 36530a708f8fSGustavo F. Padovan } else { 3654525cd185SGustavo F. Padovan l2cap_send_i_or_rr_or_rnr(chan); 36550a708f8fSGustavo F. Padovan } 36560a708f8fSGustavo F. Padovan 36570a708f8fSGustavo F. Padovan } else if (rx_control & L2CAP_CTRL_FINAL) { 3658525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 36590a708f8fSGustavo F. Padovan 3660525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3661525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 36620a708f8fSGustavo F. Padovan else 3663525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 36640a708f8fSGustavo F. Padovan 36650a708f8fSGustavo F. Padovan } else { 3666525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && 36676a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 36680a708f8fSGustavo F. Padovan __mod_retrans_timer(); 36690a708f8fSGustavo F. Padovan 3670525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 3671525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) 3672525cd185SGustavo F. Padovan l2cap_send_ack(chan); 36730a708f8fSGustavo F. Padovan else 3674525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 36750a708f8fSGustavo F. Padovan } 36760a708f8fSGustavo F. Padovan } 36770a708f8fSGustavo F. Padovan 3678525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control) 36790a708f8fSGustavo F. Padovan { 36800a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 36810a708f8fSGustavo F. Padovan 3682525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 36830a708f8fSGustavo F. Padovan 3684525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 36850a708f8fSGustavo F. Padovan 368642e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 368742e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 36880a708f8fSGustavo F. Padovan 36890a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_FINAL) { 3690525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3691525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 36920a708f8fSGustavo F. Padovan else 3693525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 36940a708f8fSGustavo F. Padovan } else { 3695525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 36960a708f8fSGustavo F. Padovan 3697525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) 3698525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_REJ_ACT; 36990a708f8fSGustavo F. Padovan } 37000a708f8fSGustavo F. Padovan } 3701525cd185SGustavo F. Padovan static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control) 37020a708f8fSGustavo F. Padovan { 37030a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 37040a708f8fSGustavo F. Padovan 3705525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 37060a708f8fSGustavo F. Padovan 3707525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 37080a708f8fSGustavo F. Padovan 37090a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) { 371042e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 371142e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 37120a708f8fSGustavo F. Padovan 3713525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 3714525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 37150a708f8fSGustavo F. Padovan 3716525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 37170a708f8fSGustavo F. Padovan 3718525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) { 37196a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 3720525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_ACT; 37210a708f8fSGustavo F. Padovan } 37220a708f8fSGustavo F. Padovan } else if (rx_control & L2CAP_CTRL_FINAL) { 3723525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_SREJ_ACT) && 37246a026610SGustavo F. Padovan chan->srej_save_reqseq == tx_seq) 3725525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SREJ_ACT; 37260a708f8fSGustavo F. Padovan else 3727525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 37280a708f8fSGustavo F. Padovan } else { 3729525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 3730525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) { 37316a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 3732525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_ACT; 37330a708f8fSGustavo F. Padovan } 37340a708f8fSGustavo F. Padovan } 37350a708f8fSGustavo F. Padovan } 37360a708f8fSGustavo F. Padovan 3737525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control) 37380a708f8fSGustavo F. Padovan { 37390a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 37400a708f8fSGustavo F. Padovan 3741525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 37420a708f8fSGustavo F. Padovan 3743525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_REMOTE_BUSY; 374442e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 374542e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 37460a708f8fSGustavo F. Padovan 37470a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3748525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 37490a708f8fSGustavo F. Padovan 3750525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SREJ_SENT)) { 3751e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 37520a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3753525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); 37540a708f8fSGustavo F. Padovan return; 37550a708f8fSGustavo F. Padovan } 37560a708f8fSGustavo F. Padovan 37570a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3758525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 37590a708f8fSGustavo F. Padovan else 3760525cd185SGustavo F. Padovan l2cap_send_sframe(chan, L2CAP_SUPER_RCV_READY); 37610a708f8fSGustavo F. Padovan } 37620a708f8fSGustavo F. Padovan 3763525cd185SGustavo F. Padovan static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) 37640a708f8fSGustavo F. Padovan { 3765525cd185SGustavo F. Padovan BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len); 37660a708f8fSGustavo F. Padovan 37670a708f8fSGustavo F. Padovan if (L2CAP_CTRL_FINAL & rx_control && 3768525cd185SGustavo F. Padovan chan->conn_state & L2CAP_CONN_WAIT_F) { 3769e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 37706a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 37710a708f8fSGustavo F. Padovan __mod_retrans_timer(); 3772525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_WAIT_F; 37730a708f8fSGustavo F. Padovan } 37740a708f8fSGustavo F. Padovan 37750a708f8fSGustavo F. Padovan switch (rx_control & L2CAP_CTRL_SUPERVISE) { 37760a708f8fSGustavo F. Padovan case L2CAP_SUPER_RCV_READY: 3777525cd185SGustavo F. Padovan l2cap_data_channel_rrframe(chan, rx_control); 37780a708f8fSGustavo F. Padovan break; 37790a708f8fSGustavo F. Padovan 37800a708f8fSGustavo F. Padovan case L2CAP_SUPER_REJECT: 3781525cd185SGustavo F. Padovan l2cap_data_channel_rejframe(chan, rx_control); 37820a708f8fSGustavo F. Padovan break; 37830a708f8fSGustavo F. Padovan 37840a708f8fSGustavo F. Padovan case L2CAP_SUPER_SELECT_REJECT: 3785525cd185SGustavo F. Padovan l2cap_data_channel_srejframe(chan, rx_control); 37860a708f8fSGustavo F. Padovan break; 37870a708f8fSGustavo F. Padovan 37880a708f8fSGustavo F. Padovan case L2CAP_SUPER_RCV_NOT_READY: 3789525cd185SGustavo F. Padovan l2cap_data_channel_rnrframe(chan, rx_control); 37900a708f8fSGustavo F. Padovan break; 37910a708f8fSGustavo F. Padovan } 37920a708f8fSGustavo F. Padovan 37930a708f8fSGustavo F. Padovan kfree_skb(skb); 37940a708f8fSGustavo F. Padovan return 0; 37950a708f8fSGustavo F. Padovan } 37960a708f8fSGustavo F. Padovan 37970a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb) 37980a708f8fSGustavo F. Padovan { 3799525cd185SGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 38000a708f8fSGustavo F. Padovan u16 control; 38010a708f8fSGustavo F. Padovan u8 req_seq; 38020a708f8fSGustavo F. Padovan int len, next_tx_seq_offset, req_seq_offset; 38030a708f8fSGustavo F. Padovan 38040a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data); 38050a708f8fSGustavo F. Padovan skb_pull(skb, 2); 38060a708f8fSGustavo F. Padovan len = skb->len; 38070a708f8fSGustavo F. Padovan 38080a708f8fSGustavo F. Padovan /* 38090a708f8fSGustavo F. Padovan * We can just drop the corrupted I-frame here. 38100a708f8fSGustavo F. Padovan * Receiver will miss it and start proper recovery 38110a708f8fSGustavo F. Padovan * procedures and ask retransmission. 38120a708f8fSGustavo F. Padovan */ 381347d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 38140a708f8fSGustavo F. Padovan goto drop; 38150a708f8fSGustavo F. Padovan 38160a708f8fSGustavo F. Padovan if (__is_sar_start(control) && __is_iframe(control)) 38170a708f8fSGustavo F. Padovan len -= 2; 38180a708f8fSGustavo F. Padovan 381947d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 38200a708f8fSGustavo F. Padovan len -= 2; 38210a708f8fSGustavo F. Padovan 382247d1ec61SGustavo F. Padovan if (len > chan->mps) { 38238c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38240a708f8fSGustavo F. Padovan goto drop; 38250a708f8fSGustavo F. Padovan } 38260a708f8fSGustavo F. Padovan 38270a708f8fSGustavo F. Padovan req_seq = __get_reqseq(control); 382842e5c802SGustavo F. Padovan req_seq_offset = (req_seq - chan->expected_ack_seq) % 64; 38290a708f8fSGustavo F. Padovan if (req_seq_offset < 0) 38300a708f8fSGustavo F. Padovan req_seq_offset += 64; 38310a708f8fSGustavo F. Padovan 38320a708f8fSGustavo F. Padovan next_tx_seq_offset = 383342e5c802SGustavo F. Padovan (chan->next_tx_seq - chan->expected_ack_seq) % 64; 38340a708f8fSGustavo F. Padovan if (next_tx_seq_offset < 0) 38350a708f8fSGustavo F. Padovan next_tx_seq_offset += 64; 38360a708f8fSGustavo F. Padovan 38370a708f8fSGustavo F. Padovan /* check for invalid req-seq */ 38380a708f8fSGustavo F. Padovan if (req_seq_offset > next_tx_seq_offset) { 38398c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38400a708f8fSGustavo F. Padovan goto drop; 38410a708f8fSGustavo F. Padovan } 38420a708f8fSGustavo F. Padovan 38430a708f8fSGustavo F. Padovan if (__is_iframe(control)) { 38440a708f8fSGustavo F. Padovan if (len < 0) { 38458c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38460a708f8fSGustavo F. Padovan goto drop; 38470a708f8fSGustavo F. Padovan } 38480a708f8fSGustavo F. Padovan 3849525cd185SGustavo F. Padovan l2cap_data_channel_iframe(chan, control, skb); 38500a708f8fSGustavo F. Padovan } else { 38510a708f8fSGustavo F. Padovan if (len != 0) { 38520a708f8fSGustavo F. Padovan BT_ERR("%d", len); 38538c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38540a708f8fSGustavo F. Padovan goto drop; 38550a708f8fSGustavo F. Padovan } 38560a708f8fSGustavo F. Padovan 3857525cd185SGustavo F. Padovan l2cap_data_channel_sframe(chan, control, skb); 38580a708f8fSGustavo F. Padovan } 38590a708f8fSGustavo F. Padovan 38600a708f8fSGustavo F. Padovan return 0; 38610a708f8fSGustavo F. Padovan 38620a708f8fSGustavo F. Padovan drop: 38630a708f8fSGustavo F. Padovan kfree_skb(skb); 38640a708f8fSGustavo F. Padovan return 0; 38650a708f8fSGustavo F. Padovan } 38660a708f8fSGustavo F. Padovan 38670a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) 38680a708f8fSGustavo F. Padovan { 386948454079SGustavo F. Padovan struct l2cap_chan *chan; 3870bf734843SDavid S. Miller struct sock *sk = NULL; 38710a708f8fSGustavo F. Padovan u16 control; 38720a708f8fSGustavo F. Padovan u8 tx_seq; 38730a708f8fSGustavo F. Padovan int len; 38740a708f8fSGustavo F. Padovan 3875baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 387648454079SGustavo F. Padovan if (!chan) { 38770a708f8fSGustavo F. Padovan BT_DBG("unknown cid 0x%4.4x", cid); 38780a708f8fSGustavo F. Padovan goto drop; 38790a708f8fSGustavo F. Padovan } 38800a708f8fSGustavo F. Padovan 388148454079SGustavo F. Padovan sk = chan->sk; 38820a708f8fSGustavo F. Padovan 388349208c9cSGustavo F. Padovan BT_DBG("chan %p, len %d", chan, skb->len); 38840a708f8fSGustavo F. Padovan 38850a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 38860a708f8fSGustavo F. Padovan goto drop; 38870a708f8fSGustavo F. Padovan 38880c1bc5c6SGustavo F. Padovan switch (chan->mode) { 38890a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 38900a708f8fSGustavo F. Padovan /* If socket recv buffers overflows we drop data here 38910a708f8fSGustavo F. Padovan * which is *bad* because L2CAP has to be reliable. 38920a708f8fSGustavo F. Padovan * But we don't have any other choice. L2CAP doesn't 38930a708f8fSGustavo F. Padovan * provide flow control mechanism. */ 38940a708f8fSGustavo F. Padovan 38950c1bc5c6SGustavo F. Padovan if (chan->imtu < skb->len) 38960a708f8fSGustavo F. Padovan goto drop; 38970a708f8fSGustavo F. Padovan 38980a708f8fSGustavo F. Padovan if (!sock_queue_rcv_skb(sk, skb)) 38990a708f8fSGustavo F. Padovan goto done; 39000a708f8fSGustavo F. Padovan break; 39010a708f8fSGustavo F. Padovan 39020a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 39030a708f8fSGustavo F. Padovan if (!sock_owned_by_user(sk)) { 39040a708f8fSGustavo F. Padovan l2cap_ertm_data_rcv(sk, skb); 39050a708f8fSGustavo F. Padovan } else { 39060a708f8fSGustavo F. Padovan if (sk_add_backlog(sk, skb)) 39070a708f8fSGustavo F. Padovan goto drop; 39080a708f8fSGustavo F. Padovan } 39090a708f8fSGustavo F. Padovan 39100a708f8fSGustavo F. Padovan goto done; 39110a708f8fSGustavo F. Padovan 39120a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 39130a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data); 39140a708f8fSGustavo F. Padovan skb_pull(skb, 2); 39150a708f8fSGustavo F. Padovan len = skb->len; 39160a708f8fSGustavo F. Padovan 391747d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 39180a708f8fSGustavo F. Padovan goto drop; 39190a708f8fSGustavo F. Padovan 39200a708f8fSGustavo F. Padovan if (__is_sar_start(control)) 39210a708f8fSGustavo F. Padovan len -= 2; 39220a708f8fSGustavo F. Padovan 392347d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 39240a708f8fSGustavo F. Padovan len -= 2; 39250a708f8fSGustavo F. Padovan 392647d1ec61SGustavo F. Padovan if (len > chan->mps || len < 0 || __is_sframe(control)) 39270a708f8fSGustavo F. Padovan goto drop; 39280a708f8fSGustavo F. Padovan 39290a708f8fSGustavo F. Padovan tx_seq = __get_txseq(control); 39300a708f8fSGustavo F. Padovan 393142e5c802SGustavo F. Padovan if (chan->expected_tx_seq == tx_seq) 393242e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 39330a708f8fSGustavo F. Padovan else 393442e5c802SGustavo F. Padovan chan->expected_tx_seq = (tx_seq + 1) % 64; 39350a708f8fSGustavo F. Padovan 3936525cd185SGustavo F. Padovan l2cap_streaming_reassembly_sdu(chan, skb, control); 39370a708f8fSGustavo F. Padovan 39380a708f8fSGustavo F. Padovan goto done; 39390a708f8fSGustavo F. Padovan 39400a708f8fSGustavo F. Padovan default: 39410c1bc5c6SGustavo F. Padovan BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode); 39420a708f8fSGustavo F. Padovan break; 39430a708f8fSGustavo F. Padovan } 39440a708f8fSGustavo F. Padovan 39450a708f8fSGustavo F. Padovan drop: 39460a708f8fSGustavo F. Padovan kfree_skb(skb); 39470a708f8fSGustavo F. Padovan 39480a708f8fSGustavo F. Padovan done: 39490a708f8fSGustavo F. Padovan if (sk) 39500a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 39510a708f8fSGustavo F. Padovan 39520a708f8fSGustavo F. Padovan return 0; 39530a708f8fSGustavo F. Padovan } 39540a708f8fSGustavo F. Padovan 39550a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb) 39560a708f8fSGustavo F. Padovan { 39576dcae1eaSDavid S. Miller struct sock *sk = NULL; 395823691d75SGustavo F. Padovan struct l2cap_chan *chan; 39590a708f8fSGustavo F. Padovan 396023691d75SGustavo F. Padovan chan = l2cap_global_chan_by_psm(0, psm, conn->src); 396123691d75SGustavo F. Padovan if (!chan) 39620a708f8fSGustavo F. Padovan goto drop; 39630a708f8fSGustavo F. Padovan 396423691d75SGustavo F. Padovan sk = chan->sk; 396523691d75SGustavo F. Padovan 39660a708f8fSGustavo F. Padovan bh_lock_sock(sk); 39670a708f8fSGustavo F. Padovan 39680a708f8fSGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 39690a708f8fSGustavo F. Padovan 39700a708f8fSGustavo F. Padovan if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) 39710a708f8fSGustavo F. Padovan goto drop; 39720a708f8fSGustavo F. Padovan 39730c1bc5c6SGustavo F. Padovan if (l2cap_pi(sk)->chan->imtu < skb->len) 39740a708f8fSGustavo F. Padovan goto drop; 39750a708f8fSGustavo F. Padovan 39760a708f8fSGustavo F. Padovan if (!sock_queue_rcv_skb(sk, skb)) 39770a708f8fSGustavo F. Padovan goto done; 39780a708f8fSGustavo F. Padovan 39790a708f8fSGustavo F. Padovan drop: 39800a708f8fSGustavo F. Padovan kfree_skb(skb); 39810a708f8fSGustavo F. Padovan 39820a708f8fSGustavo F. Padovan done: 39830a708f8fSGustavo F. Padovan if (sk) 39840a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 39850a708f8fSGustavo F. Padovan return 0; 39860a708f8fSGustavo F. Padovan } 39870a708f8fSGustavo F. Padovan 39889f69bda6SGustavo F. Padovan static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb) 39899f69bda6SGustavo F. Padovan { 39906dcae1eaSDavid S. Miller struct sock *sk = NULL; 399123691d75SGustavo F. Padovan struct l2cap_chan *chan; 39929f69bda6SGustavo F. Padovan 399323691d75SGustavo F. Padovan chan = l2cap_global_chan_by_scid(0, cid, conn->src); 399423691d75SGustavo F. Padovan if (!chan) 39959f69bda6SGustavo F. Padovan goto drop; 39969f69bda6SGustavo F. Padovan 399723691d75SGustavo F. Padovan sk = chan->sk; 399823691d75SGustavo F. Padovan 39999f69bda6SGustavo F. Padovan bh_lock_sock(sk); 40009f69bda6SGustavo F. Padovan 40019f69bda6SGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 40029f69bda6SGustavo F. Padovan 40039f69bda6SGustavo F. Padovan if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) 40049f69bda6SGustavo F. Padovan goto drop; 40059f69bda6SGustavo F. Padovan 40060c1bc5c6SGustavo F. Padovan if (l2cap_pi(sk)->chan->imtu < skb->len) 40079f69bda6SGustavo F. Padovan goto drop; 40089f69bda6SGustavo F. Padovan 40099f69bda6SGustavo F. Padovan if (!sock_queue_rcv_skb(sk, skb)) 40109f69bda6SGustavo F. Padovan goto done; 40119f69bda6SGustavo F. Padovan 40129f69bda6SGustavo F. Padovan drop: 40139f69bda6SGustavo F. Padovan kfree_skb(skb); 40149f69bda6SGustavo F. Padovan 40159f69bda6SGustavo F. Padovan done: 40169f69bda6SGustavo F. Padovan if (sk) 40179f69bda6SGustavo F. Padovan bh_unlock_sock(sk); 40189f69bda6SGustavo F. Padovan return 0; 40199f69bda6SGustavo F. Padovan } 40209f69bda6SGustavo F. Padovan 40210a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) 40220a708f8fSGustavo F. Padovan { 40230a708f8fSGustavo F. Padovan struct l2cap_hdr *lh = (void *) skb->data; 40240a708f8fSGustavo F. Padovan u16 cid, len; 40250a708f8fSGustavo F. Padovan __le16 psm; 40260a708f8fSGustavo F. Padovan 40270a708f8fSGustavo F. Padovan skb_pull(skb, L2CAP_HDR_SIZE); 40280a708f8fSGustavo F. Padovan cid = __le16_to_cpu(lh->cid); 40290a708f8fSGustavo F. Padovan len = __le16_to_cpu(lh->len); 40300a708f8fSGustavo F. Padovan 40310a708f8fSGustavo F. Padovan if (len != skb->len) { 40320a708f8fSGustavo F. Padovan kfree_skb(skb); 40330a708f8fSGustavo F. Padovan return; 40340a708f8fSGustavo F. Padovan } 40350a708f8fSGustavo F. Padovan 40360a708f8fSGustavo F. Padovan BT_DBG("len %d, cid 0x%4.4x", len, cid); 40370a708f8fSGustavo F. Padovan 40380a708f8fSGustavo F. Padovan switch (cid) { 40393300d9a9SClaudio Takahasi case L2CAP_CID_LE_SIGNALING: 40400a708f8fSGustavo F. Padovan case L2CAP_CID_SIGNALING: 40410a708f8fSGustavo F. Padovan l2cap_sig_channel(conn, skb); 40420a708f8fSGustavo F. Padovan break; 40430a708f8fSGustavo F. Padovan 40440a708f8fSGustavo F. Padovan case L2CAP_CID_CONN_LESS: 40450a708f8fSGustavo F. Padovan psm = get_unaligned_le16(skb->data); 40460a708f8fSGustavo F. Padovan skb_pull(skb, 2); 40470a708f8fSGustavo F. Padovan l2cap_conless_channel(conn, psm, skb); 40480a708f8fSGustavo F. Padovan break; 40490a708f8fSGustavo F. Padovan 40509f69bda6SGustavo F. Padovan case L2CAP_CID_LE_DATA: 40519f69bda6SGustavo F. Padovan l2cap_att_channel(conn, cid, skb); 40529f69bda6SGustavo F. Padovan break; 40539f69bda6SGustavo F. Padovan 40540a708f8fSGustavo F. Padovan default: 40550a708f8fSGustavo F. Padovan l2cap_data_channel(conn, cid, skb); 40560a708f8fSGustavo F. Padovan break; 40570a708f8fSGustavo F. Padovan } 40580a708f8fSGustavo F. Padovan } 40590a708f8fSGustavo F. Padovan 40600a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */ 40610a708f8fSGustavo F. Padovan 40620a708f8fSGustavo F. Padovan static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) 40630a708f8fSGustavo F. Padovan { 40640a708f8fSGustavo F. Padovan int exact = 0, lm1 = 0, lm2 = 0; 406523691d75SGustavo F. Padovan struct l2cap_chan *c; 40660a708f8fSGustavo F. Padovan 40670a708f8fSGustavo F. Padovan if (type != ACL_LINK) 40680a708f8fSGustavo F. Padovan return -EINVAL; 40690a708f8fSGustavo F. Padovan 40700a708f8fSGustavo F. Padovan BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); 40710a708f8fSGustavo F. Padovan 40720a708f8fSGustavo F. Padovan /* Find listening sockets and check their link_mode */ 407323691d75SGustavo F. Padovan read_lock(&chan_list_lock); 407423691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 407523691d75SGustavo F. Padovan struct sock *sk = c->sk; 40764343478fSGustavo F. Padovan 40770a708f8fSGustavo F. Padovan if (sk->sk_state != BT_LISTEN) 40780a708f8fSGustavo F. Padovan continue; 40790a708f8fSGustavo F. Padovan 40800a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { 40810a708f8fSGustavo F. Padovan lm1 |= HCI_LM_ACCEPT; 408223691d75SGustavo F. Padovan if (c->role_switch) 40830a708f8fSGustavo F. Padovan lm1 |= HCI_LM_MASTER; 40840a708f8fSGustavo F. Padovan exact++; 40850a708f8fSGustavo F. Padovan } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { 40860a708f8fSGustavo F. Padovan lm2 |= HCI_LM_ACCEPT; 408723691d75SGustavo F. Padovan if (c->role_switch) 40880a708f8fSGustavo F. Padovan lm2 |= HCI_LM_MASTER; 40890a708f8fSGustavo F. Padovan } 40900a708f8fSGustavo F. Padovan } 409123691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 40920a708f8fSGustavo F. Padovan 40930a708f8fSGustavo F. Padovan return exact ? lm1 : lm2; 40940a708f8fSGustavo F. Padovan } 40950a708f8fSGustavo F. Padovan 40960a708f8fSGustavo F. Padovan static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) 40970a708f8fSGustavo F. Padovan { 40980a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 40990a708f8fSGustavo F. Padovan 41000a708f8fSGustavo F. Padovan BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); 41010a708f8fSGustavo F. Padovan 4102acd7d370SVille Tervo if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) 41030a708f8fSGustavo F. Padovan return -EINVAL; 41040a708f8fSGustavo F. Padovan 41050a708f8fSGustavo F. Padovan if (!status) { 41060a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, status); 41070a708f8fSGustavo F. Padovan if (conn) 41080a708f8fSGustavo F. Padovan l2cap_conn_ready(conn); 41090a708f8fSGustavo F. Padovan } else 41100a708f8fSGustavo F. Padovan l2cap_conn_del(hcon, bt_err(status)); 41110a708f8fSGustavo F. Padovan 41120a708f8fSGustavo F. Padovan return 0; 41130a708f8fSGustavo F. Padovan } 41140a708f8fSGustavo F. Padovan 41150a708f8fSGustavo F. Padovan static int l2cap_disconn_ind(struct hci_conn *hcon) 41160a708f8fSGustavo F. Padovan { 41170a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 41180a708f8fSGustavo F. Padovan 41190a708f8fSGustavo F. Padovan BT_DBG("hcon %p", hcon); 41200a708f8fSGustavo F. Padovan 41210a708f8fSGustavo F. Padovan if (hcon->type != ACL_LINK || !conn) 41220a708f8fSGustavo F. Padovan return 0x13; 41230a708f8fSGustavo F. Padovan 41240a708f8fSGustavo F. Padovan return conn->disc_reason; 41250a708f8fSGustavo F. Padovan } 41260a708f8fSGustavo F. Padovan 41270a708f8fSGustavo F. Padovan static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) 41280a708f8fSGustavo F. Padovan { 41290a708f8fSGustavo F. Padovan BT_DBG("hcon %p reason %d", hcon, reason); 41300a708f8fSGustavo F. Padovan 4131acd7d370SVille Tervo if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) 41320a708f8fSGustavo F. Padovan return -EINVAL; 41330a708f8fSGustavo F. Padovan 41340a708f8fSGustavo F. Padovan l2cap_conn_del(hcon, bt_err(reason)); 41350a708f8fSGustavo F. Padovan 41360a708f8fSGustavo F. Padovan return 0; 41370a708f8fSGustavo F. Padovan } 41380a708f8fSGustavo F. Padovan 41394343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) 41400a708f8fSGustavo F. Padovan { 4141715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) 41420a708f8fSGustavo F. Padovan return; 41430a708f8fSGustavo F. Padovan 41440a708f8fSGustavo F. Padovan if (encrypt == 0x00) { 41454343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) { 4146ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 4147ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ * 5); 41484343478fSGustavo F. Padovan } else if (chan->sec_level == BT_SECURITY_HIGH) 41490f852724SGustavo F. Padovan l2cap_chan_close(chan, ECONNREFUSED); 41500a708f8fSGustavo F. Padovan } else { 41514343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) 4152ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 41530a708f8fSGustavo F. Padovan } 41540a708f8fSGustavo F. Padovan } 41550a708f8fSGustavo F. Padovan 41560a708f8fSGustavo F. Padovan static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) 41570a708f8fSGustavo F. Padovan { 41580a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 415948454079SGustavo F. Padovan struct l2cap_chan *chan; 41600a708f8fSGustavo F. Padovan 41610a708f8fSGustavo F. Padovan if (!conn) 41620a708f8fSGustavo F. Padovan return 0; 41630a708f8fSGustavo F. Padovan 41640a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 41650a708f8fSGustavo F. Padovan 4166baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 41670a708f8fSGustavo F. Padovan 4168baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 416948454079SGustavo F. Padovan struct sock *sk = chan->sk; 4170baa7e1faSGustavo F. Padovan 41710a708f8fSGustavo F. Padovan bh_lock_sock(sk); 41720a708f8fSGustavo F. Padovan 4173b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_CONNECT_PEND) { 41740a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 41750a708f8fSGustavo F. Padovan continue; 41760a708f8fSGustavo F. Padovan } 41770a708f8fSGustavo F. Padovan 41780a708f8fSGustavo F. Padovan if (!status && (sk->sk_state == BT_CONNECTED || 41790a708f8fSGustavo F. Padovan sk->sk_state == BT_CONFIG)) { 41804343478fSGustavo F. Padovan l2cap_check_encryption(chan, encrypt); 41810a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 41820a708f8fSGustavo F. Padovan continue; 41830a708f8fSGustavo F. Padovan } 41840a708f8fSGustavo F. Padovan 41850a708f8fSGustavo F. Padovan if (sk->sk_state == BT_CONNECT) { 41860a708f8fSGustavo F. Padovan if (!status) { 41870a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 4188fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 4189fe4128e0SGustavo F. Padovan req.psm = chan->psm; 41900a708f8fSGustavo F. Padovan 4191fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 4192b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 41930a708f8fSGustavo F. Padovan 4194fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 41950a708f8fSGustavo F. Padovan L2CAP_CONN_REQ, sizeof(req), &req); 41960a708f8fSGustavo F. Padovan } else { 4197ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 4198ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 10); 41990a708f8fSGustavo F. Padovan } 42000a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT2) { 42010a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 42020a708f8fSGustavo F. Padovan __u16 result; 42030a708f8fSGustavo F. Padovan 42040a708f8fSGustavo F. Padovan if (!status) { 42050a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 42060a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 42070a708f8fSGustavo F. Padovan } else { 42080a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 4209ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 10); 42100a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 42110a708f8fSGustavo F. Padovan } 42120a708f8fSGustavo F. Padovan 4213fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4214fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 42150a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 42160a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 4217fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4218fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 42190a708f8fSGustavo F. Padovan } 42200a708f8fSGustavo F. Padovan 42210a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 42220a708f8fSGustavo F. Padovan } 42230a708f8fSGustavo F. Padovan 4224baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 42250a708f8fSGustavo F. Padovan 42260a708f8fSGustavo F. Padovan return 0; 42270a708f8fSGustavo F. Padovan } 42280a708f8fSGustavo F. Padovan 42290a708f8fSGustavo F. Padovan static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) 42300a708f8fSGustavo F. Padovan { 42310a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 42320a708f8fSGustavo F. Padovan 42330a708f8fSGustavo F. Padovan if (!conn) 42340a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 42350a708f8fSGustavo F. Padovan 42360a708f8fSGustavo F. Padovan if (!conn) 42370a708f8fSGustavo F. Padovan goto drop; 42380a708f8fSGustavo F. Padovan 42390a708f8fSGustavo F. Padovan BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); 42400a708f8fSGustavo F. Padovan 42410a708f8fSGustavo F. Padovan if (!(flags & ACL_CONT)) { 42420a708f8fSGustavo F. Padovan struct l2cap_hdr *hdr; 424348454079SGustavo F. Padovan struct l2cap_chan *chan; 42440a708f8fSGustavo F. Padovan u16 cid; 42450a708f8fSGustavo F. Padovan int len; 42460a708f8fSGustavo F. Padovan 42470a708f8fSGustavo F. Padovan if (conn->rx_len) { 42480a708f8fSGustavo F. Padovan BT_ERR("Unexpected start frame (len %d)", skb->len); 42490a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 42500a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 42510a708f8fSGustavo F. Padovan conn->rx_len = 0; 42520a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42530a708f8fSGustavo F. Padovan } 42540a708f8fSGustavo F. Padovan 42550a708f8fSGustavo F. Padovan /* Start fragment always begin with Basic L2CAP header */ 42560a708f8fSGustavo F. Padovan if (skb->len < L2CAP_HDR_SIZE) { 42570a708f8fSGustavo F. Padovan BT_ERR("Frame is too short (len %d)", skb->len); 42580a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42590a708f8fSGustavo F. Padovan goto drop; 42600a708f8fSGustavo F. Padovan } 42610a708f8fSGustavo F. Padovan 42620a708f8fSGustavo F. Padovan hdr = (struct l2cap_hdr *) skb->data; 42630a708f8fSGustavo F. Padovan len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; 42640a708f8fSGustavo F. Padovan cid = __le16_to_cpu(hdr->cid); 42650a708f8fSGustavo F. Padovan 42660a708f8fSGustavo F. Padovan if (len == skb->len) { 42670a708f8fSGustavo F. Padovan /* Complete frame received */ 42680a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, skb); 42690a708f8fSGustavo F. Padovan return 0; 42700a708f8fSGustavo F. Padovan } 42710a708f8fSGustavo F. Padovan 42720a708f8fSGustavo F. Padovan BT_DBG("Start: total len %d, frag len %d", len, skb->len); 42730a708f8fSGustavo F. Padovan 42740a708f8fSGustavo F. Padovan if (skb->len > len) { 42750a708f8fSGustavo F. Padovan BT_ERR("Frame is too long (len %d, expected len %d)", 42760a708f8fSGustavo F. Padovan skb->len, len); 42770a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42780a708f8fSGustavo F. Padovan goto drop; 42790a708f8fSGustavo F. Padovan } 42800a708f8fSGustavo F. Padovan 4281baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 42820a708f8fSGustavo F. Padovan 428348454079SGustavo F. Padovan if (chan && chan->sk) { 428448454079SGustavo F. Padovan struct sock *sk = chan->sk; 428548454079SGustavo F. Padovan 42860c1bc5c6SGustavo F. Padovan if (chan->imtu < len - L2CAP_HDR_SIZE) { 428748454079SGustavo F. Padovan BT_ERR("Frame exceeding recv MTU (len %d, " 428848454079SGustavo F. Padovan "MTU %d)", len, 42890c1bc5c6SGustavo F. Padovan chan->imtu); 42900a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 42910a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42920a708f8fSGustavo F. Padovan goto drop; 42930a708f8fSGustavo F. Padovan } 42940a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 429548454079SGustavo F. Padovan } 42960a708f8fSGustavo F. Padovan 42970a708f8fSGustavo F. Padovan /* Allocate skb for the complete frame (with header) */ 42980a708f8fSGustavo F. Padovan conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); 42990a708f8fSGustavo F. Padovan if (!conn->rx_skb) 43000a708f8fSGustavo F. Padovan goto drop; 43010a708f8fSGustavo F. Padovan 43020a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 43030a708f8fSGustavo F. Padovan skb->len); 43040a708f8fSGustavo F. Padovan conn->rx_len = len - skb->len; 43050a708f8fSGustavo F. Padovan } else { 43060a708f8fSGustavo F. Padovan BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); 43070a708f8fSGustavo F. Padovan 43080a708f8fSGustavo F. Padovan if (!conn->rx_len) { 43090a708f8fSGustavo F. Padovan BT_ERR("Unexpected continuation frame (len %d)", skb->len); 43100a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 43110a708f8fSGustavo F. Padovan goto drop; 43120a708f8fSGustavo F. Padovan } 43130a708f8fSGustavo F. Padovan 43140a708f8fSGustavo F. Padovan if (skb->len > conn->rx_len) { 43150a708f8fSGustavo F. Padovan BT_ERR("Fragment is too long (len %d, expected %d)", 43160a708f8fSGustavo F. Padovan skb->len, conn->rx_len); 43170a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 43180a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 43190a708f8fSGustavo F. Padovan conn->rx_len = 0; 43200a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 43210a708f8fSGustavo F. Padovan goto drop; 43220a708f8fSGustavo F. Padovan } 43230a708f8fSGustavo F. Padovan 43240a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 43250a708f8fSGustavo F. Padovan skb->len); 43260a708f8fSGustavo F. Padovan conn->rx_len -= skb->len; 43270a708f8fSGustavo F. Padovan 43280a708f8fSGustavo F. Padovan if (!conn->rx_len) { 43290a708f8fSGustavo F. Padovan /* Complete frame received */ 43300a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, conn->rx_skb); 43310a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 43320a708f8fSGustavo F. Padovan } 43330a708f8fSGustavo F. Padovan } 43340a708f8fSGustavo F. Padovan 43350a708f8fSGustavo F. Padovan drop: 43360a708f8fSGustavo F. Padovan kfree_skb(skb); 43370a708f8fSGustavo F. Padovan return 0; 43380a708f8fSGustavo F. Padovan } 43390a708f8fSGustavo F. Padovan 43400a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p) 43410a708f8fSGustavo F. Padovan { 434223691d75SGustavo F. Padovan struct l2cap_chan *c; 43430a708f8fSGustavo F. Padovan 434423691d75SGustavo F. Padovan read_lock_bh(&chan_list_lock); 43450a708f8fSGustavo F. Padovan 434623691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 434723691d75SGustavo F. Padovan struct sock *sk = c->sk; 43480a708f8fSGustavo F. Padovan 4349903d343eSGustavo F. Padovan seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", 43500a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->src), 43510a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->dst), 435223691d75SGustavo F. Padovan sk->sk_state, __le16_to_cpu(c->psm), 435323691d75SGustavo F. Padovan c->scid, c->dcid, c->imtu, c->omtu, 435423691d75SGustavo F. Padovan c->sec_level, c->mode); 43550a708f8fSGustavo F. Padovan } 43560a708f8fSGustavo F. Padovan 435723691d75SGustavo F. Padovan read_unlock_bh(&chan_list_lock); 43580a708f8fSGustavo F. Padovan 43590a708f8fSGustavo F. Padovan return 0; 43600a708f8fSGustavo F. Padovan } 43610a708f8fSGustavo F. Padovan 43620a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file) 43630a708f8fSGustavo F. Padovan { 43640a708f8fSGustavo F. Padovan return single_open(file, l2cap_debugfs_show, inode->i_private); 43650a708f8fSGustavo F. Padovan } 43660a708f8fSGustavo F. Padovan 43670a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = { 43680a708f8fSGustavo F. Padovan .open = l2cap_debugfs_open, 43690a708f8fSGustavo F. Padovan .read = seq_read, 43700a708f8fSGustavo F. Padovan .llseek = seq_lseek, 43710a708f8fSGustavo F. Padovan .release = single_release, 43720a708f8fSGustavo F. Padovan }; 43730a708f8fSGustavo F. Padovan 43740a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs; 43750a708f8fSGustavo F. Padovan 43760a708f8fSGustavo F. Padovan static struct hci_proto l2cap_hci_proto = { 43770a708f8fSGustavo F. Padovan .name = "L2CAP", 43780a708f8fSGustavo F. Padovan .id = HCI_PROTO_L2CAP, 43790a708f8fSGustavo F. Padovan .connect_ind = l2cap_connect_ind, 43800a708f8fSGustavo F. Padovan .connect_cfm = l2cap_connect_cfm, 43810a708f8fSGustavo F. Padovan .disconn_ind = l2cap_disconn_ind, 43820a708f8fSGustavo F. Padovan .disconn_cfm = l2cap_disconn_cfm, 43830a708f8fSGustavo F. Padovan .security_cfm = l2cap_security_cfm, 43840a708f8fSGustavo F. Padovan .recv_acldata = l2cap_recv_acldata 43850a708f8fSGustavo F. Padovan }; 43860a708f8fSGustavo F. Padovan 438764274518SGustavo F. Padovan int __init l2cap_init(void) 43880a708f8fSGustavo F. Padovan { 43890a708f8fSGustavo F. Padovan int err; 43900a708f8fSGustavo F. Padovan 4391bb58f747SGustavo F. Padovan err = l2cap_init_sockets(); 43920a708f8fSGustavo F. Padovan if (err < 0) 43930a708f8fSGustavo F. Padovan return err; 43940a708f8fSGustavo F. Padovan 43950a708f8fSGustavo F. Padovan _busy_wq = create_singlethread_workqueue("l2cap"); 43960a708f8fSGustavo F. Padovan if (!_busy_wq) { 4397bb58f747SGustavo F. Padovan err = -ENOMEM; 43980a708f8fSGustavo F. Padovan goto error; 43990a708f8fSGustavo F. Padovan } 44000a708f8fSGustavo F. Padovan 44010a708f8fSGustavo F. Padovan err = hci_register_proto(&l2cap_hci_proto); 44020a708f8fSGustavo F. Padovan if (err < 0) { 44030a708f8fSGustavo F. Padovan BT_ERR("L2CAP protocol registration failed"); 44040a708f8fSGustavo F. Padovan bt_sock_unregister(BTPROTO_L2CAP); 44050a708f8fSGustavo F. Padovan goto error; 44060a708f8fSGustavo F. Padovan } 44070a708f8fSGustavo F. Padovan 44080a708f8fSGustavo F. Padovan if (bt_debugfs) { 44090a708f8fSGustavo F. Padovan l2cap_debugfs = debugfs_create_file("l2cap", 0444, 44100a708f8fSGustavo F. Padovan bt_debugfs, NULL, &l2cap_debugfs_fops); 44110a708f8fSGustavo F. Padovan if (!l2cap_debugfs) 44120a708f8fSGustavo F. Padovan BT_ERR("Failed to create L2CAP debug file"); 44130a708f8fSGustavo F. Padovan } 44140a708f8fSGustavo F. Padovan 44150a708f8fSGustavo F. Padovan return 0; 44160a708f8fSGustavo F. Padovan 44170a708f8fSGustavo F. Padovan error: 44180a708f8fSGustavo F. Padovan destroy_workqueue(_busy_wq); 4419bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 44200a708f8fSGustavo F. Padovan return err; 44210a708f8fSGustavo F. Padovan } 44220a708f8fSGustavo F. Padovan 442364274518SGustavo F. Padovan void l2cap_exit(void) 44240a708f8fSGustavo F. Padovan { 44250a708f8fSGustavo F. Padovan debugfs_remove(l2cap_debugfs); 44260a708f8fSGustavo F. Padovan 44270a708f8fSGustavo F. Padovan flush_workqueue(_busy_wq); 44280a708f8fSGustavo F. Padovan destroy_workqueue(_busy_wq); 44290a708f8fSGustavo F. Padovan 44300a708f8fSGustavo F. Padovan if (hci_unregister_proto(&l2cap_hci_proto) < 0) 44310a708f8fSGustavo F. Padovan BT_ERR("L2CAP protocol unregistration failed"); 44320a708f8fSGustavo F. Padovan 4433bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 44340a708f8fSGustavo F. Padovan } 44350a708f8fSGustavo F. Padovan 44360a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644); 44370a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); 4438