YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2000-2001 Qualcomm Incorporated |
Gustavo F. Padovan | ce5706b | 2010-07-13 11:57:11 -0300 | [diff] [blame] | 4 | Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org> |
Gustavo F. Padovan | 5d8868f | 2010-07-16 16:18:39 -0300 | [diff] [blame] | 5 | Copyright (C) 2010 Google Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
| 7 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License version 2 as |
| 11 | published by the Free Software Foundation; |
| 12 | |
| 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 14 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 16 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 17 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 18 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 19 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 22 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 23 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | SOFTWARE IS DISCLAIMED. |
| 25 | */ |
| 26 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 27 | /* Bluetooth L2CAP core. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/module.h> |
| 30 | |
| 31 | #include <linux/types.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 32 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/errno.h> |
| 34 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/sched.h> |
| 36 | #include <linux/slab.h> |
| 37 | #include <linux/poll.h> |
| 38 | #include <linux/fcntl.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/interrupt.h> |
| 41 | #include <linux/socket.h> |
| 42 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/list.h> |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 44 | #include <linux/device.h> |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 45 | #include <linux/debugfs.h> |
| 46 | #include <linux/seq_file.h> |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 47 | #include <linux/uaccess.h> |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 48 | #include <linux/crc16.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <net/sock.h> |
| 50 | |
| 51 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <asm/unaligned.h> |
| 53 | |
| 54 | #include <net/bluetooth/bluetooth.h> |
| 55 | #include <net/bluetooth/hci_core.h> |
| 56 | #include <net/bluetooth/l2cap.h> |
| 57 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 58 | int disable_ertm; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 59 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 60 | static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 61 | static u8 l2cap_fixed_chan[8] = { 0x02, }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 63 | static struct workqueue_struct *_busy_wq; |
| 64 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 65 | LIST_HEAD(chan_list); |
| 66 | DEFINE_RWLOCK(chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 68 | static void l2cap_busy_work(struct work_struct *work); |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, |
| 71 | u8 code, u8 ident, u16 dlen, void *data); |
Gustavo F. Padovan | 6e9e43f | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 72 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, |
| 73 | void *data); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 74 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data); |
Gustavo F. Padovan | 6e9e43f | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 75 | static void l2cap_send_disconn_req(struct l2cap_conn *conn, |
| 76 | struct l2cap_chan *chan, int err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 78 | static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb); |
| 79 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 80 | /* ---- L2CAP channels ---- */ |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 81 | static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 82 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 83 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 84 | |
| 85 | list_for_each_entry(c, &conn->chan_l, list) { |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 86 | if (c->dcid == cid) |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 87 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 88 | } |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 89 | return NULL; |
| 90 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 93 | static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 94 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 95 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 96 | |
| 97 | list_for_each_entry(c, &conn->chan_l, list) { |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 98 | if (c->scid == cid) |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 99 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 100 | } |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 101 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* Find channel with given SCID. |
| 105 | * Returns locked socket */ |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 106 | static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 107 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 108 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 109 | |
| 110 | read_lock(&conn->chan_lock); |
| 111 | c = __l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 112 | if (c) |
| 113 | bh_lock_sock(c->sk); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 114 | read_unlock(&conn->chan_lock); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 115 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 118 | static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 119 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 120 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 121 | |
| 122 | list_for_each_entry(c, &conn->chan_l, list) { |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 123 | if (c->ident == ident) |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 124 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 125 | } |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 126 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 129 | static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 130 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 131 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 132 | |
| 133 | read_lock(&conn->chan_lock); |
| 134 | c = __l2cap_get_chan_by_ident(conn, ident); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 135 | if (c) |
| 136 | bh_lock_sock(c->sk); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 137 | read_unlock(&conn->chan_lock); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 138 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 141 | static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src) |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 142 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 143 | struct l2cap_chan *c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 144 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 145 | list_for_each_entry(c, &chan_list, global_l) { |
| 146 | if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src)) |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 147 | goto found; |
| 148 | } |
| 149 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 150 | c = NULL; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 151 | found: |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 152 | return c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) |
| 156 | { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 157 | int err; |
| 158 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 159 | write_lock_bh(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 160 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 161 | if (psm && __l2cap_global_chan_by_addr(psm, src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 162 | err = -EADDRINUSE; |
| 163 | goto done; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 164 | } |
| 165 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 166 | if (psm) { |
| 167 | chan->psm = psm; |
| 168 | chan->sport = psm; |
| 169 | err = 0; |
| 170 | } else { |
| 171 | u16 p; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 172 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 173 | err = -EINVAL; |
| 174 | for (p = 0x1001; p < 0x1100; p += 2) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 175 | if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 176 | chan->psm = cpu_to_le16(p); |
| 177 | chan->sport = cpu_to_le16(p); |
| 178 | err = 0; |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | done: |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 184 | write_unlock_bh(&chan_list_lock); |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 185 | return err; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) |
| 189 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 190 | write_lock_bh(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 191 | |
| 192 | chan->scid = scid; |
| 193 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 194 | write_unlock_bh(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 199 | static u16 l2cap_alloc_cid(struct l2cap_conn *conn) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 200 | { |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 201 | u16 cid = L2CAP_CID_DYN_START; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 202 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 203 | for (; cid < L2CAP_CID_DYN_END; cid++) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 204 | if (!__l2cap_get_chan_by_scid(conn, cid)) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 205 | return cid; |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 211 | struct l2cap_chan *l2cap_chan_create(struct sock *sk) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 212 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 213 | struct l2cap_chan *chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 214 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 215 | chan = kzalloc(sizeof(*chan), GFP_ATOMIC); |
| 216 | if (!chan) |
| 217 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 218 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 219 | chan->sk = sk; |
| 220 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 221 | write_lock_bh(&chan_list_lock); |
| 222 | list_add(&chan->global_l, &chan_list); |
| 223 | write_unlock_bh(&chan_list_lock); |
| 224 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 225 | return chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 228 | void l2cap_chan_destroy(struct l2cap_chan *chan) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 229 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 230 | write_lock_bh(&chan_list_lock); |
| 231 | list_del(&chan->global_l); |
| 232 | write_unlock_bh(&chan_list_lock); |
| 233 | |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 234 | kfree(chan); |
| 235 | } |
| 236 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 237 | static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 238 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 239 | struct sock *sk = chan->sk; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 240 | |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 241 | BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 242 | chan->psm, chan->dcid); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 243 | |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 244 | conn->disc_reason = 0x13; |
| 245 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 246 | chan->conn = conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 247 | |
Gustavo F. Padovan | bd3c9e2 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 248 | if (sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM) { |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 249 | if (conn->hcon->type == LE_LINK) { |
| 250 | /* LE connection */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 251 | chan->omtu = L2CAP_LE_DEFAULT_MTU; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 252 | chan->scid = L2CAP_CID_LE_DATA; |
| 253 | chan->dcid = L2CAP_CID_LE_DATA; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 254 | } else { |
| 255 | /* Alloc CID for connection-oriented socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 256 | chan->scid = l2cap_alloc_cid(conn); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 257 | chan->omtu = L2CAP_DEFAULT_MTU; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 258 | } |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 259 | } else if (sk->sk_type == SOCK_DGRAM) { |
| 260 | /* Connectionless socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 261 | chan->scid = L2CAP_CID_CONN_LESS; |
| 262 | chan->dcid = L2CAP_CID_CONN_LESS; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 263 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 264 | } else { |
| 265 | /* Raw socket can send/recv signalling messages only */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 266 | chan->scid = L2CAP_CID_SIGNALING; |
| 267 | chan->dcid = L2CAP_CID_SIGNALING; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 268 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 271 | sock_hold(sk); |
| 272 | |
| 273 | list_add(&chan->list, &conn->chan_l); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 274 | } |
| 275 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 276 | /* Delete channel. |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 277 | * Must be called on the locked socket. */ |
Gustavo F. Padovan | 6e9e43f | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 278 | static void l2cap_chan_del(struct l2cap_chan *chan, int err) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 279 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 280 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 281 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 282 | struct sock *parent = bt_sk(sk)->parent; |
| 283 | |
| 284 | l2cap_sock_clear_timer(sk); |
| 285 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 286 | BT_DBG("chan %p, conn %p, err %d", chan, conn, err); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 287 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 288 | if (conn) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 289 | /* Delete from channel list */ |
| 290 | write_lock_bh(&conn->chan_lock); |
| 291 | list_del(&chan->list); |
| 292 | write_unlock_bh(&conn->chan_lock); |
| 293 | __sock_put(sk); |
| 294 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 295 | chan->conn = NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 296 | hci_conn_put(conn->hcon); |
| 297 | } |
| 298 | |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 299 | sk->sk_state = BT_CLOSED; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 300 | sock_set_flag(sk, SOCK_ZAPPED); |
| 301 | |
| 302 | if (err) |
| 303 | sk->sk_err = err; |
| 304 | |
| 305 | if (parent) { |
| 306 | bt_accept_unlink(sk); |
| 307 | parent->sk_data_ready(parent, 0); |
| 308 | } else |
| 309 | sk->sk_state_change(sk); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 310 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 311 | if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE && |
| 312 | chan->conf_state & L2CAP_CONF_INPUT_DONE)) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 313 | return; |
Gustavo F. Padovan | 2ead70b | 2011-04-01 15:13:36 -0300 | [diff] [blame] | 314 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 315 | skb_queue_purge(&chan->tx_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 316 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 317 | if (chan->mode == L2CAP_MODE_ERTM) { |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 318 | struct srej_list *l, *tmp; |
| 319 | |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 320 | del_timer(&chan->retrans_timer); |
| 321 | del_timer(&chan->monitor_timer); |
| 322 | del_timer(&chan->ack_timer); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 323 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 324 | skb_queue_purge(&chan->srej_q); |
| 325 | skb_queue_purge(&chan->busy_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 326 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 327 | list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 328 | list_del(&l->list); |
| 329 | kfree(l); |
| 330 | } |
| 331 | } |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Gustavo F. Padovan | 6e9e43f | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 334 | /* Must be called on unlocked socket. */ |
| 335 | static void l2cap_chan_close(struct sock *sk) |
| 336 | { |
| 337 | l2cap_sock_clear_timer(sk); |
| 338 | lock_sock(sk); |
| 339 | __l2cap_chan_close(l2cap_pi(sk)->chan, ECONNRESET); |
| 340 | release_sock(sk); |
| 341 | l2cap_sock_kill(sk); |
| 342 | } |
| 343 | |
| 344 | static void l2cap_chan_cleanup_listen(struct sock *parent) |
| 345 | { |
| 346 | struct sock *sk; |
| 347 | |
| 348 | BT_DBG("parent %p", parent); |
| 349 | |
| 350 | /* Close not yet accepted channels */ |
| 351 | while ((sk = bt_accept_dequeue(parent, NULL))) |
| 352 | l2cap_chan_close(sk); |
| 353 | |
| 354 | parent->sk_state = BT_CLOSED; |
| 355 | sock_set_flag(parent, SOCK_ZAPPED); |
| 356 | } |
| 357 | |
| 358 | void __l2cap_chan_close(struct l2cap_chan *chan, int reason) |
| 359 | { |
| 360 | struct l2cap_conn *conn = chan->conn; |
| 361 | struct sock *sk = chan->sk; |
| 362 | |
| 363 | BT_DBG("chan %p state %d socket %p", chan, sk->sk_state, sk->sk_socket); |
| 364 | |
| 365 | switch (sk->sk_state) { |
| 366 | case BT_LISTEN: |
| 367 | l2cap_chan_cleanup_listen(sk); |
| 368 | break; |
| 369 | |
| 370 | case BT_CONNECTED: |
| 371 | case BT_CONFIG: |
| 372 | if ((sk->sk_type == SOCK_SEQPACKET || |
| 373 | sk->sk_type == SOCK_STREAM) && |
| 374 | conn->hcon->type == ACL_LINK) { |
| 375 | l2cap_sock_set_timer(sk, sk->sk_sndtimeo); |
| 376 | l2cap_send_disconn_req(conn, chan, reason); |
| 377 | } else |
| 378 | l2cap_chan_del(chan, reason); |
| 379 | break; |
| 380 | |
| 381 | case BT_CONNECT2: |
| 382 | if ((sk->sk_type == SOCK_SEQPACKET || |
| 383 | sk->sk_type == SOCK_STREAM) && |
| 384 | conn->hcon->type == ACL_LINK) { |
| 385 | struct l2cap_conn_rsp rsp; |
| 386 | __u16 result; |
| 387 | |
| 388 | if (bt_sk(sk)->defer_setup) |
| 389 | result = L2CAP_CR_SEC_BLOCK; |
| 390 | else |
| 391 | result = L2CAP_CR_BAD_PSM; |
| 392 | |
| 393 | rsp.scid = cpu_to_le16(chan->dcid); |
| 394 | rsp.dcid = cpu_to_le16(chan->scid); |
| 395 | rsp.result = cpu_to_le16(result); |
| 396 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 397 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
| 398 | sizeof(rsp), &rsp); |
| 399 | } |
| 400 | |
| 401 | l2cap_chan_del(chan, reason); |
| 402 | break; |
| 403 | |
| 404 | case BT_CONNECT: |
| 405 | case BT_DISCONN: |
| 406 | l2cap_chan_del(chan, reason); |
| 407 | break; |
| 408 | |
| 409 | default: |
| 410 | sock_set_flag(sk, SOCK_ZAPPED); |
| 411 | break; |
| 412 | } |
| 413 | } |
| 414 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 415 | static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 416 | { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 417 | struct sock *sk = chan->sk; |
| 418 | |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 419 | if (sk->sk_type == SOCK_RAW) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 420 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 421 | case BT_SECURITY_HIGH: |
| 422 | return HCI_AT_DEDICATED_BONDING_MITM; |
| 423 | case BT_SECURITY_MEDIUM: |
| 424 | return HCI_AT_DEDICATED_BONDING; |
| 425 | default: |
| 426 | return HCI_AT_NO_BONDING; |
| 427 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 428 | } else if (chan->psm == cpu_to_le16(0x0001)) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 429 | if (chan->sec_level == BT_SECURITY_LOW) |
| 430 | chan->sec_level = BT_SECURITY_SDP; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 431 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 432 | if (chan->sec_level == BT_SECURITY_HIGH) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 433 | return HCI_AT_NO_BONDING_MITM; |
| 434 | else |
| 435 | return HCI_AT_NO_BONDING; |
| 436 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 437 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 438 | case BT_SECURITY_HIGH: |
| 439 | return HCI_AT_GENERAL_BONDING_MITM; |
| 440 | case BT_SECURITY_MEDIUM: |
| 441 | return HCI_AT_GENERAL_BONDING; |
| 442 | default: |
| 443 | return HCI_AT_NO_BONDING; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 448 | /* Service level security */ |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 449 | static inline int l2cap_check_security(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 450 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 451 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 452 | __u8 auth_type; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 453 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 454 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 455 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 456 | return hci_conn_security(conn->hcon, chan->sec_level, auth_type); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 457 | } |
| 458 | |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 459 | u8 l2cap_get_ident(struct l2cap_conn *conn) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 460 | { |
| 461 | u8 id; |
| 462 | |
| 463 | /* Get next available identificator. |
| 464 | * 1 - 128 are used by kernel. |
| 465 | * 129 - 199 are reserved. |
| 466 | * 200 - 254 are used by utilities like l2ping, etc. |
| 467 | */ |
| 468 | |
| 469 | spin_lock_bh(&conn->lock); |
| 470 | |
| 471 | if (++conn->tx_ident > 128) |
| 472 | conn->tx_ident = 1; |
| 473 | |
| 474 | id = conn->tx_ident; |
| 475 | |
| 476 | spin_unlock_bh(&conn->lock); |
| 477 | |
| 478 | return id; |
| 479 | } |
| 480 | |
Gustavo F. Padovan | 6e9e43f | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 481 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 482 | { |
| 483 | struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 484 | u8 flags; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 485 | |
| 486 | BT_DBG("code 0x%2.2x", code); |
| 487 | |
| 488 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 489 | return; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 490 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 491 | if (lmp_no_flush_capable(conn->hcon->hdev)) |
| 492 | flags = ACL_START_NO_FLUSH; |
| 493 | else |
| 494 | flags = ACL_START; |
| 495 | |
| 496 | hci_send_acl(conn->hcon, skb, flags); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 497 | } |
| 498 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 499 | static inline void l2cap_send_sframe(struct l2cap_chan *chan, u16 control) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 500 | { |
| 501 | struct sk_buff *skb; |
| 502 | struct l2cap_hdr *lh; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 503 | struct l2cap_pinfo *pi = l2cap_pi(chan->sk); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 504 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 505 | struct sock *sk = (struct sock *)pi; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 506 | int count, hlen = L2CAP_HDR_SIZE + 2; |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 507 | u8 flags; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 508 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 509 | if (sk->sk_state != BT_CONNECTED) |
| 510 | return; |
| 511 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 512 | if (chan->fcs == L2CAP_FCS_CRC16) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 513 | hlen += 2; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 514 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 515 | BT_DBG("chan %p, control 0x%2.2x", chan, control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 516 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 517 | count = min_t(unsigned int, conn->mtu, hlen); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 518 | control |= L2CAP_CTRL_FRAME_TYPE; |
| 519 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 520 | if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 521 | control |= L2CAP_CTRL_FINAL; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 522 | chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 523 | } |
| 524 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 525 | if (chan->conn_state & L2CAP_CONN_SEND_PBIT) { |
Gustavo F. Padovan | f0946cc | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 526 | control |= L2CAP_CTRL_POLL; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 527 | chan->conn_state &= ~L2CAP_CONN_SEND_PBIT; |
Gustavo F. Padovan | f0946cc | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 528 | } |
| 529 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 530 | skb = bt_skb_alloc(count, GFP_ATOMIC); |
| 531 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 532 | return; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 533 | |
| 534 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 535 | lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 536 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 537 | put_unaligned_le16(control, skb_put(skb, 2)); |
| 538 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 539 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 540 | u16 fcs = crc16(0, (u8 *)lh, count - 2); |
| 541 | put_unaligned_le16(fcs, skb_put(skb, 2)); |
| 542 | } |
| 543 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 544 | if (lmp_no_flush_capable(conn->hcon->hdev)) |
| 545 | flags = ACL_START_NO_FLUSH; |
| 546 | else |
| 547 | flags = ACL_START; |
| 548 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 549 | hci_send_acl(chan->conn->hcon, skb, flags); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 550 | } |
| 551 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 552 | static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u16 control) |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 553 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 554 | if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 555 | control |= L2CAP_SUPER_RCV_NOT_READY; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 556 | chan->conn_state |= L2CAP_CONN_RNR_SENT; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 557 | } else |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 558 | control |= L2CAP_SUPER_RCV_READY; |
| 559 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 560 | control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
Gustavo F. Padovan | 2ab25cd | 2009-10-03 02:34:40 -0300 | [diff] [blame] | 561 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 562 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 563 | } |
| 564 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 565 | static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 566 | { |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 567 | return !(chan->conf_state & L2CAP_CONF_CONNECT_PEND); |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 568 | } |
| 569 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 570 | static void l2cap_do_start(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 571 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 572 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 573 | |
| 574 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 575 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) |
| 576 | return; |
| 577 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 578 | if (l2cap_check_security(chan) && |
| 579 | __l2cap_no_conn_pending(chan)) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 580 | struct l2cap_conn_req req; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 581 | req.scid = cpu_to_le16(chan->scid); |
| 582 | req.psm = chan->psm; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 583 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 584 | chan->ident = l2cap_get_ident(conn); |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 585 | chan->conf_state |= L2CAP_CONF_CONNECT_PEND; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 586 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 587 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, |
| 588 | sizeof(req), &req); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 589 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 590 | } else { |
| 591 | struct l2cap_info_req req; |
| 592 | req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 593 | |
| 594 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 595 | conn->info_ident = l2cap_get_ident(conn); |
| 596 | |
| 597 | mod_timer(&conn->info_timer, jiffies + |
| 598 | msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); |
| 599 | |
| 600 | l2cap_send_cmd(conn, conn->info_ident, |
| 601 | L2CAP_INFO_REQ, sizeof(req), &req); |
| 602 | } |
| 603 | } |
| 604 | |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 605 | static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) |
| 606 | { |
| 607 | u32 local_feat_mask = l2cap_feat_mask; |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 608 | if (!disable_ertm) |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 609 | local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; |
| 610 | |
| 611 | switch (mode) { |
| 612 | case L2CAP_MODE_ERTM: |
| 613 | return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; |
| 614 | case L2CAP_MODE_STREAMING: |
| 615 | return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; |
| 616 | default: |
| 617 | return 0x00; |
| 618 | } |
| 619 | } |
| 620 | |
Gustavo F. Padovan | 6e9e43f | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 621 | static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err) |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 622 | { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 623 | struct sock *sk; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 624 | struct l2cap_disconn_req req; |
| 625 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 626 | if (!conn) |
| 627 | return; |
| 628 | |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 629 | sk = chan->sk; |
| 630 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 631 | if (chan->mode == L2CAP_MODE_ERTM) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 632 | del_timer(&chan->retrans_timer); |
| 633 | del_timer(&chan->monitor_timer); |
| 634 | del_timer(&chan->ack_timer); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 635 | } |
| 636 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 637 | req.dcid = cpu_to_le16(chan->dcid); |
| 638 | req.scid = cpu_to_le16(chan->scid); |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 639 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 640 | L2CAP_DISCONN_REQ, sizeof(req), &req); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 641 | |
| 642 | sk->sk_state = BT_DISCONN; |
Gustavo F. Padovan | 9b108fc | 2010-05-20 16:21:53 -0300 | [diff] [blame] | 643 | sk->sk_err = err; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 644 | } |
| 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | /* ---- L2CAP connections ---- */ |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 647 | static void l2cap_conn_start(struct l2cap_conn *conn) |
| 648 | { |
Gustavo F. Padovan | 820ffdb | 2011-04-01 00:35:21 -0300 | [diff] [blame] | 649 | struct l2cap_chan *chan, *tmp; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 650 | |
| 651 | BT_DBG("conn %p", conn); |
| 652 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 653 | read_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 654 | |
Gustavo F. Padovan | 820ffdb | 2011-04-01 00:35:21 -0300 | [diff] [blame] | 655 | list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 656 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 657 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 658 | bh_lock_sock(sk); |
| 659 | |
Gustavo F. Padovan | bd3c9e2 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 660 | if (sk->sk_type != SOCK_SEQPACKET && |
| 661 | sk->sk_type != SOCK_STREAM) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 662 | bh_unlock_sock(sk); |
| 663 | continue; |
| 664 | } |
| 665 | |
| 666 | if (sk->sk_state == BT_CONNECT) { |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 667 | struct l2cap_conn_req req; |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 668 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 669 | if (!l2cap_check_security(chan) || |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 670 | !__l2cap_no_conn_pending(chan)) { |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 671 | bh_unlock_sock(sk); |
| 672 | continue; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 673 | } |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 674 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 675 | if (!l2cap_mode_supported(chan->mode, |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 676 | conn->feat_mask) |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 677 | && chan->conf_state & |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 678 | L2CAP_CONF_STATE2_DEVICE) { |
Gustavo F. Padovan | 6e9e43f | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 679 | /* __l2cap_chan_close() calls list_del(chan) |
Gustavo F. Padovan | 820ffdb | 2011-04-01 00:35:21 -0300 | [diff] [blame] | 680 | * so release the lock */ |
| 681 | read_unlock_bh(&conn->chan_lock); |
Gustavo F. Padovan | 6e9e43f | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 682 | __l2cap_chan_close(chan, ECONNRESET); |
Gustavo F. Padovan | 820ffdb | 2011-04-01 00:35:21 -0300 | [diff] [blame] | 683 | read_lock_bh(&conn->chan_lock); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 684 | bh_unlock_sock(sk); |
| 685 | continue; |
| 686 | } |
| 687 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 688 | req.scid = cpu_to_le16(chan->scid); |
| 689 | req.psm = chan->psm; |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 690 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 691 | chan->ident = l2cap_get_ident(conn); |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 692 | chan->conf_state |= L2CAP_CONF_CONNECT_PEND; |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 693 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 694 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, |
| 695 | sizeof(req), &req); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 696 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 697 | } else if (sk->sk_state == BT_CONNECT2) { |
| 698 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 699 | char buf[128]; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 700 | rsp.scid = cpu_to_le16(chan->dcid); |
| 701 | rsp.dcid = cpu_to_le16(chan->scid); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 702 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 703 | if (l2cap_check_security(chan)) { |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 704 | if (bt_sk(sk)->defer_setup) { |
| 705 | struct sock *parent = bt_sk(sk)->parent; |
| 706 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 707 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); |
Nick Pelly | f86e4b0 | 2010-04-08 16:23:32 -0700 | [diff] [blame] | 708 | if (parent) |
| 709 | parent->sk_data_ready(parent, 0); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 710 | |
| 711 | } else { |
| 712 | sk->sk_state = BT_CONFIG; |
| 713 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 714 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 715 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 716 | } else { |
| 717 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 718 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); |
| 719 | } |
| 720 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 721 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
| 722 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 723 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 724 | if (chan->conf_state & L2CAP_CONF_REQ_SENT || |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 725 | rsp.result != L2CAP_CR_SUCCESS) { |
| 726 | bh_unlock_sock(sk); |
| 727 | continue; |
| 728 | } |
| 729 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 730 | chan->conf_state |= L2CAP_CONF_REQ_SENT; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 731 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 732 | l2cap_build_conf_req(chan, buf), buf); |
| 733 | chan->num_conf_req++; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | bh_unlock_sock(sk); |
| 737 | } |
| 738 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 739 | read_unlock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 740 | } |
| 741 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 742 | /* Find socket with cid and source bdaddr. |
| 743 | * Returns closest match, locked. |
| 744 | */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 745 | static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 746 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 747 | struct l2cap_chan *c, *c1 = NULL; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 748 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 749 | read_lock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 750 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 751 | list_for_each_entry(c, &chan_list, global_l) { |
| 752 | struct sock *sk = c->sk; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 753 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 754 | if (state && sk->sk_state != state) |
| 755 | continue; |
| 756 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 757 | if (c->scid == cid) { |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 758 | /* Exact match. */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 759 | if (!bacmp(&bt_sk(sk)->src, src)) { |
| 760 | read_unlock(&chan_list_lock); |
| 761 | return c; |
| 762 | } |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 763 | |
| 764 | /* Closest match */ |
| 765 | if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 766 | c1 = c; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 767 | } |
| 768 | } |
Gustavo F. Padovan | 280f294 | 2011-04-13 19:01:22 -0300 | [diff] [blame] | 769 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 770 | read_unlock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 771 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 772 | return c1; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | static void l2cap_le_conn_ready(struct l2cap_conn *conn) |
| 776 | { |
Gustavo F. Padovan | c916fbe | 2011-04-04 16:00:55 -0300 | [diff] [blame] | 777 | struct sock *parent, *sk; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 778 | struct l2cap_chan *chan, *pchan; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 779 | |
| 780 | BT_DBG(""); |
| 781 | |
| 782 | /* Check if we have socket listening on cid */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 783 | pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 784 | conn->src); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 785 | if (!pchan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 786 | return; |
| 787 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 788 | parent = pchan->sk; |
| 789 | |
Gustavo F. Padovan | 62f3a2c | 2011-04-14 18:34:34 -0300 | [diff] [blame] | 790 | bh_lock_sock(parent); |
| 791 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 792 | /* Check for backlog size */ |
| 793 | if (sk_acceptq_is_full(parent)) { |
| 794 | BT_DBG("backlog full %d", parent->sk_ack_backlog); |
| 795 | goto clean; |
| 796 | } |
| 797 | |
| 798 | sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC); |
| 799 | if (!sk) |
| 800 | goto clean; |
| 801 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 802 | chan = l2cap_chan_create(sk); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 803 | if (!chan) { |
| 804 | l2cap_sock_kill(sk); |
| 805 | goto clean; |
| 806 | } |
| 807 | |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 808 | l2cap_pi(sk)->chan = chan; |
| 809 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 810 | write_lock_bh(&conn->chan_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 811 | |
| 812 | hci_conn_hold(conn->hcon); |
| 813 | |
| 814 | l2cap_sock_init(sk, parent); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 815 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 816 | bacpy(&bt_sk(sk)->src, conn->src); |
| 817 | bacpy(&bt_sk(sk)->dst, conn->dst); |
| 818 | |
Gustavo F. Padovan | d101024 | 2011-03-25 00:39:48 -0300 | [diff] [blame] | 819 | bt_accept_enqueue(parent, sk); |
| 820 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 821 | __l2cap_chan_add(conn, chan); |
| 822 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 823 | l2cap_sock_set_timer(sk, sk->sk_sndtimeo); |
| 824 | |
| 825 | sk->sk_state = BT_CONNECTED; |
| 826 | parent->sk_data_ready(parent, 0); |
| 827 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 828 | write_unlock_bh(&conn->chan_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 829 | |
| 830 | clean: |
| 831 | bh_unlock_sock(parent); |
| 832 | } |
| 833 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 834 | static void l2cap_conn_ready(struct l2cap_conn *conn) |
| 835 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 836 | struct l2cap_chan *chan; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 837 | |
| 838 | BT_DBG("conn %p", conn); |
| 839 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 840 | if (!conn->hcon->out && conn->hcon->type == LE_LINK) |
| 841 | l2cap_le_conn_ready(conn); |
| 842 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 843 | read_lock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 844 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 845 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 846 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 847 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 848 | bh_lock_sock(sk); |
| 849 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 850 | if (conn->hcon->type == LE_LINK) { |
| 851 | l2cap_sock_clear_timer(sk); |
| 852 | sk->sk_state = BT_CONNECTED; |
| 853 | sk->sk_state_change(sk); |
| 854 | } |
| 855 | |
Gustavo F. Padovan | bd3c9e2 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 856 | if (sk->sk_type != SOCK_SEQPACKET && |
| 857 | sk->sk_type != SOCK_STREAM) { |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 858 | l2cap_sock_clear_timer(sk); |
| 859 | sk->sk_state = BT_CONNECTED; |
| 860 | sk->sk_state_change(sk); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 861 | } else if (sk->sk_state == BT_CONNECT) |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 862 | l2cap_do_start(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 863 | |
| 864 | bh_unlock_sock(sk); |
| 865 | } |
| 866 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 867 | read_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 868 | } |
| 869 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 870 | /* Notify sockets that we cannot guaranty reliability anymore */ |
| 871 | static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) |
| 872 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 873 | struct l2cap_chan *chan; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 874 | |
| 875 | BT_DBG("conn %p", conn); |
| 876 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 877 | read_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 878 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 879 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 880 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 881 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 882 | if (chan->force_reliable) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 883 | sk->sk_err = err; |
| 884 | } |
| 885 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 886 | read_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | static void l2cap_info_timeout(unsigned long arg) |
| 890 | { |
| 891 | struct l2cap_conn *conn = (void *) arg; |
| 892 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 893 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 894 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 895 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 896 | l2cap_conn_start(conn); |
| 897 | } |
| 898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) |
| 900 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 901 | struct l2cap_conn *conn = hcon->l2cap_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 903 | if (conn || status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | return conn; |
| 905 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 906 | conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); |
| 907 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | |
| 910 | hcon->l2cap_data = conn; |
| 911 | conn->hcon = hcon; |
| 912 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 913 | BT_DBG("hcon %p conn %p", hcon, conn); |
| 914 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 915 | if (hcon->hdev->le_mtu && hcon->type == LE_LINK) |
| 916 | conn->mtu = hcon->hdev->le_mtu; |
| 917 | else |
| 918 | conn->mtu = hcon->hdev->acl_mtu; |
| 919 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | conn->src = &hcon->hdev->bdaddr; |
| 921 | conn->dst = &hcon->dst; |
| 922 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 923 | conn->feat_mask = 0; |
| 924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | spin_lock_init(&conn->lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 926 | rwlock_init(&conn->chan_lock); |
| 927 | |
| 928 | INIT_LIST_HEAD(&conn->chan_l); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 930 | if (hcon->type != LE_LINK) |
| 931 | setup_timer(&conn->info_timer, l2cap_info_timeout, |
Dave Young | 45054dc | 2009-10-18 20:28:30 +0000 | [diff] [blame] | 932 | (unsigned long) conn); |
| 933 | |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 934 | conn->disc_reason = 0x13; |
| 935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | return conn; |
| 937 | } |
| 938 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 939 | static void l2cap_conn_del(struct hci_conn *hcon, int err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 941 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 942 | struct l2cap_chan *chan, *l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | struct sock *sk; |
| 944 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 945 | if (!conn) |
| 946 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
| 948 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 949 | |
Wei Yongjun | 7585b97 | 2009-02-25 18:29:52 +0800 | [diff] [blame] | 950 | kfree_skb(conn->rx_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
| 952 | /* Kill channels */ |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 953 | list_for_each_entry_safe(chan, l, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 954 | sk = chan->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | bh_lock_sock(sk); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 956 | l2cap_chan_del(chan, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | bh_unlock_sock(sk); |
| 958 | l2cap_sock_kill(sk); |
| 959 | } |
| 960 | |
Dave Young | 8e8440f | 2008-03-03 12:18:55 -0800 | [diff] [blame] | 961 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
| 962 | del_timer_sync(&conn->info_timer); |
Thomas Gleixner | 3ab2273 | 2008-02-26 17:42:56 -0800 | [diff] [blame] | 963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | hcon->l2cap_data = NULL; |
| 965 | kfree(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | } |
| 967 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 968 | static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 970 | write_lock_bh(&conn->chan_lock); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 971 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 972 | write_unlock_bh(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | } |
| 974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | /* ---- Socket interface ---- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | |
| 977 | /* Find socket with psm and source bdaddr. |
| 978 | * Returns closest match. |
| 979 | */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 980 | static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 982 | struct l2cap_chan *c, *c1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 984 | read_lock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 985 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 986 | list_for_each_entry(c, &chan_list, global_l) { |
| 987 | struct sock *sk = c->sk; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | if (state && sk->sk_state != state) |
| 990 | continue; |
| 991 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 992 | if (c->psm == psm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | /* Exact match. */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 994 | if (!bacmp(&bt_sk(sk)->src, src)) { |
Johannes Berg | a7567b2 | 2011-06-01 08:29:54 +0200 | [diff] [blame] | 995 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 996 | return c; |
| 997 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
| 999 | /* Closest match */ |
| 1000 | if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1001 | c1 = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | } |
| 1003 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1005 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1006 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1007 | return c1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Gustavo F. Padovan | 77a74c7 | 2011-04-12 18:17:14 -0300 | [diff] [blame] | 1010 | int l2cap_chan_connect(struct l2cap_chan *chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | { |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 1012 | struct sock *sk = chan->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | bdaddr_t *src = &bt_sk(sk)->src; |
| 1014 | bdaddr_t *dst = &bt_sk(sk)->dst; |
| 1015 | struct l2cap_conn *conn; |
| 1016 | struct hci_conn *hcon; |
| 1017 | struct hci_dev *hdev; |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1018 | __u8 auth_type; |
Marcel Holtmann | 44d0e48 | 2009-04-20 07:09:16 +0200 | [diff] [blame] | 1019 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
Marcel Holtmann | f29972d | 2009-02-12 05:07:45 +0100 | [diff] [blame] | 1021 | BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1022 | chan->psm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1024 | hdev = hci_get_route(dst, src); |
| 1025 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | return -EHOSTUNREACH; |
| 1027 | |
| 1028 | hci_dev_lock_bh(hdev); |
| 1029 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1030 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1031 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1032 | if (chan->dcid == L2CAP_CID_LE_DATA) |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 1033 | hcon = hci_connect(hdev, LE_LINK, 0, dst, |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1034 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1035 | else |
Nick Pelly | bbcda3b | 2010-02-11 11:54:28 -0800 | [diff] [blame] | 1036 | hcon = hci_connect(hdev, ACL_LINK, 0, dst, |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1037 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1038 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1039 | if (IS_ERR(hcon)) { |
| 1040 | err = PTR_ERR(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | goto done; |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1042 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | |
| 1044 | conn = l2cap_conn_add(hcon, 0); |
| 1045 | if (!conn) { |
| 1046 | hci_conn_put(hcon); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1047 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | goto done; |
| 1049 | } |
| 1050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | /* Update source addr of the socket */ |
| 1052 | bacpy(src, conn->src); |
| 1053 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1054 | l2cap_chan_add(conn, chan); |
| 1055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | sk->sk_state = BT_CONNECT; |
| 1057 | l2cap_sock_set_timer(sk, sk->sk_sndtimeo); |
| 1058 | |
| 1059 | if (hcon->state == BT_CONNECTED) { |
Gustavo F. Padovan | bd3c9e2 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 1060 | if (sk->sk_type != SOCK_SEQPACKET && |
| 1061 | sk->sk_type != SOCK_STREAM) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | l2cap_sock_clear_timer(sk); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1063 | if (l2cap_check_security(chan)) |
Johan Hedberg | d00ef24 | 2011-01-19 12:06:51 +0530 | [diff] [blame] | 1064 | sk->sk_state = BT_CONNECTED; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1065 | } else |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1066 | l2cap_do_start(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | } |
| 1068 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1069 | err = 0; |
| 1070 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | done: |
| 1072 | hci_dev_unlock_bh(hdev); |
| 1073 | hci_dev_put(hdev); |
| 1074 | return err; |
| 1075 | } |
| 1076 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1077 | int __l2cap_wait_ack(struct sock *sk) |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1078 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1079 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1080 | DECLARE_WAITQUEUE(wait, current); |
| 1081 | int err = 0; |
| 1082 | int timeo = HZ/5; |
| 1083 | |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1084 | add_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1085 | while ((chan->unacked_frames > 0 && chan->conn)) { |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1086 | set_current_state(TASK_INTERRUPTIBLE); |
| 1087 | |
| 1088 | if (!timeo) |
| 1089 | timeo = HZ/5; |
| 1090 | |
| 1091 | if (signal_pending(current)) { |
| 1092 | err = sock_intr_errno(timeo); |
| 1093 | break; |
| 1094 | } |
| 1095 | |
| 1096 | release_sock(sk); |
| 1097 | timeo = schedule_timeout(timeo); |
| 1098 | lock_sock(sk); |
| 1099 | |
| 1100 | err = sock_error(sk); |
| 1101 | if (err) |
| 1102 | break; |
| 1103 | } |
| 1104 | set_current_state(TASK_RUNNING); |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1105 | remove_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1106 | return err; |
| 1107 | } |
| 1108 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1109 | static void l2cap_monitor_timeout(unsigned long arg) |
| 1110 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1111 | struct l2cap_chan *chan = (void *) arg; |
| 1112 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1113 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1114 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1115 | |
Gustavo F. Padovan | e686219 | 2009-08-24 00:45:19 -0300 | [diff] [blame] | 1116 | bh_lock_sock(sk); |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1117 | if (chan->retry_count >= chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1118 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Andrei Emeltchenko | b13f586 | 2009-12-15 11:38:04 +0200 | [diff] [blame] | 1119 | bh_unlock_sock(sk); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1120 | return; |
| 1121 | } |
| 1122 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1123 | chan->retry_count++; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1124 | __mod_monitor_timer(); |
| 1125 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1126 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); |
Gustavo F. Padovan | e686219 | 2009-08-24 00:45:19 -0300 | [diff] [blame] | 1127 | bh_unlock_sock(sk); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | static void l2cap_retrans_timeout(unsigned long arg) |
| 1131 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1132 | struct l2cap_chan *chan = (void *) arg; |
| 1133 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1134 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1135 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1136 | |
Gustavo F. Padovan | e686219 | 2009-08-24 00:45:19 -0300 | [diff] [blame] | 1137 | bh_lock_sock(sk); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1138 | chan->retry_count = 1; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1139 | __mod_monitor_timer(); |
| 1140 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1141 | chan->conn_state |= L2CAP_CONN_WAIT_F; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1142 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1143 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); |
Gustavo F. Padovan | e686219 | 2009-08-24 00:45:19 -0300 | [diff] [blame] | 1144 | bh_unlock_sock(sk); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1145 | } |
| 1146 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1147 | static void l2cap_drop_acked_frames(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1148 | { |
| 1149 | struct sk_buff *skb; |
| 1150 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1151 | while ((skb = skb_peek(&chan->tx_q)) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1152 | chan->unacked_frames) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1153 | if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1154 | break; |
| 1155 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1156 | skb = skb_dequeue(&chan->tx_q); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1157 | kfree_skb(skb); |
| 1158 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1159 | chan->unacked_frames--; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1160 | } |
| 1161 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1162 | if (!chan->unacked_frames) |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 1163 | del_timer(&chan->retrans_timer); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1164 | } |
| 1165 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1166 | void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1167 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1168 | struct hci_conn *hcon = chan->conn->hcon; |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 1169 | u16 flags; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1170 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1171 | BT_DBG("chan %p, skb %p len %d", chan, skb, skb->len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1172 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1173 | if (!chan->flushable && lmp_no_flush_capable(hcon->hdev)) |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 1174 | flags = ACL_START_NO_FLUSH; |
| 1175 | else |
| 1176 | flags = ACL_START; |
| 1177 | |
| 1178 | hci_send_acl(hcon, skb, flags); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1179 | } |
| 1180 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1181 | void l2cap_streaming_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1182 | { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1183 | struct sk_buff *skb; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1184 | u16 control, fcs; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1185 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1186 | while ((skb = skb_dequeue(&chan->tx_q))) { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1187 | control = get_unaligned_le16(skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1188 | control |= chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT; |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1189 | put_unaligned_le16(control, skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1190 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1191 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1192 | fcs = crc16(0, (u8 *)skb->data, skb->len - 2); |
| 1193 | put_unaligned_le16(fcs, skb->data + skb->len - 2); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1194 | } |
| 1195 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1196 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1197 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1198 | chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1199 | } |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1200 | } |
| 1201 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1202 | static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1203 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1204 | struct sk_buff *skb, *tx_skb; |
| 1205 | u16 control, fcs; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1206 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1207 | skb = skb_peek(&chan->tx_q); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1208 | if (!skb) |
| 1209 | return; |
| 1210 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1211 | do { |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1212 | if (bt_cb(skb)->tx_seq == tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1213 | break; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1214 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1215 | if (skb_queue_is_last(&chan->tx_q, skb)) |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1216 | return; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1217 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1218 | } while ((skb = skb_queue_next(&chan->tx_q, skb))); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1219 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1220 | if (chan->remote_max_tx && |
| 1221 | bt_cb(skb)->retries == chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1222 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1223 | return; |
| 1224 | } |
| 1225 | |
| 1226 | tx_skb = skb_clone(skb, GFP_ATOMIC); |
| 1227 | bt_cb(skb)->retries++; |
| 1228 | control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); |
Ruiyi Zhang | a429b51 | 2011-04-18 11:04:30 +0800 | [diff] [blame] | 1229 | control &= L2CAP_CTRL_SAR; |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 1230 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1231 | if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 1232 | control |= L2CAP_CTRL_FINAL; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1233 | chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 1234 | } |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1235 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1236 | control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT) |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1237 | | (tx_seq << L2CAP_CTRL_TXSEQ_SHIFT); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 1238 | |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1239 | put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); |
| 1240 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1241 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1242 | fcs = crc16(0, (u8 *)tx_skb->data, tx_skb->len - 2); |
| 1243 | put_unaligned_le16(fcs, tx_skb->data + tx_skb->len - 2); |
| 1244 | } |
| 1245 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1246 | l2cap_do_send(chan, tx_skb); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1247 | } |
| 1248 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1249 | int l2cap_ertm_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1250 | { |
| 1251 | struct sk_buff *skb, *tx_skb; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1252 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1253 | u16 control, fcs; |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1254 | int nsent = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1255 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1256 | if (sk->sk_state != BT_CONNECTED) |
| 1257 | return -ENOTCONN; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1258 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1259 | while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1260 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1261 | if (chan->remote_max_tx && |
| 1262 | bt_cb(skb)->retries == chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1263 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1264 | break; |
| 1265 | } |
| 1266 | |
Andrei Emeltchenko | e420aba | 2009-12-23 13:07:14 +0200 | [diff] [blame] | 1267 | tx_skb = skb_clone(skb, GFP_ATOMIC); |
| 1268 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1269 | bt_cb(skb)->retries++; |
| 1270 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1271 | control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1272 | control &= L2CAP_CTRL_SAR; |
| 1273 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1274 | if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 1275 | control |= L2CAP_CTRL_FINAL; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1276 | chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 1277 | } |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1278 | control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT) |
| 1279 | | (chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1280 | put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); |
| 1281 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1282 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1283 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1284 | fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2); |
| 1285 | put_unaligned_le16(fcs, skb->data + tx_skb->len - 2); |
| 1286 | } |
| 1287 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1288 | l2cap_do_send(chan, tx_skb); |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1289 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1290 | __mod_retrans_timer(); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1291 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1292 | bt_cb(skb)->tx_seq = chan->next_tx_seq; |
| 1293 | chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1294 | |
Suraj Sumangala | 23e9fde | 2011-03-09 14:44:05 +0530 | [diff] [blame] | 1295 | if (bt_cb(skb)->retries == 1) |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1296 | chan->unacked_frames++; |
Suraj Sumangala | 23e9fde | 2011-03-09 14:44:05 +0530 | [diff] [blame] | 1297 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1298 | chan->frames_sent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1299 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1300 | if (skb_queue_is_last(&chan->tx_q, skb)) |
| 1301 | chan->tx_send_head = NULL; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1302 | else |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1303 | chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1304 | |
| 1305 | nsent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1306 | } |
| 1307 | |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1308 | return nsent; |
| 1309 | } |
| 1310 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1311 | static int l2cap_retransmit_frames(struct l2cap_chan *chan) |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1312 | { |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1313 | int ret; |
| 1314 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1315 | if (!skb_queue_empty(&chan->tx_q)) |
| 1316 | chan->tx_send_head = chan->tx_q.next; |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1317 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1318 | chan->next_tx_seq = chan->expected_ack_seq; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1319 | ret = l2cap_ertm_send(chan); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1320 | return ret; |
| 1321 | } |
| 1322 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1323 | static void l2cap_send_ack(struct l2cap_chan *chan) |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1324 | { |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1325 | u16 control = 0; |
| 1326 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1327 | control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1328 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1329 | if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1330 | control |= L2CAP_SUPER_RCV_NOT_READY; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1331 | chan->conn_state |= L2CAP_CONN_RNR_SENT; |
| 1332 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1333 | return; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1334 | } |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1335 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1336 | if (l2cap_ertm_send(chan) > 0) |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1337 | return; |
| 1338 | |
| 1339 | control |= L2CAP_SUPER_RCV_READY; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1340 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1341 | } |
| 1342 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1343 | static void l2cap_send_srejtail(struct l2cap_chan *chan) |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1344 | { |
| 1345 | struct srej_list *tail; |
| 1346 | u16 control; |
| 1347 | |
| 1348 | control = L2CAP_SUPER_SELECT_REJECT; |
| 1349 | control |= L2CAP_CTRL_FINAL; |
| 1350 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 1351 | tail = list_entry((&chan->srej_l)->prev, struct srej_list, list); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1352 | control |= tail->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
| 1353 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1354 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1355 | } |
| 1356 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1357 | static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1359 | struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1360 | struct sk_buff **frag; |
| 1361 | int err, sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
Gustavo F. Padovan | 59203a2 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1363 | if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1364 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
| 1366 | sent += count; |
| 1367 | len -= count; |
| 1368 | |
| 1369 | /* Continuation fragments (no L2CAP header) */ |
| 1370 | frag = &skb_shinfo(skb)->frag_list; |
| 1371 | while (len) { |
| 1372 | count = min_t(unsigned int, conn->mtu, len); |
| 1373 | |
| 1374 | *frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err); |
| 1375 | if (!*frag) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1376 | return err; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1377 | if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) |
| 1378 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | |
| 1380 | sent += count; |
| 1381 | len -= count; |
| 1382 | |
| 1383 | frag = &(*frag)->next; |
| 1384 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | |
| 1386 | return sent; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1389 | struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1390 | { |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1391 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1392 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1393 | struct sk_buff *skb; |
| 1394 | int err, count, hlen = L2CAP_HDR_SIZE + 2; |
| 1395 | struct l2cap_hdr *lh; |
| 1396 | |
| 1397 | BT_DBG("sk %p len %d", sk, (int)len); |
| 1398 | |
| 1399 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
| 1400 | skb = bt_skb_send_alloc(sk, count + hlen, |
| 1401 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1402 | if (!skb) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1403 | return ERR_PTR(err); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1404 | |
| 1405 | /* Create L2CAP header */ |
| 1406 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1407 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1408 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1409 | put_unaligned_le16(chan->psm, skb_put(skb, 2)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1410 | |
| 1411 | err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); |
| 1412 | if (unlikely(err < 0)) { |
| 1413 | kfree_skb(skb); |
| 1414 | return ERR_PTR(err); |
| 1415 | } |
| 1416 | return skb; |
| 1417 | } |
| 1418 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1419 | struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1420 | { |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1421 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1422 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1423 | struct sk_buff *skb; |
| 1424 | int err, count, hlen = L2CAP_HDR_SIZE; |
| 1425 | struct l2cap_hdr *lh; |
| 1426 | |
| 1427 | BT_DBG("sk %p len %d", sk, (int)len); |
| 1428 | |
| 1429 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
| 1430 | skb = bt_skb_send_alloc(sk, count + hlen, |
| 1431 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1432 | if (!skb) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1433 | return ERR_PTR(err); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1434 | |
| 1435 | /* Create L2CAP header */ |
| 1436 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1437 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1438 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
| 1439 | |
| 1440 | err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); |
| 1441 | if (unlikely(err < 0)) { |
| 1442 | kfree_skb(skb); |
| 1443 | return ERR_PTR(err); |
| 1444 | } |
| 1445 | return skb; |
| 1446 | } |
| 1447 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1448 | struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len, u16 control, u16 sdulen) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1449 | { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1450 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1451 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1452 | struct sk_buff *skb; |
| 1453 | int err, count, hlen = L2CAP_HDR_SIZE + 2; |
| 1454 | struct l2cap_hdr *lh; |
| 1455 | |
| 1456 | BT_DBG("sk %p len %d", sk, (int)len); |
| 1457 | |
Gustavo F. Padovan | 0ee0d20 | 2010-05-01 16:15:41 -0300 | [diff] [blame] | 1458 | if (!conn) |
| 1459 | return ERR_PTR(-ENOTCONN); |
| 1460 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1461 | if (sdulen) |
| 1462 | hlen += 2; |
| 1463 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1464 | if (chan->fcs == L2CAP_FCS_CRC16) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1465 | hlen += 2; |
| 1466 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1467 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
| 1468 | skb = bt_skb_send_alloc(sk, count + hlen, |
| 1469 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1470 | if (!skb) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1471 | return ERR_PTR(err); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1472 | |
| 1473 | /* Create L2CAP header */ |
| 1474 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1475 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1476 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
| 1477 | put_unaligned_le16(control, skb_put(skb, 2)); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1478 | if (sdulen) |
| 1479 | put_unaligned_le16(sdulen, skb_put(skb, 2)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1480 | |
| 1481 | err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); |
| 1482 | if (unlikely(err < 0)) { |
| 1483 | kfree_skb(skb); |
| 1484 | return ERR_PTR(err); |
| 1485 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1486 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1487 | if (chan->fcs == L2CAP_FCS_CRC16) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1488 | put_unaligned_le16(0, skb_put(skb, 2)); |
| 1489 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1490 | bt_cb(skb)->retries = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1491 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1494 | int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1495 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1496 | struct sk_buff *skb; |
| 1497 | struct sk_buff_head sar_queue; |
| 1498 | u16 control; |
| 1499 | size_t size = 0; |
| 1500 | |
Gustavo F. Padovan | ff12fd6 | 2010-05-05 22:09:15 -0300 | [diff] [blame] | 1501 | skb_queue_head_init(&sar_queue); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1502 | control = L2CAP_SDU_START; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1503 | skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1504 | if (IS_ERR(skb)) |
| 1505 | return PTR_ERR(skb); |
| 1506 | |
| 1507 | __skb_queue_tail(&sar_queue, skb); |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1508 | len -= chan->remote_mps; |
| 1509 | size += chan->remote_mps; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1510 | |
| 1511 | while (len > 0) { |
| 1512 | size_t buflen; |
| 1513 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1514 | if (len > chan->remote_mps) { |
Gustavo F. Padovan | 44651b8 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1515 | control = L2CAP_SDU_CONTINUE; |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1516 | buflen = chan->remote_mps; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1517 | } else { |
Gustavo F. Padovan | 44651b8 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1518 | control = L2CAP_SDU_END; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1519 | buflen = len; |
| 1520 | } |
| 1521 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1522 | skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1523 | if (IS_ERR(skb)) { |
| 1524 | skb_queue_purge(&sar_queue); |
| 1525 | return PTR_ERR(skb); |
| 1526 | } |
| 1527 | |
| 1528 | __skb_queue_tail(&sar_queue, skb); |
| 1529 | len -= buflen; |
| 1530 | size += buflen; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1531 | } |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1532 | skb_queue_splice_tail(&sar_queue, &chan->tx_q); |
| 1533 | if (chan->tx_send_head == NULL) |
| 1534 | chan->tx_send_head = sar_queue.next; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1535 | |
| 1536 | return size; |
| 1537 | } |
| 1538 | |
Gustavo F. Padovan | dcb1cc3 | 2011-04-28 18:50:17 -0300 | [diff] [blame^] | 1539 | int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len) |
| 1540 | { |
| 1541 | struct sock *sk = chan->sk; |
| 1542 | struct sk_buff *skb; |
| 1543 | u16 control; |
| 1544 | int err; |
| 1545 | |
| 1546 | /* Connectionless channel */ |
| 1547 | if (sk->sk_type == SOCK_DGRAM) { |
| 1548 | skb = l2cap_create_connless_pdu(chan, msg, len); |
| 1549 | if (IS_ERR(skb)) |
| 1550 | return PTR_ERR(skb); |
| 1551 | |
| 1552 | l2cap_do_send(chan, skb); |
| 1553 | return len; |
| 1554 | } |
| 1555 | |
| 1556 | switch (chan->mode) { |
| 1557 | case L2CAP_MODE_BASIC: |
| 1558 | /* Check outgoing MTU */ |
| 1559 | if (len > chan->omtu) |
| 1560 | return -EMSGSIZE; |
| 1561 | |
| 1562 | /* Create a basic PDU */ |
| 1563 | skb = l2cap_create_basic_pdu(chan, msg, len); |
| 1564 | if (IS_ERR(skb)) |
| 1565 | return PTR_ERR(skb); |
| 1566 | |
| 1567 | l2cap_do_send(chan, skb); |
| 1568 | err = len; |
| 1569 | break; |
| 1570 | |
| 1571 | case L2CAP_MODE_ERTM: |
| 1572 | case L2CAP_MODE_STREAMING: |
| 1573 | /* Entire SDU fits into one PDU */ |
| 1574 | if (len <= chan->remote_mps) { |
| 1575 | control = L2CAP_SDU_UNSEGMENTED; |
| 1576 | skb = l2cap_create_iframe_pdu(chan, msg, len, control, |
| 1577 | 0); |
| 1578 | if (IS_ERR(skb)) |
| 1579 | return PTR_ERR(skb); |
| 1580 | |
| 1581 | __skb_queue_tail(&chan->tx_q, skb); |
| 1582 | |
| 1583 | if (chan->tx_send_head == NULL) |
| 1584 | chan->tx_send_head = skb; |
| 1585 | |
| 1586 | } else { |
| 1587 | /* Segment SDU into multiples PDUs */ |
| 1588 | err = l2cap_sar_segment_sdu(chan, msg, len); |
| 1589 | if (err < 0) |
| 1590 | return err; |
| 1591 | } |
| 1592 | |
| 1593 | if (chan->mode == L2CAP_MODE_STREAMING) { |
| 1594 | l2cap_streaming_send(chan); |
| 1595 | err = len; |
| 1596 | break; |
| 1597 | } |
| 1598 | |
| 1599 | if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && |
| 1600 | (chan->conn_state & L2CAP_CONN_WAIT_F)) { |
| 1601 | err = len; |
| 1602 | break; |
| 1603 | } |
| 1604 | |
| 1605 | err = l2cap_ertm_send(chan); |
| 1606 | if (err >= 0) |
| 1607 | err = len; |
| 1608 | |
| 1609 | break; |
| 1610 | |
| 1611 | default: |
| 1612 | BT_DBG("bad state %1.1x", chan->mode); |
| 1613 | err = -EBADFD; |
| 1614 | } |
| 1615 | |
| 1616 | return err; |
| 1617 | } |
| 1618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | static void l2cap_chan_ready(struct sock *sk) |
| 1620 | { |
| 1621 | struct sock *parent = bt_sk(sk)->parent; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1622 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | |
| 1624 | BT_DBG("sk %p, parent %p", sk, parent); |
| 1625 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1626 | chan->conf_state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1627 | l2cap_sock_clear_timer(sk); |
| 1628 | |
| 1629 | if (!parent) { |
| 1630 | /* Outgoing channel. |
| 1631 | * Wake up socket sleeping on connect. |
| 1632 | */ |
| 1633 | sk->sk_state = BT_CONNECTED; |
| 1634 | sk->sk_state_change(sk); |
| 1635 | } else { |
| 1636 | /* Incoming channel. |
| 1637 | * Wake up socket sleeping on accept. |
| 1638 | */ |
| 1639 | parent->sk_data_ready(parent, 0); |
| 1640 | } |
| 1641 | } |
| 1642 | |
| 1643 | /* Copy frame to all raw sockets on that connection */ |
| 1644 | static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1645 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | struct sk_buff *nskb; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1647 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | |
| 1649 | BT_DBG("conn %p", conn); |
| 1650 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1651 | read_lock(&conn->chan_lock); |
| 1652 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1653 | struct sock *sk = chan->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | if (sk->sk_type != SOCK_RAW) |
| 1655 | continue; |
| 1656 | |
| 1657 | /* Don't send frame to the socket it came from */ |
| 1658 | if (skb->sk == sk) |
| 1659 | continue; |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1660 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 1661 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | continue; |
| 1663 | |
| 1664 | if (sock_queue_rcv_skb(sk, nskb)) |
| 1665 | kfree_skb(nskb); |
| 1666 | } |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1667 | read_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | /* ---- L2CAP signalling commands ---- */ |
| 1671 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, |
| 1672 | u8 code, u8 ident, u16 dlen, void *data) |
| 1673 | { |
| 1674 | struct sk_buff *skb, **frag; |
| 1675 | struct l2cap_cmd_hdr *cmd; |
| 1676 | struct l2cap_hdr *lh; |
| 1677 | int len, count; |
| 1678 | |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1679 | BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", |
| 1680 | conn, code, ident, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | |
| 1682 | len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; |
| 1683 | count = min_t(unsigned int, conn->mtu, len); |
| 1684 | |
| 1685 | skb = bt_skb_alloc(count, GFP_ATOMIC); |
| 1686 | if (!skb) |
| 1687 | return NULL; |
| 1688 | |
| 1689 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1690 | lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 1691 | |
| 1692 | if (conn->hcon->type == LE_LINK) |
| 1693 | lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); |
| 1694 | else |
| 1695 | lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | |
| 1697 | cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); |
| 1698 | cmd->code = code; |
| 1699 | cmd->ident = ident; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1700 | cmd->len = cpu_to_le16(dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | |
| 1702 | if (dlen) { |
| 1703 | count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; |
| 1704 | memcpy(skb_put(skb, count), data, count); |
| 1705 | data += count; |
| 1706 | } |
| 1707 | |
| 1708 | len -= skb->len; |
| 1709 | |
| 1710 | /* Continuation fragments (no L2CAP header) */ |
| 1711 | frag = &skb_shinfo(skb)->frag_list; |
| 1712 | while (len) { |
| 1713 | count = min_t(unsigned int, conn->mtu, len); |
| 1714 | |
| 1715 | *frag = bt_skb_alloc(count, GFP_ATOMIC); |
| 1716 | if (!*frag) |
| 1717 | goto fail; |
| 1718 | |
| 1719 | memcpy(skb_put(*frag, count), data, count); |
| 1720 | |
| 1721 | len -= count; |
| 1722 | data += count; |
| 1723 | |
| 1724 | frag = &(*frag)->next; |
| 1725 | } |
| 1726 | |
| 1727 | return skb; |
| 1728 | |
| 1729 | fail: |
| 1730 | kfree_skb(skb); |
| 1731 | return NULL; |
| 1732 | } |
| 1733 | |
| 1734 | static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) |
| 1735 | { |
| 1736 | struct l2cap_conf_opt *opt = *ptr; |
| 1737 | int len; |
| 1738 | |
| 1739 | len = L2CAP_CONF_OPT_SIZE + opt->len; |
| 1740 | *ptr += len; |
| 1741 | |
| 1742 | *type = opt->type; |
| 1743 | *olen = opt->len; |
| 1744 | |
| 1745 | switch (opt->len) { |
| 1746 | case 1: |
| 1747 | *val = *((u8 *) opt->val); |
| 1748 | break; |
| 1749 | |
| 1750 | case 2: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 1751 | *val = get_unaligned_le16(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | break; |
| 1753 | |
| 1754 | case 4: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 1755 | *val = get_unaligned_le32(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | break; |
| 1757 | |
| 1758 | default: |
| 1759 | *val = (unsigned long) opt->val; |
| 1760 | break; |
| 1761 | } |
| 1762 | |
| 1763 | BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); |
| 1764 | return len; |
| 1765 | } |
| 1766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) |
| 1768 | { |
| 1769 | struct l2cap_conf_opt *opt = *ptr; |
| 1770 | |
| 1771 | BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); |
| 1772 | |
| 1773 | opt->type = type; |
| 1774 | opt->len = len; |
| 1775 | |
| 1776 | switch (len) { |
| 1777 | case 1: |
| 1778 | *((u8 *) opt->val) = val; |
| 1779 | break; |
| 1780 | |
| 1781 | case 2: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 1782 | put_unaligned_le16(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | break; |
| 1784 | |
| 1785 | case 4: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 1786 | put_unaligned_le32(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | break; |
| 1788 | |
| 1789 | default: |
| 1790 | memcpy(opt->val, (void *) val, len); |
| 1791 | break; |
| 1792 | } |
| 1793 | |
| 1794 | *ptr += L2CAP_CONF_OPT_SIZE + len; |
| 1795 | } |
| 1796 | |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 1797 | static void l2cap_ack_timeout(unsigned long arg) |
| 1798 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1799 | struct l2cap_chan *chan = (void *) arg; |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 1800 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1801 | bh_lock_sock(chan->sk); |
| 1802 | l2cap_send_ack(chan); |
| 1803 | bh_unlock_sock(chan->sk); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 1804 | } |
| 1805 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1806 | static inline void l2cap_ertm_init(struct l2cap_chan *chan) |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 1807 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1808 | struct sock *sk = chan->sk; |
| 1809 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1810 | chan->expected_ack_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1811 | chan->unacked_frames = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1812 | chan->buffer_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1813 | chan->num_acked = 0; |
| 1814 | chan->frames_sent = 0; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 1815 | |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 1816 | setup_timer(&chan->retrans_timer, l2cap_retrans_timeout, |
| 1817 | (unsigned long) chan); |
| 1818 | setup_timer(&chan->monitor_timer, l2cap_monitor_timeout, |
| 1819 | (unsigned long) chan); |
| 1820 | setup_timer(&chan->ack_timer, l2cap_ack_timeout, (unsigned long) chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 1821 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 1822 | skb_queue_head_init(&chan->srej_q); |
| 1823 | skb_queue_head_init(&chan->busy_q); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1824 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 1825 | INIT_LIST_HEAD(&chan->srej_l); |
| 1826 | |
Gustavo F. Padovan | 311bb89 | 2011-03-25 20:41:00 -0300 | [diff] [blame] | 1827 | INIT_WORK(&chan->busy_work, l2cap_busy_work); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 1828 | |
| 1829 | sk->sk_backlog_rcv = l2cap_ertm_data_rcv; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 1830 | } |
| 1831 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1832 | static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) |
| 1833 | { |
| 1834 | switch (mode) { |
| 1835 | case L2CAP_MODE_STREAMING: |
| 1836 | case L2CAP_MODE_ERTM: |
| 1837 | if (l2cap_mode_supported(mode, remote_feat_mask)) |
| 1838 | return mode; |
| 1839 | /* fall through */ |
| 1840 | default: |
| 1841 | return L2CAP_MODE_BASIC; |
| 1842 | } |
| 1843 | } |
| 1844 | |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 1845 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | struct l2cap_conf_req *req = data; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1848 | struct l2cap_conf_rfc rfc = { .mode = chan->mode }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | void *ptr = req->data; |
| 1850 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1851 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1853 | if (chan->num_conf_req || chan->num_conf_rsp) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1854 | goto done; |
| 1855 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1856 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1857 | case L2CAP_MODE_STREAMING: |
| 1858 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1859 | if (chan->conf_state & L2CAP_CONF_STATE2_DEVICE) |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 1860 | break; |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 1861 | |
Gustavo F. Padovan | 2ba13ed | 2010-06-09 16:39:05 -0300 | [diff] [blame] | 1862 | /* fall through */ |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1863 | default: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1864 | chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1865 | break; |
| 1866 | } |
| 1867 | |
| 1868 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1869 | if (chan->imtu != L2CAP_DEFAULT_MTU) |
| 1870 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | 7990681 | 2011-01-24 16:01:43 -0200 | [diff] [blame] | 1871 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1872 | switch (chan->mode) { |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 1873 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1874 | if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && |
| 1875 | !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 1876 | break; |
| 1877 | |
Gustavo F. Padovan | 6254775 | 2010-06-08 20:05:31 -0300 | [diff] [blame] | 1878 | rfc.mode = L2CAP_MODE_BASIC; |
| 1879 | rfc.txwin_size = 0; |
| 1880 | rfc.max_transmit = 0; |
| 1881 | rfc.retrans_timeout = 0; |
| 1882 | rfc.monitor_timeout = 0; |
| 1883 | rfc.max_pdu_size = 0; |
| 1884 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 1885 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 1886 | (unsigned long) &rfc); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 1887 | break; |
| 1888 | |
| 1889 | case L2CAP_MODE_ERTM: |
| 1890 | rfc.mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1891 | rfc.txwin_size = chan->tx_win; |
| 1892 | rfc.max_transmit = chan->max_tx; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1893 | rfc.retrans_timeout = 0; |
| 1894 | rfc.monitor_timeout = 0; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1895 | rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1896 | if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10) |
| 1897 | rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1898 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 1899 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 1900 | (unsigned long) &rfc); |
| 1901 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1902 | if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1903 | break; |
| 1904 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1905 | if (chan->fcs == L2CAP_FCS_NONE || |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1906 | chan->conf_state & L2CAP_CONF_NO_FCS_RECV) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1907 | chan->fcs = L2CAP_FCS_NONE; |
| 1908 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1909 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1910 | break; |
| 1911 | |
| 1912 | case L2CAP_MODE_STREAMING: |
| 1913 | rfc.mode = L2CAP_MODE_STREAMING; |
| 1914 | rfc.txwin_size = 0; |
| 1915 | rfc.max_transmit = 0; |
| 1916 | rfc.retrans_timeout = 0; |
| 1917 | rfc.monitor_timeout = 0; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1918 | rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1919 | if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10) |
| 1920 | rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 1921 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 1922 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 1923 | (unsigned long) &rfc); |
| 1924 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1925 | if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1926 | break; |
| 1927 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1928 | if (chan->fcs == L2CAP_FCS_NONE || |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1929 | chan->conf_state & L2CAP_CONF_NO_FCS_RECV) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1930 | chan->fcs = L2CAP_FCS_NONE; |
| 1931 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1932 | } |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 1933 | break; |
| 1934 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1936 | req->dcid = cpu_to_le16(chan->dcid); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1937 | req->flags = cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | |
| 1939 | return ptr - data; |
| 1940 | } |
| 1941 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1942 | static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1944 | struct l2cap_conf_rsp *rsp = data; |
| 1945 | void *ptr = rsp->data; |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1946 | void *req = chan->conf_req; |
| 1947 | int len = chan->conf_len; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1948 | int type, hint, olen; |
| 1949 | unsigned long val; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 1950 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 1951 | u16 mtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1952 | u16 result = L2CAP_CONF_SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1954 | BT_DBG("chan %p", chan); |
Marcel Holtmann | 820ae1b | 2006-11-18 22:15:00 +0100 | [diff] [blame] | 1955 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1956 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 1957 | len -= l2cap_get_conf_opt(&req, &type, &olen, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | |
Gustavo F. Padovan | 589d274 | 2009-04-20 01:31:07 -0300 | [diff] [blame] | 1959 | hint = type & L2CAP_CONF_HINT; |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 1960 | type &= L2CAP_CONF_MASK; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1961 | |
| 1962 | switch (type) { |
| 1963 | case L2CAP_CONF_MTU: |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 1964 | mtu = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1965 | break; |
| 1966 | |
| 1967 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1968 | chan->flush_to = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1969 | break; |
| 1970 | |
| 1971 | case L2CAP_CONF_QOS: |
| 1972 | break; |
| 1973 | |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 1974 | case L2CAP_CONF_RFC: |
| 1975 | if (olen == sizeof(rfc)) |
| 1976 | memcpy(&rfc, (void *) val, olen); |
| 1977 | break; |
| 1978 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1979 | case L2CAP_CONF_FCS: |
| 1980 | if (val == L2CAP_FCS_NONE) |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1981 | chan->conf_state |= L2CAP_CONF_NO_FCS_RECV; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1982 | |
| 1983 | break; |
| 1984 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1985 | default: |
| 1986 | if (hint) |
| 1987 | break; |
| 1988 | |
| 1989 | result = L2CAP_CONF_UNKNOWN; |
| 1990 | *((u8 *) ptr++) = type; |
| 1991 | break; |
| 1992 | } |
| 1993 | } |
| 1994 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1995 | if (chan->num_conf_rsp || chan->num_conf_req > 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1996 | goto done; |
| 1997 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1998 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1999 | case L2CAP_MODE_STREAMING: |
| 2000 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2001 | if (!(chan->conf_state & L2CAP_CONF_STATE2_DEVICE)) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2002 | chan->mode = l2cap_select_mode(rfc.mode, |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2003 | chan->conn->feat_mask); |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 2004 | break; |
| 2005 | } |
| 2006 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2007 | if (chan->mode != rfc.mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2008 | return -ECONNREFUSED; |
Gustavo F. Padovan | 742e519 | 2010-06-08 19:09:48 -0300 | [diff] [blame] | 2009 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2010 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2014 | if (chan->mode != rfc.mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2015 | result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2016 | rfc.mode = chan->mode; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2017 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2018 | if (chan->num_conf_rsp == 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2019 | return -ECONNREFUSED; |
| 2020 | |
| 2021 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2022 | sizeof(rfc), (unsigned long) &rfc); |
| 2023 | } |
| 2024 | |
| 2025 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2026 | if (result == L2CAP_CONF_SUCCESS) { |
| 2027 | /* Configure output options and let the other side know |
| 2028 | * which ones we don't like. */ |
| 2029 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2030 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
| 2031 | result = L2CAP_CONF_UNACCEPT; |
| 2032 | else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2033 | chan->omtu = mtu; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2034 | chan->conf_state |= L2CAP_CONF_MTU_DONE; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2035 | } |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2036 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2037 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2038 | switch (rfc.mode) { |
| 2039 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2040 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2041 | chan->conf_state |= L2CAP_CONF_MODE_DONE; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2042 | break; |
| 2043 | |
| 2044 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 2045 | chan->remote_tx_win = rfc.txwin_size; |
| 2046 | chan->remote_max_tx = rfc.max_transmit; |
Mat Martineau | 86b1b26 | 2010-08-05 15:54:22 -0700 | [diff] [blame] | 2047 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2048 | if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10) |
| 2049 | rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); |
Gustavo F. Padovan | 1c76215 | 2010-05-01 16:15:40 -0300 | [diff] [blame] | 2050 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 2051 | chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2052 | |
Gustavo F. Padovan | 10467e9 | 2010-05-01 16:15:40 -0300 | [diff] [blame] | 2053 | rfc.retrans_timeout = |
| 2054 | le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO); |
| 2055 | rfc.monitor_timeout = |
| 2056 | le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2057 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2058 | chan->conf_state |= L2CAP_CONF_MODE_DONE; |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 2059 | |
| 2060 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2061 | sizeof(rfc), (unsigned long) &rfc); |
| 2062 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2063 | break; |
| 2064 | |
| 2065 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2066 | if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10) |
| 2067 | rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); |
Gustavo F. Padovan | 1c76215 | 2010-05-01 16:15:40 -0300 | [diff] [blame] | 2068 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 2069 | chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2070 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2071 | chan->conf_state |= L2CAP_CONF_MODE_DONE; |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 2072 | |
| 2073 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2074 | sizeof(rfc), (unsigned long) &rfc); |
| 2075 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2076 | break; |
| 2077 | |
| 2078 | default: |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 2079 | result = L2CAP_CONF_UNACCEPT; |
| 2080 | |
| 2081 | memset(&rfc, 0, sizeof(rfc)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2082 | rfc.mode = chan->mode; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 2083 | } |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2084 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2085 | if (result == L2CAP_CONF_SUCCESS) |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2086 | chan->conf_state |= L2CAP_CONF_OUTPUT_DONE; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2087 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2088 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2089 | rsp->result = cpu_to_le16(result); |
| 2090 | rsp->flags = cpu_to_le16(0x0000); |
| 2091 | |
| 2092 | return ptr - data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | } |
| 2094 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2095 | static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2096 | { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2097 | struct l2cap_conf_req *req = data; |
| 2098 | void *ptr = req->data; |
| 2099 | int type, olen; |
| 2100 | unsigned long val; |
| 2101 | struct l2cap_conf_rfc rfc; |
| 2102 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2103 | BT_DBG("chan %p, rsp %p, len %d, req %p", chan, rsp, len, data); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2104 | |
| 2105 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 2106 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 2107 | |
| 2108 | switch (type) { |
| 2109 | case L2CAP_CONF_MTU: |
| 2110 | if (val < L2CAP_DEFAULT_MIN_MTU) { |
| 2111 | *result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2112 | chan->imtu = L2CAP_DEFAULT_MIN_MTU; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2113 | } else |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2114 | chan->imtu = val; |
| 2115 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2116 | break; |
| 2117 | |
| 2118 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2119 | chan->flush_to = val; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2120 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2121 | 2, chan->flush_to); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2122 | break; |
| 2123 | |
| 2124 | case L2CAP_CONF_RFC: |
| 2125 | if (olen == sizeof(rfc)) |
| 2126 | memcpy(&rfc, (void *)val, olen); |
| 2127 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2128 | if ((chan->conf_state & L2CAP_CONF_STATE2_DEVICE) && |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2129 | rfc.mode != chan->mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2130 | return -ECONNREFUSED; |
| 2131 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2132 | chan->fcs = 0; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2133 | |
| 2134 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2135 | sizeof(rfc), (unsigned long) &rfc); |
| 2136 | break; |
| 2137 | } |
| 2138 | } |
| 2139 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2140 | if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 2141 | return -ECONNREFUSED; |
| 2142 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2143 | chan->mode = rfc.mode; |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 2144 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2145 | if (*result == L2CAP_CONF_SUCCESS) { |
| 2146 | switch (rfc.mode) { |
| 2147 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2148 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 2149 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 2150 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2151 | break; |
| 2152 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2153 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2154 | } |
| 2155 | } |
| 2156 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2157 | req->dcid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2158 | req->flags = cpu_to_le16(0x0000); |
| 2159 | |
| 2160 | return ptr - data; |
| 2161 | } |
| 2162 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2163 | static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | { |
| 2165 | struct l2cap_conf_rsp *rsp = data; |
| 2166 | void *ptr = rsp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2168 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2170 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2171 | rsp->result = cpu_to_le16(result); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2172 | rsp->flags = cpu_to_le16(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | |
| 2174 | return ptr - data; |
| 2175 | } |
| 2176 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2177 | void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2178 | { |
| 2179 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2180 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2181 | u8 buf[128]; |
| 2182 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2183 | rsp.scid = cpu_to_le16(chan->dcid); |
| 2184 | rsp.dcid = cpu_to_le16(chan->scid); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2185 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 2186 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 2187 | l2cap_send_cmd(conn, chan->ident, |
| 2188 | L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
| 2189 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2190 | if (chan->conf_state & L2CAP_CONF_REQ_SENT) |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2191 | return; |
| 2192 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2193 | chan->conf_state |= L2CAP_CONF_REQ_SENT; |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2194 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
| 2195 | l2cap_build_conf_req(chan, buf), buf); |
| 2196 | chan->num_conf_req++; |
| 2197 | } |
| 2198 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2199 | static void l2cap_conf_rfc_get(struct l2cap_chan *chan, void *rsp, int len) |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2200 | { |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2201 | int type, olen; |
| 2202 | unsigned long val; |
| 2203 | struct l2cap_conf_rfc rfc; |
| 2204 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2205 | BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2206 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2207 | if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2208 | return; |
| 2209 | |
| 2210 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 2211 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 2212 | |
| 2213 | switch (type) { |
| 2214 | case L2CAP_CONF_RFC: |
| 2215 | if (olen == sizeof(rfc)) |
| 2216 | memcpy(&rfc, (void *)val, olen); |
| 2217 | goto done; |
| 2218 | } |
| 2219 | } |
| 2220 | |
| 2221 | done: |
| 2222 | switch (rfc.mode) { |
| 2223 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2224 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 2225 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 2226 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2227 | break; |
| 2228 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2229 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2230 | } |
| 2231 | } |
| 2232 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2233 | static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2234 | { |
| 2235 | struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data; |
| 2236 | |
| 2237 | if (rej->reason != 0x0000) |
| 2238 | return 0; |
| 2239 | |
| 2240 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && |
| 2241 | cmd->ident == conn->info_ident) { |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2242 | del_timer(&conn->info_timer); |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2243 | |
| 2244 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 2245 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2246 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2247 | l2cap_conn_start(conn); |
| 2248 | } |
| 2249 | |
| 2250 | return 0; |
| 2251 | } |
| 2252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2254 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; |
| 2256 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2257 | struct l2cap_chan *chan = NULL, *pchan; |
Nathan Holstein | d793fe8 | 2010-10-15 11:54:02 -0400 | [diff] [blame] | 2258 | struct sock *parent, *sk = NULL; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2259 | int result, status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | |
| 2261 | u16 dcid = 0, scid = __le16_to_cpu(req->scid); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2262 | __le16 psm = req->psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | |
| 2264 | BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid); |
| 2265 | |
| 2266 | /* Check if we have socket listening on psm */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2267 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src); |
| 2268 | if (!pchan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | result = L2CAP_CR_BAD_PSM; |
| 2270 | goto sendresp; |
| 2271 | } |
| 2272 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2273 | parent = pchan->sk; |
| 2274 | |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 2275 | bh_lock_sock(parent); |
| 2276 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2277 | /* Check if the ACL is secure enough (if not SDP) */ |
| 2278 | if (psm != cpu_to_le16(0x0001) && |
| 2279 | !hci_conn_check_link_mode(conn->hcon)) { |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 2280 | conn->disc_reason = 0x05; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2281 | result = L2CAP_CR_SEC_BLOCK; |
| 2282 | goto response; |
| 2283 | } |
| 2284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2285 | result = L2CAP_CR_NO_MEM; |
| 2286 | |
| 2287 | /* Check for backlog size */ |
| 2288 | if (sk_acceptq_is_full(parent)) { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2289 | BT_DBG("backlog full %d", parent->sk_ack_backlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | goto response; |
| 2291 | } |
| 2292 | |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2293 | sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | if (!sk) |
| 2295 | goto response; |
| 2296 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2297 | chan = l2cap_chan_create(sk); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2298 | if (!chan) { |
| 2299 | l2cap_sock_kill(sk); |
| 2300 | goto response; |
| 2301 | } |
| 2302 | |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 2303 | l2cap_pi(sk)->chan = chan; |
| 2304 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2305 | write_lock_bh(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | |
| 2307 | /* Check if we already have channel with that dcid */ |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2308 | if (__l2cap_get_chan_by_dcid(conn, scid)) { |
| 2309 | write_unlock_bh(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | sock_set_flag(sk, SOCK_ZAPPED); |
| 2311 | l2cap_sock_kill(sk); |
| 2312 | goto response; |
| 2313 | } |
| 2314 | |
| 2315 | hci_conn_hold(conn->hcon); |
| 2316 | |
| 2317 | l2cap_sock_init(sk, parent); |
| 2318 | bacpy(&bt_sk(sk)->src, conn->src); |
| 2319 | bacpy(&bt_sk(sk)->dst, conn->dst); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2320 | chan->psm = psm; |
| 2321 | chan->dcid = scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | |
Gustavo F. Padovan | d101024 | 2011-03-25 00:39:48 -0300 | [diff] [blame] | 2323 | bt_accept_enqueue(parent, sk); |
| 2324 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2325 | __l2cap_chan_add(conn, chan); |
| 2326 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2327 | dcid = chan->scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | |
| 2329 | l2cap_sock_set_timer(sk, sk->sk_sndtimeo); |
| 2330 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 2331 | chan->ident = cmd->ident; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2333 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 2334 | if (l2cap_check_security(chan)) { |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 2335 | if (bt_sk(sk)->defer_setup) { |
| 2336 | sk->sk_state = BT_CONNECT2; |
| 2337 | result = L2CAP_CR_PEND; |
| 2338 | status = L2CAP_CS_AUTHOR_PEND; |
| 2339 | parent->sk_data_ready(parent, 0); |
| 2340 | } else { |
| 2341 | sk->sk_state = BT_CONFIG; |
| 2342 | result = L2CAP_CR_SUCCESS; |
| 2343 | status = L2CAP_CS_NO_INFO; |
| 2344 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2345 | } else { |
| 2346 | sk->sk_state = BT_CONNECT2; |
| 2347 | result = L2CAP_CR_PEND; |
| 2348 | status = L2CAP_CS_AUTHEN_PEND; |
| 2349 | } |
| 2350 | } else { |
| 2351 | sk->sk_state = BT_CONNECT2; |
| 2352 | result = L2CAP_CR_PEND; |
| 2353 | status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | } |
| 2355 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2356 | write_unlock_bh(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | |
| 2358 | response: |
| 2359 | bh_unlock_sock(parent); |
| 2360 | |
| 2361 | sendresp: |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2362 | rsp.scid = cpu_to_le16(scid); |
| 2363 | rsp.dcid = cpu_to_le16(dcid); |
| 2364 | rsp.result = cpu_to_le16(result); |
| 2365 | rsp.status = cpu_to_le16(status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2367 | |
| 2368 | if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { |
| 2369 | struct l2cap_info_req info; |
| 2370 | info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 2371 | |
| 2372 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 2373 | conn->info_ident = l2cap_get_ident(conn); |
| 2374 | |
| 2375 | mod_timer(&conn->info_timer, jiffies + |
| 2376 | msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); |
| 2377 | |
| 2378 | l2cap_send_cmd(conn, conn->info_ident, |
| 2379 | L2CAP_INFO_REQ, sizeof(info), &info); |
| 2380 | } |
| 2381 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2382 | if (chan && !(chan->conf_state & L2CAP_CONF_REQ_SENT) && |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2383 | result == L2CAP_CR_SUCCESS) { |
| 2384 | u8 buf[128]; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2385 | chan->conf_state |= L2CAP_CONF_REQ_SENT; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2386 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2387 | l2cap_build_conf_req(chan, buf), buf); |
| 2388 | chan->num_conf_req++; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2389 | } |
| 2390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | return 0; |
| 2392 | } |
| 2393 | |
| 2394 | static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2395 | { |
| 2396 | struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; |
| 2397 | u16 scid, dcid, result, status; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2398 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | struct sock *sk; |
| 2400 | u8 req[128]; |
| 2401 | |
| 2402 | scid = __le16_to_cpu(rsp->scid); |
| 2403 | dcid = __le16_to_cpu(rsp->dcid); |
| 2404 | result = __le16_to_cpu(rsp->result); |
| 2405 | status = __le16_to_cpu(rsp->status); |
| 2406 | |
| 2407 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status); |
| 2408 | |
| 2409 | if (scid) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2410 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2411 | if (!chan) |
João Paulo Rechi Vita | 57d3b22 | 2010-06-22 13:56:26 -0300 | [diff] [blame] | 2412 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2413 | } else { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2414 | chan = l2cap_get_chan_by_ident(conn, cmd->ident); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2415 | if (!chan) |
João Paulo Rechi Vita | 57d3b22 | 2010-06-22 13:56:26 -0300 | [diff] [blame] | 2416 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | } |
| 2418 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2419 | sk = chan->sk; |
| 2420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | switch (result) { |
| 2422 | case L2CAP_CR_SUCCESS: |
| 2423 | sk->sk_state = BT_CONFIG; |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 2424 | chan->ident = 0; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2425 | chan->dcid = dcid; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2426 | chan->conf_state &= ~L2CAP_CONF_CONNECT_PEND; |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 2427 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2428 | if (chan->conf_state & L2CAP_CONF_REQ_SENT) |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2429 | break; |
| 2430 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2431 | chan->conf_state |= L2CAP_CONF_REQ_SENT; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2434 | l2cap_build_conf_req(chan, req), req); |
| 2435 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | break; |
| 2437 | |
| 2438 | case L2CAP_CR_PEND: |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2439 | chan->conf_state |= L2CAP_CONF_CONNECT_PEND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | break; |
| 2441 | |
| 2442 | default: |
Andrei Emeltchenko | a49184c | 2010-11-03 12:32:44 +0200 | [diff] [blame] | 2443 | /* don't delete l2cap channel if sk is owned by user */ |
| 2444 | if (sock_owned_by_user(sk)) { |
| 2445 | sk->sk_state = BT_DISCONN; |
| 2446 | l2cap_sock_clear_timer(sk); |
| 2447 | l2cap_sock_set_timer(sk, HZ / 5); |
| 2448 | break; |
| 2449 | } |
| 2450 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2451 | l2cap_chan_del(chan, ECONNREFUSED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2452 | break; |
| 2453 | } |
| 2454 | |
| 2455 | bh_unlock_sock(sk); |
| 2456 | return 0; |
| 2457 | } |
| 2458 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2459 | static inline void set_default_fcs(struct l2cap_chan *chan) |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 2460 | { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2461 | struct l2cap_pinfo *pi = l2cap_pi(chan->sk); |
| 2462 | |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 2463 | /* FCS is enabled only in ERTM or streaming mode, if one or both |
| 2464 | * sides request it. |
| 2465 | */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2466 | if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2467 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2468 | else if (!(pi->chan->conf_state & L2CAP_CONF_NO_FCS_RECV)) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2469 | chan->fcs = L2CAP_FCS_CRC16; |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 2470 | } |
| 2471 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2472 | static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | { |
| 2474 | struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; |
| 2475 | u16 dcid, flags; |
| 2476 | u8 rsp[64]; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2477 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | struct sock *sk; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2479 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | |
| 2481 | dcid = __le16_to_cpu(req->dcid); |
| 2482 | flags = __le16_to_cpu(req->flags); |
| 2483 | |
| 2484 | BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); |
| 2485 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2486 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2487 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | return -ENOENT; |
| 2489 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2490 | sk = chan->sk; |
| 2491 | |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 2492 | if (sk->sk_state != BT_CONFIG) { |
| 2493 | struct l2cap_cmd_rej rej; |
| 2494 | |
| 2495 | rej.reason = cpu_to_le16(0x0002); |
| 2496 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
| 2497 | sizeof(rej), &rej); |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 2498 | goto unlock; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 2499 | } |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 2500 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2501 | /* Reject if config buffer is too small. */ |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2502 | len = cmd_len - sizeof(*req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2503 | if (chan->conf_len + len > sizeof(chan->conf_req)) { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2504 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2505 | l2cap_build_conf_rsp(chan, rsp, |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2506 | L2CAP_CONF_REJECT, flags), rsp); |
| 2507 | goto unlock; |
| 2508 | } |
| 2509 | |
| 2510 | /* Store config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2511 | memcpy(chan->conf_req + chan->conf_len, req->data, len); |
| 2512 | chan->conf_len += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | |
| 2514 | if (flags & 0x0001) { |
| 2515 | /* Incomplete config. Send empty response. */ |
| 2516 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2517 | l2cap_build_conf_rsp(chan, rsp, |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2518 | L2CAP_CONF_SUCCESS, 0x0001), rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | goto unlock; |
| 2520 | } |
| 2521 | |
| 2522 | /* Complete config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2523 | len = l2cap_parse_conf_req(chan, rsp); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2524 | if (len < 0) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 2525 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | goto unlock; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2527 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2529 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2530 | chan->num_conf_rsp++; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2531 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2532 | /* Reset config buffer. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2533 | chan->conf_len = 0; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2534 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2535 | if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE)) |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 2536 | goto unlock; |
| 2537 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2538 | if (chan->conf_state & L2CAP_CONF_INPUT_DONE) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2539 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2541 | sk->sk_state = BT_CONNECTED; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2542 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2543 | chan->next_tx_seq = 0; |
| 2544 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2545 | skb_queue_head_init(&chan->tx_q); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2546 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2547 | l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | l2cap_chan_ready(sk); |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 2550 | goto unlock; |
| 2551 | } |
| 2552 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2553 | if (!(chan->conf_state & L2CAP_CONF_REQ_SENT)) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2554 | u8 buf[64]; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2555 | chan->conf_state |= L2CAP_CONF_REQ_SENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2557 | l2cap_build_conf_req(chan, buf), buf); |
| 2558 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | } |
| 2560 | |
| 2561 | unlock: |
| 2562 | bh_unlock_sock(sk); |
| 2563 | return 0; |
| 2564 | } |
| 2565 | |
| 2566 | static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2567 | { |
| 2568 | struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; |
| 2569 | u16 scid, flags, result; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2570 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2571 | struct sock *sk; |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2572 | int len = cmd->len - sizeof(*rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | |
| 2574 | scid = __le16_to_cpu(rsp->scid); |
| 2575 | flags = __le16_to_cpu(rsp->flags); |
| 2576 | result = __le16_to_cpu(rsp->result); |
| 2577 | |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 2578 | BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", |
| 2579 | scid, flags, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2581 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2582 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | return 0; |
| 2584 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2585 | sk = chan->sk; |
| 2586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2587 | switch (result) { |
| 2588 | case L2CAP_CONF_SUCCESS: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2589 | l2cap_conf_rfc_get(chan, rsp->data, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | break; |
| 2591 | |
| 2592 | case L2CAP_CONF_UNACCEPT: |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2593 | if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2594 | char req[64]; |
| 2595 | |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 2596 | if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 2597 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 2598 | goto done; |
| 2599 | } |
| 2600 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2601 | /* throw out any old stored conf requests */ |
| 2602 | result = L2CAP_CONF_SUCCESS; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2603 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
| 2604 | req, &result); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2605 | if (len < 0) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 2606 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2607 | goto done; |
| 2608 | } |
| 2609 | |
| 2610 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 2611 | L2CAP_CONF_REQ, len, req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2612 | chan->num_conf_req++; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2613 | if (result != L2CAP_CONF_SUCCESS) |
| 2614 | goto done; |
| 2615 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | } |
| 2617 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2618 | default: |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 2619 | sk->sk_err = ECONNRESET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | l2cap_sock_set_timer(sk, HZ * 5); |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 2621 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | goto done; |
| 2623 | } |
| 2624 | |
| 2625 | if (flags & 0x01) |
| 2626 | goto done; |
| 2627 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2628 | chan->conf_state |= L2CAP_CONF_INPUT_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2630 | if (chan->conf_state & L2CAP_CONF_OUTPUT_DONE) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2631 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | sk->sk_state = BT_CONNECTED; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2634 | chan->next_tx_seq = 0; |
| 2635 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2636 | skb_queue_head_init(&chan->tx_q); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2637 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2638 | l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | l2cap_chan_ready(sk); |
| 2641 | } |
| 2642 | |
| 2643 | done: |
| 2644 | bh_unlock_sock(sk); |
| 2645 | return 0; |
| 2646 | } |
| 2647 | |
| 2648 | static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2649 | { |
| 2650 | struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; |
| 2651 | struct l2cap_disconn_rsp rsp; |
| 2652 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2653 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | struct sock *sk; |
| 2655 | |
| 2656 | scid = __le16_to_cpu(req->scid); |
| 2657 | dcid = __le16_to_cpu(req->dcid); |
| 2658 | |
| 2659 | BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); |
| 2660 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2661 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2662 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | return 0; |
| 2664 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2665 | sk = chan->sk; |
| 2666 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2667 | rsp.dcid = cpu_to_le16(chan->scid); |
| 2668 | rsp.scid = cpu_to_le16(chan->dcid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); |
| 2670 | |
| 2671 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 2672 | |
Andrei Emeltchenko | a49184c | 2010-11-03 12:32:44 +0200 | [diff] [blame] | 2673 | /* don't delete l2cap channel if sk is owned by user */ |
| 2674 | if (sock_owned_by_user(sk)) { |
| 2675 | sk->sk_state = BT_DISCONN; |
| 2676 | l2cap_sock_clear_timer(sk); |
| 2677 | l2cap_sock_set_timer(sk, HZ / 5); |
| 2678 | bh_unlock_sock(sk); |
| 2679 | return 0; |
| 2680 | } |
| 2681 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2682 | l2cap_chan_del(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | bh_unlock_sock(sk); |
| 2684 | |
| 2685 | l2cap_sock_kill(sk); |
| 2686 | return 0; |
| 2687 | } |
| 2688 | |
| 2689 | static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2690 | { |
| 2691 | struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; |
| 2692 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2693 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | struct sock *sk; |
| 2695 | |
| 2696 | scid = __le16_to_cpu(rsp->scid); |
| 2697 | dcid = __le16_to_cpu(rsp->dcid); |
| 2698 | |
| 2699 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); |
| 2700 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2701 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2702 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | return 0; |
| 2704 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2705 | sk = chan->sk; |
| 2706 | |
Andrei Emeltchenko | a49184c | 2010-11-03 12:32:44 +0200 | [diff] [blame] | 2707 | /* don't delete l2cap channel if sk is owned by user */ |
| 2708 | if (sock_owned_by_user(sk)) { |
| 2709 | sk->sk_state = BT_DISCONN; |
| 2710 | l2cap_sock_clear_timer(sk); |
| 2711 | l2cap_sock_set_timer(sk, HZ / 5); |
| 2712 | bh_unlock_sock(sk); |
| 2713 | return 0; |
| 2714 | } |
| 2715 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2716 | l2cap_chan_del(chan, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2717 | bh_unlock_sock(sk); |
| 2718 | |
| 2719 | l2cap_sock_kill(sk); |
| 2720 | return 0; |
| 2721 | } |
| 2722 | |
| 2723 | static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2724 | { |
| 2725 | struct l2cap_info_req *req = (struct l2cap_info_req *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | u16 type; |
| 2727 | |
| 2728 | type = __le16_to_cpu(req->type); |
| 2729 | |
| 2730 | BT_DBG("type 0x%4.4x", type); |
| 2731 | |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 2732 | if (type == L2CAP_IT_FEAT_MASK) { |
| 2733 | u8 buf[8]; |
Marcel Holtmann | 44dd46d | 2009-05-02 19:09:01 -0700 | [diff] [blame] | 2734 | u32 feat_mask = l2cap_feat_mask; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 2735 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
| 2736 | rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 2737 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 2738 | if (!disable_ertm) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2739 | feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING |
| 2740 | | L2CAP_FEAT_FCS; |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 2741 | put_unaligned_le32(feat_mask, rsp->data); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 2742 | l2cap_send_cmd(conn, cmd->ident, |
| 2743 | L2CAP_INFO_RSP, sizeof(buf), buf); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 2744 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
| 2745 | u8 buf[12]; |
| 2746 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
| 2747 | rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 2748 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
| 2749 | memcpy(buf + 4, l2cap_fixed_chan, 8); |
| 2750 | l2cap_send_cmd(conn, cmd->ident, |
| 2751 | L2CAP_INFO_RSP, sizeof(buf), buf); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 2752 | } else { |
| 2753 | struct l2cap_info_rsp rsp; |
| 2754 | rsp.type = cpu_to_le16(type); |
| 2755 | rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); |
| 2756 | l2cap_send_cmd(conn, cmd->ident, |
| 2757 | L2CAP_INFO_RSP, sizeof(rsp), &rsp); |
| 2758 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | |
| 2760 | return 0; |
| 2761 | } |
| 2762 | |
| 2763 | static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2764 | { |
| 2765 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; |
| 2766 | u16 type, result; |
| 2767 | |
| 2768 | type = __le16_to_cpu(rsp->type); |
| 2769 | result = __le16_to_cpu(rsp->result); |
| 2770 | |
| 2771 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); |
| 2772 | |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 2773 | /* L2CAP Info req/rsp are unbound to channels, add extra checks */ |
| 2774 | if (cmd->ident != conn->info_ident || |
| 2775 | conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
| 2776 | return 0; |
| 2777 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2778 | del_timer(&conn->info_timer); |
| 2779 | |
Ville Tervo | adb08ed | 2010-08-04 09:43:33 +0300 | [diff] [blame] | 2780 | if (result != L2CAP_IR_SUCCESS) { |
| 2781 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 2782 | conn->info_ident = 0; |
| 2783 | |
| 2784 | l2cap_conn_start(conn); |
| 2785 | |
| 2786 | return 0; |
| 2787 | } |
| 2788 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2789 | if (type == L2CAP_IT_FEAT_MASK) { |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 2790 | conn->feat_mask = get_unaligned_le32(rsp->data); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2791 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 2792 | if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 2793 | struct l2cap_info_req req; |
| 2794 | req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 2795 | |
| 2796 | conn->info_ident = l2cap_get_ident(conn); |
| 2797 | |
| 2798 | l2cap_send_cmd(conn, conn->info_ident, |
| 2799 | L2CAP_INFO_REQ, sizeof(req), &req); |
| 2800 | } else { |
| 2801 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 2802 | conn->info_ident = 0; |
| 2803 | |
| 2804 | l2cap_conn_start(conn); |
| 2805 | } |
| 2806 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2807 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 2808 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2809 | |
| 2810 | l2cap_conn_start(conn); |
| 2811 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | return 0; |
| 2814 | } |
| 2815 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 2816 | static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency, |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 2817 | u16 to_multiplier) |
| 2818 | { |
| 2819 | u16 max_latency; |
| 2820 | |
| 2821 | if (min > max || min < 6 || max > 3200) |
| 2822 | return -EINVAL; |
| 2823 | |
| 2824 | if (to_multiplier < 10 || to_multiplier > 3200) |
| 2825 | return -EINVAL; |
| 2826 | |
| 2827 | if (max >= to_multiplier * 8) |
| 2828 | return -EINVAL; |
| 2829 | |
| 2830 | max_latency = (to_multiplier * 8 / max) - 1; |
| 2831 | if (latency > 499 || latency > max_latency) |
| 2832 | return -EINVAL; |
| 2833 | |
| 2834 | return 0; |
| 2835 | } |
| 2836 | |
| 2837 | static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, |
| 2838 | struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2839 | { |
| 2840 | struct hci_conn *hcon = conn->hcon; |
| 2841 | struct l2cap_conn_param_update_req *req; |
| 2842 | struct l2cap_conn_param_update_rsp rsp; |
| 2843 | u16 min, max, latency, to_multiplier, cmd_len; |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 2844 | int err; |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 2845 | |
| 2846 | if (!(hcon->link_mode & HCI_LM_MASTER)) |
| 2847 | return -EINVAL; |
| 2848 | |
| 2849 | cmd_len = __le16_to_cpu(cmd->len); |
| 2850 | if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) |
| 2851 | return -EPROTO; |
| 2852 | |
| 2853 | req = (struct l2cap_conn_param_update_req *) data; |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 2854 | min = __le16_to_cpu(req->min); |
| 2855 | max = __le16_to_cpu(req->max); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 2856 | latency = __le16_to_cpu(req->latency); |
| 2857 | to_multiplier = __le16_to_cpu(req->to_multiplier); |
| 2858 | |
| 2859 | BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", |
| 2860 | min, max, latency, to_multiplier); |
| 2861 | |
| 2862 | memset(&rsp, 0, sizeof(rsp)); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 2863 | |
| 2864 | err = l2cap_check_conn_param(min, max, latency, to_multiplier); |
| 2865 | if (err) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 2866 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); |
| 2867 | else |
| 2868 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); |
| 2869 | |
| 2870 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, |
| 2871 | sizeof(rsp), &rsp); |
| 2872 | |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 2873 | if (!err) |
| 2874 | hci_le_conn_update(hcon, min, max, latency, to_multiplier); |
| 2875 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 2876 | return 0; |
| 2877 | } |
| 2878 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2879 | static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, |
| 2880 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
| 2881 | { |
| 2882 | int err = 0; |
| 2883 | |
| 2884 | switch (cmd->code) { |
| 2885 | case L2CAP_COMMAND_REJ: |
| 2886 | l2cap_command_rej(conn, cmd, data); |
| 2887 | break; |
| 2888 | |
| 2889 | case L2CAP_CONN_REQ: |
| 2890 | err = l2cap_connect_req(conn, cmd, data); |
| 2891 | break; |
| 2892 | |
| 2893 | case L2CAP_CONN_RSP: |
| 2894 | err = l2cap_connect_rsp(conn, cmd, data); |
| 2895 | break; |
| 2896 | |
| 2897 | case L2CAP_CONF_REQ: |
| 2898 | err = l2cap_config_req(conn, cmd, cmd_len, data); |
| 2899 | break; |
| 2900 | |
| 2901 | case L2CAP_CONF_RSP: |
| 2902 | err = l2cap_config_rsp(conn, cmd, data); |
| 2903 | break; |
| 2904 | |
| 2905 | case L2CAP_DISCONN_REQ: |
| 2906 | err = l2cap_disconnect_req(conn, cmd, data); |
| 2907 | break; |
| 2908 | |
| 2909 | case L2CAP_DISCONN_RSP: |
| 2910 | err = l2cap_disconnect_rsp(conn, cmd, data); |
| 2911 | break; |
| 2912 | |
| 2913 | case L2CAP_ECHO_REQ: |
| 2914 | l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); |
| 2915 | break; |
| 2916 | |
| 2917 | case L2CAP_ECHO_RSP: |
| 2918 | break; |
| 2919 | |
| 2920 | case L2CAP_INFO_REQ: |
| 2921 | err = l2cap_information_req(conn, cmd, data); |
| 2922 | break; |
| 2923 | |
| 2924 | case L2CAP_INFO_RSP: |
| 2925 | err = l2cap_information_rsp(conn, cmd, data); |
| 2926 | break; |
| 2927 | |
| 2928 | default: |
| 2929 | BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); |
| 2930 | err = -EINVAL; |
| 2931 | break; |
| 2932 | } |
| 2933 | |
| 2934 | return err; |
| 2935 | } |
| 2936 | |
| 2937 | static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, |
| 2938 | struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2939 | { |
| 2940 | switch (cmd->code) { |
| 2941 | case L2CAP_COMMAND_REJ: |
| 2942 | return 0; |
| 2943 | |
| 2944 | case L2CAP_CONN_PARAM_UPDATE_REQ: |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 2945 | return l2cap_conn_param_update_req(conn, cmd, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2946 | |
| 2947 | case L2CAP_CONN_PARAM_UPDATE_RSP: |
| 2948 | return 0; |
| 2949 | |
| 2950 | default: |
| 2951 | BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); |
| 2952 | return -EINVAL; |
| 2953 | } |
| 2954 | } |
| 2955 | |
| 2956 | static inline void l2cap_sig_channel(struct l2cap_conn *conn, |
| 2957 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | { |
| 2959 | u8 *data = skb->data; |
| 2960 | int len = skb->len; |
| 2961 | struct l2cap_cmd_hdr cmd; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2962 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | |
| 2964 | l2cap_raw_recv(conn, skb); |
| 2965 | |
| 2966 | while (len >= L2CAP_CMD_HDR_SIZE) { |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2967 | u16 cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 2969 | data += L2CAP_CMD_HDR_SIZE; |
| 2970 | len -= L2CAP_CMD_HDR_SIZE; |
| 2971 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2972 | cmd_len = le16_to_cpu(cmd.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2973 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2974 | BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2976 | if (cmd_len > len || !cmd.ident) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | BT_DBG("corrupted command"); |
| 2978 | break; |
| 2979 | } |
| 2980 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2981 | if (conn->hcon->type == LE_LINK) |
| 2982 | err = l2cap_le_sig_cmd(conn, &cmd, data); |
| 2983 | else |
| 2984 | err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2985 | |
| 2986 | if (err) { |
| 2987 | struct l2cap_cmd_rej rej; |
Gustavo F. Padovan | 2c6d1a2 | 2011-03-23 14:38:32 -0300 | [diff] [blame] | 2988 | |
| 2989 | BT_ERR("Wrong link type (%d)", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | |
| 2991 | /* FIXME: Map err to a valid reason */ |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2992 | rej.reason = cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); |
| 2994 | } |
| 2995 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2996 | data += cmd_len; |
| 2997 | len -= cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | kfree_skb(skb); |
| 3001 | } |
| 3002 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3003 | static int l2cap_check_fcs(struct l2cap_chan *chan, struct sk_buff *skb) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3004 | { |
| 3005 | u16 our_fcs, rcv_fcs; |
| 3006 | int hdr_size = L2CAP_HDR_SIZE + 2; |
| 3007 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3008 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3009 | skb_trim(skb, skb->len - 2); |
| 3010 | rcv_fcs = get_unaligned_le16(skb->data + skb->len); |
| 3011 | our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); |
| 3012 | |
| 3013 | if (our_fcs != rcv_fcs) |
João Paulo Rechi Vita | 7a560e5 | 2010-06-22 13:56:27 -0300 | [diff] [blame] | 3014 | return -EBADMSG; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3015 | } |
| 3016 | return 0; |
| 3017 | } |
| 3018 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3019 | static inline void l2cap_send_i_or_rr_or_rnr(struct l2cap_chan *chan) |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3020 | { |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3021 | u16 control = 0; |
| 3022 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3023 | chan->frames_sent = 0; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3024 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3025 | control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3026 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3027 | if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { |
Gustavo F. Padovan | 6498886 | 2010-05-10 14:54:14 -0300 | [diff] [blame] | 3028 | control |= L2CAP_SUPER_RCV_NOT_READY; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3029 | l2cap_send_sframe(chan, control); |
| 3030 | chan->conn_state |= L2CAP_CONN_RNR_SENT; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3031 | } |
| 3032 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3033 | if (chan->conn_state & L2CAP_CONN_REMOTE_BUSY) |
| 3034 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3035 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3036 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3037 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3038 | if (!(chan->conn_state & L2CAP_CONN_LOCAL_BUSY) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3039 | chan->frames_sent == 0) { |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3040 | control |= L2CAP_SUPER_RCV_READY; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3041 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3042 | } |
| 3043 | } |
| 3044 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3045 | static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u8 tx_seq, u8 sar) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3046 | { |
| 3047 | struct sk_buff *next_skb; |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 3048 | int tx_seq_offset, next_tx_seq_offset; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3049 | |
| 3050 | bt_cb(skb)->tx_seq = tx_seq; |
| 3051 | bt_cb(skb)->sar = sar; |
| 3052 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3053 | next_skb = skb_peek(&chan->srej_q); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3054 | if (!next_skb) { |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3055 | __skb_queue_tail(&chan->srej_q, skb); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3056 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3057 | } |
| 3058 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3059 | tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 3060 | if (tx_seq_offset < 0) |
| 3061 | tx_seq_offset += 64; |
| 3062 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3063 | do { |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3064 | if (bt_cb(next_skb)->tx_seq == tx_seq) |
| 3065 | return -EINVAL; |
| 3066 | |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 3067 | next_tx_seq_offset = (bt_cb(next_skb)->tx_seq - |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3068 | chan->buffer_seq) % 64; |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 3069 | if (next_tx_seq_offset < 0) |
| 3070 | next_tx_seq_offset += 64; |
| 3071 | |
| 3072 | if (next_tx_seq_offset > tx_seq_offset) { |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3073 | __skb_queue_before(&chan->srej_q, next_skb, skb); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3074 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3075 | } |
| 3076 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3077 | if (skb_queue_is_last(&chan->srej_q, next_skb)) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3078 | break; |
| 3079 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3080 | } while ((next_skb = skb_queue_next(&chan->srej_q, next_skb))); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3081 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3082 | __skb_queue_tail(&chan->srej_q, skb); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3083 | |
| 3084 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3085 | } |
| 3086 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3087 | static int l2cap_ertm_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3088 | { |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3089 | struct sk_buff *_skb; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3090 | int err; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3091 | |
| 3092 | switch (control & L2CAP_CTRL_SAR) { |
| 3093 | case L2CAP_SDU_UNSEGMENTED: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3094 | if (chan->conn_state & L2CAP_CONN_SAR_SDU) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3095 | goto drop; |
| 3096 | |
Ruiyi Zhang | 54f6151 | 2011-05-13 13:07:52 +0800 | [diff] [blame] | 3097 | return sock_queue_rcv_skb(chan->sk, skb); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3098 | |
| 3099 | case L2CAP_SDU_START: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3100 | if (chan->conn_state & L2CAP_CONN_SAR_SDU) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3101 | goto drop; |
| 3102 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3103 | chan->sdu_len = get_unaligned_le16(skb->data); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3104 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3105 | if (chan->sdu_len > chan->imtu) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3106 | goto disconnect; |
| 3107 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3108 | chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC); |
| 3109 | if (!chan->sdu) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3110 | return -ENOMEM; |
| 3111 | |
| 3112 | /* pull sdu_len bytes only after alloc, because of Local Busy |
| 3113 | * condition we have to be sure that this will be executed |
| 3114 | * only once, i.e., when alloc does not fail */ |
| 3115 | skb_pull(skb, 2); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3116 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3117 | memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3118 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3119 | chan->conn_state |= L2CAP_CONN_SAR_SDU; |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3120 | chan->partial_sdu_len = skb->len; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3121 | break; |
| 3122 | |
| 3123 | case L2CAP_SDU_CONTINUE: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3124 | if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3125 | goto disconnect; |
| 3126 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3127 | if (!chan->sdu) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3128 | goto disconnect; |
| 3129 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3130 | chan->partial_sdu_len += skb->len; |
| 3131 | if (chan->partial_sdu_len > chan->sdu_len) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3132 | goto drop; |
| 3133 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3134 | memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3135 | |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3136 | break; |
| 3137 | |
| 3138 | case L2CAP_SDU_END: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3139 | if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3140 | goto disconnect; |
| 3141 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3142 | if (!chan->sdu) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3143 | goto disconnect; |
| 3144 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3145 | if (!(chan->conn_state & L2CAP_CONN_SAR_RETRY)) { |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3146 | chan->partial_sdu_len += skb->len; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3147 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3148 | if (chan->partial_sdu_len > chan->imtu) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3149 | goto drop; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3150 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3151 | if (chan->partial_sdu_len != chan->sdu_len) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3152 | goto drop; |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3153 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3154 | memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3155 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3156 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3157 | _skb = skb_clone(chan->sdu, GFP_ATOMIC); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3158 | if (!_skb) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3159 | chan->conn_state |= L2CAP_CONN_SAR_RETRY; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3160 | return -ENOMEM; |
| 3161 | } |
| 3162 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3163 | err = sock_queue_rcv_skb(chan->sk, _skb); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3164 | if (err < 0) { |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3165 | kfree_skb(_skb); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3166 | chan->conn_state |= L2CAP_CONN_SAR_RETRY; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3167 | return err; |
| 3168 | } |
| 3169 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3170 | chan->conn_state &= ~L2CAP_CONN_SAR_RETRY; |
| 3171 | chan->conn_state &= ~L2CAP_CONN_SAR_SDU; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3172 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3173 | kfree_skb(chan->sdu); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3174 | break; |
| 3175 | } |
| 3176 | |
| 3177 | kfree_skb(skb); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3178 | return 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3179 | |
| 3180 | drop: |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3181 | kfree_skb(chan->sdu); |
| 3182 | chan->sdu = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3183 | |
| 3184 | disconnect: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3185 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3186 | kfree_skb(skb); |
| 3187 | return 0; |
| 3188 | } |
| 3189 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3190 | static int l2cap_try_push_rx_skb(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3191 | { |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3192 | struct sk_buff *skb; |
| 3193 | u16 control; |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3194 | int err; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3195 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3196 | while ((skb = skb_dequeue(&chan->busy_q))) { |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3197 | control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3198 | err = l2cap_ertm_reassembly_sdu(chan, skb, control); |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3199 | if (err < 0) { |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3200 | skb_queue_head(&chan->busy_q, skb); |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3201 | return -EBUSY; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3202 | } |
| 3203 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3204 | chan->buffer_seq = (chan->buffer_seq + 1) % 64; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3205 | } |
| 3206 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3207 | if (!(chan->conn_state & L2CAP_CONN_RNR_SENT)) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3208 | goto done; |
| 3209 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3210 | control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3211 | control |= L2CAP_SUPER_RCV_READY | L2CAP_CTRL_POLL; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3212 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3213 | chan->retry_count = 1; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3214 | |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3215 | del_timer(&chan->retrans_timer); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3216 | __mod_monitor_timer(); |
| 3217 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3218 | chan->conn_state |= L2CAP_CONN_WAIT_F; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3219 | |
| 3220 | done: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3221 | chan->conn_state &= ~L2CAP_CONN_LOCAL_BUSY; |
| 3222 | chan->conn_state &= ~L2CAP_CONN_RNR_SENT; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3223 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3224 | BT_DBG("chan %p, Exit local busy", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3225 | |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3226 | return 0; |
| 3227 | } |
| 3228 | |
| 3229 | static void l2cap_busy_work(struct work_struct *work) |
| 3230 | { |
| 3231 | DECLARE_WAITQUEUE(wait, current); |
Gustavo F. Padovan | 311bb89 | 2011-03-25 20:41:00 -0300 | [diff] [blame] | 3232 | struct l2cap_chan *chan = |
| 3233 | container_of(work, struct l2cap_chan, busy_work); |
| 3234 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3235 | int n_tries = 0, timeo = HZ/5, err; |
| 3236 | struct sk_buff *skb; |
| 3237 | |
| 3238 | lock_sock(sk); |
| 3239 | |
| 3240 | add_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 311bb89 | 2011-03-25 20:41:00 -0300 | [diff] [blame] | 3241 | while ((skb = skb_peek(&chan->busy_q))) { |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3242 | set_current_state(TASK_INTERRUPTIBLE); |
| 3243 | |
| 3244 | if (n_tries++ > L2CAP_LOCAL_BUSY_TRIES) { |
| 3245 | err = -EBUSY; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3246 | l2cap_send_disconn_req(chan->conn, chan, EBUSY); |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3247 | break; |
| 3248 | } |
| 3249 | |
| 3250 | if (!timeo) |
| 3251 | timeo = HZ/5; |
| 3252 | |
| 3253 | if (signal_pending(current)) { |
| 3254 | err = sock_intr_errno(timeo); |
| 3255 | break; |
| 3256 | } |
| 3257 | |
| 3258 | release_sock(sk); |
| 3259 | timeo = schedule_timeout(timeo); |
| 3260 | lock_sock(sk); |
| 3261 | |
| 3262 | err = sock_error(sk); |
| 3263 | if (err) |
| 3264 | break; |
| 3265 | |
Gustavo F. Padovan | 311bb89 | 2011-03-25 20:41:00 -0300 | [diff] [blame] | 3266 | if (l2cap_try_push_rx_skb(chan) == 0) |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3267 | break; |
| 3268 | } |
| 3269 | |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3270 | set_current_state(TASK_RUNNING); |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 3271 | remove_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3272 | |
| 3273 | release_sock(sk); |
| 3274 | } |
| 3275 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3276 | static int l2cap_push_rx_skb(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3277 | { |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3278 | int sctrl, err; |
| 3279 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3280 | if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3281 | bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT; |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3282 | __skb_queue_tail(&chan->busy_q, skb); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3283 | return l2cap_try_push_rx_skb(chan); |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3284 | |
| 3285 | |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3286 | } |
| 3287 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3288 | err = l2cap_ertm_reassembly_sdu(chan, skb, control); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3289 | if (err >= 0) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3290 | chan->buffer_seq = (chan->buffer_seq + 1) % 64; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3291 | return err; |
| 3292 | } |
| 3293 | |
| 3294 | /* Busy Condition */ |
Gustavo F. Padovan | 311bb89 | 2011-03-25 20:41:00 -0300 | [diff] [blame] | 3295 | BT_DBG("chan %p, Enter local busy", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3296 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3297 | chan->conn_state |= L2CAP_CONN_LOCAL_BUSY; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3298 | bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT; |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3299 | __skb_queue_tail(&chan->busy_q, skb); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3300 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3301 | sctrl = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3302 | sctrl |= L2CAP_SUPER_RCV_NOT_READY; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3303 | l2cap_send_sframe(chan, sctrl); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3304 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3305 | chan->conn_state |= L2CAP_CONN_RNR_SENT; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3306 | |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3307 | del_timer(&chan->ack_timer); |
Gustavo F. Padovan | 7fe9b29 | 2010-05-12 18:32:04 -0300 | [diff] [blame] | 3308 | |
Gustavo F. Padovan | 311bb89 | 2011-03-25 20:41:00 -0300 | [diff] [blame] | 3309 | queue_work(_busy_wq, &chan->busy_work); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3310 | |
| 3311 | return err; |
| 3312 | } |
| 3313 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3314 | static int l2cap_streaming_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3315 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3316 | struct sk_buff *_skb; |
| 3317 | int err = -EINVAL; |
| 3318 | |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3319 | /* |
| 3320 | * TODO: We have to notify the userland if some data is lost with the |
| 3321 | * Streaming Mode. |
| 3322 | */ |
| 3323 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3324 | switch (control & L2CAP_CTRL_SAR) { |
| 3325 | case L2CAP_SDU_UNSEGMENTED: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3326 | if (chan->conn_state & L2CAP_CONN_SAR_SDU) { |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3327 | kfree_skb(chan->sdu); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3328 | break; |
| 3329 | } |
| 3330 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3331 | err = sock_queue_rcv_skb(chan->sk, skb); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3332 | if (!err) |
| 3333 | return 0; |
| 3334 | |
| 3335 | break; |
| 3336 | |
| 3337 | case L2CAP_SDU_START: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3338 | if (chan->conn_state & L2CAP_CONN_SAR_SDU) { |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3339 | kfree_skb(chan->sdu); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3340 | break; |
| 3341 | } |
| 3342 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3343 | chan->sdu_len = get_unaligned_le16(skb->data); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3344 | skb_pull(skb, 2); |
| 3345 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3346 | if (chan->sdu_len > chan->imtu) { |
Gustavo F. Padovan | 052897c | 2010-05-01 16:15:40 -0300 | [diff] [blame] | 3347 | err = -EMSGSIZE; |
| 3348 | break; |
| 3349 | } |
| 3350 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3351 | chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC); |
| 3352 | if (!chan->sdu) { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3353 | err = -ENOMEM; |
| 3354 | break; |
| 3355 | } |
| 3356 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3357 | memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3358 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3359 | chan->conn_state |= L2CAP_CONN_SAR_SDU; |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3360 | chan->partial_sdu_len = skb->len; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3361 | err = 0; |
| 3362 | break; |
| 3363 | |
| 3364 | case L2CAP_SDU_CONTINUE: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3365 | if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3366 | break; |
| 3367 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3368 | memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3369 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3370 | chan->partial_sdu_len += skb->len; |
| 3371 | if (chan->partial_sdu_len > chan->sdu_len) |
| 3372 | kfree_skb(chan->sdu); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3373 | else |
| 3374 | err = 0; |
| 3375 | |
| 3376 | break; |
| 3377 | |
| 3378 | case L2CAP_SDU_END: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3379 | if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3380 | break; |
| 3381 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3382 | memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3383 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3384 | chan->conn_state &= ~L2CAP_CONN_SAR_SDU; |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3385 | chan->partial_sdu_len += skb->len; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3386 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3387 | if (chan->partial_sdu_len > chan->imtu) |
Gustavo F. Padovan | 36f2fd5 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3388 | goto drop; |
| 3389 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3390 | if (chan->partial_sdu_len == chan->sdu_len) { |
| 3391 | _skb = skb_clone(chan->sdu, GFP_ATOMIC); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3392 | err = sock_queue_rcv_skb(chan->sk, _skb); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3393 | if (err < 0) |
| 3394 | kfree_skb(_skb); |
| 3395 | } |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3396 | err = 0; |
| 3397 | |
Gustavo F. Padovan | 36f2fd5 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3398 | drop: |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3399 | kfree_skb(chan->sdu); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3400 | break; |
| 3401 | } |
| 3402 | |
| 3403 | kfree_skb(skb); |
| 3404 | return err; |
| 3405 | } |
| 3406 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3407 | static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3408 | { |
| 3409 | struct sk_buff *skb; |
Gustavo F. Padovan | afefdbc | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 3410 | u16 control; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3411 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3412 | while ((skb = skb_peek(&chan->srej_q))) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3413 | if (bt_cb(skb)->tx_seq != tx_seq) |
| 3414 | break; |
| 3415 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3416 | skb = skb_dequeue(&chan->srej_q); |
Gustavo F. Padovan | afefdbc | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 3417 | control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3418 | l2cap_ertm_reassembly_sdu(chan, skb, control); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3419 | chan->buffer_seq_srej = |
| 3420 | (chan->buffer_seq_srej + 1) % 64; |
Gustavo F. Padovan | 8ff50ec | 2010-05-10 19:34:11 -0300 | [diff] [blame] | 3421 | tx_seq = (tx_seq + 1) % 64; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3422 | } |
| 3423 | } |
| 3424 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3425 | static void l2cap_resend_srejframe(struct l2cap_chan *chan, u8 tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3426 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3427 | struct srej_list *l, *tmp; |
| 3428 | u16 control; |
| 3429 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3430 | list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3431 | if (l->tx_seq == tx_seq) { |
| 3432 | list_del(&l->list); |
| 3433 | kfree(l); |
| 3434 | return; |
| 3435 | } |
| 3436 | control = L2CAP_SUPER_SELECT_REJECT; |
| 3437 | control |= l->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3438 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3439 | list_del(&l->list); |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3440 | list_add_tail(&l->list, &chan->srej_l); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3441 | } |
| 3442 | } |
| 3443 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3444 | static void l2cap_send_srejframe(struct l2cap_chan *chan, u8 tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3445 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3446 | struct srej_list *new; |
| 3447 | u16 control; |
| 3448 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3449 | while (tx_seq != chan->expected_tx_seq) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3450 | control = L2CAP_SUPER_SELECT_REJECT; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3451 | control |= chan->expected_tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3452 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3453 | |
| 3454 | new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3455 | new->tx_seq = chan->expected_tx_seq; |
| 3456 | chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3457 | list_add_tail(&new->list, &chan->srej_l); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3458 | } |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3459 | chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3460 | } |
| 3461 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3462 | static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3463 | { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3464 | u8 tx_seq = __get_txseq(rx_control); |
Gustavo F. Padovan | 9f121a5 | 2009-10-03 02:34:38 -0300 | [diff] [blame] | 3465 | u8 req_seq = __get_reqseq(rx_control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3466 | u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT; |
Gustavo F. Padovan | f6337c7 | 2010-05-10 18:32:04 -0300 | [diff] [blame] | 3467 | int tx_seq_offset, expected_tx_seq_offset; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3468 | int num_to_ack = (chan->tx_win/6) + 1; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3469 | int err = 0; |
| 3470 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3471 | BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len, |
| 3472 | tx_seq, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3473 | |
Gustavo F. Padovan | 9b16dc6 | 2010-05-05 20:05:57 -0300 | [diff] [blame] | 3474 | if (L2CAP_CTRL_FINAL & rx_control && |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3475 | chan->conn_state & L2CAP_CONN_WAIT_F) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3476 | del_timer(&chan->monitor_timer); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3477 | if (chan->unacked_frames > 0) |
Gustavo F. Padovan | 1d8f5d1 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3478 | __mod_retrans_timer(); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3479 | chan->conn_state &= ~L2CAP_CONN_WAIT_F; |
Gustavo F. Padovan | 1d8f5d1 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3480 | } |
| 3481 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3482 | chan->expected_ack_seq = req_seq; |
| 3483 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 9f121a5 | 2009-10-03 02:34:38 -0300 | [diff] [blame] | 3484 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3485 | if (tx_seq == chan->expected_tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3486 | goto expected; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3487 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3488 | tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3489 | if (tx_seq_offset < 0) |
| 3490 | tx_seq_offset += 64; |
| 3491 | |
| 3492 | /* invalid tx_seq */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3493 | if (tx_seq_offset >= chan->tx_win) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3494 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3495 | goto drop; |
| 3496 | } |
| 3497 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3498 | if (chan->conn_state == L2CAP_CONN_LOCAL_BUSY) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3499 | goto drop; |
| 3500 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3501 | if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3502 | struct srej_list *first; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3503 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3504 | first = list_first_entry(&chan->srej_l, |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3505 | struct srej_list, list); |
| 3506 | if (tx_seq == first->tx_seq) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3507 | l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3508 | l2cap_check_srej_gap(chan, tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3509 | |
| 3510 | list_del(&first->list); |
| 3511 | kfree(first); |
| 3512 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3513 | if (list_empty(&chan->srej_l)) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3514 | chan->buffer_seq = chan->buffer_seq_srej; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3515 | chan->conn_state &= ~L2CAP_CONN_SREJ_SENT; |
| 3516 | l2cap_send_ack(chan); |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3517 | BT_DBG("chan %p, Exit SREJ_SENT", chan); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3518 | } |
| 3519 | } else { |
| 3520 | struct srej_list *l; |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3521 | |
| 3522 | /* duplicated tx_seq */ |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3523 | if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0) |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3524 | goto drop; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3525 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3526 | list_for_each_entry(l, &chan->srej_l, list) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3527 | if (l->tx_seq == tx_seq) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3528 | l2cap_resend_srejframe(chan, tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3529 | return 0; |
| 3530 | } |
| 3531 | } |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3532 | l2cap_send_srejframe(chan, tx_seq); |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 3533 | } |
| 3534 | } else { |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3535 | expected_tx_seq_offset = |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3536 | (chan->expected_tx_seq - chan->buffer_seq) % 64; |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3537 | if (expected_tx_seq_offset < 0) |
| 3538 | expected_tx_seq_offset += 64; |
| 3539 | |
| 3540 | /* duplicated tx_seq */ |
| 3541 | if (tx_seq_offset < expected_tx_seq_offset) |
| 3542 | goto drop; |
| 3543 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3544 | chan->conn_state |= L2CAP_CONN_SREJ_SENT; |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 3545 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3546 | BT_DBG("chan %p, Enter SREJ", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3547 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3548 | INIT_LIST_HEAD(&chan->srej_l); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3549 | chan->buffer_seq_srej = chan->buffer_seq; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3550 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3551 | __skb_queue_head_init(&chan->srej_q); |
| 3552 | __skb_queue_head_init(&chan->busy_q); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3553 | l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3554 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3555 | chan->conn_state |= L2CAP_CONN_SEND_PBIT; |
Gustavo F. Padovan | ef54fd9 | 2009-08-20 22:26:04 -0300 | [diff] [blame] | 3556 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3557 | l2cap_send_srejframe(chan, tx_seq); |
Gustavo F. Padovan | 7fe9b29 | 2010-05-12 18:32:04 -0300 | [diff] [blame] | 3558 | |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3559 | del_timer(&chan->ack_timer); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3560 | } |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 3561 | return 0; |
| 3562 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3563 | expected: |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3564 | chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3565 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3566 | if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { |
Gustavo F. Padovan | 3b1a9f3 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 3567 | bt_cb(skb)->tx_seq = tx_seq; |
| 3568 | bt_cb(skb)->sar = sar; |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3569 | __skb_queue_tail(&chan->srej_q, skb); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3570 | return 0; |
| 3571 | } |
| 3572 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3573 | err = l2cap_push_rx_skb(chan, skb, rx_control); |
Gustavo F. Padovan | 2ece368 | 2010-06-16 17:21:44 -0300 | [diff] [blame] | 3574 | if (err < 0) |
| 3575 | return 0; |
| 3576 | |
Gustavo F. Padovan | 4ec10d9 | 2009-10-03 02:34:39 -0300 | [diff] [blame] | 3577 | if (rx_control & L2CAP_CTRL_FINAL) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3578 | if (chan->conn_state & L2CAP_CONN_REJ_ACT) |
| 3579 | chan->conn_state &= ~L2CAP_CONN_REJ_ACT; |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3580 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3581 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | 4ec10d9 | 2009-10-03 02:34:39 -0300 | [diff] [blame] | 3582 | } |
| 3583 | |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3584 | __mod_ack_timer(); |
| 3585 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3586 | chan->num_acked = (chan->num_acked + 1) % num_to_ack; |
| 3587 | if (chan->num_acked == num_to_ack - 1) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3588 | l2cap_send_ack(chan); |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3589 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3590 | return 0; |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3591 | |
| 3592 | drop: |
| 3593 | kfree_skb(skb); |
| 3594 | return 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3595 | } |
| 3596 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3597 | static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3598 | { |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3599 | BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, __get_reqseq(rx_control), |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3600 | rx_control); |
| 3601 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3602 | chan->expected_ack_seq = __get_reqseq(rx_control); |
| 3603 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3604 | |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3605 | if (rx_control & L2CAP_CTRL_POLL) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3606 | chan->conn_state |= L2CAP_CONN_SEND_FBIT; |
| 3607 | if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { |
| 3608 | if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3609 | (chan->unacked_frames > 0)) |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3610 | __mod_retrans_timer(); |
| 3611 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3612 | chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; |
| 3613 | l2cap_send_srejtail(chan); |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3614 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3615 | l2cap_send_i_or_rr_or_rnr(chan); |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3616 | } |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3617 | |
| 3618 | } else if (rx_control & L2CAP_CTRL_FINAL) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3619 | chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3620 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3621 | if (chan->conn_state & L2CAP_CONN_REJ_ACT) |
| 3622 | chan->conn_state &= ~L2CAP_CONN_REJ_ACT; |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3623 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3624 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3625 | |
| 3626 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3627 | if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3628 | (chan->unacked_frames > 0)) |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3629 | __mod_retrans_timer(); |
| 3630 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3631 | chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; |
| 3632 | if (chan->conn_state & L2CAP_CONN_SREJ_SENT) |
| 3633 | l2cap_send_ack(chan); |
Andrei Emeltchenko | 894718a | 2010-12-01 16:58:24 +0200 | [diff] [blame] | 3634 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3635 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3636 | } |
| 3637 | } |
| 3638 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3639 | static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control) |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3640 | { |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3641 | u8 tx_seq = __get_reqseq(rx_control); |
| 3642 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3643 | BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3644 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3645 | chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3646 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3647 | chan->expected_ack_seq = tx_seq; |
| 3648 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3649 | |
| 3650 | if (rx_control & L2CAP_CTRL_FINAL) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3651 | if (chan->conn_state & L2CAP_CONN_REJ_ACT) |
| 3652 | chan->conn_state &= ~L2CAP_CONN_REJ_ACT; |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3653 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3654 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3655 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3656 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3657 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3658 | if (chan->conn_state & L2CAP_CONN_WAIT_F) |
| 3659 | chan->conn_state |= L2CAP_CONN_REJ_ACT; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3660 | } |
| 3661 | } |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3662 | static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control) |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3663 | { |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3664 | u8 tx_seq = __get_reqseq(rx_control); |
| 3665 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3666 | BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3667 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3668 | chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3669 | |
| 3670 | if (rx_control & L2CAP_CTRL_POLL) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3671 | chan->expected_ack_seq = tx_seq; |
| 3672 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 3673 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3674 | chan->conn_state |= L2CAP_CONN_SEND_FBIT; |
| 3675 | l2cap_retransmit_one_frame(chan, tx_seq); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3676 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3677 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3678 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3679 | if (chan->conn_state & L2CAP_CONN_WAIT_F) { |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3680 | chan->srej_save_reqseq = tx_seq; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3681 | chan->conn_state |= L2CAP_CONN_SREJ_ACT; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3682 | } |
| 3683 | } else if (rx_control & L2CAP_CTRL_FINAL) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3684 | if ((chan->conn_state & L2CAP_CONN_SREJ_ACT) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3685 | chan->srej_save_reqseq == tx_seq) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3686 | chan->conn_state &= ~L2CAP_CONN_SREJ_ACT; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3687 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3688 | l2cap_retransmit_one_frame(chan, tx_seq); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3689 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3690 | l2cap_retransmit_one_frame(chan, tx_seq); |
| 3691 | if (chan->conn_state & L2CAP_CONN_WAIT_F) { |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3692 | chan->srej_save_reqseq = tx_seq; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3693 | chan->conn_state |= L2CAP_CONN_SREJ_ACT; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3694 | } |
| 3695 | } |
| 3696 | } |
| 3697 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3698 | static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control) |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3699 | { |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3700 | u8 tx_seq = __get_reqseq(rx_control); |
| 3701 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3702 | BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3703 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3704 | chan->conn_state |= L2CAP_CONN_REMOTE_BUSY; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3705 | chan->expected_ack_seq = tx_seq; |
| 3706 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3707 | |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 3708 | if (rx_control & L2CAP_CTRL_POLL) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3709 | chan->conn_state |= L2CAP_CONN_SEND_FBIT; |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 3710 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3711 | if (!(chan->conn_state & L2CAP_CONN_SREJ_SENT)) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3712 | del_timer(&chan->retrans_timer); |
Gustavo F. Padovan | a2e12a2 | 2010-05-05 19:58:27 -0300 | [diff] [blame] | 3713 | if (rx_control & L2CAP_CTRL_POLL) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3714 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3715 | return; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3716 | } |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3717 | |
| 3718 | if (rx_control & L2CAP_CTRL_POLL) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3719 | l2cap_send_srejtail(chan); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3720 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3721 | l2cap_send_sframe(chan, L2CAP_SUPER_RCV_READY); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3722 | } |
| 3723 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3724 | static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3725 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3726 | BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3727 | |
Gustavo F. Padovan | 9b16dc6 | 2010-05-05 20:05:57 -0300 | [diff] [blame] | 3728 | if (L2CAP_CTRL_FINAL & rx_control && |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3729 | chan->conn_state & L2CAP_CONN_WAIT_F) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3730 | del_timer(&chan->monitor_timer); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3731 | if (chan->unacked_frames > 0) |
Gustavo F. Padovan | 1d8f5d1 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3732 | __mod_retrans_timer(); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3733 | chan->conn_state &= ~L2CAP_CONN_WAIT_F; |
Gustavo F. Padovan | 1d8f5d1 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3734 | } |
| 3735 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3736 | switch (rx_control & L2CAP_CTRL_SUPERVISE) { |
| 3737 | case L2CAP_SUPER_RCV_READY: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3738 | l2cap_data_channel_rrframe(chan, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3739 | break; |
| 3740 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3741 | case L2CAP_SUPER_REJECT: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3742 | l2cap_data_channel_rejframe(chan, rx_control); |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 3743 | break; |
| 3744 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3745 | case L2CAP_SUPER_SELECT_REJECT: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3746 | l2cap_data_channel_srejframe(chan, rx_control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3747 | break; |
| 3748 | |
| 3749 | case L2CAP_SUPER_RCV_NOT_READY: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3750 | l2cap_data_channel_rnrframe(chan, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3751 | break; |
| 3752 | } |
| 3753 | |
Gustavo F. Padovan | faaebd1 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 3754 | kfree_skb(skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3755 | return 0; |
| 3756 | } |
| 3757 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3758 | static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb) |
| 3759 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3760 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3761 | u16 control; |
| 3762 | u8 req_seq; |
| 3763 | int len, next_tx_seq_offset, req_seq_offset; |
| 3764 | |
| 3765 | control = get_unaligned_le16(skb->data); |
| 3766 | skb_pull(skb, 2); |
| 3767 | len = skb->len; |
| 3768 | |
| 3769 | /* |
| 3770 | * We can just drop the corrupted I-frame here. |
| 3771 | * Receiver will miss it and start proper recovery |
| 3772 | * procedures and ask retransmission. |
| 3773 | */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3774 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3775 | goto drop; |
| 3776 | |
| 3777 | if (__is_sar_start(control) && __is_iframe(control)) |
| 3778 | len -= 2; |
| 3779 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3780 | if (chan->fcs == L2CAP_FCS_CRC16) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3781 | len -= 2; |
| 3782 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3783 | if (len > chan->mps) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3784 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3785 | goto drop; |
| 3786 | } |
| 3787 | |
| 3788 | req_seq = __get_reqseq(control); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3789 | req_seq_offset = (req_seq - chan->expected_ack_seq) % 64; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3790 | if (req_seq_offset < 0) |
| 3791 | req_seq_offset += 64; |
| 3792 | |
| 3793 | next_tx_seq_offset = |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3794 | (chan->next_tx_seq - chan->expected_ack_seq) % 64; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3795 | if (next_tx_seq_offset < 0) |
| 3796 | next_tx_seq_offset += 64; |
| 3797 | |
| 3798 | /* check for invalid req-seq */ |
| 3799 | if (req_seq_offset > next_tx_seq_offset) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3800 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3801 | goto drop; |
| 3802 | } |
| 3803 | |
| 3804 | if (__is_iframe(control)) { |
| 3805 | if (len < 0) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3806 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3807 | goto drop; |
| 3808 | } |
| 3809 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3810 | l2cap_data_channel_iframe(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3811 | } else { |
| 3812 | if (len != 0) { |
| 3813 | BT_ERR("%d", len); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3814 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3815 | goto drop; |
| 3816 | } |
| 3817 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3818 | l2cap_data_channel_sframe(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | return 0; |
| 3822 | |
| 3823 | drop: |
| 3824 | kfree_skb(skb); |
| 3825 | return 0; |
| 3826 | } |
| 3827 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) |
| 3829 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3830 | struct l2cap_chan *chan; |
David S. Miller | bf73484 | 2011-04-25 13:03:02 -0700 | [diff] [blame] | 3831 | struct sock *sk = NULL; |
Nathan Holstein | 51893f8 | 2010-06-09 15:46:25 -0400 | [diff] [blame] | 3832 | u16 control; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3833 | u8 tx_seq; |
| 3834 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3835 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 3836 | chan = l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3837 | if (!chan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | BT_DBG("unknown cid 0x%4.4x", cid); |
| 3839 | goto drop; |
| 3840 | } |
| 3841 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3842 | sk = chan->sk; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3843 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3844 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3845 | |
| 3846 | if (sk->sk_state != BT_CONNECTED) |
| 3847 | goto drop; |
| 3848 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3849 | switch (chan->mode) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3850 | case L2CAP_MODE_BASIC: |
| 3851 | /* If socket recv buffers overflows we drop data here |
| 3852 | * which is *bad* because L2CAP has to be reliable. |
| 3853 | * But we don't have any other choice. L2CAP doesn't |
| 3854 | * provide flow control mechanism. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3856 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3857 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3858 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3859 | if (!sock_queue_rcv_skb(sk, skb)) |
| 3860 | goto done; |
| 3861 | break; |
| 3862 | |
| 3863 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3864 | if (!sock_owned_by_user(sk)) { |
| 3865 | l2cap_ertm_data_rcv(sk, skb); |
Gustavo F. Padovan | 277ffbe | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3866 | } else { |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3867 | if (sk_add_backlog(sk, skb)) |
Gustavo F. Padovan | 277ffbe | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3868 | goto drop; |
Gustavo F. Padovan | 277ffbe | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3869 | } |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3870 | |
Andrei Emeltchenko | fcafde2 | 2009-12-22 15:58:08 +0200 | [diff] [blame] | 3871 | goto done; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3872 | |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3873 | case L2CAP_MODE_STREAMING: |
| 3874 | control = get_unaligned_le16(skb->data); |
| 3875 | skb_pull(skb, 2); |
| 3876 | len = skb->len; |
| 3877 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3878 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 2600008 | 2010-05-11 22:02:00 -0300 | [diff] [blame] | 3879 | goto drop; |
| 3880 | |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3881 | if (__is_sar_start(control)) |
| 3882 | len -= 2; |
| 3883 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3884 | if (chan->fcs == L2CAP_FCS_CRC16) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3885 | len -= 2; |
| 3886 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3887 | if (len > chan->mps || len < 0 || __is_sframe(control)) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3888 | goto drop; |
| 3889 | |
| 3890 | tx_seq = __get_txseq(control); |
| 3891 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3892 | if (chan->expected_tx_seq == tx_seq) |
| 3893 | chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3894 | else |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3895 | chan->expected_tx_seq = (tx_seq + 1) % 64; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3896 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3897 | l2cap_streaming_reassembly_sdu(chan, skb, control); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3898 | |
| 3899 | goto done; |
| 3900 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3901 | default: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3902 | BT_DBG("chan %p: bad mode 0x%2.2x", chan, chan->mode); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3903 | break; |
| 3904 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3905 | |
| 3906 | drop: |
| 3907 | kfree_skb(skb); |
| 3908 | |
| 3909 | done: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 3910 | if (sk) |
| 3911 | bh_unlock_sock(sk); |
| 3912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3913 | return 0; |
| 3914 | } |
| 3915 | |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 3916 | static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3917 | { |
David S. Miller | 6dcae1e | 2011-05-16 23:09:26 -0400 | [diff] [blame] | 3918 | struct sock *sk = NULL; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3919 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3920 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3921 | chan = l2cap_global_chan_by_psm(0, psm, conn->src); |
| 3922 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3923 | goto drop; |
| 3924 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3925 | sk = chan->sk; |
| 3926 | |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 3927 | bh_lock_sock(sk); |
| 3928 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3929 | BT_DBG("sk %p, len %d", sk, skb->len); |
| 3930 | |
| 3931 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) |
| 3932 | goto drop; |
| 3933 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3934 | if (l2cap_pi(sk)->chan->imtu < skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3935 | goto drop; |
| 3936 | |
| 3937 | if (!sock_queue_rcv_skb(sk, skb)) |
| 3938 | goto done; |
| 3939 | |
| 3940 | drop: |
| 3941 | kfree_skb(skb); |
| 3942 | |
| 3943 | done: |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 3944 | if (sk) |
| 3945 | bh_unlock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3946 | return 0; |
| 3947 | } |
| 3948 | |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 3949 | static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb) |
| 3950 | { |
David S. Miller | 6dcae1e | 2011-05-16 23:09:26 -0400 | [diff] [blame] | 3951 | struct sock *sk = NULL; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3952 | struct l2cap_chan *chan; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 3953 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3954 | chan = l2cap_global_chan_by_scid(0, cid, conn->src); |
| 3955 | if (!chan) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 3956 | goto drop; |
| 3957 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3958 | sk = chan->sk; |
| 3959 | |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 3960 | bh_lock_sock(sk); |
| 3961 | |
| 3962 | BT_DBG("sk %p, len %d", sk, skb->len); |
| 3963 | |
| 3964 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) |
| 3965 | goto drop; |
| 3966 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3967 | if (l2cap_pi(sk)->chan->imtu < skb->len) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 3968 | goto drop; |
| 3969 | |
| 3970 | if (!sock_queue_rcv_skb(sk, skb)) |
| 3971 | goto done; |
| 3972 | |
| 3973 | drop: |
| 3974 | kfree_skb(skb); |
| 3975 | |
| 3976 | done: |
| 3977 | if (sk) |
| 3978 | bh_unlock_sock(sk); |
| 3979 | return 0; |
| 3980 | } |
| 3981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3982 | static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) |
| 3983 | { |
| 3984 | struct l2cap_hdr *lh = (void *) skb->data; |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 3985 | u16 cid, len; |
| 3986 | __le16 psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3987 | |
| 3988 | skb_pull(skb, L2CAP_HDR_SIZE); |
| 3989 | cid = __le16_to_cpu(lh->cid); |
| 3990 | len = __le16_to_cpu(lh->len); |
| 3991 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3992 | if (len != skb->len) { |
| 3993 | kfree_skb(skb); |
| 3994 | return; |
| 3995 | } |
| 3996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3997 | BT_DBG("len %d, cid 0x%4.4x", len, cid); |
| 3998 | |
| 3999 | switch (cid) { |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 4000 | case L2CAP_CID_LE_SIGNALING: |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 4001 | case L2CAP_CID_SIGNALING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | l2cap_sig_channel(conn, skb); |
| 4003 | break; |
| 4004 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 4005 | case L2CAP_CID_CONN_LESS: |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 4006 | psm = get_unaligned_le16(skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | skb_pull(skb, 2); |
| 4008 | l2cap_conless_channel(conn, psm, skb); |
| 4009 | break; |
| 4010 | |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4011 | case L2CAP_CID_LE_DATA: |
| 4012 | l2cap_att_channel(conn, cid, skb); |
| 4013 | break; |
| 4014 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4015 | default: |
| 4016 | l2cap_data_channel(conn, cid, skb); |
| 4017 | break; |
| 4018 | } |
| 4019 | } |
| 4020 | |
| 4021 | /* ---- L2CAP interface with lower layer (HCI) ---- */ |
| 4022 | |
| 4023 | static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
| 4024 | { |
| 4025 | int exact = 0, lm1 = 0, lm2 = 0; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4026 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4027 | |
| 4028 | if (type != ACL_LINK) |
João Paulo Rechi Vita | 963cf68 | 2010-06-22 13:56:28 -0300 | [diff] [blame] | 4029 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4030 | |
| 4031 | BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); |
| 4032 | |
| 4033 | /* Find listening sockets and check their link_mode */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4034 | read_lock(&chan_list_lock); |
| 4035 | list_for_each_entry(c, &chan_list, global_l) { |
| 4036 | struct sock *sk = c->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4038 | if (sk->sk_state != BT_LISTEN) |
| 4039 | continue; |
| 4040 | |
| 4041 | if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4042 | lm1 |= HCI_LM_ACCEPT; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4043 | if (c->role_switch) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4044 | lm1 |= HCI_LM_MASTER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4045 | exact++; |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4046 | } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { |
| 4047 | lm2 |= HCI_LM_ACCEPT; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4048 | if (c->role_switch) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4049 | lm2 |= HCI_LM_MASTER; |
| 4050 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4051 | } |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4052 | read_unlock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4053 | |
| 4054 | return exact ? lm1 : lm2; |
| 4055 | } |
| 4056 | |
| 4057 | static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) |
| 4058 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4059 | struct l2cap_conn *conn; |
| 4060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); |
| 4062 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 4063 | if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) |
João Paulo Rechi Vita | 963cf68 | 2010-06-22 13:56:28 -0300 | [diff] [blame] | 4064 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4065 | |
| 4066 | if (!status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | conn = l2cap_conn_add(hcon, status); |
| 4068 | if (conn) |
| 4069 | l2cap_conn_ready(conn); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4070 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | l2cap_conn_del(hcon, bt_err(status)); |
| 4072 | |
| 4073 | return 0; |
| 4074 | } |
| 4075 | |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 4076 | static int l2cap_disconn_ind(struct hci_conn *hcon) |
| 4077 | { |
| 4078 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 4079 | |
| 4080 | BT_DBG("hcon %p", hcon); |
| 4081 | |
| 4082 | if (hcon->type != ACL_LINK || !conn) |
| 4083 | return 0x13; |
| 4084 | |
| 4085 | return conn->disc_reason; |
| 4086 | } |
| 4087 | |
| 4088 | static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | { |
| 4090 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 4091 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 4092 | if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) |
João Paulo Rechi Vita | 963cf68 | 2010-06-22 13:56:28 -0300 | [diff] [blame] | 4093 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | |
| 4095 | l2cap_conn_del(hcon, bt_err(reason)); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4097 | return 0; |
| 4098 | } |
| 4099 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4100 | static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4101 | { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4102 | struct sock *sk = chan->sk; |
| 4103 | |
Gustavo F. Padovan | bd3c9e2 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 4104 | if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM) |
Marcel Holtmann | 255c760 | 2009-02-04 21:07:19 +0100 | [diff] [blame] | 4105 | return; |
| 4106 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4107 | if (encrypt == 0x00) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4108 | if (chan->sec_level == BT_SECURITY_MEDIUM) { |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4109 | l2cap_sock_clear_timer(sk); |
| 4110 | l2cap_sock_set_timer(sk, HZ * 5); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4111 | } else if (chan->sec_level == BT_SECURITY_HIGH) |
Gustavo F. Padovan | 6e9e43f | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 4112 | __l2cap_chan_close(chan, ECONNREFUSED); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4113 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4114 | if (chan->sec_level == BT_SECURITY_MEDIUM) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4115 | l2cap_sock_clear_timer(sk); |
| 4116 | } |
| 4117 | } |
| 4118 | |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 4119 | static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4120 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 4121 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4122 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4123 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4124 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | return 0; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4127 | BT_DBG("conn %p", conn); |
| 4128 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4129 | read_lock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4131 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4132 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | bh_lock_sock(sk); |
| 4135 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 4136 | if (chan->conf_state & L2CAP_CONF_CONNECT_PEND) { |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 4137 | bh_unlock_sock(sk); |
| 4138 | continue; |
| 4139 | } |
| 4140 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4141 | if (!status && (sk->sk_state == BT_CONNECTED || |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 4142 | sk->sk_state == BT_CONFIG)) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4143 | l2cap_check_encryption(chan, encrypt); |
Marcel Holtmann | 9719f8a | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 4144 | bh_unlock_sock(sk); |
| 4145 | continue; |
| 4146 | } |
| 4147 | |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4148 | if (sk->sk_state == BT_CONNECT) { |
| 4149 | if (!status) { |
| 4150 | struct l2cap_conn_req req; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4151 | req.scid = cpu_to_le16(chan->scid); |
| 4152 | req.psm = chan->psm; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4153 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 4154 | chan->ident = l2cap_get_ident(conn); |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 4155 | chan->conf_state |= L2CAP_CONF_CONNECT_PEND; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4156 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 4157 | l2cap_send_cmd(conn, chan->ident, |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4158 | L2CAP_CONN_REQ, sizeof(req), &req); |
| 4159 | } else { |
| 4160 | l2cap_sock_clear_timer(sk); |
| 4161 | l2cap_sock_set_timer(sk, HZ / 10); |
| 4162 | } |
| 4163 | } else if (sk->sk_state == BT_CONNECT2) { |
| 4164 | struct l2cap_conn_rsp rsp; |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4165 | __u16 res, stat; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4166 | |
| 4167 | if (!status) { |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4168 | if (bt_sk(sk)->defer_setup) { |
| 4169 | struct sock *parent = bt_sk(sk)->parent; |
| 4170 | res = L2CAP_CR_PEND; |
| 4171 | stat = L2CAP_CS_AUTHOR_PEND; |
| 4172 | parent->sk_data_ready(parent, 0); |
| 4173 | } else { |
| 4174 | sk->sk_state = BT_CONFIG; |
| 4175 | res = L2CAP_CR_SUCCESS; |
| 4176 | stat = L2CAP_CS_NO_INFO; |
| 4177 | } |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4178 | } else { |
| 4179 | sk->sk_state = BT_DISCONN; |
| 4180 | l2cap_sock_set_timer(sk, HZ / 10); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4181 | res = L2CAP_CR_SEC_BLOCK; |
| 4182 | stat = L2CAP_CS_NO_INFO; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4183 | } |
| 4184 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4185 | rsp.scid = cpu_to_le16(chan->dcid); |
| 4186 | rsp.dcid = cpu_to_le16(chan->scid); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4187 | rsp.result = cpu_to_le16(res); |
| 4188 | rsp.status = cpu_to_le16(stat); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 4189 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
| 4190 | sizeof(rsp), &rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | } |
| 4192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4193 | bh_unlock_sock(sk); |
| 4194 | } |
| 4195 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4196 | read_unlock(&conn->chan_lock); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4198 | return 0; |
| 4199 | } |
| 4200 | |
| 4201 | static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) |
| 4202 | { |
| 4203 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 4204 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 4205 | if (!conn) |
| 4206 | conn = l2cap_conn_add(hcon, 0); |
| 4207 | |
| 4208 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4209 | goto drop; |
| 4210 | |
| 4211 | BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); |
| 4212 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 4213 | if (!(flags & ACL_CONT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4214 | struct l2cap_hdr *hdr; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4215 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4216 | u16 cid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | int len; |
| 4218 | |
| 4219 | if (conn->rx_len) { |
| 4220 | BT_ERR("Unexpected start frame (len %d)", skb->len); |
| 4221 | kfree_skb(conn->rx_skb); |
| 4222 | conn->rx_skb = NULL; |
| 4223 | conn->rx_len = 0; |
| 4224 | l2cap_conn_unreliable(conn, ECOMM); |
| 4225 | } |
| 4226 | |
Andrei Emeltchenko | aae7fe2 | 2010-09-15 14:28:43 +0300 | [diff] [blame] | 4227 | /* Start fragment always begin with Basic L2CAP header */ |
| 4228 | if (skb->len < L2CAP_HDR_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | BT_ERR("Frame is too short (len %d)", skb->len); |
| 4230 | l2cap_conn_unreliable(conn, ECOMM); |
| 4231 | goto drop; |
| 4232 | } |
| 4233 | |
| 4234 | hdr = (struct l2cap_hdr *) skb->data; |
| 4235 | len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4236 | cid = __le16_to_cpu(hdr->cid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | |
| 4238 | if (len == skb->len) { |
| 4239 | /* Complete frame received */ |
| 4240 | l2cap_recv_frame(conn, skb); |
| 4241 | return 0; |
| 4242 | } |
| 4243 | |
| 4244 | BT_DBG("Start: total len %d, frag len %d", len, skb->len); |
| 4245 | |
| 4246 | if (skb->len > len) { |
| 4247 | BT_ERR("Frame is too long (len %d, expected len %d)", |
| 4248 | skb->len, len); |
| 4249 | l2cap_conn_unreliable(conn, ECOMM); |
| 4250 | goto drop; |
| 4251 | } |
| 4252 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4253 | chan = l2cap_get_chan_by_scid(conn, cid); |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4254 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4255 | if (chan && chan->sk) { |
| 4256 | struct sock *sk = chan->sk; |
| 4257 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4258 | if (chan->imtu < len - L2CAP_HDR_SIZE) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4259 | BT_ERR("Frame exceeding recv MTU (len %d, " |
| 4260 | "MTU %d)", len, |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4261 | chan->imtu); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4262 | bh_unlock_sock(sk); |
| 4263 | l2cap_conn_unreliable(conn, ECOMM); |
| 4264 | goto drop; |
| 4265 | } |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4266 | bh_unlock_sock(sk); |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4267 | } |
| 4268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4269 | /* Allocate skb for the complete frame (with header) */ |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 4270 | conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); |
| 4271 | if (!conn->rx_skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | goto drop; |
| 4273 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 4274 | skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4275 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | conn->rx_len = len - skb->len; |
| 4277 | } else { |
| 4278 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
| 4279 | |
| 4280 | if (!conn->rx_len) { |
| 4281 | BT_ERR("Unexpected continuation frame (len %d)", skb->len); |
| 4282 | l2cap_conn_unreliable(conn, ECOMM); |
| 4283 | goto drop; |
| 4284 | } |
| 4285 | |
| 4286 | if (skb->len > conn->rx_len) { |
| 4287 | BT_ERR("Fragment is too long (len %d, expected %d)", |
| 4288 | skb->len, conn->rx_len); |
| 4289 | kfree_skb(conn->rx_skb); |
| 4290 | conn->rx_skb = NULL; |
| 4291 | conn->rx_len = 0; |
| 4292 | l2cap_conn_unreliable(conn, ECOMM); |
| 4293 | goto drop; |
| 4294 | } |
| 4295 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 4296 | skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4297 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | conn->rx_len -= skb->len; |
| 4299 | |
| 4300 | if (!conn->rx_len) { |
| 4301 | /* Complete frame received */ |
| 4302 | l2cap_recv_frame(conn, conn->rx_skb); |
| 4303 | conn->rx_skb = NULL; |
| 4304 | } |
| 4305 | } |
| 4306 | |
| 4307 | drop: |
| 4308 | kfree_skb(skb); |
| 4309 | return 0; |
| 4310 | } |
| 4311 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4312 | static int l2cap_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4314 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4316 | read_lock_bh(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4318 | list_for_each_entry(c, &chan_list, global_l) { |
| 4319 | struct sock *sk = c->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | |
Gustavo F. Padovan | 903d343 | 2011-02-10 14:16:06 -0200 | [diff] [blame] | 4321 | seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4322 | batostr(&bt_sk(sk)->src), |
| 4323 | batostr(&bt_sk(sk)->dst), |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4324 | sk->sk_state, __le16_to_cpu(c->psm), |
| 4325 | c->scid, c->dcid, c->imtu, c->omtu, |
| 4326 | c->sec_level, c->mode); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 4327 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4329 | read_unlock_bh(&chan_list_lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 4330 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4331 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | } |
| 4333 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4334 | static int l2cap_debugfs_open(struct inode *inode, struct file *file) |
| 4335 | { |
| 4336 | return single_open(file, l2cap_debugfs_show, inode->i_private); |
| 4337 | } |
| 4338 | |
| 4339 | static const struct file_operations l2cap_debugfs_fops = { |
| 4340 | .open = l2cap_debugfs_open, |
| 4341 | .read = seq_read, |
| 4342 | .llseek = seq_lseek, |
| 4343 | .release = single_release, |
| 4344 | }; |
| 4345 | |
| 4346 | static struct dentry *l2cap_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4348 | static struct hci_proto l2cap_hci_proto = { |
| 4349 | .name = "L2CAP", |
| 4350 | .id = HCI_PROTO_L2CAP, |
| 4351 | .connect_ind = l2cap_connect_ind, |
| 4352 | .connect_cfm = l2cap_connect_cfm, |
| 4353 | .disconn_ind = l2cap_disconn_ind, |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 4354 | .disconn_cfm = l2cap_disconn_cfm, |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 4355 | .security_cfm = l2cap_security_cfm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | .recv_acldata = l2cap_recv_acldata |
| 4357 | }; |
| 4358 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 4359 | int __init l2cap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4360 | { |
| 4361 | int err; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 4362 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4363 | err = l2cap_init_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4364 | if (err < 0) |
| 4365 | return err; |
| 4366 | |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 4367 | _busy_wq = create_singlethread_workqueue("l2cap"); |
Anderson Lizardo | b78d7b4f | 2010-11-29 12:15:50 -0400 | [diff] [blame] | 4368 | if (!_busy_wq) { |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4369 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4370 | goto error; |
| 4371 | } |
| 4372 | |
| 4373 | err = hci_register_proto(&l2cap_hci_proto); |
| 4374 | if (err < 0) { |
| 4375 | BT_ERR("L2CAP protocol registration failed"); |
| 4376 | bt_sock_unregister(BTPROTO_L2CAP); |
| 4377 | goto error; |
| 4378 | } |
| 4379 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4380 | if (bt_debugfs) { |
| 4381 | l2cap_debugfs = debugfs_create_file("l2cap", 0444, |
| 4382 | bt_debugfs, NULL, &l2cap_debugfs_fops); |
| 4383 | if (!l2cap_debugfs) |
| 4384 | BT_ERR("Failed to create L2CAP debug file"); |
| 4385 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | return 0; |
| 4388 | |
| 4389 | error: |
Anderson Lizardo | b78d7b4f | 2010-11-29 12:15:50 -0400 | [diff] [blame] | 4390 | destroy_workqueue(_busy_wq); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4391 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | return err; |
| 4393 | } |
| 4394 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 4395 | void l2cap_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4397 | debugfs_remove(l2cap_debugfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 4399 | flush_workqueue(_busy_wq); |
| 4400 | destroy_workqueue(_busy_wq); |
| 4401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | if (hci_unregister_proto(&l2cap_hci_proto) < 0) |
| 4403 | BT_ERR("L2CAP protocol unregistration failed"); |
| 4404 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4405 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | } |
| 4407 | |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 4408 | module_param(disable_ertm, bool, 0644); |
| 4409 | MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); |