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 211*ab07801dSGustavo F. Padovan static void l2cap_chan_set_timer(struct l2cap_chan *chan, long timeout) 212*ab07801dSGustavo F. Padovan { 213*ab07801dSGustavo F. Padovan BT_DBG("chan %p state %d timeout %ld", chan->sk, chan->sk->sk_state, 214*ab07801dSGustavo F. Padovan timeout); 215*ab07801dSGustavo F. Padovan if (!mod_timer(&chan->chan_timer, jiffies + timeout)) 216*ab07801dSGustavo F. Padovan sock_hold(chan->sk); 217*ab07801dSGustavo F. Padovan } 218*ab07801dSGustavo F. Padovan 219*ab07801dSGustavo F. Padovan void l2cap_chan_clear_timer(struct l2cap_chan *chan) 220*ab07801dSGustavo F. Padovan { 221*ab07801dSGustavo F. Padovan BT_DBG("chan %p state %d", chan, chan->sk->sk_state); 222*ab07801dSGustavo F. Padovan 223*ab07801dSGustavo F. Padovan if (timer_pending(&chan->chan_timer) && del_timer(&chan->chan_timer)) 224*ab07801dSGustavo F. Padovan __sock_put(chan->sk); 225*ab07801dSGustavo F. Padovan } 226*ab07801dSGustavo F. Padovan 227*ab07801dSGustavo F. Padovan static void l2cap_chan_timeout(unsigned long arg) 228*ab07801dSGustavo F. Padovan { 229*ab07801dSGustavo F. Padovan struct l2cap_chan *chan = (struct l2cap_chan *) arg; 230*ab07801dSGustavo F. Padovan struct sock *sk = chan->sk; 231*ab07801dSGustavo F. Padovan int reason; 232*ab07801dSGustavo F. Padovan 233*ab07801dSGustavo F. Padovan BT_DBG("chan %p state %d", chan, sk->sk_state); 234*ab07801dSGustavo F. Padovan 235*ab07801dSGustavo F. Padovan bh_lock_sock(sk); 236*ab07801dSGustavo F. Padovan 237*ab07801dSGustavo F. Padovan if (sock_owned_by_user(sk)) { 238*ab07801dSGustavo F. Padovan /* sk is owned by user. Try again later */ 239*ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 5); 240*ab07801dSGustavo F. Padovan bh_unlock_sock(sk); 241*ab07801dSGustavo F. Padovan sock_put(sk); 242*ab07801dSGustavo F. Padovan return; 243*ab07801dSGustavo F. Padovan } 244*ab07801dSGustavo F. Padovan 245*ab07801dSGustavo F. Padovan if (sk->sk_state == BT_CONNECTED || sk->sk_state == BT_CONFIG) 246*ab07801dSGustavo F. Padovan reason = ECONNREFUSED; 247*ab07801dSGustavo F. Padovan else if (sk->sk_state == BT_CONNECT && 248*ab07801dSGustavo F. Padovan chan->sec_level != BT_SECURITY_SDP) 249*ab07801dSGustavo F. Padovan reason = ECONNREFUSED; 250*ab07801dSGustavo F. Padovan else 251*ab07801dSGustavo F. Padovan reason = ETIMEDOUT; 252*ab07801dSGustavo F. Padovan 253*ab07801dSGustavo F. Padovan __l2cap_chan_close(chan, reason); 254*ab07801dSGustavo F. Padovan 255*ab07801dSGustavo F. Padovan bh_unlock_sock(sk); 256*ab07801dSGustavo F. Padovan 257*ab07801dSGustavo F. Padovan l2cap_sock_kill(sk); 258*ab07801dSGustavo F. Padovan sock_put(sk); 259*ab07801dSGustavo F. Padovan } 260*ab07801dSGustavo 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 275*ab07801dSGustavo F. Padovan setup_timer(&chan->chan_timer, l2cap_chan_timeout, (unsigned long) chan); 276*ab07801dSGustavo 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 336*ab07801dSGustavo 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 /* Must be called on unlocked socket. */ 3874519de9aSGustavo F. Padovan static void l2cap_chan_close(struct sock *sk) 3884519de9aSGustavo F. Padovan { 389*ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(l2cap_pi(sk)->chan); 3904519de9aSGustavo F. Padovan lock_sock(sk); 3914519de9aSGustavo F. Padovan __l2cap_chan_close(l2cap_pi(sk)->chan, ECONNRESET); 3924519de9aSGustavo F. Padovan release_sock(sk); 3934519de9aSGustavo F. Padovan l2cap_sock_kill(sk); 3944519de9aSGustavo F. Padovan } 3954519de9aSGustavo F. Padovan 3964519de9aSGustavo F. Padovan static void l2cap_chan_cleanup_listen(struct sock *parent) 3974519de9aSGustavo F. Padovan { 3984519de9aSGustavo F. Padovan struct sock *sk; 3994519de9aSGustavo F. Padovan 4004519de9aSGustavo F. Padovan BT_DBG("parent %p", parent); 4014519de9aSGustavo F. Padovan 4024519de9aSGustavo F. Padovan /* Close not yet accepted channels */ 4034519de9aSGustavo F. Padovan while ((sk = bt_accept_dequeue(parent, NULL))) 4044519de9aSGustavo F. Padovan l2cap_chan_close(sk); 4054519de9aSGustavo F. Padovan 4064519de9aSGustavo F. Padovan parent->sk_state = BT_CLOSED; 4074519de9aSGustavo F. Padovan sock_set_flag(parent, SOCK_ZAPPED); 4084519de9aSGustavo F. Padovan } 4094519de9aSGustavo F. Padovan 4104519de9aSGustavo F. Padovan void __l2cap_chan_close(struct l2cap_chan *chan, int reason) 4114519de9aSGustavo F. Padovan { 4124519de9aSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 4134519de9aSGustavo F. Padovan struct sock *sk = chan->sk; 4144519de9aSGustavo F. Padovan 4154519de9aSGustavo F. Padovan BT_DBG("chan %p state %d socket %p", chan, sk->sk_state, sk->sk_socket); 4164519de9aSGustavo F. Padovan 4174519de9aSGustavo F. Padovan switch (sk->sk_state) { 4184519de9aSGustavo F. Padovan case BT_LISTEN: 4194519de9aSGustavo F. Padovan l2cap_chan_cleanup_listen(sk); 4204519de9aSGustavo F. Padovan break; 4214519de9aSGustavo F. Padovan 4224519de9aSGustavo F. Padovan case BT_CONNECTED: 4234519de9aSGustavo F. Padovan case BT_CONFIG: 424715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 4254519de9aSGustavo F. Padovan conn->hcon->type == ACL_LINK) { 426*ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, sk->sk_sndtimeo); 4274519de9aSGustavo F. Padovan l2cap_send_disconn_req(conn, chan, reason); 4284519de9aSGustavo F. Padovan } else 4294519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4304519de9aSGustavo F. Padovan break; 4314519de9aSGustavo F. Padovan 4324519de9aSGustavo F. Padovan case BT_CONNECT2: 433715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 4344519de9aSGustavo F. Padovan conn->hcon->type == ACL_LINK) { 4354519de9aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 4364519de9aSGustavo F. Padovan __u16 result; 4374519de9aSGustavo F. Padovan 4384519de9aSGustavo F. Padovan if (bt_sk(sk)->defer_setup) 4394519de9aSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 4404519de9aSGustavo F. Padovan else 4414519de9aSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 4424519de9aSGustavo F. Padovan 4434519de9aSGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4444519de9aSGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 4454519de9aSGustavo F. Padovan rsp.result = cpu_to_le16(result); 4464519de9aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 4474519de9aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4484519de9aSGustavo F. Padovan sizeof(rsp), &rsp); 4494519de9aSGustavo F. Padovan } 4504519de9aSGustavo F. Padovan 4514519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4524519de9aSGustavo F. Padovan break; 4534519de9aSGustavo F. Padovan 4544519de9aSGustavo F. Padovan case BT_CONNECT: 4554519de9aSGustavo F. Padovan case BT_DISCONN: 4564519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4574519de9aSGustavo F. Padovan break; 4584519de9aSGustavo F. Padovan 4594519de9aSGustavo F. Padovan default: 4604519de9aSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 4614519de9aSGustavo F. Padovan break; 4624519de9aSGustavo F. Padovan } 4634519de9aSGustavo F. Padovan } 4644519de9aSGustavo F. Padovan 4654343478fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) 4660a708f8fSGustavo F. Padovan { 467715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_RAW) { 4684343478fSGustavo F. Padovan switch (chan->sec_level) { 4690a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 4700a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING_MITM; 4710a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 4720a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING; 4730a708f8fSGustavo F. Padovan default: 4740a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 4750a708f8fSGustavo F. Padovan } 476fe4128e0SGustavo F. Padovan } else if (chan->psm == cpu_to_le16(0x0001)) { 4774343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_LOW) 4784343478fSGustavo F. Padovan chan->sec_level = BT_SECURITY_SDP; 4790a708f8fSGustavo F. Padovan 4804343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_HIGH) 4810a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING_MITM; 4820a708f8fSGustavo F. Padovan else 4830a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 4840a708f8fSGustavo F. Padovan } else { 4854343478fSGustavo F. Padovan switch (chan->sec_level) { 4860a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 4870a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING_MITM; 4880a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 4890a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING; 4900a708f8fSGustavo F. Padovan default: 4910a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 4920a708f8fSGustavo F. Padovan } 4930a708f8fSGustavo F. Padovan } 4940a708f8fSGustavo F. Padovan } 4950a708f8fSGustavo F. Padovan 4960a708f8fSGustavo F. Padovan /* Service level security */ 4974343478fSGustavo F. Padovan static inline int l2cap_check_security(struct l2cap_chan *chan) 4980a708f8fSGustavo F. Padovan { 4998c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 5000a708f8fSGustavo F. Padovan __u8 auth_type; 5010a708f8fSGustavo F. Padovan 5024343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 5030a708f8fSGustavo F. Padovan 5044343478fSGustavo F. Padovan return hci_conn_security(conn->hcon, chan->sec_level, auth_type); 5050a708f8fSGustavo F. Padovan } 5060a708f8fSGustavo F. Padovan 50768983259SGustavo F. Padovan u8 l2cap_get_ident(struct l2cap_conn *conn) 5080a708f8fSGustavo F. Padovan { 5090a708f8fSGustavo F. Padovan u8 id; 5100a708f8fSGustavo F. Padovan 5110a708f8fSGustavo F. Padovan /* Get next available identificator. 5120a708f8fSGustavo F. Padovan * 1 - 128 are used by kernel. 5130a708f8fSGustavo F. Padovan * 129 - 199 are reserved. 5140a708f8fSGustavo F. Padovan * 200 - 254 are used by utilities like l2ping, etc. 5150a708f8fSGustavo F. Padovan */ 5160a708f8fSGustavo F. Padovan 5170a708f8fSGustavo F. Padovan spin_lock_bh(&conn->lock); 5180a708f8fSGustavo F. Padovan 5190a708f8fSGustavo F. Padovan if (++conn->tx_ident > 128) 5200a708f8fSGustavo F. Padovan conn->tx_ident = 1; 5210a708f8fSGustavo F. Padovan 5220a708f8fSGustavo F. Padovan id = conn->tx_ident; 5230a708f8fSGustavo F. Padovan 5240a708f8fSGustavo F. Padovan spin_unlock_bh(&conn->lock); 5250a708f8fSGustavo F. Padovan 5260a708f8fSGustavo F. Padovan return id; 5270a708f8fSGustavo F. Padovan } 5280a708f8fSGustavo F. Padovan 5294519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data) 5300a708f8fSGustavo F. Padovan { 5310a708f8fSGustavo F. Padovan struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); 5320a708f8fSGustavo F. Padovan u8 flags; 5330a708f8fSGustavo F. Padovan 5340a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x", code); 5350a708f8fSGustavo F. Padovan 5360a708f8fSGustavo F. Padovan if (!skb) 5370a708f8fSGustavo F. Padovan return; 5380a708f8fSGustavo F. Padovan 5390a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 5400a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 5410a708f8fSGustavo F. Padovan else 5420a708f8fSGustavo F. Padovan flags = ACL_START; 5430a708f8fSGustavo F. Padovan 5440a708f8fSGustavo F. Padovan hci_send_acl(conn->hcon, skb, flags); 5450a708f8fSGustavo F. Padovan } 5460a708f8fSGustavo F. Padovan 547525cd185SGustavo F. Padovan static inline void l2cap_send_sframe(struct l2cap_chan *chan, u16 control) 5480a708f8fSGustavo F. Padovan { 5490a708f8fSGustavo F. Padovan struct sk_buff *skb; 5500a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 551525cd185SGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(chan->sk); 5528c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 5530a708f8fSGustavo F. Padovan struct sock *sk = (struct sock *)pi; 5540a708f8fSGustavo F. Padovan int count, hlen = L2CAP_HDR_SIZE + 2; 5550a708f8fSGustavo F. Padovan u8 flags; 5560a708f8fSGustavo F. Padovan 5570a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 5580a708f8fSGustavo F. Padovan return; 5590a708f8fSGustavo F. Padovan 56047d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 5610a708f8fSGustavo F. Padovan hlen += 2; 5620a708f8fSGustavo F. Padovan 56349208c9cSGustavo F. Padovan BT_DBG("chan %p, control 0x%2.2x", chan, control); 5640a708f8fSGustavo F. Padovan 5650a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, hlen); 5660a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FRAME_TYPE; 5670a708f8fSGustavo F. Padovan 568525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 5690a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 570525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 5710a708f8fSGustavo F. Padovan } 5720a708f8fSGustavo F. Padovan 573525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_PBIT) { 5740a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_POLL; 575525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_PBIT; 5760a708f8fSGustavo F. Padovan } 5770a708f8fSGustavo F. Padovan 5780a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 5790a708f8fSGustavo F. Padovan if (!skb) 5800a708f8fSGustavo F. Padovan return; 5810a708f8fSGustavo F. Padovan 5820a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 5830a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); 584fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 5850a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb_put(skb, 2)); 5860a708f8fSGustavo F. Padovan 58747d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 5880a708f8fSGustavo F. Padovan u16 fcs = crc16(0, (u8 *)lh, count - 2); 5890a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb_put(skb, 2)); 5900a708f8fSGustavo F. Padovan } 5910a708f8fSGustavo F. Padovan 5920a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 5930a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 5940a708f8fSGustavo F. Padovan else 5950a708f8fSGustavo F. Padovan flags = ACL_START; 5960a708f8fSGustavo F. Padovan 5978c1d787bSGustavo F. Padovan hci_send_acl(chan->conn->hcon, skb, flags); 5980a708f8fSGustavo F. Padovan } 5990a708f8fSGustavo F. Padovan 600525cd185SGustavo F. Padovan static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u16 control) 6010a708f8fSGustavo F. Padovan { 602525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 6030a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 604525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 6050a708f8fSGustavo F. Padovan } else 6060a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 6070a708f8fSGustavo F. Padovan 60842e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 6090a708f8fSGustavo F. Padovan 610525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 6110a708f8fSGustavo F. Padovan } 6120a708f8fSGustavo F. Padovan 613b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) 6140a708f8fSGustavo F. Padovan { 615b4450035SGustavo F. Padovan return !(chan->conf_state & L2CAP_CONF_CONNECT_PEND); 6160a708f8fSGustavo F. Padovan } 6170a708f8fSGustavo F. Padovan 618fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan) 6190a708f8fSGustavo F. Padovan { 6208c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 6210a708f8fSGustavo F. Padovan 6220a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { 6230a708f8fSGustavo F. Padovan if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) 6240a708f8fSGustavo F. Padovan return; 6250a708f8fSGustavo F. Padovan 6264343478fSGustavo F. Padovan if (l2cap_check_security(chan) && 6274343478fSGustavo F. Padovan __l2cap_no_conn_pending(chan)) { 6280a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 629fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 630fe4128e0SGustavo F. Padovan req.psm = chan->psm; 6310a708f8fSGustavo F. Padovan 632fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 633b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 6340a708f8fSGustavo F. Padovan 635fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 636fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 6370a708f8fSGustavo F. Padovan } 6380a708f8fSGustavo F. Padovan } else { 6390a708f8fSGustavo F. Padovan struct l2cap_info_req req; 6400a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 6410a708f8fSGustavo F. Padovan 6420a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 6430a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 6440a708f8fSGustavo F. Padovan 6450a708f8fSGustavo F. Padovan mod_timer(&conn->info_timer, jiffies + 6460a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 6470a708f8fSGustavo F. Padovan 6480a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 6490a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 6500a708f8fSGustavo F. Padovan } 6510a708f8fSGustavo F. Padovan } 6520a708f8fSGustavo F. Padovan 6530a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) 6540a708f8fSGustavo F. Padovan { 6550a708f8fSGustavo F. Padovan u32 local_feat_mask = l2cap_feat_mask; 6560a708f8fSGustavo F. Padovan if (!disable_ertm) 6570a708f8fSGustavo F. Padovan local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; 6580a708f8fSGustavo F. Padovan 6590a708f8fSGustavo F. Padovan switch (mode) { 6600a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 6610a708f8fSGustavo F. Padovan return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; 6620a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 6630a708f8fSGustavo F. Padovan return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; 6640a708f8fSGustavo F. Padovan default: 6650a708f8fSGustavo F. Padovan return 0x00; 6660a708f8fSGustavo F. Padovan } 6670a708f8fSGustavo F. Padovan } 6680a708f8fSGustavo F. Padovan 6694519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err) 6700a708f8fSGustavo F. Padovan { 671e92c8e70SGustavo F. Padovan struct sock *sk; 6720a708f8fSGustavo F. Padovan struct l2cap_disconn_req req; 6730a708f8fSGustavo F. Padovan 6740a708f8fSGustavo F. Padovan if (!conn) 6750a708f8fSGustavo F. Padovan return; 6760a708f8fSGustavo F. Padovan 677e92c8e70SGustavo F. Padovan sk = chan->sk; 678e92c8e70SGustavo F. Padovan 6790c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 680e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 681e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 682e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 6830a708f8fSGustavo F. Padovan } 6840a708f8fSGustavo F. Padovan 685fe4128e0SGustavo F. Padovan req.dcid = cpu_to_le16(chan->dcid); 686fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 6870a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 6880a708f8fSGustavo F. Padovan L2CAP_DISCONN_REQ, sizeof(req), &req); 6890a708f8fSGustavo F. Padovan 6900a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 6910a708f8fSGustavo F. Padovan sk->sk_err = err; 6920a708f8fSGustavo F. Padovan } 6930a708f8fSGustavo F. Padovan 6940a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */ 6950a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn) 6960a708f8fSGustavo F. Padovan { 697820ffdb3SGustavo F. Padovan struct l2cap_chan *chan, *tmp; 6980a708f8fSGustavo F. Padovan 6990a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 7000a708f8fSGustavo F. Padovan 701baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 7020a708f8fSGustavo F. Padovan 703820ffdb3SGustavo F. Padovan list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { 70448454079SGustavo F. Padovan struct sock *sk = chan->sk; 705baa7e1faSGustavo F. Padovan 7060a708f8fSGustavo F. Padovan bh_lock_sock(sk); 7070a708f8fSGustavo F. Padovan 708715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 7090a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7100a708f8fSGustavo F. Padovan continue; 7110a708f8fSGustavo F. Padovan } 7120a708f8fSGustavo F. Padovan 7130a708f8fSGustavo F. Padovan if (sk->sk_state == BT_CONNECT) { 7140a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 7150a708f8fSGustavo F. Padovan 7164343478fSGustavo F. Padovan if (!l2cap_check_security(chan) || 717b4450035SGustavo F. Padovan !__l2cap_no_conn_pending(chan)) { 7180a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7190a708f8fSGustavo F. Padovan continue; 7200a708f8fSGustavo F. Padovan } 7210a708f8fSGustavo F. Padovan 7220c1bc5c6SGustavo F. Padovan if (!l2cap_mode_supported(chan->mode, 7230a708f8fSGustavo F. Padovan conn->feat_mask) 724b4450035SGustavo F. Padovan && chan->conf_state & 7250a708f8fSGustavo F. Padovan L2CAP_CONF_STATE2_DEVICE) { 7264519de9aSGustavo F. Padovan /* __l2cap_chan_close() calls list_del(chan) 727820ffdb3SGustavo F. Padovan * so release the lock */ 728820ffdb3SGustavo F. Padovan read_unlock_bh(&conn->chan_lock); 7294519de9aSGustavo F. Padovan __l2cap_chan_close(chan, ECONNRESET); 730820ffdb3SGustavo F. Padovan read_lock_bh(&conn->chan_lock); 7310a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7320a708f8fSGustavo F. Padovan continue; 7330a708f8fSGustavo F. Padovan } 7340a708f8fSGustavo F. Padovan 735fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 736fe4128e0SGustavo F. Padovan req.psm = chan->psm; 7370a708f8fSGustavo F. Padovan 738fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 739b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 7400a708f8fSGustavo F. Padovan 741fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 742fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 7430a708f8fSGustavo F. Padovan 7440a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT2) { 7450a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 7460a708f8fSGustavo F. Padovan char buf[128]; 747fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 748fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 7490a708f8fSGustavo F. Padovan 7504343478fSGustavo F. Padovan if (l2cap_check_security(chan)) { 7510a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 7520a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 7530a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 7540a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); 7550a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 7560a708f8fSGustavo F. Padovan 7570a708f8fSGustavo F. Padovan } else { 7580a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 7590a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 7600a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 7610a708f8fSGustavo F. Padovan } 7620a708f8fSGustavo F. Padovan } else { 7630a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 7640a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); 7650a708f8fSGustavo F. Padovan } 7660a708f8fSGustavo F. Padovan 767fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 768fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 7690a708f8fSGustavo F. Padovan 770b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_REQ_SENT || 7710a708f8fSGustavo F. Padovan rsp.result != L2CAP_CR_SUCCESS) { 7720a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7730a708f8fSGustavo F. Padovan continue; 7740a708f8fSGustavo F. Padovan } 7750a708f8fSGustavo F. Padovan 776b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 7770a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 77873ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 77973ffa904SGustavo F. Padovan chan->num_conf_req++; 7800a708f8fSGustavo F. Padovan } 7810a708f8fSGustavo F. Padovan 7820a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7830a708f8fSGustavo F. Padovan } 7840a708f8fSGustavo F. Padovan 785baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 7860a708f8fSGustavo F. Padovan } 7870a708f8fSGustavo F. Padovan 788b62f328bSVille Tervo /* Find socket with cid and source bdaddr. 789b62f328bSVille Tervo * Returns closest match, locked. 790b62f328bSVille Tervo */ 79123691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src) 792b62f328bSVille Tervo { 79323691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 794b62f328bSVille Tervo 79523691d75SGustavo F. Padovan read_lock(&chan_list_lock); 796b62f328bSVille Tervo 79723691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 79823691d75SGustavo F. Padovan struct sock *sk = c->sk; 799fe4128e0SGustavo F. Padovan 800b62f328bSVille Tervo if (state && sk->sk_state != state) 801b62f328bSVille Tervo continue; 802b62f328bSVille Tervo 80323691d75SGustavo F. Padovan if (c->scid == cid) { 804b62f328bSVille Tervo /* Exact match. */ 80523691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 80623691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 80723691d75SGustavo F. Padovan return c; 80823691d75SGustavo F. Padovan } 809b62f328bSVille Tervo 810b62f328bSVille Tervo /* Closest match */ 811b62f328bSVille Tervo if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 81223691d75SGustavo F. Padovan c1 = c; 813b62f328bSVille Tervo } 814b62f328bSVille Tervo } 815280f294fSGustavo F. Padovan 81623691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 817b62f328bSVille Tervo 81823691d75SGustavo F. Padovan return c1; 819b62f328bSVille Tervo } 820b62f328bSVille Tervo 821b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn) 822b62f328bSVille Tervo { 823c916fbe4SGustavo F. Padovan struct sock *parent, *sk; 82423691d75SGustavo F. Padovan struct l2cap_chan *chan, *pchan; 825b62f328bSVille Tervo 826b62f328bSVille Tervo BT_DBG(""); 827b62f328bSVille Tervo 828b62f328bSVille Tervo /* Check if we have socket listening on cid */ 82923691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, 830b62f328bSVille Tervo conn->src); 83123691d75SGustavo F. Padovan if (!pchan) 832b62f328bSVille Tervo return; 833b62f328bSVille Tervo 83423691d75SGustavo F. Padovan parent = pchan->sk; 83523691d75SGustavo F. Padovan 83662f3a2cfSGustavo F. Padovan bh_lock_sock(parent); 83762f3a2cfSGustavo F. Padovan 838b62f328bSVille Tervo /* Check for backlog size */ 839b62f328bSVille Tervo if (sk_acceptq_is_full(parent)) { 840b62f328bSVille Tervo BT_DBG("backlog full %d", parent->sk_ack_backlog); 841b62f328bSVille Tervo goto clean; 842b62f328bSVille Tervo } 843b62f328bSVille Tervo 844b62f328bSVille Tervo sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC); 845b62f328bSVille Tervo if (!sk) 846b62f328bSVille Tervo goto clean; 847b62f328bSVille Tervo 84823691d75SGustavo F. Padovan chan = l2cap_chan_create(sk); 84948454079SGustavo F. Padovan if (!chan) { 85048454079SGustavo F. Padovan l2cap_sock_kill(sk); 85148454079SGustavo F. Padovan goto clean; 85248454079SGustavo F. Padovan } 85348454079SGustavo F. Padovan 8545d41ce1dSGustavo F. Padovan l2cap_pi(sk)->chan = chan; 8555d41ce1dSGustavo F. Padovan 856baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 857b62f328bSVille Tervo 858b62f328bSVille Tervo hci_conn_hold(conn->hcon); 859b62f328bSVille Tervo 860b62f328bSVille Tervo l2cap_sock_init(sk, parent); 861baa7e1faSGustavo F. Padovan 862b62f328bSVille Tervo bacpy(&bt_sk(sk)->src, conn->src); 863b62f328bSVille Tervo bacpy(&bt_sk(sk)->dst, conn->dst); 864b62f328bSVille Tervo 865d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 866d1010240SGustavo F. Padovan 86748454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 86848454079SGustavo F. Padovan 869*ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, sk->sk_sndtimeo); 870b62f328bSVille Tervo 871b62f328bSVille Tervo sk->sk_state = BT_CONNECTED; 872b62f328bSVille Tervo parent->sk_data_ready(parent, 0); 873b62f328bSVille Tervo 874baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 875b62f328bSVille Tervo 876b62f328bSVille Tervo clean: 877b62f328bSVille Tervo bh_unlock_sock(parent); 878b62f328bSVille Tervo } 879b62f328bSVille Tervo 8800a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn) 8810a708f8fSGustavo F. Padovan { 88248454079SGustavo F. Padovan struct l2cap_chan *chan; 8830a708f8fSGustavo F. Padovan 8840a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 8850a708f8fSGustavo F. Padovan 886b62f328bSVille Tervo if (!conn->hcon->out && conn->hcon->type == LE_LINK) 887b62f328bSVille Tervo l2cap_le_conn_ready(conn); 888b62f328bSVille Tervo 889baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 8900a708f8fSGustavo F. Padovan 891baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 89248454079SGustavo F. Padovan struct sock *sk = chan->sk; 893baa7e1faSGustavo F. Padovan 8940a708f8fSGustavo F. Padovan bh_lock_sock(sk); 8950a708f8fSGustavo F. Padovan 896acd7d370SVille Tervo if (conn->hcon->type == LE_LINK) { 897*ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 898acd7d370SVille Tervo sk->sk_state = BT_CONNECTED; 899acd7d370SVille Tervo sk->sk_state_change(sk); 900acd7d370SVille Tervo } 901acd7d370SVille Tervo 902715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 903*ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 9040a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 9050a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 9060a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT) 907fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 9080a708f8fSGustavo F. Padovan 9090a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9100a708f8fSGustavo F. Padovan } 9110a708f8fSGustavo F. Padovan 912baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 9130a708f8fSGustavo F. Padovan } 9140a708f8fSGustavo F. Padovan 9150a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */ 9160a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) 9170a708f8fSGustavo F. Padovan { 91848454079SGustavo F. Padovan struct l2cap_chan *chan; 9190a708f8fSGustavo F. Padovan 9200a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 9210a708f8fSGustavo F. Padovan 922baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 9230a708f8fSGustavo F. Padovan 924baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 92548454079SGustavo F. Padovan struct sock *sk = chan->sk; 926baa7e1faSGustavo F. Padovan 9274343478fSGustavo F. Padovan if (chan->force_reliable) 9280a708f8fSGustavo F. Padovan sk->sk_err = err; 9290a708f8fSGustavo F. Padovan } 9300a708f8fSGustavo F. Padovan 931baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 9320a708f8fSGustavo F. Padovan } 9330a708f8fSGustavo F. Padovan 9340a708f8fSGustavo F. Padovan static void l2cap_info_timeout(unsigned long arg) 9350a708f8fSGustavo F. Padovan { 9360a708f8fSGustavo F. Padovan struct l2cap_conn *conn = (void *) arg; 9370a708f8fSGustavo F. Padovan 9380a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 9390a708f8fSGustavo F. Padovan conn->info_ident = 0; 9400a708f8fSGustavo F. Padovan 9410a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 9420a708f8fSGustavo F. Padovan } 9430a708f8fSGustavo F. Padovan 9440a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) 9450a708f8fSGustavo F. Padovan { 9460a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 9470a708f8fSGustavo F. Padovan 9480a708f8fSGustavo F. Padovan if (conn || status) 9490a708f8fSGustavo F. Padovan return conn; 9500a708f8fSGustavo F. Padovan 9510a708f8fSGustavo F. Padovan conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); 9520a708f8fSGustavo F. Padovan if (!conn) 9530a708f8fSGustavo F. Padovan return NULL; 9540a708f8fSGustavo F. Padovan 9550a708f8fSGustavo F. Padovan hcon->l2cap_data = conn; 9560a708f8fSGustavo F. Padovan conn->hcon = hcon; 9570a708f8fSGustavo F. Padovan 9580a708f8fSGustavo F. Padovan BT_DBG("hcon %p conn %p", hcon, conn); 9590a708f8fSGustavo F. Padovan 960acd7d370SVille Tervo if (hcon->hdev->le_mtu && hcon->type == LE_LINK) 961acd7d370SVille Tervo conn->mtu = hcon->hdev->le_mtu; 962acd7d370SVille Tervo else 9630a708f8fSGustavo F. Padovan conn->mtu = hcon->hdev->acl_mtu; 964acd7d370SVille Tervo 9650a708f8fSGustavo F. Padovan conn->src = &hcon->hdev->bdaddr; 9660a708f8fSGustavo F. Padovan conn->dst = &hcon->dst; 9670a708f8fSGustavo F. Padovan 9680a708f8fSGustavo F. Padovan conn->feat_mask = 0; 9690a708f8fSGustavo F. Padovan 9700a708f8fSGustavo F. Padovan spin_lock_init(&conn->lock); 971baa7e1faSGustavo F. Padovan rwlock_init(&conn->chan_lock); 972baa7e1faSGustavo F. Padovan 973baa7e1faSGustavo F. Padovan INIT_LIST_HEAD(&conn->chan_l); 9740a708f8fSGustavo F. Padovan 975b62f328bSVille Tervo if (hcon->type != LE_LINK) 9760a708f8fSGustavo F. Padovan setup_timer(&conn->info_timer, l2cap_info_timeout, 9770a708f8fSGustavo F. Padovan (unsigned long) conn); 9780a708f8fSGustavo F. Padovan 9790a708f8fSGustavo F. Padovan conn->disc_reason = 0x13; 9800a708f8fSGustavo F. Padovan 9810a708f8fSGustavo F. Padovan return conn; 9820a708f8fSGustavo F. Padovan } 9830a708f8fSGustavo F. Padovan 9840a708f8fSGustavo F. Padovan static void l2cap_conn_del(struct hci_conn *hcon, int err) 9850a708f8fSGustavo F. Padovan { 9860a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 987baa7e1faSGustavo F. Padovan struct l2cap_chan *chan, *l; 9880a708f8fSGustavo F. Padovan struct sock *sk; 9890a708f8fSGustavo F. Padovan 9900a708f8fSGustavo F. Padovan if (!conn) 9910a708f8fSGustavo F. Padovan return; 9920a708f8fSGustavo F. Padovan 9930a708f8fSGustavo F. Padovan BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); 9940a708f8fSGustavo F. Padovan 9950a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 9960a708f8fSGustavo F. Padovan 9970a708f8fSGustavo F. Padovan /* Kill channels */ 998baa7e1faSGustavo F. Padovan list_for_each_entry_safe(chan, l, &conn->chan_l, list) { 99948454079SGustavo F. Padovan sk = chan->sk; 10000a708f8fSGustavo F. Padovan bh_lock_sock(sk); 100148454079SGustavo F. Padovan l2cap_chan_del(chan, err); 10020a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 10030a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 10040a708f8fSGustavo F. Padovan } 10050a708f8fSGustavo F. Padovan 10060a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) 10070a708f8fSGustavo F. Padovan del_timer_sync(&conn->info_timer); 10080a708f8fSGustavo F. Padovan 10090a708f8fSGustavo F. Padovan hcon->l2cap_data = NULL; 10100a708f8fSGustavo F. Padovan kfree(conn); 10110a708f8fSGustavo F. Padovan } 10120a708f8fSGustavo F. Padovan 101348454079SGustavo F. Padovan static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 10140a708f8fSGustavo F. Padovan { 1015baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 101648454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 1017baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 10180a708f8fSGustavo F. Padovan } 10190a708f8fSGustavo F. Padovan 10200a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */ 10210a708f8fSGustavo F. Padovan 10220a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr. 10230a708f8fSGustavo F. Padovan * Returns closest match. 10240a708f8fSGustavo F. Padovan */ 102523691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src) 10260a708f8fSGustavo F. Padovan { 102723691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 10280a708f8fSGustavo F. Padovan 102923691d75SGustavo F. Padovan read_lock(&chan_list_lock); 10300a708f8fSGustavo F. Padovan 103123691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 103223691d75SGustavo F. Padovan struct sock *sk = c->sk; 1033fe4128e0SGustavo F. Padovan 10340a708f8fSGustavo F. Padovan if (state && sk->sk_state != state) 10350a708f8fSGustavo F. Padovan continue; 10360a708f8fSGustavo F. Padovan 103723691d75SGustavo F. Padovan if (c->psm == psm) { 10380a708f8fSGustavo F. Padovan /* Exact match. */ 103923691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 1040a7567b20SJohannes Berg read_unlock(&chan_list_lock); 104123691d75SGustavo F. Padovan return c; 104223691d75SGustavo F. Padovan } 10430a708f8fSGustavo F. Padovan 10440a708f8fSGustavo F. Padovan /* Closest match */ 10450a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 104623691d75SGustavo F. Padovan c1 = c; 10470a708f8fSGustavo F. Padovan } 10480a708f8fSGustavo F. Padovan } 10490a708f8fSGustavo F. Padovan 105023691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 10510a708f8fSGustavo F. Padovan 105223691d75SGustavo F. Padovan return c1; 10530a708f8fSGustavo F. Padovan } 10540a708f8fSGustavo F. Padovan 105577a74c7eSGustavo F. Padovan int l2cap_chan_connect(struct l2cap_chan *chan) 10560a708f8fSGustavo F. Padovan { 10575d41ce1dSGustavo F. Padovan struct sock *sk = chan->sk; 10580a708f8fSGustavo F. Padovan bdaddr_t *src = &bt_sk(sk)->src; 10590a708f8fSGustavo F. Padovan bdaddr_t *dst = &bt_sk(sk)->dst; 10600a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 10610a708f8fSGustavo F. Padovan struct hci_conn *hcon; 10620a708f8fSGustavo F. Padovan struct hci_dev *hdev; 10630a708f8fSGustavo F. Padovan __u8 auth_type; 10640a708f8fSGustavo F. Padovan int err; 10650a708f8fSGustavo F. Padovan 10660a708f8fSGustavo F. Padovan BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), 1067fe4128e0SGustavo F. Padovan chan->psm); 10680a708f8fSGustavo F. Padovan 10690a708f8fSGustavo F. Padovan hdev = hci_get_route(dst, src); 10700a708f8fSGustavo F. Padovan if (!hdev) 10710a708f8fSGustavo F. Padovan return -EHOSTUNREACH; 10720a708f8fSGustavo F. Padovan 10730a708f8fSGustavo F. Padovan hci_dev_lock_bh(hdev); 10740a708f8fSGustavo F. Padovan 10754343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 10760a708f8fSGustavo F. Padovan 1077fe4128e0SGustavo F. Padovan if (chan->dcid == L2CAP_CID_LE_DATA) 1078acd7d370SVille Tervo hcon = hci_connect(hdev, LE_LINK, dst, 10794343478fSGustavo F. Padovan chan->sec_level, auth_type); 1080acd7d370SVille Tervo else 10810a708f8fSGustavo F. Padovan hcon = hci_connect(hdev, ACL_LINK, dst, 10824343478fSGustavo F. Padovan chan->sec_level, auth_type); 1083acd7d370SVille Tervo 108430e76272SVille Tervo if (IS_ERR(hcon)) { 108530e76272SVille Tervo err = PTR_ERR(hcon); 10860a708f8fSGustavo F. Padovan goto done; 108730e76272SVille Tervo } 10880a708f8fSGustavo F. Padovan 10890a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 10900a708f8fSGustavo F. Padovan if (!conn) { 10910a708f8fSGustavo F. Padovan hci_conn_put(hcon); 109230e76272SVille Tervo err = -ENOMEM; 10930a708f8fSGustavo F. Padovan goto done; 10940a708f8fSGustavo F. Padovan } 10950a708f8fSGustavo F. Padovan 10960a708f8fSGustavo F. Padovan /* Update source addr of the socket */ 10970a708f8fSGustavo F. Padovan bacpy(src, conn->src); 10980a708f8fSGustavo F. Padovan 109948454079SGustavo F. Padovan l2cap_chan_add(conn, chan); 110048454079SGustavo F. Padovan 11010a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT; 1102*ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, sk->sk_sndtimeo); 11030a708f8fSGustavo F. Padovan 11040a708f8fSGustavo F. Padovan if (hcon->state == BT_CONNECTED) { 1105715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 1106*ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 11074343478fSGustavo F. Padovan if (l2cap_check_security(chan)) 11080a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 11090a708f8fSGustavo F. Padovan } else 1110fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 11110a708f8fSGustavo F. Padovan } 11120a708f8fSGustavo F. Padovan 111330e76272SVille Tervo err = 0; 111430e76272SVille Tervo 11150a708f8fSGustavo F. Padovan done: 11160a708f8fSGustavo F. Padovan hci_dev_unlock_bh(hdev); 11170a708f8fSGustavo F. Padovan hci_dev_put(hdev); 11180a708f8fSGustavo F. Padovan return err; 11190a708f8fSGustavo F. Padovan } 11200a708f8fSGustavo F. Padovan 1121dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk) 11220a708f8fSGustavo F. Padovan { 11238c1d787bSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 11240a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 11250a708f8fSGustavo F. Padovan int err = 0; 11260a708f8fSGustavo F. Padovan int timeo = HZ/5; 11270a708f8fSGustavo F. Padovan 11280a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 11298c1d787bSGustavo F. Padovan while ((chan->unacked_frames > 0 && chan->conn)) { 11300a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 11310a708f8fSGustavo F. Padovan 11320a708f8fSGustavo F. Padovan if (!timeo) 11330a708f8fSGustavo F. Padovan timeo = HZ/5; 11340a708f8fSGustavo F. Padovan 11350a708f8fSGustavo F. Padovan if (signal_pending(current)) { 11360a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 11370a708f8fSGustavo F. Padovan break; 11380a708f8fSGustavo F. Padovan } 11390a708f8fSGustavo F. Padovan 11400a708f8fSGustavo F. Padovan release_sock(sk); 11410a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 11420a708f8fSGustavo F. Padovan lock_sock(sk); 11430a708f8fSGustavo F. Padovan 11440a708f8fSGustavo F. Padovan err = sock_error(sk); 11450a708f8fSGustavo F. Padovan if (err) 11460a708f8fSGustavo F. Padovan break; 11470a708f8fSGustavo F. Padovan } 11480a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 11490a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 11500a708f8fSGustavo F. Padovan return err; 11510a708f8fSGustavo F. Padovan } 11520a708f8fSGustavo F. Padovan 11530a708f8fSGustavo F. Padovan static void l2cap_monitor_timeout(unsigned long arg) 11540a708f8fSGustavo F. Padovan { 1155525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 1156525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 11570a708f8fSGustavo F. Padovan 1158525cd185SGustavo F. Padovan BT_DBG("chan %p", chan); 11590a708f8fSGustavo F. Padovan 11600a708f8fSGustavo F. Padovan bh_lock_sock(sk); 11612c03a7a4SGustavo F. Padovan if (chan->retry_count >= chan->remote_max_tx) { 11628c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 11630a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 11640a708f8fSGustavo F. Padovan return; 11650a708f8fSGustavo F. Padovan } 11660a708f8fSGustavo F. Padovan 11676a026610SGustavo F. Padovan chan->retry_count++; 11680a708f8fSGustavo F. Padovan __mod_monitor_timer(); 11690a708f8fSGustavo F. Padovan 1170525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 11710a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 11720a708f8fSGustavo F. Padovan } 11730a708f8fSGustavo F. Padovan 11740a708f8fSGustavo F. Padovan static void l2cap_retrans_timeout(unsigned long arg) 11750a708f8fSGustavo F. Padovan { 1176525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 1177525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 11780a708f8fSGustavo F. Padovan 117949208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 11800a708f8fSGustavo F. Padovan 11810a708f8fSGustavo F. Padovan bh_lock_sock(sk); 11826a026610SGustavo F. Padovan chan->retry_count = 1; 11830a708f8fSGustavo F. Padovan __mod_monitor_timer(); 11840a708f8fSGustavo F. Padovan 1185525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_WAIT_F; 11860a708f8fSGustavo F. Padovan 1187525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 11880a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 11890a708f8fSGustavo F. Padovan } 11900a708f8fSGustavo F. Padovan 119142e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan) 11920a708f8fSGustavo F. Padovan { 11930a708f8fSGustavo F. Padovan struct sk_buff *skb; 11940a708f8fSGustavo F. Padovan 119558d35f87SGustavo F. Padovan while ((skb = skb_peek(&chan->tx_q)) && 11966a026610SGustavo F. Padovan chan->unacked_frames) { 119742e5c802SGustavo F. Padovan if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) 11980a708f8fSGustavo F. Padovan break; 11990a708f8fSGustavo F. Padovan 120058d35f87SGustavo F. Padovan skb = skb_dequeue(&chan->tx_q); 12010a708f8fSGustavo F. Padovan kfree_skb(skb); 12020a708f8fSGustavo F. Padovan 12036a026610SGustavo F. Padovan chan->unacked_frames--; 12040a708f8fSGustavo F. Padovan } 12050a708f8fSGustavo F. Padovan 12066a026610SGustavo F. Padovan if (!chan->unacked_frames) 1207e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 12080a708f8fSGustavo F. Padovan } 12090a708f8fSGustavo F. Padovan 12104343478fSGustavo F. Padovan void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) 12110a708f8fSGustavo F. Padovan { 12128c1d787bSGustavo F. Padovan struct hci_conn *hcon = chan->conn->hcon; 12130a708f8fSGustavo F. Padovan u16 flags; 12140a708f8fSGustavo F. Padovan 12154343478fSGustavo F. Padovan BT_DBG("chan %p, skb %p len %d", chan, skb, skb->len); 12160a708f8fSGustavo F. Padovan 12174343478fSGustavo F. Padovan if (!chan->flushable && lmp_no_flush_capable(hcon->hdev)) 12180a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 12190a708f8fSGustavo F. Padovan else 12200a708f8fSGustavo F. Padovan flags = ACL_START; 12210a708f8fSGustavo F. Padovan 12220a708f8fSGustavo F. Padovan hci_send_acl(hcon, skb, flags); 12230a708f8fSGustavo F. Padovan } 12240a708f8fSGustavo F. Padovan 122542e5c802SGustavo F. Padovan void l2cap_streaming_send(struct l2cap_chan *chan) 12260a708f8fSGustavo F. Padovan { 12270a708f8fSGustavo F. Padovan struct sk_buff *skb; 12280a708f8fSGustavo F. Padovan u16 control, fcs; 12290a708f8fSGustavo F. Padovan 123058d35f87SGustavo F. Padovan while ((skb = skb_dequeue(&chan->tx_q))) { 12310a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data + L2CAP_HDR_SIZE); 123242e5c802SGustavo F. Padovan control |= chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT; 12330a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb->data + L2CAP_HDR_SIZE); 12340a708f8fSGustavo F. Padovan 123547d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 12360a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)skb->data, skb->len - 2); 12370a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb->data + skb->len - 2); 12380a708f8fSGustavo F. Padovan } 12390a708f8fSGustavo F. Padovan 12404343478fSGustavo F. Padovan l2cap_do_send(chan, skb); 12410a708f8fSGustavo F. Padovan 124242e5c802SGustavo F. Padovan chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; 12430a708f8fSGustavo F. Padovan } 12440a708f8fSGustavo F. Padovan } 12450a708f8fSGustavo F. Padovan 1246525cd185SGustavo F. Padovan static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq) 12470a708f8fSGustavo F. Padovan { 12480a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 12490a708f8fSGustavo F. Padovan u16 control, fcs; 12500a708f8fSGustavo F. Padovan 125158d35f87SGustavo F. Padovan skb = skb_peek(&chan->tx_q); 12520a708f8fSGustavo F. Padovan if (!skb) 12530a708f8fSGustavo F. Padovan return; 12540a708f8fSGustavo F. Padovan 12550a708f8fSGustavo F. Padovan do { 12560a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq == tx_seq) 12570a708f8fSGustavo F. Padovan break; 12580a708f8fSGustavo F. Padovan 125958d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 12600a708f8fSGustavo F. Padovan return; 12610a708f8fSGustavo F. Padovan 126258d35f87SGustavo F. Padovan } while ((skb = skb_queue_next(&chan->tx_q, skb))); 12630a708f8fSGustavo F. Padovan 12642c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 12652c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 12668c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 12670a708f8fSGustavo F. Padovan return; 12680a708f8fSGustavo F. Padovan } 12690a708f8fSGustavo F. Padovan 12700a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 12710a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 12720a708f8fSGustavo F. Padovan control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); 1273a429b519SRuiyi Zhang control &= L2CAP_CTRL_SAR; 12740a708f8fSGustavo F. Padovan 1275525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 12760a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 1277525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 12780a708f8fSGustavo F. Padovan } 12790a708f8fSGustavo F. Padovan 128042e5c802SGustavo F. Padovan control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT) 12810a708f8fSGustavo F. Padovan | (tx_seq << L2CAP_CTRL_TXSEQ_SHIFT); 12820a708f8fSGustavo F. Padovan 12830a708f8fSGustavo F. Padovan put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); 12840a708f8fSGustavo F. Padovan 128547d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 12860a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)tx_skb->data, tx_skb->len - 2); 12870a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, tx_skb->data + tx_skb->len - 2); 12880a708f8fSGustavo F. Padovan } 12890a708f8fSGustavo F. Padovan 12904343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 12910a708f8fSGustavo F. Padovan } 12920a708f8fSGustavo F. Padovan 1293525cd185SGustavo F. Padovan int l2cap_ertm_send(struct l2cap_chan *chan) 12940a708f8fSGustavo F. Padovan { 12950a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 1296525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 12970a708f8fSGustavo F. Padovan u16 control, fcs; 12980a708f8fSGustavo F. Padovan int nsent = 0; 12990a708f8fSGustavo F. Padovan 13000a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 13010a708f8fSGustavo F. Padovan return -ENOTCONN; 13020a708f8fSGustavo F. Padovan 130358d35f87SGustavo F. Padovan while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) { 13040a708f8fSGustavo F. Padovan 13052c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 13062c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 13078c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 13080a708f8fSGustavo F. Padovan break; 13090a708f8fSGustavo F. Padovan } 13100a708f8fSGustavo F. Padovan 13110a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 13120a708f8fSGustavo F. Padovan 13130a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 13140a708f8fSGustavo F. Padovan 13150a708f8fSGustavo F. Padovan control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); 13160a708f8fSGustavo F. Padovan control &= L2CAP_CTRL_SAR; 13170a708f8fSGustavo F. Padovan 1318525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 13190a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 1320525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 13210a708f8fSGustavo F. Padovan } 132242e5c802SGustavo F. Padovan control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT) 132342e5c802SGustavo F. Padovan | (chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT); 13240a708f8fSGustavo F. Padovan put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); 13250a708f8fSGustavo F. Padovan 13260a708f8fSGustavo F. Padovan 132747d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 13280a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2); 13290a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb->data + tx_skb->len - 2); 13300a708f8fSGustavo F. Padovan } 13310a708f8fSGustavo F. Padovan 13324343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 13330a708f8fSGustavo F. Padovan 13340a708f8fSGustavo F. Padovan __mod_retrans_timer(); 13350a708f8fSGustavo F. Padovan 133642e5c802SGustavo F. Padovan bt_cb(skb)->tx_seq = chan->next_tx_seq; 133742e5c802SGustavo F. Padovan chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; 13380a708f8fSGustavo F. Padovan 133923e9fde2SSuraj Sumangala if (bt_cb(skb)->retries == 1) 13406a026610SGustavo F. Padovan chan->unacked_frames++; 134123e9fde2SSuraj Sumangala 13426a026610SGustavo F. Padovan chan->frames_sent++; 13430a708f8fSGustavo F. Padovan 134458d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 134558d35f87SGustavo F. Padovan chan->tx_send_head = NULL; 13460a708f8fSGustavo F. Padovan else 134758d35f87SGustavo F. Padovan chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); 13480a708f8fSGustavo F. Padovan 13490a708f8fSGustavo F. Padovan nsent++; 13500a708f8fSGustavo F. Padovan } 13510a708f8fSGustavo F. Padovan 13520a708f8fSGustavo F. Padovan return nsent; 13530a708f8fSGustavo F. Padovan } 13540a708f8fSGustavo F. Padovan 1355525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan) 13560a708f8fSGustavo F. Padovan { 13570a708f8fSGustavo F. Padovan int ret; 13580a708f8fSGustavo F. Padovan 135958d35f87SGustavo F. Padovan if (!skb_queue_empty(&chan->tx_q)) 136058d35f87SGustavo F. Padovan chan->tx_send_head = chan->tx_q.next; 13610a708f8fSGustavo F. Padovan 136242e5c802SGustavo F. Padovan chan->next_tx_seq = chan->expected_ack_seq; 1363525cd185SGustavo F. Padovan ret = l2cap_ertm_send(chan); 13640a708f8fSGustavo F. Padovan return ret; 13650a708f8fSGustavo F. Padovan } 13660a708f8fSGustavo F. Padovan 1367525cd185SGustavo F. Padovan static void l2cap_send_ack(struct l2cap_chan *chan) 13680a708f8fSGustavo F. Padovan { 13690a708f8fSGustavo F. Padovan u16 control = 0; 13700a708f8fSGustavo F. Padovan 137142e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 13720a708f8fSGustavo F. Padovan 1373525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 13740a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 1375525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 1376525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 13770a708f8fSGustavo F. Padovan return; 13780a708f8fSGustavo F. Padovan } 13790a708f8fSGustavo F. Padovan 1380525cd185SGustavo F. Padovan if (l2cap_ertm_send(chan) > 0) 13810a708f8fSGustavo F. Padovan return; 13820a708f8fSGustavo F. Padovan 13830a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 1384525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 13850a708f8fSGustavo F. Padovan } 13860a708f8fSGustavo F. Padovan 1387525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan) 13880a708f8fSGustavo F. Padovan { 13890a708f8fSGustavo F. Padovan struct srej_list *tail; 13900a708f8fSGustavo F. Padovan u16 control; 13910a708f8fSGustavo F. Padovan 13920a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 13930a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 13940a708f8fSGustavo F. Padovan 139539d5a3eeSGustavo F. Padovan tail = list_entry((&chan->srej_l)->prev, struct srej_list, list); 13960a708f8fSGustavo F. Padovan control |= tail->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 13970a708f8fSGustavo F. Padovan 1398525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 13990a708f8fSGustavo F. Padovan } 14000a708f8fSGustavo F. Padovan 14010a708f8fSGustavo F. Padovan static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb) 14020a708f8fSGustavo F. Padovan { 14038c1d787bSGustavo F. Padovan struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn; 14040a708f8fSGustavo F. Padovan struct sk_buff **frag; 14050a708f8fSGustavo F. Padovan int err, sent = 0; 14060a708f8fSGustavo F. Padovan 14070a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) 14080a708f8fSGustavo F. Padovan return -EFAULT; 14090a708f8fSGustavo F. Padovan 14100a708f8fSGustavo F. Padovan sent += count; 14110a708f8fSGustavo F. Padovan len -= count; 14120a708f8fSGustavo F. Padovan 14130a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 14140a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 14150a708f8fSGustavo F. Padovan while (len) { 14160a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 14170a708f8fSGustavo F. Padovan 14180a708f8fSGustavo F. Padovan *frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err); 14190a708f8fSGustavo F. Padovan if (!*frag) 14200a708f8fSGustavo F. Padovan return err; 14210a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) 14220a708f8fSGustavo F. Padovan return -EFAULT; 14230a708f8fSGustavo F. Padovan 14240a708f8fSGustavo F. Padovan sent += count; 14250a708f8fSGustavo F. Padovan len -= count; 14260a708f8fSGustavo F. Padovan 14270a708f8fSGustavo F. Padovan frag = &(*frag)->next; 14280a708f8fSGustavo F. Padovan } 14290a708f8fSGustavo F. Padovan 14300a708f8fSGustavo F. Padovan return sent; 14310a708f8fSGustavo F. Padovan } 14320a708f8fSGustavo F. Padovan 1433fe4128e0SGustavo F. Padovan struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 14340a708f8fSGustavo F. Padovan { 1435fe4128e0SGustavo F. Padovan struct sock *sk = chan->sk; 14368c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 14370a708f8fSGustavo F. Padovan struct sk_buff *skb; 14380a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE + 2; 14390a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 14400a708f8fSGustavo F. Padovan 14410a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 14420a708f8fSGustavo F. Padovan 14430a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 14440a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 14450a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 14460a708f8fSGustavo F. Padovan if (!skb) 14470a708f8fSGustavo F. Padovan return ERR_PTR(err); 14480a708f8fSGustavo F. Padovan 14490a708f8fSGustavo F. Padovan /* Create L2CAP header */ 14500a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1451fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 14520a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 1453fe4128e0SGustavo F. Padovan put_unaligned_le16(chan->psm, skb_put(skb, 2)); 14540a708f8fSGustavo F. Padovan 14550a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 14560a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 14570a708f8fSGustavo F. Padovan kfree_skb(skb); 14580a708f8fSGustavo F. Padovan return ERR_PTR(err); 14590a708f8fSGustavo F. Padovan } 14600a708f8fSGustavo F. Padovan return skb; 14610a708f8fSGustavo F. Padovan } 14620a708f8fSGustavo F. Padovan 1463fe4128e0SGustavo F. Padovan struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 14640a708f8fSGustavo F. Padovan { 1465fe4128e0SGustavo F. Padovan struct sock *sk = chan->sk; 14668c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 14670a708f8fSGustavo F. Padovan struct sk_buff *skb; 14680a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE; 14690a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 14700a708f8fSGustavo F. Padovan 14710a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 14720a708f8fSGustavo F. Padovan 14730a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 14740a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 14750a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 14760a708f8fSGustavo F. Padovan if (!skb) 14770a708f8fSGustavo F. Padovan return ERR_PTR(err); 14780a708f8fSGustavo F. Padovan 14790a708f8fSGustavo F. Padovan /* Create L2CAP header */ 14800a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1481fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 14820a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 14830a708f8fSGustavo F. Padovan 14840a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 14850a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 14860a708f8fSGustavo F. Padovan kfree_skb(skb); 14870a708f8fSGustavo F. Padovan return ERR_PTR(err); 14880a708f8fSGustavo F. Padovan } 14890a708f8fSGustavo F. Padovan return skb; 14900a708f8fSGustavo F. Padovan } 14910a708f8fSGustavo F. Padovan 149247d1ec61SGustavo F. Padovan struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len, u16 control, u16 sdulen) 14930a708f8fSGustavo F. Padovan { 149447d1ec61SGustavo F. Padovan struct sock *sk = chan->sk; 14958c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 14960a708f8fSGustavo F. Padovan struct sk_buff *skb; 14970a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE + 2; 14980a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 14990a708f8fSGustavo F. Padovan 15000a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 15010a708f8fSGustavo F. Padovan 15020a708f8fSGustavo F. Padovan if (!conn) 15030a708f8fSGustavo F. Padovan return ERR_PTR(-ENOTCONN); 15040a708f8fSGustavo F. Padovan 15050a708f8fSGustavo F. Padovan if (sdulen) 15060a708f8fSGustavo F. Padovan hlen += 2; 15070a708f8fSGustavo F. Padovan 150847d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 15090a708f8fSGustavo F. Padovan hlen += 2; 15100a708f8fSGustavo F. Padovan 15110a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 15120a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 15130a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 15140a708f8fSGustavo F. Padovan if (!skb) 15150a708f8fSGustavo F. Padovan return ERR_PTR(err); 15160a708f8fSGustavo F. Padovan 15170a708f8fSGustavo F. Padovan /* Create L2CAP header */ 15180a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1519fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 15200a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 15210a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb_put(skb, 2)); 15220a708f8fSGustavo F. Padovan if (sdulen) 15230a708f8fSGustavo F. Padovan put_unaligned_le16(sdulen, skb_put(skb, 2)); 15240a708f8fSGustavo F. Padovan 15250a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 15260a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 15270a708f8fSGustavo F. Padovan kfree_skb(skb); 15280a708f8fSGustavo F. Padovan return ERR_PTR(err); 15290a708f8fSGustavo F. Padovan } 15300a708f8fSGustavo F. Padovan 153147d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 15320a708f8fSGustavo F. Padovan put_unaligned_le16(0, skb_put(skb, 2)); 15330a708f8fSGustavo F. Padovan 15340a708f8fSGustavo F. Padovan bt_cb(skb)->retries = 0; 15350a708f8fSGustavo F. Padovan return skb; 15360a708f8fSGustavo F. Padovan } 15370a708f8fSGustavo F. Padovan 15382c03a7a4SGustavo F. Padovan int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 15390a708f8fSGustavo F. Padovan { 15400a708f8fSGustavo F. Padovan struct sk_buff *skb; 15410a708f8fSGustavo F. Padovan struct sk_buff_head sar_queue; 15420a708f8fSGustavo F. Padovan u16 control; 15430a708f8fSGustavo F. Padovan size_t size = 0; 15440a708f8fSGustavo F. Padovan 15450a708f8fSGustavo F. Padovan skb_queue_head_init(&sar_queue); 15460a708f8fSGustavo F. Padovan control = L2CAP_SDU_START; 154747d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len); 15480a708f8fSGustavo F. Padovan if (IS_ERR(skb)) 15490a708f8fSGustavo F. Padovan return PTR_ERR(skb); 15500a708f8fSGustavo F. Padovan 15510a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 15522c03a7a4SGustavo F. Padovan len -= chan->remote_mps; 15532c03a7a4SGustavo F. Padovan size += chan->remote_mps; 15540a708f8fSGustavo F. Padovan 15550a708f8fSGustavo F. Padovan while (len > 0) { 15560a708f8fSGustavo F. Padovan size_t buflen; 15570a708f8fSGustavo F. Padovan 15582c03a7a4SGustavo F. Padovan if (len > chan->remote_mps) { 15590a708f8fSGustavo F. Padovan control = L2CAP_SDU_CONTINUE; 15602c03a7a4SGustavo F. Padovan buflen = chan->remote_mps; 15610a708f8fSGustavo F. Padovan } else { 15620a708f8fSGustavo F. Padovan control = L2CAP_SDU_END; 15630a708f8fSGustavo F. Padovan buflen = len; 15640a708f8fSGustavo F. Padovan } 15650a708f8fSGustavo F. Padovan 156647d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0); 15670a708f8fSGustavo F. Padovan if (IS_ERR(skb)) { 15680a708f8fSGustavo F. Padovan skb_queue_purge(&sar_queue); 15690a708f8fSGustavo F. Padovan return PTR_ERR(skb); 15700a708f8fSGustavo F. Padovan } 15710a708f8fSGustavo F. Padovan 15720a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 15730a708f8fSGustavo F. Padovan len -= buflen; 15740a708f8fSGustavo F. Padovan size += buflen; 15750a708f8fSGustavo F. Padovan } 157658d35f87SGustavo F. Padovan skb_queue_splice_tail(&sar_queue, &chan->tx_q); 157758d35f87SGustavo F. Padovan if (chan->tx_send_head == NULL) 157858d35f87SGustavo F. Padovan chan->tx_send_head = sar_queue.next; 15790a708f8fSGustavo F. Padovan 15800a708f8fSGustavo F. Padovan return size; 15810a708f8fSGustavo F. Padovan } 15820a708f8fSGustavo F. Padovan 15839a91a04aSGustavo F. Padovan int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 15849a91a04aSGustavo F. Padovan { 15859a91a04aSGustavo F. Padovan struct sk_buff *skb; 15869a91a04aSGustavo F. Padovan u16 control; 15879a91a04aSGustavo F. Padovan int err; 15889a91a04aSGustavo F. Padovan 15899a91a04aSGustavo F. Padovan /* Connectionless channel */ 1590715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { 15919a91a04aSGustavo F. Padovan skb = l2cap_create_connless_pdu(chan, msg, len); 15929a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 15939a91a04aSGustavo F. Padovan return PTR_ERR(skb); 15949a91a04aSGustavo F. Padovan 15959a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 15969a91a04aSGustavo F. Padovan return len; 15979a91a04aSGustavo F. Padovan } 15989a91a04aSGustavo F. Padovan 15999a91a04aSGustavo F. Padovan switch (chan->mode) { 16009a91a04aSGustavo F. Padovan case L2CAP_MODE_BASIC: 16019a91a04aSGustavo F. Padovan /* Check outgoing MTU */ 16029a91a04aSGustavo F. Padovan if (len > chan->omtu) 16039a91a04aSGustavo F. Padovan return -EMSGSIZE; 16049a91a04aSGustavo F. Padovan 16059a91a04aSGustavo F. Padovan /* Create a basic PDU */ 16069a91a04aSGustavo F. Padovan skb = l2cap_create_basic_pdu(chan, msg, len); 16079a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 16089a91a04aSGustavo F. Padovan return PTR_ERR(skb); 16099a91a04aSGustavo F. Padovan 16109a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 16119a91a04aSGustavo F. Padovan err = len; 16129a91a04aSGustavo F. Padovan break; 16139a91a04aSGustavo F. Padovan 16149a91a04aSGustavo F. Padovan case L2CAP_MODE_ERTM: 16159a91a04aSGustavo F. Padovan case L2CAP_MODE_STREAMING: 16169a91a04aSGustavo F. Padovan /* Entire SDU fits into one PDU */ 16179a91a04aSGustavo F. Padovan if (len <= chan->remote_mps) { 16189a91a04aSGustavo F. Padovan control = L2CAP_SDU_UNSEGMENTED; 16199a91a04aSGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, len, control, 16209a91a04aSGustavo F. Padovan 0); 16219a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 16229a91a04aSGustavo F. Padovan return PTR_ERR(skb); 16239a91a04aSGustavo F. Padovan 16249a91a04aSGustavo F. Padovan __skb_queue_tail(&chan->tx_q, skb); 16259a91a04aSGustavo F. Padovan 16269a91a04aSGustavo F. Padovan if (chan->tx_send_head == NULL) 16279a91a04aSGustavo F. Padovan chan->tx_send_head = skb; 16289a91a04aSGustavo F. Padovan 16299a91a04aSGustavo F. Padovan } else { 16309a91a04aSGustavo F. Padovan /* Segment SDU into multiples PDUs */ 16319a91a04aSGustavo F. Padovan err = l2cap_sar_segment_sdu(chan, msg, len); 16329a91a04aSGustavo F. Padovan if (err < 0) 16339a91a04aSGustavo F. Padovan return err; 16349a91a04aSGustavo F. Padovan } 16359a91a04aSGustavo F. Padovan 16369a91a04aSGustavo F. Padovan if (chan->mode == L2CAP_MODE_STREAMING) { 16379a91a04aSGustavo F. Padovan l2cap_streaming_send(chan); 16389a91a04aSGustavo F. Padovan err = len; 16399a91a04aSGustavo F. Padovan break; 16409a91a04aSGustavo F. Padovan } 16419a91a04aSGustavo F. Padovan 16429a91a04aSGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && 16439a91a04aSGustavo F. Padovan (chan->conn_state & L2CAP_CONN_WAIT_F)) { 16449a91a04aSGustavo F. Padovan err = len; 16459a91a04aSGustavo F. Padovan break; 16469a91a04aSGustavo F. Padovan } 16479a91a04aSGustavo F. Padovan 16489a91a04aSGustavo F. Padovan err = l2cap_ertm_send(chan); 16499a91a04aSGustavo F. Padovan if (err >= 0) 16509a91a04aSGustavo F. Padovan err = len; 16519a91a04aSGustavo F. Padovan 16529a91a04aSGustavo F. Padovan break; 16539a91a04aSGustavo F. Padovan 16549a91a04aSGustavo F. Padovan default: 16559a91a04aSGustavo F. Padovan BT_DBG("bad state %1.1x", chan->mode); 16569a91a04aSGustavo F. Padovan err = -EBADFD; 16579a91a04aSGustavo F. Padovan } 16589a91a04aSGustavo F. Padovan 16599a91a04aSGustavo F. Padovan return err; 16609a91a04aSGustavo F. Padovan } 16619a91a04aSGustavo F. Padovan 16620a708f8fSGustavo F. Padovan static void l2cap_chan_ready(struct sock *sk) 16630a708f8fSGustavo F. Padovan { 16640a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 1665b4450035SGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 16660a708f8fSGustavo F. Padovan 16670a708f8fSGustavo F. Padovan BT_DBG("sk %p, parent %p", sk, parent); 16680a708f8fSGustavo F. Padovan 1669b4450035SGustavo F. Padovan chan->conf_state = 0; 1670*ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 16710a708f8fSGustavo F. Padovan 16720a708f8fSGustavo F. Padovan if (!parent) { 16730a708f8fSGustavo F. Padovan /* Outgoing channel. 16740a708f8fSGustavo F. Padovan * Wake up socket sleeping on connect. 16750a708f8fSGustavo F. Padovan */ 16760a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 16770a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 16780a708f8fSGustavo F. Padovan } else { 16790a708f8fSGustavo F. Padovan /* Incoming channel. 16800a708f8fSGustavo F. Padovan * Wake up socket sleeping on accept. 16810a708f8fSGustavo F. Padovan */ 16820a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 16830a708f8fSGustavo F. Padovan } 16840a708f8fSGustavo F. Padovan } 16850a708f8fSGustavo F. Padovan 16860a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */ 16870a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) 16880a708f8fSGustavo F. Padovan { 16890a708f8fSGustavo F. Padovan struct sk_buff *nskb; 169048454079SGustavo F. Padovan struct l2cap_chan *chan; 16910a708f8fSGustavo F. Padovan 16920a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 16930a708f8fSGustavo F. Padovan 1694baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 1695baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 169648454079SGustavo F. Padovan struct sock *sk = chan->sk; 1697715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_RAW) 16980a708f8fSGustavo F. Padovan continue; 16990a708f8fSGustavo F. Padovan 17000a708f8fSGustavo F. Padovan /* Don't send frame to the socket it came from */ 17010a708f8fSGustavo F. Padovan if (skb->sk == sk) 17020a708f8fSGustavo F. Padovan continue; 17030a708f8fSGustavo F. Padovan nskb = skb_clone(skb, GFP_ATOMIC); 17040a708f8fSGustavo F. Padovan if (!nskb) 17050a708f8fSGustavo F. Padovan continue; 17060a708f8fSGustavo F. Padovan 17070a708f8fSGustavo F. Padovan if (sock_queue_rcv_skb(sk, nskb)) 17080a708f8fSGustavo F. Padovan kfree_skb(nskb); 17090a708f8fSGustavo F. Padovan } 1710baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 17110a708f8fSGustavo F. Padovan } 17120a708f8fSGustavo F. Padovan 17130a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */ 17140a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 17150a708f8fSGustavo F. Padovan u8 code, u8 ident, u16 dlen, void *data) 17160a708f8fSGustavo F. Padovan { 17170a708f8fSGustavo F. Padovan struct sk_buff *skb, **frag; 17180a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr *cmd; 17190a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 17200a708f8fSGustavo F. Padovan int len, count; 17210a708f8fSGustavo F. Padovan 17220a708f8fSGustavo F. Padovan BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", 17230a708f8fSGustavo F. Padovan conn, code, ident, dlen); 17240a708f8fSGustavo F. Padovan 17250a708f8fSGustavo F. Padovan len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; 17260a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 17270a708f8fSGustavo F. Padovan 17280a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 17290a708f8fSGustavo F. Padovan if (!skb) 17300a708f8fSGustavo F. Padovan return NULL; 17310a708f8fSGustavo F. Padovan 17320a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 17330a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); 17343300d9a9SClaudio Takahasi 17353300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 17363300d9a9SClaudio Takahasi lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); 17373300d9a9SClaudio Takahasi else 17380a708f8fSGustavo F. Padovan lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); 17390a708f8fSGustavo F. Padovan 17400a708f8fSGustavo F. Padovan cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); 17410a708f8fSGustavo F. Padovan cmd->code = code; 17420a708f8fSGustavo F. Padovan cmd->ident = ident; 17430a708f8fSGustavo F. Padovan cmd->len = cpu_to_le16(dlen); 17440a708f8fSGustavo F. Padovan 17450a708f8fSGustavo F. Padovan if (dlen) { 17460a708f8fSGustavo F. Padovan count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; 17470a708f8fSGustavo F. Padovan memcpy(skb_put(skb, count), data, count); 17480a708f8fSGustavo F. Padovan data += count; 17490a708f8fSGustavo F. Padovan } 17500a708f8fSGustavo F. Padovan 17510a708f8fSGustavo F. Padovan len -= skb->len; 17520a708f8fSGustavo F. Padovan 17530a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 17540a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 17550a708f8fSGustavo F. Padovan while (len) { 17560a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 17570a708f8fSGustavo F. Padovan 17580a708f8fSGustavo F. Padovan *frag = bt_skb_alloc(count, GFP_ATOMIC); 17590a708f8fSGustavo F. Padovan if (!*frag) 17600a708f8fSGustavo F. Padovan goto fail; 17610a708f8fSGustavo F. Padovan 17620a708f8fSGustavo F. Padovan memcpy(skb_put(*frag, count), data, count); 17630a708f8fSGustavo F. Padovan 17640a708f8fSGustavo F. Padovan len -= count; 17650a708f8fSGustavo F. Padovan data += count; 17660a708f8fSGustavo F. Padovan 17670a708f8fSGustavo F. Padovan frag = &(*frag)->next; 17680a708f8fSGustavo F. Padovan } 17690a708f8fSGustavo F. Padovan 17700a708f8fSGustavo F. Padovan return skb; 17710a708f8fSGustavo F. Padovan 17720a708f8fSGustavo F. Padovan fail: 17730a708f8fSGustavo F. Padovan kfree_skb(skb); 17740a708f8fSGustavo F. Padovan return NULL; 17750a708f8fSGustavo F. Padovan } 17760a708f8fSGustavo F. Padovan 17770a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) 17780a708f8fSGustavo F. Padovan { 17790a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 17800a708f8fSGustavo F. Padovan int len; 17810a708f8fSGustavo F. Padovan 17820a708f8fSGustavo F. Padovan len = L2CAP_CONF_OPT_SIZE + opt->len; 17830a708f8fSGustavo F. Padovan *ptr += len; 17840a708f8fSGustavo F. Padovan 17850a708f8fSGustavo F. Padovan *type = opt->type; 17860a708f8fSGustavo F. Padovan *olen = opt->len; 17870a708f8fSGustavo F. Padovan 17880a708f8fSGustavo F. Padovan switch (opt->len) { 17890a708f8fSGustavo F. Padovan case 1: 17900a708f8fSGustavo F. Padovan *val = *((u8 *) opt->val); 17910a708f8fSGustavo F. Padovan break; 17920a708f8fSGustavo F. Padovan 17930a708f8fSGustavo F. Padovan case 2: 17940a708f8fSGustavo F. Padovan *val = get_unaligned_le16(opt->val); 17950a708f8fSGustavo F. Padovan break; 17960a708f8fSGustavo F. Padovan 17970a708f8fSGustavo F. Padovan case 4: 17980a708f8fSGustavo F. Padovan *val = get_unaligned_le32(opt->val); 17990a708f8fSGustavo F. Padovan break; 18000a708f8fSGustavo F. Padovan 18010a708f8fSGustavo F. Padovan default: 18020a708f8fSGustavo F. Padovan *val = (unsigned long) opt->val; 18030a708f8fSGustavo F. Padovan break; 18040a708f8fSGustavo F. Padovan } 18050a708f8fSGustavo F. Padovan 18060a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); 18070a708f8fSGustavo F. Padovan return len; 18080a708f8fSGustavo F. Padovan } 18090a708f8fSGustavo F. Padovan 18100a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) 18110a708f8fSGustavo F. Padovan { 18120a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 18130a708f8fSGustavo F. Padovan 18140a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); 18150a708f8fSGustavo F. Padovan 18160a708f8fSGustavo F. Padovan opt->type = type; 18170a708f8fSGustavo F. Padovan opt->len = len; 18180a708f8fSGustavo F. Padovan 18190a708f8fSGustavo F. Padovan switch (len) { 18200a708f8fSGustavo F. Padovan case 1: 18210a708f8fSGustavo F. Padovan *((u8 *) opt->val) = val; 18220a708f8fSGustavo F. Padovan break; 18230a708f8fSGustavo F. Padovan 18240a708f8fSGustavo F. Padovan case 2: 18250a708f8fSGustavo F. Padovan put_unaligned_le16(val, opt->val); 18260a708f8fSGustavo F. Padovan break; 18270a708f8fSGustavo F. Padovan 18280a708f8fSGustavo F. Padovan case 4: 18290a708f8fSGustavo F. Padovan put_unaligned_le32(val, opt->val); 18300a708f8fSGustavo F. Padovan break; 18310a708f8fSGustavo F. Padovan 18320a708f8fSGustavo F. Padovan default: 18330a708f8fSGustavo F. Padovan memcpy(opt->val, (void *) val, len); 18340a708f8fSGustavo F. Padovan break; 18350a708f8fSGustavo F. Padovan } 18360a708f8fSGustavo F. Padovan 18370a708f8fSGustavo F. Padovan *ptr += L2CAP_CONF_OPT_SIZE + len; 18380a708f8fSGustavo F. Padovan } 18390a708f8fSGustavo F. Padovan 18400a708f8fSGustavo F. Padovan static void l2cap_ack_timeout(unsigned long arg) 18410a708f8fSGustavo F. Padovan { 1842525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 18430a708f8fSGustavo F. Padovan 1844525cd185SGustavo F. Padovan bh_lock_sock(chan->sk); 1845525cd185SGustavo F. Padovan l2cap_send_ack(chan); 1846525cd185SGustavo F. Padovan bh_unlock_sock(chan->sk); 18470a708f8fSGustavo F. Padovan } 18480a708f8fSGustavo F. Padovan 1849525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan) 18500a708f8fSGustavo F. Padovan { 1851525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 1852525cd185SGustavo F. Padovan 185342e5c802SGustavo F. Padovan chan->expected_ack_seq = 0; 18546a026610SGustavo F. Padovan chan->unacked_frames = 0; 185542e5c802SGustavo F. Padovan chan->buffer_seq = 0; 18566a026610SGustavo F. Padovan chan->num_acked = 0; 18576a026610SGustavo F. Padovan chan->frames_sent = 0; 18580a708f8fSGustavo F. Padovan 1859e92c8e70SGustavo F. Padovan setup_timer(&chan->retrans_timer, l2cap_retrans_timeout, 1860e92c8e70SGustavo F. Padovan (unsigned long) chan); 1861e92c8e70SGustavo F. Padovan setup_timer(&chan->monitor_timer, l2cap_monitor_timeout, 1862e92c8e70SGustavo F. Padovan (unsigned long) chan); 1863e92c8e70SGustavo F. Padovan setup_timer(&chan->ack_timer, l2cap_ack_timeout, (unsigned long) chan); 18640a708f8fSGustavo F. Padovan 1865f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->srej_q); 1866f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->busy_q); 18670a708f8fSGustavo F. Padovan 186839d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 186939d5a3eeSGustavo F. Padovan 1870311bb895SGustavo F. Padovan INIT_WORK(&chan->busy_work, l2cap_busy_work); 18710a708f8fSGustavo F. Padovan 18720a708f8fSGustavo F. Padovan sk->sk_backlog_rcv = l2cap_ertm_data_rcv; 18730a708f8fSGustavo F. Padovan } 18740a708f8fSGustavo F. Padovan 18750a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) 18760a708f8fSGustavo F. Padovan { 18770a708f8fSGustavo F. Padovan switch (mode) { 18780a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 18790a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 18800a708f8fSGustavo F. Padovan if (l2cap_mode_supported(mode, remote_feat_mask)) 18810a708f8fSGustavo F. Padovan return mode; 18820a708f8fSGustavo F. Padovan /* fall through */ 18830a708f8fSGustavo F. Padovan default: 18840a708f8fSGustavo F. Padovan return L2CAP_MODE_BASIC; 18850a708f8fSGustavo F. Padovan } 18860a708f8fSGustavo F. Padovan } 18870a708f8fSGustavo F. Padovan 1888710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) 18890a708f8fSGustavo F. Padovan { 18900a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 18910c1bc5c6SGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = chan->mode }; 18920a708f8fSGustavo F. Padovan void *ptr = req->data; 18930a708f8fSGustavo F. Padovan 189449208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 18950a708f8fSGustavo F. Padovan 189673ffa904SGustavo F. Padovan if (chan->num_conf_req || chan->num_conf_rsp) 18970a708f8fSGustavo F. Padovan goto done; 18980a708f8fSGustavo F. Padovan 18990c1bc5c6SGustavo F. Padovan switch (chan->mode) { 19000a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 19010a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 1902b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_STATE2_DEVICE) 19030a708f8fSGustavo F. Padovan break; 19040a708f8fSGustavo F. Padovan 19050a708f8fSGustavo F. Padovan /* fall through */ 19060a708f8fSGustavo F. Padovan default: 19078c1d787bSGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); 19080a708f8fSGustavo F. Padovan break; 19090a708f8fSGustavo F. Padovan } 19100a708f8fSGustavo F. Padovan 19110a708f8fSGustavo F. Padovan done: 19120c1bc5c6SGustavo F. Padovan if (chan->imtu != L2CAP_DEFAULT_MTU) 19130c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 19140a708f8fSGustavo F. Padovan 19150c1bc5c6SGustavo F. Padovan switch (chan->mode) { 19160a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 19178c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && 19188c1d787bSGustavo F. Padovan !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) 19190a708f8fSGustavo F. Padovan break; 19200a708f8fSGustavo F. Padovan 19210a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_BASIC; 19220a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 19230a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 19240a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 19250a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 19260a708f8fSGustavo F. Padovan rfc.max_pdu_size = 0; 19270a708f8fSGustavo F. Padovan 19280a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 19290a708f8fSGustavo F. Padovan (unsigned long) &rfc); 19300a708f8fSGustavo F. Padovan break; 19310a708f8fSGustavo F. Padovan 19320a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 19330a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_ERTM; 193447d1ec61SGustavo F. Padovan rfc.txwin_size = chan->tx_win; 193547d1ec61SGustavo F. Padovan rfc.max_transmit = chan->max_tx; 19360a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 19370a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 19380a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); 19398c1d787bSGustavo F. Padovan if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10) 19408c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 19410a708f8fSGustavo F. Padovan 19420a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 19430a708f8fSGustavo F. Padovan (unsigned long) &rfc); 19440a708f8fSGustavo F. Padovan 19458c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 19460a708f8fSGustavo F. Padovan break; 19470a708f8fSGustavo F. Padovan 194847d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 1949b4450035SGustavo F. Padovan chan->conf_state & L2CAP_CONF_NO_FCS_RECV) { 195047d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 195147d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 19520a708f8fSGustavo F. Padovan } 19530a708f8fSGustavo F. Padovan break; 19540a708f8fSGustavo F. Padovan 19550a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 19560a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_STREAMING; 19570a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 19580a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 19590a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 19600a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 19610a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); 19628c1d787bSGustavo F. Padovan if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10) 19638c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 19640a708f8fSGustavo F. Padovan 19650a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 19660a708f8fSGustavo F. Padovan (unsigned long) &rfc); 19670a708f8fSGustavo F. Padovan 19688c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 19690a708f8fSGustavo F. Padovan break; 19700a708f8fSGustavo F. Padovan 197147d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 1972b4450035SGustavo F. Padovan chan->conf_state & L2CAP_CONF_NO_FCS_RECV) { 197347d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 197447d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 19750a708f8fSGustavo F. Padovan } 19760a708f8fSGustavo F. Padovan break; 19770a708f8fSGustavo F. Padovan } 19780a708f8fSGustavo F. Padovan 1979fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 19800a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0); 19810a708f8fSGustavo F. Padovan 19820a708f8fSGustavo F. Padovan return ptr - data; 19830a708f8fSGustavo F. Padovan } 19840a708f8fSGustavo F. Padovan 198573ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) 19860a708f8fSGustavo F. Padovan { 19870a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 19880a708f8fSGustavo F. Padovan void *ptr = rsp->data; 198973ffa904SGustavo F. Padovan void *req = chan->conf_req; 199073ffa904SGustavo F. Padovan int len = chan->conf_len; 19910a708f8fSGustavo F. Padovan int type, hint, olen; 19920a708f8fSGustavo F. Padovan unsigned long val; 19930a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 19940a708f8fSGustavo F. Padovan u16 mtu = L2CAP_DEFAULT_MTU; 19950a708f8fSGustavo F. Padovan u16 result = L2CAP_CONF_SUCCESS; 19960a708f8fSGustavo F. Padovan 199773ffa904SGustavo F. Padovan BT_DBG("chan %p", chan); 19980a708f8fSGustavo F. Padovan 19990a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 20000a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&req, &type, &olen, &val); 20010a708f8fSGustavo F. Padovan 20020a708f8fSGustavo F. Padovan hint = type & L2CAP_CONF_HINT; 20030a708f8fSGustavo F. Padovan type &= L2CAP_CONF_MASK; 20040a708f8fSGustavo F. Padovan 20050a708f8fSGustavo F. Padovan switch (type) { 20060a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 20070a708f8fSGustavo F. Padovan mtu = val; 20080a708f8fSGustavo F. Padovan break; 20090a708f8fSGustavo F. Padovan 20100a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 20110c1bc5c6SGustavo F. Padovan chan->flush_to = val; 20120a708f8fSGustavo F. Padovan break; 20130a708f8fSGustavo F. Padovan 20140a708f8fSGustavo F. Padovan case L2CAP_CONF_QOS: 20150a708f8fSGustavo F. Padovan break; 20160a708f8fSGustavo F. Padovan 20170a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 20180a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 20190a708f8fSGustavo F. Padovan memcpy(&rfc, (void *) val, olen); 20200a708f8fSGustavo F. Padovan break; 20210a708f8fSGustavo F. Padovan 20220a708f8fSGustavo F. Padovan case L2CAP_CONF_FCS: 20230a708f8fSGustavo F. Padovan if (val == L2CAP_FCS_NONE) 2024b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_NO_FCS_RECV; 20250a708f8fSGustavo F. Padovan 20260a708f8fSGustavo F. Padovan break; 20270a708f8fSGustavo F. Padovan 20280a708f8fSGustavo F. Padovan default: 20290a708f8fSGustavo F. Padovan if (hint) 20300a708f8fSGustavo F. Padovan break; 20310a708f8fSGustavo F. Padovan 20320a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNKNOWN; 20330a708f8fSGustavo F. Padovan *((u8 *) ptr++) = type; 20340a708f8fSGustavo F. Padovan break; 20350a708f8fSGustavo F. Padovan } 20360a708f8fSGustavo F. Padovan } 20370a708f8fSGustavo F. Padovan 203873ffa904SGustavo F. Padovan if (chan->num_conf_rsp || chan->num_conf_req > 1) 20390a708f8fSGustavo F. Padovan goto done; 20400a708f8fSGustavo F. Padovan 20410c1bc5c6SGustavo F. Padovan switch (chan->mode) { 20420a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 20430a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 2044b4450035SGustavo F. Padovan if (!(chan->conf_state & L2CAP_CONF_STATE2_DEVICE)) { 20450c1bc5c6SGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, 20468c1d787bSGustavo F. Padovan chan->conn->feat_mask); 20470a708f8fSGustavo F. Padovan break; 20480a708f8fSGustavo F. Padovan } 20490a708f8fSGustavo F. Padovan 20500c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) 20510a708f8fSGustavo F. Padovan return -ECONNREFUSED; 20520a708f8fSGustavo F. Padovan 20530a708f8fSGustavo F. Padovan break; 20540a708f8fSGustavo F. Padovan } 20550a708f8fSGustavo F. Padovan 20560a708f8fSGustavo F. Padovan done: 20570c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) { 20580a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 20590c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 20600a708f8fSGustavo F. Padovan 206173ffa904SGustavo F. Padovan if (chan->num_conf_rsp == 1) 20620a708f8fSGustavo F. Padovan return -ECONNREFUSED; 20630a708f8fSGustavo F. Padovan 20640a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 20650a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 20660a708f8fSGustavo F. Padovan } 20670a708f8fSGustavo F. Padovan 20680a708f8fSGustavo F. Padovan 20690a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) { 20700a708f8fSGustavo F. Padovan /* Configure output options and let the other side know 20710a708f8fSGustavo F. Padovan * which ones we don't like. */ 20720a708f8fSGustavo F. Padovan 20730a708f8fSGustavo F. Padovan if (mtu < L2CAP_DEFAULT_MIN_MTU) 20740a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 20750a708f8fSGustavo F. Padovan else { 20760c1bc5c6SGustavo F. Padovan chan->omtu = mtu; 2077b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MTU_DONE; 20780a708f8fSGustavo F. Padovan } 20790c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); 20800a708f8fSGustavo F. Padovan 20810a708f8fSGustavo F. Padovan switch (rfc.mode) { 20820a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 208347d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2084b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MODE_DONE; 20850a708f8fSGustavo F. Padovan break; 20860a708f8fSGustavo F. Padovan 20870a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 20882c03a7a4SGustavo F. Padovan chan->remote_tx_win = rfc.txwin_size; 20892c03a7a4SGustavo F. Padovan chan->remote_max_tx = rfc.max_transmit; 20900a708f8fSGustavo F. Padovan 20918c1d787bSGustavo F. Padovan if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10) 20928c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 20930a708f8fSGustavo F. Padovan 20942c03a7a4SGustavo F. Padovan chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); 20950a708f8fSGustavo F. Padovan 20960a708f8fSGustavo F. Padovan rfc.retrans_timeout = 20970a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO); 20980a708f8fSGustavo F. Padovan rfc.monitor_timeout = 20990a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO); 21000a708f8fSGustavo F. Padovan 2101b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MODE_DONE; 21020a708f8fSGustavo F. Padovan 21030a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 21040a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 21050a708f8fSGustavo F. Padovan 21060a708f8fSGustavo F. Padovan break; 21070a708f8fSGustavo F. Padovan 21080a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 21098c1d787bSGustavo F. Padovan if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10) 21108c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 21110a708f8fSGustavo F. Padovan 21122c03a7a4SGustavo F. Padovan chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); 21130a708f8fSGustavo F. Padovan 2114b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MODE_DONE; 21150a708f8fSGustavo F. Padovan 21160a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 21170a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 21180a708f8fSGustavo F. Padovan 21190a708f8fSGustavo F. Padovan break; 21200a708f8fSGustavo F. Padovan 21210a708f8fSGustavo F. Padovan default: 21220a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 21230a708f8fSGustavo F. Padovan 21240a708f8fSGustavo F. Padovan memset(&rfc, 0, sizeof(rfc)); 21250c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 21260a708f8fSGustavo F. Padovan } 21270a708f8fSGustavo F. Padovan 21280a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) 2129b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_OUTPUT_DONE; 21300a708f8fSGustavo F. Padovan } 2131fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 21320a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 21330a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(0x0000); 21340a708f8fSGustavo F. Padovan 21350a708f8fSGustavo F. Padovan return ptr - data; 21360a708f8fSGustavo F. Padovan } 21370a708f8fSGustavo F. Padovan 2138b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result) 21390a708f8fSGustavo F. Padovan { 21400a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 21410a708f8fSGustavo F. Padovan void *ptr = req->data; 21420a708f8fSGustavo F. Padovan int type, olen; 21430a708f8fSGustavo F. Padovan unsigned long val; 21440a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 21450a708f8fSGustavo F. Padovan 2146fe4128e0SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data); 21470a708f8fSGustavo F. Padovan 21480a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 21490a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 21500a708f8fSGustavo F. Padovan 21510a708f8fSGustavo F. Padovan switch (type) { 21520a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 21530a708f8fSGustavo F. Padovan if (val < L2CAP_DEFAULT_MIN_MTU) { 21540a708f8fSGustavo F. Padovan *result = L2CAP_CONF_UNACCEPT; 21550c1bc5c6SGustavo F. Padovan chan->imtu = L2CAP_DEFAULT_MIN_MTU; 21560a708f8fSGustavo F. Padovan } else 21570c1bc5c6SGustavo F. Padovan chan->imtu = val; 21580c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 21590a708f8fSGustavo F. Padovan break; 21600a708f8fSGustavo F. Padovan 21610a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 21620c1bc5c6SGustavo F. Padovan chan->flush_to = val; 21630a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 21640c1bc5c6SGustavo F. Padovan 2, chan->flush_to); 21650a708f8fSGustavo F. Padovan break; 21660a708f8fSGustavo F. Padovan 21670a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 21680a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 21690a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 21700a708f8fSGustavo F. Padovan 2171b4450035SGustavo F. Padovan if ((chan->conf_state & L2CAP_CONF_STATE2_DEVICE) && 21720c1bc5c6SGustavo F. Padovan rfc.mode != chan->mode) 21730a708f8fSGustavo F. Padovan return -ECONNREFUSED; 21740a708f8fSGustavo F. Padovan 217547d1ec61SGustavo F. Padovan chan->fcs = 0; 21760a708f8fSGustavo F. Padovan 21770a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 21780a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 21790a708f8fSGustavo F. Padovan break; 21800a708f8fSGustavo F. Padovan } 21810a708f8fSGustavo F. Padovan } 21820a708f8fSGustavo F. Padovan 21830c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) 21840a708f8fSGustavo F. Padovan return -ECONNREFUSED; 21850a708f8fSGustavo F. Padovan 21860c1bc5c6SGustavo F. Padovan chan->mode = rfc.mode; 21870a708f8fSGustavo F. Padovan 21880a708f8fSGustavo F. Padovan if (*result == L2CAP_CONF_SUCCESS) { 21890a708f8fSGustavo F. Padovan switch (rfc.mode) { 21900a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 219147d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 219247d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 219347d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 21940a708f8fSGustavo F. Padovan break; 21950a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 219647d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 21970a708f8fSGustavo F. Padovan } 21980a708f8fSGustavo F. Padovan } 21990a708f8fSGustavo F. Padovan 2200fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 22010a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0x0000); 22020a708f8fSGustavo F. Padovan 22030a708f8fSGustavo F. Padovan return ptr - data; 22040a708f8fSGustavo F. Padovan } 22050a708f8fSGustavo F. Padovan 2206fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags) 22070a708f8fSGustavo F. Padovan { 22080a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 22090a708f8fSGustavo F. Padovan void *ptr = rsp->data; 22100a708f8fSGustavo F. Padovan 2211fe4128e0SGustavo F. Padovan BT_DBG("chan %p", chan); 22120a708f8fSGustavo F. Padovan 2213fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 22140a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 22150a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(flags); 22160a708f8fSGustavo F. Padovan 22170a708f8fSGustavo F. Padovan return ptr - data; 22180a708f8fSGustavo F. Padovan } 22190a708f8fSGustavo F. Padovan 22208c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) 2221710f9b0aSGustavo F. Padovan { 2222710f9b0aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 22238c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 2224710f9b0aSGustavo F. Padovan u8 buf[128]; 2225710f9b0aSGustavo F. Padovan 2226fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 2227fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 2228710f9b0aSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 2229710f9b0aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 2230710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 2231710f9b0aSGustavo F. Padovan L2CAP_CONN_RSP, sizeof(rsp), &rsp); 2232710f9b0aSGustavo F. Padovan 2233b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_REQ_SENT) 2234710f9b0aSGustavo F. Padovan return; 2235710f9b0aSGustavo F. Padovan 2236b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 2237710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 2238710f9b0aSGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 2239710f9b0aSGustavo F. Padovan chan->num_conf_req++; 2240710f9b0aSGustavo F. Padovan } 2241710f9b0aSGustavo F. Padovan 224247d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len) 22430a708f8fSGustavo F. Padovan { 22440a708f8fSGustavo F. Padovan int type, olen; 22450a708f8fSGustavo F. Padovan unsigned long val; 22460a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 22470a708f8fSGustavo F. Padovan 224847d1ec61SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); 22490a708f8fSGustavo F. Padovan 22500c1bc5c6SGustavo F. Padovan if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) 22510a708f8fSGustavo F. Padovan return; 22520a708f8fSGustavo F. Padovan 22530a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 22540a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 22550a708f8fSGustavo F. Padovan 22560a708f8fSGustavo F. Padovan switch (type) { 22570a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 22580a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 22590a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 22600a708f8fSGustavo F. Padovan goto done; 22610a708f8fSGustavo F. Padovan } 22620a708f8fSGustavo F. Padovan } 22630a708f8fSGustavo F. Padovan 22640a708f8fSGustavo F. Padovan done: 22650a708f8fSGustavo F. Padovan switch (rfc.mode) { 22660a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 226747d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 226847d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 226947d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 22700a708f8fSGustavo F. Padovan break; 22710a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 227247d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 22730a708f8fSGustavo F. Padovan } 22740a708f8fSGustavo F. Padovan } 22750a708f8fSGustavo F. Padovan 22760a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 22770a708f8fSGustavo F. Padovan { 22780a708f8fSGustavo F. Padovan struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data; 22790a708f8fSGustavo F. Padovan 22800a708f8fSGustavo F. Padovan if (rej->reason != 0x0000) 22810a708f8fSGustavo F. Padovan return 0; 22820a708f8fSGustavo F. Padovan 22830a708f8fSGustavo F. Padovan if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && 22840a708f8fSGustavo F. Padovan cmd->ident == conn->info_ident) { 22850a708f8fSGustavo F. Padovan del_timer(&conn->info_timer); 22860a708f8fSGustavo F. Padovan 22870a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 22880a708f8fSGustavo F. Padovan conn->info_ident = 0; 22890a708f8fSGustavo F. Padovan 22900a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 22910a708f8fSGustavo F. Padovan } 22920a708f8fSGustavo F. Padovan 22930a708f8fSGustavo F. Padovan return 0; 22940a708f8fSGustavo F. Padovan } 22950a708f8fSGustavo F. Padovan 22960a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 22970a708f8fSGustavo F. Padovan { 22980a708f8fSGustavo F. Padovan struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; 22990a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 230023691d75SGustavo F. Padovan struct l2cap_chan *chan = NULL, *pchan; 23010a708f8fSGustavo F. Padovan struct sock *parent, *sk = NULL; 23020a708f8fSGustavo F. Padovan int result, status = L2CAP_CS_NO_INFO; 23030a708f8fSGustavo F. Padovan 23040a708f8fSGustavo F. Padovan u16 dcid = 0, scid = __le16_to_cpu(req->scid); 23050a708f8fSGustavo F. Padovan __le16 psm = req->psm; 23060a708f8fSGustavo F. Padovan 23070a708f8fSGustavo F. Padovan BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid); 23080a708f8fSGustavo F. Padovan 23090a708f8fSGustavo F. Padovan /* Check if we have socket listening on psm */ 231023691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src); 231123691d75SGustavo F. Padovan if (!pchan) { 23120a708f8fSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 23130a708f8fSGustavo F. Padovan goto sendresp; 23140a708f8fSGustavo F. Padovan } 23150a708f8fSGustavo F. Padovan 231623691d75SGustavo F. Padovan parent = pchan->sk; 231723691d75SGustavo F. Padovan 23180a708f8fSGustavo F. Padovan bh_lock_sock(parent); 23190a708f8fSGustavo F. Padovan 23200a708f8fSGustavo F. Padovan /* Check if the ACL is secure enough (if not SDP) */ 23210a708f8fSGustavo F. Padovan if (psm != cpu_to_le16(0x0001) && 23220a708f8fSGustavo F. Padovan !hci_conn_check_link_mode(conn->hcon)) { 23230a708f8fSGustavo F. Padovan conn->disc_reason = 0x05; 23240a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 23250a708f8fSGustavo F. Padovan goto response; 23260a708f8fSGustavo F. Padovan } 23270a708f8fSGustavo F. Padovan 23280a708f8fSGustavo F. Padovan result = L2CAP_CR_NO_MEM; 23290a708f8fSGustavo F. Padovan 23300a708f8fSGustavo F. Padovan /* Check for backlog size */ 23310a708f8fSGustavo F. Padovan if (sk_acceptq_is_full(parent)) { 23320a708f8fSGustavo F. Padovan BT_DBG("backlog full %d", parent->sk_ack_backlog); 23330a708f8fSGustavo F. Padovan goto response; 23340a708f8fSGustavo F. Padovan } 23350a708f8fSGustavo F. Padovan 23360a708f8fSGustavo F. Padovan sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC); 23370a708f8fSGustavo F. Padovan if (!sk) 23380a708f8fSGustavo F. Padovan goto response; 23390a708f8fSGustavo F. Padovan 234023691d75SGustavo F. Padovan chan = l2cap_chan_create(sk); 234148454079SGustavo F. Padovan if (!chan) { 234248454079SGustavo F. Padovan l2cap_sock_kill(sk); 234348454079SGustavo F. Padovan goto response; 234448454079SGustavo F. Padovan } 234548454079SGustavo F. Padovan 23465d41ce1dSGustavo F. Padovan l2cap_pi(sk)->chan = chan; 23475d41ce1dSGustavo F. Padovan 2348baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 23490a708f8fSGustavo F. Padovan 23500a708f8fSGustavo F. Padovan /* Check if we already have channel with that dcid */ 2351baa7e1faSGustavo F. Padovan if (__l2cap_get_chan_by_dcid(conn, scid)) { 2352baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 23530a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 23540a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 23550a708f8fSGustavo F. Padovan goto response; 23560a708f8fSGustavo F. Padovan } 23570a708f8fSGustavo F. Padovan 23580a708f8fSGustavo F. Padovan hci_conn_hold(conn->hcon); 23590a708f8fSGustavo F. Padovan 23600a708f8fSGustavo F. Padovan l2cap_sock_init(sk, parent); 23610a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->src, conn->src); 23620a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->dst, conn->dst); 2363fe4128e0SGustavo F. Padovan chan->psm = psm; 2364fe4128e0SGustavo F. Padovan chan->dcid = scid; 23650a708f8fSGustavo F. Padovan 2366d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 2367d1010240SGustavo F. Padovan 236848454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 236948454079SGustavo F. Padovan 2370fe4128e0SGustavo F. Padovan dcid = chan->scid; 23710a708f8fSGustavo F. Padovan 2372*ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, sk->sk_sndtimeo); 23730a708f8fSGustavo F. Padovan 2374fc7f8a7eSGustavo F. Padovan chan->ident = cmd->ident; 23750a708f8fSGustavo F. Padovan 23760a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { 23774343478fSGustavo F. Padovan if (l2cap_check_security(chan)) { 23780a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 23790a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT2; 23800a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 23810a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHOR_PEND; 23820a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 23830a708f8fSGustavo F. Padovan } else { 23840a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 23850a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 23860a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 23870a708f8fSGustavo F. Padovan } 23880a708f8fSGustavo F. Padovan } else { 23890a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT2; 23900a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 23910a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHEN_PEND; 23920a708f8fSGustavo F. Padovan } 23930a708f8fSGustavo F. Padovan } else { 23940a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT2; 23950a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 23960a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 23970a708f8fSGustavo F. Padovan } 23980a708f8fSGustavo F. Padovan 2399baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 24000a708f8fSGustavo F. Padovan 24010a708f8fSGustavo F. Padovan response: 24020a708f8fSGustavo F. Padovan bh_unlock_sock(parent); 24030a708f8fSGustavo F. Padovan 24040a708f8fSGustavo F. Padovan sendresp: 24050a708f8fSGustavo F. Padovan rsp.scid = cpu_to_le16(scid); 24060a708f8fSGustavo F. Padovan rsp.dcid = cpu_to_le16(dcid); 24070a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 24080a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(status); 24090a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); 24100a708f8fSGustavo F. Padovan 24110a708f8fSGustavo F. Padovan if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { 24120a708f8fSGustavo F. Padovan struct l2cap_info_req info; 24130a708f8fSGustavo F. Padovan info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 24140a708f8fSGustavo F. Padovan 24150a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 24160a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 24170a708f8fSGustavo F. Padovan 24180a708f8fSGustavo F. Padovan mod_timer(&conn->info_timer, jiffies + 24190a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 24200a708f8fSGustavo F. Padovan 24210a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 24220a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(info), &info); 24230a708f8fSGustavo F. Padovan } 24240a708f8fSGustavo F. Padovan 2425b4450035SGustavo F. Padovan if (chan && !(chan->conf_state & L2CAP_CONF_REQ_SENT) && 24260a708f8fSGustavo F. Padovan result == L2CAP_CR_SUCCESS) { 24270a708f8fSGustavo F. Padovan u8 buf[128]; 2428b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 24290a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 243073ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 243173ffa904SGustavo F. Padovan chan->num_conf_req++; 24320a708f8fSGustavo F. Padovan } 24330a708f8fSGustavo F. Padovan 24340a708f8fSGustavo F. Padovan return 0; 24350a708f8fSGustavo F. Padovan } 24360a708f8fSGustavo F. Padovan 24370a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 24380a708f8fSGustavo F. Padovan { 24390a708f8fSGustavo F. Padovan struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; 24400a708f8fSGustavo F. Padovan u16 scid, dcid, result, status; 244148454079SGustavo F. Padovan struct l2cap_chan *chan; 24420a708f8fSGustavo F. Padovan struct sock *sk; 24430a708f8fSGustavo F. Padovan u8 req[128]; 24440a708f8fSGustavo F. Padovan 24450a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 24460a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 24470a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 24480a708f8fSGustavo F. Padovan status = __le16_to_cpu(rsp->status); 24490a708f8fSGustavo F. Padovan 24500a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status); 24510a708f8fSGustavo F. Padovan 24520a708f8fSGustavo F. Padovan if (scid) { 2453baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 245448454079SGustavo F. Padovan if (!chan) 24550a708f8fSGustavo F. Padovan return -EFAULT; 24560a708f8fSGustavo F. Padovan } else { 2457baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_ident(conn, cmd->ident); 245848454079SGustavo F. Padovan if (!chan) 24590a708f8fSGustavo F. Padovan return -EFAULT; 24600a708f8fSGustavo F. Padovan } 24610a708f8fSGustavo F. Padovan 246248454079SGustavo F. Padovan sk = chan->sk; 246348454079SGustavo F. Padovan 24640a708f8fSGustavo F. Padovan switch (result) { 24650a708f8fSGustavo F. Padovan case L2CAP_CR_SUCCESS: 24660a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 2467fc7f8a7eSGustavo F. Padovan chan->ident = 0; 2468fe4128e0SGustavo F. Padovan chan->dcid = dcid; 2469b4450035SGustavo F. Padovan chan->conf_state &= ~L2CAP_CONF_CONNECT_PEND; 24700a708f8fSGustavo F. Padovan 2471b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_REQ_SENT) 24720a708f8fSGustavo F. Padovan break; 24730a708f8fSGustavo F. Padovan 2474b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 24750a708f8fSGustavo F. Padovan 24760a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 247773ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, req), req); 247873ffa904SGustavo F. Padovan chan->num_conf_req++; 24790a708f8fSGustavo F. Padovan break; 24800a708f8fSGustavo F. Padovan 24810a708f8fSGustavo F. Padovan case L2CAP_CR_PEND: 2482b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 24830a708f8fSGustavo F. Padovan break; 24840a708f8fSGustavo F. Padovan 24850a708f8fSGustavo F. Padovan default: 24860a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 24870a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 24880a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 2489*ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 2490*ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 5); 24910a708f8fSGustavo F. Padovan break; 24920a708f8fSGustavo F. Padovan } 24930a708f8fSGustavo F. Padovan 249448454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNREFUSED); 24950a708f8fSGustavo F. Padovan break; 24960a708f8fSGustavo F. Padovan } 24970a708f8fSGustavo F. Padovan 24980a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 24990a708f8fSGustavo F. Padovan return 0; 25000a708f8fSGustavo F. Padovan } 25010a708f8fSGustavo F. Padovan 250247d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan) 25030a708f8fSGustavo F. Padovan { 250447d1ec61SGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(chan->sk); 250547d1ec61SGustavo F. Padovan 25060a708f8fSGustavo F. Padovan /* FCS is enabled only in ERTM or streaming mode, if one or both 25070a708f8fSGustavo F. Padovan * sides request it. 25080a708f8fSGustavo F. Padovan */ 25090c1bc5c6SGustavo F. Padovan if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) 251047d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2511b4450035SGustavo F. Padovan else if (!(pi->chan->conf_state & L2CAP_CONF_NO_FCS_RECV)) 251247d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_CRC16; 25130a708f8fSGustavo F. Padovan } 25140a708f8fSGustavo F. Padovan 25150a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 25160a708f8fSGustavo F. Padovan { 25170a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; 25180a708f8fSGustavo F. Padovan u16 dcid, flags; 25190a708f8fSGustavo F. Padovan u8 rsp[64]; 252048454079SGustavo F. Padovan struct l2cap_chan *chan; 25210a708f8fSGustavo F. Padovan struct sock *sk; 25220a708f8fSGustavo F. Padovan int len; 25230a708f8fSGustavo F. Padovan 25240a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 25250a708f8fSGustavo F. Padovan flags = __le16_to_cpu(req->flags); 25260a708f8fSGustavo F. Padovan 25270a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); 25280a708f8fSGustavo F. Padovan 2529baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 253048454079SGustavo F. Padovan if (!chan) 25310a708f8fSGustavo F. Padovan return -ENOENT; 25320a708f8fSGustavo F. Padovan 253348454079SGustavo F. Padovan sk = chan->sk; 253448454079SGustavo F. Padovan 25350a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONFIG) { 25360a708f8fSGustavo F. Padovan struct l2cap_cmd_rej rej; 25370a708f8fSGustavo F. Padovan 25380a708f8fSGustavo F. Padovan rej.reason = cpu_to_le16(0x0002); 25390a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, 25400a708f8fSGustavo F. Padovan sizeof(rej), &rej); 25410a708f8fSGustavo F. Padovan goto unlock; 25420a708f8fSGustavo F. Padovan } 25430a708f8fSGustavo F. Padovan 25440a708f8fSGustavo F. Padovan /* Reject if config buffer is too small. */ 25450a708f8fSGustavo F. Padovan len = cmd_len - sizeof(*req); 254673ffa904SGustavo F. Padovan if (chan->conf_len + len > sizeof(chan->conf_req)) { 25470a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2548fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 25490a708f8fSGustavo F. Padovan L2CAP_CONF_REJECT, flags), rsp); 25500a708f8fSGustavo F. Padovan goto unlock; 25510a708f8fSGustavo F. Padovan } 25520a708f8fSGustavo F. Padovan 25530a708f8fSGustavo F. Padovan /* Store config. */ 255473ffa904SGustavo F. Padovan memcpy(chan->conf_req + chan->conf_len, req->data, len); 255573ffa904SGustavo F. Padovan chan->conf_len += len; 25560a708f8fSGustavo F. Padovan 25570a708f8fSGustavo F. Padovan if (flags & 0x0001) { 25580a708f8fSGustavo F. Padovan /* Incomplete config. Send empty response. */ 25590a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2560fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 25610a708f8fSGustavo F. Padovan L2CAP_CONF_SUCCESS, 0x0001), rsp); 25620a708f8fSGustavo F. Padovan goto unlock; 25630a708f8fSGustavo F. Padovan } 25640a708f8fSGustavo F. Padovan 25650a708f8fSGustavo F. Padovan /* Complete config. */ 256673ffa904SGustavo F. Padovan len = l2cap_parse_conf_req(chan, rsp); 25670a708f8fSGustavo F. Padovan if (len < 0) { 2568e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 25690a708f8fSGustavo F. Padovan goto unlock; 25700a708f8fSGustavo F. Padovan } 25710a708f8fSGustavo F. Padovan 25720a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); 257373ffa904SGustavo F. Padovan chan->num_conf_rsp++; 25740a708f8fSGustavo F. Padovan 25750a708f8fSGustavo F. Padovan /* Reset config buffer. */ 257673ffa904SGustavo F. Padovan chan->conf_len = 0; 25770a708f8fSGustavo F. Padovan 2578b4450035SGustavo F. Padovan if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE)) 25790a708f8fSGustavo F. Padovan goto unlock; 25800a708f8fSGustavo F. Padovan 2581b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_INPUT_DONE) { 258247d1ec61SGustavo F. Padovan set_default_fcs(chan); 25830a708f8fSGustavo F. Padovan 25840a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 25850a708f8fSGustavo F. Padovan 258642e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 258742e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 258858d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 25890c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2590525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 25910a708f8fSGustavo F. Padovan 25920a708f8fSGustavo F. Padovan l2cap_chan_ready(sk); 25930a708f8fSGustavo F. Padovan goto unlock; 25940a708f8fSGustavo F. Padovan } 25950a708f8fSGustavo F. Padovan 2596b4450035SGustavo F. Padovan if (!(chan->conf_state & L2CAP_CONF_REQ_SENT)) { 25970a708f8fSGustavo F. Padovan u8 buf[64]; 2598b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 25990a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 260073ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 260173ffa904SGustavo F. Padovan chan->num_conf_req++; 26020a708f8fSGustavo F. Padovan } 26030a708f8fSGustavo F. Padovan 26040a708f8fSGustavo F. Padovan unlock: 26050a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 26060a708f8fSGustavo F. Padovan return 0; 26070a708f8fSGustavo F. Padovan } 26080a708f8fSGustavo F. Padovan 26090a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 26100a708f8fSGustavo F. Padovan { 26110a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; 26120a708f8fSGustavo F. Padovan u16 scid, flags, result; 261348454079SGustavo F. Padovan struct l2cap_chan *chan; 26140a708f8fSGustavo F. Padovan struct sock *sk; 26150a708f8fSGustavo F. Padovan int len = cmd->len - sizeof(*rsp); 26160a708f8fSGustavo F. Padovan 26170a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 26180a708f8fSGustavo F. Padovan flags = __le16_to_cpu(rsp->flags); 26190a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 26200a708f8fSGustavo F. Padovan 26210a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", 26220a708f8fSGustavo F. Padovan scid, flags, result); 26230a708f8fSGustavo F. Padovan 2624baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 262548454079SGustavo F. Padovan if (!chan) 26260a708f8fSGustavo F. Padovan return 0; 26270a708f8fSGustavo F. Padovan 262848454079SGustavo F. Padovan sk = chan->sk; 262948454079SGustavo F. Padovan 26300a708f8fSGustavo F. Padovan switch (result) { 26310a708f8fSGustavo F. Padovan case L2CAP_CONF_SUCCESS: 263247d1ec61SGustavo F. Padovan l2cap_conf_rfc_get(chan, rsp->data, len); 26330a708f8fSGustavo F. Padovan break; 26340a708f8fSGustavo F. Padovan 26350a708f8fSGustavo F. Padovan case L2CAP_CONF_UNACCEPT: 263673ffa904SGustavo F. Padovan if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { 26370a708f8fSGustavo F. Padovan char req[64]; 26380a708f8fSGustavo F. Padovan 26390a708f8fSGustavo F. Padovan if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { 2640e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 26410a708f8fSGustavo F. Padovan goto done; 26420a708f8fSGustavo F. Padovan } 26430a708f8fSGustavo F. Padovan 26440a708f8fSGustavo F. Padovan /* throw out any old stored conf requests */ 26450a708f8fSGustavo F. Padovan result = L2CAP_CONF_SUCCESS; 2646b4450035SGustavo F. Padovan len = l2cap_parse_conf_rsp(chan, rsp->data, len, 2647b4450035SGustavo F. Padovan req, &result); 26480a708f8fSGustavo F. Padovan if (len < 0) { 2649e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 26500a708f8fSGustavo F. Padovan goto done; 26510a708f8fSGustavo F. Padovan } 26520a708f8fSGustavo F. Padovan 26530a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 26540a708f8fSGustavo F. Padovan L2CAP_CONF_REQ, len, req); 265573ffa904SGustavo F. Padovan chan->num_conf_req++; 26560a708f8fSGustavo F. Padovan if (result != L2CAP_CONF_SUCCESS) 26570a708f8fSGustavo F. Padovan goto done; 26580a708f8fSGustavo F. Padovan break; 26590a708f8fSGustavo F. Padovan } 26600a708f8fSGustavo F. Padovan 26610a708f8fSGustavo F. Padovan default: 26620a708f8fSGustavo F. Padovan sk->sk_err = ECONNRESET; 2663*ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ * 5); 2664e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 26650a708f8fSGustavo F. Padovan goto done; 26660a708f8fSGustavo F. Padovan } 26670a708f8fSGustavo F. Padovan 26680a708f8fSGustavo F. Padovan if (flags & 0x01) 26690a708f8fSGustavo F. Padovan goto done; 26700a708f8fSGustavo F. Padovan 2671b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_INPUT_DONE; 26720a708f8fSGustavo F. Padovan 2673b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_OUTPUT_DONE) { 267447d1ec61SGustavo F. Padovan set_default_fcs(chan); 26750a708f8fSGustavo F. Padovan 26760a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 267742e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 267842e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 267958d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 26800c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2681525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 26820a708f8fSGustavo F. Padovan 26830a708f8fSGustavo F. Padovan l2cap_chan_ready(sk); 26840a708f8fSGustavo F. Padovan } 26850a708f8fSGustavo F. Padovan 26860a708f8fSGustavo F. Padovan done: 26870a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 26880a708f8fSGustavo F. Padovan return 0; 26890a708f8fSGustavo F. Padovan } 26900a708f8fSGustavo F. Padovan 26910a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 26920a708f8fSGustavo F. Padovan { 26930a708f8fSGustavo F. Padovan struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; 26940a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp rsp; 26950a708f8fSGustavo F. Padovan u16 dcid, scid; 269648454079SGustavo F. Padovan struct l2cap_chan *chan; 26970a708f8fSGustavo F. Padovan struct sock *sk; 26980a708f8fSGustavo F. Padovan 26990a708f8fSGustavo F. Padovan scid = __le16_to_cpu(req->scid); 27000a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 27010a708f8fSGustavo F. Padovan 27020a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); 27030a708f8fSGustavo F. Padovan 2704baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 270548454079SGustavo F. Padovan if (!chan) 27060a708f8fSGustavo F. Padovan return 0; 27070a708f8fSGustavo F. Padovan 270848454079SGustavo F. Padovan sk = chan->sk; 270948454079SGustavo F. Padovan 2710fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 2711fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 27120a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); 27130a708f8fSGustavo F. Padovan 27140a708f8fSGustavo F. Padovan sk->sk_shutdown = SHUTDOWN_MASK; 27150a708f8fSGustavo F. Padovan 27160a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 27170a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 27180a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 2719*ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 2720*ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 5); 27210a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27220a708f8fSGustavo F. Padovan return 0; 27230a708f8fSGustavo F. Padovan } 27240a708f8fSGustavo F. Padovan 272548454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNRESET); 27260a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27270a708f8fSGustavo F. Padovan 27280a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 27290a708f8fSGustavo F. Padovan return 0; 27300a708f8fSGustavo F. Padovan } 27310a708f8fSGustavo F. Padovan 27320a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 27330a708f8fSGustavo F. Padovan { 27340a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; 27350a708f8fSGustavo F. Padovan u16 dcid, scid; 273648454079SGustavo F. Padovan struct l2cap_chan *chan; 27370a708f8fSGustavo F. Padovan struct sock *sk; 27380a708f8fSGustavo F. Padovan 27390a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 27400a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 27410a708f8fSGustavo F. Padovan 27420a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); 27430a708f8fSGustavo F. Padovan 2744baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 274548454079SGustavo F. Padovan if (!chan) 27460a708f8fSGustavo F. Padovan return 0; 27470a708f8fSGustavo F. Padovan 274848454079SGustavo F. Padovan sk = chan->sk; 274948454079SGustavo F. Padovan 27500a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 27510a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 27520a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 2753*ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 2754*ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 5); 27550a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27560a708f8fSGustavo F. Padovan return 0; 27570a708f8fSGustavo F. Padovan } 27580a708f8fSGustavo F. Padovan 275948454079SGustavo F. Padovan l2cap_chan_del(chan, 0); 27600a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 27610a708f8fSGustavo F. Padovan 27620a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 27630a708f8fSGustavo F. Padovan return 0; 27640a708f8fSGustavo F. Padovan } 27650a708f8fSGustavo F. Padovan 27660a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 27670a708f8fSGustavo F. Padovan { 27680a708f8fSGustavo F. Padovan struct l2cap_info_req *req = (struct l2cap_info_req *) data; 27690a708f8fSGustavo F. Padovan u16 type; 27700a708f8fSGustavo F. Padovan 27710a708f8fSGustavo F. Padovan type = __le16_to_cpu(req->type); 27720a708f8fSGustavo F. Padovan 27730a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x", type); 27740a708f8fSGustavo F. Padovan 27750a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 27760a708f8fSGustavo F. Padovan u8 buf[8]; 27770a708f8fSGustavo F. Padovan u32 feat_mask = l2cap_feat_mask; 27780a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 27790a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 27800a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 27810a708f8fSGustavo F. Padovan if (!disable_ertm) 27820a708f8fSGustavo F. Padovan feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING 27830a708f8fSGustavo F. Padovan | L2CAP_FEAT_FCS; 27840a708f8fSGustavo F. Padovan put_unaligned_le32(feat_mask, rsp->data); 27850a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 27860a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 27870a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 27880a708f8fSGustavo F. Padovan u8 buf[12]; 27890a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 27900a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 27910a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 27920a708f8fSGustavo F. Padovan memcpy(buf + 4, l2cap_fixed_chan, 8); 27930a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 27940a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 27950a708f8fSGustavo F. Padovan } else { 27960a708f8fSGustavo F. Padovan struct l2cap_info_rsp rsp; 27970a708f8fSGustavo F. Padovan rsp.type = cpu_to_le16(type); 27980a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); 27990a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 28000a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(rsp), &rsp); 28010a708f8fSGustavo F. Padovan } 28020a708f8fSGustavo F. Padovan 28030a708f8fSGustavo F. Padovan return 0; 28040a708f8fSGustavo F. Padovan } 28050a708f8fSGustavo F. Padovan 28060a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 28070a708f8fSGustavo F. Padovan { 28080a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; 28090a708f8fSGustavo F. Padovan u16 type, result; 28100a708f8fSGustavo F. Padovan 28110a708f8fSGustavo F. Padovan type = __le16_to_cpu(rsp->type); 28120a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 28130a708f8fSGustavo F. Padovan 28140a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); 28150a708f8fSGustavo F. Padovan 2816e90165beSAndrei Emeltchenko /* L2CAP Info req/rsp are unbound to channels, add extra checks */ 2817e90165beSAndrei Emeltchenko if (cmd->ident != conn->info_ident || 2818e90165beSAndrei Emeltchenko conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) 2819e90165beSAndrei Emeltchenko return 0; 2820e90165beSAndrei Emeltchenko 28210a708f8fSGustavo F. Padovan del_timer(&conn->info_timer); 28220a708f8fSGustavo F. Padovan 28230a708f8fSGustavo F. Padovan if (result != L2CAP_IR_SUCCESS) { 28240a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 28250a708f8fSGustavo F. Padovan conn->info_ident = 0; 28260a708f8fSGustavo F. Padovan 28270a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 28280a708f8fSGustavo F. Padovan 28290a708f8fSGustavo F. Padovan return 0; 28300a708f8fSGustavo F. Padovan } 28310a708f8fSGustavo F. Padovan 28320a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 28330a708f8fSGustavo F. Padovan conn->feat_mask = get_unaligned_le32(rsp->data); 28340a708f8fSGustavo F. Padovan 28350a708f8fSGustavo F. Padovan if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { 28360a708f8fSGustavo F. Padovan struct l2cap_info_req req; 28370a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 28380a708f8fSGustavo F. Padovan 28390a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 28400a708f8fSGustavo F. Padovan 28410a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 28420a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 28430a708f8fSGustavo F. Padovan } else { 28440a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 28450a708f8fSGustavo F. Padovan conn->info_ident = 0; 28460a708f8fSGustavo F. Padovan 28470a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 28480a708f8fSGustavo F. Padovan } 28490a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 28500a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 28510a708f8fSGustavo F. Padovan conn->info_ident = 0; 28520a708f8fSGustavo F. Padovan 28530a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 28540a708f8fSGustavo F. Padovan } 28550a708f8fSGustavo F. Padovan 28560a708f8fSGustavo F. Padovan return 0; 28570a708f8fSGustavo F. Padovan } 28580a708f8fSGustavo F. Padovan 2859e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency, 2860de73115aSClaudio Takahasi u16 to_multiplier) 2861de73115aSClaudio Takahasi { 2862de73115aSClaudio Takahasi u16 max_latency; 2863de73115aSClaudio Takahasi 2864de73115aSClaudio Takahasi if (min > max || min < 6 || max > 3200) 2865de73115aSClaudio Takahasi return -EINVAL; 2866de73115aSClaudio Takahasi 2867de73115aSClaudio Takahasi if (to_multiplier < 10 || to_multiplier > 3200) 2868de73115aSClaudio Takahasi return -EINVAL; 2869de73115aSClaudio Takahasi 2870de73115aSClaudio Takahasi if (max >= to_multiplier * 8) 2871de73115aSClaudio Takahasi return -EINVAL; 2872de73115aSClaudio Takahasi 2873de73115aSClaudio Takahasi max_latency = (to_multiplier * 8 / max) - 1; 2874de73115aSClaudio Takahasi if (latency > 499 || latency > max_latency) 2875de73115aSClaudio Takahasi return -EINVAL; 2876de73115aSClaudio Takahasi 2877de73115aSClaudio Takahasi return 0; 2878de73115aSClaudio Takahasi } 2879de73115aSClaudio Takahasi 2880de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, 2881de73115aSClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 2882de73115aSClaudio Takahasi { 2883de73115aSClaudio Takahasi struct hci_conn *hcon = conn->hcon; 2884de73115aSClaudio Takahasi struct l2cap_conn_param_update_req *req; 2885de73115aSClaudio Takahasi struct l2cap_conn_param_update_rsp rsp; 2886de73115aSClaudio Takahasi u16 min, max, latency, to_multiplier, cmd_len; 28872ce603ebSClaudio Takahasi int err; 2888de73115aSClaudio Takahasi 2889de73115aSClaudio Takahasi if (!(hcon->link_mode & HCI_LM_MASTER)) 2890de73115aSClaudio Takahasi return -EINVAL; 2891de73115aSClaudio Takahasi 2892de73115aSClaudio Takahasi cmd_len = __le16_to_cpu(cmd->len); 2893de73115aSClaudio Takahasi if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) 2894de73115aSClaudio Takahasi return -EPROTO; 2895de73115aSClaudio Takahasi 2896de73115aSClaudio Takahasi req = (struct l2cap_conn_param_update_req *) data; 2897de73115aSClaudio Takahasi min = __le16_to_cpu(req->min); 2898de73115aSClaudio Takahasi max = __le16_to_cpu(req->max); 2899de73115aSClaudio Takahasi latency = __le16_to_cpu(req->latency); 2900de73115aSClaudio Takahasi to_multiplier = __le16_to_cpu(req->to_multiplier); 2901de73115aSClaudio Takahasi 2902de73115aSClaudio Takahasi BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", 2903de73115aSClaudio Takahasi min, max, latency, to_multiplier); 2904de73115aSClaudio Takahasi 2905de73115aSClaudio Takahasi memset(&rsp, 0, sizeof(rsp)); 29062ce603ebSClaudio Takahasi 29072ce603ebSClaudio Takahasi err = l2cap_check_conn_param(min, max, latency, to_multiplier); 29082ce603ebSClaudio Takahasi if (err) 2909de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); 2910de73115aSClaudio Takahasi else 2911de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); 2912de73115aSClaudio Takahasi 2913de73115aSClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, 2914de73115aSClaudio Takahasi sizeof(rsp), &rsp); 2915de73115aSClaudio Takahasi 29162ce603ebSClaudio Takahasi if (!err) 29172ce603ebSClaudio Takahasi hci_le_conn_update(hcon, min, max, latency, to_multiplier); 29182ce603ebSClaudio Takahasi 2919de73115aSClaudio Takahasi return 0; 2920de73115aSClaudio Takahasi } 2921de73115aSClaudio Takahasi 29223300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, 29233300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 29243300d9a9SClaudio Takahasi { 29253300d9a9SClaudio Takahasi int err = 0; 29263300d9a9SClaudio Takahasi 29273300d9a9SClaudio Takahasi switch (cmd->code) { 29283300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 29293300d9a9SClaudio Takahasi l2cap_command_rej(conn, cmd, data); 29303300d9a9SClaudio Takahasi break; 29313300d9a9SClaudio Takahasi 29323300d9a9SClaudio Takahasi case L2CAP_CONN_REQ: 29333300d9a9SClaudio Takahasi err = l2cap_connect_req(conn, cmd, data); 29343300d9a9SClaudio Takahasi break; 29353300d9a9SClaudio Takahasi 29363300d9a9SClaudio Takahasi case L2CAP_CONN_RSP: 29373300d9a9SClaudio Takahasi err = l2cap_connect_rsp(conn, cmd, data); 29383300d9a9SClaudio Takahasi break; 29393300d9a9SClaudio Takahasi 29403300d9a9SClaudio Takahasi case L2CAP_CONF_REQ: 29413300d9a9SClaudio Takahasi err = l2cap_config_req(conn, cmd, cmd_len, data); 29423300d9a9SClaudio Takahasi break; 29433300d9a9SClaudio Takahasi 29443300d9a9SClaudio Takahasi case L2CAP_CONF_RSP: 29453300d9a9SClaudio Takahasi err = l2cap_config_rsp(conn, cmd, data); 29463300d9a9SClaudio Takahasi break; 29473300d9a9SClaudio Takahasi 29483300d9a9SClaudio Takahasi case L2CAP_DISCONN_REQ: 29493300d9a9SClaudio Takahasi err = l2cap_disconnect_req(conn, cmd, data); 29503300d9a9SClaudio Takahasi break; 29513300d9a9SClaudio Takahasi 29523300d9a9SClaudio Takahasi case L2CAP_DISCONN_RSP: 29533300d9a9SClaudio Takahasi err = l2cap_disconnect_rsp(conn, cmd, data); 29543300d9a9SClaudio Takahasi break; 29553300d9a9SClaudio Takahasi 29563300d9a9SClaudio Takahasi case L2CAP_ECHO_REQ: 29573300d9a9SClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); 29583300d9a9SClaudio Takahasi break; 29593300d9a9SClaudio Takahasi 29603300d9a9SClaudio Takahasi case L2CAP_ECHO_RSP: 29613300d9a9SClaudio Takahasi break; 29623300d9a9SClaudio Takahasi 29633300d9a9SClaudio Takahasi case L2CAP_INFO_REQ: 29643300d9a9SClaudio Takahasi err = l2cap_information_req(conn, cmd, data); 29653300d9a9SClaudio Takahasi break; 29663300d9a9SClaudio Takahasi 29673300d9a9SClaudio Takahasi case L2CAP_INFO_RSP: 29683300d9a9SClaudio Takahasi err = l2cap_information_rsp(conn, cmd, data); 29693300d9a9SClaudio Takahasi break; 29703300d9a9SClaudio Takahasi 29713300d9a9SClaudio Takahasi default: 29723300d9a9SClaudio Takahasi BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); 29733300d9a9SClaudio Takahasi err = -EINVAL; 29743300d9a9SClaudio Takahasi break; 29753300d9a9SClaudio Takahasi } 29763300d9a9SClaudio Takahasi 29773300d9a9SClaudio Takahasi return err; 29783300d9a9SClaudio Takahasi } 29793300d9a9SClaudio Takahasi 29803300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, 29813300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 29823300d9a9SClaudio Takahasi { 29833300d9a9SClaudio Takahasi switch (cmd->code) { 29843300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 29853300d9a9SClaudio Takahasi return 0; 29863300d9a9SClaudio Takahasi 29873300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_REQ: 2988de73115aSClaudio Takahasi return l2cap_conn_param_update_req(conn, cmd, data); 29893300d9a9SClaudio Takahasi 29903300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_RSP: 29913300d9a9SClaudio Takahasi return 0; 29923300d9a9SClaudio Takahasi 29933300d9a9SClaudio Takahasi default: 29943300d9a9SClaudio Takahasi BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); 29953300d9a9SClaudio Takahasi return -EINVAL; 29963300d9a9SClaudio Takahasi } 29973300d9a9SClaudio Takahasi } 29983300d9a9SClaudio Takahasi 29993300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn, 30003300d9a9SClaudio Takahasi struct sk_buff *skb) 30010a708f8fSGustavo F. Padovan { 30020a708f8fSGustavo F. Padovan u8 *data = skb->data; 30030a708f8fSGustavo F. Padovan int len = skb->len; 30040a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr cmd; 30053300d9a9SClaudio Takahasi int err; 30060a708f8fSGustavo F. Padovan 30070a708f8fSGustavo F. Padovan l2cap_raw_recv(conn, skb); 30080a708f8fSGustavo F. Padovan 30090a708f8fSGustavo F. Padovan while (len >= L2CAP_CMD_HDR_SIZE) { 30100a708f8fSGustavo F. Padovan u16 cmd_len; 30110a708f8fSGustavo F. Padovan memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); 30120a708f8fSGustavo F. Padovan data += L2CAP_CMD_HDR_SIZE; 30130a708f8fSGustavo F. Padovan len -= L2CAP_CMD_HDR_SIZE; 30140a708f8fSGustavo F. Padovan 30150a708f8fSGustavo F. Padovan cmd_len = le16_to_cpu(cmd.len); 30160a708f8fSGustavo F. Padovan 30170a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident); 30180a708f8fSGustavo F. Padovan 30190a708f8fSGustavo F. Padovan if (cmd_len > len || !cmd.ident) { 30200a708f8fSGustavo F. Padovan BT_DBG("corrupted command"); 30210a708f8fSGustavo F. Padovan break; 30220a708f8fSGustavo F. Padovan } 30230a708f8fSGustavo F. Padovan 30243300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 30253300d9a9SClaudio Takahasi err = l2cap_le_sig_cmd(conn, &cmd, data); 30263300d9a9SClaudio Takahasi else 30273300d9a9SClaudio Takahasi err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); 30280a708f8fSGustavo F. Padovan 30290a708f8fSGustavo F. Padovan if (err) { 30300a708f8fSGustavo F. Padovan struct l2cap_cmd_rej rej; 30312c6d1a2eSGustavo F. Padovan 30322c6d1a2eSGustavo F. Padovan BT_ERR("Wrong link type (%d)", err); 30330a708f8fSGustavo F. Padovan 30340a708f8fSGustavo F. Padovan /* FIXME: Map err to a valid reason */ 30350a708f8fSGustavo F. Padovan rej.reason = cpu_to_le16(0); 30360a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); 30370a708f8fSGustavo F. Padovan } 30380a708f8fSGustavo F. Padovan 30390a708f8fSGustavo F. Padovan data += cmd_len; 30400a708f8fSGustavo F. Padovan len -= cmd_len; 30410a708f8fSGustavo F. Padovan } 30420a708f8fSGustavo F. Padovan 30430a708f8fSGustavo F. Padovan kfree_skb(skb); 30440a708f8fSGustavo F. Padovan } 30450a708f8fSGustavo F. Padovan 304647d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb) 30470a708f8fSGustavo F. Padovan { 30480a708f8fSGustavo F. Padovan u16 our_fcs, rcv_fcs; 30490a708f8fSGustavo F. Padovan int hdr_size = L2CAP_HDR_SIZE + 2; 30500a708f8fSGustavo F. Padovan 305147d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 30520a708f8fSGustavo F. Padovan skb_trim(skb, skb->len - 2); 30530a708f8fSGustavo F. Padovan rcv_fcs = get_unaligned_le16(skb->data + skb->len); 30540a708f8fSGustavo F. Padovan our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); 30550a708f8fSGustavo F. Padovan 30560a708f8fSGustavo F. Padovan if (our_fcs != rcv_fcs) 30570a708f8fSGustavo F. Padovan return -EBADMSG; 30580a708f8fSGustavo F. Padovan } 30590a708f8fSGustavo F. Padovan return 0; 30600a708f8fSGustavo F. Padovan } 30610a708f8fSGustavo F. Padovan 3062525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan) 30630a708f8fSGustavo F. Padovan { 30640a708f8fSGustavo F. Padovan u16 control = 0; 30650a708f8fSGustavo F. Padovan 30666a026610SGustavo F. Padovan chan->frames_sent = 0; 30670a708f8fSGustavo F. Padovan 306842e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 30690a708f8fSGustavo F. Padovan 3070525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 30710a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 3072525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 3073525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 30740a708f8fSGustavo F. Padovan } 30750a708f8fSGustavo F. Padovan 3076525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REMOTE_BUSY) 3077525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 30780a708f8fSGustavo F. Padovan 3079525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 30800a708f8fSGustavo F. Padovan 3081525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_LOCAL_BUSY) && 30826a026610SGustavo F. Padovan chan->frames_sent == 0) { 30830a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 3084525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 30850a708f8fSGustavo F. Padovan } 30860a708f8fSGustavo F. Padovan } 30870a708f8fSGustavo F. Padovan 308842e5c802SGustavo F. Padovan static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u8 tx_seq, u8 sar) 30890a708f8fSGustavo F. Padovan { 30900a708f8fSGustavo F. Padovan struct sk_buff *next_skb; 30910a708f8fSGustavo F. Padovan int tx_seq_offset, next_tx_seq_offset; 30920a708f8fSGustavo F. Padovan 30930a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 30940a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 30950a708f8fSGustavo F. Padovan 3096f1c6775bSGustavo F. Padovan next_skb = skb_peek(&chan->srej_q); 30970a708f8fSGustavo F. Padovan if (!next_skb) { 3098f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 30990a708f8fSGustavo F. Padovan return 0; 31000a708f8fSGustavo F. Padovan } 31010a708f8fSGustavo F. Padovan 310242e5c802SGustavo F. Padovan tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; 31030a708f8fSGustavo F. Padovan if (tx_seq_offset < 0) 31040a708f8fSGustavo F. Padovan tx_seq_offset += 64; 31050a708f8fSGustavo F. Padovan 31060a708f8fSGustavo F. Padovan do { 31070a708f8fSGustavo F. Padovan if (bt_cb(next_skb)->tx_seq == tx_seq) 31080a708f8fSGustavo F. Padovan return -EINVAL; 31090a708f8fSGustavo F. Padovan 31100a708f8fSGustavo F. Padovan next_tx_seq_offset = (bt_cb(next_skb)->tx_seq - 311142e5c802SGustavo F. Padovan chan->buffer_seq) % 64; 31120a708f8fSGustavo F. Padovan if (next_tx_seq_offset < 0) 31130a708f8fSGustavo F. Padovan next_tx_seq_offset += 64; 31140a708f8fSGustavo F. Padovan 31150a708f8fSGustavo F. Padovan if (next_tx_seq_offset > tx_seq_offset) { 3116f1c6775bSGustavo F. Padovan __skb_queue_before(&chan->srej_q, next_skb, skb); 31170a708f8fSGustavo F. Padovan return 0; 31180a708f8fSGustavo F. Padovan } 31190a708f8fSGustavo F. Padovan 3120f1c6775bSGustavo F. Padovan if (skb_queue_is_last(&chan->srej_q, next_skb)) 31210a708f8fSGustavo F. Padovan break; 31220a708f8fSGustavo F. Padovan 3123f1c6775bSGustavo F. Padovan } while ((next_skb = skb_queue_next(&chan->srej_q, next_skb))); 31240a708f8fSGustavo F. Padovan 3125f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 31260a708f8fSGustavo F. Padovan 31270a708f8fSGustavo F. Padovan return 0; 31280a708f8fSGustavo F. Padovan } 31290a708f8fSGustavo F. Padovan 3130525cd185SGustavo F. Padovan static int l2cap_ertm_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 31310a708f8fSGustavo F. Padovan { 31320a708f8fSGustavo F. Padovan struct sk_buff *_skb; 31330a708f8fSGustavo F. Padovan int err; 31340a708f8fSGustavo F. Padovan 31350a708f8fSGustavo F. Padovan switch (control & L2CAP_CTRL_SAR) { 31360a708f8fSGustavo F. Padovan case L2CAP_SDU_UNSEGMENTED: 3137525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) 31380a708f8fSGustavo F. Padovan goto drop; 31390a708f8fSGustavo F. Padovan 3140224f8af0SRuiyi Zhang return sock_queue_rcv_skb(chan->sk, skb); 31410a708f8fSGustavo F. Padovan 31420a708f8fSGustavo F. Padovan case L2CAP_SDU_START: 3143525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) 31440a708f8fSGustavo F. Padovan goto drop; 31450a708f8fSGustavo F. Padovan 31466f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 31470a708f8fSGustavo F. Padovan 31480c1bc5c6SGustavo F. Padovan if (chan->sdu_len > chan->imtu) 31490a708f8fSGustavo F. Padovan goto disconnect; 31500a708f8fSGustavo F. Padovan 31516f61fd47SGustavo F. Padovan chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC); 31526f61fd47SGustavo F. Padovan if (!chan->sdu) 31530a708f8fSGustavo F. Padovan return -ENOMEM; 31540a708f8fSGustavo F. Padovan 31550a708f8fSGustavo F. Padovan /* pull sdu_len bytes only after alloc, because of Local Busy 31560a708f8fSGustavo F. Padovan * condition we have to be sure that this will be executed 31570a708f8fSGustavo F. Padovan * only once, i.e., when alloc does not fail */ 31580a708f8fSGustavo F. Padovan skb_pull(skb, 2); 31590a708f8fSGustavo F. Padovan 31606f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31610a708f8fSGustavo F. Padovan 3162525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_SDU; 31636f61fd47SGustavo F. Padovan chan->partial_sdu_len = skb->len; 31640a708f8fSGustavo F. Padovan break; 31650a708f8fSGustavo F. Padovan 31660a708f8fSGustavo F. Padovan case L2CAP_SDU_CONTINUE: 3167525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 31680a708f8fSGustavo F. Padovan goto disconnect; 31690a708f8fSGustavo F. Padovan 31706f61fd47SGustavo F. Padovan if (!chan->sdu) 31710a708f8fSGustavo F. Padovan goto disconnect; 31720a708f8fSGustavo F. Padovan 31736f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 31746f61fd47SGustavo F. Padovan if (chan->partial_sdu_len > chan->sdu_len) 31750a708f8fSGustavo F. Padovan goto drop; 31760a708f8fSGustavo F. Padovan 31776f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31780a708f8fSGustavo F. Padovan 31790a708f8fSGustavo F. Padovan break; 31800a708f8fSGustavo F. Padovan 31810a708f8fSGustavo F. Padovan case L2CAP_SDU_END: 3182525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 31830a708f8fSGustavo F. Padovan goto disconnect; 31840a708f8fSGustavo F. Padovan 31856f61fd47SGustavo F. Padovan if (!chan->sdu) 31860a708f8fSGustavo F. Padovan goto disconnect; 31870a708f8fSGustavo F. Padovan 3188525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_RETRY)) { 31896f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 31900a708f8fSGustavo F. Padovan 31910c1bc5c6SGustavo F. Padovan if (chan->partial_sdu_len > chan->imtu) 31920a708f8fSGustavo F. Padovan goto drop; 31930a708f8fSGustavo F. Padovan 31946f61fd47SGustavo F. Padovan if (chan->partial_sdu_len != chan->sdu_len) 31950a708f8fSGustavo F. Padovan goto drop; 31960a708f8fSGustavo F. Padovan 31976f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31980a708f8fSGustavo F. Padovan } 31990a708f8fSGustavo F. Padovan 32006f61fd47SGustavo F. Padovan _skb = skb_clone(chan->sdu, GFP_ATOMIC); 32010a708f8fSGustavo F. Padovan if (!_skb) { 3202525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_RETRY; 32030a708f8fSGustavo F. Padovan return -ENOMEM; 32040a708f8fSGustavo F. Padovan } 32050a708f8fSGustavo F. Padovan 3206525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, _skb); 32070a708f8fSGustavo F. Padovan if (err < 0) { 32080a708f8fSGustavo F. Padovan kfree_skb(_skb); 3209525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_RETRY; 32100a708f8fSGustavo F. Padovan return err; 32110a708f8fSGustavo F. Padovan } 32120a708f8fSGustavo F. Padovan 3213525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_RETRY; 3214525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_SDU; 32150a708f8fSGustavo F. Padovan 32166f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 32170a708f8fSGustavo F. Padovan break; 32180a708f8fSGustavo F. Padovan } 32190a708f8fSGustavo F. Padovan 32200a708f8fSGustavo F. Padovan kfree_skb(skb); 32210a708f8fSGustavo F. Padovan return 0; 32220a708f8fSGustavo F. Padovan 32230a708f8fSGustavo F. Padovan drop: 32246f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 32256f61fd47SGustavo F. Padovan chan->sdu = NULL; 32260a708f8fSGustavo F. Padovan 32270a708f8fSGustavo F. Padovan disconnect: 32288c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 32290a708f8fSGustavo F. Padovan kfree_skb(skb); 32300a708f8fSGustavo F. Padovan return 0; 32310a708f8fSGustavo F. Padovan } 32320a708f8fSGustavo F. Padovan 3233525cd185SGustavo F. Padovan static int l2cap_try_push_rx_skb(struct l2cap_chan *chan) 32340a708f8fSGustavo F. Padovan { 32350a708f8fSGustavo F. Padovan struct sk_buff *skb; 32360a708f8fSGustavo F. Padovan u16 control; 32370a708f8fSGustavo F. Padovan int err; 32380a708f8fSGustavo F. Padovan 3239f1c6775bSGustavo F. Padovan while ((skb = skb_dequeue(&chan->busy_q))) { 32400a708f8fSGustavo F. Padovan control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT; 3241525cd185SGustavo F. Padovan err = l2cap_ertm_reassembly_sdu(chan, skb, control); 32420a708f8fSGustavo F. Padovan if (err < 0) { 3243f1c6775bSGustavo F. Padovan skb_queue_head(&chan->busy_q, skb); 32440a708f8fSGustavo F. Padovan return -EBUSY; 32450a708f8fSGustavo F. Padovan } 32460a708f8fSGustavo F. Padovan 324742e5c802SGustavo F. Padovan chan->buffer_seq = (chan->buffer_seq + 1) % 64; 32480a708f8fSGustavo F. Padovan } 32490a708f8fSGustavo F. Padovan 3250525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_RNR_SENT)) 32510a708f8fSGustavo F. Padovan goto done; 32520a708f8fSGustavo F. Padovan 325342e5c802SGustavo F. Padovan control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 32540a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY | L2CAP_CTRL_POLL; 3255525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 32566a026610SGustavo F. Padovan chan->retry_count = 1; 32570a708f8fSGustavo F. Padovan 3258e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 32590a708f8fSGustavo F. Padovan __mod_monitor_timer(); 32600a708f8fSGustavo F. Padovan 3261525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_WAIT_F; 32620a708f8fSGustavo F. Padovan 32630a708f8fSGustavo F. Padovan done: 3264525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_LOCAL_BUSY; 3265525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_RNR_SENT; 32660a708f8fSGustavo F. Padovan 326749208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit local busy", chan); 32680a708f8fSGustavo F. Padovan 32690a708f8fSGustavo F. Padovan return 0; 32700a708f8fSGustavo F. Padovan } 32710a708f8fSGustavo F. Padovan 32720a708f8fSGustavo F. Padovan static void l2cap_busy_work(struct work_struct *work) 32730a708f8fSGustavo F. Padovan { 32740a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 3275311bb895SGustavo F. Padovan struct l2cap_chan *chan = 3276311bb895SGustavo F. Padovan container_of(work, struct l2cap_chan, busy_work); 3277311bb895SGustavo F. Padovan struct sock *sk = chan->sk; 32780a708f8fSGustavo F. Padovan int n_tries = 0, timeo = HZ/5, err; 32790a708f8fSGustavo F. Padovan struct sk_buff *skb; 32800a708f8fSGustavo F. Padovan 32810a708f8fSGustavo F. Padovan lock_sock(sk); 32820a708f8fSGustavo F. Padovan 32830a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 3284311bb895SGustavo F. Padovan while ((skb = skb_peek(&chan->busy_q))) { 32850a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 32860a708f8fSGustavo F. Padovan 32870a708f8fSGustavo F. Padovan if (n_tries++ > L2CAP_LOCAL_BUSY_TRIES) { 32880a708f8fSGustavo F. Padovan err = -EBUSY; 32898c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, EBUSY); 32900a708f8fSGustavo F. Padovan break; 32910a708f8fSGustavo F. Padovan } 32920a708f8fSGustavo F. Padovan 32930a708f8fSGustavo F. Padovan if (!timeo) 32940a708f8fSGustavo F. Padovan timeo = HZ/5; 32950a708f8fSGustavo F. Padovan 32960a708f8fSGustavo F. Padovan if (signal_pending(current)) { 32970a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 32980a708f8fSGustavo F. Padovan break; 32990a708f8fSGustavo F. Padovan } 33000a708f8fSGustavo F. Padovan 33010a708f8fSGustavo F. Padovan release_sock(sk); 33020a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 33030a708f8fSGustavo F. Padovan lock_sock(sk); 33040a708f8fSGustavo F. Padovan 33050a708f8fSGustavo F. Padovan err = sock_error(sk); 33060a708f8fSGustavo F. Padovan if (err) 33070a708f8fSGustavo F. Padovan break; 33080a708f8fSGustavo F. Padovan 3309311bb895SGustavo F. Padovan if (l2cap_try_push_rx_skb(chan) == 0) 33100a708f8fSGustavo F. Padovan break; 33110a708f8fSGustavo F. Padovan } 33120a708f8fSGustavo F. Padovan 33130a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 33140a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 33150a708f8fSGustavo F. Padovan 33160a708f8fSGustavo F. Padovan release_sock(sk); 33170a708f8fSGustavo F. Padovan } 33180a708f8fSGustavo F. Padovan 3319525cd185SGustavo F. Padovan static int l2cap_push_rx_skb(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 33200a708f8fSGustavo F. Padovan { 33210a708f8fSGustavo F. Padovan int sctrl, err; 33220a708f8fSGustavo F. Padovan 3323525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 33240a708f8fSGustavo F. Padovan bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT; 3325f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->busy_q, skb); 3326525cd185SGustavo F. Padovan return l2cap_try_push_rx_skb(chan); 33270a708f8fSGustavo F. Padovan 33280a708f8fSGustavo F. Padovan 33290a708f8fSGustavo F. Padovan } 33300a708f8fSGustavo F. Padovan 3331525cd185SGustavo F. Padovan err = l2cap_ertm_reassembly_sdu(chan, skb, control); 33320a708f8fSGustavo F. Padovan if (err >= 0) { 333342e5c802SGustavo F. Padovan chan->buffer_seq = (chan->buffer_seq + 1) % 64; 33340a708f8fSGustavo F. Padovan return err; 33350a708f8fSGustavo F. Padovan } 33360a708f8fSGustavo F. Padovan 33370a708f8fSGustavo F. Padovan /* Busy Condition */ 3338311bb895SGustavo F. Padovan BT_DBG("chan %p, Enter local busy", chan); 33390a708f8fSGustavo F. Padovan 3340525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_LOCAL_BUSY; 33410a708f8fSGustavo F. Padovan bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT; 3342f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->busy_q, skb); 33430a708f8fSGustavo F. Padovan 334442e5c802SGustavo F. Padovan sctrl = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 33450a708f8fSGustavo F. Padovan sctrl |= L2CAP_SUPER_RCV_NOT_READY; 3346525cd185SGustavo F. Padovan l2cap_send_sframe(chan, sctrl); 33470a708f8fSGustavo F. Padovan 3348525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 33490a708f8fSGustavo F. Padovan 3350e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 33510a708f8fSGustavo F. Padovan 3352311bb895SGustavo F. Padovan queue_work(_busy_wq, &chan->busy_work); 33530a708f8fSGustavo F. Padovan 33540a708f8fSGustavo F. Padovan return err; 33550a708f8fSGustavo F. Padovan } 33560a708f8fSGustavo F. Padovan 3357525cd185SGustavo F. Padovan static int l2cap_streaming_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 33580a708f8fSGustavo F. Padovan { 33590a708f8fSGustavo F. Padovan struct sk_buff *_skb; 33600a708f8fSGustavo F. Padovan int err = -EINVAL; 33610a708f8fSGustavo F. Padovan 33620a708f8fSGustavo F. Padovan /* 33630a708f8fSGustavo F. Padovan * TODO: We have to notify the userland if some data is lost with the 33640a708f8fSGustavo F. Padovan * Streaming Mode. 33650a708f8fSGustavo F. Padovan */ 33660a708f8fSGustavo F. Padovan 33670a708f8fSGustavo F. Padovan switch (control & L2CAP_CTRL_SAR) { 33680a708f8fSGustavo F. Padovan case L2CAP_SDU_UNSEGMENTED: 3369525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) { 33706f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 33710a708f8fSGustavo F. Padovan break; 33720a708f8fSGustavo F. Padovan } 33730a708f8fSGustavo F. Padovan 3374525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, skb); 33750a708f8fSGustavo F. Padovan if (!err) 33760a708f8fSGustavo F. Padovan return 0; 33770a708f8fSGustavo F. Padovan 33780a708f8fSGustavo F. Padovan break; 33790a708f8fSGustavo F. Padovan 33800a708f8fSGustavo F. Padovan case L2CAP_SDU_START: 3381525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) { 33826f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 33830a708f8fSGustavo F. Padovan break; 33840a708f8fSGustavo F. Padovan } 33850a708f8fSGustavo F. Padovan 33866f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 33870a708f8fSGustavo F. Padovan skb_pull(skb, 2); 33880a708f8fSGustavo F. Padovan 33890c1bc5c6SGustavo F. Padovan if (chan->sdu_len > chan->imtu) { 33900a708f8fSGustavo F. Padovan err = -EMSGSIZE; 33910a708f8fSGustavo F. Padovan break; 33920a708f8fSGustavo F. Padovan } 33930a708f8fSGustavo F. Padovan 33946f61fd47SGustavo F. Padovan chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC); 33956f61fd47SGustavo F. Padovan if (!chan->sdu) { 33960a708f8fSGustavo F. Padovan err = -ENOMEM; 33970a708f8fSGustavo F. Padovan break; 33980a708f8fSGustavo F. Padovan } 33990a708f8fSGustavo F. Padovan 34006f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 34010a708f8fSGustavo F. Padovan 3402525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_SDU; 34036f61fd47SGustavo F. Padovan chan->partial_sdu_len = skb->len; 34040a708f8fSGustavo F. Padovan err = 0; 34050a708f8fSGustavo F. Padovan break; 34060a708f8fSGustavo F. Padovan 34070a708f8fSGustavo F. Padovan case L2CAP_SDU_CONTINUE: 3408525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 34090a708f8fSGustavo F. Padovan break; 34100a708f8fSGustavo F. Padovan 34116f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 34120a708f8fSGustavo F. Padovan 34136f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 34146f61fd47SGustavo F. Padovan if (chan->partial_sdu_len > chan->sdu_len) 34156f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 34160a708f8fSGustavo F. Padovan else 34170a708f8fSGustavo F. Padovan err = 0; 34180a708f8fSGustavo F. Padovan 34190a708f8fSGustavo F. Padovan break; 34200a708f8fSGustavo F. Padovan 34210a708f8fSGustavo F. Padovan case L2CAP_SDU_END: 3422525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 34230a708f8fSGustavo F. Padovan break; 34240a708f8fSGustavo F. Padovan 34256f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 34260a708f8fSGustavo F. Padovan 3427525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_SDU; 34286f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 34290a708f8fSGustavo F. Padovan 34300c1bc5c6SGustavo F. Padovan if (chan->partial_sdu_len > chan->imtu) 34310a708f8fSGustavo F. Padovan goto drop; 34320a708f8fSGustavo F. Padovan 34336f61fd47SGustavo F. Padovan if (chan->partial_sdu_len == chan->sdu_len) { 34346f61fd47SGustavo F. Padovan _skb = skb_clone(chan->sdu, GFP_ATOMIC); 3435525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, _skb); 34360a708f8fSGustavo F. Padovan if (err < 0) 34370a708f8fSGustavo F. Padovan kfree_skb(_skb); 34380a708f8fSGustavo F. Padovan } 34390a708f8fSGustavo F. Padovan err = 0; 34400a708f8fSGustavo F. Padovan 34410a708f8fSGustavo F. Padovan drop: 34426f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 34430a708f8fSGustavo F. Padovan break; 34440a708f8fSGustavo F. Padovan } 34450a708f8fSGustavo F. Padovan 34460a708f8fSGustavo F. Padovan kfree_skb(skb); 34470a708f8fSGustavo F. Padovan return err; 34480a708f8fSGustavo F. Padovan } 34490a708f8fSGustavo F. Padovan 3450525cd185SGustavo F. Padovan static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq) 34510a708f8fSGustavo F. Padovan { 34520a708f8fSGustavo F. Padovan struct sk_buff *skb; 34530a708f8fSGustavo F. Padovan u16 control; 34540a708f8fSGustavo F. Padovan 3455f1c6775bSGustavo F. Padovan while ((skb = skb_peek(&chan->srej_q))) { 34560a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq != tx_seq) 34570a708f8fSGustavo F. Padovan break; 34580a708f8fSGustavo F. Padovan 3459f1c6775bSGustavo F. Padovan skb = skb_dequeue(&chan->srej_q); 34600a708f8fSGustavo F. Padovan control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT; 3461525cd185SGustavo F. Padovan l2cap_ertm_reassembly_sdu(chan, skb, control); 346242e5c802SGustavo F. Padovan chan->buffer_seq_srej = 346342e5c802SGustavo F. Padovan (chan->buffer_seq_srej + 1) % 64; 34640a708f8fSGustavo F. Padovan tx_seq = (tx_seq + 1) % 64; 34650a708f8fSGustavo F. Padovan } 34660a708f8fSGustavo F. Padovan } 34670a708f8fSGustavo F. Padovan 3468525cd185SGustavo F. Padovan static void l2cap_resend_srejframe(struct l2cap_chan *chan, u8 tx_seq) 34690a708f8fSGustavo F. Padovan { 34700a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 34710a708f8fSGustavo F. Padovan u16 control; 34720a708f8fSGustavo F. Padovan 347339d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 34740a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 34750a708f8fSGustavo F. Padovan list_del(&l->list); 34760a708f8fSGustavo F. Padovan kfree(l); 34770a708f8fSGustavo F. Padovan return; 34780a708f8fSGustavo F. Padovan } 34790a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 34800a708f8fSGustavo F. Padovan control |= l->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 3481525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 34820a708f8fSGustavo F. Padovan list_del(&l->list); 348339d5a3eeSGustavo F. Padovan list_add_tail(&l->list, &chan->srej_l); 34840a708f8fSGustavo F. Padovan } 34850a708f8fSGustavo F. Padovan } 34860a708f8fSGustavo F. Padovan 3487525cd185SGustavo F. Padovan static void l2cap_send_srejframe(struct l2cap_chan *chan, u8 tx_seq) 34880a708f8fSGustavo F. Padovan { 34890a708f8fSGustavo F. Padovan struct srej_list *new; 34900a708f8fSGustavo F. Padovan u16 control; 34910a708f8fSGustavo F. Padovan 349242e5c802SGustavo F. Padovan while (tx_seq != chan->expected_tx_seq) { 34930a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 349442e5c802SGustavo F. Padovan control |= chan->expected_tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 3495525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 34960a708f8fSGustavo F. Padovan 34970a708f8fSGustavo F. Padovan new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); 349842e5c802SGustavo F. Padovan new->tx_seq = chan->expected_tx_seq; 349942e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 350039d5a3eeSGustavo F. Padovan list_add_tail(&new->list, &chan->srej_l); 35010a708f8fSGustavo F. Padovan } 350242e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 35030a708f8fSGustavo F. Padovan } 35040a708f8fSGustavo F. Padovan 3505525cd185SGustavo F. Padovan static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) 35060a708f8fSGustavo F. Padovan { 35070a708f8fSGustavo F. Padovan u8 tx_seq = __get_txseq(rx_control); 35080a708f8fSGustavo F. Padovan u8 req_seq = __get_reqseq(rx_control); 35090a708f8fSGustavo F. Padovan u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT; 35100a708f8fSGustavo F. Padovan int tx_seq_offset, expected_tx_seq_offset; 351147d1ec61SGustavo F. Padovan int num_to_ack = (chan->tx_win/6) + 1; 35120a708f8fSGustavo F. Padovan int err = 0; 35130a708f8fSGustavo F. Padovan 3514525cd185SGustavo F. Padovan BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len, 3515525cd185SGustavo F. Padovan tx_seq, rx_control); 35160a708f8fSGustavo F. Padovan 35170a708f8fSGustavo F. Padovan if (L2CAP_CTRL_FINAL & rx_control && 3518525cd185SGustavo F. Padovan chan->conn_state & L2CAP_CONN_WAIT_F) { 3519e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 35206a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 35210a708f8fSGustavo F. Padovan __mod_retrans_timer(); 3522525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_WAIT_F; 35230a708f8fSGustavo F. Padovan } 35240a708f8fSGustavo F. Padovan 352542e5c802SGustavo F. Padovan chan->expected_ack_seq = req_seq; 352642e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 35270a708f8fSGustavo F. Padovan 352842e5c802SGustavo F. Padovan if (tx_seq == chan->expected_tx_seq) 35290a708f8fSGustavo F. Padovan goto expected; 35300a708f8fSGustavo F. Padovan 353142e5c802SGustavo F. Padovan tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; 35320a708f8fSGustavo F. Padovan if (tx_seq_offset < 0) 35330a708f8fSGustavo F. Padovan tx_seq_offset += 64; 35340a708f8fSGustavo F. Padovan 35350a708f8fSGustavo F. Padovan /* invalid tx_seq */ 353647d1ec61SGustavo F. Padovan if (tx_seq_offset >= chan->tx_win) { 35378c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 35380a708f8fSGustavo F. Padovan goto drop; 35390a708f8fSGustavo F. Padovan } 35400a708f8fSGustavo F. Padovan 3541525cd185SGustavo F. Padovan if (chan->conn_state == L2CAP_CONN_LOCAL_BUSY) 35420a708f8fSGustavo F. Padovan goto drop; 35430a708f8fSGustavo F. Padovan 3544525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 35450a708f8fSGustavo F. Padovan struct srej_list *first; 35460a708f8fSGustavo F. Padovan 354739d5a3eeSGustavo F. Padovan first = list_first_entry(&chan->srej_l, 35480a708f8fSGustavo F. Padovan struct srej_list, list); 35490a708f8fSGustavo F. Padovan if (tx_seq == first->tx_seq) { 355042e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 3551525cd185SGustavo F. Padovan l2cap_check_srej_gap(chan, tx_seq); 35520a708f8fSGustavo F. Padovan 35530a708f8fSGustavo F. Padovan list_del(&first->list); 35540a708f8fSGustavo F. Padovan kfree(first); 35550a708f8fSGustavo F. Padovan 355639d5a3eeSGustavo F. Padovan if (list_empty(&chan->srej_l)) { 355742e5c802SGustavo F. Padovan chan->buffer_seq = chan->buffer_seq_srej; 3558525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SREJ_SENT; 3559525cd185SGustavo F. Padovan l2cap_send_ack(chan); 356049208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit SREJ_SENT", chan); 35610a708f8fSGustavo F. Padovan } 35620a708f8fSGustavo F. Padovan } else { 35630a708f8fSGustavo F. Padovan struct srej_list *l; 35640a708f8fSGustavo F. Padovan 35650a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 356642e5c802SGustavo F. Padovan if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0) 35670a708f8fSGustavo F. Padovan goto drop; 35680a708f8fSGustavo F. Padovan 356939d5a3eeSGustavo F. Padovan list_for_each_entry(l, &chan->srej_l, list) { 35700a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 3571525cd185SGustavo F. Padovan l2cap_resend_srejframe(chan, tx_seq); 35720a708f8fSGustavo F. Padovan return 0; 35730a708f8fSGustavo F. Padovan } 35740a708f8fSGustavo F. Padovan } 3575525cd185SGustavo F. Padovan l2cap_send_srejframe(chan, tx_seq); 35760a708f8fSGustavo F. Padovan } 35770a708f8fSGustavo F. Padovan } else { 35780a708f8fSGustavo F. Padovan expected_tx_seq_offset = 357942e5c802SGustavo F. Padovan (chan->expected_tx_seq - chan->buffer_seq) % 64; 35800a708f8fSGustavo F. Padovan if (expected_tx_seq_offset < 0) 35810a708f8fSGustavo F. Padovan expected_tx_seq_offset += 64; 35820a708f8fSGustavo F. Padovan 35830a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 35840a708f8fSGustavo F. Padovan if (tx_seq_offset < expected_tx_seq_offset) 35850a708f8fSGustavo F. Padovan goto drop; 35860a708f8fSGustavo F. Padovan 3587525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_SENT; 35880a708f8fSGustavo F. Padovan 358949208c9cSGustavo F. Padovan BT_DBG("chan %p, Enter SREJ", chan); 35900a708f8fSGustavo F. Padovan 359139d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 359242e5c802SGustavo F. Padovan chan->buffer_seq_srej = chan->buffer_seq; 35930a708f8fSGustavo F. Padovan 3594f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->srej_q); 3595f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->busy_q); 359642e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 35970a708f8fSGustavo F. Padovan 3598525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_PBIT; 35990a708f8fSGustavo F. Padovan 3600525cd185SGustavo F. Padovan l2cap_send_srejframe(chan, tx_seq); 36010a708f8fSGustavo F. Padovan 3602e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 36030a708f8fSGustavo F. Padovan } 36040a708f8fSGustavo F. Padovan return 0; 36050a708f8fSGustavo F. Padovan 36060a708f8fSGustavo F. Padovan expected: 360742e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 36080a708f8fSGustavo F. Padovan 3609525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 36100a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 36110a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 3612f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 36130a708f8fSGustavo F. Padovan return 0; 36140a708f8fSGustavo F. Padovan } 36150a708f8fSGustavo F. Padovan 3616525cd185SGustavo F. Padovan err = l2cap_push_rx_skb(chan, skb, rx_control); 36170a708f8fSGustavo F. Padovan if (err < 0) 36180a708f8fSGustavo F. Padovan return 0; 36190a708f8fSGustavo F. Padovan 36200a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_FINAL) { 3621525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3622525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 36230a708f8fSGustavo F. Padovan else 3624525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 36250a708f8fSGustavo F. Padovan } 36260a708f8fSGustavo F. Padovan 36270a708f8fSGustavo F. Padovan __mod_ack_timer(); 36280a708f8fSGustavo F. Padovan 36296a026610SGustavo F. Padovan chan->num_acked = (chan->num_acked + 1) % num_to_ack; 36306a026610SGustavo F. Padovan if (chan->num_acked == num_to_ack - 1) 3631525cd185SGustavo F. Padovan l2cap_send_ack(chan); 36320a708f8fSGustavo F. Padovan 36330a708f8fSGustavo F. Padovan return 0; 36340a708f8fSGustavo F. Padovan 36350a708f8fSGustavo F. Padovan drop: 36360a708f8fSGustavo F. Padovan kfree_skb(skb); 36370a708f8fSGustavo F. Padovan return 0; 36380a708f8fSGustavo F. Padovan } 36390a708f8fSGustavo F. Padovan 3640525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control) 36410a708f8fSGustavo F. Padovan { 364249208c9cSGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, __get_reqseq(rx_control), 36430a708f8fSGustavo F. Padovan rx_control); 36440a708f8fSGustavo F. Padovan 364542e5c802SGustavo F. Padovan chan->expected_ack_seq = __get_reqseq(rx_control); 364642e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 36470a708f8fSGustavo F. Padovan 36480a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) { 3649525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 3650525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 3651525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && 36526a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 36530a708f8fSGustavo F. Padovan __mod_retrans_timer(); 36540a708f8fSGustavo F. Padovan 3655525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 3656525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 36570a708f8fSGustavo F. Padovan } else { 3658525cd185SGustavo F. Padovan l2cap_send_i_or_rr_or_rnr(chan); 36590a708f8fSGustavo F. Padovan } 36600a708f8fSGustavo F. Padovan 36610a708f8fSGustavo F. Padovan } else if (rx_control & L2CAP_CTRL_FINAL) { 3662525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 36630a708f8fSGustavo F. Padovan 3664525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3665525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 36660a708f8fSGustavo F. Padovan else 3667525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 36680a708f8fSGustavo F. Padovan 36690a708f8fSGustavo F. Padovan } else { 3670525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && 36716a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 36720a708f8fSGustavo F. Padovan __mod_retrans_timer(); 36730a708f8fSGustavo F. Padovan 3674525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 3675525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) 3676525cd185SGustavo F. Padovan l2cap_send_ack(chan); 36770a708f8fSGustavo F. Padovan else 3678525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 36790a708f8fSGustavo F. Padovan } 36800a708f8fSGustavo F. Padovan } 36810a708f8fSGustavo F. Padovan 3682525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control) 36830a708f8fSGustavo F. Padovan { 36840a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 36850a708f8fSGustavo F. Padovan 3686525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 36870a708f8fSGustavo F. Padovan 3688525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 36890a708f8fSGustavo F. Padovan 369042e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 369142e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 36920a708f8fSGustavo F. Padovan 36930a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_FINAL) { 3694525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3695525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 36960a708f8fSGustavo F. Padovan else 3697525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 36980a708f8fSGustavo F. Padovan } else { 3699525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 37000a708f8fSGustavo F. Padovan 3701525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) 3702525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_REJ_ACT; 37030a708f8fSGustavo F. Padovan } 37040a708f8fSGustavo F. Padovan } 3705525cd185SGustavo F. Padovan static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control) 37060a708f8fSGustavo F. Padovan { 37070a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 37080a708f8fSGustavo F. Padovan 3709525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 37100a708f8fSGustavo F. Padovan 3711525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 37120a708f8fSGustavo F. Padovan 37130a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) { 371442e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 371542e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 37160a708f8fSGustavo F. Padovan 3717525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 3718525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 37190a708f8fSGustavo F. Padovan 3720525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 37210a708f8fSGustavo F. Padovan 3722525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) { 37236a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 3724525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_ACT; 37250a708f8fSGustavo F. Padovan } 37260a708f8fSGustavo F. Padovan } else if (rx_control & L2CAP_CTRL_FINAL) { 3727525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_SREJ_ACT) && 37286a026610SGustavo F. Padovan chan->srej_save_reqseq == tx_seq) 3729525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SREJ_ACT; 37300a708f8fSGustavo F. Padovan else 3731525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 37320a708f8fSGustavo F. Padovan } else { 3733525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 3734525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) { 37356a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 3736525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_ACT; 37370a708f8fSGustavo F. Padovan } 37380a708f8fSGustavo F. Padovan } 37390a708f8fSGustavo F. Padovan } 37400a708f8fSGustavo F. Padovan 3741525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control) 37420a708f8fSGustavo F. Padovan { 37430a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 37440a708f8fSGustavo F. Padovan 3745525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 37460a708f8fSGustavo F. Padovan 3747525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_REMOTE_BUSY; 374842e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 374942e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 37500a708f8fSGustavo F. Padovan 37510a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3752525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 37530a708f8fSGustavo F. Padovan 3754525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SREJ_SENT)) { 3755e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 37560a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3757525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); 37580a708f8fSGustavo F. Padovan return; 37590a708f8fSGustavo F. Padovan } 37600a708f8fSGustavo F. Padovan 37610a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3762525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 37630a708f8fSGustavo F. Padovan else 3764525cd185SGustavo F. Padovan l2cap_send_sframe(chan, L2CAP_SUPER_RCV_READY); 37650a708f8fSGustavo F. Padovan } 37660a708f8fSGustavo F. Padovan 3767525cd185SGustavo F. Padovan static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) 37680a708f8fSGustavo F. Padovan { 3769525cd185SGustavo F. Padovan BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len); 37700a708f8fSGustavo F. Padovan 37710a708f8fSGustavo F. Padovan if (L2CAP_CTRL_FINAL & rx_control && 3772525cd185SGustavo F. Padovan chan->conn_state & L2CAP_CONN_WAIT_F) { 3773e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 37746a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 37750a708f8fSGustavo F. Padovan __mod_retrans_timer(); 3776525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_WAIT_F; 37770a708f8fSGustavo F. Padovan } 37780a708f8fSGustavo F. Padovan 37790a708f8fSGustavo F. Padovan switch (rx_control & L2CAP_CTRL_SUPERVISE) { 37800a708f8fSGustavo F. Padovan case L2CAP_SUPER_RCV_READY: 3781525cd185SGustavo F. Padovan l2cap_data_channel_rrframe(chan, rx_control); 37820a708f8fSGustavo F. Padovan break; 37830a708f8fSGustavo F. Padovan 37840a708f8fSGustavo F. Padovan case L2CAP_SUPER_REJECT: 3785525cd185SGustavo F. Padovan l2cap_data_channel_rejframe(chan, rx_control); 37860a708f8fSGustavo F. Padovan break; 37870a708f8fSGustavo F. Padovan 37880a708f8fSGustavo F. Padovan case L2CAP_SUPER_SELECT_REJECT: 3789525cd185SGustavo F. Padovan l2cap_data_channel_srejframe(chan, rx_control); 37900a708f8fSGustavo F. Padovan break; 37910a708f8fSGustavo F. Padovan 37920a708f8fSGustavo F. Padovan case L2CAP_SUPER_RCV_NOT_READY: 3793525cd185SGustavo F. Padovan l2cap_data_channel_rnrframe(chan, rx_control); 37940a708f8fSGustavo F. Padovan break; 37950a708f8fSGustavo F. Padovan } 37960a708f8fSGustavo F. Padovan 37970a708f8fSGustavo F. Padovan kfree_skb(skb); 37980a708f8fSGustavo F. Padovan return 0; 37990a708f8fSGustavo F. Padovan } 38000a708f8fSGustavo F. Padovan 38010a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb) 38020a708f8fSGustavo F. Padovan { 3803525cd185SGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 38040a708f8fSGustavo F. Padovan u16 control; 38050a708f8fSGustavo F. Padovan u8 req_seq; 38060a708f8fSGustavo F. Padovan int len, next_tx_seq_offset, req_seq_offset; 38070a708f8fSGustavo F. Padovan 38080a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data); 38090a708f8fSGustavo F. Padovan skb_pull(skb, 2); 38100a708f8fSGustavo F. Padovan len = skb->len; 38110a708f8fSGustavo F. Padovan 38120a708f8fSGustavo F. Padovan /* 38130a708f8fSGustavo F. Padovan * We can just drop the corrupted I-frame here. 38140a708f8fSGustavo F. Padovan * Receiver will miss it and start proper recovery 38150a708f8fSGustavo F. Padovan * procedures and ask retransmission. 38160a708f8fSGustavo F. Padovan */ 381747d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 38180a708f8fSGustavo F. Padovan goto drop; 38190a708f8fSGustavo F. Padovan 38200a708f8fSGustavo F. Padovan if (__is_sar_start(control) && __is_iframe(control)) 38210a708f8fSGustavo F. Padovan len -= 2; 38220a708f8fSGustavo F. Padovan 382347d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 38240a708f8fSGustavo F. Padovan len -= 2; 38250a708f8fSGustavo F. Padovan 382647d1ec61SGustavo F. Padovan if (len > chan->mps) { 38278c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38280a708f8fSGustavo F. Padovan goto drop; 38290a708f8fSGustavo F. Padovan } 38300a708f8fSGustavo F. Padovan 38310a708f8fSGustavo F. Padovan req_seq = __get_reqseq(control); 383242e5c802SGustavo F. Padovan req_seq_offset = (req_seq - chan->expected_ack_seq) % 64; 38330a708f8fSGustavo F. Padovan if (req_seq_offset < 0) 38340a708f8fSGustavo F. Padovan req_seq_offset += 64; 38350a708f8fSGustavo F. Padovan 38360a708f8fSGustavo F. Padovan next_tx_seq_offset = 383742e5c802SGustavo F. Padovan (chan->next_tx_seq - chan->expected_ack_seq) % 64; 38380a708f8fSGustavo F. Padovan if (next_tx_seq_offset < 0) 38390a708f8fSGustavo F. Padovan next_tx_seq_offset += 64; 38400a708f8fSGustavo F. Padovan 38410a708f8fSGustavo F. Padovan /* check for invalid req-seq */ 38420a708f8fSGustavo F. Padovan if (req_seq_offset > next_tx_seq_offset) { 38438c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38440a708f8fSGustavo F. Padovan goto drop; 38450a708f8fSGustavo F. Padovan } 38460a708f8fSGustavo F. Padovan 38470a708f8fSGustavo F. Padovan if (__is_iframe(control)) { 38480a708f8fSGustavo F. Padovan if (len < 0) { 38498c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38500a708f8fSGustavo F. Padovan goto drop; 38510a708f8fSGustavo F. Padovan } 38520a708f8fSGustavo F. Padovan 3853525cd185SGustavo F. Padovan l2cap_data_channel_iframe(chan, control, skb); 38540a708f8fSGustavo F. Padovan } else { 38550a708f8fSGustavo F. Padovan if (len != 0) { 38560a708f8fSGustavo F. Padovan BT_ERR("%d", len); 38578c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38580a708f8fSGustavo F. Padovan goto drop; 38590a708f8fSGustavo F. Padovan } 38600a708f8fSGustavo F. Padovan 3861525cd185SGustavo F. Padovan l2cap_data_channel_sframe(chan, control, skb); 38620a708f8fSGustavo F. Padovan } 38630a708f8fSGustavo F. Padovan 38640a708f8fSGustavo F. Padovan return 0; 38650a708f8fSGustavo F. Padovan 38660a708f8fSGustavo F. Padovan drop: 38670a708f8fSGustavo F. Padovan kfree_skb(skb); 38680a708f8fSGustavo F. Padovan return 0; 38690a708f8fSGustavo F. Padovan } 38700a708f8fSGustavo F. Padovan 38710a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) 38720a708f8fSGustavo F. Padovan { 387348454079SGustavo F. Padovan struct l2cap_chan *chan; 3874bf734843SDavid S. Miller struct sock *sk = NULL; 38750a708f8fSGustavo F. Padovan u16 control; 38760a708f8fSGustavo F. Padovan u8 tx_seq; 38770a708f8fSGustavo F. Padovan int len; 38780a708f8fSGustavo F. Padovan 3879baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 388048454079SGustavo F. Padovan if (!chan) { 38810a708f8fSGustavo F. Padovan BT_DBG("unknown cid 0x%4.4x", cid); 38820a708f8fSGustavo F. Padovan goto drop; 38830a708f8fSGustavo F. Padovan } 38840a708f8fSGustavo F. Padovan 388548454079SGustavo F. Padovan sk = chan->sk; 38860a708f8fSGustavo F. Padovan 388749208c9cSGustavo F. Padovan BT_DBG("chan %p, len %d", chan, skb->len); 38880a708f8fSGustavo F. Padovan 38890a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 38900a708f8fSGustavo F. Padovan goto drop; 38910a708f8fSGustavo F. Padovan 38920c1bc5c6SGustavo F. Padovan switch (chan->mode) { 38930a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 38940a708f8fSGustavo F. Padovan /* If socket recv buffers overflows we drop data here 38950a708f8fSGustavo F. Padovan * which is *bad* because L2CAP has to be reliable. 38960a708f8fSGustavo F. Padovan * But we don't have any other choice. L2CAP doesn't 38970a708f8fSGustavo F. Padovan * provide flow control mechanism. */ 38980a708f8fSGustavo F. Padovan 38990c1bc5c6SGustavo F. Padovan if (chan->imtu < skb->len) 39000a708f8fSGustavo F. Padovan goto drop; 39010a708f8fSGustavo F. Padovan 39020a708f8fSGustavo F. Padovan if (!sock_queue_rcv_skb(sk, skb)) 39030a708f8fSGustavo F. Padovan goto done; 39040a708f8fSGustavo F. Padovan break; 39050a708f8fSGustavo F. Padovan 39060a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 39070a708f8fSGustavo F. Padovan if (!sock_owned_by_user(sk)) { 39080a708f8fSGustavo F. Padovan l2cap_ertm_data_rcv(sk, skb); 39090a708f8fSGustavo F. Padovan } else { 39100a708f8fSGustavo F. Padovan if (sk_add_backlog(sk, skb)) 39110a708f8fSGustavo F. Padovan goto drop; 39120a708f8fSGustavo F. Padovan } 39130a708f8fSGustavo F. Padovan 39140a708f8fSGustavo F. Padovan goto done; 39150a708f8fSGustavo F. Padovan 39160a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 39170a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data); 39180a708f8fSGustavo F. Padovan skb_pull(skb, 2); 39190a708f8fSGustavo F. Padovan len = skb->len; 39200a708f8fSGustavo F. Padovan 392147d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 39220a708f8fSGustavo F. Padovan goto drop; 39230a708f8fSGustavo F. Padovan 39240a708f8fSGustavo F. Padovan if (__is_sar_start(control)) 39250a708f8fSGustavo F. Padovan len -= 2; 39260a708f8fSGustavo F. Padovan 392747d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 39280a708f8fSGustavo F. Padovan len -= 2; 39290a708f8fSGustavo F. Padovan 393047d1ec61SGustavo F. Padovan if (len > chan->mps || len < 0 || __is_sframe(control)) 39310a708f8fSGustavo F. Padovan goto drop; 39320a708f8fSGustavo F. Padovan 39330a708f8fSGustavo F. Padovan tx_seq = __get_txseq(control); 39340a708f8fSGustavo F. Padovan 393542e5c802SGustavo F. Padovan if (chan->expected_tx_seq == tx_seq) 393642e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 39370a708f8fSGustavo F. Padovan else 393842e5c802SGustavo F. Padovan chan->expected_tx_seq = (tx_seq + 1) % 64; 39390a708f8fSGustavo F. Padovan 3940525cd185SGustavo F. Padovan l2cap_streaming_reassembly_sdu(chan, skb, control); 39410a708f8fSGustavo F. Padovan 39420a708f8fSGustavo F. Padovan goto done; 39430a708f8fSGustavo F. Padovan 39440a708f8fSGustavo F. Padovan default: 39450c1bc5c6SGustavo F. Padovan BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode); 39460a708f8fSGustavo F. Padovan break; 39470a708f8fSGustavo F. Padovan } 39480a708f8fSGustavo F. Padovan 39490a708f8fSGustavo F. Padovan drop: 39500a708f8fSGustavo F. Padovan kfree_skb(skb); 39510a708f8fSGustavo F. Padovan 39520a708f8fSGustavo F. Padovan done: 39530a708f8fSGustavo F. Padovan if (sk) 39540a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 39550a708f8fSGustavo F. Padovan 39560a708f8fSGustavo F. Padovan return 0; 39570a708f8fSGustavo F. Padovan } 39580a708f8fSGustavo F. Padovan 39590a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb) 39600a708f8fSGustavo F. Padovan { 39616dcae1eaSDavid S. Miller struct sock *sk = NULL; 396223691d75SGustavo F. Padovan struct l2cap_chan *chan; 39630a708f8fSGustavo F. Padovan 396423691d75SGustavo F. Padovan chan = l2cap_global_chan_by_psm(0, psm, conn->src); 396523691d75SGustavo F. Padovan if (!chan) 39660a708f8fSGustavo F. Padovan goto drop; 39670a708f8fSGustavo F. Padovan 396823691d75SGustavo F. Padovan sk = chan->sk; 396923691d75SGustavo F. Padovan 39700a708f8fSGustavo F. Padovan bh_lock_sock(sk); 39710a708f8fSGustavo F. Padovan 39720a708f8fSGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 39730a708f8fSGustavo F. Padovan 39740a708f8fSGustavo F. Padovan if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) 39750a708f8fSGustavo F. Padovan goto drop; 39760a708f8fSGustavo F. Padovan 39770c1bc5c6SGustavo F. Padovan if (l2cap_pi(sk)->chan->imtu < skb->len) 39780a708f8fSGustavo F. Padovan goto drop; 39790a708f8fSGustavo F. Padovan 39800a708f8fSGustavo F. Padovan if (!sock_queue_rcv_skb(sk, skb)) 39810a708f8fSGustavo F. Padovan goto done; 39820a708f8fSGustavo F. Padovan 39830a708f8fSGustavo F. Padovan drop: 39840a708f8fSGustavo F. Padovan kfree_skb(skb); 39850a708f8fSGustavo F. Padovan 39860a708f8fSGustavo F. Padovan done: 39870a708f8fSGustavo F. Padovan if (sk) 39880a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 39890a708f8fSGustavo F. Padovan return 0; 39900a708f8fSGustavo F. Padovan } 39910a708f8fSGustavo F. Padovan 39929f69bda6SGustavo F. Padovan static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb) 39939f69bda6SGustavo F. Padovan { 39946dcae1eaSDavid S. Miller struct sock *sk = NULL; 399523691d75SGustavo F. Padovan struct l2cap_chan *chan; 39969f69bda6SGustavo F. Padovan 399723691d75SGustavo F. Padovan chan = l2cap_global_chan_by_scid(0, cid, conn->src); 399823691d75SGustavo F. Padovan if (!chan) 39999f69bda6SGustavo F. Padovan goto drop; 40009f69bda6SGustavo F. Padovan 400123691d75SGustavo F. Padovan sk = chan->sk; 400223691d75SGustavo F. Padovan 40039f69bda6SGustavo F. Padovan bh_lock_sock(sk); 40049f69bda6SGustavo F. Padovan 40059f69bda6SGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 40069f69bda6SGustavo F. Padovan 40079f69bda6SGustavo F. Padovan if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) 40089f69bda6SGustavo F. Padovan goto drop; 40099f69bda6SGustavo F. Padovan 40100c1bc5c6SGustavo F. Padovan if (l2cap_pi(sk)->chan->imtu < skb->len) 40119f69bda6SGustavo F. Padovan goto drop; 40129f69bda6SGustavo F. Padovan 40139f69bda6SGustavo F. Padovan if (!sock_queue_rcv_skb(sk, skb)) 40149f69bda6SGustavo F. Padovan goto done; 40159f69bda6SGustavo F. Padovan 40169f69bda6SGustavo F. Padovan drop: 40179f69bda6SGustavo F. Padovan kfree_skb(skb); 40189f69bda6SGustavo F. Padovan 40199f69bda6SGustavo F. Padovan done: 40209f69bda6SGustavo F. Padovan if (sk) 40219f69bda6SGustavo F. Padovan bh_unlock_sock(sk); 40229f69bda6SGustavo F. Padovan return 0; 40239f69bda6SGustavo F. Padovan } 40249f69bda6SGustavo F. Padovan 40250a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) 40260a708f8fSGustavo F. Padovan { 40270a708f8fSGustavo F. Padovan struct l2cap_hdr *lh = (void *) skb->data; 40280a708f8fSGustavo F. Padovan u16 cid, len; 40290a708f8fSGustavo F. Padovan __le16 psm; 40300a708f8fSGustavo F. Padovan 40310a708f8fSGustavo F. Padovan skb_pull(skb, L2CAP_HDR_SIZE); 40320a708f8fSGustavo F. Padovan cid = __le16_to_cpu(lh->cid); 40330a708f8fSGustavo F. Padovan len = __le16_to_cpu(lh->len); 40340a708f8fSGustavo F. Padovan 40350a708f8fSGustavo F. Padovan if (len != skb->len) { 40360a708f8fSGustavo F. Padovan kfree_skb(skb); 40370a708f8fSGustavo F. Padovan return; 40380a708f8fSGustavo F. Padovan } 40390a708f8fSGustavo F. Padovan 40400a708f8fSGustavo F. Padovan BT_DBG("len %d, cid 0x%4.4x", len, cid); 40410a708f8fSGustavo F. Padovan 40420a708f8fSGustavo F. Padovan switch (cid) { 40433300d9a9SClaudio Takahasi case L2CAP_CID_LE_SIGNALING: 40440a708f8fSGustavo F. Padovan case L2CAP_CID_SIGNALING: 40450a708f8fSGustavo F. Padovan l2cap_sig_channel(conn, skb); 40460a708f8fSGustavo F. Padovan break; 40470a708f8fSGustavo F. Padovan 40480a708f8fSGustavo F. Padovan case L2CAP_CID_CONN_LESS: 40490a708f8fSGustavo F. Padovan psm = get_unaligned_le16(skb->data); 40500a708f8fSGustavo F. Padovan skb_pull(skb, 2); 40510a708f8fSGustavo F. Padovan l2cap_conless_channel(conn, psm, skb); 40520a708f8fSGustavo F. Padovan break; 40530a708f8fSGustavo F. Padovan 40549f69bda6SGustavo F. Padovan case L2CAP_CID_LE_DATA: 40559f69bda6SGustavo F. Padovan l2cap_att_channel(conn, cid, skb); 40569f69bda6SGustavo F. Padovan break; 40579f69bda6SGustavo F. Padovan 40580a708f8fSGustavo F. Padovan default: 40590a708f8fSGustavo F. Padovan l2cap_data_channel(conn, cid, skb); 40600a708f8fSGustavo F. Padovan break; 40610a708f8fSGustavo F. Padovan } 40620a708f8fSGustavo F. Padovan } 40630a708f8fSGustavo F. Padovan 40640a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */ 40650a708f8fSGustavo F. Padovan 40660a708f8fSGustavo F. Padovan static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) 40670a708f8fSGustavo F. Padovan { 40680a708f8fSGustavo F. Padovan int exact = 0, lm1 = 0, lm2 = 0; 406923691d75SGustavo F. Padovan struct l2cap_chan *c; 40700a708f8fSGustavo F. Padovan 40710a708f8fSGustavo F. Padovan if (type != ACL_LINK) 40720a708f8fSGustavo F. Padovan return -EINVAL; 40730a708f8fSGustavo F. Padovan 40740a708f8fSGustavo F. Padovan BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); 40750a708f8fSGustavo F. Padovan 40760a708f8fSGustavo F. Padovan /* Find listening sockets and check their link_mode */ 407723691d75SGustavo F. Padovan read_lock(&chan_list_lock); 407823691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 407923691d75SGustavo F. Padovan struct sock *sk = c->sk; 40804343478fSGustavo F. Padovan 40810a708f8fSGustavo F. Padovan if (sk->sk_state != BT_LISTEN) 40820a708f8fSGustavo F. Padovan continue; 40830a708f8fSGustavo F. Padovan 40840a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { 40850a708f8fSGustavo F. Padovan lm1 |= HCI_LM_ACCEPT; 408623691d75SGustavo F. Padovan if (c->role_switch) 40870a708f8fSGustavo F. Padovan lm1 |= HCI_LM_MASTER; 40880a708f8fSGustavo F. Padovan exact++; 40890a708f8fSGustavo F. Padovan } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { 40900a708f8fSGustavo F. Padovan lm2 |= HCI_LM_ACCEPT; 409123691d75SGustavo F. Padovan if (c->role_switch) 40920a708f8fSGustavo F. Padovan lm2 |= HCI_LM_MASTER; 40930a708f8fSGustavo F. Padovan } 40940a708f8fSGustavo F. Padovan } 409523691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 40960a708f8fSGustavo F. Padovan 40970a708f8fSGustavo F. Padovan return exact ? lm1 : lm2; 40980a708f8fSGustavo F. Padovan } 40990a708f8fSGustavo F. Padovan 41000a708f8fSGustavo F. Padovan static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) 41010a708f8fSGustavo F. Padovan { 41020a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 41030a708f8fSGustavo F. Padovan 41040a708f8fSGustavo F. Padovan BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); 41050a708f8fSGustavo F. Padovan 4106acd7d370SVille Tervo if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) 41070a708f8fSGustavo F. Padovan return -EINVAL; 41080a708f8fSGustavo F. Padovan 41090a708f8fSGustavo F. Padovan if (!status) { 41100a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, status); 41110a708f8fSGustavo F. Padovan if (conn) 41120a708f8fSGustavo F. Padovan l2cap_conn_ready(conn); 41130a708f8fSGustavo F. Padovan } else 41140a708f8fSGustavo F. Padovan l2cap_conn_del(hcon, bt_err(status)); 41150a708f8fSGustavo F. Padovan 41160a708f8fSGustavo F. Padovan return 0; 41170a708f8fSGustavo F. Padovan } 41180a708f8fSGustavo F. Padovan 41190a708f8fSGustavo F. Padovan static int l2cap_disconn_ind(struct hci_conn *hcon) 41200a708f8fSGustavo F. Padovan { 41210a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 41220a708f8fSGustavo F. Padovan 41230a708f8fSGustavo F. Padovan BT_DBG("hcon %p", hcon); 41240a708f8fSGustavo F. Padovan 41250a708f8fSGustavo F. Padovan if (hcon->type != ACL_LINK || !conn) 41260a708f8fSGustavo F. Padovan return 0x13; 41270a708f8fSGustavo F. Padovan 41280a708f8fSGustavo F. Padovan return conn->disc_reason; 41290a708f8fSGustavo F. Padovan } 41300a708f8fSGustavo F. Padovan 41310a708f8fSGustavo F. Padovan static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) 41320a708f8fSGustavo F. Padovan { 41330a708f8fSGustavo F. Padovan BT_DBG("hcon %p reason %d", hcon, reason); 41340a708f8fSGustavo F. Padovan 4135acd7d370SVille Tervo if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) 41360a708f8fSGustavo F. Padovan return -EINVAL; 41370a708f8fSGustavo F. Padovan 41380a708f8fSGustavo F. Padovan l2cap_conn_del(hcon, bt_err(reason)); 41390a708f8fSGustavo F. Padovan 41400a708f8fSGustavo F. Padovan return 0; 41410a708f8fSGustavo F. Padovan } 41420a708f8fSGustavo F. Padovan 41434343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) 41440a708f8fSGustavo F. Padovan { 4145715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) 41460a708f8fSGustavo F. Padovan return; 41470a708f8fSGustavo F. Padovan 41480a708f8fSGustavo F. Padovan if (encrypt == 0x00) { 41494343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) { 4150*ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 4151*ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ * 5); 41524343478fSGustavo F. Padovan } else if (chan->sec_level == BT_SECURITY_HIGH) 41534519de9aSGustavo F. Padovan __l2cap_chan_close(chan, ECONNREFUSED); 41540a708f8fSGustavo F. Padovan } else { 41554343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) 4156*ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 41570a708f8fSGustavo F. Padovan } 41580a708f8fSGustavo F. Padovan } 41590a708f8fSGustavo F. Padovan 41600a708f8fSGustavo F. Padovan static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) 41610a708f8fSGustavo F. Padovan { 41620a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 416348454079SGustavo F. Padovan struct l2cap_chan *chan; 41640a708f8fSGustavo F. Padovan 41650a708f8fSGustavo F. Padovan if (!conn) 41660a708f8fSGustavo F. Padovan return 0; 41670a708f8fSGustavo F. Padovan 41680a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 41690a708f8fSGustavo F. Padovan 4170baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 41710a708f8fSGustavo F. Padovan 4172baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 417348454079SGustavo F. Padovan struct sock *sk = chan->sk; 4174baa7e1faSGustavo F. Padovan 41750a708f8fSGustavo F. Padovan bh_lock_sock(sk); 41760a708f8fSGustavo F. Padovan 4177b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_CONNECT_PEND) { 41780a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 41790a708f8fSGustavo F. Padovan continue; 41800a708f8fSGustavo F. Padovan } 41810a708f8fSGustavo F. Padovan 41820a708f8fSGustavo F. Padovan if (!status && (sk->sk_state == BT_CONNECTED || 41830a708f8fSGustavo F. Padovan sk->sk_state == BT_CONFIG)) { 41844343478fSGustavo F. Padovan l2cap_check_encryption(chan, encrypt); 41850a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 41860a708f8fSGustavo F. Padovan continue; 41870a708f8fSGustavo F. Padovan } 41880a708f8fSGustavo F. Padovan 41890a708f8fSGustavo F. Padovan if (sk->sk_state == BT_CONNECT) { 41900a708f8fSGustavo F. Padovan if (!status) { 41910a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 4192fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 4193fe4128e0SGustavo F. Padovan req.psm = chan->psm; 41940a708f8fSGustavo F. Padovan 4195fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 4196b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 41970a708f8fSGustavo F. Padovan 4198fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 41990a708f8fSGustavo F. Padovan L2CAP_CONN_REQ, sizeof(req), &req); 42000a708f8fSGustavo F. Padovan } else { 4201*ab07801dSGustavo F. Padovan l2cap_chan_clear_timer(chan); 4202*ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 10); 42030a708f8fSGustavo F. Padovan } 42040a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT2) { 42050a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 42060a708f8fSGustavo F. Padovan __u16 result; 42070a708f8fSGustavo F. Padovan 42080a708f8fSGustavo F. Padovan if (!status) { 42090a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 42100a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 42110a708f8fSGustavo F. Padovan } else { 42120a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 4213*ab07801dSGustavo F. Padovan l2cap_chan_set_timer(chan, HZ / 10); 42140a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 42150a708f8fSGustavo F. Padovan } 42160a708f8fSGustavo F. Padovan 4217fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4218fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 42190a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 42200a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 4221fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4222fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 42230a708f8fSGustavo F. Padovan } 42240a708f8fSGustavo F. Padovan 42250a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 42260a708f8fSGustavo F. Padovan } 42270a708f8fSGustavo F. Padovan 4228baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 42290a708f8fSGustavo F. Padovan 42300a708f8fSGustavo F. Padovan return 0; 42310a708f8fSGustavo F. Padovan } 42320a708f8fSGustavo F. Padovan 42330a708f8fSGustavo F. Padovan static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) 42340a708f8fSGustavo F. Padovan { 42350a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 42360a708f8fSGustavo F. Padovan 42370a708f8fSGustavo F. Padovan if (!conn) 42380a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 42390a708f8fSGustavo F. Padovan 42400a708f8fSGustavo F. Padovan if (!conn) 42410a708f8fSGustavo F. Padovan goto drop; 42420a708f8fSGustavo F. Padovan 42430a708f8fSGustavo F. Padovan BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); 42440a708f8fSGustavo F. Padovan 42450a708f8fSGustavo F. Padovan if (!(flags & ACL_CONT)) { 42460a708f8fSGustavo F. Padovan struct l2cap_hdr *hdr; 424748454079SGustavo F. Padovan struct l2cap_chan *chan; 42480a708f8fSGustavo F. Padovan u16 cid; 42490a708f8fSGustavo F. Padovan int len; 42500a708f8fSGustavo F. Padovan 42510a708f8fSGustavo F. Padovan if (conn->rx_len) { 42520a708f8fSGustavo F. Padovan BT_ERR("Unexpected start frame (len %d)", skb->len); 42530a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 42540a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 42550a708f8fSGustavo F. Padovan conn->rx_len = 0; 42560a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42570a708f8fSGustavo F. Padovan } 42580a708f8fSGustavo F. Padovan 42590a708f8fSGustavo F. Padovan /* Start fragment always begin with Basic L2CAP header */ 42600a708f8fSGustavo F. Padovan if (skb->len < L2CAP_HDR_SIZE) { 42610a708f8fSGustavo F. Padovan BT_ERR("Frame is too short (len %d)", skb->len); 42620a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42630a708f8fSGustavo F. Padovan goto drop; 42640a708f8fSGustavo F. Padovan } 42650a708f8fSGustavo F. Padovan 42660a708f8fSGustavo F. Padovan hdr = (struct l2cap_hdr *) skb->data; 42670a708f8fSGustavo F. Padovan len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; 42680a708f8fSGustavo F. Padovan cid = __le16_to_cpu(hdr->cid); 42690a708f8fSGustavo F. Padovan 42700a708f8fSGustavo F. Padovan if (len == skb->len) { 42710a708f8fSGustavo F. Padovan /* Complete frame received */ 42720a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, skb); 42730a708f8fSGustavo F. Padovan return 0; 42740a708f8fSGustavo F. Padovan } 42750a708f8fSGustavo F. Padovan 42760a708f8fSGustavo F. Padovan BT_DBG("Start: total len %d, frag len %d", len, skb->len); 42770a708f8fSGustavo F. Padovan 42780a708f8fSGustavo F. Padovan if (skb->len > len) { 42790a708f8fSGustavo F. Padovan BT_ERR("Frame is too long (len %d, expected len %d)", 42800a708f8fSGustavo F. Padovan skb->len, len); 42810a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42820a708f8fSGustavo F. Padovan goto drop; 42830a708f8fSGustavo F. Padovan } 42840a708f8fSGustavo F. Padovan 4285baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 42860a708f8fSGustavo F. Padovan 428748454079SGustavo F. Padovan if (chan && chan->sk) { 428848454079SGustavo F. Padovan struct sock *sk = chan->sk; 428948454079SGustavo F. Padovan 42900c1bc5c6SGustavo F. Padovan if (chan->imtu < len - L2CAP_HDR_SIZE) { 429148454079SGustavo F. Padovan BT_ERR("Frame exceeding recv MTU (len %d, " 429248454079SGustavo F. Padovan "MTU %d)", len, 42930c1bc5c6SGustavo F. Padovan chan->imtu); 42940a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 42950a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 42960a708f8fSGustavo F. Padovan goto drop; 42970a708f8fSGustavo F. Padovan } 42980a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 429948454079SGustavo F. Padovan } 43000a708f8fSGustavo F. Padovan 43010a708f8fSGustavo F. Padovan /* Allocate skb for the complete frame (with header) */ 43020a708f8fSGustavo F. Padovan conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); 43030a708f8fSGustavo F. Padovan if (!conn->rx_skb) 43040a708f8fSGustavo F. Padovan goto drop; 43050a708f8fSGustavo F. Padovan 43060a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 43070a708f8fSGustavo F. Padovan skb->len); 43080a708f8fSGustavo F. Padovan conn->rx_len = len - skb->len; 43090a708f8fSGustavo F. Padovan } else { 43100a708f8fSGustavo F. Padovan BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); 43110a708f8fSGustavo F. Padovan 43120a708f8fSGustavo F. Padovan if (!conn->rx_len) { 43130a708f8fSGustavo F. Padovan BT_ERR("Unexpected continuation frame (len %d)", skb->len); 43140a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 43150a708f8fSGustavo F. Padovan goto drop; 43160a708f8fSGustavo F. Padovan } 43170a708f8fSGustavo F. Padovan 43180a708f8fSGustavo F. Padovan if (skb->len > conn->rx_len) { 43190a708f8fSGustavo F. Padovan BT_ERR("Fragment is too long (len %d, expected %d)", 43200a708f8fSGustavo F. Padovan skb->len, conn->rx_len); 43210a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 43220a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 43230a708f8fSGustavo F. Padovan conn->rx_len = 0; 43240a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 43250a708f8fSGustavo F. Padovan goto drop; 43260a708f8fSGustavo F. Padovan } 43270a708f8fSGustavo F. Padovan 43280a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 43290a708f8fSGustavo F. Padovan skb->len); 43300a708f8fSGustavo F. Padovan conn->rx_len -= skb->len; 43310a708f8fSGustavo F. Padovan 43320a708f8fSGustavo F. Padovan if (!conn->rx_len) { 43330a708f8fSGustavo F. Padovan /* Complete frame received */ 43340a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, conn->rx_skb); 43350a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 43360a708f8fSGustavo F. Padovan } 43370a708f8fSGustavo F. Padovan } 43380a708f8fSGustavo F. Padovan 43390a708f8fSGustavo F. Padovan drop: 43400a708f8fSGustavo F. Padovan kfree_skb(skb); 43410a708f8fSGustavo F. Padovan return 0; 43420a708f8fSGustavo F. Padovan } 43430a708f8fSGustavo F. Padovan 43440a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p) 43450a708f8fSGustavo F. Padovan { 434623691d75SGustavo F. Padovan struct l2cap_chan *c; 43470a708f8fSGustavo F. Padovan 434823691d75SGustavo F. Padovan read_lock_bh(&chan_list_lock); 43490a708f8fSGustavo F. Padovan 435023691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 435123691d75SGustavo F. Padovan struct sock *sk = c->sk; 43520a708f8fSGustavo F. Padovan 4353903d343eSGustavo F. Padovan seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", 43540a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->src), 43550a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->dst), 435623691d75SGustavo F. Padovan sk->sk_state, __le16_to_cpu(c->psm), 435723691d75SGustavo F. Padovan c->scid, c->dcid, c->imtu, c->omtu, 435823691d75SGustavo F. Padovan c->sec_level, c->mode); 43590a708f8fSGustavo F. Padovan } 43600a708f8fSGustavo F. Padovan 436123691d75SGustavo F. Padovan read_unlock_bh(&chan_list_lock); 43620a708f8fSGustavo F. Padovan 43630a708f8fSGustavo F. Padovan return 0; 43640a708f8fSGustavo F. Padovan } 43650a708f8fSGustavo F. Padovan 43660a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file) 43670a708f8fSGustavo F. Padovan { 43680a708f8fSGustavo F. Padovan return single_open(file, l2cap_debugfs_show, inode->i_private); 43690a708f8fSGustavo F. Padovan } 43700a708f8fSGustavo F. Padovan 43710a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = { 43720a708f8fSGustavo F. Padovan .open = l2cap_debugfs_open, 43730a708f8fSGustavo F. Padovan .read = seq_read, 43740a708f8fSGustavo F. Padovan .llseek = seq_lseek, 43750a708f8fSGustavo F. Padovan .release = single_release, 43760a708f8fSGustavo F. Padovan }; 43770a708f8fSGustavo F. Padovan 43780a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs; 43790a708f8fSGustavo F. Padovan 43800a708f8fSGustavo F. Padovan static struct hci_proto l2cap_hci_proto = { 43810a708f8fSGustavo F. Padovan .name = "L2CAP", 43820a708f8fSGustavo F. Padovan .id = HCI_PROTO_L2CAP, 43830a708f8fSGustavo F. Padovan .connect_ind = l2cap_connect_ind, 43840a708f8fSGustavo F. Padovan .connect_cfm = l2cap_connect_cfm, 43850a708f8fSGustavo F. Padovan .disconn_ind = l2cap_disconn_ind, 43860a708f8fSGustavo F. Padovan .disconn_cfm = l2cap_disconn_cfm, 43870a708f8fSGustavo F. Padovan .security_cfm = l2cap_security_cfm, 43880a708f8fSGustavo F. Padovan .recv_acldata = l2cap_recv_acldata 43890a708f8fSGustavo F. Padovan }; 43900a708f8fSGustavo F. Padovan 439164274518SGustavo F. Padovan int __init l2cap_init(void) 43920a708f8fSGustavo F. Padovan { 43930a708f8fSGustavo F. Padovan int err; 43940a708f8fSGustavo F. Padovan 4395bb58f747SGustavo F. Padovan err = l2cap_init_sockets(); 43960a708f8fSGustavo F. Padovan if (err < 0) 43970a708f8fSGustavo F. Padovan return err; 43980a708f8fSGustavo F. Padovan 43990a708f8fSGustavo F. Padovan _busy_wq = create_singlethread_workqueue("l2cap"); 44000a708f8fSGustavo F. Padovan if (!_busy_wq) { 4401bb58f747SGustavo F. Padovan err = -ENOMEM; 44020a708f8fSGustavo F. Padovan goto error; 44030a708f8fSGustavo F. Padovan } 44040a708f8fSGustavo F. Padovan 44050a708f8fSGustavo F. Padovan err = hci_register_proto(&l2cap_hci_proto); 44060a708f8fSGustavo F. Padovan if (err < 0) { 44070a708f8fSGustavo F. Padovan BT_ERR("L2CAP protocol registration failed"); 44080a708f8fSGustavo F. Padovan bt_sock_unregister(BTPROTO_L2CAP); 44090a708f8fSGustavo F. Padovan goto error; 44100a708f8fSGustavo F. Padovan } 44110a708f8fSGustavo F. Padovan 44120a708f8fSGustavo F. Padovan if (bt_debugfs) { 44130a708f8fSGustavo F. Padovan l2cap_debugfs = debugfs_create_file("l2cap", 0444, 44140a708f8fSGustavo F. Padovan bt_debugfs, NULL, &l2cap_debugfs_fops); 44150a708f8fSGustavo F. Padovan if (!l2cap_debugfs) 44160a708f8fSGustavo F. Padovan BT_ERR("Failed to create L2CAP debug file"); 44170a708f8fSGustavo F. Padovan } 44180a708f8fSGustavo F. Padovan 44190a708f8fSGustavo F. Padovan return 0; 44200a708f8fSGustavo F. Padovan 44210a708f8fSGustavo F. Padovan error: 44220a708f8fSGustavo F. Padovan destroy_workqueue(_busy_wq); 4423bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 44240a708f8fSGustavo F. Padovan return err; 44250a708f8fSGustavo F. Padovan } 44260a708f8fSGustavo F. Padovan 442764274518SGustavo F. Padovan void l2cap_exit(void) 44280a708f8fSGustavo F. Padovan { 44290a708f8fSGustavo F. Padovan debugfs_remove(l2cap_debugfs); 44300a708f8fSGustavo F. Padovan 44310a708f8fSGustavo F. Padovan flush_workqueue(_busy_wq); 44320a708f8fSGustavo F. Padovan destroy_workqueue(_busy_wq); 44330a708f8fSGustavo F. Padovan 44340a708f8fSGustavo F. Padovan if (hci_unregister_proto(&l2cap_hci_proto) < 0) 44350a708f8fSGustavo F. Padovan BT_ERR("L2CAP protocol unregistration failed"); 44360a708f8fSGustavo F. Padovan 4437bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 44380a708f8fSGustavo F. Padovan } 44390a708f8fSGustavo F. Padovan 44400a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644); 44410a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); 4442