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> |
Daniel Borkmann | dbb5088 | 2016-07-27 11:40:14 -0700 | [diff] [blame] | 35 | #include <linux/filter.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | #include <net/bluetooth/bluetooth.h> |
| 38 | #include <net/bluetooth/hci_core.h> |
| 39 | #include <net/bluetooth/l2cap.h> |
Marcel Holtmann | 7ef9fbf | 2013-10-10 14:54:14 -0700 | [diff] [blame] | 40 | |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 41 | #include "smp.h" |
Marcel Holtmann | 7024728 | 2013-10-10 14:54:15 -0700 | [diff] [blame] | 42 | #include "a2mp.h" |
Marcel Holtmann | 7ef9fbf | 2013-10-10 14:54:14 -0700 | [diff] [blame] | 43 | #include "amp.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Johan Hedberg | 0f1bfe4 | 2014-01-27 15:11:35 -0800 | [diff] [blame] | 45 | #define LE_FLOWCTL_MAX_CREDITS 65535 |
| 46 | |
Mat Martineau | d1de6d4 | 2012-05-17 20:53:55 -0700 | [diff] [blame] | 47 | bool disable_ertm; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 48 | |
Marcel Holtmann | 547d103 | 2013-10-12 08:18:19 -0700 | [diff] [blame] | 49 | static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN | L2CAP_FEAT_UCD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 51 | static LIST_HEAD(chan_list); |
| 52 | static DEFINE_RWLOCK(chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 54 | static u16 le_max_credits = L2CAP_LE_MAX_CREDITS; |
| 55 | static u16 le_default_mps = L2CAP_LE_DEFAULT_MPS; |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 58 | u8 code, u8 ident, u16 dlen, void *data); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 59 | 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] | 60 | void *data); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 61 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 62 | static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 64 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 65 | struct sk_buff_head *skbs, u8 event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 66 | |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 67 | static inline u8 bdaddr_type(u8 link_type, u8 bdaddr_type) |
Marcel Holtmann | 4f1654e | 2013-10-13 08:50:41 -0700 | [diff] [blame] | 68 | { |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 69 | if (link_type == LE_LINK) { |
| 70 | if (bdaddr_type == ADDR_LE_DEV_PUBLIC) |
Marcel Holtmann | 4f1654e | 2013-10-13 08:50:41 -0700 | [diff] [blame] | 71 | return BDADDR_LE_PUBLIC; |
| 72 | else |
| 73 | return BDADDR_LE_RANDOM; |
| 74 | } |
| 75 | |
| 76 | return BDADDR_BREDR; |
| 77 | } |
| 78 | |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 79 | static inline u8 bdaddr_src_type(struct hci_conn *hcon) |
| 80 | { |
| 81 | return bdaddr_type(hcon->type, hcon->src_type); |
| 82 | } |
| 83 | |
| 84 | static inline u8 bdaddr_dst_type(struct hci_conn *hcon) |
| 85 | { |
| 86 | return bdaddr_type(hcon->type, hcon->dst_type); |
| 87 | } |
| 88 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 89 | /* ---- L2CAP channels ---- */ |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 90 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 91 | static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, |
| 92 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 93 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 94 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 95 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 96 | list_for_each_entry(c, &conn->chan_l, list) { |
| 97 | if (c->dcid == cid) |
| 98 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 99 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 100 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 103 | static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, |
| 104 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 105 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 106 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 107 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 108 | list_for_each_entry(c, &conn->chan_l, list) { |
| 109 | if (c->scid == cid) |
| 110 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 111 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 112 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* Find channel with given SCID. |
Mat Martineau | ef191ad | 2012-05-02 09:42:00 -0700 | [diff] [blame] | 116 | * Returns locked channel. */ |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 117 | static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, |
| 118 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 119 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 120 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 121 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 122 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 123 | c = __l2cap_get_chan_by_scid(conn, cid); |
Mat Martineau | ef191ad | 2012-05-02 09:42:00 -0700 | [diff] [blame] | 124 | if (c) |
| 125 | l2cap_chan_lock(c); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 126 | mutex_unlock(&conn->chan_lock); |
| 127 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 128 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Mat Martineau | b1a130b | 2012-10-23 15:24:09 -0700 | [diff] [blame] | 131 | /* Find channel with given DCID. |
| 132 | * Returns locked channel. |
| 133 | */ |
| 134 | static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn, |
| 135 | u16 cid) |
| 136 | { |
| 137 | struct l2cap_chan *c; |
| 138 | |
| 139 | mutex_lock(&conn->chan_lock); |
| 140 | c = __l2cap_get_chan_by_dcid(conn, cid); |
| 141 | if (c) |
| 142 | l2cap_chan_lock(c); |
| 143 | mutex_unlock(&conn->chan_lock); |
| 144 | |
| 145 | return c; |
| 146 | } |
| 147 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 148 | static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, |
| 149 | u8 ident) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 150 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 151 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 152 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 153 | list_for_each_entry(c, &conn->chan_l, list) { |
| 154 | if (c->ident == ident) |
| 155 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 156 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 157 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 158 | } |
| 159 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 160 | static struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, |
| 161 | u8 ident) |
| 162 | { |
| 163 | struct l2cap_chan *c; |
| 164 | |
| 165 | mutex_lock(&conn->chan_lock); |
| 166 | c = __l2cap_get_chan_by_ident(conn, ident); |
| 167 | if (c) |
| 168 | l2cap_chan_lock(c); |
| 169 | mutex_unlock(&conn->chan_lock); |
| 170 | |
| 171 | return c; |
| 172 | } |
| 173 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 174 | 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] | 175 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 176 | struct l2cap_chan *c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 177 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 178 | list_for_each_entry(c, &chan_list, global_l) { |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 179 | if (c->sport == psm && !bacmp(&c->src, src)) |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 180 | return c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 181 | } |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 182 | return NULL; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) |
| 186 | { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 187 | int err; |
| 188 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 189 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 190 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 191 | if (psm && __l2cap_global_chan_by_addr(psm, src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 192 | err = -EADDRINUSE; |
| 193 | goto done; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 194 | } |
| 195 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 196 | if (psm) { |
| 197 | chan->psm = psm; |
| 198 | chan->sport = psm; |
| 199 | err = 0; |
| 200 | } else { |
Johan Hedberg | 92594a5 | 2016-01-26 17:19:10 -0500 | [diff] [blame] | 201 | u16 p, start, end, incr; |
| 202 | |
| 203 | if (chan->src_type == BDADDR_BREDR) { |
| 204 | start = L2CAP_PSM_DYN_START; |
| 205 | end = L2CAP_PSM_AUTO_END; |
| 206 | incr = 2; |
| 207 | } else { |
| 208 | start = L2CAP_PSM_LE_DYN_START; |
| 209 | end = L2CAP_PSM_LE_DYN_END; |
| 210 | incr = 1; |
| 211 | } |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 212 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 213 | err = -EINVAL; |
Johan Hedberg | 92594a5 | 2016-01-26 17:19:10 -0500 | [diff] [blame] | 214 | for (p = start; p <= end; p += incr) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 215 | if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 216 | chan->psm = cpu_to_le16(p); |
| 217 | chan->sport = cpu_to_le16(p); |
| 218 | err = 0; |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | done: |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 224 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 225 | return err; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 226 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 227 | EXPORT_SYMBOL_GPL(l2cap_add_psm); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 228 | |
| 229 | int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) |
| 230 | { |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 231 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 232 | |
Johan Hedberg | 1482430 | 2014-08-07 22:56:50 +0300 | [diff] [blame] | 233 | /* Override the defaults (which are for conn-oriented) */ |
| 234 | chan->omtu = L2CAP_DEFAULT_MTU; |
| 235 | chan->chan_type = L2CAP_CHAN_FIXED; |
| 236 | |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 237 | chan->scid = scid; |
| 238 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 239 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 244 | static u16 l2cap_alloc_cid(struct l2cap_conn *conn) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 245 | { |
Johan Hedberg | e77af75 | 2013-10-08 10:31:00 +0200 | [diff] [blame] | 246 | u16 cid, dyn_end; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 247 | |
Johan Hedberg | e77af75 | 2013-10-08 10:31:00 +0200 | [diff] [blame] | 248 | if (conn->hcon->type == LE_LINK) |
| 249 | dyn_end = L2CAP_CID_LE_DYN_END; |
| 250 | else |
| 251 | dyn_end = L2CAP_CID_DYN_END; |
| 252 | |
Johan Hedberg | ab0c127 | 2015-11-02 14:39:16 +0200 | [diff] [blame] | 253 | for (cid = L2CAP_CID_DYN_START; cid <= dyn_end; cid++) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 254 | if (!__l2cap_get_chan_by_scid(conn, cid)) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 255 | return cid; |
| 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 261 | static void l2cap_state_change(struct l2cap_chan *chan, int state) |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 262 | { |
Andrei Emeltchenko | 42d2d87 | 2012-02-17 11:40:57 +0200 | [diff] [blame] | 263 | BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 264 | state_to_string(state)); |
Gustavo F. Padovan | badaaa0 | 2011-11-23 20:11:46 -0200 | [diff] [blame] | 265 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 266 | chan->state = state; |
Gustavo Padovan | 53f5212 | 2013-10-15 19:24:45 -0300 | [diff] [blame] | 267 | chan->ops->state_change(chan, state, 0); |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 268 | } |
| 269 | |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 270 | static inline void l2cap_state_change_and_error(struct l2cap_chan *chan, |
| 271 | int state, int err) |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 272 | { |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 273 | chan->state = state; |
Gustavo Padovan | 53f5212 | 2013-10-15 19:24:45 -0300 | [diff] [blame] | 274 | chan->ops->state_change(chan, chan->state, err); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err) |
| 278 | { |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 279 | chan->ops->state_change(chan, chan->state, err); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 282 | static void __set_retrans_timer(struct l2cap_chan *chan) |
| 283 | { |
| 284 | if (!delayed_work_pending(&chan->monitor_timer) && |
| 285 | chan->retrans_timeout) { |
| 286 | l2cap_set_timer(chan, &chan->retrans_timer, |
| 287 | msecs_to_jiffies(chan->retrans_timeout)); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static void __set_monitor_timer(struct l2cap_chan *chan) |
| 292 | { |
| 293 | __clear_retrans_timer(chan); |
| 294 | if (chan->monitor_timeout) { |
| 295 | l2cap_set_timer(chan, &chan->monitor_timer, |
| 296 | msecs_to_jiffies(chan->monitor_timeout)); |
| 297 | } |
| 298 | } |
| 299 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 300 | static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head, |
| 301 | u16 seq) |
| 302 | { |
| 303 | struct sk_buff *skb; |
| 304 | |
| 305 | skb_queue_walk(head, skb) { |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 306 | if (bt_cb(skb)->l2cap.txseq == seq) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 307 | return skb; |
| 308 | } |
| 309 | |
| 310 | return NULL; |
| 311 | } |
| 312 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 313 | /* ---- L2CAP sequence number lists ---- */ |
| 314 | |
| 315 | /* For ERTM, ordered lists of sequence numbers must be tracked for |
| 316 | * SREJ requests that are received and for frames that are to be |
| 317 | * retransmitted. These seq_list functions implement a singly-linked |
| 318 | * list in an array, where membership in the list can also be checked |
| 319 | * in constant time. Items can also be added to the tail of the list |
| 320 | * and removed from the head in constant time, without further memory |
| 321 | * allocs or frees. |
| 322 | */ |
| 323 | |
| 324 | static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size) |
| 325 | { |
| 326 | size_t alloc_size, i; |
| 327 | |
| 328 | /* Allocated size is a power of 2 to map sequence numbers |
| 329 | * (which may be up to 14 bits) in to a smaller array that is |
| 330 | * sized for the negotiated ERTM transmit windows. |
| 331 | */ |
| 332 | alloc_size = roundup_pow_of_two(size); |
| 333 | |
| 334 | seq_list->list = kmalloc(sizeof(u16) * alloc_size, GFP_KERNEL); |
| 335 | if (!seq_list->list) |
| 336 | return -ENOMEM; |
| 337 | |
| 338 | seq_list->mask = alloc_size - 1; |
| 339 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 340 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
| 341 | for (i = 0; i < alloc_size; i++) |
| 342 | seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list) |
| 348 | { |
| 349 | kfree(seq_list->list); |
| 350 | } |
| 351 | |
| 352 | static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list, |
| 353 | u16 seq) |
| 354 | { |
| 355 | /* Constant-time check for list membership */ |
| 356 | return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR; |
| 357 | } |
| 358 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 359 | static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list) |
| 360 | { |
Johan Hedberg | 03a0c5d | 2014-01-18 21:32:59 +0200 | [diff] [blame] | 361 | u16 seq = seq_list->head; |
| 362 | u16 mask = seq_list->mask; |
| 363 | |
| 364 | seq_list->head = seq_list->list[seq & mask]; |
| 365 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR; |
| 366 | |
| 367 | if (seq_list->head == L2CAP_SEQ_LIST_TAIL) { |
| 368 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 369 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
| 370 | } |
| 371 | |
| 372 | return seq; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list) |
| 376 | { |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 377 | u16 i; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 378 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 379 | if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) |
| 380 | return; |
| 381 | |
| 382 | for (i = 0; i <= seq_list->mask; i++) |
| 383 | seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; |
| 384 | |
| 385 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 386 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq) |
| 390 | { |
| 391 | u16 mask = seq_list->mask; |
| 392 | |
| 393 | /* All appends happen in constant time */ |
| 394 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 395 | if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR) |
| 396 | return; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 397 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 398 | if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR) |
| 399 | seq_list->head = seq; |
| 400 | else |
| 401 | seq_list->list[seq_list->tail & mask] = seq; |
| 402 | |
| 403 | seq_list->tail = seq; |
| 404 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 407 | static void l2cap_chan_timeout(struct work_struct *work) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 408 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 409 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 410 | chan_timer.work); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 411 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 412 | int reason; |
| 413 | |
Andrei Emeltchenko | e05dcc3 | 2012-02-17 11:40:56 +0200 | [diff] [blame] | 414 | 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] | 415 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 416 | mutex_lock(&conn->chan_lock); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 417 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 418 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 419 | if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 420 | reason = ECONNREFUSED; |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 421 | else if (chan->state == BT_CONNECT && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 422 | chan->sec_level != BT_SECURITY_SDP) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 423 | reason = ECONNREFUSED; |
| 424 | else |
| 425 | reason = ETIMEDOUT; |
| 426 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 427 | l2cap_chan_close(chan, reason); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 428 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 429 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 430 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 431 | chan->ops->close(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 432 | mutex_unlock(&conn->chan_lock); |
| 433 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 434 | l2cap_chan_put(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 435 | } |
| 436 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 437 | struct l2cap_chan *l2cap_chan_create(void) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 438 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 439 | struct l2cap_chan *chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 440 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 441 | chan = kzalloc(sizeof(*chan), GFP_ATOMIC); |
| 442 | if (!chan) |
| 443 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 444 | |
Andrei Emeltchenko | c03b355 | 2012-02-21 12:54:56 +0200 | [diff] [blame] | 445 | mutex_init(&chan->lock); |
| 446 | |
Johan Hedberg | ff71411 | 2014-11-13 09:46:04 +0200 | [diff] [blame] | 447 | /* Set default lock nesting level */ |
| 448 | atomic_set(&chan->nesting, L2CAP_NESTING_NORMAL); |
| 449 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 450 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 451 | list_add(&chan->global_l, &chan_list); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 452 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 453 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 454 | INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 455 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 456 | chan->state = BT_OPEN; |
| 457 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 458 | kref_init(&chan->kref); |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 459 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 460 | /* This flag is cleared in l2cap_chan_ready() */ |
| 461 | set_bit(CONF_NOT_COMPLETE, &chan->conf_state); |
| 462 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 463 | BT_DBG("chan %p", chan); |
Szymon Janc | abc545b | 2011-11-03 16:05:44 +0100 | [diff] [blame] | 464 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 465 | return chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 466 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 467 | EXPORT_SYMBOL_GPL(l2cap_chan_create); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 468 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 469 | static void l2cap_chan_destroy(struct kref *kref) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 470 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 471 | struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref); |
| 472 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 473 | BT_DBG("chan %p", chan); |
| 474 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 475 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 476 | list_del(&chan->global_l); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 477 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 478 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 479 | kfree(chan); |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 480 | } |
| 481 | |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 482 | void l2cap_chan_hold(struct l2cap_chan *c) |
| 483 | { |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 484 | BT_DBG("chan %p orig refcnt %d", c, kref_read(&c->kref)); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 485 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 486 | kref_get(&c->kref); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void l2cap_chan_put(struct l2cap_chan *c) |
| 490 | { |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 491 | BT_DBG("chan %p orig refcnt %d", c, kref_read(&c->kref)); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 492 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 493 | kref_put(&c->kref, l2cap_chan_destroy); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 494 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 495 | EXPORT_SYMBOL_GPL(l2cap_chan_put); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 496 | |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 497 | void l2cap_chan_set_defaults(struct l2cap_chan *chan) |
| 498 | { |
| 499 | chan->fcs = L2CAP_FCS_CRC16; |
| 500 | chan->max_tx = L2CAP_DEFAULT_MAX_TX; |
| 501 | chan->tx_win = L2CAP_DEFAULT_TX_WINDOW; |
| 502 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
Jukka Rissanen | 6a5e816 | 2014-05-28 14:43:04 +0300 | [diff] [blame] | 503 | chan->remote_max_tx = chan->max_tx; |
| 504 | chan->remote_tx_win = chan->tx_win; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 505 | chan->ack_win = L2CAP_DEFAULT_TX_WINDOW; |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 506 | chan->sec_level = BT_SECURITY_LOW; |
Jukka Rissanen | 6a5e816 | 2014-05-28 14:43:04 +0300 | [diff] [blame] | 507 | chan->flush_to = L2CAP_DEFAULT_FLUSH_TO; |
| 508 | chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO; |
| 509 | chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO; |
| 510 | chan->conf_state = 0; |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 511 | |
| 512 | set_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 513 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 514 | EXPORT_SYMBOL_GPL(l2cap_chan_set_defaults); |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 515 | |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 516 | static void l2cap_le_flowctl_init(struct l2cap_chan *chan) |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 517 | { |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 518 | chan->sdu = NULL; |
| 519 | chan->sdu_last_frag = NULL; |
| 520 | chan->sdu_len = 0; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 521 | chan->tx_credits = 0; |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 522 | chan->rx_credits = le_max_credits; |
Johan Hedberg | d1d7941 | 2014-01-27 15:11:33 -0800 | [diff] [blame] | 523 | chan->mps = min_t(u16, chan->imtu, le_default_mps); |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 524 | |
| 525 | skb_queue_head_init(&chan->tx_q); |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 526 | } |
| 527 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 528 | void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 529 | { |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 530 | 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] | 531 | __le16_to_cpu(chan->psm), chan->dcid); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 532 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 533 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 534 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 535 | chan->conn = conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 536 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 537 | switch (chan->chan_type) { |
| 538 | case L2CAP_CHAN_CONN_ORIENTED: |
Johan Hedberg | 21626e6 | 2014-01-24 10:35:41 +0200 | [diff] [blame] | 539 | /* Alloc CID for connection-oriented socket */ |
| 540 | chan->scid = l2cap_alloc_cid(conn); |
| 541 | if (conn->hcon->type == ACL_LINK) |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 542 | chan->omtu = L2CAP_DEFAULT_MTU; |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 543 | break; |
| 544 | |
| 545 | case L2CAP_CHAN_CONN_LESS: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 546 | /* Connectionless socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 547 | chan->scid = L2CAP_CID_CONN_LESS; |
| 548 | chan->dcid = L2CAP_CID_CONN_LESS; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 549 | chan->omtu = L2CAP_DEFAULT_MTU; |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 550 | break; |
| 551 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 552 | case L2CAP_CHAN_FIXED: |
| 553 | /* Caller will set CID and CID specific MTU values */ |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 554 | break; |
| 555 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 556 | default: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 557 | /* Raw socket can send/recv signalling messages only */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 558 | chan->scid = L2CAP_CID_SIGNALING; |
| 559 | chan->dcid = L2CAP_CID_SIGNALING; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 560 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 561 | } |
| 562 | |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 563 | chan->local_id = L2CAP_BESTEFFORT_ID; |
| 564 | chan->local_stype = L2CAP_SERV_BESTEFFORT; |
| 565 | chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; |
| 566 | chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; |
| 567 | chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 568 | chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO; |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 569 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 570 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 571 | |
Johan Hedberg | c16900c | 2014-08-15 21:17:06 +0300 | [diff] [blame] | 572 | /* Only keep a reference for fixed channels if they requested it */ |
| 573 | if (chan->chan_type != L2CAP_CHAN_FIXED || |
| 574 | test_bit(FLAG_HOLD_HCI_CONN, &chan->flags)) |
| 575 | hci_conn_hold(conn->hcon); |
Johan Hedberg | 5ee9891 | 2013-04-29 19:35:43 +0300 | [diff] [blame] | 576 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 577 | list_add(&chan->list, &conn->chan_l); |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 580 | void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 581 | { |
| 582 | mutex_lock(&conn->chan_lock); |
| 583 | __l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 584 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 585 | } |
| 586 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 587 | void l2cap_chan_del(struct l2cap_chan *chan, int err) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 588 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 589 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 590 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 591 | __clear_chan_timer(chan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 592 | |
Johan Hedberg | 49d1174 | 2014-11-13 14:37:50 +0200 | [diff] [blame] | 593 | BT_DBG("chan %p, conn %p, err %d, state %s", chan, conn, err, |
| 594 | state_to_string(chan->state)); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 595 | |
Johan Hedberg | 72847ce | 2014-08-08 09:28:03 +0300 | [diff] [blame] | 596 | chan->ops->teardown(chan, err); |
| 597 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 598 | if (conn) { |
Andrei Emeltchenko | 56f6098 | 2012-10-15 11:58:44 +0300 | [diff] [blame] | 599 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 600 | /* Delete from channel list */ |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 601 | list_del(&chan->list); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 602 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 603 | l2cap_chan_put(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 604 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 605 | chan->conn = NULL; |
Andrei Emeltchenko | 3cabbfd | 2012-05-31 11:01:37 +0300 | [diff] [blame] | 606 | |
Johan Hedberg | c16900c | 2014-08-15 21:17:06 +0300 | [diff] [blame] | 607 | /* Reference was only held for non-fixed channels or |
| 608 | * fixed channels that explicitly requested it using the |
| 609 | * FLAG_HOLD_HCI_CONN flag. |
| 610 | */ |
| 611 | if (chan->chan_type != L2CAP_CHAN_FIXED || |
| 612 | test_bit(FLAG_HOLD_HCI_CONN, &chan->flags)) |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 613 | hci_conn_drop(conn->hcon); |
Andrei Emeltchenko | 56f6098 | 2012-10-15 11:58:44 +0300 | [diff] [blame] | 614 | |
| 615 | if (mgr && mgr->bredr_chan == chan) |
| 616 | mgr->bredr_chan = NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 617 | } |
| 618 | |
Andrei Emeltchenko | 419e08c | 2012-10-31 15:46:34 +0200 | [diff] [blame] | 619 | if (chan->hs_hchan) { |
| 620 | struct hci_chan *hs_hchan = chan->hs_hchan; |
| 621 | |
| 622 | BT_DBG("chan %p disconnect hs_hchan %p", chan, hs_hchan); |
| 623 | amp_disconnect_logical_link(hs_hchan); |
| 624 | } |
| 625 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 626 | if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 627 | return; |
Gustavo F. Padovan | 2ead70b | 2011-04-01 15:13:36 -0300 | [diff] [blame] | 628 | |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 629 | switch(chan->mode) { |
| 630 | case L2CAP_MODE_BASIC: |
| 631 | break; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 632 | |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 633 | case L2CAP_MODE_LE_FLOWCTL: |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 634 | skb_queue_purge(&chan->tx_q); |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 635 | break; |
| 636 | |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 637 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 638 | __clear_retrans_timer(chan); |
| 639 | __clear_monitor_timer(chan); |
| 640 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 641 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 642 | skb_queue_purge(&chan->srej_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 643 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 644 | l2cap_seq_list_free(&chan->srej_list); |
| 645 | l2cap_seq_list_free(&chan->retrans_list); |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 646 | |
| 647 | /* fall through */ |
| 648 | |
| 649 | case L2CAP_MODE_STREAMING: |
| 650 | skb_queue_purge(&chan->tx_q); |
| 651 | break; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 652 | } |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 653 | |
| 654 | return; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 655 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 656 | EXPORT_SYMBOL_GPL(l2cap_chan_del); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 657 | |
Johan Hedberg | f3d82d0 | 2014-09-05 22:19:50 +0300 | [diff] [blame] | 658 | static void l2cap_conn_update_id_addr(struct work_struct *work) |
Johan Hedberg | 387a33e | 2014-02-18 21:41:33 +0200 | [diff] [blame] | 659 | { |
Johan Hedberg | f3d82d0 | 2014-09-05 22:19:50 +0300 | [diff] [blame] | 660 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
| 661 | id_addr_update_work); |
| 662 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 387a33e | 2014-02-18 21:41:33 +0200 | [diff] [blame] | 663 | struct l2cap_chan *chan; |
| 664 | |
| 665 | mutex_lock(&conn->chan_lock); |
| 666 | |
| 667 | list_for_each_entry(chan, &conn->chan_l, list) { |
| 668 | l2cap_chan_lock(chan); |
| 669 | bacpy(&chan->dst, &hcon->dst); |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 670 | chan->dst_type = bdaddr_dst_type(hcon); |
Johan Hedberg | 387a33e | 2014-02-18 21:41:33 +0200 | [diff] [blame] | 671 | l2cap_chan_unlock(chan); |
| 672 | } |
| 673 | |
| 674 | mutex_unlock(&conn->chan_lock); |
| 675 | } |
| 676 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 677 | static void l2cap_chan_le_connect_reject(struct l2cap_chan *chan) |
| 678 | { |
| 679 | struct l2cap_conn *conn = chan->conn; |
| 680 | struct l2cap_le_conn_rsp rsp; |
| 681 | u16 result; |
| 682 | |
| 683 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) |
| 684 | result = L2CAP_CR_AUTHORIZATION; |
| 685 | else |
| 686 | result = L2CAP_CR_BAD_PSM; |
| 687 | |
| 688 | l2cap_state_change(chan, BT_DISCONN); |
| 689 | |
| 690 | rsp.dcid = cpu_to_le16(chan->scid); |
| 691 | rsp.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 692 | rsp.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 693 | rsp.credits = cpu_to_le16(chan->rx_credits); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 694 | rsp.result = cpu_to_le16(result); |
| 695 | |
| 696 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), |
| 697 | &rsp); |
| 698 | } |
| 699 | |
Johan Hedberg | 791d60f | 2013-05-14 22:24:44 +0300 | [diff] [blame] | 700 | static void l2cap_chan_connect_reject(struct l2cap_chan *chan) |
| 701 | { |
| 702 | struct l2cap_conn *conn = chan->conn; |
| 703 | struct l2cap_conn_rsp rsp; |
| 704 | u16 result; |
| 705 | |
| 706 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) |
| 707 | result = L2CAP_CR_SEC_BLOCK; |
| 708 | else |
| 709 | result = L2CAP_CR_BAD_PSM; |
| 710 | |
| 711 | l2cap_state_change(chan, BT_DISCONN); |
| 712 | |
| 713 | rsp.scid = cpu_to_le16(chan->dcid); |
| 714 | rsp.dcid = cpu_to_le16(chan->scid); |
| 715 | rsp.result = cpu_to_le16(result); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 716 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Johan Hedberg | 791d60f | 2013-05-14 22:24:44 +0300 | [diff] [blame] | 717 | |
| 718 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
| 719 | } |
| 720 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 721 | void l2cap_chan_close(struct l2cap_chan *chan, int reason) |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 722 | { |
| 723 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 724 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 725 | 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] | 726 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 727 | switch (chan->state) { |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 728 | case BT_LISTEN: |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 729 | chan->ops->teardown(chan, 0); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 730 | break; |
| 731 | |
| 732 | case BT_CONNECTED: |
| 733 | case BT_CONFIG: |
Johan Hedberg | 7b25c9b | 2014-01-28 15:28:04 -0800 | [diff] [blame] | 734 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { |
Gustavo Padovan | 8d836d7 | 2013-10-15 19:24:47 -0300 | [diff] [blame] | 735 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 736 | l2cap_send_disconn_req(chan, reason); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 737 | } else |
| 738 | l2cap_chan_del(chan, reason); |
| 739 | break; |
| 740 | |
| 741 | case BT_CONNECT2: |
Johan Hedberg | 791d60f | 2013-05-14 22:24:44 +0300 | [diff] [blame] | 742 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { |
| 743 | if (conn->hcon->type == ACL_LINK) |
| 744 | l2cap_chan_connect_reject(chan); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 745 | else if (conn->hcon->type == LE_LINK) |
| 746 | l2cap_chan_le_connect_reject(chan); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | l2cap_chan_del(chan, reason); |
| 750 | break; |
| 751 | |
| 752 | case BT_CONNECT: |
| 753 | case BT_DISCONN: |
| 754 | l2cap_chan_del(chan, reason); |
| 755 | break; |
| 756 | |
| 757 | default: |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 758 | chan->ops->teardown(chan, 0); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 759 | break; |
| 760 | } |
| 761 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 762 | EXPORT_SYMBOL(l2cap_chan_close); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 763 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 764 | static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 765 | { |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 766 | switch (chan->chan_type) { |
| 767 | case L2CAP_CHAN_RAW: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 768 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 769 | case BT_SECURITY_HIGH: |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 770 | case BT_SECURITY_FIPS: |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 771 | return HCI_AT_DEDICATED_BONDING_MITM; |
| 772 | case BT_SECURITY_MEDIUM: |
| 773 | return HCI_AT_DEDICATED_BONDING; |
| 774 | default: |
| 775 | return HCI_AT_NO_BONDING; |
| 776 | } |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 777 | break; |
Marcel Holtmann | 3124b84 | 2013-10-12 07:19:32 -0700 | [diff] [blame] | 778 | case L2CAP_CHAN_CONN_LESS: |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 779 | if (chan->psm == cpu_to_le16(L2CAP_PSM_3DSP)) { |
Marcel Holtmann | 3124b84 | 2013-10-12 07:19:32 -0700 | [diff] [blame] | 780 | if (chan->sec_level == BT_SECURITY_LOW) |
| 781 | chan->sec_level = BT_SECURITY_SDP; |
| 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 | 3124b84 | 2013-10-12 07:19:32 -0700 | [diff] [blame] | 785 | return HCI_AT_NO_BONDING_MITM; |
| 786 | else |
| 787 | return HCI_AT_NO_BONDING; |
| 788 | break; |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 789 | case L2CAP_CHAN_CONN_ORIENTED: |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 790 | if (chan->psm == cpu_to_le16(L2CAP_PSM_SDP)) { |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 791 | if (chan->sec_level == BT_SECURITY_LOW) |
| 792 | chan->sec_level = BT_SECURITY_SDP; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 793 | |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 794 | if (chan->sec_level == BT_SECURITY_HIGH || |
| 795 | chan->sec_level == BT_SECURITY_FIPS) |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 796 | return HCI_AT_NO_BONDING_MITM; |
| 797 | else |
| 798 | return HCI_AT_NO_BONDING; |
| 799 | } |
| 800 | /* fall through */ |
| 801 | default: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 802 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 803 | case BT_SECURITY_HIGH: |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 804 | case BT_SECURITY_FIPS: |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 805 | return HCI_AT_GENERAL_BONDING_MITM; |
| 806 | case BT_SECURITY_MEDIUM: |
| 807 | return HCI_AT_GENERAL_BONDING; |
| 808 | default: |
| 809 | return HCI_AT_NO_BONDING; |
| 810 | } |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 811 | break; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 815 | /* Service level security */ |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 816 | int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 817 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 818 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 819 | __u8 auth_type; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 820 | |
Johan Hedberg | a17de2f | 2013-05-14 13:25:37 +0300 | [diff] [blame] | 821 | if (conn->hcon->type == LE_LINK) |
| 822 | return smp_conn_security(conn->hcon, chan->sec_level); |
| 823 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 824 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 825 | |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 826 | return hci_conn_security(conn->hcon, chan->sec_level, auth_type, |
| 827 | initiator); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 828 | } |
| 829 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 830 | static u8 l2cap_get_ident(struct l2cap_conn *conn) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 831 | { |
| 832 | u8 id; |
| 833 | |
| 834 | /* Get next available identificator. |
| 835 | * 1 - 128 are used by kernel. |
| 836 | * 129 - 199 are reserved. |
| 837 | * 200 - 254 are used by utilities like l2ping, etc. |
| 838 | */ |
| 839 | |
Marcel Holtmann | 5a54e7c | 2014-07-13 20:50:15 +0200 | [diff] [blame] | 840 | mutex_lock(&conn->ident_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 841 | |
| 842 | if (++conn->tx_ident > 128) |
| 843 | conn->tx_ident = 1; |
| 844 | |
| 845 | id = conn->tx_ident; |
| 846 | |
Marcel Holtmann | 5a54e7c | 2014-07-13 20:50:15 +0200 | [diff] [blame] | 847 | mutex_unlock(&conn->ident_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 848 | |
| 849 | return id; |
| 850 | } |
| 851 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 852 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, |
| 853 | void *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 854 | { |
| 855 | struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 856 | u8 flags; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 857 | |
| 858 | BT_DBG("code 0x%2.2x", code); |
| 859 | |
| 860 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 861 | return; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 862 | |
Steven Walter | f6af675 | 2014-11-19 09:41:17 -0500 | [diff] [blame] | 863 | /* Use NO_FLUSH if supported or we have an LE link (which does |
| 864 | * not support auto-flushing packets) */ |
| 865 | if (lmp_no_flush_capable(conn->hcon->hdev) || |
| 866 | conn->hcon->type == LE_LINK) |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 867 | flags = ACL_START_NO_FLUSH; |
| 868 | else |
| 869 | flags = ACL_START; |
| 870 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 871 | bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 872 | skb->priority = HCI_PRIO_MAX; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 873 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 874 | hci_send_acl(conn->hchan, skb, flags); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 875 | } |
| 876 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 877 | static bool __chan_is_moving(struct l2cap_chan *chan) |
| 878 | { |
| 879 | return chan->move_state != L2CAP_MOVE_STABLE && |
| 880 | chan->move_state != L2CAP_MOVE_WAIT_PREPARE; |
| 881 | } |
| 882 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 883 | static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) |
| 884 | { |
| 885 | struct hci_conn *hcon = chan->conn->hcon; |
| 886 | u16 flags; |
| 887 | |
| 888 | 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] | 889 | skb->priority); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 890 | |
Mat Martineau | d5f8a75 | 2012-10-23 15:24:18 -0700 | [diff] [blame] | 891 | if (chan->hs_hcon && !__chan_is_moving(chan)) { |
| 892 | if (chan->hs_hchan) |
| 893 | hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE); |
| 894 | else |
| 895 | kfree_skb(skb); |
| 896 | |
| 897 | return; |
| 898 | } |
| 899 | |
Steven Walter | f6af675 | 2014-11-19 09:41:17 -0500 | [diff] [blame] | 900 | /* Use NO_FLUSH for LE links (where this is the only option) or |
| 901 | * if the BR/EDR link supports it and flushing has not been |
| 902 | * explicitly requested (through FLAG_FLUSHABLE). |
| 903 | */ |
| 904 | if (hcon->type == LE_LINK || |
| 905 | (!test_bit(FLAG_FLUSHABLE, &chan->flags) && |
| 906 | lmp_no_flush_capable(hcon->hdev))) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 907 | flags = ACL_START_NO_FLUSH; |
| 908 | else |
| 909 | flags = ACL_START; |
| 910 | |
| 911 | bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 912 | hci_send_acl(chan->conn->hchan, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 915 | static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control) |
| 916 | { |
| 917 | control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT; |
| 918 | control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT; |
| 919 | |
| 920 | if (enh & L2CAP_CTRL_FRAME_TYPE) { |
| 921 | /* S-Frame */ |
| 922 | control->sframe = 1; |
| 923 | control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT; |
| 924 | control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT; |
| 925 | |
| 926 | control->sar = 0; |
| 927 | control->txseq = 0; |
| 928 | } else { |
| 929 | /* I-Frame */ |
| 930 | control->sframe = 0; |
| 931 | control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT; |
| 932 | control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT; |
| 933 | |
| 934 | control->poll = 0; |
| 935 | control->super = 0; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control) |
| 940 | { |
| 941 | control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT; |
| 942 | control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT; |
| 943 | |
| 944 | if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) { |
| 945 | /* S-Frame */ |
| 946 | control->sframe = 1; |
| 947 | control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT; |
| 948 | control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT; |
| 949 | |
| 950 | control->sar = 0; |
| 951 | control->txseq = 0; |
| 952 | } else { |
| 953 | /* I-Frame */ |
| 954 | control->sframe = 0; |
| 955 | control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT; |
| 956 | control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT; |
| 957 | |
| 958 | control->poll = 0; |
| 959 | control->super = 0; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | static inline void __unpack_control(struct l2cap_chan *chan, |
| 964 | struct sk_buff *skb) |
| 965 | { |
| 966 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 967 | __unpack_extended_control(get_unaligned_le32(skb->data), |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 968 | &bt_cb(skb)->l2cap); |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 969 | skb_pull(skb, L2CAP_EXT_CTRL_SIZE); |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 970 | } else { |
| 971 | __unpack_enhanced_control(get_unaligned_le16(skb->data), |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 972 | &bt_cb(skb)->l2cap); |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 973 | skb_pull(skb, L2CAP_ENH_CTRL_SIZE); |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | |
| 977 | static u32 __pack_extended_control(struct l2cap_ctrl *control) |
| 978 | { |
| 979 | u32 packed; |
| 980 | |
| 981 | packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT; |
| 982 | packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT; |
| 983 | |
| 984 | if (control->sframe) { |
| 985 | packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT; |
| 986 | packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT; |
| 987 | packed |= L2CAP_EXT_CTRL_FRAME_TYPE; |
| 988 | } else { |
| 989 | packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT; |
| 990 | packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT; |
| 991 | } |
| 992 | |
| 993 | return packed; |
| 994 | } |
| 995 | |
| 996 | static u16 __pack_enhanced_control(struct l2cap_ctrl *control) |
| 997 | { |
| 998 | u16 packed; |
| 999 | |
| 1000 | packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT; |
| 1001 | packed |= control->final << L2CAP_CTRL_FINAL_SHIFT; |
| 1002 | |
| 1003 | if (control->sframe) { |
| 1004 | packed |= control->poll << L2CAP_CTRL_POLL_SHIFT; |
| 1005 | packed |= control->super << L2CAP_CTRL_SUPER_SHIFT; |
| 1006 | packed |= L2CAP_CTRL_FRAME_TYPE; |
| 1007 | } else { |
| 1008 | packed |= control->sar << L2CAP_CTRL_SAR_SHIFT; |
| 1009 | packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT; |
| 1010 | } |
| 1011 | |
| 1012 | return packed; |
| 1013 | } |
| 1014 | |
| 1015 | static inline void __pack_control(struct l2cap_chan *chan, |
| 1016 | struct l2cap_ctrl *control, |
| 1017 | struct sk_buff *skb) |
| 1018 | { |
| 1019 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 1020 | put_unaligned_le32(__pack_extended_control(control), |
| 1021 | skb->data + L2CAP_HDR_SIZE); |
| 1022 | } else { |
| 1023 | put_unaligned_le16(__pack_enhanced_control(control), |
| 1024 | skb->data + L2CAP_HDR_SIZE); |
| 1025 | } |
| 1026 | } |
| 1027 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 1028 | static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan) |
| 1029 | { |
| 1030 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 1031 | return L2CAP_EXT_HDR_SIZE; |
| 1032 | else |
| 1033 | return L2CAP_ENH_HDR_SIZE; |
| 1034 | } |
| 1035 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1036 | static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan, |
| 1037 | u32 control) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1038 | { |
| 1039 | struct sk_buff *skb; |
| 1040 | struct l2cap_hdr *lh; |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 1041 | int hlen = __ertm_hdr_size(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1042 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1043 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1044 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1045 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1046 | skb = bt_skb_alloc(hlen, GFP_KERNEL); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1047 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1048 | if (!skb) |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1049 | return ERR_PTR(-ENOMEM); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1050 | |
| 1051 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1052 | lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1053 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1054 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1055 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 1056 | put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 1057 | else |
| 1058 | put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1059 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1060 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1061 | u16 fcs = crc16(0, (u8 *)skb->data, skb->len); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1062 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1063 | } |
| 1064 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1065 | skb->priority = HCI_PRIO_MAX; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1066 | return skb; |
| 1067 | } |
| 1068 | |
| 1069 | static void l2cap_send_sframe(struct l2cap_chan *chan, |
| 1070 | struct l2cap_ctrl *control) |
| 1071 | { |
| 1072 | struct sk_buff *skb; |
| 1073 | u32 control_field; |
| 1074 | |
| 1075 | BT_DBG("chan %p, control %p", chan, control); |
| 1076 | |
| 1077 | if (!control->sframe) |
| 1078 | return; |
| 1079 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1080 | if (__chan_is_moving(chan)) |
| 1081 | return; |
| 1082 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1083 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) && |
| 1084 | !control->poll) |
| 1085 | control->final = 1; |
| 1086 | |
| 1087 | if (control->super == L2CAP_SUPER_RR) |
| 1088 | clear_bit(CONN_RNR_SENT, &chan->conn_state); |
| 1089 | else if (control->super == L2CAP_SUPER_RNR) |
| 1090 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
| 1091 | |
| 1092 | if (control->super != L2CAP_SUPER_SREJ) { |
| 1093 | chan->last_acked_seq = control->reqseq; |
| 1094 | __clear_ack_timer(chan); |
| 1095 | } |
| 1096 | |
| 1097 | BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq, |
| 1098 | control->final, control->poll, control->super); |
| 1099 | |
| 1100 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 1101 | control_field = __pack_extended_control(control); |
| 1102 | else |
| 1103 | control_field = __pack_enhanced_control(control); |
| 1104 | |
| 1105 | skb = l2cap_create_sframe_pdu(chan, control_field); |
| 1106 | if (!IS_ERR(skb)) |
| 1107 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1108 | } |
| 1109 | |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1110 | 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] | 1111 | { |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1112 | struct l2cap_ctrl control; |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 1113 | |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1114 | BT_DBG("chan %p, poll %d", chan, poll); |
| 1115 | |
| 1116 | memset(&control, 0, sizeof(control)); |
| 1117 | control.sframe = 1; |
| 1118 | control.poll = poll; |
| 1119 | |
| 1120 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 1121 | control.super = L2CAP_SUPER_RNR; |
| 1122 | else |
| 1123 | control.super = L2CAP_SUPER_RR; |
| 1124 | |
| 1125 | control.reqseq = chan->buffer_seq; |
| 1126 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 1127 | } |
| 1128 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1129 | static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 1130 | { |
Johan Hedberg | 5ff6f34 | 2014-08-07 22:56:43 +0300 | [diff] [blame] | 1131 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
| 1132 | return true; |
| 1133 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1134 | return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 1135 | } |
| 1136 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1137 | static bool __amp_capable(struct l2cap_chan *chan) |
| 1138 | { |
| 1139 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 1df7b17 | 2013-10-05 11:47:49 -0700 | [diff] [blame] | 1140 | struct hci_dev *hdev; |
| 1141 | bool amp_available = false; |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1142 | |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 1143 | if (!(conn->local_fixed_chan & L2CAP_FC_A2MP)) |
Marcel Holtmann | 1df7b17 | 2013-10-05 11:47:49 -0700 | [diff] [blame] | 1144 | return false; |
| 1145 | |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 1146 | if (!(conn->remote_fixed_chan & L2CAP_FC_A2MP)) |
Marcel Holtmann | 1df7b17 | 2013-10-05 11:47:49 -0700 | [diff] [blame] | 1147 | return false; |
| 1148 | |
| 1149 | read_lock(&hci_dev_list_lock); |
| 1150 | list_for_each_entry(hdev, &hci_dev_list, list) { |
| 1151 | if (hdev->amp_type != AMP_TYPE_BREDR && |
| 1152 | test_bit(HCI_UP, &hdev->flags)) { |
| 1153 | amp_available = true; |
| 1154 | break; |
| 1155 | } |
| 1156 | } |
| 1157 | read_unlock(&hci_dev_list_lock); |
| 1158 | |
| 1159 | if (chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED) |
| 1160 | return amp_available; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 1161 | |
| 1162 | return false; |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1163 | } |
| 1164 | |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 1165 | static bool l2cap_check_efs(struct l2cap_chan *chan) |
| 1166 | { |
| 1167 | /* Check EFS parameters */ |
| 1168 | return true; |
| 1169 | } |
| 1170 | |
Andrei Emeltchenko | 2766be4 | 2012-09-27 17:26:21 +0300 | [diff] [blame] | 1171 | void l2cap_send_conn_req(struct l2cap_chan *chan) |
Andrei Emeltchenko | 9b27f35 | 2012-02-24 16:00:00 +0200 | [diff] [blame] | 1172 | { |
| 1173 | struct l2cap_conn *conn = chan->conn; |
| 1174 | struct l2cap_conn_req req; |
| 1175 | |
| 1176 | req.scid = cpu_to_le16(chan->scid); |
| 1177 | req.psm = chan->psm; |
| 1178 | |
| 1179 | chan->ident = l2cap_get_ident(conn); |
| 1180 | |
| 1181 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
| 1182 | |
| 1183 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req); |
| 1184 | } |
| 1185 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 1186 | static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id) |
| 1187 | { |
| 1188 | struct l2cap_create_chan_req req; |
| 1189 | req.scid = cpu_to_le16(chan->scid); |
| 1190 | req.psm = chan->psm; |
| 1191 | req.amp_id = amp_id; |
| 1192 | |
| 1193 | chan->ident = l2cap_get_ident(chan->conn); |
| 1194 | |
| 1195 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ, |
| 1196 | sizeof(req), &req); |
| 1197 | } |
| 1198 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 1199 | static void l2cap_move_setup(struct l2cap_chan *chan) |
| 1200 | { |
| 1201 | struct sk_buff *skb; |
| 1202 | |
| 1203 | BT_DBG("chan %p", chan); |
| 1204 | |
| 1205 | if (chan->mode != L2CAP_MODE_ERTM) |
| 1206 | return; |
| 1207 | |
| 1208 | __clear_retrans_timer(chan); |
| 1209 | __clear_monitor_timer(chan); |
| 1210 | __clear_ack_timer(chan); |
| 1211 | |
| 1212 | chan->retry_count = 0; |
| 1213 | skb_queue_walk(&chan->tx_q, skb) { |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 1214 | if (bt_cb(skb)->l2cap.retries) |
| 1215 | bt_cb(skb)->l2cap.retries = 1; |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 1216 | else |
| 1217 | break; |
| 1218 | } |
| 1219 | |
| 1220 | chan->expected_tx_seq = chan->buffer_seq; |
| 1221 | |
| 1222 | clear_bit(CONN_REJ_ACT, &chan->conn_state); |
| 1223 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 1224 | l2cap_seq_list_clear(&chan->retrans_list); |
| 1225 | l2cap_seq_list_clear(&chan->srej_list); |
| 1226 | skb_queue_purge(&chan->srej_q); |
| 1227 | |
| 1228 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 1229 | chan->rx_state = L2CAP_RX_STATE_MOVE; |
| 1230 | |
| 1231 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 1232 | } |
| 1233 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 1234 | static void l2cap_move_done(struct l2cap_chan *chan) |
| 1235 | { |
| 1236 | u8 move_role = chan->move_role; |
| 1237 | BT_DBG("chan %p", chan); |
| 1238 | |
| 1239 | chan->move_state = L2CAP_MOVE_STABLE; |
| 1240 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 1241 | |
| 1242 | if (chan->mode != L2CAP_MODE_ERTM) |
| 1243 | return; |
| 1244 | |
| 1245 | switch (move_role) { |
| 1246 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 1247 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL); |
| 1248 | chan->rx_state = L2CAP_RX_STATE_WAIT_F; |
| 1249 | break; |
| 1250 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 1251 | chan->rx_state = L2CAP_RX_STATE_WAIT_P; |
| 1252 | break; |
| 1253 | } |
| 1254 | } |
| 1255 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1256 | static void l2cap_chan_ready(struct l2cap_chan *chan) |
| 1257 | { |
Johan Hedberg | 315917e | 2015-02-16 11:42:11 +0200 | [diff] [blame] | 1258 | /* The channel may have already been flagged as connected in |
| 1259 | * case of receiving data before the L2CAP info req/rsp |
| 1260 | * procedure is complete. |
| 1261 | */ |
| 1262 | if (chan->state == BT_CONNECTED) |
| 1263 | return; |
| 1264 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 1265 | /* This clears all conf flags, including CONF_NOT_COMPLETE */ |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1266 | chan->conf_state = 0; |
| 1267 | __clear_chan_timer(chan); |
| 1268 | |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 1269 | if (chan->mode == L2CAP_MODE_LE_FLOWCTL && !chan->tx_credits) |
| 1270 | chan->ops->suspend(chan); |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 1271 | |
Andrei Emeltchenko | 54a59aa | 2012-05-27 22:27:53 -0300 | [diff] [blame] | 1272 | chan->state = BT_CONNECTED; |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1273 | |
Andrei Emeltchenko | fd83e2c | 2012-05-30 09:55:32 +0300 | [diff] [blame] | 1274 | chan->ops->ready(chan); |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1275 | } |
| 1276 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1277 | static void l2cap_le_connect(struct l2cap_chan *chan) |
| 1278 | { |
| 1279 | struct l2cap_conn *conn = chan->conn; |
| 1280 | struct l2cap_le_conn_req req; |
| 1281 | |
Johan Hedberg | 595177f | 2013-12-02 22:12:22 +0200 | [diff] [blame] | 1282 | if (test_and_set_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags)) |
| 1283 | return; |
| 1284 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1285 | req.psm = chan->psm; |
| 1286 | req.scid = cpu_to_le16(chan->scid); |
| 1287 | req.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 1288 | req.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 1289 | req.credits = cpu_to_le16(chan->rx_credits); |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1290 | |
| 1291 | chan->ident = l2cap_get_ident(conn); |
| 1292 | |
| 1293 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_REQ, |
| 1294 | sizeof(req), &req); |
| 1295 | } |
| 1296 | |
| 1297 | static void l2cap_le_start(struct l2cap_chan *chan) |
| 1298 | { |
| 1299 | struct l2cap_conn *conn = chan->conn; |
| 1300 | |
| 1301 | if (!smp_conn_security(conn->hcon, chan->sec_level)) |
| 1302 | return; |
| 1303 | |
| 1304 | if (!chan->psm) { |
| 1305 | l2cap_chan_ready(chan); |
| 1306 | return; |
| 1307 | } |
| 1308 | |
| 1309 | if (chan->state == BT_CONNECT) |
| 1310 | l2cap_le_connect(chan); |
| 1311 | } |
| 1312 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1313 | static void l2cap_start_connection(struct l2cap_chan *chan) |
| 1314 | { |
| 1315 | if (__amp_capable(chan)) { |
| 1316 | BT_DBG("chan %p AMP capable: discover AMPs", chan); |
| 1317 | a2mp_discover_amp(chan); |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1318 | } else if (chan->conn->hcon->type == LE_LINK) { |
| 1319 | l2cap_le_start(chan); |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1320 | } else { |
| 1321 | l2cap_send_conn_req(chan); |
| 1322 | } |
| 1323 | } |
| 1324 | |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1325 | static void l2cap_request_info(struct l2cap_conn *conn) |
| 1326 | { |
| 1327 | struct l2cap_info_req req; |
| 1328 | |
| 1329 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
| 1330 | return; |
| 1331 | |
| 1332 | req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 1333 | |
| 1334 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 1335 | conn->info_ident = l2cap_get_ident(conn); |
| 1336 | |
| 1337 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
| 1338 | |
| 1339 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 1340 | sizeof(req), &req); |
| 1341 | } |
| 1342 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1343 | static void l2cap_do_start(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1344 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1345 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1346 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1347 | if (conn->hcon->type == LE_LINK) { |
Johan Hedberg | 96ac34f | 2013-05-13 11:15:07 +0300 | [diff] [blame] | 1348 | l2cap_le_start(chan); |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1349 | return; |
| 1350 | } |
| 1351 | |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1352 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)) { |
| 1353 | l2cap_request_info(conn); |
| 1354 | return; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1355 | } |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1356 | |
| 1357 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) |
| 1358 | return; |
| 1359 | |
| 1360 | if (l2cap_chan_check_security(chan, true) && |
| 1361 | __l2cap_no_conn_pending(chan)) |
| 1362 | l2cap_start_connection(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1363 | } |
| 1364 | |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 1365 | static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) |
| 1366 | { |
| 1367 | u32 local_feat_mask = l2cap_feat_mask; |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 1368 | if (!disable_ertm) |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 1369 | local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; |
| 1370 | |
| 1371 | switch (mode) { |
| 1372 | case L2CAP_MODE_ERTM: |
| 1373 | return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; |
| 1374 | case L2CAP_MODE_STREAMING: |
| 1375 | return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; |
| 1376 | default: |
| 1377 | return 0x00; |
| 1378 | } |
| 1379 | } |
| 1380 | |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1381 | 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] | 1382 | { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1383 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1384 | struct l2cap_disconn_req req; |
| 1385 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1386 | if (!conn) |
| 1387 | return; |
| 1388 | |
Andrei Emeltchenko | aad3d0e | 2012-09-06 15:05:42 +0300 | [diff] [blame] | 1389 | if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1390 | __clear_retrans_timer(chan); |
| 1391 | __clear_monitor_timer(chan); |
| 1392 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1393 | } |
| 1394 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 1395 | if (chan->scid == L2CAP_CID_A2MP) { |
Gustavo Padovan | d117773 | 2012-10-06 11:47:38 +0100 | [diff] [blame] | 1396 | l2cap_state_change(chan, BT_DISCONN); |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1397 | return; |
| 1398 | } |
| 1399 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1400 | req.dcid = cpu_to_le16(chan->dcid); |
| 1401 | req.scid = cpu_to_le16(chan->scid); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1402 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ, |
| 1403 | sizeof(req), &req); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1404 | |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 1405 | l2cap_state_change_and_error(chan, BT_DISCONN, err); |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1406 | } |
| 1407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | /* ---- L2CAP connections ---- */ |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1409 | static void l2cap_conn_start(struct l2cap_conn *conn) |
| 1410 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1411 | struct l2cap_chan *chan, *tmp; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1412 | |
| 1413 | BT_DBG("conn %p", conn); |
| 1414 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1415 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1416 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1417 | list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1418 | l2cap_chan_lock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1419 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1420 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1421 | l2cap_chan_ready(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1422 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1423 | continue; |
| 1424 | } |
| 1425 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1426 | if (chan->state == BT_CONNECT) { |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 1427 | if (!l2cap_chan_check_security(chan, true) || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1428 | !__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1429 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1430 | continue; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 1431 | } |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1432 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1433 | if (!l2cap_mode_supported(chan->mode, conn->feat_mask) |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1434 | && test_bit(CONF_STATE2_DEVICE, |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1435 | &chan->conf_state)) { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1436 | l2cap_chan_close(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1437 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1438 | continue; |
| 1439 | } |
| 1440 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1441 | l2cap_start_connection(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1442 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1443 | } else if (chan->state == BT_CONNECT2) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1444 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1445 | char buf[128]; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1446 | rsp.scid = cpu_to_le16(chan->dcid); |
| 1447 | rsp.dcid = cpu_to_le16(chan->scid); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1448 | |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 1449 | if (l2cap_chan_check_security(chan, false)) { |
Marcel Holtmann | bdc2578 | 2013-10-14 02:45:34 -0700 | [diff] [blame] | 1450 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1451 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 1452 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 1453 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 1454 | |
| 1455 | } else { |
Gustavo Padovan | acdcabf | 2013-10-21 14:21:39 -0200 | [diff] [blame] | 1456 | l2cap_state_change(chan, BT_CONFIG); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1457 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 1458 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 1459 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1460 | } else { |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1461 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 1462 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1463 | } |
| 1464 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1465 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1466 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1467 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1468 | if (test_bit(CONF_REQ_SENT, &chan->conf_state) || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1469 | rsp.result != L2CAP_CR_SUCCESS) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1470 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1471 | continue; |
| 1472 | } |
| 1473 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1474 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1475 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1476 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1477 | chan->num_conf_req++; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1478 | } |
| 1479 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1480 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1481 | } |
| 1482 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1483 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1484 | } |
| 1485 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1486 | static void l2cap_le_conn_ready(struct l2cap_conn *conn) |
| 1487 | { |
Marcel Holtmann | cc8dba2 | 2013-10-18 03:43:01 -0700 | [diff] [blame] | 1488 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 1489 | struct hci_dev *hdev = hcon->hdev; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1490 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 1491 | BT_DBG("%s conn %p", hdev->name, conn); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1492 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 1493 | /* For outgoing pairing which doesn't necessarily have an |
| 1494 | * associated socket (e.g. mgmt_pair_device). |
| 1495 | */ |
| 1496 | if (hcon->out) |
| 1497 | smp_conn_security(hcon, hcon->pending_sec_level); |
Marcel Holtmann | cc8dba2 | 2013-10-18 03:43:01 -0700 | [diff] [blame] | 1498 | |
Marcel Holtmann | 80afeb6 | 2014-06-23 12:18:51 +0200 | [diff] [blame] | 1499 | /* For LE slave connections, make sure the connection interval |
| 1500 | * is in the range of the minium and maximum interval that has |
| 1501 | * been configured for this connection. If not, then trigger |
| 1502 | * the connection update procedure. |
| 1503 | */ |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1504 | if (hcon->role == HCI_ROLE_SLAVE && |
Marcel Holtmann | 80afeb6 | 2014-06-23 12:18:51 +0200 | [diff] [blame] | 1505 | (hcon->le_conn_interval < hcon->le_conn_min_interval || |
| 1506 | hcon->le_conn_interval > hcon->le_conn_max_interval)) { |
| 1507 | struct l2cap_conn_param_update_req req; |
| 1508 | |
| 1509 | req.min = cpu_to_le16(hcon->le_conn_min_interval); |
| 1510 | req.max = cpu_to_le16(hcon->le_conn_max_interval); |
| 1511 | req.latency = cpu_to_le16(hcon->le_conn_latency); |
| 1512 | req.to_multiplier = cpu_to_le16(hcon->le_supv_timeout); |
| 1513 | |
| 1514 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 1515 | L2CAP_CONN_PARAM_UPDATE_REQ, sizeof(req), &req); |
| 1516 | } |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1517 | } |
| 1518 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1519 | static void l2cap_conn_ready(struct l2cap_conn *conn) |
| 1520 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1521 | struct l2cap_chan *chan; |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1522 | struct hci_conn *hcon = conn->hcon; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1523 | |
| 1524 | BT_DBG("conn %p", conn); |
| 1525 | |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1526 | if (hcon->type == ACL_LINK) |
| 1527 | l2cap_request_info(conn); |
| 1528 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 1529 | mutex_lock(&conn->chan_lock); |
| 1530 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1531 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1532 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1533 | l2cap_chan_lock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1534 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 1535 | if (chan->scid == L2CAP_CID_A2MP) { |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1536 | l2cap_chan_unlock(chan); |
| 1537 | continue; |
| 1538 | } |
| 1539 | |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1540 | if (hcon->type == LE_LINK) { |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1541 | l2cap_le_start(chan); |
Vinicius Costa Gomes | 6312845 | 2011-06-17 22:46:26 -0300 | [diff] [blame] | 1542 | } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1543 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
| 1544 | l2cap_chan_ready(chan); |
Gustavo Padovan | 1c244f7 | 2012-12-07 03:29:10 -0200 | [diff] [blame] | 1545 | } else if (chan->state == BT_CONNECT) { |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1546 | l2cap_do_start(chan); |
Gustavo Padovan | 1c244f7 | 2012-12-07 03:29:10 -0200 | [diff] [blame] | 1547 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1548 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1549 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1550 | } |
| 1551 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1552 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1553 | |
Johan Hedberg | 79a0572 | 2014-08-08 09:28:04 +0300 | [diff] [blame] | 1554 | if (hcon->type == LE_LINK) |
| 1555 | l2cap_le_conn_ready(conn); |
| 1556 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1557 | queue_work(hcon->hdev->workqueue, &conn->pending_rx_work); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1558 | } |
| 1559 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1560 | /* Notify sockets that we cannot guaranty reliability anymore */ |
| 1561 | static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) |
| 1562 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1563 | struct l2cap_chan *chan; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1564 | |
| 1565 | BT_DBG("conn %p", conn); |
| 1566 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1567 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1568 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1569 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | ecf61bd | 2011-10-11 14:04:32 +0300 | [diff] [blame] | 1570 | if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) |
Gustavo Padovan | 1d8b1fd | 2012-10-06 11:34:52 +0100 | [diff] [blame] | 1571 | l2cap_chan_set_err(chan, err); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1572 | } |
| 1573 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1574 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1575 | } |
| 1576 | |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1577 | static void l2cap_info_timeout(struct work_struct *work) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1578 | { |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1579 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1580 | info_timer.work); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1581 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1582 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 1583 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1584 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1585 | l2cap_conn_start(conn); |
| 1586 | } |
| 1587 | |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1588 | /* |
| 1589 | * l2cap_user |
| 1590 | * External modules can register l2cap_user objects on l2cap_conn. The ->probe |
| 1591 | * callback is called during registration. The ->remove callback is called |
| 1592 | * during unregistration. |
| 1593 | * An l2cap_user object can either be explicitly unregistered or when the |
| 1594 | * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon, |
| 1595 | * l2cap->hchan, .. are valid as long as the remove callback hasn't been called. |
| 1596 | * External modules must own a reference to the l2cap_conn object if they intend |
| 1597 | * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at |
| 1598 | * any time if they don't. |
| 1599 | */ |
| 1600 | |
| 1601 | int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user) |
| 1602 | { |
| 1603 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1604 | int ret; |
| 1605 | |
| 1606 | /* We need to check whether l2cap_conn is registered. If it is not, we |
| 1607 | * must not register the l2cap_user. l2cap_conn_del() is unregisters |
| 1608 | * l2cap_conn objects, but doesn't provide its own locking. Instead, it |
| 1609 | * relies on the parent hci_conn object to be locked. This itself relies |
| 1610 | * on the hci_dev object to be locked. So we must lock the hci device |
| 1611 | * here, too. */ |
| 1612 | |
| 1613 | hci_dev_lock(hdev); |
| 1614 | |
Alexey Dobriyan | 835a6a2 | 2015-06-10 20:28:33 +0300 | [diff] [blame] | 1615 | if (!list_empty(&user->list)) { |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1616 | ret = -EINVAL; |
| 1617 | goto out_unlock; |
| 1618 | } |
| 1619 | |
| 1620 | /* conn->hchan is NULL after l2cap_conn_del() was called */ |
| 1621 | if (!conn->hchan) { |
| 1622 | ret = -ENODEV; |
| 1623 | goto out_unlock; |
| 1624 | } |
| 1625 | |
| 1626 | ret = user->probe(conn, user); |
| 1627 | if (ret) |
| 1628 | goto out_unlock; |
| 1629 | |
| 1630 | list_add(&user->list, &conn->users); |
| 1631 | ret = 0; |
| 1632 | |
| 1633 | out_unlock: |
| 1634 | hci_dev_unlock(hdev); |
| 1635 | return ret; |
| 1636 | } |
| 1637 | EXPORT_SYMBOL(l2cap_register_user); |
| 1638 | |
| 1639 | void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user) |
| 1640 | { |
| 1641 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1642 | |
| 1643 | hci_dev_lock(hdev); |
| 1644 | |
Alexey Dobriyan | 835a6a2 | 2015-06-10 20:28:33 +0300 | [diff] [blame] | 1645 | if (list_empty(&user->list)) |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1646 | goto out_unlock; |
| 1647 | |
Tedd Ho-Jeong An | ab944c8 | 2015-06-30 11:43:40 -0700 | [diff] [blame] | 1648 | list_del_init(&user->list); |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1649 | user->remove(conn, user); |
| 1650 | |
| 1651 | out_unlock: |
| 1652 | hci_dev_unlock(hdev); |
| 1653 | } |
| 1654 | EXPORT_SYMBOL(l2cap_unregister_user); |
| 1655 | |
| 1656 | static void l2cap_unregister_all_users(struct l2cap_conn *conn) |
| 1657 | { |
| 1658 | struct l2cap_user *user; |
| 1659 | |
| 1660 | while (!list_empty(&conn->users)) { |
| 1661 | user = list_first_entry(&conn->users, struct l2cap_user, list); |
Tedd Ho-Jeong An | ab944c8 | 2015-06-30 11:43:40 -0700 | [diff] [blame] | 1662 | list_del_init(&user->list); |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1663 | user->remove(conn, user); |
| 1664 | } |
| 1665 | } |
| 1666 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1667 | static void l2cap_conn_del(struct hci_conn *hcon, int err) |
| 1668 | { |
| 1669 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 1670 | struct l2cap_chan *chan, *l; |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1671 | |
| 1672 | if (!conn) |
| 1673 | return; |
| 1674 | |
| 1675 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 1676 | |
| 1677 | kfree_skb(conn->rx_skb); |
| 1678 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1679 | skb_queue_purge(&conn->pending_rx); |
Jukka Taimisto | 7ab56c3 | 2014-06-12 10:15:13 +0000 | [diff] [blame] | 1680 | |
| 1681 | /* We can not call flush_work(&conn->pending_rx_work) here since we |
| 1682 | * might block if we are running on a worker from the same workqueue |
| 1683 | * pending_rx_work is waiting on. |
| 1684 | */ |
| 1685 | if (work_pending(&conn->pending_rx_work)) |
| 1686 | cancel_work_sync(&conn->pending_rx_work); |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1687 | |
Johan Hedberg | f3d82d0 | 2014-09-05 22:19:50 +0300 | [diff] [blame] | 1688 | if (work_pending(&conn->id_addr_update_work)) |
| 1689 | cancel_work_sync(&conn->id_addr_update_work); |
| 1690 | |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1691 | l2cap_unregister_all_users(conn); |
| 1692 | |
Johan Hedberg | e31fb86 | 2014-08-18 20:33:28 +0300 | [diff] [blame] | 1693 | /* Force the connection to be immediately dropped */ |
| 1694 | hcon->disc_timeout = 0; |
| 1695 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1696 | mutex_lock(&conn->chan_lock); |
| 1697 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1698 | /* Kill channels */ |
| 1699 | list_for_each_entry_safe(chan, l, &conn->chan_l, list) { |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1700 | l2cap_chan_hold(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1701 | l2cap_chan_lock(chan); |
| 1702 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1703 | l2cap_chan_del(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1704 | |
| 1705 | l2cap_chan_unlock(chan); |
| 1706 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 1707 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1708 | l2cap_chan_put(chan); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1709 | } |
| 1710 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1711 | mutex_unlock(&conn->chan_lock); |
| 1712 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1713 | hci_chan_del(conn->hchan); |
| 1714 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1715 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1716 | cancel_delayed_work_sync(&conn->info_timer); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1717 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1718 | hcon->l2cap_data = NULL; |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1719 | conn->hchan = NULL; |
| 1720 | l2cap_conn_put(conn); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1721 | } |
| 1722 | |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1723 | static void l2cap_conn_free(struct kref *ref) |
| 1724 | { |
| 1725 | struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref); |
| 1726 | |
| 1727 | hci_conn_put(conn->hcon); |
| 1728 | kfree(conn); |
| 1729 | } |
| 1730 | |
Johan Hedberg | 51bb8457 | 2014-08-15 21:06:57 +0300 | [diff] [blame] | 1731 | struct l2cap_conn *l2cap_conn_get(struct l2cap_conn *conn) |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1732 | { |
| 1733 | kref_get(&conn->ref); |
Johan Hedberg | 51bb8457 | 2014-08-15 21:06:57 +0300 | [diff] [blame] | 1734 | return conn; |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1735 | } |
| 1736 | EXPORT_SYMBOL(l2cap_conn_get); |
| 1737 | |
| 1738 | void l2cap_conn_put(struct l2cap_conn *conn) |
| 1739 | { |
| 1740 | kref_put(&conn->ref, l2cap_conn_free); |
| 1741 | } |
| 1742 | EXPORT_SYMBOL(l2cap_conn_put); |
| 1743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | /* ---- Socket interface ---- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1746 | /* Find socket with psm and source / destination bdaddr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | * Returns closest match. |
| 1748 | */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1749 | static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, |
| 1750 | bdaddr_t *src, |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 1751 | bdaddr_t *dst, |
| 1752 | u8 link_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1754 | struct l2cap_chan *c, *c1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1756 | read_lock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1757 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1758 | list_for_each_entry(c, &chan_list, global_l) { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1759 | if (state && c->state != state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | continue; |
| 1761 | |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 1762 | if (link_type == ACL_LINK && c->src_type != BDADDR_BREDR) |
| 1763 | continue; |
| 1764 | |
| 1765 | if (link_type == LE_LINK && c->src_type == BDADDR_BREDR) |
| 1766 | continue; |
| 1767 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1768 | if (c->psm == psm) { |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1769 | int src_match, dst_match; |
| 1770 | int src_any, dst_any; |
| 1771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | /* Exact match. */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1773 | src_match = !bacmp(&c->src, src); |
| 1774 | dst_match = !bacmp(&c->dst, dst); |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1775 | if (src_match && dst_match) { |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 1776 | l2cap_chan_hold(c); |
Johannes Berg | a7567b2 | 2011-06-01 08:29:54 +0200 | [diff] [blame] | 1777 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1778 | return c; |
| 1779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | |
| 1781 | /* Closest match */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1782 | src_any = !bacmp(&c->src, BDADDR_ANY); |
| 1783 | dst_any = !bacmp(&c->dst, BDADDR_ANY); |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1784 | if ((src_match && dst_any) || (src_any && dst_match) || |
| 1785 | (src_any && dst_any)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1786 | c1 = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | } |
| 1788 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 1790 | if (c1) |
| 1791 | l2cap_chan_hold(c1); |
| 1792 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1793 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1794 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1795 | return c1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | } |
| 1797 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1798 | static void l2cap_monitor_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1799 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1800 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1801 | monitor_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1802 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1803 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1804 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1805 | l2cap_chan_lock(chan); |
| 1806 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1807 | if (!chan->conn) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1808 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1809 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1810 | return; |
| 1811 | } |
| 1812 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1813 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1814 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1815 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1816 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1817 | } |
| 1818 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1819 | static void l2cap_retrans_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1820 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1821 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1822 | retrans_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1823 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1824 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1825 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1826 | l2cap_chan_lock(chan); |
| 1827 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1828 | if (!chan->conn) { |
| 1829 | l2cap_chan_unlock(chan); |
| 1830 | l2cap_chan_put(chan); |
| 1831 | return; |
| 1832 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1833 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1834 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1835 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1836 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1837 | } |
| 1838 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 1839 | static void l2cap_streaming_send(struct l2cap_chan *chan, |
| 1840 | struct sk_buff_head *skbs) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1841 | { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1842 | struct sk_buff *skb; |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1843 | struct l2cap_ctrl *control; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1844 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1845 | BT_DBG("chan %p, skbs %p", chan, skbs); |
| 1846 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1847 | if (__chan_is_moving(chan)) |
| 1848 | return; |
| 1849 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1850 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 1851 | |
| 1852 | while (!skb_queue_empty(&chan->tx_q)) { |
| 1853 | |
| 1854 | skb = skb_dequeue(&chan->tx_q); |
| 1855 | |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 1856 | bt_cb(skb)->l2cap.retries = 1; |
| 1857 | control = &bt_cb(skb)->l2cap; |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1858 | |
| 1859 | control->reqseq = 0; |
| 1860 | control->txseq = chan->next_tx_seq; |
| 1861 | |
| 1862 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1863 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1864 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1865 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 1866 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1867 | } |
| 1868 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1869 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1870 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1871 | BT_DBG("Sent txseq %u", control->txseq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1872 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1873 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1874 | chan->frames_sent++; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1875 | } |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1876 | } |
| 1877 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1878 | static int l2cap_ertm_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1879 | { |
| 1880 | struct sk_buff *skb, *tx_skb; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1881 | struct l2cap_ctrl *control; |
| 1882 | int sent = 0; |
| 1883 | |
| 1884 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1885 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1886 | if (chan->state != BT_CONNECTED) |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1887 | return -ENOTCONN; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1888 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 1889 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 1890 | return 0; |
| 1891 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1892 | if (__chan_is_moving(chan)) |
| 1893 | return 0; |
| 1894 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1895 | while (chan->tx_send_head && |
| 1896 | chan->unacked_frames < chan->remote_tx_win && |
| 1897 | chan->tx_state == L2CAP_TX_STATE_XMIT) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1898 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1899 | skb = chan->tx_send_head; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1900 | |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 1901 | bt_cb(skb)->l2cap.retries = 1; |
| 1902 | control = &bt_cb(skb)->l2cap; |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1903 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1904 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1905 | control->final = 1; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1906 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1907 | control->reqseq = chan->buffer_seq; |
| 1908 | chan->last_acked_seq = chan->buffer_seq; |
| 1909 | control->txseq = chan->next_tx_seq; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1910 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1911 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1912 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1913 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1914 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 1915 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1916 | } |
| 1917 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1918 | /* Clone after data has been modified. Data is assumed to be |
| 1919 | read-only (for locking purposes) on cloned sk_buffs. |
| 1920 | */ |
| 1921 | tx_skb = skb_clone(skb, GFP_KERNEL); |
| 1922 | |
| 1923 | if (!tx_skb) |
| 1924 | break; |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1925 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1926 | __set_retrans_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1927 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1928 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1929 | chan->unacked_frames++; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1930 | chan->frames_sent++; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1931 | sent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1932 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1933 | if (skb_queue_is_last(&chan->tx_q, skb)) |
| 1934 | chan->tx_send_head = NULL; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1935 | else |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1936 | chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1937 | |
| 1938 | l2cap_do_send(chan, tx_skb); |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1939 | BT_DBG("Sent txseq %u", control->txseq); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1940 | } |
| 1941 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1942 | BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent, |
| 1943 | chan->unacked_frames, skb_queue_len(&chan->tx_q)); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1944 | |
| 1945 | return sent; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1946 | } |
| 1947 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1948 | static void l2cap_ertm_resend(struct l2cap_chan *chan) |
| 1949 | { |
| 1950 | struct l2cap_ctrl control; |
| 1951 | struct sk_buff *skb; |
| 1952 | struct sk_buff *tx_skb; |
| 1953 | u16 seq; |
| 1954 | |
| 1955 | BT_DBG("chan %p", chan); |
| 1956 | |
| 1957 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 1958 | return; |
| 1959 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1960 | if (__chan_is_moving(chan)) |
| 1961 | return; |
| 1962 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1963 | while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) { |
| 1964 | seq = l2cap_seq_list_pop(&chan->retrans_list); |
| 1965 | |
| 1966 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq); |
| 1967 | if (!skb) { |
| 1968 | BT_DBG("Error: Can't retransmit seq %d, frame missing", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1969 | seq); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1970 | continue; |
| 1971 | } |
| 1972 | |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 1973 | bt_cb(skb)->l2cap.retries++; |
| 1974 | control = bt_cb(skb)->l2cap; |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1975 | |
| 1976 | if (chan->max_tx != 0 && |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 1977 | bt_cb(skb)->l2cap.retries > chan->max_tx) { |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1978 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1979 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1980 | l2cap_seq_list_clear(&chan->retrans_list); |
| 1981 | break; |
| 1982 | } |
| 1983 | |
| 1984 | control.reqseq = chan->buffer_seq; |
| 1985 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
| 1986 | control.final = 1; |
| 1987 | else |
| 1988 | control.final = 0; |
| 1989 | |
| 1990 | if (skb_cloned(skb)) { |
| 1991 | /* Cloned sk_buffs are read-only, so we need a |
| 1992 | * writeable copy |
| 1993 | */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 1994 | tx_skb = skb_copy(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1995 | } else { |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 1996 | tx_skb = skb_clone(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1997 | } |
| 1998 | |
| 1999 | if (!tx_skb) { |
| 2000 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2001 | break; |
| 2002 | } |
| 2003 | |
| 2004 | /* Update skb contents */ |
| 2005 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 2006 | put_unaligned_le32(__pack_extended_control(&control), |
| 2007 | tx_skb->data + L2CAP_HDR_SIZE); |
| 2008 | } else { |
| 2009 | put_unaligned_le16(__pack_enhanced_control(&control), |
| 2010 | tx_skb->data + L2CAP_HDR_SIZE); |
| 2011 | } |
| 2012 | |
Lukasz Rymanowski | 13cac15 | 2014-08-14 09:35:34 +0200 | [diff] [blame] | 2013 | /* Update FCS */ |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2014 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Lukasz Rymanowski | 13cac15 | 2014-08-14 09:35:34 +0200 | [diff] [blame] | 2015 | u16 fcs = crc16(0, (u8 *) tx_skb->data, |
| 2016 | tx_skb->len - L2CAP_FCS_SIZE); |
| 2017 | put_unaligned_le16(fcs, skb_tail_pointer(tx_skb) - |
| 2018 | L2CAP_FCS_SIZE); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | l2cap_do_send(chan, tx_skb); |
| 2022 | |
| 2023 | BT_DBG("Resent txseq %d", control.txseq); |
| 2024 | |
| 2025 | chan->last_acked_seq = chan->buffer_seq; |
| 2026 | } |
| 2027 | } |
| 2028 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2029 | static void l2cap_retransmit(struct l2cap_chan *chan, |
| 2030 | struct l2cap_ctrl *control) |
| 2031 | { |
| 2032 | BT_DBG("chan %p, control %p", chan, control); |
| 2033 | |
| 2034 | l2cap_seq_list_append(&chan->retrans_list, control->reqseq); |
| 2035 | l2cap_ertm_resend(chan); |
| 2036 | } |
| 2037 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2038 | static void l2cap_retransmit_all(struct l2cap_chan *chan, |
| 2039 | struct l2cap_ctrl *control) |
| 2040 | { |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2041 | struct sk_buff *skb; |
| 2042 | |
| 2043 | BT_DBG("chan %p, control %p", chan, control); |
| 2044 | |
| 2045 | if (control->poll) |
| 2046 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 2047 | |
| 2048 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2049 | |
| 2050 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 2051 | return; |
| 2052 | |
| 2053 | if (chan->unacked_frames) { |
| 2054 | skb_queue_walk(&chan->tx_q, skb) { |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 2055 | if (bt_cb(skb)->l2cap.txseq == control->reqseq || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2056 | skb == chan->tx_send_head) |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2057 | break; |
| 2058 | } |
| 2059 | |
| 2060 | skb_queue_walk_from(&chan->tx_q, skb) { |
| 2061 | if (skb == chan->tx_send_head) |
| 2062 | break; |
| 2063 | |
| 2064 | l2cap_seq_list_append(&chan->retrans_list, |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 2065 | bt_cb(skb)->l2cap.txseq); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | l2cap_ertm_resend(chan); |
| 2069 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2070 | } |
| 2071 | |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2072 | static void l2cap_send_ack(struct l2cap_chan *chan) |
| 2073 | { |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2074 | struct l2cap_ctrl control; |
| 2075 | u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 2076 | chan->last_acked_seq); |
| 2077 | int threshold; |
| 2078 | |
| 2079 | BT_DBG("chan %p last_acked_seq %d buffer_seq %d", |
| 2080 | chan, chan->last_acked_seq, chan->buffer_seq); |
| 2081 | |
| 2082 | memset(&control, 0, sizeof(control)); |
| 2083 | control.sframe = 1; |
| 2084 | |
| 2085 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
| 2086 | chan->rx_state == L2CAP_RX_STATE_RECV) { |
| 2087 | __clear_ack_timer(chan); |
| 2088 | control.super = L2CAP_SUPER_RNR; |
| 2089 | control.reqseq = chan->buffer_seq; |
| 2090 | l2cap_send_sframe(chan, &control); |
| 2091 | } else { |
| 2092 | if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) { |
| 2093 | l2cap_ertm_send(chan); |
| 2094 | /* If any i-frames were sent, they included an ack */ |
| 2095 | if (chan->buffer_seq == chan->last_acked_seq) |
| 2096 | frames_to_ack = 0; |
| 2097 | } |
| 2098 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2099 | /* Ack now if the window is 3/4ths full. |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2100 | * Calculate without mul or div |
| 2101 | */ |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2102 | threshold = chan->ack_win; |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2103 | threshold += threshold << 1; |
| 2104 | threshold >>= 2; |
| 2105 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2106 | BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack, |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2107 | threshold); |
| 2108 | |
| 2109 | if (frames_to_ack >= threshold) { |
| 2110 | __clear_ack_timer(chan); |
| 2111 | control.super = L2CAP_SUPER_RR; |
| 2112 | control.reqseq = chan->buffer_seq; |
| 2113 | l2cap_send_sframe(chan, &control); |
| 2114 | frames_to_ack = 0; |
| 2115 | } |
| 2116 | |
| 2117 | if (frames_to_ack) |
| 2118 | __set_ack_timer(chan); |
| 2119 | } |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2120 | } |
| 2121 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2122 | static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, |
| 2123 | struct msghdr *msg, int len, |
| 2124 | int count, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | { |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2126 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2127 | struct sk_buff **frag; |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2128 | int sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 2130 | if (!copy_from_iter_full(skb_put(skb, count), count, &msg->msg_iter)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2131 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | |
| 2133 | sent += count; |
| 2134 | len -= count; |
| 2135 | |
| 2136 | /* Continuation fragments (no L2CAP header) */ |
| 2137 | frag = &skb_shinfo(skb)->frag_list; |
| 2138 | while (len) { |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2139 | struct sk_buff *tmp; |
| 2140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | count = min_t(unsigned int, conn->mtu, len); |
| 2142 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2143 | tmp = chan->ops->alloc_skb(chan, 0, count, |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2144 | msg->msg_flags & MSG_DONTWAIT); |
| 2145 | if (IS_ERR(tmp)) |
| 2146 | return PTR_ERR(tmp); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2147 | |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2148 | *frag = tmp; |
| 2149 | |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 2150 | if (!copy_from_iter_full(skb_put(*frag, count), count, |
| 2151 | &msg->msg_iter)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2152 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | |
| 2154 | sent += count; |
| 2155 | len -= count; |
| 2156 | |
Gustavo Padovan | 2d0ed3d | 2012-05-11 13:16:12 -0300 | [diff] [blame] | 2157 | skb->len += (*frag)->len; |
| 2158 | skb->data_len += (*frag)->len; |
| 2159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | frag = &(*frag)->next; |
| 2161 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | |
| 2163 | return sent; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2166 | static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2167 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2168 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2169 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2170 | struct sk_buff *skb; |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2171 | int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2172 | struct l2cap_hdr *lh; |
| 2173 | |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2174 | BT_DBG("chan %p psm 0x%2.2x len %zu", chan, |
| 2175 | __le16_to_cpu(chan->psm), len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2176 | |
| 2177 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2178 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2179 | skb = chan->ops->alloc_skb(chan, hlen, count, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2180 | msg->msg_flags & MSG_DONTWAIT); |
| 2181 | if (IS_ERR(skb)) |
| 2182 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2183 | |
| 2184 | /* Create L2CAP header */ |
| 2185 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2186 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | daf6a78c | 2012-05-03 10:55:52 +0300 | [diff] [blame] | 2187 | lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE); |
Marcel Holtmann | 43b1b8d | 2013-10-12 06:01:26 -0700 | [diff] [blame] | 2188 | put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2189 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2190 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2191 | if (unlikely(err < 0)) { |
| 2192 | kfree_skb(skb); |
| 2193 | return ERR_PTR(err); |
| 2194 | } |
| 2195 | return skb; |
| 2196 | } |
| 2197 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2198 | static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2199 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2200 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2201 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2202 | struct sk_buff *skb; |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2203 | int err, count; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2204 | struct l2cap_hdr *lh; |
| 2205 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2206 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2207 | |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2208 | count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2209 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2210 | skb = chan->ops->alloc_skb(chan, L2CAP_HDR_SIZE, count, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2211 | msg->msg_flags & MSG_DONTWAIT); |
| 2212 | if (IS_ERR(skb)) |
| 2213 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2214 | |
| 2215 | /* Create L2CAP header */ |
| 2216 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2217 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo Padovan | 6ff9b5e | 2012-05-02 11:56:17 -0300 | [diff] [blame] | 2218 | lh->len = cpu_to_le16(len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2219 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2220 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2221 | if (unlikely(err < 0)) { |
| 2222 | kfree_skb(skb); |
| 2223 | return ERR_PTR(err); |
| 2224 | } |
| 2225 | return skb; |
| 2226 | } |
| 2227 | |
Luiz Augusto von Dentz | ab0ff76 | 2011-09-12 20:00:50 +0300 | [diff] [blame] | 2228 | static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2229 | struct msghdr *msg, size_t len, |
| 2230 | u16 sdulen) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2231 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2232 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2233 | struct sk_buff *skb; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2234 | int err, count, hlen; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2235 | struct l2cap_hdr *lh; |
| 2236 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2237 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2238 | |
Gustavo F. Padovan | 0ee0d20 | 2010-05-01 16:15:41 -0300 | [diff] [blame] | 2239 | if (!conn) |
| 2240 | return ERR_PTR(-ENOTCONN); |
| 2241 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2242 | hlen = __ertm_hdr_size(chan); |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2243 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2244 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2245 | hlen += L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2246 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2247 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2248 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2249 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2250 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2251 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2252 | skb = chan->ops->alloc_skb(chan, hlen, count, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2253 | msg->msg_flags & MSG_DONTWAIT); |
| 2254 | if (IS_ERR(skb)) |
| 2255 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2256 | |
| 2257 | /* Create L2CAP header */ |
| 2258 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2259 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2260 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2261 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2262 | /* Control header is populated later */ |
| 2263 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 2264 | put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 2265 | else |
| 2266 | put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2267 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2268 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2269 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2270 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2271 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2272 | if (unlikely(err < 0)) { |
| 2273 | kfree_skb(skb); |
| 2274 | return ERR_PTR(err); |
| 2275 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2276 | |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 2277 | bt_cb(skb)->l2cap.fcs = chan->fcs; |
| 2278 | bt_cb(skb)->l2cap.retries = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2279 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2282 | static int l2cap_segment_sdu(struct l2cap_chan *chan, |
| 2283 | struct sk_buff_head *seg_queue, |
| 2284 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2285 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2286 | struct sk_buff *skb; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2287 | u16 sdu_len; |
| 2288 | size_t pdu_len; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2289 | u8 sar; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2290 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2291 | BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2292 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2293 | /* It is critical that ERTM PDUs fit in a single HCI fragment, |
| 2294 | * so fragmented skbs are not used. The HCI layer's handling |
| 2295 | * of fragmented skbs is not compatible with ERTM's queueing. |
| 2296 | */ |
| 2297 | |
| 2298 | /* PDU size is derived from the HCI MTU */ |
| 2299 | pdu_len = chan->conn->mtu; |
| 2300 | |
Mat Martineau | a549574 | 2012-10-23 15:24:21 -0700 | [diff] [blame] | 2301 | /* Constrain PDU size for BR/EDR connections */ |
| 2302 | if (!chan->hs_hcon) |
| 2303 | pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2304 | |
| 2305 | /* Adjust for largest possible L2CAP overhead. */ |
Gustavo Padovan | 35d401d | 2012-05-25 18:57:05 -0300 | [diff] [blame] | 2306 | if (chan->fcs) |
| 2307 | pdu_len -= L2CAP_FCS_SIZE; |
| 2308 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2309 | pdu_len -= __ertm_hdr_size(chan); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2310 | |
| 2311 | /* Remote device may have requested smaller PDUs */ |
| 2312 | pdu_len = min_t(size_t, pdu_len, chan->remote_mps); |
| 2313 | |
| 2314 | if (len <= pdu_len) { |
| 2315 | sar = L2CAP_SAR_UNSEGMENTED; |
| 2316 | sdu_len = 0; |
| 2317 | pdu_len = len; |
| 2318 | } else { |
| 2319 | sar = L2CAP_SAR_START; |
| 2320 | sdu_len = len; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2321 | } |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2322 | |
| 2323 | while (len > 0) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2324 | skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2325 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2326 | if (IS_ERR(skb)) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2327 | __skb_queue_purge(seg_queue); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2328 | return PTR_ERR(skb); |
| 2329 | } |
| 2330 | |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 2331 | bt_cb(skb)->l2cap.sar = sar; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2332 | __skb_queue_tail(seg_queue, skb); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2333 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2334 | len -= pdu_len; |
Lukasz Rymanowski | 069cb27 | 2014-08-13 16:01:41 +0200 | [diff] [blame] | 2335 | if (sdu_len) |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2336 | sdu_len = 0; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2337 | |
| 2338 | if (len <= pdu_len) { |
| 2339 | sar = L2CAP_SAR_END; |
| 2340 | pdu_len = len; |
| 2341 | } else { |
| 2342 | sar = L2CAP_SAR_CONTINUE; |
| 2343 | } |
| 2344 | } |
| 2345 | |
Gustavo Padovan | f0f6279 | 2012-05-29 13:29:17 -0300 | [diff] [blame] | 2346 | return 0; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2347 | } |
| 2348 | |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2349 | static struct sk_buff *l2cap_create_le_flowctl_pdu(struct l2cap_chan *chan, |
| 2350 | struct msghdr *msg, |
| 2351 | size_t len, u16 sdulen) |
| 2352 | { |
| 2353 | struct l2cap_conn *conn = chan->conn; |
| 2354 | struct sk_buff *skb; |
| 2355 | int err, count, hlen; |
| 2356 | struct l2cap_hdr *lh; |
| 2357 | |
| 2358 | BT_DBG("chan %p len %zu", chan, len); |
| 2359 | |
| 2360 | if (!conn) |
| 2361 | return ERR_PTR(-ENOTCONN); |
| 2362 | |
| 2363 | hlen = L2CAP_HDR_SIZE; |
| 2364 | |
| 2365 | if (sdulen) |
| 2366 | hlen += L2CAP_SDULEN_SIZE; |
| 2367 | |
| 2368 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
| 2369 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2370 | skb = chan->ops->alloc_skb(chan, hlen, count, |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2371 | msg->msg_flags & MSG_DONTWAIT); |
| 2372 | if (IS_ERR(skb)) |
| 2373 | return skb; |
| 2374 | |
| 2375 | /* Create L2CAP header */ |
| 2376 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
| 2377 | lh->cid = cpu_to_le16(chan->dcid); |
| 2378 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
| 2379 | |
| 2380 | if (sdulen) |
| 2381 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
| 2382 | |
| 2383 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
| 2384 | if (unlikely(err < 0)) { |
| 2385 | kfree_skb(skb); |
| 2386 | return ERR_PTR(err); |
| 2387 | } |
| 2388 | |
| 2389 | return skb; |
| 2390 | } |
| 2391 | |
| 2392 | static int l2cap_segment_le_sdu(struct l2cap_chan *chan, |
| 2393 | struct sk_buff_head *seg_queue, |
| 2394 | struct msghdr *msg, size_t len) |
| 2395 | { |
| 2396 | struct sk_buff *skb; |
| 2397 | size_t pdu_len; |
| 2398 | u16 sdu_len; |
| 2399 | |
| 2400 | BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); |
| 2401 | |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2402 | sdu_len = len; |
Johan Hedberg | 72c6fb9 | 2014-08-15 21:06:51 +0300 | [diff] [blame] | 2403 | pdu_len = chan->remote_mps - L2CAP_SDULEN_SIZE; |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2404 | |
| 2405 | while (len > 0) { |
| 2406 | if (len <= pdu_len) |
| 2407 | pdu_len = len; |
| 2408 | |
| 2409 | skb = l2cap_create_le_flowctl_pdu(chan, msg, pdu_len, sdu_len); |
| 2410 | if (IS_ERR(skb)) { |
| 2411 | __skb_queue_purge(seg_queue); |
| 2412 | return PTR_ERR(skb); |
| 2413 | } |
| 2414 | |
| 2415 | __skb_queue_tail(seg_queue, skb); |
| 2416 | |
| 2417 | len -= pdu_len; |
| 2418 | |
| 2419 | if (sdu_len) { |
| 2420 | sdu_len = 0; |
| 2421 | pdu_len += L2CAP_SDULEN_SIZE; |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | return 0; |
| 2426 | } |
| 2427 | |
Luiz Augusto von Dentz | 8a505b7 | 2017-04-11 22:21:01 +0300 | [diff] [blame] | 2428 | static void l2cap_le_flowctl_send(struct l2cap_chan *chan) |
| 2429 | { |
| 2430 | int sent = 0; |
| 2431 | |
| 2432 | BT_DBG("chan %p", chan); |
| 2433 | |
| 2434 | while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { |
| 2435 | l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); |
| 2436 | chan->tx_credits--; |
| 2437 | sent++; |
| 2438 | } |
| 2439 | |
| 2440 | BT_DBG("Sent %d credits %u queued %u", sent, chan->tx_credits, |
| 2441 | skb_queue_len(&chan->tx_q)); |
| 2442 | } |
| 2443 | |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2444 | 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] | 2445 | { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2446 | struct sk_buff *skb; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2447 | int err; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2448 | struct sk_buff_head seg_queue; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2449 | |
Seung-Woo Kim | 31e8ce8 | 2013-11-05 18:46:33 +0900 | [diff] [blame] | 2450 | if (!chan->conn) |
| 2451 | return -ENOTCONN; |
| 2452 | |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2453 | /* Connectionless channel */ |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2454 | if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2455 | skb = l2cap_create_connless_pdu(chan, msg, len); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2456 | if (IS_ERR(skb)) |
| 2457 | return PTR_ERR(skb); |
| 2458 | |
Andrzej Kaczmarek | ede81a2 | 2014-02-25 17:16:22 +0100 | [diff] [blame] | 2459 | /* Channel lock is released before requesting new skb and then |
| 2460 | * reacquired thus we need to recheck channel state. |
| 2461 | */ |
| 2462 | if (chan->state != BT_CONNECTED) { |
| 2463 | kfree_skb(skb); |
| 2464 | return -ENOTCONN; |
| 2465 | } |
| 2466 | |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2467 | l2cap_do_send(chan, skb); |
| 2468 | return len; |
| 2469 | } |
| 2470 | |
| 2471 | switch (chan->mode) { |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 2472 | case L2CAP_MODE_LE_FLOWCTL: |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2473 | /* Check outgoing MTU */ |
| 2474 | if (len > chan->omtu) |
| 2475 | return -EMSGSIZE; |
| 2476 | |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2477 | __skb_queue_head_init(&seg_queue); |
| 2478 | |
| 2479 | err = l2cap_segment_le_sdu(chan, &seg_queue, msg, len); |
| 2480 | |
| 2481 | if (chan->state != BT_CONNECTED) { |
| 2482 | __skb_queue_purge(&seg_queue); |
| 2483 | err = -ENOTCONN; |
| 2484 | } |
| 2485 | |
| 2486 | if (err) |
| 2487 | return err; |
| 2488 | |
| 2489 | skb_queue_splice_tail_init(&seg_queue, &chan->tx_q); |
| 2490 | |
Luiz Augusto von Dentz | 8a505b7 | 2017-04-11 22:21:01 +0300 | [diff] [blame] | 2491 | l2cap_le_flowctl_send(chan); |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2492 | |
| 2493 | if (!chan->tx_credits) |
| 2494 | chan->ops->suspend(chan); |
| 2495 | |
| 2496 | err = len; |
| 2497 | |
| 2498 | break; |
| 2499 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 2500 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2501 | /* Check outgoing MTU */ |
| 2502 | if (len > chan->omtu) |
| 2503 | return -EMSGSIZE; |
| 2504 | |
| 2505 | /* Create a basic PDU */ |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2506 | skb = l2cap_create_basic_pdu(chan, msg, len); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2507 | if (IS_ERR(skb)) |
| 2508 | return PTR_ERR(skb); |
| 2509 | |
Andrzej Kaczmarek | ede81a2 | 2014-02-25 17:16:22 +0100 | [diff] [blame] | 2510 | /* Channel lock is released before requesting new skb and then |
| 2511 | * reacquired thus we need to recheck channel state. |
| 2512 | */ |
| 2513 | if (chan->state != BT_CONNECTED) { |
| 2514 | kfree_skb(skb); |
| 2515 | return -ENOTCONN; |
| 2516 | } |
| 2517 | |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2518 | l2cap_do_send(chan, skb); |
| 2519 | err = len; |
| 2520 | break; |
| 2521 | |
| 2522 | case L2CAP_MODE_ERTM: |
| 2523 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2524 | /* Check outgoing MTU */ |
| 2525 | if (len > chan->omtu) { |
| 2526 | err = -EMSGSIZE; |
| 2527 | break; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2528 | } |
| 2529 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2530 | __skb_queue_head_init(&seg_queue); |
| 2531 | |
| 2532 | /* Do segmentation before calling in to the state machine, |
| 2533 | * since it's possible to block while waiting for memory |
| 2534 | * allocation. |
| 2535 | */ |
| 2536 | err = l2cap_segment_sdu(chan, &seg_queue, msg, len); |
| 2537 | |
| 2538 | /* The channel could have been closed while segmenting, |
| 2539 | * check that it is still connected. |
| 2540 | */ |
| 2541 | if (chan->state != BT_CONNECTED) { |
| 2542 | __skb_queue_purge(&seg_queue); |
| 2543 | err = -ENOTCONN; |
| 2544 | } |
| 2545 | |
| 2546 | if (err) |
| 2547 | break; |
| 2548 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2549 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2550 | l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2551 | else |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2552 | l2cap_streaming_send(chan, &seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2553 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2554 | err = len; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2555 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2556 | /* If the skbs were not queued for sending, they'll still be in |
| 2557 | * seg_queue and need to be purged. |
| 2558 | */ |
| 2559 | __skb_queue_purge(&seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2560 | break; |
| 2561 | |
| 2562 | default: |
| 2563 | BT_DBG("bad state %1.1x", chan->mode); |
| 2564 | err = -EBADFD; |
| 2565 | } |
| 2566 | |
| 2567 | return err; |
| 2568 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 2569 | EXPORT_SYMBOL_GPL(l2cap_chan_send); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2570 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2571 | static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq) |
| 2572 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2573 | struct l2cap_ctrl control; |
| 2574 | u16 seq; |
| 2575 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2576 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2577 | |
| 2578 | memset(&control, 0, sizeof(control)); |
| 2579 | control.sframe = 1; |
| 2580 | control.super = L2CAP_SUPER_SREJ; |
| 2581 | |
| 2582 | for (seq = chan->expected_tx_seq; seq != txseq; |
| 2583 | seq = __next_seq(chan, seq)) { |
| 2584 | if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) { |
| 2585 | control.reqseq = seq; |
| 2586 | l2cap_send_sframe(chan, &control); |
| 2587 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | chan->expected_tx_seq = __next_seq(chan, txseq); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2592 | } |
| 2593 | |
| 2594 | static void l2cap_send_srej_tail(struct l2cap_chan *chan) |
| 2595 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2596 | struct l2cap_ctrl control; |
| 2597 | |
| 2598 | BT_DBG("chan %p", chan); |
| 2599 | |
| 2600 | if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR) |
| 2601 | return; |
| 2602 | |
| 2603 | memset(&control, 0, sizeof(control)); |
| 2604 | control.sframe = 1; |
| 2605 | control.super = L2CAP_SUPER_SREJ; |
| 2606 | control.reqseq = chan->srej_list.tail; |
| 2607 | l2cap_send_sframe(chan, &control); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq) |
| 2611 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2612 | struct l2cap_ctrl control; |
| 2613 | u16 initial_head; |
| 2614 | u16 seq; |
| 2615 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2616 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2617 | |
| 2618 | memset(&control, 0, sizeof(control)); |
| 2619 | control.sframe = 1; |
| 2620 | control.super = L2CAP_SUPER_SREJ; |
| 2621 | |
| 2622 | /* Capture initial list head to allow only one pass through the list. */ |
| 2623 | initial_head = chan->srej_list.head; |
| 2624 | |
| 2625 | do { |
| 2626 | seq = l2cap_seq_list_pop(&chan->srej_list); |
| 2627 | if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR) |
| 2628 | break; |
| 2629 | |
| 2630 | control.reqseq = seq; |
| 2631 | l2cap_send_sframe(chan, &control); |
| 2632 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2633 | } while (chan->srej_list.head != initial_head); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2634 | } |
| 2635 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2636 | static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 2637 | { |
| 2638 | struct sk_buff *acked_skb; |
| 2639 | u16 ackseq; |
| 2640 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2641 | BT_DBG("chan %p, reqseq %u", chan, reqseq); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2642 | |
| 2643 | if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq) |
| 2644 | return; |
| 2645 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2646 | BT_DBG("expected_ack_seq %u, unacked_frames %u", |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2647 | chan->expected_ack_seq, chan->unacked_frames); |
| 2648 | |
| 2649 | for (ackseq = chan->expected_ack_seq; ackseq != reqseq; |
| 2650 | ackseq = __next_seq(chan, ackseq)) { |
| 2651 | |
| 2652 | acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq); |
| 2653 | if (acked_skb) { |
| 2654 | skb_unlink(acked_skb, &chan->tx_q); |
| 2655 | kfree_skb(acked_skb); |
| 2656 | chan->unacked_frames--; |
| 2657 | } |
| 2658 | } |
| 2659 | |
| 2660 | chan->expected_ack_seq = reqseq; |
| 2661 | |
| 2662 | if (chan->unacked_frames == 0) |
| 2663 | __clear_retrans_timer(chan); |
| 2664 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2665 | BT_DBG("unacked_frames %u", chan->unacked_frames); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan) |
| 2669 | { |
| 2670 | BT_DBG("chan %p", chan); |
| 2671 | |
| 2672 | chan->expected_tx_seq = chan->buffer_seq; |
| 2673 | l2cap_seq_list_clear(&chan->srej_list); |
| 2674 | skb_queue_purge(&chan->srej_q); |
| 2675 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 2676 | } |
| 2677 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2678 | static void l2cap_tx_state_xmit(struct l2cap_chan *chan, |
| 2679 | struct l2cap_ctrl *control, |
| 2680 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2681 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2682 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2683 | event); |
| 2684 | |
| 2685 | switch (event) { |
| 2686 | case L2CAP_EV_DATA_REQUEST: |
| 2687 | if (chan->tx_send_head == NULL) |
| 2688 | chan->tx_send_head = skb_peek(skbs); |
| 2689 | |
| 2690 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2691 | l2cap_ertm_send(chan); |
| 2692 | break; |
| 2693 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2694 | BT_DBG("Enter LOCAL_BUSY"); |
| 2695 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2696 | |
| 2697 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2698 | /* The SREJ_SENT state must be aborted if we are to |
| 2699 | * enter the LOCAL_BUSY state. |
| 2700 | */ |
| 2701 | l2cap_abort_rx_srej_sent(chan); |
| 2702 | } |
| 2703 | |
| 2704 | l2cap_send_ack(chan); |
| 2705 | |
| 2706 | break; |
| 2707 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2708 | BT_DBG("Exit LOCAL_BUSY"); |
| 2709 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2710 | |
| 2711 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2712 | struct l2cap_ctrl local_control; |
| 2713 | |
| 2714 | memset(&local_control, 0, sizeof(local_control)); |
| 2715 | local_control.sframe = 1; |
| 2716 | local_control.super = L2CAP_SUPER_RR; |
| 2717 | local_control.poll = 1; |
| 2718 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2719 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2720 | |
| 2721 | chan->retry_count = 1; |
| 2722 | __set_monitor_timer(chan); |
| 2723 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2724 | } |
| 2725 | break; |
| 2726 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2727 | l2cap_process_reqseq(chan, control->reqseq); |
| 2728 | break; |
| 2729 | case L2CAP_EV_EXPLICIT_POLL: |
| 2730 | l2cap_send_rr_or_rnr(chan, 1); |
| 2731 | chan->retry_count = 1; |
| 2732 | __set_monitor_timer(chan); |
| 2733 | __clear_ack_timer(chan); |
| 2734 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2735 | break; |
| 2736 | case L2CAP_EV_RETRANS_TO: |
| 2737 | l2cap_send_rr_or_rnr(chan, 1); |
| 2738 | chan->retry_count = 1; |
| 2739 | __set_monitor_timer(chan); |
| 2740 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2741 | break; |
| 2742 | case L2CAP_EV_RECV_FBIT: |
| 2743 | /* Nothing to process */ |
| 2744 | break; |
| 2745 | default: |
| 2746 | break; |
| 2747 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2748 | } |
| 2749 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2750 | static void l2cap_tx_state_wait_f(struct l2cap_chan *chan, |
| 2751 | struct l2cap_ctrl *control, |
| 2752 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2753 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2754 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2755 | event); |
| 2756 | |
| 2757 | switch (event) { |
| 2758 | case L2CAP_EV_DATA_REQUEST: |
| 2759 | if (chan->tx_send_head == NULL) |
| 2760 | chan->tx_send_head = skb_peek(skbs); |
| 2761 | /* Queue data, but don't send. */ |
| 2762 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2763 | break; |
| 2764 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2765 | BT_DBG("Enter LOCAL_BUSY"); |
| 2766 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2767 | |
| 2768 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2769 | /* The SREJ_SENT state must be aborted if we are to |
| 2770 | * enter the LOCAL_BUSY state. |
| 2771 | */ |
| 2772 | l2cap_abort_rx_srej_sent(chan); |
| 2773 | } |
| 2774 | |
| 2775 | l2cap_send_ack(chan); |
| 2776 | |
| 2777 | break; |
| 2778 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2779 | BT_DBG("Exit LOCAL_BUSY"); |
| 2780 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2781 | |
| 2782 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2783 | struct l2cap_ctrl local_control; |
| 2784 | memset(&local_control, 0, sizeof(local_control)); |
| 2785 | local_control.sframe = 1; |
| 2786 | local_control.super = L2CAP_SUPER_RR; |
| 2787 | local_control.poll = 1; |
| 2788 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2789 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2790 | |
| 2791 | chan->retry_count = 1; |
| 2792 | __set_monitor_timer(chan); |
| 2793 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2794 | } |
| 2795 | break; |
| 2796 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2797 | l2cap_process_reqseq(chan, control->reqseq); |
| 2798 | |
| 2799 | /* Fall through */ |
| 2800 | |
| 2801 | case L2CAP_EV_RECV_FBIT: |
| 2802 | if (control && control->final) { |
| 2803 | __clear_monitor_timer(chan); |
| 2804 | if (chan->unacked_frames > 0) |
| 2805 | __set_retrans_timer(chan); |
| 2806 | chan->retry_count = 0; |
| 2807 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 2808 | BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state); |
| 2809 | } |
| 2810 | break; |
| 2811 | case L2CAP_EV_EXPLICIT_POLL: |
| 2812 | /* Ignore */ |
| 2813 | break; |
| 2814 | case L2CAP_EV_MONITOR_TO: |
| 2815 | if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) { |
| 2816 | l2cap_send_rr_or_rnr(chan, 1); |
| 2817 | __set_monitor_timer(chan); |
| 2818 | chan->retry_count++; |
| 2819 | } else { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 2820 | l2cap_send_disconn_req(chan, ECONNABORTED); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2821 | } |
| 2822 | break; |
| 2823 | default: |
| 2824 | break; |
| 2825 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2826 | } |
| 2827 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2828 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 2829 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2830 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2831 | BT_DBG("chan %p, control %p, skbs %p, event %d, state %d", |
| 2832 | chan, control, skbs, event, chan->tx_state); |
| 2833 | |
| 2834 | switch (chan->tx_state) { |
| 2835 | case L2CAP_TX_STATE_XMIT: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2836 | l2cap_tx_state_xmit(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2837 | break; |
| 2838 | case L2CAP_TX_STATE_WAIT_F: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2839 | l2cap_tx_state_wait_f(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2840 | break; |
| 2841 | default: |
| 2842 | /* Ignore event */ |
| 2843 | break; |
| 2844 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2845 | } |
| 2846 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2847 | static void l2cap_pass_to_tx(struct l2cap_chan *chan, |
| 2848 | struct l2cap_ctrl *control) |
| 2849 | { |
| 2850 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2851 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT); |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2852 | } |
| 2853 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2854 | static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan, |
| 2855 | struct l2cap_ctrl *control) |
| 2856 | { |
| 2857 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2858 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2859 | } |
| 2860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | /* Copy frame to all raw sockets on that connection */ |
| 2862 | static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) |
| 2863 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | struct sk_buff *nskb; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2865 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | |
| 2867 | BT_DBG("conn %p", conn); |
| 2868 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2869 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2870 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2871 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2872 | if (chan->chan_type != L2CAP_CHAN_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | continue; |
| 2874 | |
Gustavo Padovan | 7f5396a | 2013-10-21 18:22:25 -0200 | [diff] [blame] | 2875 | /* Don't send frame to the channel it came from */ |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 2876 | if (bt_cb(skb)->l2cap.chan == chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | continue; |
Gustavo Padovan | 7f5396a | 2013-10-21 18:22:25 -0200 | [diff] [blame] | 2878 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2879 | nskb = skb_clone(skb, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 2880 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | continue; |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 2882 | if (chan->ops->recv(chan, nskb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | kfree_skb(nskb); |
| 2884 | } |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2885 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2886 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | /* ---- L2CAP signalling commands ---- */ |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2890 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code, |
| 2891 | u8 ident, u16 dlen, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | { |
| 2893 | struct sk_buff *skb, **frag; |
| 2894 | struct l2cap_cmd_hdr *cmd; |
| 2895 | struct l2cap_hdr *lh; |
| 2896 | int len, count; |
| 2897 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2898 | BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u", |
| 2899 | conn, code, ident, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2900 | |
Anderson Lizardo | 300b962 | 2013-06-02 16:30:40 -0400 | [diff] [blame] | 2901 | if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE) |
| 2902 | return NULL; |
| 2903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; |
| 2905 | count = min_t(unsigned int, conn->mtu, len); |
| 2906 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2907 | skb = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | if (!skb) |
| 2909 | return NULL; |
| 2910 | |
| 2911 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2912 | lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2913 | |
| 2914 | if (conn->hcon->type == LE_LINK) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 2915 | lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2916 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 2917 | lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | |
| 2919 | cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); |
| 2920 | cmd->code = code; |
| 2921 | cmd->ident = ident; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2922 | cmd->len = cpu_to_le16(dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | |
| 2924 | if (dlen) { |
| 2925 | count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; |
| 2926 | memcpy(skb_put(skb, count), data, count); |
| 2927 | data += count; |
| 2928 | } |
| 2929 | |
| 2930 | len -= skb->len; |
| 2931 | |
| 2932 | /* Continuation fragments (no L2CAP header) */ |
| 2933 | frag = &skb_shinfo(skb)->frag_list; |
| 2934 | while (len) { |
| 2935 | count = min_t(unsigned int, conn->mtu, len); |
| 2936 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2937 | *frag = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2938 | if (!*frag) |
| 2939 | goto fail; |
| 2940 | |
| 2941 | memcpy(skb_put(*frag, count), data, count); |
| 2942 | |
| 2943 | len -= count; |
| 2944 | data += count; |
| 2945 | |
| 2946 | frag = &(*frag)->next; |
| 2947 | } |
| 2948 | |
| 2949 | return skb; |
| 2950 | |
| 2951 | fail: |
| 2952 | kfree_skb(skb); |
| 2953 | return NULL; |
| 2954 | } |
| 2955 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2956 | static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, |
| 2957 | unsigned long *val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | { |
| 2959 | struct l2cap_conf_opt *opt = *ptr; |
| 2960 | int len; |
| 2961 | |
| 2962 | len = L2CAP_CONF_OPT_SIZE + opt->len; |
| 2963 | *ptr += len; |
| 2964 | |
| 2965 | *type = opt->type; |
| 2966 | *olen = opt->len; |
| 2967 | |
| 2968 | switch (opt->len) { |
| 2969 | case 1: |
| 2970 | *val = *((u8 *) opt->val); |
| 2971 | break; |
| 2972 | |
| 2973 | case 2: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2974 | *val = get_unaligned_le16(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | break; |
| 2976 | |
| 2977 | case 4: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2978 | *val = get_unaligned_le32(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | break; |
| 2980 | |
| 2981 | default: |
| 2982 | *val = (unsigned long) opt->val; |
| 2983 | break; |
| 2984 | } |
| 2985 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2986 | 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] | 2987 | return len; |
| 2988 | } |
| 2989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) |
| 2991 | { |
| 2992 | struct l2cap_conf_opt *opt = *ptr; |
| 2993 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2994 | 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] | 2995 | |
| 2996 | opt->type = type; |
| 2997 | opt->len = len; |
| 2998 | |
| 2999 | switch (len) { |
| 3000 | case 1: |
| 3001 | *((u8 *) opt->val) = val; |
| 3002 | break; |
| 3003 | |
| 3004 | case 2: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 3005 | put_unaligned_le16(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | break; |
| 3007 | |
| 3008 | case 4: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 3009 | put_unaligned_le32(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3010 | break; |
| 3011 | |
| 3012 | default: |
| 3013 | memcpy(opt->val, (void *) val, len); |
| 3014 | break; |
| 3015 | } |
| 3016 | |
| 3017 | *ptr += L2CAP_CONF_OPT_SIZE + len; |
| 3018 | } |
| 3019 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3020 | static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) |
| 3021 | { |
| 3022 | struct l2cap_conf_efs efs; |
| 3023 | |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 3024 | switch (chan->mode) { |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3025 | case L2CAP_MODE_ERTM: |
| 3026 | efs.id = chan->local_id; |
| 3027 | efs.stype = chan->local_stype; |
| 3028 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 3029 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3030 | efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); |
| 3031 | efs.flush_to = cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3032 | break; |
| 3033 | |
| 3034 | case L2CAP_MODE_STREAMING: |
| 3035 | efs.id = 1; |
| 3036 | efs.stype = L2CAP_SERV_BESTEFFORT; |
| 3037 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 3038 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
| 3039 | efs.acc_lat = 0; |
| 3040 | efs.flush_to = 0; |
| 3041 | break; |
| 3042 | |
| 3043 | default: |
| 3044 | return; |
| 3045 | } |
| 3046 | |
| 3047 | l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 3048 | (unsigned long) &efs); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3049 | } |
| 3050 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3051 | static void l2cap_ack_timeout(struct work_struct *work) |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3052 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3053 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3054 | ack_timer.work); |
| 3055 | u16 frames_to_ack; |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3056 | |
Gustavo F. Padovan | 2fb9b3d | 2011-12-22 16:56:05 -0200 | [diff] [blame] | 3057 | BT_DBG("chan %p", chan); |
| 3058 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3059 | l2cap_chan_lock(chan); |
| 3060 | |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3061 | frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 3062 | chan->last_acked_seq); |
| 3063 | |
| 3064 | if (frames_to_ack) |
| 3065 | l2cap_send_rr_or_rnr(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3066 | |
| 3067 | l2cap_chan_unlock(chan); |
Szymon Janc | 09bfb2e | 2012-01-11 10:59:49 +0100 | [diff] [blame] | 3068 | l2cap_chan_put(chan); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3069 | } |
| 3070 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 3071 | int l2cap_ertm_init(struct l2cap_chan *chan) |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3072 | { |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3073 | int err; |
| 3074 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3075 | chan->next_tx_seq = 0; |
| 3076 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3077 | chan->expected_ack_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3078 | chan->unacked_frames = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3079 | chan->buffer_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3080 | chan->frames_sent = 0; |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3081 | chan->last_acked_seq = 0; |
| 3082 | chan->sdu = NULL; |
| 3083 | chan->sdu_last_frag = NULL; |
| 3084 | chan->sdu_len = 0; |
| 3085 | |
Mat Martineau | d34c34f | 2012-05-14 14:49:27 -0700 | [diff] [blame] | 3086 | skb_queue_head_init(&chan->tx_q); |
| 3087 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3088 | chan->local_amp_id = AMP_ID_BREDR; |
| 3089 | chan->move_id = AMP_ID_BREDR; |
Mat Martineau | 0833328 | 2012-10-23 15:24:06 -0700 | [diff] [blame] | 3090 | chan->move_state = L2CAP_MOVE_STABLE; |
| 3091 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 3092 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3093 | if (chan->mode != L2CAP_MODE_ERTM) |
| 3094 | return 0; |
| 3095 | |
| 3096 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 3097 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3098 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3099 | INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); |
| 3100 | INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); |
| 3101 | INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3102 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3103 | skb_queue_head_init(&chan->srej_q); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3104 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3105 | err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win); |
| 3106 | if (err < 0) |
| 3107 | return err; |
| 3108 | |
Mat Martineau | 9dc9aff | 2012-05-17 16:20:14 -0700 | [diff] [blame] | 3109 | err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win); |
| 3110 | if (err < 0) |
| 3111 | l2cap_seq_list_free(&chan->srej_list); |
| 3112 | |
| 3113 | return err; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3114 | } |
| 3115 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3116 | static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) |
| 3117 | { |
| 3118 | switch (mode) { |
| 3119 | case L2CAP_MODE_STREAMING: |
| 3120 | case L2CAP_MODE_ERTM: |
| 3121 | if (l2cap_mode_supported(mode, remote_feat_mask)) |
| 3122 | return mode; |
| 3123 | /* fall through */ |
| 3124 | default: |
| 3125 | return L2CAP_MODE_BASIC; |
| 3126 | } |
| 3127 | } |
| 3128 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3129 | static inline bool __l2cap_ews_supported(struct l2cap_conn *conn) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3130 | { |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 3131 | return ((conn->local_fixed_chan & L2CAP_FC_A2MP) && |
| 3132 | (conn->feat_mask & L2CAP_FEAT_EXT_WINDOW)); |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3133 | } |
| 3134 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3135 | static inline bool __l2cap_efs_supported(struct l2cap_conn *conn) |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3136 | { |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 3137 | return ((conn->local_fixed_chan & L2CAP_FC_A2MP) && |
| 3138 | (conn->feat_mask & L2CAP_FEAT_EXT_FLOW)); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3139 | } |
| 3140 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3141 | static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan, |
| 3142 | struct l2cap_conf_rfc *rfc) |
| 3143 | { |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3144 | if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) { |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3145 | u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to; |
| 3146 | |
| 3147 | /* Class 1 devices have must have ERTM timeouts |
| 3148 | * exceeding the Link Supervision Timeout. The |
| 3149 | * default Link Supervision Timeout for AMP |
| 3150 | * controllers is 10 seconds. |
| 3151 | * |
| 3152 | * Class 1 devices use 0xffffffff for their |
| 3153 | * best-effort flush timeout, so the clamping logic |
| 3154 | * will result in a timeout that meets the above |
| 3155 | * requirement. ERTM timeouts are 16-bit values, so |
| 3156 | * the maximum timeout is 65.535 seconds. |
| 3157 | */ |
| 3158 | |
| 3159 | /* Convert timeout to milliseconds and round */ |
| 3160 | ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000); |
| 3161 | |
| 3162 | /* This is the recommended formula for class 2 devices |
| 3163 | * that start ERTM timers when packets are sent to the |
| 3164 | * controller. |
| 3165 | */ |
| 3166 | ertm_to = 3 * ertm_to + 500; |
| 3167 | |
| 3168 | if (ertm_to > 0xffff) |
| 3169 | ertm_to = 0xffff; |
| 3170 | |
| 3171 | rfc->retrans_timeout = cpu_to_le16((u16) ertm_to); |
| 3172 | rfc->monitor_timeout = rfc->retrans_timeout; |
| 3173 | } else { |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3174 | rfc->retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); |
| 3175 | rfc->monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3176 | } |
| 3177 | } |
| 3178 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3179 | static inline void l2cap_txwin_setup(struct l2cap_chan *chan) |
| 3180 | { |
| 3181 | if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3182 | __l2cap_ews_supported(chan->conn)) { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3183 | /* use extended control field */ |
| 3184 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3185 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
| 3186 | } else { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3187 | chan->tx_win = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3188 | L2CAP_DEFAULT_TX_WINDOW); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3189 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
| 3190 | } |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3191 | chan->ack_win = chan->tx_win; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3192 | } |
| 3193 | |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3194 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3196 | struct l2cap_conf_req *req = data; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3197 | struct l2cap_conf_rfc rfc = { .mode = chan->mode }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | void *ptr = req->data; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3199 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3201 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3202 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3203 | if (chan->num_conf_req || chan->num_conf_rsp) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3204 | goto done; |
| 3205 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3206 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3207 | case L2CAP_MODE_STREAMING: |
| 3208 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3209 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3210 | break; |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3211 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3212 | if (__l2cap_efs_supported(chan->conn)) |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3213 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3214 | |
Gustavo F. Padovan | 2ba13ed | 2010-06-09 16:39:05 -0300 | [diff] [blame] | 3215 | /* fall through */ |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3216 | default: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3217 | chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3218 | break; |
| 3219 | } |
| 3220 | |
| 3221 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3222 | if (chan->imtu != L2CAP_DEFAULT_MTU) |
| 3223 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | 7990681c | 2011-01-24 16:01:43 -0200 | [diff] [blame] | 3224 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3225 | switch (chan->mode) { |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3226 | case L2CAP_MODE_BASIC: |
Marcel Holtmann | 6fea7ad | 2014-07-09 11:53:35 +0200 | [diff] [blame] | 3227 | if (disable_ertm) |
| 3228 | break; |
| 3229 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3230 | if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3231 | !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3232 | break; |
| 3233 | |
Gustavo F. Padovan | 6254775 | 2010-06-08 20:05:31 -0300 | [diff] [blame] | 3234 | rfc.mode = L2CAP_MODE_BASIC; |
| 3235 | rfc.txwin_size = 0; |
| 3236 | rfc.max_transmit = 0; |
| 3237 | rfc.retrans_timeout = 0; |
| 3238 | rfc.monitor_timeout = 0; |
| 3239 | rfc.max_pdu_size = 0; |
| 3240 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3241 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3242 | (unsigned long) &rfc); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3243 | break; |
| 3244 | |
| 3245 | case L2CAP_MODE_ERTM: |
| 3246 | rfc.mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3247 | rfc.max_transmit = chan->max_tx; |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3248 | |
| 3249 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3250 | |
| 3251 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3252 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3253 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3254 | rfc.max_pdu_size = cpu_to_le16(size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3255 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3256 | l2cap_txwin_setup(chan); |
| 3257 | |
| 3258 | rfc.txwin_size = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3259 | L2CAP_DEFAULT_TX_WINDOW); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3260 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3261 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3262 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3263 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3264 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3265 | l2cap_add_opt_efs(&ptr, chan); |
| 3266 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3267 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3268 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3269 | chan->tx_win); |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3270 | |
| 3271 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3272 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3273 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3274 | chan->fcs = L2CAP_FCS_NONE; |
| 3275 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3276 | chan->fcs); |
| 3277 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3278 | break; |
| 3279 | |
| 3280 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 273759e | 2012-05-17 20:53:53 -0700 | [diff] [blame] | 3281 | l2cap_txwin_setup(chan); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3282 | rfc.mode = L2CAP_MODE_STREAMING; |
| 3283 | rfc.txwin_size = 0; |
| 3284 | rfc.max_transmit = 0; |
| 3285 | rfc.retrans_timeout = 0; |
| 3286 | rfc.monitor_timeout = 0; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3287 | |
| 3288 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3289 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3290 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3291 | rfc.max_pdu_size = cpu_to_le16(size); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3292 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3293 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3294 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3295 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3296 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3297 | l2cap_add_opt_efs(&ptr, chan); |
| 3298 | |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3299 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3300 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3301 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3302 | chan->fcs = L2CAP_FCS_NONE; |
| 3303 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3304 | chan->fcs); |
| 3305 | } |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3306 | break; |
| 3307 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3309 | req->dcid = cpu_to_le16(chan->dcid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3310 | req->flags = cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3311 | |
| 3312 | return ptr - data; |
| 3313 | } |
| 3314 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3315 | static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3317 | struct l2cap_conf_rsp *rsp = data; |
| 3318 | void *ptr = rsp->data; |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3319 | void *req = chan->conf_req; |
| 3320 | int len = chan->conf_len; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3321 | int type, hint, olen; |
| 3322 | unsigned long val; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3323 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3324 | struct l2cap_conf_efs efs; |
| 3325 | u8 remote_efs = 0; |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3326 | u16 mtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3327 | u16 result = L2CAP_CONF_SUCCESS; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3328 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3330 | BT_DBG("chan %p", chan); |
Marcel Holtmann | 820ae1b | 2006-11-18 22:15:00 +0100 | [diff] [blame] | 3331 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3332 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3333 | len -= l2cap_get_conf_opt(&req, &type, &olen, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | |
Gustavo F. Padovan | 589d274 | 2009-04-20 01:31:07 -0300 | [diff] [blame] | 3335 | hint = type & L2CAP_CONF_HINT; |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 3336 | type &= L2CAP_CONF_MASK; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3337 | |
| 3338 | switch (type) { |
| 3339 | case L2CAP_CONF_MTU: |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3340 | mtu = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3341 | break; |
| 3342 | |
| 3343 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3344 | chan->flush_to = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3345 | break; |
| 3346 | |
| 3347 | case L2CAP_CONF_QOS: |
| 3348 | break; |
| 3349 | |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3350 | case L2CAP_CONF_RFC: |
| 3351 | if (olen == sizeof(rfc)) |
| 3352 | memcpy(&rfc, (void *) val, olen); |
| 3353 | break; |
| 3354 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3355 | case L2CAP_CONF_FCS: |
| 3356 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3357 | set_bit(CONF_RECV_NO_FCS, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3358 | break; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3359 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3360 | case L2CAP_CONF_EFS: |
| 3361 | remote_efs = 1; |
| 3362 | if (olen == sizeof(efs)) |
| 3363 | memcpy(&efs, (void *) val, olen); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3364 | break; |
| 3365 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3366 | case L2CAP_CONF_EWS: |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 3367 | if (!(chan->conn->local_fixed_chan & L2CAP_FC_A2MP)) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3368 | return -ECONNREFUSED; |
| 3369 | |
| 3370 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
| 3371 | set_bit(CONF_EWS_RECV, &chan->conf_state); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3372 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3373 | chan->remote_tx_win = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3374 | break; |
| 3375 | |
| 3376 | default: |
| 3377 | if (hint) |
| 3378 | break; |
| 3379 | |
| 3380 | result = L2CAP_CONF_UNKNOWN; |
| 3381 | *((u8 *) ptr++) = type; |
| 3382 | break; |
| 3383 | } |
| 3384 | } |
| 3385 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3386 | if (chan->num_conf_rsp || chan->num_conf_req > 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3387 | goto done; |
| 3388 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3389 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3390 | case L2CAP_MODE_STREAMING: |
| 3391 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3392 | if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3393 | chan->mode = l2cap_select_mode(rfc.mode, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3394 | chan->conn->feat_mask); |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3395 | break; |
| 3396 | } |
| 3397 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3398 | if (remote_efs) { |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3399 | if (__l2cap_efs_supported(chan->conn)) |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3400 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3401 | else |
| 3402 | return -ECONNREFUSED; |
| 3403 | } |
| 3404 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3405 | if (chan->mode != rfc.mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3406 | return -ECONNREFUSED; |
Gustavo F. Padovan | 742e519 | 2010-06-08 19:09:48 -0300 | [diff] [blame] | 3407 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3408 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3409 | } |
| 3410 | |
| 3411 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3412 | if (chan->mode != rfc.mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3413 | result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3414 | rfc.mode = chan->mode; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3415 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3416 | if (chan->num_conf_rsp == 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3417 | return -ECONNREFUSED; |
| 3418 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3419 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3420 | (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3421 | } |
| 3422 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3423 | if (result == L2CAP_CONF_SUCCESS) { |
| 3424 | /* Configure output options and let the other side know |
| 3425 | * which ones we don't like. */ |
| 3426 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3427 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
| 3428 | result = L2CAP_CONF_UNACCEPT; |
| 3429 | else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3430 | chan->omtu = mtu; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3431 | set_bit(CONF_MTU_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3432 | } |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3433 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3434 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3435 | if (remote_efs) { |
| 3436 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3437 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3438 | efs.stype != chan->local_stype) { |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3439 | |
| 3440 | result = L2CAP_CONF_UNACCEPT; |
| 3441 | |
| 3442 | if (chan->num_conf_req >= 1) |
| 3443 | return -ECONNREFUSED; |
| 3444 | |
| 3445 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3446 | sizeof(efs), |
| 3447 | (unsigned long) &efs); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3448 | } else { |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3449 | /* Send PENDING Conf Rsp */ |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3450 | result = L2CAP_CONF_PENDING; |
| 3451 | set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3452 | } |
| 3453 | } |
| 3454 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3455 | switch (rfc.mode) { |
| 3456 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3457 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3458 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3459 | break; |
| 3460 | |
| 3461 | case L2CAP_MODE_ERTM: |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3462 | if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) |
| 3463 | chan->remote_tx_win = rfc.txwin_size; |
| 3464 | else |
| 3465 | rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; |
| 3466 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 3467 | chan->remote_max_tx = rfc.max_transmit; |
Mat Martineau | 86b1b26 | 2010-08-05 15:54:22 -0700 | [diff] [blame] | 3468 | |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3469 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3470 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3471 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3472 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3473 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3474 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3475 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3476 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3477 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3478 | |
| 3479 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3480 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3481 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3482 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3483 | chan->remote_id = efs.id; |
| 3484 | chan->remote_stype = efs.stype; |
| 3485 | chan->remote_msdu = le16_to_cpu(efs.msdu); |
| 3486 | chan->remote_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3487 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3488 | chan->remote_acc_lat = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3489 | le32_to_cpu(efs.acc_lat); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3490 | chan->remote_sdu_itime = |
| 3491 | le32_to_cpu(efs.sdu_itime); |
| 3492 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3493 | sizeof(efs), |
| 3494 | (unsigned long) &efs); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3495 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3496 | break; |
| 3497 | |
| 3498 | case L2CAP_MODE_STREAMING: |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3499 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3500 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3501 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3502 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3503 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3504 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3505 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3506 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3507 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3508 | (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3509 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3510 | break; |
| 3511 | |
| 3512 | default: |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3513 | result = L2CAP_CONF_UNACCEPT; |
| 3514 | |
| 3515 | memset(&rfc, 0, sizeof(rfc)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3516 | rfc.mode = chan->mode; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3517 | } |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3518 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3519 | if (result == L2CAP_CONF_SUCCESS) |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3520 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3521 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3522 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3523 | rsp->result = cpu_to_le16(result); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3524 | rsp->flags = cpu_to_le16(0); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3525 | |
| 3526 | return ptr - data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3527 | } |
| 3528 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3529 | static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, |
| 3530 | void *data, u16 *result) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3531 | { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3532 | struct l2cap_conf_req *req = data; |
| 3533 | void *ptr = req->data; |
| 3534 | int type, olen; |
| 3535 | unsigned long val; |
Mat Martineau | 36e999a | 2011-12-08 17:23:21 -0800 | [diff] [blame] | 3536 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3537 | struct l2cap_conf_efs efs; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3538 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3539 | 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] | 3540 | |
| 3541 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3542 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3543 | |
| 3544 | switch (type) { |
| 3545 | case L2CAP_CONF_MTU: |
| 3546 | if (val < L2CAP_DEFAULT_MIN_MTU) { |
| 3547 | *result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3548 | chan->imtu = L2CAP_DEFAULT_MIN_MTU; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3549 | } else |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3550 | chan->imtu = val; |
| 3551 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3552 | break; |
| 3553 | |
| 3554 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3555 | chan->flush_to = val; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3556 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3557 | 2, chan->flush_to); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3558 | break; |
| 3559 | |
| 3560 | case L2CAP_CONF_RFC: |
| 3561 | if (olen == sizeof(rfc)) |
| 3562 | memcpy(&rfc, (void *)val, olen); |
| 3563 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3564 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3565 | rfc.mode != chan->mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3566 | return -ECONNREFUSED; |
| 3567 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3568 | chan->fcs = 0; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3569 | |
| 3570 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3571 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3572 | break; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3573 | |
| 3574 | case L2CAP_CONF_EWS: |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3575 | chan->ack_win = min_t(u16, val, chan->ack_win); |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3576 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3577 | chan->tx_win); |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3578 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3579 | |
| 3580 | case L2CAP_CONF_EFS: |
| 3581 | if (olen == sizeof(efs)) |
| 3582 | memcpy(&efs, (void *)val, olen); |
| 3583 | |
| 3584 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3585 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3586 | efs.stype != chan->local_stype) |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3587 | return -ECONNREFUSED; |
| 3588 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3589 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs), |
| 3590 | (unsigned long) &efs); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3591 | break; |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3592 | |
| 3593 | case L2CAP_CONF_FCS: |
| 3594 | if (*result == L2CAP_CONF_PENDING) |
| 3595 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3596 | set_bit(CONF_RECV_NO_FCS, |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3597 | &chan->conf_state); |
| 3598 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3599 | } |
| 3600 | } |
| 3601 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3602 | if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3603 | return -ECONNREFUSED; |
| 3604 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3605 | chan->mode = rfc.mode; |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3606 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3607 | if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3608 | switch (rfc.mode) { |
| 3609 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3610 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3611 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 3612 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3613 | if (!test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3614 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3615 | rfc.txwin_size); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3616 | |
| 3617 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3618 | chan->local_msdu = le16_to_cpu(efs.msdu); |
| 3619 | chan->local_sdu_itime = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3620 | le32_to_cpu(efs.sdu_itime); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3621 | chan->local_acc_lat = le32_to_cpu(efs.acc_lat); |
| 3622 | chan->local_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3623 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3624 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3625 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3626 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3627 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3628 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3629 | } |
| 3630 | } |
| 3631 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3632 | req->dcid = cpu_to_le16(chan->dcid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3633 | req->flags = cpu_to_le16(0); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3634 | |
| 3635 | return ptr - data; |
| 3636 | } |
| 3637 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3638 | static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, |
| 3639 | u16 result, u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | { |
| 3641 | struct l2cap_conf_rsp *rsp = data; |
| 3642 | void *ptr = rsp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3643 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3644 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3646 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3647 | rsp->result = cpu_to_le16(result); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3648 | rsp->flags = cpu_to_le16(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | |
| 3650 | return ptr - data; |
| 3651 | } |
| 3652 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 3653 | void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan) |
| 3654 | { |
| 3655 | struct l2cap_le_conn_rsp rsp; |
| 3656 | struct l2cap_conn *conn = chan->conn; |
| 3657 | |
| 3658 | BT_DBG("chan %p", chan); |
| 3659 | |
| 3660 | rsp.dcid = cpu_to_le16(chan->scid); |
| 3661 | rsp.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 3662 | rsp.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 3663 | rsp.credits = cpu_to_le16(chan->rx_credits); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3664 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 3665 | |
| 3666 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), |
| 3667 | &rsp); |
| 3668 | } |
| 3669 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3670 | void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3671 | { |
| 3672 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3673 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3674 | u8 buf[128]; |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3675 | u8 rsp_code; |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3676 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3677 | rsp.scid = cpu_to_le16(chan->dcid); |
| 3678 | rsp.dcid = cpu_to_le16(chan->scid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3679 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 3680 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3681 | |
| 3682 | if (chan->hs_hcon) |
| 3683 | rsp_code = L2CAP_CREATE_CHAN_RSP; |
| 3684 | else |
| 3685 | rsp_code = L2CAP_CONN_RSP; |
| 3686 | |
| 3687 | BT_DBG("chan %p rsp_code %u", chan, rsp_code); |
| 3688 | |
| 3689 | l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3690 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3691 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3692 | return; |
| 3693 | |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3694 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3695 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3696 | chan->num_conf_req++; |
| 3697 | } |
| 3698 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3699 | 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] | 3700 | { |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3701 | int type, olen; |
| 3702 | unsigned long val; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3703 | /* Use sane default values in case a misbehaving remote device |
| 3704 | * did not send an RFC or extended window size option. |
| 3705 | */ |
| 3706 | u16 txwin_ext = chan->ack_win; |
| 3707 | struct l2cap_conf_rfc rfc = { |
| 3708 | .mode = chan->mode, |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3709 | .retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO), |
| 3710 | .monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO), |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3711 | .max_pdu_size = cpu_to_le16(chan->imtu), |
| 3712 | .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW), |
| 3713 | }; |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3714 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3715 | BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3716 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3717 | if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3718 | return; |
| 3719 | |
| 3720 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3721 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3722 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3723 | switch (type) { |
| 3724 | case L2CAP_CONF_RFC: |
| 3725 | if (olen == sizeof(rfc)) |
| 3726 | memcpy(&rfc, (void *)val, olen); |
Szymon Janc | 8f321f8 | 2012-06-08 11:33:33 +0200 | [diff] [blame] | 3727 | break; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3728 | case L2CAP_CONF_EWS: |
| 3729 | txwin_ext = val; |
| 3730 | break; |
| 3731 | } |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3732 | } |
| 3733 | |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3734 | switch (rfc.mode) { |
| 3735 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3736 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3737 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3738 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
| 3739 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3740 | chan->ack_win = min_t(u16, chan->ack_win, txwin_ext); |
| 3741 | else |
| 3742 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3743 | rfc.txwin_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3744 | break; |
| 3745 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3746 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3747 | } |
| 3748 | } |
| 3749 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3750 | static inline int l2cap_command_rej(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3751 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3752 | u8 *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3753 | { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3754 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3755 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3756 | if (cmd_len < sizeof(*rej)) |
| 3757 | return -EPROTO; |
| 3758 | |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3759 | if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3760 | return 0; |
| 3761 | |
| 3762 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3763 | cmd->ident == conn->info_ident) { |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 3764 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3765 | |
| 3766 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3767 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3768 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3769 | l2cap_conn_start(conn); |
| 3770 | } |
| 3771 | |
| 3772 | return 0; |
| 3773 | } |
| 3774 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3775 | static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn, |
| 3776 | struct l2cap_cmd_hdr *cmd, |
| 3777 | u8 *data, u8 rsp_code, u8 amp_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3778 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3779 | struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; |
| 3780 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3781 | struct l2cap_chan *chan = NULL, *pchan; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3782 | int result, status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3783 | |
| 3784 | u16 dcid = 0, scid = __le16_to_cpu(req->scid); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3785 | __le16 psm = req->psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3786 | |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 3787 | 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] | 3788 | |
| 3789 | /* Check if we have socket listening on psm */ |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 3790 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src, |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 3791 | &conn->hcon->dst, ACL_LINK); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3792 | if (!pchan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3793 | result = L2CAP_CR_BAD_PSM; |
| 3794 | goto sendresp; |
| 3795 | } |
| 3796 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3797 | mutex_lock(&conn->chan_lock); |
Gustavo Padovan | 8ffb929 | 2013-10-21 14:21:41 -0200 | [diff] [blame] | 3798 | l2cap_chan_lock(pchan); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 3799 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3800 | /* Check if the ACL is secure enough (if not SDP) */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3801 | if (psm != cpu_to_le16(L2CAP_PSM_SDP) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3802 | !hci_conn_check_link_mode(conn->hcon)) { |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 3803 | conn->disc_reason = HCI_ERROR_AUTH_FAILURE; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3804 | result = L2CAP_CR_SEC_BLOCK; |
| 3805 | goto response; |
| 3806 | } |
| 3807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3808 | result = L2CAP_CR_NO_MEM; |
| 3809 | |
Gustavo Padovan | 2dfa100 | 2012-05-27 22:27:58 -0300 | [diff] [blame] | 3810 | /* Check if we already have channel with that dcid */ |
| 3811 | if (__l2cap_get_chan_by_dcid(conn, scid)) |
| 3812 | goto response; |
| 3813 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 3814 | chan = pchan->ops->new_connection(pchan); |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 3815 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3816 | goto response; |
| 3817 | |
Syam Sidhardhan | 330b6c1 | 2013-08-06 01:59:12 +0900 | [diff] [blame] | 3818 | /* For certain devices (ex: HID mouse), support for authentication, |
| 3819 | * pairing and bonding is optional. For such devices, inorder to avoid |
| 3820 | * the ACL alive for too long after L2CAP disconnection, reset the ACL |
| 3821 | * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect. |
| 3822 | */ |
| 3823 | conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT; |
| 3824 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 3825 | bacpy(&chan->src, &conn->hcon->src); |
| 3826 | bacpy(&chan->dst, &conn->hcon->dst); |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 3827 | chan->src_type = bdaddr_src_type(conn->hcon); |
| 3828 | chan->dst_type = bdaddr_dst_type(conn->hcon); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3829 | chan->psm = psm; |
| 3830 | chan->dcid = scid; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3831 | chan->local_amp_id = amp_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3832 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3833 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3834 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3835 | dcid = chan->scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3836 | |
Gustavo Padovan | 8d836d7 | 2013-10-15 19:24:47 -0300 | [diff] [blame] | 3837 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3839 | chan->ident = cmd->ident; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3840 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3841 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 3842 | if (l2cap_chan_check_security(chan, false)) { |
Marcel Holtmann | bdc2578 | 2013-10-14 02:45:34 -0700 | [diff] [blame] | 3843 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3844 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3845 | result = L2CAP_CR_PEND; |
| 3846 | status = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 3847 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3848 | } else { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3849 | /* Force pending result for AMP controllers. |
| 3850 | * The connection will succeed after the |
| 3851 | * physical link is up. |
| 3852 | */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3853 | if (amp_id == AMP_ID_BREDR) { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3854 | l2cap_state_change(chan, BT_CONFIG); |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3855 | result = L2CAP_CR_SUCCESS; |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3856 | } else { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3857 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3858 | result = L2CAP_CR_PEND; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3859 | } |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3860 | status = L2CAP_CS_NO_INFO; |
| 3861 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3862 | } else { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3863 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3864 | result = L2CAP_CR_PEND; |
| 3865 | status = L2CAP_CS_AUTHEN_PEND; |
| 3866 | } |
| 3867 | } else { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3868 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3869 | result = L2CAP_CR_PEND; |
| 3870 | status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3871 | } |
| 3872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | response: |
Gustavo Padovan | 8ffb929 | 2013-10-21 14:21:41 -0200 | [diff] [blame] | 3874 | l2cap_chan_unlock(pchan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3875 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 3876 | l2cap_chan_put(pchan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3877 | |
| 3878 | sendresp: |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3879 | rsp.scid = cpu_to_le16(scid); |
| 3880 | rsp.dcid = cpu_to_le16(dcid); |
| 3881 | rsp.result = cpu_to_le16(result); |
| 3882 | rsp.status = cpu_to_le16(status); |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3883 | l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3884 | |
| 3885 | if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { |
| 3886 | struct l2cap_info_req info; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3887 | info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3888 | |
| 3889 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 3890 | conn->info_ident = l2cap_get_ident(conn); |
| 3891 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 3892 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3893 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3894 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 3895 | sizeof(info), &info); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3896 | } |
| 3897 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3898 | if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3899 | result == L2CAP_CR_SUCCESS) { |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3900 | u8 buf[128]; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3901 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3902 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3903 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3904 | chan->num_conf_req++; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3905 | } |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3906 | |
| 3907 | return chan; |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3908 | } |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3909 | |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3910 | static int l2cap_connect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3911 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3912 | { |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3913 | struct hci_dev *hdev = conn->hcon->hdev; |
| 3914 | struct hci_conn *hcon = conn->hcon; |
| 3915 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3916 | if (cmd_len < sizeof(struct l2cap_conn_req)) |
| 3917 | return -EPROTO; |
| 3918 | |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3919 | hci_dev_lock(hdev); |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3920 | if (hci_dev_test_flag(hdev, HCI_MGMT) && |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3921 | !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags)) |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 3922 | mgmt_device_connected(hdev, hcon, 0, NULL, 0); |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3923 | hci_dev_unlock(hdev); |
| 3924 | |
Gustavo Padovan | 300229f | 2012-10-12 19:40:40 +0800 | [diff] [blame] | 3925 | l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3926 | return 0; |
| 3927 | } |
| 3928 | |
Mat Martineau | 5909cf3 | 2012-10-23 15:24:08 -0700 | [diff] [blame] | 3929 | static int l2cap_connect_create_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3930 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3931 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3932 | { |
| 3933 | struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; |
| 3934 | u16 scid, dcid, result, status; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3935 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3936 | u8 req[128]; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3937 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3938 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3939 | if (cmd_len < sizeof(*rsp)) |
| 3940 | return -EPROTO; |
| 3941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | scid = __le16_to_cpu(rsp->scid); |
| 3943 | dcid = __le16_to_cpu(rsp->dcid); |
| 3944 | result = __le16_to_cpu(rsp->result); |
| 3945 | status = __le16_to_cpu(rsp->status); |
| 3946 | |
Andrei Emeltchenko | 1b009c9 | 2012-02-21 12:54:54 +0200 | [diff] [blame] | 3947 | 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] | 3948 | dcid, scid, result, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3949 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3950 | mutex_lock(&conn->chan_lock); |
| 3951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3952 | if (scid) { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3953 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 3954 | if (!chan) { |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 3955 | err = -EBADSLT; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3956 | goto unlock; |
| 3957 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3958 | } else { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3959 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 3960 | if (!chan) { |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 3961 | err = -EBADSLT; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3962 | goto unlock; |
| 3963 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3964 | } |
| 3965 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3966 | err = 0; |
| 3967 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3968 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3969 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3970 | switch (result) { |
| 3971 | case L2CAP_CR_SUCCESS: |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 3972 | l2cap_state_change(chan, BT_CONFIG); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3973 | chan->ident = 0; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3974 | chan->dcid = dcid; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3975 | clear_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 3976 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3977 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3978 | break; |
| 3979 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3980 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3981 | l2cap_build_conf_req(chan, req), req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3982 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3983 | break; |
| 3984 | |
| 3985 | case L2CAP_CR_PEND: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3986 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3987 | break; |
| 3988 | |
| 3989 | default: |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3990 | l2cap_chan_del(chan, ECONNREFUSED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | break; |
| 3992 | } |
| 3993 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3994 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3995 | |
| 3996 | unlock: |
| 3997 | mutex_unlock(&conn->chan_lock); |
| 3998 | |
| 3999 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4000 | } |
| 4001 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4002 | static inline void set_default_fcs(struct l2cap_chan *chan) |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 4003 | { |
| 4004 | /* FCS is enabled only in ERTM or streaming mode, if one or both |
| 4005 | * sides request it. |
| 4006 | */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4007 | if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4008 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 4009 | else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4010 | chan->fcs = L2CAP_FCS_CRC16; |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 4011 | } |
| 4012 | |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 4013 | static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data, |
| 4014 | u8 ident, u16 flags) |
| 4015 | { |
| 4016 | struct l2cap_conn *conn = chan->conn; |
| 4017 | |
| 4018 | BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident, |
| 4019 | flags); |
| 4020 | |
| 4021 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
| 4022 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
| 4023 | |
| 4024 | l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP, |
| 4025 | l2cap_build_conf_rsp(chan, data, |
| 4026 | L2CAP_CONF_SUCCESS, flags), data); |
| 4027 | } |
| 4028 | |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4029 | static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident, |
| 4030 | u16 scid, u16 dcid) |
| 4031 | { |
| 4032 | struct l2cap_cmd_rej_cid rej; |
| 4033 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4034 | rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4035 | rej.scid = __cpu_to_le16(scid); |
| 4036 | rej.dcid = __cpu_to_le16(dcid); |
| 4037 | |
| 4038 | l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); |
| 4039 | } |
| 4040 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4041 | static inline int l2cap_config_req(struct l2cap_conn *conn, |
| 4042 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4043 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4044 | { |
| 4045 | struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; |
| 4046 | u16 dcid, flags; |
| 4047 | u8 rsp[64]; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4048 | struct l2cap_chan *chan; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4049 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4050 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4051 | if (cmd_len < sizeof(*req)) |
| 4052 | return -EPROTO; |
| 4053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | dcid = __le16_to_cpu(req->dcid); |
| 4055 | flags = __le16_to_cpu(req->flags); |
| 4056 | |
| 4057 | BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); |
| 4058 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4059 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4060 | if (!chan) { |
| 4061 | cmd_reject_invalid_cid(conn, cmd->ident, dcid, 0); |
| 4062 | return 0; |
| 4063 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4064 | |
David S. Miller | 033b114 | 2011-07-21 13:38:42 -0700 | [diff] [blame] | 4065 | if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4066 | cmd_reject_invalid_cid(conn, cmd->ident, chan->scid, |
| 4067 | chan->dcid); |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 4068 | goto unlock; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 4069 | } |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 4070 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4071 | /* Reject if config buffer is too small. */ |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 4072 | len = cmd_len - sizeof(*req); |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4073 | if (chan->conf_len + len > sizeof(chan->conf_req)) { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4074 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4075 | l2cap_build_conf_rsp(chan, rsp, |
| 4076 | L2CAP_CONF_REJECT, flags), rsp); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4077 | goto unlock; |
| 4078 | } |
| 4079 | |
| 4080 | /* Store config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4081 | memcpy(chan->conf_req + chan->conf_len, req->data, len); |
| 4082 | chan->conf_len += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4084 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | /* Incomplete config. Send empty response. */ |
| 4086 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4087 | l2cap_build_conf_rsp(chan, rsp, |
| 4088 | L2CAP_CONF_SUCCESS, flags), rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | goto unlock; |
| 4090 | } |
| 4091 | |
| 4092 | /* Complete config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4093 | len = l2cap_parse_conf_req(chan, rsp); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4094 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4095 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4096 | goto unlock; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4097 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4098 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4099 | chan->ident = cmd->ident; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4100 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4101 | chan->num_conf_rsp++; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4102 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4103 | /* Reset config buffer. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4104 | chan->conf_len = 0; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4105 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4106 | if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4107 | goto unlock; |
| 4108 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4109 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4110 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4111 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4112 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4113 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4114 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4115 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4116 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4117 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4118 | else |
| 4119 | l2cap_chan_ready(chan); |
| 4120 | |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4121 | goto unlock; |
| 4122 | } |
| 4123 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4124 | if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 4125 | u8 buf[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4126 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4127 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4128 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | } |
| 4130 | |
Stephen Hemminger | 49c922b | 2014-10-27 21:12:20 -0700 | [diff] [blame] | 4131 | /* Got Conf Rsp PENDING from remote side and assume we sent |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4132 | Conf Rsp PENDING in the code above */ |
| 4133 | if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 4134 | test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4135 | |
| 4136 | /* check compatibility */ |
| 4137 | |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4138 | /* Send rsp for BR/EDR channel */ |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4139 | if (!chan->hs_hcon) |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4140 | l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags); |
| 4141 | else |
| 4142 | chan->ident = cmd->ident; |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4143 | } |
| 4144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | unlock: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4146 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4147 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4148 | } |
| 4149 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4150 | static inline int l2cap_config_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4151 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4152 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4153 | { |
| 4154 | struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; |
| 4155 | u16 scid, flags, result; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4156 | struct l2cap_chan *chan; |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4157 | int len = cmd_len - sizeof(*rsp); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4158 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4160 | if (cmd_len < sizeof(*rsp)) |
| 4161 | return -EPROTO; |
| 4162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | scid = __le16_to_cpu(rsp->scid); |
| 4164 | flags = __le16_to_cpu(rsp->flags); |
| 4165 | result = __le16_to_cpu(rsp->result); |
| 4166 | |
Andrei Emeltchenko | 61386cb | 2012-03-12 12:13:07 +0200 | [diff] [blame] | 4167 | BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags, |
| 4168 | result, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4169 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4170 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4171 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | return 0; |
| 4173 | |
| 4174 | switch (result) { |
| 4175 | case L2CAP_CONF_SUCCESS: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4176 | l2cap_conf_rfc_get(chan, rsp->data, len); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4177 | clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4178 | break; |
| 4179 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4180 | case L2CAP_CONF_PENDING: |
| 4181 | set_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
| 4182 | |
| 4183 | if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
| 4184 | char buf[64]; |
| 4185 | |
| 4186 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4187 | buf, &result); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4188 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4189 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4190 | goto done; |
| 4191 | } |
| 4192 | |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4193 | if (!chan->hs_hcon) { |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4194 | l2cap_send_efs_conf_rsp(chan, buf, cmd->ident, |
| 4195 | 0); |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 4196 | } else { |
| 4197 | if (l2cap_check_efs(chan)) { |
| 4198 | amp_create_logical_link(chan); |
| 4199 | chan->ident = cmd->ident; |
| 4200 | } |
| 4201 | } |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4202 | } |
| 4203 | goto done; |
| 4204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | case L2CAP_CONF_UNACCEPT: |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4206 | if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4207 | char req[64]; |
| 4208 | |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4209 | if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4210 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4211 | goto done; |
| 4212 | } |
| 4213 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4214 | /* throw out any old stored conf requests */ |
| 4215 | result = L2CAP_CONF_SUCCESS; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 4216 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4217 | req, &result); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4218 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4219 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4220 | goto done; |
| 4221 | } |
| 4222 | |
| 4223 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4224 | L2CAP_CONF_REQ, len, req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4225 | chan->num_conf_req++; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4226 | if (result != L2CAP_CONF_SUCCESS) |
| 4227 | goto done; |
| 4228 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4229 | } |
| 4230 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 4231 | default: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4232 | l2cap_chan_set_err(chan, ECONNRESET); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 4233 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 4234 | __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4235 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | goto done; |
| 4237 | } |
| 4238 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4239 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | goto done; |
| 4241 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4242 | set_bit(CONF_INPUT_DONE, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4244 | if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4245 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4246 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4247 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4248 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4249 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4250 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4251 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4252 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4253 | else |
| 4254 | l2cap_chan_ready(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4255 | } |
| 4256 | |
| 4257 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4258 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4259 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | } |
| 4261 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4262 | static inline int l2cap_disconnect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4263 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4264 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | { |
| 4266 | struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; |
| 4267 | struct l2cap_disconn_rsp rsp; |
| 4268 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4269 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4271 | if (cmd_len != sizeof(*req)) |
| 4272 | return -EPROTO; |
| 4273 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | scid = __le16_to_cpu(req->scid); |
| 4275 | dcid = __le16_to_cpu(req->dcid); |
| 4276 | |
| 4277 | BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); |
| 4278 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4279 | mutex_lock(&conn->chan_lock); |
| 4280 | |
| 4281 | chan = __l2cap_get_chan_by_scid(conn, dcid); |
| 4282 | if (!chan) { |
| 4283 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4284 | cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid); |
| 4285 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4287 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4288 | l2cap_chan_lock(chan); |
| 4289 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4290 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4291 | rsp.scid = cpu_to_le16(chan->dcid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); |
| 4293 | |
Gustavo Padovan | 5ec1bbe | 2013-10-15 19:24:48 -0300 | [diff] [blame] | 4294 | chan->ops->set_shutdown(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4296 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4297 | l2cap_chan_del(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4298 | |
| 4299 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4301 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4302 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4303 | |
| 4304 | mutex_unlock(&conn->chan_lock); |
| 4305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | return 0; |
| 4307 | } |
| 4308 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4309 | static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4310 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4311 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | { |
| 4313 | struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; |
| 4314 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4315 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4317 | if (cmd_len != sizeof(*rsp)) |
| 4318 | return -EPROTO; |
| 4319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | scid = __le16_to_cpu(rsp->scid); |
| 4321 | dcid = __le16_to_cpu(rsp->dcid); |
| 4322 | |
| 4323 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); |
| 4324 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4325 | mutex_lock(&conn->chan_lock); |
| 4326 | |
| 4327 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 4328 | if (!chan) { |
| 4329 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4330 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4331 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4333 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4334 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4335 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4336 | l2cap_chan_del(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4337 | |
| 4338 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4340 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4341 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4342 | |
| 4343 | mutex_unlock(&conn->chan_lock); |
| 4344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | return 0; |
| 4346 | } |
| 4347 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4348 | static inline int l2cap_information_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4349 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4350 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4351 | { |
| 4352 | struct l2cap_info_req *req = (struct l2cap_info_req *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | u16 type; |
| 4354 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4355 | if (cmd_len != sizeof(*req)) |
| 4356 | return -EPROTO; |
| 4357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4358 | type = __le16_to_cpu(req->type); |
| 4359 | |
| 4360 | BT_DBG("type 0x%4.4x", type); |
| 4361 | |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4362 | if (type == L2CAP_IT_FEAT_MASK) { |
| 4363 | u8 buf[8]; |
Marcel Holtmann | 44dd46d | 2009-05-02 19:09:01 -0700 | [diff] [blame] | 4364 | u32 feat_mask = l2cap_feat_mask; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4365 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4366 | rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 4367 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 4368 | if (!disable_ertm) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4369 | feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4370 | | L2CAP_FEAT_FCS; |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 4371 | if (conn->local_fixed_chan & L2CAP_FC_A2MP) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 4372 | feat_mask |= L2CAP_FEAT_EXT_FLOW |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4373 | | L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 4374 | |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 4375 | put_unaligned_le32(feat_mask, rsp->data); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4376 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4377 | buf); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4378 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
| 4379 | u8 buf[12]; |
| 4380 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 4381 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4382 | rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 4383 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 4384 | rsp->data[0] = conn->local_fixed_chan; |
| 4385 | memset(rsp->data + 1, 0, 7); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4386 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4387 | buf); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4388 | } else { |
| 4389 | struct l2cap_info_rsp rsp; |
| 4390 | rsp.type = cpu_to_le16(type); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4391 | rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4392 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp), |
| 4393 | &rsp); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4394 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4395 | |
| 4396 | return 0; |
| 4397 | } |
| 4398 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4399 | static inline int l2cap_information_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4400 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4401 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | { |
| 4403 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; |
| 4404 | u16 type, result; |
| 4405 | |
Jaganath Kanakkassery | 3f6fa3d | 2013-06-21 19:55:11 +0530 | [diff] [blame] | 4406 | if (cmd_len < sizeof(*rsp)) |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4407 | return -EPROTO; |
| 4408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | type = __le16_to_cpu(rsp->type); |
| 4410 | result = __le16_to_cpu(rsp->result); |
| 4411 | |
| 4412 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); |
| 4413 | |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4414 | /* L2CAP Info req/rsp are unbound to channels, add extra checks */ |
| 4415 | if (cmd->ident != conn->info_ident || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4416 | conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4417 | return 0; |
| 4418 | |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 4419 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4420 | |
Ville Tervo | adb08ed | 2010-08-04 09:43:33 +0300 | [diff] [blame] | 4421 | if (result != L2CAP_IR_SUCCESS) { |
| 4422 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4423 | conn->info_ident = 0; |
| 4424 | |
| 4425 | l2cap_conn_start(conn); |
| 4426 | |
| 4427 | return 0; |
| 4428 | } |
| 4429 | |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4430 | switch (type) { |
| 4431 | case L2CAP_IT_FEAT_MASK: |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 4432 | conn->feat_mask = get_unaligned_le32(rsp->data); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4433 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 4434 | if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4435 | struct l2cap_info_req req; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4436 | req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4437 | |
| 4438 | conn->info_ident = l2cap_get_ident(conn); |
| 4439 | |
| 4440 | l2cap_send_cmd(conn, conn->info_ident, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4441 | L2CAP_INFO_REQ, sizeof(req), &req); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4442 | } else { |
| 4443 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4444 | conn->info_ident = 0; |
| 4445 | |
| 4446 | l2cap_conn_start(conn); |
| 4447 | } |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4448 | break; |
| 4449 | |
| 4450 | case L2CAP_IT_FIXED_CHAN: |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 4451 | conn->remote_fixed_chan = rsp->data[0]; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4452 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4453 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4454 | |
| 4455 | l2cap_conn_start(conn); |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4456 | break; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4457 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4459 | return 0; |
| 4460 | } |
| 4461 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4462 | static int l2cap_create_channel_req(struct l2cap_conn *conn, |
| 4463 | struct l2cap_cmd_hdr *cmd, |
| 4464 | u16 cmd_len, void *data) |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4465 | { |
| 4466 | struct l2cap_create_chan_req *req = data; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4467 | struct l2cap_create_chan_rsp rsp; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4468 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4469 | struct hci_dev *hdev; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4470 | u16 psm, scid; |
| 4471 | |
| 4472 | if (cmd_len != sizeof(*req)) |
| 4473 | return -EPROTO; |
| 4474 | |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 4475 | if (!(conn->local_fixed_chan & L2CAP_FC_A2MP)) |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4476 | return -EINVAL; |
| 4477 | |
| 4478 | psm = le16_to_cpu(req->psm); |
| 4479 | scid = le16_to_cpu(req->scid); |
| 4480 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4481 | 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] | 4482 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4483 | /* For controller id 0 make BR/EDR connection */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4484 | if (req->amp_id == AMP_ID_BREDR) { |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4485 | l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4486 | req->amp_id); |
| 4487 | return 0; |
| 4488 | } |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4489 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4490 | /* Validate AMP controller id */ |
| 4491 | hdev = hci_dev_get(req->amp_id); |
| 4492 | if (!hdev) |
| 4493 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4494 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4495 | if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4496 | hci_dev_put(hdev); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4497 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4498 | } |
| 4499 | |
| 4500 | chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4501 | req->amp_id); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4502 | if (chan) { |
| 4503 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
| 4504 | struct hci_conn *hs_hcon; |
| 4505 | |
Marcel Holtmann | 98e0f7e | 2013-10-13 02:23:40 -0700 | [diff] [blame] | 4506 | hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, |
| 4507 | &conn->hcon->dst); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4508 | if (!hs_hcon) { |
| 4509 | hci_dev_put(hdev); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4510 | cmd_reject_invalid_cid(conn, cmd->ident, chan->scid, |
| 4511 | chan->dcid); |
| 4512 | return 0; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4513 | } |
| 4514 | |
| 4515 | BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon); |
| 4516 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4517 | mgr->bredr_chan = chan; |
| 4518 | chan->hs_hcon = hs_hcon; |
Andrei Emeltchenko | fd45bf4 | 2012-11-20 17:16:22 +0200 | [diff] [blame] | 4519 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4520 | conn->mtu = hdev->block_mtu; |
| 4521 | } |
| 4522 | |
| 4523 | hci_dev_put(hdev); |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4524 | |
| 4525 | return 0; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4526 | |
| 4527 | error: |
| 4528 | rsp.dcid = 0; |
| 4529 | rsp.scid = cpu_to_le16(scid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4530 | rsp.result = cpu_to_le16(L2CAP_CR_BAD_AMP); |
| 4531 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4532 | |
| 4533 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, |
| 4534 | sizeof(rsp), &rsp); |
| 4535 | |
Johan Hedberg | dc28080 | 2013-09-16 13:05:13 +0300 | [diff] [blame] | 4536 | return 0; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4537 | } |
| 4538 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4539 | static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id) |
| 4540 | { |
| 4541 | struct l2cap_move_chan_req req; |
| 4542 | u8 ident; |
| 4543 | |
| 4544 | BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id); |
| 4545 | |
| 4546 | ident = l2cap_get_ident(chan->conn); |
| 4547 | chan->ident = ident; |
| 4548 | |
| 4549 | req.icid = cpu_to_le16(chan->scid); |
| 4550 | req.dest_amp_id = dest_amp_id; |
| 4551 | |
| 4552 | l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req), |
| 4553 | &req); |
| 4554 | |
| 4555 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4556 | } |
| 4557 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4558 | 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] | 4559 | { |
| 4560 | struct l2cap_move_chan_rsp rsp; |
| 4561 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4562 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4563 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4564 | rsp.icid = cpu_to_le16(chan->dcid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4565 | rsp.result = cpu_to_le16(result); |
| 4566 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4567 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP, |
| 4568 | sizeof(rsp), &rsp); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4569 | } |
| 4570 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4571 | 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] | 4572 | { |
| 4573 | struct l2cap_move_chan_cfm cfm; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4574 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4575 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4576 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4577 | chan->ident = l2cap_get_ident(chan->conn); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4578 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4579 | cfm.icid = cpu_to_le16(chan->scid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4580 | cfm.result = cpu_to_le16(result); |
| 4581 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4582 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM, |
| 4583 | sizeof(cfm), &cfm); |
| 4584 | |
| 4585 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4586 | } |
| 4587 | |
| 4588 | static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid) |
| 4589 | { |
| 4590 | struct l2cap_move_chan_cfm cfm; |
| 4591 | |
| 4592 | BT_DBG("conn %p, icid 0x%4.4x", conn, icid); |
| 4593 | |
| 4594 | cfm.icid = cpu_to_le16(icid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4595 | cfm.result = cpu_to_le16(L2CAP_MC_UNCONFIRMED); |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4596 | |
| 4597 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM, |
| 4598 | sizeof(cfm), &cfm); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4599 | } |
| 4600 | |
| 4601 | 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] | 4602 | u16 icid) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4603 | { |
| 4604 | struct l2cap_move_chan_cfm_rsp rsp; |
| 4605 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4606 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4607 | |
| 4608 | rsp.icid = cpu_to_le16(icid); |
| 4609 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); |
| 4610 | } |
| 4611 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 4612 | static void __release_logical_link(struct l2cap_chan *chan) |
| 4613 | { |
| 4614 | chan->hs_hchan = NULL; |
| 4615 | chan->hs_hcon = NULL; |
| 4616 | |
| 4617 | /* Placeholder - release the logical link */ |
| 4618 | } |
| 4619 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4620 | static void l2cap_logical_fail(struct l2cap_chan *chan) |
| 4621 | { |
| 4622 | /* Logical link setup failed */ |
| 4623 | if (chan->state != BT_CONNECTED) { |
| 4624 | /* Create channel failure, disconnect */ |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4625 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4626 | return; |
| 4627 | } |
| 4628 | |
| 4629 | switch (chan->move_role) { |
| 4630 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4631 | l2cap_move_done(chan); |
| 4632 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP); |
| 4633 | break; |
| 4634 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4635 | if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP || |
| 4636 | chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) { |
| 4637 | /* Remote has only sent pending or |
| 4638 | * success responses, clean up |
| 4639 | */ |
| 4640 | l2cap_move_done(chan); |
| 4641 | } |
| 4642 | |
| 4643 | /* Other amp move states imply that the move |
| 4644 | * has already aborted |
| 4645 | */ |
| 4646 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 4647 | break; |
| 4648 | } |
| 4649 | } |
| 4650 | |
| 4651 | static void l2cap_logical_finish_create(struct l2cap_chan *chan, |
| 4652 | struct hci_chan *hchan) |
| 4653 | { |
| 4654 | struct l2cap_conf_rsp rsp; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4655 | |
Andrei Emeltchenko | 336178a | 2012-10-31 15:46:27 +0200 | [diff] [blame] | 4656 | chan->hs_hchan = hchan; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4657 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4658 | |
Andrei Emeltchenko | 35ba956 | 2012-10-25 15:20:43 +0300 | [diff] [blame] | 4659 | l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4660 | |
| 4661 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Andrei Emeltchenko | fe79c6f | 2012-10-25 15:20:42 +0300 | [diff] [blame] | 4662 | int err; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4663 | |
| 4664 | set_default_fcs(chan); |
| 4665 | |
| 4666 | err = l2cap_ertm_init(chan); |
| 4667 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4668 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4669 | else |
| 4670 | l2cap_chan_ready(chan); |
| 4671 | } |
| 4672 | } |
| 4673 | |
| 4674 | static void l2cap_logical_finish_move(struct l2cap_chan *chan, |
| 4675 | struct hci_chan *hchan) |
| 4676 | { |
| 4677 | chan->hs_hcon = hchan->conn; |
| 4678 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4679 | |
| 4680 | BT_DBG("move_state %d", chan->move_state); |
| 4681 | |
| 4682 | switch (chan->move_state) { |
| 4683 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 4684 | /* Move confirm will be sent after a success |
| 4685 | * response is received |
| 4686 | */ |
| 4687 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4688 | break; |
| 4689 | case L2CAP_MOVE_WAIT_LOGICAL_CFM: |
| 4690 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4691 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4692 | } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 4693 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 4694 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4695 | } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4696 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4697 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4698 | } |
| 4699 | break; |
| 4700 | default: |
| 4701 | /* Move was not in expected state, free the channel */ |
| 4702 | __release_logical_link(chan); |
| 4703 | |
| 4704 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4705 | } |
| 4706 | } |
| 4707 | |
| 4708 | /* Call with chan locked */ |
Andrei Emeltchenko | 27695fb | 2012-10-25 15:20:45 +0300 | [diff] [blame] | 4709 | void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan, |
| 4710 | u8 status) |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4711 | { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4712 | BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status); |
| 4713 | |
| 4714 | if (status) { |
| 4715 | l2cap_logical_fail(chan); |
| 4716 | __release_logical_link(chan); |
| 4717 | return; |
| 4718 | } |
| 4719 | |
| 4720 | if (chan->state != BT_CONNECTED) { |
| 4721 | /* Ignore logical link if channel is on BR/EDR */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4722 | if (chan->local_amp_id != AMP_ID_BREDR) |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4723 | l2cap_logical_finish_create(chan, hchan); |
| 4724 | } else { |
| 4725 | l2cap_logical_finish_move(chan, hchan); |
| 4726 | } |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4727 | } |
| 4728 | |
Mat Martineau | 3f7a56c | 2012-10-23 15:24:23 -0700 | [diff] [blame] | 4729 | void l2cap_move_start(struct l2cap_chan *chan) |
| 4730 | { |
| 4731 | BT_DBG("chan %p", chan); |
| 4732 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4733 | if (chan->local_amp_id == AMP_ID_BREDR) { |
Mat Martineau | 3f7a56c | 2012-10-23 15:24:23 -0700 | [diff] [blame] | 4734 | if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED) |
| 4735 | return; |
| 4736 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4737 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4738 | /* Placeholder - start physical link setup */ |
| 4739 | } else { |
| 4740 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4741 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4742 | chan->move_id = 0; |
| 4743 | l2cap_move_setup(chan); |
| 4744 | l2cap_send_move_chan_req(chan, 0); |
| 4745 | } |
| 4746 | } |
| 4747 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4748 | static void l2cap_do_create(struct l2cap_chan *chan, int result, |
| 4749 | u8 local_amp_id, u8 remote_amp_id) |
| 4750 | { |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4751 | BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state), |
| 4752 | local_amp_id, remote_amp_id); |
| 4753 | |
Andrei Emeltchenko | 12d6cc6 | 2012-11-14 17:39:31 +0200 | [diff] [blame] | 4754 | chan->fcs = L2CAP_FCS_NONE; |
| 4755 | |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4756 | /* Outgoing channel on AMP */ |
| 4757 | if (chan->state == BT_CONNECT) { |
| 4758 | if (result == L2CAP_CR_SUCCESS) { |
| 4759 | chan->local_amp_id = local_amp_id; |
| 4760 | l2cap_send_create_chan_req(chan, remote_amp_id); |
| 4761 | } else { |
| 4762 | /* Revert to BR/EDR connect */ |
| 4763 | l2cap_send_conn_req(chan); |
| 4764 | } |
| 4765 | |
| 4766 | return; |
| 4767 | } |
| 4768 | |
| 4769 | /* Incoming channel on AMP */ |
| 4770 | if (__l2cap_no_conn_pending(chan)) { |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4771 | struct l2cap_conn_rsp rsp; |
| 4772 | char buf[128]; |
| 4773 | rsp.scid = cpu_to_le16(chan->dcid); |
| 4774 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4775 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4776 | if (result == L2CAP_CR_SUCCESS) { |
| 4777 | /* Send successful response */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4778 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 4779 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4780 | } else { |
| 4781 | /* Send negative response */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4782 | rsp.result = cpu_to_le16(L2CAP_CR_NO_MEM); |
| 4783 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4784 | } |
| 4785 | |
| 4786 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP, |
| 4787 | sizeof(rsp), &rsp); |
| 4788 | |
| 4789 | if (result == L2CAP_CR_SUCCESS) { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 4790 | l2cap_state_change(chan, BT_CONFIG); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4791 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 4792 | l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn), |
| 4793 | L2CAP_CONF_REQ, |
| 4794 | l2cap_build_conf_req(chan, buf), buf); |
| 4795 | chan->num_conf_req++; |
| 4796 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4797 | } |
| 4798 | } |
| 4799 | |
| 4800 | static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id, |
| 4801 | u8 remote_amp_id) |
| 4802 | { |
| 4803 | l2cap_move_setup(chan); |
| 4804 | chan->move_id = local_amp_id; |
| 4805 | chan->move_state = L2CAP_MOVE_WAIT_RSP; |
| 4806 | |
| 4807 | l2cap_send_move_chan_req(chan, remote_amp_id); |
| 4808 | } |
| 4809 | |
| 4810 | static void l2cap_do_move_respond(struct l2cap_chan *chan, int result) |
| 4811 | { |
| 4812 | struct hci_chan *hchan = NULL; |
| 4813 | |
| 4814 | /* Placeholder - get hci_chan for logical link */ |
| 4815 | |
| 4816 | if (hchan) { |
| 4817 | if (hchan->state == BT_CONNECTED) { |
| 4818 | /* Logical link is ready to go */ |
| 4819 | chan->hs_hcon = hchan->conn; |
| 4820 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4821 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4822 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4823 | |
| 4824 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 4825 | } else { |
| 4826 | /* Wait for logical link to be ready */ |
| 4827 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4828 | } |
| 4829 | } else { |
| 4830 | /* Logical link not available */ |
| 4831 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED); |
| 4832 | } |
| 4833 | } |
| 4834 | |
| 4835 | static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result) |
| 4836 | { |
| 4837 | if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4838 | u8 rsp_result; |
| 4839 | if (result == -EINVAL) |
| 4840 | rsp_result = L2CAP_MR_BAD_ID; |
| 4841 | else |
| 4842 | rsp_result = L2CAP_MR_NOT_ALLOWED; |
| 4843 | |
| 4844 | l2cap_send_move_chan_rsp(chan, rsp_result); |
| 4845 | } |
| 4846 | |
| 4847 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 4848 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4849 | |
| 4850 | /* Restart data transmission */ |
| 4851 | l2cap_ertm_send(chan); |
| 4852 | } |
| 4853 | |
Andrei Emeltchenko | a514b17 | 2012-11-14 17:39:30 +0200 | [diff] [blame] | 4854 | /* Invoke with locked chan */ |
| 4855 | void __l2cap_physical_cfm(struct l2cap_chan *chan, int result) |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4856 | { |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4857 | u8 local_amp_id = chan->local_amp_id; |
Andrei Emeltchenko | fffadc0 | 2012-11-01 15:37:03 +0200 | [diff] [blame] | 4858 | u8 remote_amp_id = chan->remote_amp_id; |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4859 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4860 | BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d", |
| 4861 | chan, result, local_amp_id, remote_amp_id); |
| 4862 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4863 | if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) { |
| 4864 | l2cap_chan_unlock(chan); |
| 4865 | return; |
| 4866 | } |
| 4867 | |
| 4868 | if (chan->state != BT_CONNECTED) { |
| 4869 | l2cap_do_create(chan, result, local_amp_id, remote_amp_id); |
| 4870 | } else if (result != L2CAP_MR_SUCCESS) { |
| 4871 | l2cap_do_move_cancel(chan, result); |
| 4872 | } else { |
| 4873 | switch (chan->move_role) { |
| 4874 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4875 | l2cap_do_move_initiate(chan, local_amp_id, |
| 4876 | remote_amp_id); |
| 4877 | break; |
| 4878 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4879 | l2cap_do_move_respond(chan, result); |
| 4880 | break; |
| 4881 | default: |
| 4882 | l2cap_do_move_cancel(chan, result); |
| 4883 | break; |
| 4884 | } |
| 4885 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4886 | } |
| 4887 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4888 | static inline int l2cap_move_channel_req(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4889 | struct l2cap_cmd_hdr *cmd, |
| 4890 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4891 | { |
| 4892 | struct l2cap_move_chan_req *req = data; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4893 | struct l2cap_move_chan_rsp rsp; |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4894 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4895 | u16 icid = 0; |
| 4896 | u16 result = L2CAP_MR_NOT_ALLOWED; |
| 4897 | |
| 4898 | if (cmd_len != sizeof(*req)) |
| 4899 | return -EPROTO; |
| 4900 | |
| 4901 | icid = le16_to_cpu(req->icid); |
| 4902 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4903 | 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] | 4904 | |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 4905 | if (!(conn->local_fixed_chan & L2CAP_FC_A2MP)) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4906 | return -EINVAL; |
| 4907 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4908 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 4909 | if (!chan) { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4910 | rsp.icid = cpu_to_le16(icid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4911 | rsp.result = cpu_to_le16(L2CAP_MR_NOT_ALLOWED); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4912 | l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP, |
| 4913 | sizeof(rsp), &rsp); |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4914 | return 0; |
| 4915 | } |
| 4916 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4917 | chan->ident = cmd->ident; |
| 4918 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4919 | if (chan->scid < L2CAP_CID_DYN_START || |
| 4920 | chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY || |
| 4921 | (chan->mode != L2CAP_MODE_ERTM && |
| 4922 | chan->mode != L2CAP_MODE_STREAMING)) { |
| 4923 | result = L2CAP_MR_NOT_ALLOWED; |
| 4924 | goto send_move_response; |
| 4925 | } |
| 4926 | |
| 4927 | if (chan->local_amp_id == req->dest_amp_id) { |
| 4928 | result = L2CAP_MR_SAME_ID; |
| 4929 | goto send_move_response; |
| 4930 | } |
| 4931 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4932 | if (req->dest_amp_id != AMP_ID_BREDR) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4933 | struct hci_dev *hdev; |
| 4934 | hdev = hci_dev_get(req->dest_amp_id); |
| 4935 | if (!hdev || hdev->dev_type != HCI_AMP || |
| 4936 | !test_bit(HCI_UP, &hdev->flags)) { |
| 4937 | if (hdev) |
| 4938 | hci_dev_put(hdev); |
| 4939 | |
| 4940 | result = L2CAP_MR_BAD_ID; |
| 4941 | goto send_move_response; |
| 4942 | } |
| 4943 | hci_dev_put(hdev); |
| 4944 | } |
| 4945 | |
| 4946 | /* Detect a move collision. Only send a collision response |
| 4947 | * if this side has "lost", otherwise proceed with the move. |
| 4948 | * The winner has the larger bd_addr. |
| 4949 | */ |
| 4950 | if ((__chan_is_moving(chan) || |
| 4951 | chan->move_role != L2CAP_MOVE_ROLE_NONE) && |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 4952 | bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4953 | result = L2CAP_MR_COLLISION; |
| 4954 | goto send_move_response; |
| 4955 | } |
| 4956 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4957 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 4958 | l2cap_move_setup(chan); |
| 4959 | chan->move_id = req->dest_amp_id; |
| 4960 | icid = chan->dcid; |
| 4961 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4962 | if (req->dest_amp_id == AMP_ID_BREDR) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4963 | /* Moving to BR/EDR */ |
| 4964 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4965 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4966 | result = L2CAP_MR_PEND; |
| 4967 | } else { |
| 4968 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4969 | result = L2CAP_MR_SUCCESS; |
| 4970 | } |
| 4971 | } else { |
| 4972 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4973 | /* Placeholder - uncomment when amp functions are available */ |
| 4974 | /*amp_accept_physical(chan, req->dest_amp_id);*/ |
| 4975 | result = L2CAP_MR_PEND; |
| 4976 | } |
| 4977 | |
| 4978 | send_move_response: |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4979 | l2cap_send_move_chan_rsp(chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4980 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4981 | l2cap_chan_unlock(chan); |
| 4982 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4983 | return 0; |
| 4984 | } |
| 4985 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4986 | static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result) |
| 4987 | { |
| 4988 | struct l2cap_chan *chan; |
| 4989 | struct hci_chan *hchan = NULL; |
| 4990 | |
| 4991 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 4992 | if (!chan) { |
| 4993 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 4994 | return; |
| 4995 | } |
| 4996 | |
| 4997 | __clear_chan_timer(chan); |
| 4998 | if (result == L2CAP_MR_PEND) |
| 4999 | __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT); |
| 5000 | |
| 5001 | switch (chan->move_state) { |
| 5002 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 5003 | /* Move confirm will be sent when logical link |
| 5004 | * is complete. |
| 5005 | */ |
| 5006 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 5007 | break; |
| 5008 | case L2CAP_MOVE_WAIT_RSP_SUCCESS: |
| 5009 | if (result == L2CAP_MR_PEND) { |
| 5010 | break; |
| 5011 | } else if (test_bit(CONN_LOCAL_BUSY, |
| 5012 | &chan->conn_state)) { |
| 5013 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 5014 | } else { |
| 5015 | /* Logical link is up or moving to BR/EDR, |
| 5016 | * proceed with move |
| 5017 | */ |
| 5018 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 5019 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 5020 | } |
| 5021 | break; |
| 5022 | case L2CAP_MOVE_WAIT_RSP: |
| 5023 | /* Moving to AMP */ |
| 5024 | if (result == L2CAP_MR_SUCCESS) { |
| 5025 | /* Remote is ready, send confirm immediately |
| 5026 | * after logical link is ready |
| 5027 | */ |
| 5028 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 5029 | } else { |
| 5030 | /* Both logical link and move success |
| 5031 | * are required to confirm |
| 5032 | */ |
| 5033 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP; |
| 5034 | } |
| 5035 | |
| 5036 | /* Placeholder - get hci_chan for logical link */ |
| 5037 | if (!hchan) { |
| 5038 | /* Logical link not available */ |
| 5039 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5040 | break; |
| 5041 | } |
| 5042 | |
| 5043 | /* If the logical link is not yet connected, do not |
| 5044 | * send confirmation. |
| 5045 | */ |
| 5046 | if (hchan->state != BT_CONNECTED) |
| 5047 | break; |
| 5048 | |
| 5049 | /* Logical link is already ready to go */ |
| 5050 | |
| 5051 | chan->hs_hcon = hchan->conn; |
| 5052 | chan->hs_hcon->l2cap_data = chan->conn; |
| 5053 | |
| 5054 | if (result == L2CAP_MR_SUCCESS) { |
| 5055 | /* Can confirm now */ |
| 5056 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 5057 | } else { |
| 5058 | /* Now only need move success |
| 5059 | * to confirm |
| 5060 | */ |
| 5061 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 5062 | } |
| 5063 | |
| 5064 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 5065 | break; |
| 5066 | default: |
| 5067 | /* Any other amp move state means the move failed. */ |
| 5068 | chan->move_id = chan->local_amp_id; |
| 5069 | l2cap_move_done(chan); |
| 5070 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5071 | } |
| 5072 | |
| 5073 | l2cap_chan_unlock(chan); |
| 5074 | } |
| 5075 | |
| 5076 | static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid, |
| 5077 | u16 result) |
| 5078 | { |
| 5079 | struct l2cap_chan *chan; |
| 5080 | |
| 5081 | chan = l2cap_get_chan_by_ident(conn, ident); |
| 5082 | if (!chan) { |
| 5083 | /* Could not locate channel, icid is best guess */ |
| 5084 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 5085 | return; |
| 5086 | } |
| 5087 | |
| 5088 | __clear_chan_timer(chan); |
| 5089 | |
| 5090 | if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 5091 | if (result == L2CAP_MR_COLLISION) { |
| 5092 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 5093 | } else { |
| 5094 | /* Cleanup - cancel move */ |
| 5095 | chan->move_id = chan->local_amp_id; |
| 5096 | l2cap_move_done(chan); |
| 5097 | } |
| 5098 | } |
| 5099 | |
| 5100 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5101 | |
| 5102 | l2cap_chan_unlock(chan); |
| 5103 | } |
| 5104 | |
| 5105 | static int l2cap_move_channel_rsp(struct l2cap_conn *conn, |
| 5106 | struct l2cap_cmd_hdr *cmd, |
| 5107 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5108 | { |
| 5109 | struct l2cap_move_chan_rsp *rsp = data; |
| 5110 | u16 icid, result; |
| 5111 | |
| 5112 | if (cmd_len != sizeof(*rsp)) |
| 5113 | return -EPROTO; |
| 5114 | |
| 5115 | icid = le16_to_cpu(rsp->icid); |
| 5116 | result = le16_to_cpu(rsp->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 | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 5120 | if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND) |
| 5121 | l2cap_move_continue(conn, icid, result); |
| 5122 | else |
| 5123 | l2cap_move_fail(conn, cmd->ident, icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5124 | |
| 5125 | return 0; |
| 5126 | } |
| 5127 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5128 | static int l2cap_move_channel_confirm(struct l2cap_conn *conn, |
| 5129 | struct l2cap_cmd_hdr *cmd, |
| 5130 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5131 | { |
| 5132 | struct l2cap_move_chan_cfm *cfm = data; |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5133 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5134 | u16 icid, result; |
| 5135 | |
| 5136 | if (cmd_len != sizeof(*cfm)) |
| 5137 | return -EPROTO; |
| 5138 | |
| 5139 | icid = le16_to_cpu(cfm->icid); |
| 5140 | result = le16_to_cpu(cfm->result); |
| 5141 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5142 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5143 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5144 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 5145 | if (!chan) { |
| 5146 | /* Spec requires a response even if the icid was not found */ |
| 5147 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5148 | return 0; |
| 5149 | } |
| 5150 | |
| 5151 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) { |
| 5152 | if (result == L2CAP_MC_CONFIRMED) { |
| 5153 | chan->local_amp_id = chan->move_id; |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 5154 | if (chan->local_amp_id == AMP_ID_BREDR) |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5155 | __release_logical_link(chan); |
| 5156 | } else { |
| 5157 | chan->move_id = chan->local_amp_id; |
| 5158 | } |
| 5159 | |
| 5160 | l2cap_move_done(chan); |
| 5161 | } |
| 5162 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5163 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5164 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5165 | l2cap_chan_unlock(chan); |
| 5166 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5167 | return 0; |
| 5168 | } |
| 5169 | |
| 5170 | static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5171 | struct l2cap_cmd_hdr *cmd, |
| 5172 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5173 | { |
| 5174 | struct l2cap_move_chan_cfm_rsp *rsp = data; |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5175 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5176 | u16 icid; |
| 5177 | |
| 5178 | if (cmd_len != sizeof(*rsp)) |
| 5179 | return -EPROTO; |
| 5180 | |
| 5181 | icid = le16_to_cpu(rsp->icid); |
| 5182 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5183 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5184 | |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5185 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 5186 | if (!chan) |
| 5187 | return 0; |
| 5188 | |
| 5189 | __clear_chan_timer(chan); |
| 5190 | |
| 5191 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) { |
| 5192 | chan->local_amp_id = chan->move_id; |
| 5193 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 5194 | if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan) |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5195 | __release_logical_link(chan); |
| 5196 | |
| 5197 | l2cap_move_done(chan); |
| 5198 | } |
| 5199 | |
| 5200 | l2cap_chan_unlock(chan); |
| 5201 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5202 | return 0; |
| 5203 | } |
| 5204 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5205 | static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5206 | struct l2cap_cmd_hdr *cmd, |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5207 | u16 cmd_len, u8 *data) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5208 | { |
| 5209 | struct hci_conn *hcon = conn->hcon; |
| 5210 | struct l2cap_conn_param_update_req *req; |
| 5211 | struct l2cap_conn_param_update_rsp rsp; |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5212 | u16 min, max, latency, to_multiplier; |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5213 | int err; |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5214 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 5215 | if (hcon->role != HCI_ROLE_MASTER) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5216 | return -EINVAL; |
| 5217 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5218 | if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) |
| 5219 | return -EPROTO; |
| 5220 | |
| 5221 | req = (struct l2cap_conn_param_update_req *) data; |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 5222 | min = __le16_to_cpu(req->min); |
| 5223 | max = __le16_to_cpu(req->max); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5224 | latency = __le16_to_cpu(req->latency); |
| 5225 | to_multiplier = __le16_to_cpu(req->to_multiplier); |
| 5226 | |
| 5227 | 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] | 5228 | min, max, latency, to_multiplier); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5229 | |
| 5230 | memset(&rsp, 0, sizeof(rsp)); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5231 | |
Andre Guedes | d4905f2 | 2014-06-25 21:52:52 -0300 | [diff] [blame] | 5232 | err = hci_check_conn_params(min, max, latency, to_multiplier); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5233 | if (err) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5234 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5235 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5236 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5237 | |
| 5238 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5239 | sizeof(rsp), &rsp); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5240 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5241 | if (!err) { |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5242 | u8 store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5243 | |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5244 | store_hint = hci_le_conn_update(hcon, min, max, latency, |
| 5245 | to_multiplier); |
| 5246 | mgmt_new_conn_param(hcon->hdev, &hcon->dst, hcon->dst_type, |
| 5247 | store_hint, min, max, latency, |
| 5248 | to_multiplier); |
| 5249 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5250 | } |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5251 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5252 | return 0; |
| 5253 | } |
| 5254 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5255 | static int l2cap_le_connect_rsp(struct l2cap_conn *conn, |
| 5256 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5257 | u8 *data) |
| 5258 | { |
| 5259 | struct l2cap_le_conn_rsp *rsp = (struct l2cap_le_conn_rsp *) data; |
Johan Hedberg | 3e64b7b | 2014-11-13 10:55:19 +0200 | [diff] [blame] | 5260 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5261 | u16 dcid, mtu, mps, credits, result; |
| 5262 | struct l2cap_chan *chan; |
Johan Hedberg | 3e64b7b | 2014-11-13 10:55:19 +0200 | [diff] [blame] | 5263 | int err, sec_level; |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5264 | |
| 5265 | if (cmd_len < sizeof(*rsp)) |
| 5266 | return -EPROTO; |
| 5267 | |
| 5268 | dcid = __le16_to_cpu(rsp->dcid); |
| 5269 | mtu = __le16_to_cpu(rsp->mtu); |
| 5270 | mps = __le16_to_cpu(rsp->mps); |
| 5271 | credits = __le16_to_cpu(rsp->credits); |
| 5272 | result = __le16_to_cpu(rsp->result); |
| 5273 | |
Johan Hedberg | 4062418 | 2015-11-02 14:39:17 +0200 | [diff] [blame] | 5274 | if (result == L2CAP_CR_SUCCESS && (mtu < 23 || mps < 23 || |
| 5275 | dcid < L2CAP_CID_DYN_START || |
| 5276 | dcid > L2CAP_CID_LE_DYN_END)) |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5277 | return -EPROTO; |
| 5278 | |
| 5279 | BT_DBG("dcid 0x%4.4x mtu %u mps %u credits %u result 0x%2.2x", |
| 5280 | dcid, mtu, mps, credits, result); |
| 5281 | |
| 5282 | mutex_lock(&conn->chan_lock); |
| 5283 | |
| 5284 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 5285 | if (!chan) { |
| 5286 | err = -EBADSLT; |
| 5287 | goto unlock; |
| 5288 | } |
| 5289 | |
| 5290 | err = 0; |
| 5291 | |
| 5292 | l2cap_chan_lock(chan); |
| 5293 | |
| 5294 | switch (result) { |
| 5295 | case L2CAP_CR_SUCCESS: |
Johan Hedberg | 4062418 | 2015-11-02 14:39:17 +0200 | [diff] [blame] | 5296 | if (__l2cap_get_chan_by_dcid(conn, dcid)) { |
| 5297 | err = -EBADSLT; |
| 5298 | break; |
| 5299 | } |
| 5300 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5301 | chan->ident = 0; |
| 5302 | chan->dcid = dcid; |
| 5303 | chan->omtu = mtu; |
| 5304 | chan->remote_mps = mps; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5305 | chan->tx_credits = credits; |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5306 | l2cap_chan_ready(chan); |
| 5307 | break; |
| 5308 | |
Johan Hedberg | 3e64b7b | 2014-11-13 10:55:19 +0200 | [diff] [blame] | 5309 | case L2CAP_CR_AUTHENTICATION: |
| 5310 | case L2CAP_CR_ENCRYPTION: |
| 5311 | /* If we already have MITM protection we can't do |
| 5312 | * anything. |
| 5313 | */ |
| 5314 | if (hcon->sec_level > BT_SECURITY_MEDIUM) { |
| 5315 | l2cap_chan_del(chan, ECONNREFUSED); |
| 5316 | break; |
| 5317 | } |
| 5318 | |
| 5319 | sec_level = hcon->sec_level + 1; |
| 5320 | if (chan->sec_level < sec_level) |
| 5321 | chan->sec_level = sec_level; |
| 5322 | |
| 5323 | /* We'll need to send a new Connect Request */ |
| 5324 | clear_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags); |
| 5325 | |
| 5326 | smp_conn_security(hcon, chan->sec_level); |
| 5327 | break; |
| 5328 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5329 | default: |
| 5330 | l2cap_chan_del(chan, ECONNREFUSED); |
| 5331 | break; |
| 5332 | } |
| 5333 | |
| 5334 | l2cap_chan_unlock(chan); |
| 5335 | |
| 5336 | unlock: |
| 5337 | mutex_unlock(&conn->chan_lock); |
| 5338 | |
| 5339 | return err; |
| 5340 | } |
| 5341 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5342 | static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5343 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5344 | u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5345 | { |
| 5346 | int err = 0; |
| 5347 | |
| 5348 | switch (cmd->code) { |
| 5349 | case L2CAP_COMMAND_REJ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5350 | l2cap_command_rej(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5351 | break; |
| 5352 | |
| 5353 | case L2CAP_CONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5354 | err = l2cap_connect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5355 | break; |
| 5356 | |
| 5357 | case L2CAP_CONN_RSP: |
Mat Martineau | f5a2598 | 2012-10-11 17:48:21 +0300 | [diff] [blame] | 5358 | case L2CAP_CREATE_CHAN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5359 | l2cap_connect_create_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5360 | break; |
| 5361 | |
| 5362 | case L2CAP_CONF_REQ: |
| 5363 | err = l2cap_config_req(conn, cmd, cmd_len, data); |
| 5364 | break; |
| 5365 | |
| 5366 | case L2CAP_CONF_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5367 | l2cap_config_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5368 | break; |
| 5369 | |
| 5370 | case L2CAP_DISCONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5371 | err = l2cap_disconnect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5372 | break; |
| 5373 | |
| 5374 | case L2CAP_DISCONN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5375 | l2cap_disconnect_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5376 | break; |
| 5377 | |
| 5378 | case L2CAP_ECHO_REQ: |
| 5379 | l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); |
| 5380 | break; |
| 5381 | |
| 5382 | case L2CAP_ECHO_RSP: |
| 5383 | break; |
| 5384 | |
| 5385 | case L2CAP_INFO_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5386 | err = l2cap_information_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5387 | break; |
| 5388 | |
| 5389 | case L2CAP_INFO_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5390 | l2cap_information_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5391 | break; |
| 5392 | |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 5393 | case L2CAP_CREATE_CHAN_REQ: |
| 5394 | err = l2cap_create_channel_req(conn, cmd, cmd_len, data); |
| 5395 | break; |
| 5396 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5397 | case L2CAP_MOVE_CHAN_REQ: |
| 5398 | err = l2cap_move_channel_req(conn, cmd, cmd_len, data); |
| 5399 | break; |
| 5400 | |
| 5401 | case L2CAP_MOVE_CHAN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5402 | l2cap_move_channel_rsp(conn, cmd, cmd_len, data); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5403 | break; |
| 5404 | |
| 5405 | case L2CAP_MOVE_CHAN_CFM: |
| 5406 | err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); |
| 5407 | break; |
| 5408 | |
| 5409 | case L2CAP_MOVE_CHAN_CFM_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5410 | l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5411 | break; |
| 5412 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5413 | default: |
| 5414 | BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); |
| 5415 | err = -EINVAL; |
| 5416 | break; |
| 5417 | } |
| 5418 | |
| 5419 | return err; |
| 5420 | } |
| 5421 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5422 | static int l2cap_le_connect_req(struct l2cap_conn *conn, |
| 5423 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5424 | u8 *data) |
| 5425 | { |
| 5426 | struct l2cap_le_conn_req *req = (struct l2cap_le_conn_req *) data; |
| 5427 | struct l2cap_le_conn_rsp rsp; |
| 5428 | struct l2cap_chan *chan, *pchan; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5429 | u16 dcid, scid, credits, mtu, mps; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5430 | __le16 psm; |
| 5431 | u8 result; |
| 5432 | |
| 5433 | if (cmd_len != sizeof(*req)) |
| 5434 | return -EPROTO; |
| 5435 | |
| 5436 | scid = __le16_to_cpu(req->scid); |
| 5437 | mtu = __le16_to_cpu(req->mtu); |
| 5438 | mps = __le16_to_cpu(req->mps); |
| 5439 | psm = req->psm; |
| 5440 | dcid = 0; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5441 | credits = 0; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5442 | |
| 5443 | if (mtu < 23 || mps < 23) |
| 5444 | return -EPROTO; |
| 5445 | |
| 5446 | BT_DBG("psm 0x%2.2x scid 0x%4.4x mtu %u mps %u", __le16_to_cpu(psm), |
| 5447 | scid, mtu, mps); |
| 5448 | |
| 5449 | /* Check if we have socket listening on psm */ |
| 5450 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src, |
| 5451 | &conn->hcon->dst, LE_LINK); |
| 5452 | if (!pchan) { |
| 5453 | result = L2CAP_CR_BAD_PSM; |
| 5454 | chan = NULL; |
| 5455 | goto response; |
| 5456 | } |
| 5457 | |
| 5458 | mutex_lock(&conn->chan_lock); |
| 5459 | l2cap_chan_lock(pchan); |
| 5460 | |
Johan Hedberg | 35dc6f8 | 2014-11-13 10:55:18 +0200 | [diff] [blame] | 5461 | if (!smp_sufficient_security(conn->hcon, pchan->sec_level, |
| 5462 | SMP_ALLOW_STK)) { |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5463 | result = L2CAP_CR_AUTHENTICATION; |
| 5464 | chan = NULL; |
| 5465 | goto response_unlock; |
| 5466 | } |
| 5467 | |
Johan Hedberg | 8a7889c | 2015-11-02 14:39:15 +0200 | [diff] [blame] | 5468 | /* Check for valid dynamic CID range */ |
| 5469 | if (scid < L2CAP_CID_DYN_START || scid > L2CAP_CID_LE_DYN_END) { |
| 5470 | result = L2CAP_CR_INVALID_SCID; |
| 5471 | chan = NULL; |
| 5472 | goto response_unlock; |
| 5473 | } |
| 5474 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5475 | /* Check if we already have channel with that dcid */ |
| 5476 | if (__l2cap_get_chan_by_dcid(conn, scid)) { |
Johan Hedberg | 8a7889c | 2015-11-02 14:39:15 +0200 | [diff] [blame] | 5477 | result = L2CAP_CR_SCID_IN_USE; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5478 | chan = NULL; |
| 5479 | goto response_unlock; |
| 5480 | } |
| 5481 | |
| 5482 | chan = pchan->ops->new_connection(pchan); |
| 5483 | if (!chan) { |
| 5484 | result = L2CAP_CR_NO_MEM; |
| 5485 | goto response_unlock; |
| 5486 | } |
| 5487 | |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 5488 | l2cap_le_flowctl_init(chan); |
| 5489 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5490 | bacpy(&chan->src, &conn->hcon->src); |
| 5491 | bacpy(&chan->dst, &conn->hcon->dst); |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 5492 | chan->src_type = bdaddr_src_type(conn->hcon); |
| 5493 | chan->dst_type = bdaddr_dst_type(conn->hcon); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5494 | chan->psm = psm; |
| 5495 | chan->dcid = scid; |
| 5496 | chan->omtu = mtu; |
| 5497 | chan->remote_mps = mps; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5498 | chan->tx_credits = __le16_to_cpu(req->credits); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5499 | |
| 5500 | __l2cap_chan_add(conn, chan); |
| 5501 | dcid = chan->scid; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5502 | credits = chan->rx_credits; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5503 | |
| 5504 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
| 5505 | |
| 5506 | chan->ident = cmd->ident; |
| 5507 | |
| 5508 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
| 5509 | l2cap_state_change(chan, BT_CONNECT2); |
Johan Hedberg | 434714d | 2014-09-01 09:45:03 +0300 | [diff] [blame] | 5510 | /* The following result value is actually not defined |
| 5511 | * for LE CoC but we use it to let the function know |
| 5512 | * that it should bail out after doing its cleanup |
| 5513 | * instead of sending a response. |
| 5514 | */ |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5515 | result = L2CAP_CR_PEND; |
| 5516 | chan->ops->defer(chan); |
| 5517 | } else { |
| 5518 | l2cap_chan_ready(chan); |
| 5519 | result = L2CAP_CR_SUCCESS; |
| 5520 | } |
| 5521 | |
| 5522 | response_unlock: |
| 5523 | l2cap_chan_unlock(pchan); |
| 5524 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 5525 | l2cap_chan_put(pchan); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5526 | |
| 5527 | if (result == L2CAP_CR_PEND) |
| 5528 | return 0; |
| 5529 | |
| 5530 | response: |
| 5531 | if (chan) { |
| 5532 | rsp.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 5533 | rsp.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5534 | } else { |
| 5535 | rsp.mtu = 0; |
| 5536 | rsp.mps = 0; |
| 5537 | } |
| 5538 | |
| 5539 | rsp.dcid = cpu_to_le16(dcid); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5540 | rsp.credits = cpu_to_le16(credits); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5541 | rsp.result = cpu_to_le16(result); |
| 5542 | |
| 5543 | l2cap_send_cmd(conn, cmd->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), &rsp); |
| 5544 | |
| 5545 | return 0; |
| 5546 | } |
| 5547 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5548 | static inline int l2cap_le_credits(struct l2cap_conn *conn, |
| 5549 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5550 | u8 *data) |
| 5551 | { |
| 5552 | struct l2cap_le_credits *pkt; |
| 5553 | struct l2cap_chan *chan; |
Johan Hedberg | 0f1bfe4 | 2014-01-27 15:11:35 -0800 | [diff] [blame] | 5554 | u16 cid, credits, max_credits; |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5555 | |
| 5556 | if (cmd_len != sizeof(*pkt)) |
| 5557 | return -EPROTO; |
| 5558 | |
| 5559 | pkt = (struct l2cap_le_credits *) data; |
| 5560 | cid = __le16_to_cpu(pkt->cid); |
| 5561 | credits = __le16_to_cpu(pkt->credits); |
| 5562 | |
| 5563 | BT_DBG("cid 0x%4.4x credits 0x%4.4x", cid, credits); |
| 5564 | |
| 5565 | chan = l2cap_get_chan_by_dcid(conn, cid); |
| 5566 | if (!chan) |
| 5567 | return -EBADSLT; |
| 5568 | |
Johan Hedberg | 0f1bfe4 | 2014-01-27 15:11:35 -0800 | [diff] [blame] | 5569 | max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits; |
| 5570 | if (credits > max_credits) { |
| 5571 | BT_ERR("LE credits overflow"); |
| 5572 | l2cap_send_disconn_req(chan, ECONNRESET); |
Martin Townsend | ee93053 | 2014-10-13 19:24:45 +0100 | [diff] [blame] | 5573 | l2cap_chan_unlock(chan); |
Johan Hedberg | 0f1bfe4 | 2014-01-27 15:11:35 -0800 | [diff] [blame] | 5574 | |
| 5575 | /* Return 0 so that we don't trigger an unnecessary |
| 5576 | * command reject packet. |
| 5577 | */ |
| 5578 | return 0; |
| 5579 | } |
| 5580 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5581 | chan->tx_credits += credits; |
| 5582 | |
Luiz Augusto von Dentz | 8a505b7 | 2017-04-11 22:21:01 +0300 | [diff] [blame] | 5583 | /* Resume sending */ |
| 5584 | l2cap_le_flowctl_send(chan); |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5585 | |
| 5586 | if (chan->tx_credits) |
| 5587 | chan->ops->resume(chan); |
| 5588 | |
| 5589 | l2cap_chan_unlock(chan); |
| 5590 | |
| 5591 | return 0; |
| 5592 | } |
| 5593 | |
Johan Hedberg | 71fb419 | 2013-12-10 10:52:48 +0200 | [diff] [blame] | 5594 | static inline int l2cap_le_command_rej(struct l2cap_conn *conn, |
| 5595 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5596 | u8 *data) |
| 5597 | { |
| 5598 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
| 5599 | struct l2cap_chan *chan; |
| 5600 | |
| 5601 | if (cmd_len < sizeof(*rej)) |
| 5602 | return -EPROTO; |
| 5603 | |
| 5604 | mutex_lock(&conn->chan_lock); |
| 5605 | |
| 5606 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 5607 | if (!chan) |
| 5608 | goto done; |
| 5609 | |
| 5610 | l2cap_chan_lock(chan); |
| 5611 | l2cap_chan_del(chan, ECONNREFUSED); |
| 5612 | l2cap_chan_unlock(chan); |
| 5613 | |
| 5614 | done: |
| 5615 | mutex_unlock(&conn->chan_lock); |
| 5616 | return 0; |
| 5617 | } |
| 5618 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5619 | static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5620 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5621 | u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5622 | { |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5623 | int err = 0; |
| 5624 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5625 | switch (cmd->code) { |
| 5626 | case L2CAP_COMMAND_REJ: |
Johan Hedberg | 71fb419 | 2013-12-10 10:52:48 +0200 | [diff] [blame] | 5627 | l2cap_le_command_rej(conn, cmd, cmd_len, data); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5628 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5629 | |
| 5630 | case L2CAP_CONN_PARAM_UPDATE_REQ: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5631 | err = l2cap_conn_param_update_req(conn, cmd, cmd_len, data); |
| 5632 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5633 | |
| 5634 | case L2CAP_CONN_PARAM_UPDATE_RSP: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5635 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5636 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5637 | case L2CAP_LE_CONN_RSP: |
| 5638 | l2cap_le_connect_rsp(conn, cmd, cmd_len, data); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5639 | break; |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5640 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5641 | case L2CAP_LE_CONN_REQ: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5642 | err = l2cap_le_connect_req(conn, cmd, cmd_len, data); |
| 5643 | break; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5644 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5645 | case L2CAP_LE_CREDITS: |
| 5646 | err = l2cap_le_credits(conn, cmd, cmd_len, data); |
| 5647 | break; |
| 5648 | |
Johan Hedberg | 3defe01 | 2013-05-15 10:16:06 +0300 | [diff] [blame] | 5649 | case L2CAP_DISCONN_REQ: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5650 | err = l2cap_disconnect_req(conn, cmd, cmd_len, data); |
| 5651 | break; |
Johan Hedberg | 3defe01 | 2013-05-15 10:16:06 +0300 | [diff] [blame] | 5652 | |
| 5653 | case L2CAP_DISCONN_RSP: |
| 5654 | l2cap_disconnect_rsp(conn, cmd, cmd_len, data); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5655 | break; |
Johan Hedberg | 3defe01 | 2013-05-15 10:16:06 +0300 | [diff] [blame] | 5656 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5657 | default: |
| 5658 | BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5659 | err = -EINVAL; |
| 5660 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5661 | } |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5662 | |
| 5663 | return err; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5664 | } |
| 5665 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5666 | static inline void l2cap_le_sig_channel(struct l2cap_conn *conn, |
| 5667 | struct sk_buff *skb) |
| 5668 | { |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5669 | struct hci_conn *hcon = conn->hcon; |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5670 | struct l2cap_cmd_hdr *cmd; |
| 5671 | u16 len; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5672 | int err; |
| 5673 | |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5674 | if (hcon->type != LE_LINK) |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5675 | goto drop; |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5676 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5677 | if (skb->len < L2CAP_CMD_HDR_SIZE) |
| 5678 | goto drop; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5679 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5680 | cmd = (void *) skb->data; |
| 5681 | skb_pull(skb, L2CAP_CMD_HDR_SIZE); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5682 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5683 | len = le16_to_cpu(cmd->len); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5684 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5685 | 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] | 5686 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5687 | if (len != skb->len || !cmd->ident) { |
| 5688 | BT_DBG("corrupted command"); |
| 5689 | goto drop; |
| 5690 | } |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5691 | |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5692 | err = l2cap_le_sig_cmd(conn, cmd, len, skb->data); |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5693 | if (err) { |
| 5694 | struct l2cap_cmd_rej_unk rej; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5695 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5696 | BT_ERR("Wrong link type (%d)", err); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5697 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5698 | rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5699 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
| 5700 | sizeof(rej), &rej); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5701 | } |
| 5702 | |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5703 | drop: |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5704 | kfree_skb(skb); |
| 5705 | } |
| 5706 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5707 | static inline void l2cap_sig_channel(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5708 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5709 | { |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5710 | struct hci_conn *hcon = conn->hcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5711 | u8 *data = skb->data; |
| 5712 | int len = skb->len; |
| 5713 | struct l2cap_cmd_hdr cmd; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5714 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5715 | |
| 5716 | l2cap_raw_recv(conn, skb); |
| 5717 | |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5718 | if (hcon->type != ACL_LINK) |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5719 | goto drop; |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5721 | while (len >= L2CAP_CMD_HDR_SIZE) { |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5722 | u16 cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 5724 | data += L2CAP_CMD_HDR_SIZE; |
| 5725 | len -= L2CAP_CMD_HDR_SIZE; |
| 5726 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5727 | cmd_len = le16_to_cpu(cmd.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5729 | BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, |
| 5730 | cmd.ident); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5732 | if (cmd_len > len || !cmd.ident) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5733 | BT_DBG("corrupted command"); |
| 5734 | break; |
| 5735 | } |
| 5736 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5737 | err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5738 | if (err) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 5739 | struct l2cap_cmd_rej_unk rej; |
Gustavo F. Padovan | 2c6d1a2 | 2011-03-23 14:38:32 -0300 | [diff] [blame] | 5740 | |
| 5741 | BT_ERR("Wrong link type (%d)", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5742 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5743 | rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5744 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, |
| 5745 | sizeof(rej), &rej); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | } |
| 5747 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5748 | data += cmd_len; |
| 5749 | len -= cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5750 | } |
| 5751 | |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5752 | drop: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | kfree_skb(skb); |
| 5754 | } |
| 5755 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5756 | 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] | 5757 | { |
| 5758 | u16 our_fcs, rcv_fcs; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 5759 | int hdr_size; |
| 5760 | |
| 5761 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 5762 | hdr_size = L2CAP_EXT_HDR_SIZE; |
| 5763 | else |
| 5764 | hdr_size = L2CAP_ENH_HDR_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5765 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5766 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5767 | skb_trim(skb, skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5768 | rcv_fcs = get_unaligned_le16(skb->data + skb->len); |
| 5769 | our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); |
| 5770 | |
| 5771 | if (our_fcs != rcv_fcs) |
João Paulo Rechi Vita | 7a560e5 | 2010-06-22 13:56:27 -0300 | [diff] [blame] | 5772 | return -EBADMSG; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5773 | } |
| 5774 | return 0; |
| 5775 | } |
| 5776 | |
Mat Martineau | 6ea0048 | 2012-05-17 20:53:52 -0700 | [diff] [blame] | 5777 | 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] | 5778 | { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5779 | struct l2cap_ctrl control; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5780 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5781 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5782 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5783 | memset(&control, 0, sizeof(control)); |
| 5784 | control.sframe = 1; |
| 5785 | control.final = 1; |
| 5786 | control.reqseq = chan->buffer_seq; |
| 5787 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5788 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5789 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5790 | control.super = L2CAP_SUPER_RNR; |
| 5791 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5792 | } |
| 5793 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5794 | if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
| 5795 | chan->unacked_frames > 0) |
| 5796 | __set_retrans_timer(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5797 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5798 | /* Send pending iframes */ |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 5799 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5800 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5801 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5802 | test_bit(CONN_SEND_FBIT, &chan->conn_state)) { |
| 5803 | /* F-bit wasn't sent in an s-frame or i-frame yet, so |
| 5804 | * send it now. |
| 5805 | */ |
| 5806 | control.super = L2CAP_SUPER_RR; |
| 5807 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5808 | } |
| 5809 | } |
| 5810 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5811 | static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag, |
| 5812 | struct sk_buff **last_frag) |
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 | /* skb->len reflects data in skb as well as all fragments |
| 5815 | * skb->data_len reflects only data in fragments |
| 5816 | */ |
| 5817 | if (!skb_has_frag_list(skb)) |
| 5818 | skb_shinfo(skb)->frag_list = new_frag; |
| 5819 | |
| 5820 | new_frag->next = NULL; |
| 5821 | |
| 5822 | (*last_frag)->next = new_frag; |
| 5823 | *last_frag = new_frag; |
| 5824 | |
| 5825 | skb->len += new_frag->len; |
| 5826 | skb->data_len += new_frag->len; |
| 5827 | skb->truesize += new_frag->truesize; |
| 5828 | } |
| 5829 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5830 | static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, |
| 5831 | struct l2cap_ctrl *control) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5832 | { |
| 5833 | int err = -EINVAL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5834 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5835 | switch (control->sar) { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5836 | case L2CAP_SAR_UNSEGMENTED: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5837 | if (chan->sdu) |
| 5838 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5839 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5840 | err = chan->ops->recv(chan, skb); |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5841 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5842 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5843 | case L2CAP_SAR_START: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5844 | if (chan->sdu) |
| 5845 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5846 | |
Daniel Borkmann | dbb5088 | 2016-07-27 11:40:14 -0700 | [diff] [blame] | 5847 | if (!pskb_may_pull(skb, L2CAP_SDULEN_SIZE)) |
| 5848 | break; |
| 5849 | |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5850 | chan->sdu_len = get_unaligned_le16(skb->data); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5851 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5852 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5853 | if (chan->sdu_len > chan->imtu) { |
| 5854 | err = -EMSGSIZE; |
| 5855 | break; |
| 5856 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5857 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5858 | if (skb->len >= chan->sdu_len) |
| 5859 | break; |
| 5860 | |
| 5861 | chan->sdu = skb; |
| 5862 | chan->sdu_last_frag = skb; |
| 5863 | |
| 5864 | skb = NULL; |
| 5865 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5866 | break; |
| 5867 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5868 | case L2CAP_SAR_CONTINUE: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5869 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5870 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5871 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5872 | append_skb_frag(chan->sdu, skb, |
| 5873 | &chan->sdu_last_frag); |
| 5874 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5875 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5876 | if (chan->sdu->len >= chan->sdu_len) |
| 5877 | break; |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5878 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5879 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5880 | break; |
| 5881 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5882 | case L2CAP_SAR_END: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5883 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5884 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5885 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5886 | append_skb_frag(chan->sdu, skb, |
| 5887 | &chan->sdu_last_frag); |
| 5888 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5889 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5890 | if (chan->sdu->len != chan->sdu_len) |
| 5891 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5892 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5893 | err = chan->ops->recv(chan, chan->sdu); |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5894 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5895 | if (!err) { |
| 5896 | /* Reassembly complete */ |
| 5897 | chan->sdu = NULL; |
| 5898 | chan->sdu_last_frag = NULL; |
| 5899 | chan->sdu_len = 0; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5900 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5901 | break; |
| 5902 | } |
| 5903 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5904 | if (err) { |
| 5905 | kfree_skb(skb); |
| 5906 | kfree_skb(chan->sdu); |
| 5907 | chan->sdu = NULL; |
| 5908 | chan->sdu_last_frag = NULL; |
| 5909 | chan->sdu_len = 0; |
| 5910 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5911 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5912 | return err; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5913 | } |
| 5914 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 5915 | static int l2cap_resegment(struct l2cap_chan *chan) |
| 5916 | { |
| 5917 | /* Placeholder */ |
| 5918 | return 0; |
| 5919 | } |
| 5920 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 5921 | void l2cap_chan_busy(struct l2cap_chan *chan, int busy) |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 5922 | { |
Mat Martineau | 61aa4f5 | 2012-05-17 20:53:40 -0700 | [diff] [blame] | 5923 | u8 event; |
| 5924 | |
| 5925 | if (chan->mode != L2CAP_MODE_ERTM) |
| 5926 | return; |
| 5927 | |
| 5928 | event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR; |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 5929 | l2cap_tx(chan, NULL, NULL, event); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5930 | } |
| 5931 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5932 | static int l2cap_rx_queued_iframes(struct l2cap_chan *chan) |
| 5933 | { |
Mat Martineau | 6383872 | 2012-05-17 20:53:45 -0700 | [diff] [blame] | 5934 | int err = 0; |
| 5935 | /* Pass sequential frames to l2cap_reassemble_sdu() |
| 5936 | * until a gap is encountered. |
| 5937 | */ |
| 5938 | |
| 5939 | BT_DBG("chan %p", chan); |
| 5940 | |
| 5941 | while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5942 | struct sk_buff *skb; |
| 5943 | BT_DBG("Searching for skb with txseq %d (queue len %d)", |
| 5944 | chan->buffer_seq, skb_queue_len(&chan->srej_q)); |
| 5945 | |
| 5946 | skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq); |
| 5947 | |
| 5948 | if (!skb) |
| 5949 | break; |
| 5950 | |
| 5951 | skb_unlink(skb, &chan->srej_q); |
| 5952 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 5953 | err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->l2cap); |
Mat Martineau | 6383872 | 2012-05-17 20:53:45 -0700 | [diff] [blame] | 5954 | if (err) |
| 5955 | break; |
| 5956 | } |
| 5957 | |
| 5958 | if (skb_queue_empty(&chan->srej_q)) { |
| 5959 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 5960 | l2cap_send_ack(chan); |
| 5961 | } |
| 5962 | |
| 5963 | return err; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5964 | } |
| 5965 | |
| 5966 | static void l2cap_handle_srej(struct l2cap_chan *chan, |
| 5967 | struct l2cap_ctrl *control) |
| 5968 | { |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5969 | struct sk_buff *skb; |
| 5970 | |
| 5971 | BT_DBG("chan %p, control %p", chan, control); |
| 5972 | |
| 5973 | if (control->reqseq == chan->next_tx_seq) { |
| 5974 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5975 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5976 | return; |
| 5977 | } |
| 5978 | |
| 5979 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 5980 | |
| 5981 | if (skb == NULL) { |
| 5982 | BT_DBG("Seq %d not available for retransmission", |
| 5983 | control->reqseq); |
| 5984 | return; |
| 5985 | } |
| 5986 | |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 5987 | if (chan->max_tx != 0 && bt_cb(skb)->l2cap.retries >= chan->max_tx) { |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5988 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5989 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5990 | return; |
| 5991 | } |
| 5992 | |
| 5993 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5994 | |
| 5995 | if (control->poll) { |
| 5996 | l2cap_pass_to_tx(chan, control); |
| 5997 | |
| 5998 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 5999 | l2cap_retransmit(chan, control); |
| 6000 | l2cap_ertm_send(chan); |
| 6001 | |
| 6002 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 6003 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 6004 | chan->srej_save_reqseq = control->reqseq; |
| 6005 | } |
| 6006 | } else { |
| 6007 | l2cap_pass_to_tx_fbit(chan, control); |
| 6008 | |
| 6009 | if (control->final) { |
| 6010 | if (chan->srej_save_reqseq != control->reqseq || |
| 6011 | !test_and_clear_bit(CONN_SREJ_ACT, |
| 6012 | &chan->conn_state)) |
| 6013 | l2cap_retransmit(chan, control); |
| 6014 | } else { |
| 6015 | l2cap_retransmit(chan, control); |
| 6016 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 6017 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 6018 | chan->srej_save_reqseq = control->reqseq; |
| 6019 | } |
| 6020 | } |
| 6021 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6022 | } |
| 6023 | |
| 6024 | static void l2cap_handle_rej(struct l2cap_chan *chan, |
| 6025 | struct l2cap_ctrl *control) |
| 6026 | { |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 6027 | struct sk_buff *skb; |
| 6028 | |
| 6029 | BT_DBG("chan %p, control %p", chan, control); |
| 6030 | |
| 6031 | if (control->reqseq == chan->next_tx_seq) { |
| 6032 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6033 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 6034 | return; |
| 6035 | } |
| 6036 | |
| 6037 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 6038 | |
| 6039 | if (chan->max_tx && skb && |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 6040 | bt_cb(skb)->l2cap.retries >= chan->max_tx) { |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 6041 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6042 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 6043 | return; |
| 6044 | } |
| 6045 | |
| 6046 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6047 | |
| 6048 | l2cap_pass_to_tx(chan, control); |
| 6049 | |
| 6050 | if (control->final) { |
| 6051 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
| 6052 | l2cap_retransmit_all(chan, control); |
| 6053 | } else { |
| 6054 | l2cap_retransmit_all(chan, control); |
| 6055 | l2cap_ertm_send(chan); |
| 6056 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) |
| 6057 | set_bit(CONN_REJ_ACT, &chan->conn_state); |
| 6058 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6059 | } |
| 6060 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6061 | static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq) |
| 6062 | { |
| 6063 | BT_DBG("chan %p, txseq %d", chan, txseq); |
| 6064 | |
| 6065 | BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq, |
| 6066 | chan->expected_tx_seq); |
| 6067 | |
| 6068 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 6069 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6070 | chan->tx_win) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6071 | /* See notes below regarding "double poll" and |
| 6072 | * invalid packets. |
| 6073 | */ |
| 6074 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 6075 | BT_DBG("Invalid/Ignore - after SREJ"); |
| 6076 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 6077 | } else { |
| 6078 | BT_DBG("Invalid - in window after SREJ sent"); |
| 6079 | return L2CAP_TXSEQ_INVALID; |
| 6080 | } |
| 6081 | } |
| 6082 | |
| 6083 | if (chan->srej_list.head == txseq) { |
| 6084 | BT_DBG("Expected SREJ"); |
| 6085 | return L2CAP_TXSEQ_EXPECTED_SREJ; |
| 6086 | } |
| 6087 | |
| 6088 | if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) { |
| 6089 | BT_DBG("Duplicate SREJ - txseq already stored"); |
| 6090 | return L2CAP_TXSEQ_DUPLICATE_SREJ; |
| 6091 | } |
| 6092 | |
| 6093 | if (l2cap_seq_list_contains(&chan->srej_list, txseq)) { |
| 6094 | BT_DBG("Unexpected SREJ - not requested"); |
| 6095 | return L2CAP_TXSEQ_UNEXPECTED_SREJ; |
| 6096 | } |
| 6097 | } |
| 6098 | |
| 6099 | if (chan->expected_tx_seq == txseq) { |
| 6100 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
| 6101 | chan->tx_win) { |
| 6102 | BT_DBG("Invalid - txseq outside tx window"); |
| 6103 | return L2CAP_TXSEQ_INVALID; |
| 6104 | } else { |
| 6105 | BT_DBG("Expected"); |
| 6106 | return L2CAP_TXSEQ_EXPECTED; |
| 6107 | } |
| 6108 | } |
| 6109 | |
| 6110 | if (__seq_offset(chan, txseq, chan->last_acked_seq) < |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6111 | __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6112 | BT_DBG("Duplicate - expected_tx_seq later than txseq"); |
| 6113 | return L2CAP_TXSEQ_DUPLICATE; |
| 6114 | } |
| 6115 | |
| 6116 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) { |
| 6117 | /* A source of invalid packets is a "double poll" condition, |
| 6118 | * where delays cause us to send multiple poll packets. If |
| 6119 | * the remote stack receives and processes both polls, |
| 6120 | * sequence numbers can wrap around in such a way that a |
| 6121 | * resent frame has a sequence number that looks like new data |
| 6122 | * with a sequence gap. This would trigger an erroneous SREJ |
| 6123 | * request. |
| 6124 | * |
| 6125 | * Fortunately, this is impossible with a tx window that's |
| 6126 | * less than half of the maximum sequence number, which allows |
| 6127 | * invalid frames to be safely ignored. |
| 6128 | * |
| 6129 | * With tx window sizes greater than half of the tx window |
| 6130 | * maximum, the frame is invalid and cannot be ignored. This |
| 6131 | * causes a disconnect. |
| 6132 | */ |
| 6133 | |
| 6134 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 6135 | BT_DBG("Invalid/Ignore - txseq outside tx window"); |
| 6136 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 6137 | } else { |
| 6138 | BT_DBG("Invalid - txseq outside tx window"); |
| 6139 | return L2CAP_TXSEQ_INVALID; |
| 6140 | } |
| 6141 | } else { |
| 6142 | BT_DBG("Unexpected - txseq indicates missing frames"); |
| 6143 | return L2CAP_TXSEQ_UNEXPECTED; |
| 6144 | } |
| 6145 | } |
| 6146 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6147 | static int l2cap_rx_state_recv(struct l2cap_chan *chan, |
| 6148 | struct l2cap_ctrl *control, |
| 6149 | struct sk_buff *skb, u8 event) |
| 6150 | { |
| 6151 | int err = 0; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6152 | bool skb_in_use = false; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6153 | |
| 6154 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6155 | event); |
| 6156 | |
| 6157 | switch (event) { |
| 6158 | case L2CAP_EV_RECV_IFRAME: |
| 6159 | switch (l2cap_classify_txseq(chan, control->txseq)) { |
| 6160 | case L2CAP_TXSEQ_EXPECTED: |
| 6161 | l2cap_pass_to_tx(chan, control); |
| 6162 | |
| 6163 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 6164 | BT_DBG("Busy, discarding expected seq %d", |
| 6165 | control->txseq); |
| 6166 | break; |
| 6167 | } |
| 6168 | |
| 6169 | chan->expected_tx_seq = __next_seq(chan, |
| 6170 | control->txseq); |
| 6171 | |
| 6172 | chan->buffer_seq = chan->expected_tx_seq; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6173 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6174 | |
| 6175 | err = l2cap_reassemble_sdu(chan, skb, control); |
| 6176 | if (err) |
| 6177 | break; |
| 6178 | |
| 6179 | if (control->final) { |
| 6180 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 6181 | &chan->conn_state)) { |
| 6182 | control->final = 0; |
| 6183 | l2cap_retransmit_all(chan, control); |
| 6184 | l2cap_ertm_send(chan); |
| 6185 | } |
| 6186 | } |
| 6187 | |
| 6188 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 6189 | l2cap_send_ack(chan); |
| 6190 | break; |
| 6191 | case L2CAP_TXSEQ_UNEXPECTED: |
| 6192 | l2cap_pass_to_tx(chan, control); |
| 6193 | |
| 6194 | /* Can't issue SREJ frames in the local busy state. |
| 6195 | * Drop this frame, it will be seen as missing |
| 6196 | * when local busy is exited. |
| 6197 | */ |
| 6198 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 6199 | BT_DBG("Busy, discarding unexpected seq %d", |
| 6200 | control->txseq); |
| 6201 | break; |
| 6202 | } |
| 6203 | |
| 6204 | /* There was a gap in the sequence, so an SREJ |
| 6205 | * must be sent for each missing frame. The |
| 6206 | * current frame is stored for later use. |
| 6207 | */ |
| 6208 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6209 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6210 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6211 | skb_queue_len(&chan->srej_q)); |
| 6212 | |
| 6213 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 6214 | l2cap_seq_list_clear(&chan->srej_list); |
| 6215 | l2cap_send_srej(chan, control->txseq); |
| 6216 | |
| 6217 | chan->rx_state = L2CAP_RX_STATE_SREJ_SENT; |
| 6218 | break; |
| 6219 | case L2CAP_TXSEQ_DUPLICATE: |
| 6220 | l2cap_pass_to_tx(chan, control); |
| 6221 | break; |
| 6222 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 6223 | break; |
| 6224 | case L2CAP_TXSEQ_INVALID: |
| 6225 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6226 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6227 | break; |
| 6228 | } |
| 6229 | break; |
| 6230 | case L2CAP_EV_RECV_RR: |
| 6231 | l2cap_pass_to_tx(chan, control); |
| 6232 | if (control->final) { |
| 6233 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6234 | |
Mat Martineau | e6a3ee6 | 2012-10-23 15:24:22 -0700 | [diff] [blame] | 6235 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) && |
| 6236 | !__chan_is_moving(chan)) { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6237 | control->final = 0; |
| 6238 | l2cap_retransmit_all(chan, control); |
| 6239 | } |
| 6240 | |
| 6241 | l2cap_ertm_send(chan); |
| 6242 | } else if (control->poll) { |
| 6243 | l2cap_send_i_or_rr_or_rnr(chan); |
| 6244 | } else { |
| 6245 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6246 | &chan->conn_state) && |
| 6247 | chan->unacked_frames) |
| 6248 | __set_retrans_timer(chan); |
| 6249 | |
| 6250 | l2cap_ertm_send(chan); |
| 6251 | } |
| 6252 | break; |
| 6253 | case L2CAP_EV_RECV_RNR: |
| 6254 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6255 | l2cap_pass_to_tx(chan, control); |
| 6256 | if (control && control->poll) { |
| 6257 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6258 | l2cap_send_rr_or_rnr(chan, 0); |
| 6259 | } |
| 6260 | __clear_retrans_timer(chan); |
| 6261 | l2cap_seq_list_clear(&chan->retrans_list); |
| 6262 | break; |
| 6263 | case L2CAP_EV_RECV_REJ: |
| 6264 | l2cap_handle_rej(chan, control); |
| 6265 | break; |
| 6266 | case L2CAP_EV_RECV_SREJ: |
| 6267 | l2cap_handle_srej(chan, control); |
| 6268 | break; |
| 6269 | default: |
| 6270 | break; |
| 6271 | } |
| 6272 | |
| 6273 | if (skb && !skb_in_use) { |
| 6274 | BT_DBG("Freeing %p", skb); |
| 6275 | kfree_skb(skb); |
| 6276 | } |
| 6277 | |
| 6278 | return err; |
| 6279 | } |
| 6280 | |
| 6281 | static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan, |
| 6282 | struct l2cap_ctrl *control, |
| 6283 | struct sk_buff *skb, u8 event) |
| 6284 | { |
| 6285 | int err = 0; |
| 6286 | u16 txseq = control->txseq; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6287 | bool skb_in_use = false; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6288 | |
| 6289 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6290 | event); |
| 6291 | |
| 6292 | switch (event) { |
| 6293 | case L2CAP_EV_RECV_IFRAME: |
| 6294 | switch (l2cap_classify_txseq(chan, txseq)) { |
| 6295 | case L2CAP_TXSEQ_EXPECTED: |
| 6296 | /* Keep frame for reassembly later */ |
| 6297 | l2cap_pass_to_tx(chan, control); |
| 6298 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6299 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6300 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6301 | skb_queue_len(&chan->srej_q)); |
| 6302 | |
| 6303 | chan->expected_tx_seq = __next_seq(chan, txseq); |
| 6304 | break; |
| 6305 | case L2CAP_TXSEQ_EXPECTED_SREJ: |
| 6306 | l2cap_seq_list_pop(&chan->srej_list); |
| 6307 | |
| 6308 | l2cap_pass_to_tx(chan, control); |
| 6309 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6310 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6311 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6312 | skb_queue_len(&chan->srej_q)); |
| 6313 | |
| 6314 | err = l2cap_rx_queued_iframes(chan); |
| 6315 | if (err) |
| 6316 | break; |
| 6317 | |
| 6318 | break; |
| 6319 | case L2CAP_TXSEQ_UNEXPECTED: |
| 6320 | /* Got a frame that can't be reassembled yet. |
| 6321 | * Save it for later, and send SREJs to cover |
| 6322 | * the missing frames. |
| 6323 | */ |
| 6324 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6325 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6326 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6327 | skb_queue_len(&chan->srej_q)); |
| 6328 | |
| 6329 | l2cap_pass_to_tx(chan, control); |
| 6330 | l2cap_send_srej(chan, control->txseq); |
| 6331 | break; |
| 6332 | case L2CAP_TXSEQ_UNEXPECTED_SREJ: |
| 6333 | /* This frame was requested with an SREJ, but |
| 6334 | * some expected retransmitted frames are |
| 6335 | * missing. Request retransmission of missing |
| 6336 | * SREJ'd frames. |
| 6337 | */ |
| 6338 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6339 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6340 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6341 | skb_queue_len(&chan->srej_q)); |
| 6342 | |
| 6343 | l2cap_pass_to_tx(chan, control); |
| 6344 | l2cap_send_srej_list(chan, control->txseq); |
| 6345 | break; |
| 6346 | case L2CAP_TXSEQ_DUPLICATE_SREJ: |
| 6347 | /* We've already queued this frame. Drop this copy. */ |
| 6348 | l2cap_pass_to_tx(chan, control); |
| 6349 | break; |
| 6350 | case L2CAP_TXSEQ_DUPLICATE: |
| 6351 | /* Expecting a later sequence number, so this frame |
| 6352 | * was already received. Ignore it completely. |
| 6353 | */ |
| 6354 | break; |
| 6355 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 6356 | break; |
| 6357 | case L2CAP_TXSEQ_INVALID: |
| 6358 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6359 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6360 | break; |
| 6361 | } |
| 6362 | break; |
| 6363 | case L2CAP_EV_RECV_RR: |
| 6364 | l2cap_pass_to_tx(chan, control); |
| 6365 | if (control->final) { |
| 6366 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6367 | |
| 6368 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 6369 | &chan->conn_state)) { |
| 6370 | control->final = 0; |
| 6371 | l2cap_retransmit_all(chan, control); |
| 6372 | } |
| 6373 | |
| 6374 | l2cap_ertm_send(chan); |
| 6375 | } else if (control->poll) { |
| 6376 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6377 | &chan->conn_state) && |
| 6378 | chan->unacked_frames) { |
| 6379 | __set_retrans_timer(chan); |
| 6380 | } |
| 6381 | |
| 6382 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6383 | l2cap_send_srej_tail(chan); |
| 6384 | } else { |
| 6385 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6386 | &chan->conn_state) && |
| 6387 | chan->unacked_frames) |
| 6388 | __set_retrans_timer(chan); |
| 6389 | |
| 6390 | l2cap_send_ack(chan); |
| 6391 | } |
| 6392 | break; |
| 6393 | case L2CAP_EV_RECV_RNR: |
| 6394 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6395 | l2cap_pass_to_tx(chan, control); |
| 6396 | if (control->poll) { |
| 6397 | l2cap_send_srej_tail(chan); |
| 6398 | } else { |
| 6399 | struct l2cap_ctrl rr_control; |
| 6400 | memset(&rr_control, 0, sizeof(rr_control)); |
| 6401 | rr_control.sframe = 1; |
| 6402 | rr_control.super = L2CAP_SUPER_RR; |
| 6403 | rr_control.reqseq = chan->buffer_seq; |
| 6404 | l2cap_send_sframe(chan, &rr_control); |
| 6405 | } |
| 6406 | |
| 6407 | break; |
| 6408 | case L2CAP_EV_RECV_REJ: |
| 6409 | l2cap_handle_rej(chan, control); |
| 6410 | break; |
| 6411 | case L2CAP_EV_RECV_SREJ: |
| 6412 | l2cap_handle_srej(chan, control); |
| 6413 | break; |
| 6414 | } |
| 6415 | |
| 6416 | if (skb && !skb_in_use) { |
| 6417 | BT_DBG("Freeing %p", skb); |
| 6418 | kfree_skb(skb); |
| 6419 | } |
| 6420 | |
| 6421 | return err; |
| 6422 | } |
| 6423 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6424 | static int l2cap_finish_move(struct l2cap_chan *chan) |
| 6425 | { |
| 6426 | BT_DBG("chan %p", chan); |
| 6427 | |
| 6428 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6429 | |
| 6430 | if (chan->hs_hcon) |
| 6431 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6432 | else |
| 6433 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6434 | |
| 6435 | return l2cap_resegment(chan); |
| 6436 | } |
| 6437 | |
| 6438 | static int l2cap_rx_state_wait_p(struct l2cap_chan *chan, |
| 6439 | struct l2cap_ctrl *control, |
| 6440 | struct sk_buff *skb, u8 event) |
| 6441 | { |
| 6442 | int err; |
| 6443 | |
| 6444 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6445 | event); |
| 6446 | |
| 6447 | if (!control->poll) |
| 6448 | return -EPROTO; |
| 6449 | |
| 6450 | l2cap_process_reqseq(chan, control->reqseq); |
| 6451 | |
| 6452 | if (!skb_queue_empty(&chan->tx_q)) |
| 6453 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6454 | else |
| 6455 | chan->tx_send_head = NULL; |
| 6456 | |
| 6457 | /* Rewind next_tx_seq to the point expected |
| 6458 | * by the receiver. |
| 6459 | */ |
| 6460 | chan->next_tx_seq = control->reqseq; |
| 6461 | chan->unacked_frames = 0; |
| 6462 | |
| 6463 | err = l2cap_finish_move(chan); |
| 6464 | if (err) |
| 6465 | return err; |
| 6466 | |
| 6467 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6468 | l2cap_send_i_or_rr_or_rnr(chan); |
| 6469 | |
| 6470 | if (event == L2CAP_EV_RECV_IFRAME) |
| 6471 | return -EPROTO; |
| 6472 | |
| 6473 | return l2cap_rx_state_recv(chan, control, NULL, event); |
| 6474 | } |
| 6475 | |
| 6476 | static int l2cap_rx_state_wait_f(struct l2cap_chan *chan, |
| 6477 | struct l2cap_ctrl *control, |
| 6478 | struct sk_buff *skb, u8 event) |
| 6479 | { |
| 6480 | int err; |
| 6481 | |
| 6482 | if (!control->final) |
| 6483 | return -EPROTO; |
| 6484 | |
| 6485 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6486 | |
| 6487 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6488 | l2cap_process_reqseq(chan, control->reqseq); |
| 6489 | |
| 6490 | if (!skb_queue_empty(&chan->tx_q)) |
| 6491 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6492 | else |
| 6493 | chan->tx_send_head = NULL; |
| 6494 | |
| 6495 | /* Rewind next_tx_seq to the point expected |
| 6496 | * by the receiver. |
| 6497 | */ |
| 6498 | chan->next_tx_seq = control->reqseq; |
| 6499 | chan->unacked_frames = 0; |
| 6500 | |
| 6501 | if (chan->hs_hcon) |
| 6502 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6503 | else |
| 6504 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6505 | |
| 6506 | err = l2cap_resegment(chan); |
| 6507 | |
| 6508 | if (!err) |
| 6509 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6510 | |
| 6511 | return err; |
| 6512 | } |
| 6513 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6514 | static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 6515 | { |
| 6516 | /* Make sure reqseq is for a packet that has been sent but not acked */ |
| 6517 | u16 unacked; |
| 6518 | |
| 6519 | unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq); |
| 6520 | return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked; |
| 6521 | } |
| 6522 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6523 | static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6524 | struct sk_buff *skb, u8 event) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6525 | { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6526 | int err = 0; |
| 6527 | |
| 6528 | BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan, |
| 6529 | control, skb, event, chan->rx_state); |
| 6530 | |
| 6531 | if (__valid_reqseq(chan, control->reqseq)) { |
| 6532 | switch (chan->rx_state) { |
| 6533 | case L2CAP_RX_STATE_RECV: |
| 6534 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6535 | break; |
| 6536 | case L2CAP_RX_STATE_SREJ_SENT: |
| 6537 | err = l2cap_rx_state_srej_sent(chan, control, skb, |
| 6538 | event); |
| 6539 | break; |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6540 | case L2CAP_RX_STATE_WAIT_P: |
| 6541 | err = l2cap_rx_state_wait_p(chan, control, skb, event); |
| 6542 | break; |
| 6543 | case L2CAP_RX_STATE_WAIT_F: |
| 6544 | err = l2cap_rx_state_wait_f(chan, control, skb, event); |
| 6545 | break; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6546 | default: |
| 6547 | /* shut it down */ |
| 6548 | break; |
| 6549 | } |
| 6550 | } else { |
| 6551 | BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d", |
| 6552 | control->reqseq, chan->next_tx_seq, |
| 6553 | chan->expected_ack_seq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6554 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6555 | } |
| 6556 | |
| 6557 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6558 | } |
| 6559 | |
| 6560 | static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6561 | struct sk_buff *skb) |
| 6562 | { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6563 | BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb, |
| 6564 | chan->rx_state); |
| 6565 | |
| 6566 | if (l2cap_classify_txseq(chan, control->txseq) == |
| 6567 | L2CAP_TXSEQ_EXPECTED) { |
| 6568 | l2cap_pass_to_tx(chan, control); |
| 6569 | |
| 6570 | BT_DBG("buffer_seq %d->%d", chan->buffer_seq, |
| 6571 | __next_seq(chan, chan->buffer_seq)); |
| 6572 | |
| 6573 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 6574 | |
| 6575 | l2cap_reassemble_sdu(chan, skb, control); |
| 6576 | } else { |
| 6577 | if (chan->sdu) { |
| 6578 | kfree_skb(chan->sdu); |
| 6579 | chan->sdu = NULL; |
| 6580 | } |
| 6581 | chan->sdu_last_frag = NULL; |
| 6582 | chan->sdu_len = 0; |
| 6583 | |
| 6584 | if (skb) { |
| 6585 | BT_DBG("Freeing %p", skb); |
| 6586 | kfree_skb(skb); |
| 6587 | } |
| 6588 | } |
| 6589 | |
| 6590 | chan->last_acked_seq = control->txseq; |
| 6591 | chan->expected_tx_seq = __next_seq(chan, control->txseq); |
| 6592 | |
Prasanna Karthik | 9a54421 | 2015-11-19 12:05:35 +0000 | [diff] [blame] | 6593 | return 0; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6594 | } |
| 6595 | |
| 6596 | static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) |
| 6597 | { |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 6598 | struct l2cap_ctrl *control = &bt_cb(skb)->l2cap; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6599 | u16 len; |
| 6600 | u8 event; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6601 | |
Mat Martineau | b76bbd6 | 2012-04-11 10:48:43 -0700 | [diff] [blame] | 6602 | __unpack_control(chan, skb); |
| 6603 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6604 | len = skb->len; |
| 6605 | |
| 6606 | /* |
| 6607 | * We can just drop the corrupted I-frame here. |
| 6608 | * Receiver will miss it and start proper recovery |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6609 | * procedures and ask for retransmission. |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6610 | */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6611 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6612 | goto drop; |
| 6613 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6614 | if (!control->sframe && control->sar == L2CAP_SAR_START) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6615 | len -= L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6616 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6617 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6618 | len -= L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6619 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6620 | if (len > chan->mps) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6621 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6622 | goto drop; |
| 6623 | } |
| 6624 | |
Daniel Borkmann | dbb5088 | 2016-07-27 11:40:14 -0700 | [diff] [blame] | 6625 | if ((chan->mode == L2CAP_MODE_ERTM || |
| 6626 | chan->mode == L2CAP_MODE_STREAMING) && sk_filter(chan->data, skb)) |
| 6627 | goto drop; |
| 6628 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6629 | if (!control->sframe) { |
| 6630 | int err; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6631 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6632 | BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d", |
| 6633 | control->sar, control->reqseq, control->final, |
| 6634 | control->txseq); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 6635 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6636 | /* Validate F-bit - F=0 always valid, F=1 only |
| 6637 | * valid in TX WAIT_F |
| 6638 | */ |
| 6639 | if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6640 | goto drop; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6641 | |
| 6642 | if (chan->mode != L2CAP_MODE_STREAMING) { |
| 6643 | event = L2CAP_EV_RECV_IFRAME; |
| 6644 | err = l2cap_rx(chan, control, skb, event); |
| 6645 | } else { |
| 6646 | err = l2cap_stream_rx(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6647 | } |
| 6648 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6649 | if (err) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6650 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6651 | } else { |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6652 | const u8 rx_func_to_event[4] = { |
| 6653 | L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ, |
| 6654 | L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ |
| 6655 | }; |
| 6656 | |
| 6657 | /* Only I-frames are expected in streaming mode */ |
| 6658 | if (chan->mode == L2CAP_MODE_STREAMING) |
| 6659 | goto drop; |
| 6660 | |
| 6661 | BT_DBG("sframe reqseq %d, final %d, poll %d, super %d", |
| 6662 | control->reqseq, control->final, control->poll, |
| 6663 | control->super); |
| 6664 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6665 | if (len != 0) { |
Andrei Emeltchenko | 1bb166e | 2012-11-20 17:16:21 +0200 | [diff] [blame] | 6666 | BT_ERR("Trailing bytes: %d in sframe", len); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6667 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6668 | goto drop; |
| 6669 | } |
| 6670 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6671 | /* Validate F and P bits */ |
| 6672 | if (control->final && (control->poll || |
| 6673 | chan->tx_state != L2CAP_TX_STATE_WAIT_F)) |
| 6674 | goto drop; |
| 6675 | |
| 6676 | event = rx_func_to_event[control->super]; |
| 6677 | if (l2cap_rx(chan, control, skb, event)) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6678 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6679 | } |
| 6680 | |
| 6681 | return 0; |
| 6682 | |
| 6683 | drop: |
| 6684 | kfree_skb(skb); |
| 6685 | return 0; |
| 6686 | } |
| 6687 | |
Johan Hedberg | b1c325c | 2013-12-05 09:43:34 +0200 | [diff] [blame] | 6688 | static void l2cap_chan_le_send_credits(struct l2cap_chan *chan) |
| 6689 | { |
| 6690 | struct l2cap_conn *conn = chan->conn; |
| 6691 | struct l2cap_le_credits pkt; |
| 6692 | u16 return_credits; |
| 6693 | |
| 6694 | /* We return more credits to the sender only after the amount of |
| 6695 | * credits falls below half of the initial amount. |
| 6696 | */ |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 6697 | if (chan->rx_credits >= (le_max_credits + 1) / 2) |
Johan Hedberg | b1c325c | 2013-12-05 09:43:34 +0200 | [diff] [blame] | 6698 | return; |
| 6699 | |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 6700 | return_credits = le_max_credits - chan->rx_credits; |
Johan Hedberg | b1c325c | 2013-12-05 09:43:34 +0200 | [diff] [blame] | 6701 | |
| 6702 | BT_DBG("chan %p returning %u credits to sender", chan, return_credits); |
| 6703 | |
| 6704 | chan->rx_credits += return_credits; |
| 6705 | |
| 6706 | pkt.cid = cpu_to_le16(chan->scid); |
| 6707 | pkt.credits = cpu_to_le16(return_credits); |
| 6708 | |
| 6709 | chan->ident = l2cap_get_ident(conn); |
| 6710 | |
| 6711 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt); |
| 6712 | } |
| 6713 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6714 | static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) |
| 6715 | { |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6716 | int err; |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6717 | |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6718 | if (!chan->rx_credits) { |
| 6719 | BT_ERR("No credits to receive LE L2CAP data"); |
Johan Hedberg | dfd9774 | 2014-01-27 15:11:34 -0800 | [diff] [blame] | 6720 | l2cap_send_disconn_req(chan, ECONNRESET); |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6721 | return -ENOBUFS; |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6722 | } |
| 6723 | |
| 6724 | if (chan->imtu < skb->len) { |
| 6725 | BT_ERR("Too big LE L2CAP PDU"); |
| 6726 | return -ENOBUFS; |
| 6727 | } |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6728 | |
| 6729 | chan->rx_credits--; |
| 6730 | BT_DBG("rx_credits %u -> %u", chan->rx_credits + 1, chan->rx_credits); |
| 6731 | |
| 6732 | l2cap_chan_le_send_credits(chan); |
| 6733 | |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6734 | err = 0; |
| 6735 | |
| 6736 | if (!chan->sdu) { |
| 6737 | u16 sdu_len; |
| 6738 | |
| 6739 | sdu_len = get_unaligned_le16(skb->data); |
| 6740 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
| 6741 | |
| 6742 | BT_DBG("Start of new SDU. sdu_len %u skb->len %u imtu %u", |
| 6743 | sdu_len, skb->len, chan->imtu); |
| 6744 | |
| 6745 | if (sdu_len > chan->imtu) { |
| 6746 | BT_ERR("Too big LE L2CAP SDU length received"); |
| 6747 | err = -EMSGSIZE; |
| 6748 | goto failed; |
| 6749 | } |
| 6750 | |
| 6751 | if (skb->len > sdu_len) { |
| 6752 | BT_ERR("Too much LE L2CAP data received"); |
| 6753 | err = -EINVAL; |
| 6754 | goto failed; |
| 6755 | } |
| 6756 | |
| 6757 | if (skb->len == sdu_len) |
| 6758 | return chan->ops->recv(chan, skb); |
| 6759 | |
| 6760 | chan->sdu = skb; |
| 6761 | chan->sdu_len = sdu_len; |
| 6762 | chan->sdu_last_frag = skb; |
| 6763 | |
| 6764 | return 0; |
| 6765 | } |
| 6766 | |
| 6767 | BT_DBG("SDU fragment. chan->sdu->len %u skb->len %u chan->sdu_len %u", |
| 6768 | chan->sdu->len, skb->len, chan->sdu_len); |
| 6769 | |
| 6770 | if (chan->sdu->len + skb->len > chan->sdu_len) { |
| 6771 | BT_ERR("Too much LE L2CAP data received"); |
| 6772 | err = -EINVAL; |
| 6773 | goto failed; |
| 6774 | } |
| 6775 | |
| 6776 | append_skb_frag(chan->sdu, skb, &chan->sdu_last_frag); |
| 6777 | skb = NULL; |
| 6778 | |
| 6779 | if (chan->sdu->len == chan->sdu_len) { |
| 6780 | err = chan->ops->recv(chan, chan->sdu); |
| 6781 | if (!err) { |
| 6782 | chan->sdu = NULL; |
| 6783 | chan->sdu_last_frag = NULL; |
| 6784 | chan->sdu_len = 0; |
| 6785 | } |
| 6786 | } |
| 6787 | |
| 6788 | failed: |
| 6789 | if (err) { |
| 6790 | kfree_skb(skb); |
| 6791 | kfree_skb(chan->sdu); |
| 6792 | chan->sdu = NULL; |
| 6793 | chan->sdu_last_frag = NULL; |
| 6794 | chan->sdu_len = 0; |
| 6795 | } |
| 6796 | |
| 6797 | /* We can't return an error here since we took care of the skb |
| 6798 | * freeing internally. An error return would cause the caller to |
| 6799 | * do a double-free of the skb. |
| 6800 | */ |
| 6801 | return 0; |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6802 | } |
| 6803 | |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6804 | static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid, |
| 6805 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6806 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6807 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6808 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 6809 | chan = l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6810 | if (!chan) { |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6811 | if (cid == L2CAP_CID_A2MP) { |
| 6812 | chan = a2mp_channel_create(conn, skb); |
| 6813 | if (!chan) { |
| 6814 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6815 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6816 | } |
| 6817 | |
| 6818 | l2cap_chan_lock(chan); |
| 6819 | } else { |
| 6820 | BT_DBG("unknown cid 0x%4.4x", cid); |
| 6821 | /* Drop packet and return */ |
| 6822 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6823 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6824 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6825 | } |
| 6826 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 6827 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6828 | |
Johan Hedberg | 315917e | 2015-02-16 11:42:11 +0200 | [diff] [blame] | 6829 | /* If we receive data on a fixed channel before the info req/rsp |
| 6830 | * procdure is done simply assume that the channel is supported |
| 6831 | * and mark it as ready. |
| 6832 | */ |
| 6833 | if (chan->chan_type == L2CAP_CHAN_FIXED) |
| 6834 | l2cap_chan_ready(chan); |
| 6835 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6836 | if (chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6837 | goto drop; |
| 6838 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6839 | switch (chan->mode) { |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 6840 | case L2CAP_MODE_LE_FLOWCTL: |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6841 | if (l2cap_le_data_rcv(chan, skb) < 0) |
| 6842 | goto drop; |
| 6843 | |
| 6844 | goto done; |
| 6845 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6846 | case L2CAP_MODE_BASIC: |
| 6847 | /* If socket recv buffers overflows we drop data here |
| 6848 | * which is *bad* because L2CAP has to be reliable. |
| 6849 | * But we don't have any other choice. L2CAP doesn't |
| 6850 | * provide flow control mechanism. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6851 | |
Szymon Janc | 2c96e03 | 2014-02-18 20:48:34 +0100 | [diff] [blame] | 6852 | if (chan->imtu < skb->len) { |
| 6853 | BT_ERR("Dropping L2CAP data: receive buffer overflow"); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6854 | goto drop; |
Szymon Janc | 2c96e03 | 2014-02-18 20:48:34 +0100 | [diff] [blame] | 6855 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6856 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6857 | if (!chan->ops->recv(chan, skb)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6858 | goto done; |
| 6859 | break; |
| 6860 | |
| 6861 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6862 | case L2CAP_MODE_STREAMING: |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6863 | l2cap_data_rcv(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6864 | goto done; |
| 6865 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6866 | default: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6867 | 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] | 6868 | break; |
| 6869 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6870 | |
| 6871 | drop: |
| 6872 | kfree_skb(skb); |
| 6873 | |
| 6874 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6875 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6876 | } |
| 6877 | |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6878 | static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, |
| 6879 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6880 | { |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6881 | struct hci_conn *hcon = conn->hcon; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6882 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6883 | |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6884 | if (hcon->type != ACL_LINK) |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6885 | goto free_skb; |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6886 | |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 6887 | chan = l2cap_global_chan_by_psm(0, psm, &hcon->src, &hcon->dst, |
| 6888 | ACL_LINK); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6889 | if (!chan) |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6890 | goto free_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6891 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 6892 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6893 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6894 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6895 | goto drop; |
| 6896 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 6897 | if (chan->imtu < skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6898 | goto drop; |
| 6899 | |
Marcel Holtmann | 2edf870 | 2013-10-13 12:55:29 -0700 | [diff] [blame] | 6900 | /* Store remote BD_ADDR and PSM for msg_name */ |
Johan Hedberg | a4368ff | 2015-03-30 23:21:01 +0300 | [diff] [blame] | 6901 | bacpy(&bt_cb(skb)->l2cap.bdaddr, &hcon->dst); |
| 6902 | bt_cb(skb)->l2cap.psm = psm; |
Marcel Holtmann | 2edf870 | 2013-10-13 12:55:29 -0700 | [diff] [blame] | 6903 | |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6904 | if (!chan->ops->recv(chan, skb)) { |
| 6905 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6906 | return; |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6907 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6908 | |
| 6909 | drop: |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6910 | l2cap_chan_put(chan); |
| 6911 | free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6912 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6913 | } |
| 6914 | |
| 6915 | static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) |
| 6916 | { |
| 6917 | struct l2cap_hdr *lh = (void *) skb->data; |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6918 | struct hci_conn *hcon = conn->hcon; |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 6919 | u16 cid, len; |
| 6920 | __le16 psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6921 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6922 | if (hcon->state != BT_CONNECTED) { |
| 6923 | BT_DBG("queueing pending rx skb"); |
| 6924 | skb_queue_tail(&conn->pending_rx, skb); |
| 6925 | return; |
| 6926 | } |
| 6927 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6928 | skb_pull(skb, L2CAP_HDR_SIZE); |
| 6929 | cid = __le16_to_cpu(lh->cid); |
| 6930 | len = __le16_to_cpu(lh->len); |
| 6931 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6932 | if (len != skb->len) { |
| 6933 | kfree_skb(skb); |
| 6934 | return; |
| 6935 | } |
| 6936 | |
Johan Hedberg | 9e1d7e1 | 2014-07-06 11:03:36 +0300 | [diff] [blame] | 6937 | /* Since we can't actively block incoming LE connections we must |
| 6938 | * at least ensure that we ignore incoming data from them. |
| 6939 | */ |
| 6940 | if (hcon->type == LE_LINK && |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 6941 | hci_bdaddr_list_lookup(&hcon->hdev->blacklist, &hcon->dst, |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 6942 | bdaddr_dst_type(hcon))) { |
Johan Hedberg | e493150 | 2014-07-02 09:36:21 +0300 | [diff] [blame] | 6943 | kfree_skb(skb); |
| 6944 | return; |
| 6945 | } |
| 6946 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6947 | BT_DBG("len %d, cid 0x%4.4x", len, cid); |
| 6948 | |
| 6949 | switch (cid) { |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6950 | case L2CAP_CID_SIGNALING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6951 | l2cap_sig_channel(conn, skb); |
| 6952 | break; |
| 6953 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6954 | case L2CAP_CID_CONN_LESS: |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 6955 | psm = get_unaligned((__le16 *) skb->data); |
Andrei Emeltchenko | 0181a70 | 2012-05-29 10:04:05 +0300 | [diff] [blame] | 6956 | skb_pull(skb, L2CAP_PSMLEN_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6957 | l2cap_conless_channel(conn, psm, skb); |
| 6958 | break; |
| 6959 | |
Marcel Holtmann | a287762 | 2013-10-02 23:46:54 -0700 | [diff] [blame] | 6960 | case L2CAP_CID_LE_SIGNALING: |
| 6961 | l2cap_le_sig_channel(conn, skb); |
| 6962 | break; |
| 6963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6964 | default: |
| 6965 | l2cap_data_channel(conn, cid, skb); |
| 6966 | break; |
| 6967 | } |
| 6968 | } |
| 6969 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6970 | static void process_pending_rx(struct work_struct *work) |
| 6971 | { |
| 6972 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
| 6973 | pending_rx_work); |
| 6974 | struct sk_buff *skb; |
| 6975 | |
| 6976 | BT_DBG(""); |
| 6977 | |
| 6978 | while ((skb = skb_dequeue(&conn->pending_rx))) |
| 6979 | l2cap_recv_frame(conn, skb); |
| 6980 | } |
| 6981 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6982 | static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) |
| 6983 | { |
| 6984 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 6985 | struct hci_chan *hchan; |
| 6986 | |
| 6987 | if (conn) |
| 6988 | return conn; |
| 6989 | |
| 6990 | hchan = hci_chan_create(hcon); |
| 6991 | if (!hchan) |
| 6992 | return NULL; |
| 6993 | |
Johan Hedberg | 27f70f3 | 2014-07-21 10:50:06 +0300 | [diff] [blame] | 6994 | conn = kzalloc(sizeof(*conn), GFP_KERNEL); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6995 | if (!conn) { |
| 6996 | hci_chan_del(hchan); |
| 6997 | return NULL; |
| 6998 | } |
| 6999 | |
| 7000 | kref_init(&conn->ref); |
| 7001 | hcon->l2cap_data = conn; |
Johan Hedberg | 51bb8457 | 2014-08-15 21:06:57 +0300 | [diff] [blame] | 7002 | conn->hcon = hci_conn_get(hcon); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7003 | conn->hchan = hchan; |
| 7004 | |
| 7005 | BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); |
| 7006 | |
| 7007 | switch (hcon->type) { |
| 7008 | case LE_LINK: |
| 7009 | if (hcon->hdev->le_mtu) { |
| 7010 | conn->mtu = hcon->hdev->le_mtu; |
| 7011 | break; |
| 7012 | } |
| 7013 | /* fall through */ |
| 7014 | default: |
| 7015 | conn->mtu = hcon->hdev->acl_mtu; |
| 7016 | break; |
| 7017 | } |
| 7018 | |
| 7019 | conn->feat_mask = 0; |
| 7020 | |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 7021 | conn->local_fixed_chan = L2CAP_FC_SIG_BREDR | L2CAP_FC_CONNLESS; |
| 7022 | |
| 7023 | if (hcon->type == ACL_LINK && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7024 | hci_dev_test_flag(hcon->hdev, HCI_HS_ENABLED)) |
Johan Hedberg | 0bd49fc | 2014-12-02 10:09:26 +0200 | [diff] [blame] | 7025 | conn->local_fixed_chan |= L2CAP_FC_A2MP; |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7026 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7027 | if (hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED) && |
Marcel Holtmann | f9be9e8 | 2014-12-06 00:35:45 +0100 | [diff] [blame] | 7028 | (bredr_sc_enabled(hcon->hdev) || |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 7029 | hci_dev_test_flag(hcon->hdev, HCI_FORCE_BREDR_SMP))) |
Johan Hedberg | b5ae344 | 2014-08-14 12:34:26 +0300 | [diff] [blame] | 7030 | conn->local_fixed_chan |= L2CAP_FC_SMP_BREDR; |
| 7031 | |
Marcel Holtmann | 5a54e7c | 2014-07-13 20:50:15 +0200 | [diff] [blame] | 7032 | mutex_init(&conn->ident_lock); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7033 | mutex_init(&conn->chan_lock); |
| 7034 | |
| 7035 | INIT_LIST_HEAD(&conn->chan_l); |
| 7036 | INIT_LIST_HEAD(&conn->users); |
| 7037 | |
Johan Hedberg | 276d807 | 2014-08-11 22:06:41 +0300 | [diff] [blame] | 7038 | INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7039 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 7040 | skb_queue_head_init(&conn->pending_rx); |
| 7041 | INIT_WORK(&conn->pending_rx_work, process_pending_rx); |
Johan Hedberg | f3d82d0 | 2014-09-05 22:19:50 +0300 | [diff] [blame] | 7042 | INIT_WORK(&conn->id_addr_update_work, l2cap_conn_update_id_addr); |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 7043 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7044 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
| 7045 | |
| 7046 | return conn; |
| 7047 | } |
| 7048 | |
| 7049 | static bool is_valid_psm(u16 psm, u8 dst_type) { |
| 7050 | if (!psm) |
| 7051 | return false; |
| 7052 | |
| 7053 | if (bdaddr_type_is_le(dst_type)) |
| 7054 | return (psm <= 0x00ff); |
| 7055 | |
| 7056 | /* PSM must be odd and lsb of upper byte must be 0 */ |
| 7057 | return ((psm & 0x0101) == 0x0001); |
| 7058 | } |
| 7059 | |
| 7060 | int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, |
| 7061 | bdaddr_t *dst, u8 dst_type) |
| 7062 | { |
| 7063 | struct l2cap_conn *conn; |
| 7064 | struct hci_conn *hcon; |
| 7065 | struct hci_dev *hdev; |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7066 | int err; |
| 7067 | |
| 7068 | BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", &chan->src, dst, |
| 7069 | dst_type, __le16_to_cpu(psm)); |
| 7070 | |
Johan Hedberg | 39385cb | 2016-11-12 17:03:07 +0200 | [diff] [blame] | 7071 | hdev = hci_get_route(dst, &chan->src, chan->src_type); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7072 | if (!hdev) |
| 7073 | return -EHOSTUNREACH; |
| 7074 | |
| 7075 | hci_dev_lock(hdev); |
| 7076 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7077 | if (!is_valid_psm(__le16_to_cpu(psm), dst_type) && !cid && |
| 7078 | chan->chan_type != L2CAP_CHAN_RAW) { |
| 7079 | err = -EINVAL; |
| 7080 | goto done; |
| 7081 | } |
| 7082 | |
Johan Hedberg | 21626e6 | 2014-01-24 10:35:41 +0200 | [diff] [blame] | 7083 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !psm) { |
| 7084 | err = -EINVAL; |
| 7085 | goto done; |
| 7086 | } |
| 7087 | |
| 7088 | if (chan->chan_type == L2CAP_CHAN_FIXED && !cid) { |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7089 | err = -EINVAL; |
| 7090 | goto done; |
| 7091 | } |
| 7092 | |
| 7093 | switch (chan->mode) { |
| 7094 | case L2CAP_MODE_BASIC: |
| 7095 | break; |
| 7096 | case L2CAP_MODE_LE_FLOWCTL: |
| 7097 | l2cap_le_flowctl_init(chan); |
| 7098 | break; |
| 7099 | case L2CAP_MODE_ERTM: |
| 7100 | case L2CAP_MODE_STREAMING: |
| 7101 | if (!disable_ertm) |
| 7102 | break; |
| 7103 | /* fall through */ |
| 7104 | default: |
Johan Hedberg | beb19e4 | 2014-07-18 11:15:26 +0300 | [diff] [blame] | 7105 | err = -EOPNOTSUPP; |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7106 | goto done; |
| 7107 | } |
| 7108 | |
| 7109 | switch (chan->state) { |
| 7110 | case BT_CONNECT: |
| 7111 | case BT_CONNECT2: |
| 7112 | case BT_CONFIG: |
| 7113 | /* Already connecting */ |
| 7114 | err = 0; |
| 7115 | goto done; |
| 7116 | |
| 7117 | case BT_CONNECTED: |
| 7118 | /* Already connected */ |
| 7119 | err = -EISCONN; |
| 7120 | goto done; |
| 7121 | |
| 7122 | case BT_OPEN: |
| 7123 | case BT_BOUND: |
| 7124 | /* Can connect */ |
| 7125 | break; |
| 7126 | |
| 7127 | default: |
| 7128 | err = -EBADFD; |
| 7129 | goto done; |
| 7130 | } |
| 7131 | |
| 7132 | /* Set destination address and psm */ |
| 7133 | bacpy(&chan->dst, dst); |
| 7134 | chan->dst_type = dst_type; |
| 7135 | |
| 7136 | chan->psm = psm; |
| 7137 | chan->dcid = cid; |
| 7138 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7139 | if (bdaddr_type_is_le(dst_type)) { |
| 7140 | /* Convert from L2CAP channel address type to HCI address type |
| 7141 | */ |
| 7142 | if (dst_type == BDADDR_LE_PUBLIC) |
| 7143 | dst_type = ADDR_LE_DEV_PUBLIC; |
| 7144 | else |
| 7145 | dst_type = ADDR_LE_DEV_RANDOM; |
| 7146 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7147 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | 0ad06aa | 2015-11-11 14:44:57 +0200 | [diff] [blame] | 7148 | hcon = hci_connect_le(hdev, dst, dst_type, |
| 7149 | chan->sec_level, |
| 7150 | HCI_LE_CONN_TIMEOUT, |
| 7151 | HCI_ROLE_SLAVE); |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 7152 | else |
Johan Hedberg | 0ad06aa | 2015-11-11 14:44:57 +0200 | [diff] [blame] | 7153 | hcon = hci_connect_le_scan(hdev, dst, dst_type, |
| 7154 | chan->sec_level, |
| 7155 | HCI_LE_CONN_TIMEOUT); |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 7156 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7157 | } else { |
Johan Hedberg | d93375a | 2014-07-07 15:02:27 +0300 | [diff] [blame] | 7158 | u8 auth_type = l2cap_get_auth_type(chan); |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 7159 | hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7160 | } |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7161 | |
| 7162 | if (IS_ERR(hcon)) { |
| 7163 | err = PTR_ERR(hcon); |
| 7164 | goto done; |
| 7165 | } |
| 7166 | |
| 7167 | conn = l2cap_conn_add(hcon); |
| 7168 | if (!conn) { |
| 7169 | hci_conn_drop(hcon); |
| 7170 | err = -ENOMEM; |
| 7171 | goto done; |
| 7172 | } |
| 7173 | |
Johan Hedberg | 02e246ae | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7174 | mutex_lock(&conn->chan_lock); |
| 7175 | l2cap_chan_lock(chan); |
| 7176 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7177 | if (cid && __l2cap_get_chan_by_dcid(conn, cid)) { |
| 7178 | hci_conn_drop(hcon); |
| 7179 | err = -EBUSY; |
Johan Hedberg | 02e246ae | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7180 | goto chan_unlock; |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7181 | } |
| 7182 | |
| 7183 | /* Update source addr of the socket */ |
| 7184 | bacpy(&chan->src, &hcon->src); |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 7185 | chan->src_type = bdaddr_src_type(hcon); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7186 | |
Johan Hedberg | 02e246ae | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7187 | __l2cap_chan_add(conn, chan); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7188 | |
| 7189 | /* l2cap_chan_add takes its own ref so we can drop this one */ |
| 7190 | hci_conn_drop(hcon); |
| 7191 | |
| 7192 | l2cap_state_change(chan, BT_CONNECT); |
| 7193 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
| 7194 | |
Johan Hedberg | 61202e4 | 2014-01-28 15:16:48 -0800 | [diff] [blame] | 7195 | /* Release chan->sport so that it can be reused by other |
| 7196 | * sockets (as it's only used for listening sockets). |
| 7197 | */ |
| 7198 | write_lock(&chan_list_lock); |
| 7199 | chan->sport = 0; |
| 7200 | write_unlock(&chan_list_lock); |
| 7201 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7202 | if (hcon->state == BT_CONNECTED) { |
| 7203 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
| 7204 | __clear_chan_timer(chan); |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 7205 | if (l2cap_chan_check_security(chan, true)) |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7206 | l2cap_state_change(chan, BT_CONNECTED); |
| 7207 | } else |
| 7208 | l2cap_do_start(chan); |
| 7209 | } |
| 7210 | |
| 7211 | err = 0; |
| 7212 | |
Johan Hedberg | 02e246ae | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7213 | chan_unlock: |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7214 | l2cap_chan_unlock(chan); |
Johan Hedberg | 02e246ae | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7215 | mutex_unlock(&conn->chan_lock); |
| 7216 | done: |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7217 | hci_dev_unlock(hdev); |
| 7218 | hci_dev_put(hdev); |
| 7219 | return err; |
| 7220 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 7221 | EXPORT_SYMBOL_GPL(l2cap_chan_connect); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7223 | /* ---- L2CAP interface with lower layer (HCI) ---- */ |
| 7224 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7225 | int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7226 | { |
| 7227 | int exact = 0, lm1 = 0, lm2 = 0; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7228 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7229 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 7230 | BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7231 | |
| 7232 | /* Find listening sockets and check their link_mode */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7233 | read_lock(&chan_list_lock); |
| 7234 | list_for_each_entry(c, &chan_list, global_l) { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 7235 | if (c->state != BT_LISTEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7236 | continue; |
| 7237 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 7238 | if (!bacmp(&c->src, &hdev->bdaddr)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7239 | lm1 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 7240 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7241 | lm1 |= HCI_LM_MASTER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7242 | exact++; |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 7243 | } else if (!bacmp(&c->src, BDADDR_ANY)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7244 | lm2 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 7245 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7246 | lm2 |= HCI_LM_MASTER; |
| 7247 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7248 | } |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7249 | read_unlock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7250 | |
| 7251 | return exact ? lm1 : lm2; |
| 7252 | } |
| 7253 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7254 | /* Find the next fixed channel in BT_LISTEN state, continue iteration |
| 7255 | * from an existing channel in the list or from the beginning of the |
| 7256 | * global list (by passing NULL as first parameter). |
| 7257 | */ |
| 7258 | static struct l2cap_chan *l2cap_global_fixed_chan(struct l2cap_chan *c, |
Johan Hedberg | 327a719 | 2015-01-15 13:06:45 +0200 | [diff] [blame] | 7259 | struct hci_conn *hcon) |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7260 | { |
Johan Hedberg | 327a719 | 2015-01-15 13:06:45 +0200 | [diff] [blame] | 7261 | u8 src_type = bdaddr_src_type(hcon); |
| 7262 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7263 | read_lock(&chan_list_lock); |
| 7264 | |
| 7265 | if (c) |
| 7266 | c = list_next_entry(c, global_l); |
| 7267 | else |
| 7268 | c = list_entry(chan_list.next, typeof(*c), global_l); |
| 7269 | |
| 7270 | list_for_each_entry_from(c, &chan_list, global_l) { |
| 7271 | if (c->chan_type != L2CAP_CHAN_FIXED) |
| 7272 | continue; |
| 7273 | if (c->state != BT_LISTEN) |
| 7274 | continue; |
Johan Hedberg | 327a719 | 2015-01-15 13:06:45 +0200 | [diff] [blame] | 7275 | if (bacmp(&c->src, &hcon->src) && bacmp(&c->src, BDADDR_ANY)) |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7276 | continue; |
Johan Hedberg | 327a719 | 2015-01-15 13:06:45 +0200 | [diff] [blame] | 7277 | if (src_type != c->src_type) |
Johan Hedberg | 54a1b62 | 2014-08-07 22:56:48 +0300 | [diff] [blame] | 7278 | continue; |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7279 | |
| 7280 | l2cap_chan_hold(c); |
| 7281 | read_unlock(&chan_list_lock); |
| 7282 | return c; |
| 7283 | } |
| 7284 | |
| 7285 | read_unlock(&chan_list_lock); |
| 7286 | |
| 7287 | return NULL; |
| 7288 | } |
| 7289 | |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 7290 | static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7291 | { |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7292 | struct hci_dev *hdev = hcon->hdev; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7293 | struct l2cap_conn *conn; |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7294 | struct l2cap_chan *pchan; |
| 7295 | u8 dst_type; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7296 | |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 7297 | if (hcon->type != ACL_LINK && hcon->type != LE_LINK) |
| 7298 | return; |
| 7299 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 7300 | BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7301 | |
Johan Hedberg | dc0f508 | 2014-08-07 22:56:46 +0300 | [diff] [blame] | 7302 | if (status) { |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 7303 | l2cap_conn_del(hcon, bt_to_errno(status)); |
Johan Hedberg | dc0f508 | 2014-08-07 22:56:46 +0300 | [diff] [blame] | 7304 | return; |
Andrei Emeltchenko | ba6fc31 | 2012-10-31 15:46:26 +0200 | [diff] [blame] | 7305 | } |
Johan Hedberg | dc0f508 | 2014-08-07 22:56:46 +0300 | [diff] [blame] | 7306 | |
| 7307 | conn = l2cap_conn_add(hcon); |
| 7308 | if (!conn) |
| 7309 | return; |
| 7310 | |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 7311 | dst_type = bdaddr_dst_type(hcon); |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7312 | |
| 7313 | /* If device is blocked, do not create channels for it */ |
| 7314 | if (hci_bdaddr_list_lookup(&hdev->blacklist, &hcon->dst, dst_type)) |
| 7315 | return; |
| 7316 | |
| 7317 | /* Find fixed channels and notify them of the new connection. We |
| 7318 | * use multiple individual lookups, continuing each time where |
| 7319 | * we left off, because the list lock would prevent calling the |
| 7320 | * potentially sleeping l2cap_chan_lock() function. |
| 7321 | */ |
Johan Hedberg | 327a719 | 2015-01-15 13:06:45 +0200 | [diff] [blame] | 7322 | pchan = l2cap_global_fixed_chan(NULL, hcon); |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7323 | while (pchan) { |
| 7324 | struct l2cap_chan *chan, *next; |
| 7325 | |
| 7326 | /* Client fixed channels should override server ones */ |
| 7327 | if (__l2cap_get_chan_by_dcid(conn, pchan->scid)) |
| 7328 | goto next; |
| 7329 | |
| 7330 | l2cap_chan_lock(pchan); |
| 7331 | chan = pchan->ops->new_connection(pchan); |
| 7332 | if (chan) { |
| 7333 | bacpy(&chan->src, &hcon->src); |
| 7334 | bacpy(&chan->dst, &hcon->dst); |
Johan Hedberg | a250e04 | 2015-01-15 13:06:44 +0200 | [diff] [blame] | 7335 | chan->src_type = bdaddr_src_type(hcon); |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7336 | chan->dst_type = dst_type; |
| 7337 | |
| 7338 | __l2cap_chan_add(conn, chan); |
| 7339 | } |
| 7340 | |
| 7341 | l2cap_chan_unlock(pchan); |
| 7342 | next: |
Johan Hedberg | 327a719 | 2015-01-15 13:06:45 +0200 | [diff] [blame] | 7343 | next = l2cap_global_fixed_chan(pchan, hcon); |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7344 | l2cap_chan_put(pchan); |
| 7345 | pchan = next; |
| 7346 | } |
| 7347 | |
Johan Hedberg | dc0f508 | 2014-08-07 22:56:46 +0300 | [diff] [blame] | 7348 | l2cap_conn_ready(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7349 | } |
| 7350 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7351 | int l2cap_disconn_ind(struct hci_conn *hcon) |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 7352 | { |
| 7353 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 7354 | |
| 7355 | BT_DBG("hcon %p", hcon); |
| 7356 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7357 | if (!conn) |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 7358 | return HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 7359 | return conn->disc_reason; |
| 7360 | } |
| 7361 | |
Johan Hedberg | 3a6d576 | 2015-02-18 14:53:58 +0200 | [diff] [blame] | 7362 | static void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7363 | { |
Johan Hedberg | 3a6d576 | 2015-02-18 14:53:58 +0200 | [diff] [blame] | 7364 | if (hcon->type != ACL_LINK && hcon->type != LE_LINK) |
| 7365 | return; |
| 7366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7367 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 7368 | |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 7369 | l2cap_conn_del(hcon, bt_to_errno(reason)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7370 | } |
| 7371 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7372 | static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7373 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 7374 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
Marcel Holtmann | 255c760 | 2009-02-04 21:07:19 +0100 | [diff] [blame] | 7375 | return; |
| 7376 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7377 | if (encrypt == 0x00) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7378 | if (chan->sec_level == BT_SECURITY_MEDIUM) { |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 7379 | __set_chan_timer(chan, L2CAP_ENC_TIMEOUT); |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 7380 | } else if (chan->sec_level == BT_SECURITY_HIGH || |
| 7381 | chan->sec_level == BT_SECURITY_FIPS) |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 7382 | l2cap_chan_close(chan, ECONNREFUSED); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7383 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7384 | if (chan->sec_level == BT_SECURITY_MEDIUM) |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 7385 | __clear_chan_timer(chan); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7386 | } |
| 7387 | } |
| 7388 | |
Johan Hedberg | 354fe80 | 2015-02-18 14:53:56 +0200 | [diff] [blame] | 7389 | static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7390 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 7391 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 7392 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7393 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7394 | if (!conn) |
Johan Hedberg | 354fe80 | 2015-02-18 14:53:56 +0200 | [diff] [blame] | 7395 | return; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7396 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 7397 | 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] | 7398 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 7399 | mutex_lock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7400 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 7401 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7402 | l2cap_chan_lock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7403 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 7404 | BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, |
| 7405 | state_to_string(chan->state)); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 7406 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 7407 | if (chan->scid == L2CAP_CID_A2MP) { |
Andrei Emeltchenko | 78eb2f9 | 2012-07-19 17:03:47 +0300 | [diff] [blame] | 7408 | l2cap_chan_unlock(chan); |
| 7409 | continue; |
| 7410 | } |
| 7411 | |
Johan Hedberg | 191eb39 | 2014-08-07 22:56:45 +0300 | [diff] [blame] | 7412 | if (!status && encrypt) |
| 7413 | chan->sec_level = hcon->sec_level; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 7414 | |
Andrei Emeltchenko | 96eff46 | 2012-11-15 18:14:53 +0200 | [diff] [blame] | 7415 | if (!__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7416 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 7417 | continue; |
| 7418 | } |
| 7419 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 7420 | if (!status && (chan->state == BT_CONNECTED || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7421 | chan->state == BT_CONFIG)) { |
Marcel Holtmann | d97c899 | 2013-10-14 02:53:54 -0700 | [diff] [blame] | 7422 | chan->ops->resume(chan); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7423 | l2cap_check_encryption(chan, encrypt); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7424 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 9719f8a | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 7425 | continue; |
| 7426 | } |
| 7427 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 7428 | if (chan->state == BT_CONNECT) { |
Johan Hedberg | 6d3c15d | 2013-12-02 22:13:24 +0200 | [diff] [blame] | 7429 | if (!status) |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 7430 | l2cap_start_connection(chan); |
Johan Hedberg | 6d3c15d | 2013-12-02 22:13:24 +0200 | [diff] [blame] | 7431 | else |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 7432 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Johan Hedberg | fa37c1a | 2014-11-13 10:55:17 +0200 | [diff] [blame] | 7433 | } else if (chan->state == BT_CONNECT2 && |
| 7434 | chan->mode != L2CAP_MODE_LE_FLOWCTL) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7435 | struct l2cap_conn_rsp rsp; |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7436 | __u16 res, stat; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7437 | |
| 7438 | if (!status) { |
Marcel Holtmann | bdc2578 | 2013-10-14 02:45:34 -0700 | [diff] [blame] | 7439 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7440 | res = L2CAP_CR_PEND; |
| 7441 | stat = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 7442 | chan->ops->defer(chan); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7443 | } else { |
Gustavo Padovan | acdcabf | 2013-10-21 14:21:39 -0200 | [diff] [blame] | 7444 | l2cap_state_change(chan, BT_CONFIG); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7445 | res = L2CAP_CR_SUCCESS; |
| 7446 | stat = L2CAP_CS_NO_INFO; |
| 7447 | } |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7448 | } else { |
Gustavo Padovan | acdcabf | 2013-10-21 14:21:39 -0200 | [diff] [blame] | 7449 | l2cap_state_change(chan, BT_DISCONN); |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 7450 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7451 | res = L2CAP_CR_SEC_BLOCK; |
| 7452 | stat = L2CAP_CS_NO_INFO; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7453 | } |
| 7454 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 7455 | rsp.scid = cpu_to_le16(chan->dcid); |
| 7456 | rsp.dcid = cpu_to_le16(chan->scid); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7457 | rsp.result = cpu_to_le16(res); |
| 7458 | rsp.status = cpu_to_le16(stat); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 7459 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7460 | sizeof(rsp), &rsp); |
Mat Martineau | 2d36935 | 2012-05-23 14:59:30 -0700 | [diff] [blame] | 7461 | |
| 7462 | if (!test_bit(CONF_REQ_SENT, &chan->conf_state) && |
| 7463 | res == L2CAP_CR_SUCCESS) { |
| 7464 | char buf[128]; |
| 7465 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 7466 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 7467 | L2CAP_CONF_REQ, |
| 7468 | l2cap_build_conf_req(chan, buf), |
| 7469 | buf); |
| 7470 | chan->num_conf_req++; |
| 7471 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7472 | } |
| 7473 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7474 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7475 | } |
| 7476 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 7477 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7478 | } |
| 7479 | |
Arron Wang | 9b4c333 | 2015-06-09 17:47:22 +0800 | [diff] [blame] | 7480 | void 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] | 7481 | { |
| 7482 | struct l2cap_conn *conn = hcon->l2cap_data; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7483 | struct l2cap_hdr *hdr; |
| 7484 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7485 | |
Andrei Emeltchenko | 1d13a25 | 2012-10-15 11:58:41 +0300 | [diff] [blame] | 7486 | /* For AMP controller do not create l2cap conn */ |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 7487 | if (!conn && hcon->hdev->dev_type != HCI_PRIMARY) |
Andrei Emeltchenko | 1d13a25 | 2012-10-15 11:58:41 +0300 | [diff] [blame] | 7488 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7489 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 7490 | if (!conn) |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 7491 | conn = l2cap_conn_add(hcon); |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 7492 | |
| 7493 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7494 | goto drop; |
| 7495 | |
| 7496 | BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); |
| 7497 | |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7498 | switch (flags) { |
| 7499 | case ACL_START: |
| 7500 | case ACL_START_NO_FLUSH: |
| 7501 | case ACL_COMPLETE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7502 | if (conn->rx_len) { |
| 7503 | BT_ERR("Unexpected start frame (len %d)", skb->len); |
| 7504 | kfree_skb(conn->rx_skb); |
| 7505 | conn->rx_skb = NULL; |
| 7506 | conn->rx_len = 0; |
| 7507 | l2cap_conn_unreliable(conn, ECOMM); |
| 7508 | } |
| 7509 | |
Andrei Emeltchenko | aae7fe2 | 2010-09-15 14:28:43 +0300 | [diff] [blame] | 7510 | /* Start fragment always begin with Basic L2CAP header */ |
| 7511 | if (skb->len < L2CAP_HDR_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7512 | BT_ERR("Frame is too short (len %d)", skb->len); |
| 7513 | l2cap_conn_unreliable(conn, ECOMM); |
| 7514 | goto drop; |
| 7515 | } |
| 7516 | |
| 7517 | hdr = (struct l2cap_hdr *) skb->data; |
| 7518 | len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; |
| 7519 | |
| 7520 | if (len == skb->len) { |
| 7521 | /* Complete frame received */ |
| 7522 | l2cap_recv_frame(conn, skb); |
Arron Wang | 9b4c333 | 2015-06-09 17:47:22 +0800 | [diff] [blame] | 7523 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7524 | } |
| 7525 | |
| 7526 | BT_DBG("Start: total len %d, frag len %d", len, skb->len); |
| 7527 | |
| 7528 | if (skb->len > len) { |
| 7529 | BT_ERR("Frame is too long (len %d, expected len %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7530 | skb->len, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7531 | l2cap_conn_unreliable(conn, ECOMM); |
| 7532 | goto drop; |
| 7533 | } |
| 7534 | |
| 7535 | /* Allocate skb for the complete frame (with header) */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 7536 | conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 7537 | if (!conn->rx_skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7538 | goto drop; |
| 7539 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 7540 | 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] | 7541 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7542 | conn->rx_len = len - skb->len; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7543 | break; |
| 7544 | |
| 7545 | case ACL_CONT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7546 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
| 7547 | |
| 7548 | if (!conn->rx_len) { |
| 7549 | BT_ERR("Unexpected continuation frame (len %d)", skb->len); |
| 7550 | l2cap_conn_unreliable(conn, ECOMM); |
| 7551 | goto drop; |
| 7552 | } |
| 7553 | |
| 7554 | if (skb->len > conn->rx_len) { |
| 7555 | BT_ERR("Fragment is too long (len %d, expected %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7556 | skb->len, conn->rx_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7557 | kfree_skb(conn->rx_skb); |
| 7558 | conn->rx_skb = NULL; |
| 7559 | conn->rx_len = 0; |
| 7560 | l2cap_conn_unreliable(conn, ECOMM); |
| 7561 | goto drop; |
| 7562 | } |
| 7563 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 7564 | 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] | 7565 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7566 | conn->rx_len -= skb->len; |
| 7567 | |
| 7568 | if (!conn->rx_len) { |
Johan Hedberg | c4e5baf | 2013-10-10 13:33:37 +0200 | [diff] [blame] | 7569 | /* Complete frame received. l2cap_recv_frame |
| 7570 | * takes ownership of the skb so set the global |
| 7571 | * rx_skb pointer to NULL first. |
| 7572 | */ |
| 7573 | struct sk_buff *rx_skb = conn->rx_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7574 | conn->rx_skb = NULL; |
Johan Hedberg | c4e5baf | 2013-10-10 13:33:37 +0200 | [diff] [blame] | 7575 | l2cap_recv_frame(conn, rx_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7576 | } |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7577 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7578 | } |
| 7579 | |
| 7580 | drop: |
| 7581 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7582 | } |
| 7583 | |
Johan Hedberg | 354fe80 | 2015-02-18 14:53:56 +0200 | [diff] [blame] | 7584 | static struct hci_cb l2cap_cb = { |
| 7585 | .name = "L2CAP", |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 7586 | .connect_cfm = l2cap_connect_cfm, |
Johan Hedberg | 3a6d576 | 2015-02-18 14:53:58 +0200 | [diff] [blame] | 7587 | .disconn_cfm = l2cap_disconn_cfm, |
Johan Hedberg | 354fe80 | 2015-02-18 14:53:56 +0200 | [diff] [blame] | 7588 | .security_cfm = l2cap_security_cfm, |
| 7589 | }; |
| 7590 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7591 | static int l2cap_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7592 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7593 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7594 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 7595 | read_lock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7596 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7597 | list_for_each_entry(c, &chan_list, global_l) { |
Marcel Holtmann | eeb5a06 | 2015-01-14 13:44:21 -0800 | [diff] [blame] | 7598 | seq_printf(f, "%pMR (%u) %pMR (%u) %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", |
| 7599 | &c->src, c->src_type, &c->dst, c->dst_type, |
Andrei Emeltchenko | fcb7333 | 2012-09-25 12:49:44 +0300 | [diff] [blame] | 7600 | c->state, __le16_to_cpu(c->psm), |
| 7601 | c->scid, c->dcid, c->imtu, c->omtu, |
| 7602 | c->sec_level, c->mode); |
Andrei Emeltchenko | 61e1b4b | 2012-01-19 11:19:50 +0200 | [diff] [blame] | 7603 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7604 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 7605 | read_unlock(&chan_list_lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 7606 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7607 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7608 | } |
| 7609 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7610 | static int l2cap_debugfs_open(struct inode *inode, struct file *file) |
| 7611 | { |
| 7612 | return single_open(file, l2cap_debugfs_show, inode->i_private); |
| 7613 | } |
| 7614 | |
| 7615 | static const struct file_operations l2cap_debugfs_fops = { |
| 7616 | .open = l2cap_debugfs_open, |
| 7617 | .read = seq_read, |
| 7618 | .llseek = seq_lseek, |
| 7619 | .release = single_release, |
| 7620 | }; |
| 7621 | |
| 7622 | static struct dentry *l2cap_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7623 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 7624 | int __init l2cap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7625 | { |
| 7626 | int err; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 7627 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 7628 | err = l2cap_init_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7629 | if (err < 0) |
| 7630 | return err; |
| 7631 | |
Johan Hedberg | 354fe80 | 2015-02-18 14:53:56 +0200 | [diff] [blame] | 7632 | hci_register_cb(&l2cap_cb); |
| 7633 | |
Marcel Holtmann | 1120e4b | 2013-10-17 17:24:16 -0700 | [diff] [blame] | 7634 | if (IS_ERR_OR_NULL(bt_debugfs)) |
| 7635 | return 0; |
| 7636 | |
| 7637 | l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs, |
| 7638 | NULL, &l2cap_debugfs_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7639 | |
Samuel Ortiz | 40b9397 | 2014-05-14 17:53:35 +0200 | [diff] [blame] | 7640 | debugfs_create_u16("l2cap_le_max_credits", 0644, bt_debugfs, |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 7641 | &le_max_credits); |
Samuel Ortiz | 40b9397 | 2014-05-14 17:53:35 +0200 | [diff] [blame] | 7642 | debugfs_create_u16("l2cap_le_default_mps", 0644, bt_debugfs, |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 7643 | &le_default_mps); |
| 7644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7645 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7646 | } |
| 7647 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 7648 | void l2cap_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7649 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7650 | debugfs_remove(l2cap_debugfs); |
Johan Hedberg | 354fe80 | 2015-02-18 14:53:56 +0200 | [diff] [blame] | 7651 | hci_unregister_cb(&l2cap_cb); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 7652 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7653 | } |
| 7654 | |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 7655 | module_param(disable_ertm, bool, 0644); |
| 7656 | MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); |