10a708f8fSGustavo F. Padovan /* 20a708f8fSGustavo F. Padovan BlueZ - Bluetooth protocol stack for Linux 30a708f8fSGustavo F. Padovan Copyright (C) 2000-2001 Qualcomm Incorporated 40a708f8fSGustavo F. Padovan Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org> 50a708f8fSGustavo F. Padovan Copyright (C) 2010 Google Inc. 60a708f8fSGustavo F. Padovan 70a708f8fSGustavo F. Padovan Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 80a708f8fSGustavo F. Padovan 90a708f8fSGustavo F. Padovan This program is free software; you can redistribute it and/or modify 100a708f8fSGustavo F. Padovan it under the terms of the GNU General Public License version 2 as 110a708f8fSGustavo F. Padovan published by the Free Software Foundation; 120a708f8fSGustavo F. Padovan 130a708f8fSGustavo F. Padovan THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 140a708f8fSGustavo F. Padovan OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 150a708f8fSGustavo F. Padovan FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 160a708f8fSGustavo F. Padovan IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 170a708f8fSGustavo F. Padovan CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 180a708f8fSGustavo F. Padovan WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 190a708f8fSGustavo F. Padovan ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 200a708f8fSGustavo F. Padovan OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 210a708f8fSGustavo F. Padovan 220a708f8fSGustavo F. Padovan ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 230a708f8fSGustavo F. Padovan COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 240a708f8fSGustavo F. Padovan SOFTWARE IS DISCLAIMED. 250a708f8fSGustavo F. Padovan */ 260a708f8fSGustavo F. Padovan 27bb58f747SGustavo F. Padovan /* Bluetooth L2CAP core. */ 280a708f8fSGustavo F. Padovan 290a708f8fSGustavo F. Padovan #include <linux/module.h> 300a708f8fSGustavo F. Padovan 310a708f8fSGustavo F. Padovan #include <linux/types.h> 320a708f8fSGustavo F. Padovan #include <linux/capability.h> 330a708f8fSGustavo F. Padovan #include <linux/errno.h> 340a708f8fSGustavo F. Padovan #include <linux/kernel.h> 350a708f8fSGustavo F. Padovan #include <linux/sched.h> 360a708f8fSGustavo F. Padovan #include <linux/slab.h> 370a708f8fSGustavo F. Padovan #include <linux/poll.h> 380a708f8fSGustavo F. Padovan #include <linux/fcntl.h> 390a708f8fSGustavo F. Padovan #include <linux/init.h> 400a708f8fSGustavo F. Padovan #include <linux/interrupt.h> 410a708f8fSGustavo F. Padovan #include <linux/socket.h> 420a708f8fSGustavo F. Padovan #include <linux/skbuff.h> 430a708f8fSGustavo F. Padovan #include <linux/list.h> 440a708f8fSGustavo F. Padovan #include <linux/device.h> 450a708f8fSGustavo F. Padovan #include <linux/debugfs.h> 460a708f8fSGustavo F. Padovan #include <linux/seq_file.h> 470a708f8fSGustavo F. Padovan #include <linux/uaccess.h> 480a708f8fSGustavo F. Padovan #include <linux/crc16.h> 490a708f8fSGustavo F. Padovan #include <net/sock.h> 500a708f8fSGustavo F. Padovan 510a708f8fSGustavo F. Padovan #include <asm/system.h> 520a708f8fSGustavo F. Padovan #include <asm/unaligned.h> 530a708f8fSGustavo F. Padovan 540a708f8fSGustavo F. Padovan #include <net/bluetooth/bluetooth.h> 550a708f8fSGustavo F. Padovan #include <net/bluetooth/hci_core.h> 560a708f8fSGustavo F. Padovan #include <net/bluetooth/l2cap.h> 570a708f8fSGustavo F. Padovan 58bb58f747SGustavo F. Padovan int disable_ertm; 590a708f8fSGustavo F. Padovan 600a708f8fSGustavo F. Padovan static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN; 610a708f8fSGustavo F. Padovan static u8 l2cap_fixed_chan[8] = { 0x02, }; 620a708f8fSGustavo F. Padovan 630a708f8fSGustavo F. Padovan static struct workqueue_struct *_busy_wq; 640a708f8fSGustavo F. Padovan 65bb58f747SGustavo F. Padovan struct bt_sock_list l2cap_sk_list = { 660a708f8fSGustavo F. Padovan .lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock) 670a708f8fSGustavo F. Padovan }; 680a708f8fSGustavo F. Padovan 690a708f8fSGustavo F. Padovan static void l2cap_busy_work(struct work_struct *work); 700a708f8fSGustavo F. Padovan 710a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 720a708f8fSGustavo F. Padovan u8 code, u8 ident, u16 dlen, void *data); 73710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data); 740a708f8fSGustavo F. Padovan 750a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb); 760a708f8fSGustavo F. Padovan 770a708f8fSGustavo F. Padovan /* ---- L2CAP channels ---- */ 78baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid) 790a708f8fSGustavo F. Padovan { 8048454079SGustavo F. Padovan struct l2cap_chan *c; 81baa7e1faSGustavo F. Padovan 82baa7e1faSGustavo F. Padovan list_for_each_entry(c, &conn->chan_l, list) { 83fe4128e0SGustavo F. Padovan if (c->dcid == cid) 8448454079SGustavo F. Padovan return c; 850a708f8fSGustavo F. Padovan } 86baa7e1faSGustavo F. Padovan return NULL; 87baa7e1faSGustavo F. Padovan 88baa7e1faSGustavo F. Padovan } 890a708f8fSGustavo F. Padovan 90baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) 910a708f8fSGustavo F. Padovan { 9248454079SGustavo F. Padovan struct l2cap_chan *c; 93baa7e1faSGustavo F. Padovan 94baa7e1faSGustavo F. Padovan list_for_each_entry(c, &conn->chan_l, list) { 95fe4128e0SGustavo F. Padovan if (c->scid == cid) 9648454079SGustavo F. Padovan return c; 970a708f8fSGustavo F. Padovan } 98baa7e1faSGustavo F. Padovan return NULL; 99baa7e1faSGustavo F. Padovan } 1000a708f8fSGustavo F. Padovan 1010a708f8fSGustavo F. Padovan /* Find channel with given SCID. 1020a708f8fSGustavo F. Padovan * Returns locked socket */ 103baa7e1faSGustavo F. Padovan static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) 1040a708f8fSGustavo F. Padovan { 10548454079SGustavo F. Padovan struct l2cap_chan *c; 106baa7e1faSGustavo F. Padovan 107baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 108baa7e1faSGustavo F. Padovan c = __l2cap_get_chan_by_scid(conn, cid); 10948454079SGustavo F. Padovan if (c) 11048454079SGustavo F. Padovan bh_lock_sock(c->sk); 111baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 11248454079SGustavo F. Padovan return c; 1130a708f8fSGustavo F. Padovan } 1140a708f8fSGustavo F. Padovan 115baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) 1160a708f8fSGustavo F. Padovan { 11748454079SGustavo F. Padovan struct l2cap_chan *c; 118baa7e1faSGustavo F. Padovan 119baa7e1faSGustavo F. Padovan list_for_each_entry(c, &conn->chan_l, list) { 120fc7f8a7eSGustavo F. Padovan if (c->ident == ident) 12148454079SGustavo F. Padovan return c; 1220a708f8fSGustavo F. Padovan } 123baa7e1faSGustavo F. Padovan return NULL; 124baa7e1faSGustavo F. Padovan } 1250a708f8fSGustavo F. Padovan 126baa7e1faSGustavo F. Padovan static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) 1270a708f8fSGustavo F. Padovan { 12848454079SGustavo F. Padovan struct l2cap_chan *c; 129baa7e1faSGustavo F. Padovan 130baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 131baa7e1faSGustavo F. Padovan c = __l2cap_get_chan_by_ident(conn, ident); 13248454079SGustavo F. Padovan if (c) 13348454079SGustavo F. Padovan bh_lock_sock(c->sk); 134baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 13548454079SGustavo F. Padovan return c; 1360a708f8fSGustavo F. Padovan } 1370a708f8fSGustavo F. Padovan 138baa7e1faSGustavo F. Padovan static u16 l2cap_alloc_cid(struct l2cap_conn *conn) 1390a708f8fSGustavo F. Padovan { 1400a708f8fSGustavo F. Padovan u16 cid = L2CAP_CID_DYN_START; 1410a708f8fSGustavo F. Padovan 1420a708f8fSGustavo F. Padovan for (; cid < L2CAP_CID_DYN_END; cid++) { 143baa7e1faSGustavo F. Padovan if (!__l2cap_get_chan_by_scid(conn, cid)) 1440a708f8fSGustavo F. Padovan return cid; 1450a708f8fSGustavo F. Padovan } 1460a708f8fSGustavo F. Padovan 1470a708f8fSGustavo F. Padovan return 0; 1480a708f8fSGustavo F. Padovan } 1490a708f8fSGustavo F. Padovan 1505d41ce1dSGustavo F. Padovan struct l2cap_chan *l2cap_chan_alloc(struct sock *sk) 1510a708f8fSGustavo F. Padovan { 15248454079SGustavo F. Padovan struct l2cap_chan *chan; 1530a708f8fSGustavo F. Padovan 15448454079SGustavo F. Padovan chan = kzalloc(sizeof(*chan), GFP_ATOMIC); 15548454079SGustavo F. Padovan if (!chan) 15648454079SGustavo F. Padovan return NULL; 1570a708f8fSGustavo F. Padovan 15848454079SGustavo F. Padovan chan->sk = sk; 15948454079SGustavo F. Padovan 16048454079SGustavo F. Padovan return chan; 1610a708f8fSGustavo F. Padovan } 1620a708f8fSGustavo F. Padovan 1636ff5abbfSGustavo F. Padovan void l2cap_chan_free(struct l2cap_chan *chan) 1646ff5abbfSGustavo F. Padovan { 1656ff5abbfSGustavo F. Padovan kfree(chan); 1666ff5abbfSGustavo F. Padovan } 1676ff5abbfSGustavo F. Padovan 16848454079SGustavo F. Padovan static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 1690a708f8fSGustavo F. Padovan { 17048454079SGustavo F. Padovan struct sock *sk = chan->sk; 1710a708f8fSGustavo F. Padovan 1720a708f8fSGustavo F. Padovan BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, 173fe4128e0SGustavo F. Padovan chan->psm, chan->dcid); 1740a708f8fSGustavo F. Padovan 1750a708f8fSGustavo F. Padovan conn->disc_reason = 0x13; 1760a708f8fSGustavo F. Padovan 1778c1d787bSGustavo F. Padovan chan->conn = conn; 1780a708f8fSGustavo F. Padovan 1790a708f8fSGustavo F. Padovan if (sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM) { 180b62f328bSVille Tervo if (conn->hcon->type == LE_LINK) { 181b62f328bSVille Tervo /* LE connection */ 1820c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_LE_DEFAULT_MTU; 183fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_LE_DATA; 184fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_LE_DATA; 185b62f328bSVille Tervo } else { 1860a708f8fSGustavo F. Padovan /* Alloc CID for connection-oriented socket */ 187fe4128e0SGustavo F. Padovan chan->scid = l2cap_alloc_cid(conn); 1880c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 189b62f328bSVille Tervo } 1900a708f8fSGustavo F. Padovan } else if (sk->sk_type == SOCK_DGRAM) { 1910a708f8fSGustavo F. Padovan /* Connectionless socket */ 192fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_CONN_LESS; 193fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_CONN_LESS; 1940c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 1950a708f8fSGustavo F. Padovan } else { 1960a708f8fSGustavo F. Padovan /* Raw socket can send/recv signalling messages only */ 197fe4128e0SGustavo F. Padovan chan->scid = L2CAP_CID_SIGNALING; 198fe4128e0SGustavo F. Padovan chan->dcid = L2CAP_CID_SIGNALING; 1990c1bc5c6SGustavo F. Padovan chan->omtu = L2CAP_DEFAULT_MTU; 2000a708f8fSGustavo F. Padovan } 2010a708f8fSGustavo F. Padovan 202baa7e1faSGustavo F. Padovan sock_hold(sk); 203baa7e1faSGustavo F. Padovan 204baa7e1faSGustavo F. Padovan list_add(&chan->list, &conn->chan_l); 2050a708f8fSGustavo F. Padovan } 2060a708f8fSGustavo F. Padovan 2070a708f8fSGustavo F. Padovan /* Delete channel. 2080a708f8fSGustavo F. Padovan * Must be called on the locked socket. */ 20948454079SGustavo F. Padovan void l2cap_chan_del(struct l2cap_chan *chan, int err) 2100a708f8fSGustavo F. Padovan { 21148454079SGustavo F. Padovan struct sock *sk = chan->sk; 2128c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 2130a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 2140a708f8fSGustavo F. Padovan 2150a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 2160a708f8fSGustavo F. Padovan 21749208c9cSGustavo F. Padovan BT_DBG("chan %p, conn %p, err %d", chan, conn, err); 2180a708f8fSGustavo F. Padovan 2190a708f8fSGustavo F. Padovan if (conn) { 220baa7e1faSGustavo F. Padovan /* Delete from channel list */ 221baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 222baa7e1faSGustavo F. Padovan list_del(&chan->list); 223baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 224baa7e1faSGustavo F. Padovan __sock_put(sk); 225baa7e1faSGustavo F. Padovan 2268c1d787bSGustavo F. Padovan chan->conn = NULL; 2270a708f8fSGustavo F. Padovan hci_conn_put(conn->hcon); 2280a708f8fSGustavo F. Padovan } 2290a708f8fSGustavo F. Padovan 2300a708f8fSGustavo F. Padovan sk->sk_state = BT_CLOSED; 2310a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 2320a708f8fSGustavo F. Padovan 2330a708f8fSGustavo F. Padovan if (err) 2340a708f8fSGustavo F. Padovan sk->sk_err = err; 2350a708f8fSGustavo F. Padovan 2360a708f8fSGustavo F. Padovan if (parent) { 2370a708f8fSGustavo F. Padovan bt_accept_unlink(sk); 2380a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 2390a708f8fSGustavo F. Padovan } else 2400a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 2410a708f8fSGustavo F. Padovan 242b4450035SGustavo F. Padovan if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE && 243b4450035SGustavo F. Padovan chan->conf_state & L2CAP_CONF_INPUT_DONE)) 2446ff5abbfSGustavo F. Padovan return; 2452ead70b8SGustavo F. Padovan 24658d35f87SGustavo F. Padovan skb_queue_purge(&chan->tx_q); 2470a708f8fSGustavo F. Padovan 2480c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 2490a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 2500a708f8fSGustavo F. Padovan 251e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 252e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 253e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 2540a708f8fSGustavo F. Padovan 255f1c6775bSGustavo F. Padovan skb_queue_purge(&chan->srej_q); 256f1c6775bSGustavo F. Padovan skb_queue_purge(&chan->busy_q); 2570a708f8fSGustavo F. Padovan 25839d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 2590a708f8fSGustavo F. Padovan list_del(&l->list); 2600a708f8fSGustavo F. Padovan kfree(l); 2610a708f8fSGustavo F. Padovan } 2620a708f8fSGustavo F. Padovan } 2630a708f8fSGustavo F. Padovan } 2640a708f8fSGustavo F. Padovan 2654343478fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) 2660a708f8fSGustavo F. Padovan { 2674343478fSGustavo F. Padovan struct sock *sk = chan->sk; 2684343478fSGustavo F. Padovan 2690a708f8fSGustavo F. Padovan if (sk->sk_type == SOCK_RAW) { 2704343478fSGustavo F. Padovan switch (chan->sec_level) { 2710a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 2720a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING_MITM; 2730a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 2740a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING; 2750a708f8fSGustavo F. Padovan default: 2760a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 2770a708f8fSGustavo F. Padovan } 278fe4128e0SGustavo F. Padovan } else if (chan->psm == cpu_to_le16(0x0001)) { 2794343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_LOW) 2804343478fSGustavo F. Padovan chan->sec_level = BT_SECURITY_SDP; 2810a708f8fSGustavo F. Padovan 2824343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_HIGH) 2830a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING_MITM; 2840a708f8fSGustavo F. Padovan else 2850a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 2860a708f8fSGustavo F. Padovan } else { 2874343478fSGustavo F. Padovan switch (chan->sec_level) { 2880a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 2890a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING_MITM; 2900a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 2910a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING; 2920a708f8fSGustavo F. Padovan default: 2930a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 2940a708f8fSGustavo F. Padovan } 2950a708f8fSGustavo F. Padovan } 2960a708f8fSGustavo F. Padovan } 2970a708f8fSGustavo F. Padovan 2980a708f8fSGustavo F. Padovan /* Service level security */ 2994343478fSGustavo F. Padovan static inline int l2cap_check_security(struct l2cap_chan *chan) 3000a708f8fSGustavo F. Padovan { 3018c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 3020a708f8fSGustavo F. Padovan __u8 auth_type; 3030a708f8fSGustavo F. Padovan 3044343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 3050a708f8fSGustavo F. Padovan 3064343478fSGustavo F. Padovan return hci_conn_security(conn->hcon, chan->sec_level, auth_type); 3070a708f8fSGustavo F. Padovan } 3080a708f8fSGustavo F. Padovan 30968983259SGustavo F. Padovan u8 l2cap_get_ident(struct l2cap_conn *conn) 3100a708f8fSGustavo F. Padovan { 3110a708f8fSGustavo F. Padovan u8 id; 3120a708f8fSGustavo F. Padovan 3130a708f8fSGustavo F. Padovan /* Get next available identificator. 3140a708f8fSGustavo F. Padovan * 1 - 128 are used by kernel. 3150a708f8fSGustavo F. Padovan * 129 - 199 are reserved. 3160a708f8fSGustavo F. Padovan * 200 - 254 are used by utilities like l2ping, etc. 3170a708f8fSGustavo F. Padovan */ 3180a708f8fSGustavo F. Padovan 3190a708f8fSGustavo F. Padovan spin_lock_bh(&conn->lock); 3200a708f8fSGustavo F. Padovan 3210a708f8fSGustavo F. Padovan if (++conn->tx_ident > 128) 3220a708f8fSGustavo F. Padovan conn->tx_ident = 1; 3230a708f8fSGustavo F. Padovan 3240a708f8fSGustavo F. Padovan id = conn->tx_ident; 3250a708f8fSGustavo F. Padovan 3260a708f8fSGustavo F. Padovan spin_unlock_bh(&conn->lock); 3270a708f8fSGustavo F. Padovan 3280a708f8fSGustavo F. Padovan return id; 3290a708f8fSGustavo F. Padovan } 3300a708f8fSGustavo F. Padovan 33168983259SGustavo F. Padovan void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data) 3320a708f8fSGustavo F. Padovan { 3330a708f8fSGustavo F. Padovan struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); 3340a708f8fSGustavo F. Padovan u8 flags; 3350a708f8fSGustavo F. Padovan 3360a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x", code); 3370a708f8fSGustavo F. Padovan 3380a708f8fSGustavo F. Padovan if (!skb) 3390a708f8fSGustavo F. Padovan return; 3400a708f8fSGustavo F. Padovan 3410a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 3420a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 3430a708f8fSGustavo F. Padovan else 3440a708f8fSGustavo F. Padovan flags = ACL_START; 3450a708f8fSGustavo F. Padovan 3460a708f8fSGustavo F. Padovan hci_send_acl(conn->hcon, skb, flags); 3470a708f8fSGustavo F. Padovan } 3480a708f8fSGustavo F. Padovan 349525cd185SGustavo F. Padovan static inline void l2cap_send_sframe(struct l2cap_chan *chan, u16 control) 3500a708f8fSGustavo F. Padovan { 3510a708f8fSGustavo F. Padovan struct sk_buff *skb; 3520a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 353525cd185SGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(chan->sk); 3548c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 3550a708f8fSGustavo F. Padovan struct sock *sk = (struct sock *)pi; 3560a708f8fSGustavo F. Padovan int count, hlen = L2CAP_HDR_SIZE + 2; 3570a708f8fSGustavo F. Padovan u8 flags; 3580a708f8fSGustavo F. Padovan 3590a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 3600a708f8fSGustavo F. Padovan return; 3610a708f8fSGustavo F. Padovan 36247d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 3630a708f8fSGustavo F. Padovan hlen += 2; 3640a708f8fSGustavo F. Padovan 36549208c9cSGustavo F. Padovan BT_DBG("chan %p, control 0x%2.2x", chan, control); 3660a708f8fSGustavo F. Padovan 3670a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, hlen); 3680a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FRAME_TYPE; 3690a708f8fSGustavo F. Padovan 370525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 3710a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 372525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 3730a708f8fSGustavo F. Padovan } 3740a708f8fSGustavo F. Padovan 375525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_PBIT) { 3760a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_POLL; 377525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_PBIT; 3780a708f8fSGustavo F. Padovan } 3790a708f8fSGustavo F. Padovan 3800a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 3810a708f8fSGustavo F. Padovan if (!skb) 3820a708f8fSGustavo F. Padovan return; 3830a708f8fSGustavo F. Padovan 3840a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 3850a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); 386fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 3870a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb_put(skb, 2)); 3880a708f8fSGustavo F. Padovan 38947d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 3900a708f8fSGustavo F. Padovan u16 fcs = crc16(0, (u8 *)lh, count - 2); 3910a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb_put(skb, 2)); 3920a708f8fSGustavo F. Padovan } 3930a708f8fSGustavo F. Padovan 3940a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 3950a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 3960a708f8fSGustavo F. Padovan else 3970a708f8fSGustavo F. Padovan flags = ACL_START; 3980a708f8fSGustavo F. Padovan 3998c1d787bSGustavo F. Padovan hci_send_acl(chan->conn->hcon, skb, flags); 4000a708f8fSGustavo F. Padovan } 4010a708f8fSGustavo F. Padovan 402525cd185SGustavo F. Padovan static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u16 control) 4030a708f8fSGustavo F. Padovan { 404525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 4050a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 406525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 4070a708f8fSGustavo F. Padovan } else 4080a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 4090a708f8fSGustavo F. Padovan 41042e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 4110a708f8fSGustavo F. Padovan 412525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 4130a708f8fSGustavo F. Padovan } 4140a708f8fSGustavo F. Padovan 415b4450035SGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) 4160a708f8fSGustavo F. Padovan { 417b4450035SGustavo F. Padovan return !(chan->conf_state & L2CAP_CONF_CONNECT_PEND); 4180a708f8fSGustavo F. Padovan } 4190a708f8fSGustavo F. Padovan 420fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan) 4210a708f8fSGustavo F. Padovan { 4228c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 4230a708f8fSGustavo F. Padovan 4240a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { 4250a708f8fSGustavo F. Padovan if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) 4260a708f8fSGustavo F. Padovan return; 4270a708f8fSGustavo F. Padovan 4284343478fSGustavo F. Padovan if (l2cap_check_security(chan) && 4294343478fSGustavo F. Padovan __l2cap_no_conn_pending(chan)) { 4300a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 431fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 432fe4128e0SGustavo F. Padovan req.psm = chan->psm; 4330a708f8fSGustavo F. Padovan 434fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 435b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 4360a708f8fSGustavo F. Padovan 437fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 438fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 4390a708f8fSGustavo F. Padovan } 4400a708f8fSGustavo F. Padovan } else { 4410a708f8fSGustavo F. Padovan struct l2cap_info_req req; 4420a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 4430a708f8fSGustavo F. Padovan 4440a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 4450a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 4460a708f8fSGustavo F. Padovan 4470a708f8fSGustavo F. Padovan mod_timer(&conn->info_timer, jiffies + 4480a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 4490a708f8fSGustavo F. Padovan 4500a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 4510a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 4520a708f8fSGustavo F. Padovan } 4530a708f8fSGustavo F. Padovan } 4540a708f8fSGustavo F. Padovan 4550a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) 4560a708f8fSGustavo F. Padovan { 4570a708f8fSGustavo F. Padovan u32 local_feat_mask = l2cap_feat_mask; 4580a708f8fSGustavo F. Padovan if (!disable_ertm) 4590a708f8fSGustavo F. Padovan local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; 4600a708f8fSGustavo F. Padovan 4610a708f8fSGustavo F. Padovan switch (mode) { 4620a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 4630a708f8fSGustavo F. Padovan return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; 4640a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 4650a708f8fSGustavo F. Padovan return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; 4660a708f8fSGustavo F. Padovan default: 4670a708f8fSGustavo F. Padovan return 0x00; 4680a708f8fSGustavo F. Padovan } 4690a708f8fSGustavo F. Padovan } 4700a708f8fSGustavo F. Padovan 471e92c8e70SGustavo F. Padovan void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err) 4720a708f8fSGustavo F. Padovan { 473e92c8e70SGustavo F. Padovan struct sock *sk; 4740a708f8fSGustavo F. Padovan struct l2cap_disconn_req req; 4750a708f8fSGustavo F. Padovan 4760a708f8fSGustavo F. Padovan if (!conn) 4770a708f8fSGustavo F. Padovan return; 4780a708f8fSGustavo F. Padovan 479e92c8e70SGustavo F. Padovan sk = chan->sk; 480e92c8e70SGustavo F. Padovan 4810c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) { 482e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 483e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 484e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 4850a708f8fSGustavo F. Padovan } 4860a708f8fSGustavo F. Padovan 487fe4128e0SGustavo F. Padovan req.dcid = cpu_to_le16(chan->dcid); 488fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 4890a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 4900a708f8fSGustavo F. Padovan L2CAP_DISCONN_REQ, sizeof(req), &req); 4910a708f8fSGustavo F. Padovan 4920a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 4930a708f8fSGustavo F. Padovan sk->sk_err = err; 4940a708f8fSGustavo F. Padovan } 4950a708f8fSGustavo F. Padovan 4960a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */ 4970a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn) 4980a708f8fSGustavo F. Padovan { 499820ffdb3SGustavo F. Padovan struct l2cap_chan *chan, *tmp; 5000a708f8fSGustavo F. Padovan 5010a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 5020a708f8fSGustavo F. Padovan 503baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 5040a708f8fSGustavo F. Padovan 505820ffdb3SGustavo F. Padovan list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { 50648454079SGustavo F. Padovan struct sock *sk = chan->sk; 507baa7e1faSGustavo F. Padovan 5080a708f8fSGustavo F. Padovan bh_lock_sock(sk); 5090a708f8fSGustavo F. Padovan 5100a708f8fSGustavo F. Padovan if (sk->sk_type != SOCK_SEQPACKET && 5110a708f8fSGustavo F. Padovan sk->sk_type != SOCK_STREAM) { 5120a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 5130a708f8fSGustavo F. Padovan continue; 5140a708f8fSGustavo F. Padovan } 5150a708f8fSGustavo F. Padovan 5160a708f8fSGustavo F. Padovan if (sk->sk_state == BT_CONNECT) { 5170a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 5180a708f8fSGustavo F. Padovan 5194343478fSGustavo F. Padovan if (!l2cap_check_security(chan) || 520b4450035SGustavo F. Padovan !__l2cap_no_conn_pending(chan)) { 5210a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 5220a708f8fSGustavo F. Padovan continue; 5230a708f8fSGustavo F. Padovan } 5240a708f8fSGustavo F. Padovan 5250c1bc5c6SGustavo F. Padovan if (!l2cap_mode_supported(chan->mode, 5260a708f8fSGustavo F. Padovan conn->feat_mask) 527b4450035SGustavo F. Padovan && chan->conf_state & 5280a708f8fSGustavo F. Padovan L2CAP_CONF_STATE2_DEVICE) { 529820ffdb3SGustavo F. Padovan /* __l2cap_sock_close() calls list_del(chan) 530820ffdb3SGustavo F. Padovan * so release the lock */ 531820ffdb3SGustavo F. Padovan read_unlock_bh(&conn->chan_lock); 532820ffdb3SGustavo F. Padovan __l2cap_sock_close(sk, ECONNRESET); 533820ffdb3SGustavo F. Padovan read_lock_bh(&conn->chan_lock); 5340a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 5350a708f8fSGustavo F. Padovan continue; 5360a708f8fSGustavo F. Padovan } 5370a708f8fSGustavo F. Padovan 538fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 539fe4128e0SGustavo F. Padovan req.psm = chan->psm; 5400a708f8fSGustavo F. Padovan 541fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 542b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 5430a708f8fSGustavo F. Padovan 544fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 545fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 5460a708f8fSGustavo F. Padovan 5470a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT2) { 5480a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 5490a708f8fSGustavo F. Padovan char buf[128]; 550fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 551fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 5520a708f8fSGustavo F. Padovan 5534343478fSGustavo F. Padovan if (l2cap_check_security(chan)) { 5540a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 5550a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 5560a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 5570a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); 5580a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 5590a708f8fSGustavo F. Padovan 5600a708f8fSGustavo F. Padovan } else { 5610a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 5620a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 5630a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 5640a708f8fSGustavo F. Padovan } 5650a708f8fSGustavo F. Padovan } else { 5660a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 5670a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); 5680a708f8fSGustavo F. Padovan } 5690a708f8fSGustavo F. Padovan 570fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 571fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 5720a708f8fSGustavo F. Padovan 573b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_REQ_SENT || 5740a708f8fSGustavo F. Padovan rsp.result != L2CAP_CR_SUCCESS) { 5750a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 5760a708f8fSGustavo F. Padovan continue; 5770a708f8fSGustavo F. Padovan } 5780a708f8fSGustavo F. Padovan 579b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 5800a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 58173ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 58273ffa904SGustavo F. Padovan chan->num_conf_req++; 5830a708f8fSGustavo F. Padovan } 5840a708f8fSGustavo F. Padovan 5850a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 5860a708f8fSGustavo F. Padovan } 5870a708f8fSGustavo F. Padovan 588baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 5890a708f8fSGustavo F. Padovan } 5900a708f8fSGustavo F. Padovan 591b62f328bSVille Tervo /* Find socket with cid and source bdaddr. 592b62f328bSVille Tervo * Returns closest match, locked. 593b62f328bSVille Tervo */ 594b62f328bSVille Tervo static struct sock *l2cap_get_sock_by_scid(int state, __le16 cid, bdaddr_t *src) 595b62f328bSVille Tervo { 596280f294fSGustavo F. Padovan struct sock *sk = NULL, *sk1 = NULL; 597b62f328bSVille Tervo struct hlist_node *node; 598b62f328bSVille Tervo 599b62f328bSVille Tervo read_lock(&l2cap_sk_list.lock); 600b62f328bSVille Tervo 601b62f328bSVille Tervo sk_for_each(sk, node, &l2cap_sk_list.head) { 602fe4128e0SGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 603fe4128e0SGustavo F. Padovan 604b62f328bSVille Tervo if (state && sk->sk_state != state) 605b62f328bSVille Tervo continue; 606b62f328bSVille Tervo 607fe4128e0SGustavo F. Padovan if (chan->scid == cid) { 608b62f328bSVille Tervo /* Exact match. */ 609b62f328bSVille Tervo if (!bacmp(&bt_sk(sk)->src, src)) 610b62f328bSVille Tervo break; 611b62f328bSVille Tervo 612b62f328bSVille Tervo /* Closest match */ 613b62f328bSVille Tervo if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 614b62f328bSVille Tervo sk1 = sk; 615b62f328bSVille Tervo } 616b62f328bSVille Tervo } 617280f294fSGustavo F. Padovan 618b62f328bSVille Tervo read_unlock(&l2cap_sk_list.lock); 619b62f328bSVille Tervo 620280f294fSGustavo F. Padovan return node ? sk : sk1; 621b62f328bSVille Tervo } 622b62f328bSVille Tervo 623b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn) 624b62f328bSVille Tervo { 625c916fbe4SGustavo F. Padovan struct sock *parent, *sk; 62648454079SGustavo F. Padovan struct l2cap_chan *chan; 627b62f328bSVille Tervo 628b62f328bSVille Tervo BT_DBG(""); 629b62f328bSVille Tervo 630b62f328bSVille Tervo /* Check if we have socket listening on cid */ 631b62f328bSVille Tervo parent = l2cap_get_sock_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, 632b62f328bSVille Tervo conn->src); 633b62f328bSVille Tervo if (!parent) 634b62f328bSVille Tervo return; 635b62f328bSVille Tervo 63662f3a2cfSGustavo F. Padovan bh_lock_sock(parent); 63762f3a2cfSGustavo F. Padovan 638b62f328bSVille Tervo /* Check for backlog size */ 639b62f328bSVille Tervo if (sk_acceptq_is_full(parent)) { 640b62f328bSVille Tervo BT_DBG("backlog full %d", parent->sk_ack_backlog); 641b62f328bSVille Tervo goto clean; 642b62f328bSVille Tervo } 643b62f328bSVille Tervo 644b62f328bSVille Tervo sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC); 645b62f328bSVille Tervo if (!sk) 646b62f328bSVille Tervo goto clean; 647b62f328bSVille Tervo 64848454079SGustavo F. Padovan chan = l2cap_chan_alloc(sk); 64948454079SGustavo F. Padovan if (!chan) { 65048454079SGustavo F. Padovan l2cap_sock_kill(sk); 65148454079SGustavo F. Padovan goto clean; 65248454079SGustavo F. Padovan } 65348454079SGustavo F. Padovan 6545d41ce1dSGustavo F. Padovan l2cap_pi(sk)->chan = chan; 6555d41ce1dSGustavo F. Padovan 656baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 657b62f328bSVille Tervo 658b62f328bSVille Tervo hci_conn_hold(conn->hcon); 659b62f328bSVille Tervo 660b62f328bSVille Tervo l2cap_sock_init(sk, parent); 661baa7e1faSGustavo F. Padovan 662b62f328bSVille Tervo bacpy(&bt_sk(sk)->src, conn->src); 663b62f328bSVille Tervo bacpy(&bt_sk(sk)->dst, conn->dst); 664b62f328bSVille Tervo 665d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 666d1010240SGustavo F. Padovan 66748454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 66848454079SGustavo F. Padovan 669b62f328bSVille Tervo l2cap_sock_set_timer(sk, sk->sk_sndtimeo); 670b62f328bSVille Tervo 671b62f328bSVille Tervo sk->sk_state = BT_CONNECTED; 672b62f328bSVille Tervo parent->sk_data_ready(parent, 0); 673b62f328bSVille Tervo 674baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 675b62f328bSVille Tervo 676b62f328bSVille Tervo clean: 677b62f328bSVille Tervo bh_unlock_sock(parent); 678b62f328bSVille Tervo } 679b62f328bSVille Tervo 6800a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn) 6810a708f8fSGustavo F. Padovan { 68248454079SGustavo F. Padovan struct l2cap_chan *chan; 6830a708f8fSGustavo F. Padovan 6840a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 6850a708f8fSGustavo F. Padovan 686b62f328bSVille Tervo if (!conn->hcon->out && conn->hcon->type == LE_LINK) 687b62f328bSVille Tervo l2cap_le_conn_ready(conn); 688b62f328bSVille Tervo 689baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 6900a708f8fSGustavo F. Padovan 691baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 69248454079SGustavo F. Padovan struct sock *sk = chan->sk; 693baa7e1faSGustavo F. Padovan 6940a708f8fSGustavo F. Padovan bh_lock_sock(sk); 6950a708f8fSGustavo F. Padovan 696acd7d370SVille Tervo if (conn->hcon->type == LE_LINK) { 697acd7d370SVille Tervo l2cap_sock_clear_timer(sk); 698acd7d370SVille Tervo sk->sk_state = BT_CONNECTED; 699acd7d370SVille Tervo sk->sk_state_change(sk); 700acd7d370SVille Tervo } 701acd7d370SVille Tervo 7020a708f8fSGustavo F. Padovan if (sk->sk_type != SOCK_SEQPACKET && 7030a708f8fSGustavo F. Padovan sk->sk_type != SOCK_STREAM) { 7040a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 7050a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 7060a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 7070a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT) 708fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 7090a708f8fSGustavo F. Padovan 7100a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7110a708f8fSGustavo F. Padovan } 7120a708f8fSGustavo F. Padovan 713baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 7140a708f8fSGustavo F. Padovan } 7150a708f8fSGustavo F. Padovan 7160a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */ 7170a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) 7180a708f8fSGustavo F. Padovan { 71948454079SGustavo F. Padovan struct l2cap_chan *chan; 7200a708f8fSGustavo F. Padovan 7210a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 7220a708f8fSGustavo F. Padovan 723baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 7240a708f8fSGustavo F. Padovan 725baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 72648454079SGustavo F. Padovan struct sock *sk = chan->sk; 727baa7e1faSGustavo F. Padovan 7284343478fSGustavo F. Padovan if (chan->force_reliable) 7290a708f8fSGustavo F. Padovan sk->sk_err = err; 7300a708f8fSGustavo F. Padovan } 7310a708f8fSGustavo F. Padovan 732baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 7330a708f8fSGustavo F. Padovan } 7340a708f8fSGustavo F. Padovan 7350a708f8fSGustavo F. Padovan static void l2cap_info_timeout(unsigned long arg) 7360a708f8fSGustavo F. Padovan { 7370a708f8fSGustavo F. Padovan struct l2cap_conn *conn = (void *) arg; 7380a708f8fSGustavo F. Padovan 7390a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 7400a708f8fSGustavo F. Padovan conn->info_ident = 0; 7410a708f8fSGustavo F. Padovan 7420a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 7430a708f8fSGustavo F. Padovan } 7440a708f8fSGustavo F. Padovan 7450a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) 7460a708f8fSGustavo F. Padovan { 7470a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 7480a708f8fSGustavo F. Padovan 7490a708f8fSGustavo F. Padovan if (conn || status) 7500a708f8fSGustavo F. Padovan return conn; 7510a708f8fSGustavo F. Padovan 7520a708f8fSGustavo F. Padovan conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); 7530a708f8fSGustavo F. Padovan if (!conn) 7540a708f8fSGustavo F. Padovan return NULL; 7550a708f8fSGustavo F. Padovan 7560a708f8fSGustavo F. Padovan hcon->l2cap_data = conn; 7570a708f8fSGustavo F. Padovan conn->hcon = hcon; 7580a708f8fSGustavo F. Padovan 7590a708f8fSGustavo F. Padovan BT_DBG("hcon %p conn %p", hcon, conn); 7600a708f8fSGustavo F. Padovan 761acd7d370SVille Tervo if (hcon->hdev->le_mtu && hcon->type == LE_LINK) 762acd7d370SVille Tervo conn->mtu = hcon->hdev->le_mtu; 763acd7d370SVille Tervo else 7640a708f8fSGustavo F. Padovan conn->mtu = hcon->hdev->acl_mtu; 765acd7d370SVille Tervo 7660a708f8fSGustavo F. Padovan conn->src = &hcon->hdev->bdaddr; 7670a708f8fSGustavo F. Padovan conn->dst = &hcon->dst; 7680a708f8fSGustavo F. Padovan 7690a708f8fSGustavo F. Padovan conn->feat_mask = 0; 7700a708f8fSGustavo F. Padovan 7710a708f8fSGustavo F. Padovan spin_lock_init(&conn->lock); 772baa7e1faSGustavo F. Padovan rwlock_init(&conn->chan_lock); 773baa7e1faSGustavo F. Padovan 774baa7e1faSGustavo F. Padovan INIT_LIST_HEAD(&conn->chan_l); 7750a708f8fSGustavo F. Padovan 776b62f328bSVille Tervo if (hcon->type != LE_LINK) 7770a708f8fSGustavo F. Padovan setup_timer(&conn->info_timer, l2cap_info_timeout, 7780a708f8fSGustavo F. Padovan (unsigned long) conn); 7790a708f8fSGustavo F. Padovan 7800a708f8fSGustavo F. Padovan conn->disc_reason = 0x13; 7810a708f8fSGustavo F. Padovan 7820a708f8fSGustavo F. Padovan return conn; 7830a708f8fSGustavo F. Padovan } 7840a708f8fSGustavo F. Padovan 7850a708f8fSGustavo F. Padovan static void l2cap_conn_del(struct hci_conn *hcon, int err) 7860a708f8fSGustavo F. Padovan { 7870a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 788baa7e1faSGustavo F. Padovan struct l2cap_chan *chan, *l; 7890a708f8fSGustavo F. Padovan struct sock *sk; 7900a708f8fSGustavo F. Padovan 7910a708f8fSGustavo F. Padovan if (!conn) 7920a708f8fSGustavo F. Padovan return; 7930a708f8fSGustavo F. Padovan 7940a708f8fSGustavo F. Padovan BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); 7950a708f8fSGustavo F. Padovan 7960a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 7970a708f8fSGustavo F. Padovan 7980a708f8fSGustavo F. Padovan /* Kill channels */ 799baa7e1faSGustavo F. Padovan list_for_each_entry_safe(chan, l, &conn->chan_l, list) { 80048454079SGustavo F. Padovan sk = chan->sk; 8010a708f8fSGustavo F. Padovan bh_lock_sock(sk); 80248454079SGustavo F. Padovan l2cap_chan_del(chan, err); 8030a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 8040a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 8050a708f8fSGustavo F. Padovan } 8060a708f8fSGustavo F. Padovan 8070a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) 8080a708f8fSGustavo F. Padovan del_timer_sync(&conn->info_timer); 8090a708f8fSGustavo F. Padovan 8100a708f8fSGustavo F. Padovan hcon->l2cap_data = NULL; 8110a708f8fSGustavo F. Padovan kfree(conn); 8120a708f8fSGustavo F. Padovan } 8130a708f8fSGustavo F. Padovan 81448454079SGustavo F. Padovan static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 8150a708f8fSGustavo F. Padovan { 816baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 81748454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 818baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 8190a708f8fSGustavo F. Padovan } 8200a708f8fSGustavo F. Padovan 8210a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */ 8220a708f8fSGustavo F. Padovan 8230a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr. 8240a708f8fSGustavo F. Padovan * Returns closest match. 8250a708f8fSGustavo F. Padovan */ 8260a708f8fSGustavo F. Padovan static struct sock *l2cap_get_sock_by_psm(int state, __le16 psm, bdaddr_t *src) 8270a708f8fSGustavo F. Padovan { 8280a708f8fSGustavo F. Padovan struct sock *sk = NULL, *sk1 = NULL; 8290a708f8fSGustavo F. Padovan struct hlist_node *node; 8300a708f8fSGustavo F. Padovan 8310a708f8fSGustavo F. Padovan read_lock(&l2cap_sk_list.lock); 8320a708f8fSGustavo F. Padovan 8330a708f8fSGustavo F. Padovan sk_for_each(sk, node, &l2cap_sk_list.head) { 834fe4128e0SGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 835fe4128e0SGustavo F. Padovan 8360a708f8fSGustavo F. Padovan if (state && sk->sk_state != state) 8370a708f8fSGustavo F. Padovan continue; 8380a708f8fSGustavo F. Padovan 839fe4128e0SGustavo F. Padovan if (chan->psm == psm) { 8400a708f8fSGustavo F. Padovan /* Exact match. */ 8410a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) 8420a708f8fSGustavo F. Padovan break; 8430a708f8fSGustavo F. Padovan 8440a708f8fSGustavo F. Padovan /* Closest match */ 8450a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 8460a708f8fSGustavo F. Padovan sk1 = sk; 8470a708f8fSGustavo F. Padovan } 8480a708f8fSGustavo F. Padovan } 8490a708f8fSGustavo F. Padovan 8500a708f8fSGustavo F. Padovan read_unlock(&l2cap_sk_list.lock); 8510a708f8fSGustavo F. Padovan 8520a708f8fSGustavo F. Padovan return node ? sk : sk1; 8530a708f8fSGustavo F. Padovan } 8540a708f8fSGustavo F. Padovan 85577a74c7eSGustavo F. Padovan int l2cap_chan_connect(struct l2cap_chan *chan) 8560a708f8fSGustavo F. Padovan { 8575d41ce1dSGustavo F. Padovan struct sock *sk = chan->sk; 8580a708f8fSGustavo F. Padovan bdaddr_t *src = &bt_sk(sk)->src; 8590a708f8fSGustavo F. Padovan bdaddr_t *dst = &bt_sk(sk)->dst; 8600a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 8610a708f8fSGustavo F. Padovan struct hci_conn *hcon; 8620a708f8fSGustavo F. Padovan struct hci_dev *hdev; 8630a708f8fSGustavo F. Padovan __u8 auth_type; 8640a708f8fSGustavo F. Padovan int err; 8650a708f8fSGustavo F. Padovan 8660a708f8fSGustavo F. Padovan BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), 867fe4128e0SGustavo F. Padovan chan->psm); 8680a708f8fSGustavo F. Padovan 8690a708f8fSGustavo F. Padovan hdev = hci_get_route(dst, src); 8700a708f8fSGustavo F. Padovan if (!hdev) 8710a708f8fSGustavo F. Padovan return -EHOSTUNREACH; 8720a708f8fSGustavo F. Padovan 8730a708f8fSGustavo F. Padovan hci_dev_lock_bh(hdev); 8740a708f8fSGustavo F. Padovan 8754343478fSGustavo F. Padovan auth_type = l2cap_get_auth_type(chan); 8760a708f8fSGustavo F. Padovan 877fe4128e0SGustavo F. Padovan if (chan->dcid == L2CAP_CID_LE_DATA) 878acd7d370SVille Tervo hcon = hci_connect(hdev, LE_LINK, dst, 8794343478fSGustavo F. Padovan chan->sec_level, auth_type); 880acd7d370SVille Tervo else 8810a708f8fSGustavo F. Padovan hcon = hci_connect(hdev, ACL_LINK, dst, 8824343478fSGustavo F. Padovan chan->sec_level, auth_type); 883acd7d370SVille Tervo 88430e76272SVille Tervo if (IS_ERR(hcon)) { 88530e76272SVille Tervo err = PTR_ERR(hcon); 8860a708f8fSGustavo F. Padovan goto done; 88730e76272SVille Tervo } 8880a708f8fSGustavo F. Padovan 8890a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 8900a708f8fSGustavo F. Padovan if (!conn) { 8910a708f8fSGustavo F. Padovan hci_conn_put(hcon); 89230e76272SVille Tervo err = -ENOMEM; 8930a708f8fSGustavo F. Padovan goto done; 8940a708f8fSGustavo F. Padovan } 8950a708f8fSGustavo F. Padovan 8960a708f8fSGustavo F. Padovan /* Update source addr of the socket */ 8970a708f8fSGustavo F. Padovan bacpy(src, conn->src); 8980a708f8fSGustavo F. Padovan 89948454079SGustavo F. Padovan l2cap_chan_add(conn, chan); 90048454079SGustavo F. Padovan 9010a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT; 9020a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, sk->sk_sndtimeo); 9030a708f8fSGustavo F. Padovan 9040a708f8fSGustavo F. Padovan if (hcon->state == BT_CONNECTED) { 9050a708f8fSGustavo F. Padovan if (sk->sk_type != SOCK_SEQPACKET && 9060a708f8fSGustavo F. Padovan sk->sk_type != SOCK_STREAM) { 9070a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 9084343478fSGustavo F. Padovan if (l2cap_check_security(chan)) 9090a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 9100a708f8fSGustavo F. Padovan } else 911fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 9120a708f8fSGustavo F. Padovan } 9130a708f8fSGustavo F. Padovan 91430e76272SVille Tervo err = 0; 91530e76272SVille Tervo 9160a708f8fSGustavo F. Padovan done: 9170a708f8fSGustavo F. Padovan hci_dev_unlock_bh(hdev); 9180a708f8fSGustavo F. Padovan hci_dev_put(hdev); 9190a708f8fSGustavo F. Padovan return err; 9200a708f8fSGustavo F. Padovan } 9210a708f8fSGustavo F. Padovan 922dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk) 9230a708f8fSGustavo F. Padovan { 9248c1d787bSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 9250a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 9260a708f8fSGustavo F. Padovan int err = 0; 9270a708f8fSGustavo F. Padovan int timeo = HZ/5; 9280a708f8fSGustavo F. Padovan 9290a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 9308c1d787bSGustavo F. Padovan while ((chan->unacked_frames > 0 && chan->conn)) { 9310a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 9320a708f8fSGustavo F. Padovan 9330a708f8fSGustavo F. Padovan if (!timeo) 9340a708f8fSGustavo F. Padovan timeo = HZ/5; 9350a708f8fSGustavo F. Padovan 9360a708f8fSGustavo F. Padovan if (signal_pending(current)) { 9370a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 9380a708f8fSGustavo F. Padovan break; 9390a708f8fSGustavo F. Padovan } 9400a708f8fSGustavo F. Padovan 9410a708f8fSGustavo F. Padovan release_sock(sk); 9420a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 9430a708f8fSGustavo F. Padovan lock_sock(sk); 9440a708f8fSGustavo F. Padovan 9450a708f8fSGustavo F. Padovan err = sock_error(sk); 9460a708f8fSGustavo F. Padovan if (err) 9470a708f8fSGustavo F. Padovan break; 9480a708f8fSGustavo F. Padovan } 9490a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 9500a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 9510a708f8fSGustavo F. Padovan return err; 9520a708f8fSGustavo F. Padovan } 9530a708f8fSGustavo F. Padovan 9540a708f8fSGustavo F. Padovan static void l2cap_monitor_timeout(unsigned long arg) 9550a708f8fSGustavo F. Padovan { 956525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 957525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 9580a708f8fSGustavo F. Padovan 959525cd185SGustavo F. Padovan BT_DBG("chan %p", chan); 9600a708f8fSGustavo F. Padovan 9610a708f8fSGustavo F. Padovan bh_lock_sock(sk); 9622c03a7a4SGustavo F. Padovan if (chan->retry_count >= chan->remote_max_tx) { 9638c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 9640a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9650a708f8fSGustavo F. Padovan return; 9660a708f8fSGustavo F. Padovan } 9670a708f8fSGustavo F. Padovan 9686a026610SGustavo F. Padovan chan->retry_count++; 9690a708f8fSGustavo F. Padovan __mod_monitor_timer(); 9700a708f8fSGustavo F. Padovan 971525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 9720a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9730a708f8fSGustavo F. Padovan } 9740a708f8fSGustavo F. Padovan 9750a708f8fSGustavo F. Padovan static void l2cap_retrans_timeout(unsigned long arg) 9760a708f8fSGustavo F. Padovan { 977525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 978525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 9790a708f8fSGustavo F. Padovan 98049208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 9810a708f8fSGustavo F. Padovan 9820a708f8fSGustavo F. Padovan bh_lock_sock(sk); 9836a026610SGustavo F. Padovan chan->retry_count = 1; 9840a708f8fSGustavo F. Padovan __mod_monitor_timer(); 9850a708f8fSGustavo F. Padovan 986525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_WAIT_F; 9870a708f8fSGustavo F. Padovan 988525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 9890a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9900a708f8fSGustavo F. Padovan } 9910a708f8fSGustavo F. Padovan 99242e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan) 9930a708f8fSGustavo F. Padovan { 9940a708f8fSGustavo F. Padovan struct sk_buff *skb; 9950a708f8fSGustavo F. Padovan 99658d35f87SGustavo F. Padovan while ((skb = skb_peek(&chan->tx_q)) && 9976a026610SGustavo F. Padovan chan->unacked_frames) { 99842e5c802SGustavo F. Padovan if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) 9990a708f8fSGustavo F. Padovan break; 10000a708f8fSGustavo F. Padovan 100158d35f87SGustavo F. Padovan skb = skb_dequeue(&chan->tx_q); 10020a708f8fSGustavo F. Padovan kfree_skb(skb); 10030a708f8fSGustavo F. Padovan 10046a026610SGustavo F. Padovan chan->unacked_frames--; 10050a708f8fSGustavo F. Padovan } 10060a708f8fSGustavo F. Padovan 10076a026610SGustavo F. Padovan if (!chan->unacked_frames) 1008e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 10090a708f8fSGustavo F. Padovan } 10100a708f8fSGustavo F. Padovan 10114343478fSGustavo F. Padovan void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) 10120a708f8fSGustavo F. Padovan { 10138c1d787bSGustavo F. Padovan struct hci_conn *hcon = chan->conn->hcon; 10140a708f8fSGustavo F. Padovan u16 flags; 10150a708f8fSGustavo F. Padovan 10164343478fSGustavo F. Padovan BT_DBG("chan %p, skb %p len %d", chan, skb, skb->len); 10170a708f8fSGustavo F. Padovan 10184343478fSGustavo F. Padovan if (!chan->flushable && lmp_no_flush_capable(hcon->hdev)) 10190a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 10200a708f8fSGustavo F. Padovan else 10210a708f8fSGustavo F. Padovan flags = ACL_START; 10220a708f8fSGustavo F. Padovan 10230a708f8fSGustavo F. Padovan hci_send_acl(hcon, skb, flags); 10240a708f8fSGustavo F. Padovan } 10250a708f8fSGustavo F. Padovan 102642e5c802SGustavo F. Padovan void l2cap_streaming_send(struct l2cap_chan *chan) 10270a708f8fSGustavo F. Padovan { 10280a708f8fSGustavo F. Padovan struct sk_buff *skb; 10290a708f8fSGustavo F. Padovan u16 control, fcs; 10300a708f8fSGustavo F. Padovan 103158d35f87SGustavo F. Padovan while ((skb = skb_dequeue(&chan->tx_q))) { 10320a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data + L2CAP_HDR_SIZE); 103342e5c802SGustavo F. Padovan control |= chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT; 10340a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb->data + L2CAP_HDR_SIZE); 10350a708f8fSGustavo F. Padovan 103647d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 10370a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)skb->data, skb->len - 2); 10380a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb->data + skb->len - 2); 10390a708f8fSGustavo F. Padovan } 10400a708f8fSGustavo F. Padovan 10414343478fSGustavo F. Padovan l2cap_do_send(chan, skb); 10420a708f8fSGustavo F. Padovan 104342e5c802SGustavo F. Padovan chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; 10440a708f8fSGustavo F. Padovan } 10450a708f8fSGustavo F. Padovan } 10460a708f8fSGustavo F. Padovan 1047525cd185SGustavo F. Padovan static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq) 10480a708f8fSGustavo F. Padovan { 10490a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 10500a708f8fSGustavo F. Padovan u16 control, fcs; 10510a708f8fSGustavo F. Padovan 105258d35f87SGustavo F. Padovan skb = skb_peek(&chan->tx_q); 10530a708f8fSGustavo F. Padovan if (!skb) 10540a708f8fSGustavo F. Padovan return; 10550a708f8fSGustavo F. Padovan 10560a708f8fSGustavo F. Padovan do { 10570a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq == tx_seq) 10580a708f8fSGustavo F. Padovan break; 10590a708f8fSGustavo F. Padovan 106058d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 10610a708f8fSGustavo F. Padovan return; 10620a708f8fSGustavo F. Padovan 106358d35f87SGustavo F. Padovan } while ((skb = skb_queue_next(&chan->tx_q, skb))); 10640a708f8fSGustavo F. Padovan 10652c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 10662c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 10678c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 10680a708f8fSGustavo F. Padovan return; 10690a708f8fSGustavo F. Padovan } 10700a708f8fSGustavo F. Padovan 10710a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 10720a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 10730a708f8fSGustavo F. Padovan control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); 1074a429b519SRuiyi Zhang control &= L2CAP_CTRL_SAR; 10750a708f8fSGustavo F. Padovan 1076525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 10770a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 1078525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 10790a708f8fSGustavo F. Padovan } 10800a708f8fSGustavo F. Padovan 108142e5c802SGustavo F. Padovan control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT) 10820a708f8fSGustavo F. Padovan | (tx_seq << L2CAP_CTRL_TXSEQ_SHIFT); 10830a708f8fSGustavo F. Padovan 10840a708f8fSGustavo F. Padovan put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); 10850a708f8fSGustavo F. Padovan 108647d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 10870a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)tx_skb->data, tx_skb->len - 2); 10880a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, tx_skb->data + tx_skb->len - 2); 10890a708f8fSGustavo F. Padovan } 10900a708f8fSGustavo F. Padovan 10914343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 10920a708f8fSGustavo F. Padovan } 10930a708f8fSGustavo F. Padovan 1094525cd185SGustavo F. Padovan int l2cap_ertm_send(struct l2cap_chan *chan) 10950a708f8fSGustavo F. Padovan { 10960a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 1097525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 10980a708f8fSGustavo F. Padovan u16 control, fcs; 10990a708f8fSGustavo F. Padovan int nsent = 0; 11000a708f8fSGustavo F. Padovan 11010a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 11020a708f8fSGustavo F. Padovan return -ENOTCONN; 11030a708f8fSGustavo F. Padovan 110458d35f87SGustavo F. Padovan while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) { 11050a708f8fSGustavo F. Padovan 11062c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 11072c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 11088c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); 11090a708f8fSGustavo F. Padovan break; 11100a708f8fSGustavo F. Padovan } 11110a708f8fSGustavo F. Padovan 11120a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 11130a708f8fSGustavo F. Padovan 11140a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 11150a708f8fSGustavo F. Padovan 11160a708f8fSGustavo F. Padovan control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); 11170a708f8fSGustavo F. Padovan control &= L2CAP_CTRL_SAR; 11180a708f8fSGustavo F. Padovan 1119525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 11200a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 1121525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 11220a708f8fSGustavo F. Padovan } 112342e5c802SGustavo F. Padovan control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT) 112442e5c802SGustavo F. Padovan | (chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT); 11250a708f8fSGustavo F. Padovan put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); 11260a708f8fSGustavo F. Padovan 11270a708f8fSGustavo F. Padovan 112847d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 11290a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2); 11300a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb->data + tx_skb->len - 2); 11310a708f8fSGustavo F. Padovan } 11320a708f8fSGustavo F. Padovan 11334343478fSGustavo F. Padovan l2cap_do_send(chan, tx_skb); 11340a708f8fSGustavo F. Padovan 11350a708f8fSGustavo F. Padovan __mod_retrans_timer(); 11360a708f8fSGustavo F. Padovan 113742e5c802SGustavo F. Padovan bt_cb(skb)->tx_seq = chan->next_tx_seq; 113842e5c802SGustavo F. Padovan chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; 11390a708f8fSGustavo F. Padovan 114023e9fde2SSuraj Sumangala if (bt_cb(skb)->retries == 1) 11416a026610SGustavo F. Padovan chan->unacked_frames++; 114223e9fde2SSuraj Sumangala 11436a026610SGustavo F. Padovan chan->frames_sent++; 11440a708f8fSGustavo F. Padovan 114558d35f87SGustavo F. Padovan if (skb_queue_is_last(&chan->tx_q, skb)) 114658d35f87SGustavo F. Padovan chan->tx_send_head = NULL; 11470a708f8fSGustavo F. Padovan else 114858d35f87SGustavo F. Padovan chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); 11490a708f8fSGustavo F. Padovan 11500a708f8fSGustavo F. Padovan nsent++; 11510a708f8fSGustavo F. Padovan } 11520a708f8fSGustavo F. Padovan 11530a708f8fSGustavo F. Padovan return nsent; 11540a708f8fSGustavo F. Padovan } 11550a708f8fSGustavo F. Padovan 1156525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan) 11570a708f8fSGustavo F. Padovan { 11580a708f8fSGustavo F. Padovan int ret; 11590a708f8fSGustavo F. Padovan 116058d35f87SGustavo F. Padovan if (!skb_queue_empty(&chan->tx_q)) 116158d35f87SGustavo F. Padovan chan->tx_send_head = chan->tx_q.next; 11620a708f8fSGustavo F. Padovan 116342e5c802SGustavo F. Padovan chan->next_tx_seq = chan->expected_ack_seq; 1164525cd185SGustavo F. Padovan ret = l2cap_ertm_send(chan); 11650a708f8fSGustavo F. Padovan return ret; 11660a708f8fSGustavo F. Padovan } 11670a708f8fSGustavo F. Padovan 1168525cd185SGustavo F. Padovan static void l2cap_send_ack(struct l2cap_chan *chan) 11690a708f8fSGustavo F. Padovan { 11700a708f8fSGustavo F. Padovan u16 control = 0; 11710a708f8fSGustavo F. Padovan 117242e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 11730a708f8fSGustavo F. Padovan 1174525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 11750a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 1176525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 1177525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 11780a708f8fSGustavo F. Padovan return; 11790a708f8fSGustavo F. Padovan } 11800a708f8fSGustavo F. Padovan 1181525cd185SGustavo F. Padovan if (l2cap_ertm_send(chan) > 0) 11820a708f8fSGustavo F. Padovan return; 11830a708f8fSGustavo F. Padovan 11840a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 1185525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 11860a708f8fSGustavo F. Padovan } 11870a708f8fSGustavo F. Padovan 1188525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan) 11890a708f8fSGustavo F. Padovan { 11900a708f8fSGustavo F. Padovan struct srej_list *tail; 11910a708f8fSGustavo F. Padovan u16 control; 11920a708f8fSGustavo F. Padovan 11930a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 11940a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 11950a708f8fSGustavo F. Padovan 119639d5a3eeSGustavo F. Padovan tail = list_entry((&chan->srej_l)->prev, struct srej_list, list); 11970a708f8fSGustavo F. Padovan control |= tail->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 11980a708f8fSGustavo F. Padovan 1199525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 12000a708f8fSGustavo F. Padovan } 12010a708f8fSGustavo F. Padovan 12020a708f8fSGustavo F. Padovan static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb) 12030a708f8fSGustavo F. Padovan { 12048c1d787bSGustavo F. Padovan struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn; 12050a708f8fSGustavo F. Padovan struct sk_buff **frag; 12060a708f8fSGustavo F. Padovan int err, sent = 0; 12070a708f8fSGustavo F. Padovan 12080a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) 12090a708f8fSGustavo F. Padovan return -EFAULT; 12100a708f8fSGustavo F. Padovan 12110a708f8fSGustavo F. Padovan sent += count; 12120a708f8fSGustavo F. Padovan len -= count; 12130a708f8fSGustavo F. Padovan 12140a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 12150a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 12160a708f8fSGustavo F. Padovan while (len) { 12170a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 12180a708f8fSGustavo F. Padovan 12190a708f8fSGustavo F. Padovan *frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err); 12200a708f8fSGustavo F. Padovan if (!*frag) 12210a708f8fSGustavo F. Padovan return err; 12220a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) 12230a708f8fSGustavo F. Padovan return -EFAULT; 12240a708f8fSGustavo F. Padovan 12250a708f8fSGustavo F. Padovan sent += count; 12260a708f8fSGustavo F. Padovan len -= count; 12270a708f8fSGustavo F. Padovan 12280a708f8fSGustavo F. Padovan frag = &(*frag)->next; 12290a708f8fSGustavo F. Padovan } 12300a708f8fSGustavo F. Padovan 12310a708f8fSGustavo F. Padovan return sent; 12320a708f8fSGustavo F. Padovan } 12330a708f8fSGustavo F. Padovan 1234fe4128e0SGustavo F. Padovan struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 12350a708f8fSGustavo F. Padovan { 1236fe4128e0SGustavo F. Padovan struct sock *sk = chan->sk; 12378c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 12380a708f8fSGustavo F. Padovan struct sk_buff *skb; 12390a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE + 2; 12400a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 12410a708f8fSGustavo F. Padovan 12420a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 12430a708f8fSGustavo F. Padovan 12440a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 12450a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 12460a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 12470a708f8fSGustavo F. Padovan if (!skb) 12480a708f8fSGustavo F. Padovan return ERR_PTR(err); 12490a708f8fSGustavo F. Padovan 12500a708f8fSGustavo F. Padovan /* Create L2CAP header */ 12510a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1252fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 12530a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 1254fe4128e0SGustavo F. Padovan put_unaligned_le16(chan->psm, skb_put(skb, 2)); 12550a708f8fSGustavo F. Padovan 12560a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 12570a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 12580a708f8fSGustavo F. Padovan kfree_skb(skb); 12590a708f8fSGustavo F. Padovan return ERR_PTR(err); 12600a708f8fSGustavo F. Padovan } 12610a708f8fSGustavo F. Padovan return skb; 12620a708f8fSGustavo F. Padovan } 12630a708f8fSGustavo F. Padovan 1264fe4128e0SGustavo F. Padovan struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 12650a708f8fSGustavo F. Padovan { 1266fe4128e0SGustavo F. Padovan struct sock *sk = chan->sk; 12678c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 12680a708f8fSGustavo F. Padovan struct sk_buff *skb; 12690a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE; 12700a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 12710a708f8fSGustavo F. Padovan 12720a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 12730a708f8fSGustavo F. Padovan 12740a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 12750a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 12760a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 12770a708f8fSGustavo F. Padovan if (!skb) 12780a708f8fSGustavo F. Padovan return ERR_PTR(err); 12790a708f8fSGustavo F. Padovan 12800a708f8fSGustavo F. Padovan /* Create L2CAP header */ 12810a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1282fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 12830a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 12840a708f8fSGustavo F. Padovan 12850a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 12860a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 12870a708f8fSGustavo F. Padovan kfree_skb(skb); 12880a708f8fSGustavo F. Padovan return ERR_PTR(err); 12890a708f8fSGustavo F. Padovan } 12900a708f8fSGustavo F. Padovan return skb; 12910a708f8fSGustavo F. Padovan } 12920a708f8fSGustavo F. Padovan 129347d1ec61SGustavo F. Padovan struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len, u16 control, u16 sdulen) 12940a708f8fSGustavo F. Padovan { 129547d1ec61SGustavo F. Padovan struct sock *sk = chan->sk; 12968c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 12970a708f8fSGustavo F. Padovan struct sk_buff *skb; 12980a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE + 2; 12990a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 13000a708f8fSGustavo F. Padovan 13010a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 13020a708f8fSGustavo F. Padovan 13030a708f8fSGustavo F. Padovan if (!conn) 13040a708f8fSGustavo F. Padovan return ERR_PTR(-ENOTCONN); 13050a708f8fSGustavo F. Padovan 13060a708f8fSGustavo F. Padovan if (sdulen) 13070a708f8fSGustavo F. Padovan hlen += 2; 13080a708f8fSGustavo F. Padovan 130947d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 13100a708f8fSGustavo F. Padovan hlen += 2; 13110a708f8fSGustavo F. Padovan 13120a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 13130a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 13140a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 13150a708f8fSGustavo F. Padovan if (!skb) 13160a708f8fSGustavo F. Padovan return ERR_PTR(err); 13170a708f8fSGustavo F. Padovan 13180a708f8fSGustavo F. Padovan /* Create L2CAP header */ 13190a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 1320fe4128e0SGustavo F. Padovan lh->cid = cpu_to_le16(chan->dcid); 13210a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 13220a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb_put(skb, 2)); 13230a708f8fSGustavo F. Padovan if (sdulen) 13240a708f8fSGustavo F. Padovan put_unaligned_le16(sdulen, skb_put(skb, 2)); 13250a708f8fSGustavo F. Padovan 13260a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 13270a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 13280a708f8fSGustavo F. Padovan kfree_skb(skb); 13290a708f8fSGustavo F. Padovan return ERR_PTR(err); 13300a708f8fSGustavo F. Padovan } 13310a708f8fSGustavo F. Padovan 133247d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 13330a708f8fSGustavo F. Padovan put_unaligned_le16(0, skb_put(skb, 2)); 13340a708f8fSGustavo F. Padovan 13350a708f8fSGustavo F. Padovan bt_cb(skb)->retries = 0; 13360a708f8fSGustavo F. Padovan return skb; 13370a708f8fSGustavo F. Padovan } 13380a708f8fSGustavo F. Padovan 13392c03a7a4SGustavo F. Padovan int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 13400a708f8fSGustavo F. Padovan { 13410a708f8fSGustavo F. Padovan struct sk_buff *skb; 13420a708f8fSGustavo F. Padovan struct sk_buff_head sar_queue; 13430a708f8fSGustavo F. Padovan u16 control; 13440a708f8fSGustavo F. Padovan size_t size = 0; 13450a708f8fSGustavo F. Padovan 13460a708f8fSGustavo F. Padovan skb_queue_head_init(&sar_queue); 13470a708f8fSGustavo F. Padovan control = L2CAP_SDU_START; 134847d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len); 13490a708f8fSGustavo F. Padovan if (IS_ERR(skb)) 13500a708f8fSGustavo F. Padovan return PTR_ERR(skb); 13510a708f8fSGustavo F. Padovan 13520a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 13532c03a7a4SGustavo F. Padovan len -= chan->remote_mps; 13542c03a7a4SGustavo F. Padovan size += chan->remote_mps; 13550a708f8fSGustavo F. Padovan 13560a708f8fSGustavo F. Padovan while (len > 0) { 13570a708f8fSGustavo F. Padovan size_t buflen; 13580a708f8fSGustavo F. Padovan 13592c03a7a4SGustavo F. Padovan if (len > chan->remote_mps) { 13600a708f8fSGustavo F. Padovan control = L2CAP_SDU_CONTINUE; 13612c03a7a4SGustavo F. Padovan buflen = chan->remote_mps; 13620a708f8fSGustavo F. Padovan } else { 13630a708f8fSGustavo F. Padovan control = L2CAP_SDU_END; 13640a708f8fSGustavo F. Padovan buflen = len; 13650a708f8fSGustavo F. Padovan } 13660a708f8fSGustavo F. Padovan 136747d1ec61SGustavo F. Padovan skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0); 13680a708f8fSGustavo F. Padovan if (IS_ERR(skb)) { 13690a708f8fSGustavo F. Padovan skb_queue_purge(&sar_queue); 13700a708f8fSGustavo F. Padovan return PTR_ERR(skb); 13710a708f8fSGustavo F. Padovan } 13720a708f8fSGustavo F. Padovan 13730a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 13740a708f8fSGustavo F. Padovan len -= buflen; 13750a708f8fSGustavo F. Padovan size += buflen; 13760a708f8fSGustavo F. Padovan } 137758d35f87SGustavo F. Padovan skb_queue_splice_tail(&sar_queue, &chan->tx_q); 137858d35f87SGustavo F. Padovan if (chan->tx_send_head == NULL) 137958d35f87SGustavo F. Padovan chan->tx_send_head = sar_queue.next; 13800a708f8fSGustavo F. Padovan 13810a708f8fSGustavo F. Padovan return size; 13820a708f8fSGustavo F. Padovan } 13830a708f8fSGustavo F. Padovan 13840a708f8fSGustavo F. Padovan static void l2cap_chan_ready(struct sock *sk) 13850a708f8fSGustavo F. Padovan { 13860a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 1387b4450035SGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 13880a708f8fSGustavo F. Padovan 13890a708f8fSGustavo F. Padovan BT_DBG("sk %p, parent %p", sk, parent); 13900a708f8fSGustavo F. Padovan 1391b4450035SGustavo F. Padovan chan->conf_state = 0; 13920a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 13930a708f8fSGustavo F. Padovan 13940a708f8fSGustavo F. Padovan if (!parent) { 13950a708f8fSGustavo F. Padovan /* Outgoing channel. 13960a708f8fSGustavo F. Padovan * Wake up socket sleeping on connect. 13970a708f8fSGustavo F. Padovan */ 13980a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 13990a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 14000a708f8fSGustavo F. Padovan } else { 14010a708f8fSGustavo F. Padovan /* Incoming channel. 14020a708f8fSGustavo F. Padovan * Wake up socket sleeping on accept. 14030a708f8fSGustavo F. Padovan */ 14040a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 14050a708f8fSGustavo F. Padovan } 14060a708f8fSGustavo F. Padovan } 14070a708f8fSGustavo F. Padovan 14080a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */ 14090a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) 14100a708f8fSGustavo F. Padovan { 14110a708f8fSGustavo F. Padovan struct sk_buff *nskb; 141248454079SGustavo F. Padovan struct l2cap_chan *chan; 14130a708f8fSGustavo F. Padovan 14140a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 14150a708f8fSGustavo F. Padovan 1416baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 1417baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 141848454079SGustavo F. Padovan struct sock *sk = chan->sk; 14190a708f8fSGustavo F. Padovan if (sk->sk_type != SOCK_RAW) 14200a708f8fSGustavo F. Padovan continue; 14210a708f8fSGustavo F. Padovan 14220a708f8fSGustavo F. Padovan /* Don't send frame to the socket it came from */ 14230a708f8fSGustavo F. Padovan if (skb->sk == sk) 14240a708f8fSGustavo F. Padovan continue; 14250a708f8fSGustavo F. Padovan nskb = skb_clone(skb, GFP_ATOMIC); 14260a708f8fSGustavo F. Padovan if (!nskb) 14270a708f8fSGustavo F. Padovan continue; 14280a708f8fSGustavo F. Padovan 14290a708f8fSGustavo F. Padovan if (sock_queue_rcv_skb(sk, nskb)) 14300a708f8fSGustavo F. Padovan kfree_skb(nskb); 14310a708f8fSGustavo F. Padovan } 1432baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 14330a708f8fSGustavo F. Padovan } 14340a708f8fSGustavo F. Padovan 14350a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */ 14360a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 14370a708f8fSGustavo F. Padovan u8 code, u8 ident, u16 dlen, void *data) 14380a708f8fSGustavo F. Padovan { 14390a708f8fSGustavo F. Padovan struct sk_buff *skb, **frag; 14400a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr *cmd; 14410a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 14420a708f8fSGustavo F. Padovan int len, count; 14430a708f8fSGustavo F. Padovan 14440a708f8fSGustavo F. Padovan BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", 14450a708f8fSGustavo F. Padovan conn, code, ident, dlen); 14460a708f8fSGustavo F. Padovan 14470a708f8fSGustavo F. Padovan len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; 14480a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 14490a708f8fSGustavo F. Padovan 14500a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 14510a708f8fSGustavo F. Padovan if (!skb) 14520a708f8fSGustavo F. Padovan return NULL; 14530a708f8fSGustavo F. Padovan 14540a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 14550a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); 14563300d9a9SClaudio Takahasi 14573300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 14583300d9a9SClaudio Takahasi lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); 14593300d9a9SClaudio Takahasi else 14600a708f8fSGustavo F. Padovan lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); 14610a708f8fSGustavo F. Padovan 14620a708f8fSGustavo F. Padovan cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); 14630a708f8fSGustavo F. Padovan cmd->code = code; 14640a708f8fSGustavo F. Padovan cmd->ident = ident; 14650a708f8fSGustavo F. Padovan cmd->len = cpu_to_le16(dlen); 14660a708f8fSGustavo F. Padovan 14670a708f8fSGustavo F. Padovan if (dlen) { 14680a708f8fSGustavo F. Padovan count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; 14690a708f8fSGustavo F. Padovan memcpy(skb_put(skb, count), data, count); 14700a708f8fSGustavo F. Padovan data += count; 14710a708f8fSGustavo F. Padovan } 14720a708f8fSGustavo F. Padovan 14730a708f8fSGustavo F. Padovan len -= skb->len; 14740a708f8fSGustavo F. Padovan 14750a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 14760a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 14770a708f8fSGustavo F. Padovan while (len) { 14780a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 14790a708f8fSGustavo F. Padovan 14800a708f8fSGustavo F. Padovan *frag = bt_skb_alloc(count, GFP_ATOMIC); 14810a708f8fSGustavo F. Padovan if (!*frag) 14820a708f8fSGustavo F. Padovan goto fail; 14830a708f8fSGustavo F. Padovan 14840a708f8fSGustavo F. Padovan memcpy(skb_put(*frag, count), data, count); 14850a708f8fSGustavo F. Padovan 14860a708f8fSGustavo F. Padovan len -= count; 14870a708f8fSGustavo F. Padovan data += count; 14880a708f8fSGustavo F. Padovan 14890a708f8fSGustavo F. Padovan frag = &(*frag)->next; 14900a708f8fSGustavo F. Padovan } 14910a708f8fSGustavo F. Padovan 14920a708f8fSGustavo F. Padovan return skb; 14930a708f8fSGustavo F. Padovan 14940a708f8fSGustavo F. Padovan fail: 14950a708f8fSGustavo F. Padovan kfree_skb(skb); 14960a708f8fSGustavo F. Padovan return NULL; 14970a708f8fSGustavo F. Padovan } 14980a708f8fSGustavo F. Padovan 14990a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) 15000a708f8fSGustavo F. Padovan { 15010a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 15020a708f8fSGustavo F. Padovan int len; 15030a708f8fSGustavo F. Padovan 15040a708f8fSGustavo F. Padovan len = L2CAP_CONF_OPT_SIZE + opt->len; 15050a708f8fSGustavo F. Padovan *ptr += len; 15060a708f8fSGustavo F. Padovan 15070a708f8fSGustavo F. Padovan *type = opt->type; 15080a708f8fSGustavo F. Padovan *olen = opt->len; 15090a708f8fSGustavo F. Padovan 15100a708f8fSGustavo F. Padovan switch (opt->len) { 15110a708f8fSGustavo F. Padovan case 1: 15120a708f8fSGustavo F. Padovan *val = *((u8 *) opt->val); 15130a708f8fSGustavo F. Padovan break; 15140a708f8fSGustavo F. Padovan 15150a708f8fSGustavo F. Padovan case 2: 15160a708f8fSGustavo F. Padovan *val = get_unaligned_le16(opt->val); 15170a708f8fSGustavo F. Padovan break; 15180a708f8fSGustavo F. Padovan 15190a708f8fSGustavo F. Padovan case 4: 15200a708f8fSGustavo F. Padovan *val = get_unaligned_le32(opt->val); 15210a708f8fSGustavo F. Padovan break; 15220a708f8fSGustavo F. Padovan 15230a708f8fSGustavo F. Padovan default: 15240a708f8fSGustavo F. Padovan *val = (unsigned long) opt->val; 15250a708f8fSGustavo F. Padovan break; 15260a708f8fSGustavo F. Padovan } 15270a708f8fSGustavo F. Padovan 15280a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); 15290a708f8fSGustavo F. Padovan return len; 15300a708f8fSGustavo F. Padovan } 15310a708f8fSGustavo F. Padovan 15320a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) 15330a708f8fSGustavo F. Padovan { 15340a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 15350a708f8fSGustavo F. Padovan 15360a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); 15370a708f8fSGustavo F. Padovan 15380a708f8fSGustavo F. Padovan opt->type = type; 15390a708f8fSGustavo F. Padovan opt->len = len; 15400a708f8fSGustavo F. Padovan 15410a708f8fSGustavo F. Padovan switch (len) { 15420a708f8fSGustavo F. Padovan case 1: 15430a708f8fSGustavo F. Padovan *((u8 *) opt->val) = val; 15440a708f8fSGustavo F. Padovan break; 15450a708f8fSGustavo F. Padovan 15460a708f8fSGustavo F. Padovan case 2: 15470a708f8fSGustavo F. Padovan put_unaligned_le16(val, opt->val); 15480a708f8fSGustavo F. Padovan break; 15490a708f8fSGustavo F. Padovan 15500a708f8fSGustavo F. Padovan case 4: 15510a708f8fSGustavo F. Padovan put_unaligned_le32(val, opt->val); 15520a708f8fSGustavo F. Padovan break; 15530a708f8fSGustavo F. Padovan 15540a708f8fSGustavo F. Padovan default: 15550a708f8fSGustavo F. Padovan memcpy(opt->val, (void *) val, len); 15560a708f8fSGustavo F. Padovan break; 15570a708f8fSGustavo F. Padovan } 15580a708f8fSGustavo F. Padovan 15590a708f8fSGustavo F. Padovan *ptr += L2CAP_CONF_OPT_SIZE + len; 15600a708f8fSGustavo F. Padovan } 15610a708f8fSGustavo F. Padovan 15620a708f8fSGustavo F. Padovan static void l2cap_ack_timeout(unsigned long arg) 15630a708f8fSGustavo F. Padovan { 1564525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 15650a708f8fSGustavo F. Padovan 1566525cd185SGustavo F. Padovan bh_lock_sock(chan->sk); 1567525cd185SGustavo F. Padovan l2cap_send_ack(chan); 1568525cd185SGustavo F. Padovan bh_unlock_sock(chan->sk); 15690a708f8fSGustavo F. Padovan } 15700a708f8fSGustavo F. Padovan 1571525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan) 15720a708f8fSGustavo F. Padovan { 1573525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 1574525cd185SGustavo F. Padovan 157542e5c802SGustavo F. Padovan chan->expected_ack_seq = 0; 15766a026610SGustavo F. Padovan chan->unacked_frames = 0; 157742e5c802SGustavo F. Padovan chan->buffer_seq = 0; 15786a026610SGustavo F. Padovan chan->num_acked = 0; 15796a026610SGustavo F. Padovan chan->frames_sent = 0; 15800a708f8fSGustavo F. Padovan 1581e92c8e70SGustavo F. Padovan setup_timer(&chan->retrans_timer, l2cap_retrans_timeout, 1582e92c8e70SGustavo F. Padovan (unsigned long) chan); 1583e92c8e70SGustavo F. Padovan setup_timer(&chan->monitor_timer, l2cap_monitor_timeout, 1584e92c8e70SGustavo F. Padovan (unsigned long) chan); 1585e92c8e70SGustavo F. Padovan setup_timer(&chan->ack_timer, l2cap_ack_timeout, (unsigned long) chan); 15860a708f8fSGustavo F. Padovan 1587f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->srej_q); 1588f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->busy_q); 15890a708f8fSGustavo F. Padovan 159039d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 159139d5a3eeSGustavo F. Padovan 1592311bb895SGustavo F. Padovan INIT_WORK(&chan->busy_work, l2cap_busy_work); 15930a708f8fSGustavo F. Padovan 15940a708f8fSGustavo F. Padovan sk->sk_backlog_rcv = l2cap_ertm_data_rcv; 15950a708f8fSGustavo F. Padovan } 15960a708f8fSGustavo F. Padovan 15970a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) 15980a708f8fSGustavo F. Padovan { 15990a708f8fSGustavo F. Padovan switch (mode) { 16000a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 16010a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 16020a708f8fSGustavo F. Padovan if (l2cap_mode_supported(mode, remote_feat_mask)) 16030a708f8fSGustavo F. Padovan return mode; 16040a708f8fSGustavo F. Padovan /* fall through */ 16050a708f8fSGustavo F. Padovan default: 16060a708f8fSGustavo F. Padovan return L2CAP_MODE_BASIC; 16070a708f8fSGustavo F. Padovan } 16080a708f8fSGustavo F. Padovan } 16090a708f8fSGustavo F. Padovan 1610710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) 16110a708f8fSGustavo F. Padovan { 16120a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 16130c1bc5c6SGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = chan->mode }; 16140a708f8fSGustavo F. Padovan void *ptr = req->data; 16150a708f8fSGustavo F. Padovan 161649208c9cSGustavo F. Padovan BT_DBG("chan %p", chan); 16170a708f8fSGustavo F. Padovan 161873ffa904SGustavo F. Padovan if (chan->num_conf_req || chan->num_conf_rsp) 16190a708f8fSGustavo F. Padovan goto done; 16200a708f8fSGustavo F. Padovan 16210c1bc5c6SGustavo F. Padovan switch (chan->mode) { 16220a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 16230a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 1624b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_STATE2_DEVICE) 16250a708f8fSGustavo F. Padovan break; 16260a708f8fSGustavo F. Padovan 16270a708f8fSGustavo F. Padovan /* fall through */ 16280a708f8fSGustavo F. Padovan default: 16298c1d787bSGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); 16300a708f8fSGustavo F. Padovan break; 16310a708f8fSGustavo F. Padovan } 16320a708f8fSGustavo F. Padovan 16330a708f8fSGustavo F. Padovan done: 16340c1bc5c6SGustavo F. Padovan if (chan->imtu != L2CAP_DEFAULT_MTU) 16350c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 16360a708f8fSGustavo F. Padovan 16370c1bc5c6SGustavo F. Padovan switch (chan->mode) { 16380a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 16398c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && 16408c1d787bSGustavo F. Padovan !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) 16410a708f8fSGustavo F. Padovan break; 16420a708f8fSGustavo F. Padovan 16430a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_BASIC; 16440a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 16450a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 16460a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 16470a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 16480a708f8fSGustavo F. Padovan rfc.max_pdu_size = 0; 16490a708f8fSGustavo F. Padovan 16500a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 16510a708f8fSGustavo F. Padovan (unsigned long) &rfc); 16520a708f8fSGustavo F. Padovan break; 16530a708f8fSGustavo F. Padovan 16540a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 16550a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_ERTM; 165647d1ec61SGustavo F. Padovan rfc.txwin_size = chan->tx_win; 165747d1ec61SGustavo F. Padovan rfc.max_transmit = chan->max_tx; 16580a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 16590a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 16600a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); 16618c1d787bSGustavo F. Padovan if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10) 16628c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 16630a708f8fSGustavo F. Padovan 16640a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 16650a708f8fSGustavo F. Padovan (unsigned long) &rfc); 16660a708f8fSGustavo F. Padovan 16678c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 16680a708f8fSGustavo F. Padovan break; 16690a708f8fSGustavo F. Padovan 167047d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 1671b4450035SGustavo F. Padovan chan->conf_state & L2CAP_CONF_NO_FCS_RECV) { 167247d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 167347d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 16740a708f8fSGustavo F. Padovan } 16750a708f8fSGustavo F. Padovan break; 16760a708f8fSGustavo F. Padovan 16770a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 16780a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_STREAMING; 16790a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 16800a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 16810a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 16820a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 16830a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); 16848c1d787bSGustavo F. Padovan if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10) 16858c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 16860a708f8fSGustavo F. Padovan 16870a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 16880a708f8fSGustavo F. Padovan (unsigned long) &rfc); 16890a708f8fSGustavo F. Padovan 16908c1d787bSGustavo F. Padovan if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) 16910a708f8fSGustavo F. Padovan break; 16920a708f8fSGustavo F. Padovan 169347d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_NONE || 1694b4450035SGustavo F. Padovan chan->conf_state & L2CAP_CONF_NO_FCS_RECV) { 169547d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 169647d1ec61SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); 16970a708f8fSGustavo F. Padovan } 16980a708f8fSGustavo F. Padovan break; 16990a708f8fSGustavo F. Padovan } 17000a708f8fSGustavo F. Padovan 1701fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 17020a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0); 17030a708f8fSGustavo F. Padovan 17040a708f8fSGustavo F. Padovan return ptr - data; 17050a708f8fSGustavo F. Padovan } 17060a708f8fSGustavo F. Padovan 170773ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) 17080a708f8fSGustavo F. Padovan { 17090a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 17100a708f8fSGustavo F. Padovan void *ptr = rsp->data; 171173ffa904SGustavo F. Padovan void *req = chan->conf_req; 171273ffa904SGustavo F. Padovan int len = chan->conf_len; 17130a708f8fSGustavo F. Padovan int type, hint, olen; 17140a708f8fSGustavo F. Padovan unsigned long val; 17150a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 17160a708f8fSGustavo F. Padovan u16 mtu = L2CAP_DEFAULT_MTU; 17170a708f8fSGustavo F. Padovan u16 result = L2CAP_CONF_SUCCESS; 17180a708f8fSGustavo F. Padovan 171973ffa904SGustavo F. Padovan BT_DBG("chan %p", chan); 17200a708f8fSGustavo F. Padovan 17210a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 17220a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&req, &type, &olen, &val); 17230a708f8fSGustavo F. Padovan 17240a708f8fSGustavo F. Padovan hint = type & L2CAP_CONF_HINT; 17250a708f8fSGustavo F. Padovan type &= L2CAP_CONF_MASK; 17260a708f8fSGustavo F. Padovan 17270a708f8fSGustavo F. Padovan switch (type) { 17280a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 17290a708f8fSGustavo F. Padovan mtu = val; 17300a708f8fSGustavo F. Padovan break; 17310a708f8fSGustavo F. Padovan 17320a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 17330c1bc5c6SGustavo F. Padovan chan->flush_to = val; 17340a708f8fSGustavo F. Padovan break; 17350a708f8fSGustavo F. Padovan 17360a708f8fSGustavo F. Padovan case L2CAP_CONF_QOS: 17370a708f8fSGustavo F. Padovan break; 17380a708f8fSGustavo F. Padovan 17390a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 17400a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 17410a708f8fSGustavo F. Padovan memcpy(&rfc, (void *) val, olen); 17420a708f8fSGustavo F. Padovan break; 17430a708f8fSGustavo F. Padovan 17440a708f8fSGustavo F. Padovan case L2CAP_CONF_FCS: 17450a708f8fSGustavo F. Padovan if (val == L2CAP_FCS_NONE) 1746b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_NO_FCS_RECV; 17470a708f8fSGustavo F. Padovan 17480a708f8fSGustavo F. Padovan break; 17490a708f8fSGustavo F. Padovan 17500a708f8fSGustavo F. Padovan default: 17510a708f8fSGustavo F. Padovan if (hint) 17520a708f8fSGustavo F. Padovan break; 17530a708f8fSGustavo F. Padovan 17540a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNKNOWN; 17550a708f8fSGustavo F. Padovan *((u8 *) ptr++) = type; 17560a708f8fSGustavo F. Padovan break; 17570a708f8fSGustavo F. Padovan } 17580a708f8fSGustavo F. Padovan } 17590a708f8fSGustavo F. Padovan 176073ffa904SGustavo F. Padovan if (chan->num_conf_rsp || chan->num_conf_req > 1) 17610a708f8fSGustavo F. Padovan goto done; 17620a708f8fSGustavo F. Padovan 17630c1bc5c6SGustavo F. Padovan switch (chan->mode) { 17640a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 17650a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 1766b4450035SGustavo F. Padovan if (!(chan->conf_state & L2CAP_CONF_STATE2_DEVICE)) { 17670c1bc5c6SGustavo F. Padovan chan->mode = l2cap_select_mode(rfc.mode, 17688c1d787bSGustavo F. Padovan chan->conn->feat_mask); 17690a708f8fSGustavo F. Padovan break; 17700a708f8fSGustavo F. Padovan } 17710a708f8fSGustavo F. Padovan 17720c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) 17730a708f8fSGustavo F. Padovan return -ECONNREFUSED; 17740a708f8fSGustavo F. Padovan 17750a708f8fSGustavo F. Padovan break; 17760a708f8fSGustavo F. Padovan } 17770a708f8fSGustavo F. Padovan 17780a708f8fSGustavo F. Padovan done: 17790c1bc5c6SGustavo F. Padovan if (chan->mode != rfc.mode) { 17800a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 17810c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 17820a708f8fSGustavo F. Padovan 178373ffa904SGustavo F. Padovan if (chan->num_conf_rsp == 1) 17840a708f8fSGustavo F. Padovan return -ECONNREFUSED; 17850a708f8fSGustavo F. Padovan 17860a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 17870a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 17880a708f8fSGustavo F. Padovan } 17890a708f8fSGustavo F. Padovan 17900a708f8fSGustavo F. Padovan 17910a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) { 17920a708f8fSGustavo F. Padovan /* Configure output options and let the other side know 17930a708f8fSGustavo F. Padovan * which ones we don't like. */ 17940a708f8fSGustavo F. Padovan 17950a708f8fSGustavo F. Padovan if (mtu < L2CAP_DEFAULT_MIN_MTU) 17960a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 17970a708f8fSGustavo F. Padovan else { 17980c1bc5c6SGustavo F. Padovan chan->omtu = mtu; 1799b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MTU_DONE; 18000a708f8fSGustavo F. Padovan } 18010c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); 18020a708f8fSGustavo F. Padovan 18030a708f8fSGustavo F. Padovan switch (rfc.mode) { 18040a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 180547d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 1806b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MODE_DONE; 18070a708f8fSGustavo F. Padovan break; 18080a708f8fSGustavo F. Padovan 18090a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 18102c03a7a4SGustavo F. Padovan chan->remote_tx_win = rfc.txwin_size; 18112c03a7a4SGustavo F. Padovan chan->remote_max_tx = rfc.max_transmit; 18120a708f8fSGustavo F. Padovan 18138c1d787bSGustavo F. Padovan if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10) 18148c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 18150a708f8fSGustavo F. Padovan 18162c03a7a4SGustavo F. Padovan chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); 18170a708f8fSGustavo F. Padovan 18180a708f8fSGustavo F. Padovan rfc.retrans_timeout = 18190a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO); 18200a708f8fSGustavo F. Padovan rfc.monitor_timeout = 18210a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO); 18220a708f8fSGustavo F. Padovan 1823b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MODE_DONE; 18240a708f8fSGustavo F. Padovan 18250a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 18260a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 18270a708f8fSGustavo F. Padovan 18280a708f8fSGustavo F. Padovan break; 18290a708f8fSGustavo F. Padovan 18300a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 18318c1d787bSGustavo F. Padovan if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10) 18328c1d787bSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); 18330a708f8fSGustavo F. Padovan 18342c03a7a4SGustavo F. Padovan chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); 18350a708f8fSGustavo F. Padovan 1836b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_MODE_DONE; 18370a708f8fSGustavo F. Padovan 18380a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 18390a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 18400a708f8fSGustavo F. Padovan 18410a708f8fSGustavo F. Padovan break; 18420a708f8fSGustavo F. Padovan 18430a708f8fSGustavo F. Padovan default: 18440a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 18450a708f8fSGustavo F. Padovan 18460a708f8fSGustavo F. Padovan memset(&rfc, 0, sizeof(rfc)); 18470c1bc5c6SGustavo F. Padovan rfc.mode = chan->mode; 18480a708f8fSGustavo F. Padovan } 18490a708f8fSGustavo F. Padovan 18500a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) 1851b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_OUTPUT_DONE; 18520a708f8fSGustavo F. Padovan } 1853fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 18540a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 18550a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(0x0000); 18560a708f8fSGustavo F. Padovan 18570a708f8fSGustavo F. Padovan return ptr - data; 18580a708f8fSGustavo F. Padovan } 18590a708f8fSGustavo F. Padovan 1860b4450035SGustavo F. Padovan static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result) 18610a708f8fSGustavo F. Padovan { 18620a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 18630a708f8fSGustavo F. Padovan void *ptr = req->data; 18640a708f8fSGustavo F. Padovan int type, olen; 18650a708f8fSGustavo F. Padovan unsigned long val; 18660a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 18670a708f8fSGustavo F. Padovan 1868fe4128e0SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data); 18690a708f8fSGustavo F. Padovan 18700a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 18710a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 18720a708f8fSGustavo F. Padovan 18730a708f8fSGustavo F. Padovan switch (type) { 18740a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 18750a708f8fSGustavo F. Padovan if (val < L2CAP_DEFAULT_MIN_MTU) { 18760a708f8fSGustavo F. Padovan *result = L2CAP_CONF_UNACCEPT; 18770c1bc5c6SGustavo F. Padovan chan->imtu = L2CAP_DEFAULT_MIN_MTU; 18780a708f8fSGustavo F. Padovan } else 18790c1bc5c6SGustavo F. Padovan chan->imtu = val; 18800c1bc5c6SGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); 18810a708f8fSGustavo F. Padovan break; 18820a708f8fSGustavo F. Padovan 18830a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 18840c1bc5c6SGustavo F. Padovan chan->flush_to = val; 18850a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 18860c1bc5c6SGustavo F. Padovan 2, chan->flush_to); 18870a708f8fSGustavo F. Padovan break; 18880a708f8fSGustavo F. Padovan 18890a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 18900a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 18910a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 18920a708f8fSGustavo F. Padovan 1893b4450035SGustavo F. Padovan if ((chan->conf_state & L2CAP_CONF_STATE2_DEVICE) && 18940c1bc5c6SGustavo F. Padovan rfc.mode != chan->mode) 18950a708f8fSGustavo F. Padovan return -ECONNREFUSED; 18960a708f8fSGustavo F. Padovan 189747d1ec61SGustavo F. Padovan chan->fcs = 0; 18980a708f8fSGustavo F. Padovan 18990a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 19000a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 19010a708f8fSGustavo F. Padovan break; 19020a708f8fSGustavo F. Padovan } 19030a708f8fSGustavo F. Padovan } 19040a708f8fSGustavo F. Padovan 19050c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) 19060a708f8fSGustavo F. Padovan return -ECONNREFUSED; 19070a708f8fSGustavo F. Padovan 19080c1bc5c6SGustavo F. Padovan chan->mode = rfc.mode; 19090a708f8fSGustavo F. Padovan 19100a708f8fSGustavo F. Padovan if (*result == L2CAP_CONF_SUCCESS) { 19110a708f8fSGustavo F. Padovan switch (rfc.mode) { 19120a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 191347d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 191447d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 191547d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 19160a708f8fSGustavo F. Padovan break; 19170a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 191847d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 19190a708f8fSGustavo F. Padovan } 19200a708f8fSGustavo F. Padovan } 19210a708f8fSGustavo F. Padovan 1922fe4128e0SGustavo F. Padovan req->dcid = cpu_to_le16(chan->dcid); 19230a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0x0000); 19240a708f8fSGustavo F. Padovan 19250a708f8fSGustavo F. Padovan return ptr - data; 19260a708f8fSGustavo F. Padovan } 19270a708f8fSGustavo F. Padovan 1928fe4128e0SGustavo F. Padovan static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags) 19290a708f8fSGustavo F. Padovan { 19300a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 19310a708f8fSGustavo F. Padovan void *ptr = rsp->data; 19320a708f8fSGustavo F. Padovan 1933fe4128e0SGustavo F. Padovan BT_DBG("chan %p", chan); 19340a708f8fSGustavo F. Padovan 1935fe4128e0SGustavo F. Padovan rsp->scid = cpu_to_le16(chan->dcid); 19360a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 19370a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(flags); 19380a708f8fSGustavo F. Padovan 19390a708f8fSGustavo F. Padovan return ptr - data; 19400a708f8fSGustavo F. Padovan } 19410a708f8fSGustavo F. Padovan 19428c1d787bSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) 1943710f9b0aSGustavo F. Padovan { 1944710f9b0aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 19458c1d787bSGustavo F. Padovan struct l2cap_conn *conn = chan->conn; 1946710f9b0aSGustavo F. Padovan u8 buf[128]; 1947710f9b0aSGustavo F. Padovan 1948fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 1949fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 1950710f9b0aSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 1951710f9b0aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 1952710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 1953710f9b0aSGustavo F. Padovan L2CAP_CONN_RSP, sizeof(rsp), &rsp); 1954710f9b0aSGustavo F. Padovan 1955b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_REQ_SENT) 1956710f9b0aSGustavo F. Padovan return; 1957710f9b0aSGustavo F. Padovan 1958b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 1959710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 1960710f9b0aSGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 1961710f9b0aSGustavo F. Padovan chan->num_conf_req++; 1962710f9b0aSGustavo F. Padovan } 1963710f9b0aSGustavo F. Padovan 196447d1ec61SGustavo F. Padovan static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len) 19650a708f8fSGustavo F. Padovan { 19660a708f8fSGustavo F. Padovan int type, olen; 19670a708f8fSGustavo F. Padovan unsigned long val; 19680a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 19690a708f8fSGustavo F. Padovan 197047d1ec61SGustavo F. Padovan BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); 19710a708f8fSGustavo F. Padovan 19720c1bc5c6SGustavo F. Padovan if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) 19730a708f8fSGustavo F. Padovan return; 19740a708f8fSGustavo F. Padovan 19750a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 19760a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 19770a708f8fSGustavo F. Padovan 19780a708f8fSGustavo F. Padovan switch (type) { 19790a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 19800a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 19810a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 19820a708f8fSGustavo F. Padovan goto done; 19830a708f8fSGustavo F. Padovan } 19840a708f8fSGustavo F. Padovan } 19850a708f8fSGustavo F. Padovan 19860a708f8fSGustavo F. Padovan done: 19870a708f8fSGustavo F. Padovan switch (rfc.mode) { 19880a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 198947d1ec61SGustavo F. Padovan chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 199047d1ec61SGustavo F. Padovan chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 199147d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 19920a708f8fSGustavo F. Padovan break; 19930a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 199447d1ec61SGustavo F. Padovan chan->mps = le16_to_cpu(rfc.max_pdu_size); 19950a708f8fSGustavo F. Padovan } 19960a708f8fSGustavo F. Padovan } 19970a708f8fSGustavo F. Padovan 19980a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 19990a708f8fSGustavo F. Padovan { 20000a708f8fSGustavo F. Padovan struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data; 20010a708f8fSGustavo F. Padovan 20020a708f8fSGustavo F. Padovan if (rej->reason != 0x0000) 20030a708f8fSGustavo F. Padovan return 0; 20040a708f8fSGustavo F. Padovan 20050a708f8fSGustavo F. Padovan if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && 20060a708f8fSGustavo F. Padovan cmd->ident == conn->info_ident) { 20070a708f8fSGustavo F. Padovan del_timer(&conn->info_timer); 20080a708f8fSGustavo F. Padovan 20090a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 20100a708f8fSGustavo F. Padovan conn->info_ident = 0; 20110a708f8fSGustavo F. Padovan 20120a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 20130a708f8fSGustavo F. Padovan } 20140a708f8fSGustavo F. Padovan 20150a708f8fSGustavo F. Padovan return 0; 20160a708f8fSGustavo F. Padovan } 20170a708f8fSGustavo F. Padovan 20180a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 20190a708f8fSGustavo F. Padovan { 20200a708f8fSGustavo F. Padovan struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; 20210a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 202273ffa904SGustavo F. Padovan struct l2cap_chan *chan = NULL; 20230a708f8fSGustavo F. Padovan struct sock *parent, *sk = NULL; 20240a708f8fSGustavo F. Padovan int result, status = L2CAP_CS_NO_INFO; 20250a708f8fSGustavo F. Padovan 20260a708f8fSGustavo F. Padovan u16 dcid = 0, scid = __le16_to_cpu(req->scid); 20270a708f8fSGustavo F. Padovan __le16 psm = req->psm; 20280a708f8fSGustavo F. Padovan 20290a708f8fSGustavo F. Padovan BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid); 20300a708f8fSGustavo F. Padovan 20310a708f8fSGustavo F. Padovan /* Check if we have socket listening on psm */ 20320a708f8fSGustavo F. Padovan parent = l2cap_get_sock_by_psm(BT_LISTEN, psm, conn->src); 20330a708f8fSGustavo F. Padovan if (!parent) { 20340a708f8fSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 20350a708f8fSGustavo F. Padovan goto sendresp; 20360a708f8fSGustavo F. Padovan } 20370a708f8fSGustavo F. Padovan 20380a708f8fSGustavo F. Padovan bh_lock_sock(parent); 20390a708f8fSGustavo F. Padovan 20400a708f8fSGustavo F. Padovan /* Check if the ACL is secure enough (if not SDP) */ 20410a708f8fSGustavo F. Padovan if (psm != cpu_to_le16(0x0001) && 20420a708f8fSGustavo F. Padovan !hci_conn_check_link_mode(conn->hcon)) { 20430a708f8fSGustavo F. Padovan conn->disc_reason = 0x05; 20440a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 20450a708f8fSGustavo F. Padovan goto response; 20460a708f8fSGustavo F. Padovan } 20470a708f8fSGustavo F. Padovan 20480a708f8fSGustavo F. Padovan result = L2CAP_CR_NO_MEM; 20490a708f8fSGustavo F. Padovan 20500a708f8fSGustavo F. Padovan /* Check for backlog size */ 20510a708f8fSGustavo F. Padovan if (sk_acceptq_is_full(parent)) { 20520a708f8fSGustavo F. Padovan BT_DBG("backlog full %d", parent->sk_ack_backlog); 20530a708f8fSGustavo F. Padovan goto response; 20540a708f8fSGustavo F. Padovan } 20550a708f8fSGustavo F. Padovan 20560a708f8fSGustavo F. Padovan sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC); 20570a708f8fSGustavo F. Padovan if (!sk) 20580a708f8fSGustavo F. Padovan goto response; 20590a708f8fSGustavo F. Padovan 206048454079SGustavo F. Padovan chan = l2cap_chan_alloc(sk); 206148454079SGustavo F. Padovan if (!chan) { 206248454079SGustavo F. Padovan l2cap_sock_kill(sk); 206348454079SGustavo F. Padovan goto response; 206448454079SGustavo F. Padovan } 206548454079SGustavo F. Padovan 20665d41ce1dSGustavo F. Padovan l2cap_pi(sk)->chan = chan; 20675d41ce1dSGustavo F. Padovan 2068baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 20690a708f8fSGustavo F. Padovan 20700a708f8fSGustavo F. Padovan /* Check if we already have channel with that dcid */ 2071baa7e1faSGustavo F. Padovan if (__l2cap_get_chan_by_dcid(conn, scid)) { 2072baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 20730a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 20740a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 20750a708f8fSGustavo F. Padovan goto response; 20760a708f8fSGustavo F. Padovan } 20770a708f8fSGustavo F. Padovan 20780a708f8fSGustavo F. Padovan hci_conn_hold(conn->hcon); 20790a708f8fSGustavo F. Padovan 20800a708f8fSGustavo F. Padovan l2cap_sock_init(sk, parent); 20810a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->src, conn->src); 20820a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->dst, conn->dst); 2083fe4128e0SGustavo F. Padovan chan->psm = psm; 2084fe4128e0SGustavo F. Padovan chan->dcid = scid; 20850a708f8fSGustavo F. Padovan 2086d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 2087d1010240SGustavo F. Padovan 208848454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 208948454079SGustavo F. Padovan 2090fe4128e0SGustavo F. Padovan dcid = chan->scid; 20910a708f8fSGustavo F. Padovan 20920a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, sk->sk_sndtimeo); 20930a708f8fSGustavo F. Padovan 2094fc7f8a7eSGustavo F. Padovan chan->ident = cmd->ident; 20950a708f8fSGustavo F. Padovan 20960a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { 20974343478fSGustavo F. Padovan if (l2cap_check_security(chan)) { 20980a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 20990a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT2; 21000a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 21010a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHOR_PEND; 21020a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 21030a708f8fSGustavo F. Padovan } else { 21040a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 21050a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 21060a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 21070a708f8fSGustavo F. Padovan } 21080a708f8fSGustavo F. Padovan } else { 21090a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT2; 21100a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 21110a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHEN_PEND; 21120a708f8fSGustavo F. Padovan } 21130a708f8fSGustavo F. Padovan } else { 21140a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT2; 21150a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 21160a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 21170a708f8fSGustavo F. Padovan } 21180a708f8fSGustavo F. Padovan 2119baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 21200a708f8fSGustavo F. Padovan 21210a708f8fSGustavo F. Padovan response: 21220a708f8fSGustavo F. Padovan bh_unlock_sock(parent); 21230a708f8fSGustavo F. Padovan 21240a708f8fSGustavo F. Padovan sendresp: 21250a708f8fSGustavo F. Padovan rsp.scid = cpu_to_le16(scid); 21260a708f8fSGustavo F. Padovan rsp.dcid = cpu_to_le16(dcid); 21270a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 21280a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(status); 21290a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); 21300a708f8fSGustavo F. Padovan 21310a708f8fSGustavo F. Padovan if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { 21320a708f8fSGustavo F. Padovan struct l2cap_info_req info; 21330a708f8fSGustavo F. Padovan info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 21340a708f8fSGustavo F. Padovan 21350a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 21360a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 21370a708f8fSGustavo F. Padovan 21380a708f8fSGustavo F. Padovan mod_timer(&conn->info_timer, jiffies + 21390a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 21400a708f8fSGustavo F. Padovan 21410a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 21420a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(info), &info); 21430a708f8fSGustavo F. Padovan } 21440a708f8fSGustavo F. Padovan 2145b4450035SGustavo F. Padovan if (chan && !(chan->conf_state & L2CAP_CONF_REQ_SENT) && 21460a708f8fSGustavo F. Padovan result == L2CAP_CR_SUCCESS) { 21470a708f8fSGustavo F. Padovan u8 buf[128]; 2148b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 21490a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 215073ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 215173ffa904SGustavo F. Padovan chan->num_conf_req++; 21520a708f8fSGustavo F. Padovan } 21530a708f8fSGustavo F. Padovan 21540a708f8fSGustavo F. Padovan return 0; 21550a708f8fSGustavo F. Padovan } 21560a708f8fSGustavo F. Padovan 21570a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 21580a708f8fSGustavo F. Padovan { 21590a708f8fSGustavo F. Padovan struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; 21600a708f8fSGustavo F. Padovan u16 scid, dcid, result, status; 216148454079SGustavo F. Padovan struct l2cap_chan *chan; 21620a708f8fSGustavo F. Padovan struct sock *sk; 21630a708f8fSGustavo F. Padovan u8 req[128]; 21640a708f8fSGustavo F. Padovan 21650a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 21660a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 21670a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 21680a708f8fSGustavo F. Padovan status = __le16_to_cpu(rsp->status); 21690a708f8fSGustavo F. Padovan 21700a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status); 21710a708f8fSGustavo F. Padovan 21720a708f8fSGustavo F. Padovan if (scid) { 2173baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 217448454079SGustavo F. Padovan if (!chan) 21750a708f8fSGustavo F. Padovan return -EFAULT; 21760a708f8fSGustavo F. Padovan } else { 2177baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_ident(conn, cmd->ident); 217848454079SGustavo F. Padovan if (!chan) 21790a708f8fSGustavo F. Padovan return -EFAULT; 21800a708f8fSGustavo F. Padovan } 21810a708f8fSGustavo F. Padovan 218248454079SGustavo F. Padovan sk = chan->sk; 218348454079SGustavo F. Padovan 21840a708f8fSGustavo F. Padovan switch (result) { 21850a708f8fSGustavo F. Padovan case L2CAP_CR_SUCCESS: 21860a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 2187fc7f8a7eSGustavo F. Padovan chan->ident = 0; 2188fe4128e0SGustavo F. Padovan chan->dcid = dcid; 2189b4450035SGustavo F. Padovan chan->conf_state &= ~L2CAP_CONF_CONNECT_PEND; 21900a708f8fSGustavo F. Padovan 2191b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_REQ_SENT) 21920a708f8fSGustavo F. Padovan break; 21930a708f8fSGustavo F. Padovan 2194b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 21950a708f8fSGustavo F. Padovan 21960a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 219773ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, req), req); 219873ffa904SGustavo F. Padovan chan->num_conf_req++; 21990a708f8fSGustavo F. Padovan break; 22000a708f8fSGustavo F. Padovan 22010a708f8fSGustavo F. Padovan case L2CAP_CR_PEND: 2202b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 22030a708f8fSGustavo F. Padovan break; 22040a708f8fSGustavo F. Padovan 22050a708f8fSGustavo F. Padovan default: 22060a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 22070a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 22080a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 22090a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 22100a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, HZ / 5); 22110a708f8fSGustavo F. Padovan break; 22120a708f8fSGustavo F. Padovan } 22130a708f8fSGustavo F. Padovan 221448454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNREFUSED); 22150a708f8fSGustavo F. Padovan break; 22160a708f8fSGustavo F. Padovan } 22170a708f8fSGustavo F. Padovan 22180a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 22190a708f8fSGustavo F. Padovan return 0; 22200a708f8fSGustavo F. Padovan } 22210a708f8fSGustavo F. Padovan 222247d1ec61SGustavo F. Padovan static inline void set_default_fcs(struct l2cap_chan *chan) 22230a708f8fSGustavo F. Padovan { 222447d1ec61SGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(chan->sk); 222547d1ec61SGustavo F. Padovan 22260a708f8fSGustavo F. Padovan /* FCS is enabled only in ERTM or streaming mode, if one or both 22270a708f8fSGustavo F. Padovan * sides request it. 22280a708f8fSGustavo F. Padovan */ 22290c1bc5c6SGustavo F. Padovan if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) 223047d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_NONE; 2231b4450035SGustavo F. Padovan else if (!(pi->chan->conf_state & L2CAP_CONF_NO_FCS_RECV)) 223247d1ec61SGustavo F. Padovan chan->fcs = L2CAP_FCS_CRC16; 22330a708f8fSGustavo F. Padovan } 22340a708f8fSGustavo F. Padovan 22350a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 22360a708f8fSGustavo F. Padovan { 22370a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; 22380a708f8fSGustavo F. Padovan u16 dcid, flags; 22390a708f8fSGustavo F. Padovan u8 rsp[64]; 224048454079SGustavo F. Padovan struct l2cap_chan *chan; 22410a708f8fSGustavo F. Padovan struct sock *sk; 22420a708f8fSGustavo F. Padovan int len; 22430a708f8fSGustavo F. Padovan 22440a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 22450a708f8fSGustavo F. Padovan flags = __le16_to_cpu(req->flags); 22460a708f8fSGustavo F. Padovan 22470a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); 22480a708f8fSGustavo F. Padovan 2249baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 225048454079SGustavo F. Padovan if (!chan) 22510a708f8fSGustavo F. Padovan return -ENOENT; 22520a708f8fSGustavo F. Padovan 225348454079SGustavo F. Padovan sk = chan->sk; 225448454079SGustavo F. Padovan 22550a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONFIG) { 22560a708f8fSGustavo F. Padovan struct l2cap_cmd_rej rej; 22570a708f8fSGustavo F. Padovan 22580a708f8fSGustavo F. Padovan rej.reason = cpu_to_le16(0x0002); 22590a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, 22600a708f8fSGustavo F. Padovan sizeof(rej), &rej); 22610a708f8fSGustavo F. Padovan goto unlock; 22620a708f8fSGustavo F. Padovan } 22630a708f8fSGustavo F. Padovan 22640a708f8fSGustavo F. Padovan /* Reject if config buffer is too small. */ 22650a708f8fSGustavo F. Padovan len = cmd_len - sizeof(*req); 226673ffa904SGustavo F. Padovan if (chan->conf_len + len > sizeof(chan->conf_req)) { 22670a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2268fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 22690a708f8fSGustavo F. Padovan L2CAP_CONF_REJECT, flags), rsp); 22700a708f8fSGustavo F. Padovan goto unlock; 22710a708f8fSGustavo F. Padovan } 22720a708f8fSGustavo F. Padovan 22730a708f8fSGustavo F. Padovan /* Store config. */ 227473ffa904SGustavo F. Padovan memcpy(chan->conf_req + chan->conf_len, req->data, len); 227573ffa904SGustavo F. Padovan chan->conf_len += len; 22760a708f8fSGustavo F. Padovan 22770a708f8fSGustavo F. Padovan if (flags & 0x0001) { 22780a708f8fSGustavo F. Padovan /* Incomplete config. Send empty response. */ 22790a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 2280fe4128e0SGustavo F. Padovan l2cap_build_conf_rsp(chan, rsp, 22810a708f8fSGustavo F. Padovan L2CAP_CONF_SUCCESS, 0x0001), rsp); 22820a708f8fSGustavo F. Padovan goto unlock; 22830a708f8fSGustavo F. Padovan } 22840a708f8fSGustavo F. Padovan 22850a708f8fSGustavo F. Padovan /* Complete config. */ 228673ffa904SGustavo F. Padovan len = l2cap_parse_conf_req(chan, rsp); 22870a708f8fSGustavo F. Padovan if (len < 0) { 2288e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 22890a708f8fSGustavo F. Padovan goto unlock; 22900a708f8fSGustavo F. Padovan } 22910a708f8fSGustavo F. Padovan 22920a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); 229373ffa904SGustavo F. Padovan chan->num_conf_rsp++; 22940a708f8fSGustavo F. Padovan 22950a708f8fSGustavo F. Padovan /* Reset config buffer. */ 229673ffa904SGustavo F. Padovan chan->conf_len = 0; 22970a708f8fSGustavo F. Padovan 2298b4450035SGustavo F. Padovan if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE)) 22990a708f8fSGustavo F. Padovan goto unlock; 23000a708f8fSGustavo F. Padovan 2301b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_INPUT_DONE) { 230247d1ec61SGustavo F. Padovan set_default_fcs(chan); 23030a708f8fSGustavo F. Padovan 23040a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 23050a708f8fSGustavo F. Padovan 230642e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 230742e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 230858d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 23090c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2310525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 23110a708f8fSGustavo F. Padovan 23120a708f8fSGustavo F. Padovan l2cap_chan_ready(sk); 23130a708f8fSGustavo F. Padovan goto unlock; 23140a708f8fSGustavo F. Padovan } 23150a708f8fSGustavo F. Padovan 2316b4450035SGustavo F. Padovan if (!(chan->conf_state & L2CAP_CONF_REQ_SENT)) { 23170a708f8fSGustavo F. Padovan u8 buf[64]; 2318b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_REQ_SENT; 23190a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 232073ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 232173ffa904SGustavo F. Padovan chan->num_conf_req++; 23220a708f8fSGustavo F. Padovan } 23230a708f8fSGustavo F. Padovan 23240a708f8fSGustavo F. Padovan unlock: 23250a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 23260a708f8fSGustavo F. Padovan return 0; 23270a708f8fSGustavo F. Padovan } 23280a708f8fSGustavo F. Padovan 23290a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 23300a708f8fSGustavo F. Padovan { 23310a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; 23320a708f8fSGustavo F. Padovan u16 scid, flags, result; 233348454079SGustavo F. Padovan struct l2cap_chan *chan; 23340a708f8fSGustavo F. Padovan struct sock *sk; 23350a708f8fSGustavo F. Padovan int len = cmd->len - sizeof(*rsp); 23360a708f8fSGustavo F. Padovan 23370a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 23380a708f8fSGustavo F. Padovan flags = __le16_to_cpu(rsp->flags); 23390a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 23400a708f8fSGustavo F. Padovan 23410a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", 23420a708f8fSGustavo F. Padovan scid, flags, result); 23430a708f8fSGustavo F. Padovan 2344baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 234548454079SGustavo F. Padovan if (!chan) 23460a708f8fSGustavo F. Padovan return 0; 23470a708f8fSGustavo F. Padovan 234848454079SGustavo F. Padovan sk = chan->sk; 234948454079SGustavo F. Padovan 23500a708f8fSGustavo F. Padovan switch (result) { 23510a708f8fSGustavo F. Padovan case L2CAP_CONF_SUCCESS: 235247d1ec61SGustavo F. Padovan l2cap_conf_rfc_get(chan, rsp->data, len); 23530a708f8fSGustavo F. Padovan break; 23540a708f8fSGustavo F. Padovan 23550a708f8fSGustavo F. Padovan case L2CAP_CONF_UNACCEPT: 235673ffa904SGustavo F. Padovan if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { 23570a708f8fSGustavo F. Padovan char req[64]; 23580a708f8fSGustavo F. Padovan 23590a708f8fSGustavo F. Padovan if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { 2360e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 23610a708f8fSGustavo F. Padovan goto done; 23620a708f8fSGustavo F. Padovan } 23630a708f8fSGustavo F. Padovan 23640a708f8fSGustavo F. Padovan /* throw out any old stored conf requests */ 23650a708f8fSGustavo F. Padovan result = L2CAP_CONF_SUCCESS; 2366b4450035SGustavo F. Padovan len = l2cap_parse_conf_rsp(chan, rsp->data, len, 2367b4450035SGustavo F. Padovan req, &result); 23680a708f8fSGustavo F. Padovan if (len < 0) { 2369e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 23700a708f8fSGustavo F. Padovan goto done; 23710a708f8fSGustavo F. Padovan } 23720a708f8fSGustavo F. Padovan 23730a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 23740a708f8fSGustavo F. Padovan L2CAP_CONF_REQ, len, req); 237573ffa904SGustavo F. Padovan chan->num_conf_req++; 23760a708f8fSGustavo F. Padovan if (result != L2CAP_CONF_SUCCESS) 23770a708f8fSGustavo F. Padovan goto done; 23780a708f8fSGustavo F. Padovan break; 23790a708f8fSGustavo F. Padovan } 23800a708f8fSGustavo F. Padovan 23810a708f8fSGustavo F. Padovan default: 23820a708f8fSGustavo F. Padovan sk->sk_err = ECONNRESET; 23830a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, HZ * 5); 2384e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 23850a708f8fSGustavo F. Padovan goto done; 23860a708f8fSGustavo F. Padovan } 23870a708f8fSGustavo F. Padovan 23880a708f8fSGustavo F. Padovan if (flags & 0x01) 23890a708f8fSGustavo F. Padovan goto done; 23900a708f8fSGustavo F. Padovan 2391b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_INPUT_DONE; 23920a708f8fSGustavo F. Padovan 2393b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_OUTPUT_DONE) { 239447d1ec61SGustavo F. Padovan set_default_fcs(chan); 23950a708f8fSGustavo F. Padovan 23960a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 239742e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 239842e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 239958d35f87SGustavo F. Padovan skb_queue_head_init(&chan->tx_q); 24000c1bc5c6SGustavo F. Padovan if (chan->mode == L2CAP_MODE_ERTM) 2401525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 24020a708f8fSGustavo F. Padovan 24030a708f8fSGustavo F. Padovan l2cap_chan_ready(sk); 24040a708f8fSGustavo F. Padovan } 24050a708f8fSGustavo F. Padovan 24060a708f8fSGustavo F. Padovan done: 24070a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 24080a708f8fSGustavo F. Padovan return 0; 24090a708f8fSGustavo F. Padovan } 24100a708f8fSGustavo F. Padovan 24110a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 24120a708f8fSGustavo F. Padovan { 24130a708f8fSGustavo F. Padovan struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; 24140a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp rsp; 24150a708f8fSGustavo F. Padovan u16 dcid, scid; 241648454079SGustavo F. Padovan struct l2cap_chan *chan; 24170a708f8fSGustavo F. Padovan struct sock *sk; 24180a708f8fSGustavo F. Padovan 24190a708f8fSGustavo F. Padovan scid = __le16_to_cpu(req->scid); 24200a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 24210a708f8fSGustavo F. Padovan 24220a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); 24230a708f8fSGustavo F. Padovan 2424baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 242548454079SGustavo F. Padovan if (!chan) 24260a708f8fSGustavo F. Padovan return 0; 24270a708f8fSGustavo F. Padovan 242848454079SGustavo F. Padovan sk = chan->sk; 242948454079SGustavo F. Padovan 2430fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 2431fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 24320a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); 24330a708f8fSGustavo F. Padovan 24340a708f8fSGustavo F. Padovan sk->sk_shutdown = SHUTDOWN_MASK; 24350a708f8fSGustavo F. Padovan 24360a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 24370a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 24380a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 24390a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 24400a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, HZ / 5); 24410a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 24420a708f8fSGustavo F. Padovan return 0; 24430a708f8fSGustavo F. Padovan } 24440a708f8fSGustavo F. Padovan 244548454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNRESET); 24460a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 24470a708f8fSGustavo F. Padovan 24480a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 24490a708f8fSGustavo F. Padovan return 0; 24500a708f8fSGustavo F. Padovan } 24510a708f8fSGustavo F. Padovan 24520a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 24530a708f8fSGustavo F. Padovan { 24540a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; 24550a708f8fSGustavo F. Padovan u16 dcid, scid; 245648454079SGustavo F. Padovan struct l2cap_chan *chan; 24570a708f8fSGustavo F. Padovan struct sock *sk; 24580a708f8fSGustavo F. Padovan 24590a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 24600a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 24610a708f8fSGustavo F. Padovan 24620a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); 24630a708f8fSGustavo F. Padovan 2464baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 246548454079SGustavo F. Padovan if (!chan) 24660a708f8fSGustavo F. Padovan return 0; 24670a708f8fSGustavo F. Padovan 246848454079SGustavo F. Padovan sk = chan->sk; 246948454079SGustavo F. Padovan 24700a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 24710a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 24720a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 24730a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 24740a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, HZ / 5); 24750a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 24760a708f8fSGustavo F. Padovan return 0; 24770a708f8fSGustavo F. Padovan } 24780a708f8fSGustavo F. Padovan 247948454079SGustavo F. Padovan l2cap_chan_del(chan, 0); 24800a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 24810a708f8fSGustavo F. Padovan 24820a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 24830a708f8fSGustavo F. Padovan return 0; 24840a708f8fSGustavo F. Padovan } 24850a708f8fSGustavo F. Padovan 24860a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 24870a708f8fSGustavo F. Padovan { 24880a708f8fSGustavo F. Padovan struct l2cap_info_req *req = (struct l2cap_info_req *) data; 24890a708f8fSGustavo F. Padovan u16 type; 24900a708f8fSGustavo F. Padovan 24910a708f8fSGustavo F. Padovan type = __le16_to_cpu(req->type); 24920a708f8fSGustavo F. Padovan 24930a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x", type); 24940a708f8fSGustavo F. Padovan 24950a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 24960a708f8fSGustavo F. Padovan u8 buf[8]; 24970a708f8fSGustavo F. Padovan u32 feat_mask = l2cap_feat_mask; 24980a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 24990a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 25000a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 25010a708f8fSGustavo F. Padovan if (!disable_ertm) 25020a708f8fSGustavo F. Padovan feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING 25030a708f8fSGustavo F. Padovan | L2CAP_FEAT_FCS; 25040a708f8fSGustavo F. Padovan put_unaligned_le32(feat_mask, rsp->data); 25050a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 25060a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 25070a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 25080a708f8fSGustavo F. Padovan u8 buf[12]; 25090a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 25100a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 25110a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 25120a708f8fSGustavo F. Padovan memcpy(buf + 4, l2cap_fixed_chan, 8); 25130a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 25140a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 25150a708f8fSGustavo F. Padovan } else { 25160a708f8fSGustavo F. Padovan struct l2cap_info_rsp rsp; 25170a708f8fSGustavo F. Padovan rsp.type = cpu_to_le16(type); 25180a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); 25190a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 25200a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(rsp), &rsp); 25210a708f8fSGustavo F. Padovan } 25220a708f8fSGustavo F. Padovan 25230a708f8fSGustavo F. Padovan return 0; 25240a708f8fSGustavo F. Padovan } 25250a708f8fSGustavo F. Padovan 25260a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 25270a708f8fSGustavo F. Padovan { 25280a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; 25290a708f8fSGustavo F. Padovan u16 type, result; 25300a708f8fSGustavo F. Padovan 25310a708f8fSGustavo F. Padovan type = __le16_to_cpu(rsp->type); 25320a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 25330a708f8fSGustavo F. Padovan 25340a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); 25350a708f8fSGustavo F. Padovan 2536e90165beSAndrei Emeltchenko /* L2CAP Info req/rsp are unbound to channels, add extra checks */ 2537e90165beSAndrei Emeltchenko if (cmd->ident != conn->info_ident || 2538e90165beSAndrei Emeltchenko conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) 2539e90165beSAndrei Emeltchenko return 0; 2540e90165beSAndrei Emeltchenko 25410a708f8fSGustavo F. Padovan del_timer(&conn->info_timer); 25420a708f8fSGustavo F. Padovan 25430a708f8fSGustavo F. Padovan if (result != L2CAP_IR_SUCCESS) { 25440a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 25450a708f8fSGustavo F. Padovan conn->info_ident = 0; 25460a708f8fSGustavo F. Padovan 25470a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 25480a708f8fSGustavo F. Padovan 25490a708f8fSGustavo F. Padovan return 0; 25500a708f8fSGustavo F. Padovan } 25510a708f8fSGustavo F. Padovan 25520a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 25530a708f8fSGustavo F. Padovan conn->feat_mask = get_unaligned_le32(rsp->data); 25540a708f8fSGustavo F. Padovan 25550a708f8fSGustavo F. Padovan if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { 25560a708f8fSGustavo F. Padovan struct l2cap_info_req req; 25570a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 25580a708f8fSGustavo F. Padovan 25590a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 25600a708f8fSGustavo F. Padovan 25610a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 25620a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 25630a708f8fSGustavo F. Padovan } else { 25640a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 25650a708f8fSGustavo F. Padovan conn->info_ident = 0; 25660a708f8fSGustavo F. Padovan 25670a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 25680a708f8fSGustavo F. Padovan } 25690a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 25700a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 25710a708f8fSGustavo F. Padovan conn->info_ident = 0; 25720a708f8fSGustavo F. Padovan 25730a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 25740a708f8fSGustavo F. Padovan } 25750a708f8fSGustavo F. Padovan 25760a708f8fSGustavo F. Padovan return 0; 25770a708f8fSGustavo F. Padovan } 25780a708f8fSGustavo F. Padovan 2579e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency, 2580de73115aSClaudio Takahasi u16 to_multiplier) 2581de73115aSClaudio Takahasi { 2582de73115aSClaudio Takahasi u16 max_latency; 2583de73115aSClaudio Takahasi 2584de73115aSClaudio Takahasi if (min > max || min < 6 || max > 3200) 2585de73115aSClaudio Takahasi return -EINVAL; 2586de73115aSClaudio Takahasi 2587de73115aSClaudio Takahasi if (to_multiplier < 10 || to_multiplier > 3200) 2588de73115aSClaudio Takahasi return -EINVAL; 2589de73115aSClaudio Takahasi 2590de73115aSClaudio Takahasi if (max >= to_multiplier * 8) 2591de73115aSClaudio Takahasi return -EINVAL; 2592de73115aSClaudio Takahasi 2593de73115aSClaudio Takahasi max_latency = (to_multiplier * 8 / max) - 1; 2594de73115aSClaudio Takahasi if (latency > 499 || latency > max_latency) 2595de73115aSClaudio Takahasi return -EINVAL; 2596de73115aSClaudio Takahasi 2597de73115aSClaudio Takahasi return 0; 2598de73115aSClaudio Takahasi } 2599de73115aSClaudio Takahasi 2600de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, 2601de73115aSClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 2602de73115aSClaudio Takahasi { 2603de73115aSClaudio Takahasi struct hci_conn *hcon = conn->hcon; 2604de73115aSClaudio Takahasi struct l2cap_conn_param_update_req *req; 2605de73115aSClaudio Takahasi struct l2cap_conn_param_update_rsp rsp; 2606de73115aSClaudio Takahasi u16 min, max, latency, to_multiplier, cmd_len; 26072ce603ebSClaudio Takahasi int err; 2608de73115aSClaudio Takahasi 2609de73115aSClaudio Takahasi if (!(hcon->link_mode & HCI_LM_MASTER)) 2610de73115aSClaudio Takahasi return -EINVAL; 2611de73115aSClaudio Takahasi 2612de73115aSClaudio Takahasi cmd_len = __le16_to_cpu(cmd->len); 2613de73115aSClaudio Takahasi if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) 2614de73115aSClaudio Takahasi return -EPROTO; 2615de73115aSClaudio Takahasi 2616de73115aSClaudio Takahasi req = (struct l2cap_conn_param_update_req *) data; 2617de73115aSClaudio Takahasi min = __le16_to_cpu(req->min); 2618de73115aSClaudio Takahasi max = __le16_to_cpu(req->max); 2619de73115aSClaudio Takahasi latency = __le16_to_cpu(req->latency); 2620de73115aSClaudio Takahasi to_multiplier = __le16_to_cpu(req->to_multiplier); 2621de73115aSClaudio Takahasi 2622de73115aSClaudio Takahasi BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", 2623de73115aSClaudio Takahasi min, max, latency, to_multiplier); 2624de73115aSClaudio Takahasi 2625de73115aSClaudio Takahasi memset(&rsp, 0, sizeof(rsp)); 26262ce603ebSClaudio Takahasi 26272ce603ebSClaudio Takahasi err = l2cap_check_conn_param(min, max, latency, to_multiplier); 26282ce603ebSClaudio Takahasi if (err) 2629de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); 2630de73115aSClaudio Takahasi else 2631de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); 2632de73115aSClaudio Takahasi 2633de73115aSClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, 2634de73115aSClaudio Takahasi sizeof(rsp), &rsp); 2635de73115aSClaudio Takahasi 26362ce603ebSClaudio Takahasi if (!err) 26372ce603ebSClaudio Takahasi hci_le_conn_update(hcon, min, max, latency, to_multiplier); 26382ce603ebSClaudio Takahasi 2639de73115aSClaudio Takahasi return 0; 2640de73115aSClaudio Takahasi } 2641de73115aSClaudio Takahasi 26423300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, 26433300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 26443300d9a9SClaudio Takahasi { 26453300d9a9SClaudio Takahasi int err = 0; 26463300d9a9SClaudio Takahasi 26473300d9a9SClaudio Takahasi switch (cmd->code) { 26483300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 26493300d9a9SClaudio Takahasi l2cap_command_rej(conn, cmd, data); 26503300d9a9SClaudio Takahasi break; 26513300d9a9SClaudio Takahasi 26523300d9a9SClaudio Takahasi case L2CAP_CONN_REQ: 26533300d9a9SClaudio Takahasi err = l2cap_connect_req(conn, cmd, data); 26543300d9a9SClaudio Takahasi break; 26553300d9a9SClaudio Takahasi 26563300d9a9SClaudio Takahasi case L2CAP_CONN_RSP: 26573300d9a9SClaudio Takahasi err = l2cap_connect_rsp(conn, cmd, data); 26583300d9a9SClaudio Takahasi break; 26593300d9a9SClaudio Takahasi 26603300d9a9SClaudio Takahasi case L2CAP_CONF_REQ: 26613300d9a9SClaudio Takahasi err = l2cap_config_req(conn, cmd, cmd_len, data); 26623300d9a9SClaudio Takahasi break; 26633300d9a9SClaudio Takahasi 26643300d9a9SClaudio Takahasi case L2CAP_CONF_RSP: 26653300d9a9SClaudio Takahasi err = l2cap_config_rsp(conn, cmd, data); 26663300d9a9SClaudio Takahasi break; 26673300d9a9SClaudio Takahasi 26683300d9a9SClaudio Takahasi case L2CAP_DISCONN_REQ: 26693300d9a9SClaudio Takahasi err = l2cap_disconnect_req(conn, cmd, data); 26703300d9a9SClaudio Takahasi break; 26713300d9a9SClaudio Takahasi 26723300d9a9SClaudio Takahasi case L2CAP_DISCONN_RSP: 26733300d9a9SClaudio Takahasi err = l2cap_disconnect_rsp(conn, cmd, data); 26743300d9a9SClaudio Takahasi break; 26753300d9a9SClaudio Takahasi 26763300d9a9SClaudio Takahasi case L2CAP_ECHO_REQ: 26773300d9a9SClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); 26783300d9a9SClaudio Takahasi break; 26793300d9a9SClaudio Takahasi 26803300d9a9SClaudio Takahasi case L2CAP_ECHO_RSP: 26813300d9a9SClaudio Takahasi break; 26823300d9a9SClaudio Takahasi 26833300d9a9SClaudio Takahasi case L2CAP_INFO_REQ: 26843300d9a9SClaudio Takahasi err = l2cap_information_req(conn, cmd, data); 26853300d9a9SClaudio Takahasi break; 26863300d9a9SClaudio Takahasi 26873300d9a9SClaudio Takahasi case L2CAP_INFO_RSP: 26883300d9a9SClaudio Takahasi err = l2cap_information_rsp(conn, cmd, data); 26893300d9a9SClaudio Takahasi break; 26903300d9a9SClaudio Takahasi 26913300d9a9SClaudio Takahasi default: 26923300d9a9SClaudio Takahasi BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); 26933300d9a9SClaudio Takahasi err = -EINVAL; 26943300d9a9SClaudio Takahasi break; 26953300d9a9SClaudio Takahasi } 26963300d9a9SClaudio Takahasi 26973300d9a9SClaudio Takahasi return err; 26983300d9a9SClaudio Takahasi } 26993300d9a9SClaudio Takahasi 27003300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, 27013300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 27023300d9a9SClaudio Takahasi { 27033300d9a9SClaudio Takahasi switch (cmd->code) { 27043300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 27053300d9a9SClaudio Takahasi return 0; 27063300d9a9SClaudio Takahasi 27073300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_REQ: 2708de73115aSClaudio Takahasi return l2cap_conn_param_update_req(conn, cmd, data); 27093300d9a9SClaudio Takahasi 27103300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_RSP: 27113300d9a9SClaudio Takahasi return 0; 27123300d9a9SClaudio Takahasi 27133300d9a9SClaudio Takahasi default: 27143300d9a9SClaudio Takahasi BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); 27153300d9a9SClaudio Takahasi return -EINVAL; 27163300d9a9SClaudio Takahasi } 27173300d9a9SClaudio Takahasi } 27183300d9a9SClaudio Takahasi 27193300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn, 27203300d9a9SClaudio Takahasi struct sk_buff *skb) 27210a708f8fSGustavo F. Padovan { 27220a708f8fSGustavo F. Padovan u8 *data = skb->data; 27230a708f8fSGustavo F. Padovan int len = skb->len; 27240a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr cmd; 27253300d9a9SClaudio Takahasi int err; 27260a708f8fSGustavo F. Padovan 27270a708f8fSGustavo F. Padovan l2cap_raw_recv(conn, skb); 27280a708f8fSGustavo F. Padovan 27290a708f8fSGustavo F. Padovan while (len >= L2CAP_CMD_HDR_SIZE) { 27300a708f8fSGustavo F. Padovan u16 cmd_len; 27310a708f8fSGustavo F. Padovan memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); 27320a708f8fSGustavo F. Padovan data += L2CAP_CMD_HDR_SIZE; 27330a708f8fSGustavo F. Padovan len -= L2CAP_CMD_HDR_SIZE; 27340a708f8fSGustavo F. Padovan 27350a708f8fSGustavo F. Padovan cmd_len = le16_to_cpu(cmd.len); 27360a708f8fSGustavo F. Padovan 27370a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident); 27380a708f8fSGustavo F. Padovan 27390a708f8fSGustavo F. Padovan if (cmd_len > len || !cmd.ident) { 27400a708f8fSGustavo F. Padovan BT_DBG("corrupted command"); 27410a708f8fSGustavo F. Padovan break; 27420a708f8fSGustavo F. Padovan } 27430a708f8fSGustavo F. Padovan 27443300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 27453300d9a9SClaudio Takahasi err = l2cap_le_sig_cmd(conn, &cmd, data); 27463300d9a9SClaudio Takahasi else 27473300d9a9SClaudio Takahasi err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); 27480a708f8fSGustavo F. Padovan 27490a708f8fSGustavo F. Padovan if (err) { 27500a708f8fSGustavo F. Padovan struct l2cap_cmd_rej rej; 27512c6d1a2eSGustavo F. Padovan 27522c6d1a2eSGustavo F. Padovan BT_ERR("Wrong link type (%d)", err); 27530a708f8fSGustavo F. Padovan 27540a708f8fSGustavo F. Padovan /* FIXME: Map err to a valid reason */ 27550a708f8fSGustavo F. Padovan rej.reason = cpu_to_le16(0); 27560a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); 27570a708f8fSGustavo F. Padovan } 27580a708f8fSGustavo F. Padovan 27590a708f8fSGustavo F. Padovan data += cmd_len; 27600a708f8fSGustavo F. Padovan len -= cmd_len; 27610a708f8fSGustavo F. Padovan } 27620a708f8fSGustavo F. Padovan 27630a708f8fSGustavo F. Padovan kfree_skb(skb); 27640a708f8fSGustavo F. Padovan } 27650a708f8fSGustavo F. Padovan 276647d1ec61SGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb) 27670a708f8fSGustavo F. Padovan { 27680a708f8fSGustavo F. Padovan u16 our_fcs, rcv_fcs; 27690a708f8fSGustavo F. Padovan int hdr_size = L2CAP_HDR_SIZE + 2; 27700a708f8fSGustavo F. Padovan 277147d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) { 27720a708f8fSGustavo F. Padovan skb_trim(skb, skb->len - 2); 27730a708f8fSGustavo F. Padovan rcv_fcs = get_unaligned_le16(skb->data + skb->len); 27740a708f8fSGustavo F. Padovan our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); 27750a708f8fSGustavo F. Padovan 27760a708f8fSGustavo F. Padovan if (our_fcs != rcv_fcs) 27770a708f8fSGustavo F. Padovan return -EBADMSG; 27780a708f8fSGustavo F. Padovan } 27790a708f8fSGustavo F. Padovan return 0; 27800a708f8fSGustavo F. Padovan } 27810a708f8fSGustavo F. Padovan 2782525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan) 27830a708f8fSGustavo F. Padovan { 27840a708f8fSGustavo F. Padovan u16 control = 0; 27850a708f8fSGustavo F. Padovan 27866a026610SGustavo F. Padovan chan->frames_sent = 0; 27870a708f8fSGustavo F. Padovan 278842e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 27890a708f8fSGustavo F. Padovan 2790525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 27910a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 2792525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 2793525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 27940a708f8fSGustavo F. Padovan } 27950a708f8fSGustavo F. Padovan 2796525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REMOTE_BUSY) 2797525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 27980a708f8fSGustavo F. Padovan 2799525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 28000a708f8fSGustavo F. Padovan 2801525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_LOCAL_BUSY) && 28026a026610SGustavo F. Padovan chan->frames_sent == 0) { 28030a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 2804525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 28050a708f8fSGustavo F. Padovan } 28060a708f8fSGustavo F. Padovan } 28070a708f8fSGustavo F. Padovan 280842e5c802SGustavo F. Padovan static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u8 tx_seq, u8 sar) 28090a708f8fSGustavo F. Padovan { 28100a708f8fSGustavo F. Padovan struct sk_buff *next_skb; 28110a708f8fSGustavo F. Padovan int tx_seq_offset, next_tx_seq_offset; 28120a708f8fSGustavo F. Padovan 28130a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 28140a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 28150a708f8fSGustavo F. Padovan 2816f1c6775bSGustavo F. Padovan next_skb = skb_peek(&chan->srej_q); 28170a708f8fSGustavo F. Padovan if (!next_skb) { 2818f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 28190a708f8fSGustavo F. Padovan return 0; 28200a708f8fSGustavo F. Padovan } 28210a708f8fSGustavo F. Padovan 282242e5c802SGustavo F. Padovan tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; 28230a708f8fSGustavo F. Padovan if (tx_seq_offset < 0) 28240a708f8fSGustavo F. Padovan tx_seq_offset += 64; 28250a708f8fSGustavo F. Padovan 28260a708f8fSGustavo F. Padovan do { 28270a708f8fSGustavo F. Padovan if (bt_cb(next_skb)->tx_seq == tx_seq) 28280a708f8fSGustavo F. Padovan return -EINVAL; 28290a708f8fSGustavo F. Padovan 28300a708f8fSGustavo F. Padovan next_tx_seq_offset = (bt_cb(next_skb)->tx_seq - 283142e5c802SGustavo F. Padovan chan->buffer_seq) % 64; 28320a708f8fSGustavo F. Padovan if (next_tx_seq_offset < 0) 28330a708f8fSGustavo F. Padovan next_tx_seq_offset += 64; 28340a708f8fSGustavo F. Padovan 28350a708f8fSGustavo F. Padovan if (next_tx_seq_offset > tx_seq_offset) { 2836f1c6775bSGustavo F. Padovan __skb_queue_before(&chan->srej_q, next_skb, skb); 28370a708f8fSGustavo F. Padovan return 0; 28380a708f8fSGustavo F. Padovan } 28390a708f8fSGustavo F. Padovan 2840f1c6775bSGustavo F. Padovan if (skb_queue_is_last(&chan->srej_q, next_skb)) 28410a708f8fSGustavo F. Padovan break; 28420a708f8fSGustavo F. Padovan 2843f1c6775bSGustavo F. Padovan } while ((next_skb = skb_queue_next(&chan->srej_q, next_skb))); 28440a708f8fSGustavo F. Padovan 2845f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 28460a708f8fSGustavo F. Padovan 28470a708f8fSGustavo F. Padovan return 0; 28480a708f8fSGustavo F. Padovan } 28490a708f8fSGustavo F. Padovan 2850525cd185SGustavo F. Padovan static int l2cap_ertm_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 28510a708f8fSGustavo F. Padovan { 28520a708f8fSGustavo F. Padovan struct sk_buff *_skb; 28530a708f8fSGustavo F. Padovan int err; 28540a708f8fSGustavo F. Padovan 28550a708f8fSGustavo F. Padovan switch (control & L2CAP_CTRL_SAR) { 28560a708f8fSGustavo F. Padovan case L2CAP_SDU_UNSEGMENTED: 2857525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) 28580a708f8fSGustavo F. Padovan goto drop; 28590a708f8fSGustavo F. Padovan 2860525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, skb); 28610a708f8fSGustavo F. Padovan if (!err) 28620a708f8fSGustavo F. Padovan return err; 28630a708f8fSGustavo F. Padovan 28640a708f8fSGustavo F. Padovan break; 28650a708f8fSGustavo F. Padovan 28660a708f8fSGustavo F. Padovan case L2CAP_SDU_START: 2867525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) 28680a708f8fSGustavo F. Padovan goto drop; 28690a708f8fSGustavo F. Padovan 28706f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 28710a708f8fSGustavo F. Padovan 28720c1bc5c6SGustavo F. Padovan if (chan->sdu_len > chan->imtu) 28730a708f8fSGustavo F. Padovan goto disconnect; 28740a708f8fSGustavo F. Padovan 28756f61fd47SGustavo F. Padovan chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC); 28766f61fd47SGustavo F. Padovan if (!chan->sdu) 28770a708f8fSGustavo F. Padovan return -ENOMEM; 28780a708f8fSGustavo F. Padovan 28790a708f8fSGustavo F. Padovan /* pull sdu_len bytes only after alloc, because of Local Busy 28800a708f8fSGustavo F. Padovan * condition we have to be sure that this will be executed 28810a708f8fSGustavo F. Padovan * only once, i.e., when alloc does not fail */ 28820a708f8fSGustavo F. Padovan skb_pull(skb, 2); 28830a708f8fSGustavo F. Padovan 28846f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 28850a708f8fSGustavo F. Padovan 2886525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_SDU; 28876f61fd47SGustavo F. Padovan chan->partial_sdu_len = skb->len; 28880a708f8fSGustavo F. Padovan break; 28890a708f8fSGustavo F. Padovan 28900a708f8fSGustavo F. Padovan case L2CAP_SDU_CONTINUE: 2891525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 28920a708f8fSGustavo F. Padovan goto disconnect; 28930a708f8fSGustavo F. Padovan 28946f61fd47SGustavo F. Padovan if (!chan->sdu) 28950a708f8fSGustavo F. Padovan goto disconnect; 28960a708f8fSGustavo F. Padovan 28976f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 28986f61fd47SGustavo F. Padovan if (chan->partial_sdu_len > chan->sdu_len) 28990a708f8fSGustavo F. Padovan goto drop; 29000a708f8fSGustavo F. Padovan 29016f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 29020a708f8fSGustavo F. Padovan 29030a708f8fSGustavo F. Padovan break; 29040a708f8fSGustavo F. Padovan 29050a708f8fSGustavo F. Padovan case L2CAP_SDU_END: 2906525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 29070a708f8fSGustavo F. Padovan goto disconnect; 29080a708f8fSGustavo F. Padovan 29096f61fd47SGustavo F. Padovan if (!chan->sdu) 29100a708f8fSGustavo F. Padovan goto disconnect; 29110a708f8fSGustavo F. Padovan 2912525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_RETRY)) { 29136f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 29140a708f8fSGustavo F. Padovan 29150c1bc5c6SGustavo F. Padovan if (chan->partial_sdu_len > chan->imtu) 29160a708f8fSGustavo F. Padovan goto drop; 29170a708f8fSGustavo F. Padovan 29186f61fd47SGustavo F. Padovan if (chan->partial_sdu_len != chan->sdu_len) 29190a708f8fSGustavo F. Padovan goto drop; 29200a708f8fSGustavo F. Padovan 29216f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 29220a708f8fSGustavo F. Padovan } 29230a708f8fSGustavo F. Padovan 29246f61fd47SGustavo F. Padovan _skb = skb_clone(chan->sdu, GFP_ATOMIC); 29250a708f8fSGustavo F. Padovan if (!_skb) { 2926525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_RETRY; 29270a708f8fSGustavo F. Padovan return -ENOMEM; 29280a708f8fSGustavo F. Padovan } 29290a708f8fSGustavo F. Padovan 2930525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, _skb); 29310a708f8fSGustavo F. Padovan if (err < 0) { 29320a708f8fSGustavo F. Padovan kfree_skb(_skb); 2933525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_RETRY; 29340a708f8fSGustavo F. Padovan return err; 29350a708f8fSGustavo F. Padovan } 29360a708f8fSGustavo F. Padovan 2937525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_RETRY; 2938525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_SDU; 29390a708f8fSGustavo F. Padovan 29406f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 29410a708f8fSGustavo F. Padovan break; 29420a708f8fSGustavo F. Padovan } 29430a708f8fSGustavo F. Padovan 29440a708f8fSGustavo F. Padovan kfree_skb(skb); 29450a708f8fSGustavo F. Padovan return 0; 29460a708f8fSGustavo F. Padovan 29470a708f8fSGustavo F. Padovan drop: 29486f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 29496f61fd47SGustavo F. Padovan chan->sdu = NULL; 29500a708f8fSGustavo F. Padovan 29510a708f8fSGustavo F. Padovan disconnect: 29528c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 29530a708f8fSGustavo F. Padovan kfree_skb(skb); 29540a708f8fSGustavo F. Padovan return 0; 29550a708f8fSGustavo F. Padovan } 29560a708f8fSGustavo F. Padovan 2957525cd185SGustavo F. Padovan static int l2cap_try_push_rx_skb(struct l2cap_chan *chan) 29580a708f8fSGustavo F. Padovan { 29590a708f8fSGustavo F. Padovan struct sk_buff *skb; 29600a708f8fSGustavo F. Padovan u16 control; 29610a708f8fSGustavo F. Padovan int err; 29620a708f8fSGustavo F. Padovan 2963f1c6775bSGustavo F. Padovan while ((skb = skb_dequeue(&chan->busy_q))) { 29640a708f8fSGustavo F. Padovan control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT; 2965525cd185SGustavo F. Padovan err = l2cap_ertm_reassembly_sdu(chan, skb, control); 29660a708f8fSGustavo F. Padovan if (err < 0) { 2967f1c6775bSGustavo F. Padovan skb_queue_head(&chan->busy_q, skb); 29680a708f8fSGustavo F. Padovan return -EBUSY; 29690a708f8fSGustavo F. Padovan } 29700a708f8fSGustavo F. Padovan 297142e5c802SGustavo F. Padovan chan->buffer_seq = (chan->buffer_seq + 1) % 64; 29720a708f8fSGustavo F. Padovan } 29730a708f8fSGustavo F. Padovan 2974525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_RNR_SENT)) 29750a708f8fSGustavo F. Padovan goto done; 29760a708f8fSGustavo F. Padovan 297742e5c802SGustavo F. Padovan control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 29780a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY | L2CAP_CTRL_POLL; 2979525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 29806a026610SGustavo F. Padovan chan->retry_count = 1; 29810a708f8fSGustavo F. Padovan 2982e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 29830a708f8fSGustavo F. Padovan __mod_monitor_timer(); 29840a708f8fSGustavo F. Padovan 2985525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_WAIT_F; 29860a708f8fSGustavo F. Padovan 29870a708f8fSGustavo F. Padovan done: 2988525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_LOCAL_BUSY; 2989525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_RNR_SENT; 29900a708f8fSGustavo F. Padovan 299149208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit local busy", chan); 29920a708f8fSGustavo F. Padovan 29930a708f8fSGustavo F. Padovan return 0; 29940a708f8fSGustavo F. Padovan } 29950a708f8fSGustavo F. Padovan 29960a708f8fSGustavo F. Padovan static void l2cap_busy_work(struct work_struct *work) 29970a708f8fSGustavo F. Padovan { 29980a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 2999311bb895SGustavo F. Padovan struct l2cap_chan *chan = 3000311bb895SGustavo F. Padovan container_of(work, struct l2cap_chan, busy_work); 3001311bb895SGustavo F. Padovan struct sock *sk = chan->sk; 30020a708f8fSGustavo F. Padovan int n_tries = 0, timeo = HZ/5, err; 30030a708f8fSGustavo F. Padovan struct sk_buff *skb; 30040a708f8fSGustavo F. Padovan 30050a708f8fSGustavo F. Padovan lock_sock(sk); 30060a708f8fSGustavo F. Padovan 30070a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 3008311bb895SGustavo F. Padovan while ((skb = skb_peek(&chan->busy_q))) { 30090a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 30100a708f8fSGustavo F. Padovan 30110a708f8fSGustavo F. Padovan if (n_tries++ > L2CAP_LOCAL_BUSY_TRIES) { 30120a708f8fSGustavo F. Padovan err = -EBUSY; 30138c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, EBUSY); 30140a708f8fSGustavo F. Padovan break; 30150a708f8fSGustavo F. Padovan } 30160a708f8fSGustavo F. Padovan 30170a708f8fSGustavo F. Padovan if (!timeo) 30180a708f8fSGustavo F. Padovan timeo = HZ/5; 30190a708f8fSGustavo F. Padovan 30200a708f8fSGustavo F. Padovan if (signal_pending(current)) { 30210a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 30220a708f8fSGustavo F. Padovan break; 30230a708f8fSGustavo F. Padovan } 30240a708f8fSGustavo F. Padovan 30250a708f8fSGustavo F. Padovan release_sock(sk); 30260a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 30270a708f8fSGustavo F. Padovan lock_sock(sk); 30280a708f8fSGustavo F. Padovan 30290a708f8fSGustavo F. Padovan err = sock_error(sk); 30300a708f8fSGustavo F. Padovan if (err) 30310a708f8fSGustavo F. Padovan break; 30320a708f8fSGustavo F. Padovan 3033311bb895SGustavo F. Padovan if (l2cap_try_push_rx_skb(chan) == 0) 30340a708f8fSGustavo F. Padovan break; 30350a708f8fSGustavo F. Padovan } 30360a708f8fSGustavo F. Padovan 30370a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 30380a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 30390a708f8fSGustavo F. Padovan 30400a708f8fSGustavo F. Padovan release_sock(sk); 30410a708f8fSGustavo F. Padovan } 30420a708f8fSGustavo F. Padovan 3043525cd185SGustavo F. Padovan static int l2cap_push_rx_skb(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 30440a708f8fSGustavo F. Padovan { 30450a708f8fSGustavo F. Padovan int sctrl, err; 30460a708f8fSGustavo F. Padovan 3047525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 30480a708f8fSGustavo F. Padovan bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT; 3049f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->busy_q, skb); 3050525cd185SGustavo F. Padovan return l2cap_try_push_rx_skb(chan); 30510a708f8fSGustavo F. Padovan 30520a708f8fSGustavo F. Padovan 30530a708f8fSGustavo F. Padovan } 30540a708f8fSGustavo F. Padovan 3055525cd185SGustavo F. Padovan err = l2cap_ertm_reassembly_sdu(chan, skb, control); 30560a708f8fSGustavo F. Padovan if (err >= 0) { 305742e5c802SGustavo F. Padovan chan->buffer_seq = (chan->buffer_seq + 1) % 64; 30580a708f8fSGustavo F. Padovan return err; 30590a708f8fSGustavo F. Padovan } 30600a708f8fSGustavo F. Padovan 30610a708f8fSGustavo F. Padovan /* Busy Condition */ 3062311bb895SGustavo F. Padovan BT_DBG("chan %p, Enter local busy", chan); 30630a708f8fSGustavo F. Padovan 3064525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_LOCAL_BUSY; 30650a708f8fSGustavo F. Padovan bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT; 3066f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->busy_q, skb); 30670a708f8fSGustavo F. Padovan 306842e5c802SGustavo F. Padovan sctrl = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 30690a708f8fSGustavo F. Padovan sctrl |= L2CAP_SUPER_RCV_NOT_READY; 3070525cd185SGustavo F. Padovan l2cap_send_sframe(chan, sctrl); 30710a708f8fSGustavo F. Padovan 3072525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 30730a708f8fSGustavo F. Padovan 3074e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 30750a708f8fSGustavo F. Padovan 3076311bb895SGustavo F. Padovan queue_work(_busy_wq, &chan->busy_work); 30770a708f8fSGustavo F. Padovan 30780a708f8fSGustavo F. Padovan return err; 30790a708f8fSGustavo F. Padovan } 30800a708f8fSGustavo F. Padovan 3081525cd185SGustavo F. Padovan static int l2cap_streaming_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 30820a708f8fSGustavo F. Padovan { 30830a708f8fSGustavo F. Padovan struct sk_buff *_skb; 30840a708f8fSGustavo F. Padovan int err = -EINVAL; 30850a708f8fSGustavo F. Padovan 30860a708f8fSGustavo F. Padovan /* 30870a708f8fSGustavo F. Padovan * TODO: We have to notify the userland if some data is lost with the 30880a708f8fSGustavo F. Padovan * Streaming Mode. 30890a708f8fSGustavo F. Padovan */ 30900a708f8fSGustavo F. Padovan 30910a708f8fSGustavo F. Padovan switch (control & L2CAP_CTRL_SAR) { 30920a708f8fSGustavo F. Padovan case L2CAP_SDU_UNSEGMENTED: 3093525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) { 30946f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 30950a708f8fSGustavo F. Padovan break; 30960a708f8fSGustavo F. Padovan } 30970a708f8fSGustavo F. Padovan 3098525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, skb); 30990a708f8fSGustavo F. Padovan if (!err) 31000a708f8fSGustavo F. Padovan return 0; 31010a708f8fSGustavo F. Padovan 31020a708f8fSGustavo F. Padovan break; 31030a708f8fSGustavo F. Padovan 31040a708f8fSGustavo F. Padovan case L2CAP_SDU_START: 3105525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) { 31066f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 31070a708f8fSGustavo F. Padovan break; 31080a708f8fSGustavo F. Padovan } 31090a708f8fSGustavo F. Padovan 31106f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 31110a708f8fSGustavo F. Padovan skb_pull(skb, 2); 31120a708f8fSGustavo F. Padovan 31130c1bc5c6SGustavo F. Padovan if (chan->sdu_len > chan->imtu) { 31140a708f8fSGustavo F. Padovan err = -EMSGSIZE; 31150a708f8fSGustavo F. Padovan break; 31160a708f8fSGustavo F. Padovan } 31170a708f8fSGustavo F. Padovan 31186f61fd47SGustavo F. Padovan chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC); 31196f61fd47SGustavo F. Padovan if (!chan->sdu) { 31200a708f8fSGustavo F. Padovan err = -ENOMEM; 31210a708f8fSGustavo F. Padovan break; 31220a708f8fSGustavo F. Padovan } 31230a708f8fSGustavo F. Padovan 31246f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31250a708f8fSGustavo F. Padovan 3126525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_SDU; 31276f61fd47SGustavo F. Padovan chan->partial_sdu_len = skb->len; 31280a708f8fSGustavo F. Padovan err = 0; 31290a708f8fSGustavo F. Padovan break; 31300a708f8fSGustavo F. Padovan 31310a708f8fSGustavo F. Padovan case L2CAP_SDU_CONTINUE: 3132525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 31330a708f8fSGustavo F. Padovan break; 31340a708f8fSGustavo F. Padovan 31356f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31360a708f8fSGustavo F. Padovan 31376f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 31386f61fd47SGustavo F. Padovan if (chan->partial_sdu_len > chan->sdu_len) 31396f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 31400a708f8fSGustavo F. Padovan else 31410a708f8fSGustavo F. Padovan err = 0; 31420a708f8fSGustavo F. Padovan 31430a708f8fSGustavo F. Padovan break; 31440a708f8fSGustavo F. Padovan 31450a708f8fSGustavo F. Padovan case L2CAP_SDU_END: 3146525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 31470a708f8fSGustavo F. Padovan break; 31480a708f8fSGustavo F. Padovan 31496f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31500a708f8fSGustavo F. Padovan 3151525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_SDU; 31526f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 31530a708f8fSGustavo F. Padovan 31540c1bc5c6SGustavo F. Padovan if (chan->partial_sdu_len > chan->imtu) 31550a708f8fSGustavo F. Padovan goto drop; 31560a708f8fSGustavo F. Padovan 31576f61fd47SGustavo F. Padovan if (chan->partial_sdu_len == chan->sdu_len) { 31586f61fd47SGustavo F. Padovan _skb = skb_clone(chan->sdu, GFP_ATOMIC); 3159525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, _skb); 31600a708f8fSGustavo F. Padovan if (err < 0) 31610a708f8fSGustavo F. Padovan kfree_skb(_skb); 31620a708f8fSGustavo F. Padovan } 31630a708f8fSGustavo F. Padovan err = 0; 31640a708f8fSGustavo F. Padovan 31650a708f8fSGustavo F. Padovan drop: 31666f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 31670a708f8fSGustavo F. Padovan break; 31680a708f8fSGustavo F. Padovan } 31690a708f8fSGustavo F. Padovan 31700a708f8fSGustavo F. Padovan kfree_skb(skb); 31710a708f8fSGustavo F. Padovan return err; 31720a708f8fSGustavo F. Padovan } 31730a708f8fSGustavo F. Padovan 3174525cd185SGustavo F. Padovan static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq) 31750a708f8fSGustavo F. Padovan { 31760a708f8fSGustavo F. Padovan struct sk_buff *skb; 31770a708f8fSGustavo F. Padovan u16 control; 31780a708f8fSGustavo F. Padovan 3179f1c6775bSGustavo F. Padovan while ((skb = skb_peek(&chan->srej_q))) { 31800a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq != tx_seq) 31810a708f8fSGustavo F. Padovan break; 31820a708f8fSGustavo F. Padovan 3183f1c6775bSGustavo F. Padovan skb = skb_dequeue(&chan->srej_q); 31840a708f8fSGustavo F. Padovan control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT; 3185525cd185SGustavo F. Padovan l2cap_ertm_reassembly_sdu(chan, skb, control); 318642e5c802SGustavo F. Padovan chan->buffer_seq_srej = 318742e5c802SGustavo F. Padovan (chan->buffer_seq_srej + 1) % 64; 31880a708f8fSGustavo F. Padovan tx_seq = (tx_seq + 1) % 64; 31890a708f8fSGustavo F. Padovan } 31900a708f8fSGustavo F. Padovan } 31910a708f8fSGustavo F. Padovan 3192525cd185SGustavo F. Padovan static void l2cap_resend_srejframe(struct l2cap_chan *chan, u8 tx_seq) 31930a708f8fSGustavo F. Padovan { 31940a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 31950a708f8fSGustavo F. Padovan u16 control; 31960a708f8fSGustavo F. Padovan 319739d5a3eeSGustavo F. Padovan list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { 31980a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 31990a708f8fSGustavo F. Padovan list_del(&l->list); 32000a708f8fSGustavo F. Padovan kfree(l); 32010a708f8fSGustavo F. Padovan return; 32020a708f8fSGustavo F. Padovan } 32030a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 32040a708f8fSGustavo F. Padovan control |= l->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 3205525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 32060a708f8fSGustavo F. Padovan list_del(&l->list); 320739d5a3eeSGustavo F. Padovan list_add_tail(&l->list, &chan->srej_l); 32080a708f8fSGustavo F. Padovan } 32090a708f8fSGustavo F. Padovan } 32100a708f8fSGustavo F. Padovan 3211525cd185SGustavo F. Padovan static void l2cap_send_srejframe(struct l2cap_chan *chan, u8 tx_seq) 32120a708f8fSGustavo F. Padovan { 32130a708f8fSGustavo F. Padovan struct srej_list *new; 32140a708f8fSGustavo F. Padovan u16 control; 32150a708f8fSGustavo F. Padovan 321642e5c802SGustavo F. Padovan while (tx_seq != chan->expected_tx_seq) { 32170a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 321842e5c802SGustavo F. Padovan control |= chan->expected_tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 3219525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 32200a708f8fSGustavo F. Padovan 32210a708f8fSGustavo F. Padovan new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); 322242e5c802SGustavo F. Padovan new->tx_seq = chan->expected_tx_seq; 322342e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 322439d5a3eeSGustavo F. Padovan list_add_tail(&new->list, &chan->srej_l); 32250a708f8fSGustavo F. Padovan } 322642e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 32270a708f8fSGustavo F. Padovan } 32280a708f8fSGustavo F. Padovan 3229525cd185SGustavo F. Padovan static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) 32300a708f8fSGustavo F. Padovan { 32310a708f8fSGustavo F. Padovan u8 tx_seq = __get_txseq(rx_control); 32320a708f8fSGustavo F. Padovan u8 req_seq = __get_reqseq(rx_control); 32330a708f8fSGustavo F. Padovan u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT; 32340a708f8fSGustavo F. Padovan int tx_seq_offset, expected_tx_seq_offset; 323547d1ec61SGustavo F. Padovan int num_to_ack = (chan->tx_win/6) + 1; 32360a708f8fSGustavo F. Padovan int err = 0; 32370a708f8fSGustavo F. Padovan 3238525cd185SGustavo F. Padovan BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len, 3239525cd185SGustavo F. Padovan tx_seq, rx_control); 32400a708f8fSGustavo F. Padovan 32410a708f8fSGustavo F. Padovan if (L2CAP_CTRL_FINAL & rx_control && 3242525cd185SGustavo F. Padovan chan->conn_state & L2CAP_CONN_WAIT_F) { 3243e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 32446a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 32450a708f8fSGustavo F. Padovan __mod_retrans_timer(); 3246525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_WAIT_F; 32470a708f8fSGustavo F. Padovan } 32480a708f8fSGustavo F. Padovan 324942e5c802SGustavo F. Padovan chan->expected_ack_seq = req_seq; 325042e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 32510a708f8fSGustavo F. Padovan 325242e5c802SGustavo F. Padovan if (tx_seq == chan->expected_tx_seq) 32530a708f8fSGustavo F. Padovan goto expected; 32540a708f8fSGustavo F. Padovan 325542e5c802SGustavo F. Padovan tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; 32560a708f8fSGustavo F. Padovan if (tx_seq_offset < 0) 32570a708f8fSGustavo F. Padovan tx_seq_offset += 64; 32580a708f8fSGustavo F. Padovan 32590a708f8fSGustavo F. Padovan /* invalid tx_seq */ 326047d1ec61SGustavo F. Padovan if (tx_seq_offset >= chan->tx_win) { 32618c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 32620a708f8fSGustavo F. Padovan goto drop; 32630a708f8fSGustavo F. Padovan } 32640a708f8fSGustavo F. Padovan 3265525cd185SGustavo F. Padovan if (chan->conn_state == L2CAP_CONN_LOCAL_BUSY) 32660a708f8fSGustavo F. Padovan goto drop; 32670a708f8fSGustavo F. Padovan 3268525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 32690a708f8fSGustavo F. Padovan struct srej_list *first; 32700a708f8fSGustavo F. Padovan 327139d5a3eeSGustavo F. Padovan first = list_first_entry(&chan->srej_l, 32720a708f8fSGustavo F. Padovan struct srej_list, list); 32730a708f8fSGustavo F. Padovan if (tx_seq == first->tx_seq) { 327442e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 3275525cd185SGustavo F. Padovan l2cap_check_srej_gap(chan, tx_seq); 32760a708f8fSGustavo F. Padovan 32770a708f8fSGustavo F. Padovan list_del(&first->list); 32780a708f8fSGustavo F. Padovan kfree(first); 32790a708f8fSGustavo F. Padovan 328039d5a3eeSGustavo F. Padovan if (list_empty(&chan->srej_l)) { 328142e5c802SGustavo F. Padovan chan->buffer_seq = chan->buffer_seq_srej; 3282525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SREJ_SENT; 3283525cd185SGustavo F. Padovan l2cap_send_ack(chan); 328449208c9cSGustavo F. Padovan BT_DBG("chan %p, Exit SREJ_SENT", chan); 32850a708f8fSGustavo F. Padovan } 32860a708f8fSGustavo F. Padovan } else { 32870a708f8fSGustavo F. Padovan struct srej_list *l; 32880a708f8fSGustavo F. Padovan 32890a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 329042e5c802SGustavo F. Padovan if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0) 32910a708f8fSGustavo F. Padovan goto drop; 32920a708f8fSGustavo F. Padovan 329339d5a3eeSGustavo F. Padovan list_for_each_entry(l, &chan->srej_l, list) { 32940a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 3295525cd185SGustavo F. Padovan l2cap_resend_srejframe(chan, tx_seq); 32960a708f8fSGustavo F. Padovan return 0; 32970a708f8fSGustavo F. Padovan } 32980a708f8fSGustavo F. Padovan } 3299525cd185SGustavo F. Padovan l2cap_send_srejframe(chan, tx_seq); 33000a708f8fSGustavo F. Padovan } 33010a708f8fSGustavo F. Padovan } else { 33020a708f8fSGustavo F. Padovan expected_tx_seq_offset = 330342e5c802SGustavo F. Padovan (chan->expected_tx_seq - chan->buffer_seq) % 64; 33040a708f8fSGustavo F. Padovan if (expected_tx_seq_offset < 0) 33050a708f8fSGustavo F. Padovan expected_tx_seq_offset += 64; 33060a708f8fSGustavo F. Padovan 33070a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 33080a708f8fSGustavo F. Padovan if (tx_seq_offset < expected_tx_seq_offset) 33090a708f8fSGustavo F. Padovan goto drop; 33100a708f8fSGustavo F. Padovan 3311525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_SENT; 33120a708f8fSGustavo F. Padovan 331349208c9cSGustavo F. Padovan BT_DBG("chan %p, Enter SREJ", chan); 33140a708f8fSGustavo F. Padovan 331539d5a3eeSGustavo F. Padovan INIT_LIST_HEAD(&chan->srej_l); 331642e5c802SGustavo F. Padovan chan->buffer_seq_srej = chan->buffer_seq; 33170a708f8fSGustavo F. Padovan 3318f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->srej_q); 3319f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->busy_q); 332042e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 33210a708f8fSGustavo F. Padovan 3322525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_PBIT; 33230a708f8fSGustavo F. Padovan 3324525cd185SGustavo F. Padovan l2cap_send_srejframe(chan, tx_seq); 33250a708f8fSGustavo F. Padovan 3326e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 33270a708f8fSGustavo F. Padovan } 33280a708f8fSGustavo F. Padovan return 0; 33290a708f8fSGustavo F. Padovan 33300a708f8fSGustavo F. Padovan expected: 333142e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 33320a708f8fSGustavo F. Padovan 3333525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 33340a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 33350a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 3336f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 33370a708f8fSGustavo F. Padovan return 0; 33380a708f8fSGustavo F. Padovan } 33390a708f8fSGustavo F. Padovan 3340525cd185SGustavo F. Padovan err = l2cap_push_rx_skb(chan, skb, rx_control); 33410a708f8fSGustavo F. Padovan if (err < 0) 33420a708f8fSGustavo F. Padovan return 0; 33430a708f8fSGustavo F. Padovan 33440a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_FINAL) { 3345525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3346525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 33470a708f8fSGustavo F. Padovan else 3348525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 33490a708f8fSGustavo F. Padovan } 33500a708f8fSGustavo F. Padovan 33510a708f8fSGustavo F. Padovan __mod_ack_timer(); 33520a708f8fSGustavo F. Padovan 33536a026610SGustavo F. Padovan chan->num_acked = (chan->num_acked + 1) % num_to_ack; 33546a026610SGustavo F. Padovan if (chan->num_acked == num_to_ack - 1) 3355525cd185SGustavo F. Padovan l2cap_send_ack(chan); 33560a708f8fSGustavo F. Padovan 33570a708f8fSGustavo F. Padovan return 0; 33580a708f8fSGustavo F. Padovan 33590a708f8fSGustavo F. Padovan drop: 33600a708f8fSGustavo F. Padovan kfree_skb(skb); 33610a708f8fSGustavo F. Padovan return 0; 33620a708f8fSGustavo F. Padovan } 33630a708f8fSGustavo F. Padovan 3364525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control) 33650a708f8fSGustavo F. Padovan { 336649208c9cSGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, __get_reqseq(rx_control), 33670a708f8fSGustavo F. Padovan rx_control); 33680a708f8fSGustavo F. Padovan 336942e5c802SGustavo F. Padovan chan->expected_ack_seq = __get_reqseq(rx_control); 337042e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 33710a708f8fSGustavo F. Padovan 33720a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) { 3373525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 3374525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 3375525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && 33766a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 33770a708f8fSGustavo F. Padovan __mod_retrans_timer(); 33780a708f8fSGustavo F. Padovan 3379525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 3380525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 33810a708f8fSGustavo F. Padovan } else { 3382525cd185SGustavo F. Padovan l2cap_send_i_or_rr_or_rnr(chan); 33830a708f8fSGustavo F. Padovan } 33840a708f8fSGustavo F. Padovan 33850a708f8fSGustavo F. Padovan } else if (rx_control & L2CAP_CTRL_FINAL) { 3386525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 33870a708f8fSGustavo F. Padovan 3388525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3389525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 33900a708f8fSGustavo F. Padovan else 3391525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 33920a708f8fSGustavo F. Padovan 33930a708f8fSGustavo F. Padovan } else { 3394525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && 33956a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 33960a708f8fSGustavo F. Padovan __mod_retrans_timer(); 33970a708f8fSGustavo F. Padovan 3398525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 3399525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) 3400525cd185SGustavo F. Padovan l2cap_send_ack(chan); 34010a708f8fSGustavo F. Padovan else 3402525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 34030a708f8fSGustavo F. Padovan } 34040a708f8fSGustavo F. Padovan } 34050a708f8fSGustavo F. Padovan 3406525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control) 34070a708f8fSGustavo F. Padovan { 34080a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 34090a708f8fSGustavo F. Padovan 3410525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 34110a708f8fSGustavo F. Padovan 3412525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 34130a708f8fSGustavo F. Padovan 341442e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 341542e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 34160a708f8fSGustavo F. Padovan 34170a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_FINAL) { 3418525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3419525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 34200a708f8fSGustavo F. Padovan else 3421525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 34220a708f8fSGustavo F. Padovan } else { 3423525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 34240a708f8fSGustavo F. Padovan 3425525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) 3426525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_REJ_ACT; 34270a708f8fSGustavo F. Padovan } 34280a708f8fSGustavo F. Padovan } 3429525cd185SGustavo F. Padovan static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control) 34300a708f8fSGustavo F. Padovan { 34310a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 34320a708f8fSGustavo F. Padovan 3433525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 34340a708f8fSGustavo F. Padovan 3435525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 34360a708f8fSGustavo F. Padovan 34370a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) { 343842e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 343942e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 34400a708f8fSGustavo F. Padovan 3441525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 3442525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 34430a708f8fSGustavo F. Padovan 3444525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 34450a708f8fSGustavo F. Padovan 3446525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) { 34476a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 3448525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_ACT; 34490a708f8fSGustavo F. Padovan } 34500a708f8fSGustavo F. Padovan } else if (rx_control & L2CAP_CTRL_FINAL) { 3451525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_SREJ_ACT) && 34526a026610SGustavo F. Padovan chan->srej_save_reqseq == tx_seq) 3453525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SREJ_ACT; 34540a708f8fSGustavo F. Padovan else 3455525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 34560a708f8fSGustavo F. Padovan } else { 3457525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 3458525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) { 34596a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 3460525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_ACT; 34610a708f8fSGustavo F. Padovan } 34620a708f8fSGustavo F. Padovan } 34630a708f8fSGustavo F. Padovan } 34640a708f8fSGustavo F. Padovan 3465525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control) 34660a708f8fSGustavo F. Padovan { 34670a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 34680a708f8fSGustavo F. Padovan 3469525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 34700a708f8fSGustavo F. Padovan 3471525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_REMOTE_BUSY; 347242e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 347342e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 34740a708f8fSGustavo F. Padovan 34750a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3476525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 34770a708f8fSGustavo F. Padovan 3478525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SREJ_SENT)) { 3479e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 34800a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3481525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); 34820a708f8fSGustavo F. Padovan return; 34830a708f8fSGustavo F. Padovan } 34840a708f8fSGustavo F. Padovan 34850a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3486525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 34870a708f8fSGustavo F. Padovan else 3488525cd185SGustavo F. Padovan l2cap_send_sframe(chan, L2CAP_SUPER_RCV_READY); 34890a708f8fSGustavo F. Padovan } 34900a708f8fSGustavo F. Padovan 3491525cd185SGustavo F. Padovan static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) 34920a708f8fSGustavo F. Padovan { 3493525cd185SGustavo F. Padovan BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len); 34940a708f8fSGustavo F. Padovan 34950a708f8fSGustavo F. Padovan if (L2CAP_CTRL_FINAL & rx_control && 3496525cd185SGustavo F. Padovan chan->conn_state & L2CAP_CONN_WAIT_F) { 3497e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 34986a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 34990a708f8fSGustavo F. Padovan __mod_retrans_timer(); 3500525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_WAIT_F; 35010a708f8fSGustavo F. Padovan } 35020a708f8fSGustavo F. Padovan 35030a708f8fSGustavo F. Padovan switch (rx_control & L2CAP_CTRL_SUPERVISE) { 35040a708f8fSGustavo F. Padovan case L2CAP_SUPER_RCV_READY: 3505525cd185SGustavo F. Padovan l2cap_data_channel_rrframe(chan, rx_control); 35060a708f8fSGustavo F. Padovan break; 35070a708f8fSGustavo F. Padovan 35080a708f8fSGustavo F. Padovan case L2CAP_SUPER_REJECT: 3509525cd185SGustavo F. Padovan l2cap_data_channel_rejframe(chan, rx_control); 35100a708f8fSGustavo F. Padovan break; 35110a708f8fSGustavo F. Padovan 35120a708f8fSGustavo F. Padovan case L2CAP_SUPER_SELECT_REJECT: 3513525cd185SGustavo F. Padovan l2cap_data_channel_srejframe(chan, rx_control); 35140a708f8fSGustavo F. Padovan break; 35150a708f8fSGustavo F. Padovan 35160a708f8fSGustavo F. Padovan case L2CAP_SUPER_RCV_NOT_READY: 3517525cd185SGustavo F. Padovan l2cap_data_channel_rnrframe(chan, rx_control); 35180a708f8fSGustavo F. Padovan break; 35190a708f8fSGustavo F. Padovan } 35200a708f8fSGustavo F. Padovan 35210a708f8fSGustavo F. Padovan kfree_skb(skb); 35220a708f8fSGustavo F. Padovan return 0; 35230a708f8fSGustavo F. Padovan } 35240a708f8fSGustavo F. Padovan 35250a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb) 35260a708f8fSGustavo F. Padovan { 3527525cd185SGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 35280a708f8fSGustavo F. Padovan u16 control; 35290a708f8fSGustavo F. Padovan u8 req_seq; 35300a708f8fSGustavo F. Padovan int len, next_tx_seq_offset, req_seq_offset; 35310a708f8fSGustavo F. Padovan 35320a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data); 35330a708f8fSGustavo F. Padovan skb_pull(skb, 2); 35340a708f8fSGustavo F. Padovan len = skb->len; 35350a708f8fSGustavo F. Padovan 35360a708f8fSGustavo F. Padovan /* 35370a708f8fSGustavo F. Padovan * We can just drop the corrupted I-frame here. 35380a708f8fSGustavo F. Padovan * Receiver will miss it and start proper recovery 35390a708f8fSGustavo F. Padovan * procedures and ask retransmission. 35400a708f8fSGustavo F. Padovan */ 354147d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 35420a708f8fSGustavo F. Padovan goto drop; 35430a708f8fSGustavo F. Padovan 35440a708f8fSGustavo F. Padovan if (__is_sar_start(control) && __is_iframe(control)) 35450a708f8fSGustavo F. Padovan len -= 2; 35460a708f8fSGustavo F. Padovan 354747d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 35480a708f8fSGustavo F. Padovan len -= 2; 35490a708f8fSGustavo F. Padovan 355047d1ec61SGustavo F. Padovan if (len > chan->mps) { 35518c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 35520a708f8fSGustavo F. Padovan goto drop; 35530a708f8fSGustavo F. Padovan } 35540a708f8fSGustavo F. Padovan 35550a708f8fSGustavo F. Padovan req_seq = __get_reqseq(control); 355642e5c802SGustavo F. Padovan req_seq_offset = (req_seq - chan->expected_ack_seq) % 64; 35570a708f8fSGustavo F. Padovan if (req_seq_offset < 0) 35580a708f8fSGustavo F. Padovan req_seq_offset += 64; 35590a708f8fSGustavo F. Padovan 35600a708f8fSGustavo F. Padovan next_tx_seq_offset = 356142e5c802SGustavo F. Padovan (chan->next_tx_seq - chan->expected_ack_seq) % 64; 35620a708f8fSGustavo F. Padovan if (next_tx_seq_offset < 0) 35630a708f8fSGustavo F. Padovan next_tx_seq_offset += 64; 35640a708f8fSGustavo F. Padovan 35650a708f8fSGustavo F. Padovan /* check for invalid req-seq */ 35660a708f8fSGustavo F. Padovan if (req_seq_offset > next_tx_seq_offset) { 35678c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 35680a708f8fSGustavo F. Padovan goto drop; 35690a708f8fSGustavo F. Padovan } 35700a708f8fSGustavo F. Padovan 35710a708f8fSGustavo F. Padovan if (__is_iframe(control)) { 35720a708f8fSGustavo F. Padovan if (len < 0) { 35738c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 35740a708f8fSGustavo F. Padovan goto drop; 35750a708f8fSGustavo F. Padovan } 35760a708f8fSGustavo F. Padovan 3577525cd185SGustavo F. Padovan l2cap_data_channel_iframe(chan, control, skb); 35780a708f8fSGustavo F. Padovan } else { 35790a708f8fSGustavo F. Padovan if (len != 0) { 35800a708f8fSGustavo F. Padovan BT_ERR("%d", len); 35818c1d787bSGustavo F. Padovan l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); 35820a708f8fSGustavo F. Padovan goto drop; 35830a708f8fSGustavo F. Padovan } 35840a708f8fSGustavo F. Padovan 3585525cd185SGustavo F. Padovan l2cap_data_channel_sframe(chan, control, skb); 35860a708f8fSGustavo F. Padovan } 35870a708f8fSGustavo F. Padovan 35880a708f8fSGustavo F. Padovan return 0; 35890a708f8fSGustavo F. Padovan 35900a708f8fSGustavo F. Padovan drop: 35910a708f8fSGustavo F. Padovan kfree_skb(skb); 35920a708f8fSGustavo F. Padovan return 0; 35930a708f8fSGustavo F. Padovan } 35940a708f8fSGustavo F. Padovan 35950a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) 35960a708f8fSGustavo F. Padovan { 359748454079SGustavo F. Padovan struct l2cap_chan *chan; 35980a708f8fSGustavo F. Padovan struct sock *sk; 35990a708f8fSGustavo F. Padovan struct l2cap_pinfo *pi; 36000a708f8fSGustavo F. Padovan u16 control; 36010a708f8fSGustavo F. Padovan u8 tx_seq; 36020a708f8fSGustavo F. Padovan int len; 36030a708f8fSGustavo F. Padovan 3604baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 360548454079SGustavo F. Padovan if (!chan) { 36060a708f8fSGustavo F. Padovan BT_DBG("unknown cid 0x%4.4x", cid); 36070a708f8fSGustavo F. Padovan goto drop; 36080a708f8fSGustavo F. Padovan } 36090a708f8fSGustavo F. Padovan 361048454079SGustavo F. Padovan sk = chan->sk; 36110a708f8fSGustavo F. Padovan pi = l2cap_pi(sk); 36120a708f8fSGustavo F. Padovan 361349208c9cSGustavo F. Padovan BT_DBG("chan %p, len %d", chan, skb->len); 36140a708f8fSGustavo F. Padovan 36150a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 36160a708f8fSGustavo F. Padovan goto drop; 36170a708f8fSGustavo F. Padovan 36180c1bc5c6SGustavo F. Padovan switch (chan->mode) { 36190a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 36200a708f8fSGustavo F. Padovan /* If socket recv buffers overflows we drop data here 36210a708f8fSGustavo F. Padovan * which is *bad* because L2CAP has to be reliable. 36220a708f8fSGustavo F. Padovan * But we don't have any other choice. L2CAP doesn't 36230a708f8fSGustavo F. Padovan * provide flow control mechanism. */ 36240a708f8fSGustavo F. Padovan 36250c1bc5c6SGustavo F. Padovan if (chan->imtu < skb->len) 36260a708f8fSGustavo F. Padovan goto drop; 36270a708f8fSGustavo F. Padovan 36280a708f8fSGustavo F. Padovan if (!sock_queue_rcv_skb(sk, skb)) 36290a708f8fSGustavo F. Padovan goto done; 36300a708f8fSGustavo F. Padovan break; 36310a708f8fSGustavo F. Padovan 36320a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 36330a708f8fSGustavo F. Padovan if (!sock_owned_by_user(sk)) { 36340a708f8fSGustavo F. Padovan l2cap_ertm_data_rcv(sk, skb); 36350a708f8fSGustavo F. Padovan } else { 36360a708f8fSGustavo F. Padovan if (sk_add_backlog(sk, skb)) 36370a708f8fSGustavo F. Padovan goto drop; 36380a708f8fSGustavo F. Padovan } 36390a708f8fSGustavo F. Padovan 36400a708f8fSGustavo F. Padovan goto done; 36410a708f8fSGustavo F. Padovan 36420a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 36430a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data); 36440a708f8fSGustavo F. Padovan skb_pull(skb, 2); 36450a708f8fSGustavo F. Padovan len = skb->len; 36460a708f8fSGustavo F. Padovan 364747d1ec61SGustavo F. Padovan if (l2cap_check_fcs(chan, skb)) 36480a708f8fSGustavo F. Padovan goto drop; 36490a708f8fSGustavo F. Padovan 36500a708f8fSGustavo F. Padovan if (__is_sar_start(control)) 36510a708f8fSGustavo F. Padovan len -= 2; 36520a708f8fSGustavo F. Padovan 365347d1ec61SGustavo F. Padovan if (chan->fcs == L2CAP_FCS_CRC16) 36540a708f8fSGustavo F. Padovan len -= 2; 36550a708f8fSGustavo F. Padovan 365647d1ec61SGustavo F. Padovan if (len > chan->mps || len < 0 || __is_sframe(control)) 36570a708f8fSGustavo F. Padovan goto drop; 36580a708f8fSGustavo F. Padovan 36590a708f8fSGustavo F. Padovan tx_seq = __get_txseq(control); 36600a708f8fSGustavo F. Padovan 366142e5c802SGustavo F. Padovan if (chan->expected_tx_seq == tx_seq) 366242e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 36630a708f8fSGustavo F. Padovan else 366442e5c802SGustavo F. Padovan chan->expected_tx_seq = (tx_seq + 1) % 64; 36650a708f8fSGustavo F. Padovan 3666525cd185SGustavo F. Padovan l2cap_streaming_reassembly_sdu(chan, skb, control); 36670a708f8fSGustavo F. Padovan 36680a708f8fSGustavo F. Padovan goto done; 36690a708f8fSGustavo F. Padovan 36700a708f8fSGustavo F. Padovan default: 36710c1bc5c6SGustavo F. Padovan BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode); 36720a708f8fSGustavo F. Padovan break; 36730a708f8fSGustavo F. Padovan } 36740a708f8fSGustavo F. Padovan 36750a708f8fSGustavo F. Padovan drop: 36760a708f8fSGustavo F. Padovan kfree_skb(skb); 36770a708f8fSGustavo F. Padovan 36780a708f8fSGustavo F. Padovan done: 36790a708f8fSGustavo F. Padovan if (sk) 36800a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 36810a708f8fSGustavo F. Padovan 36820a708f8fSGustavo F. Padovan return 0; 36830a708f8fSGustavo F. Padovan } 36840a708f8fSGustavo F. Padovan 36850a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb) 36860a708f8fSGustavo F. Padovan { 36870a708f8fSGustavo F. Padovan struct sock *sk; 36880a708f8fSGustavo F. Padovan 36890a708f8fSGustavo F. Padovan sk = l2cap_get_sock_by_psm(0, psm, conn->src); 36900a708f8fSGustavo F. Padovan if (!sk) 36910a708f8fSGustavo F. Padovan goto drop; 36920a708f8fSGustavo F. Padovan 36930a708f8fSGustavo F. Padovan bh_lock_sock(sk); 36940a708f8fSGustavo F. Padovan 36950a708f8fSGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 36960a708f8fSGustavo F. Padovan 36970a708f8fSGustavo F. Padovan if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) 36980a708f8fSGustavo F. Padovan goto drop; 36990a708f8fSGustavo F. Padovan 37000c1bc5c6SGustavo F. Padovan if (l2cap_pi(sk)->chan->imtu < skb->len) 37010a708f8fSGustavo F. Padovan goto drop; 37020a708f8fSGustavo F. Padovan 37030a708f8fSGustavo F. Padovan if (!sock_queue_rcv_skb(sk, skb)) 37040a708f8fSGustavo F. Padovan goto done; 37050a708f8fSGustavo F. Padovan 37060a708f8fSGustavo F. Padovan drop: 37070a708f8fSGustavo F. Padovan kfree_skb(skb); 37080a708f8fSGustavo F. Padovan 37090a708f8fSGustavo F. Padovan done: 37100a708f8fSGustavo F. Padovan if (sk) 37110a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 37120a708f8fSGustavo F. Padovan return 0; 37130a708f8fSGustavo F. Padovan } 37140a708f8fSGustavo F. Padovan 37159f69bda6SGustavo F. Padovan static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb) 37169f69bda6SGustavo F. Padovan { 37179f69bda6SGustavo F. Padovan struct sock *sk; 37189f69bda6SGustavo F. Padovan 37199f69bda6SGustavo F. Padovan sk = l2cap_get_sock_by_scid(0, cid, conn->src); 37209f69bda6SGustavo F. Padovan if (!sk) 37219f69bda6SGustavo F. Padovan goto drop; 37229f69bda6SGustavo F. Padovan 37239f69bda6SGustavo F. Padovan bh_lock_sock(sk); 37249f69bda6SGustavo F. Padovan 37259f69bda6SGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 37269f69bda6SGustavo F. Padovan 37279f69bda6SGustavo F. Padovan if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) 37289f69bda6SGustavo F. Padovan goto drop; 37299f69bda6SGustavo F. Padovan 37300c1bc5c6SGustavo F. Padovan if (l2cap_pi(sk)->chan->imtu < skb->len) 37319f69bda6SGustavo F. Padovan goto drop; 37329f69bda6SGustavo F. Padovan 37339f69bda6SGustavo F. Padovan if (!sock_queue_rcv_skb(sk, skb)) 37349f69bda6SGustavo F. Padovan goto done; 37359f69bda6SGustavo F. Padovan 37369f69bda6SGustavo F. Padovan drop: 37379f69bda6SGustavo F. Padovan kfree_skb(skb); 37389f69bda6SGustavo F. Padovan 37399f69bda6SGustavo F. Padovan done: 37409f69bda6SGustavo F. Padovan if (sk) 37419f69bda6SGustavo F. Padovan bh_unlock_sock(sk); 37429f69bda6SGustavo F. Padovan return 0; 37439f69bda6SGustavo F. Padovan } 37449f69bda6SGustavo F. Padovan 37450a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) 37460a708f8fSGustavo F. Padovan { 37470a708f8fSGustavo F. Padovan struct l2cap_hdr *lh = (void *) skb->data; 37480a708f8fSGustavo F. Padovan u16 cid, len; 37490a708f8fSGustavo F. Padovan __le16 psm; 37500a708f8fSGustavo F. Padovan 37510a708f8fSGustavo F. Padovan skb_pull(skb, L2CAP_HDR_SIZE); 37520a708f8fSGustavo F. Padovan cid = __le16_to_cpu(lh->cid); 37530a708f8fSGustavo F. Padovan len = __le16_to_cpu(lh->len); 37540a708f8fSGustavo F. Padovan 37550a708f8fSGustavo F. Padovan if (len != skb->len) { 37560a708f8fSGustavo F. Padovan kfree_skb(skb); 37570a708f8fSGustavo F. Padovan return; 37580a708f8fSGustavo F. Padovan } 37590a708f8fSGustavo F. Padovan 37600a708f8fSGustavo F. Padovan BT_DBG("len %d, cid 0x%4.4x", len, cid); 37610a708f8fSGustavo F. Padovan 37620a708f8fSGustavo F. Padovan switch (cid) { 37633300d9a9SClaudio Takahasi case L2CAP_CID_LE_SIGNALING: 37640a708f8fSGustavo F. Padovan case L2CAP_CID_SIGNALING: 37650a708f8fSGustavo F. Padovan l2cap_sig_channel(conn, skb); 37660a708f8fSGustavo F. Padovan break; 37670a708f8fSGustavo F. Padovan 37680a708f8fSGustavo F. Padovan case L2CAP_CID_CONN_LESS: 37690a708f8fSGustavo F. Padovan psm = get_unaligned_le16(skb->data); 37700a708f8fSGustavo F. Padovan skb_pull(skb, 2); 37710a708f8fSGustavo F. Padovan l2cap_conless_channel(conn, psm, skb); 37720a708f8fSGustavo F. Padovan break; 37730a708f8fSGustavo F. Padovan 37749f69bda6SGustavo F. Padovan case L2CAP_CID_LE_DATA: 37759f69bda6SGustavo F. Padovan l2cap_att_channel(conn, cid, skb); 37769f69bda6SGustavo F. Padovan break; 37779f69bda6SGustavo F. Padovan 37780a708f8fSGustavo F. Padovan default: 37790a708f8fSGustavo F. Padovan l2cap_data_channel(conn, cid, skb); 37800a708f8fSGustavo F. Padovan break; 37810a708f8fSGustavo F. Padovan } 37820a708f8fSGustavo F. Padovan } 37830a708f8fSGustavo F. Padovan 37840a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */ 37850a708f8fSGustavo F. Padovan 37860a708f8fSGustavo F. Padovan static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) 37870a708f8fSGustavo F. Padovan { 37880a708f8fSGustavo F. Padovan int exact = 0, lm1 = 0, lm2 = 0; 37890a708f8fSGustavo F. Padovan register struct sock *sk; 37900a708f8fSGustavo F. Padovan struct hlist_node *node; 37910a708f8fSGustavo F. Padovan 37920a708f8fSGustavo F. Padovan if (type != ACL_LINK) 37930a708f8fSGustavo F. Padovan return -EINVAL; 37940a708f8fSGustavo F. Padovan 37950a708f8fSGustavo F. Padovan BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); 37960a708f8fSGustavo F. Padovan 37970a708f8fSGustavo F. Padovan /* Find listening sockets and check their link_mode */ 37980a708f8fSGustavo F. Padovan read_lock(&l2cap_sk_list.lock); 37990a708f8fSGustavo F. Padovan sk_for_each(sk, node, &l2cap_sk_list.head) { 38004343478fSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 38014343478fSGustavo F. Padovan 38020a708f8fSGustavo F. Padovan if (sk->sk_state != BT_LISTEN) 38030a708f8fSGustavo F. Padovan continue; 38040a708f8fSGustavo F. Padovan 38050a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { 38060a708f8fSGustavo F. Padovan lm1 |= HCI_LM_ACCEPT; 38074343478fSGustavo F. Padovan if (chan->role_switch) 38080a708f8fSGustavo F. Padovan lm1 |= HCI_LM_MASTER; 38090a708f8fSGustavo F. Padovan exact++; 38100a708f8fSGustavo F. Padovan } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { 38110a708f8fSGustavo F. Padovan lm2 |= HCI_LM_ACCEPT; 38124343478fSGustavo F. Padovan if (chan->role_switch) 38130a708f8fSGustavo F. Padovan lm2 |= HCI_LM_MASTER; 38140a708f8fSGustavo F. Padovan } 38150a708f8fSGustavo F. Padovan } 38160a708f8fSGustavo F. Padovan read_unlock(&l2cap_sk_list.lock); 38170a708f8fSGustavo F. Padovan 38180a708f8fSGustavo F. Padovan return exact ? lm1 : lm2; 38190a708f8fSGustavo F. Padovan } 38200a708f8fSGustavo F. Padovan 38210a708f8fSGustavo F. Padovan static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) 38220a708f8fSGustavo F. Padovan { 38230a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 38240a708f8fSGustavo F. Padovan 38250a708f8fSGustavo F. Padovan BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); 38260a708f8fSGustavo F. Padovan 3827acd7d370SVille Tervo if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) 38280a708f8fSGustavo F. Padovan return -EINVAL; 38290a708f8fSGustavo F. Padovan 38300a708f8fSGustavo F. Padovan if (!status) { 38310a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, status); 38320a708f8fSGustavo F. Padovan if (conn) 38330a708f8fSGustavo F. Padovan l2cap_conn_ready(conn); 38340a708f8fSGustavo F. Padovan } else 38350a708f8fSGustavo F. Padovan l2cap_conn_del(hcon, bt_err(status)); 38360a708f8fSGustavo F. Padovan 38370a708f8fSGustavo F. Padovan return 0; 38380a708f8fSGustavo F. Padovan } 38390a708f8fSGustavo F. Padovan 38400a708f8fSGustavo F. Padovan static int l2cap_disconn_ind(struct hci_conn *hcon) 38410a708f8fSGustavo F. Padovan { 38420a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 38430a708f8fSGustavo F. Padovan 38440a708f8fSGustavo F. Padovan BT_DBG("hcon %p", hcon); 38450a708f8fSGustavo F. Padovan 38460a708f8fSGustavo F. Padovan if (hcon->type != ACL_LINK || !conn) 38470a708f8fSGustavo F. Padovan return 0x13; 38480a708f8fSGustavo F. Padovan 38490a708f8fSGustavo F. Padovan return conn->disc_reason; 38500a708f8fSGustavo F. Padovan } 38510a708f8fSGustavo F. Padovan 38520a708f8fSGustavo F. Padovan static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) 38530a708f8fSGustavo F. Padovan { 38540a708f8fSGustavo F. Padovan BT_DBG("hcon %p reason %d", hcon, reason); 38550a708f8fSGustavo F. Padovan 3856acd7d370SVille Tervo if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) 38570a708f8fSGustavo F. Padovan return -EINVAL; 38580a708f8fSGustavo F. Padovan 38590a708f8fSGustavo F. Padovan l2cap_conn_del(hcon, bt_err(reason)); 38600a708f8fSGustavo F. Padovan 38610a708f8fSGustavo F. Padovan return 0; 38620a708f8fSGustavo F. Padovan } 38630a708f8fSGustavo F. Padovan 38644343478fSGustavo F. Padovan static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) 38650a708f8fSGustavo F. Padovan { 38664343478fSGustavo F. Padovan struct sock *sk = chan->sk; 38674343478fSGustavo F. Padovan 38680a708f8fSGustavo F. Padovan if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM) 38690a708f8fSGustavo F. Padovan return; 38700a708f8fSGustavo F. Padovan 38710a708f8fSGustavo F. Padovan if (encrypt == 0x00) { 38724343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) { 38730a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 38740a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, HZ * 5); 38754343478fSGustavo F. Padovan } else if (chan->sec_level == BT_SECURITY_HIGH) 38760a708f8fSGustavo F. Padovan __l2cap_sock_close(sk, ECONNREFUSED); 38770a708f8fSGustavo F. Padovan } else { 38784343478fSGustavo F. Padovan if (chan->sec_level == BT_SECURITY_MEDIUM) 38790a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 38800a708f8fSGustavo F. Padovan } 38810a708f8fSGustavo F. Padovan } 38820a708f8fSGustavo F. Padovan 38830a708f8fSGustavo F. Padovan static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) 38840a708f8fSGustavo F. Padovan { 38850a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 388648454079SGustavo F. Padovan struct l2cap_chan *chan; 38870a708f8fSGustavo F. Padovan 38880a708f8fSGustavo F. Padovan if (!conn) 38890a708f8fSGustavo F. Padovan return 0; 38900a708f8fSGustavo F. Padovan 38910a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 38920a708f8fSGustavo F. Padovan 3893baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 38940a708f8fSGustavo F. Padovan 3895baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 389648454079SGustavo F. Padovan struct sock *sk = chan->sk; 3897baa7e1faSGustavo F. Padovan 38980a708f8fSGustavo F. Padovan bh_lock_sock(sk); 38990a708f8fSGustavo F. Padovan 3900b4450035SGustavo F. Padovan if (chan->conf_state & L2CAP_CONF_CONNECT_PEND) { 39010a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 39020a708f8fSGustavo F. Padovan continue; 39030a708f8fSGustavo F. Padovan } 39040a708f8fSGustavo F. Padovan 39050a708f8fSGustavo F. Padovan if (!status && (sk->sk_state == BT_CONNECTED || 39060a708f8fSGustavo F. Padovan sk->sk_state == BT_CONFIG)) { 39074343478fSGustavo F. Padovan l2cap_check_encryption(chan, encrypt); 39080a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 39090a708f8fSGustavo F. Padovan continue; 39100a708f8fSGustavo F. Padovan } 39110a708f8fSGustavo F. Padovan 39120a708f8fSGustavo F. Padovan if (sk->sk_state == BT_CONNECT) { 39130a708f8fSGustavo F. Padovan if (!status) { 39140a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 3915fe4128e0SGustavo F. Padovan req.scid = cpu_to_le16(chan->scid); 3916fe4128e0SGustavo F. Padovan req.psm = chan->psm; 39170a708f8fSGustavo F. Padovan 3918fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 3919b4450035SGustavo F. Padovan chan->conf_state |= L2CAP_CONF_CONNECT_PEND; 39200a708f8fSGustavo F. Padovan 3921fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 39220a708f8fSGustavo F. Padovan L2CAP_CONN_REQ, sizeof(req), &req); 39230a708f8fSGustavo F. Padovan } else { 39240a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 39250a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, HZ / 10); 39260a708f8fSGustavo F. Padovan } 39270a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT2) { 39280a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 39290a708f8fSGustavo F. Padovan __u16 result; 39300a708f8fSGustavo F. Padovan 39310a708f8fSGustavo F. Padovan if (!status) { 39320a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 39330a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 39340a708f8fSGustavo F. Padovan } else { 39350a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 39360a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, HZ / 10); 39370a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 39380a708f8fSGustavo F. Padovan } 39390a708f8fSGustavo F. Padovan 3940fe4128e0SGustavo F. Padovan rsp.scid = cpu_to_le16(chan->dcid); 3941fe4128e0SGustavo F. Padovan rsp.dcid = cpu_to_le16(chan->scid); 39420a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 39430a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 3944fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 3945fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 39460a708f8fSGustavo F. Padovan } 39470a708f8fSGustavo F. Padovan 39480a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 39490a708f8fSGustavo F. Padovan } 39500a708f8fSGustavo F. Padovan 3951baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 39520a708f8fSGustavo F. Padovan 39530a708f8fSGustavo F. Padovan return 0; 39540a708f8fSGustavo F. Padovan } 39550a708f8fSGustavo F. Padovan 39560a708f8fSGustavo F. Padovan static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) 39570a708f8fSGustavo F. Padovan { 39580a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 39590a708f8fSGustavo F. Padovan 39600a708f8fSGustavo F. Padovan if (!conn) 39610a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 39620a708f8fSGustavo F. Padovan 39630a708f8fSGustavo F. Padovan if (!conn) 39640a708f8fSGustavo F. Padovan goto drop; 39650a708f8fSGustavo F. Padovan 39660a708f8fSGustavo F. Padovan BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); 39670a708f8fSGustavo F. Padovan 39680a708f8fSGustavo F. Padovan if (!(flags & ACL_CONT)) { 39690a708f8fSGustavo F. Padovan struct l2cap_hdr *hdr; 397048454079SGustavo F. Padovan struct l2cap_chan *chan; 39710a708f8fSGustavo F. Padovan u16 cid; 39720a708f8fSGustavo F. Padovan int len; 39730a708f8fSGustavo F. Padovan 39740a708f8fSGustavo F. Padovan if (conn->rx_len) { 39750a708f8fSGustavo F. Padovan BT_ERR("Unexpected start frame (len %d)", skb->len); 39760a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 39770a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 39780a708f8fSGustavo F. Padovan conn->rx_len = 0; 39790a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 39800a708f8fSGustavo F. Padovan } 39810a708f8fSGustavo F. Padovan 39820a708f8fSGustavo F. Padovan /* Start fragment always begin with Basic L2CAP header */ 39830a708f8fSGustavo F. Padovan if (skb->len < L2CAP_HDR_SIZE) { 39840a708f8fSGustavo F. Padovan BT_ERR("Frame is too short (len %d)", skb->len); 39850a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 39860a708f8fSGustavo F. Padovan goto drop; 39870a708f8fSGustavo F. Padovan } 39880a708f8fSGustavo F. Padovan 39890a708f8fSGustavo F. Padovan hdr = (struct l2cap_hdr *) skb->data; 39900a708f8fSGustavo F. Padovan len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; 39910a708f8fSGustavo F. Padovan cid = __le16_to_cpu(hdr->cid); 39920a708f8fSGustavo F. Padovan 39930a708f8fSGustavo F. Padovan if (len == skb->len) { 39940a708f8fSGustavo F. Padovan /* Complete frame received */ 39950a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, skb); 39960a708f8fSGustavo F. Padovan return 0; 39970a708f8fSGustavo F. Padovan } 39980a708f8fSGustavo F. Padovan 39990a708f8fSGustavo F. Padovan BT_DBG("Start: total len %d, frag len %d", len, skb->len); 40000a708f8fSGustavo F. Padovan 40010a708f8fSGustavo F. Padovan if (skb->len > len) { 40020a708f8fSGustavo F. Padovan BT_ERR("Frame is too long (len %d, expected len %d)", 40030a708f8fSGustavo F. Padovan skb->len, len); 40040a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 40050a708f8fSGustavo F. Padovan goto drop; 40060a708f8fSGustavo F. Padovan } 40070a708f8fSGustavo F. Padovan 4008baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 40090a708f8fSGustavo F. Padovan 401048454079SGustavo F. Padovan if (chan && chan->sk) { 401148454079SGustavo F. Padovan struct sock *sk = chan->sk; 401248454079SGustavo F. Padovan 40130c1bc5c6SGustavo F. Padovan if (chan->imtu < len - L2CAP_HDR_SIZE) { 401448454079SGustavo F. Padovan BT_ERR("Frame exceeding recv MTU (len %d, " 401548454079SGustavo F. Padovan "MTU %d)", len, 40160c1bc5c6SGustavo F. Padovan chan->imtu); 40170a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 40180a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 40190a708f8fSGustavo F. Padovan goto drop; 40200a708f8fSGustavo F. Padovan } 40210a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 402248454079SGustavo F. Padovan } 40230a708f8fSGustavo F. Padovan 40240a708f8fSGustavo F. Padovan /* Allocate skb for the complete frame (with header) */ 40250a708f8fSGustavo F. Padovan conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); 40260a708f8fSGustavo F. Padovan if (!conn->rx_skb) 40270a708f8fSGustavo F. Padovan goto drop; 40280a708f8fSGustavo F. Padovan 40290a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 40300a708f8fSGustavo F. Padovan skb->len); 40310a708f8fSGustavo F. Padovan conn->rx_len = len - skb->len; 40320a708f8fSGustavo F. Padovan } else { 40330a708f8fSGustavo F. Padovan BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); 40340a708f8fSGustavo F. Padovan 40350a708f8fSGustavo F. Padovan if (!conn->rx_len) { 40360a708f8fSGustavo F. Padovan BT_ERR("Unexpected continuation frame (len %d)", skb->len); 40370a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 40380a708f8fSGustavo F. Padovan goto drop; 40390a708f8fSGustavo F. Padovan } 40400a708f8fSGustavo F. Padovan 40410a708f8fSGustavo F. Padovan if (skb->len > conn->rx_len) { 40420a708f8fSGustavo F. Padovan BT_ERR("Fragment is too long (len %d, expected %d)", 40430a708f8fSGustavo F. Padovan skb->len, conn->rx_len); 40440a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 40450a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 40460a708f8fSGustavo F. Padovan conn->rx_len = 0; 40470a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 40480a708f8fSGustavo F. Padovan goto drop; 40490a708f8fSGustavo F. Padovan } 40500a708f8fSGustavo F. Padovan 40510a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 40520a708f8fSGustavo F. Padovan skb->len); 40530a708f8fSGustavo F. Padovan conn->rx_len -= skb->len; 40540a708f8fSGustavo F. Padovan 40550a708f8fSGustavo F. Padovan if (!conn->rx_len) { 40560a708f8fSGustavo F. Padovan /* Complete frame received */ 40570a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, conn->rx_skb); 40580a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 40590a708f8fSGustavo F. Padovan } 40600a708f8fSGustavo F. Padovan } 40610a708f8fSGustavo F. Padovan 40620a708f8fSGustavo F. Padovan drop: 40630a708f8fSGustavo F. Padovan kfree_skb(skb); 40640a708f8fSGustavo F. Padovan return 0; 40650a708f8fSGustavo F. Padovan } 40660a708f8fSGustavo F. Padovan 40670a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p) 40680a708f8fSGustavo F. Padovan { 40690a708f8fSGustavo F. Padovan struct sock *sk; 40700a708f8fSGustavo F. Padovan struct hlist_node *node; 40710a708f8fSGustavo F. Padovan 40720a708f8fSGustavo F. Padovan read_lock_bh(&l2cap_sk_list.lock); 40730a708f8fSGustavo F. Padovan 40740a708f8fSGustavo F. Padovan sk_for_each(sk, node, &l2cap_sk_list.head) { 40750a708f8fSGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(sk); 40760c1bc5c6SGustavo F. Padovan struct l2cap_chan *chan = pi->chan; 40770a708f8fSGustavo F. Padovan 4078903d343eSGustavo F. Padovan seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", 40790a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->src), 40800a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->dst), 4081fe4128e0SGustavo F. Padovan sk->sk_state, __le16_to_cpu(chan->psm), 4082fe4128e0SGustavo F. Padovan chan->scid, chan->dcid, 40830c1bc5c6SGustavo F. Padovan chan->imtu, chan->omtu, chan->sec_level, 40840c1bc5c6SGustavo F. Padovan chan->mode); 40850a708f8fSGustavo F. Padovan } 40860a708f8fSGustavo F. Padovan 40870a708f8fSGustavo F. Padovan read_unlock_bh(&l2cap_sk_list.lock); 40880a708f8fSGustavo F. Padovan 40890a708f8fSGustavo F. Padovan return 0; 40900a708f8fSGustavo F. Padovan } 40910a708f8fSGustavo F. Padovan 40920a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file) 40930a708f8fSGustavo F. Padovan { 40940a708f8fSGustavo F. Padovan return single_open(file, l2cap_debugfs_show, inode->i_private); 40950a708f8fSGustavo F. Padovan } 40960a708f8fSGustavo F. Padovan 40970a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = { 40980a708f8fSGustavo F. Padovan .open = l2cap_debugfs_open, 40990a708f8fSGustavo F. Padovan .read = seq_read, 41000a708f8fSGustavo F. Padovan .llseek = seq_lseek, 41010a708f8fSGustavo F. Padovan .release = single_release, 41020a708f8fSGustavo F. Padovan }; 41030a708f8fSGustavo F. Padovan 41040a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs; 41050a708f8fSGustavo F. Padovan 41060a708f8fSGustavo F. Padovan static struct hci_proto l2cap_hci_proto = { 41070a708f8fSGustavo F. Padovan .name = "L2CAP", 41080a708f8fSGustavo F. Padovan .id = HCI_PROTO_L2CAP, 41090a708f8fSGustavo F. Padovan .connect_ind = l2cap_connect_ind, 41100a708f8fSGustavo F. Padovan .connect_cfm = l2cap_connect_cfm, 41110a708f8fSGustavo F. Padovan .disconn_ind = l2cap_disconn_ind, 41120a708f8fSGustavo F. Padovan .disconn_cfm = l2cap_disconn_cfm, 41130a708f8fSGustavo F. Padovan .security_cfm = l2cap_security_cfm, 41140a708f8fSGustavo F. Padovan .recv_acldata = l2cap_recv_acldata 41150a708f8fSGustavo F. Padovan }; 41160a708f8fSGustavo F. Padovan 411764274518SGustavo F. Padovan int __init l2cap_init(void) 41180a708f8fSGustavo F. Padovan { 41190a708f8fSGustavo F. Padovan int err; 41200a708f8fSGustavo F. Padovan 4121bb58f747SGustavo F. Padovan err = l2cap_init_sockets(); 41220a708f8fSGustavo F. Padovan if (err < 0) 41230a708f8fSGustavo F. Padovan return err; 41240a708f8fSGustavo F. Padovan 41250a708f8fSGustavo F. Padovan _busy_wq = create_singlethread_workqueue("l2cap"); 41260a708f8fSGustavo F. Padovan if (!_busy_wq) { 4127bb58f747SGustavo F. Padovan err = -ENOMEM; 41280a708f8fSGustavo F. Padovan goto error; 41290a708f8fSGustavo F. Padovan } 41300a708f8fSGustavo F. Padovan 41310a708f8fSGustavo F. Padovan err = hci_register_proto(&l2cap_hci_proto); 41320a708f8fSGustavo F. Padovan if (err < 0) { 41330a708f8fSGustavo F. Padovan BT_ERR("L2CAP protocol registration failed"); 41340a708f8fSGustavo F. Padovan bt_sock_unregister(BTPROTO_L2CAP); 41350a708f8fSGustavo F. Padovan goto error; 41360a708f8fSGustavo F. Padovan } 41370a708f8fSGustavo F. Padovan 41380a708f8fSGustavo F. Padovan if (bt_debugfs) { 41390a708f8fSGustavo F. Padovan l2cap_debugfs = debugfs_create_file("l2cap", 0444, 41400a708f8fSGustavo F. Padovan bt_debugfs, NULL, &l2cap_debugfs_fops); 41410a708f8fSGustavo F. Padovan if (!l2cap_debugfs) 41420a708f8fSGustavo F. Padovan BT_ERR("Failed to create L2CAP debug file"); 41430a708f8fSGustavo F. Padovan } 41440a708f8fSGustavo F. Padovan 41450a708f8fSGustavo F. Padovan return 0; 41460a708f8fSGustavo F. Padovan 41470a708f8fSGustavo F. Padovan error: 41480a708f8fSGustavo F. Padovan destroy_workqueue(_busy_wq); 4149bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 41500a708f8fSGustavo F. Padovan return err; 41510a708f8fSGustavo F. Padovan } 41520a708f8fSGustavo F. Padovan 415364274518SGustavo F. Padovan void l2cap_exit(void) 41540a708f8fSGustavo F. Padovan { 41550a708f8fSGustavo F. Padovan debugfs_remove(l2cap_debugfs); 41560a708f8fSGustavo F. Padovan 41570a708f8fSGustavo F. Padovan flush_workqueue(_busy_wq); 41580a708f8fSGustavo F. Padovan destroy_workqueue(_busy_wq); 41590a708f8fSGustavo F. Padovan 41600a708f8fSGustavo F. Padovan if (hci_unregister_proto(&l2cap_hci_proto) < 0) 41610a708f8fSGustavo F. Padovan BT_ERR("L2CAP protocol unregistration failed"); 41620a708f8fSGustavo F. Padovan 4163bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 41640a708f8fSGustavo F. Padovan } 41650a708f8fSGustavo F. Padovan 41660a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644); 41670a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); 4168