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) { 83baa7e1faSGustavo F. Padovan struct sock *s = c->sk; 84baa7e1faSGustavo F. Padovan if (l2cap_pi(s)->dcid == cid) 8548454079SGustavo F. Padovan return c; 860a708f8fSGustavo F. Padovan } 87baa7e1faSGustavo F. Padovan return NULL; 88baa7e1faSGustavo F. Padovan 89baa7e1faSGustavo F. Padovan } 900a708f8fSGustavo F. Padovan 91baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) 920a708f8fSGustavo F. Padovan { 9348454079SGustavo F. Padovan struct l2cap_chan *c; 94baa7e1faSGustavo F. Padovan 95baa7e1faSGustavo F. Padovan list_for_each_entry(c, &conn->chan_l, list) { 96baa7e1faSGustavo F. Padovan struct sock *s = c->sk; 97baa7e1faSGustavo F. Padovan if (l2cap_pi(s)->scid == cid) 9848454079SGustavo F. Padovan return c; 990a708f8fSGustavo F. Padovan } 100baa7e1faSGustavo F. Padovan return NULL; 101baa7e1faSGustavo F. Padovan } 1020a708f8fSGustavo F. Padovan 1030a708f8fSGustavo F. Padovan /* Find channel with given SCID. 1040a708f8fSGustavo F. Padovan * Returns locked socket */ 105baa7e1faSGustavo F. Padovan static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) 1060a708f8fSGustavo F. Padovan { 10748454079SGustavo F. Padovan struct l2cap_chan *c; 108baa7e1faSGustavo F. Padovan 109baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 110baa7e1faSGustavo F. Padovan c = __l2cap_get_chan_by_scid(conn, cid); 11148454079SGustavo F. Padovan if (c) 11248454079SGustavo F. Padovan bh_lock_sock(c->sk); 113baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 11448454079SGustavo F. Padovan return c; 1150a708f8fSGustavo F. Padovan } 1160a708f8fSGustavo F. Padovan 117baa7e1faSGustavo F. Padovan static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) 1180a708f8fSGustavo F. Padovan { 11948454079SGustavo F. Padovan struct l2cap_chan *c; 120baa7e1faSGustavo F. Padovan 121baa7e1faSGustavo F. Padovan list_for_each_entry(c, &conn->chan_l, list) { 122fc7f8a7eSGustavo F. Padovan if (c->ident == ident) 12348454079SGustavo F. Padovan return c; 1240a708f8fSGustavo F. Padovan } 125baa7e1faSGustavo F. Padovan return NULL; 126baa7e1faSGustavo F. Padovan } 1270a708f8fSGustavo F. Padovan 128baa7e1faSGustavo F. Padovan static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) 1290a708f8fSGustavo F. Padovan { 13048454079SGustavo F. Padovan struct l2cap_chan *c; 131baa7e1faSGustavo F. Padovan 132baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 133baa7e1faSGustavo F. Padovan c = __l2cap_get_chan_by_ident(conn, ident); 13448454079SGustavo F. Padovan if (c) 13548454079SGustavo F. Padovan bh_lock_sock(c->sk); 136baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 13748454079SGustavo F. Padovan return c; 1380a708f8fSGustavo F. Padovan } 1390a708f8fSGustavo F. Padovan 140baa7e1faSGustavo F. Padovan static u16 l2cap_alloc_cid(struct l2cap_conn *conn) 1410a708f8fSGustavo F. Padovan { 1420a708f8fSGustavo F. Padovan u16 cid = L2CAP_CID_DYN_START; 1430a708f8fSGustavo F. Padovan 1440a708f8fSGustavo F. Padovan for (; cid < L2CAP_CID_DYN_END; cid++) { 145baa7e1faSGustavo F. Padovan if (!__l2cap_get_chan_by_scid(conn, cid)) 1460a708f8fSGustavo F. Padovan return cid; 1470a708f8fSGustavo F. Padovan } 1480a708f8fSGustavo F. Padovan 1490a708f8fSGustavo F. Padovan return 0; 1500a708f8fSGustavo F. Padovan } 1510a708f8fSGustavo F. Padovan 15248454079SGustavo F. Padovan static struct l2cap_chan *l2cap_chan_alloc(struct sock *sk) 1530a708f8fSGustavo F. Padovan { 15448454079SGustavo F. Padovan struct l2cap_chan *chan; 1550a708f8fSGustavo F. Padovan 15648454079SGustavo F. Padovan chan = kzalloc(sizeof(*chan), GFP_ATOMIC); 15748454079SGustavo F. Padovan if (!chan) 15848454079SGustavo F. Padovan return NULL; 1590a708f8fSGustavo F. Padovan 16048454079SGustavo F. Padovan chan->sk = sk; 16148454079SGustavo F. Padovan 16248454079SGustavo F. Padovan return chan; 1630a708f8fSGustavo F. Padovan } 1640a708f8fSGustavo F. Padovan 16548454079SGustavo F. Padovan static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 1660a708f8fSGustavo F. Padovan { 16748454079SGustavo F. Padovan struct sock *sk = chan->sk; 1680a708f8fSGustavo F. Padovan 1690a708f8fSGustavo F. Padovan BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, 1700a708f8fSGustavo F. Padovan l2cap_pi(sk)->psm, l2cap_pi(sk)->dcid); 1710a708f8fSGustavo F. Padovan 1720a708f8fSGustavo F. Padovan conn->disc_reason = 0x13; 1730a708f8fSGustavo F. Padovan 1740a708f8fSGustavo F. Padovan l2cap_pi(sk)->conn = conn; 1750a708f8fSGustavo F. Padovan 1760a708f8fSGustavo F. Padovan if (sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM) { 177b62f328bSVille Tervo if (conn->hcon->type == LE_LINK) { 178b62f328bSVille Tervo /* LE connection */ 179b62f328bSVille Tervo l2cap_pi(sk)->omtu = L2CAP_LE_DEFAULT_MTU; 180b62f328bSVille Tervo l2cap_pi(sk)->scid = L2CAP_CID_LE_DATA; 181b62f328bSVille Tervo l2cap_pi(sk)->dcid = L2CAP_CID_LE_DATA; 182b62f328bSVille Tervo } else { 1830a708f8fSGustavo F. Padovan /* Alloc CID for connection-oriented socket */ 184baa7e1faSGustavo F. Padovan l2cap_pi(sk)->scid = l2cap_alloc_cid(conn); 185b62f328bSVille Tervo l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU; 186b62f328bSVille Tervo } 1870a708f8fSGustavo F. Padovan } else if (sk->sk_type == SOCK_DGRAM) { 1880a708f8fSGustavo F. Padovan /* Connectionless socket */ 1890a708f8fSGustavo F. Padovan l2cap_pi(sk)->scid = L2CAP_CID_CONN_LESS; 1900a708f8fSGustavo F. Padovan l2cap_pi(sk)->dcid = L2CAP_CID_CONN_LESS; 1910a708f8fSGustavo F. Padovan l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU; 1920a708f8fSGustavo F. Padovan } else { 1930a708f8fSGustavo F. Padovan /* Raw socket can send/recv signalling messages only */ 1940a708f8fSGustavo F. Padovan l2cap_pi(sk)->scid = L2CAP_CID_SIGNALING; 1950a708f8fSGustavo F. Padovan l2cap_pi(sk)->dcid = L2CAP_CID_SIGNALING; 1960a708f8fSGustavo F. Padovan l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU; 1970a708f8fSGustavo F. Padovan } 1980a708f8fSGustavo F. Padovan 199baa7e1faSGustavo F. Padovan sock_hold(sk); 200baa7e1faSGustavo F. Padovan 201baa7e1faSGustavo F. Padovan list_add(&chan->list, &conn->chan_l); 2020a708f8fSGustavo F. Padovan } 2030a708f8fSGustavo F. Padovan 2040a708f8fSGustavo F. Padovan /* Delete channel. 2050a708f8fSGustavo F. Padovan * Must be called on the locked socket. */ 20648454079SGustavo F. Padovan void l2cap_chan_del(struct l2cap_chan *chan, int err) 2070a708f8fSGustavo F. Padovan { 20848454079SGustavo F. Padovan struct sock *sk = chan->sk; 2090a708f8fSGustavo F. Padovan struct l2cap_conn *conn = l2cap_pi(sk)->conn; 2100a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 2110a708f8fSGustavo F. Padovan 2120a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 2130a708f8fSGustavo F. Padovan 2140a708f8fSGustavo F. Padovan BT_DBG("sk %p, conn %p, err %d", sk, conn, err); 2150a708f8fSGustavo F. Padovan 2160a708f8fSGustavo F. Padovan if (conn) { 217baa7e1faSGustavo F. Padovan /* Delete from channel list */ 218baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 219baa7e1faSGustavo F. Padovan list_del(&chan->list); 220baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 221baa7e1faSGustavo F. Padovan __sock_put(sk); 222baa7e1faSGustavo F. Padovan 2230a708f8fSGustavo F. Padovan l2cap_pi(sk)->conn = NULL; 2240a708f8fSGustavo F. Padovan hci_conn_put(conn->hcon); 2250a708f8fSGustavo F. Padovan } 2260a708f8fSGustavo F. Padovan 2270a708f8fSGustavo F. Padovan sk->sk_state = BT_CLOSED; 2280a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 2290a708f8fSGustavo F. Padovan 2300a708f8fSGustavo F. Padovan if (err) 2310a708f8fSGustavo F. Padovan sk->sk_err = err; 2320a708f8fSGustavo F. Padovan 2330a708f8fSGustavo F. Padovan if (parent) { 2340a708f8fSGustavo F. Padovan bt_accept_unlink(sk); 2350a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 2360a708f8fSGustavo F. Padovan } else 2370a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 2380a708f8fSGustavo F. Padovan 239*2ead70b8SGustavo F. Padovan if (!(l2cap_pi(sk)->conf_state & L2CAP_CONF_OUTPUT_DONE && 240*2ead70b8SGustavo F. Padovan l2cap_pi(sk)->conf_state & L2CAP_CONF_INPUT_DONE)) 241*2ead70b8SGustavo F. Padovan goto free; 242*2ead70b8SGustavo F. Padovan 2430a708f8fSGustavo F. Padovan skb_queue_purge(TX_QUEUE(sk)); 2440a708f8fSGustavo F. Padovan 2450a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) { 2460a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 2470a708f8fSGustavo F. Padovan 248e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 249e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 250e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 2510a708f8fSGustavo F. Padovan 252f1c6775bSGustavo F. Padovan skb_queue_purge(&chan->srej_q); 253f1c6775bSGustavo F. Padovan skb_queue_purge(&chan->busy_q); 2540a708f8fSGustavo F. Padovan 2550a708f8fSGustavo F. Padovan list_for_each_entry_safe(l, tmp, SREJ_LIST(sk), list) { 2560a708f8fSGustavo F. Padovan list_del(&l->list); 2570a708f8fSGustavo F. Padovan kfree(l); 2580a708f8fSGustavo F. Padovan } 2590a708f8fSGustavo F. Padovan } 26048454079SGustavo F. Padovan 261*2ead70b8SGustavo F. Padovan free: 26248454079SGustavo F. Padovan kfree(chan); 2630a708f8fSGustavo F. Padovan } 2640a708f8fSGustavo F. Padovan 2650a708f8fSGustavo F. Padovan static inline u8 l2cap_get_auth_type(struct sock *sk) 2660a708f8fSGustavo F. Padovan { 2670a708f8fSGustavo F. Padovan if (sk->sk_type == SOCK_RAW) { 2680a708f8fSGustavo F. Padovan switch (l2cap_pi(sk)->sec_level) { 2690a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 2700a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING_MITM; 2710a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 2720a708f8fSGustavo F. Padovan return HCI_AT_DEDICATED_BONDING; 2730a708f8fSGustavo F. Padovan default: 2740a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 2750a708f8fSGustavo F. Padovan } 2760a708f8fSGustavo F. Padovan } else if (l2cap_pi(sk)->psm == cpu_to_le16(0x0001)) { 2770a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->sec_level == BT_SECURITY_LOW) 2780a708f8fSGustavo F. Padovan l2cap_pi(sk)->sec_level = BT_SECURITY_SDP; 2790a708f8fSGustavo F. Padovan 2800a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->sec_level == BT_SECURITY_HIGH) 2810a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING_MITM; 2820a708f8fSGustavo F. Padovan else 2830a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 2840a708f8fSGustavo F. Padovan } else { 2850a708f8fSGustavo F. Padovan switch (l2cap_pi(sk)->sec_level) { 2860a708f8fSGustavo F. Padovan case BT_SECURITY_HIGH: 2870a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING_MITM; 2880a708f8fSGustavo F. Padovan case BT_SECURITY_MEDIUM: 2890a708f8fSGustavo F. Padovan return HCI_AT_GENERAL_BONDING; 2900a708f8fSGustavo F. Padovan default: 2910a708f8fSGustavo F. Padovan return HCI_AT_NO_BONDING; 2920a708f8fSGustavo F. Padovan } 2930a708f8fSGustavo F. Padovan } 2940a708f8fSGustavo F. Padovan } 2950a708f8fSGustavo F. Padovan 2960a708f8fSGustavo F. Padovan /* Service level security */ 2970a708f8fSGustavo F. Padovan static inline int l2cap_check_security(struct sock *sk) 2980a708f8fSGustavo F. Padovan { 2990a708f8fSGustavo F. Padovan struct l2cap_conn *conn = l2cap_pi(sk)->conn; 3000a708f8fSGustavo F. Padovan __u8 auth_type; 3010a708f8fSGustavo F. Padovan 3020a708f8fSGustavo F. Padovan auth_type = l2cap_get_auth_type(sk); 3030a708f8fSGustavo F. Padovan 3040a708f8fSGustavo F. Padovan return hci_conn_security(conn->hcon, l2cap_pi(sk)->sec_level, 3050a708f8fSGustavo F. Padovan auth_type); 3060a708f8fSGustavo F. Padovan } 3070a708f8fSGustavo F. Padovan 30868983259SGustavo F. Padovan u8 l2cap_get_ident(struct l2cap_conn *conn) 3090a708f8fSGustavo F. Padovan { 3100a708f8fSGustavo F. Padovan u8 id; 3110a708f8fSGustavo F. Padovan 3120a708f8fSGustavo F. Padovan /* Get next available identificator. 3130a708f8fSGustavo F. Padovan * 1 - 128 are used by kernel. 3140a708f8fSGustavo F. Padovan * 129 - 199 are reserved. 3150a708f8fSGustavo F. Padovan * 200 - 254 are used by utilities like l2ping, etc. 3160a708f8fSGustavo F. Padovan */ 3170a708f8fSGustavo F. Padovan 3180a708f8fSGustavo F. Padovan spin_lock_bh(&conn->lock); 3190a708f8fSGustavo F. Padovan 3200a708f8fSGustavo F. Padovan if (++conn->tx_ident > 128) 3210a708f8fSGustavo F. Padovan conn->tx_ident = 1; 3220a708f8fSGustavo F. Padovan 3230a708f8fSGustavo F. Padovan id = conn->tx_ident; 3240a708f8fSGustavo F. Padovan 3250a708f8fSGustavo F. Padovan spin_unlock_bh(&conn->lock); 3260a708f8fSGustavo F. Padovan 3270a708f8fSGustavo F. Padovan return id; 3280a708f8fSGustavo F. Padovan } 3290a708f8fSGustavo F. Padovan 33068983259SGustavo F. Padovan void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data) 3310a708f8fSGustavo F. Padovan { 3320a708f8fSGustavo F. Padovan struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); 3330a708f8fSGustavo F. Padovan u8 flags; 3340a708f8fSGustavo F. Padovan 3350a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x", code); 3360a708f8fSGustavo F. Padovan 3370a708f8fSGustavo F. Padovan if (!skb) 3380a708f8fSGustavo F. Padovan return; 3390a708f8fSGustavo F. Padovan 3400a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 3410a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 3420a708f8fSGustavo F. Padovan else 3430a708f8fSGustavo F. Padovan flags = ACL_START; 3440a708f8fSGustavo F. Padovan 3450a708f8fSGustavo F. Padovan hci_send_acl(conn->hcon, skb, flags); 3460a708f8fSGustavo F. Padovan } 3470a708f8fSGustavo F. Padovan 348525cd185SGustavo F. Padovan static inline void l2cap_send_sframe(struct l2cap_chan *chan, u16 control) 3490a708f8fSGustavo F. Padovan { 3500a708f8fSGustavo F. Padovan struct sk_buff *skb; 3510a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 352525cd185SGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(chan->sk); 3530a708f8fSGustavo F. Padovan struct l2cap_conn *conn = pi->conn; 3540a708f8fSGustavo F. Padovan struct sock *sk = (struct sock *)pi; 3550a708f8fSGustavo F. Padovan int count, hlen = L2CAP_HDR_SIZE + 2; 3560a708f8fSGustavo F. Padovan u8 flags; 3570a708f8fSGustavo F. Padovan 3580a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 3590a708f8fSGustavo F. Padovan return; 3600a708f8fSGustavo F. Padovan 3610a708f8fSGustavo F. Padovan if (pi->fcs == L2CAP_FCS_CRC16) 3620a708f8fSGustavo F. Padovan hlen += 2; 3630a708f8fSGustavo F. Padovan 3640a708f8fSGustavo F. Padovan BT_DBG("pi %p, control 0x%2.2x", pi, control); 3650a708f8fSGustavo F. Padovan 3660a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, hlen); 3670a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FRAME_TYPE; 3680a708f8fSGustavo F. Padovan 369525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 3700a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 371525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 3720a708f8fSGustavo F. Padovan } 3730a708f8fSGustavo F. Padovan 374525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_PBIT) { 3750a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_POLL; 376525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_PBIT; 3770a708f8fSGustavo F. Padovan } 3780a708f8fSGustavo F. Padovan 3790a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 3800a708f8fSGustavo F. Padovan if (!skb) 3810a708f8fSGustavo F. Padovan return; 3820a708f8fSGustavo F. Padovan 3830a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 3840a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); 3850a708f8fSGustavo F. Padovan lh->cid = cpu_to_le16(pi->dcid); 3860a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb_put(skb, 2)); 3870a708f8fSGustavo F. Padovan 3880a708f8fSGustavo F. Padovan if (pi->fcs == L2CAP_FCS_CRC16) { 3890a708f8fSGustavo F. Padovan u16 fcs = crc16(0, (u8 *)lh, count - 2); 3900a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb_put(skb, 2)); 3910a708f8fSGustavo F. Padovan } 3920a708f8fSGustavo F. Padovan 3930a708f8fSGustavo F. Padovan if (lmp_no_flush_capable(conn->hcon->hdev)) 3940a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 3950a708f8fSGustavo F. Padovan else 3960a708f8fSGustavo F. Padovan flags = ACL_START; 3970a708f8fSGustavo F. Padovan 3980a708f8fSGustavo F. Padovan hci_send_acl(pi->conn->hcon, skb, flags); 3990a708f8fSGustavo F. Padovan } 4000a708f8fSGustavo F. Padovan 401525cd185SGustavo F. Padovan static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u16 control) 4020a708f8fSGustavo F. Padovan { 403525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 4040a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 405525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 4060a708f8fSGustavo F. Padovan } else 4070a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 4080a708f8fSGustavo F. Padovan 40942e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 4100a708f8fSGustavo F. Padovan 411525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 4120a708f8fSGustavo F. Padovan } 4130a708f8fSGustavo F. Padovan 4140a708f8fSGustavo F. Padovan static inline int __l2cap_no_conn_pending(struct sock *sk) 4150a708f8fSGustavo F. Padovan { 4160a708f8fSGustavo F. Padovan return !(l2cap_pi(sk)->conf_state & L2CAP_CONF_CONNECT_PEND); 4170a708f8fSGustavo F. Padovan } 4180a708f8fSGustavo F. Padovan 419fc7f8a7eSGustavo F. Padovan static void l2cap_do_start(struct l2cap_chan *chan) 4200a708f8fSGustavo F. Padovan { 421fc7f8a7eSGustavo F. Padovan struct sock *sk = chan->sk; 4220a708f8fSGustavo F. Padovan struct l2cap_conn *conn = l2cap_pi(sk)->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 4280a708f8fSGustavo F. Padovan if (l2cap_check_security(sk) && __l2cap_no_conn_pending(sk)) { 4290a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 4300a708f8fSGustavo F. Padovan req.scid = cpu_to_le16(l2cap_pi(sk)->scid); 4310a708f8fSGustavo F. Padovan req.psm = l2cap_pi(sk)->psm; 4320a708f8fSGustavo F. Padovan 433fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 4340a708f8fSGustavo F. Padovan l2cap_pi(sk)->conf_state |= L2CAP_CONF_CONNECT_PEND; 4350a708f8fSGustavo F. Padovan 436fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 437fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 4380a708f8fSGustavo F. Padovan } 4390a708f8fSGustavo F. Padovan } else { 4400a708f8fSGustavo F. Padovan struct l2cap_info_req req; 4410a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 4420a708f8fSGustavo F. Padovan 4430a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 4440a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 4450a708f8fSGustavo F. Padovan 4460a708f8fSGustavo F. Padovan mod_timer(&conn->info_timer, jiffies + 4470a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 4480a708f8fSGustavo F. Padovan 4490a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 4500a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 4510a708f8fSGustavo F. Padovan } 4520a708f8fSGustavo F. Padovan } 4530a708f8fSGustavo F. Padovan 4540a708f8fSGustavo F. Padovan static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) 4550a708f8fSGustavo F. Padovan { 4560a708f8fSGustavo F. Padovan u32 local_feat_mask = l2cap_feat_mask; 4570a708f8fSGustavo F. Padovan if (!disable_ertm) 4580a708f8fSGustavo F. Padovan local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; 4590a708f8fSGustavo F. Padovan 4600a708f8fSGustavo F. Padovan switch (mode) { 4610a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 4620a708f8fSGustavo F. Padovan return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; 4630a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 4640a708f8fSGustavo F. Padovan return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; 4650a708f8fSGustavo F. Padovan default: 4660a708f8fSGustavo F. Padovan return 0x00; 4670a708f8fSGustavo F. Padovan } 4680a708f8fSGustavo F. Padovan } 4690a708f8fSGustavo F. Padovan 470e92c8e70SGustavo F. Padovan void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err) 4710a708f8fSGustavo F. Padovan { 472e92c8e70SGustavo F. Padovan struct sock *sk; 4730a708f8fSGustavo F. Padovan struct l2cap_disconn_req req; 4740a708f8fSGustavo F. Padovan 4750a708f8fSGustavo F. Padovan if (!conn) 4760a708f8fSGustavo F. Padovan return; 4770a708f8fSGustavo F. Padovan 478e92c8e70SGustavo F. Padovan sk = chan->sk; 479e92c8e70SGustavo F. Padovan 4800a708f8fSGustavo F. Padovan skb_queue_purge(TX_QUEUE(sk)); 4810a708f8fSGustavo F. Padovan 4820a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) { 483e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 484e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 485e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 4860a708f8fSGustavo F. Padovan } 4870a708f8fSGustavo F. Padovan 4880a708f8fSGustavo F. Padovan req.dcid = cpu_to_le16(l2cap_pi(sk)->dcid); 4890a708f8fSGustavo F. Padovan req.scid = cpu_to_le16(l2cap_pi(sk)->scid); 4900a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 4910a708f8fSGustavo F. Padovan L2CAP_DISCONN_REQ, sizeof(req), &req); 4920a708f8fSGustavo F. Padovan 4930a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 4940a708f8fSGustavo F. Padovan sk->sk_err = err; 4950a708f8fSGustavo F. Padovan } 4960a708f8fSGustavo F. Padovan 4970a708f8fSGustavo F. Padovan /* ---- L2CAP connections ---- */ 4980a708f8fSGustavo F. Padovan static void l2cap_conn_start(struct l2cap_conn *conn) 4990a708f8fSGustavo F. Padovan { 500820ffdb3SGustavo F. Padovan struct l2cap_chan *chan, *tmp; 5010a708f8fSGustavo F. Padovan 5020a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 5030a708f8fSGustavo F. Padovan 504baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 5050a708f8fSGustavo F. Padovan 506820ffdb3SGustavo F. Padovan list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { 50748454079SGustavo F. Padovan struct sock *sk = chan->sk; 508baa7e1faSGustavo F. Padovan 5090a708f8fSGustavo F. Padovan bh_lock_sock(sk); 5100a708f8fSGustavo F. Padovan 5110a708f8fSGustavo F. Padovan if (sk->sk_type != SOCK_SEQPACKET && 5120a708f8fSGustavo F. Padovan sk->sk_type != SOCK_STREAM) { 5130a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 5140a708f8fSGustavo F. Padovan continue; 5150a708f8fSGustavo F. Padovan } 5160a708f8fSGustavo F. Padovan 5170a708f8fSGustavo F. Padovan if (sk->sk_state == BT_CONNECT) { 5180a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 5190a708f8fSGustavo F. Padovan 5200a708f8fSGustavo F. Padovan if (!l2cap_check_security(sk) || 5210a708f8fSGustavo F. Padovan !__l2cap_no_conn_pending(sk)) { 5220a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 5230a708f8fSGustavo F. Padovan continue; 5240a708f8fSGustavo F. Padovan } 5250a708f8fSGustavo F. Padovan 5260a708f8fSGustavo F. Padovan if (!l2cap_mode_supported(l2cap_pi(sk)->mode, 5270a708f8fSGustavo F. Padovan conn->feat_mask) 5280a708f8fSGustavo F. Padovan && l2cap_pi(sk)->conf_state & 5290a708f8fSGustavo F. Padovan L2CAP_CONF_STATE2_DEVICE) { 530820ffdb3SGustavo F. Padovan /* __l2cap_sock_close() calls list_del(chan) 531820ffdb3SGustavo F. Padovan * so release the lock */ 532820ffdb3SGustavo F. Padovan read_unlock_bh(&conn->chan_lock); 533820ffdb3SGustavo F. Padovan __l2cap_sock_close(sk, ECONNRESET); 534820ffdb3SGustavo F. Padovan read_lock_bh(&conn->chan_lock); 5350a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 5360a708f8fSGustavo F. Padovan continue; 5370a708f8fSGustavo F. Padovan } 5380a708f8fSGustavo F. Padovan 5390a708f8fSGustavo F. Padovan req.scid = cpu_to_le16(l2cap_pi(sk)->scid); 5400a708f8fSGustavo F. Padovan req.psm = l2cap_pi(sk)->psm; 5410a708f8fSGustavo F. Padovan 542fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 5430a708f8fSGustavo F. Padovan l2cap_pi(sk)->conf_state |= L2CAP_CONF_CONNECT_PEND; 5440a708f8fSGustavo F. Padovan 545fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, 546fc7f8a7eSGustavo F. Padovan sizeof(req), &req); 5470a708f8fSGustavo F. Padovan 5480a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT2) { 5490a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 5500a708f8fSGustavo F. Padovan char buf[128]; 5510a708f8fSGustavo F. Padovan rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid); 5520a708f8fSGustavo F. Padovan rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid); 5530a708f8fSGustavo F. Padovan 5540a708f8fSGustavo F. Padovan if (l2cap_check_security(sk)) { 5550a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 5560a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 5570a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 5580a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); 5590a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 5600a708f8fSGustavo F. Padovan 5610a708f8fSGustavo F. Padovan } else { 5620a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 5630a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 5640a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 5650a708f8fSGustavo F. Padovan } 5660a708f8fSGustavo F. Padovan } else { 5670a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_PEND); 5680a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); 5690a708f8fSGustavo F. Padovan } 5700a708f8fSGustavo F. Padovan 571fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 572fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 5730a708f8fSGustavo F. Padovan 5740a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT || 5750a708f8fSGustavo F. Padovan rsp.result != L2CAP_CR_SUCCESS) { 5760a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 5770a708f8fSGustavo F. Padovan continue; 5780a708f8fSGustavo F. Padovan } 5790a708f8fSGustavo F. Padovan 5800a708f8fSGustavo F. Padovan l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT; 5810a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 58273ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 58373ffa904SGustavo F. Padovan chan->num_conf_req++; 5840a708f8fSGustavo F. Padovan } 5850a708f8fSGustavo F. Padovan 5860a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 5870a708f8fSGustavo F. Padovan } 5880a708f8fSGustavo F. Padovan 589baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 5900a708f8fSGustavo F. Padovan } 5910a708f8fSGustavo F. Padovan 592b62f328bSVille Tervo /* Find socket with cid and source bdaddr. 593b62f328bSVille Tervo * Returns closest match, locked. 594b62f328bSVille Tervo */ 595b62f328bSVille Tervo static struct sock *l2cap_get_sock_by_scid(int state, __le16 cid, bdaddr_t *src) 596b62f328bSVille Tervo { 597b62f328bSVille Tervo struct sock *s, *sk = NULL, *sk1 = NULL; 598b62f328bSVille Tervo struct hlist_node *node; 599b62f328bSVille Tervo 600b62f328bSVille Tervo read_lock(&l2cap_sk_list.lock); 601b62f328bSVille Tervo 602b62f328bSVille Tervo sk_for_each(sk, node, &l2cap_sk_list.head) { 603b62f328bSVille Tervo if (state && sk->sk_state != state) 604b62f328bSVille Tervo continue; 605b62f328bSVille Tervo 606b62f328bSVille Tervo if (l2cap_pi(sk)->scid == cid) { 607b62f328bSVille Tervo /* Exact match. */ 608b62f328bSVille Tervo if (!bacmp(&bt_sk(sk)->src, src)) 609b62f328bSVille Tervo break; 610b62f328bSVille Tervo 611b62f328bSVille Tervo /* Closest match */ 612b62f328bSVille Tervo if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 613b62f328bSVille Tervo sk1 = sk; 614b62f328bSVille Tervo } 615b62f328bSVille Tervo } 616b62f328bSVille Tervo s = node ? sk : sk1; 617b62f328bSVille Tervo if (s) 618b62f328bSVille Tervo bh_lock_sock(s); 619b62f328bSVille Tervo read_unlock(&l2cap_sk_list.lock); 620b62f328bSVille Tervo 621b62f328bSVille Tervo return s; 622b62f328bSVille Tervo } 623b62f328bSVille Tervo 624b62f328bSVille Tervo static void l2cap_le_conn_ready(struct l2cap_conn *conn) 625b62f328bSVille Tervo { 626b62f328bSVille Tervo struct sock *parent, *uninitialized_var(sk); 62748454079SGustavo F. Padovan struct l2cap_chan *chan; 628b62f328bSVille Tervo 629b62f328bSVille Tervo BT_DBG(""); 630b62f328bSVille Tervo 631b62f328bSVille Tervo /* Check if we have socket listening on cid */ 632b62f328bSVille Tervo parent = l2cap_get_sock_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, 633b62f328bSVille Tervo conn->src); 634b62f328bSVille Tervo if (!parent) 635b62f328bSVille Tervo return; 636b62f328bSVille Tervo 637b62f328bSVille Tervo /* Check for backlog size */ 638b62f328bSVille Tervo if (sk_acceptq_is_full(parent)) { 639b62f328bSVille Tervo BT_DBG("backlog full %d", parent->sk_ack_backlog); 640b62f328bSVille Tervo goto clean; 641b62f328bSVille Tervo } 642b62f328bSVille Tervo 643b62f328bSVille Tervo sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC); 644b62f328bSVille Tervo if (!sk) 645b62f328bSVille Tervo goto clean; 646b62f328bSVille Tervo 64748454079SGustavo F. Padovan chan = l2cap_chan_alloc(sk); 64848454079SGustavo F. Padovan if (!chan) { 64948454079SGustavo F. Padovan l2cap_sock_kill(sk); 65048454079SGustavo F. Padovan goto clean; 65148454079SGustavo F. Padovan } 65248454079SGustavo F. Padovan 653baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 654b62f328bSVille Tervo 655b62f328bSVille Tervo hci_conn_hold(conn->hcon); 656b62f328bSVille Tervo 657b62f328bSVille Tervo l2cap_sock_init(sk, parent); 658baa7e1faSGustavo F. Padovan 659b62f328bSVille Tervo bacpy(&bt_sk(sk)->src, conn->src); 660b62f328bSVille Tervo bacpy(&bt_sk(sk)->dst, conn->dst); 661b62f328bSVille Tervo 662d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 663d1010240SGustavo F. Padovan 66448454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 66548454079SGustavo F. Padovan 66648454079SGustavo F. Padovan l2cap_pi(sk)->chan = chan; 667b62f328bSVille Tervo 668b62f328bSVille Tervo l2cap_sock_set_timer(sk, sk->sk_sndtimeo); 669b62f328bSVille Tervo 670b62f328bSVille Tervo sk->sk_state = BT_CONNECTED; 671b62f328bSVille Tervo parent->sk_data_ready(parent, 0); 672b62f328bSVille Tervo 673baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 674b62f328bSVille Tervo 675b62f328bSVille Tervo clean: 676b62f328bSVille Tervo bh_unlock_sock(parent); 677b62f328bSVille Tervo } 678b62f328bSVille Tervo 6790a708f8fSGustavo F. Padovan static void l2cap_conn_ready(struct l2cap_conn *conn) 6800a708f8fSGustavo F. Padovan { 68148454079SGustavo F. Padovan struct l2cap_chan *chan; 6820a708f8fSGustavo F. Padovan 6830a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 6840a708f8fSGustavo F. Padovan 685b62f328bSVille Tervo if (!conn->hcon->out && conn->hcon->type == LE_LINK) 686b62f328bSVille Tervo l2cap_le_conn_ready(conn); 687b62f328bSVille Tervo 688baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 6890a708f8fSGustavo F. Padovan 690baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 69148454079SGustavo F. Padovan struct sock *sk = chan->sk; 692baa7e1faSGustavo F. Padovan 6930a708f8fSGustavo F. Padovan bh_lock_sock(sk); 6940a708f8fSGustavo F. Padovan 695acd7d370SVille Tervo if (conn->hcon->type == LE_LINK) { 696acd7d370SVille Tervo l2cap_sock_clear_timer(sk); 697acd7d370SVille Tervo sk->sk_state = BT_CONNECTED; 698acd7d370SVille Tervo sk->sk_state_change(sk); 699acd7d370SVille Tervo } 700acd7d370SVille Tervo 7010a708f8fSGustavo F. Padovan if (sk->sk_type != SOCK_SEQPACKET && 7020a708f8fSGustavo F. Padovan sk->sk_type != SOCK_STREAM) { 7030a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 7040a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 7050a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 7060a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT) 707fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 7080a708f8fSGustavo F. Padovan 7090a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 7100a708f8fSGustavo F. Padovan } 7110a708f8fSGustavo F. Padovan 712baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 7130a708f8fSGustavo F. Padovan } 7140a708f8fSGustavo F. Padovan 7150a708f8fSGustavo F. Padovan /* Notify sockets that we cannot guaranty reliability anymore */ 7160a708f8fSGustavo F. Padovan static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) 7170a708f8fSGustavo F. Padovan { 71848454079SGustavo F. Padovan struct l2cap_chan *chan; 7190a708f8fSGustavo F. Padovan 7200a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 7210a708f8fSGustavo F. Padovan 722baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 7230a708f8fSGustavo F. Padovan 724baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 72548454079SGustavo F. Padovan struct sock *sk = chan->sk; 726baa7e1faSGustavo F. Padovan 7270a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->force_reliable) 7280a708f8fSGustavo F. Padovan sk->sk_err = err; 7290a708f8fSGustavo F. Padovan } 7300a708f8fSGustavo F. Padovan 731baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 7320a708f8fSGustavo F. Padovan } 7330a708f8fSGustavo F. Padovan 7340a708f8fSGustavo F. Padovan static void l2cap_info_timeout(unsigned long arg) 7350a708f8fSGustavo F. Padovan { 7360a708f8fSGustavo F. Padovan struct l2cap_conn *conn = (void *) arg; 7370a708f8fSGustavo F. Padovan 7380a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 7390a708f8fSGustavo F. Padovan conn->info_ident = 0; 7400a708f8fSGustavo F. Padovan 7410a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 7420a708f8fSGustavo F. Padovan } 7430a708f8fSGustavo F. Padovan 7440a708f8fSGustavo F. Padovan static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) 7450a708f8fSGustavo F. Padovan { 7460a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 7470a708f8fSGustavo F. Padovan 7480a708f8fSGustavo F. Padovan if (conn || status) 7490a708f8fSGustavo F. Padovan return conn; 7500a708f8fSGustavo F. Padovan 7510a708f8fSGustavo F. Padovan conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); 7520a708f8fSGustavo F. Padovan if (!conn) 7530a708f8fSGustavo F. Padovan return NULL; 7540a708f8fSGustavo F. Padovan 7550a708f8fSGustavo F. Padovan hcon->l2cap_data = conn; 7560a708f8fSGustavo F. Padovan conn->hcon = hcon; 7570a708f8fSGustavo F. Padovan 7580a708f8fSGustavo F. Padovan BT_DBG("hcon %p conn %p", hcon, conn); 7590a708f8fSGustavo F. Padovan 760acd7d370SVille Tervo if (hcon->hdev->le_mtu && hcon->type == LE_LINK) 761acd7d370SVille Tervo conn->mtu = hcon->hdev->le_mtu; 762acd7d370SVille Tervo else 7630a708f8fSGustavo F. Padovan conn->mtu = hcon->hdev->acl_mtu; 764acd7d370SVille Tervo 7650a708f8fSGustavo F. Padovan conn->src = &hcon->hdev->bdaddr; 7660a708f8fSGustavo F. Padovan conn->dst = &hcon->dst; 7670a708f8fSGustavo F. Padovan 7680a708f8fSGustavo F. Padovan conn->feat_mask = 0; 7690a708f8fSGustavo F. Padovan 7700a708f8fSGustavo F. Padovan spin_lock_init(&conn->lock); 771baa7e1faSGustavo F. Padovan rwlock_init(&conn->chan_lock); 772baa7e1faSGustavo F. Padovan 773baa7e1faSGustavo F. Padovan INIT_LIST_HEAD(&conn->chan_l); 7740a708f8fSGustavo F. Padovan 775b62f328bSVille Tervo if (hcon->type != LE_LINK) 7760a708f8fSGustavo F. Padovan setup_timer(&conn->info_timer, l2cap_info_timeout, 7770a708f8fSGustavo F. Padovan (unsigned long) conn); 7780a708f8fSGustavo F. Padovan 7790a708f8fSGustavo F. Padovan conn->disc_reason = 0x13; 7800a708f8fSGustavo F. Padovan 7810a708f8fSGustavo F. Padovan return conn; 7820a708f8fSGustavo F. Padovan } 7830a708f8fSGustavo F. Padovan 7840a708f8fSGustavo F. Padovan static void l2cap_conn_del(struct hci_conn *hcon, int err) 7850a708f8fSGustavo F. Padovan { 7860a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 787baa7e1faSGustavo F. Padovan struct l2cap_chan *chan, *l; 7880a708f8fSGustavo F. Padovan struct sock *sk; 7890a708f8fSGustavo F. Padovan 7900a708f8fSGustavo F. Padovan if (!conn) 7910a708f8fSGustavo F. Padovan return; 7920a708f8fSGustavo F. Padovan 7930a708f8fSGustavo F. Padovan BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); 7940a708f8fSGustavo F. Padovan 7950a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 7960a708f8fSGustavo F. Padovan 7970a708f8fSGustavo F. Padovan /* Kill channels */ 798baa7e1faSGustavo F. Padovan list_for_each_entry_safe(chan, l, &conn->chan_l, list) { 79948454079SGustavo F. Padovan sk = chan->sk; 8000a708f8fSGustavo F. Padovan bh_lock_sock(sk); 80148454079SGustavo F. Padovan l2cap_chan_del(chan, err); 8020a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 8030a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 8040a708f8fSGustavo F. Padovan } 8050a708f8fSGustavo F. Padovan 8060a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) 8070a708f8fSGustavo F. Padovan del_timer_sync(&conn->info_timer); 8080a708f8fSGustavo F. Padovan 8090a708f8fSGustavo F. Padovan hcon->l2cap_data = NULL; 8100a708f8fSGustavo F. Padovan kfree(conn); 8110a708f8fSGustavo F. Padovan } 8120a708f8fSGustavo F. Padovan 81348454079SGustavo F. Padovan static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) 8140a708f8fSGustavo F. Padovan { 815baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 81648454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 817baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 8180a708f8fSGustavo F. Padovan } 8190a708f8fSGustavo F. Padovan 8200a708f8fSGustavo F. Padovan /* ---- Socket interface ---- */ 8210a708f8fSGustavo F. Padovan 8220a708f8fSGustavo F. Padovan /* Find socket with psm and source bdaddr. 8230a708f8fSGustavo F. Padovan * Returns closest match. 8240a708f8fSGustavo F. Padovan */ 8250a708f8fSGustavo F. Padovan static struct sock *l2cap_get_sock_by_psm(int state, __le16 psm, bdaddr_t *src) 8260a708f8fSGustavo F. Padovan { 8270a708f8fSGustavo F. Padovan struct sock *sk = NULL, *sk1 = NULL; 8280a708f8fSGustavo F. Padovan struct hlist_node *node; 8290a708f8fSGustavo F. Padovan 8300a708f8fSGustavo F. Padovan read_lock(&l2cap_sk_list.lock); 8310a708f8fSGustavo F. Padovan 8320a708f8fSGustavo F. Padovan sk_for_each(sk, node, &l2cap_sk_list.head) { 8330a708f8fSGustavo F. Padovan if (state && sk->sk_state != state) 8340a708f8fSGustavo F. Padovan continue; 8350a708f8fSGustavo F. Padovan 8360a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->psm == psm) { 8370a708f8fSGustavo F. Padovan /* Exact match. */ 8380a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, src)) 8390a708f8fSGustavo F. Padovan break; 8400a708f8fSGustavo F. Padovan 8410a708f8fSGustavo F. Padovan /* Closest match */ 8420a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 8430a708f8fSGustavo F. Padovan sk1 = sk; 8440a708f8fSGustavo F. Padovan } 8450a708f8fSGustavo F. Padovan } 8460a708f8fSGustavo F. Padovan 8470a708f8fSGustavo F. Padovan read_unlock(&l2cap_sk_list.lock); 8480a708f8fSGustavo F. Padovan 8490a708f8fSGustavo F. Padovan return node ? sk : sk1; 8500a708f8fSGustavo F. Padovan } 8510a708f8fSGustavo F. Padovan 8524e34c50bSGustavo F. Padovan int l2cap_do_connect(struct sock *sk) 8530a708f8fSGustavo F. Padovan { 8540a708f8fSGustavo F. Padovan bdaddr_t *src = &bt_sk(sk)->src; 8550a708f8fSGustavo F. Padovan bdaddr_t *dst = &bt_sk(sk)->dst; 8560a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 85748454079SGustavo F. Padovan struct l2cap_chan *chan; 8580a708f8fSGustavo F. Padovan struct hci_conn *hcon; 8590a708f8fSGustavo F. Padovan struct hci_dev *hdev; 8600a708f8fSGustavo F. Padovan __u8 auth_type; 8610a708f8fSGustavo F. Padovan int err; 8620a708f8fSGustavo F. Padovan 8630a708f8fSGustavo F. Padovan BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), 8640a708f8fSGustavo F. Padovan l2cap_pi(sk)->psm); 8650a708f8fSGustavo F. Padovan 8660a708f8fSGustavo F. Padovan hdev = hci_get_route(dst, src); 8670a708f8fSGustavo F. Padovan if (!hdev) 8680a708f8fSGustavo F. Padovan return -EHOSTUNREACH; 8690a708f8fSGustavo F. Padovan 8700a708f8fSGustavo F. Padovan hci_dev_lock_bh(hdev); 8710a708f8fSGustavo F. Padovan 8720a708f8fSGustavo F. Padovan auth_type = l2cap_get_auth_type(sk); 8730a708f8fSGustavo F. Padovan 874acd7d370SVille Tervo if (l2cap_pi(sk)->dcid == L2CAP_CID_LE_DATA) 875acd7d370SVille Tervo hcon = hci_connect(hdev, LE_LINK, dst, 876acd7d370SVille Tervo l2cap_pi(sk)->sec_level, auth_type); 877acd7d370SVille Tervo else 8780a708f8fSGustavo F. Padovan hcon = hci_connect(hdev, ACL_LINK, dst, 8790a708f8fSGustavo F. Padovan l2cap_pi(sk)->sec_level, auth_type); 880acd7d370SVille Tervo 88130e76272SVille Tervo if (IS_ERR(hcon)) { 88230e76272SVille Tervo err = PTR_ERR(hcon); 8830a708f8fSGustavo F. Padovan goto done; 88430e76272SVille Tervo } 8850a708f8fSGustavo F. Padovan 8860a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 8870a708f8fSGustavo F. Padovan if (!conn) { 8880a708f8fSGustavo F. Padovan hci_conn_put(hcon); 88930e76272SVille Tervo err = -ENOMEM; 8900a708f8fSGustavo F. Padovan goto done; 8910a708f8fSGustavo F. Padovan } 8920a708f8fSGustavo F. Padovan 89348454079SGustavo F. Padovan chan = l2cap_chan_alloc(sk); 89448454079SGustavo F. Padovan if (!chan) { 89548454079SGustavo F. Padovan hci_conn_put(hcon); 89648454079SGustavo F. Padovan err = -ENOMEM; 89748454079SGustavo F. Padovan goto done; 89848454079SGustavo F. Padovan } 89948454079SGustavo F. Padovan 9000a708f8fSGustavo F. Padovan /* Update source addr of the socket */ 9010a708f8fSGustavo F. Padovan bacpy(src, conn->src); 9020a708f8fSGustavo F. Padovan 90348454079SGustavo F. Padovan l2cap_chan_add(conn, chan); 90448454079SGustavo F. Padovan 90548454079SGustavo F. Padovan l2cap_pi(sk)->chan = chan; 9060a708f8fSGustavo F. Padovan 9070a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT; 9080a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, sk->sk_sndtimeo); 9090a708f8fSGustavo F. Padovan 9100a708f8fSGustavo F. Padovan if (hcon->state == BT_CONNECTED) { 9110a708f8fSGustavo F. Padovan if (sk->sk_type != SOCK_SEQPACKET && 9120a708f8fSGustavo F. Padovan sk->sk_type != SOCK_STREAM) { 9130a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 9140a708f8fSGustavo F. Padovan if (l2cap_check_security(sk)) 9150a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 9160a708f8fSGustavo F. Padovan } else 917fc7f8a7eSGustavo F. Padovan l2cap_do_start(chan); 9180a708f8fSGustavo F. Padovan } 9190a708f8fSGustavo F. Padovan 92030e76272SVille Tervo err = 0; 92130e76272SVille Tervo 9220a708f8fSGustavo F. Padovan done: 9230a708f8fSGustavo F. Padovan hci_dev_unlock_bh(hdev); 9240a708f8fSGustavo F. Padovan hci_dev_put(hdev); 9250a708f8fSGustavo F. Padovan return err; 9260a708f8fSGustavo F. Padovan } 9270a708f8fSGustavo F. Padovan 928dcba0dbaSGustavo F. Padovan int __l2cap_wait_ack(struct sock *sk) 9290a708f8fSGustavo F. Padovan { 9300a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 9310a708f8fSGustavo F. Padovan int err = 0; 9320a708f8fSGustavo F. Padovan int timeo = HZ/5; 9330a708f8fSGustavo F. Padovan 9340a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 9356a026610SGustavo F. Padovan while ((l2cap_pi(sk)->chan->unacked_frames > 0 && l2cap_pi(sk)->conn)) { 9360a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 9370a708f8fSGustavo F. Padovan 9380a708f8fSGustavo F. Padovan if (!timeo) 9390a708f8fSGustavo F. Padovan timeo = HZ/5; 9400a708f8fSGustavo F. Padovan 9410a708f8fSGustavo F. Padovan if (signal_pending(current)) { 9420a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 9430a708f8fSGustavo F. Padovan break; 9440a708f8fSGustavo F. Padovan } 9450a708f8fSGustavo F. Padovan 9460a708f8fSGustavo F. Padovan release_sock(sk); 9470a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 9480a708f8fSGustavo F. Padovan lock_sock(sk); 9490a708f8fSGustavo F. Padovan 9500a708f8fSGustavo F. Padovan err = sock_error(sk); 9510a708f8fSGustavo F. Padovan if (err) 9520a708f8fSGustavo F. Padovan break; 9530a708f8fSGustavo F. Padovan } 9540a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 9550a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 9560a708f8fSGustavo F. Padovan return err; 9570a708f8fSGustavo F. Padovan } 9580a708f8fSGustavo F. Padovan 9590a708f8fSGustavo F. Padovan static void l2cap_monitor_timeout(unsigned long arg) 9600a708f8fSGustavo F. Padovan { 961525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 962525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 9630a708f8fSGustavo F. Padovan 964525cd185SGustavo F. Padovan BT_DBG("chan %p", chan); 9650a708f8fSGustavo F. Padovan 9660a708f8fSGustavo F. Padovan bh_lock_sock(sk); 9672c03a7a4SGustavo F. Padovan if (chan->retry_count >= chan->remote_max_tx) { 968e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(l2cap_pi(sk)->conn, chan, ECONNABORTED); 9690a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9700a708f8fSGustavo F. Padovan return; 9710a708f8fSGustavo F. Padovan } 9720a708f8fSGustavo F. Padovan 9736a026610SGustavo F. Padovan chan->retry_count++; 9740a708f8fSGustavo F. Padovan __mod_monitor_timer(); 9750a708f8fSGustavo F. Padovan 976525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 9770a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9780a708f8fSGustavo F. Padovan } 9790a708f8fSGustavo F. Padovan 9800a708f8fSGustavo F. Padovan static void l2cap_retrans_timeout(unsigned long arg) 9810a708f8fSGustavo F. Padovan { 982525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 983525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 9840a708f8fSGustavo F. Padovan 9850a708f8fSGustavo F. Padovan BT_DBG("sk %p", sk); 9860a708f8fSGustavo F. Padovan 9870a708f8fSGustavo F. Padovan bh_lock_sock(sk); 9886a026610SGustavo F. Padovan chan->retry_count = 1; 9890a708f8fSGustavo F. Padovan __mod_monitor_timer(); 9900a708f8fSGustavo F. Padovan 991525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_WAIT_F; 9920a708f8fSGustavo F. Padovan 993525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); 9940a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 9950a708f8fSGustavo F. Padovan } 9960a708f8fSGustavo F. Padovan 99742e5c802SGustavo F. Padovan static void l2cap_drop_acked_frames(struct l2cap_chan *chan) 9980a708f8fSGustavo F. Padovan { 99942e5c802SGustavo F. Padovan struct sock *sk = chan->sk; 10000a708f8fSGustavo F. Padovan struct sk_buff *skb; 10010a708f8fSGustavo F. Padovan 10020a708f8fSGustavo F. Padovan while ((skb = skb_peek(TX_QUEUE(sk))) && 10036a026610SGustavo F. Padovan chan->unacked_frames) { 100442e5c802SGustavo F. Padovan if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) 10050a708f8fSGustavo F. Padovan break; 10060a708f8fSGustavo F. Padovan 10070a708f8fSGustavo F. Padovan skb = skb_dequeue(TX_QUEUE(sk)); 10080a708f8fSGustavo F. Padovan kfree_skb(skb); 10090a708f8fSGustavo F. Padovan 10106a026610SGustavo F. Padovan chan->unacked_frames--; 10110a708f8fSGustavo F. Padovan } 10120a708f8fSGustavo F. Padovan 10136a026610SGustavo F. Padovan if (!chan->unacked_frames) 1014e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 10150a708f8fSGustavo F. Padovan } 10160a708f8fSGustavo F. Padovan 1017fd83ccdbSGustavo F. Padovan void l2cap_do_send(struct sock *sk, struct sk_buff *skb) 10180a708f8fSGustavo F. Padovan { 10190a708f8fSGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(sk); 10200a708f8fSGustavo F. Padovan struct hci_conn *hcon = pi->conn->hcon; 10210a708f8fSGustavo F. Padovan u16 flags; 10220a708f8fSGustavo F. Padovan 10230a708f8fSGustavo F. Padovan BT_DBG("sk %p, skb %p len %d", sk, skb, skb->len); 10240a708f8fSGustavo F. Padovan 10250a708f8fSGustavo F. Padovan if (!pi->flushable && lmp_no_flush_capable(hcon->hdev)) 10260a708f8fSGustavo F. Padovan flags = ACL_START_NO_FLUSH; 10270a708f8fSGustavo F. Padovan else 10280a708f8fSGustavo F. Padovan flags = ACL_START; 10290a708f8fSGustavo F. Padovan 10300a708f8fSGustavo F. Padovan hci_send_acl(hcon, skb, flags); 10310a708f8fSGustavo F. Padovan } 10320a708f8fSGustavo F. Padovan 103342e5c802SGustavo F. Padovan void l2cap_streaming_send(struct l2cap_chan *chan) 10340a708f8fSGustavo F. Padovan { 103542e5c802SGustavo F. Padovan struct sock *sk = chan->sk; 10360a708f8fSGustavo F. Padovan struct sk_buff *skb; 10370a708f8fSGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(sk); 10380a708f8fSGustavo F. Padovan u16 control, fcs; 10390a708f8fSGustavo F. Padovan 10400a708f8fSGustavo F. Padovan while ((skb = skb_dequeue(TX_QUEUE(sk)))) { 10410a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data + L2CAP_HDR_SIZE); 104242e5c802SGustavo F. Padovan control |= chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT; 10430a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb->data + L2CAP_HDR_SIZE); 10440a708f8fSGustavo F. Padovan 10450a708f8fSGustavo F. Padovan if (pi->fcs == L2CAP_FCS_CRC16) { 10460a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)skb->data, skb->len - 2); 10470a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb->data + skb->len - 2); 10480a708f8fSGustavo F. Padovan } 10490a708f8fSGustavo F. Padovan 10500a708f8fSGustavo F. Padovan l2cap_do_send(sk, skb); 10510a708f8fSGustavo F. Padovan 105242e5c802SGustavo F. Padovan chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; 10530a708f8fSGustavo F. Padovan } 10540a708f8fSGustavo F. Padovan } 10550a708f8fSGustavo F. Padovan 1056525cd185SGustavo F. Padovan static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq) 10570a708f8fSGustavo F. Padovan { 1058525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 10590a708f8fSGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(sk); 10600a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 10610a708f8fSGustavo F. Padovan u16 control, fcs; 10620a708f8fSGustavo F. Padovan 10630a708f8fSGustavo F. Padovan skb = skb_peek(TX_QUEUE(sk)); 10640a708f8fSGustavo F. Padovan if (!skb) 10650a708f8fSGustavo F. Padovan return; 10660a708f8fSGustavo F. Padovan 10670a708f8fSGustavo F. Padovan do { 10680a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq == tx_seq) 10690a708f8fSGustavo F. Padovan break; 10700a708f8fSGustavo F. Padovan 10710a708f8fSGustavo F. Padovan if (skb_queue_is_last(TX_QUEUE(sk), skb)) 10720a708f8fSGustavo F. Padovan return; 10730a708f8fSGustavo F. Padovan 10740a708f8fSGustavo F. Padovan } while ((skb = skb_queue_next(TX_QUEUE(sk), skb))); 10750a708f8fSGustavo F. Padovan 10762c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 10772c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 1078e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(pi->conn, chan, ECONNABORTED); 10790a708f8fSGustavo F. Padovan return; 10800a708f8fSGustavo F. Padovan } 10810a708f8fSGustavo F. Padovan 10820a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 10830a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 10840a708f8fSGustavo F. Padovan control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); 10850a708f8fSGustavo F. Padovan 1086525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 10870a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 1088525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 10890a708f8fSGustavo F. Padovan } 10900a708f8fSGustavo F. Padovan 109142e5c802SGustavo F. Padovan control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT) 10920a708f8fSGustavo F. Padovan | (tx_seq << L2CAP_CTRL_TXSEQ_SHIFT); 10930a708f8fSGustavo F. Padovan 10940a708f8fSGustavo F. Padovan put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); 10950a708f8fSGustavo F. Padovan 10960a708f8fSGustavo F. Padovan if (pi->fcs == L2CAP_FCS_CRC16) { 10970a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)tx_skb->data, tx_skb->len - 2); 10980a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, tx_skb->data + tx_skb->len - 2); 10990a708f8fSGustavo F. Padovan } 11000a708f8fSGustavo F. Padovan 11010a708f8fSGustavo F. Padovan l2cap_do_send(sk, tx_skb); 11020a708f8fSGustavo F. Padovan } 11030a708f8fSGustavo F. Padovan 1104525cd185SGustavo F. Padovan int l2cap_ertm_send(struct l2cap_chan *chan) 11050a708f8fSGustavo F. Padovan { 11060a708f8fSGustavo F. Padovan struct sk_buff *skb, *tx_skb; 1107525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 11080a708f8fSGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(sk); 11090a708f8fSGustavo F. Padovan u16 control, fcs; 11100a708f8fSGustavo F. Padovan int nsent = 0; 11110a708f8fSGustavo F. Padovan 11120a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 11130a708f8fSGustavo F. Padovan return -ENOTCONN; 11140a708f8fSGustavo F. Padovan 111542e5c802SGustavo F. Padovan while ((skb = sk->sk_send_head) && (!l2cap_tx_window_full(chan))) { 11160a708f8fSGustavo F. Padovan 11172c03a7a4SGustavo F. Padovan if (chan->remote_max_tx && 11182c03a7a4SGustavo F. Padovan bt_cb(skb)->retries == chan->remote_max_tx) { 1119e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(pi->conn, chan, ECONNABORTED); 11200a708f8fSGustavo F. Padovan break; 11210a708f8fSGustavo F. Padovan } 11220a708f8fSGustavo F. Padovan 11230a708f8fSGustavo F. Padovan tx_skb = skb_clone(skb, GFP_ATOMIC); 11240a708f8fSGustavo F. Padovan 11250a708f8fSGustavo F. Padovan bt_cb(skb)->retries++; 11260a708f8fSGustavo F. Padovan 11270a708f8fSGustavo F. Padovan control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); 11280a708f8fSGustavo F. Padovan control &= L2CAP_CTRL_SAR; 11290a708f8fSGustavo F. Padovan 1130525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { 11310a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 1132525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; 11330a708f8fSGustavo F. Padovan } 113442e5c802SGustavo F. Padovan control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT) 113542e5c802SGustavo F. Padovan | (chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT); 11360a708f8fSGustavo F. Padovan put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); 11370a708f8fSGustavo F. Padovan 11380a708f8fSGustavo F. Padovan 11390a708f8fSGustavo F. Padovan if (pi->fcs == L2CAP_FCS_CRC16) { 11400a708f8fSGustavo F. Padovan fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2); 11410a708f8fSGustavo F. Padovan put_unaligned_le16(fcs, skb->data + tx_skb->len - 2); 11420a708f8fSGustavo F. Padovan } 11430a708f8fSGustavo F. Padovan 11440a708f8fSGustavo F. Padovan l2cap_do_send(sk, tx_skb); 11450a708f8fSGustavo F. Padovan 11460a708f8fSGustavo F. Padovan __mod_retrans_timer(); 11470a708f8fSGustavo F. Padovan 114842e5c802SGustavo F. Padovan bt_cb(skb)->tx_seq = chan->next_tx_seq; 114942e5c802SGustavo F. Padovan chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; 11500a708f8fSGustavo F. Padovan 115123e9fde2SSuraj Sumangala if (bt_cb(skb)->retries == 1) 11526a026610SGustavo F. Padovan chan->unacked_frames++; 115323e9fde2SSuraj Sumangala 11546a026610SGustavo F. Padovan chan->frames_sent++; 11550a708f8fSGustavo F. Padovan 11560a708f8fSGustavo F. Padovan if (skb_queue_is_last(TX_QUEUE(sk), skb)) 11570a708f8fSGustavo F. Padovan sk->sk_send_head = NULL; 11580a708f8fSGustavo F. Padovan else 11590a708f8fSGustavo F. Padovan sk->sk_send_head = skb_queue_next(TX_QUEUE(sk), skb); 11600a708f8fSGustavo F. Padovan 11610a708f8fSGustavo F. Padovan nsent++; 11620a708f8fSGustavo F. Padovan } 11630a708f8fSGustavo F. Padovan 11640a708f8fSGustavo F. Padovan return nsent; 11650a708f8fSGustavo F. Padovan } 11660a708f8fSGustavo F. Padovan 1167525cd185SGustavo F. Padovan static int l2cap_retransmit_frames(struct l2cap_chan *chan) 11680a708f8fSGustavo F. Padovan { 1169525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 11700a708f8fSGustavo F. Padovan int ret; 11710a708f8fSGustavo F. Padovan 11720a708f8fSGustavo F. Padovan if (!skb_queue_empty(TX_QUEUE(sk))) 11730a708f8fSGustavo F. Padovan sk->sk_send_head = TX_QUEUE(sk)->next; 11740a708f8fSGustavo F. Padovan 117542e5c802SGustavo F. Padovan chan->next_tx_seq = chan->expected_ack_seq; 1176525cd185SGustavo F. Padovan ret = l2cap_ertm_send(chan); 11770a708f8fSGustavo F. Padovan return ret; 11780a708f8fSGustavo F. Padovan } 11790a708f8fSGustavo F. Padovan 1180525cd185SGustavo F. Padovan static void l2cap_send_ack(struct l2cap_chan *chan) 11810a708f8fSGustavo F. Padovan { 11820a708f8fSGustavo F. Padovan u16 control = 0; 11830a708f8fSGustavo F. Padovan 118442e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 11850a708f8fSGustavo F. Padovan 1186525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 11870a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 1188525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 1189525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 11900a708f8fSGustavo F. Padovan return; 11910a708f8fSGustavo F. Padovan } 11920a708f8fSGustavo F. Padovan 1193525cd185SGustavo F. Padovan if (l2cap_ertm_send(chan) > 0) 11940a708f8fSGustavo F. Padovan return; 11950a708f8fSGustavo F. Padovan 11960a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 1197525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 11980a708f8fSGustavo F. Padovan } 11990a708f8fSGustavo F. Padovan 1200525cd185SGustavo F. Padovan static void l2cap_send_srejtail(struct l2cap_chan *chan) 12010a708f8fSGustavo F. Padovan { 12020a708f8fSGustavo F. Padovan struct srej_list *tail; 12030a708f8fSGustavo F. Padovan u16 control; 12040a708f8fSGustavo F. Padovan 12050a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 12060a708f8fSGustavo F. Padovan control |= L2CAP_CTRL_FINAL; 12070a708f8fSGustavo F. Padovan 1208525cd185SGustavo F. Padovan tail = list_entry(SREJ_LIST(chan->sk)->prev, struct srej_list, list); 12090a708f8fSGustavo F. Padovan control |= tail->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 12100a708f8fSGustavo F. Padovan 1211525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 12120a708f8fSGustavo F. Padovan } 12130a708f8fSGustavo F. Padovan 12140a708f8fSGustavo F. Padovan static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb) 12150a708f8fSGustavo F. Padovan { 12160a708f8fSGustavo F. Padovan struct l2cap_conn *conn = l2cap_pi(sk)->conn; 12170a708f8fSGustavo F. Padovan struct sk_buff **frag; 12180a708f8fSGustavo F. Padovan int err, sent = 0; 12190a708f8fSGustavo F. Padovan 12200a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) 12210a708f8fSGustavo F. Padovan return -EFAULT; 12220a708f8fSGustavo F. Padovan 12230a708f8fSGustavo F. Padovan sent += count; 12240a708f8fSGustavo F. Padovan len -= count; 12250a708f8fSGustavo F. Padovan 12260a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 12270a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 12280a708f8fSGustavo F. Padovan while (len) { 12290a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 12300a708f8fSGustavo F. Padovan 12310a708f8fSGustavo F. Padovan *frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err); 12320a708f8fSGustavo F. Padovan if (!*frag) 12330a708f8fSGustavo F. Padovan return err; 12340a708f8fSGustavo F. Padovan if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) 12350a708f8fSGustavo F. Padovan return -EFAULT; 12360a708f8fSGustavo F. Padovan 12370a708f8fSGustavo F. Padovan sent += count; 12380a708f8fSGustavo F. Padovan len -= count; 12390a708f8fSGustavo F. Padovan 12400a708f8fSGustavo F. Padovan frag = &(*frag)->next; 12410a708f8fSGustavo F. Padovan } 12420a708f8fSGustavo F. Padovan 12430a708f8fSGustavo F. Padovan return sent; 12440a708f8fSGustavo F. Padovan } 12450a708f8fSGustavo F. Padovan 1246fd83ccdbSGustavo F. Padovan struct sk_buff *l2cap_create_connless_pdu(struct sock *sk, struct msghdr *msg, size_t len) 12470a708f8fSGustavo F. Padovan { 12480a708f8fSGustavo F. Padovan struct l2cap_conn *conn = l2cap_pi(sk)->conn; 12490a708f8fSGustavo F. Padovan struct sk_buff *skb; 12500a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE + 2; 12510a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 12520a708f8fSGustavo F. Padovan 12530a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 12540a708f8fSGustavo F. Padovan 12550a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 12560a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 12570a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 12580a708f8fSGustavo F. Padovan if (!skb) 12590a708f8fSGustavo F. Padovan return ERR_PTR(err); 12600a708f8fSGustavo F. Padovan 12610a708f8fSGustavo F. Padovan /* Create L2CAP header */ 12620a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 12630a708f8fSGustavo F. Padovan lh->cid = cpu_to_le16(l2cap_pi(sk)->dcid); 12640a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 12650a708f8fSGustavo F. Padovan put_unaligned_le16(l2cap_pi(sk)->psm, skb_put(skb, 2)); 12660a708f8fSGustavo F. Padovan 12670a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 12680a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 12690a708f8fSGustavo F. Padovan kfree_skb(skb); 12700a708f8fSGustavo F. Padovan return ERR_PTR(err); 12710a708f8fSGustavo F. Padovan } 12720a708f8fSGustavo F. Padovan return skb; 12730a708f8fSGustavo F. Padovan } 12740a708f8fSGustavo F. Padovan 1275fd83ccdbSGustavo F. Padovan struct sk_buff *l2cap_create_basic_pdu(struct sock *sk, struct msghdr *msg, size_t len) 12760a708f8fSGustavo F. Padovan { 12770a708f8fSGustavo F. Padovan struct l2cap_conn *conn = l2cap_pi(sk)->conn; 12780a708f8fSGustavo F. Padovan struct sk_buff *skb; 12790a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE; 12800a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 12810a708f8fSGustavo F. Padovan 12820a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 12830a708f8fSGustavo F. Padovan 12840a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 12850a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 12860a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 12870a708f8fSGustavo F. Padovan if (!skb) 12880a708f8fSGustavo F. Padovan return ERR_PTR(err); 12890a708f8fSGustavo F. Padovan 12900a708f8fSGustavo F. Padovan /* Create L2CAP header */ 12910a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 12920a708f8fSGustavo F. Padovan lh->cid = cpu_to_le16(l2cap_pi(sk)->dcid); 12930a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 12940a708f8fSGustavo F. Padovan 12950a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 12960a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 12970a708f8fSGustavo F. Padovan kfree_skb(skb); 12980a708f8fSGustavo F. Padovan return ERR_PTR(err); 12990a708f8fSGustavo F. Padovan } 13000a708f8fSGustavo F. Padovan return skb; 13010a708f8fSGustavo F. Padovan } 13020a708f8fSGustavo F. Padovan 1303fd83ccdbSGustavo F. Padovan struct sk_buff *l2cap_create_iframe_pdu(struct sock *sk, struct msghdr *msg, size_t len, u16 control, u16 sdulen) 13040a708f8fSGustavo F. Padovan { 13050a708f8fSGustavo F. Padovan struct l2cap_conn *conn = l2cap_pi(sk)->conn; 13060a708f8fSGustavo F. Padovan struct sk_buff *skb; 13070a708f8fSGustavo F. Padovan int err, count, hlen = L2CAP_HDR_SIZE + 2; 13080a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 13090a708f8fSGustavo F. Padovan 13100a708f8fSGustavo F. Padovan BT_DBG("sk %p len %d", sk, (int)len); 13110a708f8fSGustavo F. Padovan 13120a708f8fSGustavo F. Padovan if (!conn) 13130a708f8fSGustavo F. Padovan return ERR_PTR(-ENOTCONN); 13140a708f8fSGustavo F. Padovan 13150a708f8fSGustavo F. Padovan if (sdulen) 13160a708f8fSGustavo F. Padovan hlen += 2; 13170a708f8fSGustavo F. Padovan 13180a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->fcs == L2CAP_FCS_CRC16) 13190a708f8fSGustavo F. Padovan hlen += 2; 13200a708f8fSGustavo F. Padovan 13210a708f8fSGustavo F. Padovan count = min_t(unsigned int, (conn->mtu - hlen), len); 13220a708f8fSGustavo F. Padovan skb = bt_skb_send_alloc(sk, count + hlen, 13230a708f8fSGustavo F. Padovan msg->msg_flags & MSG_DONTWAIT, &err); 13240a708f8fSGustavo F. Padovan if (!skb) 13250a708f8fSGustavo F. Padovan return ERR_PTR(err); 13260a708f8fSGustavo F. Padovan 13270a708f8fSGustavo F. Padovan /* Create L2CAP header */ 13280a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 13290a708f8fSGustavo F. Padovan lh->cid = cpu_to_le16(l2cap_pi(sk)->dcid); 13300a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); 13310a708f8fSGustavo F. Padovan put_unaligned_le16(control, skb_put(skb, 2)); 13320a708f8fSGustavo F. Padovan if (sdulen) 13330a708f8fSGustavo F. Padovan put_unaligned_le16(sdulen, skb_put(skb, 2)); 13340a708f8fSGustavo F. Padovan 13350a708f8fSGustavo F. Padovan err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); 13360a708f8fSGustavo F. Padovan if (unlikely(err < 0)) { 13370a708f8fSGustavo F. Padovan kfree_skb(skb); 13380a708f8fSGustavo F. Padovan return ERR_PTR(err); 13390a708f8fSGustavo F. Padovan } 13400a708f8fSGustavo F. Padovan 13410a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->fcs == L2CAP_FCS_CRC16) 13420a708f8fSGustavo F. Padovan put_unaligned_le16(0, skb_put(skb, 2)); 13430a708f8fSGustavo F. Padovan 13440a708f8fSGustavo F. Padovan bt_cb(skb)->retries = 0; 13450a708f8fSGustavo F. Padovan return skb; 13460a708f8fSGustavo F. Padovan } 13470a708f8fSGustavo F. Padovan 13482c03a7a4SGustavo F. Padovan int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) 13490a708f8fSGustavo F. Padovan { 13502c03a7a4SGustavo F. Padovan struct sock *sk = chan->sk; 13510a708f8fSGustavo F. Padovan struct sk_buff *skb; 13520a708f8fSGustavo F. Padovan struct sk_buff_head sar_queue; 13530a708f8fSGustavo F. Padovan u16 control; 13540a708f8fSGustavo F. Padovan size_t size = 0; 13550a708f8fSGustavo F. Padovan 13560a708f8fSGustavo F. Padovan skb_queue_head_init(&sar_queue); 13570a708f8fSGustavo F. Padovan control = L2CAP_SDU_START; 13582c03a7a4SGustavo F. Padovan skb = l2cap_create_iframe_pdu(sk, msg, chan->remote_mps, control, len); 13590a708f8fSGustavo F. Padovan if (IS_ERR(skb)) 13600a708f8fSGustavo F. Padovan return PTR_ERR(skb); 13610a708f8fSGustavo F. Padovan 13620a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 13632c03a7a4SGustavo F. Padovan len -= chan->remote_mps; 13642c03a7a4SGustavo F. Padovan size += chan->remote_mps; 13650a708f8fSGustavo F. Padovan 13660a708f8fSGustavo F. Padovan while (len > 0) { 13670a708f8fSGustavo F. Padovan size_t buflen; 13680a708f8fSGustavo F. Padovan 13692c03a7a4SGustavo F. Padovan if (len > chan->remote_mps) { 13700a708f8fSGustavo F. Padovan control = L2CAP_SDU_CONTINUE; 13712c03a7a4SGustavo F. Padovan buflen = chan->remote_mps; 13720a708f8fSGustavo F. Padovan } else { 13730a708f8fSGustavo F. Padovan control = L2CAP_SDU_END; 13740a708f8fSGustavo F. Padovan buflen = len; 13750a708f8fSGustavo F. Padovan } 13760a708f8fSGustavo F. Padovan 13770a708f8fSGustavo F. Padovan skb = l2cap_create_iframe_pdu(sk, msg, buflen, control, 0); 13780a708f8fSGustavo F. Padovan if (IS_ERR(skb)) { 13790a708f8fSGustavo F. Padovan skb_queue_purge(&sar_queue); 13800a708f8fSGustavo F. Padovan return PTR_ERR(skb); 13810a708f8fSGustavo F. Padovan } 13820a708f8fSGustavo F. Padovan 13830a708f8fSGustavo F. Padovan __skb_queue_tail(&sar_queue, skb); 13840a708f8fSGustavo F. Padovan len -= buflen; 13850a708f8fSGustavo F. Padovan size += buflen; 13860a708f8fSGustavo F. Padovan } 13870a708f8fSGustavo F. Padovan skb_queue_splice_tail(&sar_queue, TX_QUEUE(sk)); 13880a708f8fSGustavo F. Padovan if (sk->sk_send_head == NULL) 13890a708f8fSGustavo F. Padovan sk->sk_send_head = sar_queue.next; 13900a708f8fSGustavo F. Padovan 13910a708f8fSGustavo F. Padovan return size; 13920a708f8fSGustavo F. Padovan } 13930a708f8fSGustavo F. Padovan 13940a708f8fSGustavo F. Padovan static void l2cap_chan_ready(struct sock *sk) 13950a708f8fSGustavo F. Padovan { 13960a708f8fSGustavo F. Padovan struct sock *parent = bt_sk(sk)->parent; 13970a708f8fSGustavo F. Padovan 13980a708f8fSGustavo F. Padovan BT_DBG("sk %p, parent %p", sk, parent); 13990a708f8fSGustavo F. Padovan 14000a708f8fSGustavo F. Padovan l2cap_pi(sk)->conf_state = 0; 14010a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 14020a708f8fSGustavo F. Padovan 14030a708f8fSGustavo F. Padovan if (!parent) { 14040a708f8fSGustavo F. Padovan /* Outgoing channel. 14050a708f8fSGustavo F. Padovan * Wake up socket sleeping on connect. 14060a708f8fSGustavo F. Padovan */ 14070a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 14080a708f8fSGustavo F. Padovan sk->sk_state_change(sk); 14090a708f8fSGustavo F. Padovan } else { 14100a708f8fSGustavo F. Padovan /* Incoming channel. 14110a708f8fSGustavo F. Padovan * Wake up socket sleeping on accept. 14120a708f8fSGustavo F. Padovan */ 14130a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 14140a708f8fSGustavo F. Padovan } 14150a708f8fSGustavo F. Padovan } 14160a708f8fSGustavo F. Padovan 14170a708f8fSGustavo F. Padovan /* Copy frame to all raw sockets on that connection */ 14180a708f8fSGustavo F. Padovan static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) 14190a708f8fSGustavo F. Padovan { 14200a708f8fSGustavo F. Padovan struct sk_buff *nskb; 142148454079SGustavo F. Padovan struct l2cap_chan *chan; 14220a708f8fSGustavo F. Padovan 14230a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 14240a708f8fSGustavo F. Padovan 1425baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 1426baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 142748454079SGustavo F. Padovan struct sock *sk = chan->sk; 14280a708f8fSGustavo F. Padovan if (sk->sk_type != SOCK_RAW) 14290a708f8fSGustavo F. Padovan continue; 14300a708f8fSGustavo F. Padovan 14310a708f8fSGustavo F. Padovan /* Don't send frame to the socket it came from */ 14320a708f8fSGustavo F. Padovan if (skb->sk == sk) 14330a708f8fSGustavo F. Padovan continue; 14340a708f8fSGustavo F. Padovan nskb = skb_clone(skb, GFP_ATOMIC); 14350a708f8fSGustavo F. Padovan if (!nskb) 14360a708f8fSGustavo F. Padovan continue; 14370a708f8fSGustavo F. Padovan 14380a708f8fSGustavo F. Padovan if (sock_queue_rcv_skb(sk, nskb)) 14390a708f8fSGustavo F. Padovan kfree_skb(nskb); 14400a708f8fSGustavo F. Padovan } 1441baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 14420a708f8fSGustavo F. Padovan } 14430a708f8fSGustavo F. Padovan 14440a708f8fSGustavo F. Padovan /* ---- L2CAP signalling commands ---- */ 14450a708f8fSGustavo F. Padovan static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, 14460a708f8fSGustavo F. Padovan u8 code, u8 ident, u16 dlen, void *data) 14470a708f8fSGustavo F. Padovan { 14480a708f8fSGustavo F. Padovan struct sk_buff *skb, **frag; 14490a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr *cmd; 14500a708f8fSGustavo F. Padovan struct l2cap_hdr *lh; 14510a708f8fSGustavo F. Padovan int len, count; 14520a708f8fSGustavo F. Padovan 14530a708f8fSGustavo F. Padovan BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", 14540a708f8fSGustavo F. Padovan conn, code, ident, dlen); 14550a708f8fSGustavo F. Padovan 14560a708f8fSGustavo F. Padovan len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; 14570a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 14580a708f8fSGustavo F. Padovan 14590a708f8fSGustavo F. Padovan skb = bt_skb_alloc(count, GFP_ATOMIC); 14600a708f8fSGustavo F. Padovan if (!skb) 14610a708f8fSGustavo F. Padovan return NULL; 14620a708f8fSGustavo F. Padovan 14630a708f8fSGustavo F. Padovan lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 14640a708f8fSGustavo F. Padovan lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); 14653300d9a9SClaudio Takahasi 14663300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 14673300d9a9SClaudio Takahasi lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); 14683300d9a9SClaudio Takahasi else 14690a708f8fSGustavo F. Padovan lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); 14700a708f8fSGustavo F. Padovan 14710a708f8fSGustavo F. Padovan cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); 14720a708f8fSGustavo F. Padovan cmd->code = code; 14730a708f8fSGustavo F. Padovan cmd->ident = ident; 14740a708f8fSGustavo F. Padovan cmd->len = cpu_to_le16(dlen); 14750a708f8fSGustavo F. Padovan 14760a708f8fSGustavo F. Padovan if (dlen) { 14770a708f8fSGustavo F. Padovan count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; 14780a708f8fSGustavo F. Padovan memcpy(skb_put(skb, count), data, count); 14790a708f8fSGustavo F. Padovan data += count; 14800a708f8fSGustavo F. Padovan } 14810a708f8fSGustavo F. Padovan 14820a708f8fSGustavo F. Padovan len -= skb->len; 14830a708f8fSGustavo F. Padovan 14840a708f8fSGustavo F. Padovan /* Continuation fragments (no L2CAP header) */ 14850a708f8fSGustavo F. Padovan frag = &skb_shinfo(skb)->frag_list; 14860a708f8fSGustavo F. Padovan while (len) { 14870a708f8fSGustavo F. Padovan count = min_t(unsigned int, conn->mtu, len); 14880a708f8fSGustavo F. Padovan 14890a708f8fSGustavo F. Padovan *frag = bt_skb_alloc(count, GFP_ATOMIC); 14900a708f8fSGustavo F. Padovan if (!*frag) 14910a708f8fSGustavo F. Padovan goto fail; 14920a708f8fSGustavo F. Padovan 14930a708f8fSGustavo F. Padovan memcpy(skb_put(*frag, count), data, count); 14940a708f8fSGustavo F. Padovan 14950a708f8fSGustavo F. Padovan len -= count; 14960a708f8fSGustavo F. Padovan data += count; 14970a708f8fSGustavo F. Padovan 14980a708f8fSGustavo F. Padovan frag = &(*frag)->next; 14990a708f8fSGustavo F. Padovan } 15000a708f8fSGustavo F. Padovan 15010a708f8fSGustavo F. Padovan return skb; 15020a708f8fSGustavo F. Padovan 15030a708f8fSGustavo F. Padovan fail: 15040a708f8fSGustavo F. Padovan kfree_skb(skb); 15050a708f8fSGustavo F. Padovan return NULL; 15060a708f8fSGustavo F. Padovan } 15070a708f8fSGustavo F. Padovan 15080a708f8fSGustavo F. Padovan static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) 15090a708f8fSGustavo F. Padovan { 15100a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 15110a708f8fSGustavo F. Padovan int len; 15120a708f8fSGustavo F. Padovan 15130a708f8fSGustavo F. Padovan len = L2CAP_CONF_OPT_SIZE + opt->len; 15140a708f8fSGustavo F. Padovan *ptr += len; 15150a708f8fSGustavo F. Padovan 15160a708f8fSGustavo F. Padovan *type = opt->type; 15170a708f8fSGustavo F. Padovan *olen = opt->len; 15180a708f8fSGustavo F. Padovan 15190a708f8fSGustavo F. Padovan switch (opt->len) { 15200a708f8fSGustavo F. Padovan case 1: 15210a708f8fSGustavo F. Padovan *val = *((u8 *) opt->val); 15220a708f8fSGustavo F. Padovan break; 15230a708f8fSGustavo F. Padovan 15240a708f8fSGustavo F. Padovan case 2: 15250a708f8fSGustavo F. Padovan *val = get_unaligned_le16(opt->val); 15260a708f8fSGustavo F. Padovan break; 15270a708f8fSGustavo F. Padovan 15280a708f8fSGustavo F. Padovan case 4: 15290a708f8fSGustavo F. Padovan *val = get_unaligned_le32(opt->val); 15300a708f8fSGustavo F. Padovan break; 15310a708f8fSGustavo F. Padovan 15320a708f8fSGustavo F. Padovan default: 15330a708f8fSGustavo F. Padovan *val = (unsigned long) opt->val; 15340a708f8fSGustavo F. Padovan break; 15350a708f8fSGustavo F. Padovan } 15360a708f8fSGustavo F. Padovan 15370a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); 15380a708f8fSGustavo F. Padovan return len; 15390a708f8fSGustavo F. Padovan } 15400a708f8fSGustavo F. Padovan 15410a708f8fSGustavo F. Padovan static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) 15420a708f8fSGustavo F. Padovan { 15430a708f8fSGustavo F. Padovan struct l2cap_conf_opt *opt = *ptr; 15440a708f8fSGustavo F. Padovan 15450a708f8fSGustavo F. Padovan BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); 15460a708f8fSGustavo F. Padovan 15470a708f8fSGustavo F. Padovan opt->type = type; 15480a708f8fSGustavo F. Padovan opt->len = len; 15490a708f8fSGustavo F. Padovan 15500a708f8fSGustavo F. Padovan switch (len) { 15510a708f8fSGustavo F. Padovan case 1: 15520a708f8fSGustavo F. Padovan *((u8 *) opt->val) = val; 15530a708f8fSGustavo F. Padovan break; 15540a708f8fSGustavo F. Padovan 15550a708f8fSGustavo F. Padovan case 2: 15560a708f8fSGustavo F. Padovan put_unaligned_le16(val, opt->val); 15570a708f8fSGustavo F. Padovan break; 15580a708f8fSGustavo F. Padovan 15590a708f8fSGustavo F. Padovan case 4: 15600a708f8fSGustavo F. Padovan put_unaligned_le32(val, opt->val); 15610a708f8fSGustavo F. Padovan break; 15620a708f8fSGustavo F. Padovan 15630a708f8fSGustavo F. Padovan default: 15640a708f8fSGustavo F. Padovan memcpy(opt->val, (void *) val, len); 15650a708f8fSGustavo F. Padovan break; 15660a708f8fSGustavo F. Padovan } 15670a708f8fSGustavo F. Padovan 15680a708f8fSGustavo F. Padovan *ptr += L2CAP_CONF_OPT_SIZE + len; 15690a708f8fSGustavo F. Padovan } 15700a708f8fSGustavo F. Padovan 15710a708f8fSGustavo F. Padovan static void l2cap_ack_timeout(unsigned long arg) 15720a708f8fSGustavo F. Padovan { 1573525cd185SGustavo F. Padovan struct l2cap_chan *chan = (void *) arg; 15740a708f8fSGustavo F. Padovan 1575525cd185SGustavo F. Padovan bh_lock_sock(chan->sk); 1576525cd185SGustavo F. Padovan l2cap_send_ack(chan); 1577525cd185SGustavo F. Padovan bh_unlock_sock(chan->sk); 15780a708f8fSGustavo F. Padovan } 15790a708f8fSGustavo F. Padovan 1580525cd185SGustavo F. Padovan static inline void l2cap_ertm_init(struct l2cap_chan *chan) 15810a708f8fSGustavo F. Padovan { 1582525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 1583525cd185SGustavo F. Padovan 158442e5c802SGustavo F. Padovan chan->expected_ack_seq = 0; 15856a026610SGustavo F. Padovan chan->unacked_frames = 0; 158642e5c802SGustavo F. Padovan chan->buffer_seq = 0; 15876a026610SGustavo F. Padovan chan->num_acked = 0; 15886a026610SGustavo F. Padovan chan->frames_sent = 0; 15890a708f8fSGustavo F. Padovan 1590e92c8e70SGustavo F. Padovan setup_timer(&chan->retrans_timer, l2cap_retrans_timeout, 1591e92c8e70SGustavo F. Padovan (unsigned long) chan); 1592e92c8e70SGustavo F. Padovan setup_timer(&chan->monitor_timer, l2cap_monitor_timeout, 1593e92c8e70SGustavo F. Padovan (unsigned long) chan); 1594e92c8e70SGustavo F. Padovan setup_timer(&chan->ack_timer, l2cap_ack_timeout, (unsigned long) chan); 15950a708f8fSGustavo F. Padovan 1596f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->srej_q); 1597f1c6775bSGustavo F. Padovan skb_queue_head_init(&chan->busy_q); 15980a708f8fSGustavo F. Padovan 1599311bb895SGustavo F. Padovan INIT_WORK(&chan->busy_work, l2cap_busy_work); 16000a708f8fSGustavo F. Padovan 16010a708f8fSGustavo F. Padovan sk->sk_backlog_rcv = l2cap_ertm_data_rcv; 16020a708f8fSGustavo F. Padovan } 16030a708f8fSGustavo F. Padovan 16040a708f8fSGustavo F. Padovan static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) 16050a708f8fSGustavo F. Padovan { 16060a708f8fSGustavo F. Padovan switch (mode) { 16070a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 16080a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 16090a708f8fSGustavo F. Padovan if (l2cap_mode_supported(mode, remote_feat_mask)) 16100a708f8fSGustavo F. Padovan return mode; 16110a708f8fSGustavo F. Padovan /* fall through */ 16120a708f8fSGustavo F. Padovan default: 16130a708f8fSGustavo F. Padovan return L2CAP_MODE_BASIC; 16140a708f8fSGustavo F. Padovan } 16150a708f8fSGustavo F. Padovan } 16160a708f8fSGustavo F. Padovan 1617710f9b0aSGustavo F. Padovan static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) 16180a708f8fSGustavo F. Padovan { 161973ffa904SGustavo F. Padovan struct sock *sk = chan->sk; 16200a708f8fSGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(sk); 16210a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 16220a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = pi->mode }; 16230a708f8fSGustavo F. Padovan void *ptr = req->data; 16240a708f8fSGustavo F. Padovan 16250a708f8fSGustavo F. Padovan BT_DBG("sk %p", sk); 16260a708f8fSGustavo F. Padovan 162773ffa904SGustavo F. Padovan if (chan->num_conf_req || chan->num_conf_rsp) 16280a708f8fSGustavo F. Padovan goto done; 16290a708f8fSGustavo F. Padovan 16300a708f8fSGustavo F. Padovan switch (pi->mode) { 16310a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 16320a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 16330a708f8fSGustavo F. Padovan if (pi->conf_state & L2CAP_CONF_STATE2_DEVICE) 16340a708f8fSGustavo F. Padovan break; 16350a708f8fSGustavo F. Padovan 16360a708f8fSGustavo F. Padovan /* fall through */ 16370a708f8fSGustavo F. Padovan default: 16380a708f8fSGustavo F. Padovan pi->mode = l2cap_select_mode(rfc.mode, pi->conn->feat_mask); 16390a708f8fSGustavo F. Padovan break; 16400a708f8fSGustavo F. Padovan } 16410a708f8fSGustavo F. Padovan 16420a708f8fSGustavo F. Padovan done: 16430a708f8fSGustavo F. Padovan if (pi->imtu != L2CAP_DEFAULT_MTU) 16440a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->imtu); 16450a708f8fSGustavo F. Padovan 16460a708f8fSGustavo F. Padovan switch (pi->mode) { 16470a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 16480a708f8fSGustavo F. Padovan if (!(pi->conn->feat_mask & L2CAP_FEAT_ERTM) && 16490a708f8fSGustavo F. Padovan !(pi->conn->feat_mask & L2CAP_FEAT_STREAMING)) 16500a708f8fSGustavo F. Padovan break; 16510a708f8fSGustavo F. Padovan 16520a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_BASIC; 16530a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 16540a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 16550a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 16560a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 16570a708f8fSGustavo F. Padovan rfc.max_pdu_size = 0; 16580a708f8fSGustavo F. Padovan 16590a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 16600a708f8fSGustavo F. Padovan (unsigned long) &rfc); 16610a708f8fSGustavo F. Padovan break; 16620a708f8fSGustavo F. Padovan 16630a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 16640a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_ERTM; 16650a708f8fSGustavo F. Padovan rfc.txwin_size = pi->tx_win; 16660a708f8fSGustavo F. Padovan rfc.max_transmit = pi->max_tx; 16670a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 16680a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 16690a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); 16700a708f8fSGustavo F. Padovan if (L2CAP_DEFAULT_MAX_PDU_SIZE > pi->conn->mtu - 10) 16710a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(pi->conn->mtu - 10); 16720a708f8fSGustavo F. Padovan 16730a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 16740a708f8fSGustavo F. Padovan (unsigned long) &rfc); 16750a708f8fSGustavo F. Padovan 16760a708f8fSGustavo F. Padovan if (!(pi->conn->feat_mask & L2CAP_FEAT_FCS)) 16770a708f8fSGustavo F. Padovan break; 16780a708f8fSGustavo F. Padovan 16790a708f8fSGustavo F. Padovan if (pi->fcs == L2CAP_FCS_NONE || 16800a708f8fSGustavo F. Padovan pi->conf_state & L2CAP_CONF_NO_FCS_RECV) { 16810a708f8fSGustavo F. Padovan pi->fcs = L2CAP_FCS_NONE; 16820a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, pi->fcs); 16830a708f8fSGustavo F. Padovan } 16840a708f8fSGustavo F. Padovan break; 16850a708f8fSGustavo F. Padovan 16860a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 16870a708f8fSGustavo F. Padovan rfc.mode = L2CAP_MODE_STREAMING; 16880a708f8fSGustavo F. Padovan rfc.txwin_size = 0; 16890a708f8fSGustavo F. Padovan rfc.max_transmit = 0; 16900a708f8fSGustavo F. Padovan rfc.retrans_timeout = 0; 16910a708f8fSGustavo F. Padovan rfc.monitor_timeout = 0; 16920a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); 16930a708f8fSGustavo F. Padovan if (L2CAP_DEFAULT_MAX_PDU_SIZE > pi->conn->mtu - 10) 16940a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(pi->conn->mtu - 10); 16950a708f8fSGustavo F. Padovan 16960a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), 16970a708f8fSGustavo F. Padovan (unsigned long) &rfc); 16980a708f8fSGustavo F. Padovan 16990a708f8fSGustavo F. Padovan if (!(pi->conn->feat_mask & L2CAP_FEAT_FCS)) 17000a708f8fSGustavo F. Padovan break; 17010a708f8fSGustavo F. Padovan 17020a708f8fSGustavo F. Padovan if (pi->fcs == L2CAP_FCS_NONE || 17030a708f8fSGustavo F. Padovan pi->conf_state & L2CAP_CONF_NO_FCS_RECV) { 17040a708f8fSGustavo F. Padovan pi->fcs = L2CAP_FCS_NONE; 17050a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, pi->fcs); 17060a708f8fSGustavo F. Padovan } 17070a708f8fSGustavo F. Padovan break; 17080a708f8fSGustavo F. Padovan } 17090a708f8fSGustavo F. Padovan 17100a708f8fSGustavo F. Padovan req->dcid = cpu_to_le16(pi->dcid); 17110a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0); 17120a708f8fSGustavo F. Padovan 17130a708f8fSGustavo F. Padovan return ptr - data; 17140a708f8fSGustavo F. Padovan } 17150a708f8fSGustavo F. Padovan 171673ffa904SGustavo F. Padovan static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) 17170a708f8fSGustavo F. Padovan { 171873ffa904SGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(chan->sk); 17190a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 17200a708f8fSGustavo F. Padovan void *ptr = rsp->data; 172173ffa904SGustavo F. Padovan void *req = chan->conf_req; 172273ffa904SGustavo F. Padovan int len = chan->conf_len; 17230a708f8fSGustavo F. Padovan int type, hint, olen; 17240a708f8fSGustavo F. Padovan unsigned long val; 17250a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; 17260a708f8fSGustavo F. Padovan u16 mtu = L2CAP_DEFAULT_MTU; 17270a708f8fSGustavo F. Padovan u16 result = L2CAP_CONF_SUCCESS; 17280a708f8fSGustavo F. Padovan 172973ffa904SGustavo F. Padovan BT_DBG("chan %p", chan); 17300a708f8fSGustavo F. Padovan 17310a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 17320a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&req, &type, &olen, &val); 17330a708f8fSGustavo F. Padovan 17340a708f8fSGustavo F. Padovan hint = type & L2CAP_CONF_HINT; 17350a708f8fSGustavo F. Padovan type &= L2CAP_CONF_MASK; 17360a708f8fSGustavo F. Padovan 17370a708f8fSGustavo F. Padovan switch (type) { 17380a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 17390a708f8fSGustavo F. Padovan mtu = val; 17400a708f8fSGustavo F. Padovan break; 17410a708f8fSGustavo F. Padovan 17420a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 17430a708f8fSGustavo F. Padovan pi->flush_to = val; 17440a708f8fSGustavo F. Padovan break; 17450a708f8fSGustavo F. Padovan 17460a708f8fSGustavo F. Padovan case L2CAP_CONF_QOS: 17470a708f8fSGustavo F. Padovan break; 17480a708f8fSGustavo F. Padovan 17490a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 17500a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 17510a708f8fSGustavo F. Padovan memcpy(&rfc, (void *) val, olen); 17520a708f8fSGustavo F. Padovan break; 17530a708f8fSGustavo F. Padovan 17540a708f8fSGustavo F. Padovan case L2CAP_CONF_FCS: 17550a708f8fSGustavo F. Padovan if (val == L2CAP_FCS_NONE) 17560a708f8fSGustavo F. Padovan pi->conf_state |= L2CAP_CONF_NO_FCS_RECV; 17570a708f8fSGustavo F. Padovan 17580a708f8fSGustavo F. Padovan break; 17590a708f8fSGustavo F. Padovan 17600a708f8fSGustavo F. Padovan default: 17610a708f8fSGustavo F. Padovan if (hint) 17620a708f8fSGustavo F. Padovan break; 17630a708f8fSGustavo F. Padovan 17640a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNKNOWN; 17650a708f8fSGustavo F. Padovan *((u8 *) ptr++) = type; 17660a708f8fSGustavo F. Padovan break; 17670a708f8fSGustavo F. Padovan } 17680a708f8fSGustavo F. Padovan } 17690a708f8fSGustavo F. Padovan 177073ffa904SGustavo F. Padovan if (chan->num_conf_rsp || chan->num_conf_req > 1) 17710a708f8fSGustavo F. Padovan goto done; 17720a708f8fSGustavo F. Padovan 17730a708f8fSGustavo F. Padovan switch (pi->mode) { 17740a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 17750a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 17760a708f8fSGustavo F. Padovan if (!(pi->conf_state & L2CAP_CONF_STATE2_DEVICE)) { 17770a708f8fSGustavo F. Padovan pi->mode = l2cap_select_mode(rfc.mode, 17780a708f8fSGustavo F. Padovan pi->conn->feat_mask); 17790a708f8fSGustavo F. Padovan break; 17800a708f8fSGustavo F. Padovan } 17810a708f8fSGustavo F. Padovan 17820a708f8fSGustavo F. Padovan if (pi->mode != rfc.mode) 17830a708f8fSGustavo F. Padovan return -ECONNREFUSED; 17840a708f8fSGustavo F. Padovan 17850a708f8fSGustavo F. Padovan break; 17860a708f8fSGustavo F. Padovan } 17870a708f8fSGustavo F. Padovan 17880a708f8fSGustavo F. Padovan done: 17890a708f8fSGustavo F. Padovan if (pi->mode != rfc.mode) { 17900a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 17910a708f8fSGustavo F. Padovan rfc.mode = pi->mode; 17920a708f8fSGustavo F. Padovan 179373ffa904SGustavo F. Padovan if (chan->num_conf_rsp == 1) 17940a708f8fSGustavo F. Padovan return -ECONNREFUSED; 17950a708f8fSGustavo F. Padovan 17960a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 17970a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 17980a708f8fSGustavo F. Padovan } 17990a708f8fSGustavo F. Padovan 18000a708f8fSGustavo F. Padovan 18010a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) { 18020a708f8fSGustavo F. Padovan /* Configure output options and let the other side know 18030a708f8fSGustavo F. Padovan * which ones we don't like. */ 18040a708f8fSGustavo F. Padovan 18050a708f8fSGustavo F. Padovan if (mtu < L2CAP_DEFAULT_MIN_MTU) 18060a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 18070a708f8fSGustavo F. Padovan else { 18080a708f8fSGustavo F. Padovan pi->omtu = mtu; 18090a708f8fSGustavo F. Padovan pi->conf_state |= L2CAP_CONF_MTU_DONE; 18100a708f8fSGustavo F. Padovan } 18110a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->omtu); 18120a708f8fSGustavo F. Padovan 18130a708f8fSGustavo F. Padovan switch (rfc.mode) { 18140a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 18150a708f8fSGustavo F. Padovan pi->fcs = L2CAP_FCS_NONE; 18160a708f8fSGustavo F. Padovan pi->conf_state |= L2CAP_CONF_MODE_DONE; 18170a708f8fSGustavo F. Padovan break; 18180a708f8fSGustavo F. Padovan 18190a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 18202c03a7a4SGustavo F. Padovan chan->remote_tx_win = rfc.txwin_size; 18212c03a7a4SGustavo F. Padovan chan->remote_max_tx = rfc.max_transmit; 18220a708f8fSGustavo F. Padovan 18230a708f8fSGustavo F. Padovan if (le16_to_cpu(rfc.max_pdu_size) > pi->conn->mtu - 10) 18240a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(pi->conn->mtu - 10); 18250a708f8fSGustavo F. Padovan 18262c03a7a4SGustavo F. Padovan chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); 18270a708f8fSGustavo F. Padovan 18280a708f8fSGustavo F. Padovan rfc.retrans_timeout = 18290a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO); 18300a708f8fSGustavo F. Padovan rfc.monitor_timeout = 18310a708f8fSGustavo F. Padovan le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO); 18320a708f8fSGustavo F. Padovan 18330a708f8fSGustavo F. Padovan pi->conf_state |= L2CAP_CONF_MODE_DONE; 18340a708f8fSGustavo F. Padovan 18350a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 18360a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 18370a708f8fSGustavo F. Padovan 18380a708f8fSGustavo F. Padovan break; 18390a708f8fSGustavo F. Padovan 18400a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 18410a708f8fSGustavo F. Padovan if (le16_to_cpu(rfc.max_pdu_size) > pi->conn->mtu - 10) 18420a708f8fSGustavo F. Padovan rfc.max_pdu_size = cpu_to_le16(pi->conn->mtu - 10); 18430a708f8fSGustavo F. Padovan 18442c03a7a4SGustavo F. Padovan chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); 18450a708f8fSGustavo F. Padovan 18460a708f8fSGustavo F. Padovan pi->conf_state |= L2CAP_CONF_MODE_DONE; 18470a708f8fSGustavo F. Padovan 18480a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 18490a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 18500a708f8fSGustavo F. Padovan 18510a708f8fSGustavo F. Padovan break; 18520a708f8fSGustavo F. Padovan 18530a708f8fSGustavo F. Padovan default: 18540a708f8fSGustavo F. Padovan result = L2CAP_CONF_UNACCEPT; 18550a708f8fSGustavo F. Padovan 18560a708f8fSGustavo F. Padovan memset(&rfc, 0, sizeof(rfc)); 18570a708f8fSGustavo F. Padovan rfc.mode = pi->mode; 18580a708f8fSGustavo F. Padovan } 18590a708f8fSGustavo F. Padovan 18600a708f8fSGustavo F. Padovan if (result == L2CAP_CONF_SUCCESS) 18610a708f8fSGustavo F. Padovan pi->conf_state |= L2CAP_CONF_OUTPUT_DONE; 18620a708f8fSGustavo F. Padovan } 18630a708f8fSGustavo F. Padovan rsp->scid = cpu_to_le16(pi->dcid); 18640a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 18650a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(0x0000); 18660a708f8fSGustavo F. Padovan 18670a708f8fSGustavo F. Padovan return ptr - data; 18680a708f8fSGustavo F. Padovan } 18690a708f8fSGustavo F. Padovan 18700a708f8fSGustavo F. Padovan static int l2cap_parse_conf_rsp(struct sock *sk, void *rsp, int len, void *data, u16 *result) 18710a708f8fSGustavo F. Padovan { 18720a708f8fSGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(sk); 18730a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = data; 18740a708f8fSGustavo F. Padovan void *ptr = req->data; 18750a708f8fSGustavo F. Padovan int type, olen; 18760a708f8fSGustavo F. Padovan unsigned long val; 18770a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 18780a708f8fSGustavo F. Padovan 18790a708f8fSGustavo F. Padovan BT_DBG("sk %p, rsp %p, len %d, req %p", sk, rsp, len, data); 18800a708f8fSGustavo F. Padovan 18810a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 18820a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 18830a708f8fSGustavo F. Padovan 18840a708f8fSGustavo F. Padovan switch (type) { 18850a708f8fSGustavo F. Padovan case L2CAP_CONF_MTU: 18860a708f8fSGustavo F. Padovan if (val < L2CAP_DEFAULT_MIN_MTU) { 18870a708f8fSGustavo F. Padovan *result = L2CAP_CONF_UNACCEPT; 18880a708f8fSGustavo F. Padovan pi->imtu = L2CAP_DEFAULT_MIN_MTU; 18890a708f8fSGustavo F. Padovan } else 18900a708f8fSGustavo F. Padovan pi->imtu = val; 18910a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->imtu); 18920a708f8fSGustavo F. Padovan break; 18930a708f8fSGustavo F. Padovan 18940a708f8fSGustavo F. Padovan case L2CAP_CONF_FLUSH_TO: 18950a708f8fSGustavo F. Padovan pi->flush_to = val; 18960a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 18970a708f8fSGustavo F. Padovan 2, pi->flush_to); 18980a708f8fSGustavo F. Padovan break; 18990a708f8fSGustavo F. Padovan 19000a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 19010a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 19020a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 19030a708f8fSGustavo F. Padovan 19040a708f8fSGustavo F. Padovan if ((pi->conf_state & L2CAP_CONF_STATE2_DEVICE) && 19050a708f8fSGustavo F. Padovan rfc.mode != pi->mode) 19060a708f8fSGustavo F. Padovan return -ECONNREFUSED; 19070a708f8fSGustavo F. Padovan 19080a708f8fSGustavo F. Padovan pi->fcs = 0; 19090a708f8fSGustavo F. Padovan 19100a708f8fSGustavo F. Padovan l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, 19110a708f8fSGustavo F. Padovan sizeof(rfc), (unsigned long) &rfc); 19120a708f8fSGustavo F. Padovan break; 19130a708f8fSGustavo F. Padovan } 19140a708f8fSGustavo F. Padovan } 19150a708f8fSGustavo F. Padovan 19160a708f8fSGustavo F. Padovan if (pi->mode == L2CAP_MODE_BASIC && pi->mode != rfc.mode) 19170a708f8fSGustavo F. Padovan return -ECONNREFUSED; 19180a708f8fSGustavo F. Padovan 19190a708f8fSGustavo F. Padovan pi->mode = rfc.mode; 19200a708f8fSGustavo F. Padovan 19210a708f8fSGustavo F. Padovan if (*result == L2CAP_CONF_SUCCESS) { 19220a708f8fSGustavo F. Padovan switch (rfc.mode) { 19230a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 19240a708f8fSGustavo F. Padovan pi->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 19250a708f8fSGustavo F. Padovan pi->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 19260a708f8fSGustavo F. Padovan pi->mps = le16_to_cpu(rfc.max_pdu_size); 19270a708f8fSGustavo F. Padovan break; 19280a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 19290a708f8fSGustavo F. Padovan pi->mps = le16_to_cpu(rfc.max_pdu_size); 19300a708f8fSGustavo F. Padovan } 19310a708f8fSGustavo F. Padovan } 19320a708f8fSGustavo F. Padovan 19330a708f8fSGustavo F. Padovan req->dcid = cpu_to_le16(pi->dcid); 19340a708f8fSGustavo F. Padovan req->flags = cpu_to_le16(0x0000); 19350a708f8fSGustavo F. Padovan 19360a708f8fSGustavo F. Padovan return ptr - data; 19370a708f8fSGustavo F. Padovan } 19380a708f8fSGustavo F. Padovan 19390a708f8fSGustavo F. Padovan static int l2cap_build_conf_rsp(struct sock *sk, void *data, u16 result, u16 flags) 19400a708f8fSGustavo F. Padovan { 19410a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = data; 19420a708f8fSGustavo F. Padovan void *ptr = rsp->data; 19430a708f8fSGustavo F. Padovan 19440a708f8fSGustavo F. Padovan BT_DBG("sk %p", sk); 19450a708f8fSGustavo F. Padovan 19460a708f8fSGustavo F. Padovan rsp->scid = cpu_to_le16(l2cap_pi(sk)->dcid); 19470a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(result); 19480a708f8fSGustavo F. Padovan rsp->flags = cpu_to_le16(flags); 19490a708f8fSGustavo F. Padovan 19500a708f8fSGustavo F. Padovan return ptr - data; 19510a708f8fSGustavo F. Padovan } 19520a708f8fSGustavo F. Padovan 1953710f9b0aSGustavo F. Padovan void __l2cap_connect_rsp_defer(struct sock *sk) 1954710f9b0aSGustavo F. Padovan { 1955710f9b0aSGustavo F. Padovan struct l2cap_conn_rsp rsp; 1956710f9b0aSGustavo F. Padovan struct l2cap_conn *conn = l2cap_pi(sk)->conn; 1957710f9b0aSGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 1958710f9b0aSGustavo F. Padovan u8 buf[128]; 1959710f9b0aSGustavo F. Padovan 1960710f9b0aSGustavo F. Padovan sk->sk_state = BT_CONFIG; 1961710f9b0aSGustavo F. Padovan 1962710f9b0aSGustavo F. Padovan rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid); 1963710f9b0aSGustavo F. Padovan rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid); 1964710f9b0aSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); 1965710f9b0aSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 1966710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 1967710f9b0aSGustavo F. Padovan L2CAP_CONN_RSP, sizeof(rsp), &rsp); 1968710f9b0aSGustavo F. Padovan 1969710f9b0aSGustavo F. Padovan if (l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT) 1970710f9b0aSGustavo F. Padovan return; 1971710f9b0aSGustavo F. Padovan 1972710f9b0aSGustavo F. Padovan l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT; 1973710f9b0aSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 1974710f9b0aSGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 1975710f9b0aSGustavo F. Padovan chan->num_conf_req++; 1976710f9b0aSGustavo F. Padovan } 1977710f9b0aSGustavo F. Padovan 19780a708f8fSGustavo F. Padovan static void l2cap_conf_rfc_get(struct sock *sk, void *rsp, int len) 19790a708f8fSGustavo F. Padovan { 19800a708f8fSGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(sk); 19810a708f8fSGustavo F. Padovan int type, olen; 19820a708f8fSGustavo F. Padovan unsigned long val; 19830a708f8fSGustavo F. Padovan struct l2cap_conf_rfc rfc; 19840a708f8fSGustavo F. Padovan 19850a708f8fSGustavo F. Padovan BT_DBG("sk %p, rsp %p, len %d", sk, rsp, len); 19860a708f8fSGustavo F. Padovan 19870a708f8fSGustavo F. Padovan if ((pi->mode != L2CAP_MODE_ERTM) && (pi->mode != L2CAP_MODE_STREAMING)) 19880a708f8fSGustavo F. Padovan return; 19890a708f8fSGustavo F. Padovan 19900a708f8fSGustavo F. Padovan while (len >= L2CAP_CONF_OPT_SIZE) { 19910a708f8fSGustavo F. Padovan len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); 19920a708f8fSGustavo F. Padovan 19930a708f8fSGustavo F. Padovan switch (type) { 19940a708f8fSGustavo F. Padovan case L2CAP_CONF_RFC: 19950a708f8fSGustavo F. Padovan if (olen == sizeof(rfc)) 19960a708f8fSGustavo F. Padovan memcpy(&rfc, (void *)val, olen); 19970a708f8fSGustavo F. Padovan goto done; 19980a708f8fSGustavo F. Padovan } 19990a708f8fSGustavo F. Padovan } 20000a708f8fSGustavo F. Padovan 20010a708f8fSGustavo F. Padovan done: 20020a708f8fSGustavo F. Padovan switch (rfc.mode) { 20030a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 20040a708f8fSGustavo F. Padovan pi->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); 20050a708f8fSGustavo F. Padovan pi->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); 20060a708f8fSGustavo F. Padovan pi->mps = le16_to_cpu(rfc.max_pdu_size); 20070a708f8fSGustavo F. Padovan break; 20080a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 20090a708f8fSGustavo F. Padovan pi->mps = le16_to_cpu(rfc.max_pdu_size); 20100a708f8fSGustavo F. Padovan } 20110a708f8fSGustavo F. Padovan } 20120a708f8fSGustavo F. Padovan 20130a708f8fSGustavo F. Padovan static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 20140a708f8fSGustavo F. Padovan { 20150a708f8fSGustavo F. Padovan struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data; 20160a708f8fSGustavo F. Padovan 20170a708f8fSGustavo F. Padovan if (rej->reason != 0x0000) 20180a708f8fSGustavo F. Padovan return 0; 20190a708f8fSGustavo F. Padovan 20200a708f8fSGustavo F. Padovan if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && 20210a708f8fSGustavo F. Padovan cmd->ident == conn->info_ident) { 20220a708f8fSGustavo F. Padovan del_timer(&conn->info_timer); 20230a708f8fSGustavo F. Padovan 20240a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 20250a708f8fSGustavo F. Padovan conn->info_ident = 0; 20260a708f8fSGustavo F. Padovan 20270a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 20280a708f8fSGustavo F. Padovan } 20290a708f8fSGustavo F. Padovan 20300a708f8fSGustavo F. Padovan return 0; 20310a708f8fSGustavo F. Padovan } 20320a708f8fSGustavo F. Padovan 20330a708f8fSGustavo F. Padovan static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 20340a708f8fSGustavo F. Padovan { 20350a708f8fSGustavo F. Padovan struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; 20360a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 203773ffa904SGustavo F. Padovan struct l2cap_chan *chan = NULL; 20380a708f8fSGustavo F. Padovan struct sock *parent, *sk = NULL; 20390a708f8fSGustavo F. Padovan int result, status = L2CAP_CS_NO_INFO; 20400a708f8fSGustavo F. Padovan 20410a708f8fSGustavo F. Padovan u16 dcid = 0, scid = __le16_to_cpu(req->scid); 20420a708f8fSGustavo F. Padovan __le16 psm = req->psm; 20430a708f8fSGustavo F. Padovan 20440a708f8fSGustavo F. Padovan BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid); 20450a708f8fSGustavo F. Padovan 20460a708f8fSGustavo F. Padovan /* Check if we have socket listening on psm */ 20470a708f8fSGustavo F. Padovan parent = l2cap_get_sock_by_psm(BT_LISTEN, psm, conn->src); 20480a708f8fSGustavo F. Padovan if (!parent) { 20490a708f8fSGustavo F. Padovan result = L2CAP_CR_BAD_PSM; 20500a708f8fSGustavo F. Padovan goto sendresp; 20510a708f8fSGustavo F. Padovan } 20520a708f8fSGustavo F. Padovan 20530a708f8fSGustavo F. Padovan bh_lock_sock(parent); 20540a708f8fSGustavo F. Padovan 20550a708f8fSGustavo F. Padovan /* Check if the ACL is secure enough (if not SDP) */ 20560a708f8fSGustavo F. Padovan if (psm != cpu_to_le16(0x0001) && 20570a708f8fSGustavo F. Padovan !hci_conn_check_link_mode(conn->hcon)) { 20580a708f8fSGustavo F. Padovan conn->disc_reason = 0x05; 20590a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 20600a708f8fSGustavo F. Padovan goto response; 20610a708f8fSGustavo F. Padovan } 20620a708f8fSGustavo F. Padovan 20630a708f8fSGustavo F. Padovan result = L2CAP_CR_NO_MEM; 20640a708f8fSGustavo F. Padovan 20650a708f8fSGustavo F. Padovan /* Check for backlog size */ 20660a708f8fSGustavo F. Padovan if (sk_acceptq_is_full(parent)) { 20670a708f8fSGustavo F. Padovan BT_DBG("backlog full %d", parent->sk_ack_backlog); 20680a708f8fSGustavo F. Padovan goto response; 20690a708f8fSGustavo F. Padovan } 20700a708f8fSGustavo F. Padovan 20710a708f8fSGustavo F. Padovan sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC); 20720a708f8fSGustavo F. Padovan if (!sk) 20730a708f8fSGustavo F. Padovan goto response; 20740a708f8fSGustavo F. Padovan 207548454079SGustavo F. Padovan chan = l2cap_chan_alloc(sk); 207648454079SGustavo F. Padovan if (!chan) { 207748454079SGustavo F. Padovan l2cap_sock_kill(sk); 207848454079SGustavo F. Padovan goto response; 207948454079SGustavo F. Padovan } 208048454079SGustavo F. Padovan 2081baa7e1faSGustavo F. Padovan write_lock_bh(&conn->chan_lock); 20820a708f8fSGustavo F. Padovan 20830a708f8fSGustavo F. Padovan /* Check if we already have channel with that dcid */ 2084baa7e1faSGustavo F. Padovan if (__l2cap_get_chan_by_dcid(conn, scid)) { 2085baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 20860a708f8fSGustavo F. Padovan sock_set_flag(sk, SOCK_ZAPPED); 20870a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 20880a708f8fSGustavo F. Padovan goto response; 20890a708f8fSGustavo F. Padovan } 20900a708f8fSGustavo F. Padovan 20910a708f8fSGustavo F. Padovan hci_conn_hold(conn->hcon); 20920a708f8fSGustavo F. Padovan 20930a708f8fSGustavo F. Padovan l2cap_sock_init(sk, parent); 20940a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->src, conn->src); 20950a708f8fSGustavo F. Padovan bacpy(&bt_sk(sk)->dst, conn->dst); 20960a708f8fSGustavo F. Padovan l2cap_pi(sk)->psm = psm; 20970a708f8fSGustavo F. Padovan l2cap_pi(sk)->dcid = scid; 20980a708f8fSGustavo F. Padovan 2099d1010240SGustavo F. Padovan bt_accept_enqueue(parent, sk); 2100d1010240SGustavo F. Padovan 210148454079SGustavo F. Padovan __l2cap_chan_add(conn, chan); 210248454079SGustavo F. Padovan 210348454079SGustavo F. Padovan l2cap_pi(sk)->chan = chan; 210448454079SGustavo F. Padovan 21050a708f8fSGustavo F. Padovan dcid = l2cap_pi(sk)->scid; 21060a708f8fSGustavo F. Padovan 21070a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, sk->sk_sndtimeo); 21080a708f8fSGustavo F. Padovan 2109fc7f8a7eSGustavo F. Padovan chan->ident = cmd->ident; 21100a708f8fSGustavo F. Padovan 21110a708f8fSGustavo F. Padovan if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { 21120a708f8fSGustavo F. Padovan if (l2cap_check_security(sk)) { 21130a708f8fSGustavo F. Padovan if (bt_sk(sk)->defer_setup) { 21140a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT2; 21150a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 21160a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHOR_PEND; 21170a708f8fSGustavo F. Padovan parent->sk_data_ready(parent, 0); 21180a708f8fSGustavo F. Padovan } else { 21190a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 21200a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 21210a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 21220a708f8fSGustavo F. Padovan } 21230a708f8fSGustavo F. Padovan } else { 21240a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT2; 21250a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 21260a708f8fSGustavo F. Padovan status = L2CAP_CS_AUTHEN_PEND; 21270a708f8fSGustavo F. Padovan } 21280a708f8fSGustavo F. Padovan } else { 21290a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECT2; 21300a708f8fSGustavo F. Padovan result = L2CAP_CR_PEND; 21310a708f8fSGustavo F. Padovan status = L2CAP_CS_NO_INFO; 21320a708f8fSGustavo F. Padovan } 21330a708f8fSGustavo F. Padovan 2134baa7e1faSGustavo F. Padovan write_unlock_bh(&conn->chan_lock); 21350a708f8fSGustavo F. Padovan 21360a708f8fSGustavo F. Padovan response: 21370a708f8fSGustavo F. Padovan bh_unlock_sock(parent); 21380a708f8fSGustavo F. Padovan 21390a708f8fSGustavo F. Padovan sendresp: 21400a708f8fSGustavo F. Padovan rsp.scid = cpu_to_le16(scid); 21410a708f8fSGustavo F. Padovan rsp.dcid = cpu_to_le16(dcid); 21420a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 21430a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(status); 21440a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); 21450a708f8fSGustavo F. Padovan 21460a708f8fSGustavo F. Padovan if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { 21470a708f8fSGustavo F. Padovan struct l2cap_info_req info; 21480a708f8fSGustavo F. Padovan info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 21490a708f8fSGustavo F. Padovan 21500a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; 21510a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 21520a708f8fSGustavo F. Padovan 21530a708f8fSGustavo F. Padovan mod_timer(&conn->info_timer, jiffies + 21540a708f8fSGustavo F. Padovan msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); 21550a708f8fSGustavo F. Padovan 21560a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 21570a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(info), &info); 21580a708f8fSGustavo F. Padovan } 21590a708f8fSGustavo F. Padovan 216073ffa904SGustavo F. Padovan if (chan && !(l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT) && 21610a708f8fSGustavo F. Padovan result == L2CAP_CR_SUCCESS) { 21620a708f8fSGustavo F. Padovan u8 buf[128]; 21630a708f8fSGustavo F. Padovan l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT; 21640a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 216573ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 216673ffa904SGustavo F. Padovan chan->num_conf_req++; 21670a708f8fSGustavo F. Padovan } 21680a708f8fSGustavo F. Padovan 21690a708f8fSGustavo F. Padovan return 0; 21700a708f8fSGustavo F. Padovan } 21710a708f8fSGustavo F. Padovan 21720a708f8fSGustavo F. Padovan static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 21730a708f8fSGustavo F. Padovan { 21740a708f8fSGustavo F. Padovan struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; 21750a708f8fSGustavo F. Padovan u16 scid, dcid, result, status; 217648454079SGustavo F. Padovan struct l2cap_chan *chan; 21770a708f8fSGustavo F. Padovan struct sock *sk; 21780a708f8fSGustavo F. Padovan u8 req[128]; 21790a708f8fSGustavo F. Padovan 21800a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 21810a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 21820a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 21830a708f8fSGustavo F. Padovan status = __le16_to_cpu(rsp->status); 21840a708f8fSGustavo F. Padovan 21850a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status); 21860a708f8fSGustavo F. Padovan 21870a708f8fSGustavo F. Padovan if (scid) { 2188baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 218948454079SGustavo F. Padovan if (!chan) 21900a708f8fSGustavo F. Padovan return -EFAULT; 21910a708f8fSGustavo F. Padovan } else { 2192baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_ident(conn, cmd->ident); 219348454079SGustavo F. Padovan if (!chan) 21940a708f8fSGustavo F. Padovan return -EFAULT; 21950a708f8fSGustavo F. Padovan } 21960a708f8fSGustavo F. Padovan 219748454079SGustavo F. Padovan sk = chan->sk; 219848454079SGustavo F. Padovan 21990a708f8fSGustavo F. Padovan switch (result) { 22000a708f8fSGustavo F. Padovan case L2CAP_CR_SUCCESS: 22010a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 2202fc7f8a7eSGustavo F. Padovan chan->ident = 0; 22030a708f8fSGustavo F. Padovan l2cap_pi(sk)->dcid = dcid; 22040a708f8fSGustavo F. Padovan l2cap_pi(sk)->conf_state &= ~L2CAP_CONF_CONNECT_PEND; 22050a708f8fSGustavo F. Padovan 22060a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT) 22070a708f8fSGustavo F. Padovan break; 22080a708f8fSGustavo F. Padovan 22090a708f8fSGustavo F. Padovan l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT; 22100a708f8fSGustavo F. Padovan 22110a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 221273ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, req), req); 221373ffa904SGustavo F. Padovan chan->num_conf_req++; 22140a708f8fSGustavo F. Padovan break; 22150a708f8fSGustavo F. Padovan 22160a708f8fSGustavo F. Padovan case L2CAP_CR_PEND: 22170a708f8fSGustavo F. Padovan l2cap_pi(sk)->conf_state |= L2CAP_CONF_CONNECT_PEND; 22180a708f8fSGustavo F. Padovan break; 22190a708f8fSGustavo F. Padovan 22200a708f8fSGustavo F. Padovan default: 22210a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 22220a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 22230a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 22240a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 22250a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, HZ / 5); 22260a708f8fSGustavo F. Padovan break; 22270a708f8fSGustavo F. Padovan } 22280a708f8fSGustavo F. Padovan 222948454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNREFUSED); 22300a708f8fSGustavo F. Padovan break; 22310a708f8fSGustavo F. Padovan } 22320a708f8fSGustavo F. Padovan 22330a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 22340a708f8fSGustavo F. Padovan return 0; 22350a708f8fSGustavo F. Padovan } 22360a708f8fSGustavo F. Padovan 22370a708f8fSGustavo F. Padovan static inline void set_default_fcs(struct l2cap_pinfo *pi) 22380a708f8fSGustavo F. Padovan { 22390a708f8fSGustavo F. Padovan /* FCS is enabled only in ERTM or streaming mode, if one or both 22400a708f8fSGustavo F. Padovan * sides request it. 22410a708f8fSGustavo F. Padovan */ 22420a708f8fSGustavo F. Padovan if (pi->mode != L2CAP_MODE_ERTM && pi->mode != L2CAP_MODE_STREAMING) 22430a708f8fSGustavo F. Padovan pi->fcs = L2CAP_FCS_NONE; 22440a708f8fSGustavo F. Padovan else if (!(pi->conf_state & L2CAP_CONF_NO_FCS_RECV)) 22450a708f8fSGustavo F. Padovan pi->fcs = L2CAP_FCS_CRC16; 22460a708f8fSGustavo F. Padovan } 22470a708f8fSGustavo F. Padovan 22480a708f8fSGustavo F. Padovan static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 22490a708f8fSGustavo F. Padovan { 22500a708f8fSGustavo F. Padovan struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; 22510a708f8fSGustavo F. Padovan u16 dcid, flags; 22520a708f8fSGustavo F. Padovan u8 rsp[64]; 225348454079SGustavo F. Padovan struct l2cap_chan *chan; 22540a708f8fSGustavo F. Padovan struct sock *sk; 22550a708f8fSGustavo F. Padovan int len; 22560a708f8fSGustavo F. Padovan 22570a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 22580a708f8fSGustavo F. Padovan flags = __le16_to_cpu(req->flags); 22590a708f8fSGustavo F. Padovan 22600a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); 22610a708f8fSGustavo F. Padovan 2262baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 226348454079SGustavo F. Padovan if (!chan) 22640a708f8fSGustavo F. Padovan return -ENOENT; 22650a708f8fSGustavo F. Padovan 226648454079SGustavo F. Padovan sk = chan->sk; 226748454079SGustavo F. Padovan 22680a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONFIG) { 22690a708f8fSGustavo F. Padovan struct l2cap_cmd_rej rej; 22700a708f8fSGustavo F. Padovan 22710a708f8fSGustavo F. Padovan rej.reason = cpu_to_le16(0x0002); 22720a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, 22730a708f8fSGustavo F. Padovan sizeof(rej), &rej); 22740a708f8fSGustavo F. Padovan goto unlock; 22750a708f8fSGustavo F. Padovan } 22760a708f8fSGustavo F. Padovan 22770a708f8fSGustavo F. Padovan /* Reject if config buffer is too small. */ 22780a708f8fSGustavo F. Padovan len = cmd_len - sizeof(*req); 227973ffa904SGustavo F. Padovan if (chan->conf_len + len > sizeof(chan->conf_req)) { 22800a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 22810a708f8fSGustavo F. Padovan l2cap_build_conf_rsp(sk, rsp, 22820a708f8fSGustavo F. Padovan L2CAP_CONF_REJECT, flags), rsp); 22830a708f8fSGustavo F. Padovan goto unlock; 22840a708f8fSGustavo F. Padovan } 22850a708f8fSGustavo F. Padovan 22860a708f8fSGustavo F. Padovan /* Store config. */ 228773ffa904SGustavo F. Padovan memcpy(chan->conf_req + chan->conf_len, req->data, len); 228873ffa904SGustavo F. Padovan chan->conf_len += len; 22890a708f8fSGustavo F. Padovan 22900a708f8fSGustavo F. Padovan if (flags & 0x0001) { 22910a708f8fSGustavo F. Padovan /* Incomplete config. Send empty response. */ 22920a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, 22930a708f8fSGustavo F. Padovan l2cap_build_conf_rsp(sk, rsp, 22940a708f8fSGustavo F. Padovan L2CAP_CONF_SUCCESS, 0x0001), rsp); 22950a708f8fSGustavo F. Padovan goto unlock; 22960a708f8fSGustavo F. Padovan } 22970a708f8fSGustavo F. Padovan 22980a708f8fSGustavo F. Padovan /* Complete config. */ 229973ffa904SGustavo F. Padovan len = l2cap_parse_conf_req(chan, rsp); 23000a708f8fSGustavo F. Padovan if (len < 0) { 2301e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 23020a708f8fSGustavo F. Padovan goto unlock; 23030a708f8fSGustavo F. Padovan } 23040a708f8fSGustavo F. Padovan 23050a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); 230673ffa904SGustavo F. Padovan chan->num_conf_rsp++; 23070a708f8fSGustavo F. Padovan 23080a708f8fSGustavo F. Padovan /* Reset config buffer. */ 230973ffa904SGustavo F. Padovan chan->conf_len = 0; 23100a708f8fSGustavo F. Padovan 23110a708f8fSGustavo F. Padovan if (!(l2cap_pi(sk)->conf_state & L2CAP_CONF_OUTPUT_DONE)) 23120a708f8fSGustavo F. Padovan goto unlock; 23130a708f8fSGustavo F. Padovan 23140a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->conf_state & L2CAP_CONF_INPUT_DONE) { 23150a708f8fSGustavo F. Padovan set_default_fcs(l2cap_pi(sk)); 23160a708f8fSGustavo F. Padovan 23170a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 23180a708f8fSGustavo F. Padovan 231942e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 232042e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 23210a708f8fSGustavo F. Padovan __skb_queue_head_init(TX_QUEUE(sk)); 23220a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) 2323525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 23240a708f8fSGustavo F. Padovan 23250a708f8fSGustavo F. Padovan l2cap_chan_ready(sk); 23260a708f8fSGustavo F. Padovan goto unlock; 23270a708f8fSGustavo F. Padovan } 23280a708f8fSGustavo F. Padovan 23290a708f8fSGustavo F. Padovan if (!(l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT)) { 23300a708f8fSGustavo F. Padovan u8 buf[64]; 23310a708f8fSGustavo F. Padovan l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT; 23320a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, 233373ffa904SGustavo F. Padovan l2cap_build_conf_req(chan, buf), buf); 233473ffa904SGustavo F. Padovan chan->num_conf_req++; 23350a708f8fSGustavo F. Padovan } 23360a708f8fSGustavo F. Padovan 23370a708f8fSGustavo F. Padovan unlock: 23380a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 23390a708f8fSGustavo F. Padovan return 0; 23400a708f8fSGustavo F. Padovan } 23410a708f8fSGustavo F. Padovan 23420a708f8fSGustavo F. Padovan static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 23430a708f8fSGustavo F. Padovan { 23440a708f8fSGustavo F. Padovan struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; 23450a708f8fSGustavo F. Padovan u16 scid, flags, result; 234648454079SGustavo F. Padovan struct l2cap_chan *chan; 23470a708f8fSGustavo F. Padovan struct sock *sk; 23480a708f8fSGustavo F. Padovan int len = cmd->len - sizeof(*rsp); 23490a708f8fSGustavo F. Padovan 23500a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 23510a708f8fSGustavo F. Padovan flags = __le16_to_cpu(rsp->flags); 23520a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 23530a708f8fSGustavo F. Padovan 23540a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", 23550a708f8fSGustavo F. Padovan scid, flags, result); 23560a708f8fSGustavo F. Padovan 2357baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 235848454079SGustavo F. Padovan if (!chan) 23590a708f8fSGustavo F. Padovan return 0; 23600a708f8fSGustavo F. Padovan 236148454079SGustavo F. Padovan sk = chan->sk; 236248454079SGustavo F. Padovan 23630a708f8fSGustavo F. Padovan switch (result) { 23640a708f8fSGustavo F. Padovan case L2CAP_CONF_SUCCESS: 23650a708f8fSGustavo F. Padovan l2cap_conf_rfc_get(sk, rsp->data, len); 23660a708f8fSGustavo F. Padovan break; 23670a708f8fSGustavo F. Padovan 23680a708f8fSGustavo F. Padovan case L2CAP_CONF_UNACCEPT: 236973ffa904SGustavo F. Padovan if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { 23700a708f8fSGustavo F. Padovan char req[64]; 23710a708f8fSGustavo F. Padovan 23720a708f8fSGustavo F. Padovan if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { 2373e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 23740a708f8fSGustavo F. Padovan goto done; 23750a708f8fSGustavo F. Padovan } 23760a708f8fSGustavo F. Padovan 23770a708f8fSGustavo F. Padovan /* throw out any old stored conf requests */ 23780a708f8fSGustavo F. Padovan result = L2CAP_CONF_SUCCESS; 23790a708f8fSGustavo F. Padovan len = l2cap_parse_conf_rsp(sk, rsp->data, 23800a708f8fSGustavo F. Padovan len, req, &result); 23810a708f8fSGustavo F. Padovan if (len < 0) { 2382e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 23830a708f8fSGustavo F. Padovan goto done; 23840a708f8fSGustavo F. Padovan } 23850a708f8fSGustavo F. Padovan 23860a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, l2cap_get_ident(conn), 23870a708f8fSGustavo F. Padovan L2CAP_CONF_REQ, len, req); 238873ffa904SGustavo F. Padovan chan->num_conf_req++; 23890a708f8fSGustavo F. Padovan if (result != L2CAP_CONF_SUCCESS) 23900a708f8fSGustavo F. Padovan goto done; 23910a708f8fSGustavo F. Padovan break; 23920a708f8fSGustavo F. Padovan } 23930a708f8fSGustavo F. Padovan 23940a708f8fSGustavo F. Padovan default: 23950a708f8fSGustavo F. Padovan sk->sk_err = ECONNRESET; 23960a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, HZ * 5); 2397e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(conn, chan, ECONNRESET); 23980a708f8fSGustavo F. Padovan goto done; 23990a708f8fSGustavo F. Padovan } 24000a708f8fSGustavo F. Padovan 24010a708f8fSGustavo F. Padovan if (flags & 0x01) 24020a708f8fSGustavo F. Padovan goto done; 24030a708f8fSGustavo F. Padovan 24040a708f8fSGustavo F. Padovan l2cap_pi(sk)->conf_state |= L2CAP_CONF_INPUT_DONE; 24050a708f8fSGustavo F. Padovan 24060a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->conf_state & L2CAP_CONF_OUTPUT_DONE) { 24070a708f8fSGustavo F. Padovan set_default_fcs(l2cap_pi(sk)); 24080a708f8fSGustavo F. Padovan 24090a708f8fSGustavo F. Padovan sk->sk_state = BT_CONNECTED; 241042e5c802SGustavo F. Padovan chan->next_tx_seq = 0; 241142e5c802SGustavo F. Padovan chan->expected_tx_seq = 0; 24120a708f8fSGustavo F. Padovan __skb_queue_head_init(TX_QUEUE(sk)); 24130a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) 2414525cd185SGustavo F. Padovan l2cap_ertm_init(chan); 24150a708f8fSGustavo F. Padovan 24160a708f8fSGustavo F. Padovan l2cap_chan_ready(sk); 24170a708f8fSGustavo F. Padovan } 24180a708f8fSGustavo F. Padovan 24190a708f8fSGustavo F. Padovan done: 24200a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 24210a708f8fSGustavo F. Padovan return 0; 24220a708f8fSGustavo F. Padovan } 24230a708f8fSGustavo F. Padovan 24240a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 24250a708f8fSGustavo F. Padovan { 24260a708f8fSGustavo F. Padovan struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; 24270a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp rsp; 24280a708f8fSGustavo F. Padovan u16 dcid, scid; 242948454079SGustavo F. Padovan struct l2cap_chan *chan; 24300a708f8fSGustavo F. Padovan struct sock *sk; 24310a708f8fSGustavo F. Padovan 24320a708f8fSGustavo F. Padovan scid = __le16_to_cpu(req->scid); 24330a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(req->dcid); 24340a708f8fSGustavo F. Padovan 24350a708f8fSGustavo F. Padovan BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); 24360a708f8fSGustavo F. Padovan 2437baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, dcid); 243848454079SGustavo F. Padovan if (!chan) 24390a708f8fSGustavo F. Padovan return 0; 24400a708f8fSGustavo F. Padovan 244148454079SGustavo F. Padovan sk = chan->sk; 244248454079SGustavo F. Padovan 24430a708f8fSGustavo F. Padovan rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid); 24440a708f8fSGustavo F. Padovan rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid); 24450a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); 24460a708f8fSGustavo F. Padovan 24470a708f8fSGustavo F. Padovan sk->sk_shutdown = SHUTDOWN_MASK; 24480a708f8fSGustavo F. Padovan 24490a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 24500a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 24510a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 24520a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 24530a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, HZ / 5); 24540a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 24550a708f8fSGustavo F. Padovan return 0; 24560a708f8fSGustavo F. Padovan } 24570a708f8fSGustavo F. Padovan 245848454079SGustavo F. Padovan l2cap_chan_del(chan, ECONNRESET); 24590a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 24600a708f8fSGustavo F. Padovan 24610a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 24620a708f8fSGustavo F. Padovan return 0; 24630a708f8fSGustavo F. Padovan } 24640a708f8fSGustavo F. Padovan 24650a708f8fSGustavo F. Padovan static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 24660a708f8fSGustavo F. Padovan { 24670a708f8fSGustavo F. Padovan struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; 24680a708f8fSGustavo F. Padovan u16 dcid, scid; 246948454079SGustavo F. Padovan struct l2cap_chan *chan; 24700a708f8fSGustavo F. Padovan struct sock *sk; 24710a708f8fSGustavo F. Padovan 24720a708f8fSGustavo F. Padovan scid = __le16_to_cpu(rsp->scid); 24730a708f8fSGustavo F. Padovan dcid = __le16_to_cpu(rsp->dcid); 24740a708f8fSGustavo F. Padovan 24750a708f8fSGustavo F. Padovan BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); 24760a708f8fSGustavo F. Padovan 2477baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, scid); 247848454079SGustavo F. Padovan if (!chan) 24790a708f8fSGustavo F. Padovan return 0; 24800a708f8fSGustavo F. Padovan 248148454079SGustavo F. Padovan sk = chan->sk; 248248454079SGustavo F. Padovan 24830a708f8fSGustavo F. Padovan /* don't delete l2cap channel if sk is owned by user */ 24840a708f8fSGustavo F. Padovan if (sock_owned_by_user(sk)) { 24850a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 24860a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 24870a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, HZ / 5); 24880a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 24890a708f8fSGustavo F. Padovan return 0; 24900a708f8fSGustavo F. Padovan } 24910a708f8fSGustavo F. Padovan 249248454079SGustavo F. Padovan l2cap_chan_del(chan, 0); 24930a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 24940a708f8fSGustavo F. Padovan 24950a708f8fSGustavo F. Padovan l2cap_sock_kill(sk); 24960a708f8fSGustavo F. Padovan return 0; 24970a708f8fSGustavo F. Padovan } 24980a708f8fSGustavo F. Padovan 24990a708f8fSGustavo F. Padovan static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 25000a708f8fSGustavo F. Padovan { 25010a708f8fSGustavo F. Padovan struct l2cap_info_req *req = (struct l2cap_info_req *) data; 25020a708f8fSGustavo F. Padovan u16 type; 25030a708f8fSGustavo F. Padovan 25040a708f8fSGustavo F. Padovan type = __le16_to_cpu(req->type); 25050a708f8fSGustavo F. Padovan 25060a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x", type); 25070a708f8fSGustavo F. Padovan 25080a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 25090a708f8fSGustavo F. Padovan u8 buf[8]; 25100a708f8fSGustavo F. Padovan u32 feat_mask = l2cap_feat_mask; 25110a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 25120a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); 25130a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 25140a708f8fSGustavo F. Padovan if (!disable_ertm) 25150a708f8fSGustavo F. Padovan feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING 25160a708f8fSGustavo F. Padovan | L2CAP_FEAT_FCS; 25170a708f8fSGustavo F. Padovan put_unaligned_le32(feat_mask, rsp->data); 25180a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 25190a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 25200a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 25210a708f8fSGustavo F. Padovan u8 buf[12]; 25220a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 25230a708f8fSGustavo F. Padovan rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 25240a708f8fSGustavo F. Padovan rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); 25250a708f8fSGustavo F. Padovan memcpy(buf + 4, l2cap_fixed_chan, 8); 25260a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 25270a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(buf), buf); 25280a708f8fSGustavo F. Padovan } else { 25290a708f8fSGustavo F. Padovan struct l2cap_info_rsp rsp; 25300a708f8fSGustavo F. Padovan rsp.type = cpu_to_le16(type); 25310a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); 25320a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd->ident, 25330a708f8fSGustavo F. Padovan L2CAP_INFO_RSP, sizeof(rsp), &rsp); 25340a708f8fSGustavo F. Padovan } 25350a708f8fSGustavo F. Padovan 25360a708f8fSGustavo F. Padovan return 0; 25370a708f8fSGustavo F. Padovan } 25380a708f8fSGustavo F. Padovan 25390a708f8fSGustavo F. Padovan static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) 25400a708f8fSGustavo F. Padovan { 25410a708f8fSGustavo F. Padovan struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; 25420a708f8fSGustavo F. Padovan u16 type, result; 25430a708f8fSGustavo F. Padovan 25440a708f8fSGustavo F. Padovan type = __le16_to_cpu(rsp->type); 25450a708f8fSGustavo F. Padovan result = __le16_to_cpu(rsp->result); 25460a708f8fSGustavo F. Padovan 25470a708f8fSGustavo F. Padovan BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); 25480a708f8fSGustavo F. Padovan 2549e90165beSAndrei Emeltchenko /* L2CAP Info req/rsp are unbound to channels, add extra checks */ 2550e90165beSAndrei Emeltchenko if (cmd->ident != conn->info_ident || 2551e90165beSAndrei Emeltchenko conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) 2552e90165beSAndrei Emeltchenko return 0; 2553e90165beSAndrei Emeltchenko 25540a708f8fSGustavo F. Padovan del_timer(&conn->info_timer); 25550a708f8fSGustavo F. Padovan 25560a708f8fSGustavo F. Padovan if (result != L2CAP_IR_SUCCESS) { 25570a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 25580a708f8fSGustavo F. Padovan conn->info_ident = 0; 25590a708f8fSGustavo F. Padovan 25600a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 25610a708f8fSGustavo F. Padovan 25620a708f8fSGustavo F. Padovan return 0; 25630a708f8fSGustavo F. Padovan } 25640a708f8fSGustavo F. Padovan 25650a708f8fSGustavo F. Padovan if (type == L2CAP_IT_FEAT_MASK) { 25660a708f8fSGustavo F. Padovan conn->feat_mask = get_unaligned_le32(rsp->data); 25670a708f8fSGustavo F. Padovan 25680a708f8fSGustavo F. Padovan if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { 25690a708f8fSGustavo F. Padovan struct l2cap_info_req req; 25700a708f8fSGustavo F. Padovan req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); 25710a708f8fSGustavo F. Padovan 25720a708f8fSGustavo F. Padovan conn->info_ident = l2cap_get_ident(conn); 25730a708f8fSGustavo F. Padovan 25740a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, conn->info_ident, 25750a708f8fSGustavo F. Padovan L2CAP_INFO_REQ, sizeof(req), &req); 25760a708f8fSGustavo F. Padovan } else { 25770a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 25780a708f8fSGustavo F. Padovan conn->info_ident = 0; 25790a708f8fSGustavo F. Padovan 25800a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 25810a708f8fSGustavo F. Padovan } 25820a708f8fSGustavo F. Padovan } else if (type == L2CAP_IT_FIXED_CHAN) { 25830a708f8fSGustavo F. Padovan conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; 25840a708f8fSGustavo F. Padovan conn->info_ident = 0; 25850a708f8fSGustavo F. Padovan 25860a708f8fSGustavo F. Padovan l2cap_conn_start(conn); 25870a708f8fSGustavo F. Padovan } 25880a708f8fSGustavo F. Padovan 25890a708f8fSGustavo F. Padovan return 0; 25900a708f8fSGustavo F. Padovan } 25910a708f8fSGustavo F. Padovan 2592e2174ca4SGustavo F. Padovan static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency, 2593de73115aSClaudio Takahasi u16 to_multiplier) 2594de73115aSClaudio Takahasi { 2595de73115aSClaudio Takahasi u16 max_latency; 2596de73115aSClaudio Takahasi 2597de73115aSClaudio Takahasi if (min > max || min < 6 || max > 3200) 2598de73115aSClaudio Takahasi return -EINVAL; 2599de73115aSClaudio Takahasi 2600de73115aSClaudio Takahasi if (to_multiplier < 10 || to_multiplier > 3200) 2601de73115aSClaudio Takahasi return -EINVAL; 2602de73115aSClaudio Takahasi 2603de73115aSClaudio Takahasi if (max >= to_multiplier * 8) 2604de73115aSClaudio Takahasi return -EINVAL; 2605de73115aSClaudio Takahasi 2606de73115aSClaudio Takahasi max_latency = (to_multiplier * 8 / max) - 1; 2607de73115aSClaudio Takahasi if (latency > 499 || latency > max_latency) 2608de73115aSClaudio Takahasi return -EINVAL; 2609de73115aSClaudio Takahasi 2610de73115aSClaudio Takahasi return 0; 2611de73115aSClaudio Takahasi } 2612de73115aSClaudio Takahasi 2613de73115aSClaudio Takahasi static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, 2614de73115aSClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 2615de73115aSClaudio Takahasi { 2616de73115aSClaudio Takahasi struct hci_conn *hcon = conn->hcon; 2617de73115aSClaudio Takahasi struct l2cap_conn_param_update_req *req; 2618de73115aSClaudio Takahasi struct l2cap_conn_param_update_rsp rsp; 2619de73115aSClaudio Takahasi u16 min, max, latency, to_multiplier, cmd_len; 26202ce603ebSClaudio Takahasi int err; 2621de73115aSClaudio Takahasi 2622de73115aSClaudio Takahasi if (!(hcon->link_mode & HCI_LM_MASTER)) 2623de73115aSClaudio Takahasi return -EINVAL; 2624de73115aSClaudio Takahasi 2625de73115aSClaudio Takahasi cmd_len = __le16_to_cpu(cmd->len); 2626de73115aSClaudio Takahasi if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) 2627de73115aSClaudio Takahasi return -EPROTO; 2628de73115aSClaudio Takahasi 2629de73115aSClaudio Takahasi req = (struct l2cap_conn_param_update_req *) data; 2630de73115aSClaudio Takahasi min = __le16_to_cpu(req->min); 2631de73115aSClaudio Takahasi max = __le16_to_cpu(req->max); 2632de73115aSClaudio Takahasi latency = __le16_to_cpu(req->latency); 2633de73115aSClaudio Takahasi to_multiplier = __le16_to_cpu(req->to_multiplier); 2634de73115aSClaudio Takahasi 2635de73115aSClaudio Takahasi BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", 2636de73115aSClaudio Takahasi min, max, latency, to_multiplier); 2637de73115aSClaudio Takahasi 2638de73115aSClaudio Takahasi memset(&rsp, 0, sizeof(rsp)); 26392ce603ebSClaudio Takahasi 26402ce603ebSClaudio Takahasi err = l2cap_check_conn_param(min, max, latency, to_multiplier); 26412ce603ebSClaudio Takahasi if (err) 2642de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); 2643de73115aSClaudio Takahasi else 2644de73115aSClaudio Takahasi rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); 2645de73115aSClaudio Takahasi 2646de73115aSClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, 2647de73115aSClaudio Takahasi sizeof(rsp), &rsp); 2648de73115aSClaudio Takahasi 26492ce603ebSClaudio Takahasi if (!err) 26502ce603ebSClaudio Takahasi hci_le_conn_update(hcon, min, max, latency, to_multiplier); 26512ce603ebSClaudio Takahasi 2652de73115aSClaudio Takahasi return 0; 2653de73115aSClaudio Takahasi } 2654de73115aSClaudio Takahasi 26553300d9a9SClaudio Takahasi static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, 26563300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) 26573300d9a9SClaudio Takahasi { 26583300d9a9SClaudio Takahasi int err = 0; 26593300d9a9SClaudio Takahasi 26603300d9a9SClaudio Takahasi switch (cmd->code) { 26613300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 26623300d9a9SClaudio Takahasi l2cap_command_rej(conn, cmd, data); 26633300d9a9SClaudio Takahasi break; 26643300d9a9SClaudio Takahasi 26653300d9a9SClaudio Takahasi case L2CAP_CONN_REQ: 26663300d9a9SClaudio Takahasi err = l2cap_connect_req(conn, cmd, data); 26673300d9a9SClaudio Takahasi break; 26683300d9a9SClaudio Takahasi 26693300d9a9SClaudio Takahasi case L2CAP_CONN_RSP: 26703300d9a9SClaudio Takahasi err = l2cap_connect_rsp(conn, cmd, data); 26713300d9a9SClaudio Takahasi break; 26723300d9a9SClaudio Takahasi 26733300d9a9SClaudio Takahasi case L2CAP_CONF_REQ: 26743300d9a9SClaudio Takahasi err = l2cap_config_req(conn, cmd, cmd_len, data); 26753300d9a9SClaudio Takahasi break; 26763300d9a9SClaudio Takahasi 26773300d9a9SClaudio Takahasi case L2CAP_CONF_RSP: 26783300d9a9SClaudio Takahasi err = l2cap_config_rsp(conn, cmd, data); 26793300d9a9SClaudio Takahasi break; 26803300d9a9SClaudio Takahasi 26813300d9a9SClaudio Takahasi case L2CAP_DISCONN_REQ: 26823300d9a9SClaudio Takahasi err = l2cap_disconnect_req(conn, cmd, data); 26833300d9a9SClaudio Takahasi break; 26843300d9a9SClaudio Takahasi 26853300d9a9SClaudio Takahasi case L2CAP_DISCONN_RSP: 26863300d9a9SClaudio Takahasi err = l2cap_disconnect_rsp(conn, cmd, data); 26873300d9a9SClaudio Takahasi break; 26883300d9a9SClaudio Takahasi 26893300d9a9SClaudio Takahasi case L2CAP_ECHO_REQ: 26903300d9a9SClaudio Takahasi l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); 26913300d9a9SClaudio Takahasi break; 26923300d9a9SClaudio Takahasi 26933300d9a9SClaudio Takahasi case L2CAP_ECHO_RSP: 26943300d9a9SClaudio Takahasi break; 26953300d9a9SClaudio Takahasi 26963300d9a9SClaudio Takahasi case L2CAP_INFO_REQ: 26973300d9a9SClaudio Takahasi err = l2cap_information_req(conn, cmd, data); 26983300d9a9SClaudio Takahasi break; 26993300d9a9SClaudio Takahasi 27003300d9a9SClaudio Takahasi case L2CAP_INFO_RSP: 27013300d9a9SClaudio Takahasi err = l2cap_information_rsp(conn, cmd, data); 27023300d9a9SClaudio Takahasi break; 27033300d9a9SClaudio Takahasi 27043300d9a9SClaudio Takahasi default: 27053300d9a9SClaudio Takahasi BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); 27063300d9a9SClaudio Takahasi err = -EINVAL; 27073300d9a9SClaudio Takahasi break; 27083300d9a9SClaudio Takahasi } 27093300d9a9SClaudio Takahasi 27103300d9a9SClaudio Takahasi return err; 27113300d9a9SClaudio Takahasi } 27123300d9a9SClaudio Takahasi 27133300d9a9SClaudio Takahasi static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, 27143300d9a9SClaudio Takahasi struct l2cap_cmd_hdr *cmd, u8 *data) 27153300d9a9SClaudio Takahasi { 27163300d9a9SClaudio Takahasi switch (cmd->code) { 27173300d9a9SClaudio Takahasi case L2CAP_COMMAND_REJ: 27183300d9a9SClaudio Takahasi return 0; 27193300d9a9SClaudio Takahasi 27203300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_REQ: 2721de73115aSClaudio Takahasi return l2cap_conn_param_update_req(conn, cmd, data); 27223300d9a9SClaudio Takahasi 27233300d9a9SClaudio Takahasi case L2CAP_CONN_PARAM_UPDATE_RSP: 27243300d9a9SClaudio Takahasi return 0; 27253300d9a9SClaudio Takahasi 27263300d9a9SClaudio Takahasi default: 27273300d9a9SClaudio Takahasi BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); 27283300d9a9SClaudio Takahasi return -EINVAL; 27293300d9a9SClaudio Takahasi } 27303300d9a9SClaudio Takahasi } 27313300d9a9SClaudio Takahasi 27323300d9a9SClaudio Takahasi static inline void l2cap_sig_channel(struct l2cap_conn *conn, 27333300d9a9SClaudio Takahasi struct sk_buff *skb) 27340a708f8fSGustavo F. Padovan { 27350a708f8fSGustavo F. Padovan u8 *data = skb->data; 27360a708f8fSGustavo F. Padovan int len = skb->len; 27370a708f8fSGustavo F. Padovan struct l2cap_cmd_hdr cmd; 27383300d9a9SClaudio Takahasi int err; 27390a708f8fSGustavo F. Padovan 27400a708f8fSGustavo F. Padovan l2cap_raw_recv(conn, skb); 27410a708f8fSGustavo F. Padovan 27420a708f8fSGustavo F. Padovan while (len >= L2CAP_CMD_HDR_SIZE) { 27430a708f8fSGustavo F. Padovan u16 cmd_len; 27440a708f8fSGustavo F. Padovan memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); 27450a708f8fSGustavo F. Padovan data += L2CAP_CMD_HDR_SIZE; 27460a708f8fSGustavo F. Padovan len -= L2CAP_CMD_HDR_SIZE; 27470a708f8fSGustavo F. Padovan 27480a708f8fSGustavo F. Padovan cmd_len = le16_to_cpu(cmd.len); 27490a708f8fSGustavo F. Padovan 27500a708f8fSGustavo F. Padovan BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident); 27510a708f8fSGustavo F. Padovan 27520a708f8fSGustavo F. Padovan if (cmd_len > len || !cmd.ident) { 27530a708f8fSGustavo F. Padovan BT_DBG("corrupted command"); 27540a708f8fSGustavo F. Padovan break; 27550a708f8fSGustavo F. Padovan } 27560a708f8fSGustavo F. Padovan 27573300d9a9SClaudio Takahasi if (conn->hcon->type == LE_LINK) 27583300d9a9SClaudio Takahasi err = l2cap_le_sig_cmd(conn, &cmd, data); 27593300d9a9SClaudio Takahasi else 27603300d9a9SClaudio Takahasi err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); 27610a708f8fSGustavo F. Padovan 27620a708f8fSGustavo F. Padovan if (err) { 27630a708f8fSGustavo F. Padovan struct l2cap_cmd_rej rej; 27642c6d1a2eSGustavo F. Padovan 27652c6d1a2eSGustavo F. Padovan BT_ERR("Wrong link type (%d)", err); 27660a708f8fSGustavo F. Padovan 27670a708f8fSGustavo F. Padovan /* FIXME: Map err to a valid reason */ 27680a708f8fSGustavo F. Padovan rej.reason = cpu_to_le16(0); 27690a708f8fSGustavo F. Padovan l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); 27700a708f8fSGustavo F. Padovan } 27710a708f8fSGustavo F. Padovan 27720a708f8fSGustavo F. Padovan data += cmd_len; 27730a708f8fSGustavo F. Padovan len -= cmd_len; 27740a708f8fSGustavo F. Padovan } 27750a708f8fSGustavo F. Padovan 27760a708f8fSGustavo F. Padovan kfree_skb(skb); 27770a708f8fSGustavo F. Padovan } 27780a708f8fSGustavo F. Padovan 27790a708f8fSGustavo F. Padovan static int l2cap_check_fcs(struct l2cap_pinfo *pi, struct sk_buff *skb) 27800a708f8fSGustavo F. Padovan { 27810a708f8fSGustavo F. Padovan u16 our_fcs, rcv_fcs; 27820a708f8fSGustavo F. Padovan int hdr_size = L2CAP_HDR_SIZE + 2; 27830a708f8fSGustavo F. Padovan 27840a708f8fSGustavo F. Padovan if (pi->fcs == L2CAP_FCS_CRC16) { 27850a708f8fSGustavo F. Padovan skb_trim(skb, skb->len - 2); 27860a708f8fSGustavo F. Padovan rcv_fcs = get_unaligned_le16(skb->data + skb->len); 27870a708f8fSGustavo F. Padovan our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); 27880a708f8fSGustavo F. Padovan 27890a708f8fSGustavo F. Padovan if (our_fcs != rcv_fcs) 27900a708f8fSGustavo F. Padovan return -EBADMSG; 27910a708f8fSGustavo F. Padovan } 27920a708f8fSGustavo F. Padovan return 0; 27930a708f8fSGustavo F. Padovan } 27940a708f8fSGustavo F. Padovan 2795525cd185SGustavo F. Padovan static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan) 27960a708f8fSGustavo F. Padovan { 27970a708f8fSGustavo F. Padovan u16 control = 0; 27980a708f8fSGustavo F. Padovan 27996a026610SGustavo F. Padovan chan->frames_sent = 0; 28000a708f8fSGustavo F. Padovan 280142e5c802SGustavo F. Padovan control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 28020a708f8fSGustavo F. Padovan 2803525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 28040a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_NOT_READY; 2805525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 2806525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 28070a708f8fSGustavo F. Padovan } 28080a708f8fSGustavo F. Padovan 2809525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REMOTE_BUSY) 2810525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 28110a708f8fSGustavo F. Padovan 2812525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 28130a708f8fSGustavo F. Padovan 2814525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_LOCAL_BUSY) && 28156a026610SGustavo F. Padovan chan->frames_sent == 0) { 28160a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY; 2817525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 28180a708f8fSGustavo F. Padovan } 28190a708f8fSGustavo F. Padovan } 28200a708f8fSGustavo F. Padovan 282142e5c802SGustavo F. Padovan static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u8 tx_seq, u8 sar) 28220a708f8fSGustavo F. Padovan { 28230a708f8fSGustavo F. Padovan struct sk_buff *next_skb; 28240a708f8fSGustavo F. Padovan int tx_seq_offset, next_tx_seq_offset; 28250a708f8fSGustavo F. Padovan 28260a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 28270a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 28280a708f8fSGustavo F. Padovan 2829f1c6775bSGustavo F. Padovan next_skb = skb_peek(&chan->srej_q); 28300a708f8fSGustavo F. Padovan if (!next_skb) { 2831f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 28320a708f8fSGustavo F. Padovan return 0; 28330a708f8fSGustavo F. Padovan } 28340a708f8fSGustavo F. Padovan 283542e5c802SGustavo F. Padovan tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; 28360a708f8fSGustavo F. Padovan if (tx_seq_offset < 0) 28370a708f8fSGustavo F. Padovan tx_seq_offset += 64; 28380a708f8fSGustavo F. Padovan 28390a708f8fSGustavo F. Padovan do { 28400a708f8fSGustavo F. Padovan if (bt_cb(next_skb)->tx_seq == tx_seq) 28410a708f8fSGustavo F. Padovan return -EINVAL; 28420a708f8fSGustavo F. Padovan 28430a708f8fSGustavo F. Padovan next_tx_seq_offset = (bt_cb(next_skb)->tx_seq - 284442e5c802SGustavo F. Padovan chan->buffer_seq) % 64; 28450a708f8fSGustavo F. Padovan if (next_tx_seq_offset < 0) 28460a708f8fSGustavo F. Padovan next_tx_seq_offset += 64; 28470a708f8fSGustavo F. Padovan 28480a708f8fSGustavo F. Padovan if (next_tx_seq_offset > tx_seq_offset) { 2849f1c6775bSGustavo F. Padovan __skb_queue_before(&chan->srej_q, next_skb, skb); 28500a708f8fSGustavo F. Padovan return 0; 28510a708f8fSGustavo F. Padovan } 28520a708f8fSGustavo F. Padovan 2853f1c6775bSGustavo F. Padovan if (skb_queue_is_last(&chan->srej_q, next_skb)) 28540a708f8fSGustavo F. Padovan break; 28550a708f8fSGustavo F. Padovan 2856f1c6775bSGustavo F. Padovan } while ((next_skb = skb_queue_next(&chan->srej_q, next_skb))); 28570a708f8fSGustavo F. Padovan 2858f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 28590a708f8fSGustavo F. Padovan 28600a708f8fSGustavo F. Padovan return 0; 28610a708f8fSGustavo F. Padovan } 28620a708f8fSGustavo F. Padovan 2863525cd185SGustavo F. Padovan static int l2cap_ertm_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 28640a708f8fSGustavo F. Padovan { 2865525cd185SGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(chan->sk); 28660a708f8fSGustavo F. Padovan struct sk_buff *_skb; 28670a708f8fSGustavo F. Padovan int err; 28680a708f8fSGustavo F. Padovan 28690a708f8fSGustavo F. Padovan switch (control & L2CAP_CTRL_SAR) { 28700a708f8fSGustavo F. Padovan case L2CAP_SDU_UNSEGMENTED: 2871525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) 28720a708f8fSGustavo F. Padovan goto drop; 28730a708f8fSGustavo F. Padovan 2874525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, skb); 28750a708f8fSGustavo F. Padovan if (!err) 28760a708f8fSGustavo F. Padovan return err; 28770a708f8fSGustavo F. Padovan 28780a708f8fSGustavo F. Padovan break; 28790a708f8fSGustavo F. Padovan 28800a708f8fSGustavo F. Padovan case L2CAP_SDU_START: 2881525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) 28820a708f8fSGustavo F. Padovan goto drop; 28830a708f8fSGustavo F. Padovan 28846f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 28850a708f8fSGustavo F. Padovan 28866f61fd47SGustavo F. Padovan if (chan->sdu_len > pi->imtu) 28870a708f8fSGustavo F. Padovan goto disconnect; 28880a708f8fSGustavo F. Padovan 28896f61fd47SGustavo F. Padovan chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC); 28906f61fd47SGustavo F. Padovan if (!chan->sdu) 28910a708f8fSGustavo F. Padovan return -ENOMEM; 28920a708f8fSGustavo F. Padovan 28930a708f8fSGustavo F. Padovan /* pull sdu_len bytes only after alloc, because of Local Busy 28940a708f8fSGustavo F. Padovan * condition we have to be sure that this will be executed 28950a708f8fSGustavo F. Padovan * only once, i.e., when alloc does not fail */ 28960a708f8fSGustavo F. Padovan skb_pull(skb, 2); 28970a708f8fSGustavo F. Padovan 28986f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 28990a708f8fSGustavo F. Padovan 2900525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_SDU; 29016f61fd47SGustavo F. Padovan chan->partial_sdu_len = skb->len; 29020a708f8fSGustavo F. Padovan break; 29030a708f8fSGustavo F. Padovan 29040a708f8fSGustavo F. Padovan case L2CAP_SDU_CONTINUE: 2905525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 29060a708f8fSGustavo F. Padovan goto disconnect; 29070a708f8fSGustavo F. Padovan 29086f61fd47SGustavo F. Padovan if (!chan->sdu) 29090a708f8fSGustavo F. Padovan goto disconnect; 29100a708f8fSGustavo F. Padovan 29116f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 29126f61fd47SGustavo F. Padovan if (chan->partial_sdu_len > chan->sdu_len) 29130a708f8fSGustavo F. Padovan goto drop; 29140a708f8fSGustavo F. Padovan 29156f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 29160a708f8fSGustavo F. Padovan 29170a708f8fSGustavo F. Padovan break; 29180a708f8fSGustavo F. Padovan 29190a708f8fSGustavo F. Padovan case L2CAP_SDU_END: 2920525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 29210a708f8fSGustavo F. Padovan goto disconnect; 29220a708f8fSGustavo F. Padovan 29236f61fd47SGustavo F. Padovan if (!chan->sdu) 29240a708f8fSGustavo F. Padovan goto disconnect; 29250a708f8fSGustavo F. Padovan 2926525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_RETRY)) { 29276f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 29280a708f8fSGustavo F. Padovan 29296f61fd47SGustavo F. Padovan if (chan->partial_sdu_len > pi->imtu) 29300a708f8fSGustavo F. Padovan goto drop; 29310a708f8fSGustavo F. Padovan 29326f61fd47SGustavo F. Padovan if (chan->partial_sdu_len != chan->sdu_len) 29330a708f8fSGustavo F. Padovan goto drop; 29340a708f8fSGustavo F. Padovan 29356f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 29360a708f8fSGustavo F. Padovan } 29370a708f8fSGustavo F. Padovan 29386f61fd47SGustavo F. Padovan _skb = skb_clone(chan->sdu, GFP_ATOMIC); 29390a708f8fSGustavo F. Padovan if (!_skb) { 2940525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_RETRY; 29410a708f8fSGustavo F. Padovan return -ENOMEM; 29420a708f8fSGustavo F. Padovan } 29430a708f8fSGustavo F. Padovan 2944525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, _skb); 29450a708f8fSGustavo F. Padovan if (err < 0) { 29460a708f8fSGustavo F. Padovan kfree_skb(_skb); 2947525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_RETRY; 29480a708f8fSGustavo F. Padovan return err; 29490a708f8fSGustavo F. Padovan } 29500a708f8fSGustavo F. Padovan 2951525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_RETRY; 2952525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_SDU; 29530a708f8fSGustavo F. Padovan 29546f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 29550a708f8fSGustavo F. Padovan break; 29560a708f8fSGustavo F. Padovan } 29570a708f8fSGustavo F. Padovan 29580a708f8fSGustavo F. Padovan kfree_skb(skb); 29590a708f8fSGustavo F. Padovan return 0; 29600a708f8fSGustavo F. Padovan 29610a708f8fSGustavo F. Padovan drop: 29626f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 29636f61fd47SGustavo F. Padovan chan->sdu = NULL; 29640a708f8fSGustavo F. Padovan 29650a708f8fSGustavo F. Padovan disconnect: 2966e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(pi->conn, chan, ECONNRESET); 29670a708f8fSGustavo F. Padovan kfree_skb(skb); 29680a708f8fSGustavo F. Padovan return 0; 29690a708f8fSGustavo F. Padovan } 29700a708f8fSGustavo F. Padovan 2971525cd185SGustavo F. Padovan static int l2cap_try_push_rx_skb(struct l2cap_chan *chan) 29720a708f8fSGustavo F. Padovan { 2973525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 29740a708f8fSGustavo F. Padovan struct sk_buff *skb; 29750a708f8fSGustavo F. Padovan u16 control; 29760a708f8fSGustavo F. Padovan int err; 29770a708f8fSGustavo F. Padovan 2978f1c6775bSGustavo F. Padovan while ((skb = skb_dequeue(&chan->busy_q))) { 29790a708f8fSGustavo F. Padovan control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT; 2980525cd185SGustavo F. Padovan err = l2cap_ertm_reassembly_sdu(chan, skb, control); 29810a708f8fSGustavo F. Padovan if (err < 0) { 2982f1c6775bSGustavo F. Padovan skb_queue_head(&chan->busy_q, skb); 29830a708f8fSGustavo F. Padovan return -EBUSY; 29840a708f8fSGustavo F. Padovan } 29850a708f8fSGustavo F. Padovan 298642e5c802SGustavo F. Padovan chan->buffer_seq = (chan->buffer_seq + 1) % 64; 29870a708f8fSGustavo F. Padovan } 29880a708f8fSGustavo F. Padovan 2989525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_RNR_SENT)) 29900a708f8fSGustavo F. Padovan goto done; 29910a708f8fSGustavo F. Padovan 299242e5c802SGustavo F. Padovan control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 29930a708f8fSGustavo F. Padovan control |= L2CAP_SUPER_RCV_READY | L2CAP_CTRL_POLL; 2994525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 29956a026610SGustavo F. Padovan chan->retry_count = 1; 29960a708f8fSGustavo F. Padovan 2997e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 29980a708f8fSGustavo F. Padovan __mod_monitor_timer(); 29990a708f8fSGustavo F. Padovan 3000525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_WAIT_F; 30010a708f8fSGustavo F. Padovan 30020a708f8fSGustavo F. Padovan done: 3003525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_LOCAL_BUSY; 3004525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_RNR_SENT; 30050a708f8fSGustavo F. Padovan 30060a708f8fSGustavo F. Padovan BT_DBG("sk %p, Exit local busy", sk); 30070a708f8fSGustavo F. Padovan 30080a708f8fSGustavo F. Padovan return 0; 30090a708f8fSGustavo F. Padovan } 30100a708f8fSGustavo F. Padovan 30110a708f8fSGustavo F. Padovan static void l2cap_busy_work(struct work_struct *work) 30120a708f8fSGustavo F. Padovan { 30130a708f8fSGustavo F. Padovan DECLARE_WAITQUEUE(wait, current); 3014311bb895SGustavo F. Padovan struct l2cap_chan *chan = 3015311bb895SGustavo F. Padovan container_of(work, struct l2cap_chan, busy_work); 3016311bb895SGustavo F. Padovan struct sock *sk = chan->sk; 30170a708f8fSGustavo F. Padovan int n_tries = 0, timeo = HZ/5, err; 30180a708f8fSGustavo F. Padovan struct sk_buff *skb; 30190a708f8fSGustavo F. Padovan 30200a708f8fSGustavo F. Padovan lock_sock(sk); 30210a708f8fSGustavo F. Padovan 30220a708f8fSGustavo F. Padovan add_wait_queue(sk_sleep(sk), &wait); 3023311bb895SGustavo F. Padovan while ((skb = skb_peek(&chan->busy_q))) { 30240a708f8fSGustavo F. Padovan set_current_state(TASK_INTERRUPTIBLE); 30250a708f8fSGustavo F. Padovan 30260a708f8fSGustavo F. Padovan if (n_tries++ > L2CAP_LOCAL_BUSY_TRIES) { 30270a708f8fSGustavo F. Padovan err = -EBUSY; 3028311bb895SGustavo F. Padovan l2cap_send_disconn_req(l2cap_pi(sk)->conn, chan, EBUSY); 30290a708f8fSGustavo F. Padovan break; 30300a708f8fSGustavo F. Padovan } 30310a708f8fSGustavo F. Padovan 30320a708f8fSGustavo F. Padovan if (!timeo) 30330a708f8fSGustavo F. Padovan timeo = HZ/5; 30340a708f8fSGustavo F. Padovan 30350a708f8fSGustavo F. Padovan if (signal_pending(current)) { 30360a708f8fSGustavo F. Padovan err = sock_intr_errno(timeo); 30370a708f8fSGustavo F. Padovan break; 30380a708f8fSGustavo F. Padovan } 30390a708f8fSGustavo F. Padovan 30400a708f8fSGustavo F. Padovan release_sock(sk); 30410a708f8fSGustavo F. Padovan timeo = schedule_timeout(timeo); 30420a708f8fSGustavo F. Padovan lock_sock(sk); 30430a708f8fSGustavo F. Padovan 30440a708f8fSGustavo F. Padovan err = sock_error(sk); 30450a708f8fSGustavo F. Padovan if (err) 30460a708f8fSGustavo F. Padovan break; 30470a708f8fSGustavo F. Padovan 3048311bb895SGustavo F. Padovan if (l2cap_try_push_rx_skb(chan) == 0) 30490a708f8fSGustavo F. Padovan break; 30500a708f8fSGustavo F. Padovan } 30510a708f8fSGustavo F. Padovan 30520a708f8fSGustavo F. Padovan set_current_state(TASK_RUNNING); 30530a708f8fSGustavo F. Padovan remove_wait_queue(sk_sleep(sk), &wait); 30540a708f8fSGustavo F. Padovan 30550a708f8fSGustavo F. Padovan release_sock(sk); 30560a708f8fSGustavo F. Padovan } 30570a708f8fSGustavo F. Padovan 3058525cd185SGustavo F. Padovan static int l2cap_push_rx_skb(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 30590a708f8fSGustavo F. Padovan { 30600a708f8fSGustavo F. Padovan int sctrl, err; 30610a708f8fSGustavo F. Padovan 3062525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { 30630a708f8fSGustavo F. Padovan bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT; 3064f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->busy_q, skb); 3065525cd185SGustavo F. Padovan return l2cap_try_push_rx_skb(chan); 30660a708f8fSGustavo F. Padovan 30670a708f8fSGustavo F. Padovan 30680a708f8fSGustavo F. Padovan } 30690a708f8fSGustavo F. Padovan 3070525cd185SGustavo F. Padovan err = l2cap_ertm_reassembly_sdu(chan, skb, control); 30710a708f8fSGustavo F. Padovan if (err >= 0) { 307242e5c802SGustavo F. Padovan chan->buffer_seq = (chan->buffer_seq + 1) % 64; 30730a708f8fSGustavo F. Padovan return err; 30740a708f8fSGustavo F. Padovan } 30750a708f8fSGustavo F. Padovan 30760a708f8fSGustavo F. Padovan /* Busy Condition */ 3077311bb895SGustavo F. Padovan BT_DBG("chan %p, Enter local busy", chan); 30780a708f8fSGustavo F. Padovan 3079525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_LOCAL_BUSY; 30800a708f8fSGustavo F. Padovan bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT; 3081f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->busy_q, skb); 30820a708f8fSGustavo F. Padovan 308342e5c802SGustavo F. Padovan sctrl = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; 30840a708f8fSGustavo F. Padovan sctrl |= L2CAP_SUPER_RCV_NOT_READY; 3085525cd185SGustavo F. Padovan l2cap_send_sframe(chan, sctrl); 30860a708f8fSGustavo F. Padovan 3087525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_RNR_SENT; 30880a708f8fSGustavo F. Padovan 3089e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 30900a708f8fSGustavo F. Padovan 3091311bb895SGustavo F. Padovan queue_work(_busy_wq, &chan->busy_work); 30920a708f8fSGustavo F. Padovan 30930a708f8fSGustavo F. Padovan return err; 30940a708f8fSGustavo F. Padovan } 30950a708f8fSGustavo F. Padovan 3096525cd185SGustavo F. Padovan static int l2cap_streaming_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) 30970a708f8fSGustavo F. Padovan { 3098525cd185SGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(chan->sk); 30990a708f8fSGustavo F. Padovan struct sk_buff *_skb; 31000a708f8fSGustavo F. Padovan int err = -EINVAL; 31010a708f8fSGustavo F. Padovan 31020a708f8fSGustavo F. Padovan /* 31030a708f8fSGustavo F. Padovan * TODO: We have to notify the userland if some data is lost with the 31040a708f8fSGustavo F. Padovan * Streaming Mode. 31050a708f8fSGustavo F. Padovan */ 31060a708f8fSGustavo F. Padovan 31070a708f8fSGustavo F. Padovan switch (control & L2CAP_CTRL_SAR) { 31080a708f8fSGustavo F. Padovan case L2CAP_SDU_UNSEGMENTED: 3109525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) { 31106f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 31110a708f8fSGustavo F. Padovan break; 31120a708f8fSGustavo F. Padovan } 31130a708f8fSGustavo F. Padovan 3114525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, skb); 31150a708f8fSGustavo F. Padovan if (!err) 31160a708f8fSGustavo F. Padovan return 0; 31170a708f8fSGustavo F. Padovan 31180a708f8fSGustavo F. Padovan break; 31190a708f8fSGustavo F. Padovan 31200a708f8fSGustavo F. Padovan case L2CAP_SDU_START: 3121525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SAR_SDU) { 31226f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 31230a708f8fSGustavo F. Padovan break; 31240a708f8fSGustavo F. Padovan } 31250a708f8fSGustavo F. Padovan 31266f61fd47SGustavo F. Padovan chan->sdu_len = get_unaligned_le16(skb->data); 31270a708f8fSGustavo F. Padovan skb_pull(skb, 2); 31280a708f8fSGustavo F. Padovan 31296f61fd47SGustavo F. Padovan if (chan->sdu_len > pi->imtu) { 31300a708f8fSGustavo F. Padovan err = -EMSGSIZE; 31310a708f8fSGustavo F. Padovan break; 31320a708f8fSGustavo F. Padovan } 31330a708f8fSGustavo F. Padovan 31346f61fd47SGustavo F. Padovan chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC); 31356f61fd47SGustavo F. Padovan if (!chan->sdu) { 31360a708f8fSGustavo F. Padovan err = -ENOMEM; 31370a708f8fSGustavo F. Padovan break; 31380a708f8fSGustavo F. Padovan } 31390a708f8fSGustavo F. Padovan 31406f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31410a708f8fSGustavo F. Padovan 3142525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SAR_SDU; 31436f61fd47SGustavo F. Padovan chan->partial_sdu_len = skb->len; 31440a708f8fSGustavo F. Padovan err = 0; 31450a708f8fSGustavo F. Padovan break; 31460a708f8fSGustavo F. Padovan 31470a708f8fSGustavo F. Padovan case L2CAP_SDU_CONTINUE: 3148525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 31490a708f8fSGustavo F. Padovan break; 31500a708f8fSGustavo F. Padovan 31516f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31520a708f8fSGustavo F. Padovan 31536f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 31546f61fd47SGustavo F. Padovan if (chan->partial_sdu_len > chan->sdu_len) 31556f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 31560a708f8fSGustavo F. Padovan else 31570a708f8fSGustavo F. Padovan err = 0; 31580a708f8fSGustavo F. Padovan 31590a708f8fSGustavo F. Padovan break; 31600a708f8fSGustavo F. Padovan 31610a708f8fSGustavo F. Padovan case L2CAP_SDU_END: 3162525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) 31630a708f8fSGustavo F. Padovan break; 31640a708f8fSGustavo F. Padovan 31656f61fd47SGustavo F. Padovan memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); 31660a708f8fSGustavo F. Padovan 3167525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SAR_SDU; 31686f61fd47SGustavo F. Padovan chan->partial_sdu_len += skb->len; 31690a708f8fSGustavo F. Padovan 31706f61fd47SGustavo F. Padovan if (chan->partial_sdu_len > pi->imtu) 31710a708f8fSGustavo F. Padovan goto drop; 31720a708f8fSGustavo F. Padovan 31736f61fd47SGustavo F. Padovan if (chan->partial_sdu_len == chan->sdu_len) { 31746f61fd47SGustavo F. Padovan _skb = skb_clone(chan->sdu, GFP_ATOMIC); 3175525cd185SGustavo F. Padovan err = sock_queue_rcv_skb(chan->sk, _skb); 31760a708f8fSGustavo F. Padovan if (err < 0) 31770a708f8fSGustavo F. Padovan kfree_skb(_skb); 31780a708f8fSGustavo F. Padovan } 31790a708f8fSGustavo F. Padovan err = 0; 31800a708f8fSGustavo F. Padovan 31810a708f8fSGustavo F. Padovan drop: 31826f61fd47SGustavo F. Padovan kfree_skb(chan->sdu); 31830a708f8fSGustavo F. Padovan break; 31840a708f8fSGustavo F. Padovan } 31850a708f8fSGustavo F. Padovan 31860a708f8fSGustavo F. Padovan kfree_skb(skb); 31870a708f8fSGustavo F. Padovan return err; 31880a708f8fSGustavo F. Padovan } 31890a708f8fSGustavo F. Padovan 3190525cd185SGustavo F. Padovan static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq) 31910a708f8fSGustavo F. Padovan { 31920a708f8fSGustavo F. Padovan struct sk_buff *skb; 31930a708f8fSGustavo F. Padovan u16 control; 31940a708f8fSGustavo F. Padovan 3195f1c6775bSGustavo F. Padovan while ((skb = skb_peek(&chan->srej_q))) { 31960a708f8fSGustavo F. Padovan if (bt_cb(skb)->tx_seq != tx_seq) 31970a708f8fSGustavo F. Padovan break; 31980a708f8fSGustavo F. Padovan 3199f1c6775bSGustavo F. Padovan skb = skb_dequeue(&chan->srej_q); 32000a708f8fSGustavo F. Padovan control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT; 3201525cd185SGustavo F. Padovan l2cap_ertm_reassembly_sdu(chan, skb, control); 320242e5c802SGustavo F. Padovan chan->buffer_seq_srej = 320342e5c802SGustavo F. Padovan (chan->buffer_seq_srej + 1) % 64; 32040a708f8fSGustavo F. Padovan tx_seq = (tx_seq + 1) % 64; 32050a708f8fSGustavo F. Padovan } 32060a708f8fSGustavo F. Padovan } 32070a708f8fSGustavo F. Padovan 3208525cd185SGustavo F. Padovan static void l2cap_resend_srejframe(struct l2cap_chan *chan, u8 tx_seq) 32090a708f8fSGustavo F. Padovan { 3210525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 32110a708f8fSGustavo F. Padovan struct srej_list *l, *tmp; 32120a708f8fSGustavo F. Padovan u16 control; 32130a708f8fSGustavo F. Padovan 32140a708f8fSGustavo F. Padovan list_for_each_entry_safe(l, tmp, SREJ_LIST(sk), list) { 32150a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 32160a708f8fSGustavo F. Padovan list_del(&l->list); 32170a708f8fSGustavo F. Padovan kfree(l); 32180a708f8fSGustavo F. Padovan return; 32190a708f8fSGustavo F. Padovan } 32200a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 32210a708f8fSGustavo F. Padovan control |= l->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 3222525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 32230a708f8fSGustavo F. Padovan list_del(&l->list); 32240a708f8fSGustavo F. Padovan list_add_tail(&l->list, SREJ_LIST(sk)); 32250a708f8fSGustavo F. Padovan } 32260a708f8fSGustavo F. Padovan } 32270a708f8fSGustavo F. Padovan 3228525cd185SGustavo F. Padovan static void l2cap_send_srejframe(struct l2cap_chan *chan, u8 tx_seq) 32290a708f8fSGustavo F. Padovan { 3230525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 32310a708f8fSGustavo F. Padovan struct srej_list *new; 32320a708f8fSGustavo F. Padovan u16 control; 32330a708f8fSGustavo F. Padovan 323442e5c802SGustavo F. Padovan while (tx_seq != chan->expected_tx_seq) { 32350a708f8fSGustavo F. Padovan control = L2CAP_SUPER_SELECT_REJECT; 323642e5c802SGustavo F. Padovan control |= chan->expected_tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; 3237525cd185SGustavo F. Padovan l2cap_send_sframe(chan, control); 32380a708f8fSGustavo F. Padovan 32390a708f8fSGustavo F. Padovan new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); 324042e5c802SGustavo F. Padovan new->tx_seq = chan->expected_tx_seq; 324142e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 32420a708f8fSGustavo F. Padovan list_add_tail(&new->list, SREJ_LIST(sk)); 32430a708f8fSGustavo F. Padovan } 324442e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 32450a708f8fSGustavo F. Padovan } 32460a708f8fSGustavo F. Padovan 3247525cd185SGustavo F. Padovan static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) 32480a708f8fSGustavo F. Padovan { 3249525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 32500a708f8fSGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(sk); 32510a708f8fSGustavo F. Padovan u8 tx_seq = __get_txseq(rx_control); 32520a708f8fSGustavo F. Padovan u8 req_seq = __get_reqseq(rx_control); 32530a708f8fSGustavo F. Padovan u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT; 32540a708f8fSGustavo F. Padovan int tx_seq_offset, expected_tx_seq_offset; 32550a708f8fSGustavo F. Padovan int num_to_ack = (pi->tx_win/6) + 1; 32560a708f8fSGustavo F. Padovan int err = 0; 32570a708f8fSGustavo F. Padovan 3258525cd185SGustavo F. Padovan BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len, 3259525cd185SGustavo F. Padovan tx_seq, rx_control); 32600a708f8fSGustavo F. Padovan 32610a708f8fSGustavo F. Padovan if (L2CAP_CTRL_FINAL & rx_control && 3262525cd185SGustavo F. Padovan chan->conn_state & L2CAP_CONN_WAIT_F) { 3263e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 32646a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 32650a708f8fSGustavo F. Padovan __mod_retrans_timer(); 3266525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_WAIT_F; 32670a708f8fSGustavo F. Padovan } 32680a708f8fSGustavo F. Padovan 326942e5c802SGustavo F. Padovan chan->expected_ack_seq = req_seq; 327042e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 32710a708f8fSGustavo F. Padovan 327242e5c802SGustavo F. Padovan if (tx_seq == chan->expected_tx_seq) 32730a708f8fSGustavo F. Padovan goto expected; 32740a708f8fSGustavo F. Padovan 327542e5c802SGustavo F. Padovan tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; 32760a708f8fSGustavo F. Padovan if (tx_seq_offset < 0) 32770a708f8fSGustavo F. Padovan tx_seq_offset += 64; 32780a708f8fSGustavo F. Padovan 32790a708f8fSGustavo F. Padovan /* invalid tx_seq */ 32800a708f8fSGustavo F. Padovan if (tx_seq_offset >= pi->tx_win) { 3281e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(pi->conn, chan, ECONNRESET); 32820a708f8fSGustavo F. Padovan goto drop; 32830a708f8fSGustavo F. Padovan } 32840a708f8fSGustavo F. Padovan 3285525cd185SGustavo F. Padovan if (chan->conn_state == L2CAP_CONN_LOCAL_BUSY) 32860a708f8fSGustavo F. Padovan goto drop; 32870a708f8fSGustavo F. Padovan 3288525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 32890a708f8fSGustavo F. Padovan struct srej_list *first; 32900a708f8fSGustavo F. Padovan 32910a708f8fSGustavo F. Padovan first = list_first_entry(SREJ_LIST(sk), 32920a708f8fSGustavo F. Padovan struct srej_list, list); 32930a708f8fSGustavo F. Padovan if (tx_seq == first->tx_seq) { 329442e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 3295525cd185SGustavo F. Padovan l2cap_check_srej_gap(chan, tx_seq); 32960a708f8fSGustavo F. Padovan 32970a708f8fSGustavo F. Padovan list_del(&first->list); 32980a708f8fSGustavo F. Padovan kfree(first); 32990a708f8fSGustavo F. Padovan 33000a708f8fSGustavo F. Padovan if (list_empty(SREJ_LIST(sk))) { 330142e5c802SGustavo F. Padovan chan->buffer_seq = chan->buffer_seq_srej; 3302525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SREJ_SENT; 3303525cd185SGustavo F. Padovan l2cap_send_ack(chan); 33040a708f8fSGustavo F. Padovan BT_DBG("sk %p, Exit SREJ_SENT", sk); 33050a708f8fSGustavo F. Padovan } 33060a708f8fSGustavo F. Padovan } else { 33070a708f8fSGustavo F. Padovan struct srej_list *l; 33080a708f8fSGustavo F. Padovan 33090a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 331042e5c802SGustavo F. Padovan if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0) 33110a708f8fSGustavo F. Padovan goto drop; 33120a708f8fSGustavo F. Padovan 33130a708f8fSGustavo F. Padovan list_for_each_entry(l, SREJ_LIST(sk), list) { 33140a708f8fSGustavo F. Padovan if (l->tx_seq == tx_seq) { 3315525cd185SGustavo F. Padovan l2cap_resend_srejframe(chan, tx_seq); 33160a708f8fSGustavo F. Padovan return 0; 33170a708f8fSGustavo F. Padovan } 33180a708f8fSGustavo F. Padovan } 3319525cd185SGustavo F. Padovan l2cap_send_srejframe(chan, tx_seq); 33200a708f8fSGustavo F. Padovan } 33210a708f8fSGustavo F. Padovan } else { 33220a708f8fSGustavo F. Padovan expected_tx_seq_offset = 332342e5c802SGustavo F. Padovan (chan->expected_tx_seq - chan->buffer_seq) % 64; 33240a708f8fSGustavo F. Padovan if (expected_tx_seq_offset < 0) 33250a708f8fSGustavo F. Padovan expected_tx_seq_offset += 64; 33260a708f8fSGustavo F. Padovan 33270a708f8fSGustavo F. Padovan /* duplicated tx_seq */ 33280a708f8fSGustavo F. Padovan if (tx_seq_offset < expected_tx_seq_offset) 33290a708f8fSGustavo F. Padovan goto drop; 33300a708f8fSGustavo F. Padovan 3331525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_SENT; 33320a708f8fSGustavo F. Padovan 33330a708f8fSGustavo F. Padovan BT_DBG("sk %p, Enter SREJ", sk); 33340a708f8fSGustavo F. Padovan 33350a708f8fSGustavo F. Padovan INIT_LIST_HEAD(SREJ_LIST(sk)); 333642e5c802SGustavo F. Padovan chan->buffer_seq_srej = chan->buffer_seq; 33370a708f8fSGustavo F. Padovan 3338f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->srej_q); 3339f1c6775bSGustavo F. Padovan __skb_queue_head_init(&chan->busy_q); 334042e5c802SGustavo F. Padovan l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); 33410a708f8fSGustavo F. Padovan 3342525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_PBIT; 33430a708f8fSGustavo F. Padovan 3344525cd185SGustavo F. Padovan l2cap_send_srejframe(chan, tx_seq); 33450a708f8fSGustavo F. Padovan 3346e92c8e70SGustavo F. Padovan del_timer(&chan->ack_timer); 33470a708f8fSGustavo F. Padovan } 33480a708f8fSGustavo F. Padovan return 0; 33490a708f8fSGustavo F. Padovan 33500a708f8fSGustavo F. Padovan expected: 335142e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 33520a708f8fSGustavo F. Padovan 3353525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 33540a708f8fSGustavo F. Padovan bt_cb(skb)->tx_seq = tx_seq; 33550a708f8fSGustavo F. Padovan bt_cb(skb)->sar = sar; 3356f1c6775bSGustavo F. Padovan __skb_queue_tail(&chan->srej_q, skb); 33570a708f8fSGustavo F. Padovan return 0; 33580a708f8fSGustavo F. Padovan } 33590a708f8fSGustavo F. Padovan 3360525cd185SGustavo F. Padovan err = l2cap_push_rx_skb(chan, skb, rx_control); 33610a708f8fSGustavo F. Padovan if (err < 0) 33620a708f8fSGustavo F. Padovan return 0; 33630a708f8fSGustavo F. Padovan 33640a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_FINAL) { 3365525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3366525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 33670a708f8fSGustavo F. Padovan else 3368525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 33690a708f8fSGustavo F. Padovan } 33700a708f8fSGustavo F. Padovan 33710a708f8fSGustavo F. Padovan __mod_ack_timer(); 33720a708f8fSGustavo F. Padovan 33736a026610SGustavo F. Padovan chan->num_acked = (chan->num_acked + 1) % num_to_ack; 33746a026610SGustavo F. Padovan if (chan->num_acked == num_to_ack - 1) 3375525cd185SGustavo F. Padovan l2cap_send_ack(chan); 33760a708f8fSGustavo F. Padovan 33770a708f8fSGustavo F. Padovan return 0; 33780a708f8fSGustavo F. Padovan 33790a708f8fSGustavo F. Padovan drop: 33800a708f8fSGustavo F. Padovan kfree_skb(skb); 33810a708f8fSGustavo F. Padovan return 0; 33820a708f8fSGustavo F. Padovan } 33830a708f8fSGustavo F. Padovan 3384525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control) 33850a708f8fSGustavo F. Padovan { 3386525cd185SGustavo F. Padovan struct sock *sk = chan->sk; 33870a708f8fSGustavo F. Padovan 33880a708f8fSGustavo F. Padovan BT_DBG("sk %p, req_seq %d ctrl 0x%4.4x", sk, __get_reqseq(rx_control), 33890a708f8fSGustavo F. Padovan rx_control); 33900a708f8fSGustavo F. Padovan 339142e5c802SGustavo F. Padovan chan->expected_ack_seq = __get_reqseq(rx_control); 339242e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 33930a708f8fSGustavo F. Padovan 33940a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) { 3395525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 3396525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { 3397525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && 33986a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 33990a708f8fSGustavo F. Padovan __mod_retrans_timer(); 34000a708f8fSGustavo F. Padovan 3401525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 3402525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 34030a708f8fSGustavo F. Padovan } else { 3404525cd185SGustavo F. Padovan l2cap_send_i_or_rr_or_rnr(chan); 34050a708f8fSGustavo F. Padovan } 34060a708f8fSGustavo F. Padovan 34070a708f8fSGustavo F. Padovan } else if (rx_control & L2CAP_CTRL_FINAL) { 3408525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 34090a708f8fSGustavo F. Padovan 3410525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3411525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 34120a708f8fSGustavo F. Padovan else 3413525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 34140a708f8fSGustavo F. Padovan 34150a708f8fSGustavo F. Padovan } else { 3416525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && 34176a026610SGustavo F. Padovan (chan->unacked_frames > 0)) 34180a708f8fSGustavo F. Padovan __mod_retrans_timer(); 34190a708f8fSGustavo F. Padovan 3420525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 3421525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_SREJ_SENT) 3422525cd185SGustavo F. Padovan l2cap_send_ack(chan); 34230a708f8fSGustavo F. Padovan else 3424525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 34250a708f8fSGustavo F. Padovan } 34260a708f8fSGustavo F. Padovan } 34270a708f8fSGustavo F. Padovan 3428525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control) 34290a708f8fSGustavo F. Padovan { 34300a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 34310a708f8fSGustavo F. Padovan 3432525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 34330a708f8fSGustavo F. Padovan 3434525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 34350a708f8fSGustavo F. Padovan 343642e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 343742e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 34380a708f8fSGustavo F. Padovan 34390a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_FINAL) { 3440525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_REJ_ACT) 3441525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REJ_ACT; 34420a708f8fSGustavo F. Padovan else 3443525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 34440a708f8fSGustavo F. Padovan } else { 3445525cd185SGustavo F. Padovan l2cap_retransmit_frames(chan); 34460a708f8fSGustavo F. Padovan 3447525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) 3448525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_REJ_ACT; 34490a708f8fSGustavo F. Padovan } 34500a708f8fSGustavo F. Padovan } 3451525cd185SGustavo F. Padovan static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control) 34520a708f8fSGustavo F. Padovan { 34530a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 34540a708f8fSGustavo F. Padovan 3455525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 34560a708f8fSGustavo F. Padovan 3457525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; 34580a708f8fSGustavo F. Padovan 34590a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) { 346042e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 346142e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 34620a708f8fSGustavo F. Padovan 3463525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 3464525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 34650a708f8fSGustavo F. Padovan 3466525cd185SGustavo F. Padovan l2cap_ertm_send(chan); 34670a708f8fSGustavo F. Padovan 3468525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) { 34696a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 3470525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_ACT; 34710a708f8fSGustavo F. Padovan } 34720a708f8fSGustavo F. Padovan } else if (rx_control & L2CAP_CTRL_FINAL) { 3473525cd185SGustavo F. Padovan if ((chan->conn_state & L2CAP_CONN_SREJ_ACT) && 34746a026610SGustavo F. Padovan chan->srej_save_reqseq == tx_seq) 3475525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_SREJ_ACT; 34760a708f8fSGustavo F. Padovan else 3477525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 34780a708f8fSGustavo F. Padovan } else { 3479525cd185SGustavo F. Padovan l2cap_retransmit_one_frame(chan, tx_seq); 3480525cd185SGustavo F. Padovan if (chan->conn_state & L2CAP_CONN_WAIT_F) { 34816a026610SGustavo F. Padovan chan->srej_save_reqseq = tx_seq; 3482525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SREJ_ACT; 34830a708f8fSGustavo F. Padovan } 34840a708f8fSGustavo F. Padovan } 34850a708f8fSGustavo F. Padovan } 34860a708f8fSGustavo F. Padovan 3487525cd185SGustavo F. Padovan static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control) 34880a708f8fSGustavo F. Padovan { 34890a708f8fSGustavo F. Padovan u8 tx_seq = __get_reqseq(rx_control); 34900a708f8fSGustavo F. Padovan 3491525cd185SGustavo F. Padovan BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); 34920a708f8fSGustavo F. Padovan 3493525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_REMOTE_BUSY; 349442e5c802SGustavo F. Padovan chan->expected_ack_seq = tx_seq; 349542e5c802SGustavo F. Padovan l2cap_drop_acked_frames(chan); 34960a708f8fSGustavo F. Padovan 34970a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3498525cd185SGustavo F. Padovan chan->conn_state |= L2CAP_CONN_SEND_FBIT; 34990a708f8fSGustavo F. Padovan 3500525cd185SGustavo F. Padovan if (!(chan->conn_state & L2CAP_CONN_SREJ_SENT)) { 3501e92c8e70SGustavo F. Padovan del_timer(&chan->retrans_timer); 35020a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3503525cd185SGustavo F. Padovan l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); 35040a708f8fSGustavo F. Padovan return; 35050a708f8fSGustavo F. Padovan } 35060a708f8fSGustavo F. Padovan 35070a708f8fSGustavo F. Padovan if (rx_control & L2CAP_CTRL_POLL) 3508525cd185SGustavo F. Padovan l2cap_send_srejtail(chan); 35090a708f8fSGustavo F. Padovan else 3510525cd185SGustavo F. Padovan l2cap_send_sframe(chan, L2CAP_SUPER_RCV_READY); 35110a708f8fSGustavo F. Padovan } 35120a708f8fSGustavo F. Padovan 3513525cd185SGustavo F. Padovan static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) 35140a708f8fSGustavo F. Padovan { 3515525cd185SGustavo F. Padovan BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len); 35160a708f8fSGustavo F. Padovan 35170a708f8fSGustavo F. Padovan if (L2CAP_CTRL_FINAL & rx_control && 3518525cd185SGustavo F. Padovan chan->conn_state & L2CAP_CONN_WAIT_F) { 3519e92c8e70SGustavo F. Padovan del_timer(&chan->monitor_timer); 35206a026610SGustavo F. Padovan if (chan->unacked_frames > 0) 35210a708f8fSGustavo F. Padovan __mod_retrans_timer(); 3522525cd185SGustavo F. Padovan chan->conn_state &= ~L2CAP_CONN_WAIT_F; 35230a708f8fSGustavo F. Padovan } 35240a708f8fSGustavo F. Padovan 35250a708f8fSGustavo F. Padovan switch (rx_control & L2CAP_CTRL_SUPERVISE) { 35260a708f8fSGustavo F. Padovan case L2CAP_SUPER_RCV_READY: 3527525cd185SGustavo F. Padovan l2cap_data_channel_rrframe(chan, rx_control); 35280a708f8fSGustavo F. Padovan break; 35290a708f8fSGustavo F. Padovan 35300a708f8fSGustavo F. Padovan case L2CAP_SUPER_REJECT: 3531525cd185SGustavo F. Padovan l2cap_data_channel_rejframe(chan, rx_control); 35320a708f8fSGustavo F. Padovan break; 35330a708f8fSGustavo F. Padovan 35340a708f8fSGustavo F. Padovan case L2CAP_SUPER_SELECT_REJECT: 3535525cd185SGustavo F. Padovan l2cap_data_channel_srejframe(chan, rx_control); 35360a708f8fSGustavo F. Padovan break; 35370a708f8fSGustavo F. Padovan 35380a708f8fSGustavo F. Padovan case L2CAP_SUPER_RCV_NOT_READY: 3539525cd185SGustavo F. Padovan l2cap_data_channel_rnrframe(chan, rx_control); 35400a708f8fSGustavo F. Padovan break; 35410a708f8fSGustavo F. Padovan } 35420a708f8fSGustavo F. Padovan 35430a708f8fSGustavo F. Padovan kfree_skb(skb); 35440a708f8fSGustavo F. Padovan return 0; 35450a708f8fSGustavo F. Padovan } 35460a708f8fSGustavo F. Padovan 35470a708f8fSGustavo F. Padovan static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb) 35480a708f8fSGustavo F. Padovan { 3549525cd185SGustavo F. Padovan struct l2cap_chan *chan = l2cap_pi(sk)->chan; 35500a708f8fSGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(sk); 35510a708f8fSGustavo F. Padovan u16 control; 35520a708f8fSGustavo F. Padovan u8 req_seq; 35530a708f8fSGustavo F. Padovan int len, next_tx_seq_offset, req_seq_offset; 35540a708f8fSGustavo F. Padovan 35550a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data); 35560a708f8fSGustavo F. Padovan skb_pull(skb, 2); 35570a708f8fSGustavo F. Padovan len = skb->len; 35580a708f8fSGustavo F. Padovan 35590a708f8fSGustavo F. Padovan /* 35600a708f8fSGustavo F. Padovan * We can just drop the corrupted I-frame here. 35610a708f8fSGustavo F. Padovan * Receiver will miss it and start proper recovery 35620a708f8fSGustavo F. Padovan * procedures and ask retransmission. 35630a708f8fSGustavo F. Padovan */ 35640a708f8fSGustavo F. Padovan if (l2cap_check_fcs(pi, skb)) 35650a708f8fSGustavo F. Padovan goto drop; 35660a708f8fSGustavo F. Padovan 35670a708f8fSGustavo F. Padovan if (__is_sar_start(control) && __is_iframe(control)) 35680a708f8fSGustavo F. Padovan len -= 2; 35690a708f8fSGustavo F. Padovan 35700a708f8fSGustavo F. Padovan if (pi->fcs == L2CAP_FCS_CRC16) 35710a708f8fSGustavo F. Padovan len -= 2; 35720a708f8fSGustavo F. Padovan 35730a708f8fSGustavo F. Padovan if (len > pi->mps) { 3574e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(pi->conn, chan, ECONNRESET); 35750a708f8fSGustavo F. Padovan goto drop; 35760a708f8fSGustavo F. Padovan } 35770a708f8fSGustavo F. Padovan 35780a708f8fSGustavo F. Padovan req_seq = __get_reqseq(control); 357942e5c802SGustavo F. Padovan req_seq_offset = (req_seq - chan->expected_ack_seq) % 64; 35800a708f8fSGustavo F. Padovan if (req_seq_offset < 0) 35810a708f8fSGustavo F. Padovan req_seq_offset += 64; 35820a708f8fSGustavo F. Padovan 35830a708f8fSGustavo F. Padovan next_tx_seq_offset = 358442e5c802SGustavo F. Padovan (chan->next_tx_seq - chan->expected_ack_seq) % 64; 35850a708f8fSGustavo F. Padovan if (next_tx_seq_offset < 0) 35860a708f8fSGustavo F. Padovan next_tx_seq_offset += 64; 35870a708f8fSGustavo F. Padovan 35880a708f8fSGustavo F. Padovan /* check for invalid req-seq */ 35890a708f8fSGustavo F. Padovan if (req_seq_offset > next_tx_seq_offset) { 3590e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(pi->conn, chan, ECONNRESET); 35910a708f8fSGustavo F. Padovan goto drop; 35920a708f8fSGustavo F. Padovan } 35930a708f8fSGustavo F. Padovan 35940a708f8fSGustavo F. Padovan if (__is_iframe(control)) { 35950a708f8fSGustavo F. Padovan if (len < 0) { 3596e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(pi->conn, chan, ECONNRESET); 35970a708f8fSGustavo F. Padovan goto drop; 35980a708f8fSGustavo F. Padovan } 35990a708f8fSGustavo F. Padovan 3600525cd185SGustavo F. Padovan l2cap_data_channel_iframe(chan, control, skb); 36010a708f8fSGustavo F. Padovan } else { 36020a708f8fSGustavo F. Padovan if (len != 0) { 36030a708f8fSGustavo F. Padovan BT_ERR("%d", len); 3604e92c8e70SGustavo F. Padovan l2cap_send_disconn_req(pi->conn, chan, ECONNRESET); 36050a708f8fSGustavo F. Padovan goto drop; 36060a708f8fSGustavo F. Padovan } 36070a708f8fSGustavo F. Padovan 3608525cd185SGustavo F. Padovan l2cap_data_channel_sframe(chan, control, skb); 36090a708f8fSGustavo F. Padovan } 36100a708f8fSGustavo F. Padovan 36110a708f8fSGustavo F. Padovan return 0; 36120a708f8fSGustavo F. Padovan 36130a708f8fSGustavo F. Padovan drop: 36140a708f8fSGustavo F. Padovan kfree_skb(skb); 36150a708f8fSGustavo F. Padovan return 0; 36160a708f8fSGustavo F. Padovan } 36170a708f8fSGustavo F. Padovan 36180a708f8fSGustavo F. Padovan static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) 36190a708f8fSGustavo F. Padovan { 362048454079SGustavo F. Padovan struct l2cap_chan *chan; 36210a708f8fSGustavo F. Padovan struct sock *sk; 36220a708f8fSGustavo F. Padovan struct l2cap_pinfo *pi; 36230a708f8fSGustavo F. Padovan u16 control; 36240a708f8fSGustavo F. Padovan u8 tx_seq; 36250a708f8fSGustavo F. Padovan int len; 36260a708f8fSGustavo F. Padovan 3627baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 362848454079SGustavo F. Padovan if (!chan) { 36290a708f8fSGustavo F. Padovan BT_DBG("unknown cid 0x%4.4x", cid); 36300a708f8fSGustavo F. Padovan goto drop; 36310a708f8fSGustavo F. Padovan } 36320a708f8fSGustavo F. Padovan 363348454079SGustavo F. Padovan sk = chan->sk; 36340a708f8fSGustavo F. Padovan pi = l2cap_pi(sk); 36350a708f8fSGustavo F. Padovan 36360a708f8fSGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 36370a708f8fSGustavo F. Padovan 36380a708f8fSGustavo F. Padovan if (sk->sk_state != BT_CONNECTED) 36390a708f8fSGustavo F. Padovan goto drop; 36400a708f8fSGustavo F. Padovan 36410a708f8fSGustavo F. Padovan switch (pi->mode) { 36420a708f8fSGustavo F. Padovan case L2CAP_MODE_BASIC: 36430a708f8fSGustavo F. Padovan /* If socket recv buffers overflows we drop data here 36440a708f8fSGustavo F. Padovan * which is *bad* because L2CAP has to be reliable. 36450a708f8fSGustavo F. Padovan * But we don't have any other choice. L2CAP doesn't 36460a708f8fSGustavo F. Padovan * provide flow control mechanism. */ 36470a708f8fSGustavo F. Padovan 36480a708f8fSGustavo F. Padovan if (pi->imtu < skb->len) 36490a708f8fSGustavo F. Padovan goto drop; 36500a708f8fSGustavo F. Padovan 36510a708f8fSGustavo F. Padovan if (!sock_queue_rcv_skb(sk, skb)) 36520a708f8fSGustavo F. Padovan goto done; 36530a708f8fSGustavo F. Padovan break; 36540a708f8fSGustavo F. Padovan 36550a708f8fSGustavo F. Padovan case L2CAP_MODE_ERTM: 36560a708f8fSGustavo F. Padovan if (!sock_owned_by_user(sk)) { 36570a708f8fSGustavo F. Padovan l2cap_ertm_data_rcv(sk, skb); 36580a708f8fSGustavo F. Padovan } else { 36590a708f8fSGustavo F. Padovan if (sk_add_backlog(sk, skb)) 36600a708f8fSGustavo F. Padovan goto drop; 36610a708f8fSGustavo F. Padovan } 36620a708f8fSGustavo F. Padovan 36630a708f8fSGustavo F. Padovan goto done; 36640a708f8fSGustavo F. Padovan 36650a708f8fSGustavo F. Padovan case L2CAP_MODE_STREAMING: 36660a708f8fSGustavo F. Padovan control = get_unaligned_le16(skb->data); 36670a708f8fSGustavo F. Padovan skb_pull(skb, 2); 36680a708f8fSGustavo F. Padovan len = skb->len; 36690a708f8fSGustavo F. Padovan 36700a708f8fSGustavo F. Padovan if (l2cap_check_fcs(pi, skb)) 36710a708f8fSGustavo F. Padovan goto drop; 36720a708f8fSGustavo F. Padovan 36730a708f8fSGustavo F. Padovan if (__is_sar_start(control)) 36740a708f8fSGustavo F. Padovan len -= 2; 36750a708f8fSGustavo F. Padovan 36760a708f8fSGustavo F. Padovan if (pi->fcs == L2CAP_FCS_CRC16) 36770a708f8fSGustavo F. Padovan len -= 2; 36780a708f8fSGustavo F. Padovan 36790a708f8fSGustavo F. Padovan if (len > pi->mps || len < 0 || __is_sframe(control)) 36800a708f8fSGustavo F. Padovan goto drop; 36810a708f8fSGustavo F. Padovan 36820a708f8fSGustavo F. Padovan tx_seq = __get_txseq(control); 36830a708f8fSGustavo F. Padovan 368442e5c802SGustavo F. Padovan if (chan->expected_tx_seq == tx_seq) 368542e5c802SGustavo F. Padovan chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; 36860a708f8fSGustavo F. Padovan else 368742e5c802SGustavo F. Padovan chan->expected_tx_seq = (tx_seq + 1) % 64; 36880a708f8fSGustavo F. Padovan 3689525cd185SGustavo F. Padovan l2cap_streaming_reassembly_sdu(chan, skb, control); 36900a708f8fSGustavo F. Padovan 36910a708f8fSGustavo F. Padovan goto done; 36920a708f8fSGustavo F. Padovan 36930a708f8fSGustavo F. Padovan default: 36940a708f8fSGustavo F. Padovan BT_DBG("sk %p: bad mode 0x%2.2x", sk, pi->mode); 36950a708f8fSGustavo F. Padovan break; 36960a708f8fSGustavo F. Padovan } 36970a708f8fSGustavo F. Padovan 36980a708f8fSGustavo F. Padovan drop: 36990a708f8fSGustavo F. Padovan kfree_skb(skb); 37000a708f8fSGustavo F. Padovan 37010a708f8fSGustavo F. Padovan done: 37020a708f8fSGustavo F. Padovan if (sk) 37030a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 37040a708f8fSGustavo F. Padovan 37050a708f8fSGustavo F. Padovan return 0; 37060a708f8fSGustavo F. Padovan } 37070a708f8fSGustavo F. Padovan 37080a708f8fSGustavo F. Padovan static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb) 37090a708f8fSGustavo F. Padovan { 37100a708f8fSGustavo F. Padovan struct sock *sk; 37110a708f8fSGustavo F. Padovan 37120a708f8fSGustavo F. Padovan sk = l2cap_get_sock_by_psm(0, psm, conn->src); 37130a708f8fSGustavo F. Padovan if (!sk) 37140a708f8fSGustavo F. Padovan goto drop; 37150a708f8fSGustavo F. Padovan 37160a708f8fSGustavo F. Padovan bh_lock_sock(sk); 37170a708f8fSGustavo F. Padovan 37180a708f8fSGustavo F. Padovan BT_DBG("sk %p, len %d", sk, skb->len); 37190a708f8fSGustavo F. Padovan 37200a708f8fSGustavo F. Padovan if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) 37210a708f8fSGustavo F. Padovan goto drop; 37220a708f8fSGustavo F. Padovan 37230a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->imtu < skb->len) 37240a708f8fSGustavo F. Padovan goto drop; 37250a708f8fSGustavo F. Padovan 37260a708f8fSGustavo F. Padovan if (!sock_queue_rcv_skb(sk, skb)) 37270a708f8fSGustavo F. Padovan goto done; 37280a708f8fSGustavo F. Padovan 37290a708f8fSGustavo F. Padovan drop: 37300a708f8fSGustavo F. Padovan kfree_skb(skb); 37310a708f8fSGustavo F. Padovan 37320a708f8fSGustavo F. Padovan done: 37330a708f8fSGustavo F. Padovan if (sk) 37340a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 37350a708f8fSGustavo F. Padovan return 0; 37360a708f8fSGustavo F. Padovan } 37370a708f8fSGustavo F. Padovan 37380a708f8fSGustavo F. Padovan static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) 37390a708f8fSGustavo F. Padovan { 37400a708f8fSGustavo F. Padovan struct l2cap_hdr *lh = (void *) skb->data; 37410a708f8fSGustavo F. Padovan u16 cid, len; 37420a708f8fSGustavo F. Padovan __le16 psm; 37430a708f8fSGustavo F. Padovan 37440a708f8fSGustavo F. Padovan skb_pull(skb, L2CAP_HDR_SIZE); 37450a708f8fSGustavo F. Padovan cid = __le16_to_cpu(lh->cid); 37460a708f8fSGustavo F. Padovan len = __le16_to_cpu(lh->len); 37470a708f8fSGustavo F. Padovan 37480a708f8fSGustavo F. Padovan if (len != skb->len) { 37490a708f8fSGustavo F. Padovan kfree_skb(skb); 37500a708f8fSGustavo F. Padovan return; 37510a708f8fSGustavo F. Padovan } 37520a708f8fSGustavo F. Padovan 37530a708f8fSGustavo F. Padovan BT_DBG("len %d, cid 0x%4.4x", len, cid); 37540a708f8fSGustavo F. Padovan 37550a708f8fSGustavo F. Padovan switch (cid) { 37563300d9a9SClaudio Takahasi case L2CAP_CID_LE_SIGNALING: 37570a708f8fSGustavo F. Padovan case L2CAP_CID_SIGNALING: 37580a708f8fSGustavo F. Padovan l2cap_sig_channel(conn, skb); 37590a708f8fSGustavo F. Padovan break; 37600a708f8fSGustavo F. Padovan 37610a708f8fSGustavo F. Padovan case L2CAP_CID_CONN_LESS: 37620a708f8fSGustavo F. Padovan psm = get_unaligned_le16(skb->data); 37630a708f8fSGustavo F. Padovan skb_pull(skb, 2); 37640a708f8fSGustavo F. Padovan l2cap_conless_channel(conn, psm, skb); 37650a708f8fSGustavo F. Padovan break; 37660a708f8fSGustavo F. Padovan 37670a708f8fSGustavo F. Padovan default: 37680a708f8fSGustavo F. Padovan l2cap_data_channel(conn, cid, skb); 37690a708f8fSGustavo F. Padovan break; 37700a708f8fSGustavo F. Padovan } 37710a708f8fSGustavo F. Padovan } 37720a708f8fSGustavo F. Padovan 37730a708f8fSGustavo F. Padovan /* ---- L2CAP interface with lower layer (HCI) ---- */ 37740a708f8fSGustavo F. Padovan 37750a708f8fSGustavo F. Padovan static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) 37760a708f8fSGustavo F. Padovan { 37770a708f8fSGustavo F. Padovan int exact = 0, lm1 = 0, lm2 = 0; 37780a708f8fSGustavo F. Padovan register struct sock *sk; 37790a708f8fSGustavo F. Padovan struct hlist_node *node; 37800a708f8fSGustavo F. Padovan 37810a708f8fSGustavo F. Padovan if (type != ACL_LINK) 37820a708f8fSGustavo F. Padovan return -EINVAL; 37830a708f8fSGustavo F. Padovan 37840a708f8fSGustavo F. Padovan BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); 37850a708f8fSGustavo F. Padovan 37860a708f8fSGustavo F. Padovan /* Find listening sockets and check their link_mode */ 37870a708f8fSGustavo F. Padovan read_lock(&l2cap_sk_list.lock); 37880a708f8fSGustavo F. Padovan sk_for_each(sk, node, &l2cap_sk_list.head) { 37890a708f8fSGustavo F. Padovan if (sk->sk_state != BT_LISTEN) 37900a708f8fSGustavo F. Padovan continue; 37910a708f8fSGustavo F. Padovan 37920a708f8fSGustavo F. Padovan if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { 37930a708f8fSGustavo F. Padovan lm1 |= HCI_LM_ACCEPT; 37940a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->role_switch) 37950a708f8fSGustavo F. Padovan lm1 |= HCI_LM_MASTER; 37960a708f8fSGustavo F. Padovan exact++; 37970a708f8fSGustavo F. Padovan } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { 37980a708f8fSGustavo F. Padovan lm2 |= HCI_LM_ACCEPT; 37990a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->role_switch) 38000a708f8fSGustavo F. Padovan lm2 |= HCI_LM_MASTER; 38010a708f8fSGustavo F. Padovan } 38020a708f8fSGustavo F. Padovan } 38030a708f8fSGustavo F. Padovan read_unlock(&l2cap_sk_list.lock); 38040a708f8fSGustavo F. Padovan 38050a708f8fSGustavo F. Padovan return exact ? lm1 : lm2; 38060a708f8fSGustavo F. Padovan } 38070a708f8fSGustavo F. Padovan 38080a708f8fSGustavo F. Padovan static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) 38090a708f8fSGustavo F. Padovan { 38100a708f8fSGustavo F. Padovan struct l2cap_conn *conn; 38110a708f8fSGustavo F. Padovan 38120a708f8fSGustavo F. Padovan BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); 38130a708f8fSGustavo F. Padovan 3814acd7d370SVille Tervo if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) 38150a708f8fSGustavo F. Padovan return -EINVAL; 38160a708f8fSGustavo F. Padovan 38170a708f8fSGustavo F. Padovan if (!status) { 38180a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, status); 38190a708f8fSGustavo F. Padovan if (conn) 38200a708f8fSGustavo F. Padovan l2cap_conn_ready(conn); 38210a708f8fSGustavo F. Padovan } else 38220a708f8fSGustavo F. Padovan l2cap_conn_del(hcon, bt_err(status)); 38230a708f8fSGustavo F. Padovan 38240a708f8fSGustavo F. Padovan return 0; 38250a708f8fSGustavo F. Padovan } 38260a708f8fSGustavo F. Padovan 38270a708f8fSGustavo F. Padovan static int l2cap_disconn_ind(struct hci_conn *hcon) 38280a708f8fSGustavo F. Padovan { 38290a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 38300a708f8fSGustavo F. Padovan 38310a708f8fSGustavo F. Padovan BT_DBG("hcon %p", hcon); 38320a708f8fSGustavo F. Padovan 38330a708f8fSGustavo F. Padovan if (hcon->type != ACL_LINK || !conn) 38340a708f8fSGustavo F. Padovan return 0x13; 38350a708f8fSGustavo F. Padovan 38360a708f8fSGustavo F. Padovan return conn->disc_reason; 38370a708f8fSGustavo F. Padovan } 38380a708f8fSGustavo F. Padovan 38390a708f8fSGustavo F. Padovan static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) 38400a708f8fSGustavo F. Padovan { 38410a708f8fSGustavo F. Padovan BT_DBG("hcon %p reason %d", hcon, reason); 38420a708f8fSGustavo F. Padovan 3843acd7d370SVille Tervo if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) 38440a708f8fSGustavo F. Padovan return -EINVAL; 38450a708f8fSGustavo F. Padovan 38460a708f8fSGustavo F. Padovan l2cap_conn_del(hcon, bt_err(reason)); 38470a708f8fSGustavo F. Padovan 38480a708f8fSGustavo F. Padovan return 0; 38490a708f8fSGustavo F. Padovan } 38500a708f8fSGustavo F. Padovan 38510a708f8fSGustavo F. Padovan static inline void l2cap_check_encryption(struct sock *sk, u8 encrypt) 38520a708f8fSGustavo F. Padovan { 38530a708f8fSGustavo F. Padovan if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM) 38540a708f8fSGustavo F. Padovan return; 38550a708f8fSGustavo F. Padovan 38560a708f8fSGustavo F. Padovan if (encrypt == 0x00) { 38570a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->sec_level == BT_SECURITY_MEDIUM) { 38580a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 38590a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, HZ * 5); 38600a708f8fSGustavo F. Padovan } else if (l2cap_pi(sk)->sec_level == BT_SECURITY_HIGH) 38610a708f8fSGustavo F. Padovan __l2cap_sock_close(sk, ECONNREFUSED); 38620a708f8fSGustavo F. Padovan } else { 38630a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->sec_level == BT_SECURITY_MEDIUM) 38640a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 38650a708f8fSGustavo F. Padovan } 38660a708f8fSGustavo F. Padovan } 38670a708f8fSGustavo F. Padovan 38680a708f8fSGustavo F. Padovan static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) 38690a708f8fSGustavo F. Padovan { 38700a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 387148454079SGustavo F. Padovan struct l2cap_chan *chan; 38720a708f8fSGustavo F. Padovan 38730a708f8fSGustavo F. Padovan if (!conn) 38740a708f8fSGustavo F. Padovan return 0; 38750a708f8fSGustavo F. Padovan 38760a708f8fSGustavo F. Padovan BT_DBG("conn %p", conn); 38770a708f8fSGustavo F. Padovan 3878baa7e1faSGustavo F. Padovan read_lock(&conn->chan_lock); 38790a708f8fSGustavo F. Padovan 3880baa7e1faSGustavo F. Padovan list_for_each_entry(chan, &conn->chan_l, list) { 388148454079SGustavo F. Padovan struct sock *sk = chan->sk; 3882baa7e1faSGustavo F. Padovan 38830a708f8fSGustavo F. Padovan bh_lock_sock(sk); 38840a708f8fSGustavo F. Padovan 38850a708f8fSGustavo F. Padovan if (l2cap_pi(sk)->conf_state & L2CAP_CONF_CONNECT_PEND) { 38860a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 38870a708f8fSGustavo F. Padovan continue; 38880a708f8fSGustavo F. Padovan } 38890a708f8fSGustavo F. Padovan 38900a708f8fSGustavo F. Padovan if (!status && (sk->sk_state == BT_CONNECTED || 38910a708f8fSGustavo F. Padovan sk->sk_state == BT_CONFIG)) { 38920a708f8fSGustavo F. Padovan l2cap_check_encryption(sk, encrypt); 38930a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 38940a708f8fSGustavo F. Padovan continue; 38950a708f8fSGustavo F. Padovan } 38960a708f8fSGustavo F. Padovan 38970a708f8fSGustavo F. Padovan if (sk->sk_state == BT_CONNECT) { 38980a708f8fSGustavo F. Padovan if (!status) { 38990a708f8fSGustavo F. Padovan struct l2cap_conn_req req; 39000a708f8fSGustavo F. Padovan req.scid = cpu_to_le16(l2cap_pi(sk)->scid); 39010a708f8fSGustavo F. Padovan req.psm = l2cap_pi(sk)->psm; 39020a708f8fSGustavo F. Padovan 3903fc7f8a7eSGustavo F. Padovan chan->ident = l2cap_get_ident(conn); 39040a708f8fSGustavo F. Padovan l2cap_pi(sk)->conf_state |= L2CAP_CONF_CONNECT_PEND; 39050a708f8fSGustavo F. Padovan 3906fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, 39070a708f8fSGustavo F. Padovan L2CAP_CONN_REQ, sizeof(req), &req); 39080a708f8fSGustavo F. Padovan } else { 39090a708f8fSGustavo F. Padovan l2cap_sock_clear_timer(sk); 39100a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, HZ / 10); 39110a708f8fSGustavo F. Padovan } 39120a708f8fSGustavo F. Padovan } else if (sk->sk_state == BT_CONNECT2) { 39130a708f8fSGustavo F. Padovan struct l2cap_conn_rsp rsp; 39140a708f8fSGustavo F. Padovan __u16 result; 39150a708f8fSGustavo F. Padovan 39160a708f8fSGustavo F. Padovan if (!status) { 39170a708f8fSGustavo F. Padovan sk->sk_state = BT_CONFIG; 39180a708f8fSGustavo F. Padovan result = L2CAP_CR_SUCCESS; 39190a708f8fSGustavo F. Padovan } else { 39200a708f8fSGustavo F. Padovan sk->sk_state = BT_DISCONN; 39210a708f8fSGustavo F. Padovan l2cap_sock_set_timer(sk, HZ / 10); 39220a708f8fSGustavo F. Padovan result = L2CAP_CR_SEC_BLOCK; 39230a708f8fSGustavo F. Padovan } 39240a708f8fSGustavo F. Padovan 39250a708f8fSGustavo F. Padovan rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid); 39260a708f8fSGustavo F. Padovan rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid); 39270a708f8fSGustavo F. Padovan rsp.result = cpu_to_le16(result); 39280a708f8fSGustavo F. Padovan rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); 3929fc7f8a7eSGustavo F. Padovan l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, 3930fc7f8a7eSGustavo F. Padovan sizeof(rsp), &rsp); 39310a708f8fSGustavo F. Padovan } 39320a708f8fSGustavo F. Padovan 39330a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 39340a708f8fSGustavo F. Padovan } 39350a708f8fSGustavo F. Padovan 3936baa7e1faSGustavo F. Padovan read_unlock(&conn->chan_lock); 39370a708f8fSGustavo F. Padovan 39380a708f8fSGustavo F. Padovan return 0; 39390a708f8fSGustavo F. Padovan } 39400a708f8fSGustavo F. Padovan 39410a708f8fSGustavo F. Padovan static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) 39420a708f8fSGustavo F. Padovan { 39430a708f8fSGustavo F. Padovan struct l2cap_conn *conn = hcon->l2cap_data; 39440a708f8fSGustavo F. Padovan 39450a708f8fSGustavo F. Padovan if (!conn) 39460a708f8fSGustavo F. Padovan conn = l2cap_conn_add(hcon, 0); 39470a708f8fSGustavo F. Padovan 39480a708f8fSGustavo F. Padovan if (!conn) 39490a708f8fSGustavo F. Padovan goto drop; 39500a708f8fSGustavo F. Padovan 39510a708f8fSGustavo F. Padovan BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); 39520a708f8fSGustavo F. Padovan 39530a708f8fSGustavo F. Padovan if (!(flags & ACL_CONT)) { 39540a708f8fSGustavo F. Padovan struct l2cap_hdr *hdr; 395548454079SGustavo F. Padovan struct l2cap_chan *chan; 39560a708f8fSGustavo F. Padovan u16 cid; 39570a708f8fSGustavo F. Padovan int len; 39580a708f8fSGustavo F. Padovan 39590a708f8fSGustavo F. Padovan if (conn->rx_len) { 39600a708f8fSGustavo F. Padovan BT_ERR("Unexpected start frame (len %d)", skb->len); 39610a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 39620a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 39630a708f8fSGustavo F. Padovan conn->rx_len = 0; 39640a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 39650a708f8fSGustavo F. Padovan } 39660a708f8fSGustavo F. Padovan 39670a708f8fSGustavo F. Padovan /* Start fragment always begin with Basic L2CAP header */ 39680a708f8fSGustavo F. Padovan if (skb->len < L2CAP_HDR_SIZE) { 39690a708f8fSGustavo F. Padovan BT_ERR("Frame is too short (len %d)", skb->len); 39700a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 39710a708f8fSGustavo F. Padovan goto drop; 39720a708f8fSGustavo F. Padovan } 39730a708f8fSGustavo F. Padovan 39740a708f8fSGustavo F. Padovan hdr = (struct l2cap_hdr *) skb->data; 39750a708f8fSGustavo F. Padovan len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; 39760a708f8fSGustavo F. Padovan cid = __le16_to_cpu(hdr->cid); 39770a708f8fSGustavo F. Padovan 39780a708f8fSGustavo F. Padovan if (len == skb->len) { 39790a708f8fSGustavo F. Padovan /* Complete frame received */ 39800a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, skb); 39810a708f8fSGustavo F. Padovan return 0; 39820a708f8fSGustavo F. Padovan } 39830a708f8fSGustavo F. Padovan 39840a708f8fSGustavo F. Padovan BT_DBG("Start: total len %d, frag len %d", len, skb->len); 39850a708f8fSGustavo F. Padovan 39860a708f8fSGustavo F. Padovan if (skb->len > len) { 39870a708f8fSGustavo F. Padovan BT_ERR("Frame is too long (len %d, expected len %d)", 39880a708f8fSGustavo F. Padovan skb->len, len); 39890a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 39900a708f8fSGustavo F. Padovan goto drop; 39910a708f8fSGustavo F. Padovan } 39920a708f8fSGustavo F. Padovan 3993baa7e1faSGustavo F. Padovan chan = l2cap_get_chan_by_scid(conn, cid); 39940a708f8fSGustavo F. Padovan 399548454079SGustavo F. Padovan if (chan && chan->sk) { 399648454079SGustavo F. Padovan struct sock *sk = chan->sk; 399748454079SGustavo F. Padovan 399848454079SGustavo F. Padovan if (l2cap_pi(sk)->imtu < len - L2CAP_HDR_SIZE) { 399948454079SGustavo F. Padovan BT_ERR("Frame exceeding recv MTU (len %d, " 400048454079SGustavo F. Padovan "MTU %d)", len, 400148454079SGustavo F. Padovan l2cap_pi(sk)->imtu); 40020a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 40030a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 40040a708f8fSGustavo F. Padovan goto drop; 40050a708f8fSGustavo F. Padovan } 40060a708f8fSGustavo F. Padovan bh_unlock_sock(sk); 400748454079SGustavo F. Padovan } 40080a708f8fSGustavo F. Padovan 40090a708f8fSGustavo F. Padovan /* Allocate skb for the complete frame (with header) */ 40100a708f8fSGustavo F. Padovan conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); 40110a708f8fSGustavo F. Padovan if (!conn->rx_skb) 40120a708f8fSGustavo F. Padovan goto drop; 40130a708f8fSGustavo F. Padovan 40140a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 40150a708f8fSGustavo F. Padovan skb->len); 40160a708f8fSGustavo F. Padovan conn->rx_len = len - skb->len; 40170a708f8fSGustavo F. Padovan } else { 40180a708f8fSGustavo F. Padovan BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); 40190a708f8fSGustavo F. Padovan 40200a708f8fSGustavo F. Padovan if (!conn->rx_len) { 40210a708f8fSGustavo F. Padovan BT_ERR("Unexpected continuation frame (len %d)", skb->len); 40220a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 40230a708f8fSGustavo F. Padovan goto drop; 40240a708f8fSGustavo F. Padovan } 40250a708f8fSGustavo F. Padovan 40260a708f8fSGustavo F. Padovan if (skb->len > conn->rx_len) { 40270a708f8fSGustavo F. Padovan BT_ERR("Fragment is too long (len %d, expected %d)", 40280a708f8fSGustavo F. Padovan skb->len, conn->rx_len); 40290a708f8fSGustavo F. Padovan kfree_skb(conn->rx_skb); 40300a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 40310a708f8fSGustavo F. Padovan conn->rx_len = 0; 40320a708f8fSGustavo F. Padovan l2cap_conn_unreliable(conn, ECOMM); 40330a708f8fSGustavo F. Padovan goto drop; 40340a708f8fSGustavo F. Padovan } 40350a708f8fSGustavo F. Padovan 40360a708f8fSGustavo F. Padovan skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), 40370a708f8fSGustavo F. Padovan skb->len); 40380a708f8fSGustavo F. Padovan conn->rx_len -= skb->len; 40390a708f8fSGustavo F. Padovan 40400a708f8fSGustavo F. Padovan if (!conn->rx_len) { 40410a708f8fSGustavo F. Padovan /* Complete frame received */ 40420a708f8fSGustavo F. Padovan l2cap_recv_frame(conn, conn->rx_skb); 40430a708f8fSGustavo F. Padovan conn->rx_skb = NULL; 40440a708f8fSGustavo F. Padovan } 40450a708f8fSGustavo F. Padovan } 40460a708f8fSGustavo F. Padovan 40470a708f8fSGustavo F. Padovan drop: 40480a708f8fSGustavo F. Padovan kfree_skb(skb); 40490a708f8fSGustavo F. Padovan return 0; 40500a708f8fSGustavo F. Padovan } 40510a708f8fSGustavo F. Padovan 40520a708f8fSGustavo F. Padovan static int l2cap_debugfs_show(struct seq_file *f, void *p) 40530a708f8fSGustavo F. Padovan { 40540a708f8fSGustavo F. Padovan struct sock *sk; 40550a708f8fSGustavo F. Padovan struct hlist_node *node; 40560a708f8fSGustavo F. Padovan 40570a708f8fSGustavo F. Padovan read_lock_bh(&l2cap_sk_list.lock); 40580a708f8fSGustavo F. Padovan 40590a708f8fSGustavo F. Padovan sk_for_each(sk, node, &l2cap_sk_list.head) { 40600a708f8fSGustavo F. Padovan struct l2cap_pinfo *pi = l2cap_pi(sk); 40610a708f8fSGustavo F. Padovan 4062903d343eSGustavo F. Padovan seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", 40630a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->src), 40640a708f8fSGustavo F. Padovan batostr(&bt_sk(sk)->dst), 40650a708f8fSGustavo F. Padovan sk->sk_state, __le16_to_cpu(pi->psm), 40660a708f8fSGustavo F. Padovan pi->scid, pi->dcid, 4067903d343eSGustavo F. Padovan pi->imtu, pi->omtu, pi->sec_level, 4068903d343eSGustavo F. Padovan pi->mode); 40690a708f8fSGustavo F. Padovan } 40700a708f8fSGustavo F. Padovan 40710a708f8fSGustavo F. Padovan read_unlock_bh(&l2cap_sk_list.lock); 40720a708f8fSGustavo F. Padovan 40730a708f8fSGustavo F. Padovan return 0; 40740a708f8fSGustavo F. Padovan } 40750a708f8fSGustavo F. Padovan 40760a708f8fSGustavo F. Padovan static int l2cap_debugfs_open(struct inode *inode, struct file *file) 40770a708f8fSGustavo F. Padovan { 40780a708f8fSGustavo F. Padovan return single_open(file, l2cap_debugfs_show, inode->i_private); 40790a708f8fSGustavo F. Padovan } 40800a708f8fSGustavo F. Padovan 40810a708f8fSGustavo F. Padovan static const struct file_operations l2cap_debugfs_fops = { 40820a708f8fSGustavo F. Padovan .open = l2cap_debugfs_open, 40830a708f8fSGustavo F. Padovan .read = seq_read, 40840a708f8fSGustavo F. Padovan .llseek = seq_lseek, 40850a708f8fSGustavo F. Padovan .release = single_release, 40860a708f8fSGustavo F. Padovan }; 40870a708f8fSGustavo F. Padovan 40880a708f8fSGustavo F. Padovan static struct dentry *l2cap_debugfs; 40890a708f8fSGustavo F. Padovan 40900a708f8fSGustavo F. Padovan static struct hci_proto l2cap_hci_proto = { 40910a708f8fSGustavo F. Padovan .name = "L2CAP", 40920a708f8fSGustavo F. Padovan .id = HCI_PROTO_L2CAP, 40930a708f8fSGustavo F. Padovan .connect_ind = l2cap_connect_ind, 40940a708f8fSGustavo F. Padovan .connect_cfm = l2cap_connect_cfm, 40950a708f8fSGustavo F. Padovan .disconn_ind = l2cap_disconn_ind, 40960a708f8fSGustavo F. Padovan .disconn_cfm = l2cap_disconn_cfm, 40970a708f8fSGustavo F. Padovan .security_cfm = l2cap_security_cfm, 40980a708f8fSGustavo F. Padovan .recv_acldata = l2cap_recv_acldata 40990a708f8fSGustavo F. Padovan }; 41000a708f8fSGustavo F. Padovan 410164274518SGustavo F. Padovan int __init l2cap_init(void) 41020a708f8fSGustavo F. Padovan { 41030a708f8fSGustavo F. Padovan int err; 41040a708f8fSGustavo F. Padovan 4105bb58f747SGustavo F. Padovan err = l2cap_init_sockets(); 41060a708f8fSGustavo F. Padovan if (err < 0) 41070a708f8fSGustavo F. Padovan return err; 41080a708f8fSGustavo F. Padovan 41090a708f8fSGustavo F. Padovan _busy_wq = create_singlethread_workqueue("l2cap"); 41100a708f8fSGustavo F. Padovan if (!_busy_wq) { 4111bb58f747SGustavo F. Padovan err = -ENOMEM; 41120a708f8fSGustavo F. Padovan goto error; 41130a708f8fSGustavo F. Padovan } 41140a708f8fSGustavo F. Padovan 41150a708f8fSGustavo F. Padovan err = hci_register_proto(&l2cap_hci_proto); 41160a708f8fSGustavo F. Padovan if (err < 0) { 41170a708f8fSGustavo F. Padovan BT_ERR("L2CAP protocol registration failed"); 41180a708f8fSGustavo F. Padovan bt_sock_unregister(BTPROTO_L2CAP); 41190a708f8fSGustavo F. Padovan goto error; 41200a708f8fSGustavo F. Padovan } 41210a708f8fSGustavo F. Padovan 41220a708f8fSGustavo F. Padovan if (bt_debugfs) { 41230a708f8fSGustavo F. Padovan l2cap_debugfs = debugfs_create_file("l2cap", 0444, 41240a708f8fSGustavo F. Padovan bt_debugfs, NULL, &l2cap_debugfs_fops); 41250a708f8fSGustavo F. Padovan if (!l2cap_debugfs) 41260a708f8fSGustavo F. Padovan BT_ERR("Failed to create L2CAP debug file"); 41270a708f8fSGustavo F. Padovan } 41280a708f8fSGustavo F. Padovan 41290a708f8fSGustavo F. Padovan return 0; 41300a708f8fSGustavo F. Padovan 41310a708f8fSGustavo F. Padovan error: 41320a708f8fSGustavo F. Padovan destroy_workqueue(_busy_wq); 4133bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 41340a708f8fSGustavo F. Padovan return err; 41350a708f8fSGustavo F. Padovan } 41360a708f8fSGustavo F. Padovan 413764274518SGustavo F. Padovan void l2cap_exit(void) 41380a708f8fSGustavo F. Padovan { 41390a708f8fSGustavo F. Padovan debugfs_remove(l2cap_debugfs); 41400a708f8fSGustavo F. Padovan 41410a708f8fSGustavo F. Padovan flush_workqueue(_busy_wq); 41420a708f8fSGustavo F. Padovan destroy_workqueue(_busy_wq); 41430a708f8fSGustavo F. Padovan 41440a708f8fSGustavo F. Padovan if (hci_unregister_proto(&l2cap_hci_proto) < 0) 41450a708f8fSGustavo F. Padovan BT_ERR("L2CAP protocol unregistration failed"); 41460a708f8fSGustavo F. Padovan 4147bb58f747SGustavo F. Padovan l2cap_cleanup_sockets(); 41480a708f8fSGustavo F. Padovan } 41490a708f8fSGustavo F. Padovan 41500a708f8fSGustavo F. Padovan module_param(disable_ertm, bool, 0644); 41510a708f8fSGustavo F. Padovan MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); 4152