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. |
Gustavo F. Padovan | 590051d | 2011-12-18 13:39:33 -0200 | [diff] [blame] | 6 | Copyright (C) 2011 ProFUSION Embedded Systems |
Mat Martineau | 422e925 | 2012-04-27 16:50:55 -0700 | [diff] [blame] | 7 | Copyright (c) 2012 Code Aurora Forum. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
| 9 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 10 | |
| 11 | This program is free software; you can redistribute it and/or modify |
| 12 | it under the terms of the GNU General Public License version 2 as |
| 13 | published by the Free Software Foundation; |
| 14 | |
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 18 | 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] | 19 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 20 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 21 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 23 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 24 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 25 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | SOFTWARE IS DISCLAIMED. |
| 27 | */ |
| 28 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 29 | /* Bluetooth L2CAP core. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/module.h> |
| 32 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 33 | #include <linux/debugfs.h> |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 34 | #include <linux/crc16.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #include <net/bluetooth/bluetooth.h> |
| 37 | #include <net/bluetooth/hci_core.h> |
| 38 | #include <net/bluetooth/l2cap.h> |
Marcel Holtmann | 7ef9fbf | 2013-10-10 14:54:14 -0700 | [diff] [blame] | 39 | |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 40 | #include "smp.h" |
Marcel Holtmann | 7024728 | 2013-10-10 14:54:15 -0700 | [diff] [blame] | 41 | #include "a2mp.h" |
Marcel Holtmann | 7ef9fbf | 2013-10-10 14:54:14 -0700 | [diff] [blame] | 42 | #include "amp.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Johan Hedberg | 0f1bfe4 | 2014-01-27 15:11:35 -0800 | [diff] [blame] | 44 | #define LE_FLOWCTL_MAX_CREDITS 65535 |
| 45 | |
Mat Martineau | d1de6d4 | 2012-05-17 20:53:55 -0700 | [diff] [blame] | 46 | bool disable_ertm; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 47 | |
Marcel Holtmann | 547d103 | 2013-10-12 08:18:19 -0700 | [diff] [blame] | 48 | static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN | L2CAP_FEAT_UCD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 50 | static LIST_HEAD(chan_list); |
| 51 | static DEFINE_RWLOCK(chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 53 | static u16 le_max_credits = L2CAP_LE_MAX_CREDITS; |
| 54 | static u16 le_default_mps = L2CAP_LE_DEFAULT_MPS; |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 57 | u8 code, u8 ident, u16 dlen, void *data); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 58 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 59 | void *data); |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 60 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 61 | static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 63 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 64 | struct sk_buff_head *skbs, u8 event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 65 | |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 66 | static inline u8 bdaddr_type(u8 link_type, u8 bdaddr_type) |
Marcel Holtmann | 4f1654e | 2013-10-13 08:50:41 -0700 | [diff] [blame] | 67 | { |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 68 | if (link_type == LE_LINK) { |
| 69 | if (bdaddr_type == ADDR_LE_DEV_PUBLIC) |
Marcel Holtmann | 4f1654e | 2013-10-13 08:50:41 -0700 | [diff] [blame] | 70 | return BDADDR_LE_PUBLIC; |
| 71 | else |
| 72 | return BDADDR_LE_RANDOM; |
| 73 | } |
| 74 | |
| 75 | return BDADDR_BREDR; |
| 76 | } |
| 77 | |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 78 | static inline u8 bdaddr_src_type(struct hci_conn *hcon) |
| 79 | { |
| 80 | return bdaddr_type(hcon->type, hcon->src_type); |
| 81 | } |
| 82 | |
| 83 | static inline u8 bdaddr_dst_type(struct hci_conn *hcon) |
| 84 | { |
| 85 | return bdaddr_type(hcon->type, hcon->dst_type); |
| 86 | } |
| 87 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 88 | /* ---- L2CAP channels ---- */ |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 89 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 90 | static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, |
| 91 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 92 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 93 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 94 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 95 | list_for_each_entry(c, &conn->chan_l, list) { |
| 96 | if (c->dcid == cid) |
| 97 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 98 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 99 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 102 | static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, |
| 103 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 104 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 105 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 106 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 107 | list_for_each_entry(c, &conn->chan_l, list) { |
| 108 | if (c->scid == cid) |
| 109 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 110 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 111 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* Find channel with given SCID. |
Mat Martineau | ef191ad | 2012-05-02 09:42:00 -0700 | [diff] [blame] | 115 | * Returns locked channel. */ |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 116 | static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, |
| 117 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 118 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 119 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 120 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 121 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 122 | c = __l2cap_get_chan_by_scid(conn, cid); |
Mat Martineau | ef191ad | 2012-05-02 09:42:00 -0700 | [diff] [blame] | 123 | if (c) |
| 124 | l2cap_chan_lock(c); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 125 | mutex_unlock(&conn->chan_lock); |
| 126 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 127 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Mat Martineau | b1a130b | 2012-10-23 15:24:09 -0700 | [diff] [blame] | 130 | /* Find channel with given DCID. |
| 131 | * Returns locked channel. |
| 132 | */ |
| 133 | static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn, |
| 134 | u16 cid) |
| 135 | { |
| 136 | struct l2cap_chan *c; |
| 137 | |
| 138 | mutex_lock(&conn->chan_lock); |
| 139 | c = __l2cap_get_chan_by_dcid(conn, cid); |
| 140 | if (c) |
| 141 | l2cap_chan_lock(c); |
| 142 | mutex_unlock(&conn->chan_lock); |
| 143 | |
| 144 | return c; |
| 145 | } |
| 146 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 147 | static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, |
| 148 | u8 ident) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 149 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 150 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 151 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 152 | list_for_each_entry(c, &conn->chan_l, list) { |
| 153 | if (c->ident == ident) |
| 154 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 155 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 156 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 159 | static struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, |
| 160 | u8 ident) |
| 161 | { |
| 162 | struct l2cap_chan *c; |
| 163 | |
| 164 | mutex_lock(&conn->chan_lock); |
| 165 | c = __l2cap_get_chan_by_ident(conn, ident); |
| 166 | if (c) |
| 167 | l2cap_chan_lock(c); |
| 168 | mutex_unlock(&conn->chan_lock); |
| 169 | |
| 170 | return c; |
| 171 | } |
| 172 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 173 | 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] | 174 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 175 | struct l2cap_chan *c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 176 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 177 | list_for_each_entry(c, &chan_list, global_l) { |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 178 | if (c->sport == psm && !bacmp(&c->src, src)) |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 179 | return c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 180 | } |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 181 | return NULL; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) |
| 185 | { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 186 | int err; |
| 187 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 188 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 189 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 190 | if (psm && __l2cap_global_chan_by_addr(psm, src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 191 | err = -EADDRINUSE; |
| 192 | goto done; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 193 | } |
| 194 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 195 | if (psm) { |
| 196 | chan->psm = psm; |
| 197 | chan->sport = psm; |
| 198 | err = 0; |
| 199 | } else { |
| 200 | u16 p; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 201 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 202 | err = -EINVAL; |
| 203 | for (p = 0x1001; p < 0x1100; p += 2) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 204 | if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 205 | chan->psm = cpu_to_le16(p); |
| 206 | chan->sport = cpu_to_le16(p); |
| 207 | err = 0; |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | done: |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 213 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 214 | return err; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 215 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 216 | EXPORT_SYMBOL_GPL(l2cap_add_psm); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 217 | |
| 218 | int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) |
| 219 | { |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 220 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 221 | |
Johan Hedberg | 1482430 | 2014-08-07 22:56:50 +0300 | [diff] [blame] | 222 | /* Override the defaults (which are for conn-oriented) */ |
| 223 | chan->omtu = L2CAP_DEFAULT_MTU; |
| 224 | chan->chan_type = L2CAP_CHAN_FIXED; |
| 225 | |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 226 | chan->scid = scid; |
| 227 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 228 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 233 | static u16 l2cap_alloc_cid(struct l2cap_conn *conn) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 234 | { |
Johan Hedberg | e77af75 | 2013-10-08 10:31:00 +0200 | [diff] [blame] | 235 | u16 cid, dyn_end; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 236 | |
Johan Hedberg | e77af75 | 2013-10-08 10:31:00 +0200 | [diff] [blame] | 237 | if (conn->hcon->type == LE_LINK) |
| 238 | dyn_end = L2CAP_CID_LE_DYN_END; |
| 239 | else |
| 240 | dyn_end = L2CAP_CID_DYN_END; |
| 241 | |
| 242 | for (cid = L2CAP_CID_DYN_START; cid < dyn_end; cid++) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 243 | if (!__l2cap_get_chan_by_scid(conn, cid)) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 244 | return cid; |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 250 | static void l2cap_state_change(struct l2cap_chan *chan, int state) |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 251 | { |
Andrei Emeltchenko | 42d2d87 | 2012-02-17 11:40:57 +0200 | [diff] [blame] | 252 | BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 253 | state_to_string(state)); |
Gustavo F. Padovan | badaaa0 | 2011-11-23 20:11:46 -0200 | [diff] [blame] | 254 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 255 | chan->state = state; |
Gustavo Padovan | 53f5212 | 2013-10-15 19:24:45 -0300 | [diff] [blame] | 256 | chan->ops->state_change(chan, state, 0); |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 257 | } |
| 258 | |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 259 | static inline void l2cap_state_change_and_error(struct l2cap_chan *chan, |
| 260 | int state, int err) |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 261 | { |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 262 | chan->state = state; |
Gustavo Padovan | 53f5212 | 2013-10-15 19:24:45 -0300 | [diff] [blame] | 263 | chan->ops->state_change(chan, chan->state, err); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err) |
| 267 | { |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 268 | chan->ops->state_change(chan, chan->state, err); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 271 | static void __set_retrans_timer(struct l2cap_chan *chan) |
| 272 | { |
| 273 | if (!delayed_work_pending(&chan->monitor_timer) && |
| 274 | chan->retrans_timeout) { |
| 275 | l2cap_set_timer(chan, &chan->retrans_timer, |
| 276 | msecs_to_jiffies(chan->retrans_timeout)); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | static void __set_monitor_timer(struct l2cap_chan *chan) |
| 281 | { |
| 282 | __clear_retrans_timer(chan); |
| 283 | if (chan->monitor_timeout) { |
| 284 | l2cap_set_timer(chan, &chan->monitor_timer, |
| 285 | msecs_to_jiffies(chan->monitor_timeout)); |
| 286 | } |
| 287 | } |
| 288 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 289 | static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head, |
| 290 | u16 seq) |
| 291 | { |
| 292 | struct sk_buff *skb; |
| 293 | |
| 294 | skb_queue_walk(head, skb) { |
| 295 | if (bt_cb(skb)->control.txseq == seq) |
| 296 | return skb; |
| 297 | } |
| 298 | |
| 299 | return NULL; |
| 300 | } |
| 301 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 302 | /* ---- L2CAP sequence number lists ---- */ |
| 303 | |
| 304 | /* For ERTM, ordered lists of sequence numbers must be tracked for |
| 305 | * SREJ requests that are received and for frames that are to be |
| 306 | * retransmitted. These seq_list functions implement a singly-linked |
| 307 | * list in an array, where membership in the list can also be checked |
| 308 | * in constant time. Items can also be added to the tail of the list |
| 309 | * and removed from the head in constant time, without further memory |
| 310 | * allocs or frees. |
| 311 | */ |
| 312 | |
| 313 | static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size) |
| 314 | { |
| 315 | size_t alloc_size, i; |
| 316 | |
| 317 | /* Allocated size is a power of 2 to map sequence numbers |
| 318 | * (which may be up to 14 bits) in to a smaller array that is |
| 319 | * sized for the negotiated ERTM transmit windows. |
| 320 | */ |
| 321 | alloc_size = roundup_pow_of_two(size); |
| 322 | |
| 323 | seq_list->list = kmalloc(sizeof(u16) * alloc_size, GFP_KERNEL); |
| 324 | if (!seq_list->list) |
| 325 | return -ENOMEM; |
| 326 | |
| 327 | seq_list->mask = alloc_size - 1; |
| 328 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 329 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
| 330 | for (i = 0; i < alloc_size; i++) |
| 331 | seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list) |
| 337 | { |
| 338 | kfree(seq_list->list); |
| 339 | } |
| 340 | |
| 341 | static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list, |
| 342 | u16 seq) |
| 343 | { |
| 344 | /* Constant-time check for list membership */ |
| 345 | return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR; |
| 346 | } |
| 347 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 348 | static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list) |
| 349 | { |
Johan Hedberg | 03a0c5d | 2014-01-18 21:32:59 +0200 | [diff] [blame] | 350 | u16 seq = seq_list->head; |
| 351 | u16 mask = seq_list->mask; |
| 352 | |
| 353 | seq_list->head = seq_list->list[seq & mask]; |
| 354 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR; |
| 355 | |
| 356 | if (seq_list->head == L2CAP_SEQ_LIST_TAIL) { |
| 357 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 358 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
| 359 | } |
| 360 | |
| 361 | return seq; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list) |
| 365 | { |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 366 | u16 i; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 367 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 368 | if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) |
| 369 | return; |
| 370 | |
| 371 | for (i = 0; i <= seq_list->mask; i++) |
| 372 | seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; |
| 373 | |
| 374 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 375 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq) |
| 379 | { |
| 380 | u16 mask = seq_list->mask; |
| 381 | |
| 382 | /* All appends happen in constant time */ |
| 383 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 384 | if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR) |
| 385 | return; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 386 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 387 | if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR) |
| 388 | seq_list->head = seq; |
| 389 | else |
| 390 | seq_list->list[seq_list->tail & mask] = seq; |
| 391 | |
| 392 | seq_list->tail = seq; |
| 393 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 396 | static void l2cap_chan_timeout(struct work_struct *work) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 397 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 398 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 399 | chan_timer.work); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 400 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 401 | int reason; |
| 402 | |
Andrei Emeltchenko | e05dcc3 | 2012-02-17 11:40:56 +0200 | [diff] [blame] | 403 | BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 404 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 405 | mutex_lock(&conn->chan_lock); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 406 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 407 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 408 | if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 409 | reason = ECONNREFUSED; |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 410 | else if (chan->state == BT_CONNECT && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 411 | chan->sec_level != BT_SECURITY_SDP) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 412 | reason = ECONNREFUSED; |
| 413 | else |
| 414 | reason = ETIMEDOUT; |
| 415 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 416 | l2cap_chan_close(chan, reason); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 417 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 418 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 419 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 420 | chan->ops->close(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 421 | mutex_unlock(&conn->chan_lock); |
| 422 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 423 | l2cap_chan_put(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 424 | } |
| 425 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 426 | struct l2cap_chan *l2cap_chan_create(void) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 427 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 428 | struct l2cap_chan *chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 429 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 430 | chan = kzalloc(sizeof(*chan), GFP_ATOMIC); |
| 431 | if (!chan) |
| 432 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 433 | |
Andrei Emeltchenko | c03b355 | 2012-02-21 12:54:56 +0200 | [diff] [blame] | 434 | mutex_init(&chan->lock); |
| 435 | |
Johan Hedberg | ff71411 | 2014-11-13 09:46:04 +0200 | [diff] [blame] | 436 | /* Set default lock nesting level */ |
| 437 | atomic_set(&chan->nesting, L2CAP_NESTING_NORMAL); |
| 438 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 439 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 440 | list_add(&chan->global_l, &chan_list); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 441 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 442 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 443 | INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 444 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 445 | chan->state = BT_OPEN; |
| 446 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 447 | kref_init(&chan->kref); |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 448 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 449 | /* This flag is cleared in l2cap_chan_ready() */ |
| 450 | set_bit(CONF_NOT_COMPLETE, &chan->conf_state); |
| 451 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 452 | BT_DBG("chan %p", chan); |
Szymon Janc | abc545b | 2011-11-03 16:05:44 +0100 | [diff] [blame] | 453 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 454 | return chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 455 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 456 | EXPORT_SYMBOL_GPL(l2cap_chan_create); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 457 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 458 | static void l2cap_chan_destroy(struct kref *kref) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 459 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 460 | struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref); |
| 461 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 462 | BT_DBG("chan %p", chan); |
| 463 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 464 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 465 | list_del(&chan->global_l); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 466 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 467 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 468 | kfree(chan); |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 469 | } |
| 470 | |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 471 | void l2cap_chan_hold(struct l2cap_chan *c) |
| 472 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 473 | BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount)); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 474 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 475 | kref_get(&c->kref); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | void l2cap_chan_put(struct l2cap_chan *c) |
| 479 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 480 | BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount)); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 481 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 482 | kref_put(&c->kref, l2cap_chan_destroy); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 483 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 484 | EXPORT_SYMBOL_GPL(l2cap_chan_put); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 485 | |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 486 | void l2cap_chan_set_defaults(struct l2cap_chan *chan) |
| 487 | { |
| 488 | chan->fcs = L2CAP_FCS_CRC16; |
| 489 | chan->max_tx = L2CAP_DEFAULT_MAX_TX; |
| 490 | chan->tx_win = L2CAP_DEFAULT_TX_WINDOW; |
| 491 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
Jukka Rissanen | 6a5e816 | 2014-05-28 14:43:04 +0300 | [diff] [blame] | 492 | chan->remote_max_tx = chan->max_tx; |
| 493 | chan->remote_tx_win = chan->tx_win; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 494 | chan->ack_win = L2CAP_DEFAULT_TX_WINDOW; |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 495 | chan->sec_level = BT_SECURITY_LOW; |
Jukka Rissanen | 6a5e816 | 2014-05-28 14:43:04 +0300 | [diff] [blame] | 496 | chan->flush_to = L2CAP_DEFAULT_FLUSH_TO; |
| 497 | chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO; |
| 498 | chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO; |
| 499 | chan->conf_state = 0; |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 500 | |
| 501 | set_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 502 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 503 | EXPORT_SYMBOL_GPL(l2cap_chan_set_defaults); |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 504 | |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 505 | static void l2cap_le_flowctl_init(struct l2cap_chan *chan) |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 506 | { |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 507 | chan->sdu = NULL; |
| 508 | chan->sdu_last_frag = NULL; |
| 509 | chan->sdu_len = 0; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 510 | chan->tx_credits = 0; |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 511 | chan->rx_credits = le_max_credits; |
Johan Hedberg | d1d7941 | 2014-01-27 15:11:33 -0800 | [diff] [blame] | 512 | chan->mps = min_t(u16, chan->imtu, le_default_mps); |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 513 | |
| 514 | skb_queue_head_init(&chan->tx_q); |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 515 | } |
| 516 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 517 | void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 518 | { |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 519 | BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 520 | __le16_to_cpu(chan->psm), chan->dcid); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 521 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 522 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 523 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 524 | chan->conn = conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 525 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 526 | switch (chan->chan_type) { |
| 527 | case L2CAP_CHAN_CONN_ORIENTED: |
Johan Hedberg | 21626e6 | 2014-01-24 10:35:41 +0200 | [diff] [blame] | 528 | /* Alloc CID for connection-oriented socket */ |
| 529 | chan->scid = l2cap_alloc_cid(conn); |
| 530 | if (conn->hcon->type == ACL_LINK) |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 531 | chan->omtu = L2CAP_DEFAULT_MTU; |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 532 | break; |
| 533 | |
| 534 | case L2CAP_CHAN_CONN_LESS: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 535 | /* Connectionless socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 536 | chan->scid = L2CAP_CID_CONN_LESS; |
| 537 | chan->dcid = L2CAP_CID_CONN_LESS; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 538 | chan->omtu = L2CAP_DEFAULT_MTU; |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 539 | break; |
| 540 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 541 | case L2CAP_CHAN_FIXED: |
| 542 | /* Caller will set CID and CID specific MTU values */ |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 543 | break; |
| 544 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 545 | default: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 546 | /* Raw socket can send/recv signalling messages only */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 547 | chan->scid = L2CAP_CID_SIGNALING; |
| 548 | chan->dcid = L2CAP_CID_SIGNALING; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 549 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 550 | } |
| 551 | |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 552 | chan->local_id = L2CAP_BESTEFFORT_ID; |
| 553 | chan->local_stype = L2CAP_SERV_BESTEFFORT; |
| 554 | chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; |
| 555 | chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; |
| 556 | chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 557 | chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO; |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 558 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 559 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 560 | |
Johan Hedberg | c16900c | 2014-08-15 21:17:06 +0300 | [diff] [blame] | 561 | /* Only keep a reference for fixed channels if they requested it */ |
| 562 | if (chan->chan_type != L2CAP_CHAN_FIXED || |
| 563 | test_bit(FLAG_HOLD_HCI_CONN, &chan->flags)) |
| 564 | hci_conn_hold(conn->hcon); |
Johan Hedberg | 5ee9891 | 2013-04-29 19:35:43 +0300 | [diff] [blame] | 565 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 566 | list_add(&chan->list, &conn->chan_l); |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 567 | } |
| 568 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 569 | void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 570 | { |
| 571 | mutex_lock(&conn->chan_lock); |
| 572 | __l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 573 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 574 | } |
| 575 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 576 | void l2cap_chan_del(struct l2cap_chan *chan, int err) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 577 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 578 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 579 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 580 | __clear_chan_timer(chan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 581 | |
Johan Hedberg | 49d1174 | 2014-11-13 14:37:50 +0200 | [diff] [blame] | 582 | BT_DBG("chan %p, conn %p, err %d, state %s", chan, conn, err, |
| 583 | state_to_string(chan->state)); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 584 | |
Johan Hedberg | 72847ce | 2014-08-08 09:28:03 +0300 | [diff] [blame] | 585 | chan->ops->teardown(chan, err); |
| 586 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 587 | if (conn) { |
Andrei Emeltchenko | 56f6098 | 2012-10-15 11:58:44 +0300 | [diff] [blame] | 588 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 589 | /* Delete from channel list */ |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 590 | list_del(&chan->list); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 591 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 592 | l2cap_chan_put(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 593 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 594 | chan->conn = NULL; |
Andrei Emeltchenko | 3cabbfd | 2012-05-31 11:01:37 +0300 | [diff] [blame] | 595 | |
Johan Hedberg | c16900c | 2014-08-15 21:17:06 +0300 | [diff] [blame] | 596 | /* Reference was only held for non-fixed channels or |
| 597 | * fixed channels that explicitly requested it using the |
| 598 | * FLAG_HOLD_HCI_CONN flag. |
| 599 | */ |
| 600 | if (chan->chan_type != L2CAP_CHAN_FIXED || |
| 601 | test_bit(FLAG_HOLD_HCI_CONN, &chan->flags)) |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 602 | hci_conn_drop(conn->hcon); |
Andrei Emeltchenko | 56f6098 | 2012-10-15 11:58:44 +0300 | [diff] [blame] | 603 | |
| 604 | if (mgr && mgr->bredr_chan == chan) |
| 605 | mgr->bredr_chan = NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 606 | } |
| 607 | |
Andrei Emeltchenko | 419e08c | 2012-10-31 15:46:34 +0200 | [diff] [blame] | 608 | if (chan->hs_hchan) { |
| 609 | struct hci_chan *hs_hchan = chan->hs_hchan; |
| 610 | |
| 611 | BT_DBG("chan %p disconnect hs_hchan %p", chan, hs_hchan); |
| 612 | amp_disconnect_logical_link(hs_hchan); |
| 613 | } |
| 614 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 615 | if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 616 | return; |
Gustavo F. Padovan | 2ead70b | 2011-04-01 15:13:36 -0300 | [diff] [blame] | 617 | |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 618 | switch(chan->mode) { |
| 619 | case L2CAP_MODE_BASIC: |
| 620 | break; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 621 | |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 622 | case L2CAP_MODE_LE_FLOWCTL: |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 623 | skb_queue_purge(&chan->tx_q); |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 624 | break; |
| 625 | |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 626 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 627 | __clear_retrans_timer(chan); |
| 628 | __clear_monitor_timer(chan); |
| 629 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 630 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 631 | skb_queue_purge(&chan->srej_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 632 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 633 | l2cap_seq_list_free(&chan->srej_list); |
| 634 | l2cap_seq_list_free(&chan->retrans_list); |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 635 | |
| 636 | /* fall through */ |
| 637 | |
| 638 | case L2CAP_MODE_STREAMING: |
| 639 | skb_queue_purge(&chan->tx_q); |
| 640 | break; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 641 | } |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 642 | |
| 643 | return; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 644 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 645 | EXPORT_SYMBOL_GPL(l2cap_chan_del); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 646 | |
Johan Hedberg | f3d82d0 | 2014-09-05 22:19:50 +0300 | [diff] [blame] | 647 | static void l2cap_conn_update_id_addr(struct work_struct *work) |
Johan Hedberg | 387a33e | 2014-02-18 21:41:33 +0200 | [diff] [blame] | 648 | { |
Johan Hedberg | f3d82d0 | 2014-09-05 22:19:50 +0300 | [diff] [blame] | 649 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
| 650 | id_addr_update_work); |
| 651 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 387a33e | 2014-02-18 21:41:33 +0200 | [diff] [blame] | 652 | struct l2cap_chan *chan; |
| 653 | |
| 654 | mutex_lock(&conn->chan_lock); |
| 655 | |
| 656 | list_for_each_entry(chan, &conn->chan_l, list) { |
| 657 | l2cap_chan_lock(chan); |
| 658 | bacpy(&chan->dst, &hcon->dst); |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 659 | chan->dst_type = bdaddr_dst_type(hcon); |
Johan Hedberg | 387a33e | 2014-02-18 21:41:33 +0200 | [diff] [blame] | 660 | l2cap_chan_unlock(chan); |
| 661 | } |
| 662 | |
| 663 | mutex_unlock(&conn->chan_lock); |
| 664 | } |
| 665 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 666 | static void l2cap_chan_le_connect_reject(struct l2cap_chan *chan) |
| 667 | { |
| 668 | struct l2cap_conn *conn = chan->conn; |
| 669 | struct l2cap_le_conn_rsp rsp; |
| 670 | u16 result; |
| 671 | |
| 672 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) |
| 673 | result = L2CAP_CR_AUTHORIZATION; |
| 674 | else |
| 675 | result = L2CAP_CR_BAD_PSM; |
| 676 | |
| 677 | l2cap_state_change(chan, BT_DISCONN); |
| 678 | |
| 679 | rsp.dcid = cpu_to_le16(chan->scid); |
| 680 | rsp.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 681 | rsp.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 682 | rsp.credits = cpu_to_le16(chan->rx_credits); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 683 | rsp.result = cpu_to_le16(result); |
| 684 | |
| 685 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), |
| 686 | &rsp); |
| 687 | } |
| 688 | |
Johan Hedberg | 791d60f | 2013-05-14 22:24:44 +0300 | [diff] [blame] | 689 | static void l2cap_chan_connect_reject(struct l2cap_chan *chan) |
| 690 | { |
| 691 | struct l2cap_conn *conn = chan->conn; |
| 692 | struct l2cap_conn_rsp rsp; |
| 693 | u16 result; |
| 694 | |
| 695 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) |
| 696 | result = L2CAP_CR_SEC_BLOCK; |
| 697 | else |
| 698 | result = L2CAP_CR_BAD_PSM; |
| 699 | |
| 700 | l2cap_state_change(chan, BT_DISCONN); |
| 701 | |
| 702 | rsp.scid = cpu_to_le16(chan->dcid); |
| 703 | rsp.dcid = cpu_to_le16(chan->scid); |
| 704 | rsp.result = cpu_to_le16(result); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 705 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Johan Hedberg | 791d60f | 2013-05-14 22:24:44 +0300 | [diff] [blame] | 706 | |
| 707 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
| 708 | } |
| 709 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 710 | void l2cap_chan_close(struct l2cap_chan *chan, int reason) |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 711 | { |
| 712 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 713 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 714 | BT_DBG("chan %p state %s", chan, state_to_string(chan->state)); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 715 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 716 | switch (chan->state) { |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 717 | case BT_LISTEN: |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 718 | chan->ops->teardown(chan, 0); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 719 | break; |
| 720 | |
| 721 | case BT_CONNECTED: |
| 722 | case BT_CONFIG: |
Johan Hedberg | 7b25c9b | 2014-01-28 15:28:04 -0800 | [diff] [blame] | 723 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { |
Gustavo Padovan | 8d836d7 | 2013-10-15 19:24:47 -0300 | [diff] [blame] | 724 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 725 | l2cap_send_disconn_req(chan, reason); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 726 | } else |
| 727 | l2cap_chan_del(chan, reason); |
| 728 | break; |
| 729 | |
| 730 | case BT_CONNECT2: |
Johan Hedberg | 791d60f | 2013-05-14 22:24:44 +0300 | [diff] [blame] | 731 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { |
| 732 | if (conn->hcon->type == ACL_LINK) |
| 733 | l2cap_chan_connect_reject(chan); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 734 | else if (conn->hcon->type == LE_LINK) |
| 735 | l2cap_chan_le_connect_reject(chan); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | l2cap_chan_del(chan, reason); |
| 739 | break; |
| 740 | |
| 741 | case BT_CONNECT: |
| 742 | case BT_DISCONN: |
| 743 | l2cap_chan_del(chan, reason); |
| 744 | break; |
| 745 | |
| 746 | default: |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 747 | chan->ops->teardown(chan, 0); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 748 | break; |
| 749 | } |
| 750 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 751 | EXPORT_SYMBOL(l2cap_chan_close); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 752 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 753 | static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 754 | { |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 755 | switch (chan->chan_type) { |
| 756 | case L2CAP_CHAN_RAW: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 757 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 758 | case BT_SECURITY_HIGH: |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 759 | case BT_SECURITY_FIPS: |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 760 | return HCI_AT_DEDICATED_BONDING_MITM; |
| 761 | case BT_SECURITY_MEDIUM: |
| 762 | return HCI_AT_DEDICATED_BONDING; |
| 763 | default: |
| 764 | return HCI_AT_NO_BONDING; |
| 765 | } |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 766 | break; |
Marcel Holtmann | 3124b84 | 2013-10-12 07:19:32 -0700 | [diff] [blame] | 767 | case L2CAP_CHAN_CONN_LESS: |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 768 | if (chan->psm == cpu_to_le16(L2CAP_PSM_3DSP)) { |
Marcel Holtmann | 3124b84 | 2013-10-12 07:19:32 -0700 | [diff] [blame] | 769 | if (chan->sec_level == BT_SECURITY_LOW) |
| 770 | chan->sec_level = BT_SECURITY_SDP; |
| 771 | } |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 772 | if (chan->sec_level == BT_SECURITY_HIGH || |
| 773 | chan->sec_level == BT_SECURITY_FIPS) |
Marcel Holtmann | 3124b84 | 2013-10-12 07:19:32 -0700 | [diff] [blame] | 774 | return HCI_AT_NO_BONDING_MITM; |
| 775 | else |
| 776 | return HCI_AT_NO_BONDING; |
| 777 | break; |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 778 | case L2CAP_CHAN_CONN_ORIENTED: |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 779 | if (chan->psm == cpu_to_le16(L2CAP_PSM_SDP)) { |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 780 | if (chan->sec_level == BT_SECURITY_LOW) |
| 781 | chan->sec_level = BT_SECURITY_SDP; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 782 | |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 783 | if (chan->sec_level == BT_SECURITY_HIGH || |
| 784 | chan->sec_level == BT_SECURITY_FIPS) |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 785 | return HCI_AT_NO_BONDING_MITM; |
| 786 | else |
| 787 | return HCI_AT_NO_BONDING; |
| 788 | } |
| 789 | /* fall through */ |
| 790 | default: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 791 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 792 | case BT_SECURITY_HIGH: |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 793 | case BT_SECURITY_FIPS: |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 794 | return HCI_AT_GENERAL_BONDING_MITM; |
| 795 | case BT_SECURITY_MEDIUM: |
| 796 | return HCI_AT_GENERAL_BONDING; |
| 797 | default: |
| 798 | return HCI_AT_NO_BONDING; |
| 799 | } |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 800 | break; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 804 | /* Service level security */ |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 805 | int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 806 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 807 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 808 | __u8 auth_type; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 809 | |
Johan Hedberg | a17de2f | 2013-05-14 13:25:37 +0300 | [diff] [blame] | 810 | if (conn->hcon->type == LE_LINK) |
| 811 | return smp_conn_security(conn->hcon, chan->sec_level); |
| 812 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 813 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 814 | |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 815 | return hci_conn_security(conn->hcon, chan->sec_level, auth_type, |
| 816 | initiator); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 817 | } |
| 818 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 819 | static u8 l2cap_get_ident(struct l2cap_conn *conn) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 820 | { |
| 821 | u8 id; |
| 822 | |
| 823 | /* Get next available identificator. |
| 824 | * 1 - 128 are used by kernel. |
| 825 | * 129 - 199 are reserved. |
| 826 | * 200 - 254 are used by utilities like l2ping, etc. |
| 827 | */ |
| 828 | |
Marcel Holtmann | 5a54e7c | 2014-07-13 20:50:15 +0200 | [diff] [blame] | 829 | mutex_lock(&conn->ident_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 830 | |
| 831 | if (++conn->tx_ident > 128) |
| 832 | conn->tx_ident = 1; |
| 833 | |
| 834 | id = conn->tx_ident; |
| 835 | |
Marcel Holtmann | 5a54e7c | 2014-07-13 20:50:15 +0200 | [diff] [blame] | 836 | mutex_unlock(&conn->ident_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 837 | |
| 838 | return id; |
| 839 | } |
| 840 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 841 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, |
| 842 | void *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 843 | { |
| 844 | struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 845 | u8 flags; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 846 | |
| 847 | BT_DBG("code 0x%2.2x", code); |
| 848 | |
| 849 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 850 | return; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 851 | |
Steven Walter | f6af675 | 2014-11-19 09:41:17 -0500 | [diff] [blame] | 852 | /* Use NO_FLUSH if supported or we have an LE link (which does |
| 853 | * not support auto-flushing packets) */ |
| 854 | if (lmp_no_flush_capable(conn->hcon->hdev) || |
| 855 | conn->hcon->type == LE_LINK) |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 856 | flags = ACL_START_NO_FLUSH; |
| 857 | else |
| 858 | flags = ACL_START; |
| 859 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 860 | bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 861 | skb->priority = HCI_PRIO_MAX; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 862 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 863 | hci_send_acl(conn->hchan, skb, flags); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 864 | } |
| 865 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 866 | static bool __chan_is_moving(struct l2cap_chan *chan) |
| 867 | { |
| 868 | return chan->move_state != L2CAP_MOVE_STABLE && |
| 869 | chan->move_state != L2CAP_MOVE_WAIT_PREPARE; |
| 870 | } |
| 871 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 872 | static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) |
| 873 | { |
| 874 | struct hci_conn *hcon = chan->conn->hcon; |
| 875 | u16 flags; |
| 876 | |
| 877 | BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 878 | skb->priority); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 879 | |
Mat Martineau | d5f8a75 | 2012-10-23 15:24:18 -0700 | [diff] [blame] | 880 | if (chan->hs_hcon && !__chan_is_moving(chan)) { |
| 881 | if (chan->hs_hchan) |
| 882 | hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE); |
| 883 | else |
| 884 | kfree_skb(skb); |
| 885 | |
| 886 | return; |
| 887 | } |
| 888 | |
Steven Walter | f6af675 | 2014-11-19 09:41:17 -0500 | [diff] [blame] | 889 | /* Use NO_FLUSH for LE links (where this is the only option) or |
| 890 | * if the BR/EDR link supports it and flushing has not been |
| 891 | * explicitly requested (through FLAG_FLUSHABLE). |
| 892 | */ |
| 893 | if (hcon->type == LE_LINK || |
| 894 | (!test_bit(FLAG_FLUSHABLE, &chan->flags) && |
| 895 | lmp_no_flush_capable(hcon->hdev))) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 896 | flags = ACL_START_NO_FLUSH; |
| 897 | else |
| 898 | flags = ACL_START; |
| 899 | |
| 900 | bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 901 | hci_send_acl(chan->conn->hchan, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 904 | static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control) |
| 905 | { |
| 906 | control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT; |
| 907 | control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT; |
| 908 | |
| 909 | if (enh & L2CAP_CTRL_FRAME_TYPE) { |
| 910 | /* S-Frame */ |
| 911 | control->sframe = 1; |
| 912 | control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT; |
| 913 | control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT; |
| 914 | |
| 915 | control->sar = 0; |
| 916 | control->txseq = 0; |
| 917 | } else { |
| 918 | /* I-Frame */ |
| 919 | control->sframe = 0; |
| 920 | control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT; |
| 921 | control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT; |
| 922 | |
| 923 | control->poll = 0; |
| 924 | control->super = 0; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control) |
| 929 | { |
| 930 | control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT; |
| 931 | control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT; |
| 932 | |
| 933 | if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) { |
| 934 | /* S-Frame */ |
| 935 | control->sframe = 1; |
| 936 | control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT; |
| 937 | control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT; |
| 938 | |
| 939 | control->sar = 0; |
| 940 | control->txseq = 0; |
| 941 | } else { |
| 942 | /* I-Frame */ |
| 943 | control->sframe = 0; |
| 944 | control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT; |
| 945 | control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT; |
| 946 | |
| 947 | control->poll = 0; |
| 948 | control->super = 0; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | static inline void __unpack_control(struct l2cap_chan *chan, |
| 953 | struct sk_buff *skb) |
| 954 | { |
| 955 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 956 | __unpack_extended_control(get_unaligned_le32(skb->data), |
| 957 | &bt_cb(skb)->control); |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 958 | skb_pull(skb, L2CAP_EXT_CTRL_SIZE); |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 959 | } else { |
| 960 | __unpack_enhanced_control(get_unaligned_le16(skb->data), |
| 961 | &bt_cb(skb)->control); |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 962 | skb_pull(skb, L2CAP_ENH_CTRL_SIZE); |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | |
| 966 | static u32 __pack_extended_control(struct l2cap_ctrl *control) |
| 967 | { |
| 968 | u32 packed; |
| 969 | |
| 970 | packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT; |
| 971 | packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT; |
| 972 | |
| 973 | if (control->sframe) { |
| 974 | packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT; |
| 975 | packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT; |
| 976 | packed |= L2CAP_EXT_CTRL_FRAME_TYPE; |
| 977 | } else { |
| 978 | packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT; |
| 979 | packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT; |
| 980 | } |
| 981 | |
| 982 | return packed; |
| 983 | } |
| 984 | |
| 985 | static u16 __pack_enhanced_control(struct l2cap_ctrl *control) |
| 986 | { |
| 987 | u16 packed; |
| 988 | |
| 989 | packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT; |
| 990 | packed |= control->final << L2CAP_CTRL_FINAL_SHIFT; |
| 991 | |
| 992 | if (control->sframe) { |
| 993 | packed |= control->poll << L2CAP_CTRL_POLL_SHIFT; |
| 994 | packed |= control->super << L2CAP_CTRL_SUPER_SHIFT; |
| 995 | packed |= L2CAP_CTRL_FRAME_TYPE; |
| 996 | } else { |
| 997 | packed |= control->sar << L2CAP_CTRL_SAR_SHIFT; |
| 998 | packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT; |
| 999 | } |
| 1000 | |
| 1001 | return packed; |
| 1002 | } |
| 1003 | |
| 1004 | static inline void __pack_control(struct l2cap_chan *chan, |
| 1005 | struct l2cap_ctrl *control, |
| 1006 | struct sk_buff *skb) |
| 1007 | { |
| 1008 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 1009 | put_unaligned_le32(__pack_extended_control(control), |
| 1010 | skb->data + L2CAP_HDR_SIZE); |
| 1011 | } else { |
| 1012 | put_unaligned_le16(__pack_enhanced_control(control), |
| 1013 | skb->data + L2CAP_HDR_SIZE); |
| 1014 | } |
| 1015 | } |
| 1016 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 1017 | static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan) |
| 1018 | { |
| 1019 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 1020 | return L2CAP_EXT_HDR_SIZE; |
| 1021 | else |
| 1022 | return L2CAP_ENH_HDR_SIZE; |
| 1023 | } |
| 1024 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1025 | static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan, |
| 1026 | u32 control) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1027 | { |
| 1028 | struct sk_buff *skb; |
| 1029 | struct l2cap_hdr *lh; |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 1030 | int hlen = __ertm_hdr_size(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1031 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1032 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1033 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1034 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1035 | skb = bt_skb_alloc(hlen, GFP_KERNEL); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1036 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1037 | if (!skb) |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1038 | return ERR_PTR(-ENOMEM); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1039 | |
| 1040 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1041 | lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1042 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1043 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1044 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 1045 | put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 1046 | else |
| 1047 | put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1048 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1049 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1050 | u16 fcs = crc16(0, (u8 *)skb->data, skb->len); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1051 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1052 | } |
| 1053 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1054 | skb->priority = HCI_PRIO_MAX; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1055 | return skb; |
| 1056 | } |
| 1057 | |
| 1058 | static void l2cap_send_sframe(struct l2cap_chan *chan, |
| 1059 | struct l2cap_ctrl *control) |
| 1060 | { |
| 1061 | struct sk_buff *skb; |
| 1062 | u32 control_field; |
| 1063 | |
| 1064 | BT_DBG("chan %p, control %p", chan, control); |
| 1065 | |
| 1066 | if (!control->sframe) |
| 1067 | return; |
| 1068 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1069 | if (__chan_is_moving(chan)) |
| 1070 | return; |
| 1071 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1072 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) && |
| 1073 | !control->poll) |
| 1074 | control->final = 1; |
| 1075 | |
| 1076 | if (control->super == L2CAP_SUPER_RR) |
| 1077 | clear_bit(CONN_RNR_SENT, &chan->conn_state); |
| 1078 | else if (control->super == L2CAP_SUPER_RNR) |
| 1079 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
| 1080 | |
| 1081 | if (control->super != L2CAP_SUPER_SREJ) { |
| 1082 | chan->last_acked_seq = control->reqseq; |
| 1083 | __clear_ack_timer(chan); |
| 1084 | } |
| 1085 | |
| 1086 | BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq, |
| 1087 | control->final, control->poll, control->super); |
| 1088 | |
| 1089 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 1090 | control_field = __pack_extended_control(control); |
| 1091 | else |
| 1092 | control_field = __pack_enhanced_control(control); |
| 1093 | |
| 1094 | skb = l2cap_create_sframe_pdu(chan, control_field); |
| 1095 | if (!IS_ERR(skb)) |
| 1096 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1097 | } |
| 1098 | |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1099 | static void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, bool poll) |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 1100 | { |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1101 | struct l2cap_ctrl control; |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 1102 | |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1103 | BT_DBG("chan %p, poll %d", chan, poll); |
| 1104 | |
| 1105 | memset(&control, 0, sizeof(control)); |
| 1106 | control.sframe = 1; |
| 1107 | control.poll = poll; |
| 1108 | |
| 1109 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 1110 | control.super = L2CAP_SUPER_RNR; |
| 1111 | else |
| 1112 | control.super = L2CAP_SUPER_RR; |
| 1113 | |
| 1114 | control.reqseq = chan->buffer_seq; |
| 1115 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 1116 | } |
| 1117 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1118 | static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 1119 | { |
Johan Hedberg | 5ff6f34 | 2014-08-07 22:56:43 +0300 | [diff] [blame] | 1120 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
| 1121 | return true; |
| 1122 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1123 | return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 1124 | } |
| 1125 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1126 | static bool __amp_capable(struct l2cap_chan *chan) |
| 1127 | { |
| 1128 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 1df7b17 | 2013-10-05 11:47:49 -0700 | [diff] [blame] | 1129 | struct hci_dev *hdev; |
| 1130 | bool amp_available = false; |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1131 | |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 1132 | if (!(conn->local_fixed_chan & L2CAP_FC_A2MP)) |
Marcel Holtmann | 1df7b17 | 2013-10-05 11:47:49 -0700 | [diff] [blame] | 1133 | return false; |
| 1134 | |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 1135 | if (!(conn->remote_fixed_chan & L2CAP_FC_A2MP)) |
Marcel Holtmann | 1df7b17 | 2013-10-05 11:47:49 -0700 | [diff] [blame] | 1136 | return false; |
| 1137 | |
| 1138 | read_lock(&hci_dev_list_lock); |
| 1139 | list_for_each_entry(hdev, &hci_dev_list, list) { |
| 1140 | if (hdev->amp_type != AMP_TYPE_BREDR && |
| 1141 | test_bit(HCI_UP, &hdev->flags)) { |
| 1142 | amp_available = true; |
| 1143 | break; |
| 1144 | } |
| 1145 | } |
| 1146 | read_unlock(&hci_dev_list_lock); |
| 1147 | |
| 1148 | if (chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED) |
| 1149 | return amp_available; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 1150 | |
| 1151 | return false; |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1152 | } |
| 1153 | |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 1154 | static bool l2cap_check_efs(struct l2cap_chan *chan) |
| 1155 | { |
| 1156 | /* Check EFS parameters */ |
| 1157 | return true; |
| 1158 | } |
| 1159 | |
Andrei Emeltchenko | 2766be4 | 2012-09-27 17:26:21 +0300 | [diff] [blame] | 1160 | void l2cap_send_conn_req(struct l2cap_chan *chan) |
Andrei Emeltchenko | 9b27f35 | 2012-02-24 16:00:00 +0200 | [diff] [blame] | 1161 | { |
| 1162 | struct l2cap_conn *conn = chan->conn; |
| 1163 | struct l2cap_conn_req req; |
| 1164 | |
| 1165 | req.scid = cpu_to_le16(chan->scid); |
| 1166 | req.psm = chan->psm; |
| 1167 | |
| 1168 | chan->ident = l2cap_get_ident(conn); |
| 1169 | |
| 1170 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
| 1171 | |
| 1172 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req); |
| 1173 | } |
| 1174 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 1175 | static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id) |
| 1176 | { |
| 1177 | struct l2cap_create_chan_req req; |
| 1178 | req.scid = cpu_to_le16(chan->scid); |
| 1179 | req.psm = chan->psm; |
| 1180 | req.amp_id = amp_id; |
| 1181 | |
| 1182 | chan->ident = l2cap_get_ident(chan->conn); |
| 1183 | |
| 1184 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ, |
| 1185 | sizeof(req), &req); |
| 1186 | } |
| 1187 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 1188 | static void l2cap_move_setup(struct l2cap_chan *chan) |
| 1189 | { |
| 1190 | struct sk_buff *skb; |
| 1191 | |
| 1192 | BT_DBG("chan %p", chan); |
| 1193 | |
| 1194 | if (chan->mode != L2CAP_MODE_ERTM) |
| 1195 | return; |
| 1196 | |
| 1197 | __clear_retrans_timer(chan); |
| 1198 | __clear_monitor_timer(chan); |
| 1199 | __clear_ack_timer(chan); |
| 1200 | |
| 1201 | chan->retry_count = 0; |
| 1202 | skb_queue_walk(&chan->tx_q, skb) { |
| 1203 | if (bt_cb(skb)->control.retries) |
| 1204 | bt_cb(skb)->control.retries = 1; |
| 1205 | else |
| 1206 | break; |
| 1207 | } |
| 1208 | |
| 1209 | chan->expected_tx_seq = chan->buffer_seq; |
| 1210 | |
| 1211 | clear_bit(CONN_REJ_ACT, &chan->conn_state); |
| 1212 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 1213 | l2cap_seq_list_clear(&chan->retrans_list); |
| 1214 | l2cap_seq_list_clear(&chan->srej_list); |
| 1215 | skb_queue_purge(&chan->srej_q); |
| 1216 | |
| 1217 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 1218 | chan->rx_state = L2CAP_RX_STATE_MOVE; |
| 1219 | |
| 1220 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 1221 | } |
| 1222 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 1223 | static void l2cap_move_done(struct l2cap_chan *chan) |
| 1224 | { |
| 1225 | u8 move_role = chan->move_role; |
| 1226 | BT_DBG("chan %p", chan); |
| 1227 | |
| 1228 | chan->move_state = L2CAP_MOVE_STABLE; |
| 1229 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 1230 | |
| 1231 | if (chan->mode != L2CAP_MODE_ERTM) |
| 1232 | return; |
| 1233 | |
| 1234 | switch (move_role) { |
| 1235 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 1236 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL); |
| 1237 | chan->rx_state = L2CAP_RX_STATE_WAIT_F; |
| 1238 | break; |
| 1239 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 1240 | chan->rx_state = L2CAP_RX_STATE_WAIT_P; |
| 1241 | break; |
| 1242 | } |
| 1243 | } |
| 1244 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1245 | static void l2cap_chan_ready(struct l2cap_chan *chan) |
| 1246 | { |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 1247 | /* This clears all conf flags, including CONF_NOT_COMPLETE */ |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1248 | chan->conf_state = 0; |
| 1249 | __clear_chan_timer(chan); |
| 1250 | |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 1251 | if (chan->mode == L2CAP_MODE_LE_FLOWCTL && !chan->tx_credits) |
| 1252 | chan->ops->suspend(chan); |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 1253 | |
Andrei Emeltchenko | 54a59aa | 2012-05-27 22:27:53 -0300 | [diff] [blame] | 1254 | chan->state = BT_CONNECTED; |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1255 | |
Andrei Emeltchenko | fd83e2c | 2012-05-30 09:55:32 +0300 | [diff] [blame] | 1256 | chan->ops->ready(chan); |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1257 | } |
| 1258 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1259 | static void l2cap_le_connect(struct l2cap_chan *chan) |
| 1260 | { |
| 1261 | struct l2cap_conn *conn = chan->conn; |
| 1262 | struct l2cap_le_conn_req req; |
| 1263 | |
Johan Hedberg | 595177f | 2013-12-02 22:12:22 +0200 | [diff] [blame] | 1264 | if (test_and_set_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags)) |
| 1265 | return; |
| 1266 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1267 | req.psm = chan->psm; |
| 1268 | req.scid = cpu_to_le16(chan->scid); |
| 1269 | req.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 1270 | req.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 1271 | req.credits = cpu_to_le16(chan->rx_credits); |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1272 | |
| 1273 | chan->ident = l2cap_get_ident(conn); |
| 1274 | |
| 1275 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_REQ, |
| 1276 | sizeof(req), &req); |
| 1277 | } |
| 1278 | |
| 1279 | static void l2cap_le_start(struct l2cap_chan *chan) |
| 1280 | { |
| 1281 | struct l2cap_conn *conn = chan->conn; |
| 1282 | |
| 1283 | if (!smp_conn_security(conn->hcon, chan->sec_level)) |
| 1284 | return; |
| 1285 | |
| 1286 | if (!chan->psm) { |
| 1287 | l2cap_chan_ready(chan); |
| 1288 | return; |
| 1289 | } |
| 1290 | |
| 1291 | if (chan->state == BT_CONNECT) |
| 1292 | l2cap_le_connect(chan); |
| 1293 | } |
| 1294 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1295 | static void l2cap_start_connection(struct l2cap_chan *chan) |
| 1296 | { |
| 1297 | if (__amp_capable(chan)) { |
| 1298 | BT_DBG("chan %p AMP capable: discover AMPs", chan); |
| 1299 | a2mp_discover_amp(chan); |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1300 | } else if (chan->conn->hcon->type == LE_LINK) { |
| 1301 | l2cap_le_start(chan); |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1302 | } else { |
| 1303 | l2cap_send_conn_req(chan); |
| 1304 | } |
| 1305 | } |
| 1306 | |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1307 | static void l2cap_request_info(struct l2cap_conn *conn) |
| 1308 | { |
| 1309 | struct l2cap_info_req req; |
| 1310 | |
| 1311 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
| 1312 | return; |
| 1313 | |
| 1314 | req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 1315 | |
| 1316 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 1317 | conn->info_ident = l2cap_get_ident(conn); |
| 1318 | |
| 1319 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
| 1320 | |
| 1321 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 1322 | sizeof(req), &req); |
| 1323 | } |
| 1324 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1325 | static void l2cap_do_start(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1326 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1327 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1328 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1329 | if (conn->hcon->type == LE_LINK) { |
Johan Hedberg | 96ac34f | 2013-05-13 11:15:07 +0300 | [diff] [blame] | 1330 | l2cap_le_start(chan); |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1331 | return; |
| 1332 | } |
| 1333 | |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1334 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)) { |
| 1335 | l2cap_request_info(conn); |
| 1336 | return; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1337 | } |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1338 | |
| 1339 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) |
| 1340 | return; |
| 1341 | |
| 1342 | if (l2cap_chan_check_security(chan, true) && |
| 1343 | __l2cap_no_conn_pending(chan)) |
| 1344 | l2cap_start_connection(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1345 | } |
| 1346 | |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 1347 | static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) |
| 1348 | { |
| 1349 | u32 local_feat_mask = l2cap_feat_mask; |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 1350 | if (!disable_ertm) |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 1351 | local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; |
| 1352 | |
| 1353 | switch (mode) { |
| 1354 | case L2CAP_MODE_ERTM: |
| 1355 | return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; |
| 1356 | case L2CAP_MODE_STREAMING: |
| 1357 | return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; |
| 1358 | default: |
| 1359 | return 0x00; |
| 1360 | } |
| 1361 | } |
| 1362 | |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1363 | static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err) |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1364 | { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1365 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1366 | struct l2cap_disconn_req req; |
| 1367 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1368 | if (!conn) |
| 1369 | return; |
| 1370 | |
Andrei Emeltchenko | aad3d0e | 2012-09-06 15:05:42 +0300 | [diff] [blame] | 1371 | if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1372 | __clear_retrans_timer(chan); |
| 1373 | __clear_monitor_timer(chan); |
| 1374 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1375 | } |
| 1376 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 1377 | if (chan->scid == L2CAP_CID_A2MP) { |
Gustavo Padovan | d117773 | 2012-10-06 11:47:38 +0100 | [diff] [blame] | 1378 | l2cap_state_change(chan, BT_DISCONN); |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1379 | return; |
| 1380 | } |
| 1381 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1382 | req.dcid = cpu_to_le16(chan->dcid); |
| 1383 | req.scid = cpu_to_le16(chan->scid); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1384 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ, |
| 1385 | sizeof(req), &req); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1386 | |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 1387 | l2cap_state_change_and_error(chan, BT_DISCONN, err); |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1388 | } |
| 1389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | /* ---- L2CAP connections ---- */ |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1391 | static void l2cap_conn_start(struct l2cap_conn *conn) |
| 1392 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1393 | struct l2cap_chan *chan, *tmp; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1394 | |
| 1395 | BT_DBG("conn %p", conn); |
| 1396 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1397 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1398 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1399 | list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1400 | l2cap_chan_lock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1401 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1402 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1403 | l2cap_chan_ready(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1404 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1405 | continue; |
| 1406 | } |
| 1407 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1408 | if (chan->state == BT_CONNECT) { |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 1409 | if (!l2cap_chan_check_security(chan, true) || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1410 | !__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1411 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1412 | continue; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 1413 | } |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1414 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1415 | if (!l2cap_mode_supported(chan->mode, conn->feat_mask) |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1416 | && test_bit(CONF_STATE2_DEVICE, |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1417 | &chan->conf_state)) { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1418 | l2cap_chan_close(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1419 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1420 | continue; |
| 1421 | } |
| 1422 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1423 | l2cap_start_connection(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1424 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1425 | } else if (chan->state == BT_CONNECT2) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1426 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1427 | char buf[128]; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1428 | rsp.scid = cpu_to_le16(chan->dcid); |
| 1429 | rsp.dcid = cpu_to_le16(chan->scid); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1430 | |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 1431 | if (l2cap_chan_check_security(chan, false)) { |
Marcel Holtmann | bdc2578 | 2013-10-14 02:45:34 -0700 | [diff] [blame] | 1432 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1433 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 1434 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 1435 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 1436 | |
| 1437 | } else { |
Gustavo Padovan | acdcabf | 2013-10-21 14:21:39 -0200 | [diff] [blame] | 1438 | l2cap_state_change(chan, BT_CONFIG); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1439 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 1440 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 1441 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1442 | } else { |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1443 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 1444 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1445 | } |
| 1446 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1447 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1448 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1449 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1450 | if (test_bit(CONF_REQ_SENT, &chan->conf_state) || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1451 | rsp.result != L2CAP_CR_SUCCESS) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1452 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1453 | continue; |
| 1454 | } |
| 1455 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1456 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1457 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1458 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1459 | chan->num_conf_req++; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1460 | } |
| 1461 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1462 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1463 | } |
| 1464 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1465 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1466 | } |
| 1467 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1468 | static void l2cap_le_conn_ready(struct l2cap_conn *conn) |
| 1469 | { |
Marcel Holtmann | cc8dba2 | 2013-10-18 03:43:01 -0700 | [diff] [blame] | 1470 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 1471 | struct hci_dev *hdev = hcon->hdev; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1472 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 1473 | BT_DBG("%s conn %p", hdev->name, conn); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1474 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 1475 | /* For outgoing pairing which doesn't necessarily have an |
| 1476 | * associated socket (e.g. mgmt_pair_device). |
| 1477 | */ |
| 1478 | if (hcon->out) |
| 1479 | smp_conn_security(hcon, hcon->pending_sec_level); |
Marcel Holtmann | cc8dba2 | 2013-10-18 03:43:01 -0700 | [diff] [blame] | 1480 | |
Marcel Holtmann | 80afeb6 | 2014-06-23 12:18:51 +0200 | [diff] [blame] | 1481 | /* For LE slave connections, make sure the connection interval |
| 1482 | * is in the range of the minium and maximum interval that has |
| 1483 | * been configured for this connection. If not, then trigger |
| 1484 | * the connection update procedure. |
| 1485 | */ |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1486 | if (hcon->role == HCI_ROLE_SLAVE && |
Marcel Holtmann | 80afeb6 | 2014-06-23 12:18:51 +0200 | [diff] [blame] | 1487 | (hcon->le_conn_interval < hcon->le_conn_min_interval || |
| 1488 | hcon->le_conn_interval > hcon->le_conn_max_interval)) { |
| 1489 | struct l2cap_conn_param_update_req req; |
| 1490 | |
| 1491 | req.min = cpu_to_le16(hcon->le_conn_min_interval); |
| 1492 | req.max = cpu_to_le16(hcon->le_conn_max_interval); |
| 1493 | req.latency = cpu_to_le16(hcon->le_conn_latency); |
| 1494 | req.to_multiplier = cpu_to_le16(hcon->le_supv_timeout); |
| 1495 | |
| 1496 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 1497 | L2CAP_CONN_PARAM_UPDATE_REQ, sizeof(req), &req); |
| 1498 | } |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1499 | } |
| 1500 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1501 | static void l2cap_conn_ready(struct l2cap_conn *conn) |
| 1502 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1503 | struct l2cap_chan *chan; |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1504 | struct hci_conn *hcon = conn->hcon; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1505 | |
| 1506 | BT_DBG("conn %p", conn); |
| 1507 | |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1508 | if (hcon->type == ACL_LINK) |
| 1509 | l2cap_request_info(conn); |
| 1510 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 1511 | mutex_lock(&conn->chan_lock); |
| 1512 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1513 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1514 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1515 | l2cap_chan_lock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1516 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 1517 | if (chan->scid == L2CAP_CID_A2MP) { |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1518 | l2cap_chan_unlock(chan); |
| 1519 | continue; |
| 1520 | } |
| 1521 | |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1522 | if (hcon->type == LE_LINK) { |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1523 | l2cap_le_start(chan); |
Vinicius Costa Gomes | 6312845 | 2011-06-17 22:46:26 -0300 | [diff] [blame] | 1524 | } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1525 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
| 1526 | l2cap_chan_ready(chan); |
Gustavo Padovan | 1c244f7 | 2012-12-07 03:29:10 -0200 | [diff] [blame] | 1527 | } else if (chan->state == BT_CONNECT) { |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1528 | l2cap_do_start(chan); |
Gustavo Padovan | 1c244f7 | 2012-12-07 03:29:10 -0200 | [diff] [blame] | 1529 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1530 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1531 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1532 | } |
| 1533 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1534 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1535 | |
Johan Hedberg | 79a0572 | 2014-08-08 09:28:04 +0300 | [diff] [blame] | 1536 | if (hcon->type == LE_LINK) |
| 1537 | l2cap_le_conn_ready(conn); |
| 1538 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1539 | queue_work(hcon->hdev->workqueue, &conn->pending_rx_work); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1540 | } |
| 1541 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1542 | /* Notify sockets that we cannot guaranty reliability anymore */ |
| 1543 | static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) |
| 1544 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1545 | struct l2cap_chan *chan; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1546 | |
| 1547 | BT_DBG("conn %p", conn); |
| 1548 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1549 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1550 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1551 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | ecf61bd | 2011-10-11 14:04:32 +0300 | [diff] [blame] | 1552 | if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) |
Gustavo Padovan | 1d8b1fd | 2012-10-06 11:34:52 +0100 | [diff] [blame] | 1553 | l2cap_chan_set_err(chan, err); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1554 | } |
| 1555 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1556 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1557 | } |
| 1558 | |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1559 | static void l2cap_info_timeout(struct work_struct *work) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1560 | { |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1561 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1562 | info_timer.work); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1563 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1564 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 1565 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1566 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1567 | l2cap_conn_start(conn); |
| 1568 | } |
| 1569 | |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1570 | /* |
| 1571 | * l2cap_user |
| 1572 | * External modules can register l2cap_user objects on l2cap_conn. The ->probe |
| 1573 | * callback is called during registration. The ->remove callback is called |
| 1574 | * during unregistration. |
| 1575 | * An l2cap_user object can either be explicitly unregistered or when the |
| 1576 | * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon, |
| 1577 | * l2cap->hchan, .. are valid as long as the remove callback hasn't been called. |
| 1578 | * External modules must own a reference to the l2cap_conn object if they intend |
| 1579 | * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at |
| 1580 | * any time if they don't. |
| 1581 | */ |
| 1582 | |
| 1583 | int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user) |
| 1584 | { |
| 1585 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1586 | int ret; |
| 1587 | |
| 1588 | /* We need to check whether l2cap_conn is registered. If it is not, we |
| 1589 | * must not register the l2cap_user. l2cap_conn_del() is unregisters |
| 1590 | * l2cap_conn objects, but doesn't provide its own locking. Instead, it |
| 1591 | * relies on the parent hci_conn object to be locked. This itself relies |
| 1592 | * on the hci_dev object to be locked. So we must lock the hci device |
| 1593 | * here, too. */ |
| 1594 | |
| 1595 | hci_dev_lock(hdev); |
| 1596 | |
| 1597 | if (user->list.next || user->list.prev) { |
| 1598 | ret = -EINVAL; |
| 1599 | goto out_unlock; |
| 1600 | } |
| 1601 | |
| 1602 | /* conn->hchan is NULL after l2cap_conn_del() was called */ |
| 1603 | if (!conn->hchan) { |
| 1604 | ret = -ENODEV; |
| 1605 | goto out_unlock; |
| 1606 | } |
| 1607 | |
| 1608 | ret = user->probe(conn, user); |
| 1609 | if (ret) |
| 1610 | goto out_unlock; |
| 1611 | |
| 1612 | list_add(&user->list, &conn->users); |
| 1613 | ret = 0; |
| 1614 | |
| 1615 | out_unlock: |
| 1616 | hci_dev_unlock(hdev); |
| 1617 | return ret; |
| 1618 | } |
| 1619 | EXPORT_SYMBOL(l2cap_register_user); |
| 1620 | |
| 1621 | void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user) |
| 1622 | { |
| 1623 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1624 | |
| 1625 | hci_dev_lock(hdev); |
| 1626 | |
| 1627 | if (!user->list.next || !user->list.prev) |
| 1628 | goto out_unlock; |
| 1629 | |
| 1630 | list_del(&user->list); |
| 1631 | user->list.next = NULL; |
| 1632 | user->list.prev = NULL; |
| 1633 | user->remove(conn, user); |
| 1634 | |
| 1635 | out_unlock: |
| 1636 | hci_dev_unlock(hdev); |
| 1637 | } |
| 1638 | EXPORT_SYMBOL(l2cap_unregister_user); |
| 1639 | |
| 1640 | static void l2cap_unregister_all_users(struct l2cap_conn *conn) |
| 1641 | { |
| 1642 | struct l2cap_user *user; |
| 1643 | |
| 1644 | while (!list_empty(&conn->users)) { |
| 1645 | user = list_first_entry(&conn->users, struct l2cap_user, list); |
| 1646 | list_del(&user->list); |
| 1647 | user->list.next = NULL; |
| 1648 | user->list.prev = NULL; |
| 1649 | user->remove(conn, user); |
| 1650 | } |
| 1651 | } |
| 1652 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1653 | static void l2cap_conn_del(struct hci_conn *hcon, int err) |
| 1654 | { |
| 1655 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 1656 | struct l2cap_chan *chan, *l; |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1657 | |
| 1658 | if (!conn) |
| 1659 | return; |
| 1660 | |
| 1661 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 1662 | |
| 1663 | kfree_skb(conn->rx_skb); |
| 1664 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1665 | skb_queue_purge(&conn->pending_rx); |
Jukka Taimisto | 7ab56c3 | 2014-06-12 10:15:13 +0000 | [diff] [blame] | 1666 | |
| 1667 | /* We can not call flush_work(&conn->pending_rx_work) here since we |
| 1668 | * might block if we are running on a worker from the same workqueue |
| 1669 | * pending_rx_work is waiting on. |
| 1670 | */ |
| 1671 | if (work_pending(&conn->pending_rx_work)) |
| 1672 | cancel_work_sync(&conn->pending_rx_work); |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1673 | |
Johan Hedberg | f3d82d0 | 2014-09-05 22:19:50 +0300 | [diff] [blame] | 1674 | if (work_pending(&conn->id_addr_update_work)) |
| 1675 | cancel_work_sync(&conn->id_addr_update_work); |
| 1676 | |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1677 | l2cap_unregister_all_users(conn); |
| 1678 | |
Johan Hedberg | e31fb86 | 2014-08-18 20:33:28 +0300 | [diff] [blame] | 1679 | /* Force the connection to be immediately dropped */ |
| 1680 | hcon->disc_timeout = 0; |
| 1681 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1682 | mutex_lock(&conn->chan_lock); |
| 1683 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1684 | /* Kill channels */ |
| 1685 | list_for_each_entry_safe(chan, l, &conn->chan_l, list) { |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1686 | l2cap_chan_hold(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1687 | l2cap_chan_lock(chan); |
| 1688 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1689 | l2cap_chan_del(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1690 | |
| 1691 | l2cap_chan_unlock(chan); |
| 1692 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 1693 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1694 | l2cap_chan_put(chan); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1695 | } |
| 1696 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1697 | mutex_unlock(&conn->chan_lock); |
| 1698 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1699 | hci_chan_del(conn->hchan); |
| 1700 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1701 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1702 | cancel_delayed_work_sync(&conn->info_timer); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1703 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1704 | hcon->l2cap_data = NULL; |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1705 | conn->hchan = NULL; |
| 1706 | l2cap_conn_put(conn); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1707 | } |
| 1708 | |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1709 | static void l2cap_conn_free(struct kref *ref) |
| 1710 | { |
| 1711 | struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref); |
| 1712 | |
| 1713 | hci_conn_put(conn->hcon); |
| 1714 | kfree(conn); |
| 1715 | } |
| 1716 | |
Johan Hedberg | 51bb8457 | 2014-08-15 21:06:57 +0300 | [diff] [blame] | 1717 | struct l2cap_conn *l2cap_conn_get(struct l2cap_conn *conn) |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1718 | { |
| 1719 | kref_get(&conn->ref); |
Johan Hedberg | 51bb8457 | 2014-08-15 21:06:57 +0300 | [diff] [blame] | 1720 | return conn; |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1721 | } |
| 1722 | EXPORT_SYMBOL(l2cap_conn_get); |
| 1723 | |
| 1724 | void l2cap_conn_put(struct l2cap_conn *conn) |
| 1725 | { |
| 1726 | kref_put(&conn->ref, l2cap_conn_free); |
| 1727 | } |
| 1728 | EXPORT_SYMBOL(l2cap_conn_put); |
| 1729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | /* ---- Socket interface ---- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1732 | /* Find socket with psm and source / destination bdaddr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | * Returns closest match. |
| 1734 | */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1735 | static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, |
| 1736 | bdaddr_t *src, |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 1737 | bdaddr_t *dst, |
| 1738 | u8 link_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1740 | struct l2cap_chan *c, *c1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1742 | read_lock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1743 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1744 | list_for_each_entry(c, &chan_list, global_l) { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1745 | if (state && c->state != state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | continue; |
| 1747 | |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 1748 | if (link_type == ACL_LINK && c->src_type != BDADDR_BREDR) |
| 1749 | continue; |
| 1750 | |
| 1751 | if (link_type == LE_LINK && c->src_type == BDADDR_BREDR) |
| 1752 | continue; |
| 1753 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1754 | if (c->psm == psm) { |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1755 | int src_match, dst_match; |
| 1756 | int src_any, dst_any; |
| 1757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | /* Exact match. */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1759 | src_match = !bacmp(&c->src, src); |
| 1760 | dst_match = !bacmp(&c->dst, dst); |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1761 | if (src_match && dst_match) { |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 1762 | l2cap_chan_hold(c); |
Johannes Berg | a7567b2 | 2011-06-01 08:29:54 +0200 | [diff] [blame] | 1763 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1764 | return c; |
| 1765 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | |
| 1767 | /* Closest match */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1768 | src_any = !bacmp(&c->src, BDADDR_ANY); |
| 1769 | dst_any = !bacmp(&c->dst, BDADDR_ANY); |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1770 | if ((src_match && dst_any) || (src_any && dst_match) || |
| 1771 | (src_any && dst_any)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1772 | c1 = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | } |
| 1774 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 1776 | if (c1) |
| 1777 | l2cap_chan_hold(c1); |
| 1778 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1779 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1780 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1781 | return c1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1784 | static void l2cap_monitor_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1785 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1786 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1787 | monitor_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1788 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1789 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1790 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1791 | l2cap_chan_lock(chan); |
| 1792 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1793 | if (!chan->conn) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1794 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1795 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1796 | return; |
| 1797 | } |
| 1798 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1799 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1800 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1801 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1802 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1803 | } |
| 1804 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1805 | static void l2cap_retrans_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1806 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1807 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1808 | retrans_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1809 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1810 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1811 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1812 | l2cap_chan_lock(chan); |
| 1813 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1814 | if (!chan->conn) { |
| 1815 | l2cap_chan_unlock(chan); |
| 1816 | l2cap_chan_put(chan); |
| 1817 | return; |
| 1818 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1819 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1820 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1821 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1822 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1823 | } |
| 1824 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 1825 | static void l2cap_streaming_send(struct l2cap_chan *chan, |
| 1826 | struct sk_buff_head *skbs) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1827 | { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1828 | struct sk_buff *skb; |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1829 | struct l2cap_ctrl *control; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1830 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1831 | BT_DBG("chan %p, skbs %p", chan, skbs); |
| 1832 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1833 | if (__chan_is_moving(chan)) |
| 1834 | return; |
| 1835 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1836 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 1837 | |
| 1838 | while (!skb_queue_empty(&chan->tx_q)) { |
| 1839 | |
| 1840 | skb = skb_dequeue(&chan->tx_q); |
| 1841 | |
| 1842 | bt_cb(skb)->control.retries = 1; |
| 1843 | control = &bt_cb(skb)->control; |
| 1844 | |
| 1845 | control->reqseq = 0; |
| 1846 | control->txseq = chan->next_tx_seq; |
| 1847 | |
| 1848 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1849 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1850 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1851 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 1852 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1853 | } |
| 1854 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1855 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1856 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1857 | BT_DBG("Sent txseq %u", control->txseq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1858 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1859 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1860 | chan->frames_sent++; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1861 | } |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1862 | } |
| 1863 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1864 | static int l2cap_ertm_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1865 | { |
| 1866 | struct sk_buff *skb, *tx_skb; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1867 | struct l2cap_ctrl *control; |
| 1868 | int sent = 0; |
| 1869 | |
| 1870 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1871 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1872 | if (chan->state != BT_CONNECTED) |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1873 | return -ENOTCONN; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1874 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 1875 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 1876 | return 0; |
| 1877 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1878 | if (__chan_is_moving(chan)) |
| 1879 | return 0; |
| 1880 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1881 | while (chan->tx_send_head && |
| 1882 | chan->unacked_frames < chan->remote_tx_win && |
| 1883 | chan->tx_state == L2CAP_TX_STATE_XMIT) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1884 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1885 | skb = chan->tx_send_head; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1886 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1887 | bt_cb(skb)->control.retries = 1; |
| 1888 | control = &bt_cb(skb)->control; |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1889 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1890 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1891 | control->final = 1; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1892 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1893 | control->reqseq = chan->buffer_seq; |
| 1894 | chan->last_acked_seq = chan->buffer_seq; |
| 1895 | control->txseq = chan->next_tx_seq; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1896 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1897 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1898 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1899 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1900 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 1901 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1902 | } |
| 1903 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1904 | /* Clone after data has been modified. Data is assumed to be |
| 1905 | read-only (for locking purposes) on cloned sk_buffs. |
| 1906 | */ |
| 1907 | tx_skb = skb_clone(skb, GFP_KERNEL); |
| 1908 | |
| 1909 | if (!tx_skb) |
| 1910 | break; |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1911 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1912 | __set_retrans_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1913 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1914 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1915 | chan->unacked_frames++; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1916 | chan->frames_sent++; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1917 | sent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1918 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1919 | if (skb_queue_is_last(&chan->tx_q, skb)) |
| 1920 | chan->tx_send_head = NULL; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1921 | else |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1922 | chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1923 | |
| 1924 | l2cap_do_send(chan, tx_skb); |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1925 | BT_DBG("Sent txseq %u", control->txseq); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1926 | } |
| 1927 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1928 | BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent, |
| 1929 | chan->unacked_frames, skb_queue_len(&chan->tx_q)); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1930 | |
| 1931 | return sent; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1932 | } |
| 1933 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1934 | static void l2cap_ertm_resend(struct l2cap_chan *chan) |
| 1935 | { |
| 1936 | struct l2cap_ctrl control; |
| 1937 | struct sk_buff *skb; |
| 1938 | struct sk_buff *tx_skb; |
| 1939 | u16 seq; |
| 1940 | |
| 1941 | BT_DBG("chan %p", chan); |
| 1942 | |
| 1943 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 1944 | return; |
| 1945 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1946 | if (__chan_is_moving(chan)) |
| 1947 | return; |
| 1948 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1949 | while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) { |
| 1950 | seq = l2cap_seq_list_pop(&chan->retrans_list); |
| 1951 | |
| 1952 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq); |
| 1953 | if (!skb) { |
| 1954 | BT_DBG("Error: Can't retransmit seq %d, frame missing", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1955 | seq); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1956 | continue; |
| 1957 | } |
| 1958 | |
| 1959 | bt_cb(skb)->control.retries++; |
| 1960 | control = bt_cb(skb)->control; |
| 1961 | |
| 1962 | if (chan->max_tx != 0 && |
| 1963 | bt_cb(skb)->control.retries > chan->max_tx) { |
| 1964 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1965 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1966 | l2cap_seq_list_clear(&chan->retrans_list); |
| 1967 | break; |
| 1968 | } |
| 1969 | |
| 1970 | control.reqseq = chan->buffer_seq; |
| 1971 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
| 1972 | control.final = 1; |
| 1973 | else |
| 1974 | control.final = 0; |
| 1975 | |
| 1976 | if (skb_cloned(skb)) { |
| 1977 | /* Cloned sk_buffs are read-only, so we need a |
| 1978 | * writeable copy |
| 1979 | */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 1980 | tx_skb = skb_copy(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1981 | } else { |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 1982 | tx_skb = skb_clone(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1983 | } |
| 1984 | |
| 1985 | if (!tx_skb) { |
| 1986 | l2cap_seq_list_clear(&chan->retrans_list); |
| 1987 | break; |
| 1988 | } |
| 1989 | |
| 1990 | /* Update skb contents */ |
| 1991 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 1992 | put_unaligned_le32(__pack_extended_control(&control), |
| 1993 | tx_skb->data + L2CAP_HDR_SIZE); |
| 1994 | } else { |
| 1995 | put_unaligned_le16(__pack_enhanced_control(&control), |
| 1996 | tx_skb->data + L2CAP_HDR_SIZE); |
| 1997 | } |
| 1998 | |
Lukasz Rymanowski | 13cac15 | 2014-08-14 09:35:34 +0200 | [diff] [blame] | 1999 | /* Update FCS */ |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2000 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Lukasz Rymanowski | 13cac15 | 2014-08-14 09:35:34 +0200 | [diff] [blame] | 2001 | u16 fcs = crc16(0, (u8 *) tx_skb->data, |
| 2002 | tx_skb->len - L2CAP_FCS_SIZE); |
| 2003 | put_unaligned_le16(fcs, skb_tail_pointer(tx_skb) - |
| 2004 | L2CAP_FCS_SIZE); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2005 | } |
| 2006 | |
| 2007 | l2cap_do_send(chan, tx_skb); |
| 2008 | |
| 2009 | BT_DBG("Resent txseq %d", control.txseq); |
| 2010 | |
| 2011 | chan->last_acked_seq = chan->buffer_seq; |
| 2012 | } |
| 2013 | } |
| 2014 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2015 | static void l2cap_retransmit(struct l2cap_chan *chan, |
| 2016 | struct l2cap_ctrl *control) |
| 2017 | { |
| 2018 | BT_DBG("chan %p, control %p", chan, control); |
| 2019 | |
| 2020 | l2cap_seq_list_append(&chan->retrans_list, control->reqseq); |
| 2021 | l2cap_ertm_resend(chan); |
| 2022 | } |
| 2023 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2024 | static void l2cap_retransmit_all(struct l2cap_chan *chan, |
| 2025 | struct l2cap_ctrl *control) |
| 2026 | { |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2027 | struct sk_buff *skb; |
| 2028 | |
| 2029 | BT_DBG("chan %p, control %p", chan, control); |
| 2030 | |
| 2031 | if (control->poll) |
| 2032 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 2033 | |
| 2034 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2035 | |
| 2036 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 2037 | return; |
| 2038 | |
| 2039 | if (chan->unacked_frames) { |
| 2040 | skb_queue_walk(&chan->tx_q, skb) { |
| 2041 | if (bt_cb(skb)->control.txseq == control->reqseq || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2042 | skb == chan->tx_send_head) |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2043 | break; |
| 2044 | } |
| 2045 | |
| 2046 | skb_queue_walk_from(&chan->tx_q, skb) { |
| 2047 | if (skb == chan->tx_send_head) |
| 2048 | break; |
| 2049 | |
| 2050 | l2cap_seq_list_append(&chan->retrans_list, |
| 2051 | bt_cb(skb)->control.txseq); |
| 2052 | } |
| 2053 | |
| 2054 | l2cap_ertm_resend(chan); |
| 2055 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2056 | } |
| 2057 | |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2058 | static void l2cap_send_ack(struct l2cap_chan *chan) |
| 2059 | { |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2060 | struct l2cap_ctrl control; |
| 2061 | u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 2062 | chan->last_acked_seq); |
| 2063 | int threshold; |
| 2064 | |
| 2065 | BT_DBG("chan %p last_acked_seq %d buffer_seq %d", |
| 2066 | chan, chan->last_acked_seq, chan->buffer_seq); |
| 2067 | |
| 2068 | memset(&control, 0, sizeof(control)); |
| 2069 | control.sframe = 1; |
| 2070 | |
| 2071 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
| 2072 | chan->rx_state == L2CAP_RX_STATE_RECV) { |
| 2073 | __clear_ack_timer(chan); |
| 2074 | control.super = L2CAP_SUPER_RNR; |
| 2075 | control.reqseq = chan->buffer_seq; |
| 2076 | l2cap_send_sframe(chan, &control); |
| 2077 | } else { |
| 2078 | if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) { |
| 2079 | l2cap_ertm_send(chan); |
| 2080 | /* If any i-frames were sent, they included an ack */ |
| 2081 | if (chan->buffer_seq == chan->last_acked_seq) |
| 2082 | frames_to_ack = 0; |
| 2083 | } |
| 2084 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2085 | /* Ack now if the window is 3/4ths full. |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2086 | * Calculate without mul or div |
| 2087 | */ |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2088 | threshold = chan->ack_win; |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2089 | threshold += threshold << 1; |
| 2090 | threshold >>= 2; |
| 2091 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2092 | BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack, |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2093 | threshold); |
| 2094 | |
| 2095 | if (frames_to_ack >= threshold) { |
| 2096 | __clear_ack_timer(chan); |
| 2097 | control.super = L2CAP_SUPER_RR; |
| 2098 | control.reqseq = chan->buffer_seq; |
| 2099 | l2cap_send_sframe(chan, &control); |
| 2100 | frames_to_ack = 0; |
| 2101 | } |
| 2102 | |
| 2103 | if (frames_to_ack) |
| 2104 | __set_ack_timer(chan); |
| 2105 | } |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2106 | } |
| 2107 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2108 | static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, |
| 2109 | struct msghdr *msg, int len, |
| 2110 | int count, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | { |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2112 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2113 | struct sk_buff **frag; |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2114 | int sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | |
Al Viro | 1783639 | 2014-11-24 17:07:38 -0500 | [diff] [blame] | 2116 | if (copy_from_iter(skb_put(skb, count), count, &msg->msg_iter) != count) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2117 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | |
| 2119 | sent += count; |
| 2120 | len -= count; |
| 2121 | |
| 2122 | /* Continuation fragments (no L2CAP header) */ |
| 2123 | frag = &skb_shinfo(skb)->frag_list; |
| 2124 | while (len) { |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2125 | struct sk_buff *tmp; |
| 2126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | count = min_t(unsigned int, conn->mtu, len); |
| 2128 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2129 | tmp = chan->ops->alloc_skb(chan, 0, count, |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2130 | msg->msg_flags & MSG_DONTWAIT); |
| 2131 | if (IS_ERR(tmp)) |
| 2132 | return PTR_ERR(tmp); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2133 | |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2134 | *frag = tmp; |
| 2135 | |
Al Viro | 1783639 | 2014-11-24 17:07:38 -0500 | [diff] [blame] | 2136 | if (copy_from_iter(skb_put(*frag, count), count, |
| 2137 | &msg->msg_iter) != count) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2138 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | |
| 2140 | sent += count; |
| 2141 | len -= count; |
| 2142 | |
Gustavo Padovan | 2d0ed3d | 2012-05-11 13:16:12 -0300 | [diff] [blame] | 2143 | skb->len += (*frag)->len; |
| 2144 | skb->data_len += (*frag)->len; |
| 2145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | frag = &(*frag)->next; |
| 2147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | |
| 2149 | return sent; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2150 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2152 | static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2153 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2154 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2155 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2156 | struct sk_buff *skb; |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2157 | int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2158 | struct l2cap_hdr *lh; |
| 2159 | |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2160 | BT_DBG("chan %p psm 0x%2.2x len %zu", chan, |
| 2161 | __le16_to_cpu(chan->psm), len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2162 | |
| 2163 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2164 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2165 | skb = chan->ops->alloc_skb(chan, hlen, count, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2166 | msg->msg_flags & MSG_DONTWAIT); |
| 2167 | if (IS_ERR(skb)) |
| 2168 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2169 | |
| 2170 | /* Create L2CAP header */ |
| 2171 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2172 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | daf6a78c | 2012-05-03 10:55:52 +0300 | [diff] [blame] | 2173 | lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE); |
Marcel Holtmann | 43b1b8d | 2013-10-12 06:01:26 -0700 | [diff] [blame] | 2174 | put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2175 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2176 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2177 | if (unlikely(err < 0)) { |
| 2178 | kfree_skb(skb); |
| 2179 | return ERR_PTR(err); |
| 2180 | } |
| 2181 | return skb; |
| 2182 | } |
| 2183 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2184 | static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2185 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2186 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2187 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2188 | struct sk_buff *skb; |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2189 | int err, count; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2190 | struct l2cap_hdr *lh; |
| 2191 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2192 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2193 | |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2194 | count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2195 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2196 | skb = chan->ops->alloc_skb(chan, L2CAP_HDR_SIZE, count, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2197 | msg->msg_flags & MSG_DONTWAIT); |
| 2198 | if (IS_ERR(skb)) |
| 2199 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2200 | |
| 2201 | /* Create L2CAP header */ |
| 2202 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2203 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo Padovan | 6ff9b5e | 2012-05-02 11:56:17 -0300 | [diff] [blame] | 2204 | lh->len = cpu_to_le16(len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2205 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2206 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2207 | if (unlikely(err < 0)) { |
| 2208 | kfree_skb(skb); |
| 2209 | return ERR_PTR(err); |
| 2210 | } |
| 2211 | return skb; |
| 2212 | } |
| 2213 | |
Luiz Augusto von Dentz | ab0ff76 | 2011-09-12 20:00:50 +0300 | [diff] [blame] | 2214 | static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2215 | struct msghdr *msg, size_t len, |
| 2216 | u16 sdulen) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2217 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2218 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2219 | struct sk_buff *skb; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2220 | int err, count, hlen; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2221 | struct l2cap_hdr *lh; |
| 2222 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2223 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2224 | |
Gustavo F. Padovan | 0ee0d20 | 2010-05-01 16:15:41 -0300 | [diff] [blame] | 2225 | if (!conn) |
| 2226 | return ERR_PTR(-ENOTCONN); |
| 2227 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2228 | hlen = __ertm_hdr_size(chan); |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2229 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2230 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2231 | hlen += L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2232 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2233 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2234 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2235 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2236 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2237 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2238 | skb = chan->ops->alloc_skb(chan, hlen, count, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2239 | msg->msg_flags & MSG_DONTWAIT); |
| 2240 | if (IS_ERR(skb)) |
| 2241 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2242 | |
| 2243 | /* Create L2CAP header */ |
| 2244 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2245 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2246 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2247 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2248 | /* Control header is populated later */ |
| 2249 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 2250 | put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 2251 | else |
| 2252 | put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2253 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2254 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2255 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2256 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2257 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2258 | if (unlikely(err < 0)) { |
| 2259 | kfree_skb(skb); |
| 2260 | return ERR_PTR(err); |
| 2261 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2262 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2263 | bt_cb(skb)->control.fcs = chan->fcs; |
Mat Martineau | 3ce3514 | 2012-04-25 16:36:14 -0700 | [diff] [blame] | 2264 | bt_cb(skb)->control.retries = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2265 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | } |
| 2267 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2268 | static int l2cap_segment_sdu(struct l2cap_chan *chan, |
| 2269 | struct sk_buff_head *seg_queue, |
| 2270 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2271 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2272 | struct sk_buff *skb; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2273 | u16 sdu_len; |
| 2274 | size_t pdu_len; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2275 | u8 sar; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2276 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2277 | BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2278 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2279 | /* It is critical that ERTM PDUs fit in a single HCI fragment, |
| 2280 | * so fragmented skbs are not used. The HCI layer's handling |
| 2281 | * of fragmented skbs is not compatible with ERTM's queueing. |
| 2282 | */ |
| 2283 | |
| 2284 | /* PDU size is derived from the HCI MTU */ |
| 2285 | pdu_len = chan->conn->mtu; |
| 2286 | |
Mat Martineau | a549574 | 2012-10-23 15:24:21 -0700 | [diff] [blame] | 2287 | /* Constrain PDU size for BR/EDR connections */ |
| 2288 | if (!chan->hs_hcon) |
| 2289 | pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2290 | |
| 2291 | /* Adjust for largest possible L2CAP overhead. */ |
Gustavo Padovan | 35d401d | 2012-05-25 18:57:05 -0300 | [diff] [blame] | 2292 | if (chan->fcs) |
| 2293 | pdu_len -= L2CAP_FCS_SIZE; |
| 2294 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2295 | pdu_len -= __ertm_hdr_size(chan); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2296 | |
| 2297 | /* Remote device may have requested smaller PDUs */ |
| 2298 | pdu_len = min_t(size_t, pdu_len, chan->remote_mps); |
| 2299 | |
| 2300 | if (len <= pdu_len) { |
| 2301 | sar = L2CAP_SAR_UNSEGMENTED; |
| 2302 | sdu_len = 0; |
| 2303 | pdu_len = len; |
| 2304 | } else { |
| 2305 | sar = L2CAP_SAR_START; |
| 2306 | sdu_len = len; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2307 | } |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2308 | |
| 2309 | while (len > 0) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2310 | skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2311 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2312 | if (IS_ERR(skb)) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2313 | __skb_queue_purge(seg_queue); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2314 | return PTR_ERR(skb); |
| 2315 | } |
| 2316 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2317 | bt_cb(skb)->control.sar = sar; |
| 2318 | __skb_queue_tail(seg_queue, skb); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2319 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2320 | len -= pdu_len; |
Lukasz Rymanowski | 069cb27 | 2014-08-13 16:01:41 +0200 | [diff] [blame] | 2321 | if (sdu_len) |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2322 | sdu_len = 0; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2323 | |
| 2324 | if (len <= pdu_len) { |
| 2325 | sar = L2CAP_SAR_END; |
| 2326 | pdu_len = len; |
| 2327 | } else { |
| 2328 | sar = L2CAP_SAR_CONTINUE; |
| 2329 | } |
| 2330 | } |
| 2331 | |
Gustavo Padovan | f0f6279 | 2012-05-29 13:29:17 -0300 | [diff] [blame] | 2332 | return 0; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2333 | } |
| 2334 | |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2335 | static struct sk_buff *l2cap_create_le_flowctl_pdu(struct l2cap_chan *chan, |
| 2336 | struct msghdr *msg, |
| 2337 | size_t len, u16 sdulen) |
| 2338 | { |
| 2339 | struct l2cap_conn *conn = chan->conn; |
| 2340 | struct sk_buff *skb; |
| 2341 | int err, count, hlen; |
| 2342 | struct l2cap_hdr *lh; |
| 2343 | |
| 2344 | BT_DBG("chan %p len %zu", chan, len); |
| 2345 | |
| 2346 | if (!conn) |
| 2347 | return ERR_PTR(-ENOTCONN); |
| 2348 | |
| 2349 | hlen = L2CAP_HDR_SIZE; |
| 2350 | |
| 2351 | if (sdulen) |
| 2352 | hlen += L2CAP_SDULEN_SIZE; |
| 2353 | |
| 2354 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
| 2355 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2356 | skb = chan->ops->alloc_skb(chan, hlen, count, |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2357 | msg->msg_flags & MSG_DONTWAIT); |
| 2358 | if (IS_ERR(skb)) |
| 2359 | return skb; |
| 2360 | |
| 2361 | /* Create L2CAP header */ |
| 2362 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
| 2363 | lh->cid = cpu_to_le16(chan->dcid); |
| 2364 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
| 2365 | |
| 2366 | if (sdulen) |
| 2367 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
| 2368 | |
| 2369 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
| 2370 | if (unlikely(err < 0)) { |
| 2371 | kfree_skb(skb); |
| 2372 | return ERR_PTR(err); |
| 2373 | } |
| 2374 | |
| 2375 | return skb; |
| 2376 | } |
| 2377 | |
| 2378 | static int l2cap_segment_le_sdu(struct l2cap_chan *chan, |
| 2379 | struct sk_buff_head *seg_queue, |
| 2380 | struct msghdr *msg, size_t len) |
| 2381 | { |
| 2382 | struct sk_buff *skb; |
| 2383 | size_t pdu_len; |
| 2384 | u16 sdu_len; |
| 2385 | |
| 2386 | BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); |
| 2387 | |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2388 | sdu_len = len; |
Johan Hedberg | 72c6fb9 | 2014-08-15 21:06:51 +0300 | [diff] [blame] | 2389 | pdu_len = chan->remote_mps - L2CAP_SDULEN_SIZE; |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2390 | |
| 2391 | while (len > 0) { |
| 2392 | if (len <= pdu_len) |
| 2393 | pdu_len = len; |
| 2394 | |
| 2395 | skb = l2cap_create_le_flowctl_pdu(chan, msg, pdu_len, sdu_len); |
| 2396 | if (IS_ERR(skb)) { |
| 2397 | __skb_queue_purge(seg_queue); |
| 2398 | return PTR_ERR(skb); |
| 2399 | } |
| 2400 | |
| 2401 | __skb_queue_tail(seg_queue, skb); |
| 2402 | |
| 2403 | len -= pdu_len; |
| 2404 | |
| 2405 | if (sdu_len) { |
| 2406 | sdu_len = 0; |
| 2407 | pdu_len += L2CAP_SDULEN_SIZE; |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | return 0; |
| 2412 | } |
| 2413 | |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2414 | int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len) |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2415 | { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2416 | struct sk_buff *skb; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2417 | int err; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2418 | struct sk_buff_head seg_queue; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2419 | |
Seung-Woo Kim | 31e8ce8 | 2013-11-05 18:46:33 +0900 | [diff] [blame] | 2420 | if (!chan->conn) |
| 2421 | return -ENOTCONN; |
| 2422 | |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2423 | /* Connectionless channel */ |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2424 | if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2425 | skb = l2cap_create_connless_pdu(chan, msg, len); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2426 | if (IS_ERR(skb)) |
| 2427 | return PTR_ERR(skb); |
| 2428 | |
Andrzej Kaczmarek | ede81a2 | 2014-02-25 17:16:22 +0100 | [diff] [blame] | 2429 | /* Channel lock is released before requesting new skb and then |
| 2430 | * reacquired thus we need to recheck channel state. |
| 2431 | */ |
| 2432 | if (chan->state != BT_CONNECTED) { |
| 2433 | kfree_skb(skb); |
| 2434 | return -ENOTCONN; |
| 2435 | } |
| 2436 | |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2437 | l2cap_do_send(chan, skb); |
| 2438 | return len; |
| 2439 | } |
| 2440 | |
| 2441 | switch (chan->mode) { |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 2442 | case L2CAP_MODE_LE_FLOWCTL: |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2443 | /* Check outgoing MTU */ |
| 2444 | if (len > chan->omtu) |
| 2445 | return -EMSGSIZE; |
| 2446 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 2447 | if (!chan->tx_credits) |
| 2448 | return -EAGAIN; |
| 2449 | |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2450 | __skb_queue_head_init(&seg_queue); |
| 2451 | |
| 2452 | err = l2cap_segment_le_sdu(chan, &seg_queue, msg, len); |
| 2453 | |
| 2454 | if (chan->state != BT_CONNECTED) { |
| 2455 | __skb_queue_purge(&seg_queue); |
| 2456 | err = -ENOTCONN; |
| 2457 | } |
| 2458 | |
| 2459 | if (err) |
| 2460 | return err; |
| 2461 | |
| 2462 | skb_queue_splice_tail_init(&seg_queue, &chan->tx_q); |
| 2463 | |
| 2464 | while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { |
| 2465 | l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); |
| 2466 | chan->tx_credits--; |
| 2467 | } |
| 2468 | |
| 2469 | if (!chan->tx_credits) |
| 2470 | chan->ops->suspend(chan); |
| 2471 | |
| 2472 | err = len; |
| 2473 | |
| 2474 | break; |
| 2475 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 2476 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2477 | /* Check outgoing MTU */ |
| 2478 | if (len > chan->omtu) |
| 2479 | return -EMSGSIZE; |
| 2480 | |
| 2481 | /* Create a basic PDU */ |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2482 | skb = l2cap_create_basic_pdu(chan, msg, len); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2483 | if (IS_ERR(skb)) |
| 2484 | return PTR_ERR(skb); |
| 2485 | |
Andrzej Kaczmarek | ede81a2 | 2014-02-25 17:16:22 +0100 | [diff] [blame] | 2486 | /* Channel lock is released before requesting new skb and then |
| 2487 | * reacquired thus we need to recheck channel state. |
| 2488 | */ |
| 2489 | if (chan->state != BT_CONNECTED) { |
| 2490 | kfree_skb(skb); |
| 2491 | return -ENOTCONN; |
| 2492 | } |
| 2493 | |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2494 | l2cap_do_send(chan, skb); |
| 2495 | err = len; |
| 2496 | break; |
| 2497 | |
| 2498 | case L2CAP_MODE_ERTM: |
| 2499 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2500 | /* Check outgoing MTU */ |
| 2501 | if (len > chan->omtu) { |
| 2502 | err = -EMSGSIZE; |
| 2503 | break; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2504 | } |
| 2505 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2506 | __skb_queue_head_init(&seg_queue); |
| 2507 | |
| 2508 | /* Do segmentation before calling in to the state machine, |
| 2509 | * since it's possible to block while waiting for memory |
| 2510 | * allocation. |
| 2511 | */ |
| 2512 | err = l2cap_segment_sdu(chan, &seg_queue, msg, len); |
| 2513 | |
| 2514 | /* The channel could have been closed while segmenting, |
| 2515 | * check that it is still connected. |
| 2516 | */ |
| 2517 | if (chan->state != BT_CONNECTED) { |
| 2518 | __skb_queue_purge(&seg_queue); |
| 2519 | err = -ENOTCONN; |
| 2520 | } |
| 2521 | |
| 2522 | if (err) |
| 2523 | break; |
| 2524 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2525 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2526 | l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2527 | else |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2528 | l2cap_streaming_send(chan, &seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2529 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2530 | err = len; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2531 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2532 | /* If the skbs were not queued for sending, they'll still be in |
| 2533 | * seg_queue and need to be purged. |
| 2534 | */ |
| 2535 | __skb_queue_purge(&seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2536 | break; |
| 2537 | |
| 2538 | default: |
| 2539 | BT_DBG("bad state %1.1x", chan->mode); |
| 2540 | err = -EBADFD; |
| 2541 | } |
| 2542 | |
| 2543 | return err; |
| 2544 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 2545 | EXPORT_SYMBOL_GPL(l2cap_chan_send); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2546 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2547 | static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq) |
| 2548 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2549 | struct l2cap_ctrl control; |
| 2550 | u16 seq; |
| 2551 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2552 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2553 | |
| 2554 | memset(&control, 0, sizeof(control)); |
| 2555 | control.sframe = 1; |
| 2556 | control.super = L2CAP_SUPER_SREJ; |
| 2557 | |
| 2558 | for (seq = chan->expected_tx_seq; seq != txseq; |
| 2559 | seq = __next_seq(chan, seq)) { |
| 2560 | if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) { |
| 2561 | control.reqseq = seq; |
| 2562 | l2cap_send_sframe(chan, &control); |
| 2563 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2564 | } |
| 2565 | } |
| 2566 | |
| 2567 | chan->expected_tx_seq = __next_seq(chan, txseq); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | static void l2cap_send_srej_tail(struct l2cap_chan *chan) |
| 2571 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2572 | struct l2cap_ctrl control; |
| 2573 | |
| 2574 | BT_DBG("chan %p", chan); |
| 2575 | |
| 2576 | if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR) |
| 2577 | return; |
| 2578 | |
| 2579 | memset(&control, 0, sizeof(control)); |
| 2580 | control.sframe = 1; |
| 2581 | control.super = L2CAP_SUPER_SREJ; |
| 2582 | control.reqseq = chan->srej_list.tail; |
| 2583 | l2cap_send_sframe(chan, &control); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq) |
| 2587 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2588 | struct l2cap_ctrl control; |
| 2589 | u16 initial_head; |
| 2590 | u16 seq; |
| 2591 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2592 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2593 | |
| 2594 | memset(&control, 0, sizeof(control)); |
| 2595 | control.sframe = 1; |
| 2596 | control.super = L2CAP_SUPER_SREJ; |
| 2597 | |
| 2598 | /* Capture initial list head to allow only one pass through the list. */ |
| 2599 | initial_head = chan->srej_list.head; |
| 2600 | |
| 2601 | do { |
| 2602 | seq = l2cap_seq_list_pop(&chan->srej_list); |
| 2603 | if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR) |
| 2604 | break; |
| 2605 | |
| 2606 | control.reqseq = seq; |
| 2607 | l2cap_send_sframe(chan, &control); |
| 2608 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2609 | } while (chan->srej_list.head != initial_head); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2610 | } |
| 2611 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2612 | static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 2613 | { |
| 2614 | struct sk_buff *acked_skb; |
| 2615 | u16 ackseq; |
| 2616 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2617 | BT_DBG("chan %p, reqseq %u", chan, reqseq); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2618 | |
| 2619 | if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq) |
| 2620 | return; |
| 2621 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2622 | BT_DBG("expected_ack_seq %u, unacked_frames %u", |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2623 | chan->expected_ack_seq, chan->unacked_frames); |
| 2624 | |
| 2625 | for (ackseq = chan->expected_ack_seq; ackseq != reqseq; |
| 2626 | ackseq = __next_seq(chan, ackseq)) { |
| 2627 | |
| 2628 | acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq); |
| 2629 | if (acked_skb) { |
| 2630 | skb_unlink(acked_skb, &chan->tx_q); |
| 2631 | kfree_skb(acked_skb); |
| 2632 | chan->unacked_frames--; |
| 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | chan->expected_ack_seq = reqseq; |
| 2637 | |
| 2638 | if (chan->unacked_frames == 0) |
| 2639 | __clear_retrans_timer(chan); |
| 2640 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2641 | BT_DBG("unacked_frames %u", chan->unacked_frames); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan) |
| 2645 | { |
| 2646 | BT_DBG("chan %p", chan); |
| 2647 | |
| 2648 | chan->expected_tx_seq = chan->buffer_seq; |
| 2649 | l2cap_seq_list_clear(&chan->srej_list); |
| 2650 | skb_queue_purge(&chan->srej_q); |
| 2651 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 2652 | } |
| 2653 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2654 | static void l2cap_tx_state_xmit(struct l2cap_chan *chan, |
| 2655 | struct l2cap_ctrl *control, |
| 2656 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2657 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2658 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2659 | event); |
| 2660 | |
| 2661 | switch (event) { |
| 2662 | case L2CAP_EV_DATA_REQUEST: |
| 2663 | if (chan->tx_send_head == NULL) |
| 2664 | chan->tx_send_head = skb_peek(skbs); |
| 2665 | |
| 2666 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2667 | l2cap_ertm_send(chan); |
| 2668 | break; |
| 2669 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2670 | BT_DBG("Enter LOCAL_BUSY"); |
| 2671 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2672 | |
| 2673 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2674 | /* The SREJ_SENT state must be aborted if we are to |
| 2675 | * enter the LOCAL_BUSY state. |
| 2676 | */ |
| 2677 | l2cap_abort_rx_srej_sent(chan); |
| 2678 | } |
| 2679 | |
| 2680 | l2cap_send_ack(chan); |
| 2681 | |
| 2682 | break; |
| 2683 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2684 | BT_DBG("Exit LOCAL_BUSY"); |
| 2685 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2686 | |
| 2687 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2688 | struct l2cap_ctrl local_control; |
| 2689 | |
| 2690 | memset(&local_control, 0, sizeof(local_control)); |
| 2691 | local_control.sframe = 1; |
| 2692 | local_control.super = L2CAP_SUPER_RR; |
| 2693 | local_control.poll = 1; |
| 2694 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2695 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2696 | |
| 2697 | chan->retry_count = 1; |
| 2698 | __set_monitor_timer(chan); |
| 2699 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2700 | } |
| 2701 | break; |
| 2702 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2703 | l2cap_process_reqseq(chan, control->reqseq); |
| 2704 | break; |
| 2705 | case L2CAP_EV_EXPLICIT_POLL: |
| 2706 | l2cap_send_rr_or_rnr(chan, 1); |
| 2707 | chan->retry_count = 1; |
| 2708 | __set_monitor_timer(chan); |
| 2709 | __clear_ack_timer(chan); |
| 2710 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2711 | break; |
| 2712 | case L2CAP_EV_RETRANS_TO: |
| 2713 | l2cap_send_rr_or_rnr(chan, 1); |
| 2714 | chan->retry_count = 1; |
| 2715 | __set_monitor_timer(chan); |
| 2716 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2717 | break; |
| 2718 | case L2CAP_EV_RECV_FBIT: |
| 2719 | /* Nothing to process */ |
| 2720 | break; |
| 2721 | default: |
| 2722 | break; |
| 2723 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2726 | static void l2cap_tx_state_wait_f(struct l2cap_chan *chan, |
| 2727 | struct l2cap_ctrl *control, |
| 2728 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2729 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2730 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2731 | event); |
| 2732 | |
| 2733 | switch (event) { |
| 2734 | case L2CAP_EV_DATA_REQUEST: |
| 2735 | if (chan->tx_send_head == NULL) |
| 2736 | chan->tx_send_head = skb_peek(skbs); |
| 2737 | /* Queue data, but don't send. */ |
| 2738 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2739 | break; |
| 2740 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2741 | BT_DBG("Enter LOCAL_BUSY"); |
| 2742 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2743 | |
| 2744 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2745 | /* The SREJ_SENT state must be aborted if we are to |
| 2746 | * enter the LOCAL_BUSY state. |
| 2747 | */ |
| 2748 | l2cap_abort_rx_srej_sent(chan); |
| 2749 | } |
| 2750 | |
| 2751 | l2cap_send_ack(chan); |
| 2752 | |
| 2753 | break; |
| 2754 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2755 | BT_DBG("Exit LOCAL_BUSY"); |
| 2756 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2757 | |
| 2758 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2759 | struct l2cap_ctrl local_control; |
| 2760 | memset(&local_control, 0, sizeof(local_control)); |
| 2761 | local_control.sframe = 1; |
| 2762 | local_control.super = L2CAP_SUPER_RR; |
| 2763 | local_control.poll = 1; |
| 2764 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2765 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2766 | |
| 2767 | chan->retry_count = 1; |
| 2768 | __set_monitor_timer(chan); |
| 2769 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2770 | } |
| 2771 | break; |
| 2772 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2773 | l2cap_process_reqseq(chan, control->reqseq); |
| 2774 | |
| 2775 | /* Fall through */ |
| 2776 | |
| 2777 | case L2CAP_EV_RECV_FBIT: |
| 2778 | if (control && control->final) { |
| 2779 | __clear_monitor_timer(chan); |
| 2780 | if (chan->unacked_frames > 0) |
| 2781 | __set_retrans_timer(chan); |
| 2782 | chan->retry_count = 0; |
| 2783 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 2784 | BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state); |
| 2785 | } |
| 2786 | break; |
| 2787 | case L2CAP_EV_EXPLICIT_POLL: |
| 2788 | /* Ignore */ |
| 2789 | break; |
| 2790 | case L2CAP_EV_MONITOR_TO: |
| 2791 | if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) { |
| 2792 | l2cap_send_rr_or_rnr(chan, 1); |
| 2793 | __set_monitor_timer(chan); |
| 2794 | chan->retry_count++; |
| 2795 | } else { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 2796 | l2cap_send_disconn_req(chan, ECONNABORTED); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2797 | } |
| 2798 | break; |
| 2799 | default: |
| 2800 | break; |
| 2801 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2802 | } |
| 2803 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2804 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 2805 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2806 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2807 | BT_DBG("chan %p, control %p, skbs %p, event %d, state %d", |
| 2808 | chan, control, skbs, event, chan->tx_state); |
| 2809 | |
| 2810 | switch (chan->tx_state) { |
| 2811 | case L2CAP_TX_STATE_XMIT: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2812 | l2cap_tx_state_xmit(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2813 | break; |
| 2814 | case L2CAP_TX_STATE_WAIT_F: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2815 | l2cap_tx_state_wait_f(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2816 | break; |
| 2817 | default: |
| 2818 | /* Ignore event */ |
| 2819 | break; |
| 2820 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2821 | } |
| 2822 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2823 | static void l2cap_pass_to_tx(struct l2cap_chan *chan, |
| 2824 | struct l2cap_ctrl *control) |
| 2825 | { |
| 2826 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2827 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT); |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2828 | } |
| 2829 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2830 | static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan, |
| 2831 | struct l2cap_ctrl *control) |
| 2832 | { |
| 2833 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2834 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2835 | } |
| 2836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | /* Copy frame to all raw sockets on that connection */ |
| 2838 | static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) |
| 2839 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | struct sk_buff *nskb; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2841 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | |
| 2843 | BT_DBG("conn %p", conn); |
| 2844 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2845 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2846 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2847 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2848 | if (chan->chan_type != L2CAP_CHAN_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | continue; |
| 2850 | |
Gustavo Padovan | 7f5396a | 2013-10-21 18:22:25 -0200 | [diff] [blame] | 2851 | /* Don't send frame to the channel it came from */ |
| 2852 | if (bt_cb(skb)->chan == chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | continue; |
Gustavo Padovan | 7f5396a | 2013-10-21 18:22:25 -0200 | [diff] [blame] | 2854 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2855 | nskb = skb_clone(skb, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 2856 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | continue; |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 2858 | if (chan->ops->recv(chan, nskb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | kfree_skb(nskb); |
| 2860 | } |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2861 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2862 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | /* ---- L2CAP signalling commands ---- */ |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2866 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code, |
| 2867 | u8 ident, u16 dlen, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | { |
| 2869 | struct sk_buff *skb, **frag; |
| 2870 | struct l2cap_cmd_hdr *cmd; |
| 2871 | struct l2cap_hdr *lh; |
| 2872 | int len, count; |
| 2873 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2874 | BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u", |
| 2875 | conn, code, ident, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | |
Anderson Lizardo | 300b962 | 2013-06-02 16:30:40 -0400 | [diff] [blame] | 2877 | if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE) |
| 2878 | return NULL; |
| 2879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2880 | len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; |
| 2881 | count = min_t(unsigned int, conn->mtu, len); |
| 2882 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2883 | skb = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2884 | if (!skb) |
| 2885 | return NULL; |
| 2886 | |
| 2887 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2888 | lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2889 | |
| 2890 | if (conn->hcon->type == LE_LINK) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 2891 | lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2892 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 2893 | lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | |
| 2895 | cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); |
| 2896 | cmd->code = code; |
| 2897 | cmd->ident = ident; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2898 | cmd->len = cpu_to_le16(dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | |
| 2900 | if (dlen) { |
| 2901 | count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; |
| 2902 | memcpy(skb_put(skb, count), data, count); |
| 2903 | data += count; |
| 2904 | } |
| 2905 | |
| 2906 | len -= skb->len; |
| 2907 | |
| 2908 | /* Continuation fragments (no L2CAP header) */ |
| 2909 | frag = &skb_shinfo(skb)->frag_list; |
| 2910 | while (len) { |
| 2911 | count = min_t(unsigned int, conn->mtu, len); |
| 2912 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2913 | *frag = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | if (!*frag) |
| 2915 | goto fail; |
| 2916 | |
| 2917 | memcpy(skb_put(*frag, count), data, count); |
| 2918 | |
| 2919 | len -= count; |
| 2920 | data += count; |
| 2921 | |
| 2922 | frag = &(*frag)->next; |
| 2923 | } |
| 2924 | |
| 2925 | return skb; |
| 2926 | |
| 2927 | fail: |
| 2928 | kfree_skb(skb); |
| 2929 | return NULL; |
| 2930 | } |
| 2931 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2932 | static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, |
| 2933 | unsigned long *val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2934 | { |
| 2935 | struct l2cap_conf_opt *opt = *ptr; |
| 2936 | int len; |
| 2937 | |
| 2938 | len = L2CAP_CONF_OPT_SIZE + opt->len; |
| 2939 | *ptr += len; |
| 2940 | |
| 2941 | *type = opt->type; |
| 2942 | *olen = opt->len; |
| 2943 | |
| 2944 | switch (opt->len) { |
| 2945 | case 1: |
| 2946 | *val = *((u8 *) opt->val); |
| 2947 | break; |
| 2948 | |
| 2949 | case 2: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2950 | *val = get_unaligned_le16(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | break; |
| 2952 | |
| 2953 | case 4: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2954 | *val = get_unaligned_le32(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | break; |
| 2956 | |
| 2957 | default: |
| 2958 | *val = (unsigned long) opt->val; |
| 2959 | break; |
| 2960 | } |
| 2961 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2962 | BT_DBG("type 0x%2.2x len %u val 0x%lx", *type, opt->len, *val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | return len; |
| 2964 | } |
| 2965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) |
| 2967 | { |
| 2968 | struct l2cap_conf_opt *opt = *ptr; |
| 2969 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2970 | BT_DBG("type 0x%2.2x len %u val 0x%lx", type, len, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | |
| 2972 | opt->type = type; |
| 2973 | opt->len = len; |
| 2974 | |
| 2975 | switch (len) { |
| 2976 | case 1: |
| 2977 | *((u8 *) opt->val) = val; |
| 2978 | break; |
| 2979 | |
| 2980 | case 2: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 2981 | put_unaligned_le16(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | break; |
| 2983 | |
| 2984 | case 4: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 2985 | put_unaligned_le32(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | break; |
| 2987 | |
| 2988 | default: |
| 2989 | memcpy(opt->val, (void *) val, len); |
| 2990 | break; |
| 2991 | } |
| 2992 | |
| 2993 | *ptr += L2CAP_CONF_OPT_SIZE + len; |
| 2994 | } |
| 2995 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2996 | static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) |
| 2997 | { |
| 2998 | struct l2cap_conf_efs efs; |
| 2999 | |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 3000 | switch (chan->mode) { |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3001 | case L2CAP_MODE_ERTM: |
| 3002 | efs.id = chan->local_id; |
| 3003 | efs.stype = chan->local_stype; |
| 3004 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 3005 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3006 | efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); |
| 3007 | efs.flush_to = cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3008 | break; |
| 3009 | |
| 3010 | case L2CAP_MODE_STREAMING: |
| 3011 | efs.id = 1; |
| 3012 | efs.stype = L2CAP_SERV_BESTEFFORT; |
| 3013 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 3014 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
| 3015 | efs.acc_lat = 0; |
| 3016 | efs.flush_to = 0; |
| 3017 | break; |
| 3018 | |
| 3019 | default: |
| 3020 | return; |
| 3021 | } |
| 3022 | |
| 3023 | l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 3024 | (unsigned long) &efs); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3025 | } |
| 3026 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3027 | static void l2cap_ack_timeout(struct work_struct *work) |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3028 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3029 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3030 | ack_timer.work); |
| 3031 | u16 frames_to_ack; |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3032 | |
Gustavo F. Padovan | 2fb9b3d | 2011-12-22 16:56:05 -0200 | [diff] [blame] | 3033 | BT_DBG("chan %p", chan); |
| 3034 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3035 | l2cap_chan_lock(chan); |
| 3036 | |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3037 | frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 3038 | chan->last_acked_seq); |
| 3039 | |
| 3040 | if (frames_to_ack) |
| 3041 | l2cap_send_rr_or_rnr(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3042 | |
| 3043 | l2cap_chan_unlock(chan); |
Szymon Janc | 09bfb2e | 2012-01-11 10:59:49 +0100 | [diff] [blame] | 3044 | l2cap_chan_put(chan); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3045 | } |
| 3046 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 3047 | int l2cap_ertm_init(struct l2cap_chan *chan) |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3048 | { |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3049 | int err; |
| 3050 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3051 | chan->next_tx_seq = 0; |
| 3052 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3053 | chan->expected_ack_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3054 | chan->unacked_frames = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3055 | chan->buffer_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3056 | chan->frames_sent = 0; |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3057 | chan->last_acked_seq = 0; |
| 3058 | chan->sdu = NULL; |
| 3059 | chan->sdu_last_frag = NULL; |
| 3060 | chan->sdu_len = 0; |
| 3061 | |
Mat Martineau | d34c34f | 2012-05-14 14:49:27 -0700 | [diff] [blame] | 3062 | skb_queue_head_init(&chan->tx_q); |
| 3063 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3064 | chan->local_amp_id = AMP_ID_BREDR; |
| 3065 | chan->move_id = AMP_ID_BREDR; |
Mat Martineau | 0833328 | 2012-10-23 15:24:06 -0700 | [diff] [blame] | 3066 | chan->move_state = L2CAP_MOVE_STABLE; |
| 3067 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 3068 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3069 | if (chan->mode != L2CAP_MODE_ERTM) |
| 3070 | return 0; |
| 3071 | |
| 3072 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 3073 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3074 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3075 | INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); |
| 3076 | INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); |
| 3077 | INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3078 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3079 | skb_queue_head_init(&chan->srej_q); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3080 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3081 | err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win); |
| 3082 | if (err < 0) |
| 3083 | return err; |
| 3084 | |
Mat Martineau | 9dc9aff | 2012-05-17 16:20:14 -0700 | [diff] [blame] | 3085 | err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win); |
| 3086 | if (err < 0) |
| 3087 | l2cap_seq_list_free(&chan->srej_list); |
| 3088 | |
| 3089 | return err; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3090 | } |
| 3091 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3092 | static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) |
| 3093 | { |
| 3094 | switch (mode) { |
| 3095 | case L2CAP_MODE_STREAMING: |
| 3096 | case L2CAP_MODE_ERTM: |
| 3097 | if (l2cap_mode_supported(mode, remote_feat_mask)) |
| 3098 | return mode; |
| 3099 | /* fall through */ |
| 3100 | default: |
| 3101 | return L2CAP_MODE_BASIC; |
| 3102 | } |
| 3103 | } |
| 3104 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3105 | static inline bool __l2cap_ews_supported(struct l2cap_conn *conn) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3106 | { |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 3107 | return ((conn->local_fixed_chan & L2CAP_FC_A2MP) && |
| 3108 | (conn->feat_mask & L2CAP_FEAT_EXT_WINDOW)); |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3109 | } |
| 3110 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3111 | static inline bool __l2cap_efs_supported(struct l2cap_conn *conn) |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3112 | { |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 3113 | return ((conn->local_fixed_chan & L2CAP_FC_A2MP) && |
| 3114 | (conn->feat_mask & L2CAP_FEAT_EXT_FLOW)); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3115 | } |
| 3116 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3117 | static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan, |
| 3118 | struct l2cap_conf_rfc *rfc) |
| 3119 | { |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3120 | if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) { |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3121 | u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to; |
| 3122 | |
| 3123 | /* Class 1 devices have must have ERTM timeouts |
| 3124 | * exceeding the Link Supervision Timeout. The |
| 3125 | * default Link Supervision Timeout for AMP |
| 3126 | * controllers is 10 seconds. |
| 3127 | * |
| 3128 | * Class 1 devices use 0xffffffff for their |
| 3129 | * best-effort flush timeout, so the clamping logic |
| 3130 | * will result in a timeout that meets the above |
| 3131 | * requirement. ERTM timeouts are 16-bit values, so |
| 3132 | * the maximum timeout is 65.535 seconds. |
| 3133 | */ |
| 3134 | |
| 3135 | /* Convert timeout to milliseconds and round */ |
| 3136 | ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000); |
| 3137 | |
| 3138 | /* This is the recommended formula for class 2 devices |
| 3139 | * that start ERTM timers when packets are sent to the |
| 3140 | * controller. |
| 3141 | */ |
| 3142 | ertm_to = 3 * ertm_to + 500; |
| 3143 | |
| 3144 | if (ertm_to > 0xffff) |
| 3145 | ertm_to = 0xffff; |
| 3146 | |
| 3147 | rfc->retrans_timeout = cpu_to_le16((u16) ertm_to); |
| 3148 | rfc->monitor_timeout = rfc->retrans_timeout; |
| 3149 | } else { |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3150 | rfc->retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); |
| 3151 | rfc->monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3152 | } |
| 3153 | } |
| 3154 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3155 | static inline void l2cap_txwin_setup(struct l2cap_chan *chan) |
| 3156 | { |
| 3157 | if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3158 | __l2cap_ews_supported(chan->conn)) { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3159 | /* use extended control field */ |
| 3160 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3161 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
| 3162 | } else { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3163 | chan->tx_win = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3164 | L2CAP_DEFAULT_TX_WINDOW); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3165 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
| 3166 | } |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3167 | chan->ack_win = chan->tx_win; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3168 | } |
| 3169 | |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3170 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | struct l2cap_conf_req *req = data; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3173 | struct l2cap_conf_rfc rfc = { .mode = chan->mode }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | void *ptr = req->data; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3175 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3177 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3179 | if (chan->num_conf_req || chan->num_conf_rsp) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3180 | goto done; |
| 3181 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3182 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3183 | case L2CAP_MODE_STREAMING: |
| 3184 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3185 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3186 | break; |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3187 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3188 | if (__l2cap_efs_supported(chan->conn)) |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3189 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3190 | |
Gustavo F. Padovan | 2ba13ed | 2010-06-09 16:39:05 -0300 | [diff] [blame] | 3191 | /* fall through */ |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3192 | default: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3193 | chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3194 | break; |
| 3195 | } |
| 3196 | |
| 3197 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3198 | if (chan->imtu != L2CAP_DEFAULT_MTU) |
| 3199 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | 7990681c | 2011-01-24 16:01:43 -0200 | [diff] [blame] | 3200 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3201 | switch (chan->mode) { |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3202 | case L2CAP_MODE_BASIC: |
Marcel Holtmann | 6fea7ad | 2014-07-09 11:53:35 +0200 | [diff] [blame] | 3203 | if (disable_ertm) |
| 3204 | break; |
| 3205 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3206 | if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3207 | !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3208 | break; |
| 3209 | |
Gustavo F. Padovan | 6254775 | 2010-06-08 20:05:31 -0300 | [diff] [blame] | 3210 | rfc.mode = L2CAP_MODE_BASIC; |
| 3211 | rfc.txwin_size = 0; |
| 3212 | rfc.max_transmit = 0; |
| 3213 | rfc.retrans_timeout = 0; |
| 3214 | rfc.monitor_timeout = 0; |
| 3215 | rfc.max_pdu_size = 0; |
| 3216 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3217 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3218 | (unsigned long) &rfc); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3219 | break; |
| 3220 | |
| 3221 | case L2CAP_MODE_ERTM: |
| 3222 | rfc.mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3223 | rfc.max_transmit = chan->max_tx; |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3224 | |
| 3225 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3226 | |
| 3227 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3228 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3229 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3230 | rfc.max_pdu_size = cpu_to_le16(size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3231 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3232 | l2cap_txwin_setup(chan); |
| 3233 | |
| 3234 | rfc.txwin_size = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3235 | L2CAP_DEFAULT_TX_WINDOW); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3236 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3237 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3238 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3239 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3240 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3241 | l2cap_add_opt_efs(&ptr, chan); |
| 3242 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3243 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3244 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3245 | chan->tx_win); |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3246 | |
| 3247 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3248 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3249 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3250 | chan->fcs = L2CAP_FCS_NONE; |
| 3251 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3252 | chan->fcs); |
| 3253 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3254 | break; |
| 3255 | |
| 3256 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 273759e | 2012-05-17 20:53:53 -0700 | [diff] [blame] | 3257 | l2cap_txwin_setup(chan); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3258 | rfc.mode = L2CAP_MODE_STREAMING; |
| 3259 | rfc.txwin_size = 0; |
| 3260 | rfc.max_transmit = 0; |
| 3261 | rfc.retrans_timeout = 0; |
| 3262 | rfc.monitor_timeout = 0; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3263 | |
| 3264 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3265 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3266 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3267 | rfc.max_pdu_size = cpu_to_le16(size); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3268 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3269 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3270 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3271 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3272 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3273 | l2cap_add_opt_efs(&ptr, chan); |
| 3274 | |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3275 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3276 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3277 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3278 | chan->fcs = L2CAP_FCS_NONE; |
| 3279 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3280 | chan->fcs); |
| 3281 | } |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3282 | break; |
| 3283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3284 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3285 | req->dcid = cpu_to_le16(chan->dcid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3286 | req->flags = cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | |
| 3288 | return ptr - data; |
| 3289 | } |
| 3290 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3291 | static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3292 | { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3293 | struct l2cap_conf_rsp *rsp = data; |
| 3294 | void *ptr = rsp->data; |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3295 | void *req = chan->conf_req; |
| 3296 | int len = chan->conf_len; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3297 | int type, hint, olen; |
| 3298 | unsigned long val; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3299 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3300 | struct l2cap_conf_efs efs; |
| 3301 | u8 remote_efs = 0; |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3302 | u16 mtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3303 | u16 result = L2CAP_CONF_SUCCESS; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3304 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3306 | BT_DBG("chan %p", chan); |
Marcel Holtmann | 820ae1b | 2006-11-18 22:15:00 +0100 | [diff] [blame] | 3307 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3308 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3309 | len -= l2cap_get_conf_opt(&req, &type, &olen, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3310 | |
Gustavo F. Padovan | 589d274 | 2009-04-20 01:31:07 -0300 | [diff] [blame] | 3311 | hint = type & L2CAP_CONF_HINT; |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 3312 | type &= L2CAP_CONF_MASK; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3313 | |
| 3314 | switch (type) { |
| 3315 | case L2CAP_CONF_MTU: |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3316 | mtu = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3317 | break; |
| 3318 | |
| 3319 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3320 | chan->flush_to = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3321 | break; |
| 3322 | |
| 3323 | case L2CAP_CONF_QOS: |
| 3324 | break; |
| 3325 | |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3326 | case L2CAP_CONF_RFC: |
| 3327 | if (olen == sizeof(rfc)) |
| 3328 | memcpy(&rfc, (void *) val, olen); |
| 3329 | break; |
| 3330 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3331 | case L2CAP_CONF_FCS: |
| 3332 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3333 | set_bit(CONF_RECV_NO_FCS, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3334 | break; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3335 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3336 | case L2CAP_CONF_EFS: |
| 3337 | remote_efs = 1; |
| 3338 | if (olen == sizeof(efs)) |
| 3339 | memcpy(&efs, (void *) val, olen); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3340 | break; |
| 3341 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3342 | case L2CAP_CONF_EWS: |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 3343 | if (!(chan->conn->local_fixed_chan & L2CAP_FC_A2MP)) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3344 | return -ECONNREFUSED; |
| 3345 | |
| 3346 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
| 3347 | set_bit(CONF_EWS_RECV, &chan->conf_state); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3348 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3349 | chan->remote_tx_win = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3350 | break; |
| 3351 | |
| 3352 | default: |
| 3353 | if (hint) |
| 3354 | break; |
| 3355 | |
| 3356 | result = L2CAP_CONF_UNKNOWN; |
| 3357 | *((u8 *) ptr++) = type; |
| 3358 | break; |
| 3359 | } |
| 3360 | } |
| 3361 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3362 | if (chan->num_conf_rsp || chan->num_conf_req > 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3363 | goto done; |
| 3364 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3365 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3366 | case L2CAP_MODE_STREAMING: |
| 3367 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3368 | if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3369 | chan->mode = l2cap_select_mode(rfc.mode, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3370 | chan->conn->feat_mask); |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3371 | break; |
| 3372 | } |
| 3373 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3374 | if (remote_efs) { |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3375 | if (__l2cap_efs_supported(chan->conn)) |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3376 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3377 | else |
| 3378 | return -ECONNREFUSED; |
| 3379 | } |
| 3380 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3381 | if (chan->mode != rfc.mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3382 | return -ECONNREFUSED; |
Gustavo F. Padovan | 742e519 | 2010-06-08 19:09:48 -0300 | [diff] [blame] | 3383 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3384 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3385 | } |
| 3386 | |
| 3387 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3388 | if (chan->mode != rfc.mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3389 | result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3390 | rfc.mode = chan->mode; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3391 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3392 | if (chan->num_conf_rsp == 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3393 | return -ECONNREFUSED; |
| 3394 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3395 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3396 | (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3397 | } |
| 3398 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3399 | if (result == L2CAP_CONF_SUCCESS) { |
| 3400 | /* Configure output options and let the other side know |
| 3401 | * which ones we don't like. */ |
| 3402 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3403 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
| 3404 | result = L2CAP_CONF_UNACCEPT; |
| 3405 | else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3406 | chan->omtu = mtu; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3407 | set_bit(CONF_MTU_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3408 | } |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3409 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3410 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3411 | if (remote_efs) { |
| 3412 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3413 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3414 | efs.stype != chan->local_stype) { |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3415 | |
| 3416 | result = L2CAP_CONF_UNACCEPT; |
| 3417 | |
| 3418 | if (chan->num_conf_req >= 1) |
| 3419 | return -ECONNREFUSED; |
| 3420 | |
| 3421 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3422 | sizeof(efs), |
| 3423 | (unsigned long) &efs); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3424 | } else { |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3425 | /* Send PENDING Conf Rsp */ |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3426 | result = L2CAP_CONF_PENDING; |
| 3427 | set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3428 | } |
| 3429 | } |
| 3430 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3431 | switch (rfc.mode) { |
| 3432 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3433 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3434 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3435 | break; |
| 3436 | |
| 3437 | case L2CAP_MODE_ERTM: |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3438 | if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) |
| 3439 | chan->remote_tx_win = rfc.txwin_size; |
| 3440 | else |
| 3441 | rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; |
| 3442 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 3443 | chan->remote_max_tx = rfc.max_transmit; |
Mat Martineau | 86b1b26 | 2010-08-05 15:54:22 -0700 | [diff] [blame] | 3444 | |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3445 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3446 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3447 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3448 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3449 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3450 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3451 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3452 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3453 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3454 | |
| 3455 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3456 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3457 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3458 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3459 | chan->remote_id = efs.id; |
| 3460 | chan->remote_stype = efs.stype; |
| 3461 | chan->remote_msdu = le16_to_cpu(efs.msdu); |
| 3462 | chan->remote_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3463 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3464 | chan->remote_acc_lat = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3465 | le32_to_cpu(efs.acc_lat); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3466 | chan->remote_sdu_itime = |
| 3467 | le32_to_cpu(efs.sdu_itime); |
| 3468 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3469 | sizeof(efs), |
| 3470 | (unsigned long) &efs); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3471 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3472 | break; |
| 3473 | |
| 3474 | case L2CAP_MODE_STREAMING: |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3475 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3476 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3477 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3478 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3479 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3480 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3481 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3482 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3483 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3484 | (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3485 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3486 | break; |
| 3487 | |
| 3488 | default: |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3489 | result = L2CAP_CONF_UNACCEPT; |
| 3490 | |
| 3491 | memset(&rfc, 0, sizeof(rfc)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3492 | rfc.mode = chan->mode; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3493 | } |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3494 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3495 | if (result == L2CAP_CONF_SUCCESS) |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3496 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3497 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3498 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3499 | rsp->result = cpu_to_le16(result); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3500 | rsp->flags = cpu_to_le16(0); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3501 | |
| 3502 | return ptr - data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | } |
| 3504 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3505 | static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, |
| 3506 | void *data, u16 *result) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3507 | { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3508 | struct l2cap_conf_req *req = data; |
| 3509 | void *ptr = req->data; |
| 3510 | int type, olen; |
| 3511 | unsigned long val; |
Mat Martineau | 36e999a | 2011-12-08 17:23:21 -0800 | [diff] [blame] | 3512 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3513 | struct l2cap_conf_efs efs; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3514 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3515 | 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] | 3516 | |
| 3517 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3518 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3519 | |
| 3520 | switch (type) { |
| 3521 | case L2CAP_CONF_MTU: |
| 3522 | if (val < L2CAP_DEFAULT_MIN_MTU) { |
| 3523 | *result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3524 | chan->imtu = L2CAP_DEFAULT_MIN_MTU; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3525 | } else |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3526 | chan->imtu = val; |
| 3527 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3528 | break; |
| 3529 | |
| 3530 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3531 | chan->flush_to = val; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3532 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3533 | 2, chan->flush_to); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3534 | break; |
| 3535 | |
| 3536 | case L2CAP_CONF_RFC: |
| 3537 | if (olen == sizeof(rfc)) |
| 3538 | memcpy(&rfc, (void *)val, olen); |
| 3539 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3540 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3541 | rfc.mode != chan->mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3542 | return -ECONNREFUSED; |
| 3543 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3544 | chan->fcs = 0; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3545 | |
| 3546 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3547 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3548 | break; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3549 | |
| 3550 | case L2CAP_CONF_EWS: |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3551 | chan->ack_win = min_t(u16, val, chan->ack_win); |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3552 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3553 | chan->tx_win); |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3554 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3555 | |
| 3556 | case L2CAP_CONF_EFS: |
| 3557 | if (olen == sizeof(efs)) |
| 3558 | memcpy(&efs, (void *)val, olen); |
| 3559 | |
| 3560 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3561 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3562 | efs.stype != chan->local_stype) |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3563 | return -ECONNREFUSED; |
| 3564 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3565 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs), |
| 3566 | (unsigned long) &efs); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3567 | break; |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3568 | |
| 3569 | case L2CAP_CONF_FCS: |
| 3570 | if (*result == L2CAP_CONF_PENDING) |
| 3571 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3572 | set_bit(CONF_RECV_NO_FCS, |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3573 | &chan->conf_state); |
| 3574 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3575 | } |
| 3576 | } |
| 3577 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3578 | if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3579 | return -ECONNREFUSED; |
| 3580 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3581 | chan->mode = rfc.mode; |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3582 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3583 | if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3584 | switch (rfc.mode) { |
| 3585 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3586 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3587 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 3588 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3589 | if (!test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3590 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3591 | rfc.txwin_size); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3592 | |
| 3593 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3594 | chan->local_msdu = le16_to_cpu(efs.msdu); |
| 3595 | chan->local_sdu_itime = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3596 | le32_to_cpu(efs.sdu_itime); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3597 | chan->local_acc_lat = le32_to_cpu(efs.acc_lat); |
| 3598 | chan->local_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3599 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3600 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3601 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3602 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3603 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3604 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3605 | } |
| 3606 | } |
| 3607 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3608 | req->dcid = cpu_to_le16(chan->dcid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3609 | req->flags = cpu_to_le16(0); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3610 | |
| 3611 | return ptr - data; |
| 3612 | } |
| 3613 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3614 | static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, |
| 3615 | u16 result, u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | { |
| 3617 | struct l2cap_conf_rsp *rsp = data; |
| 3618 | void *ptr = rsp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3620 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3621 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3622 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3623 | rsp->result = cpu_to_le16(result); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3624 | rsp->flags = cpu_to_le16(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3625 | |
| 3626 | return ptr - data; |
| 3627 | } |
| 3628 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 3629 | void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan) |
| 3630 | { |
| 3631 | struct l2cap_le_conn_rsp rsp; |
| 3632 | struct l2cap_conn *conn = chan->conn; |
| 3633 | |
| 3634 | BT_DBG("chan %p", chan); |
| 3635 | |
| 3636 | rsp.dcid = cpu_to_le16(chan->scid); |
| 3637 | rsp.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 3638 | rsp.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 3639 | rsp.credits = cpu_to_le16(chan->rx_credits); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3640 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 3641 | |
| 3642 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), |
| 3643 | &rsp); |
| 3644 | } |
| 3645 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3646 | void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3647 | { |
| 3648 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3649 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3650 | u8 buf[128]; |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3651 | u8 rsp_code; |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3652 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3653 | rsp.scid = cpu_to_le16(chan->dcid); |
| 3654 | rsp.dcid = cpu_to_le16(chan->scid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3655 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 3656 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3657 | |
| 3658 | if (chan->hs_hcon) |
| 3659 | rsp_code = L2CAP_CREATE_CHAN_RSP; |
| 3660 | else |
| 3661 | rsp_code = L2CAP_CONN_RSP; |
| 3662 | |
| 3663 | BT_DBG("chan %p rsp_code %u", chan, rsp_code); |
| 3664 | |
| 3665 | l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp); |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3666 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3667 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3668 | return; |
| 3669 | |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3670 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3671 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3672 | chan->num_conf_req++; |
| 3673 | } |
| 3674 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3675 | 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] | 3676 | { |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3677 | int type, olen; |
| 3678 | unsigned long val; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3679 | /* Use sane default values in case a misbehaving remote device |
| 3680 | * did not send an RFC or extended window size option. |
| 3681 | */ |
| 3682 | u16 txwin_ext = chan->ack_win; |
| 3683 | struct l2cap_conf_rfc rfc = { |
| 3684 | .mode = chan->mode, |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3685 | .retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO), |
| 3686 | .monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO), |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3687 | .max_pdu_size = cpu_to_le16(chan->imtu), |
| 3688 | .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW), |
| 3689 | }; |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3690 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3691 | BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3692 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3693 | if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3694 | return; |
| 3695 | |
| 3696 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3697 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3698 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3699 | switch (type) { |
| 3700 | case L2CAP_CONF_RFC: |
| 3701 | if (olen == sizeof(rfc)) |
| 3702 | memcpy(&rfc, (void *)val, olen); |
Szymon Janc | 8f321f8 | 2012-06-08 11:33:33 +0200 | [diff] [blame] | 3703 | break; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3704 | case L2CAP_CONF_EWS: |
| 3705 | txwin_ext = val; |
| 3706 | break; |
| 3707 | } |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3708 | } |
| 3709 | |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3710 | switch (rfc.mode) { |
| 3711 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3712 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3713 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3714 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
| 3715 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3716 | chan->ack_win = min_t(u16, chan->ack_win, txwin_ext); |
| 3717 | else |
| 3718 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3719 | rfc.txwin_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3720 | break; |
| 3721 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3722 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3723 | } |
| 3724 | } |
| 3725 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3726 | static inline int l2cap_command_rej(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3727 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3728 | u8 *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3729 | { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3730 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3731 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3732 | if (cmd_len < sizeof(*rej)) |
| 3733 | return -EPROTO; |
| 3734 | |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3735 | if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3736 | return 0; |
| 3737 | |
| 3738 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3739 | cmd->ident == conn->info_ident) { |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 3740 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3741 | |
| 3742 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3743 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3744 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3745 | l2cap_conn_start(conn); |
| 3746 | } |
| 3747 | |
| 3748 | return 0; |
| 3749 | } |
| 3750 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3751 | static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn, |
| 3752 | struct l2cap_cmd_hdr *cmd, |
| 3753 | u8 *data, u8 rsp_code, u8 amp_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3754 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; |
| 3756 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3757 | struct l2cap_chan *chan = NULL, *pchan; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3758 | int result, status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | |
| 3760 | u16 dcid = 0, scid = __le16_to_cpu(req->scid); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3761 | __le16 psm = req->psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3762 | |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 3763 | BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | |
| 3765 | /* Check if we have socket listening on psm */ |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 3766 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src, |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 3767 | &conn->hcon->dst, ACL_LINK); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3768 | if (!pchan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3769 | result = L2CAP_CR_BAD_PSM; |
| 3770 | goto sendresp; |
| 3771 | } |
| 3772 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3773 | mutex_lock(&conn->chan_lock); |
Gustavo Padovan | 8ffb929 | 2013-10-21 14:21:41 -0200 | [diff] [blame] | 3774 | l2cap_chan_lock(pchan); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 3775 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3776 | /* Check if the ACL is secure enough (if not SDP) */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3777 | if (psm != cpu_to_le16(L2CAP_PSM_SDP) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3778 | !hci_conn_check_link_mode(conn->hcon)) { |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 3779 | conn->disc_reason = HCI_ERROR_AUTH_FAILURE; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3780 | result = L2CAP_CR_SEC_BLOCK; |
| 3781 | goto response; |
| 3782 | } |
| 3783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | result = L2CAP_CR_NO_MEM; |
| 3785 | |
Gustavo Padovan | 2dfa100 | 2012-05-27 22:27:58 -0300 | [diff] [blame] | 3786 | /* Check if we already have channel with that dcid */ |
| 3787 | if (__l2cap_get_chan_by_dcid(conn, scid)) |
| 3788 | goto response; |
| 3789 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 3790 | chan = pchan->ops->new_connection(pchan); |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 3791 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3792 | goto response; |
| 3793 | |
Syam Sidhardhan | 330b6c1 | 2013-08-06 01:59:12 +0900 | [diff] [blame] | 3794 | /* For certain devices (ex: HID mouse), support for authentication, |
| 3795 | * pairing and bonding is optional. For such devices, inorder to avoid |
| 3796 | * the ACL alive for too long after L2CAP disconnection, reset the ACL |
| 3797 | * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect. |
| 3798 | */ |
| 3799 | conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT; |
| 3800 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 3801 | bacpy(&chan->src, &conn->hcon->src); |
| 3802 | bacpy(&chan->dst, &conn->hcon->dst); |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 3803 | chan->src_type = bdaddr_src_type(conn->hcon); |
| 3804 | chan->dst_type = bdaddr_dst_type(conn->hcon); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3805 | chan->psm = psm; |
| 3806 | chan->dcid = scid; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3807 | chan->local_amp_id = amp_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3808 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3809 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3810 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3811 | dcid = chan->scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | |
Gustavo Padovan | 8d836d7 | 2013-10-15 19:24:47 -0300 | [diff] [blame] | 3813 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3814 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3815 | chan->ident = cmd->ident; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3816 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3817 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 3818 | if (l2cap_chan_check_security(chan, false)) { |
Marcel Holtmann | bdc2578 | 2013-10-14 02:45:34 -0700 | [diff] [blame] | 3819 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3820 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3821 | result = L2CAP_CR_PEND; |
| 3822 | status = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 3823 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3824 | } else { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3825 | /* Force pending result for AMP controllers. |
| 3826 | * The connection will succeed after the |
| 3827 | * physical link is up. |
| 3828 | */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3829 | if (amp_id == AMP_ID_BREDR) { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3830 | l2cap_state_change(chan, BT_CONFIG); |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3831 | result = L2CAP_CR_SUCCESS; |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3832 | } else { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3833 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3834 | result = L2CAP_CR_PEND; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3835 | } |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3836 | status = L2CAP_CS_NO_INFO; |
| 3837 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3838 | } else { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3839 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3840 | result = L2CAP_CR_PEND; |
| 3841 | status = L2CAP_CS_AUTHEN_PEND; |
| 3842 | } |
| 3843 | } else { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3844 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3845 | result = L2CAP_CR_PEND; |
| 3846 | status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3847 | } |
| 3848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3849 | response: |
Gustavo Padovan | 8ffb929 | 2013-10-21 14:21:41 -0200 | [diff] [blame] | 3850 | l2cap_chan_unlock(pchan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3851 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 3852 | l2cap_chan_put(pchan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3853 | |
| 3854 | sendresp: |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3855 | rsp.scid = cpu_to_le16(scid); |
| 3856 | rsp.dcid = cpu_to_le16(dcid); |
| 3857 | rsp.result = cpu_to_le16(result); |
| 3858 | rsp.status = cpu_to_le16(status); |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3859 | l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3860 | |
| 3861 | if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { |
| 3862 | struct l2cap_info_req info; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3863 | info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3864 | |
| 3865 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 3866 | conn->info_ident = l2cap_get_ident(conn); |
| 3867 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 3868 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3869 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3870 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 3871 | sizeof(info), &info); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3872 | } |
| 3873 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3874 | if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3875 | result == L2CAP_CR_SUCCESS) { |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3876 | u8 buf[128]; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3877 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3878 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3879 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3880 | chan->num_conf_req++; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3881 | } |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3882 | |
| 3883 | return chan; |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3884 | } |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3885 | |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3886 | static int l2cap_connect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3887 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3888 | { |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3889 | struct hci_dev *hdev = conn->hcon->hdev; |
| 3890 | struct hci_conn *hcon = conn->hcon; |
| 3891 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3892 | if (cmd_len < sizeof(struct l2cap_conn_req)) |
| 3893 | return -EPROTO; |
| 3894 | |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3895 | hci_dev_lock(hdev); |
| 3896 | if (test_bit(HCI_MGMT, &hdev->dev_flags) && |
| 3897 | !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags)) |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 3898 | mgmt_device_connected(hdev, hcon, 0, NULL, 0); |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3899 | hci_dev_unlock(hdev); |
| 3900 | |
Gustavo Padovan | 300229f | 2012-10-12 19:40:40 +0800 | [diff] [blame] | 3901 | l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3902 | return 0; |
| 3903 | } |
| 3904 | |
Mat Martineau | 5909cf3 | 2012-10-23 15:24:08 -0700 | [diff] [blame] | 3905 | static int l2cap_connect_create_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3906 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3907 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3908 | { |
| 3909 | struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; |
| 3910 | u16 scid, dcid, result, status; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3911 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3912 | u8 req[128]; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3913 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3914 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3915 | if (cmd_len < sizeof(*rsp)) |
| 3916 | return -EPROTO; |
| 3917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3918 | scid = __le16_to_cpu(rsp->scid); |
| 3919 | dcid = __le16_to_cpu(rsp->dcid); |
| 3920 | result = __le16_to_cpu(rsp->result); |
| 3921 | status = __le16_to_cpu(rsp->status); |
| 3922 | |
Andrei Emeltchenko | 1b009c9 | 2012-02-21 12:54:54 +0200 | [diff] [blame] | 3923 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3924 | dcid, scid, result, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3925 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3926 | mutex_lock(&conn->chan_lock); |
| 3927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3928 | if (scid) { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3929 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 3930 | if (!chan) { |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 3931 | err = -EBADSLT; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3932 | goto unlock; |
| 3933 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3934 | } else { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3935 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 3936 | if (!chan) { |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 3937 | err = -EBADSLT; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3938 | goto unlock; |
| 3939 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3940 | } |
| 3941 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3942 | err = 0; |
| 3943 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3944 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3946 | switch (result) { |
| 3947 | case L2CAP_CR_SUCCESS: |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 3948 | l2cap_state_change(chan, BT_CONFIG); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3949 | chan->ident = 0; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3950 | chan->dcid = dcid; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3951 | clear_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 3952 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3953 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3954 | break; |
| 3955 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3957 | l2cap_build_conf_req(chan, req), req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3958 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3959 | break; |
| 3960 | |
| 3961 | case L2CAP_CR_PEND: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3962 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | break; |
| 3964 | |
| 3965 | default: |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3966 | l2cap_chan_del(chan, ECONNREFUSED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | break; |
| 3968 | } |
| 3969 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3970 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3971 | |
| 3972 | unlock: |
| 3973 | mutex_unlock(&conn->chan_lock); |
| 3974 | |
| 3975 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3976 | } |
| 3977 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3978 | static inline void set_default_fcs(struct l2cap_chan *chan) |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 3979 | { |
| 3980 | /* FCS is enabled only in ERTM or streaming mode, if one or both |
| 3981 | * sides request it. |
| 3982 | */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3983 | if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3984 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3985 | else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3986 | chan->fcs = L2CAP_FCS_CRC16; |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 3987 | } |
| 3988 | |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 3989 | static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data, |
| 3990 | u8 ident, u16 flags) |
| 3991 | { |
| 3992 | struct l2cap_conn *conn = chan->conn; |
| 3993 | |
| 3994 | BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident, |
| 3995 | flags); |
| 3996 | |
| 3997 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
| 3998 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
| 3999 | |
| 4000 | l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP, |
| 4001 | l2cap_build_conf_rsp(chan, data, |
| 4002 | L2CAP_CONF_SUCCESS, flags), data); |
| 4003 | } |
| 4004 | |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4005 | static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident, |
| 4006 | u16 scid, u16 dcid) |
| 4007 | { |
| 4008 | struct l2cap_cmd_rej_cid rej; |
| 4009 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4010 | rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4011 | rej.scid = __cpu_to_le16(scid); |
| 4012 | rej.dcid = __cpu_to_le16(dcid); |
| 4013 | |
| 4014 | l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); |
| 4015 | } |
| 4016 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4017 | static inline int l2cap_config_req(struct l2cap_conn *conn, |
| 4018 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4019 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4020 | { |
| 4021 | struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; |
| 4022 | u16 dcid, flags; |
| 4023 | u8 rsp[64]; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4024 | struct l2cap_chan *chan; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4025 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4026 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4027 | if (cmd_len < sizeof(*req)) |
| 4028 | return -EPROTO; |
| 4029 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4030 | dcid = __le16_to_cpu(req->dcid); |
| 4031 | flags = __le16_to_cpu(req->flags); |
| 4032 | |
| 4033 | BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); |
| 4034 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4035 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4036 | if (!chan) { |
| 4037 | cmd_reject_invalid_cid(conn, cmd->ident, dcid, 0); |
| 4038 | return 0; |
| 4039 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4040 | |
David S. Miller | 033b114 | 2011-07-21 13:38:42 -0700 | [diff] [blame] | 4041 | if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4042 | cmd_reject_invalid_cid(conn, cmd->ident, chan->scid, |
| 4043 | chan->dcid); |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 4044 | goto unlock; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 4045 | } |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 4046 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4047 | /* Reject if config buffer is too small. */ |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 4048 | len = cmd_len - sizeof(*req); |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4049 | if (chan->conf_len + len > sizeof(chan->conf_req)) { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4050 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4051 | l2cap_build_conf_rsp(chan, rsp, |
| 4052 | L2CAP_CONF_REJECT, flags), rsp); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4053 | goto unlock; |
| 4054 | } |
| 4055 | |
| 4056 | /* Store config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4057 | memcpy(chan->conf_req + chan->conf_len, req->data, len); |
| 4058 | chan->conf_len += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4060 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | /* Incomplete config. Send empty response. */ |
| 4062 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4063 | l2cap_build_conf_rsp(chan, rsp, |
| 4064 | L2CAP_CONF_SUCCESS, flags), rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4065 | goto unlock; |
| 4066 | } |
| 4067 | |
| 4068 | /* Complete config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4069 | len = l2cap_parse_conf_req(chan, rsp); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4070 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4071 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4072 | goto unlock; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4073 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4075 | chan->ident = cmd->ident; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4076 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4077 | chan->num_conf_rsp++; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4078 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4079 | /* Reset config buffer. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4080 | chan->conf_len = 0; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4081 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4082 | if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4083 | goto unlock; |
| 4084 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4085 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4086 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4087 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4088 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4089 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4090 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4091 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4092 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4093 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4094 | else |
| 4095 | l2cap_chan_ready(chan); |
| 4096 | |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4097 | goto unlock; |
| 4098 | } |
| 4099 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4100 | if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 4101 | u8 buf[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4102 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4103 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4104 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4105 | } |
| 4106 | |
Stephen Hemminger | 49c922b | 2014-10-27 21:12:20 -0700 | [diff] [blame] | 4107 | /* Got Conf Rsp PENDING from remote side and assume we sent |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4108 | Conf Rsp PENDING in the code above */ |
| 4109 | if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 4110 | test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4111 | |
| 4112 | /* check compatibility */ |
| 4113 | |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4114 | /* Send rsp for BR/EDR channel */ |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4115 | if (!chan->hs_hcon) |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4116 | l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags); |
| 4117 | else |
| 4118 | chan->ident = cmd->ident; |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4119 | } |
| 4120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | unlock: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4122 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4123 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | } |
| 4125 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4126 | static inline int l2cap_config_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4127 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4128 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | { |
| 4130 | struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; |
| 4131 | u16 scid, flags, result; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4132 | struct l2cap_chan *chan; |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4133 | int len = cmd_len - sizeof(*rsp); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4134 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4135 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4136 | if (cmd_len < sizeof(*rsp)) |
| 4137 | return -EPROTO; |
| 4138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | scid = __le16_to_cpu(rsp->scid); |
| 4140 | flags = __le16_to_cpu(rsp->flags); |
| 4141 | result = __le16_to_cpu(rsp->result); |
| 4142 | |
Andrei Emeltchenko | 61386cb | 2012-03-12 12:13:07 +0200 | [diff] [blame] | 4143 | BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags, |
| 4144 | result, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4146 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4147 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4148 | return 0; |
| 4149 | |
| 4150 | switch (result) { |
| 4151 | case L2CAP_CONF_SUCCESS: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4152 | l2cap_conf_rfc_get(chan, rsp->data, len); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4153 | clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | break; |
| 4155 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4156 | case L2CAP_CONF_PENDING: |
| 4157 | set_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
| 4158 | |
| 4159 | if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
| 4160 | char buf[64]; |
| 4161 | |
| 4162 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4163 | buf, &result); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4164 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4165 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4166 | goto done; |
| 4167 | } |
| 4168 | |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4169 | if (!chan->hs_hcon) { |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4170 | l2cap_send_efs_conf_rsp(chan, buf, cmd->ident, |
| 4171 | 0); |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 4172 | } else { |
| 4173 | if (l2cap_check_efs(chan)) { |
| 4174 | amp_create_logical_link(chan); |
| 4175 | chan->ident = cmd->ident; |
| 4176 | } |
| 4177 | } |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4178 | } |
| 4179 | goto done; |
| 4180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | case L2CAP_CONF_UNACCEPT: |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4182 | if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4183 | char req[64]; |
| 4184 | |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4185 | if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4186 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4187 | goto done; |
| 4188 | } |
| 4189 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4190 | /* throw out any old stored conf requests */ |
| 4191 | result = L2CAP_CONF_SUCCESS; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 4192 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4193 | req, &result); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4194 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4195 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4196 | goto done; |
| 4197 | } |
| 4198 | |
| 4199 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4200 | L2CAP_CONF_REQ, len, req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4201 | chan->num_conf_req++; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4202 | if (result != L2CAP_CONF_SUCCESS) |
| 4203 | goto done; |
| 4204 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | } |
| 4206 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 4207 | default: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4208 | l2cap_chan_set_err(chan, ECONNRESET); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 4209 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 4210 | __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4211 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4212 | goto done; |
| 4213 | } |
| 4214 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4215 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | goto done; |
| 4217 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4218 | set_bit(CONF_INPUT_DONE, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4220 | if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4221 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4222 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4223 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4224 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4225 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4226 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4227 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4228 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4229 | else |
| 4230 | l2cap_chan_ready(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4234 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4235 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | } |
| 4237 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4238 | static inline int l2cap_disconnect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4239 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4240 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | { |
| 4242 | struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; |
| 4243 | struct l2cap_disconn_rsp rsp; |
| 4244 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4245 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4246 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4247 | if (cmd_len != sizeof(*req)) |
| 4248 | return -EPROTO; |
| 4249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | scid = __le16_to_cpu(req->scid); |
| 4251 | dcid = __le16_to_cpu(req->dcid); |
| 4252 | |
| 4253 | BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); |
| 4254 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4255 | mutex_lock(&conn->chan_lock); |
| 4256 | |
| 4257 | chan = __l2cap_get_chan_by_scid(conn, dcid); |
| 4258 | if (!chan) { |
| 4259 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4260 | cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid); |
| 4261 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4262 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4264 | l2cap_chan_lock(chan); |
| 4265 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4266 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4267 | rsp.scid = cpu_to_le16(chan->dcid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); |
| 4269 | |
Gustavo Padovan | 5ec1bbe | 2013-10-15 19:24:48 -0300 | [diff] [blame] | 4270 | chan->ops->set_shutdown(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4272 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4273 | l2cap_chan_del(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4274 | |
| 4275 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4277 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4278 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4279 | |
| 4280 | mutex_unlock(&conn->chan_lock); |
| 4281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | return 0; |
| 4283 | } |
| 4284 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4285 | static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4286 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4287 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4288 | { |
| 4289 | struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; |
| 4290 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4291 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4293 | if (cmd_len != sizeof(*rsp)) |
| 4294 | return -EPROTO; |
| 4295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | scid = __le16_to_cpu(rsp->scid); |
| 4297 | dcid = __le16_to_cpu(rsp->dcid); |
| 4298 | |
| 4299 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); |
| 4300 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4301 | mutex_lock(&conn->chan_lock); |
| 4302 | |
| 4303 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 4304 | if (!chan) { |
| 4305 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4307 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4308 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4309 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4310 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4311 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4312 | l2cap_chan_del(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4313 | |
| 4314 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4316 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4317 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4318 | |
| 4319 | mutex_unlock(&conn->chan_lock); |
| 4320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | return 0; |
| 4322 | } |
| 4323 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4324 | static inline int l2cap_information_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4325 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4326 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | { |
| 4328 | struct l2cap_info_req *req = (struct l2cap_info_req *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4329 | u16 type; |
| 4330 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4331 | if (cmd_len != sizeof(*req)) |
| 4332 | return -EPROTO; |
| 4333 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4334 | type = __le16_to_cpu(req->type); |
| 4335 | |
| 4336 | BT_DBG("type 0x%4.4x", type); |
| 4337 | |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4338 | if (type == L2CAP_IT_FEAT_MASK) { |
| 4339 | u8 buf[8]; |
Marcel Holtmann | 44dd46d | 2009-05-02 19:09:01 -0700 | [diff] [blame] | 4340 | u32 feat_mask = l2cap_feat_mask; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4341 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4342 | rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 4343 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 4344 | if (!disable_ertm) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4345 | feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4346 | | L2CAP_FEAT_FCS; |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 4347 | if (conn->local_fixed_chan & L2CAP_FC_A2MP) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 4348 | feat_mask |= L2CAP_FEAT_EXT_FLOW |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4349 | | L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 4350 | |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 4351 | put_unaligned_le32(feat_mask, rsp->data); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4352 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4353 | buf); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4354 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
| 4355 | u8 buf[12]; |
| 4356 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 4357 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4358 | rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 4359 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 4360 | rsp->data[0] = conn->local_fixed_chan; |
| 4361 | memset(rsp->data + 1, 0, 7); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4362 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4363 | buf); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4364 | } else { |
| 4365 | struct l2cap_info_rsp rsp; |
| 4366 | rsp.type = cpu_to_le16(type); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4367 | rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4368 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp), |
| 4369 | &rsp); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4371 | |
| 4372 | return 0; |
| 4373 | } |
| 4374 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4375 | static inline int l2cap_information_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4376 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4377 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4378 | { |
| 4379 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; |
| 4380 | u16 type, result; |
| 4381 | |
Jaganath Kanakkassery | 3f6fa3d | 2013-06-21 19:55:11 +0530 | [diff] [blame] | 4382 | if (cmd_len < sizeof(*rsp)) |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4383 | return -EPROTO; |
| 4384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4385 | type = __le16_to_cpu(rsp->type); |
| 4386 | result = __le16_to_cpu(rsp->result); |
| 4387 | |
| 4388 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); |
| 4389 | |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4390 | /* L2CAP Info req/rsp are unbound to channels, add extra checks */ |
| 4391 | if (cmd->ident != conn->info_ident || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4392 | conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4393 | return 0; |
| 4394 | |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 4395 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4396 | |
Ville Tervo | adb08ed | 2010-08-04 09:43:33 +0300 | [diff] [blame] | 4397 | if (result != L2CAP_IR_SUCCESS) { |
| 4398 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4399 | conn->info_ident = 0; |
| 4400 | |
| 4401 | l2cap_conn_start(conn); |
| 4402 | |
| 4403 | return 0; |
| 4404 | } |
| 4405 | |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4406 | switch (type) { |
| 4407 | case L2CAP_IT_FEAT_MASK: |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 4408 | conn->feat_mask = get_unaligned_le32(rsp->data); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4409 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 4410 | if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4411 | struct l2cap_info_req req; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4412 | req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4413 | |
| 4414 | conn->info_ident = l2cap_get_ident(conn); |
| 4415 | |
| 4416 | l2cap_send_cmd(conn, conn->info_ident, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4417 | L2CAP_INFO_REQ, sizeof(req), &req); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4418 | } else { |
| 4419 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4420 | conn->info_ident = 0; |
| 4421 | |
| 4422 | l2cap_conn_start(conn); |
| 4423 | } |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4424 | break; |
| 4425 | |
| 4426 | case L2CAP_IT_FIXED_CHAN: |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 4427 | conn->remote_fixed_chan = rsp->data[0]; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4428 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4429 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4430 | |
| 4431 | l2cap_conn_start(conn); |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4432 | break; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4433 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | return 0; |
| 4436 | } |
| 4437 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4438 | static int l2cap_create_channel_req(struct l2cap_conn *conn, |
| 4439 | struct l2cap_cmd_hdr *cmd, |
| 4440 | u16 cmd_len, void *data) |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4441 | { |
| 4442 | struct l2cap_create_chan_req *req = data; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4443 | struct l2cap_create_chan_rsp rsp; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4444 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4445 | struct hci_dev *hdev; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4446 | u16 psm, scid; |
| 4447 | |
| 4448 | if (cmd_len != sizeof(*req)) |
| 4449 | return -EPROTO; |
| 4450 | |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 4451 | if (!(conn->local_fixed_chan & L2CAP_FC_A2MP)) |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4452 | return -EINVAL; |
| 4453 | |
| 4454 | psm = le16_to_cpu(req->psm); |
| 4455 | scid = le16_to_cpu(req->scid); |
| 4456 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4457 | BT_DBG("psm 0x%2.2x, scid 0x%4.4x, amp_id %d", psm, scid, req->amp_id); |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4458 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4459 | /* For controller id 0 make BR/EDR connection */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4460 | if (req->amp_id == AMP_ID_BREDR) { |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4461 | l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4462 | req->amp_id); |
| 4463 | return 0; |
| 4464 | } |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4465 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4466 | /* Validate AMP controller id */ |
| 4467 | hdev = hci_dev_get(req->amp_id); |
| 4468 | if (!hdev) |
| 4469 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4470 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4471 | if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4472 | hci_dev_put(hdev); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4473 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4474 | } |
| 4475 | |
| 4476 | chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4477 | req->amp_id); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4478 | if (chan) { |
| 4479 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
| 4480 | struct hci_conn *hs_hcon; |
| 4481 | |
Marcel Holtmann | 98e0f7e | 2013-10-13 02:23:40 -0700 | [diff] [blame] | 4482 | hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, |
| 4483 | &conn->hcon->dst); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4484 | if (!hs_hcon) { |
| 4485 | hci_dev_put(hdev); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4486 | cmd_reject_invalid_cid(conn, cmd->ident, chan->scid, |
| 4487 | chan->dcid); |
| 4488 | return 0; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4489 | } |
| 4490 | |
| 4491 | BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon); |
| 4492 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4493 | mgr->bredr_chan = chan; |
| 4494 | chan->hs_hcon = hs_hcon; |
Andrei Emeltchenko | fd45bf4 | 2012-11-20 17:16:22 +0200 | [diff] [blame] | 4495 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4496 | conn->mtu = hdev->block_mtu; |
| 4497 | } |
| 4498 | |
| 4499 | hci_dev_put(hdev); |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4500 | |
| 4501 | return 0; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4502 | |
| 4503 | error: |
| 4504 | rsp.dcid = 0; |
| 4505 | rsp.scid = cpu_to_le16(scid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4506 | rsp.result = cpu_to_le16(L2CAP_CR_BAD_AMP); |
| 4507 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4508 | |
| 4509 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, |
| 4510 | sizeof(rsp), &rsp); |
| 4511 | |
Johan Hedberg | dc28080 | 2013-09-16 13:05:13 +0300 | [diff] [blame] | 4512 | return 0; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4513 | } |
| 4514 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4515 | static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id) |
| 4516 | { |
| 4517 | struct l2cap_move_chan_req req; |
| 4518 | u8 ident; |
| 4519 | |
| 4520 | BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id); |
| 4521 | |
| 4522 | ident = l2cap_get_ident(chan->conn); |
| 4523 | chan->ident = ident; |
| 4524 | |
| 4525 | req.icid = cpu_to_le16(chan->scid); |
| 4526 | req.dest_amp_id = dest_amp_id; |
| 4527 | |
| 4528 | l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req), |
| 4529 | &req); |
| 4530 | |
| 4531 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4532 | } |
| 4533 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4534 | static void l2cap_send_move_chan_rsp(struct l2cap_chan *chan, u16 result) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4535 | { |
| 4536 | struct l2cap_move_chan_rsp rsp; |
| 4537 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4538 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4539 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4540 | rsp.icid = cpu_to_le16(chan->dcid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4541 | rsp.result = cpu_to_le16(result); |
| 4542 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4543 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP, |
| 4544 | sizeof(rsp), &rsp); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4545 | } |
| 4546 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4547 | static void l2cap_send_move_chan_cfm(struct l2cap_chan *chan, u16 result) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4548 | { |
| 4549 | struct l2cap_move_chan_cfm cfm; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4550 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4551 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4552 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4553 | chan->ident = l2cap_get_ident(chan->conn); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4554 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4555 | cfm.icid = cpu_to_le16(chan->scid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4556 | cfm.result = cpu_to_le16(result); |
| 4557 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4558 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM, |
| 4559 | sizeof(cfm), &cfm); |
| 4560 | |
| 4561 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4562 | } |
| 4563 | |
| 4564 | static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid) |
| 4565 | { |
| 4566 | struct l2cap_move_chan_cfm cfm; |
| 4567 | |
| 4568 | BT_DBG("conn %p, icid 0x%4.4x", conn, icid); |
| 4569 | |
| 4570 | cfm.icid = cpu_to_le16(icid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4571 | cfm.result = cpu_to_le16(L2CAP_MC_UNCONFIRMED); |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4572 | |
| 4573 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM, |
| 4574 | sizeof(cfm), &cfm); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4578 | u16 icid) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4579 | { |
| 4580 | struct l2cap_move_chan_cfm_rsp rsp; |
| 4581 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4582 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4583 | |
| 4584 | rsp.icid = cpu_to_le16(icid); |
| 4585 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); |
| 4586 | } |
| 4587 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 4588 | static void __release_logical_link(struct l2cap_chan *chan) |
| 4589 | { |
| 4590 | chan->hs_hchan = NULL; |
| 4591 | chan->hs_hcon = NULL; |
| 4592 | |
| 4593 | /* Placeholder - release the logical link */ |
| 4594 | } |
| 4595 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4596 | static void l2cap_logical_fail(struct l2cap_chan *chan) |
| 4597 | { |
| 4598 | /* Logical link setup failed */ |
| 4599 | if (chan->state != BT_CONNECTED) { |
| 4600 | /* Create channel failure, disconnect */ |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4601 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4602 | return; |
| 4603 | } |
| 4604 | |
| 4605 | switch (chan->move_role) { |
| 4606 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4607 | l2cap_move_done(chan); |
| 4608 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP); |
| 4609 | break; |
| 4610 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4611 | if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP || |
| 4612 | chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) { |
| 4613 | /* Remote has only sent pending or |
| 4614 | * success responses, clean up |
| 4615 | */ |
| 4616 | l2cap_move_done(chan); |
| 4617 | } |
| 4618 | |
| 4619 | /* Other amp move states imply that the move |
| 4620 | * has already aborted |
| 4621 | */ |
| 4622 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 4623 | break; |
| 4624 | } |
| 4625 | } |
| 4626 | |
| 4627 | static void l2cap_logical_finish_create(struct l2cap_chan *chan, |
| 4628 | struct hci_chan *hchan) |
| 4629 | { |
| 4630 | struct l2cap_conf_rsp rsp; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4631 | |
Andrei Emeltchenko | 336178a | 2012-10-31 15:46:27 +0200 | [diff] [blame] | 4632 | chan->hs_hchan = hchan; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4633 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4634 | |
Andrei Emeltchenko | 35ba956 | 2012-10-25 15:20:43 +0300 | [diff] [blame] | 4635 | l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4636 | |
| 4637 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Andrei Emeltchenko | fe79c6f | 2012-10-25 15:20:42 +0300 | [diff] [blame] | 4638 | int err; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4639 | |
| 4640 | set_default_fcs(chan); |
| 4641 | |
| 4642 | err = l2cap_ertm_init(chan); |
| 4643 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4644 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4645 | else |
| 4646 | l2cap_chan_ready(chan); |
| 4647 | } |
| 4648 | } |
| 4649 | |
| 4650 | static void l2cap_logical_finish_move(struct l2cap_chan *chan, |
| 4651 | struct hci_chan *hchan) |
| 4652 | { |
| 4653 | chan->hs_hcon = hchan->conn; |
| 4654 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4655 | |
| 4656 | BT_DBG("move_state %d", chan->move_state); |
| 4657 | |
| 4658 | switch (chan->move_state) { |
| 4659 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 4660 | /* Move confirm will be sent after a success |
| 4661 | * response is received |
| 4662 | */ |
| 4663 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4664 | break; |
| 4665 | case L2CAP_MOVE_WAIT_LOGICAL_CFM: |
| 4666 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4667 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4668 | } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 4669 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 4670 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4671 | } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4672 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4673 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4674 | } |
| 4675 | break; |
| 4676 | default: |
| 4677 | /* Move was not in expected state, free the channel */ |
| 4678 | __release_logical_link(chan); |
| 4679 | |
| 4680 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4681 | } |
| 4682 | } |
| 4683 | |
| 4684 | /* Call with chan locked */ |
Andrei Emeltchenko | 27695fb | 2012-10-25 15:20:45 +0300 | [diff] [blame] | 4685 | void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan, |
| 4686 | u8 status) |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4687 | { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4688 | BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status); |
| 4689 | |
| 4690 | if (status) { |
| 4691 | l2cap_logical_fail(chan); |
| 4692 | __release_logical_link(chan); |
| 4693 | return; |
| 4694 | } |
| 4695 | |
| 4696 | if (chan->state != BT_CONNECTED) { |
| 4697 | /* Ignore logical link if channel is on BR/EDR */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4698 | if (chan->local_amp_id != AMP_ID_BREDR) |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4699 | l2cap_logical_finish_create(chan, hchan); |
| 4700 | } else { |
| 4701 | l2cap_logical_finish_move(chan, hchan); |
| 4702 | } |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4703 | } |
| 4704 | |
Mat Martineau | 3f7a56c | 2012-10-23 15:24:23 -0700 | [diff] [blame] | 4705 | void l2cap_move_start(struct l2cap_chan *chan) |
| 4706 | { |
| 4707 | BT_DBG("chan %p", chan); |
| 4708 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4709 | if (chan->local_amp_id == AMP_ID_BREDR) { |
Mat Martineau | 3f7a56c | 2012-10-23 15:24:23 -0700 | [diff] [blame] | 4710 | if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED) |
| 4711 | return; |
| 4712 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4713 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4714 | /* Placeholder - start physical link setup */ |
| 4715 | } else { |
| 4716 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4717 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4718 | chan->move_id = 0; |
| 4719 | l2cap_move_setup(chan); |
| 4720 | l2cap_send_move_chan_req(chan, 0); |
| 4721 | } |
| 4722 | } |
| 4723 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4724 | static void l2cap_do_create(struct l2cap_chan *chan, int result, |
| 4725 | u8 local_amp_id, u8 remote_amp_id) |
| 4726 | { |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4727 | BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state), |
| 4728 | local_amp_id, remote_amp_id); |
| 4729 | |
Andrei Emeltchenko | 12d6cc6 | 2012-11-14 17:39:31 +0200 | [diff] [blame] | 4730 | chan->fcs = L2CAP_FCS_NONE; |
| 4731 | |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4732 | /* Outgoing channel on AMP */ |
| 4733 | if (chan->state == BT_CONNECT) { |
| 4734 | if (result == L2CAP_CR_SUCCESS) { |
| 4735 | chan->local_amp_id = local_amp_id; |
| 4736 | l2cap_send_create_chan_req(chan, remote_amp_id); |
| 4737 | } else { |
| 4738 | /* Revert to BR/EDR connect */ |
| 4739 | l2cap_send_conn_req(chan); |
| 4740 | } |
| 4741 | |
| 4742 | return; |
| 4743 | } |
| 4744 | |
| 4745 | /* Incoming channel on AMP */ |
| 4746 | if (__l2cap_no_conn_pending(chan)) { |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4747 | struct l2cap_conn_rsp rsp; |
| 4748 | char buf[128]; |
| 4749 | rsp.scid = cpu_to_le16(chan->dcid); |
| 4750 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4751 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4752 | if (result == L2CAP_CR_SUCCESS) { |
| 4753 | /* Send successful response */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4754 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 4755 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4756 | } else { |
| 4757 | /* Send negative response */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4758 | rsp.result = cpu_to_le16(L2CAP_CR_NO_MEM); |
| 4759 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP, |
| 4763 | sizeof(rsp), &rsp); |
| 4764 | |
| 4765 | if (result == L2CAP_CR_SUCCESS) { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 4766 | l2cap_state_change(chan, BT_CONFIG); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4767 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 4768 | l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn), |
| 4769 | L2CAP_CONF_REQ, |
| 4770 | l2cap_build_conf_req(chan, buf), buf); |
| 4771 | chan->num_conf_req++; |
| 4772 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4773 | } |
| 4774 | } |
| 4775 | |
| 4776 | static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id, |
| 4777 | u8 remote_amp_id) |
| 4778 | { |
| 4779 | l2cap_move_setup(chan); |
| 4780 | chan->move_id = local_amp_id; |
| 4781 | chan->move_state = L2CAP_MOVE_WAIT_RSP; |
| 4782 | |
| 4783 | l2cap_send_move_chan_req(chan, remote_amp_id); |
| 4784 | } |
| 4785 | |
| 4786 | static void l2cap_do_move_respond(struct l2cap_chan *chan, int result) |
| 4787 | { |
| 4788 | struct hci_chan *hchan = NULL; |
| 4789 | |
| 4790 | /* Placeholder - get hci_chan for logical link */ |
| 4791 | |
| 4792 | if (hchan) { |
| 4793 | if (hchan->state == BT_CONNECTED) { |
| 4794 | /* Logical link is ready to go */ |
| 4795 | chan->hs_hcon = hchan->conn; |
| 4796 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4797 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4798 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4799 | |
| 4800 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 4801 | } else { |
| 4802 | /* Wait for logical link to be ready */ |
| 4803 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4804 | } |
| 4805 | } else { |
| 4806 | /* Logical link not available */ |
| 4807 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED); |
| 4808 | } |
| 4809 | } |
| 4810 | |
| 4811 | static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result) |
| 4812 | { |
| 4813 | if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4814 | u8 rsp_result; |
| 4815 | if (result == -EINVAL) |
| 4816 | rsp_result = L2CAP_MR_BAD_ID; |
| 4817 | else |
| 4818 | rsp_result = L2CAP_MR_NOT_ALLOWED; |
| 4819 | |
| 4820 | l2cap_send_move_chan_rsp(chan, rsp_result); |
| 4821 | } |
| 4822 | |
| 4823 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 4824 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4825 | |
| 4826 | /* Restart data transmission */ |
| 4827 | l2cap_ertm_send(chan); |
| 4828 | } |
| 4829 | |
Andrei Emeltchenko | a514b17 | 2012-11-14 17:39:30 +0200 | [diff] [blame] | 4830 | /* Invoke with locked chan */ |
| 4831 | void __l2cap_physical_cfm(struct l2cap_chan *chan, int result) |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4832 | { |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4833 | u8 local_amp_id = chan->local_amp_id; |
Andrei Emeltchenko | fffadc0 | 2012-11-01 15:37:03 +0200 | [diff] [blame] | 4834 | u8 remote_amp_id = chan->remote_amp_id; |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4835 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4836 | BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d", |
| 4837 | chan, result, local_amp_id, remote_amp_id); |
| 4838 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4839 | if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) { |
| 4840 | l2cap_chan_unlock(chan); |
| 4841 | return; |
| 4842 | } |
| 4843 | |
| 4844 | if (chan->state != BT_CONNECTED) { |
| 4845 | l2cap_do_create(chan, result, local_amp_id, remote_amp_id); |
| 4846 | } else if (result != L2CAP_MR_SUCCESS) { |
| 4847 | l2cap_do_move_cancel(chan, result); |
| 4848 | } else { |
| 4849 | switch (chan->move_role) { |
| 4850 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4851 | l2cap_do_move_initiate(chan, local_amp_id, |
| 4852 | remote_amp_id); |
| 4853 | break; |
| 4854 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4855 | l2cap_do_move_respond(chan, result); |
| 4856 | break; |
| 4857 | default: |
| 4858 | l2cap_do_move_cancel(chan, result); |
| 4859 | break; |
| 4860 | } |
| 4861 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4862 | } |
| 4863 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4864 | static inline int l2cap_move_channel_req(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4865 | struct l2cap_cmd_hdr *cmd, |
| 4866 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4867 | { |
| 4868 | struct l2cap_move_chan_req *req = data; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4869 | struct l2cap_move_chan_rsp rsp; |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4870 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4871 | u16 icid = 0; |
| 4872 | u16 result = L2CAP_MR_NOT_ALLOWED; |
| 4873 | |
| 4874 | if (cmd_len != sizeof(*req)) |
| 4875 | return -EPROTO; |
| 4876 | |
| 4877 | icid = le16_to_cpu(req->icid); |
| 4878 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4879 | BT_DBG("icid 0x%4.4x, dest_amp_id %d", icid, req->dest_amp_id); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4880 | |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 4881 | if (!(conn->local_fixed_chan & L2CAP_FC_A2MP)) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4882 | return -EINVAL; |
| 4883 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4884 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 4885 | if (!chan) { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4886 | rsp.icid = cpu_to_le16(icid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4887 | rsp.result = cpu_to_le16(L2CAP_MR_NOT_ALLOWED); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4888 | l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP, |
| 4889 | sizeof(rsp), &rsp); |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4890 | return 0; |
| 4891 | } |
| 4892 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4893 | chan->ident = cmd->ident; |
| 4894 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4895 | if (chan->scid < L2CAP_CID_DYN_START || |
| 4896 | chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY || |
| 4897 | (chan->mode != L2CAP_MODE_ERTM && |
| 4898 | chan->mode != L2CAP_MODE_STREAMING)) { |
| 4899 | result = L2CAP_MR_NOT_ALLOWED; |
| 4900 | goto send_move_response; |
| 4901 | } |
| 4902 | |
| 4903 | if (chan->local_amp_id == req->dest_amp_id) { |
| 4904 | result = L2CAP_MR_SAME_ID; |
| 4905 | goto send_move_response; |
| 4906 | } |
| 4907 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4908 | if (req->dest_amp_id != AMP_ID_BREDR) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4909 | struct hci_dev *hdev; |
| 4910 | hdev = hci_dev_get(req->dest_amp_id); |
| 4911 | if (!hdev || hdev->dev_type != HCI_AMP || |
| 4912 | !test_bit(HCI_UP, &hdev->flags)) { |
| 4913 | if (hdev) |
| 4914 | hci_dev_put(hdev); |
| 4915 | |
| 4916 | result = L2CAP_MR_BAD_ID; |
| 4917 | goto send_move_response; |
| 4918 | } |
| 4919 | hci_dev_put(hdev); |
| 4920 | } |
| 4921 | |
| 4922 | /* Detect a move collision. Only send a collision response |
| 4923 | * if this side has "lost", otherwise proceed with the move. |
| 4924 | * The winner has the larger bd_addr. |
| 4925 | */ |
| 4926 | if ((__chan_is_moving(chan) || |
| 4927 | chan->move_role != L2CAP_MOVE_ROLE_NONE) && |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 4928 | bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4929 | result = L2CAP_MR_COLLISION; |
| 4930 | goto send_move_response; |
| 4931 | } |
| 4932 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4933 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 4934 | l2cap_move_setup(chan); |
| 4935 | chan->move_id = req->dest_amp_id; |
| 4936 | icid = chan->dcid; |
| 4937 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4938 | if (req->dest_amp_id == AMP_ID_BREDR) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4939 | /* Moving to BR/EDR */ |
| 4940 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4941 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4942 | result = L2CAP_MR_PEND; |
| 4943 | } else { |
| 4944 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4945 | result = L2CAP_MR_SUCCESS; |
| 4946 | } |
| 4947 | } else { |
| 4948 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4949 | /* Placeholder - uncomment when amp functions are available */ |
| 4950 | /*amp_accept_physical(chan, req->dest_amp_id);*/ |
| 4951 | result = L2CAP_MR_PEND; |
| 4952 | } |
| 4953 | |
| 4954 | send_move_response: |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4955 | l2cap_send_move_chan_rsp(chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4956 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4957 | l2cap_chan_unlock(chan); |
| 4958 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4959 | return 0; |
| 4960 | } |
| 4961 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4962 | static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result) |
| 4963 | { |
| 4964 | struct l2cap_chan *chan; |
| 4965 | struct hci_chan *hchan = NULL; |
| 4966 | |
| 4967 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 4968 | if (!chan) { |
| 4969 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 4970 | return; |
| 4971 | } |
| 4972 | |
| 4973 | __clear_chan_timer(chan); |
| 4974 | if (result == L2CAP_MR_PEND) |
| 4975 | __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT); |
| 4976 | |
| 4977 | switch (chan->move_state) { |
| 4978 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 4979 | /* Move confirm will be sent when logical link |
| 4980 | * is complete. |
| 4981 | */ |
| 4982 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4983 | break; |
| 4984 | case L2CAP_MOVE_WAIT_RSP_SUCCESS: |
| 4985 | if (result == L2CAP_MR_PEND) { |
| 4986 | break; |
| 4987 | } else if (test_bit(CONN_LOCAL_BUSY, |
| 4988 | &chan->conn_state)) { |
| 4989 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4990 | } else { |
| 4991 | /* Logical link is up or moving to BR/EDR, |
| 4992 | * proceed with move |
| 4993 | */ |
| 4994 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 4995 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4996 | } |
| 4997 | break; |
| 4998 | case L2CAP_MOVE_WAIT_RSP: |
| 4999 | /* Moving to AMP */ |
| 5000 | if (result == L2CAP_MR_SUCCESS) { |
| 5001 | /* Remote is ready, send confirm immediately |
| 5002 | * after logical link is ready |
| 5003 | */ |
| 5004 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 5005 | } else { |
| 5006 | /* Both logical link and move success |
| 5007 | * are required to confirm |
| 5008 | */ |
| 5009 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP; |
| 5010 | } |
| 5011 | |
| 5012 | /* Placeholder - get hci_chan for logical link */ |
| 5013 | if (!hchan) { |
| 5014 | /* Logical link not available */ |
| 5015 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5016 | break; |
| 5017 | } |
| 5018 | |
| 5019 | /* If the logical link is not yet connected, do not |
| 5020 | * send confirmation. |
| 5021 | */ |
| 5022 | if (hchan->state != BT_CONNECTED) |
| 5023 | break; |
| 5024 | |
| 5025 | /* Logical link is already ready to go */ |
| 5026 | |
| 5027 | chan->hs_hcon = hchan->conn; |
| 5028 | chan->hs_hcon->l2cap_data = chan->conn; |
| 5029 | |
| 5030 | if (result == L2CAP_MR_SUCCESS) { |
| 5031 | /* Can confirm now */ |
| 5032 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 5033 | } else { |
| 5034 | /* Now only need move success |
| 5035 | * to confirm |
| 5036 | */ |
| 5037 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 5038 | } |
| 5039 | |
| 5040 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 5041 | break; |
| 5042 | default: |
| 5043 | /* Any other amp move state means the move failed. */ |
| 5044 | chan->move_id = chan->local_amp_id; |
| 5045 | l2cap_move_done(chan); |
| 5046 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5047 | } |
| 5048 | |
| 5049 | l2cap_chan_unlock(chan); |
| 5050 | } |
| 5051 | |
| 5052 | static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid, |
| 5053 | u16 result) |
| 5054 | { |
| 5055 | struct l2cap_chan *chan; |
| 5056 | |
| 5057 | chan = l2cap_get_chan_by_ident(conn, ident); |
| 5058 | if (!chan) { |
| 5059 | /* Could not locate channel, icid is best guess */ |
| 5060 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 5061 | return; |
| 5062 | } |
| 5063 | |
| 5064 | __clear_chan_timer(chan); |
| 5065 | |
| 5066 | if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 5067 | if (result == L2CAP_MR_COLLISION) { |
| 5068 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 5069 | } else { |
| 5070 | /* Cleanup - cancel move */ |
| 5071 | chan->move_id = chan->local_amp_id; |
| 5072 | l2cap_move_done(chan); |
| 5073 | } |
| 5074 | } |
| 5075 | |
| 5076 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5077 | |
| 5078 | l2cap_chan_unlock(chan); |
| 5079 | } |
| 5080 | |
| 5081 | static int l2cap_move_channel_rsp(struct l2cap_conn *conn, |
| 5082 | struct l2cap_cmd_hdr *cmd, |
| 5083 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5084 | { |
| 5085 | struct l2cap_move_chan_rsp *rsp = data; |
| 5086 | u16 icid, result; |
| 5087 | |
| 5088 | if (cmd_len != sizeof(*rsp)) |
| 5089 | return -EPROTO; |
| 5090 | |
| 5091 | icid = le16_to_cpu(rsp->icid); |
| 5092 | result = le16_to_cpu(rsp->result); |
| 5093 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5094 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5095 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 5096 | if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND) |
| 5097 | l2cap_move_continue(conn, icid, result); |
| 5098 | else |
| 5099 | l2cap_move_fail(conn, cmd->ident, icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5100 | |
| 5101 | return 0; |
| 5102 | } |
| 5103 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5104 | static int l2cap_move_channel_confirm(struct l2cap_conn *conn, |
| 5105 | struct l2cap_cmd_hdr *cmd, |
| 5106 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5107 | { |
| 5108 | struct l2cap_move_chan_cfm *cfm = data; |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5109 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5110 | u16 icid, result; |
| 5111 | |
| 5112 | if (cmd_len != sizeof(*cfm)) |
| 5113 | return -EPROTO; |
| 5114 | |
| 5115 | icid = le16_to_cpu(cfm->icid); |
| 5116 | result = le16_to_cpu(cfm->result); |
| 5117 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5118 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5119 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5120 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 5121 | if (!chan) { |
| 5122 | /* Spec requires a response even if the icid was not found */ |
| 5123 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5124 | return 0; |
| 5125 | } |
| 5126 | |
| 5127 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) { |
| 5128 | if (result == L2CAP_MC_CONFIRMED) { |
| 5129 | chan->local_amp_id = chan->move_id; |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 5130 | if (chan->local_amp_id == AMP_ID_BREDR) |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5131 | __release_logical_link(chan); |
| 5132 | } else { |
| 5133 | chan->move_id = chan->local_amp_id; |
| 5134 | } |
| 5135 | |
| 5136 | l2cap_move_done(chan); |
| 5137 | } |
| 5138 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5139 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5140 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5141 | l2cap_chan_unlock(chan); |
| 5142 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5143 | return 0; |
| 5144 | } |
| 5145 | |
| 5146 | static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5147 | struct l2cap_cmd_hdr *cmd, |
| 5148 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5149 | { |
| 5150 | struct l2cap_move_chan_cfm_rsp *rsp = data; |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5151 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5152 | u16 icid; |
| 5153 | |
| 5154 | if (cmd_len != sizeof(*rsp)) |
| 5155 | return -EPROTO; |
| 5156 | |
| 5157 | icid = le16_to_cpu(rsp->icid); |
| 5158 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5159 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5160 | |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5161 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 5162 | if (!chan) |
| 5163 | return 0; |
| 5164 | |
| 5165 | __clear_chan_timer(chan); |
| 5166 | |
| 5167 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) { |
| 5168 | chan->local_amp_id = chan->move_id; |
| 5169 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 5170 | if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan) |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5171 | __release_logical_link(chan); |
| 5172 | |
| 5173 | l2cap_move_done(chan); |
| 5174 | } |
| 5175 | |
| 5176 | l2cap_chan_unlock(chan); |
| 5177 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5178 | return 0; |
| 5179 | } |
| 5180 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5181 | static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5182 | struct l2cap_cmd_hdr *cmd, |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5183 | u16 cmd_len, u8 *data) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5184 | { |
| 5185 | struct hci_conn *hcon = conn->hcon; |
| 5186 | struct l2cap_conn_param_update_req *req; |
| 5187 | struct l2cap_conn_param_update_rsp rsp; |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5188 | u16 min, max, latency, to_multiplier; |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5189 | int err; |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5190 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 5191 | if (hcon->role != HCI_ROLE_MASTER) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5192 | return -EINVAL; |
| 5193 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5194 | if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) |
| 5195 | return -EPROTO; |
| 5196 | |
| 5197 | req = (struct l2cap_conn_param_update_req *) data; |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 5198 | min = __le16_to_cpu(req->min); |
| 5199 | max = __le16_to_cpu(req->max); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5200 | latency = __le16_to_cpu(req->latency); |
| 5201 | to_multiplier = __le16_to_cpu(req->to_multiplier); |
| 5202 | |
| 5203 | BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5204 | min, max, latency, to_multiplier); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5205 | |
| 5206 | memset(&rsp, 0, sizeof(rsp)); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5207 | |
Andre Guedes | d4905f2 | 2014-06-25 21:52:52 -0300 | [diff] [blame] | 5208 | err = hci_check_conn_params(min, max, latency, to_multiplier); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5209 | if (err) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5210 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5211 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5212 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5213 | |
| 5214 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5215 | sizeof(rsp), &rsp); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5216 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5217 | if (!err) { |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5218 | u8 store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5219 | |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5220 | store_hint = hci_le_conn_update(hcon, min, max, latency, |
| 5221 | to_multiplier); |
| 5222 | mgmt_new_conn_param(hcon->hdev, &hcon->dst, hcon->dst_type, |
| 5223 | store_hint, min, max, latency, |
| 5224 | to_multiplier); |
| 5225 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5226 | } |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5227 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5228 | return 0; |
| 5229 | } |
| 5230 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5231 | static int l2cap_le_connect_rsp(struct l2cap_conn *conn, |
| 5232 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5233 | u8 *data) |
| 5234 | { |
| 5235 | struct l2cap_le_conn_rsp *rsp = (struct l2cap_le_conn_rsp *) data; |
Johan Hedberg | 3e64b7b | 2014-11-13 10:55:19 +0200 | [diff] [blame] | 5236 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5237 | u16 dcid, mtu, mps, credits, result; |
| 5238 | struct l2cap_chan *chan; |
Johan Hedberg | 3e64b7b | 2014-11-13 10:55:19 +0200 | [diff] [blame] | 5239 | int err, sec_level; |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5240 | |
| 5241 | if (cmd_len < sizeof(*rsp)) |
| 5242 | return -EPROTO; |
| 5243 | |
| 5244 | dcid = __le16_to_cpu(rsp->dcid); |
| 5245 | mtu = __le16_to_cpu(rsp->mtu); |
| 5246 | mps = __le16_to_cpu(rsp->mps); |
| 5247 | credits = __le16_to_cpu(rsp->credits); |
| 5248 | result = __le16_to_cpu(rsp->result); |
| 5249 | |
| 5250 | if (result == L2CAP_CR_SUCCESS && (mtu < 23 || mps < 23)) |
| 5251 | return -EPROTO; |
| 5252 | |
| 5253 | BT_DBG("dcid 0x%4.4x mtu %u mps %u credits %u result 0x%2.2x", |
| 5254 | dcid, mtu, mps, credits, result); |
| 5255 | |
| 5256 | mutex_lock(&conn->chan_lock); |
| 5257 | |
| 5258 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 5259 | if (!chan) { |
| 5260 | err = -EBADSLT; |
| 5261 | goto unlock; |
| 5262 | } |
| 5263 | |
| 5264 | err = 0; |
| 5265 | |
| 5266 | l2cap_chan_lock(chan); |
| 5267 | |
| 5268 | switch (result) { |
| 5269 | case L2CAP_CR_SUCCESS: |
| 5270 | chan->ident = 0; |
| 5271 | chan->dcid = dcid; |
| 5272 | chan->omtu = mtu; |
| 5273 | chan->remote_mps = mps; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5274 | chan->tx_credits = credits; |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5275 | l2cap_chan_ready(chan); |
| 5276 | break; |
| 5277 | |
Johan Hedberg | 3e64b7b | 2014-11-13 10:55:19 +0200 | [diff] [blame] | 5278 | case L2CAP_CR_AUTHENTICATION: |
| 5279 | case L2CAP_CR_ENCRYPTION: |
| 5280 | /* If we already have MITM protection we can't do |
| 5281 | * anything. |
| 5282 | */ |
| 5283 | if (hcon->sec_level > BT_SECURITY_MEDIUM) { |
| 5284 | l2cap_chan_del(chan, ECONNREFUSED); |
| 5285 | break; |
| 5286 | } |
| 5287 | |
| 5288 | sec_level = hcon->sec_level + 1; |
| 5289 | if (chan->sec_level < sec_level) |
| 5290 | chan->sec_level = sec_level; |
| 5291 | |
| 5292 | /* We'll need to send a new Connect Request */ |
| 5293 | clear_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags); |
| 5294 | |
| 5295 | smp_conn_security(hcon, chan->sec_level); |
| 5296 | break; |
| 5297 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5298 | default: |
| 5299 | l2cap_chan_del(chan, ECONNREFUSED); |
| 5300 | break; |
| 5301 | } |
| 5302 | |
| 5303 | l2cap_chan_unlock(chan); |
| 5304 | |
| 5305 | unlock: |
| 5306 | mutex_unlock(&conn->chan_lock); |
| 5307 | |
| 5308 | return err; |
| 5309 | } |
| 5310 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5311 | static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5312 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5313 | u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5314 | { |
| 5315 | int err = 0; |
| 5316 | |
| 5317 | switch (cmd->code) { |
| 5318 | case L2CAP_COMMAND_REJ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5319 | l2cap_command_rej(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5320 | break; |
| 5321 | |
| 5322 | case L2CAP_CONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5323 | err = l2cap_connect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5324 | break; |
| 5325 | |
| 5326 | case L2CAP_CONN_RSP: |
Mat Martineau | f5a2598 | 2012-10-11 17:48:21 +0300 | [diff] [blame] | 5327 | case L2CAP_CREATE_CHAN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5328 | l2cap_connect_create_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5329 | break; |
| 5330 | |
| 5331 | case L2CAP_CONF_REQ: |
| 5332 | err = l2cap_config_req(conn, cmd, cmd_len, data); |
| 5333 | break; |
| 5334 | |
| 5335 | case L2CAP_CONF_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5336 | l2cap_config_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5337 | break; |
| 5338 | |
| 5339 | case L2CAP_DISCONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5340 | err = l2cap_disconnect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5341 | break; |
| 5342 | |
| 5343 | case L2CAP_DISCONN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5344 | l2cap_disconnect_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5345 | break; |
| 5346 | |
| 5347 | case L2CAP_ECHO_REQ: |
| 5348 | l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); |
| 5349 | break; |
| 5350 | |
| 5351 | case L2CAP_ECHO_RSP: |
| 5352 | break; |
| 5353 | |
| 5354 | case L2CAP_INFO_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5355 | err = l2cap_information_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5356 | break; |
| 5357 | |
| 5358 | case L2CAP_INFO_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5359 | l2cap_information_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5360 | break; |
| 5361 | |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 5362 | case L2CAP_CREATE_CHAN_REQ: |
| 5363 | err = l2cap_create_channel_req(conn, cmd, cmd_len, data); |
| 5364 | break; |
| 5365 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5366 | case L2CAP_MOVE_CHAN_REQ: |
| 5367 | err = l2cap_move_channel_req(conn, cmd, cmd_len, data); |
| 5368 | break; |
| 5369 | |
| 5370 | case L2CAP_MOVE_CHAN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5371 | l2cap_move_channel_rsp(conn, cmd, cmd_len, data); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5372 | break; |
| 5373 | |
| 5374 | case L2CAP_MOVE_CHAN_CFM: |
| 5375 | err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); |
| 5376 | break; |
| 5377 | |
| 5378 | case L2CAP_MOVE_CHAN_CFM_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5379 | l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5380 | break; |
| 5381 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5382 | default: |
| 5383 | BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); |
| 5384 | err = -EINVAL; |
| 5385 | break; |
| 5386 | } |
| 5387 | |
| 5388 | return err; |
| 5389 | } |
| 5390 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5391 | static int l2cap_le_connect_req(struct l2cap_conn *conn, |
| 5392 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5393 | u8 *data) |
| 5394 | { |
| 5395 | struct l2cap_le_conn_req *req = (struct l2cap_le_conn_req *) data; |
| 5396 | struct l2cap_le_conn_rsp rsp; |
| 5397 | struct l2cap_chan *chan, *pchan; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5398 | u16 dcid, scid, credits, mtu, mps; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5399 | __le16 psm; |
| 5400 | u8 result; |
| 5401 | |
| 5402 | if (cmd_len != sizeof(*req)) |
| 5403 | return -EPROTO; |
| 5404 | |
| 5405 | scid = __le16_to_cpu(req->scid); |
| 5406 | mtu = __le16_to_cpu(req->mtu); |
| 5407 | mps = __le16_to_cpu(req->mps); |
| 5408 | psm = req->psm; |
| 5409 | dcid = 0; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5410 | credits = 0; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5411 | |
| 5412 | if (mtu < 23 || mps < 23) |
| 5413 | return -EPROTO; |
| 5414 | |
| 5415 | BT_DBG("psm 0x%2.2x scid 0x%4.4x mtu %u mps %u", __le16_to_cpu(psm), |
| 5416 | scid, mtu, mps); |
| 5417 | |
| 5418 | /* Check if we have socket listening on psm */ |
| 5419 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src, |
| 5420 | &conn->hcon->dst, LE_LINK); |
| 5421 | if (!pchan) { |
| 5422 | result = L2CAP_CR_BAD_PSM; |
| 5423 | chan = NULL; |
| 5424 | goto response; |
| 5425 | } |
| 5426 | |
| 5427 | mutex_lock(&conn->chan_lock); |
| 5428 | l2cap_chan_lock(pchan); |
| 5429 | |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 5430 | if (!smp_sufficient_security(conn->hcon, pchan->sec_level, |
| 5431 | SMP_ALLOW_STK)) { |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5432 | result = L2CAP_CR_AUTHENTICATION; |
| 5433 | chan = NULL; |
| 5434 | goto response_unlock; |
| 5435 | } |
| 5436 | |
| 5437 | /* Check if we already have channel with that dcid */ |
| 5438 | if (__l2cap_get_chan_by_dcid(conn, scid)) { |
| 5439 | result = L2CAP_CR_NO_MEM; |
| 5440 | chan = NULL; |
| 5441 | goto response_unlock; |
| 5442 | } |
| 5443 | |
| 5444 | chan = pchan->ops->new_connection(pchan); |
| 5445 | if (!chan) { |
| 5446 | result = L2CAP_CR_NO_MEM; |
| 5447 | goto response_unlock; |
| 5448 | } |
| 5449 | |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 5450 | l2cap_le_flowctl_init(chan); |
| 5451 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5452 | bacpy(&chan->src, &conn->hcon->src); |
| 5453 | bacpy(&chan->dst, &conn->hcon->dst); |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 5454 | chan->src_type = bdaddr_src_type(conn->hcon); |
| 5455 | chan->dst_type = bdaddr_dst_type(conn->hcon); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5456 | chan->psm = psm; |
| 5457 | chan->dcid = scid; |
| 5458 | chan->omtu = mtu; |
| 5459 | chan->remote_mps = mps; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5460 | chan->tx_credits = __le16_to_cpu(req->credits); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5461 | |
| 5462 | __l2cap_chan_add(conn, chan); |
| 5463 | dcid = chan->scid; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5464 | credits = chan->rx_credits; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5465 | |
| 5466 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
| 5467 | |
| 5468 | chan->ident = cmd->ident; |
| 5469 | |
| 5470 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
| 5471 | l2cap_state_change(chan, BT_CONNECT2); |
Johan Hedberg | 434714d | 2014-09-01 09:45:03 +0300 | [diff] [blame] | 5472 | /* The following result value is actually not defined |
| 5473 | * for LE CoC but we use it to let the function know |
| 5474 | * that it should bail out after doing its cleanup |
| 5475 | * instead of sending a response. |
| 5476 | */ |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5477 | result = L2CAP_CR_PEND; |
| 5478 | chan->ops->defer(chan); |
| 5479 | } else { |
| 5480 | l2cap_chan_ready(chan); |
| 5481 | result = L2CAP_CR_SUCCESS; |
| 5482 | } |
| 5483 | |
| 5484 | response_unlock: |
| 5485 | l2cap_chan_unlock(pchan); |
| 5486 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 5487 | l2cap_chan_put(pchan); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5488 | |
| 5489 | if (result == L2CAP_CR_PEND) |
| 5490 | return 0; |
| 5491 | |
| 5492 | response: |
| 5493 | if (chan) { |
| 5494 | rsp.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 5495 | rsp.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5496 | } else { |
| 5497 | rsp.mtu = 0; |
| 5498 | rsp.mps = 0; |
| 5499 | } |
| 5500 | |
| 5501 | rsp.dcid = cpu_to_le16(dcid); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5502 | rsp.credits = cpu_to_le16(credits); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5503 | rsp.result = cpu_to_le16(result); |
| 5504 | |
| 5505 | l2cap_send_cmd(conn, cmd->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), &rsp); |
| 5506 | |
| 5507 | return 0; |
| 5508 | } |
| 5509 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5510 | static inline int l2cap_le_credits(struct l2cap_conn *conn, |
| 5511 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5512 | u8 *data) |
| 5513 | { |
| 5514 | struct l2cap_le_credits *pkt; |
| 5515 | struct l2cap_chan *chan; |
Johan Hedberg | 0f1bfe4 | 2014-01-27 15:11:35 -0800 | [diff] [blame] | 5516 | u16 cid, credits, max_credits; |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5517 | |
| 5518 | if (cmd_len != sizeof(*pkt)) |
| 5519 | return -EPROTO; |
| 5520 | |
| 5521 | pkt = (struct l2cap_le_credits *) data; |
| 5522 | cid = __le16_to_cpu(pkt->cid); |
| 5523 | credits = __le16_to_cpu(pkt->credits); |
| 5524 | |
| 5525 | BT_DBG("cid 0x%4.4x credits 0x%4.4x", cid, credits); |
| 5526 | |
| 5527 | chan = l2cap_get_chan_by_dcid(conn, cid); |
| 5528 | if (!chan) |
| 5529 | return -EBADSLT; |
| 5530 | |
Johan Hedberg | 0f1bfe4 | 2014-01-27 15:11:35 -0800 | [diff] [blame] | 5531 | max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits; |
| 5532 | if (credits > max_credits) { |
| 5533 | BT_ERR("LE credits overflow"); |
| 5534 | l2cap_send_disconn_req(chan, ECONNRESET); |
Martin Townsend | ee93053 | 2014-10-13 19:24:45 +0100 | [diff] [blame] | 5535 | l2cap_chan_unlock(chan); |
Johan Hedberg | 0f1bfe4 | 2014-01-27 15:11:35 -0800 | [diff] [blame] | 5536 | |
| 5537 | /* Return 0 so that we don't trigger an unnecessary |
| 5538 | * command reject packet. |
| 5539 | */ |
| 5540 | return 0; |
| 5541 | } |
| 5542 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5543 | chan->tx_credits += credits; |
| 5544 | |
| 5545 | while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { |
| 5546 | l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); |
| 5547 | chan->tx_credits--; |
| 5548 | } |
| 5549 | |
| 5550 | if (chan->tx_credits) |
| 5551 | chan->ops->resume(chan); |
| 5552 | |
| 5553 | l2cap_chan_unlock(chan); |
| 5554 | |
| 5555 | return 0; |
| 5556 | } |
| 5557 | |
Johan Hedberg | 71fb419 | 2013-12-10 10:52:48 +0200 | [diff] [blame] | 5558 | static inline int l2cap_le_command_rej(struct l2cap_conn *conn, |
| 5559 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5560 | u8 *data) |
| 5561 | { |
| 5562 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
| 5563 | struct l2cap_chan *chan; |
| 5564 | |
| 5565 | if (cmd_len < sizeof(*rej)) |
| 5566 | return -EPROTO; |
| 5567 | |
| 5568 | mutex_lock(&conn->chan_lock); |
| 5569 | |
| 5570 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 5571 | if (!chan) |
| 5572 | goto done; |
| 5573 | |
| 5574 | l2cap_chan_lock(chan); |
| 5575 | l2cap_chan_del(chan, ECONNREFUSED); |
| 5576 | l2cap_chan_unlock(chan); |
| 5577 | |
| 5578 | done: |
| 5579 | mutex_unlock(&conn->chan_lock); |
| 5580 | return 0; |
| 5581 | } |
| 5582 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5583 | static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5584 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5585 | u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5586 | { |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5587 | int err = 0; |
| 5588 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5589 | switch (cmd->code) { |
| 5590 | case L2CAP_COMMAND_REJ: |
Johan Hedberg | 71fb419 | 2013-12-10 10:52:48 +0200 | [diff] [blame] | 5591 | l2cap_le_command_rej(conn, cmd, cmd_len, data); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5592 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5593 | |
| 5594 | case L2CAP_CONN_PARAM_UPDATE_REQ: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5595 | err = l2cap_conn_param_update_req(conn, cmd, cmd_len, data); |
| 5596 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5597 | |
| 5598 | case L2CAP_CONN_PARAM_UPDATE_RSP: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5599 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5600 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5601 | case L2CAP_LE_CONN_RSP: |
| 5602 | l2cap_le_connect_rsp(conn, cmd, cmd_len, data); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5603 | break; |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5604 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5605 | case L2CAP_LE_CONN_REQ: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5606 | err = l2cap_le_connect_req(conn, cmd, cmd_len, data); |
| 5607 | break; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5608 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5609 | case L2CAP_LE_CREDITS: |
| 5610 | err = l2cap_le_credits(conn, cmd, cmd_len, data); |
| 5611 | break; |
| 5612 | |
Johan Hedberg | 3defe01 | 2013-05-15 10:16:06 +0300 | [diff] [blame] | 5613 | case L2CAP_DISCONN_REQ: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5614 | err = l2cap_disconnect_req(conn, cmd, cmd_len, data); |
| 5615 | break; |
Johan Hedberg | 3defe01 | 2013-05-15 10:16:06 +0300 | [diff] [blame] | 5616 | |
| 5617 | case L2CAP_DISCONN_RSP: |
| 5618 | l2cap_disconnect_rsp(conn, cmd, cmd_len, data); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5619 | break; |
Johan Hedberg | 3defe01 | 2013-05-15 10:16:06 +0300 | [diff] [blame] | 5620 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5621 | default: |
| 5622 | BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5623 | err = -EINVAL; |
| 5624 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5625 | } |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5626 | |
| 5627 | return err; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5628 | } |
| 5629 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5630 | static inline void l2cap_le_sig_channel(struct l2cap_conn *conn, |
| 5631 | struct sk_buff *skb) |
| 5632 | { |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5633 | struct hci_conn *hcon = conn->hcon; |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5634 | struct l2cap_cmd_hdr *cmd; |
| 5635 | u16 len; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5636 | int err; |
| 5637 | |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5638 | if (hcon->type != LE_LINK) |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5639 | goto drop; |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5640 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5641 | if (skb->len < L2CAP_CMD_HDR_SIZE) |
| 5642 | goto drop; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5643 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5644 | cmd = (void *) skb->data; |
| 5645 | skb_pull(skb, L2CAP_CMD_HDR_SIZE); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5646 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5647 | len = le16_to_cpu(cmd->len); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5648 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5649 | BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd->code, len, cmd->ident); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5650 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5651 | if (len != skb->len || !cmd->ident) { |
| 5652 | BT_DBG("corrupted command"); |
| 5653 | goto drop; |
| 5654 | } |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5655 | |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5656 | err = l2cap_le_sig_cmd(conn, cmd, len, skb->data); |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5657 | if (err) { |
| 5658 | struct l2cap_cmd_rej_unk rej; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5659 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5660 | BT_ERR("Wrong link type (%d)", err); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5661 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5662 | rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5663 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
| 5664 | sizeof(rej), &rej); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5665 | } |
| 5666 | |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5667 | drop: |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5668 | kfree_skb(skb); |
| 5669 | } |
| 5670 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5671 | static inline void l2cap_sig_channel(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5672 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5673 | { |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5674 | struct hci_conn *hcon = conn->hcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5675 | u8 *data = skb->data; |
| 5676 | int len = skb->len; |
| 5677 | struct l2cap_cmd_hdr cmd; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5678 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5679 | |
| 5680 | l2cap_raw_recv(conn, skb); |
| 5681 | |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5682 | if (hcon->type != ACL_LINK) |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5683 | goto drop; |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5685 | while (len >= L2CAP_CMD_HDR_SIZE) { |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5686 | u16 cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5687 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 5688 | data += L2CAP_CMD_HDR_SIZE; |
| 5689 | len -= L2CAP_CMD_HDR_SIZE; |
| 5690 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5691 | cmd_len = le16_to_cpu(cmd.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5693 | BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, |
| 5694 | cmd.ident); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5696 | if (cmd_len > len || !cmd.ident) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | BT_DBG("corrupted command"); |
| 5698 | break; |
| 5699 | } |
| 5700 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5701 | err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | if (err) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 5703 | struct l2cap_cmd_rej_unk rej; |
Gustavo F. Padovan | 2c6d1a2 | 2011-03-23 14:38:32 -0300 | [diff] [blame] | 5704 | |
| 5705 | BT_ERR("Wrong link type (%d)", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5707 | rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5708 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, |
| 5709 | sizeof(rej), &rej); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | } |
| 5711 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5712 | data += cmd_len; |
| 5713 | len -= cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5714 | } |
| 5715 | |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5716 | drop: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5717 | kfree_skb(skb); |
| 5718 | } |
| 5719 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5720 | 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] | 5721 | { |
| 5722 | u16 our_fcs, rcv_fcs; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 5723 | int hdr_size; |
| 5724 | |
| 5725 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 5726 | hdr_size = L2CAP_EXT_HDR_SIZE; |
| 5727 | else |
| 5728 | hdr_size = L2CAP_ENH_HDR_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5729 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5730 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5731 | skb_trim(skb, skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5732 | rcv_fcs = get_unaligned_le16(skb->data + skb->len); |
| 5733 | our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); |
| 5734 | |
| 5735 | if (our_fcs != rcv_fcs) |
João Paulo Rechi Vita | 7a560e5 | 2010-06-22 13:56:27 -0300 | [diff] [blame] | 5736 | return -EBADMSG; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5737 | } |
| 5738 | return 0; |
| 5739 | } |
| 5740 | |
Mat Martineau | 6ea0048 | 2012-05-17 20:53:52 -0700 | [diff] [blame] | 5741 | static 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] | 5742 | { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5743 | struct l2cap_ctrl control; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5744 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5745 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5746 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5747 | memset(&control, 0, sizeof(control)); |
| 5748 | control.sframe = 1; |
| 5749 | control.final = 1; |
| 5750 | control.reqseq = chan->buffer_seq; |
| 5751 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5752 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5753 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5754 | control.super = L2CAP_SUPER_RNR; |
| 5755 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5756 | } |
| 5757 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5758 | if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
| 5759 | chan->unacked_frames > 0) |
| 5760 | __set_retrans_timer(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5761 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5762 | /* Send pending iframes */ |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 5763 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5764 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5765 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5766 | test_bit(CONN_SEND_FBIT, &chan->conn_state)) { |
| 5767 | /* F-bit wasn't sent in an s-frame or i-frame yet, so |
| 5768 | * send it now. |
| 5769 | */ |
| 5770 | control.super = L2CAP_SUPER_RR; |
| 5771 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5772 | } |
| 5773 | } |
| 5774 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5775 | static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag, |
| 5776 | struct sk_buff **last_frag) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5777 | { |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5778 | /* skb->len reflects data in skb as well as all fragments |
| 5779 | * skb->data_len reflects only data in fragments |
| 5780 | */ |
| 5781 | if (!skb_has_frag_list(skb)) |
| 5782 | skb_shinfo(skb)->frag_list = new_frag; |
| 5783 | |
| 5784 | new_frag->next = NULL; |
| 5785 | |
| 5786 | (*last_frag)->next = new_frag; |
| 5787 | *last_frag = new_frag; |
| 5788 | |
| 5789 | skb->len += new_frag->len; |
| 5790 | skb->data_len += new_frag->len; |
| 5791 | skb->truesize += new_frag->truesize; |
| 5792 | } |
| 5793 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5794 | static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, |
| 5795 | struct l2cap_ctrl *control) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5796 | { |
| 5797 | int err = -EINVAL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5798 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5799 | switch (control->sar) { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5800 | case L2CAP_SAR_UNSEGMENTED: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5801 | if (chan->sdu) |
| 5802 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5803 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5804 | err = chan->ops->recv(chan, skb); |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5805 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5806 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5807 | case L2CAP_SAR_START: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5808 | if (chan->sdu) |
| 5809 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5810 | |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5811 | chan->sdu_len = get_unaligned_le16(skb->data); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5812 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5813 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5814 | if (chan->sdu_len > chan->imtu) { |
| 5815 | err = -EMSGSIZE; |
| 5816 | break; |
| 5817 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5818 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5819 | if (skb->len >= chan->sdu_len) |
| 5820 | break; |
| 5821 | |
| 5822 | chan->sdu = skb; |
| 5823 | chan->sdu_last_frag = skb; |
| 5824 | |
| 5825 | skb = NULL; |
| 5826 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5827 | break; |
| 5828 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5829 | case L2CAP_SAR_CONTINUE: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5830 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5831 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5832 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5833 | append_skb_frag(chan->sdu, skb, |
| 5834 | &chan->sdu_last_frag); |
| 5835 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5836 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5837 | if (chan->sdu->len >= chan->sdu_len) |
| 5838 | break; |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5839 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5840 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5841 | break; |
| 5842 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5843 | case L2CAP_SAR_END: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5844 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5845 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5846 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5847 | append_skb_frag(chan->sdu, skb, |
| 5848 | &chan->sdu_last_frag); |
| 5849 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5850 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5851 | if (chan->sdu->len != chan->sdu_len) |
| 5852 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5853 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5854 | err = chan->ops->recv(chan, chan->sdu); |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5855 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5856 | if (!err) { |
| 5857 | /* Reassembly complete */ |
| 5858 | chan->sdu = NULL; |
| 5859 | chan->sdu_last_frag = NULL; |
| 5860 | chan->sdu_len = 0; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5861 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5862 | break; |
| 5863 | } |
| 5864 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5865 | if (err) { |
| 5866 | kfree_skb(skb); |
| 5867 | kfree_skb(chan->sdu); |
| 5868 | chan->sdu = NULL; |
| 5869 | chan->sdu_last_frag = NULL; |
| 5870 | chan->sdu_len = 0; |
| 5871 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5872 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5873 | return err; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5874 | } |
| 5875 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 5876 | static int l2cap_resegment(struct l2cap_chan *chan) |
| 5877 | { |
| 5878 | /* Placeholder */ |
| 5879 | return 0; |
| 5880 | } |
| 5881 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 5882 | void l2cap_chan_busy(struct l2cap_chan *chan, int busy) |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 5883 | { |
Mat Martineau | 61aa4f5 | 2012-05-17 20:53:40 -0700 | [diff] [blame] | 5884 | u8 event; |
| 5885 | |
| 5886 | if (chan->mode != L2CAP_MODE_ERTM) |
| 5887 | return; |
| 5888 | |
| 5889 | event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR; |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 5890 | l2cap_tx(chan, NULL, NULL, event); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5891 | } |
| 5892 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5893 | static int l2cap_rx_queued_iframes(struct l2cap_chan *chan) |
| 5894 | { |
Mat Martineau | 6383872 | 2012-05-17 20:53:45 -0700 | [diff] [blame] | 5895 | int err = 0; |
| 5896 | /* Pass sequential frames to l2cap_reassemble_sdu() |
| 5897 | * until a gap is encountered. |
| 5898 | */ |
| 5899 | |
| 5900 | BT_DBG("chan %p", chan); |
| 5901 | |
| 5902 | while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5903 | struct sk_buff *skb; |
| 5904 | BT_DBG("Searching for skb with txseq %d (queue len %d)", |
| 5905 | chan->buffer_seq, skb_queue_len(&chan->srej_q)); |
| 5906 | |
| 5907 | skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq); |
| 5908 | |
| 5909 | if (!skb) |
| 5910 | break; |
| 5911 | |
| 5912 | skb_unlink(skb, &chan->srej_q); |
| 5913 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 5914 | err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->control); |
| 5915 | if (err) |
| 5916 | break; |
| 5917 | } |
| 5918 | |
| 5919 | if (skb_queue_empty(&chan->srej_q)) { |
| 5920 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 5921 | l2cap_send_ack(chan); |
| 5922 | } |
| 5923 | |
| 5924 | return err; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5925 | } |
| 5926 | |
| 5927 | static void l2cap_handle_srej(struct l2cap_chan *chan, |
| 5928 | struct l2cap_ctrl *control) |
| 5929 | { |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5930 | struct sk_buff *skb; |
| 5931 | |
| 5932 | BT_DBG("chan %p, control %p", chan, control); |
| 5933 | |
| 5934 | if (control->reqseq == chan->next_tx_seq) { |
| 5935 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5936 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5937 | return; |
| 5938 | } |
| 5939 | |
| 5940 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 5941 | |
| 5942 | if (skb == NULL) { |
| 5943 | BT_DBG("Seq %d not available for retransmission", |
| 5944 | control->reqseq); |
| 5945 | return; |
| 5946 | } |
| 5947 | |
| 5948 | if (chan->max_tx != 0 && bt_cb(skb)->control.retries >= chan->max_tx) { |
| 5949 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5950 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5951 | return; |
| 5952 | } |
| 5953 | |
| 5954 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5955 | |
| 5956 | if (control->poll) { |
| 5957 | l2cap_pass_to_tx(chan, control); |
| 5958 | |
| 5959 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 5960 | l2cap_retransmit(chan, control); |
| 5961 | l2cap_ertm_send(chan); |
| 5962 | |
| 5963 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 5964 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5965 | chan->srej_save_reqseq = control->reqseq; |
| 5966 | } |
| 5967 | } else { |
| 5968 | l2cap_pass_to_tx_fbit(chan, control); |
| 5969 | |
| 5970 | if (control->final) { |
| 5971 | if (chan->srej_save_reqseq != control->reqseq || |
| 5972 | !test_and_clear_bit(CONN_SREJ_ACT, |
| 5973 | &chan->conn_state)) |
| 5974 | l2cap_retransmit(chan, control); |
| 5975 | } else { |
| 5976 | l2cap_retransmit(chan, control); |
| 5977 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 5978 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5979 | chan->srej_save_reqseq = control->reqseq; |
| 5980 | } |
| 5981 | } |
| 5982 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5983 | } |
| 5984 | |
| 5985 | static void l2cap_handle_rej(struct l2cap_chan *chan, |
| 5986 | struct l2cap_ctrl *control) |
| 5987 | { |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5988 | struct sk_buff *skb; |
| 5989 | |
| 5990 | BT_DBG("chan %p, control %p", chan, control); |
| 5991 | |
| 5992 | if (control->reqseq == chan->next_tx_seq) { |
| 5993 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5994 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5995 | return; |
| 5996 | } |
| 5997 | |
| 5998 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 5999 | |
| 6000 | if (chan->max_tx && skb && |
| 6001 | bt_cb(skb)->control.retries >= chan->max_tx) { |
| 6002 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6003 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 6004 | return; |
| 6005 | } |
| 6006 | |
| 6007 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6008 | |
| 6009 | l2cap_pass_to_tx(chan, control); |
| 6010 | |
| 6011 | if (control->final) { |
| 6012 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
| 6013 | l2cap_retransmit_all(chan, control); |
| 6014 | } else { |
| 6015 | l2cap_retransmit_all(chan, control); |
| 6016 | l2cap_ertm_send(chan); |
| 6017 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) |
| 6018 | set_bit(CONN_REJ_ACT, &chan->conn_state); |
| 6019 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6020 | } |
| 6021 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6022 | static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq) |
| 6023 | { |
| 6024 | BT_DBG("chan %p, txseq %d", chan, txseq); |
| 6025 | |
| 6026 | BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq, |
| 6027 | chan->expected_tx_seq); |
| 6028 | |
| 6029 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 6030 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6031 | chan->tx_win) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6032 | /* See notes below regarding "double poll" and |
| 6033 | * invalid packets. |
| 6034 | */ |
| 6035 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 6036 | BT_DBG("Invalid/Ignore - after SREJ"); |
| 6037 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 6038 | } else { |
| 6039 | BT_DBG("Invalid - in window after SREJ sent"); |
| 6040 | return L2CAP_TXSEQ_INVALID; |
| 6041 | } |
| 6042 | } |
| 6043 | |
| 6044 | if (chan->srej_list.head == txseq) { |
| 6045 | BT_DBG("Expected SREJ"); |
| 6046 | return L2CAP_TXSEQ_EXPECTED_SREJ; |
| 6047 | } |
| 6048 | |
| 6049 | if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) { |
| 6050 | BT_DBG("Duplicate SREJ - txseq already stored"); |
| 6051 | return L2CAP_TXSEQ_DUPLICATE_SREJ; |
| 6052 | } |
| 6053 | |
| 6054 | if (l2cap_seq_list_contains(&chan->srej_list, txseq)) { |
| 6055 | BT_DBG("Unexpected SREJ - not requested"); |
| 6056 | return L2CAP_TXSEQ_UNEXPECTED_SREJ; |
| 6057 | } |
| 6058 | } |
| 6059 | |
| 6060 | if (chan->expected_tx_seq == txseq) { |
| 6061 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
| 6062 | chan->tx_win) { |
| 6063 | BT_DBG("Invalid - txseq outside tx window"); |
| 6064 | return L2CAP_TXSEQ_INVALID; |
| 6065 | } else { |
| 6066 | BT_DBG("Expected"); |
| 6067 | return L2CAP_TXSEQ_EXPECTED; |
| 6068 | } |
| 6069 | } |
| 6070 | |
| 6071 | if (__seq_offset(chan, txseq, chan->last_acked_seq) < |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6072 | __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6073 | BT_DBG("Duplicate - expected_tx_seq later than txseq"); |
| 6074 | return L2CAP_TXSEQ_DUPLICATE; |
| 6075 | } |
| 6076 | |
| 6077 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) { |
| 6078 | /* A source of invalid packets is a "double poll" condition, |
| 6079 | * where delays cause us to send multiple poll packets. If |
| 6080 | * the remote stack receives and processes both polls, |
| 6081 | * sequence numbers can wrap around in such a way that a |
| 6082 | * resent frame has a sequence number that looks like new data |
| 6083 | * with a sequence gap. This would trigger an erroneous SREJ |
| 6084 | * request. |
| 6085 | * |
| 6086 | * Fortunately, this is impossible with a tx window that's |
| 6087 | * less than half of the maximum sequence number, which allows |
| 6088 | * invalid frames to be safely ignored. |
| 6089 | * |
| 6090 | * With tx window sizes greater than half of the tx window |
| 6091 | * maximum, the frame is invalid and cannot be ignored. This |
| 6092 | * causes a disconnect. |
| 6093 | */ |
| 6094 | |
| 6095 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 6096 | BT_DBG("Invalid/Ignore - txseq outside tx window"); |
| 6097 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 6098 | } else { |
| 6099 | BT_DBG("Invalid - txseq outside tx window"); |
| 6100 | return L2CAP_TXSEQ_INVALID; |
| 6101 | } |
| 6102 | } else { |
| 6103 | BT_DBG("Unexpected - txseq indicates missing frames"); |
| 6104 | return L2CAP_TXSEQ_UNEXPECTED; |
| 6105 | } |
| 6106 | } |
| 6107 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6108 | static int l2cap_rx_state_recv(struct l2cap_chan *chan, |
| 6109 | struct l2cap_ctrl *control, |
| 6110 | struct sk_buff *skb, u8 event) |
| 6111 | { |
| 6112 | int err = 0; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6113 | bool skb_in_use = false; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6114 | |
| 6115 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6116 | event); |
| 6117 | |
| 6118 | switch (event) { |
| 6119 | case L2CAP_EV_RECV_IFRAME: |
| 6120 | switch (l2cap_classify_txseq(chan, control->txseq)) { |
| 6121 | case L2CAP_TXSEQ_EXPECTED: |
| 6122 | l2cap_pass_to_tx(chan, control); |
| 6123 | |
| 6124 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 6125 | BT_DBG("Busy, discarding expected seq %d", |
| 6126 | control->txseq); |
| 6127 | break; |
| 6128 | } |
| 6129 | |
| 6130 | chan->expected_tx_seq = __next_seq(chan, |
| 6131 | control->txseq); |
| 6132 | |
| 6133 | chan->buffer_seq = chan->expected_tx_seq; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6134 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6135 | |
| 6136 | err = l2cap_reassemble_sdu(chan, skb, control); |
| 6137 | if (err) |
| 6138 | break; |
| 6139 | |
| 6140 | if (control->final) { |
| 6141 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 6142 | &chan->conn_state)) { |
| 6143 | control->final = 0; |
| 6144 | l2cap_retransmit_all(chan, control); |
| 6145 | l2cap_ertm_send(chan); |
| 6146 | } |
| 6147 | } |
| 6148 | |
| 6149 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 6150 | l2cap_send_ack(chan); |
| 6151 | break; |
| 6152 | case L2CAP_TXSEQ_UNEXPECTED: |
| 6153 | l2cap_pass_to_tx(chan, control); |
| 6154 | |
| 6155 | /* Can't issue SREJ frames in the local busy state. |
| 6156 | * Drop this frame, it will be seen as missing |
| 6157 | * when local busy is exited. |
| 6158 | */ |
| 6159 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 6160 | BT_DBG("Busy, discarding unexpected seq %d", |
| 6161 | control->txseq); |
| 6162 | break; |
| 6163 | } |
| 6164 | |
| 6165 | /* There was a gap in the sequence, so an SREJ |
| 6166 | * must be sent for each missing frame. The |
| 6167 | * current frame is stored for later use. |
| 6168 | */ |
| 6169 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6170 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6171 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6172 | skb_queue_len(&chan->srej_q)); |
| 6173 | |
| 6174 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 6175 | l2cap_seq_list_clear(&chan->srej_list); |
| 6176 | l2cap_send_srej(chan, control->txseq); |
| 6177 | |
| 6178 | chan->rx_state = L2CAP_RX_STATE_SREJ_SENT; |
| 6179 | break; |
| 6180 | case L2CAP_TXSEQ_DUPLICATE: |
| 6181 | l2cap_pass_to_tx(chan, control); |
| 6182 | break; |
| 6183 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 6184 | break; |
| 6185 | case L2CAP_TXSEQ_INVALID: |
| 6186 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6187 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6188 | break; |
| 6189 | } |
| 6190 | break; |
| 6191 | case L2CAP_EV_RECV_RR: |
| 6192 | l2cap_pass_to_tx(chan, control); |
| 6193 | if (control->final) { |
| 6194 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6195 | |
Mat Martineau | e6a3ee6 | 2012-10-23 15:24:22 -0700 | [diff] [blame] | 6196 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) && |
| 6197 | !__chan_is_moving(chan)) { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6198 | control->final = 0; |
| 6199 | l2cap_retransmit_all(chan, control); |
| 6200 | } |
| 6201 | |
| 6202 | l2cap_ertm_send(chan); |
| 6203 | } else if (control->poll) { |
| 6204 | l2cap_send_i_or_rr_or_rnr(chan); |
| 6205 | } else { |
| 6206 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6207 | &chan->conn_state) && |
| 6208 | chan->unacked_frames) |
| 6209 | __set_retrans_timer(chan); |
| 6210 | |
| 6211 | l2cap_ertm_send(chan); |
| 6212 | } |
| 6213 | break; |
| 6214 | case L2CAP_EV_RECV_RNR: |
| 6215 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6216 | l2cap_pass_to_tx(chan, control); |
| 6217 | if (control && control->poll) { |
| 6218 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6219 | l2cap_send_rr_or_rnr(chan, 0); |
| 6220 | } |
| 6221 | __clear_retrans_timer(chan); |
| 6222 | l2cap_seq_list_clear(&chan->retrans_list); |
| 6223 | break; |
| 6224 | case L2CAP_EV_RECV_REJ: |
| 6225 | l2cap_handle_rej(chan, control); |
| 6226 | break; |
| 6227 | case L2CAP_EV_RECV_SREJ: |
| 6228 | l2cap_handle_srej(chan, control); |
| 6229 | break; |
| 6230 | default: |
| 6231 | break; |
| 6232 | } |
| 6233 | |
| 6234 | if (skb && !skb_in_use) { |
| 6235 | BT_DBG("Freeing %p", skb); |
| 6236 | kfree_skb(skb); |
| 6237 | } |
| 6238 | |
| 6239 | return err; |
| 6240 | } |
| 6241 | |
| 6242 | static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan, |
| 6243 | struct l2cap_ctrl *control, |
| 6244 | struct sk_buff *skb, u8 event) |
| 6245 | { |
| 6246 | int err = 0; |
| 6247 | u16 txseq = control->txseq; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6248 | bool skb_in_use = false; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6249 | |
| 6250 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6251 | event); |
| 6252 | |
| 6253 | switch (event) { |
| 6254 | case L2CAP_EV_RECV_IFRAME: |
| 6255 | switch (l2cap_classify_txseq(chan, txseq)) { |
| 6256 | case L2CAP_TXSEQ_EXPECTED: |
| 6257 | /* Keep frame for reassembly later */ |
| 6258 | l2cap_pass_to_tx(chan, control); |
| 6259 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6260 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6261 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6262 | skb_queue_len(&chan->srej_q)); |
| 6263 | |
| 6264 | chan->expected_tx_seq = __next_seq(chan, txseq); |
| 6265 | break; |
| 6266 | case L2CAP_TXSEQ_EXPECTED_SREJ: |
| 6267 | l2cap_seq_list_pop(&chan->srej_list); |
| 6268 | |
| 6269 | l2cap_pass_to_tx(chan, control); |
| 6270 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6271 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6272 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6273 | skb_queue_len(&chan->srej_q)); |
| 6274 | |
| 6275 | err = l2cap_rx_queued_iframes(chan); |
| 6276 | if (err) |
| 6277 | break; |
| 6278 | |
| 6279 | break; |
| 6280 | case L2CAP_TXSEQ_UNEXPECTED: |
| 6281 | /* Got a frame that can't be reassembled yet. |
| 6282 | * Save it for later, and send SREJs to cover |
| 6283 | * the missing frames. |
| 6284 | */ |
| 6285 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6286 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6287 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6288 | skb_queue_len(&chan->srej_q)); |
| 6289 | |
| 6290 | l2cap_pass_to_tx(chan, control); |
| 6291 | l2cap_send_srej(chan, control->txseq); |
| 6292 | break; |
| 6293 | case L2CAP_TXSEQ_UNEXPECTED_SREJ: |
| 6294 | /* This frame was requested with an SREJ, but |
| 6295 | * some expected retransmitted frames are |
| 6296 | * missing. Request retransmission of missing |
| 6297 | * SREJ'd frames. |
| 6298 | */ |
| 6299 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6300 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6301 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6302 | skb_queue_len(&chan->srej_q)); |
| 6303 | |
| 6304 | l2cap_pass_to_tx(chan, control); |
| 6305 | l2cap_send_srej_list(chan, control->txseq); |
| 6306 | break; |
| 6307 | case L2CAP_TXSEQ_DUPLICATE_SREJ: |
| 6308 | /* We've already queued this frame. Drop this copy. */ |
| 6309 | l2cap_pass_to_tx(chan, control); |
| 6310 | break; |
| 6311 | case L2CAP_TXSEQ_DUPLICATE: |
| 6312 | /* Expecting a later sequence number, so this frame |
| 6313 | * was already received. Ignore it completely. |
| 6314 | */ |
| 6315 | break; |
| 6316 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 6317 | break; |
| 6318 | case L2CAP_TXSEQ_INVALID: |
| 6319 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6320 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6321 | break; |
| 6322 | } |
| 6323 | break; |
| 6324 | case L2CAP_EV_RECV_RR: |
| 6325 | l2cap_pass_to_tx(chan, control); |
| 6326 | if (control->final) { |
| 6327 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6328 | |
| 6329 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 6330 | &chan->conn_state)) { |
| 6331 | control->final = 0; |
| 6332 | l2cap_retransmit_all(chan, control); |
| 6333 | } |
| 6334 | |
| 6335 | l2cap_ertm_send(chan); |
| 6336 | } else if (control->poll) { |
| 6337 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6338 | &chan->conn_state) && |
| 6339 | chan->unacked_frames) { |
| 6340 | __set_retrans_timer(chan); |
| 6341 | } |
| 6342 | |
| 6343 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6344 | l2cap_send_srej_tail(chan); |
| 6345 | } else { |
| 6346 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6347 | &chan->conn_state) && |
| 6348 | chan->unacked_frames) |
| 6349 | __set_retrans_timer(chan); |
| 6350 | |
| 6351 | l2cap_send_ack(chan); |
| 6352 | } |
| 6353 | break; |
| 6354 | case L2CAP_EV_RECV_RNR: |
| 6355 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6356 | l2cap_pass_to_tx(chan, control); |
| 6357 | if (control->poll) { |
| 6358 | l2cap_send_srej_tail(chan); |
| 6359 | } else { |
| 6360 | struct l2cap_ctrl rr_control; |
| 6361 | memset(&rr_control, 0, sizeof(rr_control)); |
| 6362 | rr_control.sframe = 1; |
| 6363 | rr_control.super = L2CAP_SUPER_RR; |
| 6364 | rr_control.reqseq = chan->buffer_seq; |
| 6365 | l2cap_send_sframe(chan, &rr_control); |
| 6366 | } |
| 6367 | |
| 6368 | break; |
| 6369 | case L2CAP_EV_RECV_REJ: |
| 6370 | l2cap_handle_rej(chan, control); |
| 6371 | break; |
| 6372 | case L2CAP_EV_RECV_SREJ: |
| 6373 | l2cap_handle_srej(chan, control); |
| 6374 | break; |
| 6375 | } |
| 6376 | |
| 6377 | if (skb && !skb_in_use) { |
| 6378 | BT_DBG("Freeing %p", skb); |
| 6379 | kfree_skb(skb); |
| 6380 | } |
| 6381 | |
| 6382 | return err; |
| 6383 | } |
| 6384 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6385 | static int l2cap_finish_move(struct l2cap_chan *chan) |
| 6386 | { |
| 6387 | BT_DBG("chan %p", chan); |
| 6388 | |
| 6389 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6390 | |
| 6391 | if (chan->hs_hcon) |
| 6392 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6393 | else |
| 6394 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6395 | |
| 6396 | return l2cap_resegment(chan); |
| 6397 | } |
| 6398 | |
| 6399 | static int l2cap_rx_state_wait_p(struct l2cap_chan *chan, |
| 6400 | struct l2cap_ctrl *control, |
| 6401 | struct sk_buff *skb, u8 event) |
| 6402 | { |
| 6403 | int err; |
| 6404 | |
| 6405 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6406 | event); |
| 6407 | |
| 6408 | if (!control->poll) |
| 6409 | return -EPROTO; |
| 6410 | |
| 6411 | l2cap_process_reqseq(chan, control->reqseq); |
| 6412 | |
| 6413 | if (!skb_queue_empty(&chan->tx_q)) |
| 6414 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6415 | else |
| 6416 | chan->tx_send_head = NULL; |
| 6417 | |
| 6418 | /* Rewind next_tx_seq to the point expected |
| 6419 | * by the receiver. |
| 6420 | */ |
| 6421 | chan->next_tx_seq = control->reqseq; |
| 6422 | chan->unacked_frames = 0; |
| 6423 | |
| 6424 | err = l2cap_finish_move(chan); |
| 6425 | if (err) |
| 6426 | return err; |
| 6427 | |
| 6428 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6429 | l2cap_send_i_or_rr_or_rnr(chan); |
| 6430 | |
| 6431 | if (event == L2CAP_EV_RECV_IFRAME) |
| 6432 | return -EPROTO; |
| 6433 | |
| 6434 | return l2cap_rx_state_recv(chan, control, NULL, event); |
| 6435 | } |
| 6436 | |
| 6437 | static int l2cap_rx_state_wait_f(struct l2cap_chan *chan, |
| 6438 | struct l2cap_ctrl *control, |
| 6439 | struct sk_buff *skb, u8 event) |
| 6440 | { |
| 6441 | int err; |
| 6442 | |
| 6443 | if (!control->final) |
| 6444 | return -EPROTO; |
| 6445 | |
| 6446 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6447 | |
| 6448 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6449 | l2cap_process_reqseq(chan, control->reqseq); |
| 6450 | |
| 6451 | if (!skb_queue_empty(&chan->tx_q)) |
| 6452 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6453 | else |
| 6454 | chan->tx_send_head = NULL; |
| 6455 | |
| 6456 | /* Rewind next_tx_seq to the point expected |
| 6457 | * by the receiver. |
| 6458 | */ |
| 6459 | chan->next_tx_seq = control->reqseq; |
| 6460 | chan->unacked_frames = 0; |
| 6461 | |
| 6462 | if (chan->hs_hcon) |
| 6463 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6464 | else |
| 6465 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6466 | |
| 6467 | err = l2cap_resegment(chan); |
| 6468 | |
| 6469 | if (!err) |
| 6470 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6471 | |
| 6472 | return err; |
| 6473 | } |
| 6474 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6475 | static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 6476 | { |
| 6477 | /* Make sure reqseq is for a packet that has been sent but not acked */ |
| 6478 | u16 unacked; |
| 6479 | |
| 6480 | unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq); |
| 6481 | return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked; |
| 6482 | } |
| 6483 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6484 | static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6485 | struct sk_buff *skb, u8 event) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6486 | { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6487 | int err = 0; |
| 6488 | |
| 6489 | BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan, |
| 6490 | control, skb, event, chan->rx_state); |
| 6491 | |
| 6492 | if (__valid_reqseq(chan, control->reqseq)) { |
| 6493 | switch (chan->rx_state) { |
| 6494 | case L2CAP_RX_STATE_RECV: |
| 6495 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6496 | break; |
| 6497 | case L2CAP_RX_STATE_SREJ_SENT: |
| 6498 | err = l2cap_rx_state_srej_sent(chan, control, skb, |
| 6499 | event); |
| 6500 | break; |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6501 | case L2CAP_RX_STATE_WAIT_P: |
| 6502 | err = l2cap_rx_state_wait_p(chan, control, skb, event); |
| 6503 | break; |
| 6504 | case L2CAP_RX_STATE_WAIT_F: |
| 6505 | err = l2cap_rx_state_wait_f(chan, control, skb, event); |
| 6506 | break; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6507 | default: |
| 6508 | /* shut it down */ |
| 6509 | break; |
| 6510 | } |
| 6511 | } else { |
| 6512 | BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d", |
| 6513 | control->reqseq, chan->next_tx_seq, |
| 6514 | chan->expected_ack_seq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6515 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6516 | } |
| 6517 | |
| 6518 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6519 | } |
| 6520 | |
| 6521 | static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6522 | struct sk_buff *skb) |
| 6523 | { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6524 | int err = 0; |
| 6525 | |
| 6526 | BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb, |
| 6527 | chan->rx_state); |
| 6528 | |
| 6529 | if (l2cap_classify_txseq(chan, control->txseq) == |
| 6530 | L2CAP_TXSEQ_EXPECTED) { |
| 6531 | l2cap_pass_to_tx(chan, control); |
| 6532 | |
| 6533 | BT_DBG("buffer_seq %d->%d", chan->buffer_seq, |
| 6534 | __next_seq(chan, chan->buffer_seq)); |
| 6535 | |
| 6536 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 6537 | |
| 6538 | l2cap_reassemble_sdu(chan, skb, control); |
| 6539 | } else { |
| 6540 | if (chan->sdu) { |
| 6541 | kfree_skb(chan->sdu); |
| 6542 | chan->sdu = NULL; |
| 6543 | } |
| 6544 | chan->sdu_last_frag = NULL; |
| 6545 | chan->sdu_len = 0; |
| 6546 | |
| 6547 | if (skb) { |
| 6548 | BT_DBG("Freeing %p", skb); |
| 6549 | kfree_skb(skb); |
| 6550 | } |
| 6551 | } |
| 6552 | |
| 6553 | chan->last_acked_seq = control->txseq; |
| 6554 | chan->expected_tx_seq = __next_seq(chan, control->txseq); |
| 6555 | |
| 6556 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6557 | } |
| 6558 | |
| 6559 | static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) |
| 6560 | { |
| 6561 | struct l2cap_ctrl *control = &bt_cb(skb)->control; |
| 6562 | u16 len; |
| 6563 | u8 event; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6564 | |
Mat Martineau | b76bbd6 | 2012-04-11 10:48:43 -0700 | [diff] [blame] | 6565 | __unpack_control(chan, skb); |
| 6566 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6567 | len = skb->len; |
| 6568 | |
| 6569 | /* |
| 6570 | * We can just drop the corrupted I-frame here. |
| 6571 | * Receiver will miss it and start proper recovery |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6572 | * procedures and ask for retransmission. |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6573 | */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6574 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6575 | goto drop; |
| 6576 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6577 | if (!control->sframe && control->sar == L2CAP_SAR_START) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6578 | len -= L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6579 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6580 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6581 | len -= L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6582 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6583 | if (len > chan->mps) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6584 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6585 | goto drop; |
| 6586 | } |
| 6587 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6588 | if (!control->sframe) { |
| 6589 | int err; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6590 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6591 | BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d", |
| 6592 | control->sar, control->reqseq, control->final, |
| 6593 | control->txseq); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 6594 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6595 | /* Validate F-bit - F=0 always valid, F=1 only |
| 6596 | * valid in TX WAIT_F |
| 6597 | */ |
| 6598 | if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6599 | goto drop; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6600 | |
| 6601 | if (chan->mode != L2CAP_MODE_STREAMING) { |
| 6602 | event = L2CAP_EV_RECV_IFRAME; |
| 6603 | err = l2cap_rx(chan, control, skb, event); |
| 6604 | } else { |
| 6605 | err = l2cap_stream_rx(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6606 | } |
| 6607 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6608 | if (err) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6609 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6610 | } else { |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6611 | const u8 rx_func_to_event[4] = { |
| 6612 | L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ, |
| 6613 | L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ |
| 6614 | }; |
| 6615 | |
| 6616 | /* Only I-frames are expected in streaming mode */ |
| 6617 | if (chan->mode == L2CAP_MODE_STREAMING) |
| 6618 | goto drop; |
| 6619 | |
| 6620 | BT_DBG("sframe reqseq %d, final %d, poll %d, super %d", |
| 6621 | control->reqseq, control->final, control->poll, |
| 6622 | control->super); |
| 6623 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6624 | if (len != 0) { |
Andrei Emeltchenko | 1bb166e | 2012-11-20 17:16:21 +0200 | [diff] [blame] | 6625 | BT_ERR("Trailing bytes: %d in sframe", len); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6626 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6627 | goto drop; |
| 6628 | } |
| 6629 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6630 | /* Validate F and P bits */ |
| 6631 | if (control->final && (control->poll || |
| 6632 | chan->tx_state != L2CAP_TX_STATE_WAIT_F)) |
| 6633 | goto drop; |
| 6634 | |
| 6635 | event = rx_func_to_event[control->super]; |
| 6636 | if (l2cap_rx(chan, control, skb, event)) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6637 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6638 | } |
| 6639 | |
| 6640 | return 0; |
| 6641 | |
| 6642 | drop: |
| 6643 | kfree_skb(skb); |
| 6644 | return 0; |
| 6645 | } |
| 6646 | |
Johan Hedberg | b1c325c | 2013-12-05 09:43:34 +0200 | [diff] [blame] | 6647 | static void l2cap_chan_le_send_credits(struct l2cap_chan *chan) |
| 6648 | { |
| 6649 | struct l2cap_conn *conn = chan->conn; |
| 6650 | struct l2cap_le_credits pkt; |
| 6651 | u16 return_credits; |
| 6652 | |
| 6653 | /* We return more credits to the sender only after the amount of |
| 6654 | * credits falls below half of the initial amount. |
| 6655 | */ |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 6656 | if (chan->rx_credits >= (le_max_credits + 1) / 2) |
Johan Hedberg | b1c325c | 2013-12-05 09:43:34 +0200 | [diff] [blame] | 6657 | return; |
| 6658 | |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 6659 | return_credits = le_max_credits - chan->rx_credits; |
Johan Hedberg | b1c325c | 2013-12-05 09:43:34 +0200 | [diff] [blame] | 6660 | |
| 6661 | BT_DBG("chan %p returning %u credits to sender", chan, return_credits); |
| 6662 | |
| 6663 | chan->rx_credits += return_credits; |
| 6664 | |
| 6665 | pkt.cid = cpu_to_le16(chan->scid); |
| 6666 | pkt.credits = cpu_to_le16(return_credits); |
| 6667 | |
| 6668 | chan->ident = l2cap_get_ident(conn); |
| 6669 | |
| 6670 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt); |
| 6671 | } |
| 6672 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6673 | static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) |
| 6674 | { |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6675 | int err; |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6676 | |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6677 | if (!chan->rx_credits) { |
| 6678 | BT_ERR("No credits to receive LE L2CAP data"); |
Johan Hedberg | dfd9774 | 2014-01-27 15:11:34 -0800 | [diff] [blame] | 6679 | l2cap_send_disconn_req(chan, ECONNRESET); |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6680 | return -ENOBUFS; |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6681 | } |
| 6682 | |
| 6683 | if (chan->imtu < skb->len) { |
| 6684 | BT_ERR("Too big LE L2CAP PDU"); |
| 6685 | return -ENOBUFS; |
| 6686 | } |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6687 | |
| 6688 | chan->rx_credits--; |
| 6689 | BT_DBG("rx_credits %u -> %u", chan->rx_credits + 1, chan->rx_credits); |
| 6690 | |
| 6691 | l2cap_chan_le_send_credits(chan); |
| 6692 | |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6693 | err = 0; |
| 6694 | |
| 6695 | if (!chan->sdu) { |
| 6696 | u16 sdu_len; |
| 6697 | |
| 6698 | sdu_len = get_unaligned_le16(skb->data); |
| 6699 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
| 6700 | |
| 6701 | BT_DBG("Start of new SDU. sdu_len %u skb->len %u imtu %u", |
| 6702 | sdu_len, skb->len, chan->imtu); |
| 6703 | |
| 6704 | if (sdu_len > chan->imtu) { |
| 6705 | BT_ERR("Too big LE L2CAP SDU length received"); |
| 6706 | err = -EMSGSIZE; |
| 6707 | goto failed; |
| 6708 | } |
| 6709 | |
| 6710 | if (skb->len > sdu_len) { |
| 6711 | BT_ERR("Too much LE L2CAP data received"); |
| 6712 | err = -EINVAL; |
| 6713 | goto failed; |
| 6714 | } |
| 6715 | |
| 6716 | if (skb->len == sdu_len) |
| 6717 | return chan->ops->recv(chan, skb); |
| 6718 | |
| 6719 | chan->sdu = skb; |
| 6720 | chan->sdu_len = sdu_len; |
| 6721 | chan->sdu_last_frag = skb; |
| 6722 | |
| 6723 | return 0; |
| 6724 | } |
| 6725 | |
| 6726 | BT_DBG("SDU fragment. chan->sdu->len %u skb->len %u chan->sdu_len %u", |
| 6727 | chan->sdu->len, skb->len, chan->sdu_len); |
| 6728 | |
| 6729 | if (chan->sdu->len + skb->len > chan->sdu_len) { |
| 6730 | BT_ERR("Too much LE L2CAP data received"); |
| 6731 | err = -EINVAL; |
| 6732 | goto failed; |
| 6733 | } |
| 6734 | |
| 6735 | append_skb_frag(chan->sdu, skb, &chan->sdu_last_frag); |
| 6736 | skb = NULL; |
| 6737 | |
| 6738 | if (chan->sdu->len == chan->sdu_len) { |
| 6739 | err = chan->ops->recv(chan, chan->sdu); |
| 6740 | if (!err) { |
| 6741 | chan->sdu = NULL; |
| 6742 | chan->sdu_last_frag = NULL; |
| 6743 | chan->sdu_len = 0; |
| 6744 | } |
| 6745 | } |
| 6746 | |
| 6747 | failed: |
| 6748 | if (err) { |
| 6749 | kfree_skb(skb); |
| 6750 | kfree_skb(chan->sdu); |
| 6751 | chan->sdu = NULL; |
| 6752 | chan->sdu_last_frag = NULL; |
| 6753 | chan->sdu_len = 0; |
| 6754 | } |
| 6755 | |
| 6756 | /* We can't return an error here since we took care of the skb |
| 6757 | * freeing internally. An error return would cause the caller to |
| 6758 | * do a double-free of the skb. |
| 6759 | */ |
| 6760 | return 0; |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6761 | } |
| 6762 | |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6763 | static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid, |
| 6764 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6765 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6766 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6767 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 6768 | chan = l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6769 | if (!chan) { |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6770 | if (cid == L2CAP_CID_A2MP) { |
| 6771 | chan = a2mp_channel_create(conn, skb); |
| 6772 | if (!chan) { |
| 6773 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6774 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6775 | } |
| 6776 | |
| 6777 | l2cap_chan_lock(chan); |
| 6778 | } else { |
| 6779 | BT_DBG("unknown cid 0x%4.4x", cid); |
| 6780 | /* Drop packet and return */ |
| 6781 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6782 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6784 | } |
| 6785 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 6786 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6787 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6788 | if (chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6789 | goto drop; |
| 6790 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6791 | switch (chan->mode) { |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 6792 | case L2CAP_MODE_LE_FLOWCTL: |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6793 | if (l2cap_le_data_rcv(chan, skb) < 0) |
| 6794 | goto drop; |
| 6795 | |
| 6796 | goto done; |
| 6797 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6798 | case L2CAP_MODE_BASIC: |
| 6799 | /* If socket recv buffers overflows we drop data here |
| 6800 | * which is *bad* because L2CAP has to be reliable. |
| 6801 | * But we don't have any other choice. L2CAP doesn't |
| 6802 | * provide flow control mechanism. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6803 | |
Szymon Janc | 2c96e03 | 2014-02-18 20:48:34 +0100 | [diff] [blame] | 6804 | if (chan->imtu < skb->len) { |
| 6805 | BT_ERR("Dropping L2CAP data: receive buffer overflow"); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6806 | goto drop; |
Szymon Janc | 2c96e03 | 2014-02-18 20:48:34 +0100 | [diff] [blame] | 6807 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6808 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6809 | if (!chan->ops->recv(chan, skb)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6810 | goto done; |
| 6811 | break; |
| 6812 | |
| 6813 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6814 | case L2CAP_MODE_STREAMING: |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6815 | l2cap_data_rcv(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6816 | goto done; |
| 6817 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6818 | default: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6819 | 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] | 6820 | break; |
| 6821 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | |
| 6823 | drop: |
| 6824 | kfree_skb(skb); |
| 6825 | |
| 6826 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6827 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6828 | } |
| 6829 | |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6830 | static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, |
| 6831 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6832 | { |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6833 | struct hci_conn *hcon = conn->hcon; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6834 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6835 | |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6836 | if (hcon->type != ACL_LINK) |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6837 | goto free_skb; |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6838 | |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 6839 | chan = l2cap_global_chan_by_psm(0, psm, &hcon->src, &hcon->dst, |
| 6840 | ACL_LINK); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6841 | if (!chan) |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6842 | goto free_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6843 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 6844 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6845 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6846 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6847 | goto drop; |
| 6848 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 6849 | if (chan->imtu < skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6850 | goto drop; |
| 6851 | |
Marcel Holtmann | 2edf870 | 2013-10-13 12:55:29 -0700 | [diff] [blame] | 6852 | /* Store remote BD_ADDR and PSM for msg_name */ |
Marcel Holtmann | 06ae331 | 2013-10-18 03:43:00 -0700 | [diff] [blame] | 6853 | bacpy(&bt_cb(skb)->bdaddr, &hcon->dst); |
Marcel Holtmann | 2edf870 | 2013-10-13 12:55:29 -0700 | [diff] [blame] | 6854 | bt_cb(skb)->psm = psm; |
| 6855 | |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6856 | if (!chan->ops->recv(chan, skb)) { |
| 6857 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6858 | return; |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6859 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6860 | |
| 6861 | drop: |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6862 | l2cap_chan_put(chan); |
| 6863 | free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6864 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6865 | } |
| 6866 | |
| 6867 | static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) |
| 6868 | { |
| 6869 | struct l2cap_hdr *lh = (void *) skb->data; |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6870 | struct hci_conn *hcon = conn->hcon; |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 6871 | u16 cid, len; |
| 6872 | __le16 psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6873 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6874 | if (hcon->state != BT_CONNECTED) { |
| 6875 | BT_DBG("queueing pending rx skb"); |
| 6876 | skb_queue_tail(&conn->pending_rx, skb); |
| 6877 | return; |
| 6878 | } |
| 6879 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6880 | skb_pull(skb, L2CAP_HDR_SIZE); |
| 6881 | cid = __le16_to_cpu(lh->cid); |
| 6882 | len = __le16_to_cpu(lh->len); |
| 6883 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6884 | if (len != skb->len) { |
| 6885 | kfree_skb(skb); |
| 6886 | return; |
| 6887 | } |
| 6888 | |
Johan Hedberg | 9e1d7e1 | 2014-07-06 11:03:36 +0300 | [diff] [blame] | 6889 | /* Since we can't actively block incoming LE connections we must |
| 6890 | * at least ensure that we ignore incoming data from them. |
| 6891 | */ |
| 6892 | if (hcon->type == LE_LINK && |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 6893 | hci_bdaddr_list_lookup(&hcon->hdev->blacklist, &hcon->dst, |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 6894 | bdaddr_dst_type(hcon))) { |
Johan Hedberg | e493150 | 2014-07-02 09:36:21 +0300 | [diff] [blame] | 6895 | kfree_skb(skb); |
| 6896 | return; |
| 6897 | } |
| 6898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6899 | BT_DBG("len %d, cid 0x%4.4x", len, cid); |
| 6900 | |
| 6901 | switch (cid) { |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6902 | case L2CAP_CID_SIGNALING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6903 | l2cap_sig_channel(conn, skb); |
| 6904 | break; |
| 6905 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6906 | case L2CAP_CID_CONN_LESS: |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 6907 | psm = get_unaligned((__le16 *) skb->data); |
Andrei Emeltchenko | 0181a70 | 2012-05-29 10:04:05 +0300 | [diff] [blame] | 6908 | skb_pull(skb, L2CAP_PSMLEN_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6909 | l2cap_conless_channel(conn, psm, skb); |
| 6910 | break; |
| 6911 | |
Marcel Holtmann | a287762 | 2013-10-02 23:46:54 -0700 | [diff] [blame] | 6912 | case L2CAP_CID_LE_SIGNALING: |
| 6913 | l2cap_le_sig_channel(conn, skb); |
| 6914 | break; |
| 6915 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6916 | default: |
| 6917 | l2cap_data_channel(conn, cid, skb); |
| 6918 | break; |
| 6919 | } |
| 6920 | } |
| 6921 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6922 | static void process_pending_rx(struct work_struct *work) |
| 6923 | { |
| 6924 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
| 6925 | pending_rx_work); |
| 6926 | struct sk_buff *skb; |
| 6927 | |
| 6928 | BT_DBG(""); |
| 6929 | |
| 6930 | while ((skb = skb_dequeue(&conn->pending_rx))) |
| 6931 | l2cap_recv_frame(conn, skb); |
| 6932 | } |
| 6933 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6934 | static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) |
| 6935 | { |
| 6936 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 6937 | struct hci_chan *hchan; |
| 6938 | |
| 6939 | if (conn) |
| 6940 | return conn; |
| 6941 | |
| 6942 | hchan = hci_chan_create(hcon); |
| 6943 | if (!hchan) |
| 6944 | return NULL; |
| 6945 | |
Johan Hedberg | 27f70f3 | 2014-07-21 10:50:06 +0300 | [diff] [blame] | 6946 | conn = kzalloc(sizeof(*conn), GFP_KERNEL); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6947 | if (!conn) { |
| 6948 | hci_chan_del(hchan); |
| 6949 | return NULL; |
| 6950 | } |
| 6951 | |
| 6952 | kref_init(&conn->ref); |
| 6953 | hcon->l2cap_data = conn; |
Johan Hedberg | 51bb8457 | 2014-08-15 21:06:57 +0300 | [diff] [blame] | 6954 | conn->hcon = hci_conn_get(hcon); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6955 | conn->hchan = hchan; |
| 6956 | |
| 6957 | BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); |
| 6958 | |
| 6959 | switch (hcon->type) { |
| 6960 | case LE_LINK: |
| 6961 | if (hcon->hdev->le_mtu) { |
| 6962 | conn->mtu = hcon->hdev->le_mtu; |
| 6963 | break; |
| 6964 | } |
| 6965 | /* fall through */ |
| 6966 | default: |
| 6967 | conn->mtu = hcon->hdev->acl_mtu; |
| 6968 | break; |
| 6969 | } |
| 6970 | |
| 6971 | conn->feat_mask = 0; |
| 6972 | |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 6973 | conn->local_fixed_chan = L2CAP_FC_SIG_BREDR | L2CAP_FC_CONNLESS; |
| 6974 | |
| 6975 | if (hcon->type == ACL_LINK && |
| 6976 | test_bit(HCI_HS_ENABLED, &hcon->hdev->dev_flags)) |
| 6977 | conn->local_fixed_chan |= L2CAP_FC_A2MP; |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6978 | |
Marcel Holtmann | f9be9e8 | 2014-12-06 00:35:45 +0100 | [diff] [blame] | 6979 | if (test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags) && |
| 6980 | (bredr_sc_enabled(hcon->hdev) || |
Marcel Holtmann | 300acfde | 2014-12-31 14:43:16 -0800 | [diff] [blame] | 6981 | test_bit(HCI_FORCE_BREDR_SMP, &hcon->hdev->dbg_flags))) |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 6982 | conn->local_fixed_chan |= L2CAP_FC_SMP_BREDR; |
| 6983 | |
Marcel Holtmann | 5a54e7c | 2014-07-13 20:50:15 +0200 | [diff] [blame] | 6984 | mutex_init(&conn->ident_lock); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6985 | mutex_init(&conn->chan_lock); |
| 6986 | |
| 6987 | INIT_LIST_HEAD(&conn->chan_l); |
| 6988 | INIT_LIST_HEAD(&conn->users); |
| 6989 | |
Johan Hedberg | 276d807 | 2014-08-11 22:06:41 +0300 | [diff] [blame] | 6990 | INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6991 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6992 | skb_queue_head_init(&conn->pending_rx); |
| 6993 | INIT_WORK(&conn->pending_rx_work, process_pending_rx); |
Johan Hedberg | f3d82d0 | 2014-09-05 22:19:50 +0300 | [diff] [blame] | 6994 | INIT_WORK(&conn->id_addr_update_work, l2cap_conn_update_id_addr); |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6995 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6996 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
| 6997 | |
| 6998 | return conn; |
| 6999 | } |
| 7000 | |
| 7001 | static bool is_valid_psm(u16 psm, u8 dst_type) { |
| 7002 | if (!psm) |
| 7003 | return false; |
| 7004 | |
| 7005 | if (bdaddr_type_is_le(dst_type)) |
| 7006 | return (psm <= 0x00ff); |
| 7007 | |
| 7008 | /* PSM must be odd and lsb of upper byte must be 0 */ |
| 7009 | return ((psm & 0x0101) == 0x0001); |
| 7010 | } |
| 7011 | |
| 7012 | int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, |
| 7013 | bdaddr_t *dst, u8 dst_type) |
| 7014 | { |
| 7015 | struct l2cap_conn *conn; |
| 7016 | struct hci_conn *hcon; |
| 7017 | struct hci_dev *hdev; |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7018 | int err; |
| 7019 | |
| 7020 | BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", &chan->src, dst, |
| 7021 | dst_type, __le16_to_cpu(psm)); |
| 7022 | |
| 7023 | hdev = hci_get_route(dst, &chan->src); |
| 7024 | if (!hdev) |
| 7025 | return -EHOSTUNREACH; |
| 7026 | |
| 7027 | hci_dev_lock(hdev); |
| 7028 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7029 | if (!is_valid_psm(__le16_to_cpu(psm), dst_type) && !cid && |
| 7030 | chan->chan_type != L2CAP_CHAN_RAW) { |
| 7031 | err = -EINVAL; |
| 7032 | goto done; |
| 7033 | } |
| 7034 | |
Johan Hedberg | 21626e6 | 2014-01-24 10:35:41 +0200 | [diff] [blame] | 7035 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !psm) { |
| 7036 | err = -EINVAL; |
| 7037 | goto done; |
| 7038 | } |
| 7039 | |
| 7040 | if (chan->chan_type == L2CAP_CHAN_FIXED && !cid) { |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7041 | err = -EINVAL; |
| 7042 | goto done; |
| 7043 | } |
| 7044 | |
| 7045 | switch (chan->mode) { |
| 7046 | case L2CAP_MODE_BASIC: |
| 7047 | break; |
| 7048 | case L2CAP_MODE_LE_FLOWCTL: |
| 7049 | l2cap_le_flowctl_init(chan); |
| 7050 | break; |
| 7051 | case L2CAP_MODE_ERTM: |
| 7052 | case L2CAP_MODE_STREAMING: |
| 7053 | if (!disable_ertm) |
| 7054 | break; |
| 7055 | /* fall through */ |
| 7056 | default: |
Johan Hedberg | beb19e4 | 2014-07-18 11:15:26 +0300 | [diff] [blame] | 7057 | err = -EOPNOTSUPP; |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7058 | goto done; |
| 7059 | } |
| 7060 | |
| 7061 | switch (chan->state) { |
| 7062 | case BT_CONNECT: |
| 7063 | case BT_CONNECT2: |
| 7064 | case BT_CONFIG: |
| 7065 | /* Already connecting */ |
| 7066 | err = 0; |
| 7067 | goto done; |
| 7068 | |
| 7069 | case BT_CONNECTED: |
| 7070 | /* Already connected */ |
| 7071 | err = -EISCONN; |
| 7072 | goto done; |
| 7073 | |
| 7074 | case BT_OPEN: |
| 7075 | case BT_BOUND: |
| 7076 | /* Can connect */ |
| 7077 | break; |
| 7078 | |
| 7079 | default: |
| 7080 | err = -EBADFD; |
| 7081 | goto done; |
| 7082 | } |
| 7083 | |
| 7084 | /* Set destination address and psm */ |
| 7085 | bacpy(&chan->dst, dst); |
| 7086 | chan->dst_type = dst_type; |
| 7087 | |
| 7088 | chan->psm = psm; |
| 7089 | chan->dcid = cid; |
| 7090 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7091 | if (bdaddr_type_is_le(dst_type)) { |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 7092 | u8 role; |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 7093 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7094 | /* Convert from L2CAP channel address type to HCI address type |
| 7095 | */ |
| 7096 | if (dst_type == BDADDR_LE_PUBLIC) |
| 7097 | dst_type = ADDR_LE_DEV_PUBLIC; |
| 7098 | else |
| 7099 | dst_type = ADDR_LE_DEV_RANDOM; |
| 7100 | |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 7101 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 7102 | role = HCI_ROLE_SLAVE; |
| 7103 | else |
| 7104 | role = HCI_ROLE_MASTER; |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 7105 | |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 7106 | hcon = hci_connect_le(hdev, dst, dst_type, chan->sec_level, |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 7107 | HCI_LE_CONN_TIMEOUT, role); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7108 | } else { |
Johan Hedberg | d93375a | 2014-07-07 15:02:27 +0300 | [diff] [blame] | 7109 | u8 auth_type = l2cap_get_auth_type(chan); |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 7110 | hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7111 | } |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7112 | |
| 7113 | if (IS_ERR(hcon)) { |
| 7114 | err = PTR_ERR(hcon); |
| 7115 | goto done; |
| 7116 | } |
| 7117 | |
| 7118 | conn = l2cap_conn_add(hcon); |
| 7119 | if (!conn) { |
| 7120 | hci_conn_drop(hcon); |
| 7121 | err = -ENOMEM; |
| 7122 | goto done; |
| 7123 | } |
| 7124 | |
Johan Hedberg | 02e246a | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7125 | mutex_lock(&conn->chan_lock); |
| 7126 | l2cap_chan_lock(chan); |
| 7127 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7128 | if (cid && __l2cap_get_chan_by_dcid(conn, cid)) { |
| 7129 | hci_conn_drop(hcon); |
| 7130 | err = -EBUSY; |
Johan Hedberg | 02e246a | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7131 | goto chan_unlock; |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7132 | } |
| 7133 | |
| 7134 | /* Update source addr of the socket */ |
| 7135 | bacpy(&chan->src, &hcon->src); |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 7136 | chan->src_type = bdaddr_src_type(hcon); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7137 | |
Johan Hedberg | 02e246a | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7138 | __l2cap_chan_add(conn, chan); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7139 | |
| 7140 | /* l2cap_chan_add takes its own ref so we can drop this one */ |
| 7141 | hci_conn_drop(hcon); |
| 7142 | |
| 7143 | l2cap_state_change(chan, BT_CONNECT); |
| 7144 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
| 7145 | |
Johan Hedberg | 61202e4 | 2014-01-28 15:16:48 -0800 | [diff] [blame] | 7146 | /* Release chan->sport so that it can be reused by other |
| 7147 | * sockets (as it's only used for listening sockets). |
| 7148 | */ |
| 7149 | write_lock(&chan_list_lock); |
| 7150 | chan->sport = 0; |
| 7151 | write_unlock(&chan_list_lock); |
| 7152 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7153 | if (hcon->state == BT_CONNECTED) { |
| 7154 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
| 7155 | __clear_chan_timer(chan); |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 7156 | if (l2cap_chan_check_security(chan, true)) |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7157 | l2cap_state_change(chan, BT_CONNECTED); |
| 7158 | } else |
| 7159 | l2cap_do_start(chan); |
| 7160 | } |
| 7161 | |
| 7162 | err = 0; |
| 7163 | |
Johan Hedberg | 02e246a | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7164 | chan_unlock: |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7165 | l2cap_chan_unlock(chan); |
Johan Hedberg | 02e246a | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7166 | mutex_unlock(&conn->chan_lock); |
| 7167 | done: |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7168 | hci_dev_unlock(hdev); |
| 7169 | hci_dev_put(hdev); |
| 7170 | return err; |
| 7171 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 7172 | EXPORT_SYMBOL_GPL(l2cap_chan_connect); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7174 | /* ---- L2CAP interface with lower layer (HCI) ---- */ |
| 7175 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7176 | int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7177 | { |
| 7178 | int exact = 0, lm1 = 0, lm2 = 0; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7179 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7180 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 7181 | BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7182 | |
| 7183 | /* Find listening sockets and check their link_mode */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7184 | read_lock(&chan_list_lock); |
| 7185 | list_for_each_entry(c, &chan_list, global_l) { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 7186 | if (c->state != BT_LISTEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7187 | continue; |
| 7188 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 7189 | if (!bacmp(&c->src, &hdev->bdaddr)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7190 | lm1 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 7191 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7192 | lm1 |= HCI_LM_MASTER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7193 | exact++; |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 7194 | } else if (!bacmp(&c->src, BDADDR_ANY)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7195 | lm2 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 7196 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7197 | lm2 |= HCI_LM_MASTER; |
| 7198 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7199 | } |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7200 | read_unlock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7201 | |
| 7202 | return exact ? lm1 : lm2; |
| 7203 | } |
| 7204 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7205 | /* Find the next fixed channel in BT_LISTEN state, continue iteration |
| 7206 | * from an existing channel in the list or from the beginning of the |
| 7207 | * global list (by passing NULL as first parameter). |
| 7208 | */ |
| 7209 | static struct l2cap_chan *l2cap_global_fixed_chan(struct l2cap_chan *c, |
Johan Hedberg | 327a719 | 2015-01-15 13:06:45 +0200 | [diff] [blame] | 7210 | struct hci_conn *hcon) |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7211 | { |
Johan Hedberg | 327a719 | 2015-01-15 13:06:45 +0200 | [diff] [blame] | 7212 | u8 src_type = bdaddr_src_type(hcon); |
| 7213 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7214 | read_lock(&chan_list_lock); |
| 7215 | |
| 7216 | if (c) |
| 7217 | c = list_next_entry(c, global_l); |
| 7218 | else |
| 7219 | c = list_entry(chan_list.next, typeof(*c), global_l); |
| 7220 | |
| 7221 | list_for_each_entry_from(c, &chan_list, global_l) { |
| 7222 | if (c->chan_type != L2CAP_CHAN_FIXED) |
| 7223 | continue; |
| 7224 | if (c->state != BT_LISTEN) |
| 7225 | continue; |
Johan Hedberg | 327a719 | 2015-01-15 13:06:45 +0200 | [diff] [blame] | 7226 | if (bacmp(&c->src, &hcon->src) && bacmp(&c->src, BDADDR_ANY)) |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7227 | continue; |
Johan Hedberg | 327a719 | 2015-01-15 13:06:45 +0200 | [diff] [blame] | 7228 | if (src_type != c->src_type) |
Johan Hedberg | 54a1b62 | 2014-08-07 22:56:48 +0300 | [diff] [blame] | 7229 | continue; |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7230 | |
| 7231 | l2cap_chan_hold(c); |
| 7232 | read_unlock(&chan_list_lock); |
| 7233 | return c; |
| 7234 | } |
| 7235 | |
| 7236 | read_unlock(&chan_list_lock); |
| 7237 | |
| 7238 | return NULL; |
| 7239 | } |
| 7240 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 7241 | void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7242 | { |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7243 | struct hci_dev *hdev = hcon->hdev; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7244 | struct l2cap_conn *conn; |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7245 | struct l2cap_chan *pchan; |
| 7246 | u8 dst_type; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7247 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 7248 | BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7249 | |
Johan Hedberg | dc0f508 | 2014-08-07 22:56:46 +0300 | [diff] [blame] | 7250 | if (status) { |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 7251 | l2cap_conn_del(hcon, bt_to_errno(status)); |
Johan Hedberg | dc0f508 | 2014-08-07 22:56:46 +0300 | [diff] [blame] | 7252 | return; |
Andrei Emeltchenko | ba6fc31 | 2012-10-31 15:46:26 +0200 | [diff] [blame] | 7253 | } |
Johan Hedberg | dc0f508 | 2014-08-07 22:56:46 +0300 | [diff] [blame] | 7254 | |
| 7255 | conn = l2cap_conn_add(hcon); |
| 7256 | if (!conn) |
| 7257 | return; |
| 7258 | |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 7259 | dst_type = bdaddr_dst_type(hcon); |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7260 | |
| 7261 | /* If device is blocked, do not create channels for it */ |
| 7262 | if (hci_bdaddr_list_lookup(&hdev->blacklist, &hcon->dst, dst_type)) |
| 7263 | return; |
| 7264 | |
| 7265 | /* Find fixed channels and notify them of the new connection. We |
| 7266 | * use multiple individual lookups, continuing each time where |
| 7267 | * we left off, because the list lock would prevent calling the |
| 7268 | * potentially sleeping l2cap_chan_lock() function. |
| 7269 | */ |
Johan Hedberg | 327a719 | 2015-01-15 13:06:45 +0200 | [diff] [blame] | 7270 | pchan = l2cap_global_fixed_chan(NULL, hcon); |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7271 | while (pchan) { |
| 7272 | struct l2cap_chan *chan, *next; |
| 7273 | |
| 7274 | /* Client fixed channels should override server ones */ |
| 7275 | if (__l2cap_get_chan_by_dcid(conn, pchan->scid)) |
| 7276 | goto next; |
| 7277 | |
| 7278 | l2cap_chan_lock(pchan); |
| 7279 | chan = pchan->ops->new_connection(pchan); |
| 7280 | if (chan) { |
| 7281 | bacpy(&chan->src, &hcon->src); |
| 7282 | bacpy(&chan->dst, &hcon->dst); |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 7283 | chan->src_type = bdaddr_src_type(hcon); |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7284 | chan->dst_type = dst_type; |
| 7285 | |
| 7286 | __l2cap_chan_add(conn, chan); |
| 7287 | } |
| 7288 | |
| 7289 | l2cap_chan_unlock(pchan); |
| 7290 | next: |
Johan Hedberg | 327a719 | 2015-01-15 13:06:45 +0200 | [diff] [blame] | 7291 | next = l2cap_global_fixed_chan(pchan, hcon); |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7292 | l2cap_chan_put(pchan); |
| 7293 | pchan = next; |
| 7294 | } |
| 7295 | |
Johan Hedberg | dc0f508 | 2014-08-07 22:56:46 +0300 | [diff] [blame] | 7296 | l2cap_conn_ready(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7297 | } |
| 7298 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7299 | int l2cap_disconn_ind(struct hci_conn *hcon) |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 7300 | { |
| 7301 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 7302 | |
| 7303 | BT_DBG("hcon %p", hcon); |
| 7304 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7305 | if (!conn) |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 7306 | return HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 7307 | return conn->disc_reason; |
| 7308 | } |
| 7309 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 7310 | void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7311 | { |
| 7312 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 7313 | |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 7314 | l2cap_conn_del(hcon, bt_to_errno(reason)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7315 | } |
| 7316 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7317 | static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7318 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 7319 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
Marcel Holtmann | 255c760 | 2009-02-04 21:07:19 +0100 | [diff] [blame] | 7320 | return; |
| 7321 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7322 | if (encrypt == 0x00) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7323 | if (chan->sec_level == BT_SECURITY_MEDIUM) { |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 7324 | __set_chan_timer(chan, L2CAP_ENC_TIMEOUT); |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 7325 | } else if (chan->sec_level == BT_SECURITY_HIGH || |
| 7326 | chan->sec_level == BT_SECURITY_FIPS) |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 7327 | l2cap_chan_close(chan, ECONNREFUSED); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7328 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7329 | if (chan->sec_level == BT_SECURITY_MEDIUM) |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 7330 | __clear_chan_timer(chan); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7331 | } |
| 7332 | } |
| 7333 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7334 | int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7335 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 7336 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 7337 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7338 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7339 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7340 | return 0; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7341 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 7342 | BT_DBG("conn %p status 0x%2.2x encrypt %u", conn, status, encrypt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7343 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 7344 | mutex_lock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7345 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 7346 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7347 | l2cap_chan_lock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7348 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 7349 | BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, |
| 7350 | state_to_string(chan->state)); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 7351 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 7352 | if (chan->scid == L2CAP_CID_A2MP) { |
Andrei Emeltchenko | 78eb2f9 | 2012-07-19 17:03:47 +0300 | [diff] [blame] | 7353 | l2cap_chan_unlock(chan); |
| 7354 | continue; |
| 7355 | } |
| 7356 | |
Johan Hedberg | 191eb39 | 2014-08-07 22:56:45 +0300 | [diff] [blame] | 7357 | if (!status && encrypt) |
| 7358 | chan->sec_level = hcon->sec_level; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 7359 | |
Andrei Emeltchenko | 96eff46 | 2012-11-15 18:14:53 +0200 | [diff] [blame] | 7360 | if (!__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7361 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 7362 | continue; |
| 7363 | } |
| 7364 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 7365 | if (!status && (chan->state == BT_CONNECTED || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7366 | chan->state == BT_CONFIG)) { |
Marcel Holtmann | d97c899 | 2013-10-14 02:53:54 -0700 | [diff] [blame] | 7367 | chan->ops->resume(chan); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7368 | l2cap_check_encryption(chan, encrypt); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7369 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 9719f8a | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 7370 | continue; |
| 7371 | } |
| 7372 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 7373 | if (chan->state == BT_CONNECT) { |
Johan Hedberg | 6d3c15d | 2013-12-02 22:13:24 +0200 | [diff] [blame] | 7374 | if (!status) |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 7375 | l2cap_start_connection(chan); |
Johan Hedberg | 6d3c15d | 2013-12-02 22:13:24 +0200 | [diff] [blame] | 7376 | else |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 7377 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Johan Hedberg | fa37c1a | 2014-11-13 10:55:17 +0200 | [diff] [blame] | 7378 | } else if (chan->state == BT_CONNECT2 && |
| 7379 | chan->mode != L2CAP_MODE_LE_FLOWCTL) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7380 | struct l2cap_conn_rsp rsp; |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7381 | __u16 res, stat; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7382 | |
| 7383 | if (!status) { |
Marcel Holtmann | bdc2578 | 2013-10-14 02:45:34 -0700 | [diff] [blame] | 7384 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7385 | res = L2CAP_CR_PEND; |
| 7386 | stat = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 7387 | chan->ops->defer(chan); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7388 | } else { |
Gustavo Padovan | acdcabf | 2013-10-21 14:21:39 -0200 | [diff] [blame] | 7389 | l2cap_state_change(chan, BT_CONFIG); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7390 | res = L2CAP_CR_SUCCESS; |
| 7391 | stat = L2CAP_CS_NO_INFO; |
| 7392 | } |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7393 | } else { |
Gustavo Padovan | acdcabf | 2013-10-21 14:21:39 -0200 | [diff] [blame] | 7394 | l2cap_state_change(chan, BT_DISCONN); |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 7395 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7396 | res = L2CAP_CR_SEC_BLOCK; |
| 7397 | stat = L2CAP_CS_NO_INFO; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7398 | } |
| 7399 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 7400 | rsp.scid = cpu_to_le16(chan->dcid); |
| 7401 | rsp.dcid = cpu_to_le16(chan->scid); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7402 | rsp.result = cpu_to_le16(res); |
| 7403 | rsp.status = cpu_to_le16(stat); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 7404 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7405 | sizeof(rsp), &rsp); |
Mat Martineau | 2d36935 | 2012-05-23 14:59:30 -0700 | [diff] [blame] | 7406 | |
| 7407 | if (!test_bit(CONF_REQ_SENT, &chan->conf_state) && |
| 7408 | res == L2CAP_CR_SUCCESS) { |
| 7409 | char buf[128]; |
| 7410 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 7411 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 7412 | L2CAP_CONF_REQ, |
| 7413 | l2cap_build_conf_req(chan, buf), |
| 7414 | buf); |
| 7415 | chan->num_conf_req++; |
| 7416 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7417 | } |
| 7418 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7419 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7420 | } |
| 7421 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 7422 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7424 | return 0; |
| 7425 | } |
| 7426 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7427 | int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7428 | { |
| 7429 | struct l2cap_conn *conn = hcon->l2cap_data; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7430 | struct l2cap_hdr *hdr; |
| 7431 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7432 | |
Andrei Emeltchenko | 1d13a25 | 2012-10-15 11:58:41 +0300 | [diff] [blame] | 7433 | /* For AMP controller do not create l2cap conn */ |
| 7434 | if (!conn && hcon->hdev->dev_type != HCI_BREDR) |
| 7435 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7436 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 7437 | if (!conn) |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 7438 | conn = l2cap_conn_add(hcon); |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 7439 | |
| 7440 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7441 | goto drop; |
| 7442 | |
| 7443 | BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); |
| 7444 | |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7445 | switch (flags) { |
| 7446 | case ACL_START: |
| 7447 | case ACL_START_NO_FLUSH: |
| 7448 | case ACL_COMPLETE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7449 | if (conn->rx_len) { |
| 7450 | BT_ERR("Unexpected start frame (len %d)", skb->len); |
| 7451 | kfree_skb(conn->rx_skb); |
| 7452 | conn->rx_skb = NULL; |
| 7453 | conn->rx_len = 0; |
| 7454 | l2cap_conn_unreliable(conn, ECOMM); |
| 7455 | } |
| 7456 | |
Andrei Emeltchenko | aae7fe2 | 2010-09-15 14:28:43 +0300 | [diff] [blame] | 7457 | /* Start fragment always begin with Basic L2CAP header */ |
| 7458 | if (skb->len < L2CAP_HDR_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7459 | BT_ERR("Frame is too short (len %d)", skb->len); |
| 7460 | l2cap_conn_unreliable(conn, ECOMM); |
| 7461 | goto drop; |
| 7462 | } |
| 7463 | |
| 7464 | hdr = (struct l2cap_hdr *) skb->data; |
| 7465 | len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; |
| 7466 | |
| 7467 | if (len == skb->len) { |
| 7468 | /* Complete frame received */ |
| 7469 | l2cap_recv_frame(conn, skb); |
| 7470 | return 0; |
| 7471 | } |
| 7472 | |
| 7473 | BT_DBG("Start: total len %d, frag len %d", len, skb->len); |
| 7474 | |
| 7475 | if (skb->len > len) { |
| 7476 | BT_ERR("Frame is too long (len %d, expected len %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7477 | skb->len, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7478 | l2cap_conn_unreliable(conn, ECOMM); |
| 7479 | goto drop; |
| 7480 | } |
| 7481 | |
| 7482 | /* Allocate skb for the complete frame (with header) */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 7483 | conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 7484 | if (!conn->rx_skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7485 | goto drop; |
| 7486 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 7487 | skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7488 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7489 | conn->rx_len = len - skb->len; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7490 | break; |
| 7491 | |
| 7492 | case ACL_CONT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7493 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
| 7494 | |
| 7495 | if (!conn->rx_len) { |
| 7496 | BT_ERR("Unexpected continuation frame (len %d)", skb->len); |
| 7497 | l2cap_conn_unreliable(conn, ECOMM); |
| 7498 | goto drop; |
| 7499 | } |
| 7500 | |
| 7501 | if (skb->len > conn->rx_len) { |
| 7502 | BT_ERR("Fragment is too long (len %d, expected %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7503 | skb->len, conn->rx_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7504 | kfree_skb(conn->rx_skb); |
| 7505 | conn->rx_skb = NULL; |
| 7506 | conn->rx_len = 0; |
| 7507 | l2cap_conn_unreliable(conn, ECOMM); |
| 7508 | goto drop; |
| 7509 | } |
| 7510 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 7511 | skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7512 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7513 | conn->rx_len -= skb->len; |
| 7514 | |
| 7515 | if (!conn->rx_len) { |
Johan Hedberg | c4e5baf | 2013-10-10 13:33:37 +0200 | [diff] [blame] | 7516 | /* Complete frame received. l2cap_recv_frame |
| 7517 | * takes ownership of the skb so set the global |
| 7518 | * rx_skb pointer to NULL first. |
| 7519 | */ |
| 7520 | struct sk_buff *rx_skb = conn->rx_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7521 | conn->rx_skb = NULL; |
Johan Hedberg | c4e5baf | 2013-10-10 13:33:37 +0200 | [diff] [blame] | 7522 | l2cap_recv_frame(conn, rx_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7523 | } |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7524 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7525 | } |
| 7526 | |
| 7527 | drop: |
| 7528 | kfree_skb(skb); |
| 7529 | return 0; |
| 7530 | } |
| 7531 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7532 | static int l2cap_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7533 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7534 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7535 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 7536 | read_lock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7537 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7538 | list_for_each_entry(c, &chan_list, global_l) { |
Marcel Holtmann | eeb5a06 | 2015-01-14 13:44:21 -0800 | [diff] [blame] | 7539 | seq_printf(f, "%pMR (%u) %pMR (%u) %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", |
| 7540 | &c->src, c->src_type, &c->dst, c->dst_type, |
Andrei Emeltchenko | fcb7333 | 2012-09-25 12:49:44 +0300 | [diff] [blame] | 7541 | c->state, __le16_to_cpu(c->psm), |
| 7542 | c->scid, c->dcid, c->imtu, c->omtu, |
| 7543 | c->sec_level, c->mode); |
Andrei Emeltchenko | 61e1b4b | 2012-01-19 11:19:50 +0200 | [diff] [blame] | 7544 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7545 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 7546 | read_unlock(&chan_list_lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 7547 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7548 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7549 | } |
| 7550 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7551 | static int l2cap_debugfs_open(struct inode *inode, struct file *file) |
| 7552 | { |
| 7553 | return single_open(file, l2cap_debugfs_show, inode->i_private); |
| 7554 | } |
| 7555 | |
| 7556 | static const struct file_operations l2cap_debugfs_fops = { |
| 7557 | .open = l2cap_debugfs_open, |
| 7558 | .read = seq_read, |
| 7559 | .llseek = seq_lseek, |
| 7560 | .release = single_release, |
| 7561 | }; |
| 7562 | |
| 7563 | static struct dentry *l2cap_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7564 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 7565 | int __init l2cap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7566 | { |
| 7567 | int err; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 7568 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 7569 | err = l2cap_init_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7570 | if (err < 0) |
| 7571 | return err; |
| 7572 | |
Marcel Holtmann | 1120e4b | 2013-10-17 17:24:16 -0700 | [diff] [blame] | 7573 | if (IS_ERR_OR_NULL(bt_debugfs)) |
| 7574 | return 0; |
| 7575 | |
| 7576 | l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs, |
| 7577 | NULL, &l2cap_debugfs_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7578 | |
Samuel Ortiz | 40b9397 | 2014-05-14 17:53:35 +0200 | [diff] [blame] | 7579 | debugfs_create_u16("l2cap_le_max_credits", 0644, bt_debugfs, |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 7580 | &le_max_credits); |
Samuel Ortiz | 40b9397 | 2014-05-14 17:53:35 +0200 | [diff] [blame] | 7581 | debugfs_create_u16("l2cap_le_default_mps", 0644, bt_debugfs, |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 7582 | &le_default_mps); |
| 7583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7584 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7585 | } |
| 7586 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 7587 | void l2cap_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7588 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7589 | debugfs_remove(l2cap_debugfs); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 7590 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7591 | } |
| 7592 | |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 7593 | module_param(disable_ertm, bool, 0644); |
| 7594 | MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); |