10a708f8fSGustavo F. Padovan /* 20a708f8fSGustavo F. Padovan BlueZ - Bluetooth protocol stack for Linux 30a708f8fSGustavo F. Padovan Copyright (C) 2000-2001 Qualcomm Incorporated 40a708f8fSGustavo F. Padovan Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org> 50a708f8fSGustavo F. Padovan Copyright (C) 2010 Google Inc. 6590051deSGustavo F. Padovan Copyright (C) 2011 ProFUSION Embedded Systems 70a708f8fSGustavo F. Padovan 80a708f8fSGustavo F. Padovan Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 90a708f8fSGustavo F. Padovan 100a708f8fSGustavo F. Padovan This program is free software; you can redistribute it and/or modify 110a708f8fSGustavo F. Padovan it under the terms of the GNU General Public License version 2 as 120a708f8fSGustavo F. Padovan published by the Free Software Foundation; 130a708f8fSGustavo F. Padovan 140a708f8fSGustavo F. Padovan THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 150a708f8fSGustavo F. Padovan OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 160a708f8fSGustavo F. Padovan FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 170a708f8fSGustavo F. Padovan IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 180a708f8fSGustavo F. Padovan CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 190a708f8fSGustavo F. Padovan WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 200a708f8fSGustavo F. Padovan ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 210a708f8fSGustavo F. Padovan OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 220a708f8fSGustavo F. Padovan 230a708f8fSGustavo F. Padovan ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 240a708f8fSGustavo F. Padovan COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 250a708f8fSGustavo F. Padovan SOFTWARE IS DISCLAIMED. 260a708f8fSGustavo F. Padovan */ 270a708f8fSGustavo F. Padovan 28bb58f747SGustavo F. Padovan /* Bluetooth L2CAP core. */ 290a708f8fSGustavo F. Padovan 300a708f8fSGustavo F. Padovan #include <linux/module.h> 310a708f8fSGustavo F. Padovan 320a708f8fSGustavo F. Padovan #include <linux/types.h> 330a708f8fSGustavo F. Padovan #include <linux/capability.h> 340a708f8fSGustavo F. Padovan #include <linux/errno.h> 350a708f8fSGustavo F. Padovan #include <linux/kernel.h> 360a708f8fSGustavo F. Padovan #include <linux/sched.h> 370a708f8fSGustavo F. Padovan #include <linux/slab.h> 380a708f8fSGustavo F. Padovan #include <linux/poll.h> 390a708f8fSGustavo F. Padovan #include <linux/fcntl.h> 400a708f8fSGustavo F. Padovan #include <linux/init.h> 410a708f8fSGustavo F. Padovan #include <linux/interrupt.h> 420a708f8fSGustavo F. Padovan #include <linux/socket.h> 430a708f8fSGustavo F. Padovan #include <linux/skbuff.h> 440a708f8fSGustavo F. Padovan #include <linux/list.h> 450a708f8fSGustavo F. Padovan #include <linux/device.h> 460a708f8fSGustavo F. Padovan #include <linux/debugfs.h> 470a708f8fSGustavo F. Padovan #include <linux/seq_file.h> 480a708f8fSGustavo F. Padovan #include <linux/uaccess.h> 490a708f8fSGustavo F. Padovan #include <linux/crc16.h> 500a708f8fSGustavo F. Padovan #include <net/sock.h> 510a708f8fSGustavo F. Padovan 520a708f8fSGustavo F. Padovan #include <asm/system.h> 530a708f8fSGustavo F. Padovan #include <asm/unaligned.h> 540a708f8fSGustavo F. Padovan 550a708f8fSGustavo F. Padovan #include <net/bluetooth/bluetooth.h> 560a708f8fSGustavo F. Padovan #include <net/bluetooth/hci_core.h> 570a708f8fSGustavo F. Padovan #include <net/bluetooth/l2cap.h> 58b501d6a1SAnderson Briglia #include <net/bluetooth/smp.h> 590a708f8fSGustavo F. Padovan 60bb58f747SGustavo F. Padovan int disable_ertm; 610a708f8fSGustavo F. Padovan 620a708f8fSGustavo F. Padovan static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN; 6350a147cdSMat Martineau static u8 l2cap_fixed_chan[8] = { L2CAP_FC_L2CAP, }; 640a708f8fSGustavo F. Padovan 65b5ad8b7fSJohannes Berg static LIST_HEAD(chan_list); 66b5ad8b7fSJohannes Berg static DEFINE_RWLOCK(chan_list_lock); 670a708f8fSGustavo F. Padovan 680a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 690a708f8fSGustavo F. Padovan u8 code, u8 ident, u16 dlen, void *data); 704519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, 714519de9aSGustavo F. Padovan void *data); 72710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data); 734519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, 744519de9aSGustavo F. Padovan struct l2cap_chan *chan, int err); 750a708f8fSGustavo F. Padovan 760a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb); 770a708f8fSGustavo F. Padovan 780a708f8fSGustavo F. Padovan /* ---- L2CAP channels ---- */ 7971ba0e56SGustavo F. Padovan 80baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid) 810a708f8fSGustavo F. Padovan { 823d57dc68SGustavo F. Padovan struct l2cap_chan *c, *r = NULL; 83baa7e1faSGustavo F. Padovan 843d57dc68SGustavo F. Padovan rcu_read_lock(); 853d57dc68SGustavo F. Padovan 863d57dc68SGustavo F. Padovan list_for_each_entry_rcu(c, &conn->chan_l, list) { 873d57dc68SGustavo F. Padovan if (c->dcid == cid) { 883d57dc68SGustavo F. Padovan r = c; 893d57dc68SGustavo F. Padovan break; 900a708f8fSGustavo F. Padovan } 913d57dc68SGustavo F. Padovan } 923d57dc68SGustavo F. Padovan 933d57dc68SGustavo F. Padovan rcu_read_unlock(); 943d57dc68SGustavo F. Padovan return r; 95baa7e1faSGustavo F. Padovan } 960a708f8fSGustavo F. Padovan 97baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) 980a708f8fSGustavo F. Padovan { 993d57dc68SGustavo F. Padovan struct l2cap_chan *c, *r = NULL; 100baa7e1faSGustavo F. Padovan 1013d57dc68SGustavo F. Padovan rcu_read_lock(); 1023d57dc68SGustavo F. Padovan 1033d57dc68SGustavo F. Padovan list_for_each_entry_rcu(c, &conn->chan_l, list) { 1043d57dc68SGustavo F. Padovan if (c->scid == cid) { 1053d57dc68SGustavo F. Padovan r = c; 1063d57dc68SGustavo F. Padovan break; 1070a708f8fSGustavo F. Padovan } 1083d57dc68SGustavo F. Padovan } 1093d57dc68SGustavo F. Padovan 1103d57dc68SGustavo F. Padovan rcu_read_unlock(); 1113d57dc68SGustavo F. Padovan return r; 112baa7e1faSGustavo F. Padovan } 1130a708f8fSGustavo F. Padovan 1140a708f8fSGustavo F. Padovan /* Find channel with given SCID. 1150a708f8fSGustavo F. Padovan * Returns locked socket */ 116baa7e1faSGustavo F. Padovan static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) 1170a708f8fSGustavo F. Padovan { 11848454079SGustavo F. Padovan struct l2cap_chan *c; 119baa7e1faSGustavo F. Padovan 120baa7e1faSGustavo F. Padovan c = __l2cap_get_chan_by_scid(conn, cid); 12148454079SGustavo F. Padovan if (c) 122aa2ac881SGustavo F. Padovan lock_sock(c->sk); 12348454079SGustavo F. Padovan return c; 1240a708f8fSGustavo F. Padovan } 1250a708f8fSGustavo F. Padovan 126baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) 1270a708f8fSGustavo F. Padovan { 1283d57dc68SGustavo F. Padovan struct l2cap_chan *c, *r = NULL; 129baa7e1faSGustavo F. Padovan 1303d57dc68SGustavo F. Padovan rcu_read_lock(); 1313d57dc68SGustavo F. Padovan 1323d57dc68SGustavo F. Padovan list_for_each_entry_rcu(c, &conn->chan_l, list) { 1333d57dc68SGustavo F. Padovan if (c->ident == ident) { 1343d57dc68SGustavo F. Padovan r = c; 1353d57dc68SGustavo F. Padovan break; 1360a708f8fSGustavo F. Padovan } 1373d57dc68SGustavo F. Padovan } 1383d57dc68SGustavo F. Padovan 1393d57dc68SGustavo F. Padovan rcu_read_unlock(); 1403d57dc68SGustavo F. Padovan return r; 141baa7e1faSGustavo F. Padovan } 1420a708f8fSGustavo F. Padovan 143baa7e1faSGustavo F. Padovan static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) 1440a708f8fSGustavo F. Padovan { 14548454079SGustavo F. Padovan struct l2cap_chan *c; 146baa7e1faSGustavo F. Padovan 147baa7e1faSGustavo F. Padovan c = __l2cap_get_chan_by_ident(conn, ident); 14848454079SGustavo F. Padovan if (c) 149aa2ac881SGustavo F. Padovan lock_sock(c->sk); 15048454079SGustavo F. Padovan return c; 1510a708f8fSGustavo F. Padovan } 1520a708f8fSGustavo F. Padovan 15323691d75SGustavo F. Padovan static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src) 1549e4425ffSGustavo F. Padovan { 15523691d75SGustavo F. Padovan struct l2cap_chan *c; 1569e4425ffSGustavo F. Padovan 15723691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 15823691d75SGustavo F. Padovan if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src)) 15923691d75SGustavo F. Padovan return c; 1609e4425ffSGustavo F. Padovan } 161250938cbSSzymon Janc return NULL; 162250938cbSSzymon Janc } 1639e4425ffSGustavo F. Padovan 1649e4425ffSGustavo F. Padovan int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) 1659e4425ffSGustavo F. Padovan { 16673b2ec18SGustavo F. Padovan int err; 16773b2ec18SGustavo F. Padovan 16823691d75SGustavo F. Padovan write_lock_bh(&chan_list_lock); 1699e4425ffSGustavo F. Padovan 17023691d75SGustavo F. Padovan if (psm && __l2cap_global_chan_by_addr(psm, src)) { 17173b2ec18SGustavo F. Padovan err = -EADDRINUSE; 17273b2ec18SGustavo F. Padovan goto done; 1739e4425ffSGustavo F. Padovan } 1749e4425ffSGustavo F. Padovan 17573b2ec18SGustavo F. Padovan if (psm) { 1769e4425ffSGustavo F. Padovan chan->psm = psm; 1779e4425ffSGustavo F. Padovan chan->sport = psm; 17873b2ec18SGustavo F. Padovan err = 0; 17973b2ec18SGustavo F. Padovan } else { 18073b2ec18SGustavo F. Padovan u16 p; 1819e4425ffSGustavo F. Padovan 18273b2ec18SGustavo F. Padovan err = -EINVAL; 18373b2ec18SGustavo F. Padovan for (p = 0x1001; p < 0x1100; p += 2) 18423691d75SGustavo F. Padovan if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { 18573b2ec18SGustavo F. Padovan chan->psm = cpu_to_le16(p); 18673b2ec18SGustavo F. Padovan chan->sport = cpu_to_le16(p); 18773b2ec18SGustavo F. Padovan err = 0; 18873b2ec18SGustavo F. Padovan break; 18973b2ec18SGustavo F. Padovan } 19073b2ec18SGustavo F. Padovan } 19173b2ec18SGustavo F. Padovan 19273b2ec18SGustavo F. Padovan done: 19323691d75SGustavo F. Padovan write_unlock_bh(&chan_list_lock); 19473b2ec18SGustavo F. Padovan return err; 1959e4425ffSGustavo F. Padovan } 1969e4425ffSGustavo F. Padovan 1979e4425ffSGustavo F. Padovan int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) 1989e4425ffSGustavo F. Padovan { 19923691d75SGustavo F. Padovan write_lock_bh(&chan_list_lock); 2009e4425ffSGustavo F. Padovan 2019e4425ffSGustavo F. Padovan chan->scid = scid; 2029e4425ffSGustavo F. Padovan 20323691d75SGustavo F. Padovan write_unlock_bh(&chan_list_lock); 2049e4425ffSGustavo F. Padovan 2059e4425ffSGustavo F. Padovan return 0; 2069e4425ffSGustavo F. Padovan } 2079e4425ffSGustavo F. Padovan 208baa7e1faSGustavo F. Padovan static u16 l2cap_alloc_cid(struct l2cap_conn *conn) 2090a708f8fSGustavo F. Padovan { 2100a708f8fSGustavo F. Padovan u16 cid = L2CAP_CID_DYN_START; 2110a708f8fSGustavo F. Padovan 2120a708f8fSGustavo F. Padovan for (; cid < L2CAP_CID_DYN_END; cid++) { 213baa7e1faSGustavo F. Padovan if (!__l2cap_get_chan_by_scid(conn, cid)) 2140a708f8fSGustavo F. Padovan return cid; 2150a708f8fSGustavo F. Padovan } 2160a708f8fSGustavo F. Padovan 2170a708f8fSGustavo F. Padovan return 0; 2180a708f8fSGustavo F. Padovan } 2190a708f8fSGustavo F. Padovan 220badaaa00SGustavo F. Padovan static char *state_to_string(int state) 221badaaa00SGustavo F. Padovan { 222badaaa00SGustavo F. Padovan switch(state) { 223badaaa00SGustavo F. Padovan case BT_CONNECTED: 224badaaa00SGustavo F. Padovan return "BT_CONNECTED"; 225badaaa00SGustavo F. Padovan case BT_OPEN: 226badaaa00SGustavo F. Padovan return "BT_OPEN"; 227badaaa00SGustavo F. Padovan case BT_BOUND: 228badaaa00SGustavo F. Padovan return "BT_BOUND"; 229badaaa00SGustavo F. Padovan case BT_LISTEN: 230badaaa00SGustavo F. Padovan return "BT_LISTEN"; 231badaaa00SGustavo F. Padovan case BT_CONNECT: 232badaaa00SGustavo F. Padovan return "BT_CONNECT"; 233badaaa00SGustavo F. Padovan case BT_CONNECT2: 234badaaa00SGustavo F. Padovan return "BT_CONNECT2"; 235badaaa00SGustavo F. Padovan case BT_CONFIG: 236badaaa00SGustavo F. Padovan return "BT_CONFIG"; 237badaaa00SGustavo F. Padovan case BT_DISCONN: 238badaaa00SGustavo F. Padovan return "BT_DISCONN"; 239badaaa00SGustavo F. Padovan case BT_CLOSED: 240badaaa00SGustavo F. Padovan return "BT_CLOSED"; 241badaaa00SGustavo F. Padovan } 242badaaa00SGustavo F. Padovan 243badaaa00SGustavo F. Padovan return "invalid state"; 244badaaa00SGustavo F. Padovan } 245badaaa00SGustavo F. Padovan 24689bc500eSGustavo F. Padovan static void l2cap_state_change(struct l2cap_chan *chan, int state) 24789bc500eSGustavo F. Padovan { 248badaaa00SGustavo F. Padovan BT_DBG("%p %s -> %s", chan, state_to_string(chan->state), 249badaaa00SGustavo F. Padovan state_to_string(state)); 250badaaa00SGustavo F. Padovan 25189bc500eSGustavo F. Padovan chan->state = state; 25289bc500eSGustavo F. Padovan chan->ops->state_change(chan->data, state); 25389bc500eSGustavo F. Padovan } 25489bc500eSGustavo F. Padovan 255721c4181SGustavo F. Padovan static void l2cap_chan_timeout(struct work_struct *work) 256ab07801dSGustavo F. Padovan { 257721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 258721c4181SGustavo F. Padovan chan_timer.work); 259ab07801dSGustavo F. Padovan struct sock *sk = chan->sk; 260ab07801dSGustavo F. Padovan int reason; 261ab07801dSGustavo F. Padovan 26289bc500eSGustavo F. Padovan BT_DBG("chan %p state %d", chan, chan->state); 263ab07801dSGustavo F. Padovan 264721c4181SGustavo F. Padovan lock_sock(sk); 265ab07801dSGustavo F. Padovan 26689bc500eSGustavo F. Padovan if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) 267ab07801dSGustavo F. Padovan reason = ECONNREFUSED; 26889bc500eSGustavo F. Padovan else if (chan->state == BT_CONNECT && 269ab07801dSGustavo F. Padovan chan->sec_level != BT_SECURITY_SDP) 270ab07801dSGustavo F. Padovan reason = ECONNREFUSED; 271ab07801dSGustavo F. Padovan else 272ab07801dSGustavo F. Padovan reason = ETIMEDOUT; 273ab07801dSGustavo F. Padovan 2740f852724SGustavo F. Padovan l2cap_chan_close(chan, reason); 275ab07801dSGustavo F. Padovan 276721c4181SGustavo F. Padovan release_sock(sk); 277ab07801dSGustavo F. Padovan 278ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 279371fd835SUlisses Furquim l2cap_chan_put(chan); 280ab07801dSGustavo F. Padovan } 281ab07801dSGustavo F. Padovan 28223691d75SGustavo F. Padovan struct l2cap_chan *l2cap_chan_create(struct sock *sk) 2830a708f8fSGustavo F. Padovan { 28448454079SGustavo F. Padovan struct l2cap_chan *chan; 2850a708f8fSGustavo F. Padovan 28648454079SGustavo F. Padovan chan = kzalloc(sizeof(*chan), GFP_ATOMIC); 28748454079SGustavo F. Padovan if (!chan) 28848454079SGustavo F. Padovan return NULL; 2890a708f8fSGustavo F. Padovan 29048454079SGustavo F. Padovan chan->sk = sk; 29148454079SGustavo F. Padovan 29223691d75SGustavo F. Padovan write_lock_bh(&chan_list_lock); 29323691d75SGustavo F. Padovan list_add(&chan->global_l, &chan_list); 29423691d75SGustavo F. Padovan write_unlock_bh(&chan_list_lock); 29523691d75SGustavo F. Padovan 296721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); 297ab07801dSGustavo F. Padovan 29889bc500eSGustavo F. Padovan chan->state = BT_OPEN; 29989bc500eSGustavo F. Padovan 30071ba0e56SGustavo F. Padovan atomic_set(&chan->refcnt, 1); 30171ba0e56SGustavo F. Padovan 302abc545b8SSzymon Janc BT_DBG("sk %p chan %p", sk, chan); 303abc545b8SSzymon Janc 30448454079SGustavo F. Padovan return chan; 3050a708f8fSGustavo F. Padovan } 3060a708f8fSGustavo F. Padovan 30723691d75SGustavo F. Padovan void l2cap_chan_destroy(struct l2cap_chan *chan) 3086ff5abbfSGustavo F. Padovan { 30923691d75SGustavo F. Padovan write_lock_bh(&chan_list_lock); 31023691d75SGustavo F. Padovan list_del(&chan->global_l); 31123691d75SGustavo F. Padovan write_unlock_bh(&chan_list_lock); 31223691d75SGustavo F. Padovan 313371fd835SUlisses Furquim l2cap_chan_put(chan); 3146ff5abbfSGustavo F. Padovan } 3156ff5abbfSGustavo F. Padovan 3163d57dc68SGustavo F. Padovan static void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 3170a708f8fSGustavo F. Padovan { 3180a708f8fSGustavo F. Padovan BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, 319fe4128e0SGustavo F. Padovan chan->psm, chan->dcid); 3200a708f8fSGustavo F. Padovan 3219f5a0d7bSAndrei Emeltchenko conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; 3220a708f8fSGustavo F. Padovan 3238c1d787bSGustavo F. Padovan chan->conn = conn; 3240a708f8fSGustavo F. Padovan 325715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { 326b62f328bSVille Tervo if (conn->hcon->type == LE_LINK) { 327b62f328bSVille Tervo /* LE connection */ 3280c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_LE_DEFAULT_MTU; 329fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_LE_DATA; 330fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_LE_DATA; 331b62f328bSVille Tervo } else { 3320a708f8fSGustavo F. Padovan /* Alloc CID for connection-oriented socket */ 333fe4128e0SGustavo F. Padovan chan->scid = l2cap_alloc_cid(conn); 3340c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 335b62f328bSVille Tervo } 336715ec005SGustavo F. Padovan } else if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { 3370a708f8fSGustavo F. Padovan /* Connectionless socket */ 338fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_CONN_LESS; 339fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_CONN_LESS; 3400c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 3410a708f8fSGustavo F. Padovan } else { 3420a708f8fSGustavo F. Padovan /* Raw socket can send/recv signalling messages only */ 343fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_SIGNALING; 344fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_SIGNALING; 3450c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 3460a708f8fSGustavo F. Padovan } 3470a708f8fSGustavo F. Padovan 3488f7975b1SAndrei Emeltchenko chan->local_id = L2CAP_BESTEFFORT_ID; 3498f7975b1SAndrei Emeltchenko chan->local_stype = L2CAP_SERV_BESTEFFORT; 3508f7975b1SAndrei Emeltchenko chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; 3518f7975b1SAndrei Emeltchenko chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; 3528f7975b1SAndrei Emeltchenko chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; 3538f7975b1SAndrei Emeltchenko chan->local_flush_to = L2CAP_DEFAULT_FLUSH_TO; 3548f7975b1SAndrei Emeltchenko 355371fd835SUlisses Furquim l2cap_chan_hold(chan); 356baa7e1faSGustavo F. Padovan 3573d57dc68SGustavo F. Padovan list_add_rcu(&chan->list, &conn->chan_l); 3580a708f8fSGustavo F. Padovan } 3590a708f8fSGustavo F. Padovan 3600a708f8fSGustavo F. Padovan /* Delete channel. 3610a708f8fSGustavo F. Padovan * Must be called on the locked socket. */ 3624519de9aSGustavo F. Padovan static void l2cap_chan_del(struct l2cap_chan *chan, int err) 3630a708f8fSGustavo F. Padovan { 36448454079SGustavo F. Padovan struct sock *sk = chan->sk; 3658c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 3660a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 3670a708f8fSGustavo F. Padovan 368c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 3690a708f8fSGustavo F. Padovan 37049208c9cSGustavo F. Padovan BT_DBG("chan %p, conn %p, err %d", chan, conn, err); 3710a708f8fSGustavo F. Padovan 3720a708f8fSGustavo F. Padovan if (conn) { 373baa7e1faSGustavo F. Padovan /* Delete from channel list */ 3743d57dc68SGustavo F. Padovan list_del_rcu(&chan->list); 3753d57dc68SGustavo F. Padovan synchronize_rcu(); 3763d57dc68SGustavo F. Padovan 377371fd835SUlisses Furquim l2cap_chan_put(chan); 378baa7e1faSGustavo F. Padovan 3798c1d787bSGustavo F. Padovan chan->conn = NULL; 3800a708f8fSGustavo F. Padovan hci_conn_put(conn->hcon); 3810a708f8fSGustavo F. Padovan } 3820a708f8fSGustavo F. Padovan 38389bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CLOSED); 3840a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 3850a708f8fSGustavo F. Padovan 3860a708f8fSGustavo F. Padovan if (err) 3870a708f8fSGustavo F. Padovan sk->sk_err = err; 3880a708f8fSGustavo F. Padovan 3890a708f8fSGustavo F. Padovan if (parent) { 3900a708f8fSGustavo F. Padovan bt_accept_unlink(sk); 3910a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 3920a708f8fSGustavo F. Padovan } else 3930a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 3940a708f8fSGustavo F. Padovan 395c1360a1cSGustavo F. Padovan if (!(test_bit(CONF_OUTPUT_DONE, &chan->conf_state) && 396c1360a1cSGustavo F. Padovan test_bit(CONF_INPUT_DONE, &chan->conf_state))) 3976ff5abbfSGustavo F. Padovan return; 3982ead70b8SGustavo F. Padovan 39958d35f87SGustavo F. Padovan skb_queue_purge(&chan->tx_q); 4000a708f8fSGustavo F. Padovan 4010c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 4020a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 4030a708f8fSGustavo F. Padovan 4041a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 4051a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 4061a09bcb9SGustavo F. Padovan __clear_ack_timer(chan); 4070a708f8fSGustavo F. Padovan 408f1c6775bSGustavo F. Padovan skb_queue_purge(&chan->srej_q); 4090a708f8fSGustavo F. Padovan 41039d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 4110a708f8fSGustavo F. Padovan list_del(&l->list); 4120a708f8fSGustavo F. Padovan kfree(l); 4130a708f8fSGustavo F. Padovan } 4140a708f8fSGustavo F. Padovan } 4150a708f8fSGustavo F. Padovan } 4160a708f8fSGustavo F. Padovan 4174519de9aSGustavo F. Padovan static void l2cap_chan_cleanup_listen(struct sock *parent) 4184519de9aSGustavo F. Padovan { 4194519de9aSGustavo F. Padovan struct sock *sk; 4204519de9aSGustavo F. Padovan 4214519de9aSGustavo F. Padovan BT_DBG("parent %p", parent); 4224519de9aSGustavo F. Padovan 4234519de9aSGustavo F. Padovan /* Close not yet accepted channels */ 4240f852724SGustavo F. Padovan while ((sk = bt_accept_dequeue(parent, NULL))) { 425ba3bd0eeSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 426c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 4270f852724SGustavo F. Padovan lock_sock(sk); 428ba3bd0eeSGustavo F. Padovan l2cap_chan_close(chan, ECONNRESET); 4290f852724SGustavo F. Padovan release_sock(sk); 430ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 4310f852724SGustavo F. Padovan } 4324519de9aSGustavo F. Padovan } 4334519de9aSGustavo F. Padovan 4340f852724SGustavo F. Padovan void l2cap_chan_close(struct l2cap_chan *chan, int reason) 4354519de9aSGustavo F. Padovan { 4364519de9aSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 4374519de9aSGustavo F. Padovan struct sock *sk = chan->sk; 4384519de9aSGustavo F. Padovan 43989bc500eSGustavo F. Padovan BT_DBG("chan %p state %d socket %p", chan, chan->state, sk->sk_socket); 4404519de9aSGustavo F. Padovan 44189bc500eSGustavo F. Padovan switch (chan->state) { 4424519de9aSGustavo F. Padovan case BT_LISTEN: 4434519de9aSGustavo F. Padovan l2cap_chan_cleanup_listen(sk); 44489bc500eSGustavo F. Padovan 44589bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CLOSED); 44689bc500eSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 4474519de9aSGustavo F. Padovan break; 4484519de9aSGustavo F. Padovan 4494519de9aSGustavo F. Padovan case BT_CONNECTED: 4504519de9aSGustavo F. Padovan case BT_CONFIG: 451715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 4524519de9aSGustavo F. Padovan conn->hcon->type == ACL_LINK) { 453c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 454c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 4554519de9aSGustavo F. Padovan l2cap_send_disconn_req(conn, chan, reason); 4564519de9aSGustavo F. Padovan } else 4574519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4584519de9aSGustavo F. Padovan break; 4594519de9aSGustavo F. Padovan 4604519de9aSGustavo F. Padovan case BT_CONNECT2: 461715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 4624519de9aSGustavo F. Padovan conn->hcon->type == ACL_LINK) { 4634519de9aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 4644519de9aSGustavo F. Padovan __u16 result; 4654519de9aSGustavo F. Padovan 4664519de9aSGustavo F. Padovan if (bt_sk(sk)->defer_setup) 4674519de9aSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 4684519de9aSGustavo F. Padovan else 4694519de9aSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 47089bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_DISCONN); 4714519de9aSGustavo F. Padovan 4724519de9aSGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4734519de9aSGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 4744519de9aSGustavo F. Padovan rsp.result = cpu_to_le16(result); 4754519de9aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 4764519de9aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4774519de9aSGustavo F. Padovan sizeof(rsp), &rsp); 4784519de9aSGustavo F. Padovan } 4794519de9aSGustavo F. Padovan 4804519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4814519de9aSGustavo F. Padovan break; 4824519de9aSGustavo F. Padovan 4834519de9aSGustavo F. Padovan case BT_CONNECT: 4844519de9aSGustavo F. Padovan case BT_DISCONN: 4854519de9aSGustavo F. Padovan l2cap_chan_del(chan, reason); 4864519de9aSGustavo F. Padovan break; 4874519de9aSGustavo F. Padovan 4884519de9aSGustavo F. Padovan default: 4894519de9aSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 4904519de9aSGustavo F. Padovan break; 4914519de9aSGustavo F. Padovan } 4924519de9aSGustavo F. Padovan } 4934519de9aSGustavo F. Padovan 4944343478fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) 4950a708f8fSGustavo F. Padovan { 496715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_RAW) { 4974343478fSGustavo F. Padovan switch (chan->sec_level) { 4980a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 4990a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING_MITM; 5000a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 5010a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING; 5020a708f8fSGustavo F. Padovan default: 5030a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 5040a708f8fSGustavo F. Padovan } 505fe4128e0SGustavo F. Padovan } else if (chan->psm == cpu_to_le16(0x0001)) { 5064343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_LOW) 5074343478fSGustavo F. Padovan chan->sec_level = BT_SECURITY_SDP; 5080a708f8fSGustavo F. Padovan 5094343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_HIGH) 5100a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING_MITM; 5110a708f8fSGustavo F. Padovan else 5120a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 5130a708f8fSGustavo F. Padovan } else { 5144343478fSGustavo F. Padovan switch (chan->sec_level) { 5150a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 5160a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING_MITM; 5170a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 5180a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING; 5190a708f8fSGustavo F. Padovan default: 5200a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 5210a708f8fSGustavo F. Padovan } 5220a708f8fSGustavo F. Padovan } 5230a708f8fSGustavo F. Padovan } 5240a708f8fSGustavo F. Padovan 5250a708f8fSGustavo F. Padovan /* Service level security */ 526d45fc423SGustavo F. Padovan int l2cap_chan_check_security(struct l2cap_chan *chan) 5270a708f8fSGustavo F. Padovan { 5288c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 5290a708f8fSGustavo F. Padovan __u8 auth_type; 5300a708f8fSGustavo F. Padovan 5314343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 5320a708f8fSGustavo F. Padovan 5334343478fSGustavo F. Padovan return hci_conn_security(conn->hcon, chan->sec_level, auth_type); 5340a708f8fSGustavo F. Padovan } 5350a708f8fSGustavo F. Padovan 536b5ad8b7fSJohannes Berg static u8 l2cap_get_ident(struct l2cap_conn *conn) 5370a708f8fSGustavo F. Padovan { 5380a708f8fSGustavo F. Padovan u8 id; 5390a708f8fSGustavo F. Padovan 5400a708f8fSGustavo F. Padovan /* Get next available identificator. 5410a708f8fSGustavo F. Padovan * 1 - 128 are used by kernel. 5420a708f8fSGustavo F. Padovan * 129 - 199 are reserved. 5430a708f8fSGustavo F. Padovan * 200 - 254 are used by utilities like l2ping, etc. 5440a708f8fSGustavo F. Padovan */ 5450a708f8fSGustavo F. Padovan 5460a708f8fSGustavo F. Padovan spin_lock_bh(&conn->lock); 5470a708f8fSGustavo F. Padovan 5480a708f8fSGustavo F. Padovan if (++conn->tx_ident > 128) 5490a708f8fSGustavo F. Padovan conn->tx_ident = 1; 5500a708f8fSGustavo F. Padovan 5510a708f8fSGustavo F. Padovan id = conn->tx_ident; 5520a708f8fSGustavo F. Padovan 5530a708f8fSGustavo F. Padovan spin_unlock_bh(&conn->lock); 5540a708f8fSGustavo F. Padovan 5550a708f8fSGustavo F. Padovan return id; 5560a708f8fSGustavo F. Padovan } 5570a708f8fSGustavo F. Padovan 5584519de9aSGustavo F. Padovan static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data) 5590a708f8fSGustavo F. Padovan { 5600a708f8fSGustavo F. Padovan struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); 5610a708f8fSGustavo F. Padovan u8 flags; 5620a708f8fSGustavo F. Padovan 5630a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x", code); 5640a708f8fSGustavo F. Padovan 5650a708f8fSGustavo F. Padovan if (!skb) 5660a708f8fSGustavo F. Padovan return; 5670a708f8fSGustavo F. Padovan 5680a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 5690a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 5700a708f8fSGustavo F. Padovan else 5710a708f8fSGustavo F. Padovan flags = ACL_START; 5720a708f8fSGustavo F. Padovan 57314b12d0bSJaikumar Ganesh bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; 5745e59b791SLuiz Augusto von Dentz skb->priority = HCI_PRIO_MAX; 57514b12d0bSJaikumar Ganesh 57673d80debSLuiz Augusto von Dentz hci_send_acl(conn->hchan, skb, flags); 5770a708f8fSGustavo F. Padovan } 5780a708f8fSGustavo F. Padovan 57973d80debSLuiz Augusto von Dentz static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) 58073d80debSLuiz Augusto von Dentz { 58173d80debSLuiz Augusto von Dentz struct hci_conn *hcon = chan->conn->hcon; 58273d80debSLuiz Augusto von Dentz u16 flags; 58373d80debSLuiz Augusto von Dentz 58473d80debSLuiz Augusto von Dentz BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len, 58573d80debSLuiz Augusto von Dentz skb->priority); 58673d80debSLuiz Augusto von Dentz 58773d80debSLuiz Augusto von Dentz if (!test_bit(FLAG_FLUSHABLE, &chan->flags) && 58873d80debSLuiz Augusto von Dentz lmp_no_flush_capable(hcon->hdev)) 58973d80debSLuiz Augusto von Dentz flags = ACL_START_NO_FLUSH; 59073d80debSLuiz Augusto von Dentz else 59173d80debSLuiz Augusto von Dentz flags = ACL_START; 59273d80debSLuiz Augusto von Dentz 59373d80debSLuiz Augusto von Dentz bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); 59473d80debSLuiz Augusto von Dentz hci_send_acl(chan->conn->hchan, skb, flags); 5950a708f8fSGustavo F. Padovan } 5960a708f8fSGustavo F. Padovan 59788843ab0SAndrei Emeltchenko static inline void l2cap_send_sframe(struct l2cap_chan *chan, u32 control) 5980a708f8fSGustavo F. Padovan { 5990a708f8fSGustavo F. Padovan struct sk_buff *skb; 6000a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 6018c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 602e4ca6d98SAndrei Emeltchenko int count, hlen; 6030a708f8fSGustavo F. Padovan 60489bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 6050a708f8fSGustavo F. Padovan return; 6060a708f8fSGustavo F. Padovan 607e4ca6d98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 608e4ca6d98SAndrei Emeltchenko hlen = L2CAP_EXT_HDR_SIZE; 609e4ca6d98SAndrei Emeltchenko else 610e4ca6d98SAndrei Emeltchenko hlen = L2CAP_ENH_HDR_SIZE; 6110a708f8fSGustavo F. Padovan 6120a708f8fSGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 61303a51213SAndrei Emeltchenko hlen += L2CAP_FCS_SIZE; 6140a708f8fSGustavo F. Padovan 61588843ab0SAndrei Emeltchenko BT_DBG("chan %p, control 0x%8.8x", chan, control); 6160a708f8fSGustavo F. Padovan 6170a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, hlen); 618793c2f1cSAndrei Emeltchenko 619793c2f1cSAndrei Emeltchenko control |= __set_sframe(chan); 6200a708f8fSGustavo F. Padovan 621e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 62203f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 6230a708f8fSGustavo F. Padovan 624e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_PBIT, &chan->conn_state)) 625e3781735SAndrei Emeltchenko control |= __set_ctrl_poll(chan); 6260a708f8fSGustavo F. Padovan 6270a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 6280a708f8fSGustavo F. Padovan if (!skb) 6290a708f8fSGustavo F. Padovan return; 6300a708f8fSGustavo F. Padovan 6310a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 6320a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); 633fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 63488843ab0SAndrei Emeltchenko 63588843ab0SAndrei Emeltchenko __put_control(chan, control, skb_put(skb, __ctrl_size(chan))); 6360a708f8fSGustavo F. Padovan 63747d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 63803a51213SAndrei Emeltchenko u16 fcs = crc16(0, (u8 *)lh, count - L2CAP_FCS_SIZE); 63903a51213SAndrei Emeltchenko put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); 6400a708f8fSGustavo F. Padovan } 6410a708f8fSGustavo F. Padovan 64273d80debSLuiz Augusto von Dentz skb->priority = HCI_PRIO_MAX; 64373d80debSLuiz Augusto von Dentz l2cap_do_send(chan, skb); 6440a708f8fSGustavo F. Padovan } 6450a708f8fSGustavo F. Padovan 64688843ab0SAndrei Emeltchenko static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u32 control) 6470a708f8fSGustavo F. Padovan { 648e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 649ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 650e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 6510a708f8fSGustavo F. Padovan } else 652ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 6530a708f8fSGustavo F. Padovan 6540b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 6550a708f8fSGustavo F. Padovan 656525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 6570a708f8fSGustavo F. Padovan } 6580a708f8fSGustavo F. Padovan 659b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) 6600a708f8fSGustavo F. Padovan { 661c1360a1cSGustavo F. Padovan return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); 6620a708f8fSGustavo F. Padovan } 6630a708f8fSGustavo F. Padovan 664fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan) 6650a708f8fSGustavo F. Padovan { 6668c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 6670a708f8fSGustavo F. Padovan 6680a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { 6690a708f8fSGustavo F. Padovan if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) 6700a708f8fSGustavo F. Padovan return; 6710a708f8fSGustavo F. Padovan 672d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan) && 6734343478fSGustavo F. Padovan __l2cap_no_conn_pending(chan)) { 6740a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 675fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 676fe4128e0SGustavo F. Padovan req.psm = chan->psm; 6770a708f8fSGustavo F. Padovan 678fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 679c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 6800a708f8fSGustavo F. Padovan 681fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 682fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 6830a708f8fSGustavo F. Padovan } 6840a708f8fSGustavo F. Padovan } else { 6850a708f8fSGustavo F. Padovan struct l2cap_info_req req; 6860a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 6870a708f8fSGustavo F. Padovan 6880a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 6890a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 6900a708f8fSGustavo F. Padovan 691030013d8SGustavo F. Padovan schedule_delayed_work(&conn->info_timer, 6920a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 6930a708f8fSGustavo F. Padovan 6940a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 6950a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 6960a708f8fSGustavo F. Padovan } 6970a708f8fSGustavo F. Padovan } 6980a708f8fSGustavo F. Padovan 6990a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) 7000a708f8fSGustavo F. Padovan { 7010a708f8fSGustavo F. Padovan u32 local_feat_mask = l2cap_feat_mask; 7020a708f8fSGustavo F. Padovan if (!disable_ertm) 7030a708f8fSGustavo F. Padovan local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; 7040a708f8fSGustavo F. Padovan 7050a708f8fSGustavo F. Padovan switch (mode) { 7060a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 7070a708f8fSGustavo F. Padovan return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; 7080a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 7090a708f8fSGustavo F. Padovan return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; 7100a708f8fSGustavo F. Padovan default: 7110a708f8fSGustavo F. Padovan return 0x00; 7120a708f8fSGustavo F. Padovan } 7130a708f8fSGustavo F. Padovan } 7140a708f8fSGustavo F. Padovan 7154519de9aSGustavo F. Padovan static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err) 7160a708f8fSGustavo F. Padovan { 717e92c8e70SGustavo F. Padovan struct sock *sk; 7180a708f8fSGustavo F. Padovan struct l2cap_disconn_req req; 7190a708f8fSGustavo F. Padovan 7200a708f8fSGustavo F. Padovan if (!conn) 7210a708f8fSGustavo F. Padovan return; 7220a708f8fSGustavo F. Padovan 723e92c8e70SGustavo F. Padovan sk = chan->sk; 724e92c8e70SGustavo F. Padovan 7250c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 7261a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 7271a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 7281a09bcb9SGustavo F. Padovan __clear_ack_timer(chan); 7290a708f8fSGustavo F. Padovan } 7300a708f8fSGustavo F. Padovan 731fe4128e0SGustavo F. Padovan req.dcid = cpu_to_le16(chan->dcid); 732fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 7330a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 7340a708f8fSGustavo F. Padovan L2CAP_DISCONN_REQ, sizeof(req), &req); 7350a708f8fSGustavo F. Padovan 73689bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_DISCONN); 7370a708f8fSGustavo F. Padovan sk->sk_err = err; 7380a708f8fSGustavo F. Padovan } 7390a708f8fSGustavo F. Padovan 7400a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */ 7410a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn) 7420a708f8fSGustavo F. Padovan { 7433d57dc68SGustavo F. Padovan struct l2cap_chan *chan; 7440a708f8fSGustavo F. Padovan 7450a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 7460a708f8fSGustavo F. Padovan 7473d57dc68SGustavo F. Padovan rcu_read_lock(); 7480a708f8fSGustavo F. Padovan 7493d57dc68SGustavo F. Padovan list_for_each_entry_rcu(chan, &conn->chan_l, list) { 75048454079SGustavo F. Padovan struct sock *sk = chan->sk; 751baa7e1faSGustavo F. Padovan 7520a708f8fSGustavo F. Padovan bh_lock_sock(sk); 7530a708f8fSGustavo F. Padovan 754715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 7550a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7560a708f8fSGustavo F. Padovan continue; 7570a708f8fSGustavo F. Padovan } 7580a708f8fSGustavo F. Padovan 75989bc500eSGustavo F. Padovan if (chan->state == BT_CONNECT) { 7600a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 7610a708f8fSGustavo F. Padovan 762d45fc423SGustavo F. Padovan if (!l2cap_chan_check_security(chan) || 763b4450035SGustavo F. Padovan !__l2cap_no_conn_pending(chan)) { 7640a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7650a708f8fSGustavo F. Padovan continue; 7660a708f8fSGustavo F. Padovan } 7670a708f8fSGustavo F. Padovan 768c1360a1cSGustavo F. Padovan if (!l2cap_mode_supported(chan->mode, conn->feat_mask) 769c1360a1cSGustavo F. Padovan && test_bit(CONF_STATE2_DEVICE, 770c1360a1cSGustavo F. Padovan &chan->conf_state)) { 7710f852724SGustavo F. Padovan /* l2cap_chan_close() calls list_del(chan) 772820ffdb3SGustavo F. Padovan * so release the lock */ 7730f852724SGustavo F. Padovan l2cap_chan_close(chan, ECONNRESET); 7740a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7750a708f8fSGustavo F. Padovan continue; 7760a708f8fSGustavo F. Padovan } 7770a708f8fSGustavo F. Padovan 778fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 779fe4128e0SGustavo F. Padovan req.psm = chan->psm; 7800a708f8fSGustavo F. Padovan 781fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 782c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 7830a708f8fSGustavo F. Padovan 784fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 785fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 7860a708f8fSGustavo F. Padovan 78789bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT2) { 7880a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 7890a708f8fSGustavo F. Padovan char buf[128]; 790fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 791fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 7920a708f8fSGustavo F. Padovan 793d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan)) { 7940a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 7950a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 7960a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 7970a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); 79805e9a2f6SIlia Kolomisnky if (parent) 7990a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 8000a708f8fSGustavo F. Padovan 8010a708f8fSGustavo F. Padovan } else { 80289bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONFIG); 8030a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 8040a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 8050a708f8fSGustavo F. Padovan } 8060a708f8fSGustavo F. Padovan } else { 8070a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 8080a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); 8090a708f8fSGustavo F. Padovan } 8100a708f8fSGustavo F. Padovan 811fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 812fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 8130a708f8fSGustavo F. Padovan 814c1360a1cSGustavo F. Padovan if (test_bit(CONF_REQ_SENT, &chan->conf_state) || 8150a708f8fSGustavo F. Padovan rsp.result != L2CAP_CR_SUCCESS) { 8160a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 8170a708f8fSGustavo F. Padovan continue; 8180a708f8fSGustavo F. Padovan } 8190a708f8fSGustavo F. Padovan 820c1360a1cSGustavo F. Padovan set_bit(CONF_REQ_SENT, &chan->conf_state); 8210a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 82273ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 82373ffa904SGustavo F. Padovan chan->num_conf_req++; 8240a708f8fSGustavo F. Padovan } 8250a708f8fSGustavo F. Padovan 8260a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 8270a708f8fSGustavo F. Padovan } 8280a708f8fSGustavo F. Padovan 8293d57dc68SGustavo F. Padovan rcu_read_unlock(); 8300a708f8fSGustavo F. Padovan } 8310a708f8fSGustavo F. Padovan 832b62f328bSVille Tervo /* Find socket with cid and source bdaddr. 833b62f328bSVille Tervo * Returns closest match, locked. 834b62f328bSVille Tervo */ 83523691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src) 836b62f328bSVille Tervo { 83723691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 838b62f328bSVille Tervo 83923691d75SGustavo F. Padovan read_lock(&chan_list_lock); 840b62f328bSVille Tervo 84123691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 84223691d75SGustavo F. Padovan struct sock *sk = c->sk; 843fe4128e0SGustavo F. Padovan 84489bc500eSGustavo F. Padovan if (state && c->state != state) 845b62f328bSVille Tervo continue; 846b62f328bSVille Tervo 84723691d75SGustavo F. Padovan if (c->scid == cid) { 848b62f328bSVille Tervo /* Exact match. */ 84923691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 85023691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 85123691d75SGustavo F. Padovan return c; 85223691d75SGustavo F. Padovan } 853b62f328bSVille Tervo 854b62f328bSVille Tervo /* Closest match */ 855b62f328bSVille Tervo if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 85623691d75SGustavo F. Padovan c1 = c; 857b62f328bSVille Tervo } 858b62f328bSVille Tervo } 859280f294fSGustavo F. Padovan 86023691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 861b62f328bSVille Tervo 86223691d75SGustavo F. Padovan return c1; 863b62f328bSVille Tervo } 864b62f328bSVille Tervo 865b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn) 866b62f328bSVille Tervo { 867c916fbe4SGustavo F. Padovan struct sock *parent, *sk; 86823691d75SGustavo F. Padovan struct l2cap_chan *chan, *pchan; 869b62f328bSVille Tervo 870b62f328bSVille Tervo BT_DBG(""); 871b62f328bSVille Tervo 872b62f328bSVille Tervo /* Check if we have socket listening on cid */ 87323691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, 874b62f328bSVille Tervo conn->src); 87523691d75SGustavo F. Padovan if (!pchan) 876b62f328bSVille Tervo return; 877b62f328bSVille Tervo 87823691d75SGustavo F. Padovan parent = pchan->sk; 87923691d75SGustavo F. Padovan 880aa2ac881SGustavo F. Padovan lock_sock(parent); 88162f3a2cfSGustavo F. Padovan 882b62f328bSVille Tervo /* Check for backlog size */ 883b62f328bSVille Tervo if (sk_acceptq_is_full(parent)) { 884b62f328bSVille Tervo BT_DBG("backlog full %d", parent->sk_ack_backlog); 885b62f328bSVille Tervo goto clean; 886b62f328bSVille Tervo } 887b62f328bSVille Tervo 88880808e43SGustavo F. Padovan chan = pchan->ops->new_connection(pchan->data); 88980808e43SGustavo F. Padovan if (!chan) 890b62f328bSVille Tervo goto clean; 891b62f328bSVille Tervo 89280808e43SGustavo F. Padovan sk = chan->sk; 8935d41ce1dSGustavo F. Padovan 894b62f328bSVille Tervo hci_conn_hold(conn->hcon); 895b62f328bSVille Tervo 896b62f328bSVille Tervo bacpy(&bt_sk(sk)->src, conn->src); 897b62f328bSVille Tervo bacpy(&bt_sk(sk)->dst, conn->dst); 898b62f328bSVille Tervo 899d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 900d1010240SGustavo F. Padovan 9013d57dc68SGustavo F. Padovan l2cap_chan_add(conn, chan); 90248454079SGustavo F. Padovan 903c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 904b62f328bSVille Tervo 90589bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 906b62f328bSVille Tervo parent->sk_data_ready(parent, 0); 907b62f328bSVille Tervo 908b62f328bSVille Tervo clean: 909aa2ac881SGustavo F. Padovan release_sock(parent); 910b62f328bSVille Tervo } 911b62f328bSVille Tervo 912f1cb9af5SVinicius Costa Gomes static void l2cap_chan_ready(struct sock *sk) 913f1cb9af5SVinicius Costa Gomes { 914f1cb9af5SVinicius Costa Gomes struct l2cap_chan *chan = l2cap_pi(sk)->chan; 915f1cb9af5SVinicius Costa Gomes struct sock *parent = bt_sk(sk)->parent; 916f1cb9af5SVinicius Costa Gomes 917f1cb9af5SVinicius Costa Gomes BT_DBG("sk %p, parent %p", sk, parent); 918f1cb9af5SVinicius Costa Gomes 919f1cb9af5SVinicius Costa Gomes chan->conf_state = 0; 920f1cb9af5SVinicius Costa Gomes __clear_chan_timer(chan); 921f1cb9af5SVinicius Costa Gomes 92243f3dc41SVinicius Costa Gomes l2cap_state_change(chan, BT_CONNECTED); 923f1cb9af5SVinicius Costa Gomes sk->sk_state_change(sk); 924f1cb9af5SVinicius Costa Gomes 925f1cb9af5SVinicius Costa Gomes if (parent) 926f1cb9af5SVinicius Costa Gomes parent->sk_data_ready(parent, 0); 927f1cb9af5SVinicius Costa Gomes } 928f1cb9af5SVinicius Costa Gomes 9290a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn) 9300a708f8fSGustavo F. Padovan { 93148454079SGustavo F. Padovan struct l2cap_chan *chan; 9320a708f8fSGustavo F. Padovan 9330a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 9340a708f8fSGustavo F. Padovan 935b62f328bSVille Tervo if (!conn->hcon->out && conn->hcon->type == LE_LINK) 936b62f328bSVille Tervo l2cap_le_conn_ready(conn); 937b62f328bSVille Tervo 938160dc6acSVinicius Costa Gomes if (conn->hcon->out && conn->hcon->type == LE_LINK) 939160dc6acSVinicius Costa Gomes smp_conn_security(conn, conn->hcon->pending_sec_level); 940160dc6acSVinicius Costa Gomes 9413d57dc68SGustavo F. Padovan rcu_read_lock(); 9420a708f8fSGustavo F. Padovan 9433d57dc68SGustavo F. Padovan list_for_each_entry_rcu(chan, &conn->chan_l, list) { 94448454079SGustavo F. Padovan struct sock *sk = chan->sk; 945baa7e1faSGustavo F. Padovan 9460a708f8fSGustavo F. Padovan bh_lock_sock(sk); 9470a708f8fSGustavo F. Padovan 94863128451SVinicius Costa Gomes if (conn->hcon->type == LE_LINK) { 949b501d6a1SAnderson Briglia if (smp_conn_security(conn, chan->sec_level)) 950f1cb9af5SVinicius Costa Gomes l2cap_chan_ready(sk); 951acd7d370SVille Tervo 95263128451SVinicius Costa Gomes } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 953c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 95489bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 9550a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 956b501d6a1SAnderson Briglia 95789bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT) 958fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 9590a708f8fSGustavo F. Padovan 9600a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9610a708f8fSGustavo F. Padovan } 9620a708f8fSGustavo F. Padovan 9633d57dc68SGustavo F. Padovan rcu_read_unlock(); 9640a708f8fSGustavo F. Padovan } 9650a708f8fSGustavo F. Padovan 9660a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */ 9670a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) 9680a708f8fSGustavo F. Padovan { 96948454079SGustavo F. Padovan struct l2cap_chan *chan; 9700a708f8fSGustavo F. Padovan 9710a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 9720a708f8fSGustavo F. Padovan 9733d57dc68SGustavo F. Padovan rcu_read_lock(); 9740a708f8fSGustavo F. Padovan 9753d57dc68SGustavo F. Padovan list_for_each_entry_rcu(chan, &conn->chan_l, list) { 97648454079SGustavo F. Padovan struct sock *sk = chan->sk; 977baa7e1faSGustavo F. Padovan 978ecf61bdbSAndrei Emeltchenko if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) 9790a708f8fSGustavo F. Padovan sk->sk_err = err; 9800a708f8fSGustavo F. Padovan } 9810a708f8fSGustavo F. Padovan 9823d57dc68SGustavo F. Padovan rcu_read_unlock(); 9830a708f8fSGustavo F. Padovan } 9840a708f8fSGustavo F. Padovan 985f878fcadSGustavo F. Padovan static void l2cap_info_timeout(struct work_struct *work) 9860a708f8fSGustavo F. Padovan { 987f878fcadSGustavo F. Padovan struct l2cap_conn *conn = container_of(work, struct l2cap_conn, 988030013d8SGustavo F. Padovan info_timer.work); 9890a708f8fSGustavo F. Padovan 9900a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 9910a708f8fSGustavo F. Padovan conn->info_ident = 0; 9920a708f8fSGustavo F. Padovan 9930a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 9940a708f8fSGustavo F. Padovan } 9950a708f8fSGustavo F. Padovan 9965d3de7dfSVinicius Costa Gomes static void l2cap_conn_del(struct hci_conn *hcon, int err) 9975d3de7dfSVinicius Costa Gomes { 9985d3de7dfSVinicius Costa Gomes struct l2cap_conn *conn = hcon->l2cap_data; 9995d3de7dfSVinicius Costa Gomes struct l2cap_chan *chan, *l; 10005d3de7dfSVinicius Costa Gomes struct sock *sk; 10015d3de7dfSVinicius Costa Gomes 10025d3de7dfSVinicius Costa Gomes if (!conn) 10035d3de7dfSVinicius Costa Gomes return; 10045d3de7dfSVinicius Costa Gomes 10055d3de7dfSVinicius Costa Gomes BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); 10065d3de7dfSVinicius Costa Gomes 10075d3de7dfSVinicius Costa Gomes kfree_skb(conn->rx_skb); 10085d3de7dfSVinicius Costa Gomes 10095d3de7dfSVinicius Costa Gomes /* Kill channels */ 10105d3de7dfSVinicius Costa Gomes list_for_each_entry_safe(chan, l, &conn->chan_l, list) { 10115d3de7dfSVinicius Costa Gomes sk = chan->sk; 1012aa2ac881SGustavo F. Padovan lock_sock(sk); 10135d3de7dfSVinicius Costa Gomes l2cap_chan_del(chan, err); 1014aa2ac881SGustavo F. Padovan release_sock(sk); 10155d3de7dfSVinicius Costa Gomes chan->ops->close(chan->data); 10165d3de7dfSVinicius Costa Gomes } 10175d3de7dfSVinicius Costa Gomes 101873d80debSLuiz Augusto von Dentz hci_chan_del(conn->hchan); 101973d80debSLuiz Augusto von Dentz 10205d3de7dfSVinicius Costa Gomes if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) 1021371fd835SUlisses Furquim __cancel_delayed_work(&conn->info_timer); 10225d3de7dfSVinicius Costa Gomes 1023d26a2345SVinicius Costa Gomes if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->pend)) { 1024371fd835SUlisses Furquim __cancel_delayed_work(&conn->security_timer); 10258aab4757SVinicius Costa Gomes smp_chan_destroy(conn); 1026d26a2345SVinicius Costa Gomes } 10275d3de7dfSVinicius Costa Gomes 10285d3de7dfSVinicius Costa Gomes hcon->l2cap_data = NULL; 10295d3de7dfSVinicius Costa Gomes kfree(conn); 10305d3de7dfSVinicius Costa Gomes } 10315d3de7dfSVinicius Costa Gomes 10326c9d42a1SGustavo F. Padovan static void security_timeout(struct work_struct *work) 10335d3de7dfSVinicius Costa Gomes { 10346c9d42a1SGustavo F. Padovan struct l2cap_conn *conn = container_of(work, struct l2cap_conn, 10356c9d42a1SGustavo F. Padovan security_timer.work); 10365d3de7dfSVinicius Costa Gomes 10375d3de7dfSVinicius Costa Gomes l2cap_conn_del(conn->hcon, ETIMEDOUT); 10385d3de7dfSVinicius Costa Gomes } 10395d3de7dfSVinicius Costa Gomes 10400a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) 10410a708f8fSGustavo F. Padovan { 10420a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 104373d80debSLuiz Augusto von Dentz struct hci_chan *hchan; 10440a708f8fSGustavo F. Padovan 10450a708f8fSGustavo F. Padovan if (conn || status) 10460a708f8fSGustavo F. Padovan return conn; 10470a708f8fSGustavo F. Padovan 104873d80debSLuiz Augusto von Dentz hchan = hci_chan_create(hcon); 104973d80debSLuiz Augusto von Dentz if (!hchan) 10500a708f8fSGustavo F. Padovan return NULL; 10510a708f8fSGustavo F. Padovan 105273d80debSLuiz Augusto von Dentz conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); 105373d80debSLuiz Augusto von Dentz if (!conn) { 105473d80debSLuiz Augusto von Dentz hci_chan_del(hchan); 105573d80debSLuiz Augusto von Dentz return NULL; 105673d80debSLuiz Augusto von Dentz } 105773d80debSLuiz Augusto von Dentz 10580a708f8fSGustavo F. Padovan hcon->l2cap_data = conn; 10590a708f8fSGustavo F. Padovan conn->hcon = hcon; 106073d80debSLuiz Augusto von Dentz conn->hchan = hchan; 10610a708f8fSGustavo F. Padovan 106273d80debSLuiz Augusto von Dentz BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); 10630a708f8fSGustavo F. Padovan 1064acd7d370SVille Tervo if (hcon->hdev->le_mtu && hcon->type == LE_LINK) 1065acd7d370SVille Tervo conn->mtu = hcon->hdev->le_mtu; 1066acd7d370SVille Tervo else 10670a708f8fSGustavo F. Padovan conn->mtu = hcon->hdev->acl_mtu; 1068acd7d370SVille Tervo 10690a708f8fSGustavo F. Padovan conn->src = &hcon->hdev->bdaddr; 10700a708f8fSGustavo F. Padovan conn->dst = &hcon->dst; 10710a708f8fSGustavo F. Padovan 10720a708f8fSGustavo F. Padovan conn->feat_mask = 0; 10730a708f8fSGustavo F. Padovan 10740a708f8fSGustavo F. Padovan spin_lock_init(&conn->lock); 1075baa7e1faSGustavo F. Padovan 1076baa7e1faSGustavo F. Padovan INIT_LIST_HEAD(&conn->chan_l); 10770a708f8fSGustavo F. Padovan 10785d3de7dfSVinicius Costa Gomes if (hcon->type == LE_LINK) 10796c9d42a1SGustavo F. Padovan INIT_DELAYED_WORK(&conn->security_timer, security_timeout); 10805d3de7dfSVinicius Costa Gomes else 1081030013d8SGustavo F. Padovan INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); 10820a708f8fSGustavo F. Padovan 10839f5a0d7bSAndrei Emeltchenko conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; 10840a708f8fSGustavo F. Padovan 10850a708f8fSGustavo F. Padovan return conn; 10860a708f8fSGustavo F. Padovan } 10870a708f8fSGustavo F. Padovan 10880a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */ 10890a708f8fSGustavo F. Padovan 10900a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr. 10910a708f8fSGustavo F. Padovan * Returns closest match. 10920a708f8fSGustavo F. Padovan */ 109323691d75SGustavo F. Padovan static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src) 10940a708f8fSGustavo F. Padovan { 109523691d75SGustavo F. Padovan struct l2cap_chan *c, *c1 = NULL; 10960a708f8fSGustavo F. Padovan 109723691d75SGustavo F. Padovan read_lock(&chan_list_lock); 10980a708f8fSGustavo F. Padovan 109923691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 110023691d75SGustavo F. Padovan struct sock *sk = c->sk; 1101fe4128e0SGustavo F. Padovan 110289bc500eSGustavo F. Padovan if (state && c->state != state) 11030a708f8fSGustavo F. Padovan continue; 11040a708f8fSGustavo F. Padovan 110523691d75SGustavo F. Padovan if (c->psm == psm) { 11060a708f8fSGustavo F. Padovan /* Exact match. */ 110723691d75SGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) { 1108a7567b20SJohannes Berg read_unlock(&chan_list_lock); 110923691d75SGustavo F. Padovan return c; 111023691d75SGustavo F. Padovan } 11110a708f8fSGustavo F. Padovan 11120a708f8fSGustavo F. Padovan /* Closest match */ 11130a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 111423691d75SGustavo F. Padovan c1 = c; 11150a708f8fSGustavo F. Padovan } 11160a708f8fSGustavo F. Padovan } 11170a708f8fSGustavo F. Padovan 111823691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 11190a708f8fSGustavo F. Padovan 112023691d75SGustavo F. Padovan return c1; 11210a708f8fSGustavo F. Padovan } 11220a708f8fSGustavo F. Padovan 112303a00194SGustavo F. Padovan inline int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, bdaddr_t *dst) 11240a708f8fSGustavo F. Padovan { 11255d41ce1dSGustavo F. Padovan struct sock *sk = chan->sk; 11260a708f8fSGustavo F. Padovan bdaddr_t *src = &bt_sk(sk)->src; 11270a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 11280a708f8fSGustavo F. Padovan struct hci_conn *hcon; 11290a708f8fSGustavo F. Padovan struct hci_dev *hdev; 11300a708f8fSGustavo F. Padovan __u8 auth_type; 11310a708f8fSGustavo F. Padovan int err; 11320a708f8fSGustavo F. Padovan 11330a708f8fSGustavo F. Padovan BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), 1134fe4128e0SGustavo F. Padovan chan->psm); 11350a708f8fSGustavo F. Padovan 11360a708f8fSGustavo F. Padovan hdev = hci_get_route(dst, src); 11370a708f8fSGustavo F. Padovan if (!hdev) 11380a708f8fSGustavo F. Padovan return -EHOSTUNREACH; 11390a708f8fSGustavo F. Padovan 114009fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 11410a708f8fSGustavo F. Padovan 114203a00194SGustavo F. Padovan lock_sock(sk); 114303a00194SGustavo F. Padovan 114403a00194SGustavo F. Padovan /* PSM must be odd and lsb of upper byte must be 0 */ 114503a00194SGustavo F. Padovan if ((__le16_to_cpu(psm) & 0x0101) != 0x0001 && !cid && 114603a00194SGustavo F. Padovan chan->chan_type != L2CAP_CHAN_RAW) { 114703a00194SGustavo F. Padovan err = -EINVAL; 114803a00194SGustavo F. Padovan goto done; 114903a00194SGustavo F. Padovan } 115003a00194SGustavo F. Padovan 115103a00194SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !(psm || cid)) { 115203a00194SGustavo F. Padovan err = -EINVAL; 115303a00194SGustavo F. Padovan goto done; 115403a00194SGustavo F. Padovan } 115503a00194SGustavo F. Padovan 115603a00194SGustavo F. Padovan switch (chan->mode) { 115703a00194SGustavo F. Padovan case L2CAP_MODE_BASIC: 115803a00194SGustavo F. Padovan break; 115903a00194SGustavo F. Padovan case L2CAP_MODE_ERTM: 116003a00194SGustavo F. Padovan case L2CAP_MODE_STREAMING: 116103a00194SGustavo F. Padovan if (!disable_ertm) 116203a00194SGustavo F. Padovan break; 116303a00194SGustavo F. Padovan /* fall through */ 116403a00194SGustavo F. Padovan default: 116503a00194SGustavo F. Padovan err = -ENOTSUPP; 116603a00194SGustavo F. Padovan goto done; 116703a00194SGustavo F. Padovan } 116803a00194SGustavo F. Padovan 116903a00194SGustavo F. Padovan switch (sk->sk_state) { 117003a00194SGustavo F. Padovan case BT_CONNECT: 117103a00194SGustavo F. Padovan case BT_CONNECT2: 117203a00194SGustavo F. Padovan case BT_CONFIG: 117303a00194SGustavo F. Padovan /* Already connecting */ 117403a00194SGustavo F. Padovan err = 0; 117503a00194SGustavo F. Padovan goto done; 117603a00194SGustavo F. Padovan 117703a00194SGustavo F. Padovan case BT_CONNECTED: 117803a00194SGustavo F. Padovan /* Already connected */ 117903a00194SGustavo F. Padovan err = -EISCONN; 118003a00194SGustavo F. Padovan goto done; 118103a00194SGustavo F. Padovan 118203a00194SGustavo F. Padovan case BT_OPEN: 118303a00194SGustavo F. Padovan case BT_BOUND: 118403a00194SGustavo F. Padovan /* Can connect */ 118503a00194SGustavo F. Padovan break; 118603a00194SGustavo F. Padovan 118703a00194SGustavo F. Padovan default: 118803a00194SGustavo F. Padovan err = -EBADFD; 118903a00194SGustavo F. Padovan goto done; 119003a00194SGustavo F. Padovan } 119103a00194SGustavo F. Padovan 119203a00194SGustavo F. Padovan /* Set destination address and psm */ 1193*9219b2a0SGustavo F. Padovan bacpy(&bt_sk(sk)->dst, dst); 119403a00194SGustavo F. Padovan chan->psm = psm; 119503a00194SGustavo F. Padovan chan->dcid = cid; 119603a00194SGustavo F. Padovan 11974343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 11980a708f8fSGustavo F. Padovan 1199fe4128e0SGustavo F. Padovan if (chan->dcid == L2CAP_CID_LE_DATA) 1200acd7d370SVille Tervo hcon = hci_connect(hdev, LE_LINK, dst, 12014343478fSGustavo F. Padovan chan->sec_level, auth_type); 1202acd7d370SVille Tervo else 12030a708f8fSGustavo F. Padovan hcon = hci_connect(hdev, ACL_LINK, dst, 12044343478fSGustavo F. Padovan chan->sec_level, auth_type); 1205acd7d370SVille Tervo 120630e76272SVille Tervo if (IS_ERR(hcon)) { 120730e76272SVille Tervo err = PTR_ERR(hcon); 12080a708f8fSGustavo F. Padovan goto done; 120930e76272SVille Tervo } 12100a708f8fSGustavo F. Padovan 12110a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 12120a708f8fSGustavo F. Padovan if (!conn) { 12130a708f8fSGustavo F. Padovan hci_conn_put(hcon); 121430e76272SVille Tervo err = -ENOMEM; 12150a708f8fSGustavo F. Padovan goto done; 12160a708f8fSGustavo F. Padovan } 12170a708f8fSGustavo F. Padovan 12180a708f8fSGustavo F. Padovan /* Update source addr of the socket */ 12190a708f8fSGustavo F. Padovan bacpy(src, conn->src); 12200a708f8fSGustavo F. Padovan 122148454079SGustavo F. Padovan l2cap_chan_add(conn, chan); 122248454079SGustavo F. Padovan 122389bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECT); 1224c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 12250a708f8fSGustavo F. Padovan 12260a708f8fSGustavo F. Padovan if (hcon->state == BT_CONNECTED) { 1227715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 1228c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 1229d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan)) 123089bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 12310a708f8fSGustavo F. Padovan } else 1232fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 12330a708f8fSGustavo F. Padovan } 12340a708f8fSGustavo F. Padovan 123530e76272SVille Tervo err = 0; 123630e76272SVille Tervo 12370a708f8fSGustavo F. Padovan done: 123809fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 12390a708f8fSGustavo F. Padovan hci_dev_put(hdev); 12400a708f8fSGustavo F. Padovan return err; 12410a708f8fSGustavo F. Padovan } 12420a708f8fSGustavo F. Padovan 1243dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk) 12440a708f8fSGustavo F. Padovan { 12458c1d787bSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 12460a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 12470a708f8fSGustavo F. Padovan int err = 0; 12480a708f8fSGustavo F. Padovan int timeo = HZ/5; 12490a708f8fSGustavo F. Padovan 12500a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 12510a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 1252a71a0cf4SPeter Hurley while (chan->unacked_frames > 0 && chan->conn) { 12530a708f8fSGustavo F. Padovan if (!timeo) 12540a708f8fSGustavo F. Padovan timeo = HZ/5; 12550a708f8fSGustavo F. Padovan 12560a708f8fSGustavo F. Padovan if (signal_pending(current)) { 12570a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 12580a708f8fSGustavo F. Padovan break; 12590a708f8fSGustavo F. Padovan } 12600a708f8fSGustavo F. Padovan 12610a708f8fSGustavo F. Padovan release_sock(sk); 12620a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 12630a708f8fSGustavo F. Padovan lock_sock(sk); 1264a71a0cf4SPeter Hurley set_current_state(TASK_INTERRUPTIBLE); 12650a708f8fSGustavo F. Padovan 12660a708f8fSGustavo F. Padovan err = sock_error(sk); 12670a708f8fSGustavo F. Padovan if (err) 12680a708f8fSGustavo F. Padovan break; 12690a708f8fSGustavo F. Padovan } 12700a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 12710a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 12720a708f8fSGustavo F. Padovan return err; 12730a708f8fSGustavo F. Padovan } 12740a708f8fSGustavo F. Padovan 1275721c4181SGustavo F. Padovan static void l2cap_monitor_timeout(struct work_struct *work) 12760a708f8fSGustavo F. Padovan { 1277721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 1278721c4181SGustavo F. Padovan monitor_timer.work); 1279525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 12800a708f8fSGustavo F. Padovan 1281525cd185SGustavo F. Padovan BT_DBG("chan %p", chan); 12820a708f8fSGustavo F. Padovan 1283721c4181SGustavo F. Padovan lock_sock(sk); 12842c03a7a4SGustavo F. Padovan if (chan->retry_count >= chan->remote_max_tx) { 12858c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 1286721c4181SGustavo F. Padovan release_sock(sk); 12870a708f8fSGustavo F. Padovan return; 12880a708f8fSGustavo F. Padovan } 12890a708f8fSGustavo F. Padovan 12906a026610SGustavo F. Padovan chan->retry_count++; 12911a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 12920a708f8fSGustavo F. Padovan 1293525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 1294721c4181SGustavo F. Padovan release_sock(sk); 12950a708f8fSGustavo F. Padovan } 12960a708f8fSGustavo F. Padovan 1297721c4181SGustavo F. Padovan static void l2cap_retrans_timeout(struct work_struct *work) 12980a708f8fSGustavo F. Padovan { 1299721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 1300721c4181SGustavo F. Padovan retrans_timer.work); 1301525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 13020a708f8fSGustavo F. Padovan 130349208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 13040a708f8fSGustavo F. Padovan 1305721c4181SGustavo F. Padovan lock_sock(sk); 13066a026610SGustavo F. Padovan chan->retry_count = 1; 13071a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 13080a708f8fSGustavo F. Padovan 1309e2ab4353SGustavo F. Padovan set_bit(CONN_WAIT_F, &chan->conn_state); 13100a708f8fSGustavo F. Padovan 1311525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 1312721c4181SGustavo F. Padovan release_sock(sk); 13130a708f8fSGustavo F. Padovan } 13140a708f8fSGustavo F. Padovan 131542e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan) 13160a708f8fSGustavo F. Padovan { 13170a708f8fSGustavo F. Padovan struct sk_buff *skb; 13180a708f8fSGustavo F. Padovan 131958d35f87SGustavo F. Padovan while ((skb = skb_peek(&chan->tx_q)) && 13206a026610SGustavo F. Padovan chan->unacked_frames) { 132142e5c802SGustavo F. Padovan if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) 13220a708f8fSGustavo F. Padovan break; 13230a708f8fSGustavo F. Padovan 132458d35f87SGustavo F. Padovan skb = skb_dequeue(&chan->tx_q); 13250a708f8fSGustavo F. Padovan kfree_skb(skb); 13260a708f8fSGustavo F. Padovan 13276a026610SGustavo F. Padovan chan->unacked_frames--; 13280a708f8fSGustavo F. Padovan } 13290a708f8fSGustavo F. Padovan 13306a026610SGustavo F. Padovan if (!chan->unacked_frames) 13311a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 13320a708f8fSGustavo F. Padovan } 13330a708f8fSGustavo F. Padovan 133467c9e840SSzymon Janc static void l2cap_streaming_send(struct l2cap_chan *chan) 13350a708f8fSGustavo F. Padovan { 13360a708f8fSGustavo F. Padovan struct sk_buff *skb; 133788843ab0SAndrei Emeltchenko u32 control; 133888843ab0SAndrei Emeltchenko u16 fcs; 13390a708f8fSGustavo F. Padovan 134058d35f87SGustavo F. Padovan while ((skb = skb_dequeue(&chan->tx_q))) { 134188843ab0SAndrei Emeltchenko control = __get_control(chan, skb->data + L2CAP_HDR_SIZE); 1342fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, chan->next_tx_seq); 134388843ab0SAndrei Emeltchenko __put_control(chan, control, skb->data + L2CAP_HDR_SIZE); 13440a708f8fSGustavo F. Padovan 134547d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 134603a51213SAndrei Emeltchenko fcs = crc16(0, (u8 *)skb->data, 134703a51213SAndrei Emeltchenko skb->len - L2CAP_FCS_SIZE); 134803a51213SAndrei Emeltchenko put_unaligned_le16(fcs, 134903a51213SAndrei Emeltchenko skb->data + skb->len - L2CAP_FCS_SIZE); 13500a708f8fSGustavo F. Padovan } 13510a708f8fSGustavo F. Padovan 13524343478fSGustavo F. Padovan l2cap_do_send(chan, skb); 13530a708f8fSGustavo F. Padovan 1354836be934SAndrei Emeltchenko chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); 13550a708f8fSGustavo F. Padovan } 13560a708f8fSGustavo F. Padovan } 13570a708f8fSGustavo F. Padovan 1358fb45de7dSAndrei Emeltchenko static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u16 tx_seq) 13590a708f8fSGustavo F. Padovan { 13600a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 136188843ab0SAndrei Emeltchenko u16 fcs; 136288843ab0SAndrei Emeltchenko u32 control; 13630a708f8fSGustavo F. Padovan 136458d35f87SGustavo F. Padovan skb = skb_peek(&chan->tx_q); 13650a708f8fSGustavo F. Padovan if (!skb) 13660a708f8fSGustavo F. Padovan return; 13670a708f8fSGustavo F. Padovan 1368d1726b6dSSzymon Janc while (bt_cb(skb)->tx_seq != tx_seq) { 136958d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 13700a708f8fSGustavo F. Padovan return; 13710a708f8fSGustavo F. Padovan 1372d1726b6dSSzymon Janc skb = skb_queue_next(&chan->tx_q, skb); 1373d1726b6dSSzymon Janc } 13740a708f8fSGustavo F. Padovan 13752c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 13762c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 13778c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 13780a708f8fSGustavo F. Padovan return; 13790a708f8fSGustavo F. Padovan } 13800a708f8fSGustavo F. Padovan 13810a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 13820a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 138388843ab0SAndrei Emeltchenko 138488843ab0SAndrei Emeltchenko control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE); 13857e0ef6eeSAndrei Emeltchenko control &= __get_sar_mask(chan); 13860a708f8fSGustavo F. Padovan 1387e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 138803f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 13890a708f8fSGustavo F. Padovan 13900b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 1391fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, tx_seq); 13920a708f8fSGustavo F. Padovan 139388843ab0SAndrei Emeltchenko __put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE); 13940a708f8fSGustavo F. Padovan 139547d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 139603a51213SAndrei Emeltchenko fcs = crc16(0, (u8 *)tx_skb->data, 139703a51213SAndrei Emeltchenko tx_skb->len - L2CAP_FCS_SIZE); 139803a51213SAndrei Emeltchenko put_unaligned_le16(fcs, 139903a51213SAndrei Emeltchenko tx_skb->data + tx_skb->len - L2CAP_FCS_SIZE); 14000a708f8fSGustavo F. Padovan } 14010a708f8fSGustavo F. Padovan 14024343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 14030a708f8fSGustavo F. Padovan } 14040a708f8fSGustavo F. Padovan 140567c9e840SSzymon Janc static int l2cap_ertm_send(struct l2cap_chan *chan) 14060a708f8fSGustavo F. Padovan { 14070a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 140888843ab0SAndrei Emeltchenko u16 fcs; 140988843ab0SAndrei Emeltchenko u32 control; 14100a708f8fSGustavo F. Padovan int nsent = 0; 14110a708f8fSGustavo F. Padovan 141289bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 14130a708f8fSGustavo F. Padovan return -ENOTCONN; 14140a708f8fSGustavo F. Padovan 141558d35f87SGustavo F. Padovan while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) { 14160a708f8fSGustavo F. Padovan 14172c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 14182c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 14198c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 14200a708f8fSGustavo F. Padovan break; 14210a708f8fSGustavo F. Padovan } 14220a708f8fSGustavo F. Padovan 14230a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 14240a708f8fSGustavo F. Padovan 14250a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 14260a708f8fSGustavo F. Padovan 142788843ab0SAndrei Emeltchenko control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE); 14287e0ef6eeSAndrei Emeltchenko control &= __get_sar_mask(chan); 14290a708f8fSGustavo F. Padovan 1430e2ab4353SGustavo F. Padovan if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 143103f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 1432e2ab4353SGustavo F. Padovan 14330b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 1434fb45de7dSAndrei Emeltchenko control |= __set_txseq(chan, chan->next_tx_seq); 14350a708f8fSGustavo F. Padovan 143688843ab0SAndrei Emeltchenko __put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE); 14370a708f8fSGustavo F. Padovan 143847d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 143903a51213SAndrei Emeltchenko fcs = crc16(0, (u8 *)skb->data, 144003a51213SAndrei Emeltchenko tx_skb->len - L2CAP_FCS_SIZE); 144103a51213SAndrei Emeltchenko put_unaligned_le16(fcs, skb->data + 144203a51213SAndrei Emeltchenko tx_skb->len - L2CAP_FCS_SIZE); 14430a708f8fSGustavo F. Padovan } 14440a708f8fSGustavo F. Padovan 14454343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 14460a708f8fSGustavo F. Padovan 14471a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 14480a708f8fSGustavo F. Padovan 144942e5c802SGustavo F. Padovan bt_cb(skb)->tx_seq = chan->next_tx_seq; 1450836be934SAndrei Emeltchenko 1451836be934SAndrei Emeltchenko chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); 14520a708f8fSGustavo F. Padovan 145323e9fde2SSuraj Sumangala if (bt_cb(skb)->retries == 1) 14546a026610SGustavo F. Padovan chan->unacked_frames++; 145523e9fde2SSuraj Sumangala 14566a026610SGustavo F. Padovan chan->frames_sent++; 14570a708f8fSGustavo F. Padovan 145858d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 145958d35f87SGustavo F. Padovan chan->tx_send_head = NULL; 14600a708f8fSGustavo F. Padovan else 146158d35f87SGustavo F. Padovan chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); 14620a708f8fSGustavo F. Padovan 14630a708f8fSGustavo F. Padovan nsent++; 14640a708f8fSGustavo F. Padovan } 14650a708f8fSGustavo F. Padovan 14660a708f8fSGustavo F. Padovan return nsent; 14670a708f8fSGustavo F. Padovan } 14680a708f8fSGustavo F. Padovan 1469525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan) 14700a708f8fSGustavo F. Padovan { 14710a708f8fSGustavo F. Padovan int ret; 14720a708f8fSGustavo F. Padovan 147358d35f87SGustavo F. Padovan if (!skb_queue_empty(&chan->tx_q)) 147458d35f87SGustavo F. Padovan chan->tx_send_head = chan->tx_q.next; 14750a708f8fSGustavo F. Padovan 147642e5c802SGustavo F. Padovan chan->next_tx_seq = chan->expected_ack_seq; 1477525cd185SGustavo F. Padovan ret = l2cap_ertm_send(chan); 14780a708f8fSGustavo F. Padovan return ret; 14790a708f8fSGustavo F. Padovan } 14800a708f8fSGustavo F. Padovan 1481525cd185SGustavo F. Padovan static void l2cap_send_ack(struct l2cap_chan *chan) 14820a708f8fSGustavo F. Padovan { 148388843ab0SAndrei Emeltchenko u32 control = 0; 14840a708f8fSGustavo F. Padovan 14850b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 14860a708f8fSGustavo F. Padovan 1487e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 1488ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 1489e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 1490525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 14910a708f8fSGustavo F. Padovan return; 14920a708f8fSGustavo F. Padovan } 14930a708f8fSGustavo F. Padovan 1494525cd185SGustavo F. Padovan if (l2cap_ertm_send(chan) > 0) 14950a708f8fSGustavo F. Padovan return; 14960a708f8fSGustavo F. Padovan 1497ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 1498525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 14990a708f8fSGustavo F. Padovan } 15000a708f8fSGustavo F. Padovan 1501525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan) 15020a708f8fSGustavo F. Padovan { 15030a708f8fSGustavo F. Padovan struct srej_list *tail; 150488843ab0SAndrei Emeltchenko u32 control; 15050a708f8fSGustavo F. Padovan 1506ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 150703f6715dSAndrei Emeltchenko control |= __set_ctrl_final(chan); 15080a708f8fSGustavo F. Padovan 150939d5a3eeSGustavo F. Padovan tail = list_entry((&chan->srej_l)->prev, struct srej_list, list); 15100b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, tail->tx_seq); 15110a708f8fSGustavo F. Padovan 1512525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 15130a708f8fSGustavo F. Padovan } 15140a708f8fSGustavo F. Padovan 15150a708f8fSGustavo F. Padovan static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb) 15160a708f8fSGustavo F. Padovan { 15178c1d787bSGustavo F. Padovan struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn; 15180a708f8fSGustavo F. Padovan struct sk_buff **frag; 15190a708f8fSGustavo F. Padovan int err, sent = 0; 15200a708f8fSGustavo F. Padovan 15210a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) 15220a708f8fSGustavo F. Padovan return -EFAULT; 15230a708f8fSGustavo F. Padovan 15240a708f8fSGustavo F. Padovan sent += count; 15250a708f8fSGustavo F. Padovan len -= count; 15260a708f8fSGustavo F. Padovan 15270a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 15280a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 15290a708f8fSGustavo F. Padovan while (len) { 15300a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 15310a708f8fSGustavo F. Padovan 15320a708f8fSGustavo F. Padovan *frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err); 15330a708f8fSGustavo F. Padovan if (!*frag) 15340a708f8fSGustavo F. Padovan return err; 15350a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) 15360a708f8fSGustavo F. Padovan return -EFAULT; 15370a708f8fSGustavo F. Padovan 15385e59b791SLuiz Augusto von Dentz (*frag)->priority = skb->priority; 15395e59b791SLuiz Augusto von Dentz 15400a708f8fSGustavo F. Padovan sent += count; 15410a708f8fSGustavo F. Padovan len -= count; 15420a708f8fSGustavo F. Padovan 15430a708f8fSGustavo F. Padovan frag = &(*frag)->next; 15440a708f8fSGustavo F. Padovan } 15450a708f8fSGustavo F. Padovan 15460a708f8fSGustavo F. Padovan return sent; 15470a708f8fSGustavo F. Padovan } 15480a708f8fSGustavo F. Padovan 15495e59b791SLuiz Augusto von Dentz static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, 15505e59b791SLuiz Augusto von Dentz struct msghdr *msg, size_t len, 15515e59b791SLuiz Augusto von Dentz u32 priority) 15520a708f8fSGustavo F. Padovan { 1553fe4128e0SGustavo F. Padovan struct sock *sk = chan->sk; 15548c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 15550a708f8fSGustavo F. Padovan struct sk_buff *skb; 155603a51213SAndrei Emeltchenko int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; 15570a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 15580a708f8fSGustavo F. Padovan 15595e59b791SLuiz Augusto von Dentz BT_DBG("sk %p len %d priority %u", sk, (int)len, priority); 15600a708f8fSGustavo F. Padovan 15610a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 15620a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 15630a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 15640a708f8fSGustavo F. Padovan if (!skb) 15650a708f8fSGustavo F. Padovan return ERR_PTR(err); 15660a708f8fSGustavo F. Padovan 15675e59b791SLuiz Augusto von Dentz skb->priority = priority; 15685e59b791SLuiz Augusto von Dentz 15690a708f8fSGustavo F. Padovan /* Create L2CAP header */ 15700a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1571fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 15720a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 1573fe4128e0SGustavo F. Padovan put_unaligned_le16(chan->psm, skb_put(skb, 2)); 15740a708f8fSGustavo F. Padovan 15750a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 15760a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 15770a708f8fSGustavo F. Padovan kfree_skb(skb); 15780a708f8fSGustavo F. Padovan return ERR_PTR(err); 15790a708f8fSGustavo F. Padovan } 15800a708f8fSGustavo F. Padovan return skb; 15810a708f8fSGustavo F. Padovan } 15820a708f8fSGustavo F. Padovan 15835e59b791SLuiz Augusto von Dentz static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, 15845e59b791SLuiz Augusto von Dentz struct msghdr *msg, size_t len, 15855e59b791SLuiz Augusto von Dentz u32 priority) 15860a708f8fSGustavo F. Padovan { 1587fe4128e0SGustavo F. Padovan struct sock *sk = chan->sk; 15888c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 15890a708f8fSGustavo F. Padovan struct sk_buff *skb; 15900a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE; 15910a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 15920a708f8fSGustavo F. Padovan 15930a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 15940a708f8fSGustavo F. Padovan 15950a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 15960a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 15970a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 15980a708f8fSGustavo F. Padovan if (!skb) 15990a708f8fSGustavo F. Padovan return ERR_PTR(err); 16000a708f8fSGustavo F. Padovan 16015e59b791SLuiz Augusto von Dentz skb->priority = priority; 16025e59b791SLuiz Augusto von Dentz 16030a708f8fSGustavo F. Padovan /* Create L2CAP header */ 16040a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1605fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 16060a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 16070a708f8fSGustavo F. Padovan 16080a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 16090a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 16100a708f8fSGustavo F. Padovan kfree_skb(skb); 16110a708f8fSGustavo F. Padovan return ERR_PTR(err); 16120a708f8fSGustavo F. Padovan } 16130a708f8fSGustavo F. Padovan return skb; 16140a708f8fSGustavo F. Padovan } 16150a708f8fSGustavo F. Padovan 1616ab0ff76dSLuiz Augusto von Dentz static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, 1617ab0ff76dSLuiz Augusto von Dentz struct msghdr *msg, size_t len, 161888843ab0SAndrei Emeltchenko u32 control, u16 sdulen) 16190a708f8fSGustavo F. Padovan { 162047d1ec61SGustavo F. Padovan struct sock *sk = chan->sk; 16218c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 16220a708f8fSGustavo F. Padovan struct sk_buff *skb; 1623e4ca6d98SAndrei Emeltchenko int err, count, hlen; 16240a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 16250a708f8fSGustavo F. Padovan 16260a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 16270a708f8fSGustavo F. Padovan 16280a708f8fSGustavo F. Padovan if (!conn) 16290a708f8fSGustavo F. Padovan return ERR_PTR(-ENOTCONN); 16300a708f8fSGustavo F. Padovan 1631e4ca6d98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 1632e4ca6d98SAndrei Emeltchenko hlen = L2CAP_EXT_HDR_SIZE; 1633e4ca6d98SAndrei Emeltchenko else 1634e4ca6d98SAndrei Emeltchenko hlen = L2CAP_ENH_HDR_SIZE; 1635e4ca6d98SAndrei Emeltchenko 16360a708f8fSGustavo F. Padovan if (sdulen) 163703a51213SAndrei Emeltchenko hlen += L2CAP_SDULEN_SIZE; 16380a708f8fSGustavo F. Padovan 163947d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 164003a51213SAndrei Emeltchenko hlen += L2CAP_FCS_SIZE; 16410a708f8fSGustavo F. Padovan 16420a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 16430a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 16440a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 16450a708f8fSGustavo F. Padovan if (!skb) 16460a708f8fSGustavo F. Padovan return ERR_PTR(err); 16470a708f8fSGustavo F. Padovan 16480a708f8fSGustavo F. Padovan /* Create L2CAP header */ 16490a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1650fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 16510a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 165288843ab0SAndrei Emeltchenko 165388843ab0SAndrei Emeltchenko __put_control(chan, control, skb_put(skb, __ctrl_size(chan))); 165488843ab0SAndrei Emeltchenko 16550a708f8fSGustavo F. Padovan if (sdulen) 165603a51213SAndrei Emeltchenko put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); 16570a708f8fSGustavo F. Padovan 16580a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 16590a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 16600a708f8fSGustavo F. Padovan kfree_skb(skb); 16610a708f8fSGustavo F. Padovan return ERR_PTR(err); 16620a708f8fSGustavo F. Padovan } 16630a708f8fSGustavo F. Padovan 166447d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 166503a51213SAndrei Emeltchenko put_unaligned_le16(0, skb_put(skb, L2CAP_FCS_SIZE)); 16660a708f8fSGustavo F. Padovan 16670a708f8fSGustavo F. Padovan bt_cb(skb)->retries = 0; 16680a708f8fSGustavo F. Padovan return skb; 16690a708f8fSGustavo F. Padovan } 16700a708f8fSGustavo F. Padovan 167167c9e840SSzymon Janc static int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 16720a708f8fSGustavo F. Padovan { 16730a708f8fSGustavo F. Padovan struct sk_buff *skb; 16740a708f8fSGustavo F. Padovan struct sk_buff_head sar_queue; 167588843ab0SAndrei Emeltchenko u32 control; 16760a708f8fSGustavo F. Padovan size_t size = 0; 16770a708f8fSGustavo F. Padovan 16780a708f8fSGustavo F. Padovan skb_queue_head_init(&sar_queue); 16797e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_START); 168047d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len); 16810a708f8fSGustavo F. Padovan if (IS_ERR(skb)) 16820a708f8fSGustavo F. Padovan return PTR_ERR(skb); 16830a708f8fSGustavo F. Padovan 16840a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 16852c03a7a4SGustavo F. Padovan len -= chan->remote_mps; 16862c03a7a4SGustavo F. Padovan size += chan->remote_mps; 16870a708f8fSGustavo F. Padovan 16880a708f8fSGustavo F. Padovan while (len > 0) { 16890a708f8fSGustavo F. Padovan size_t buflen; 16900a708f8fSGustavo F. Padovan 16912c03a7a4SGustavo F. Padovan if (len > chan->remote_mps) { 16927e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_CONTINUE); 16932c03a7a4SGustavo F. Padovan buflen = chan->remote_mps; 16940a708f8fSGustavo F. Padovan } else { 16957e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_END); 16960a708f8fSGustavo F. Padovan buflen = len; 16970a708f8fSGustavo F. Padovan } 16980a708f8fSGustavo F. Padovan 169947d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0); 17000a708f8fSGustavo F. Padovan if (IS_ERR(skb)) { 17010a708f8fSGustavo F. Padovan skb_queue_purge(&sar_queue); 17020a708f8fSGustavo F. Padovan return PTR_ERR(skb); 17030a708f8fSGustavo F. Padovan } 17040a708f8fSGustavo F. Padovan 17050a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 17060a708f8fSGustavo F. Padovan len -= buflen; 17070a708f8fSGustavo F. Padovan size += buflen; 17080a708f8fSGustavo F. Padovan } 170958d35f87SGustavo F. Padovan skb_queue_splice_tail(&sar_queue, &chan->tx_q); 171058d35f87SGustavo F. Padovan if (chan->tx_send_head == NULL) 171158d35f87SGustavo F. Padovan chan->tx_send_head = sar_queue.next; 17120a708f8fSGustavo F. Padovan 17130a708f8fSGustavo F. Padovan return size; 17140a708f8fSGustavo F. Padovan } 17150a708f8fSGustavo F. Padovan 17165e59b791SLuiz Augusto von Dentz int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, 17175e59b791SLuiz Augusto von Dentz u32 priority) 17189a91a04aSGustavo F. Padovan { 17199a91a04aSGustavo F. Padovan struct sk_buff *skb; 172088843ab0SAndrei Emeltchenko u32 control; 17219a91a04aSGustavo F. Padovan int err; 17229a91a04aSGustavo F. Padovan 17239a91a04aSGustavo F. Padovan /* Connectionless channel */ 1724715ec005SGustavo F. Padovan if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { 17255e59b791SLuiz Augusto von Dentz skb = l2cap_create_connless_pdu(chan, msg, len, priority); 17269a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 17279a91a04aSGustavo F. Padovan return PTR_ERR(skb); 17289a91a04aSGustavo F. Padovan 17299a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 17309a91a04aSGustavo F. Padovan return len; 17319a91a04aSGustavo F. Padovan } 17329a91a04aSGustavo F. Padovan 17339a91a04aSGustavo F. Padovan switch (chan->mode) { 17349a91a04aSGustavo F. Padovan case L2CAP_MODE_BASIC: 17359a91a04aSGustavo F. Padovan /* Check outgoing MTU */ 17369a91a04aSGustavo F. Padovan if (len > chan->omtu) 17379a91a04aSGustavo F. Padovan return -EMSGSIZE; 17389a91a04aSGustavo F. Padovan 17399a91a04aSGustavo F. Padovan /* Create a basic PDU */ 17405e59b791SLuiz Augusto von Dentz skb = l2cap_create_basic_pdu(chan, msg, len, priority); 17419a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 17429a91a04aSGustavo F. Padovan return PTR_ERR(skb); 17439a91a04aSGustavo F. Padovan 17449a91a04aSGustavo F. Padovan l2cap_do_send(chan, skb); 17459a91a04aSGustavo F. Padovan err = len; 17469a91a04aSGustavo F. Padovan break; 17479a91a04aSGustavo F. Padovan 17489a91a04aSGustavo F. Padovan case L2CAP_MODE_ERTM: 17499a91a04aSGustavo F. Padovan case L2CAP_MODE_STREAMING: 17509a91a04aSGustavo F. Padovan /* Entire SDU fits into one PDU */ 17519a91a04aSGustavo F. Padovan if (len <= chan->remote_mps) { 17527e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, L2CAP_SAR_UNSEGMENTED); 17539a91a04aSGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, len, control, 17549a91a04aSGustavo F. Padovan 0); 17559a91a04aSGustavo F. Padovan if (IS_ERR(skb)) 17569a91a04aSGustavo F. Padovan return PTR_ERR(skb); 17579a91a04aSGustavo F. Padovan 17589a91a04aSGustavo F. Padovan __skb_queue_tail(&chan->tx_q, skb); 17599a91a04aSGustavo F. Padovan 17609a91a04aSGustavo F. Padovan if (chan->tx_send_head == NULL) 17619a91a04aSGustavo F. Padovan chan->tx_send_head = skb; 17629a91a04aSGustavo F. Padovan 17639a91a04aSGustavo F. Padovan } else { 17649a91a04aSGustavo F. Padovan /* Segment SDU into multiples PDUs */ 17659a91a04aSGustavo F. Padovan err = l2cap_sar_segment_sdu(chan, msg, len); 17669a91a04aSGustavo F. Padovan if (err < 0) 17679a91a04aSGustavo F. Padovan return err; 17689a91a04aSGustavo F. Padovan } 17699a91a04aSGustavo F. Padovan 17709a91a04aSGustavo F. Padovan if (chan->mode == L2CAP_MODE_STREAMING) { 17719a91a04aSGustavo F. Padovan l2cap_streaming_send(chan); 17729a91a04aSGustavo F. Padovan err = len; 17739a91a04aSGustavo F. Padovan break; 17749a91a04aSGustavo F. Padovan } 17759a91a04aSGustavo F. Padovan 1776e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 1777e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 17789a91a04aSGustavo F. Padovan err = len; 17799a91a04aSGustavo F. Padovan break; 17809a91a04aSGustavo F. Padovan } 17819a91a04aSGustavo F. Padovan 17829a91a04aSGustavo F. Padovan err = l2cap_ertm_send(chan); 17839a91a04aSGustavo F. Padovan if (err >= 0) 17849a91a04aSGustavo F. Padovan err = len; 17859a91a04aSGustavo F. Padovan 17869a91a04aSGustavo F. Padovan break; 17879a91a04aSGustavo F. Padovan 17889a91a04aSGustavo F. Padovan default: 17899a91a04aSGustavo F. Padovan BT_DBG("bad state %1.1x", chan->mode); 17909a91a04aSGustavo F. Padovan err = -EBADFD; 17919a91a04aSGustavo F. Padovan } 17929a91a04aSGustavo F. Padovan 17939a91a04aSGustavo F. Padovan return err; 17949a91a04aSGustavo F. Padovan } 17959a91a04aSGustavo F. Padovan 17960a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */ 17970a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) 17980a708f8fSGustavo F. Padovan { 17990a708f8fSGustavo F. Padovan struct sk_buff *nskb; 180048454079SGustavo F. Padovan struct l2cap_chan *chan; 18010a708f8fSGustavo F. Padovan 18020a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 18030a708f8fSGustavo F. Padovan 18043d57dc68SGustavo F. Padovan rcu_read_lock(); 18053d57dc68SGustavo F. Padovan 18063d57dc68SGustavo F. Padovan list_for_each_entry_rcu(chan, &conn->chan_l, list) { 180748454079SGustavo F. Padovan struct sock *sk = chan->sk; 1808715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_RAW) 18090a708f8fSGustavo F. Padovan continue; 18100a708f8fSGustavo F. Padovan 18110a708f8fSGustavo F. Padovan /* Don't send frame to the socket it came from */ 18120a708f8fSGustavo F. Padovan if (skb->sk == sk) 18130a708f8fSGustavo F. Padovan continue; 18140a708f8fSGustavo F. Padovan nskb = skb_clone(skb, GFP_ATOMIC); 18150a708f8fSGustavo F. Padovan if (!nskb) 18160a708f8fSGustavo F. Padovan continue; 18170a708f8fSGustavo F. Padovan 181823070494SGustavo F. Padovan if (chan->ops->recv(chan->data, nskb)) 18190a708f8fSGustavo F. Padovan kfree_skb(nskb); 18200a708f8fSGustavo F. Padovan } 18213d57dc68SGustavo F. Padovan 18223d57dc68SGustavo F. Padovan rcu_read_unlock(); 18230a708f8fSGustavo F. Padovan } 18240a708f8fSGustavo F. Padovan 18250a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */ 18260a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 18270a708f8fSGustavo F. Padovan u8 code, u8 ident, u16 dlen, void *data) 18280a708f8fSGustavo F. Padovan { 18290a708f8fSGustavo F. Padovan struct sk_buff *skb, **frag; 18300a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr *cmd; 18310a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 18320a708f8fSGustavo F. Padovan int len, count; 18330a708f8fSGustavo F. Padovan 18340a708f8fSGustavo F. Padovan BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", 18350a708f8fSGustavo F. Padovan conn, code, ident, dlen); 18360a708f8fSGustavo F. Padovan 18370a708f8fSGustavo F. Padovan len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; 18380a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 18390a708f8fSGustavo F. Padovan 18400a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 18410a708f8fSGustavo F. Padovan if (!skb) 18420a708f8fSGustavo F. Padovan return NULL; 18430a708f8fSGustavo F. Padovan 18440a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 18450a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); 18463300d9a9SClaudio Takahasi 18473300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 18483300d9a9SClaudio Takahasi lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); 18493300d9a9SClaudio Takahasi else 18500a708f8fSGustavo F. Padovan lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); 18510a708f8fSGustavo F. Padovan 18520a708f8fSGustavo F. Padovan cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); 18530a708f8fSGustavo F. Padovan cmd->code = code; 18540a708f8fSGustavo F. Padovan cmd->ident = ident; 18550a708f8fSGustavo F. Padovan cmd->len = cpu_to_le16(dlen); 18560a708f8fSGustavo F. Padovan 18570a708f8fSGustavo F. Padovan if (dlen) { 18580a708f8fSGustavo F. Padovan count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; 18590a708f8fSGustavo F. Padovan memcpy(skb_put(skb, count), data, count); 18600a708f8fSGustavo F. Padovan data += count; 18610a708f8fSGustavo F. Padovan } 18620a708f8fSGustavo F. Padovan 18630a708f8fSGustavo F. Padovan len -= skb->len; 18640a708f8fSGustavo F. Padovan 18650a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 18660a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 18670a708f8fSGustavo F. Padovan while (len) { 18680a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 18690a708f8fSGustavo F. Padovan 18700a708f8fSGustavo F. Padovan *frag = bt_skb_alloc(count, GFP_ATOMIC); 18710a708f8fSGustavo F. Padovan if (!*frag) 18720a708f8fSGustavo F. Padovan goto fail; 18730a708f8fSGustavo F. Padovan 18740a708f8fSGustavo F. Padovan memcpy(skb_put(*frag, count), data, count); 18750a708f8fSGustavo F. Padovan 18760a708f8fSGustavo F. Padovan len -= count; 18770a708f8fSGustavo F. Padovan data += count; 18780a708f8fSGustavo F. Padovan 18790a708f8fSGustavo F. Padovan frag = &(*frag)->next; 18800a708f8fSGustavo F. Padovan } 18810a708f8fSGustavo F. Padovan 18820a708f8fSGustavo F. Padovan return skb; 18830a708f8fSGustavo F. Padovan 18840a708f8fSGustavo F. Padovan fail: 18850a708f8fSGustavo F. Padovan kfree_skb(skb); 18860a708f8fSGustavo F. Padovan return NULL; 18870a708f8fSGustavo F. Padovan } 18880a708f8fSGustavo F. Padovan 18890a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) 18900a708f8fSGustavo F. Padovan { 18910a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 18920a708f8fSGustavo F. Padovan int len; 18930a708f8fSGustavo F. Padovan 18940a708f8fSGustavo F. Padovan len = L2CAP_CONF_OPT_SIZE + opt->len; 18950a708f8fSGustavo F. Padovan *ptr += len; 18960a708f8fSGustavo F. Padovan 18970a708f8fSGustavo F. Padovan *type = opt->type; 18980a708f8fSGustavo F. Padovan *olen = opt->len; 18990a708f8fSGustavo F. Padovan 19000a708f8fSGustavo F. Padovan switch (opt->len) { 19010a708f8fSGustavo F. Padovan case 1: 19020a708f8fSGustavo F. Padovan *val = *((u8 *) opt->val); 19030a708f8fSGustavo F. Padovan break; 19040a708f8fSGustavo F. Padovan 19050a708f8fSGustavo F. Padovan case 2: 19060a708f8fSGustavo F. Padovan *val = get_unaligned_le16(opt->val); 19070a708f8fSGustavo F. Padovan break; 19080a708f8fSGustavo F. Padovan 19090a708f8fSGustavo F. Padovan case 4: 19100a708f8fSGustavo F. Padovan *val = get_unaligned_le32(opt->val); 19110a708f8fSGustavo F. Padovan break; 19120a708f8fSGustavo F. Padovan 19130a708f8fSGustavo F. Padovan default: 19140a708f8fSGustavo F. Padovan *val = (unsigned long) opt->val; 19150a708f8fSGustavo F. Padovan break; 19160a708f8fSGustavo F. Padovan } 19170a708f8fSGustavo F. Padovan 19180a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); 19190a708f8fSGustavo F. Padovan return len; 19200a708f8fSGustavo F. Padovan } 19210a708f8fSGustavo F. Padovan 19220a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) 19230a708f8fSGustavo F. Padovan { 19240a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 19250a708f8fSGustavo F. Padovan 19260a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); 19270a708f8fSGustavo F. Padovan 19280a708f8fSGustavo F. Padovan opt->type = type; 19290a708f8fSGustavo F. Padovan opt->len = len; 19300a708f8fSGustavo F. Padovan 19310a708f8fSGustavo F. Padovan switch (len) { 19320a708f8fSGustavo F. Padovan case 1: 19330a708f8fSGustavo F. Padovan *((u8 *) opt->val) = val; 19340a708f8fSGustavo F. Padovan break; 19350a708f8fSGustavo F. Padovan 19360a708f8fSGustavo F. Padovan case 2: 19370a708f8fSGustavo F. Padovan put_unaligned_le16(val, opt->val); 19380a708f8fSGustavo F. Padovan break; 19390a708f8fSGustavo F. Padovan 19400a708f8fSGustavo F. Padovan case 4: 19410a708f8fSGustavo F. Padovan put_unaligned_le32(val, opt->val); 19420a708f8fSGustavo F. Padovan break; 19430a708f8fSGustavo F. Padovan 19440a708f8fSGustavo F. Padovan default: 19450a708f8fSGustavo F. Padovan memcpy(opt->val, (void *) val, len); 19460a708f8fSGustavo F. Padovan break; 19470a708f8fSGustavo F. Padovan } 19480a708f8fSGustavo F. Padovan 19490a708f8fSGustavo F. Padovan *ptr += L2CAP_CONF_OPT_SIZE + len; 19500a708f8fSGustavo F. Padovan } 19510a708f8fSGustavo F. Padovan 1952f89cef09SAndrei Emeltchenko static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) 1953f89cef09SAndrei Emeltchenko { 1954f89cef09SAndrei Emeltchenko struct l2cap_conf_efs efs; 1955f89cef09SAndrei Emeltchenko 1956f89cef09SAndrei Emeltchenko switch (chan->mode) { 1957f89cef09SAndrei Emeltchenko case L2CAP_MODE_ERTM: 1958f89cef09SAndrei Emeltchenko efs.id = chan->local_id; 1959f89cef09SAndrei Emeltchenko efs.stype = chan->local_stype; 1960f89cef09SAndrei Emeltchenko efs.msdu = cpu_to_le16(chan->local_msdu); 1961f89cef09SAndrei Emeltchenko efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); 1962f89cef09SAndrei Emeltchenko efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); 1963f89cef09SAndrei Emeltchenko efs.flush_to = cpu_to_le32(L2CAP_DEFAULT_FLUSH_TO); 1964f89cef09SAndrei Emeltchenko break; 1965f89cef09SAndrei Emeltchenko 1966f89cef09SAndrei Emeltchenko case L2CAP_MODE_STREAMING: 1967f89cef09SAndrei Emeltchenko efs.id = 1; 1968f89cef09SAndrei Emeltchenko efs.stype = L2CAP_SERV_BESTEFFORT; 1969f89cef09SAndrei Emeltchenko efs.msdu = cpu_to_le16(chan->local_msdu); 1970f89cef09SAndrei Emeltchenko efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); 1971f89cef09SAndrei Emeltchenko efs.acc_lat = 0; 1972f89cef09SAndrei Emeltchenko efs.flush_to = 0; 1973f89cef09SAndrei Emeltchenko break; 1974f89cef09SAndrei Emeltchenko 1975f89cef09SAndrei Emeltchenko default: 1976f89cef09SAndrei Emeltchenko return; 1977f89cef09SAndrei Emeltchenko } 1978f89cef09SAndrei Emeltchenko 1979f89cef09SAndrei Emeltchenko l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), 1980f89cef09SAndrei Emeltchenko (unsigned long) &efs); 1981f89cef09SAndrei Emeltchenko } 1982f89cef09SAndrei Emeltchenko 1983721c4181SGustavo F. Padovan static void l2cap_ack_timeout(struct work_struct *work) 19840a708f8fSGustavo F. Padovan { 1985721c4181SGustavo F. Padovan struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 1986721c4181SGustavo F. Padovan ack_timer.work); 19870a708f8fSGustavo F. Padovan 19882fb9b3d4SGustavo F. Padovan BT_DBG("chan %p", chan); 19892fb9b3d4SGustavo F. Padovan 1990721c4181SGustavo F. Padovan lock_sock(chan->sk); 1991525cd185SGustavo F. Padovan l2cap_send_ack(chan); 1992721c4181SGustavo F. Padovan release_sock(chan->sk); 19930a708f8fSGustavo F. Padovan } 19940a708f8fSGustavo F. Padovan 1995525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan) 19960a708f8fSGustavo F. Padovan { 199742e5c802SGustavo F. Padovan chan->expected_ack_seq = 0; 19986a026610SGustavo F. Padovan chan->unacked_frames = 0; 199942e5c802SGustavo F. Padovan chan->buffer_seq = 0; 20006a026610SGustavo F. Padovan chan->num_acked = 0; 20016a026610SGustavo F. Padovan chan->frames_sent = 0; 20020a708f8fSGustavo F. Padovan 2003721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); 2004721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); 2005721c4181SGustavo F. Padovan INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); 20060a708f8fSGustavo F. Padovan 2007f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->srej_q); 20080a708f8fSGustavo F. Padovan 200939d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 20100a708f8fSGustavo F. Padovan } 20110a708f8fSGustavo F. Padovan 20120a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) 20130a708f8fSGustavo F. Padovan { 20140a708f8fSGustavo F. Padovan switch (mode) { 20150a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 20160a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 20170a708f8fSGustavo F. Padovan if (l2cap_mode_supported(mode, remote_feat_mask)) 20180a708f8fSGustavo F. Padovan return mode; 20190a708f8fSGustavo F. Padovan /* fall through */ 20200a708f8fSGustavo F. Padovan default: 20210a708f8fSGustavo F. Padovan return L2CAP_MODE_BASIC; 20220a708f8fSGustavo F. Padovan } 20230a708f8fSGustavo F. Padovan } 20240a708f8fSGustavo F. Padovan 20256327eb98SAndrei Emeltchenko static inline bool __l2cap_ews_supported(struct l2cap_chan *chan) 20266327eb98SAndrei Emeltchenko { 20276327eb98SAndrei Emeltchenko return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; 20286327eb98SAndrei Emeltchenko } 20296327eb98SAndrei Emeltchenko 2030f89cef09SAndrei Emeltchenko static inline bool __l2cap_efs_supported(struct l2cap_chan *chan) 2031f89cef09SAndrei Emeltchenko { 2032f89cef09SAndrei Emeltchenko return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW; 2033f89cef09SAndrei Emeltchenko } 2034f89cef09SAndrei Emeltchenko 20356327eb98SAndrei Emeltchenko static inline void l2cap_txwin_setup(struct l2cap_chan *chan) 20366327eb98SAndrei Emeltchenko { 20376327eb98SAndrei Emeltchenko if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && 2038836be934SAndrei Emeltchenko __l2cap_ews_supported(chan)) { 20396327eb98SAndrei Emeltchenko /* use extended control field */ 20406327eb98SAndrei Emeltchenko set_bit(FLAG_EXT_CTRL, &chan->flags); 2041836be934SAndrei Emeltchenko chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; 2042836be934SAndrei Emeltchenko } else { 20436327eb98SAndrei Emeltchenko chan->tx_win = min_t(u16, chan->tx_win, 20446327eb98SAndrei Emeltchenko L2CAP_DEFAULT_TX_WINDOW); 2045836be934SAndrei Emeltchenko chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; 2046836be934SAndrei Emeltchenko } 20476327eb98SAndrei Emeltchenko } 20486327eb98SAndrei Emeltchenko 2049710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) 20500a708f8fSGustavo F. Padovan { 20510a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 20520c1bc5c6SGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = chan->mode }; 20530a708f8fSGustavo F. Padovan void *ptr = req->data; 2054c8f79162SAndrei Emeltchenko u16 size; 20550a708f8fSGustavo F. Padovan 205649208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 20570a708f8fSGustavo F. Padovan 205873ffa904SGustavo F. Padovan if (chan->num_conf_req || chan->num_conf_rsp) 20590a708f8fSGustavo F. Padovan goto done; 20600a708f8fSGustavo F. Padovan 20610c1bc5c6SGustavo F. Padovan switch (chan->mode) { 20620a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 20630a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 2064c1360a1cSGustavo F. Padovan if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) 20650a708f8fSGustavo F. Padovan break; 20660a708f8fSGustavo F. Padovan 2067f89cef09SAndrei Emeltchenko if (__l2cap_efs_supported(chan)) 2068f89cef09SAndrei Emeltchenko set_bit(FLAG_EFS_ENABLE, &chan->flags); 2069f89cef09SAndrei Emeltchenko 20700a708f8fSGustavo F. Padovan /* fall through */ 20710a708f8fSGustavo F. Padovan default: 20728c1d787bSGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); 20730a708f8fSGustavo F. Padovan break; 20740a708f8fSGustavo F. Padovan } 20750a708f8fSGustavo F. Padovan 20760a708f8fSGustavo F. Padovan done: 20770c1bc5c6SGustavo F. Padovan if (chan->imtu != L2CAP_DEFAULT_MTU) 20780c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 20790a708f8fSGustavo F. Padovan 20800c1bc5c6SGustavo F. Padovan switch (chan->mode) { 20810a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 20828c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && 20838c1d787bSGustavo F. Padovan !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) 20840a708f8fSGustavo F. Padovan break; 20850a708f8fSGustavo F. Padovan 20860a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_BASIC; 20870a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 20880a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 20890a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 20900a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 20910a708f8fSGustavo F. Padovan rfc.max_pdu_size = 0; 20920a708f8fSGustavo F. Padovan 20930a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 20940a708f8fSGustavo F. Padovan (unsigned long) &rfc); 20950a708f8fSGustavo F. Padovan break; 20960a708f8fSGustavo F. Padovan 20970a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 20980a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_ERTM; 209947d1ec61SGustavo F. Padovan rfc.max_transmit = chan->max_tx; 21000a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 21010a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 2102c8f79162SAndrei Emeltchenko 2103c8f79162SAndrei Emeltchenko size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - 2104c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2105c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2106c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2107c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 21080a708f8fSGustavo F. Padovan 21096327eb98SAndrei Emeltchenko l2cap_txwin_setup(chan); 21106327eb98SAndrei Emeltchenko 21116327eb98SAndrei Emeltchenko rfc.txwin_size = min_t(u16, chan->tx_win, 21126327eb98SAndrei Emeltchenko L2CAP_DEFAULT_TX_WINDOW); 21130a708f8fSGustavo F. Padovan 21140a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 21150a708f8fSGustavo F. Padovan (unsigned long) &rfc); 21160a708f8fSGustavo F. Padovan 2117f89cef09SAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) 2118f89cef09SAndrei Emeltchenko l2cap_add_opt_efs(&ptr, chan); 2119f89cef09SAndrei Emeltchenko 21208c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 21210a708f8fSGustavo F. Padovan break; 21220a708f8fSGustavo F. Padovan 212347d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 2124c1360a1cSGustavo F. Padovan test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { 212547d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 212647d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 21270a708f8fSGustavo F. Padovan } 21286327eb98SAndrei Emeltchenko 21296327eb98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 21306327eb98SAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, 21316327eb98SAndrei Emeltchenko chan->tx_win); 21320a708f8fSGustavo F. Padovan break; 21330a708f8fSGustavo F. Padovan 21340a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 21350a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_STREAMING; 21360a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 21370a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 21380a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 21390a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 2140c8f79162SAndrei Emeltchenko 2141c8f79162SAndrei Emeltchenko size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - 2142c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2143c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2144c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2145c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 21460a708f8fSGustavo F. Padovan 21470a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 21480a708f8fSGustavo F. Padovan (unsigned long) &rfc); 21490a708f8fSGustavo F. Padovan 2150f89cef09SAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) 2151f89cef09SAndrei Emeltchenko l2cap_add_opt_efs(&ptr, chan); 2152f89cef09SAndrei Emeltchenko 21538c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 21540a708f8fSGustavo F. Padovan break; 21550a708f8fSGustavo F. Padovan 215647d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 2157c1360a1cSGustavo F. Padovan test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { 215847d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 215947d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 21600a708f8fSGustavo F. Padovan } 21610a708f8fSGustavo F. Padovan break; 21620a708f8fSGustavo F. Padovan } 21630a708f8fSGustavo F. Padovan 2164fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 21650a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0); 21660a708f8fSGustavo F. Padovan 21670a708f8fSGustavo F. Padovan return ptr - data; 21680a708f8fSGustavo F. Padovan } 21690a708f8fSGustavo F. Padovan 217073ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) 21710a708f8fSGustavo F. Padovan { 21720a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 21730a708f8fSGustavo F. Padovan void *ptr = rsp->data; 217473ffa904SGustavo F. Padovan void *req = chan->conf_req; 217573ffa904SGustavo F. Padovan int len = chan->conf_len; 21760a708f8fSGustavo F. Padovan int type, hint, olen; 21770a708f8fSGustavo F. Padovan unsigned long val; 21780a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 217942dceae2SAndrei Emeltchenko struct l2cap_conf_efs efs; 218042dceae2SAndrei Emeltchenko u8 remote_efs = 0; 21810a708f8fSGustavo F. Padovan u16 mtu = L2CAP_DEFAULT_MTU; 21820a708f8fSGustavo F. Padovan u16 result = L2CAP_CONF_SUCCESS; 2183c8f79162SAndrei Emeltchenko u16 size; 21840a708f8fSGustavo F. Padovan 218573ffa904SGustavo F. Padovan BT_DBG("chan %p", chan); 21860a708f8fSGustavo F. Padovan 21870a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 21880a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&req, &type, &olen, &val); 21890a708f8fSGustavo F. Padovan 21900a708f8fSGustavo F. Padovan hint = type & L2CAP_CONF_HINT; 21910a708f8fSGustavo F. Padovan type &= L2CAP_CONF_MASK; 21920a708f8fSGustavo F. Padovan 21930a708f8fSGustavo F. Padovan switch (type) { 21940a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 21950a708f8fSGustavo F. Padovan mtu = val; 21960a708f8fSGustavo F. Padovan break; 21970a708f8fSGustavo F. Padovan 21980a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 21990c1bc5c6SGustavo F. Padovan chan->flush_to = val; 22000a708f8fSGustavo F. Padovan break; 22010a708f8fSGustavo F. Padovan 22020a708f8fSGustavo F. Padovan case L2CAP_CONF_QOS: 22030a708f8fSGustavo F. Padovan break; 22040a708f8fSGustavo F. Padovan 22050a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 22060a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 22070a708f8fSGustavo F. Padovan memcpy(&rfc, (void *) val, olen); 22080a708f8fSGustavo F. Padovan break; 22090a708f8fSGustavo F. Padovan 22100a708f8fSGustavo F. Padovan case L2CAP_CONF_FCS: 22110a708f8fSGustavo F. Padovan if (val == L2CAP_FCS_NONE) 2212c1360a1cSGustavo F. Padovan set_bit(CONF_NO_FCS_RECV, &chan->conf_state); 221342dceae2SAndrei Emeltchenko break; 22140a708f8fSGustavo F. Padovan 221542dceae2SAndrei Emeltchenko case L2CAP_CONF_EFS: 221642dceae2SAndrei Emeltchenko remote_efs = 1; 221742dceae2SAndrei Emeltchenko if (olen == sizeof(efs)) 221842dceae2SAndrei Emeltchenko memcpy(&efs, (void *) val, olen); 22190a708f8fSGustavo F. Padovan break; 22200a708f8fSGustavo F. Padovan 22216327eb98SAndrei Emeltchenko case L2CAP_CONF_EWS: 22226327eb98SAndrei Emeltchenko if (!enable_hs) 22236327eb98SAndrei Emeltchenko return -ECONNREFUSED; 22246327eb98SAndrei Emeltchenko 22256327eb98SAndrei Emeltchenko set_bit(FLAG_EXT_CTRL, &chan->flags); 22266327eb98SAndrei Emeltchenko set_bit(CONF_EWS_RECV, &chan->conf_state); 2227836be934SAndrei Emeltchenko chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; 22286327eb98SAndrei Emeltchenko chan->remote_tx_win = val; 22290a708f8fSGustavo F. Padovan break; 22300a708f8fSGustavo F. Padovan 22310a708f8fSGustavo F. Padovan default: 22320a708f8fSGustavo F. Padovan if (hint) 22330a708f8fSGustavo F. Padovan break; 22340a708f8fSGustavo F. Padovan 22350a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNKNOWN; 22360a708f8fSGustavo F. Padovan *((u8 *) ptr++) = type; 22370a708f8fSGustavo F. Padovan break; 22380a708f8fSGustavo F. Padovan } 22390a708f8fSGustavo F. Padovan } 22400a708f8fSGustavo F. Padovan 224173ffa904SGustavo F. Padovan if (chan->num_conf_rsp || chan->num_conf_req > 1) 22420a708f8fSGustavo F. Padovan goto done; 22430a708f8fSGustavo F. Padovan 22440c1bc5c6SGustavo F. Padovan switch (chan->mode) { 22450a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 22460a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 2247c1360a1cSGustavo F. Padovan if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { 22480c1bc5c6SGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, 22498c1d787bSGustavo F. Padovan chan->conn->feat_mask); 22500a708f8fSGustavo F. Padovan break; 22510a708f8fSGustavo F. Padovan } 22520a708f8fSGustavo F. Padovan 225342dceae2SAndrei Emeltchenko if (remote_efs) { 225442dceae2SAndrei Emeltchenko if (__l2cap_efs_supported(chan)) 225542dceae2SAndrei Emeltchenko set_bit(FLAG_EFS_ENABLE, &chan->flags); 225642dceae2SAndrei Emeltchenko else 225742dceae2SAndrei Emeltchenko return -ECONNREFUSED; 225842dceae2SAndrei Emeltchenko } 225942dceae2SAndrei Emeltchenko 22600c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) 22610a708f8fSGustavo F. Padovan return -ECONNREFUSED; 22620a708f8fSGustavo F. Padovan 22630a708f8fSGustavo F. Padovan break; 22640a708f8fSGustavo F. Padovan } 22650a708f8fSGustavo F. Padovan 22660a708f8fSGustavo F. Padovan done: 22670c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) { 22680a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 22690c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 22700a708f8fSGustavo F. Padovan 227173ffa904SGustavo F. Padovan if (chan->num_conf_rsp == 1) 22720a708f8fSGustavo F. Padovan return -ECONNREFUSED; 22730a708f8fSGustavo F. Padovan 22740a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 22750a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 22760a708f8fSGustavo F. Padovan } 22770a708f8fSGustavo F. Padovan 22780a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) { 22790a708f8fSGustavo F. Padovan /* Configure output options and let the other side know 22800a708f8fSGustavo F. Padovan * which ones we don't like. */ 22810a708f8fSGustavo F. Padovan 22820a708f8fSGustavo F. Padovan if (mtu < L2CAP_DEFAULT_MIN_MTU) 22830a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 22840a708f8fSGustavo F. Padovan else { 22850c1bc5c6SGustavo F. Padovan chan->omtu = mtu; 2286c1360a1cSGustavo F. Padovan set_bit(CONF_MTU_DONE, &chan->conf_state); 22870a708f8fSGustavo F. Padovan } 22880c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); 22890a708f8fSGustavo F. Padovan 229042dceae2SAndrei Emeltchenko if (remote_efs) { 229142dceae2SAndrei Emeltchenko if (chan->local_stype != L2CAP_SERV_NOTRAFIC && 229242dceae2SAndrei Emeltchenko efs.stype != L2CAP_SERV_NOTRAFIC && 229342dceae2SAndrei Emeltchenko efs.stype != chan->local_stype) { 229442dceae2SAndrei Emeltchenko 229542dceae2SAndrei Emeltchenko result = L2CAP_CONF_UNACCEPT; 229642dceae2SAndrei Emeltchenko 229742dceae2SAndrei Emeltchenko if (chan->num_conf_req >= 1) 229842dceae2SAndrei Emeltchenko return -ECONNREFUSED; 229942dceae2SAndrei Emeltchenko 230042dceae2SAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, 230142dceae2SAndrei Emeltchenko sizeof(efs), 230242dceae2SAndrei Emeltchenko (unsigned long) &efs); 23030e8b207eSAndrei Emeltchenko } else { 23043e6b3b95SGustavo F. Padovan /* Send PENDING Conf Rsp */ 23050e8b207eSAndrei Emeltchenko result = L2CAP_CONF_PENDING; 23060e8b207eSAndrei Emeltchenko set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 230742dceae2SAndrei Emeltchenko } 230842dceae2SAndrei Emeltchenko } 230942dceae2SAndrei Emeltchenko 23100a708f8fSGustavo F. Padovan switch (rfc.mode) { 23110a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 231247d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2313c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 23140a708f8fSGustavo F. Padovan break; 23150a708f8fSGustavo F. Padovan 23160a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 23176327eb98SAndrei Emeltchenko if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) 23182c03a7a4SGustavo F. Padovan chan->remote_tx_win = rfc.txwin_size; 23196327eb98SAndrei Emeltchenko else 23206327eb98SAndrei Emeltchenko rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; 23216327eb98SAndrei Emeltchenko 23222c03a7a4SGustavo F. Padovan chan->remote_max_tx = rfc.max_transmit; 23230a708f8fSGustavo F. Padovan 2324c8f79162SAndrei Emeltchenko size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), 2325c8f79162SAndrei Emeltchenko chan->conn->mtu - 2326c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2327c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2328c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2329c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 2330c8f79162SAndrei Emeltchenko chan->remote_mps = size; 23310a708f8fSGustavo F. Padovan 23320a708f8fSGustavo F. Padovan rfc.retrans_timeout = 23330a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO); 23340a708f8fSGustavo F. Padovan rfc.monitor_timeout = 23350a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO); 23360a708f8fSGustavo F. Padovan 2337c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 23380a708f8fSGustavo F. Padovan 23390a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 23400a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 23410a708f8fSGustavo F. Padovan 234242dceae2SAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { 234342dceae2SAndrei Emeltchenko chan->remote_id = efs.id; 234442dceae2SAndrei Emeltchenko chan->remote_stype = efs.stype; 234542dceae2SAndrei Emeltchenko chan->remote_msdu = le16_to_cpu(efs.msdu); 234642dceae2SAndrei Emeltchenko chan->remote_flush_to = 234742dceae2SAndrei Emeltchenko le32_to_cpu(efs.flush_to); 234842dceae2SAndrei Emeltchenko chan->remote_acc_lat = 234942dceae2SAndrei Emeltchenko le32_to_cpu(efs.acc_lat); 235042dceae2SAndrei Emeltchenko chan->remote_sdu_itime = 235142dceae2SAndrei Emeltchenko le32_to_cpu(efs.sdu_itime); 235242dceae2SAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, 235342dceae2SAndrei Emeltchenko sizeof(efs), (unsigned long) &efs); 235442dceae2SAndrei Emeltchenko } 23550a708f8fSGustavo F. Padovan break; 23560a708f8fSGustavo F. Padovan 23570a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 2358c8f79162SAndrei Emeltchenko size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), 2359c8f79162SAndrei Emeltchenko chan->conn->mtu - 2360c8f79162SAndrei Emeltchenko L2CAP_EXT_HDR_SIZE - 2361c8f79162SAndrei Emeltchenko L2CAP_SDULEN_SIZE - 2362c8f79162SAndrei Emeltchenko L2CAP_FCS_SIZE); 2363c8f79162SAndrei Emeltchenko rfc.max_pdu_size = cpu_to_le16(size); 2364c8f79162SAndrei Emeltchenko chan->remote_mps = size; 23650a708f8fSGustavo F. Padovan 2366c1360a1cSGustavo F. Padovan set_bit(CONF_MODE_DONE, &chan->conf_state); 23670a708f8fSGustavo F. Padovan 23680a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 23690a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 23700a708f8fSGustavo F. Padovan 23710a708f8fSGustavo F. Padovan break; 23720a708f8fSGustavo F. Padovan 23730a708f8fSGustavo F. Padovan default: 23740a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 23750a708f8fSGustavo F. Padovan 23760a708f8fSGustavo F. Padovan memset(&rfc, 0, sizeof(rfc)); 23770c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 23780a708f8fSGustavo F. Padovan } 23790a708f8fSGustavo F. Padovan 23800a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) 2381c1360a1cSGustavo F. Padovan set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 23820a708f8fSGustavo F. Padovan } 2383fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 23840a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 23850a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(0x0000); 23860a708f8fSGustavo F. Padovan 23870a708f8fSGustavo F. Padovan return ptr - data; 23880a708f8fSGustavo F. Padovan } 23890a708f8fSGustavo F. Padovan 2390b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result) 23910a708f8fSGustavo F. Padovan { 23920a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 23930a708f8fSGustavo F. Padovan void *ptr = req->data; 23940a708f8fSGustavo F. Padovan int type, olen; 23950a708f8fSGustavo F. Padovan unsigned long val; 239636e999a8SMat Martineau struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 239766af7aafSAndrei Emeltchenko struct l2cap_conf_efs efs; 23980a708f8fSGustavo F. Padovan 2399fe4128e0SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data); 24000a708f8fSGustavo F. Padovan 24010a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 24020a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 24030a708f8fSGustavo F. Padovan 24040a708f8fSGustavo F. Padovan switch (type) { 24050a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 24060a708f8fSGustavo F. Padovan if (val < L2CAP_DEFAULT_MIN_MTU) { 24070a708f8fSGustavo F. Padovan *result = L2CAP_CONF_UNACCEPT; 24080c1bc5c6SGustavo F. Padovan chan->imtu = L2CAP_DEFAULT_MIN_MTU; 24090a708f8fSGustavo F. Padovan } else 24100c1bc5c6SGustavo F. Padovan chan->imtu = val; 24110c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 24120a708f8fSGustavo F. Padovan break; 24130a708f8fSGustavo F. Padovan 24140a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 24150c1bc5c6SGustavo F. Padovan chan->flush_to = val; 24160a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 24170c1bc5c6SGustavo F. Padovan 2, chan->flush_to); 24180a708f8fSGustavo F. Padovan break; 24190a708f8fSGustavo F. Padovan 24200a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 24210a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 24220a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 24230a708f8fSGustavo F. Padovan 2424c1360a1cSGustavo F. Padovan if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && 24250c1bc5c6SGustavo F. Padovan rfc.mode != chan->mode) 24260a708f8fSGustavo F. Padovan return -ECONNREFUSED; 24270a708f8fSGustavo F. Padovan 242847d1ec61SGustavo F. Padovan chan->fcs = 0; 24290a708f8fSGustavo F. Padovan 24300a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 24310a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 24320a708f8fSGustavo F. Padovan break; 24336327eb98SAndrei Emeltchenko 24346327eb98SAndrei Emeltchenko case L2CAP_CONF_EWS: 24356327eb98SAndrei Emeltchenko chan->tx_win = min_t(u16, val, 24366327eb98SAndrei Emeltchenko L2CAP_DEFAULT_EXT_WINDOW); 24373e6b3b95SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, 24383e6b3b95SGustavo F. Padovan chan->tx_win); 24396327eb98SAndrei Emeltchenko break; 244066af7aafSAndrei Emeltchenko 244166af7aafSAndrei Emeltchenko case L2CAP_CONF_EFS: 244266af7aafSAndrei Emeltchenko if (olen == sizeof(efs)) 244366af7aafSAndrei Emeltchenko memcpy(&efs, (void *)val, olen); 244466af7aafSAndrei Emeltchenko 244566af7aafSAndrei Emeltchenko if (chan->local_stype != L2CAP_SERV_NOTRAFIC && 244666af7aafSAndrei Emeltchenko efs.stype != L2CAP_SERV_NOTRAFIC && 244766af7aafSAndrei Emeltchenko efs.stype != chan->local_stype) 244866af7aafSAndrei Emeltchenko return -ECONNREFUSED; 244966af7aafSAndrei Emeltchenko 245066af7aafSAndrei Emeltchenko l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, 245166af7aafSAndrei Emeltchenko sizeof(efs), (unsigned long) &efs); 245266af7aafSAndrei Emeltchenko break; 24530a708f8fSGustavo F. Padovan } 24540a708f8fSGustavo F. Padovan } 24550a708f8fSGustavo F. Padovan 24560c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) 24570a708f8fSGustavo F. Padovan return -ECONNREFUSED; 24580a708f8fSGustavo F. Padovan 24590c1bc5c6SGustavo F. Padovan chan->mode = rfc.mode; 24600a708f8fSGustavo F. Padovan 24610e8b207eSAndrei Emeltchenko if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { 24620a708f8fSGustavo F. Padovan switch (rfc.mode) { 24630a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 246447d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 246547d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 246647d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 246766af7aafSAndrei Emeltchenko 246866af7aafSAndrei Emeltchenko if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { 246966af7aafSAndrei Emeltchenko chan->local_msdu = le16_to_cpu(efs.msdu); 247066af7aafSAndrei Emeltchenko chan->local_sdu_itime = 247166af7aafSAndrei Emeltchenko le32_to_cpu(efs.sdu_itime); 247266af7aafSAndrei Emeltchenko chan->local_acc_lat = le32_to_cpu(efs.acc_lat); 247366af7aafSAndrei Emeltchenko chan->local_flush_to = 247466af7aafSAndrei Emeltchenko le32_to_cpu(efs.flush_to); 247566af7aafSAndrei Emeltchenko } 24760a708f8fSGustavo F. Padovan break; 247766af7aafSAndrei Emeltchenko 24780a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 247947d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 24800a708f8fSGustavo F. Padovan } 24810a708f8fSGustavo F. Padovan } 24820a708f8fSGustavo F. Padovan 2483fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 24840a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0x0000); 24850a708f8fSGustavo F. Padovan 24860a708f8fSGustavo F. Padovan return ptr - data; 24870a708f8fSGustavo F. Padovan } 24880a708f8fSGustavo F. Padovan 2489fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags) 24900a708f8fSGustavo F. Padovan { 24910a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 24920a708f8fSGustavo F. Padovan void *ptr = rsp->data; 24930a708f8fSGustavo F. Padovan 2494fe4128e0SGustavo F. Padovan BT_DBG("chan %p", chan); 24950a708f8fSGustavo F. Padovan 2496fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 24970a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 24980a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(flags); 24990a708f8fSGustavo F. Padovan 25000a708f8fSGustavo F. Padovan return ptr - data; 25010a708f8fSGustavo F. Padovan } 25020a708f8fSGustavo F. Padovan 25038c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) 2504710f9b0aSGustavo F. Padovan { 2505710f9b0aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 25068c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 2507710f9b0aSGustavo F. Padovan u8 buf[128]; 2508710f9b0aSGustavo F. Padovan 2509fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 2510fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 2511710f9b0aSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 2512710f9b0aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 2513710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 2514710f9b0aSGustavo F. Padovan L2CAP_CONN_RSP, sizeof(rsp), &rsp); 2515710f9b0aSGustavo F. Padovan 2516c1360a1cSGustavo F. Padovan if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) 2517710f9b0aSGustavo F. Padovan return; 2518710f9b0aSGustavo F. Padovan 2519710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 2520710f9b0aSGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 2521710f9b0aSGustavo F. Padovan chan->num_conf_req++; 2522710f9b0aSGustavo F. Padovan } 2523710f9b0aSGustavo F. Padovan 252447d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len) 25250a708f8fSGustavo F. Padovan { 25260a708f8fSGustavo F. Padovan int type, olen; 25270a708f8fSGustavo F. Padovan unsigned long val; 25280a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 25290a708f8fSGustavo F. Padovan 253047d1ec61SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); 25310a708f8fSGustavo F. Padovan 25320c1bc5c6SGustavo F. Padovan if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) 25330a708f8fSGustavo F. Padovan return; 25340a708f8fSGustavo F. Padovan 25350a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 25360a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 25370a708f8fSGustavo F. Padovan 25380a708f8fSGustavo F. Padovan switch (type) { 25390a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 25400a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 25410a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 25420a708f8fSGustavo F. Padovan goto done; 25430a708f8fSGustavo F. Padovan } 25440a708f8fSGustavo F. Padovan } 25450a708f8fSGustavo F. Padovan 254636e999a8SMat Martineau /* Use sane default values in case a misbehaving remote device 254736e999a8SMat Martineau * did not send an RFC option. 254836e999a8SMat Martineau */ 254936e999a8SMat Martineau rfc.mode = chan->mode; 255036e999a8SMat Martineau rfc.retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); 255136e999a8SMat Martineau rfc.monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); 255236e999a8SMat Martineau rfc.max_pdu_size = cpu_to_le16(chan->imtu); 255336e999a8SMat Martineau 255436e999a8SMat Martineau BT_ERR("Expected RFC option was not found, using defaults"); 255536e999a8SMat Martineau 25560a708f8fSGustavo F. Padovan done: 25570a708f8fSGustavo F. Padovan switch (rfc.mode) { 25580a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 255947d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 256047d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 256147d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 25620a708f8fSGustavo F. Padovan break; 25630a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 256447d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 25650a708f8fSGustavo F. Padovan } 25660a708f8fSGustavo F. Padovan } 25670a708f8fSGustavo F. Padovan 25680a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 25690a708f8fSGustavo F. Padovan { 2570e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; 25710a708f8fSGustavo F. Padovan 2572e2fd318eSIlia Kolomisnky if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) 25730a708f8fSGustavo F. Padovan return 0; 25740a708f8fSGustavo F. Padovan 25750a708f8fSGustavo F. Padovan if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && 25760a708f8fSGustavo F. Padovan cmd->ident == conn->info_ident) { 2577371fd835SUlisses Furquim __cancel_delayed_work(&conn->info_timer); 25780a708f8fSGustavo F. Padovan 25790a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 25800a708f8fSGustavo F. Padovan conn->info_ident = 0; 25810a708f8fSGustavo F. Padovan 25820a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 25830a708f8fSGustavo F. Padovan } 25840a708f8fSGustavo F. Padovan 25850a708f8fSGustavo F. Padovan return 0; 25860a708f8fSGustavo F. Padovan } 25870a708f8fSGustavo F. Padovan 25880a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 25890a708f8fSGustavo F. Padovan { 25900a708f8fSGustavo F. Padovan struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; 25910a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 259223691d75SGustavo F. Padovan struct l2cap_chan *chan = NULL, *pchan; 25930a708f8fSGustavo F. Padovan struct sock *parent, *sk = NULL; 25940a708f8fSGustavo F. Padovan int result, status = L2CAP_CS_NO_INFO; 25950a708f8fSGustavo F. Padovan 25960a708f8fSGustavo F. Padovan u16 dcid = 0, scid = __le16_to_cpu(req->scid); 25970a708f8fSGustavo F. Padovan __le16 psm = req->psm; 25980a708f8fSGustavo F. Padovan 25990a708f8fSGustavo F. Padovan BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid); 26000a708f8fSGustavo F. Padovan 26010a708f8fSGustavo F. Padovan /* Check if we have socket listening on psm */ 260223691d75SGustavo F. Padovan pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src); 260323691d75SGustavo F. Padovan if (!pchan) { 26040a708f8fSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 26050a708f8fSGustavo F. Padovan goto sendresp; 26060a708f8fSGustavo F. Padovan } 26070a708f8fSGustavo F. Padovan 260823691d75SGustavo F. Padovan parent = pchan->sk; 260923691d75SGustavo F. Padovan 2610aa2ac881SGustavo F. Padovan lock_sock(parent); 26110a708f8fSGustavo F. Padovan 26120a708f8fSGustavo F. Padovan /* Check if the ACL is secure enough (if not SDP) */ 26130a708f8fSGustavo F. Padovan if (psm != cpu_to_le16(0x0001) && 26140a708f8fSGustavo F. Padovan !hci_conn_check_link_mode(conn->hcon)) { 26159f5a0d7bSAndrei Emeltchenko conn->disc_reason = HCI_ERROR_AUTH_FAILURE; 26160a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 26170a708f8fSGustavo F. Padovan goto response; 26180a708f8fSGustavo F. Padovan } 26190a708f8fSGustavo F. Padovan 26200a708f8fSGustavo F. Padovan result = L2CAP_CR_NO_MEM; 26210a708f8fSGustavo F. Padovan 26220a708f8fSGustavo F. Padovan /* Check for backlog size */ 26230a708f8fSGustavo F. Padovan if (sk_acceptq_is_full(parent)) { 26240a708f8fSGustavo F. Padovan BT_DBG("backlog full %d", parent->sk_ack_backlog); 26250a708f8fSGustavo F. Padovan goto response; 26260a708f8fSGustavo F. Padovan } 26270a708f8fSGustavo F. Padovan 262880808e43SGustavo F. Padovan chan = pchan->ops->new_connection(pchan->data); 262980808e43SGustavo F. Padovan if (!chan) 26300a708f8fSGustavo F. Padovan goto response; 26310a708f8fSGustavo F. Padovan 263280808e43SGustavo F. Padovan sk = chan->sk; 263380808e43SGustavo F. Padovan 26340a708f8fSGustavo F. Padovan /* Check if we already have channel with that dcid */ 2635baa7e1faSGustavo F. Padovan if (__l2cap_get_chan_by_dcid(conn, scid)) { 26360a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 2637ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 26380a708f8fSGustavo F. Padovan goto response; 26390a708f8fSGustavo F. Padovan } 26400a708f8fSGustavo F. Padovan 26410a708f8fSGustavo F. Padovan hci_conn_hold(conn->hcon); 26420a708f8fSGustavo F. Padovan 26430a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->src, conn->src); 26440a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->dst, conn->dst); 2645fe4128e0SGustavo F. Padovan chan->psm = psm; 2646fe4128e0SGustavo F. Padovan chan->dcid = scid; 26470a708f8fSGustavo F. Padovan 2648d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 2649d1010240SGustavo F. Padovan 26503d57dc68SGustavo F. Padovan l2cap_chan_add(conn, chan); 265148454079SGustavo F. Padovan 2652fe4128e0SGustavo F. Padovan dcid = chan->scid; 26530a708f8fSGustavo F. Padovan 2654c9b66675SGustavo F. Padovan __set_chan_timer(chan, sk->sk_sndtimeo); 26550a708f8fSGustavo F. Padovan 2656fc7f8a7eSGustavo F. Padovan chan->ident = cmd->ident; 26570a708f8fSGustavo F. Padovan 26580a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { 2659d45fc423SGustavo F. Padovan if (l2cap_chan_check_security(chan)) { 26600a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 266189bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECT2); 26620a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 26630a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHOR_PEND; 26640a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 26650a708f8fSGustavo F. Padovan } else { 266689bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONFIG); 26670a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 26680a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 26690a708f8fSGustavo F. Padovan } 26700a708f8fSGustavo F. Padovan } else { 267189bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECT2); 26720a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 26730a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHEN_PEND; 26740a708f8fSGustavo F. Padovan } 26750a708f8fSGustavo F. Padovan } else { 267689bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECT2); 26770a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 26780a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 26790a708f8fSGustavo F. Padovan } 26800a708f8fSGustavo F. Padovan 26810a708f8fSGustavo F. Padovan response: 2682aa2ac881SGustavo F. Padovan release_sock(parent); 26830a708f8fSGustavo F. Padovan 26840a708f8fSGustavo F. Padovan sendresp: 26850a708f8fSGustavo F. Padovan rsp.scid = cpu_to_le16(scid); 26860a708f8fSGustavo F. Padovan rsp.dcid = cpu_to_le16(dcid); 26870a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 26880a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(status); 26890a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); 26900a708f8fSGustavo F. Padovan 26910a708f8fSGustavo F. Padovan if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { 26920a708f8fSGustavo F. Padovan struct l2cap_info_req info; 26930a708f8fSGustavo F. Padovan info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 26940a708f8fSGustavo F. Padovan 26950a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 26960a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 26970a708f8fSGustavo F. Padovan 2698030013d8SGustavo F. Padovan schedule_delayed_work(&conn->info_timer, 26990a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 27000a708f8fSGustavo F. Padovan 27010a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 27020a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(info), &info); 27030a708f8fSGustavo F. Padovan } 27040a708f8fSGustavo F. Padovan 2705c1360a1cSGustavo F. Padovan if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && 27060a708f8fSGustavo F. Padovan result == L2CAP_CR_SUCCESS) { 27070a708f8fSGustavo F. Padovan u8 buf[128]; 2708c1360a1cSGustavo F. Padovan set_bit(CONF_REQ_SENT, &chan->conf_state); 27090a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 271073ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 271173ffa904SGustavo F. Padovan chan->num_conf_req++; 27120a708f8fSGustavo F. Padovan } 27130a708f8fSGustavo F. Padovan 27140a708f8fSGustavo F. Padovan return 0; 27150a708f8fSGustavo F. Padovan } 27160a708f8fSGustavo F. Padovan 27170a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 27180a708f8fSGustavo F. Padovan { 27190a708f8fSGustavo F. Padovan struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; 27200a708f8fSGustavo F. Padovan u16 scid, dcid, result, status; 272148454079SGustavo F. Padovan struct l2cap_chan *chan; 27220a708f8fSGustavo F. Padovan struct sock *sk; 27230a708f8fSGustavo F. Padovan u8 req[128]; 27240a708f8fSGustavo F. Padovan 27250a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 27260a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 27270a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 27280a708f8fSGustavo F. Padovan status = __le16_to_cpu(rsp->status); 27290a708f8fSGustavo F. Padovan 27300a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status); 27310a708f8fSGustavo F. Padovan 27320a708f8fSGustavo F. Padovan if (scid) { 2733baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 273448454079SGustavo F. Padovan if (!chan) 27350a708f8fSGustavo F. Padovan return -EFAULT; 27360a708f8fSGustavo F. Padovan } else { 2737baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_ident(conn, cmd->ident); 273848454079SGustavo F. Padovan if (!chan) 27390a708f8fSGustavo F. Padovan return -EFAULT; 27400a708f8fSGustavo F. Padovan } 27410a708f8fSGustavo F. Padovan 274248454079SGustavo F. Padovan sk = chan->sk; 274348454079SGustavo F. Padovan 27440a708f8fSGustavo F. Padovan switch (result) { 27450a708f8fSGustavo F. Padovan case L2CAP_CR_SUCCESS: 274689bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONFIG); 2747fc7f8a7eSGustavo F. Padovan chan->ident = 0; 2748fe4128e0SGustavo F. Padovan chan->dcid = dcid; 2749c1360a1cSGustavo F. Padovan clear_bit(CONF_CONNECT_PEND, &chan->conf_state); 27500a708f8fSGustavo F. Padovan 2751c1360a1cSGustavo F. Padovan if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) 27520a708f8fSGustavo F. Padovan break; 27530a708f8fSGustavo F. Padovan 27540a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 275573ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, req), req); 275673ffa904SGustavo F. Padovan chan->num_conf_req++; 27570a708f8fSGustavo F. Padovan break; 27580a708f8fSGustavo F. Padovan 27590a708f8fSGustavo F. Padovan case L2CAP_CR_PEND: 2760c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 27610a708f8fSGustavo F. Padovan break; 27620a708f8fSGustavo F. Padovan 27630a708f8fSGustavo F. Padovan default: 276448454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNREFUSED); 27650a708f8fSGustavo F. Padovan break; 27660a708f8fSGustavo F. Padovan } 27670a708f8fSGustavo F. Padovan 2768aa2ac881SGustavo F. Padovan release_sock(sk); 27690a708f8fSGustavo F. Padovan return 0; 27700a708f8fSGustavo F. Padovan } 27710a708f8fSGustavo F. Padovan 277247d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan) 27730a708f8fSGustavo F. Padovan { 27740a708f8fSGustavo F. Padovan /* FCS is enabled only in ERTM or streaming mode, if one or both 27750a708f8fSGustavo F. Padovan * sides request it. 27760a708f8fSGustavo F. Padovan */ 27770c1bc5c6SGustavo F. Padovan if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) 277847d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2779c1360a1cSGustavo F. Padovan else if (!test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) 278047d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_CRC16; 27810a708f8fSGustavo F. Padovan } 27820a708f8fSGustavo F. Padovan 27830a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 27840a708f8fSGustavo F. Padovan { 27850a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; 27860a708f8fSGustavo F. Padovan u16 dcid, flags; 27870a708f8fSGustavo F. Padovan u8 rsp[64]; 278848454079SGustavo F. Padovan struct l2cap_chan *chan; 27890a708f8fSGustavo F. Padovan struct sock *sk; 27900a708f8fSGustavo F. Padovan int len; 27910a708f8fSGustavo F. Padovan 27920a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 27930a708f8fSGustavo F. Padovan flags = __le16_to_cpu(req->flags); 27940a708f8fSGustavo F. Padovan 27950a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); 27960a708f8fSGustavo F. Padovan 2797baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 279848454079SGustavo F. Padovan if (!chan) 27990a708f8fSGustavo F. Padovan return -ENOENT; 28000a708f8fSGustavo F. Padovan 280148454079SGustavo F. Padovan sk = chan->sk; 280248454079SGustavo F. Padovan 2803033b1142SDavid S. Miller if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { 2804e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_cid rej; 28050a708f8fSGustavo F. Padovan 2806e2fd318eSIlia Kolomisnky rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID); 2807e2fd318eSIlia Kolomisnky rej.scid = cpu_to_le16(chan->scid); 2808e2fd318eSIlia Kolomisnky rej.dcid = cpu_to_le16(chan->dcid); 2809e2fd318eSIlia Kolomisnky 28100a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, 28110a708f8fSGustavo F. Padovan sizeof(rej), &rej); 28120a708f8fSGustavo F. Padovan goto unlock; 28130a708f8fSGustavo F. Padovan } 28140a708f8fSGustavo F. Padovan 28150a708f8fSGustavo F. Padovan /* Reject if config buffer is too small. */ 28160a708f8fSGustavo F. Padovan len = cmd_len - sizeof(*req); 28177ac28817SDan Rosenberg if (len < 0 || chan->conf_len + len > sizeof(chan->conf_req)) { 28180a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2819fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 28200a708f8fSGustavo F. Padovan L2CAP_CONF_REJECT, flags), rsp); 28210a708f8fSGustavo F. Padovan goto unlock; 28220a708f8fSGustavo F. Padovan } 28230a708f8fSGustavo F. Padovan 28240a708f8fSGustavo F. Padovan /* Store config. */ 282573ffa904SGustavo F. Padovan memcpy(chan->conf_req + chan->conf_len, req->data, len); 282673ffa904SGustavo F. Padovan chan->conf_len += len; 28270a708f8fSGustavo F. Padovan 28280a708f8fSGustavo F. Padovan if (flags & 0x0001) { 28290a708f8fSGustavo F. Padovan /* Incomplete config. Send empty response. */ 28300a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2831fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 28320a708f8fSGustavo F. Padovan L2CAP_CONF_SUCCESS, 0x0001), rsp); 28330a708f8fSGustavo F. Padovan goto unlock; 28340a708f8fSGustavo F. Padovan } 28350a708f8fSGustavo F. Padovan 28360a708f8fSGustavo F. Padovan /* Complete config. */ 283773ffa904SGustavo F. Padovan len = l2cap_parse_conf_req(chan, rsp); 28380a708f8fSGustavo F. Padovan if (len < 0) { 2839e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 28400a708f8fSGustavo F. Padovan goto unlock; 28410a708f8fSGustavo F. Padovan } 28420a708f8fSGustavo F. Padovan 28430a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); 284473ffa904SGustavo F. Padovan chan->num_conf_rsp++; 28450a708f8fSGustavo F. Padovan 28460a708f8fSGustavo F. Padovan /* Reset config buffer. */ 284773ffa904SGustavo F. Padovan chan->conf_len = 0; 28480a708f8fSGustavo F. Padovan 2849c1360a1cSGustavo F. Padovan if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) 28500a708f8fSGustavo F. Padovan goto unlock; 28510a708f8fSGustavo F. Padovan 2852c1360a1cSGustavo F. Padovan if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { 285347d1ec61SGustavo F. Padovan set_default_fcs(chan); 28540a708f8fSGustavo F. Padovan 285589bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 28560a708f8fSGustavo F. Padovan 285742e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 285842e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 285958d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 28600c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2861525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 28620a708f8fSGustavo F. Padovan 28630a708f8fSGustavo F. Padovan l2cap_chan_ready(sk); 28640a708f8fSGustavo F. Padovan goto unlock; 28650a708f8fSGustavo F. Padovan } 28660a708f8fSGustavo F. Padovan 2867c1360a1cSGustavo F. Padovan if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { 28680a708f8fSGustavo F. Padovan u8 buf[64]; 28690a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 287073ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 287173ffa904SGustavo F. Padovan chan->num_conf_req++; 28720a708f8fSGustavo F. Padovan } 28730a708f8fSGustavo F. Padovan 28740e8b207eSAndrei Emeltchenko /* Got Conf Rsp PENDING from remote side and asume we sent 28750e8b207eSAndrei Emeltchenko Conf Rsp PENDING in the code above */ 28760e8b207eSAndrei Emeltchenko if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && 28770e8b207eSAndrei Emeltchenko test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { 28780e8b207eSAndrei Emeltchenko 28790e8b207eSAndrei Emeltchenko /* check compatibility */ 28800e8b207eSAndrei Emeltchenko 28810e8b207eSAndrei Emeltchenko clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 28820e8b207eSAndrei Emeltchenko set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 28830e8b207eSAndrei Emeltchenko 28840e8b207eSAndrei Emeltchenko l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 28850e8b207eSAndrei Emeltchenko l2cap_build_conf_rsp(chan, rsp, 28860e8b207eSAndrei Emeltchenko L2CAP_CONF_SUCCESS, 0x0000), rsp); 28870e8b207eSAndrei Emeltchenko } 28880e8b207eSAndrei Emeltchenko 28890a708f8fSGustavo F. Padovan unlock: 2890aa2ac881SGustavo F. Padovan release_sock(sk); 28910a708f8fSGustavo F. Padovan return 0; 28920a708f8fSGustavo F. Padovan } 28930a708f8fSGustavo F. Padovan 28940a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 28950a708f8fSGustavo F. Padovan { 28960a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; 28970a708f8fSGustavo F. Padovan u16 scid, flags, result; 289848454079SGustavo F. Padovan struct l2cap_chan *chan; 28990a708f8fSGustavo F. Padovan struct sock *sk; 29000a708f8fSGustavo F. Padovan int len = cmd->len - sizeof(*rsp); 29010a708f8fSGustavo F. Padovan 29020a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 29030a708f8fSGustavo F. Padovan flags = __le16_to_cpu(rsp->flags); 29040a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 29050a708f8fSGustavo F. Padovan 29060a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", 29070a708f8fSGustavo F. Padovan scid, flags, result); 29080a708f8fSGustavo F. Padovan 2909baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 291048454079SGustavo F. Padovan if (!chan) 29110a708f8fSGustavo F. Padovan return 0; 29120a708f8fSGustavo F. Padovan 291348454079SGustavo F. Padovan sk = chan->sk; 291448454079SGustavo F. Padovan 29150a708f8fSGustavo F. Padovan switch (result) { 29160a708f8fSGustavo F. Padovan case L2CAP_CONF_SUCCESS: 291747d1ec61SGustavo F. Padovan l2cap_conf_rfc_get(chan, rsp->data, len); 29180e8b207eSAndrei Emeltchenko clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); 29190a708f8fSGustavo F. Padovan break; 29200a708f8fSGustavo F. Padovan 29210e8b207eSAndrei Emeltchenko case L2CAP_CONF_PENDING: 29220e8b207eSAndrei Emeltchenko set_bit(CONF_REM_CONF_PEND, &chan->conf_state); 29230e8b207eSAndrei Emeltchenko 29240e8b207eSAndrei Emeltchenko if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { 29250e8b207eSAndrei Emeltchenko char buf[64]; 29260e8b207eSAndrei Emeltchenko 29270e8b207eSAndrei Emeltchenko len = l2cap_parse_conf_rsp(chan, rsp->data, len, 29280e8b207eSAndrei Emeltchenko buf, &result); 29290e8b207eSAndrei Emeltchenko if (len < 0) { 29300e8b207eSAndrei Emeltchenko l2cap_send_disconn_req(conn, chan, ECONNRESET); 29310e8b207eSAndrei Emeltchenko goto done; 29320e8b207eSAndrei Emeltchenko } 29330e8b207eSAndrei Emeltchenko 29340e8b207eSAndrei Emeltchenko /* check compatibility */ 29350e8b207eSAndrei Emeltchenko 29360e8b207eSAndrei Emeltchenko clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); 29370e8b207eSAndrei Emeltchenko set_bit(CONF_OUTPUT_DONE, &chan->conf_state); 29380e8b207eSAndrei Emeltchenko 29390e8b207eSAndrei Emeltchenko l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 29400e8b207eSAndrei Emeltchenko l2cap_build_conf_rsp(chan, buf, 29410e8b207eSAndrei Emeltchenko L2CAP_CONF_SUCCESS, 0x0000), buf); 29420e8b207eSAndrei Emeltchenko } 29430e8b207eSAndrei Emeltchenko goto done; 29440e8b207eSAndrei Emeltchenko 29450a708f8fSGustavo F. Padovan case L2CAP_CONF_UNACCEPT: 294673ffa904SGustavo F. Padovan if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { 29470a708f8fSGustavo F. Padovan char req[64]; 29480a708f8fSGustavo F. Padovan 29490a708f8fSGustavo F. Padovan if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { 2950e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 29510a708f8fSGustavo F. Padovan goto done; 29520a708f8fSGustavo F. Padovan } 29530a708f8fSGustavo F. Padovan 29540a708f8fSGustavo F. Padovan /* throw out any old stored conf requests */ 29550a708f8fSGustavo F. Padovan result = L2CAP_CONF_SUCCESS; 2956b4450035SGustavo F. Padovan len = l2cap_parse_conf_rsp(chan, rsp->data, len, 2957b4450035SGustavo F. Padovan req, &result); 29580a708f8fSGustavo F. Padovan if (len < 0) { 2959e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 29600a708f8fSGustavo F. Padovan goto done; 29610a708f8fSGustavo F. Padovan } 29620a708f8fSGustavo F. Padovan 29630a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 29640a708f8fSGustavo F. Padovan L2CAP_CONF_REQ, len, req); 296573ffa904SGustavo F. Padovan chan->num_conf_req++; 29660a708f8fSGustavo F. Padovan if (result != L2CAP_CONF_SUCCESS) 29670a708f8fSGustavo F. Padovan goto done; 29680a708f8fSGustavo F. Padovan break; 29690a708f8fSGustavo F. Padovan } 29700a708f8fSGustavo F. Padovan 29710a708f8fSGustavo F. Padovan default: 29720a708f8fSGustavo F. Padovan sk->sk_err = ECONNRESET; 2973f3f668b0SAndrzej Kaczmarek __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT); 2974e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 29750a708f8fSGustavo F. Padovan goto done; 29760a708f8fSGustavo F. Padovan } 29770a708f8fSGustavo F. Padovan 29780a708f8fSGustavo F. Padovan if (flags & 0x01) 29790a708f8fSGustavo F. Padovan goto done; 29800a708f8fSGustavo F. Padovan 2981c1360a1cSGustavo F. Padovan set_bit(CONF_INPUT_DONE, &chan->conf_state); 29820a708f8fSGustavo F. Padovan 2983c1360a1cSGustavo F. Padovan if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { 298447d1ec61SGustavo F. Padovan set_default_fcs(chan); 29850a708f8fSGustavo F. Padovan 298689bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONNECTED); 298742e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 298842e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 298958d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 29900c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2991525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 29920a708f8fSGustavo F. Padovan 29930a708f8fSGustavo F. Padovan l2cap_chan_ready(sk); 29940a708f8fSGustavo F. Padovan } 29950a708f8fSGustavo F. Padovan 29960a708f8fSGustavo F. Padovan done: 2997aa2ac881SGustavo F. Padovan release_sock(sk); 29980a708f8fSGustavo F. Padovan return 0; 29990a708f8fSGustavo F. Padovan } 30000a708f8fSGustavo F. Padovan 30010a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 30020a708f8fSGustavo F. Padovan { 30030a708f8fSGustavo F. Padovan struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; 30040a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp rsp; 30050a708f8fSGustavo F. Padovan u16 dcid, scid; 300648454079SGustavo F. Padovan struct l2cap_chan *chan; 30070a708f8fSGustavo F. Padovan struct sock *sk; 30080a708f8fSGustavo F. Padovan 30090a708f8fSGustavo F. Padovan scid = __le16_to_cpu(req->scid); 30100a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 30110a708f8fSGustavo F. Padovan 30120a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); 30130a708f8fSGustavo F. Padovan 3014baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 301548454079SGustavo F. Padovan if (!chan) 30160a708f8fSGustavo F. Padovan return 0; 30170a708f8fSGustavo F. Padovan 301848454079SGustavo F. Padovan sk = chan->sk; 301948454079SGustavo F. Padovan 3020fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 3021fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 30220a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); 30230a708f8fSGustavo F. Padovan 30240a708f8fSGustavo F. Padovan sk->sk_shutdown = SHUTDOWN_MASK; 30250a708f8fSGustavo F. Padovan 302648454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNRESET); 3027aa2ac881SGustavo F. Padovan release_sock(sk); 30280a708f8fSGustavo F. Padovan 3029ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 30300a708f8fSGustavo F. Padovan return 0; 30310a708f8fSGustavo F. Padovan } 30320a708f8fSGustavo F. Padovan 30330a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 30340a708f8fSGustavo F. Padovan { 30350a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; 30360a708f8fSGustavo F. Padovan u16 dcid, scid; 303748454079SGustavo F. Padovan struct l2cap_chan *chan; 30380a708f8fSGustavo F. Padovan struct sock *sk; 30390a708f8fSGustavo F. Padovan 30400a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 30410a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 30420a708f8fSGustavo F. Padovan 30430a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); 30440a708f8fSGustavo F. Padovan 3045baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 304648454079SGustavo F. Padovan if (!chan) 30470a708f8fSGustavo F. Padovan return 0; 30480a708f8fSGustavo F. Padovan 304948454079SGustavo F. Padovan sk = chan->sk; 305048454079SGustavo F. Padovan 305148454079SGustavo F. Padovan l2cap_chan_del(chan, 0); 3052aa2ac881SGustavo F. Padovan release_sock(sk); 30530a708f8fSGustavo F. Padovan 3054ba3bd0eeSGustavo F. Padovan chan->ops->close(chan->data); 30550a708f8fSGustavo F. Padovan return 0; 30560a708f8fSGustavo F. Padovan } 30570a708f8fSGustavo F. Padovan 30580a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 30590a708f8fSGustavo F. Padovan { 30600a708f8fSGustavo F. Padovan struct l2cap_info_req *req = (struct l2cap_info_req *) data; 30610a708f8fSGustavo F. Padovan u16 type; 30620a708f8fSGustavo F. Padovan 30630a708f8fSGustavo F. Padovan type = __le16_to_cpu(req->type); 30640a708f8fSGustavo F. Padovan 30650a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x", type); 30660a708f8fSGustavo F. Padovan 30670a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 30680a708f8fSGustavo F. Padovan u8 buf[8]; 30690a708f8fSGustavo F. Padovan u32 feat_mask = l2cap_feat_mask; 30700a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 30710a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 30720a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 30730a708f8fSGustavo F. Padovan if (!disable_ertm) 30740a708f8fSGustavo F. Padovan feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING 30750a708f8fSGustavo F. Padovan | L2CAP_FEAT_FCS; 3076a5fd6f30SAndrei Emeltchenko if (enable_hs) 30776327eb98SAndrei Emeltchenko feat_mask |= L2CAP_FEAT_EXT_FLOW 30786327eb98SAndrei Emeltchenko | L2CAP_FEAT_EXT_WINDOW; 3079a5fd6f30SAndrei Emeltchenko 30800a708f8fSGustavo F. Padovan put_unaligned_le32(feat_mask, rsp->data); 30810a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 30820a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 30830a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 30840a708f8fSGustavo F. Padovan u8 buf[12]; 30850a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 308650a147cdSMat Martineau 308750a147cdSMat Martineau if (enable_hs) 308850a147cdSMat Martineau l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; 308950a147cdSMat Martineau else 309050a147cdSMat Martineau l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; 309150a147cdSMat Martineau 30920a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 30930a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 3094c6337ea6SAndrei Emeltchenko memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan)); 30950a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 30960a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 30970a708f8fSGustavo F. Padovan } else { 30980a708f8fSGustavo F. Padovan struct l2cap_info_rsp rsp; 30990a708f8fSGustavo F. Padovan rsp.type = cpu_to_le16(type); 31000a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); 31010a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 31020a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(rsp), &rsp); 31030a708f8fSGustavo F. Padovan } 31040a708f8fSGustavo F. Padovan 31050a708f8fSGustavo F. Padovan return 0; 31060a708f8fSGustavo F. Padovan } 31070a708f8fSGustavo F. Padovan 31080a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 31090a708f8fSGustavo F. Padovan { 31100a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; 31110a708f8fSGustavo F. Padovan u16 type, result; 31120a708f8fSGustavo F. Padovan 31130a708f8fSGustavo F. Padovan type = __le16_to_cpu(rsp->type); 31140a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 31150a708f8fSGustavo F. Padovan 31160a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); 31170a708f8fSGustavo F. Padovan 3118e90165beSAndrei Emeltchenko /* L2CAP Info req/rsp are unbound to channels, add extra checks */ 3119e90165beSAndrei Emeltchenko if (cmd->ident != conn->info_ident || 3120e90165beSAndrei Emeltchenko conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) 3121e90165beSAndrei Emeltchenko return 0; 3122e90165beSAndrei Emeltchenko 3123371fd835SUlisses Furquim __cancel_delayed_work(&conn->info_timer); 31240a708f8fSGustavo F. Padovan 31250a708f8fSGustavo F. Padovan if (result != L2CAP_IR_SUCCESS) { 31260a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 31270a708f8fSGustavo F. Padovan conn->info_ident = 0; 31280a708f8fSGustavo F. Padovan 31290a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 31300a708f8fSGustavo F. Padovan 31310a708f8fSGustavo F. Padovan return 0; 31320a708f8fSGustavo F. Padovan } 31330a708f8fSGustavo F. Padovan 31340a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 31350a708f8fSGustavo F. Padovan conn->feat_mask = get_unaligned_le32(rsp->data); 31360a708f8fSGustavo F. Padovan 31370a708f8fSGustavo F. Padovan if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { 31380a708f8fSGustavo F. Padovan struct l2cap_info_req req; 31390a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 31400a708f8fSGustavo F. Padovan 31410a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 31420a708f8fSGustavo F. Padovan 31430a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 31440a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 31450a708f8fSGustavo F. Padovan } else { 31460a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 31470a708f8fSGustavo F. Padovan conn->info_ident = 0; 31480a708f8fSGustavo F. Padovan 31490a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 31500a708f8fSGustavo F. Padovan } 31510a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 31520a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 31530a708f8fSGustavo F. Padovan conn->info_ident = 0; 31540a708f8fSGustavo F. Padovan 31550a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 31560a708f8fSGustavo F. Padovan } 31570a708f8fSGustavo F. Padovan 31580a708f8fSGustavo F. Padovan return 0; 31590a708f8fSGustavo F. Padovan } 31600a708f8fSGustavo F. Padovan 3161f94ff6ffSMat Martineau static inline int l2cap_create_channel_req(struct l2cap_conn *conn, 3162f94ff6ffSMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, 3163f94ff6ffSMat Martineau void *data) 3164f94ff6ffSMat Martineau { 3165f94ff6ffSMat Martineau struct l2cap_create_chan_req *req = data; 3166f94ff6ffSMat Martineau struct l2cap_create_chan_rsp rsp; 3167f94ff6ffSMat Martineau u16 psm, scid; 3168f94ff6ffSMat Martineau 3169f94ff6ffSMat Martineau if (cmd_len != sizeof(*req)) 3170f94ff6ffSMat Martineau return -EPROTO; 3171f94ff6ffSMat Martineau 3172f94ff6ffSMat Martineau if (!enable_hs) 3173f94ff6ffSMat Martineau return -EINVAL; 3174f94ff6ffSMat Martineau 3175f94ff6ffSMat Martineau psm = le16_to_cpu(req->psm); 3176f94ff6ffSMat Martineau scid = le16_to_cpu(req->scid); 3177f94ff6ffSMat Martineau 3178f94ff6ffSMat Martineau BT_DBG("psm %d, scid %d, amp_id %d", psm, scid, req->amp_id); 3179f94ff6ffSMat Martineau 3180f94ff6ffSMat Martineau /* Placeholder: Always reject */ 3181f94ff6ffSMat Martineau rsp.dcid = 0; 3182f94ff6ffSMat Martineau rsp.scid = cpu_to_le16(scid); 3183f94ff6ffSMat Martineau rsp.result = L2CAP_CR_NO_MEM; 3184f94ff6ffSMat Martineau rsp.status = L2CAP_CS_NO_INFO; 3185f94ff6ffSMat Martineau 3186f94ff6ffSMat Martineau l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, 3187f94ff6ffSMat Martineau sizeof(rsp), &rsp); 3188f94ff6ffSMat Martineau 3189f94ff6ffSMat Martineau return 0; 3190f94ff6ffSMat Martineau } 3191f94ff6ffSMat Martineau 3192f94ff6ffSMat Martineau static inline int l2cap_create_channel_rsp(struct l2cap_conn *conn, 3193f94ff6ffSMat Martineau struct l2cap_cmd_hdr *cmd, void *data) 3194f94ff6ffSMat Martineau { 3195f94ff6ffSMat Martineau BT_DBG("conn %p", conn); 3196f94ff6ffSMat Martineau 3197f94ff6ffSMat Martineau return l2cap_connect_rsp(conn, cmd, data); 3198f94ff6ffSMat Martineau } 3199f94ff6ffSMat Martineau 32008d5a04a1SMat Martineau static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident, 32018d5a04a1SMat Martineau u16 icid, u16 result) 32028d5a04a1SMat Martineau { 32038d5a04a1SMat Martineau struct l2cap_move_chan_rsp rsp; 32048d5a04a1SMat Martineau 32058d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 32068d5a04a1SMat Martineau 32078d5a04a1SMat Martineau rsp.icid = cpu_to_le16(icid); 32088d5a04a1SMat Martineau rsp.result = cpu_to_le16(result); 32098d5a04a1SMat Martineau 32108d5a04a1SMat Martineau l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_RSP, sizeof(rsp), &rsp); 32118d5a04a1SMat Martineau } 32128d5a04a1SMat Martineau 32138d5a04a1SMat Martineau static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn, 32148d5a04a1SMat Martineau struct l2cap_chan *chan, u16 icid, u16 result) 32158d5a04a1SMat Martineau { 32168d5a04a1SMat Martineau struct l2cap_move_chan_cfm cfm; 32178d5a04a1SMat Martineau u8 ident; 32188d5a04a1SMat Martineau 32198d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 32208d5a04a1SMat Martineau 32218d5a04a1SMat Martineau ident = l2cap_get_ident(conn); 32228d5a04a1SMat Martineau if (chan) 32238d5a04a1SMat Martineau chan->ident = ident; 32248d5a04a1SMat Martineau 32258d5a04a1SMat Martineau cfm.icid = cpu_to_le16(icid); 32268d5a04a1SMat Martineau cfm.result = cpu_to_le16(result); 32278d5a04a1SMat Martineau 32288d5a04a1SMat Martineau l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM, sizeof(cfm), &cfm); 32298d5a04a1SMat Martineau } 32308d5a04a1SMat Martineau 32318d5a04a1SMat Martineau static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident, 32328d5a04a1SMat Martineau u16 icid) 32338d5a04a1SMat Martineau { 32348d5a04a1SMat Martineau struct l2cap_move_chan_cfm_rsp rsp; 32358d5a04a1SMat Martineau 32368d5a04a1SMat Martineau BT_DBG("icid %d", icid); 32378d5a04a1SMat Martineau 32388d5a04a1SMat Martineau rsp.icid = cpu_to_le16(icid); 32398d5a04a1SMat Martineau l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); 32408d5a04a1SMat Martineau } 32418d5a04a1SMat Martineau 32428d5a04a1SMat Martineau static inline int l2cap_move_channel_req(struct l2cap_conn *conn, 32438d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 32448d5a04a1SMat Martineau { 32458d5a04a1SMat Martineau struct l2cap_move_chan_req *req = data; 32468d5a04a1SMat Martineau u16 icid = 0; 32478d5a04a1SMat Martineau u16 result = L2CAP_MR_NOT_ALLOWED; 32488d5a04a1SMat Martineau 32498d5a04a1SMat Martineau if (cmd_len != sizeof(*req)) 32508d5a04a1SMat Martineau return -EPROTO; 32518d5a04a1SMat Martineau 32528d5a04a1SMat Martineau icid = le16_to_cpu(req->icid); 32538d5a04a1SMat Martineau 32548d5a04a1SMat Martineau BT_DBG("icid %d, dest_amp_id %d", icid, req->dest_amp_id); 32558d5a04a1SMat Martineau 32568d5a04a1SMat Martineau if (!enable_hs) 32578d5a04a1SMat Martineau return -EINVAL; 32588d5a04a1SMat Martineau 32598d5a04a1SMat Martineau /* Placeholder: Always refuse */ 32608d5a04a1SMat Martineau l2cap_send_move_chan_rsp(conn, cmd->ident, icid, result); 32618d5a04a1SMat Martineau 32628d5a04a1SMat Martineau return 0; 32638d5a04a1SMat Martineau } 32648d5a04a1SMat Martineau 32658d5a04a1SMat Martineau static inline int l2cap_move_channel_rsp(struct l2cap_conn *conn, 32668d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 32678d5a04a1SMat Martineau { 32688d5a04a1SMat Martineau struct l2cap_move_chan_rsp *rsp = data; 32698d5a04a1SMat Martineau u16 icid, result; 32708d5a04a1SMat Martineau 32718d5a04a1SMat Martineau if (cmd_len != sizeof(*rsp)) 32728d5a04a1SMat Martineau return -EPROTO; 32738d5a04a1SMat Martineau 32748d5a04a1SMat Martineau icid = le16_to_cpu(rsp->icid); 32758d5a04a1SMat Martineau result = le16_to_cpu(rsp->result); 32768d5a04a1SMat Martineau 32778d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 32788d5a04a1SMat Martineau 32798d5a04a1SMat Martineau /* Placeholder: Always unconfirmed */ 32808d5a04a1SMat Martineau l2cap_send_move_chan_cfm(conn, NULL, icid, L2CAP_MC_UNCONFIRMED); 32818d5a04a1SMat Martineau 32828d5a04a1SMat Martineau return 0; 32838d5a04a1SMat Martineau } 32848d5a04a1SMat Martineau 32858d5a04a1SMat Martineau static inline int l2cap_move_channel_confirm(struct l2cap_conn *conn, 32868d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 32878d5a04a1SMat Martineau { 32888d5a04a1SMat Martineau struct l2cap_move_chan_cfm *cfm = data; 32898d5a04a1SMat Martineau u16 icid, result; 32908d5a04a1SMat Martineau 32918d5a04a1SMat Martineau if (cmd_len != sizeof(*cfm)) 32928d5a04a1SMat Martineau return -EPROTO; 32938d5a04a1SMat Martineau 32948d5a04a1SMat Martineau icid = le16_to_cpu(cfm->icid); 32958d5a04a1SMat Martineau result = le16_to_cpu(cfm->result); 32968d5a04a1SMat Martineau 32978d5a04a1SMat Martineau BT_DBG("icid %d, result %d", icid, result); 32988d5a04a1SMat Martineau 32998d5a04a1SMat Martineau l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); 33008d5a04a1SMat Martineau 33018d5a04a1SMat Martineau return 0; 33028d5a04a1SMat Martineau } 33038d5a04a1SMat Martineau 33048d5a04a1SMat Martineau static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, 33058d5a04a1SMat Martineau struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) 33068d5a04a1SMat Martineau { 33078d5a04a1SMat Martineau struct l2cap_move_chan_cfm_rsp *rsp = data; 33088d5a04a1SMat Martineau u16 icid; 33098d5a04a1SMat Martineau 33108d5a04a1SMat Martineau if (cmd_len != sizeof(*rsp)) 33118d5a04a1SMat Martineau return -EPROTO; 33128d5a04a1SMat Martineau 33138d5a04a1SMat Martineau icid = le16_to_cpu(rsp->icid); 33148d5a04a1SMat Martineau 33158d5a04a1SMat Martineau BT_DBG("icid %d", icid); 33168d5a04a1SMat Martineau 33178d5a04a1SMat Martineau return 0; 33188d5a04a1SMat Martineau } 33198d5a04a1SMat Martineau 3320e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency, 3321de73115aSClaudio Takahasi u16 to_multiplier) 3322de73115aSClaudio Takahasi { 3323de73115aSClaudio Takahasi u16 max_latency; 3324de73115aSClaudio Takahasi 3325de73115aSClaudio Takahasi if (min > max || min < 6 || max > 3200) 3326de73115aSClaudio Takahasi return -EINVAL; 3327de73115aSClaudio Takahasi 3328de73115aSClaudio Takahasi if (to_multiplier < 10 || to_multiplier > 3200) 3329de73115aSClaudio Takahasi return -EINVAL; 3330de73115aSClaudio Takahasi 3331de73115aSClaudio Takahasi if (max >= to_multiplier * 8) 3332de73115aSClaudio Takahasi return -EINVAL; 3333de73115aSClaudio Takahasi 3334de73115aSClaudio Takahasi max_latency = (to_multiplier * 8 / max) - 1; 3335de73115aSClaudio Takahasi if (latency > 499 || latency > max_latency) 3336de73115aSClaudio Takahasi return -EINVAL; 3337de73115aSClaudio Takahasi 3338de73115aSClaudio Takahasi return 0; 3339de73115aSClaudio Takahasi } 3340de73115aSClaudio Takahasi 3341de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, 3342de73115aSClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 3343de73115aSClaudio Takahasi { 3344de73115aSClaudio Takahasi struct hci_conn *hcon = conn->hcon; 3345de73115aSClaudio Takahasi struct l2cap_conn_param_update_req *req; 3346de73115aSClaudio Takahasi struct l2cap_conn_param_update_rsp rsp; 3347de73115aSClaudio Takahasi u16 min, max, latency, to_multiplier, cmd_len; 33482ce603ebSClaudio Takahasi int err; 3349de73115aSClaudio Takahasi 3350de73115aSClaudio Takahasi if (!(hcon->link_mode & HCI_LM_MASTER)) 3351de73115aSClaudio Takahasi return -EINVAL; 3352de73115aSClaudio Takahasi 3353de73115aSClaudio Takahasi cmd_len = __le16_to_cpu(cmd->len); 3354de73115aSClaudio Takahasi if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) 3355de73115aSClaudio Takahasi return -EPROTO; 3356de73115aSClaudio Takahasi 3357de73115aSClaudio Takahasi req = (struct l2cap_conn_param_update_req *) data; 3358de73115aSClaudio Takahasi min = __le16_to_cpu(req->min); 3359de73115aSClaudio Takahasi max = __le16_to_cpu(req->max); 3360de73115aSClaudio Takahasi latency = __le16_to_cpu(req->latency); 3361de73115aSClaudio Takahasi to_multiplier = __le16_to_cpu(req->to_multiplier); 3362de73115aSClaudio Takahasi 3363de73115aSClaudio Takahasi BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", 3364de73115aSClaudio Takahasi min, max, latency, to_multiplier); 3365de73115aSClaudio Takahasi 3366de73115aSClaudio Takahasi memset(&rsp, 0, sizeof(rsp)); 33672ce603ebSClaudio Takahasi 33682ce603ebSClaudio Takahasi err = l2cap_check_conn_param(min, max, latency, to_multiplier); 33692ce603ebSClaudio Takahasi if (err) 3370de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); 3371de73115aSClaudio Takahasi else 3372de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); 3373de73115aSClaudio Takahasi 3374de73115aSClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, 3375de73115aSClaudio Takahasi sizeof(rsp), &rsp); 3376de73115aSClaudio Takahasi 33772ce603ebSClaudio Takahasi if (!err) 33782ce603ebSClaudio Takahasi hci_le_conn_update(hcon, min, max, latency, to_multiplier); 33792ce603ebSClaudio Takahasi 3380de73115aSClaudio Takahasi return 0; 3381de73115aSClaudio Takahasi } 3382de73115aSClaudio Takahasi 33833300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, 33843300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 33853300d9a9SClaudio Takahasi { 33863300d9a9SClaudio Takahasi int err = 0; 33873300d9a9SClaudio Takahasi 33883300d9a9SClaudio Takahasi switch (cmd->code) { 33893300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 33903300d9a9SClaudio Takahasi l2cap_command_rej(conn, cmd, data); 33913300d9a9SClaudio Takahasi break; 33923300d9a9SClaudio Takahasi 33933300d9a9SClaudio Takahasi case L2CAP_CONN_REQ: 33943300d9a9SClaudio Takahasi err = l2cap_connect_req(conn, cmd, data); 33953300d9a9SClaudio Takahasi break; 33963300d9a9SClaudio Takahasi 33973300d9a9SClaudio Takahasi case L2CAP_CONN_RSP: 33983300d9a9SClaudio Takahasi err = l2cap_connect_rsp(conn, cmd, data); 33993300d9a9SClaudio Takahasi break; 34003300d9a9SClaudio Takahasi 34013300d9a9SClaudio Takahasi case L2CAP_CONF_REQ: 34023300d9a9SClaudio Takahasi err = l2cap_config_req(conn, cmd, cmd_len, data); 34033300d9a9SClaudio Takahasi break; 34043300d9a9SClaudio Takahasi 34053300d9a9SClaudio Takahasi case L2CAP_CONF_RSP: 34063300d9a9SClaudio Takahasi err = l2cap_config_rsp(conn, cmd, data); 34073300d9a9SClaudio Takahasi break; 34083300d9a9SClaudio Takahasi 34093300d9a9SClaudio Takahasi case L2CAP_DISCONN_REQ: 34103300d9a9SClaudio Takahasi err = l2cap_disconnect_req(conn, cmd, data); 34113300d9a9SClaudio Takahasi break; 34123300d9a9SClaudio Takahasi 34133300d9a9SClaudio Takahasi case L2CAP_DISCONN_RSP: 34143300d9a9SClaudio Takahasi err = l2cap_disconnect_rsp(conn, cmd, data); 34153300d9a9SClaudio Takahasi break; 34163300d9a9SClaudio Takahasi 34173300d9a9SClaudio Takahasi case L2CAP_ECHO_REQ: 34183300d9a9SClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); 34193300d9a9SClaudio Takahasi break; 34203300d9a9SClaudio Takahasi 34213300d9a9SClaudio Takahasi case L2CAP_ECHO_RSP: 34223300d9a9SClaudio Takahasi break; 34233300d9a9SClaudio Takahasi 34243300d9a9SClaudio Takahasi case L2CAP_INFO_REQ: 34253300d9a9SClaudio Takahasi err = l2cap_information_req(conn, cmd, data); 34263300d9a9SClaudio Takahasi break; 34273300d9a9SClaudio Takahasi 34283300d9a9SClaudio Takahasi case L2CAP_INFO_RSP: 34293300d9a9SClaudio Takahasi err = l2cap_information_rsp(conn, cmd, data); 34303300d9a9SClaudio Takahasi break; 34313300d9a9SClaudio Takahasi 3432f94ff6ffSMat Martineau case L2CAP_CREATE_CHAN_REQ: 3433f94ff6ffSMat Martineau err = l2cap_create_channel_req(conn, cmd, cmd_len, data); 3434f94ff6ffSMat Martineau break; 3435f94ff6ffSMat Martineau 3436f94ff6ffSMat Martineau case L2CAP_CREATE_CHAN_RSP: 3437f94ff6ffSMat Martineau err = l2cap_create_channel_rsp(conn, cmd, data); 3438f94ff6ffSMat Martineau break; 3439f94ff6ffSMat Martineau 34408d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_REQ: 34418d5a04a1SMat Martineau err = l2cap_move_channel_req(conn, cmd, cmd_len, data); 34428d5a04a1SMat Martineau break; 34438d5a04a1SMat Martineau 34448d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_RSP: 34458d5a04a1SMat Martineau err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data); 34468d5a04a1SMat Martineau break; 34478d5a04a1SMat Martineau 34488d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_CFM: 34498d5a04a1SMat Martineau err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); 34508d5a04a1SMat Martineau break; 34518d5a04a1SMat Martineau 34528d5a04a1SMat Martineau case L2CAP_MOVE_CHAN_CFM_RSP: 34538d5a04a1SMat Martineau err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); 34548d5a04a1SMat Martineau break; 34558d5a04a1SMat Martineau 34563300d9a9SClaudio Takahasi default: 34573300d9a9SClaudio Takahasi BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); 34583300d9a9SClaudio Takahasi err = -EINVAL; 34593300d9a9SClaudio Takahasi break; 34603300d9a9SClaudio Takahasi } 34613300d9a9SClaudio Takahasi 34623300d9a9SClaudio Takahasi return err; 34633300d9a9SClaudio Takahasi } 34643300d9a9SClaudio Takahasi 34653300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, 34663300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 34673300d9a9SClaudio Takahasi { 34683300d9a9SClaudio Takahasi switch (cmd->code) { 34693300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 34703300d9a9SClaudio Takahasi return 0; 34713300d9a9SClaudio Takahasi 34723300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_REQ: 3473de73115aSClaudio Takahasi return l2cap_conn_param_update_req(conn, cmd, data); 34743300d9a9SClaudio Takahasi 34753300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_RSP: 34763300d9a9SClaudio Takahasi return 0; 34773300d9a9SClaudio Takahasi 34783300d9a9SClaudio Takahasi default: 34793300d9a9SClaudio Takahasi BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); 34803300d9a9SClaudio Takahasi return -EINVAL; 34813300d9a9SClaudio Takahasi } 34823300d9a9SClaudio Takahasi } 34833300d9a9SClaudio Takahasi 34843300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn, 34853300d9a9SClaudio Takahasi struct sk_buff *skb) 34860a708f8fSGustavo F. Padovan { 34870a708f8fSGustavo F. Padovan u8 *data = skb->data; 34880a708f8fSGustavo F. Padovan int len = skb->len; 34890a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr cmd; 34903300d9a9SClaudio Takahasi int err; 34910a708f8fSGustavo F. Padovan 34920a708f8fSGustavo F. Padovan l2cap_raw_recv(conn, skb); 34930a708f8fSGustavo F. Padovan 34940a708f8fSGustavo F. Padovan while (len >= L2CAP_CMD_HDR_SIZE) { 34950a708f8fSGustavo F. Padovan u16 cmd_len; 34960a708f8fSGustavo F. Padovan memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); 34970a708f8fSGustavo F. Padovan data += L2CAP_CMD_HDR_SIZE; 34980a708f8fSGustavo F. Padovan len -= L2CAP_CMD_HDR_SIZE; 34990a708f8fSGustavo F. Padovan 35000a708f8fSGustavo F. Padovan cmd_len = le16_to_cpu(cmd.len); 35010a708f8fSGustavo F. Padovan 35020a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident); 35030a708f8fSGustavo F. Padovan 35040a708f8fSGustavo F. Padovan if (cmd_len > len || !cmd.ident) { 35050a708f8fSGustavo F. Padovan BT_DBG("corrupted command"); 35060a708f8fSGustavo F. Padovan break; 35070a708f8fSGustavo F. Padovan } 35080a708f8fSGustavo F. Padovan 35093300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 35103300d9a9SClaudio Takahasi err = l2cap_le_sig_cmd(conn, &cmd, data); 35113300d9a9SClaudio Takahasi else 35123300d9a9SClaudio Takahasi err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); 35130a708f8fSGustavo F. Padovan 35140a708f8fSGustavo F. Padovan if (err) { 3515e2fd318eSIlia Kolomisnky struct l2cap_cmd_rej_unk rej; 35162c6d1a2eSGustavo F. Padovan 35172c6d1a2eSGustavo F. Padovan BT_ERR("Wrong link type (%d)", err); 35180a708f8fSGustavo F. Padovan 35190a708f8fSGustavo F. Padovan /* FIXME: Map err to a valid reason */ 3520e2fd318eSIlia Kolomisnky rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); 35210a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); 35220a708f8fSGustavo F. Padovan } 35230a708f8fSGustavo F. Padovan 35240a708f8fSGustavo F. Padovan data += cmd_len; 35250a708f8fSGustavo F. Padovan len -= cmd_len; 35260a708f8fSGustavo F. Padovan } 35270a708f8fSGustavo F. Padovan 35280a708f8fSGustavo F. Padovan kfree_skb(skb); 35290a708f8fSGustavo F. Padovan } 35300a708f8fSGustavo F. Padovan 353147d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb) 35320a708f8fSGustavo F. Padovan { 35330a708f8fSGustavo F. Padovan u16 our_fcs, rcv_fcs; 3534e4ca6d98SAndrei Emeltchenko int hdr_size; 3535e4ca6d98SAndrei Emeltchenko 3536e4ca6d98SAndrei Emeltchenko if (test_bit(FLAG_EXT_CTRL, &chan->flags)) 3537e4ca6d98SAndrei Emeltchenko hdr_size = L2CAP_EXT_HDR_SIZE; 3538e4ca6d98SAndrei Emeltchenko else 3539e4ca6d98SAndrei Emeltchenko hdr_size = L2CAP_ENH_HDR_SIZE; 35400a708f8fSGustavo F. Padovan 354147d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 354203a51213SAndrei Emeltchenko skb_trim(skb, skb->len - L2CAP_FCS_SIZE); 35430a708f8fSGustavo F. Padovan rcv_fcs = get_unaligned_le16(skb->data + skb->len); 35440a708f8fSGustavo F. Padovan our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); 35450a708f8fSGustavo F. Padovan 35460a708f8fSGustavo F. Padovan if (our_fcs != rcv_fcs) 35470a708f8fSGustavo F. Padovan return -EBADMSG; 35480a708f8fSGustavo F. Padovan } 35490a708f8fSGustavo F. Padovan return 0; 35500a708f8fSGustavo F. Padovan } 35510a708f8fSGustavo F. Padovan 3552525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan) 35530a708f8fSGustavo F. Padovan { 355488843ab0SAndrei Emeltchenko u32 control = 0; 35550a708f8fSGustavo F. Padovan 35566a026610SGustavo F. Padovan chan->frames_sent = 0; 35570a708f8fSGustavo F. Padovan 35580b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->buffer_seq); 35590a708f8fSGustavo F. Padovan 3560e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 3561ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 3562525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 3563e2ab4353SGustavo F. Padovan set_bit(CONN_RNR_SENT, &chan->conn_state); 35640a708f8fSGustavo F. Padovan } 35650a708f8fSGustavo F. Padovan 3566e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) 3567525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 35680a708f8fSGustavo F. Padovan 3569525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 35700a708f8fSGustavo F. Padovan 3571e2ab4353SGustavo F. Padovan if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && 35726a026610SGustavo F. Padovan chan->frames_sent == 0) { 3573ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 3574525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 35750a708f8fSGustavo F. Padovan } 35760a708f8fSGustavo F. Padovan } 35770a708f8fSGustavo F. Padovan 3578fb45de7dSAndrei Emeltchenko static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u16 tx_seq, u8 sar) 35790a708f8fSGustavo F. Padovan { 35800a708f8fSGustavo F. Padovan struct sk_buff *next_skb; 35810a708f8fSGustavo F. Padovan int tx_seq_offset, next_tx_seq_offset; 35820a708f8fSGustavo F. Padovan 35830a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 35840a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 35850a708f8fSGustavo F. Padovan 3586f1c6775bSGustavo F. Padovan next_skb = skb_peek(&chan->srej_q); 35870a708f8fSGustavo F. Padovan 3588836be934SAndrei Emeltchenko tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq); 35890a708f8fSGustavo F. Padovan 3590039d9572SSzymon Janc while (next_skb) { 35910a708f8fSGustavo F. Padovan if (bt_cb(next_skb)->tx_seq == tx_seq) 35920a708f8fSGustavo F. Padovan return -EINVAL; 35930a708f8fSGustavo F. Padovan 3594836be934SAndrei Emeltchenko next_tx_seq_offset = __seq_offset(chan, 3595836be934SAndrei Emeltchenko bt_cb(next_skb)->tx_seq, chan->buffer_seq); 35960a708f8fSGustavo F. Padovan 35970a708f8fSGustavo F. Padovan if (next_tx_seq_offset > tx_seq_offset) { 3598f1c6775bSGustavo F. Padovan __skb_queue_before(&chan->srej_q, next_skb, skb); 35990a708f8fSGustavo F. Padovan return 0; 36000a708f8fSGustavo F. Padovan } 36010a708f8fSGustavo F. Padovan 3602f1c6775bSGustavo F. Padovan if (skb_queue_is_last(&chan->srej_q, next_skb)) 3603039d9572SSzymon Janc next_skb = NULL; 3604039d9572SSzymon Janc else 3605039d9572SSzymon Janc next_skb = skb_queue_next(&chan->srej_q, next_skb); 3606039d9572SSzymon Janc } 36070a708f8fSGustavo F. Padovan 3608f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 36090a708f8fSGustavo F. Padovan 36100a708f8fSGustavo F. Padovan return 0; 36110a708f8fSGustavo F. Padovan } 36120a708f8fSGustavo F. Padovan 361384084a31SMat Martineau static void append_skb_frag(struct sk_buff *skb, 361484084a31SMat Martineau struct sk_buff *new_frag, struct sk_buff **last_frag) 36150a708f8fSGustavo F. Padovan { 361684084a31SMat Martineau /* skb->len reflects data in skb as well as all fragments 361784084a31SMat Martineau * skb->data_len reflects only data in fragments 361884084a31SMat Martineau */ 361984084a31SMat Martineau if (!skb_has_frag_list(skb)) 362084084a31SMat Martineau skb_shinfo(skb)->frag_list = new_frag; 362184084a31SMat Martineau 362284084a31SMat Martineau new_frag->next = NULL; 362384084a31SMat Martineau 362484084a31SMat Martineau (*last_frag)->next = new_frag; 362584084a31SMat Martineau *last_frag = new_frag; 362684084a31SMat Martineau 362784084a31SMat Martineau skb->len += new_frag->len; 362884084a31SMat Martineau skb->data_len += new_frag->len; 362984084a31SMat Martineau skb->truesize += new_frag->truesize; 363084084a31SMat Martineau } 363184084a31SMat Martineau 363288843ab0SAndrei Emeltchenko static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u32 control) 363384084a31SMat Martineau { 363484084a31SMat Martineau int err = -EINVAL; 36350a708f8fSGustavo F. Padovan 36367e0ef6eeSAndrei Emeltchenko switch (__get_ctrl_sar(chan, control)) { 36377e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_UNSEGMENTED: 363884084a31SMat Martineau if (chan->sdu) 363984084a31SMat Martineau break; 36400a708f8fSGustavo F. Padovan 364184084a31SMat Martineau err = chan->ops->recv(chan->data, skb); 364284084a31SMat Martineau break; 36430a708f8fSGustavo F. Padovan 36447e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_START: 364584084a31SMat Martineau if (chan->sdu) 364684084a31SMat Martineau break; 36470a708f8fSGustavo F. Padovan 36486f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 364903a51213SAndrei Emeltchenko skb_pull(skb, L2CAP_SDULEN_SIZE); 36500a708f8fSGustavo F. Padovan 365184084a31SMat Martineau if (chan->sdu_len > chan->imtu) { 365284084a31SMat Martineau err = -EMSGSIZE; 365384084a31SMat Martineau break; 365484084a31SMat Martineau } 36550a708f8fSGustavo F. Padovan 365684084a31SMat Martineau if (skb->len >= chan->sdu_len) 365784084a31SMat Martineau break; 365884084a31SMat Martineau 365984084a31SMat Martineau chan->sdu = skb; 366084084a31SMat Martineau chan->sdu_last_frag = skb; 366184084a31SMat Martineau 366284084a31SMat Martineau skb = NULL; 366384084a31SMat Martineau err = 0; 36640a708f8fSGustavo F. Padovan break; 36650a708f8fSGustavo F. Padovan 36667e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_CONTINUE: 36676f61fd47SGustavo F. Padovan if (!chan->sdu) 366884084a31SMat Martineau break; 36690a708f8fSGustavo F. Padovan 367084084a31SMat Martineau append_skb_frag(chan->sdu, skb, 367184084a31SMat Martineau &chan->sdu_last_frag); 367284084a31SMat Martineau skb = NULL; 36730a708f8fSGustavo F. Padovan 367484084a31SMat Martineau if (chan->sdu->len >= chan->sdu_len) 367584084a31SMat Martineau break; 36760a708f8fSGustavo F. Padovan 367784084a31SMat Martineau err = 0; 36780a708f8fSGustavo F. Padovan break; 36790a708f8fSGustavo F. Padovan 36807e0ef6eeSAndrei Emeltchenko case L2CAP_SAR_END: 36816f61fd47SGustavo F. Padovan if (!chan->sdu) 368284084a31SMat Martineau break; 36830a708f8fSGustavo F. Padovan 368484084a31SMat Martineau append_skb_frag(chan->sdu, skb, 368584084a31SMat Martineau &chan->sdu_last_frag); 368684084a31SMat Martineau skb = NULL; 36870a708f8fSGustavo F. Padovan 368884084a31SMat Martineau if (chan->sdu->len != chan->sdu_len) 368984084a31SMat Martineau break; 36900a708f8fSGustavo F. Padovan 369184084a31SMat Martineau err = chan->ops->recv(chan->data, chan->sdu); 36920a708f8fSGustavo F. Padovan 369384084a31SMat Martineau if (!err) { 369484084a31SMat Martineau /* Reassembly complete */ 369584084a31SMat Martineau chan->sdu = NULL; 369684084a31SMat Martineau chan->sdu_last_frag = NULL; 369784084a31SMat Martineau chan->sdu_len = 0; 36980a708f8fSGustavo F. Padovan } 36990a708f8fSGustavo F. Padovan break; 37000a708f8fSGustavo F. Padovan } 37010a708f8fSGustavo F. Padovan 370284084a31SMat Martineau if (err) { 37030a708f8fSGustavo F. Padovan kfree_skb(skb); 37046f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 37056f61fd47SGustavo F. Padovan chan->sdu = NULL; 370684084a31SMat Martineau chan->sdu_last_frag = NULL; 370784084a31SMat Martineau chan->sdu_len = 0; 370884084a31SMat Martineau } 37090a708f8fSGustavo F. Padovan 371084084a31SMat Martineau return err; 37110a708f8fSGustavo F. Padovan } 37120a708f8fSGustavo F. Padovan 371326f880d2SMat Martineau static void l2cap_ertm_enter_local_busy(struct l2cap_chan *chan) 37140a708f8fSGustavo F. Padovan { 371588843ab0SAndrei Emeltchenko u32 control; 37160a708f8fSGustavo F. Padovan 371726f880d2SMat Martineau BT_DBG("chan %p, Enter local busy", chan); 371826f880d2SMat Martineau 371926f880d2SMat Martineau set_bit(CONN_LOCAL_BUSY, &chan->conn_state); 372026f880d2SMat Martineau 37210b209faeSAndrei Emeltchenko control = __set_reqseq(chan, chan->buffer_seq); 3722ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); 372326f880d2SMat Martineau l2cap_send_sframe(chan, control); 372426f880d2SMat Martineau 372526f880d2SMat Martineau set_bit(CONN_RNR_SENT, &chan->conn_state); 372626f880d2SMat Martineau 372726f880d2SMat Martineau __clear_ack_timer(chan); 37280a708f8fSGustavo F. Padovan } 37290a708f8fSGustavo F. Padovan 373026f880d2SMat Martineau static void l2cap_ertm_exit_local_busy(struct l2cap_chan *chan) 373126f880d2SMat Martineau { 373288843ab0SAndrei Emeltchenko u32 control; 37330a708f8fSGustavo F. Padovan 3734e2ab4353SGustavo F. Padovan if (!test_bit(CONN_RNR_SENT, &chan->conn_state)) 37350a708f8fSGustavo F. Padovan goto done; 37360a708f8fSGustavo F. Padovan 37370b209faeSAndrei Emeltchenko control = __set_reqseq(chan, chan->buffer_seq); 3738e3781735SAndrei Emeltchenko control |= __set_ctrl_poll(chan); 3739ab784b73SAndrei Emeltchenko control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); 3740525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 37416a026610SGustavo F. Padovan chan->retry_count = 1; 37420a708f8fSGustavo F. Padovan 37431a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 37441a09bcb9SGustavo F. Padovan __set_monitor_timer(chan); 37450a708f8fSGustavo F. Padovan 3746e2ab4353SGustavo F. Padovan set_bit(CONN_WAIT_F, &chan->conn_state); 37470a708f8fSGustavo F. Padovan 37480a708f8fSGustavo F. Padovan done: 3749e2ab4353SGustavo F. Padovan clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); 3750e2ab4353SGustavo F. Padovan clear_bit(CONN_RNR_SENT, &chan->conn_state); 37510a708f8fSGustavo F. Padovan 375249208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit local busy", chan); 37530a708f8fSGustavo F. Padovan } 37540a708f8fSGustavo F. Padovan 3755e328140fSMat Martineau void l2cap_chan_busy(struct l2cap_chan *chan, int busy) 37560a708f8fSGustavo F. Padovan { 3757e328140fSMat Martineau if (chan->mode == L2CAP_MODE_ERTM) { 3758e328140fSMat Martineau if (busy) 375926f880d2SMat Martineau l2cap_ertm_enter_local_busy(chan); 3760e328140fSMat Martineau else 3761e328140fSMat Martineau l2cap_ertm_exit_local_busy(chan); 37620a708f8fSGustavo F. Padovan } 37630a708f8fSGustavo F. Padovan } 37640a708f8fSGustavo F. Padovan 3765fb45de7dSAndrei Emeltchenko static void l2cap_check_srej_gap(struct l2cap_chan *chan, u16 tx_seq) 37660a708f8fSGustavo F. Padovan { 37670a708f8fSGustavo F. Padovan struct sk_buff *skb; 376888843ab0SAndrei Emeltchenko u32 control; 37690a708f8fSGustavo F. Padovan 3770e328140fSMat Martineau while ((skb = skb_peek(&chan->srej_q)) && 3771e328140fSMat Martineau !test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 3772e328140fSMat Martineau int err; 3773e328140fSMat Martineau 37740a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq != tx_seq) 37750a708f8fSGustavo F. Padovan break; 37760a708f8fSGustavo F. Padovan 3777f1c6775bSGustavo F. Padovan skb = skb_dequeue(&chan->srej_q); 37787e0ef6eeSAndrei Emeltchenko control = __set_ctrl_sar(chan, bt_cb(skb)->sar); 377984084a31SMat Martineau err = l2cap_reassemble_sdu(chan, skb, control); 3780e328140fSMat Martineau 3781e328140fSMat Martineau if (err < 0) { 3782e328140fSMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 3783e328140fSMat Martineau break; 3784e328140fSMat Martineau } 3785e328140fSMat Martineau 3786836be934SAndrei Emeltchenko chan->buffer_seq_srej = __next_seq(chan, chan->buffer_seq_srej); 3787836be934SAndrei Emeltchenko tx_seq = __next_seq(chan, tx_seq); 37880a708f8fSGustavo F. Padovan } 37890a708f8fSGustavo F. Padovan } 37900a708f8fSGustavo F. Padovan 3791fb45de7dSAndrei Emeltchenko static void l2cap_resend_srejframe(struct l2cap_chan *chan, u16 tx_seq) 37920a708f8fSGustavo F. Padovan { 37930a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 379488843ab0SAndrei Emeltchenko u32 control; 37950a708f8fSGustavo F. Padovan 379639d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 37970a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 37980a708f8fSGustavo F. Padovan list_del(&l->list); 37990a708f8fSGustavo F. Padovan kfree(l); 38000a708f8fSGustavo F. Padovan return; 38010a708f8fSGustavo F. Padovan } 3802ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 38030b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, l->tx_seq); 3804525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 38050a708f8fSGustavo F. Padovan list_del(&l->list); 380639d5a3eeSGustavo F. Padovan list_add_tail(&l->list, &chan->srej_l); 38070a708f8fSGustavo F. Padovan } 38080a708f8fSGustavo F. Padovan } 38090a708f8fSGustavo F. Padovan 3810aef89f21SSzymon Janc static int l2cap_send_srejframe(struct l2cap_chan *chan, u16 tx_seq) 38110a708f8fSGustavo F. Padovan { 38120a708f8fSGustavo F. Padovan struct srej_list *new; 381388843ab0SAndrei Emeltchenko u32 control; 38140a708f8fSGustavo F. Padovan 381542e5c802SGustavo F. Padovan while (tx_seq != chan->expected_tx_seq) { 3816ab784b73SAndrei Emeltchenko control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); 38170b209faeSAndrei Emeltchenko control |= __set_reqseq(chan, chan->expected_tx_seq); 3818525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 38190a708f8fSGustavo F. Padovan 38200a708f8fSGustavo F. Padovan new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); 3821aef89f21SSzymon Janc if (!new) 3822aef89f21SSzymon Janc return -ENOMEM; 3823aef89f21SSzymon Janc 382442e5c802SGustavo F. Padovan new->tx_seq = chan->expected_tx_seq; 3825836be934SAndrei Emeltchenko 3826836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); 3827836be934SAndrei Emeltchenko 382839d5a3eeSGustavo F. Padovan list_add_tail(&new->list, &chan->srej_l); 38290a708f8fSGustavo F. Padovan } 3830836be934SAndrei Emeltchenko 3831836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); 3832aef89f21SSzymon Janc 3833aef89f21SSzymon Janc return 0; 38340a708f8fSGustavo F. Padovan } 38350a708f8fSGustavo F. Padovan 383688843ab0SAndrei Emeltchenko static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb) 38370a708f8fSGustavo F. Padovan { 3838fb45de7dSAndrei Emeltchenko u16 tx_seq = __get_txseq(chan, rx_control); 38390b209faeSAndrei Emeltchenko u16 req_seq = __get_reqseq(chan, rx_control); 38407e0ef6eeSAndrei Emeltchenko u8 sar = __get_ctrl_sar(chan, rx_control); 38410a708f8fSGustavo F. Padovan int tx_seq_offset, expected_tx_seq_offset; 384247d1ec61SGustavo F. Padovan int num_to_ack = (chan->tx_win/6) + 1; 38430a708f8fSGustavo F. Padovan int err = 0; 38440a708f8fSGustavo F. Padovan 384588843ab0SAndrei Emeltchenko BT_DBG("chan %p len %d tx_seq %d rx_control 0x%8.8x", chan, skb->len, 3846525cd185SGustavo F. Padovan tx_seq, rx_control); 38470a708f8fSGustavo F. Padovan 384803f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control) && 3849e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 38501a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 38516a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 38521a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 3853e2ab4353SGustavo F. Padovan clear_bit(CONN_WAIT_F, &chan->conn_state); 38540a708f8fSGustavo F. Padovan } 38550a708f8fSGustavo F. Padovan 385642e5c802SGustavo F. Padovan chan->expected_ack_seq = req_seq; 385742e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 38580a708f8fSGustavo F. Padovan 3859836be934SAndrei Emeltchenko tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq); 38600a708f8fSGustavo F. Padovan 38610a708f8fSGustavo F. Padovan /* invalid tx_seq */ 386247d1ec61SGustavo F. Padovan if (tx_seq_offset >= chan->tx_win) { 38638c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 38640a708f8fSGustavo F. Padovan goto drop; 38650a708f8fSGustavo F. Padovan } 38660a708f8fSGustavo F. Padovan 3867e2ab4353SGustavo F. Padovan if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) 38680a708f8fSGustavo F. Padovan goto drop; 38690a708f8fSGustavo F. Padovan 387002f1b641SMat Martineau if (tx_seq == chan->expected_tx_seq) 387102f1b641SMat Martineau goto expected; 387202f1b641SMat Martineau 3873e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 38740a708f8fSGustavo F. Padovan struct srej_list *first; 38750a708f8fSGustavo F. Padovan 387639d5a3eeSGustavo F. Padovan first = list_first_entry(&chan->srej_l, 38770a708f8fSGustavo F. Padovan struct srej_list, list); 38780a708f8fSGustavo F. Padovan if (tx_seq == first->tx_seq) { 387942e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 3880525cd185SGustavo F. Padovan l2cap_check_srej_gap(chan, tx_seq); 38810a708f8fSGustavo F. Padovan 38820a708f8fSGustavo F. Padovan list_del(&first->list); 38830a708f8fSGustavo F. Padovan kfree(first); 38840a708f8fSGustavo F. Padovan 388539d5a3eeSGustavo F. Padovan if (list_empty(&chan->srej_l)) { 388642e5c802SGustavo F. Padovan chan->buffer_seq = chan->buffer_seq_srej; 3887e2ab4353SGustavo F. Padovan clear_bit(CONN_SREJ_SENT, &chan->conn_state); 3888525cd185SGustavo F. Padovan l2cap_send_ack(chan); 388949208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit SREJ_SENT", chan); 38900a708f8fSGustavo F. Padovan } 38910a708f8fSGustavo F. Padovan } else { 38920a708f8fSGustavo F. Padovan struct srej_list *l; 38930a708f8fSGustavo F. Padovan 38940a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 389542e5c802SGustavo F. Padovan if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0) 38960a708f8fSGustavo F. Padovan goto drop; 38970a708f8fSGustavo F. Padovan 389839d5a3eeSGustavo F. Padovan list_for_each_entry(l, &chan->srej_l, list) { 38990a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 3900525cd185SGustavo F. Padovan l2cap_resend_srejframe(chan, tx_seq); 39010a708f8fSGustavo F. Padovan return 0; 39020a708f8fSGustavo F. Padovan } 39030a708f8fSGustavo F. Padovan } 3904aef89f21SSzymon Janc 3905aef89f21SSzymon Janc err = l2cap_send_srejframe(chan, tx_seq); 3906aef89f21SSzymon Janc if (err < 0) { 3907aef89f21SSzymon Janc l2cap_send_disconn_req(chan->conn, chan, -err); 3908aef89f21SSzymon Janc return err; 3909aef89f21SSzymon Janc } 39100a708f8fSGustavo F. Padovan } 39110a708f8fSGustavo F. Padovan } else { 3912836be934SAndrei Emeltchenko expected_tx_seq_offset = __seq_offset(chan, 3913836be934SAndrei Emeltchenko chan->expected_tx_seq, chan->buffer_seq); 39140a708f8fSGustavo F. Padovan 39150a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 39160a708f8fSGustavo F. Padovan if (tx_seq_offset < expected_tx_seq_offset) 39170a708f8fSGustavo F. Padovan goto drop; 39180a708f8fSGustavo F. Padovan 3919e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_SENT, &chan->conn_state); 39200a708f8fSGustavo F. Padovan 392149208c9cSGustavo F. Padovan BT_DBG("chan %p, Enter SREJ", chan); 39220a708f8fSGustavo F. Padovan 392339d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 392442e5c802SGustavo F. Padovan chan->buffer_seq_srej = chan->buffer_seq; 39250a708f8fSGustavo F. Padovan 3926f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->srej_q); 392742e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 39280a708f8fSGustavo F. Padovan 3929e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_PBIT, &chan->conn_state); 39300a708f8fSGustavo F. Padovan 3931aef89f21SSzymon Janc err = l2cap_send_srejframe(chan, tx_seq); 3932aef89f21SSzymon Janc if (err < 0) { 3933aef89f21SSzymon Janc l2cap_send_disconn_req(chan->conn, chan, -err); 3934aef89f21SSzymon Janc return err; 3935aef89f21SSzymon Janc } 39360a708f8fSGustavo F. Padovan 39371a09bcb9SGustavo F. Padovan __clear_ack_timer(chan); 39380a708f8fSGustavo F. Padovan } 39390a708f8fSGustavo F. Padovan return 0; 39400a708f8fSGustavo F. Padovan 39410a708f8fSGustavo F. Padovan expected: 3942836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); 39430a708f8fSGustavo F. Padovan 3944e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 39450a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 39460a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 3947f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 39480a708f8fSGustavo F. Padovan return 0; 39490a708f8fSGustavo F. Padovan } 39500a708f8fSGustavo F. Padovan 395184084a31SMat Martineau err = l2cap_reassemble_sdu(chan, skb, rx_control); 3952836be934SAndrei Emeltchenko chan->buffer_seq = __next_seq(chan, chan->buffer_seq); 3953836be934SAndrei Emeltchenko 3954e328140fSMat Martineau if (err < 0) { 3955e328140fSMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 3956e328140fSMat Martineau return err; 3957e328140fSMat Martineau } 39580a708f8fSGustavo F. Padovan 395903f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control)) { 3960e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 3961525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 39620a708f8fSGustavo F. Padovan } 39630a708f8fSGustavo F. Padovan 39640a708f8fSGustavo F. Padovan 39656a026610SGustavo F. Padovan chan->num_acked = (chan->num_acked + 1) % num_to_ack; 39666a026610SGustavo F. Padovan if (chan->num_acked == num_to_ack - 1) 3967525cd185SGustavo F. Padovan l2cap_send_ack(chan); 39684d611e4dSGustavo F. Padovan else 39694d611e4dSGustavo F. Padovan __set_ack_timer(chan); 39700a708f8fSGustavo F. Padovan 39710a708f8fSGustavo F. Padovan return 0; 39720a708f8fSGustavo F. Padovan 39730a708f8fSGustavo F. Padovan drop: 39740a708f8fSGustavo F. Padovan kfree_skb(skb); 39750a708f8fSGustavo F. Padovan return 0; 39760a708f8fSGustavo F. Padovan } 39770a708f8fSGustavo F. Padovan 397888843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u32 rx_control) 39790a708f8fSGustavo F. Padovan { 398088843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, 39810b209faeSAndrei Emeltchenko __get_reqseq(chan, rx_control), rx_control); 39820a708f8fSGustavo F. Padovan 39830b209faeSAndrei Emeltchenko chan->expected_ack_seq = __get_reqseq(chan, rx_control); 398442e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 39850a708f8fSGustavo F. Padovan 3986e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) { 3987e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 3988e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 3989e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 39906a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 39911a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 39920a708f8fSGustavo F. Padovan 3993e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 3994525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 39950a708f8fSGustavo F. Padovan } else { 3996525cd185SGustavo F. Padovan l2cap_send_i_or_rr_or_rnr(chan); 39970a708f8fSGustavo F. Padovan } 39980a708f8fSGustavo F. Padovan 399903f6715dSAndrei Emeltchenko } else if (__is_ctrl_final(chan, rx_control)) { 4000e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 40010a708f8fSGustavo F. Padovan 4002e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 4003525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 40040a708f8fSGustavo F. Padovan 40050a708f8fSGustavo F. Padovan } else { 4006e2ab4353SGustavo F. Padovan if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && 40076a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 40081a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 40090a708f8fSGustavo F. Padovan 4010e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 4011e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) 4012525cd185SGustavo F. Padovan l2cap_send_ack(chan); 40130a708f8fSGustavo F. Padovan else 4014525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 40150a708f8fSGustavo F. Padovan } 40160a708f8fSGustavo F. Padovan } 40170a708f8fSGustavo F. Padovan 401888843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u32 rx_control) 40190a708f8fSGustavo F. Padovan { 40200b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 40210a708f8fSGustavo F. Padovan 402288843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); 40230a708f8fSGustavo F. Padovan 4024e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 40250a708f8fSGustavo F. Padovan 402642e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 402742e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 40280a708f8fSGustavo F. Padovan 402903f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control)) { 4030e2ab4353SGustavo F. Padovan if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) 4031525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 40320a708f8fSGustavo F. Padovan } else { 4033525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 40340a708f8fSGustavo F. Padovan 4035e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) 4036e2ab4353SGustavo F. Padovan set_bit(CONN_REJ_ACT, &chan->conn_state); 40370a708f8fSGustavo F. Padovan } 40380a708f8fSGustavo F. Padovan } 403988843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u32 rx_control) 40400a708f8fSGustavo F. Padovan { 40410b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 40420a708f8fSGustavo F. Padovan 404388843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); 40440a708f8fSGustavo F. Padovan 4045e2ab4353SGustavo F. Padovan clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); 40460a708f8fSGustavo F. Padovan 4047e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) { 404842e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 404942e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 40500a708f8fSGustavo F. Padovan 4051e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 4052525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 40530a708f8fSGustavo F. Padovan 4054525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 40550a708f8fSGustavo F. Padovan 4056e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) { 40576a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 4058e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_ACT, &chan->conn_state); 40590a708f8fSGustavo F. Padovan } 406003f6715dSAndrei Emeltchenko } else if (__is_ctrl_final(chan, rx_control)) { 4061e2ab4353SGustavo F. Padovan if (test_bit(CONN_SREJ_ACT, &chan->conn_state) && 40626a026610SGustavo F. Padovan chan->srej_save_reqseq == tx_seq) 4063e2ab4353SGustavo F. Padovan clear_bit(CONN_SREJ_ACT, &chan->conn_state); 40640a708f8fSGustavo F. Padovan else 4065525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 40660a708f8fSGustavo F. Padovan } else { 4067525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 4068e2ab4353SGustavo F. Padovan if (test_bit(CONN_WAIT_F, &chan->conn_state)) { 40696a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 4070e2ab4353SGustavo F. Padovan set_bit(CONN_SREJ_ACT, &chan->conn_state); 40710a708f8fSGustavo F. Padovan } 40720a708f8fSGustavo F. Padovan } 40730a708f8fSGustavo F. Padovan } 40740a708f8fSGustavo F. Padovan 407588843ab0SAndrei Emeltchenko static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u32 rx_control) 40760a708f8fSGustavo F. Padovan { 40770b209faeSAndrei Emeltchenko u16 tx_seq = __get_reqseq(chan, rx_control); 40780a708f8fSGustavo F. Padovan 407988843ab0SAndrei Emeltchenko BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); 40800a708f8fSGustavo F. Padovan 4081e2ab4353SGustavo F. Padovan set_bit(CONN_REMOTE_BUSY, &chan->conn_state); 408242e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 408342e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 40840a708f8fSGustavo F. Padovan 4085e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) 4086e2ab4353SGustavo F. Padovan set_bit(CONN_SEND_FBIT, &chan->conn_state); 40870a708f8fSGustavo F. Padovan 4088e2ab4353SGustavo F. Padovan if (!test_bit(CONN_SREJ_SENT, &chan->conn_state)) { 40891a09bcb9SGustavo F. Padovan __clear_retrans_timer(chan); 4090e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) 4091525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); 40920a708f8fSGustavo F. Padovan return; 40930a708f8fSGustavo F. Padovan } 40940a708f8fSGustavo F. Padovan 4095e3781735SAndrei Emeltchenko if (__is_ctrl_poll(chan, rx_control)) { 4096525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 4097ab784b73SAndrei Emeltchenko } else { 4098ab784b73SAndrei Emeltchenko rx_control = __set_ctrl_super(chan, L2CAP_SUPER_RR); 4099ab784b73SAndrei Emeltchenko l2cap_send_sframe(chan, rx_control); 4100ab784b73SAndrei Emeltchenko } 41010a708f8fSGustavo F. Padovan } 41020a708f8fSGustavo F. Padovan 410388843ab0SAndrei Emeltchenko static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb) 41040a708f8fSGustavo F. Padovan { 410588843ab0SAndrei Emeltchenko BT_DBG("chan %p rx_control 0x%8.8x len %d", chan, rx_control, skb->len); 41060a708f8fSGustavo F. Padovan 410703f6715dSAndrei Emeltchenko if (__is_ctrl_final(chan, rx_control) && 4108e2ab4353SGustavo F. Padovan test_bit(CONN_WAIT_F, &chan->conn_state)) { 41091a09bcb9SGustavo F. Padovan __clear_monitor_timer(chan); 41106a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 41111a09bcb9SGustavo F. Padovan __set_retrans_timer(chan); 4112e2ab4353SGustavo F. Padovan clear_bit(CONN_WAIT_F, &chan->conn_state); 41130a708f8fSGustavo F. Padovan } 41140a708f8fSGustavo F. Padovan 4115ab784b73SAndrei Emeltchenko switch (__get_ctrl_super(chan, rx_control)) { 4116ab784b73SAndrei Emeltchenko case L2CAP_SUPER_RR: 4117525cd185SGustavo F. Padovan l2cap_data_channel_rrframe(chan, rx_control); 41180a708f8fSGustavo F. Padovan break; 41190a708f8fSGustavo F. Padovan 4120ab784b73SAndrei Emeltchenko case L2CAP_SUPER_REJ: 4121525cd185SGustavo F. Padovan l2cap_data_channel_rejframe(chan, rx_control); 41220a708f8fSGustavo F. Padovan break; 41230a708f8fSGustavo F. Padovan 4124ab784b73SAndrei Emeltchenko case L2CAP_SUPER_SREJ: 4125525cd185SGustavo F. Padovan l2cap_data_channel_srejframe(chan, rx_control); 41260a708f8fSGustavo F. Padovan break; 41270a708f8fSGustavo F. Padovan 4128ab784b73SAndrei Emeltchenko case L2CAP_SUPER_RNR: 4129525cd185SGustavo F. Padovan l2cap_data_channel_rnrframe(chan, rx_control); 41300a708f8fSGustavo F. Padovan break; 41310a708f8fSGustavo F. Padovan } 41320a708f8fSGustavo F. Padovan 41330a708f8fSGustavo F. Padovan kfree_skb(skb); 41340a708f8fSGustavo F. Padovan return 0; 41350a708f8fSGustavo F. Padovan } 41360a708f8fSGustavo F. Padovan 41370a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb) 41380a708f8fSGustavo F. Padovan { 4139525cd185SGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 414088843ab0SAndrei Emeltchenko u32 control; 41410b209faeSAndrei Emeltchenko u16 req_seq; 41420a708f8fSGustavo F. Padovan int len, next_tx_seq_offset, req_seq_offset; 41430a708f8fSGustavo F. Padovan 414488843ab0SAndrei Emeltchenko control = __get_control(chan, skb->data); 414588843ab0SAndrei Emeltchenko skb_pull(skb, __ctrl_size(chan)); 41460a708f8fSGustavo F. Padovan len = skb->len; 41470a708f8fSGustavo F. Padovan 41480a708f8fSGustavo F. Padovan /* 41490a708f8fSGustavo F. Padovan * We can just drop the corrupted I-frame here. 41500a708f8fSGustavo F. Padovan * Receiver will miss it and start proper recovery 41510a708f8fSGustavo F. Padovan * procedures and ask retransmission. 41520a708f8fSGustavo F. Padovan */ 415347d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 41540a708f8fSGustavo F. Padovan goto drop; 41550a708f8fSGustavo F. Padovan 4156793c2f1cSAndrei Emeltchenko if (__is_sar_start(chan, control) && !__is_sframe(chan, control)) 415703a51213SAndrei Emeltchenko len -= L2CAP_SDULEN_SIZE; 41580a708f8fSGustavo F. Padovan 415947d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 416003a51213SAndrei Emeltchenko len -= L2CAP_FCS_SIZE; 41610a708f8fSGustavo F. Padovan 416247d1ec61SGustavo F. Padovan if (len > chan->mps) { 41638c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 41640a708f8fSGustavo F. Padovan goto drop; 41650a708f8fSGustavo F. Padovan } 41660a708f8fSGustavo F. Padovan 41670b209faeSAndrei Emeltchenko req_seq = __get_reqseq(chan, control); 41680a708f8fSGustavo F. Padovan 4169836be934SAndrei Emeltchenko req_seq_offset = __seq_offset(chan, req_seq, chan->expected_ack_seq); 4170836be934SAndrei Emeltchenko 4171836be934SAndrei Emeltchenko next_tx_seq_offset = __seq_offset(chan, chan->next_tx_seq, 4172836be934SAndrei Emeltchenko chan->expected_ack_seq); 41730a708f8fSGustavo F. Padovan 41740a708f8fSGustavo F. Padovan /* check for invalid req-seq */ 41750a708f8fSGustavo F. Padovan if (req_seq_offset > next_tx_seq_offset) { 41768c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 41770a708f8fSGustavo F. Padovan goto drop; 41780a708f8fSGustavo F. Padovan } 41790a708f8fSGustavo F. Padovan 4180793c2f1cSAndrei Emeltchenko if (!__is_sframe(chan, control)) { 41810a708f8fSGustavo F. Padovan if (len < 0) { 41828c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 41830a708f8fSGustavo F. Padovan goto drop; 41840a708f8fSGustavo F. Padovan } 41850a708f8fSGustavo F. Padovan 4186525cd185SGustavo F. Padovan l2cap_data_channel_iframe(chan, control, skb); 41870a708f8fSGustavo F. Padovan } else { 41880a708f8fSGustavo F. Padovan if (len != 0) { 41890a708f8fSGustavo F. Padovan BT_ERR("%d", len); 41908c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 41910a708f8fSGustavo F. Padovan goto drop; 41920a708f8fSGustavo F. Padovan } 41930a708f8fSGustavo F. Padovan 4194525cd185SGustavo F. Padovan l2cap_data_channel_sframe(chan, control, skb); 41950a708f8fSGustavo F. Padovan } 41960a708f8fSGustavo F. Padovan 41970a708f8fSGustavo F. Padovan return 0; 41980a708f8fSGustavo F. Padovan 41990a708f8fSGustavo F. Padovan drop: 42000a708f8fSGustavo F. Padovan kfree_skb(skb); 42010a708f8fSGustavo F. Padovan return 0; 42020a708f8fSGustavo F. Padovan } 42030a708f8fSGustavo F. Padovan 42040a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) 42050a708f8fSGustavo F. Padovan { 420648454079SGustavo F. Padovan struct l2cap_chan *chan; 4207bf734843SDavid S. Miller struct sock *sk = NULL; 420888843ab0SAndrei Emeltchenko u32 control; 4209fb45de7dSAndrei Emeltchenko u16 tx_seq; 42100a708f8fSGustavo F. Padovan int len; 42110a708f8fSGustavo F. Padovan 4212baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 421348454079SGustavo F. Padovan if (!chan) { 42140a708f8fSGustavo F. Padovan BT_DBG("unknown cid 0x%4.4x", cid); 42150a708f8fSGustavo F. Padovan goto drop; 42160a708f8fSGustavo F. Padovan } 42170a708f8fSGustavo F. Padovan 421848454079SGustavo F. Padovan sk = chan->sk; 42190a708f8fSGustavo F. Padovan 422049208c9cSGustavo F. Padovan BT_DBG("chan %p, len %d", chan, skb->len); 42210a708f8fSGustavo F. Padovan 422289bc500eSGustavo F. Padovan if (chan->state != BT_CONNECTED) 42230a708f8fSGustavo F. Padovan goto drop; 42240a708f8fSGustavo F. Padovan 42250c1bc5c6SGustavo F. Padovan switch (chan->mode) { 42260a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 42270a708f8fSGustavo F. Padovan /* If socket recv buffers overflows we drop data here 42280a708f8fSGustavo F. Padovan * which is *bad* because L2CAP has to be reliable. 42290a708f8fSGustavo F. Padovan * But we don't have any other choice. L2CAP doesn't 42300a708f8fSGustavo F. Padovan * provide flow control mechanism. */ 42310a708f8fSGustavo F. Padovan 42320c1bc5c6SGustavo F. Padovan if (chan->imtu < skb->len) 42330a708f8fSGustavo F. Padovan goto drop; 42340a708f8fSGustavo F. Padovan 423523070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 42360a708f8fSGustavo F. Padovan goto done; 42370a708f8fSGustavo F. Padovan break; 42380a708f8fSGustavo F. Padovan 42390a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 42400a708f8fSGustavo F. Padovan l2cap_ertm_data_rcv(sk, skb); 42410a708f8fSGustavo F. Padovan 42420a708f8fSGustavo F. Padovan goto done; 42430a708f8fSGustavo F. Padovan 42440a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 424588843ab0SAndrei Emeltchenko control = __get_control(chan, skb->data); 424688843ab0SAndrei Emeltchenko skb_pull(skb, __ctrl_size(chan)); 42470a708f8fSGustavo F. Padovan len = skb->len; 42480a708f8fSGustavo F. Padovan 424947d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 42500a708f8fSGustavo F. Padovan goto drop; 42510a708f8fSGustavo F. Padovan 42527e0ef6eeSAndrei Emeltchenko if (__is_sar_start(chan, control)) 425303a51213SAndrei Emeltchenko len -= L2CAP_SDULEN_SIZE; 42540a708f8fSGustavo F. Padovan 425547d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 425603a51213SAndrei Emeltchenko len -= L2CAP_FCS_SIZE; 42570a708f8fSGustavo F. Padovan 4258793c2f1cSAndrei Emeltchenko if (len > chan->mps || len < 0 || __is_sframe(chan, control)) 42590a708f8fSGustavo F. Padovan goto drop; 42600a708f8fSGustavo F. Padovan 4261fb45de7dSAndrei Emeltchenko tx_seq = __get_txseq(chan, control); 42620a708f8fSGustavo F. Padovan 426384084a31SMat Martineau if (chan->expected_tx_seq != tx_seq) { 426484084a31SMat Martineau /* Frame(s) missing - must discard partial SDU */ 426584084a31SMat Martineau kfree_skb(chan->sdu); 426684084a31SMat Martineau chan->sdu = NULL; 426784084a31SMat Martineau chan->sdu_last_frag = NULL; 426884084a31SMat Martineau chan->sdu_len = 0; 426984084a31SMat Martineau 427084084a31SMat Martineau /* TODO: Notify userland of missing data */ 427184084a31SMat Martineau } 427284084a31SMat Martineau 4273836be934SAndrei Emeltchenko chan->expected_tx_seq = __next_seq(chan, tx_seq); 42740a708f8fSGustavo F. Padovan 427584084a31SMat Martineau if (l2cap_reassemble_sdu(chan, skb, control) == -EMSGSIZE) 427684084a31SMat Martineau l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 42770a708f8fSGustavo F. Padovan 42780a708f8fSGustavo F. Padovan goto done; 42790a708f8fSGustavo F. Padovan 42800a708f8fSGustavo F. Padovan default: 42810c1bc5c6SGustavo F. Padovan BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode); 42820a708f8fSGustavo F. Padovan break; 42830a708f8fSGustavo F. Padovan } 42840a708f8fSGustavo F. Padovan 42850a708f8fSGustavo F. Padovan drop: 42860a708f8fSGustavo F. Padovan kfree_skb(skb); 42870a708f8fSGustavo F. Padovan 42880a708f8fSGustavo F. Padovan done: 42890a708f8fSGustavo F. Padovan if (sk) 4290aa2ac881SGustavo F. Padovan release_sock(sk); 42910a708f8fSGustavo F. Padovan 42920a708f8fSGustavo F. Padovan return 0; 42930a708f8fSGustavo F. Padovan } 42940a708f8fSGustavo F. Padovan 42950a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb) 42960a708f8fSGustavo F. Padovan { 42976dcae1eaSDavid S. Miller struct sock *sk = NULL; 429823691d75SGustavo F. Padovan struct l2cap_chan *chan; 42990a708f8fSGustavo F. Padovan 430023691d75SGustavo F. Padovan chan = l2cap_global_chan_by_psm(0, psm, conn->src); 430123691d75SGustavo F. Padovan if (!chan) 43020a708f8fSGustavo F. Padovan goto drop; 43030a708f8fSGustavo F. Padovan 430423691d75SGustavo F. Padovan sk = chan->sk; 430523691d75SGustavo F. Padovan 4306aa2ac881SGustavo F. Padovan lock_sock(sk); 43070a708f8fSGustavo F. Padovan 43080a708f8fSGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 43090a708f8fSGustavo F. Padovan 431089bc500eSGustavo F. Padovan if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) 43110a708f8fSGustavo F. Padovan goto drop; 43120a708f8fSGustavo F. Padovan 4313e13e21dcSVinicius Costa Gomes if (chan->imtu < skb->len) 43140a708f8fSGustavo F. Padovan goto drop; 43150a708f8fSGustavo F. Padovan 431623070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 43170a708f8fSGustavo F. Padovan goto done; 43180a708f8fSGustavo F. Padovan 43190a708f8fSGustavo F. Padovan drop: 43200a708f8fSGustavo F. Padovan kfree_skb(skb); 43210a708f8fSGustavo F. Padovan 43220a708f8fSGustavo F. Padovan done: 43230a708f8fSGustavo F. Padovan if (sk) 4324aa2ac881SGustavo F. Padovan release_sock(sk); 43250a708f8fSGustavo F. Padovan return 0; 43260a708f8fSGustavo F. Padovan } 43270a708f8fSGustavo F. Padovan 43289f69bda6SGustavo F. Padovan static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb) 43299f69bda6SGustavo F. Padovan { 43306dcae1eaSDavid S. Miller struct sock *sk = NULL; 433123691d75SGustavo F. Padovan struct l2cap_chan *chan; 43329f69bda6SGustavo F. Padovan 433323691d75SGustavo F. Padovan chan = l2cap_global_chan_by_scid(0, cid, conn->src); 433423691d75SGustavo F. Padovan if (!chan) 43359f69bda6SGustavo F. Padovan goto drop; 43369f69bda6SGustavo F. Padovan 433723691d75SGustavo F. Padovan sk = chan->sk; 433823691d75SGustavo F. Padovan 4339aa2ac881SGustavo F. Padovan lock_sock(sk); 43409f69bda6SGustavo F. Padovan 43419f69bda6SGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 43429f69bda6SGustavo F. Padovan 434389bc500eSGustavo F. Padovan if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) 43449f69bda6SGustavo F. Padovan goto drop; 43459f69bda6SGustavo F. Padovan 4346e13e21dcSVinicius Costa Gomes if (chan->imtu < skb->len) 43479f69bda6SGustavo F. Padovan goto drop; 43489f69bda6SGustavo F. Padovan 434923070494SGustavo F. Padovan if (!chan->ops->recv(chan->data, skb)) 43509f69bda6SGustavo F. Padovan goto done; 43519f69bda6SGustavo F. Padovan 43529f69bda6SGustavo F. Padovan drop: 43539f69bda6SGustavo F. Padovan kfree_skb(skb); 43549f69bda6SGustavo F. Padovan 43559f69bda6SGustavo F. Padovan done: 43569f69bda6SGustavo F. Padovan if (sk) 4357aa2ac881SGustavo F. Padovan release_sock(sk); 43589f69bda6SGustavo F. Padovan return 0; 43599f69bda6SGustavo F. Padovan } 43609f69bda6SGustavo F. Padovan 43610a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) 43620a708f8fSGustavo F. Padovan { 43630a708f8fSGustavo F. Padovan struct l2cap_hdr *lh = (void *) skb->data; 43640a708f8fSGustavo F. Padovan u16 cid, len; 43650a708f8fSGustavo F. Padovan __le16 psm; 43660a708f8fSGustavo F. Padovan 43670a708f8fSGustavo F. Padovan skb_pull(skb, L2CAP_HDR_SIZE); 43680a708f8fSGustavo F. Padovan cid = __le16_to_cpu(lh->cid); 43690a708f8fSGustavo F. Padovan len = __le16_to_cpu(lh->len); 43700a708f8fSGustavo F. Padovan 43710a708f8fSGustavo F. Padovan if (len != skb->len) { 43720a708f8fSGustavo F. Padovan kfree_skb(skb); 43730a708f8fSGustavo F. Padovan return; 43740a708f8fSGustavo F. Padovan } 43750a708f8fSGustavo F. Padovan 43760a708f8fSGustavo F. Padovan BT_DBG("len %d, cid 0x%4.4x", len, cid); 43770a708f8fSGustavo F. Padovan 43780a708f8fSGustavo F. Padovan switch (cid) { 43793300d9a9SClaudio Takahasi case L2CAP_CID_LE_SIGNALING: 43800a708f8fSGustavo F. Padovan case L2CAP_CID_SIGNALING: 43810a708f8fSGustavo F. Padovan l2cap_sig_channel(conn, skb); 43820a708f8fSGustavo F. Padovan break; 43830a708f8fSGustavo F. Padovan 43840a708f8fSGustavo F. Padovan case L2CAP_CID_CONN_LESS: 43850a708f8fSGustavo F. Padovan psm = get_unaligned_le16(skb->data); 43860a708f8fSGustavo F. Padovan skb_pull(skb, 2); 43870a708f8fSGustavo F. Padovan l2cap_conless_channel(conn, psm, skb); 43880a708f8fSGustavo F. Padovan break; 43890a708f8fSGustavo F. Padovan 43909f69bda6SGustavo F. Padovan case L2CAP_CID_LE_DATA: 43919f69bda6SGustavo F. Padovan l2cap_att_channel(conn, cid, skb); 43929f69bda6SGustavo F. Padovan break; 43939f69bda6SGustavo F. Padovan 4394b501d6a1SAnderson Briglia case L2CAP_CID_SMP: 4395b501d6a1SAnderson Briglia if (smp_sig_channel(conn, skb)) 4396b501d6a1SAnderson Briglia l2cap_conn_del(conn->hcon, EACCES); 4397b501d6a1SAnderson Briglia break; 4398b501d6a1SAnderson Briglia 43990a708f8fSGustavo F. Padovan default: 44000a708f8fSGustavo F. Padovan l2cap_data_channel(conn, cid, skb); 44010a708f8fSGustavo F. Padovan break; 44020a708f8fSGustavo F. Padovan } 44030a708f8fSGustavo F. Padovan } 44040a708f8fSGustavo F. Padovan 44050a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */ 44060a708f8fSGustavo F. Padovan 4407686ebf28SUlisses Furquim int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) 44080a708f8fSGustavo F. Padovan { 44090a708f8fSGustavo F. Padovan int exact = 0, lm1 = 0, lm2 = 0; 441023691d75SGustavo F. Padovan struct l2cap_chan *c; 44110a708f8fSGustavo F. Padovan 44120a708f8fSGustavo F. Padovan BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); 44130a708f8fSGustavo F. Padovan 44140a708f8fSGustavo F. Padovan /* Find listening sockets and check their link_mode */ 441523691d75SGustavo F. Padovan read_lock(&chan_list_lock); 441623691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 441723691d75SGustavo F. Padovan struct sock *sk = c->sk; 44184343478fSGustavo F. Padovan 441989bc500eSGustavo F. Padovan if (c->state != BT_LISTEN) 44200a708f8fSGustavo F. Padovan continue; 44210a708f8fSGustavo F. Padovan 44220a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { 44230a708f8fSGustavo F. Padovan lm1 |= HCI_LM_ACCEPT; 442443bd0f32SAndrei Emeltchenko if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 44250a708f8fSGustavo F. Padovan lm1 |= HCI_LM_MASTER; 44260a708f8fSGustavo F. Padovan exact++; 44270a708f8fSGustavo F. Padovan } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { 44280a708f8fSGustavo F. Padovan lm2 |= HCI_LM_ACCEPT; 442943bd0f32SAndrei Emeltchenko if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 44300a708f8fSGustavo F. Padovan lm2 |= HCI_LM_MASTER; 44310a708f8fSGustavo F. Padovan } 44320a708f8fSGustavo F. Padovan } 443323691d75SGustavo F. Padovan read_unlock(&chan_list_lock); 44340a708f8fSGustavo F. Padovan 44350a708f8fSGustavo F. Padovan return exact ? lm1 : lm2; 44360a708f8fSGustavo F. Padovan } 44370a708f8fSGustavo F. Padovan 4438686ebf28SUlisses Furquim int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) 44390a708f8fSGustavo F. Padovan { 44400a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 44410a708f8fSGustavo F. Padovan 44420a708f8fSGustavo F. Padovan BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); 44430a708f8fSGustavo F. Padovan 44440a708f8fSGustavo F. Padovan if (!status) { 44450a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, status); 44460a708f8fSGustavo F. Padovan if (conn) 44470a708f8fSGustavo F. Padovan l2cap_conn_ready(conn); 44480a708f8fSGustavo F. Padovan } else 4449e175072fSJoe Perches l2cap_conn_del(hcon, bt_to_errno(status)); 44500a708f8fSGustavo F. Padovan 44510a708f8fSGustavo F. Padovan return 0; 44520a708f8fSGustavo F. Padovan } 44530a708f8fSGustavo F. Padovan 4454686ebf28SUlisses Furquim int l2cap_disconn_ind(struct hci_conn *hcon) 44550a708f8fSGustavo F. Padovan { 44560a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 44570a708f8fSGustavo F. Padovan 44580a708f8fSGustavo F. Padovan BT_DBG("hcon %p", hcon); 44590a708f8fSGustavo F. Padovan 4460686ebf28SUlisses Furquim if (!conn) 44619f5a0d7bSAndrei Emeltchenko return HCI_ERROR_REMOTE_USER_TERM; 44620a708f8fSGustavo F. Padovan return conn->disc_reason; 44630a708f8fSGustavo F. Padovan } 44640a708f8fSGustavo F. Padovan 4465686ebf28SUlisses Furquim int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) 44660a708f8fSGustavo F. Padovan { 44670a708f8fSGustavo F. Padovan BT_DBG("hcon %p reason %d", hcon, reason); 44680a708f8fSGustavo F. Padovan 4469e175072fSJoe Perches l2cap_conn_del(hcon, bt_to_errno(reason)); 44700a708f8fSGustavo F. Padovan return 0; 44710a708f8fSGustavo F. Padovan } 44720a708f8fSGustavo F. Padovan 44734343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) 44740a708f8fSGustavo F. Padovan { 4475715ec005SGustavo F. Padovan if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) 44760a708f8fSGustavo F. Padovan return; 44770a708f8fSGustavo F. Padovan 44780a708f8fSGustavo F. Padovan if (encrypt == 0x00) { 44794343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) { 4480c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 4481f3f668b0SAndrzej Kaczmarek __set_chan_timer(chan, L2CAP_ENC_TIMEOUT); 44824343478fSGustavo F. Padovan } else if (chan->sec_level == BT_SECURITY_HIGH) 44830f852724SGustavo F. Padovan l2cap_chan_close(chan, ECONNREFUSED); 44840a708f8fSGustavo F. Padovan } else { 44854343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) 4486c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 44870a708f8fSGustavo F. Padovan } 44880a708f8fSGustavo F. Padovan } 44890a708f8fSGustavo F. Padovan 4490686ebf28SUlisses Furquim int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) 44910a708f8fSGustavo F. Padovan { 44920a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 449348454079SGustavo F. Padovan struct l2cap_chan *chan; 44940a708f8fSGustavo F. Padovan 44950a708f8fSGustavo F. Padovan if (!conn) 44960a708f8fSGustavo F. Padovan return 0; 44970a708f8fSGustavo F. Padovan 44980a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 44990a708f8fSGustavo F. Padovan 4500160dc6acSVinicius Costa Gomes if (hcon->type == LE_LINK) { 4501160dc6acSVinicius Costa Gomes smp_distribute_keys(conn, 0); 4502371fd835SUlisses Furquim __cancel_delayed_work(&conn->security_timer); 4503160dc6acSVinicius Costa Gomes } 4504160dc6acSVinicius Costa Gomes 45053d57dc68SGustavo F. Padovan rcu_read_lock(); 45060a708f8fSGustavo F. Padovan 45073d57dc68SGustavo F. Padovan list_for_each_entry_rcu(chan, &conn->chan_l, list) { 450848454079SGustavo F. Padovan struct sock *sk = chan->sk; 4509baa7e1faSGustavo F. Padovan 45100a708f8fSGustavo F. Padovan bh_lock_sock(sk); 45110a708f8fSGustavo F. Padovan 4512f1cb9af5SVinicius Costa Gomes BT_DBG("chan->scid %d", chan->scid); 4513f1cb9af5SVinicius Costa Gomes 4514f1cb9af5SVinicius Costa Gomes if (chan->scid == L2CAP_CID_LE_DATA) { 4515f1cb9af5SVinicius Costa Gomes if (!status && encrypt) { 4516f1cb9af5SVinicius Costa Gomes chan->sec_level = hcon->sec_level; 4517f1cb9af5SVinicius Costa Gomes l2cap_chan_ready(sk); 4518f1cb9af5SVinicius Costa Gomes } 4519f1cb9af5SVinicius Costa Gomes 4520f1cb9af5SVinicius Costa Gomes bh_unlock_sock(sk); 4521f1cb9af5SVinicius Costa Gomes continue; 4522f1cb9af5SVinicius Costa Gomes } 4523f1cb9af5SVinicius Costa Gomes 4524c1360a1cSGustavo F. Padovan if (test_bit(CONF_CONNECT_PEND, &chan->conf_state)) { 45250a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 45260a708f8fSGustavo F. Padovan continue; 45270a708f8fSGustavo F. Padovan } 45280a708f8fSGustavo F. Padovan 452989bc500eSGustavo F. Padovan if (!status && (chan->state == BT_CONNECTED || 453089bc500eSGustavo F. Padovan chan->state == BT_CONFIG)) { 45314343478fSGustavo F. Padovan l2cap_check_encryption(chan, encrypt); 45320a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 45330a708f8fSGustavo F. Padovan continue; 45340a708f8fSGustavo F. Padovan } 45350a708f8fSGustavo F. Padovan 453689bc500eSGustavo F. Padovan if (chan->state == BT_CONNECT) { 45370a708f8fSGustavo F. Padovan if (!status) { 45380a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 4539fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 4540fe4128e0SGustavo F. Padovan req.psm = chan->psm; 45410a708f8fSGustavo F. Padovan 4542fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 4543c1360a1cSGustavo F. Padovan set_bit(CONF_CONNECT_PEND, &chan->conf_state); 45440a708f8fSGustavo F. Padovan 4545fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 45460a708f8fSGustavo F. Padovan L2CAP_CONN_REQ, sizeof(req), &req); 45470a708f8fSGustavo F. Padovan } else { 4548c9b66675SGustavo F. Padovan __clear_chan_timer(chan); 4549f3f668b0SAndrzej Kaczmarek __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); 45500a708f8fSGustavo F. Padovan } 455189bc500eSGustavo F. Padovan } else if (chan->state == BT_CONNECT2) { 45520a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 4553df3c3931SJohan Hedberg __u16 res, stat; 45540a708f8fSGustavo F. Padovan 45550a708f8fSGustavo F. Padovan if (!status) { 4556df3c3931SJohan Hedberg if (bt_sk(sk)->defer_setup) { 4557df3c3931SJohan Hedberg struct sock *parent = bt_sk(sk)->parent; 4558df3c3931SJohan Hedberg res = L2CAP_CR_PEND; 4559df3c3931SJohan Hedberg stat = L2CAP_CS_AUTHOR_PEND; 456005e9a2f6SIlia Kolomisnky if (parent) 4561df3c3931SJohan Hedberg parent->sk_data_ready(parent, 0); 4562df3c3931SJohan Hedberg } else { 456389bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_CONFIG); 4564df3c3931SJohan Hedberg res = L2CAP_CR_SUCCESS; 4565df3c3931SJohan Hedberg stat = L2CAP_CS_NO_INFO; 4566df3c3931SJohan Hedberg } 45670a708f8fSGustavo F. Padovan } else { 456889bc500eSGustavo F. Padovan l2cap_state_change(chan, BT_DISCONN); 4569f3f668b0SAndrzej Kaczmarek __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); 4570df3c3931SJohan Hedberg res = L2CAP_CR_SEC_BLOCK; 4571df3c3931SJohan Hedberg stat = L2CAP_CS_NO_INFO; 45720a708f8fSGustavo F. Padovan } 45730a708f8fSGustavo F. Padovan 4574fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 4575fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 4576df3c3931SJohan Hedberg rsp.result = cpu_to_le16(res); 4577df3c3931SJohan Hedberg rsp.status = cpu_to_le16(stat); 4578fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 4579fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 45800a708f8fSGustavo F. Padovan } 45810a708f8fSGustavo F. Padovan 45820a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 45830a708f8fSGustavo F. Padovan } 45840a708f8fSGustavo F. Padovan 45853d57dc68SGustavo F. Padovan rcu_read_unlock(); 45860a708f8fSGustavo F. Padovan 45870a708f8fSGustavo F. Padovan return 0; 45880a708f8fSGustavo F. Padovan } 45890a708f8fSGustavo F. Padovan 4590686ebf28SUlisses Furquim int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) 45910a708f8fSGustavo F. Padovan { 45920a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 45930a708f8fSGustavo F. Padovan 45940a708f8fSGustavo F. Padovan if (!conn) 45950a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 45960a708f8fSGustavo F. Padovan 45970a708f8fSGustavo F. Padovan if (!conn) 45980a708f8fSGustavo F. Padovan goto drop; 45990a708f8fSGustavo F. Padovan 46000a708f8fSGustavo F. Padovan BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); 46010a708f8fSGustavo F. Padovan 46020a708f8fSGustavo F. Padovan if (!(flags & ACL_CONT)) { 46030a708f8fSGustavo F. Padovan struct l2cap_hdr *hdr; 460448454079SGustavo F. Padovan struct l2cap_chan *chan; 46050a708f8fSGustavo F. Padovan u16 cid; 46060a708f8fSGustavo F. Padovan int len; 46070a708f8fSGustavo F. Padovan 46080a708f8fSGustavo F. Padovan if (conn->rx_len) { 46090a708f8fSGustavo F. Padovan BT_ERR("Unexpected start frame (len %d)", skb->len); 46100a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 46110a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 46120a708f8fSGustavo F. Padovan conn->rx_len = 0; 46130a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46140a708f8fSGustavo F. Padovan } 46150a708f8fSGustavo F. Padovan 46160a708f8fSGustavo F. Padovan /* Start fragment always begin with Basic L2CAP header */ 46170a708f8fSGustavo F. Padovan if (skb->len < L2CAP_HDR_SIZE) { 46180a708f8fSGustavo F. Padovan BT_ERR("Frame is too short (len %d)", skb->len); 46190a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46200a708f8fSGustavo F. Padovan goto drop; 46210a708f8fSGustavo F. Padovan } 46220a708f8fSGustavo F. Padovan 46230a708f8fSGustavo F. Padovan hdr = (struct l2cap_hdr *) skb->data; 46240a708f8fSGustavo F. Padovan len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; 46250a708f8fSGustavo F. Padovan cid = __le16_to_cpu(hdr->cid); 46260a708f8fSGustavo F. Padovan 46270a708f8fSGustavo F. Padovan if (len == skb->len) { 46280a708f8fSGustavo F. Padovan /* Complete frame received */ 46290a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, skb); 46300a708f8fSGustavo F. Padovan return 0; 46310a708f8fSGustavo F. Padovan } 46320a708f8fSGustavo F. Padovan 46330a708f8fSGustavo F. Padovan BT_DBG("Start: total len %d, frag len %d", len, skb->len); 46340a708f8fSGustavo F. Padovan 46350a708f8fSGustavo F. Padovan if (skb->len > len) { 46360a708f8fSGustavo F. Padovan BT_ERR("Frame is too long (len %d, expected len %d)", 46370a708f8fSGustavo F. Padovan skb->len, len); 46380a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46390a708f8fSGustavo F. Padovan goto drop; 46400a708f8fSGustavo F. Padovan } 46410a708f8fSGustavo F. Padovan 4642baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 46430a708f8fSGustavo F. Padovan 464448454079SGustavo F. Padovan if (chan && chan->sk) { 464548454079SGustavo F. Padovan struct sock *sk = chan->sk; 464648454079SGustavo F. Padovan 46470c1bc5c6SGustavo F. Padovan if (chan->imtu < len - L2CAP_HDR_SIZE) { 464848454079SGustavo F. Padovan BT_ERR("Frame exceeding recv MTU (len %d, " 464948454079SGustavo F. Padovan "MTU %d)", len, 46500c1bc5c6SGustavo F. Padovan chan->imtu); 4651aa2ac881SGustavo F. Padovan release_sock(sk); 46520a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46530a708f8fSGustavo F. Padovan goto drop; 46540a708f8fSGustavo F. Padovan } 4655aa2ac881SGustavo F. Padovan release_sock(sk); 465648454079SGustavo F. Padovan } 46570a708f8fSGustavo F. Padovan 46580a708f8fSGustavo F. Padovan /* Allocate skb for the complete frame (with header) */ 46590a708f8fSGustavo F. Padovan conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); 46600a708f8fSGustavo F. Padovan if (!conn->rx_skb) 46610a708f8fSGustavo F. Padovan goto drop; 46620a708f8fSGustavo F. Padovan 46630a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 46640a708f8fSGustavo F. Padovan skb->len); 46650a708f8fSGustavo F. Padovan conn->rx_len = len - skb->len; 46660a708f8fSGustavo F. Padovan } else { 46670a708f8fSGustavo F. Padovan BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); 46680a708f8fSGustavo F. Padovan 46690a708f8fSGustavo F. Padovan if (!conn->rx_len) { 46700a708f8fSGustavo F. Padovan BT_ERR("Unexpected continuation frame (len %d)", skb->len); 46710a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46720a708f8fSGustavo F. Padovan goto drop; 46730a708f8fSGustavo F. Padovan } 46740a708f8fSGustavo F. Padovan 46750a708f8fSGustavo F. Padovan if (skb->len > conn->rx_len) { 46760a708f8fSGustavo F. Padovan BT_ERR("Fragment is too long (len %d, expected %d)", 46770a708f8fSGustavo F. Padovan skb->len, conn->rx_len); 46780a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 46790a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 46800a708f8fSGustavo F. Padovan conn->rx_len = 0; 46810a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 46820a708f8fSGustavo F. Padovan goto drop; 46830a708f8fSGustavo F. Padovan } 46840a708f8fSGustavo F. Padovan 46850a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 46860a708f8fSGustavo F. Padovan skb->len); 46870a708f8fSGustavo F. Padovan conn->rx_len -= skb->len; 46880a708f8fSGustavo F. Padovan 46890a708f8fSGustavo F. Padovan if (!conn->rx_len) { 46900a708f8fSGustavo F. Padovan /* Complete frame received */ 46910a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, conn->rx_skb); 46920a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 46930a708f8fSGustavo F. Padovan } 46940a708f8fSGustavo F. Padovan } 46950a708f8fSGustavo F. Padovan 46960a708f8fSGustavo F. Padovan drop: 46970a708f8fSGustavo F. Padovan kfree_skb(skb); 46980a708f8fSGustavo F. Padovan return 0; 46990a708f8fSGustavo F. Padovan } 47000a708f8fSGustavo F. Padovan 47010a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p) 47020a708f8fSGustavo F. Padovan { 470323691d75SGustavo F. Padovan struct l2cap_chan *c; 47040a708f8fSGustavo F. Padovan 470523691d75SGustavo F. Padovan read_lock_bh(&chan_list_lock); 47060a708f8fSGustavo F. Padovan 470723691d75SGustavo F. Padovan list_for_each_entry(c, &chan_list, global_l) { 470823691d75SGustavo F. Padovan struct sock *sk = c->sk; 47090a708f8fSGustavo F. Padovan 4710903d343eSGustavo F. Padovan seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", 47110a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->src), 47120a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->dst), 471389bc500eSGustavo F. Padovan c->state, __le16_to_cpu(c->psm), 471423691d75SGustavo F. Padovan c->scid, c->dcid, c->imtu, c->omtu, 471523691d75SGustavo F. Padovan c->sec_level, c->mode); 47160a708f8fSGustavo F. Padovan } 47170a708f8fSGustavo F. Padovan 471823691d75SGustavo F. Padovan read_unlock_bh(&chan_list_lock); 47190a708f8fSGustavo F. Padovan 47200a708f8fSGustavo F. Padovan return 0; 47210a708f8fSGustavo F. Padovan } 47220a708f8fSGustavo F. Padovan 47230a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file) 47240a708f8fSGustavo F. Padovan { 47250a708f8fSGustavo F. Padovan return single_open(file, l2cap_debugfs_show, inode->i_private); 47260a708f8fSGustavo F. Padovan } 47270a708f8fSGustavo F. Padovan 47280a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = { 47290a708f8fSGustavo F. Padovan .open = l2cap_debugfs_open, 47300a708f8fSGustavo F. Padovan .read = seq_read, 47310a708f8fSGustavo F. Padovan .llseek = seq_lseek, 47320a708f8fSGustavo F. Padovan .release = single_release, 47330a708f8fSGustavo F. Padovan }; 47340a708f8fSGustavo F. Padovan 47350a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs; 47360a708f8fSGustavo F. Padovan 473764274518SGustavo F. Padovan int __init l2cap_init(void) 47380a708f8fSGustavo F. Padovan { 47390a708f8fSGustavo F. Padovan int err; 47400a708f8fSGustavo F. Padovan 4741bb58f747SGustavo F. Padovan err = l2cap_init_sockets(); 47420a708f8fSGustavo F. Padovan if (err < 0) 47430a708f8fSGustavo F. Padovan return err; 47440a708f8fSGustavo F. Padovan 47450a708f8fSGustavo F. Padovan if (bt_debugfs) { 47460a708f8fSGustavo F. Padovan l2cap_debugfs = debugfs_create_file("l2cap", 0444, 47470a708f8fSGustavo F. Padovan bt_debugfs, NULL, &l2cap_debugfs_fops); 47480a708f8fSGustavo F. Padovan if (!l2cap_debugfs) 47490a708f8fSGustavo F. Padovan BT_ERR("Failed to create L2CAP debug file"); 47500a708f8fSGustavo F. Padovan } 47510a708f8fSGustavo F. Padovan 47520a708f8fSGustavo F. Padovan return 0; 47530a708f8fSGustavo F. Padovan } 47540a708f8fSGustavo F. Padovan 475564274518SGustavo F. Padovan void l2cap_exit(void) 47560a708f8fSGustavo F. Padovan { 47570a708f8fSGustavo F. Padovan debugfs_remove(l2cap_debugfs); 4758bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 47590a708f8fSGustavo F. Padovan } 47600a708f8fSGustavo F. Padovan 47610a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644); 47620a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); 4763