YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | Copyright (C) 2000-2001 Qualcomm Incorporated |
Gustavo F. Padovan | ce5706b | 2010-07-13 11:57:11 -0300 | [diff] [blame] | 4 | Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org> |
Gustavo F. Padovan | 5d8868f | 2010-07-16 16:18:39 -0300 | [diff] [blame] | 5 | Copyright (C) 2010 Google Inc. |
Gustavo F. Padovan | 590051d | 2011-12-18 13:39:33 -0200 | [diff] [blame] | 6 | Copyright (C) 2011 ProFUSION Embedded Systems |
Mat Martineau | 422e925 | 2012-04-27 16:50:55 -0700 | [diff] [blame] | 7 | Copyright (c) 2012 Code Aurora Forum. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
| 9 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 10 | |
| 11 | This program is free software; you can redistribute it and/or modify |
| 12 | it under the terms of the GNU General Public License version 2 as |
| 13 | published by the Free Software Foundation; |
| 14 | |
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 18 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 19 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 20 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 21 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 23 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 24 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 25 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | SOFTWARE IS DISCLAIMED. |
| 27 | */ |
| 28 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 29 | /* Bluetooth L2CAP core. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/module.h> |
| 32 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 33 | #include <linux/debugfs.h> |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 34 | #include <linux/crc16.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #include <net/bluetooth/bluetooth.h> |
| 37 | #include <net/bluetooth/hci_core.h> |
| 38 | #include <net/bluetooth/l2cap.h> |
Marcel Holtmann | 7ef9fbf | 2013-10-10 14:54:14 -0700 | [diff] [blame] | 39 | |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 40 | #include "smp.h" |
Marcel Holtmann | 7024728 | 2013-10-10 14:54:15 -0700 | [diff] [blame] | 41 | #include "a2mp.h" |
Marcel Holtmann | 7ef9fbf | 2013-10-10 14:54:14 -0700 | [diff] [blame] | 42 | #include "amp.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Johan Hedberg | 0f1bfe4 | 2014-01-27 15:11:35 -0800 | [diff] [blame] | 44 | #define LE_FLOWCTL_MAX_CREDITS 65535 |
| 45 | |
Mat Martineau | d1de6d4 | 2012-05-17 20:53:55 -0700 | [diff] [blame] | 46 | bool disable_ertm; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 47 | |
Marcel Holtmann | 547d103 | 2013-10-12 08:18:19 -0700 | [diff] [blame] | 48 | static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN | L2CAP_FEAT_UCD; |
Marcel Holtmann | a6801ca | 2014-07-11 06:03:08 +0200 | [diff] [blame] | 49 | static u8 l2cap_fixed_chan[8] = { L2CAP_FC_SIG_BREDR | L2CAP_FC_CONNLESS, }; |
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 | 710f9b0a | 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 | |
Marcel Holtmann | 4f1654e | 2013-10-13 08:50:41 -0700 | [diff] [blame] | 67 | static inline __u8 bdaddr_type(struct hci_conn *hcon, __u8 type) |
| 68 | { |
| 69 | if (hcon->type == LE_LINK) { |
| 70 | if (type == ADDR_LE_DEV_PUBLIC) |
| 71 | return BDADDR_LE_PUBLIC; |
| 72 | else |
| 73 | return BDADDR_LE_RANDOM; |
| 74 | } |
| 75 | |
| 76 | return BDADDR_BREDR; |
| 77 | } |
| 78 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 79 | /* ---- L2CAP channels ---- */ |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 80 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 81 | static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, |
| 82 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 83 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 84 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 85 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 86 | list_for_each_entry(c, &conn->chan_l, list) { |
| 87 | if (c->dcid == cid) |
| 88 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 89 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 90 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 93 | static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, |
| 94 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 95 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 96 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 97 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 98 | list_for_each_entry(c, &conn->chan_l, list) { |
| 99 | if (c->scid == cid) |
| 100 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 101 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 102 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* Find channel with given SCID. |
Mat Martineau | ef191ad | 2012-05-02 09:42:00 -0700 | [diff] [blame] | 106 | * Returns locked channel. */ |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 107 | static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, |
| 108 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 109 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 110 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 111 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 112 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 113 | c = __l2cap_get_chan_by_scid(conn, cid); |
Mat Martineau | ef191ad | 2012-05-02 09:42:00 -0700 | [diff] [blame] | 114 | if (c) |
| 115 | l2cap_chan_lock(c); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 116 | mutex_unlock(&conn->chan_lock); |
| 117 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 118 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Mat Martineau | b1a130b | 2012-10-23 15:24:09 -0700 | [diff] [blame] | 121 | /* Find channel with given DCID. |
| 122 | * Returns locked channel. |
| 123 | */ |
| 124 | static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn, |
| 125 | u16 cid) |
| 126 | { |
| 127 | struct l2cap_chan *c; |
| 128 | |
| 129 | mutex_lock(&conn->chan_lock); |
| 130 | c = __l2cap_get_chan_by_dcid(conn, cid); |
| 131 | if (c) |
| 132 | l2cap_chan_lock(c); |
| 133 | mutex_unlock(&conn->chan_lock); |
| 134 | |
| 135 | return c; |
| 136 | } |
| 137 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 138 | static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, |
| 139 | u8 ident) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 140 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 141 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 142 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 143 | list_for_each_entry(c, &conn->chan_l, list) { |
| 144 | if (c->ident == ident) |
| 145 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 146 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 147 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 150 | static struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, |
| 151 | u8 ident) |
| 152 | { |
| 153 | struct l2cap_chan *c; |
| 154 | |
| 155 | mutex_lock(&conn->chan_lock); |
| 156 | c = __l2cap_get_chan_by_ident(conn, ident); |
| 157 | if (c) |
| 158 | l2cap_chan_lock(c); |
| 159 | mutex_unlock(&conn->chan_lock); |
| 160 | |
| 161 | return c; |
| 162 | } |
| 163 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 164 | 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] | 165 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 166 | struct l2cap_chan *c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 167 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 168 | list_for_each_entry(c, &chan_list, global_l) { |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 169 | if (c->sport == psm && !bacmp(&c->src, src)) |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 170 | return c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 171 | } |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 172 | return NULL; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) |
| 176 | { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 177 | int err; |
| 178 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 179 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 180 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 181 | if (psm && __l2cap_global_chan_by_addr(psm, src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 182 | err = -EADDRINUSE; |
| 183 | goto done; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 184 | } |
| 185 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 186 | if (psm) { |
| 187 | chan->psm = psm; |
| 188 | chan->sport = psm; |
| 189 | err = 0; |
| 190 | } else { |
| 191 | u16 p; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 192 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 193 | err = -EINVAL; |
| 194 | for (p = 0x1001; p < 0x1100; p += 2) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 195 | if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 196 | chan->psm = cpu_to_le16(p); |
| 197 | chan->sport = cpu_to_le16(p); |
| 198 | err = 0; |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | done: |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 204 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 205 | return err; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 206 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 207 | EXPORT_SYMBOL_GPL(l2cap_add_psm); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 208 | |
| 209 | int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) |
| 210 | { |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 211 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 212 | |
Johan Hedberg | 1482430 | 2014-08-07 22:56:50 +0300 | [diff] [blame] | 213 | /* Override the defaults (which are for conn-oriented) */ |
| 214 | chan->omtu = L2CAP_DEFAULT_MTU; |
| 215 | chan->chan_type = L2CAP_CHAN_FIXED; |
| 216 | |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 217 | chan->scid = scid; |
| 218 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 219 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 224 | static u16 l2cap_alloc_cid(struct l2cap_conn *conn) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 225 | { |
Johan Hedberg | e77af75 | 2013-10-08 10:31:00 +0200 | [diff] [blame] | 226 | u16 cid, dyn_end; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 227 | |
Johan Hedberg | e77af75 | 2013-10-08 10:31:00 +0200 | [diff] [blame] | 228 | if (conn->hcon->type == LE_LINK) |
| 229 | dyn_end = L2CAP_CID_LE_DYN_END; |
| 230 | else |
| 231 | dyn_end = L2CAP_CID_DYN_END; |
| 232 | |
| 233 | for (cid = L2CAP_CID_DYN_START; cid < dyn_end; cid++) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 234 | if (!__l2cap_get_chan_by_scid(conn, cid)) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 235 | return cid; |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 241 | static void l2cap_state_change(struct l2cap_chan *chan, int state) |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 242 | { |
Andrei Emeltchenko | 42d2d87 | 2012-02-17 11:40:57 +0200 | [diff] [blame] | 243 | BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 244 | state_to_string(state)); |
Gustavo F. Padovan | badaaa0 | 2011-11-23 20:11:46 -0200 | [diff] [blame] | 245 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 246 | chan->state = state; |
Gustavo Padovan | 53f5212 | 2013-10-15 19:24:45 -0300 | [diff] [blame] | 247 | chan->ops->state_change(chan, state, 0); |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 248 | } |
| 249 | |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 250 | static inline void l2cap_state_change_and_error(struct l2cap_chan *chan, |
| 251 | int state, int err) |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 252 | { |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 253 | chan->state = state; |
Gustavo Padovan | 53f5212 | 2013-10-15 19:24:45 -0300 | [diff] [blame] | 254 | chan->ops->state_change(chan, chan->state, err); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err) |
| 258 | { |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 259 | chan->ops->state_change(chan, chan->state, err); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 262 | static void __set_retrans_timer(struct l2cap_chan *chan) |
| 263 | { |
| 264 | if (!delayed_work_pending(&chan->monitor_timer) && |
| 265 | chan->retrans_timeout) { |
| 266 | l2cap_set_timer(chan, &chan->retrans_timer, |
| 267 | msecs_to_jiffies(chan->retrans_timeout)); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | static void __set_monitor_timer(struct l2cap_chan *chan) |
| 272 | { |
| 273 | __clear_retrans_timer(chan); |
| 274 | if (chan->monitor_timeout) { |
| 275 | l2cap_set_timer(chan, &chan->monitor_timer, |
| 276 | msecs_to_jiffies(chan->monitor_timeout)); |
| 277 | } |
| 278 | } |
| 279 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 280 | static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head, |
| 281 | u16 seq) |
| 282 | { |
| 283 | struct sk_buff *skb; |
| 284 | |
| 285 | skb_queue_walk(head, skb) { |
| 286 | if (bt_cb(skb)->control.txseq == seq) |
| 287 | return skb; |
| 288 | } |
| 289 | |
| 290 | return NULL; |
| 291 | } |
| 292 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 293 | /* ---- L2CAP sequence number lists ---- */ |
| 294 | |
| 295 | /* For ERTM, ordered lists of sequence numbers must be tracked for |
| 296 | * SREJ requests that are received and for frames that are to be |
| 297 | * retransmitted. These seq_list functions implement a singly-linked |
| 298 | * list in an array, where membership in the list can also be checked |
| 299 | * in constant time. Items can also be added to the tail of the list |
| 300 | * and removed from the head in constant time, without further memory |
| 301 | * allocs or frees. |
| 302 | */ |
| 303 | |
| 304 | static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size) |
| 305 | { |
| 306 | size_t alloc_size, i; |
| 307 | |
| 308 | /* Allocated size is a power of 2 to map sequence numbers |
| 309 | * (which may be up to 14 bits) in to a smaller array that is |
| 310 | * sized for the negotiated ERTM transmit windows. |
| 311 | */ |
| 312 | alloc_size = roundup_pow_of_two(size); |
| 313 | |
| 314 | seq_list->list = kmalloc(sizeof(u16) * alloc_size, GFP_KERNEL); |
| 315 | if (!seq_list->list) |
| 316 | return -ENOMEM; |
| 317 | |
| 318 | seq_list->mask = alloc_size - 1; |
| 319 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 320 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
| 321 | for (i = 0; i < alloc_size; i++) |
| 322 | seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list) |
| 328 | { |
| 329 | kfree(seq_list->list); |
| 330 | } |
| 331 | |
| 332 | static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list, |
| 333 | u16 seq) |
| 334 | { |
| 335 | /* Constant-time check for list membership */ |
| 336 | return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR; |
| 337 | } |
| 338 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 339 | static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list) |
| 340 | { |
Johan Hedberg | 03a0c5d | 2014-01-18 21:32:59 +0200 | [diff] [blame] | 341 | u16 seq = seq_list->head; |
| 342 | u16 mask = seq_list->mask; |
| 343 | |
| 344 | seq_list->head = seq_list->list[seq & mask]; |
| 345 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR; |
| 346 | |
| 347 | if (seq_list->head == L2CAP_SEQ_LIST_TAIL) { |
| 348 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 349 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
| 350 | } |
| 351 | |
| 352 | return seq; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list) |
| 356 | { |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 357 | u16 i; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 358 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 359 | if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) |
| 360 | return; |
| 361 | |
| 362 | for (i = 0; i <= seq_list->mask; i++) |
| 363 | seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; |
| 364 | |
| 365 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 366 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq) |
| 370 | { |
| 371 | u16 mask = seq_list->mask; |
| 372 | |
| 373 | /* All appends happen in constant time */ |
| 374 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 375 | if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR) |
| 376 | return; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 377 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 378 | if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR) |
| 379 | seq_list->head = seq; |
| 380 | else |
| 381 | seq_list->list[seq_list->tail & mask] = seq; |
| 382 | |
| 383 | seq_list->tail = seq; |
| 384 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 387 | static void l2cap_chan_timeout(struct work_struct *work) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 388 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 389 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 390 | chan_timer.work); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 391 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 392 | int reason; |
| 393 | |
Andrei Emeltchenko | e05dcc3 | 2012-02-17 11:40:56 +0200 | [diff] [blame] | 394 | 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] | 395 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 396 | mutex_lock(&conn->chan_lock); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 397 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 398 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 399 | if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 400 | reason = ECONNREFUSED; |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 401 | else if (chan->state == BT_CONNECT && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 402 | chan->sec_level != BT_SECURITY_SDP) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 403 | reason = ECONNREFUSED; |
| 404 | else |
| 405 | reason = ETIMEDOUT; |
| 406 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 407 | l2cap_chan_close(chan, reason); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 408 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 409 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 410 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 411 | chan->ops->close(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 412 | mutex_unlock(&conn->chan_lock); |
| 413 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 414 | l2cap_chan_put(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 415 | } |
| 416 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 417 | struct l2cap_chan *l2cap_chan_create(void) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 418 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 419 | struct l2cap_chan *chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 420 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 421 | chan = kzalloc(sizeof(*chan), GFP_ATOMIC); |
| 422 | if (!chan) |
| 423 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 424 | |
Andrei Emeltchenko | c03b355 | 2012-02-21 12:54:56 +0200 | [diff] [blame] | 425 | mutex_init(&chan->lock); |
| 426 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 427 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 428 | list_add(&chan->global_l, &chan_list); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 429 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 430 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 431 | INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 432 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 433 | chan->state = BT_OPEN; |
| 434 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 435 | kref_init(&chan->kref); |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 436 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 437 | /* This flag is cleared in l2cap_chan_ready() */ |
| 438 | set_bit(CONF_NOT_COMPLETE, &chan->conf_state); |
| 439 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 440 | BT_DBG("chan %p", chan); |
Szymon Janc | abc545b | 2011-11-03 16:05:44 +0100 | [diff] [blame] | 441 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 442 | return chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 443 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 444 | EXPORT_SYMBOL_GPL(l2cap_chan_create); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 445 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 446 | static void l2cap_chan_destroy(struct kref *kref) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 447 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 448 | struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref); |
| 449 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 450 | BT_DBG("chan %p", chan); |
| 451 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 452 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 453 | list_del(&chan->global_l); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 454 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 455 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 456 | kfree(chan); |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 457 | } |
| 458 | |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 459 | void l2cap_chan_hold(struct l2cap_chan *c) |
| 460 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 461 | BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount)); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 462 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 463 | kref_get(&c->kref); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | void l2cap_chan_put(struct l2cap_chan *c) |
| 467 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 468 | BT_DBG("chan %p orig refcnt %d", c, atomic_read(&c->kref.refcount)); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 469 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 470 | kref_put(&c->kref, l2cap_chan_destroy); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 471 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 472 | EXPORT_SYMBOL_GPL(l2cap_chan_put); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 473 | |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 474 | void l2cap_chan_set_defaults(struct l2cap_chan *chan) |
| 475 | { |
| 476 | chan->fcs = L2CAP_FCS_CRC16; |
| 477 | chan->max_tx = L2CAP_DEFAULT_MAX_TX; |
| 478 | chan->tx_win = L2CAP_DEFAULT_TX_WINDOW; |
| 479 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
Jukka Rissanen | 6a5e816 | 2014-05-28 14:43:04 +0300 | [diff] [blame] | 480 | chan->remote_max_tx = chan->max_tx; |
| 481 | chan->remote_tx_win = chan->tx_win; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 482 | chan->ack_win = L2CAP_DEFAULT_TX_WINDOW; |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 483 | chan->sec_level = BT_SECURITY_LOW; |
Jukka Rissanen | 6a5e816 | 2014-05-28 14:43:04 +0300 | [diff] [blame] | 484 | chan->flush_to = L2CAP_DEFAULT_FLUSH_TO; |
| 485 | chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO; |
| 486 | chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO; |
| 487 | chan->conf_state = 0; |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 488 | |
| 489 | set_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 490 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 491 | EXPORT_SYMBOL_GPL(l2cap_chan_set_defaults); |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 492 | |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 493 | static void l2cap_le_flowctl_init(struct l2cap_chan *chan) |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 494 | { |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 495 | chan->sdu = NULL; |
| 496 | chan->sdu_last_frag = NULL; |
| 497 | chan->sdu_len = 0; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 498 | chan->tx_credits = 0; |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 499 | chan->rx_credits = le_max_credits; |
Johan Hedberg | d1d7941 | 2014-01-27 15:11:33 -0800 | [diff] [blame] | 500 | chan->mps = min_t(u16, chan->imtu, le_default_mps); |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 501 | |
| 502 | skb_queue_head_init(&chan->tx_q); |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 503 | } |
| 504 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 505 | void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 506 | { |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 507 | 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] | 508 | __le16_to_cpu(chan->psm), chan->dcid); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 509 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 510 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 511 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 512 | chan->conn = conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 513 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 514 | switch (chan->chan_type) { |
| 515 | case L2CAP_CHAN_CONN_ORIENTED: |
Johan Hedberg | 21626e6 | 2014-01-24 10:35:41 +0200 | [diff] [blame] | 516 | /* Alloc CID for connection-oriented socket */ |
| 517 | chan->scid = l2cap_alloc_cid(conn); |
| 518 | if (conn->hcon->type == ACL_LINK) |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 519 | chan->omtu = L2CAP_DEFAULT_MTU; |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 520 | break; |
| 521 | |
| 522 | case L2CAP_CHAN_CONN_LESS: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 523 | /* Connectionless socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 524 | chan->scid = L2CAP_CID_CONN_LESS; |
| 525 | chan->dcid = L2CAP_CID_CONN_LESS; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 526 | chan->omtu = L2CAP_DEFAULT_MTU; |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 527 | break; |
| 528 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 529 | case L2CAP_CHAN_FIXED: |
| 530 | /* Caller will set CID and CID specific MTU values */ |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 531 | break; |
| 532 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 533 | default: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 534 | /* Raw socket can send/recv signalling messages only */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 535 | chan->scid = L2CAP_CID_SIGNALING; |
| 536 | chan->dcid = L2CAP_CID_SIGNALING; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 537 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 540 | chan->local_id = L2CAP_BESTEFFORT_ID; |
| 541 | chan->local_stype = L2CAP_SERV_BESTEFFORT; |
| 542 | chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; |
| 543 | chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; |
| 544 | chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 545 | chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO; |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 546 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 547 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 548 | |
Johan Hedberg | c16900c | 2014-08-15 21:17:06 +0300 | [diff] [blame] | 549 | /* Only keep a reference for fixed channels if they requested it */ |
| 550 | if (chan->chan_type != L2CAP_CHAN_FIXED || |
| 551 | test_bit(FLAG_HOLD_HCI_CONN, &chan->flags)) |
| 552 | hci_conn_hold(conn->hcon); |
Johan Hedberg | 5ee9891 | 2013-04-29 19:35:43 +0300 | [diff] [blame] | 553 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 554 | list_add(&chan->list, &conn->chan_l); |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 555 | } |
| 556 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 557 | void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 558 | { |
| 559 | mutex_lock(&conn->chan_lock); |
| 560 | __l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 561 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 562 | } |
| 563 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 564 | void l2cap_chan_del(struct l2cap_chan *chan, int err) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 565 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 566 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 567 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 568 | __clear_chan_timer(chan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 569 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 570 | BT_DBG("chan %p, conn %p, err %d", chan, conn, err); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 571 | |
Johan Hedberg | 72847ce | 2014-08-08 09:28:03 +0300 | [diff] [blame] | 572 | chan->ops->teardown(chan, err); |
| 573 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 574 | if (conn) { |
Andrei Emeltchenko | 56f6098 | 2012-10-15 11:58:44 +0300 | [diff] [blame] | 575 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 576 | /* Delete from channel list */ |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 577 | list_del(&chan->list); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 578 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 579 | l2cap_chan_put(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 580 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 581 | chan->conn = NULL; |
Andrei Emeltchenko | 3cabbfd | 2012-05-31 11:01:37 +0300 | [diff] [blame] | 582 | |
Johan Hedberg | c16900c | 2014-08-15 21:17:06 +0300 | [diff] [blame] | 583 | /* Reference was only held for non-fixed channels or |
| 584 | * fixed channels that explicitly requested it using the |
| 585 | * FLAG_HOLD_HCI_CONN flag. |
| 586 | */ |
| 587 | if (chan->chan_type != L2CAP_CHAN_FIXED || |
| 588 | test_bit(FLAG_HOLD_HCI_CONN, &chan->flags)) |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 589 | hci_conn_drop(conn->hcon); |
Andrei Emeltchenko | 56f6098 | 2012-10-15 11:58:44 +0300 | [diff] [blame] | 590 | |
| 591 | if (mgr && mgr->bredr_chan == chan) |
| 592 | mgr->bredr_chan = NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 593 | } |
| 594 | |
Andrei Emeltchenko | 419e08c | 2012-10-31 15:46:34 +0200 | [diff] [blame] | 595 | if (chan->hs_hchan) { |
| 596 | struct hci_chan *hs_hchan = chan->hs_hchan; |
| 597 | |
| 598 | BT_DBG("chan %p disconnect hs_hchan %p", chan, hs_hchan); |
| 599 | amp_disconnect_logical_link(hs_hchan); |
| 600 | } |
| 601 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 602 | if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 603 | return; |
Gustavo F. Padovan | 2ead70b | 2011-04-01 15:13:36 -0300 | [diff] [blame] | 604 | |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 605 | switch(chan->mode) { |
| 606 | case L2CAP_MODE_BASIC: |
| 607 | break; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 608 | |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 609 | case L2CAP_MODE_LE_FLOWCTL: |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 610 | skb_queue_purge(&chan->tx_q); |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 611 | break; |
| 612 | |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 613 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 614 | __clear_retrans_timer(chan); |
| 615 | __clear_monitor_timer(chan); |
| 616 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 617 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 618 | skb_queue_purge(&chan->srej_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 619 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 620 | l2cap_seq_list_free(&chan->srej_list); |
| 621 | l2cap_seq_list_free(&chan->retrans_list); |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 622 | |
| 623 | /* fall through */ |
| 624 | |
| 625 | case L2CAP_MODE_STREAMING: |
| 626 | skb_queue_purge(&chan->tx_q); |
| 627 | break; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 628 | } |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 629 | |
| 630 | return; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 631 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 632 | EXPORT_SYMBOL_GPL(l2cap_chan_del); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 633 | |
Johan Hedberg | f3d82d0 | 2014-09-05 22:19:50 +0300 | [diff] [blame] | 634 | static void l2cap_conn_update_id_addr(struct work_struct *work) |
Johan Hedberg | 387a33e | 2014-02-18 21:41:33 +0200 | [diff] [blame] | 635 | { |
Johan Hedberg | f3d82d0 | 2014-09-05 22:19:50 +0300 | [diff] [blame] | 636 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
| 637 | id_addr_update_work); |
| 638 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | 387a33e | 2014-02-18 21:41:33 +0200 | [diff] [blame] | 639 | struct l2cap_chan *chan; |
| 640 | |
| 641 | mutex_lock(&conn->chan_lock); |
| 642 | |
| 643 | list_for_each_entry(chan, &conn->chan_l, list) { |
| 644 | l2cap_chan_lock(chan); |
| 645 | bacpy(&chan->dst, &hcon->dst); |
| 646 | chan->dst_type = bdaddr_type(hcon, hcon->dst_type); |
| 647 | l2cap_chan_unlock(chan); |
| 648 | } |
| 649 | |
| 650 | mutex_unlock(&conn->chan_lock); |
| 651 | } |
| 652 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 653 | static void l2cap_chan_le_connect_reject(struct l2cap_chan *chan) |
| 654 | { |
| 655 | struct l2cap_conn *conn = chan->conn; |
| 656 | struct l2cap_le_conn_rsp rsp; |
| 657 | u16 result; |
| 658 | |
| 659 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) |
| 660 | result = L2CAP_CR_AUTHORIZATION; |
| 661 | else |
| 662 | result = L2CAP_CR_BAD_PSM; |
| 663 | |
| 664 | l2cap_state_change(chan, BT_DISCONN); |
| 665 | |
| 666 | rsp.dcid = cpu_to_le16(chan->scid); |
| 667 | rsp.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 668 | rsp.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 669 | rsp.credits = cpu_to_le16(chan->rx_credits); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 670 | rsp.result = cpu_to_le16(result); |
| 671 | |
| 672 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), |
| 673 | &rsp); |
| 674 | } |
| 675 | |
Johan Hedberg | 791d60f | 2013-05-14 22:24:44 +0300 | [diff] [blame] | 676 | static void l2cap_chan_connect_reject(struct l2cap_chan *chan) |
| 677 | { |
| 678 | struct l2cap_conn *conn = chan->conn; |
| 679 | struct l2cap_conn_rsp rsp; |
| 680 | u16 result; |
| 681 | |
| 682 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) |
| 683 | result = L2CAP_CR_SEC_BLOCK; |
| 684 | else |
| 685 | result = L2CAP_CR_BAD_PSM; |
| 686 | |
| 687 | l2cap_state_change(chan, BT_DISCONN); |
| 688 | |
| 689 | rsp.scid = cpu_to_le16(chan->dcid); |
| 690 | rsp.dcid = cpu_to_le16(chan->scid); |
| 691 | rsp.result = cpu_to_le16(result); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 692 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Johan Hedberg | 791d60f | 2013-05-14 22:24:44 +0300 | [diff] [blame] | 693 | |
| 694 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
| 695 | } |
| 696 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 697 | void l2cap_chan_close(struct l2cap_chan *chan, int reason) |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 698 | { |
| 699 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 700 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 701 | 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] | 702 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 703 | switch (chan->state) { |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 704 | case BT_LISTEN: |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 705 | chan->ops->teardown(chan, 0); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 706 | break; |
| 707 | |
| 708 | case BT_CONNECTED: |
| 709 | case BT_CONFIG: |
Johan Hedberg | 7b25c9b | 2014-01-28 15:28:04 -0800 | [diff] [blame] | 710 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { |
Gustavo Padovan | 8d836d7 | 2013-10-15 19:24:47 -0300 | [diff] [blame] | 711 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 712 | l2cap_send_disconn_req(chan, reason); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 713 | } else |
| 714 | l2cap_chan_del(chan, reason); |
| 715 | break; |
| 716 | |
| 717 | case BT_CONNECT2: |
Johan Hedberg | 791d60f | 2013-05-14 22:24:44 +0300 | [diff] [blame] | 718 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { |
| 719 | if (conn->hcon->type == ACL_LINK) |
| 720 | l2cap_chan_connect_reject(chan); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 721 | else if (conn->hcon->type == LE_LINK) |
| 722 | l2cap_chan_le_connect_reject(chan); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | l2cap_chan_del(chan, reason); |
| 726 | break; |
| 727 | |
| 728 | case BT_CONNECT: |
| 729 | case BT_DISCONN: |
| 730 | l2cap_chan_del(chan, reason); |
| 731 | break; |
| 732 | |
| 733 | default: |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 734 | chan->ops->teardown(chan, 0); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 735 | break; |
| 736 | } |
| 737 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 738 | EXPORT_SYMBOL(l2cap_chan_close); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 739 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 740 | static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 741 | { |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 742 | switch (chan->chan_type) { |
| 743 | case L2CAP_CHAN_RAW: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 744 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 745 | case BT_SECURITY_HIGH: |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 746 | case BT_SECURITY_FIPS: |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 747 | return HCI_AT_DEDICATED_BONDING_MITM; |
| 748 | case BT_SECURITY_MEDIUM: |
| 749 | return HCI_AT_DEDICATED_BONDING; |
| 750 | default: |
| 751 | return HCI_AT_NO_BONDING; |
| 752 | } |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 753 | break; |
Marcel Holtmann | 3124b84 | 2013-10-12 07:19:32 -0700 | [diff] [blame] | 754 | case L2CAP_CHAN_CONN_LESS: |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 755 | if (chan->psm == cpu_to_le16(L2CAP_PSM_3DSP)) { |
Marcel Holtmann | 3124b84 | 2013-10-12 07:19:32 -0700 | [diff] [blame] | 756 | if (chan->sec_level == BT_SECURITY_LOW) |
| 757 | chan->sec_level = BT_SECURITY_SDP; |
| 758 | } |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 759 | if (chan->sec_level == BT_SECURITY_HIGH || |
| 760 | chan->sec_level == BT_SECURITY_FIPS) |
Marcel Holtmann | 3124b84 | 2013-10-12 07:19:32 -0700 | [diff] [blame] | 761 | return HCI_AT_NO_BONDING_MITM; |
| 762 | else |
| 763 | return HCI_AT_NO_BONDING; |
| 764 | break; |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 765 | case L2CAP_CHAN_CONN_ORIENTED: |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 766 | if (chan->psm == cpu_to_le16(L2CAP_PSM_SDP)) { |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 767 | if (chan->sec_level == BT_SECURITY_LOW) |
| 768 | chan->sec_level = BT_SECURITY_SDP; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 769 | |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 770 | if (chan->sec_level == BT_SECURITY_HIGH || |
| 771 | chan->sec_level == BT_SECURITY_FIPS) |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 772 | return HCI_AT_NO_BONDING_MITM; |
| 773 | else |
| 774 | return HCI_AT_NO_BONDING; |
| 775 | } |
| 776 | /* fall through */ |
| 777 | default: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 778 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 779 | case BT_SECURITY_HIGH: |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 780 | case BT_SECURITY_FIPS: |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 781 | return HCI_AT_GENERAL_BONDING_MITM; |
| 782 | case BT_SECURITY_MEDIUM: |
| 783 | return HCI_AT_GENERAL_BONDING; |
| 784 | default: |
| 785 | return HCI_AT_NO_BONDING; |
| 786 | } |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 787 | break; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 788 | } |
| 789 | } |
| 790 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 791 | /* Service level security */ |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 792 | int l2cap_chan_check_security(struct l2cap_chan *chan, bool initiator) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 793 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 794 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 795 | __u8 auth_type; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 796 | |
Johan Hedberg | a17de2f | 2013-05-14 13:25:37 +0300 | [diff] [blame] | 797 | if (conn->hcon->type == LE_LINK) |
| 798 | return smp_conn_security(conn->hcon, chan->sec_level); |
| 799 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 800 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 801 | |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 802 | return hci_conn_security(conn->hcon, chan->sec_level, auth_type, |
| 803 | initiator); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 804 | } |
| 805 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 806 | static u8 l2cap_get_ident(struct l2cap_conn *conn) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 807 | { |
| 808 | u8 id; |
| 809 | |
| 810 | /* Get next available identificator. |
| 811 | * 1 - 128 are used by kernel. |
| 812 | * 129 - 199 are reserved. |
| 813 | * 200 - 254 are used by utilities like l2ping, etc. |
| 814 | */ |
| 815 | |
Marcel Holtmann | 5a54e7c | 2014-07-13 20:50:15 +0200 | [diff] [blame] | 816 | mutex_lock(&conn->ident_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 817 | |
| 818 | if (++conn->tx_ident > 128) |
| 819 | conn->tx_ident = 1; |
| 820 | |
| 821 | id = conn->tx_ident; |
| 822 | |
Marcel Holtmann | 5a54e7c | 2014-07-13 20:50:15 +0200 | [diff] [blame] | 823 | mutex_unlock(&conn->ident_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 824 | |
| 825 | return id; |
| 826 | } |
| 827 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 828 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, |
| 829 | void *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 830 | { |
| 831 | struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 832 | u8 flags; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 833 | |
| 834 | BT_DBG("code 0x%2.2x", code); |
| 835 | |
| 836 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 837 | return; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 838 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 839 | if (lmp_no_flush_capable(conn->hcon->hdev)) |
| 840 | flags = ACL_START_NO_FLUSH; |
| 841 | else |
| 842 | flags = ACL_START; |
| 843 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 844 | bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 845 | skb->priority = HCI_PRIO_MAX; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 846 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 847 | hci_send_acl(conn->hchan, skb, flags); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 848 | } |
| 849 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 850 | static bool __chan_is_moving(struct l2cap_chan *chan) |
| 851 | { |
| 852 | return chan->move_state != L2CAP_MOVE_STABLE && |
| 853 | chan->move_state != L2CAP_MOVE_WAIT_PREPARE; |
| 854 | } |
| 855 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 856 | static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) |
| 857 | { |
| 858 | struct hci_conn *hcon = chan->conn->hcon; |
| 859 | u16 flags; |
| 860 | |
| 861 | 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] | 862 | skb->priority); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 863 | |
Mat Martineau | d5f8a75 | 2012-10-23 15:24:18 -0700 | [diff] [blame] | 864 | if (chan->hs_hcon && !__chan_is_moving(chan)) { |
| 865 | if (chan->hs_hchan) |
| 866 | hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE); |
| 867 | else |
| 868 | kfree_skb(skb); |
| 869 | |
| 870 | return; |
| 871 | } |
| 872 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 873 | if (!test_bit(FLAG_FLUSHABLE, &chan->flags) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 874 | lmp_no_flush_capable(hcon->hdev)) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 875 | flags = ACL_START_NO_FLUSH; |
| 876 | else |
| 877 | flags = ACL_START; |
| 878 | |
| 879 | bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 880 | hci_send_acl(chan->conn->hchan, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 883 | static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control) |
| 884 | { |
| 885 | control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT; |
| 886 | control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT; |
| 887 | |
| 888 | if (enh & L2CAP_CTRL_FRAME_TYPE) { |
| 889 | /* S-Frame */ |
| 890 | control->sframe = 1; |
| 891 | control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT; |
| 892 | control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT; |
| 893 | |
| 894 | control->sar = 0; |
| 895 | control->txseq = 0; |
| 896 | } else { |
| 897 | /* I-Frame */ |
| 898 | control->sframe = 0; |
| 899 | control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT; |
| 900 | control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT; |
| 901 | |
| 902 | control->poll = 0; |
| 903 | control->super = 0; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control) |
| 908 | { |
| 909 | control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT; |
| 910 | control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT; |
| 911 | |
| 912 | if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) { |
| 913 | /* S-Frame */ |
| 914 | control->sframe = 1; |
| 915 | control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT; |
| 916 | control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT; |
| 917 | |
| 918 | control->sar = 0; |
| 919 | control->txseq = 0; |
| 920 | } else { |
| 921 | /* I-Frame */ |
| 922 | control->sframe = 0; |
| 923 | control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT; |
| 924 | control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT; |
| 925 | |
| 926 | control->poll = 0; |
| 927 | control->super = 0; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | static inline void __unpack_control(struct l2cap_chan *chan, |
| 932 | struct sk_buff *skb) |
| 933 | { |
| 934 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 935 | __unpack_extended_control(get_unaligned_le32(skb->data), |
| 936 | &bt_cb(skb)->control); |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 937 | skb_pull(skb, L2CAP_EXT_CTRL_SIZE); |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 938 | } else { |
| 939 | __unpack_enhanced_control(get_unaligned_le16(skb->data), |
| 940 | &bt_cb(skb)->control); |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 941 | skb_pull(skb, L2CAP_ENH_CTRL_SIZE); |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | |
| 945 | static u32 __pack_extended_control(struct l2cap_ctrl *control) |
| 946 | { |
| 947 | u32 packed; |
| 948 | |
| 949 | packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT; |
| 950 | packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT; |
| 951 | |
| 952 | if (control->sframe) { |
| 953 | packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT; |
| 954 | packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT; |
| 955 | packed |= L2CAP_EXT_CTRL_FRAME_TYPE; |
| 956 | } else { |
| 957 | packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT; |
| 958 | packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT; |
| 959 | } |
| 960 | |
| 961 | return packed; |
| 962 | } |
| 963 | |
| 964 | static u16 __pack_enhanced_control(struct l2cap_ctrl *control) |
| 965 | { |
| 966 | u16 packed; |
| 967 | |
| 968 | packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT; |
| 969 | packed |= control->final << L2CAP_CTRL_FINAL_SHIFT; |
| 970 | |
| 971 | if (control->sframe) { |
| 972 | packed |= control->poll << L2CAP_CTRL_POLL_SHIFT; |
| 973 | packed |= control->super << L2CAP_CTRL_SUPER_SHIFT; |
| 974 | packed |= L2CAP_CTRL_FRAME_TYPE; |
| 975 | } else { |
| 976 | packed |= control->sar << L2CAP_CTRL_SAR_SHIFT; |
| 977 | packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT; |
| 978 | } |
| 979 | |
| 980 | return packed; |
| 981 | } |
| 982 | |
| 983 | static inline void __pack_control(struct l2cap_chan *chan, |
| 984 | struct l2cap_ctrl *control, |
| 985 | struct sk_buff *skb) |
| 986 | { |
| 987 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 988 | put_unaligned_le32(__pack_extended_control(control), |
| 989 | skb->data + L2CAP_HDR_SIZE); |
| 990 | } else { |
| 991 | put_unaligned_le16(__pack_enhanced_control(control), |
| 992 | skb->data + L2CAP_HDR_SIZE); |
| 993 | } |
| 994 | } |
| 995 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 996 | static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan) |
| 997 | { |
| 998 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 999 | return L2CAP_EXT_HDR_SIZE; |
| 1000 | else |
| 1001 | return L2CAP_ENH_HDR_SIZE; |
| 1002 | } |
| 1003 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1004 | static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan, |
| 1005 | u32 control) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1006 | { |
| 1007 | struct sk_buff *skb; |
| 1008 | struct l2cap_hdr *lh; |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 1009 | int hlen = __ertm_hdr_size(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1010 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1011 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1012 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1013 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1014 | skb = bt_skb_alloc(hlen, GFP_KERNEL); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1015 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1016 | if (!skb) |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1017 | return ERR_PTR(-ENOMEM); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1018 | |
| 1019 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1020 | lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1021 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1022 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1023 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 1024 | put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 1025 | else |
| 1026 | put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1027 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1028 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1029 | u16 fcs = crc16(0, (u8 *)skb->data, skb->len); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1030 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1031 | } |
| 1032 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1033 | skb->priority = HCI_PRIO_MAX; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1034 | return skb; |
| 1035 | } |
| 1036 | |
| 1037 | static void l2cap_send_sframe(struct l2cap_chan *chan, |
| 1038 | struct l2cap_ctrl *control) |
| 1039 | { |
| 1040 | struct sk_buff *skb; |
| 1041 | u32 control_field; |
| 1042 | |
| 1043 | BT_DBG("chan %p, control %p", chan, control); |
| 1044 | |
| 1045 | if (!control->sframe) |
| 1046 | return; |
| 1047 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1048 | if (__chan_is_moving(chan)) |
| 1049 | return; |
| 1050 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1051 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) && |
| 1052 | !control->poll) |
| 1053 | control->final = 1; |
| 1054 | |
| 1055 | if (control->super == L2CAP_SUPER_RR) |
| 1056 | clear_bit(CONN_RNR_SENT, &chan->conn_state); |
| 1057 | else if (control->super == L2CAP_SUPER_RNR) |
| 1058 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
| 1059 | |
| 1060 | if (control->super != L2CAP_SUPER_SREJ) { |
| 1061 | chan->last_acked_seq = control->reqseq; |
| 1062 | __clear_ack_timer(chan); |
| 1063 | } |
| 1064 | |
| 1065 | BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq, |
| 1066 | control->final, control->poll, control->super); |
| 1067 | |
| 1068 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 1069 | control_field = __pack_extended_control(control); |
| 1070 | else |
| 1071 | control_field = __pack_enhanced_control(control); |
| 1072 | |
| 1073 | skb = l2cap_create_sframe_pdu(chan, control_field); |
| 1074 | if (!IS_ERR(skb)) |
| 1075 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1076 | } |
| 1077 | |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1078 | 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] | 1079 | { |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1080 | struct l2cap_ctrl control; |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 1081 | |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1082 | BT_DBG("chan %p, poll %d", chan, poll); |
| 1083 | |
| 1084 | memset(&control, 0, sizeof(control)); |
| 1085 | control.sframe = 1; |
| 1086 | control.poll = poll; |
| 1087 | |
| 1088 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 1089 | control.super = L2CAP_SUPER_RNR; |
| 1090 | else |
| 1091 | control.super = L2CAP_SUPER_RR; |
| 1092 | |
| 1093 | control.reqseq = chan->buffer_seq; |
| 1094 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 1095 | } |
| 1096 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1097 | static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 1098 | { |
Johan Hedberg | 5ff6f34 | 2014-08-07 22:56:43 +0300 | [diff] [blame] | 1099 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
| 1100 | return true; |
| 1101 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1102 | return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 1103 | } |
| 1104 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1105 | static bool __amp_capable(struct l2cap_chan *chan) |
| 1106 | { |
| 1107 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 1df7b17 | 2013-10-05 11:47:49 -0700 | [diff] [blame] | 1108 | struct hci_dev *hdev; |
| 1109 | bool amp_available = false; |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1110 | |
Marcel Holtmann | 1df7b17 | 2013-10-05 11:47:49 -0700 | [diff] [blame] | 1111 | if (!conn->hs_enabled) |
| 1112 | return false; |
| 1113 | |
| 1114 | if (!(conn->fixed_chan_mask & L2CAP_FC_A2MP)) |
| 1115 | return false; |
| 1116 | |
| 1117 | read_lock(&hci_dev_list_lock); |
| 1118 | list_for_each_entry(hdev, &hci_dev_list, list) { |
| 1119 | if (hdev->amp_type != AMP_TYPE_BREDR && |
| 1120 | test_bit(HCI_UP, &hdev->flags)) { |
| 1121 | amp_available = true; |
| 1122 | break; |
| 1123 | } |
| 1124 | } |
| 1125 | read_unlock(&hci_dev_list_lock); |
| 1126 | |
| 1127 | if (chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED) |
| 1128 | return amp_available; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 1129 | |
| 1130 | return false; |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1131 | } |
| 1132 | |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 1133 | static bool l2cap_check_efs(struct l2cap_chan *chan) |
| 1134 | { |
| 1135 | /* Check EFS parameters */ |
| 1136 | return true; |
| 1137 | } |
| 1138 | |
Andrei Emeltchenko | 2766be4 | 2012-09-27 17:26:21 +0300 | [diff] [blame] | 1139 | void l2cap_send_conn_req(struct l2cap_chan *chan) |
Andrei Emeltchenko | 9b27f35 | 2012-02-24 16:00:00 +0200 | [diff] [blame] | 1140 | { |
| 1141 | struct l2cap_conn *conn = chan->conn; |
| 1142 | struct l2cap_conn_req req; |
| 1143 | |
| 1144 | req.scid = cpu_to_le16(chan->scid); |
| 1145 | req.psm = chan->psm; |
| 1146 | |
| 1147 | chan->ident = l2cap_get_ident(conn); |
| 1148 | |
| 1149 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
| 1150 | |
| 1151 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req); |
| 1152 | } |
| 1153 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 1154 | static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id) |
| 1155 | { |
| 1156 | struct l2cap_create_chan_req req; |
| 1157 | req.scid = cpu_to_le16(chan->scid); |
| 1158 | req.psm = chan->psm; |
| 1159 | req.amp_id = amp_id; |
| 1160 | |
| 1161 | chan->ident = l2cap_get_ident(chan->conn); |
| 1162 | |
| 1163 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ, |
| 1164 | sizeof(req), &req); |
| 1165 | } |
| 1166 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 1167 | static void l2cap_move_setup(struct l2cap_chan *chan) |
| 1168 | { |
| 1169 | struct sk_buff *skb; |
| 1170 | |
| 1171 | BT_DBG("chan %p", chan); |
| 1172 | |
| 1173 | if (chan->mode != L2CAP_MODE_ERTM) |
| 1174 | return; |
| 1175 | |
| 1176 | __clear_retrans_timer(chan); |
| 1177 | __clear_monitor_timer(chan); |
| 1178 | __clear_ack_timer(chan); |
| 1179 | |
| 1180 | chan->retry_count = 0; |
| 1181 | skb_queue_walk(&chan->tx_q, skb) { |
| 1182 | if (bt_cb(skb)->control.retries) |
| 1183 | bt_cb(skb)->control.retries = 1; |
| 1184 | else |
| 1185 | break; |
| 1186 | } |
| 1187 | |
| 1188 | chan->expected_tx_seq = chan->buffer_seq; |
| 1189 | |
| 1190 | clear_bit(CONN_REJ_ACT, &chan->conn_state); |
| 1191 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 1192 | l2cap_seq_list_clear(&chan->retrans_list); |
| 1193 | l2cap_seq_list_clear(&chan->srej_list); |
| 1194 | skb_queue_purge(&chan->srej_q); |
| 1195 | |
| 1196 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 1197 | chan->rx_state = L2CAP_RX_STATE_MOVE; |
| 1198 | |
| 1199 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 1200 | } |
| 1201 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 1202 | static void l2cap_move_done(struct l2cap_chan *chan) |
| 1203 | { |
| 1204 | u8 move_role = chan->move_role; |
| 1205 | BT_DBG("chan %p", chan); |
| 1206 | |
| 1207 | chan->move_state = L2CAP_MOVE_STABLE; |
| 1208 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 1209 | |
| 1210 | if (chan->mode != L2CAP_MODE_ERTM) |
| 1211 | return; |
| 1212 | |
| 1213 | switch (move_role) { |
| 1214 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 1215 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL); |
| 1216 | chan->rx_state = L2CAP_RX_STATE_WAIT_F; |
| 1217 | break; |
| 1218 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 1219 | chan->rx_state = L2CAP_RX_STATE_WAIT_P; |
| 1220 | break; |
| 1221 | } |
| 1222 | } |
| 1223 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1224 | static void l2cap_chan_ready(struct l2cap_chan *chan) |
| 1225 | { |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 1226 | /* This clears all conf flags, including CONF_NOT_COMPLETE */ |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1227 | chan->conf_state = 0; |
| 1228 | __clear_chan_timer(chan); |
| 1229 | |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 1230 | if (chan->mode == L2CAP_MODE_LE_FLOWCTL && !chan->tx_credits) |
| 1231 | chan->ops->suspend(chan); |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 1232 | |
Andrei Emeltchenko | 54a59aa | 2012-05-27 22:27:53 -0300 | [diff] [blame] | 1233 | chan->state = BT_CONNECTED; |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1234 | |
Andrei Emeltchenko | fd83e2c | 2012-05-30 09:55:32 +0300 | [diff] [blame] | 1235 | chan->ops->ready(chan); |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1236 | } |
| 1237 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1238 | static void l2cap_le_connect(struct l2cap_chan *chan) |
| 1239 | { |
| 1240 | struct l2cap_conn *conn = chan->conn; |
| 1241 | struct l2cap_le_conn_req req; |
| 1242 | |
Johan Hedberg | 595177f | 2013-12-02 22:12:22 +0200 | [diff] [blame] | 1243 | if (test_and_set_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags)) |
| 1244 | return; |
| 1245 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1246 | req.psm = chan->psm; |
| 1247 | req.scid = cpu_to_le16(chan->scid); |
| 1248 | req.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 1249 | req.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 1250 | req.credits = cpu_to_le16(chan->rx_credits); |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1251 | |
| 1252 | chan->ident = l2cap_get_ident(conn); |
| 1253 | |
| 1254 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_REQ, |
| 1255 | sizeof(req), &req); |
| 1256 | } |
| 1257 | |
| 1258 | static void l2cap_le_start(struct l2cap_chan *chan) |
| 1259 | { |
| 1260 | struct l2cap_conn *conn = chan->conn; |
| 1261 | |
| 1262 | if (!smp_conn_security(conn->hcon, chan->sec_level)) |
| 1263 | return; |
| 1264 | |
| 1265 | if (!chan->psm) { |
| 1266 | l2cap_chan_ready(chan); |
| 1267 | return; |
| 1268 | } |
| 1269 | |
| 1270 | if (chan->state == BT_CONNECT) |
| 1271 | l2cap_le_connect(chan); |
| 1272 | } |
| 1273 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1274 | static void l2cap_start_connection(struct l2cap_chan *chan) |
| 1275 | { |
| 1276 | if (__amp_capable(chan)) { |
| 1277 | BT_DBG("chan %p AMP capable: discover AMPs", chan); |
| 1278 | a2mp_discover_amp(chan); |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1279 | } else if (chan->conn->hcon->type == LE_LINK) { |
| 1280 | l2cap_le_start(chan); |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1281 | } else { |
| 1282 | l2cap_send_conn_req(chan); |
| 1283 | } |
| 1284 | } |
| 1285 | |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1286 | static void l2cap_request_info(struct l2cap_conn *conn) |
| 1287 | { |
| 1288 | struct l2cap_info_req req; |
| 1289 | |
| 1290 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
| 1291 | return; |
| 1292 | |
| 1293 | req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 1294 | |
| 1295 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 1296 | conn->info_ident = l2cap_get_ident(conn); |
| 1297 | |
| 1298 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
| 1299 | |
| 1300 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 1301 | sizeof(req), &req); |
| 1302 | } |
| 1303 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1304 | static void l2cap_do_start(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1305 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1306 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1307 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1308 | if (conn->hcon->type == LE_LINK) { |
Johan Hedberg | 96ac34f | 2013-05-13 11:15:07 +0300 | [diff] [blame] | 1309 | l2cap_le_start(chan); |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1310 | return; |
| 1311 | } |
| 1312 | |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1313 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)) { |
| 1314 | l2cap_request_info(conn); |
| 1315 | return; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1316 | } |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1317 | |
| 1318 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) |
| 1319 | return; |
| 1320 | |
| 1321 | if (l2cap_chan_check_security(chan, true) && |
| 1322 | __l2cap_no_conn_pending(chan)) |
| 1323 | l2cap_start_connection(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1324 | } |
| 1325 | |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 1326 | static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) |
| 1327 | { |
| 1328 | u32 local_feat_mask = l2cap_feat_mask; |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 1329 | if (!disable_ertm) |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 1330 | local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; |
| 1331 | |
| 1332 | switch (mode) { |
| 1333 | case L2CAP_MODE_ERTM: |
| 1334 | return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; |
| 1335 | case L2CAP_MODE_STREAMING: |
| 1336 | return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; |
| 1337 | default: |
| 1338 | return 0x00; |
| 1339 | } |
| 1340 | } |
| 1341 | |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1342 | 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] | 1343 | { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1344 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1345 | struct l2cap_disconn_req req; |
| 1346 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1347 | if (!conn) |
| 1348 | return; |
| 1349 | |
Andrei Emeltchenko | aad3d0e | 2012-09-06 15:05:42 +0300 | [diff] [blame] | 1350 | if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1351 | __clear_retrans_timer(chan); |
| 1352 | __clear_monitor_timer(chan); |
| 1353 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1354 | } |
| 1355 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 1356 | if (chan->scid == L2CAP_CID_A2MP) { |
Gustavo Padovan | d117773 | 2012-10-06 11:47:38 +0100 | [diff] [blame] | 1357 | l2cap_state_change(chan, BT_DISCONN); |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1358 | return; |
| 1359 | } |
| 1360 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1361 | req.dcid = cpu_to_le16(chan->dcid); |
| 1362 | req.scid = cpu_to_le16(chan->scid); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1363 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ, |
| 1364 | sizeof(req), &req); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1365 | |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 1366 | l2cap_state_change_and_error(chan, BT_DISCONN, err); |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1367 | } |
| 1368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | /* ---- L2CAP connections ---- */ |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1370 | static void l2cap_conn_start(struct l2cap_conn *conn) |
| 1371 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1372 | struct l2cap_chan *chan, *tmp; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1373 | |
| 1374 | BT_DBG("conn %p", conn); |
| 1375 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1376 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1377 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1378 | list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1379 | l2cap_chan_lock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1380 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1381 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1382 | l2cap_chan_ready(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1383 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1384 | continue; |
| 1385 | } |
| 1386 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1387 | if (chan->state == BT_CONNECT) { |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 1388 | if (!l2cap_chan_check_security(chan, true) || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1389 | !__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1390 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1391 | continue; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 1392 | } |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1393 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1394 | if (!l2cap_mode_supported(chan->mode, conn->feat_mask) |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1395 | && test_bit(CONF_STATE2_DEVICE, |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1396 | &chan->conf_state)) { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1397 | l2cap_chan_close(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1398 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1399 | continue; |
| 1400 | } |
| 1401 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1402 | l2cap_start_connection(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1403 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1404 | } else if (chan->state == BT_CONNECT2) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1405 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1406 | char buf[128]; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1407 | rsp.scid = cpu_to_le16(chan->dcid); |
| 1408 | rsp.dcid = cpu_to_le16(chan->scid); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1409 | |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 1410 | if (l2cap_chan_check_security(chan, false)) { |
Marcel Holtmann | bdc2578 | 2013-10-14 02:45:34 -0700 | [diff] [blame] | 1411 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1412 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 1413 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 1414 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 1415 | |
| 1416 | } else { |
Gustavo Padovan | acdcabf | 2013-10-21 14:21:39 -0200 | [diff] [blame] | 1417 | l2cap_state_change(chan, BT_CONFIG); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1418 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 1419 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 1420 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1421 | } else { |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1422 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 1423 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1424 | } |
| 1425 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1426 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1427 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1428 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1429 | if (test_bit(CONF_REQ_SENT, &chan->conf_state) || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1430 | rsp.result != L2CAP_CR_SUCCESS) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1431 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1432 | continue; |
| 1433 | } |
| 1434 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1435 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1436 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1437 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1438 | chan->num_conf_req++; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1439 | } |
| 1440 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1441 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1442 | } |
| 1443 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1444 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1445 | } |
| 1446 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1447 | static void l2cap_le_conn_ready(struct l2cap_conn *conn) |
| 1448 | { |
Marcel Holtmann | cc8dba2 | 2013-10-18 03:43:01 -0700 | [diff] [blame] | 1449 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 1450 | struct hci_dev *hdev = hcon->hdev; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1451 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 1452 | BT_DBG("%s conn %p", hdev->name, conn); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1453 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 1454 | /* For outgoing pairing which doesn't necessarily have an |
| 1455 | * associated socket (e.g. mgmt_pair_device). |
| 1456 | */ |
| 1457 | if (hcon->out) |
| 1458 | smp_conn_security(hcon, hcon->pending_sec_level); |
Marcel Holtmann | cc8dba2 | 2013-10-18 03:43:01 -0700 | [diff] [blame] | 1459 | |
Marcel Holtmann | 80afeb6 | 2014-06-23 12:18:51 +0200 | [diff] [blame] | 1460 | /* For LE slave connections, make sure the connection interval |
| 1461 | * is in the range of the minium and maximum interval that has |
| 1462 | * been configured for this connection. If not, then trigger |
| 1463 | * the connection update procedure. |
| 1464 | */ |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1465 | if (hcon->role == HCI_ROLE_SLAVE && |
Marcel Holtmann | 80afeb6 | 2014-06-23 12:18:51 +0200 | [diff] [blame] | 1466 | (hcon->le_conn_interval < hcon->le_conn_min_interval || |
| 1467 | hcon->le_conn_interval > hcon->le_conn_max_interval)) { |
| 1468 | struct l2cap_conn_param_update_req req; |
| 1469 | |
| 1470 | req.min = cpu_to_le16(hcon->le_conn_min_interval); |
| 1471 | req.max = cpu_to_le16(hcon->le_conn_max_interval); |
| 1472 | req.latency = cpu_to_le16(hcon->le_conn_latency); |
| 1473 | req.to_multiplier = cpu_to_le16(hcon->le_supv_timeout); |
| 1474 | |
| 1475 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 1476 | L2CAP_CONN_PARAM_UPDATE_REQ, sizeof(req), &req); |
| 1477 | } |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1478 | } |
| 1479 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1480 | static void l2cap_conn_ready(struct l2cap_conn *conn) |
| 1481 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1482 | struct l2cap_chan *chan; |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1483 | struct hci_conn *hcon = conn->hcon; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1484 | |
| 1485 | BT_DBG("conn %p", conn); |
| 1486 | |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1487 | if (hcon->type == ACL_LINK) |
| 1488 | l2cap_request_info(conn); |
| 1489 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 1490 | mutex_lock(&conn->chan_lock); |
| 1491 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1492 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1493 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1494 | l2cap_chan_lock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1495 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 1496 | if (chan->scid == L2CAP_CID_A2MP) { |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1497 | l2cap_chan_unlock(chan); |
| 1498 | continue; |
| 1499 | } |
| 1500 | |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1501 | if (hcon->type == LE_LINK) { |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1502 | l2cap_le_start(chan); |
Vinicius Costa Gomes | 6312845 | 2011-06-17 22:46:26 -0300 | [diff] [blame] | 1503 | } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Johan Hedberg | aeaeb4b | 2014-09-10 17:37:46 -0700 | [diff] [blame] | 1504 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
| 1505 | l2cap_chan_ready(chan); |
Gustavo Padovan | 1c244f7 | 2012-12-07 03:29:10 -0200 | [diff] [blame] | 1506 | } else if (chan->state == BT_CONNECT) { |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1507 | l2cap_do_start(chan); |
Gustavo Padovan | 1c244f7 | 2012-12-07 03:29:10 -0200 | [diff] [blame] | 1508 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1509 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1510 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1511 | } |
| 1512 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1513 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1514 | |
Johan Hedberg | 79a0572 | 2014-08-08 09:28:04 +0300 | [diff] [blame] | 1515 | if (hcon->type == LE_LINK) |
| 1516 | l2cap_le_conn_ready(conn); |
| 1517 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1518 | queue_work(hcon->hdev->workqueue, &conn->pending_rx_work); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1519 | } |
| 1520 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1521 | /* Notify sockets that we cannot guaranty reliability anymore */ |
| 1522 | static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) |
| 1523 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1524 | struct l2cap_chan *chan; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1525 | |
| 1526 | BT_DBG("conn %p", conn); |
| 1527 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1528 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1529 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1530 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | ecf61bd | 2011-10-11 14:04:32 +0300 | [diff] [blame] | 1531 | if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) |
Gustavo Padovan | 1d8b1fd | 2012-10-06 11:34:52 +0100 | [diff] [blame] | 1532 | l2cap_chan_set_err(chan, err); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1533 | } |
| 1534 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1535 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1536 | } |
| 1537 | |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1538 | static void l2cap_info_timeout(struct work_struct *work) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1539 | { |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1540 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1541 | info_timer.work); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1542 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1543 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 1544 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1545 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1546 | l2cap_conn_start(conn); |
| 1547 | } |
| 1548 | |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1549 | /* |
| 1550 | * l2cap_user |
| 1551 | * External modules can register l2cap_user objects on l2cap_conn. The ->probe |
| 1552 | * callback is called during registration. The ->remove callback is called |
| 1553 | * during unregistration. |
| 1554 | * An l2cap_user object can either be explicitly unregistered or when the |
| 1555 | * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon, |
| 1556 | * l2cap->hchan, .. are valid as long as the remove callback hasn't been called. |
| 1557 | * External modules must own a reference to the l2cap_conn object if they intend |
| 1558 | * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at |
| 1559 | * any time if they don't. |
| 1560 | */ |
| 1561 | |
| 1562 | int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user) |
| 1563 | { |
| 1564 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1565 | int ret; |
| 1566 | |
| 1567 | /* We need to check whether l2cap_conn is registered. If it is not, we |
| 1568 | * must not register the l2cap_user. l2cap_conn_del() is unregisters |
| 1569 | * l2cap_conn objects, but doesn't provide its own locking. Instead, it |
| 1570 | * relies on the parent hci_conn object to be locked. This itself relies |
| 1571 | * on the hci_dev object to be locked. So we must lock the hci device |
| 1572 | * here, too. */ |
| 1573 | |
| 1574 | hci_dev_lock(hdev); |
| 1575 | |
| 1576 | if (user->list.next || user->list.prev) { |
| 1577 | ret = -EINVAL; |
| 1578 | goto out_unlock; |
| 1579 | } |
| 1580 | |
| 1581 | /* conn->hchan is NULL after l2cap_conn_del() was called */ |
| 1582 | if (!conn->hchan) { |
| 1583 | ret = -ENODEV; |
| 1584 | goto out_unlock; |
| 1585 | } |
| 1586 | |
| 1587 | ret = user->probe(conn, user); |
| 1588 | if (ret) |
| 1589 | goto out_unlock; |
| 1590 | |
| 1591 | list_add(&user->list, &conn->users); |
| 1592 | ret = 0; |
| 1593 | |
| 1594 | out_unlock: |
| 1595 | hci_dev_unlock(hdev); |
| 1596 | return ret; |
| 1597 | } |
| 1598 | EXPORT_SYMBOL(l2cap_register_user); |
| 1599 | |
| 1600 | void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user) |
| 1601 | { |
| 1602 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1603 | |
| 1604 | hci_dev_lock(hdev); |
| 1605 | |
| 1606 | if (!user->list.next || !user->list.prev) |
| 1607 | goto out_unlock; |
| 1608 | |
| 1609 | list_del(&user->list); |
| 1610 | user->list.next = NULL; |
| 1611 | user->list.prev = NULL; |
| 1612 | user->remove(conn, user); |
| 1613 | |
| 1614 | out_unlock: |
| 1615 | hci_dev_unlock(hdev); |
| 1616 | } |
| 1617 | EXPORT_SYMBOL(l2cap_unregister_user); |
| 1618 | |
| 1619 | static void l2cap_unregister_all_users(struct l2cap_conn *conn) |
| 1620 | { |
| 1621 | struct l2cap_user *user; |
| 1622 | |
| 1623 | while (!list_empty(&conn->users)) { |
| 1624 | user = list_first_entry(&conn->users, struct l2cap_user, list); |
| 1625 | list_del(&user->list); |
| 1626 | user->list.next = NULL; |
| 1627 | user->list.prev = NULL; |
| 1628 | user->remove(conn, user); |
| 1629 | } |
| 1630 | } |
| 1631 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1632 | static void l2cap_conn_del(struct hci_conn *hcon, int err) |
| 1633 | { |
| 1634 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 1635 | struct l2cap_chan *chan, *l; |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1636 | |
| 1637 | if (!conn) |
| 1638 | return; |
| 1639 | |
| 1640 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 1641 | |
| 1642 | kfree_skb(conn->rx_skb); |
| 1643 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1644 | skb_queue_purge(&conn->pending_rx); |
Jukka Taimisto | 7ab56c3 | 2014-06-12 10:15:13 +0000 | [diff] [blame] | 1645 | |
| 1646 | /* We can not call flush_work(&conn->pending_rx_work) here since we |
| 1647 | * might block if we are running on a worker from the same workqueue |
| 1648 | * pending_rx_work is waiting on. |
| 1649 | */ |
| 1650 | if (work_pending(&conn->pending_rx_work)) |
| 1651 | cancel_work_sync(&conn->pending_rx_work); |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1652 | |
Johan Hedberg | f3d82d0 | 2014-09-05 22:19:50 +0300 | [diff] [blame] | 1653 | if (work_pending(&conn->id_addr_update_work)) |
| 1654 | cancel_work_sync(&conn->id_addr_update_work); |
| 1655 | |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1656 | l2cap_unregister_all_users(conn); |
| 1657 | |
Johan Hedberg | e31fb86 | 2014-08-18 20:33:28 +0300 | [diff] [blame] | 1658 | /* Force the connection to be immediately dropped */ |
| 1659 | hcon->disc_timeout = 0; |
| 1660 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1661 | mutex_lock(&conn->chan_lock); |
| 1662 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1663 | /* Kill channels */ |
| 1664 | list_for_each_entry_safe(chan, l, &conn->chan_l, list) { |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1665 | l2cap_chan_hold(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1666 | l2cap_chan_lock(chan); |
| 1667 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1668 | l2cap_chan_del(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1669 | |
| 1670 | l2cap_chan_unlock(chan); |
| 1671 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 1672 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1673 | l2cap_chan_put(chan); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1674 | } |
| 1675 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1676 | mutex_unlock(&conn->chan_lock); |
| 1677 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1678 | hci_chan_del(conn->hchan); |
| 1679 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1680 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1681 | cancel_delayed_work_sync(&conn->info_timer); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1682 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1683 | hcon->l2cap_data = NULL; |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1684 | conn->hchan = NULL; |
| 1685 | l2cap_conn_put(conn); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1686 | } |
| 1687 | |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1688 | static void l2cap_conn_free(struct kref *ref) |
| 1689 | { |
| 1690 | struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref); |
| 1691 | |
| 1692 | hci_conn_put(conn->hcon); |
| 1693 | kfree(conn); |
| 1694 | } |
| 1695 | |
Johan Hedberg | 51bb8457 | 2014-08-15 21:06:57 +0300 | [diff] [blame] | 1696 | struct l2cap_conn *l2cap_conn_get(struct l2cap_conn *conn) |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1697 | { |
| 1698 | kref_get(&conn->ref); |
Johan Hedberg | 51bb8457 | 2014-08-15 21:06:57 +0300 | [diff] [blame] | 1699 | return conn; |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1700 | } |
| 1701 | EXPORT_SYMBOL(l2cap_conn_get); |
| 1702 | |
| 1703 | void l2cap_conn_put(struct l2cap_conn *conn) |
| 1704 | { |
| 1705 | kref_put(&conn->ref, l2cap_conn_free); |
| 1706 | } |
| 1707 | EXPORT_SYMBOL(l2cap_conn_put); |
| 1708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | /* ---- Socket interface ---- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1711 | /* Find socket with psm and source / destination bdaddr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | * Returns closest match. |
| 1713 | */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1714 | static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, |
| 1715 | bdaddr_t *src, |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 1716 | bdaddr_t *dst, |
| 1717 | u8 link_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1719 | struct l2cap_chan *c, *c1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1721 | read_lock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1722 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1723 | list_for_each_entry(c, &chan_list, global_l) { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1724 | if (state && c->state != state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | continue; |
| 1726 | |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 1727 | if (link_type == ACL_LINK && c->src_type != BDADDR_BREDR) |
| 1728 | continue; |
| 1729 | |
| 1730 | if (link_type == LE_LINK && c->src_type == BDADDR_BREDR) |
| 1731 | continue; |
| 1732 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1733 | if (c->psm == psm) { |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1734 | int src_match, dst_match; |
| 1735 | int src_any, dst_any; |
| 1736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | /* Exact match. */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1738 | src_match = !bacmp(&c->src, src); |
| 1739 | dst_match = !bacmp(&c->dst, dst); |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1740 | if (src_match && dst_match) { |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 1741 | l2cap_chan_hold(c); |
Johannes Berg | a7567b2 | 2011-06-01 08:29:54 +0200 | [diff] [blame] | 1742 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1743 | return c; |
| 1744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | |
| 1746 | /* Closest match */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1747 | src_any = !bacmp(&c->src, BDADDR_ANY); |
| 1748 | dst_any = !bacmp(&c->dst, BDADDR_ANY); |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1749 | if ((src_match && dst_any) || (src_any && dst_match) || |
| 1750 | (src_any && dst_any)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1751 | c1 = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | } |
| 1753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 1755 | if (c1) |
| 1756 | l2cap_chan_hold(c1); |
| 1757 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1758 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1759 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1760 | return c1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1763 | static void l2cap_monitor_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1764 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1765 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1766 | monitor_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1767 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1768 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1769 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1770 | l2cap_chan_lock(chan); |
| 1771 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1772 | if (!chan->conn) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1773 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1774 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1775 | return; |
| 1776 | } |
| 1777 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1778 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1779 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1780 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1781 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1782 | } |
| 1783 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1784 | static void l2cap_retrans_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1785 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1786 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1787 | retrans_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1788 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1789 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1790 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1791 | l2cap_chan_lock(chan); |
| 1792 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1793 | if (!chan->conn) { |
| 1794 | l2cap_chan_unlock(chan); |
| 1795 | l2cap_chan_put(chan); |
| 1796 | return; |
| 1797 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1798 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1799 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1800 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1801 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1802 | } |
| 1803 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 1804 | static void l2cap_streaming_send(struct l2cap_chan *chan, |
| 1805 | struct sk_buff_head *skbs) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1806 | { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1807 | struct sk_buff *skb; |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1808 | struct l2cap_ctrl *control; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1809 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1810 | BT_DBG("chan %p, skbs %p", chan, skbs); |
| 1811 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1812 | if (__chan_is_moving(chan)) |
| 1813 | return; |
| 1814 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1815 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 1816 | |
| 1817 | while (!skb_queue_empty(&chan->tx_q)) { |
| 1818 | |
| 1819 | skb = skb_dequeue(&chan->tx_q); |
| 1820 | |
| 1821 | bt_cb(skb)->control.retries = 1; |
| 1822 | control = &bt_cb(skb)->control; |
| 1823 | |
| 1824 | control->reqseq = 0; |
| 1825 | control->txseq = chan->next_tx_seq; |
| 1826 | |
| 1827 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1828 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1829 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1830 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 1831 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1832 | } |
| 1833 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1834 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1835 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1836 | BT_DBG("Sent txseq %u", control->txseq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1837 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1838 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1839 | chan->frames_sent++; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1840 | } |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1841 | } |
| 1842 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1843 | static int l2cap_ertm_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1844 | { |
| 1845 | struct sk_buff *skb, *tx_skb; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1846 | struct l2cap_ctrl *control; |
| 1847 | int sent = 0; |
| 1848 | |
| 1849 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1850 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1851 | if (chan->state != BT_CONNECTED) |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1852 | return -ENOTCONN; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1853 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 1854 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 1855 | return 0; |
| 1856 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1857 | if (__chan_is_moving(chan)) |
| 1858 | return 0; |
| 1859 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1860 | while (chan->tx_send_head && |
| 1861 | chan->unacked_frames < chan->remote_tx_win && |
| 1862 | chan->tx_state == L2CAP_TX_STATE_XMIT) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1863 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1864 | skb = chan->tx_send_head; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1865 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1866 | bt_cb(skb)->control.retries = 1; |
| 1867 | control = &bt_cb(skb)->control; |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1868 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1869 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1870 | control->final = 1; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1871 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1872 | control->reqseq = chan->buffer_seq; |
| 1873 | chan->last_acked_seq = chan->buffer_seq; |
| 1874 | control->txseq = chan->next_tx_seq; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1875 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1876 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1877 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1878 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1879 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 1880 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1881 | } |
| 1882 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1883 | /* Clone after data has been modified. Data is assumed to be |
| 1884 | read-only (for locking purposes) on cloned sk_buffs. |
| 1885 | */ |
| 1886 | tx_skb = skb_clone(skb, GFP_KERNEL); |
| 1887 | |
| 1888 | if (!tx_skb) |
| 1889 | break; |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1890 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1891 | __set_retrans_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1892 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1893 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1894 | chan->unacked_frames++; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1895 | chan->frames_sent++; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1896 | sent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1897 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1898 | if (skb_queue_is_last(&chan->tx_q, skb)) |
| 1899 | chan->tx_send_head = NULL; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1900 | else |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1901 | chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1902 | |
| 1903 | l2cap_do_send(chan, tx_skb); |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1904 | BT_DBG("Sent txseq %u", control->txseq); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1905 | } |
| 1906 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1907 | BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent, |
| 1908 | chan->unacked_frames, skb_queue_len(&chan->tx_q)); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1909 | |
| 1910 | return sent; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1911 | } |
| 1912 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1913 | static void l2cap_ertm_resend(struct l2cap_chan *chan) |
| 1914 | { |
| 1915 | struct l2cap_ctrl control; |
| 1916 | struct sk_buff *skb; |
| 1917 | struct sk_buff *tx_skb; |
| 1918 | u16 seq; |
| 1919 | |
| 1920 | BT_DBG("chan %p", chan); |
| 1921 | |
| 1922 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 1923 | return; |
| 1924 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1925 | if (__chan_is_moving(chan)) |
| 1926 | return; |
| 1927 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1928 | while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) { |
| 1929 | seq = l2cap_seq_list_pop(&chan->retrans_list); |
| 1930 | |
| 1931 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq); |
| 1932 | if (!skb) { |
| 1933 | BT_DBG("Error: Can't retransmit seq %d, frame missing", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1934 | seq); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1935 | continue; |
| 1936 | } |
| 1937 | |
| 1938 | bt_cb(skb)->control.retries++; |
| 1939 | control = bt_cb(skb)->control; |
| 1940 | |
| 1941 | if (chan->max_tx != 0 && |
| 1942 | bt_cb(skb)->control.retries > chan->max_tx) { |
| 1943 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1944 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1945 | l2cap_seq_list_clear(&chan->retrans_list); |
| 1946 | break; |
| 1947 | } |
| 1948 | |
| 1949 | control.reqseq = chan->buffer_seq; |
| 1950 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
| 1951 | control.final = 1; |
| 1952 | else |
| 1953 | control.final = 0; |
| 1954 | |
| 1955 | if (skb_cloned(skb)) { |
| 1956 | /* Cloned sk_buffs are read-only, so we need a |
| 1957 | * writeable copy |
| 1958 | */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 1959 | tx_skb = skb_copy(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1960 | } else { |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 1961 | tx_skb = skb_clone(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | if (!tx_skb) { |
| 1965 | l2cap_seq_list_clear(&chan->retrans_list); |
| 1966 | break; |
| 1967 | } |
| 1968 | |
| 1969 | /* Update skb contents */ |
| 1970 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 1971 | put_unaligned_le32(__pack_extended_control(&control), |
| 1972 | tx_skb->data + L2CAP_HDR_SIZE); |
| 1973 | } else { |
| 1974 | put_unaligned_le16(__pack_enhanced_control(&control), |
| 1975 | tx_skb->data + L2CAP_HDR_SIZE); |
| 1976 | } |
| 1977 | |
Lukasz Rymanowski | 13cac15 | 2014-08-14 09:35:34 +0200 | [diff] [blame] | 1978 | /* Update FCS */ |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1979 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Lukasz Rymanowski | 13cac15 | 2014-08-14 09:35:34 +0200 | [diff] [blame] | 1980 | u16 fcs = crc16(0, (u8 *) tx_skb->data, |
| 1981 | tx_skb->len - L2CAP_FCS_SIZE); |
| 1982 | put_unaligned_le16(fcs, skb_tail_pointer(tx_skb) - |
| 1983 | L2CAP_FCS_SIZE); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | l2cap_do_send(chan, tx_skb); |
| 1987 | |
| 1988 | BT_DBG("Resent txseq %d", control.txseq); |
| 1989 | |
| 1990 | chan->last_acked_seq = chan->buffer_seq; |
| 1991 | } |
| 1992 | } |
| 1993 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 1994 | static void l2cap_retransmit(struct l2cap_chan *chan, |
| 1995 | struct l2cap_ctrl *control) |
| 1996 | { |
| 1997 | BT_DBG("chan %p, control %p", chan, control); |
| 1998 | |
| 1999 | l2cap_seq_list_append(&chan->retrans_list, control->reqseq); |
| 2000 | l2cap_ertm_resend(chan); |
| 2001 | } |
| 2002 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2003 | static void l2cap_retransmit_all(struct l2cap_chan *chan, |
| 2004 | struct l2cap_ctrl *control) |
| 2005 | { |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2006 | struct sk_buff *skb; |
| 2007 | |
| 2008 | BT_DBG("chan %p, control %p", chan, control); |
| 2009 | |
| 2010 | if (control->poll) |
| 2011 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 2012 | |
| 2013 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2014 | |
| 2015 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 2016 | return; |
| 2017 | |
| 2018 | if (chan->unacked_frames) { |
| 2019 | skb_queue_walk(&chan->tx_q, skb) { |
| 2020 | if (bt_cb(skb)->control.txseq == control->reqseq || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2021 | skb == chan->tx_send_head) |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2022 | break; |
| 2023 | } |
| 2024 | |
| 2025 | skb_queue_walk_from(&chan->tx_q, skb) { |
| 2026 | if (skb == chan->tx_send_head) |
| 2027 | break; |
| 2028 | |
| 2029 | l2cap_seq_list_append(&chan->retrans_list, |
| 2030 | bt_cb(skb)->control.txseq); |
| 2031 | } |
| 2032 | |
| 2033 | l2cap_ertm_resend(chan); |
| 2034 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2035 | } |
| 2036 | |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2037 | static void l2cap_send_ack(struct l2cap_chan *chan) |
| 2038 | { |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2039 | struct l2cap_ctrl control; |
| 2040 | u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 2041 | chan->last_acked_seq); |
| 2042 | int threshold; |
| 2043 | |
| 2044 | BT_DBG("chan %p last_acked_seq %d buffer_seq %d", |
| 2045 | chan, chan->last_acked_seq, chan->buffer_seq); |
| 2046 | |
| 2047 | memset(&control, 0, sizeof(control)); |
| 2048 | control.sframe = 1; |
| 2049 | |
| 2050 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
| 2051 | chan->rx_state == L2CAP_RX_STATE_RECV) { |
| 2052 | __clear_ack_timer(chan); |
| 2053 | control.super = L2CAP_SUPER_RNR; |
| 2054 | control.reqseq = chan->buffer_seq; |
| 2055 | l2cap_send_sframe(chan, &control); |
| 2056 | } else { |
| 2057 | if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) { |
| 2058 | l2cap_ertm_send(chan); |
| 2059 | /* If any i-frames were sent, they included an ack */ |
| 2060 | if (chan->buffer_seq == chan->last_acked_seq) |
| 2061 | frames_to_ack = 0; |
| 2062 | } |
| 2063 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2064 | /* Ack now if the window is 3/4ths full. |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2065 | * Calculate without mul or div |
| 2066 | */ |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2067 | threshold = chan->ack_win; |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2068 | threshold += threshold << 1; |
| 2069 | threshold >>= 2; |
| 2070 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2071 | BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack, |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2072 | threshold); |
| 2073 | |
| 2074 | if (frames_to_ack >= threshold) { |
| 2075 | __clear_ack_timer(chan); |
| 2076 | control.super = L2CAP_SUPER_RR; |
| 2077 | control.reqseq = chan->buffer_seq; |
| 2078 | l2cap_send_sframe(chan, &control); |
| 2079 | frames_to_ack = 0; |
| 2080 | } |
| 2081 | |
| 2082 | if (frames_to_ack) |
| 2083 | __set_ack_timer(chan); |
| 2084 | } |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2085 | } |
| 2086 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2087 | static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, |
| 2088 | struct msghdr *msg, int len, |
| 2089 | int count, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | { |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2091 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2092 | struct sk_buff **frag; |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2093 | int sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | |
Jukka Rissanen | 0498878 | 2014-06-18 16:37:07 +0300 | [diff] [blame] | 2095 | if (chan->ops->memcpy_fromiovec(chan, skb_put(skb, count), |
| 2096 | msg->msg_iov, count)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2097 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | |
| 2099 | sent += count; |
| 2100 | len -= count; |
| 2101 | |
| 2102 | /* Continuation fragments (no L2CAP header) */ |
| 2103 | frag = &skb_shinfo(skb)->frag_list; |
| 2104 | while (len) { |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2105 | struct sk_buff *tmp; |
| 2106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | count = min_t(unsigned int, conn->mtu, len); |
| 2108 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2109 | tmp = chan->ops->alloc_skb(chan, 0, count, |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2110 | msg->msg_flags & MSG_DONTWAIT); |
| 2111 | if (IS_ERR(tmp)) |
| 2112 | return PTR_ERR(tmp); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2113 | |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2114 | *frag = tmp; |
| 2115 | |
Jukka Rissanen | 0498878 | 2014-06-18 16:37:07 +0300 | [diff] [blame] | 2116 | if (chan->ops->memcpy_fromiovec(chan, skb_put(*frag, count), |
| 2117 | msg->msg_iov, count)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2118 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | |
| 2120 | sent += count; |
| 2121 | len -= count; |
| 2122 | |
Gustavo Padovan | 2d0ed3d | 2012-05-11 13:16:12 -0300 | [diff] [blame] | 2123 | skb->len += (*frag)->len; |
| 2124 | skb->data_len += (*frag)->len; |
| 2125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | frag = &(*frag)->next; |
| 2127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | |
| 2129 | return sent; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2132 | static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2133 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2134 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2135 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2136 | struct sk_buff *skb; |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2137 | int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2138 | struct l2cap_hdr *lh; |
| 2139 | |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2140 | BT_DBG("chan %p psm 0x%2.2x len %zu", chan, |
| 2141 | __le16_to_cpu(chan->psm), len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2142 | |
| 2143 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2144 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2145 | skb = chan->ops->alloc_skb(chan, hlen, count, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2146 | msg->msg_flags & MSG_DONTWAIT); |
| 2147 | if (IS_ERR(skb)) |
| 2148 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2149 | |
| 2150 | /* Create L2CAP header */ |
| 2151 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2152 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | daf6a78c | 2012-05-03 10:55:52 +0300 | [diff] [blame] | 2153 | lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE); |
Marcel Holtmann | 43b1b8d | 2013-10-12 06:01:26 -0700 | [diff] [blame] | 2154 | put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2155 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2156 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2157 | if (unlikely(err < 0)) { |
| 2158 | kfree_skb(skb); |
| 2159 | return ERR_PTR(err); |
| 2160 | } |
| 2161 | return skb; |
| 2162 | } |
| 2163 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2164 | static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2165 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2166 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2167 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2168 | struct sk_buff *skb; |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2169 | int err, count; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2170 | struct l2cap_hdr *lh; |
| 2171 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2172 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2173 | |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2174 | count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2175 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2176 | skb = chan->ops->alloc_skb(chan, L2CAP_HDR_SIZE, count, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2177 | msg->msg_flags & MSG_DONTWAIT); |
| 2178 | if (IS_ERR(skb)) |
| 2179 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2180 | |
| 2181 | /* Create L2CAP header */ |
| 2182 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2183 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo Padovan | 6ff9b5e | 2012-05-02 11:56:17 -0300 | [diff] [blame] | 2184 | lh->len = cpu_to_le16(len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2185 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2186 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2187 | if (unlikely(err < 0)) { |
| 2188 | kfree_skb(skb); |
| 2189 | return ERR_PTR(err); |
| 2190 | } |
| 2191 | return skb; |
| 2192 | } |
| 2193 | |
Luiz Augusto von Dentz | ab0ff76 | 2011-09-12 20:00:50 +0300 | [diff] [blame] | 2194 | static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2195 | struct msghdr *msg, size_t len, |
| 2196 | u16 sdulen) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2197 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2198 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2199 | struct sk_buff *skb; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2200 | int err, count, hlen; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2201 | struct l2cap_hdr *lh; |
| 2202 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2203 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2204 | |
Gustavo F. Padovan | 0ee0d20 | 2010-05-01 16:15:41 -0300 | [diff] [blame] | 2205 | if (!conn) |
| 2206 | return ERR_PTR(-ENOTCONN); |
| 2207 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2208 | hlen = __ertm_hdr_size(chan); |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2209 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2210 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2211 | hlen += L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2212 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2213 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2214 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2215 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2216 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2217 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2218 | skb = chan->ops->alloc_skb(chan, hlen, count, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2219 | msg->msg_flags & MSG_DONTWAIT); |
| 2220 | if (IS_ERR(skb)) |
| 2221 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2222 | |
| 2223 | /* Create L2CAP header */ |
| 2224 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2225 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2226 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2227 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2228 | /* Control header is populated later */ |
| 2229 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 2230 | put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 2231 | else |
| 2232 | put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2233 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2234 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2235 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2236 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2237 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2238 | if (unlikely(err < 0)) { |
| 2239 | kfree_skb(skb); |
| 2240 | return ERR_PTR(err); |
| 2241 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2242 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2243 | bt_cb(skb)->control.fcs = chan->fcs; |
Mat Martineau | 3ce3514 | 2012-04-25 16:36:14 -0700 | [diff] [blame] | 2244 | bt_cb(skb)->control.retries = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2245 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | } |
| 2247 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2248 | static int l2cap_segment_sdu(struct l2cap_chan *chan, |
| 2249 | struct sk_buff_head *seg_queue, |
| 2250 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2251 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2252 | struct sk_buff *skb; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2253 | u16 sdu_len; |
| 2254 | size_t pdu_len; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2255 | u8 sar; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2256 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2257 | BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2258 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2259 | /* It is critical that ERTM PDUs fit in a single HCI fragment, |
| 2260 | * so fragmented skbs are not used. The HCI layer's handling |
| 2261 | * of fragmented skbs is not compatible with ERTM's queueing. |
| 2262 | */ |
| 2263 | |
| 2264 | /* PDU size is derived from the HCI MTU */ |
| 2265 | pdu_len = chan->conn->mtu; |
| 2266 | |
Mat Martineau | a549574 | 2012-10-23 15:24:21 -0700 | [diff] [blame] | 2267 | /* Constrain PDU size for BR/EDR connections */ |
| 2268 | if (!chan->hs_hcon) |
| 2269 | pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2270 | |
| 2271 | /* Adjust for largest possible L2CAP overhead. */ |
Gustavo Padovan | 35d401d | 2012-05-25 18:57:05 -0300 | [diff] [blame] | 2272 | if (chan->fcs) |
| 2273 | pdu_len -= L2CAP_FCS_SIZE; |
| 2274 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2275 | pdu_len -= __ertm_hdr_size(chan); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2276 | |
| 2277 | /* Remote device may have requested smaller PDUs */ |
| 2278 | pdu_len = min_t(size_t, pdu_len, chan->remote_mps); |
| 2279 | |
| 2280 | if (len <= pdu_len) { |
| 2281 | sar = L2CAP_SAR_UNSEGMENTED; |
| 2282 | sdu_len = 0; |
| 2283 | pdu_len = len; |
| 2284 | } else { |
| 2285 | sar = L2CAP_SAR_START; |
| 2286 | sdu_len = len; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2287 | } |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2288 | |
| 2289 | while (len > 0) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2290 | skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2291 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2292 | if (IS_ERR(skb)) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2293 | __skb_queue_purge(seg_queue); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2294 | return PTR_ERR(skb); |
| 2295 | } |
| 2296 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2297 | bt_cb(skb)->control.sar = sar; |
| 2298 | __skb_queue_tail(seg_queue, skb); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2299 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2300 | len -= pdu_len; |
Lukasz Rymanowski | 069cb27 | 2014-08-13 16:01:41 +0200 | [diff] [blame] | 2301 | if (sdu_len) |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2302 | sdu_len = 0; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2303 | |
| 2304 | if (len <= pdu_len) { |
| 2305 | sar = L2CAP_SAR_END; |
| 2306 | pdu_len = len; |
| 2307 | } else { |
| 2308 | sar = L2CAP_SAR_CONTINUE; |
| 2309 | } |
| 2310 | } |
| 2311 | |
Gustavo Padovan | f0f6279 | 2012-05-29 13:29:17 -0300 | [diff] [blame] | 2312 | return 0; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2313 | } |
| 2314 | |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2315 | static struct sk_buff *l2cap_create_le_flowctl_pdu(struct l2cap_chan *chan, |
| 2316 | struct msghdr *msg, |
| 2317 | size_t len, u16 sdulen) |
| 2318 | { |
| 2319 | struct l2cap_conn *conn = chan->conn; |
| 2320 | struct sk_buff *skb; |
| 2321 | int err, count, hlen; |
| 2322 | struct l2cap_hdr *lh; |
| 2323 | |
| 2324 | BT_DBG("chan %p len %zu", chan, len); |
| 2325 | |
| 2326 | if (!conn) |
| 2327 | return ERR_PTR(-ENOTCONN); |
| 2328 | |
| 2329 | hlen = L2CAP_HDR_SIZE; |
| 2330 | |
| 2331 | if (sdulen) |
| 2332 | hlen += L2CAP_SDULEN_SIZE; |
| 2333 | |
| 2334 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
| 2335 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2336 | skb = chan->ops->alloc_skb(chan, hlen, count, |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2337 | msg->msg_flags & MSG_DONTWAIT); |
| 2338 | if (IS_ERR(skb)) |
| 2339 | return skb; |
| 2340 | |
| 2341 | /* Create L2CAP header */ |
| 2342 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
| 2343 | lh->cid = cpu_to_le16(chan->dcid); |
| 2344 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
| 2345 | |
| 2346 | if (sdulen) |
| 2347 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
| 2348 | |
| 2349 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
| 2350 | if (unlikely(err < 0)) { |
| 2351 | kfree_skb(skb); |
| 2352 | return ERR_PTR(err); |
| 2353 | } |
| 2354 | |
| 2355 | return skb; |
| 2356 | } |
| 2357 | |
| 2358 | static int l2cap_segment_le_sdu(struct l2cap_chan *chan, |
| 2359 | struct sk_buff_head *seg_queue, |
| 2360 | struct msghdr *msg, size_t len) |
| 2361 | { |
| 2362 | struct sk_buff *skb; |
| 2363 | size_t pdu_len; |
| 2364 | u16 sdu_len; |
| 2365 | |
| 2366 | BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); |
| 2367 | |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2368 | sdu_len = len; |
Johan Hedberg | 72c6fb9 | 2014-08-15 21:06:51 +0300 | [diff] [blame] | 2369 | pdu_len = chan->remote_mps - L2CAP_SDULEN_SIZE; |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2370 | |
| 2371 | while (len > 0) { |
| 2372 | if (len <= pdu_len) |
| 2373 | pdu_len = len; |
| 2374 | |
| 2375 | skb = l2cap_create_le_flowctl_pdu(chan, msg, pdu_len, sdu_len); |
| 2376 | if (IS_ERR(skb)) { |
| 2377 | __skb_queue_purge(seg_queue); |
| 2378 | return PTR_ERR(skb); |
| 2379 | } |
| 2380 | |
| 2381 | __skb_queue_tail(seg_queue, skb); |
| 2382 | |
| 2383 | len -= pdu_len; |
| 2384 | |
| 2385 | if (sdu_len) { |
| 2386 | sdu_len = 0; |
| 2387 | pdu_len += L2CAP_SDULEN_SIZE; |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | return 0; |
| 2392 | } |
| 2393 | |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2394 | 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] | 2395 | { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2396 | struct sk_buff *skb; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2397 | int err; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2398 | struct sk_buff_head seg_queue; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2399 | |
Seung-Woo Kim | 31e8ce8 | 2013-11-05 18:46:33 +0900 | [diff] [blame] | 2400 | if (!chan->conn) |
| 2401 | return -ENOTCONN; |
| 2402 | |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2403 | /* Connectionless channel */ |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2404 | if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2405 | skb = l2cap_create_connless_pdu(chan, msg, len); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2406 | if (IS_ERR(skb)) |
| 2407 | return PTR_ERR(skb); |
| 2408 | |
Andrzej Kaczmarek | ede81a2 | 2014-02-25 17:16:22 +0100 | [diff] [blame] | 2409 | /* Channel lock is released before requesting new skb and then |
| 2410 | * reacquired thus we need to recheck channel state. |
| 2411 | */ |
| 2412 | if (chan->state != BT_CONNECTED) { |
| 2413 | kfree_skb(skb); |
| 2414 | return -ENOTCONN; |
| 2415 | } |
| 2416 | |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2417 | l2cap_do_send(chan, skb); |
| 2418 | return len; |
| 2419 | } |
| 2420 | |
| 2421 | switch (chan->mode) { |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 2422 | case L2CAP_MODE_LE_FLOWCTL: |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2423 | /* Check outgoing MTU */ |
| 2424 | if (len > chan->omtu) |
| 2425 | return -EMSGSIZE; |
| 2426 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 2427 | if (!chan->tx_credits) |
| 2428 | return -EAGAIN; |
| 2429 | |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2430 | __skb_queue_head_init(&seg_queue); |
| 2431 | |
| 2432 | err = l2cap_segment_le_sdu(chan, &seg_queue, msg, len); |
| 2433 | |
| 2434 | if (chan->state != BT_CONNECTED) { |
| 2435 | __skb_queue_purge(&seg_queue); |
| 2436 | err = -ENOTCONN; |
| 2437 | } |
| 2438 | |
| 2439 | if (err) |
| 2440 | return err; |
| 2441 | |
| 2442 | skb_queue_splice_tail_init(&seg_queue, &chan->tx_q); |
| 2443 | |
| 2444 | while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { |
| 2445 | l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); |
| 2446 | chan->tx_credits--; |
| 2447 | } |
| 2448 | |
| 2449 | if (!chan->tx_credits) |
| 2450 | chan->ops->suspend(chan); |
| 2451 | |
| 2452 | err = len; |
| 2453 | |
| 2454 | break; |
| 2455 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 2456 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2457 | /* Check outgoing MTU */ |
| 2458 | if (len > chan->omtu) |
| 2459 | return -EMSGSIZE; |
| 2460 | |
| 2461 | /* Create a basic PDU */ |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2462 | skb = l2cap_create_basic_pdu(chan, msg, len); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2463 | if (IS_ERR(skb)) |
| 2464 | return PTR_ERR(skb); |
| 2465 | |
Andrzej Kaczmarek | ede81a2 | 2014-02-25 17:16:22 +0100 | [diff] [blame] | 2466 | /* Channel lock is released before requesting new skb and then |
| 2467 | * reacquired thus we need to recheck channel state. |
| 2468 | */ |
| 2469 | if (chan->state != BT_CONNECTED) { |
| 2470 | kfree_skb(skb); |
| 2471 | return -ENOTCONN; |
| 2472 | } |
| 2473 | |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2474 | l2cap_do_send(chan, skb); |
| 2475 | err = len; |
| 2476 | break; |
| 2477 | |
| 2478 | case L2CAP_MODE_ERTM: |
| 2479 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2480 | /* Check outgoing MTU */ |
| 2481 | if (len > chan->omtu) { |
| 2482 | err = -EMSGSIZE; |
| 2483 | break; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2484 | } |
| 2485 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2486 | __skb_queue_head_init(&seg_queue); |
| 2487 | |
| 2488 | /* Do segmentation before calling in to the state machine, |
| 2489 | * since it's possible to block while waiting for memory |
| 2490 | * allocation. |
| 2491 | */ |
| 2492 | err = l2cap_segment_sdu(chan, &seg_queue, msg, len); |
| 2493 | |
| 2494 | /* The channel could have been closed while segmenting, |
| 2495 | * check that it is still connected. |
| 2496 | */ |
| 2497 | if (chan->state != BT_CONNECTED) { |
| 2498 | __skb_queue_purge(&seg_queue); |
| 2499 | err = -ENOTCONN; |
| 2500 | } |
| 2501 | |
| 2502 | if (err) |
| 2503 | break; |
| 2504 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2505 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2506 | l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2507 | else |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2508 | l2cap_streaming_send(chan, &seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2509 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2510 | err = len; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2511 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2512 | /* If the skbs were not queued for sending, they'll still be in |
| 2513 | * seg_queue and need to be purged. |
| 2514 | */ |
| 2515 | __skb_queue_purge(&seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2516 | break; |
| 2517 | |
| 2518 | default: |
| 2519 | BT_DBG("bad state %1.1x", chan->mode); |
| 2520 | err = -EBADFD; |
| 2521 | } |
| 2522 | |
| 2523 | return err; |
| 2524 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 2525 | EXPORT_SYMBOL_GPL(l2cap_chan_send); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2526 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2527 | static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq) |
| 2528 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2529 | struct l2cap_ctrl control; |
| 2530 | u16 seq; |
| 2531 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2532 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2533 | |
| 2534 | memset(&control, 0, sizeof(control)); |
| 2535 | control.sframe = 1; |
| 2536 | control.super = L2CAP_SUPER_SREJ; |
| 2537 | |
| 2538 | for (seq = chan->expected_tx_seq; seq != txseq; |
| 2539 | seq = __next_seq(chan, seq)) { |
| 2540 | if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) { |
| 2541 | control.reqseq = seq; |
| 2542 | l2cap_send_sframe(chan, &control); |
| 2543 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2544 | } |
| 2545 | } |
| 2546 | |
| 2547 | chan->expected_tx_seq = __next_seq(chan, txseq); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2548 | } |
| 2549 | |
| 2550 | static void l2cap_send_srej_tail(struct l2cap_chan *chan) |
| 2551 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2552 | struct l2cap_ctrl control; |
| 2553 | |
| 2554 | BT_DBG("chan %p", chan); |
| 2555 | |
| 2556 | if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR) |
| 2557 | return; |
| 2558 | |
| 2559 | memset(&control, 0, sizeof(control)); |
| 2560 | control.sframe = 1; |
| 2561 | control.super = L2CAP_SUPER_SREJ; |
| 2562 | control.reqseq = chan->srej_list.tail; |
| 2563 | l2cap_send_sframe(chan, &control); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2564 | } |
| 2565 | |
| 2566 | static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq) |
| 2567 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2568 | struct l2cap_ctrl control; |
| 2569 | u16 initial_head; |
| 2570 | u16 seq; |
| 2571 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2572 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2573 | |
| 2574 | memset(&control, 0, sizeof(control)); |
| 2575 | control.sframe = 1; |
| 2576 | control.super = L2CAP_SUPER_SREJ; |
| 2577 | |
| 2578 | /* Capture initial list head to allow only one pass through the list. */ |
| 2579 | initial_head = chan->srej_list.head; |
| 2580 | |
| 2581 | do { |
| 2582 | seq = l2cap_seq_list_pop(&chan->srej_list); |
| 2583 | if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR) |
| 2584 | break; |
| 2585 | |
| 2586 | control.reqseq = seq; |
| 2587 | l2cap_send_sframe(chan, &control); |
| 2588 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2589 | } while (chan->srej_list.head != initial_head); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2590 | } |
| 2591 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2592 | static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 2593 | { |
| 2594 | struct sk_buff *acked_skb; |
| 2595 | u16 ackseq; |
| 2596 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2597 | BT_DBG("chan %p, reqseq %u", chan, reqseq); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2598 | |
| 2599 | if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq) |
| 2600 | return; |
| 2601 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2602 | BT_DBG("expected_ack_seq %u, unacked_frames %u", |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2603 | chan->expected_ack_seq, chan->unacked_frames); |
| 2604 | |
| 2605 | for (ackseq = chan->expected_ack_seq; ackseq != reqseq; |
| 2606 | ackseq = __next_seq(chan, ackseq)) { |
| 2607 | |
| 2608 | acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq); |
| 2609 | if (acked_skb) { |
| 2610 | skb_unlink(acked_skb, &chan->tx_q); |
| 2611 | kfree_skb(acked_skb); |
| 2612 | chan->unacked_frames--; |
| 2613 | } |
| 2614 | } |
| 2615 | |
| 2616 | chan->expected_ack_seq = reqseq; |
| 2617 | |
| 2618 | if (chan->unacked_frames == 0) |
| 2619 | __clear_retrans_timer(chan); |
| 2620 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2621 | BT_DBG("unacked_frames %u", chan->unacked_frames); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan) |
| 2625 | { |
| 2626 | BT_DBG("chan %p", chan); |
| 2627 | |
| 2628 | chan->expected_tx_seq = chan->buffer_seq; |
| 2629 | l2cap_seq_list_clear(&chan->srej_list); |
| 2630 | skb_queue_purge(&chan->srej_q); |
| 2631 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 2632 | } |
| 2633 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2634 | static void l2cap_tx_state_xmit(struct l2cap_chan *chan, |
| 2635 | struct l2cap_ctrl *control, |
| 2636 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2637 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2638 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2639 | event); |
| 2640 | |
| 2641 | switch (event) { |
| 2642 | case L2CAP_EV_DATA_REQUEST: |
| 2643 | if (chan->tx_send_head == NULL) |
| 2644 | chan->tx_send_head = skb_peek(skbs); |
| 2645 | |
| 2646 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2647 | l2cap_ertm_send(chan); |
| 2648 | break; |
| 2649 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2650 | BT_DBG("Enter LOCAL_BUSY"); |
| 2651 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2652 | |
| 2653 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2654 | /* The SREJ_SENT state must be aborted if we are to |
| 2655 | * enter the LOCAL_BUSY state. |
| 2656 | */ |
| 2657 | l2cap_abort_rx_srej_sent(chan); |
| 2658 | } |
| 2659 | |
| 2660 | l2cap_send_ack(chan); |
| 2661 | |
| 2662 | break; |
| 2663 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2664 | BT_DBG("Exit LOCAL_BUSY"); |
| 2665 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2666 | |
| 2667 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2668 | struct l2cap_ctrl local_control; |
| 2669 | |
| 2670 | memset(&local_control, 0, sizeof(local_control)); |
| 2671 | local_control.sframe = 1; |
| 2672 | local_control.super = L2CAP_SUPER_RR; |
| 2673 | local_control.poll = 1; |
| 2674 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2675 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2676 | |
| 2677 | chan->retry_count = 1; |
| 2678 | __set_monitor_timer(chan); |
| 2679 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2680 | } |
| 2681 | break; |
| 2682 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2683 | l2cap_process_reqseq(chan, control->reqseq); |
| 2684 | break; |
| 2685 | case L2CAP_EV_EXPLICIT_POLL: |
| 2686 | l2cap_send_rr_or_rnr(chan, 1); |
| 2687 | chan->retry_count = 1; |
| 2688 | __set_monitor_timer(chan); |
| 2689 | __clear_ack_timer(chan); |
| 2690 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2691 | break; |
| 2692 | case L2CAP_EV_RETRANS_TO: |
| 2693 | l2cap_send_rr_or_rnr(chan, 1); |
| 2694 | chan->retry_count = 1; |
| 2695 | __set_monitor_timer(chan); |
| 2696 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2697 | break; |
| 2698 | case L2CAP_EV_RECV_FBIT: |
| 2699 | /* Nothing to process */ |
| 2700 | break; |
| 2701 | default: |
| 2702 | break; |
| 2703 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2704 | } |
| 2705 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2706 | static void l2cap_tx_state_wait_f(struct l2cap_chan *chan, |
| 2707 | struct l2cap_ctrl *control, |
| 2708 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2709 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2710 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2711 | event); |
| 2712 | |
| 2713 | switch (event) { |
| 2714 | case L2CAP_EV_DATA_REQUEST: |
| 2715 | if (chan->tx_send_head == NULL) |
| 2716 | chan->tx_send_head = skb_peek(skbs); |
| 2717 | /* Queue data, but don't send. */ |
| 2718 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2719 | break; |
| 2720 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2721 | BT_DBG("Enter LOCAL_BUSY"); |
| 2722 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2723 | |
| 2724 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2725 | /* The SREJ_SENT state must be aborted if we are to |
| 2726 | * enter the LOCAL_BUSY state. |
| 2727 | */ |
| 2728 | l2cap_abort_rx_srej_sent(chan); |
| 2729 | } |
| 2730 | |
| 2731 | l2cap_send_ack(chan); |
| 2732 | |
| 2733 | break; |
| 2734 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2735 | BT_DBG("Exit LOCAL_BUSY"); |
| 2736 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2737 | |
| 2738 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2739 | struct l2cap_ctrl local_control; |
| 2740 | memset(&local_control, 0, sizeof(local_control)); |
| 2741 | local_control.sframe = 1; |
| 2742 | local_control.super = L2CAP_SUPER_RR; |
| 2743 | local_control.poll = 1; |
| 2744 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2745 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2746 | |
| 2747 | chan->retry_count = 1; |
| 2748 | __set_monitor_timer(chan); |
| 2749 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2750 | } |
| 2751 | break; |
| 2752 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2753 | l2cap_process_reqseq(chan, control->reqseq); |
| 2754 | |
| 2755 | /* Fall through */ |
| 2756 | |
| 2757 | case L2CAP_EV_RECV_FBIT: |
| 2758 | if (control && control->final) { |
| 2759 | __clear_monitor_timer(chan); |
| 2760 | if (chan->unacked_frames > 0) |
| 2761 | __set_retrans_timer(chan); |
| 2762 | chan->retry_count = 0; |
| 2763 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 2764 | BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state); |
| 2765 | } |
| 2766 | break; |
| 2767 | case L2CAP_EV_EXPLICIT_POLL: |
| 2768 | /* Ignore */ |
| 2769 | break; |
| 2770 | case L2CAP_EV_MONITOR_TO: |
| 2771 | if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) { |
| 2772 | l2cap_send_rr_or_rnr(chan, 1); |
| 2773 | __set_monitor_timer(chan); |
| 2774 | chan->retry_count++; |
| 2775 | } else { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 2776 | l2cap_send_disconn_req(chan, ECONNABORTED); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2777 | } |
| 2778 | break; |
| 2779 | default: |
| 2780 | break; |
| 2781 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2782 | } |
| 2783 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2784 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 2785 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2786 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2787 | BT_DBG("chan %p, control %p, skbs %p, event %d, state %d", |
| 2788 | chan, control, skbs, event, chan->tx_state); |
| 2789 | |
| 2790 | switch (chan->tx_state) { |
| 2791 | case L2CAP_TX_STATE_XMIT: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2792 | l2cap_tx_state_xmit(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2793 | break; |
| 2794 | case L2CAP_TX_STATE_WAIT_F: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2795 | l2cap_tx_state_wait_f(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2796 | break; |
| 2797 | default: |
| 2798 | /* Ignore event */ |
| 2799 | break; |
| 2800 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2803 | static void l2cap_pass_to_tx(struct l2cap_chan *chan, |
| 2804 | struct l2cap_ctrl *control) |
| 2805 | { |
| 2806 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2807 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT); |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2808 | } |
| 2809 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2810 | static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan, |
| 2811 | struct l2cap_ctrl *control) |
| 2812 | { |
| 2813 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2814 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2815 | } |
| 2816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | /* Copy frame to all raw sockets on that connection */ |
| 2818 | static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) |
| 2819 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | struct sk_buff *nskb; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2821 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | |
| 2823 | BT_DBG("conn %p", conn); |
| 2824 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2825 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2826 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2827 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2828 | if (chan->chan_type != L2CAP_CHAN_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | continue; |
| 2830 | |
Gustavo Padovan | 7f5396a | 2013-10-21 18:22:25 -0200 | [diff] [blame] | 2831 | /* Don't send frame to the channel it came from */ |
| 2832 | if (bt_cb(skb)->chan == chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | continue; |
Gustavo Padovan | 7f5396a | 2013-10-21 18:22:25 -0200 | [diff] [blame] | 2834 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2835 | nskb = skb_clone(skb, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 2836 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | continue; |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 2838 | if (chan->ops->recv(chan, nskb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | kfree_skb(nskb); |
| 2840 | } |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2841 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2842 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | /* ---- L2CAP signalling commands ---- */ |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2846 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code, |
| 2847 | u8 ident, u16 dlen, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | { |
| 2849 | struct sk_buff *skb, **frag; |
| 2850 | struct l2cap_cmd_hdr *cmd; |
| 2851 | struct l2cap_hdr *lh; |
| 2852 | int len, count; |
| 2853 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2854 | BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u", |
| 2855 | conn, code, ident, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | |
Anderson Lizardo | 300b962 | 2013-06-02 16:30:40 -0400 | [diff] [blame] | 2857 | if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE) |
| 2858 | return NULL; |
| 2859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; |
| 2861 | count = min_t(unsigned int, conn->mtu, len); |
| 2862 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2863 | skb = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | if (!skb) |
| 2865 | return NULL; |
| 2866 | |
| 2867 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2868 | lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2869 | |
| 2870 | if (conn->hcon->type == LE_LINK) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 2871 | lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2872 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 2873 | lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2874 | |
| 2875 | cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); |
| 2876 | cmd->code = code; |
| 2877 | cmd->ident = ident; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2878 | cmd->len = cpu_to_le16(dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | |
| 2880 | if (dlen) { |
| 2881 | count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; |
| 2882 | memcpy(skb_put(skb, count), data, count); |
| 2883 | data += count; |
| 2884 | } |
| 2885 | |
| 2886 | len -= skb->len; |
| 2887 | |
| 2888 | /* Continuation fragments (no L2CAP header) */ |
| 2889 | frag = &skb_shinfo(skb)->frag_list; |
| 2890 | while (len) { |
| 2891 | count = min_t(unsigned int, conn->mtu, len); |
| 2892 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2893 | *frag = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | if (!*frag) |
| 2895 | goto fail; |
| 2896 | |
| 2897 | memcpy(skb_put(*frag, count), data, count); |
| 2898 | |
| 2899 | len -= count; |
| 2900 | data += count; |
| 2901 | |
| 2902 | frag = &(*frag)->next; |
| 2903 | } |
| 2904 | |
| 2905 | return skb; |
| 2906 | |
| 2907 | fail: |
| 2908 | kfree_skb(skb); |
| 2909 | return NULL; |
| 2910 | } |
| 2911 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2912 | static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, |
| 2913 | unsigned long *val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2914 | { |
| 2915 | struct l2cap_conf_opt *opt = *ptr; |
| 2916 | int len; |
| 2917 | |
| 2918 | len = L2CAP_CONF_OPT_SIZE + opt->len; |
| 2919 | *ptr += len; |
| 2920 | |
| 2921 | *type = opt->type; |
| 2922 | *olen = opt->len; |
| 2923 | |
| 2924 | switch (opt->len) { |
| 2925 | case 1: |
| 2926 | *val = *((u8 *) opt->val); |
| 2927 | break; |
| 2928 | |
| 2929 | case 2: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2930 | *val = get_unaligned_le16(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | break; |
| 2932 | |
| 2933 | case 4: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2934 | *val = get_unaligned_le32(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | break; |
| 2936 | |
| 2937 | default: |
| 2938 | *val = (unsigned long) opt->val; |
| 2939 | break; |
| 2940 | } |
| 2941 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2942 | 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] | 2943 | return len; |
| 2944 | } |
| 2945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) |
| 2947 | { |
| 2948 | struct l2cap_conf_opt *opt = *ptr; |
| 2949 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2950 | 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] | 2951 | |
| 2952 | opt->type = type; |
| 2953 | opt->len = len; |
| 2954 | |
| 2955 | switch (len) { |
| 2956 | case 1: |
| 2957 | *((u8 *) opt->val) = val; |
| 2958 | break; |
| 2959 | |
| 2960 | case 2: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 2961 | put_unaligned_le16(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2962 | break; |
| 2963 | |
| 2964 | case 4: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 2965 | put_unaligned_le32(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | break; |
| 2967 | |
| 2968 | default: |
| 2969 | memcpy(opt->val, (void *) val, len); |
| 2970 | break; |
| 2971 | } |
| 2972 | |
| 2973 | *ptr += L2CAP_CONF_OPT_SIZE + len; |
| 2974 | } |
| 2975 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2976 | static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) |
| 2977 | { |
| 2978 | struct l2cap_conf_efs efs; |
| 2979 | |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 2980 | switch (chan->mode) { |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2981 | case L2CAP_MODE_ERTM: |
| 2982 | efs.id = chan->local_id; |
| 2983 | efs.stype = chan->local_stype; |
| 2984 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 2985 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 2986 | efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); |
| 2987 | efs.flush_to = cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2988 | break; |
| 2989 | |
| 2990 | case L2CAP_MODE_STREAMING: |
| 2991 | efs.id = 1; |
| 2992 | efs.stype = L2CAP_SERV_BESTEFFORT; |
| 2993 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 2994 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
| 2995 | efs.acc_lat = 0; |
| 2996 | efs.flush_to = 0; |
| 2997 | break; |
| 2998 | |
| 2999 | default: |
| 3000 | return; |
| 3001 | } |
| 3002 | |
| 3003 | l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 3004 | (unsigned long) &efs); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3005 | } |
| 3006 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3007 | static void l2cap_ack_timeout(struct work_struct *work) |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3008 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3009 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3010 | ack_timer.work); |
| 3011 | u16 frames_to_ack; |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3012 | |
Gustavo F. Padovan | 2fb9b3d | 2011-12-22 16:56:05 -0200 | [diff] [blame] | 3013 | BT_DBG("chan %p", chan); |
| 3014 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3015 | l2cap_chan_lock(chan); |
| 3016 | |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3017 | frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 3018 | chan->last_acked_seq); |
| 3019 | |
| 3020 | if (frames_to_ack) |
| 3021 | l2cap_send_rr_or_rnr(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3022 | |
| 3023 | l2cap_chan_unlock(chan); |
Szymon Janc | 09bfb2e | 2012-01-11 10:59:49 +0100 | [diff] [blame] | 3024 | l2cap_chan_put(chan); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3025 | } |
| 3026 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 3027 | int l2cap_ertm_init(struct l2cap_chan *chan) |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3028 | { |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3029 | int err; |
| 3030 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3031 | chan->next_tx_seq = 0; |
| 3032 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3033 | chan->expected_ack_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3034 | chan->unacked_frames = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3035 | chan->buffer_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3036 | chan->frames_sent = 0; |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3037 | chan->last_acked_seq = 0; |
| 3038 | chan->sdu = NULL; |
| 3039 | chan->sdu_last_frag = NULL; |
| 3040 | chan->sdu_len = 0; |
| 3041 | |
Mat Martineau | d34c34f | 2012-05-14 14:49:27 -0700 | [diff] [blame] | 3042 | skb_queue_head_init(&chan->tx_q); |
| 3043 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3044 | chan->local_amp_id = AMP_ID_BREDR; |
| 3045 | chan->move_id = AMP_ID_BREDR; |
Mat Martineau | 0833328 | 2012-10-23 15:24:06 -0700 | [diff] [blame] | 3046 | chan->move_state = L2CAP_MOVE_STABLE; |
| 3047 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 3048 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3049 | if (chan->mode != L2CAP_MODE_ERTM) |
| 3050 | return 0; |
| 3051 | |
| 3052 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 3053 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3054 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3055 | INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); |
| 3056 | INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); |
| 3057 | INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3058 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3059 | skb_queue_head_init(&chan->srej_q); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3060 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3061 | err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win); |
| 3062 | if (err < 0) |
| 3063 | return err; |
| 3064 | |
Mat Martineau | 9dc9aff | 2012-05-17 16:20:14 -0700 | [diff] [blame] | 3065 | err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win); |
| 3066 | if (err < 0) |
| 3067 | l2cap_seq_list_free(&chan->srej_list); |
| 3068 | |
| 3069 | return err; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3070 | } |
| 3071 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3072 | static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) |
| 3073 | { |
| 3074 | switch (mode) { |
| 3075 | case L2CAP_MODE_STREAMING: |
| 3076 | case L2CAP_MODE_ERTM: |
| 3077 | if (l2cap_mode_supported(mode, remote_feat_mask)) |
| 3078 | return mode; |
| 3079 | /* fall through */ |
| 3080 | default: |
| 3081 | return L2CAP_MODE_BASIC; |
| 3082 | } |
| 3083 | } |
| 3084 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3085 | static inline bool __l2cap_ews_supported(struct l2cap_conn *conn) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3086 | { |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3087 | return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3088 | } |
| 3089 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3090 | static inline bool __l2cap_efs_supported(struct l2cap_conn *conn) |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3091 | { |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3092 | return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_FLOW; |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3093 | } |
| 3094 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3095 | static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan, |
| 3096 | struct l2cap_conf_rfc *rfc) |
| 3097 | { |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3098 | if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) { |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3099 | u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to; |
| 3100 | |
| 3101 | /* Class 1 devices have must have ERTM timeouts |
| 3102 | * exceeding the Link Supervision Timeout. The |
| 3103 | * default Link Supervision Timeout for AMP |
| 3104 | * controllers is 10 seconds. |
| 3105 | * |
| 3106 | * Class 1 devices use 0xffffffff for their |
| 3107 | * best-effort flush timeout, so the clamping logic |
| 3108 | * will result in a timeout that meets the above |
| 3109 | * requirement. ERTM timeouts are 16-bit values, so |
| 3110 | * the maximum timeout is 65.535 seconds. |
| 3111 | */ |
| 3112 | |
| 3113 | /* Convert timeout to milliseconds and round */ |
| 3114 | ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000); |
| 3115 | |
| 3116 | /* This is the recommended formula for class 2 devices |
| 3117 | * that start ERTM timers when packets are sent to the |
| 3118 | * controller. |
| 3119 | */ |
| 3120 | ertm_to = 3 * ertm_to + 500; |
| 3121 | |
| 3122 | if (ertm_to > 0xffff) |
| 3123 | ertm_to = 0xffff; |
| 3124 | |
| 3125 | rfc->retrans_timeout = cpu_to_le16((u16) ertm_to); |
| 3126 | rfc->monitor_timeout = rfc->retrans_timeout; |
| 3127 | } else { |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3128 | rfc->retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); |
| 3129 | rfc->monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3130 | } |
| 3131 | } |
| 3132 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3133 | static inline void l2cap_txwin_setup(struct l2cap_chan *chan) |
| 3134 | { |
| 3135 | if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3136 | __l2cap_ews_supported(chan->conn)) { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3137 | /* use extended control field */ |
| 3138 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3139 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
| 3140 | } else { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3141 | chan->tx_win = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3142 | L2CAP_DEFAULT_TX_WINDOW); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3143 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
| 3144 | } |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3145 | chan->ack_win = chan->tx_win; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3146 | } |
| 3147 | |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3148 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | struct l2cap_conf_req *req = data; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3151 | struct l2cap_conf_rfc rfc = { .mode = chan->mode }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | void *ptr = req->data; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3153 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3155 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3157 | if (chan->num_conf_req || chan->num_conf_rsp) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3158 | goto done; |
| 3159 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3160 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3161 | case L2CAP_MODE_STREAMING: |
| 3162 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3163 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3164 | break; |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3165 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3166 | if (__l2cap_efs_supported(chan->conn)) |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3167 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3168 | |
Gustavo F. Padovan | 2ba13ed | 2010-06-09 16:39:05 -0300 | [diff] [blame] | 3169 | /* fall through */ |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3170 | default: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3171 | chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3172 | break; |
| 3173 | } |
| 3174 | |
| 3175 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3176 | if (chan->imtu != L2CAP_DEFAULT_MTU) |
| 3177 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | 7990681c | 2011-01-24 16:01:43 -0200 | [diff] [blame] | 3178 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3179 | switch (chan->mode) { |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3180 | case L2CAP_MODE_BASIC: |
Marcel Holtmann | 6fea7ad | 2014-07-09 11:53:35 +0200 | [diff] [blame] | 3181 | if (disable_ertm) |
| 3182 | break; |
| 3183 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3184 | if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3185 | !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3186 | break; |
| 3187 | |
Gustavo F. Padovan | 6254775 | 2010-06-08 20:05:31 -0300 | [diff] [blame] | 3188 | rfc.mode = L2CAP_MODE_BASIC; |
| 3189 | rfc.txwin_size = 0; |
| 3190 | rfc.max_transmit = 0; |
| 3191 | rfc.retrans_timeout = 0; |
| 3192 | rfc.monitor_timeout = 0; |
| 3193 | rfc.max_pdu_size = 0; |
| 3194 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3195 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3196 | (unsigned long) &rfc); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3197 | break; |
| 3198 | |
| 3199 | case L2CAP_MODE_ERTM: |
| 3200 | rfc.mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3201 | rfc.max_transmit = chan->max_tx; |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3202 | |
| 3203 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3204 | |
| 3205 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3206 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3207 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3208 | rfc.max_pdu_size = cpu_to_le16(size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3209 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3210 | l2cap_txwin_setup(chan); |
| 3211 | |
| 3212 | rfc.txwin_size = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3213 | L2CAP_DEFAULT_TX_WINDOW); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3214 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3215 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3216 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3217 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3218 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3219 | l2cap_add_opt_efs(&ptr, chan); |
| 3220 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3221 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3222 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3223 | chan->tx_win); |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3224 | |
| 3225 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3226 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3227 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3228 | chan->fcs = L2CAP_FCS_NONE; |
| 3229 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3230 | chan->fcs); |
| 3231 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3232 | break; |
| 3233 | |
| 3234 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 273759e | 2012-05-17 20:53:53 -0700 | [diff] [blame] | 3235 | l2cap_txwin_setup(chan); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3236 | rfc.mode = L2CAP_MODE_STREAMING; |
| 3237 | rfc.txwin_size = 0; |
| 3238 | rfc.max_transmit = 0; |
| 3239 | rfc.retrans_timeout = 0; |
| 3240 | rfc.monitor_timeout = 0; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3241 | |
| 3242 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3243 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3244 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3245 | rfc.max_pdu_size = cpu_to_le16(size); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3246 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3247 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3248 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3249 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3250 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3251 | l2cap_add_opt_efs(&ptr, chan); |
| 3252 | |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3253 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3254 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3255 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3256 | chan->fcs = L2CAP_FCS_NONE; |
| 3257 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3258 | chan->fcs); |
| 3259 | } |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3260 | break; |
| 3261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3263 | req->dcid = cpu_to_le16(chan->dcid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3264 | req->flags = cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | |
| 3266 | return ptr - data; |
| 3267 | } |
| 3268 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3269 | static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3271 | struct l2cap_conf_rsp *rsp = data; |
| 3272 | void *ptr = rsp->data; |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3273 | void *req = chan->conf_req; |
| 3274 | int len = chan->conf_len; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3275 | int type, hint, olen; |
| 3276 | unsigned long val; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3277 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3278 | struct l2cap_conf_efs efs; |
| 3279 | u8 remote_efs = 0; |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3280 | u16 mtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3281 | u16 result = L2CAP_CONF_SUCCESS; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3282 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3284 | BT_DBG("chan %p", chan); |
Marcel Holtmann | 820ae1b | 2006-11-18 22:15:00 +0100 | [diff] [blame] | 3285 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3286 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3287 | len -= l2cap_get_conf_opt(&req, &type, &olen, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | |
Gustavo F. Padovan | 589d274 | 2009-04-20 01:31:07 -0300 | [diff] [blame] | 3289 | hint = type & L2CAP_CONF_HINT; |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 3290 | type &= L2CAP_CONF_MASK; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3291 | |
| 3292 | switch (type) { |
| 3293 | case L2CAP_CONF_MTU: |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3294 | mtu = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3295 | break; |
| 3296 | |
| 3297 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3298 | chan->flush_to = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3299 | break; |
| 3300 | |
| 3301 | case L2CAP_CONF_QOS: |
| 3302 | break; |
| 3303 | |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3304 | case L2CAP_CONF_RFC: |
| 3305 | if (olen == sizeof(rfc)) |
| 3306 | memcpy(&rfc, (void *) val, olen); |
| 3307 | break; |
| 3308 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3309 | case L2CAP_CONF_FCS: |
| 3310 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3311 | set_bit(CONF_RECV_NO_FCS, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3312 | break; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3313 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3314 | case L2CAP_CONF_EFS: |
| 3315 | remote_efs = 1; |
| 3316 | if (olen == sizeof(efs)) |
| 3317 | memcpy(&efs, (void *) val, olen); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3318 | break; |
| 3319 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3320 | case L2CAP_CONF_EWS: |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3321 | if (!chan->conn->hs_enabled) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3322 | return -ECONNREFUSED; |
| 3323 | |
| 3324 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
| 3325 | set_bit(CONF_EWS_RECV, &chan->conf_state); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3326 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3327 | chan->remote_tx_win = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3328 | break; |
| 3329 | |
| 3330 | default: |
| 3331 | if (hint) |
| 3332 | break; |
| 3333 | |
| 3334 | result = L2CAP_CONF_UNKNOWN; |
| 3335 | *((u8 *) ptr++) = type; |
| 3336 | break; |
| 3337 | } |
| 3338 | } |
| 3339 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3340 | if (chan->num_conf_rsp || chan->num_conf_req > 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3341 | goto done; |
| 3342 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3343 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3344 | case L2CAP_MODE_STREAMING: |
| 3345 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3346 | if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3347 | chan->mode = l2cap_select_mode(rfc.mode, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3348 | chan->conn->feat_mask); |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3349 | break; |
| 3350 | } |
| 3351 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3352 | if (remote_efs) { |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3353 | if (__l2cap_efs_supported(chan->conn)) |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3354 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3355 | else |
| 3356 | return -ECONNREFUSED; |
| 3357 | } |
| 3358 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3359 | if (chan->mode != rfc.mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3360 | return -ECONNREFUSED; |
Gustavo F. Padovan | 742e519 | 2010-06-08 19:09:48 -0300 | [diff] [blame] | 3361 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3362 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3366 | if (chan->mode != rfc.mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3367 | result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3368 | rfc.mode = chan->mode; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3369 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3370 | if (chan->num_conf_rsp == 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3371 | return -ECONNREFUSED; |
| 3372 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3373 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3374 | (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3375 | } |
| 3376 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3377 | if (result == L2CAP_CONF_SUCCESS) { |
| 3378 | /* Configure output options and let the other side know |
| 3379 | * which ones we don't like. */ |
| 3380 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3381 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
| 3382 | result = L2CAP_CONF_UNACCEPT; |
| 3383 | else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3384 | chan->omtu = mtu; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3385 | set_bit(CONF_MTU_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3386 | } |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3387 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3388 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3389 | if (remote_efs) { |
| 3390 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3391 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3392 | efs.stype != chan->local_stype) { |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3393 | |
| 3394 | result = L2CAP_CONF_UNACCEPT; |
| 3395 | |
| 3396 | if (chan->num_conf_req >= 1) |
| 3397 | return -ECONNREFUSED; |
| 3398 | |
| 3399 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3400 | sizeof(efs), |
| 3401 | (unsigned long) &efs); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3402 | } else { |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3403 | /* Send PENDING Conf Rsp */ |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3404 | result = L2CAP_CONF_PENDING; |
| 3405 | set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3406 | } |
| 3407 | } |
| 3408 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3409 | switch (rfc.mode) { |
| 3410 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3411 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3412 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3413 | break; |
| 3414 | |
| 3415 | case L2CAP_MODE_ERTM: |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3416 | if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) |
| 3417 | chan->remote_tx_win = rfc.txwin_size; |
| 3418 | else |
| 3419 | rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; |
| 3420 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 3421 | chan->remote_max_tx = rfc.max_transmit; |
Mat Martineau | 86b1b26 | 2010-08-05 15:54:22 -0700 | [diff] [blame] | 3422 | |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3423 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3424 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3425 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3426 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3427 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3428 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3429 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3430 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3431 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3432 | |
| 3433 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3434 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3435 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3436 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3437 | chan->remote_id = efs.id; |
| 3438 | chan->remote_stype = efs.stype; |
| 3439 | chan->remote_msdu = le16_to_cpu(efs.msdu); |
| 3440 | chan->remote_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3441 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3442 | chan->remote_acc_lat = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3443 | le32_to_cpu(efs.acc_lat); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3444 | chan->remote_sdu_itime = |
| 3445 | le32_to_cpu(efs.sdu_itime); |
| 3446 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3447 | sizeof(efs), |
| 3448 | (unsigned long) &efs); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3449 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3450 | break; |
| 3451 | |
| 3452 | case L2CAP_MODE_STREAMING: |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3453 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3454 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3455 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3456 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3457 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3458 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3459 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3460 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3461 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3462 | (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3463 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3464 | break; |
| 3465 | |
| 3466 | default: |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3467 | result = L2CAP_CONF_UNACCEPT; |
| 3468 | |
| 3469 | memset(&rfc, 0, sizeof(rfc)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3470 | rfc.mode = chan->mode; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3471 | } |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3472 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3473 | if (result == L2CAP_CONF_SUCCESS) |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3474 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3475 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3476 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3477 | rsp->result = cpu_to_le16(result); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3478 | rsp->flags = cpu_to_le16(0); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3479 | |
| 3480 | return ptr - data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | } |
| 3482 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3483 | static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, |
| 3484 | void *data, u16 *result) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3485 | { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3486 | struct l2cap_conf_req *req = data; |
| 3487 | void *ptr = req->data; |
| 3488 | int type, olen; |
| 3489 | unsigned long val; |
Mat Martineau | 36e999a | 2011-12-08 17:23:21 -0800 | [diff] [blame] | 3490 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3491 | struct l2cap_conf_efs efs; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3492 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3493 | 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] | 3494 | |
| 3495 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3496 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3497 | |
| 3498 | switch (type) { |
| 3499 | case L2CAP_CONF_MTU: |
| 3500 | if (val < L2CAP_DEFAULT_MIN_MTU) { |
| 3501 | *result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3502 | chan->imtu = L2CAP_DEFAULT_MIN_MTU; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3503 | } else |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3504 | chan->imtu = val; |
| 3505 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3506 | break; |
| 3507 | |
| 3508 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3509 | chan->flush_to = val; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3510 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3511 | 2, chan->flush_to); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3512 | break; |
| 3513 | |
| 3514 | case L2CAP_CONF_RFC: |
| 3515 | if (olen == sizeof(rfc)) |
| 3516 | memcpy(&rfc, (void *)val, olen); |
| 3517 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3518 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3519 | rfc.mode != chan->mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3520 | return -ECONNREFUSED; |
| 3521 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3522 | chan->fcs = 0; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3523 | |
| 3524 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3525 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3526 | break; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3527 | |
| 3528 | case L2CAP_CONF_EWS: |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3529 | chan->ack_win = min_t(u16, val, chan->ack_win); |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3530 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3531 | chan->tx_win); |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3532 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3533 | |
| 3534 | case L2CAP_CONF_EFS: |
| 3535 | if (olen == sizeof(efs)) |
| 3536 | memcpy(&efs, (void *)val, olen); |
| 3537 | |
| 3538 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3539 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3540 | efs.stype != chan->local_stype) |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3541 | return -ECONNREFUSED; |
| 3542 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3543 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs), |
| 3544 | (unsigned long) &efs); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3545 | break; |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3546 | |
| 3547 | case L2CAP_CONF_FCS: |
| 3548 | if (*result == L2CAP_CONF_PENDING) |
| 3549 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3550 | set_bit(CONF_RECV_NO_FCS, |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3551 | &chan->conf_state); |
| 3552 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3553 | } |
| 3554 | } |
| 3555 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3556 | if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3557 | return -ECONNREFUSED; |
| 3558 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3559 | chan->mode = rfc.mode; |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3560 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3561 | if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3562 | switch (rfc.mode) { |
| 3563 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3564 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3565 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 3566 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3567 | if (!test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3568 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3569 | rfc.txwin_size); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3570 | |
| 3571 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3572 | chan->local_msdu = le16_to_cpu(efs.msdu); |
| 3573 | chan->local_sdu_itime = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3574 | le32_to_cpu(efs.sdu_itime); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3575 | chan->local_acc_lat = le32_to_cpu(efs.acc_lat); |
| 3576 | chan->local_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3577 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3578 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3579 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3580 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3581 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3582 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3583 | } |
| 3584 | } |
| 3585 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3586 | req->dcid = cpu_to_le16(chan->dcid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3587 | req->flags = cpu_to_le16(0); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3588 | |
| 3589 | return ptr - data; |
| 3590 | } |
| 3591 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3592 | static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, |
| 3593 | u16 result, u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | { |
| 3595 | struct l2cap_conf_rsp *rsp = data; |
| 3596 | void *ptr = rsp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3597 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3598 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3600 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3601 | rsp->result = cpu_to_le16(result); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3602 | rsp->flags = cpu_to_le16(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3603 | |
| 3604 | return ptr - data; |
| 3605 | } |
| 3606 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 3607 | void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan) |
| 3608 | { |
| 3609 | struct l2cap_le_conn_rsp rsp; |
| 3610 | struct l2cap_conn *conn = chan->conn; |
| 3611 | |
| 3612 | BT_DBG("chan %p", chan); |
| 3613 | |
| 3614 | rsp.dcid = cpu_to_le16(chan->scid); |
| 3615 | rsp.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 3616 | rsp.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 3617 | rsp.credits = cpu_to_le16(chan->rx_credits); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3618 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 3619 | |
| 3620 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), |
| 3621 | &rsp); |
| 3622 | } |
| 3623 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3624 | void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3625 | { |
| 3626 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3627 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3628 | u8 buf[128]; |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3629 | u8 rsp_code; |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3630 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3631 | rsp.scid = cpu_to_le16(chan->dcid); |
| 3632 | rsp.dcid = cpu_to_le16(chan->scid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3633 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 3634 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3635 | |
| 3636 | if (chan->hs_hcon) |
| 3637 | rsp_code = L2CAP_CREATE_CHAN_RSP; |
| 3638 | else |
| 3639 | rsp_code = L2CAP_CONN_RSP; |
| 3640 | |
| 3641 | BT_DBG("chan %p rsp_code %u", chan, rsp_code); |
| 3642 | |
| 3643 | l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp); |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3644 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3645 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3646 | return; |
| 3647 | |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3648 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3649 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3650 | chan->num_conf_req++; |
| 3651 | } |
| 3652 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3653 | 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] | 3654 | { |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3655 | int type, olen; |
| 3656 | unsigned long val; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3657 | /* Use sane default values in case a misbehaving remote device |
| 3658 | * did not send an RFC or extended window size option. |
| 3659 | */ |
| 3660 | u16 txwin_ext = chan->ack_win; |
| 3661 | struct l2cap_conf_rfc rfc = { |
| 3662 | .mode = chan->mode, |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3663 | .retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO), |
| 3664 | .monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO), |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3665 | .max_pdu_size = cpu_to_le16(chan->imtu), |
| 3666 | .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW), |
| 3667 | }; |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3668 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3669 | BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3670 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3671 | if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3672 | return; |
| 3673 | |
| 3674 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3675 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3676 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3677 | switch (type) { |
| 3678 | case L2CAP_CONF_RFC: |
| 3679 | if (olen == sizeof(rfc)) |
| 3680 | memcpy(&rfc, (void *)val, olen); |
Szymon Janc | 8f321f8 | 2012-06-08 11:33:33 +0200 | [diff] [blame] | 3681 | break; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3682 | case L2CAP_CONF_EWS: |
| 3683 | txwin_ext = val; |
| 3684 | break; |
| 3685 | } |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3686 | } |
| 3687 | |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3688 | switch (rfc.mode) { |
| 3689 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3690 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3691 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3692 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
| 3693 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3694 | chan->ack_win = min_t(u16, chan->ack_win, txwin_ext); |
| 3695 | else |
| 3696 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3697 | rfc.txwin_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3698 | break; |
| 3699 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3700 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3701 | } |
| 3702 | } |
| 3703 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3704 | static inline int l2cap_command_rej(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3705 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3706 | u8 *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3707 | { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3708 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3709 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3710 | if (cmd_len < sizeof(*rej)) |
| 3711 | return -EPROTO; |
| 3712 | |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3713 | if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3714 | return 0; |
| 3715 | |
| 3716 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3717 | cmd->ident == conn->info_ident) { |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 3718 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3719 | |
| 3720 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3721 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3722 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3723 | l2cap_conn_start(conn); |
| 3724 | } |
| 3725 | |
| 3726 | return 0; |
| 3727 | } |
| 3728 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3729 | static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn, |
| 3730 | struct l2cap_cmd_hdr *cmd, |
| 3731 | u8 *data, u8 rsp_code, u8 amp_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3732 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; |
| 3734 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3735 | struct l2cap_chan *chan = NULL, *pchan; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3736 | int result, status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3737 | |
| 3738 | u16 dcid = 0, scid = __le16_to_cpu(req->scid); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3739 | __le16 psm = req->psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3740 | |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 3741 | 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] | 3742 | |
| 3743 | /* Check if we have socket listening on psm */ |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 3744 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src, |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 3745 | &conn->hcon->dst, ACL_LINK); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3746 | if (!pchan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3747 | result = L2CAP_CR_BAD_PSM; |
| 3748 | goto sendresp; |
| 3749 | } |
| 3750 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3751 | mutex_lock(&conn->chan_lock); |
Gustavo Padovan | 8ffb929 | 2013-10-21 14:21:41 -0200 | [diff] [blame] | 3752 | l2cap_chan_lock(pchan); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 3753 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3754 | /* Check if the ACL is secure enough (if not SDP) */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3755 | if (psm != cpu_to_le16(L2CAP_PSM_SDP) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3756 | !hci_conn_check_link_mode(conn->hcon)) { |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 3757 | conn->disc_reason = HCI_ERROR_AUTH_FAILURE; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3758 | result = L2CAP_CR_SEC_BLOCK; |
| 3759 | goto response; |
| 3760 | } |
| 3761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3762 | result = L2CAP_CR_NO_MEM; |
| 3763 | |
Gustavo Padovan | 2dfa100 | 2012-05-27 22:27:58 -0300 | [diff] [blame] | 3764 | /* Check if we already have channel with that dcid */ |
| 3765 | if (__l2cap_get_chan_by_dcid(conn, scid)) |
| 3766 | goto response; |
| 3767 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 3768 | chan = pchan->ops->new_connection(pchan); |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 3769 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | goto response; |
| 3771 | |
Syam Sidhardhan | 330b6c1 | 2013-08-06 01:59:12 +0900 | [diff] [blame] | 3772 | /* For certain devices (ex: HID mouse), support for authentication, |
| 3773 | * pairing and bonding is optional. For such devices, inorder to avoid |
| 3774 | * the ACL alive for too long after L2CAP disconnection, reset the ACL |
| 3775 | * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect. |
| 3776 | */ |
| 3777 | conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT; |
| 3778 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 3779 | bacpy(&chan->src, &conn->hcon->src); |
| 3780 | bacpy(&chan->dst, &conn->hcon->dst); |
Marcel Holtmann | 4f1654e | 2013-10-13 08:50:41 -0700 | [diff] [blame] | 3781 | chan->src_type = bdaddr_type(conn->hcon, conn->hcon->src_type); |
| 3782 | chan->dst_type = bdaddr_type(conn->hcon, conn->hcon->dst_type); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3783 | chan->psm = psm; |
| 3784 | chan->dcid = scid; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3785 | chan->local_amp_id = amp_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3786 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3787 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3788 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3789 | dcid = chan->scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3790 | |
Gustavo Padovan | 8d836d7 | 2013-10-15 19:24:47 -0300 | [diff] [blame] | 3791 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3792 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3793 | chan->ident = cmd->ident; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3794 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3795 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 3796 | if (l2cap_chan_check_security(chan, false)) { |
Marcel Holtmann | bdc2578 | 2013-10-14 02:45:34 -0700 | [diff] [blame] | 3797 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3798 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3799 | result = L2CAP_CR_PEND; |
| 3800 | status = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 3801 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3802 | } else { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3803 | /* Force pending result for AMP controllers. |
| 3804 | * The connection will succeed after the |
| 3805 | * physical link is up. |
| 3806 | */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3807 | if (amp_id == AMP_ID_BREDR) { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3808 | l2cap_state_change(chan, BT_CONFIG); |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3809 | result = L2CAP_CR_SUCCESS; |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3810 | } else { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3811 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3812 | result = L2CAP_CR_PEND; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3813 | } |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3814 | status = L2CAP_CS_NO_INFO; |
| 3815 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3816 | } else { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3817 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3818 | result = L2CAP_CR_PEND; |
| 3819 | status = L2CAP_CS_AUTHEN_PEND; |
| 3820 | } |
| 3821 | } else { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3822 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3823 | result = L2CAP_CR_PEND; |
| 3824 | status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3825 | } |
| 3826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3827 | response: |
Gustavo Padovan | 8ffb929 | 2013-10-21 14:21:41 -0200 | [diff] [blame] | 3828 | l2cap_chan_unlock(pchan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3829 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 3830 | l2cap_chan_put(pchan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3831 | |
| 3832 | sendresp: |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3833 | rsp.scid = cpu_to_le16(scid); |
| 3834 | rsp.dcid = cpu_to_le16(dcid); |
| 3835 | rsp.result = cpu_to_le16(result); |
| 3836 | rsp.status = cpu_to_le16(status); |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3837 | l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3838 | |
| 3839 | if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { |
| 3840 | struct l2cap_info_req info; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3841 | info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3842 | |
| 3843 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 3844 | conn->info_ident = l2cap_get_ident(conn); |
| 3845 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 3846 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3847 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3848 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 3849 | sizeof(info), &info); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3850 | } |
| 3851 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3852 | if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3853 | result == L2CAP_CR_SUCCESS) { |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3854 | u8 buf[128]; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3855 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3856 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3857 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3858 | chan->num_conf_req++; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3859 | } |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3860 | |
| 3861 | return chan; |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3862 | } |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3863 | |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3864 | static int l2cap_connect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3865 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3866 | { |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3867 | struct hci_dev *hdev = conn->hcon->hdev; |
| 3868 | struct hci_conn *hcon = conn->hcon; |
| 3869 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3870 | if (cmd_len < sizeof(struct l2cap_conn_req)) |
| 3871 | return -EPROTO; |
| 3872 | |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3873 | hci_dev_lock(hdev); |
| 3874 | if (test_bit(HCI_MGMT, &hdev->dev_flags) && |
| 3875 | !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags)) |
| 3876 | mgmt_device_connected(hdev, &hcon->dst, hcon->type, |
| 3877 | hcon->dst_type, 0, NULL, 0, |
| 3878 | hcon->dev_class); |
| 3879 | hci_dev_unlock(hdev); |
| 3880 | |
Gustavo Padovan | 300229f | 2012-10-12 19:40:40 +0800 | [diff] [blame] | 3881 | l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3882 | return 0; |
| 3883 | } |
| 3884 | |
Mat Martineau | 5909cf3 | 2012-10-23 15:24:08 -0700 | [diff] [blame] | 3885 | static int l2cap_connect_create_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3886 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3887 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3888 | { |
| 3889 | struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; |
| 3890 | u16 scid, dcid, result, status; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3891 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3892 | u8 req[128]; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3893 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3894 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3895 | if (cmd_len < sizeof(*rsp)) |
| 3896 | return -EPROTO; |
| 3897 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3898 | scid = __le16_to_cpu(rsp->scid); |
| 3899 | dcid = __le16_to_cpu(rsp->dcid); |
| 3900 | result = __le16_to_cpu(rsp->result); |
| 3901 | status = __le16_to_cpu(rsp->status); |
| 3902 | |
Andrei Emeltchenko | 1b009c9 | 2012-02-21 12:54:54 +0200 | [diff] [blame] | 3903 | 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] | 3904 | dcid, scid, result, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3905 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3906 | mutex_lock(&conn->chan_lock); |
| 3907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3908 | if (scid) { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3909 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 3910 | if (!chan) { |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 3911 | err = -EBADSLT; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3912 | goto unlock; |
| 3913 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3914 | } else { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3915 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 3916 | if (!chan) { |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 3917 | err = -EBADSLT; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3918 | goto unlock; |
| 3919 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3920 | } |
| 3921 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3922 | err = 0; |
| 3923 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3924 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3926 | switch (result) { |
| 3927 | case L2CAP_CR_SUCCESS: |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 3928 | l2cap_state_change(chan, BT_CONFIG); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3929 | chan->ident = 0; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3930 | chan->dcid = dcid; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3931 | clear_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 3932 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3933 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3934 | break; |
| 3935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3936 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3937 | l2cap_build_conf_req(chan, req), req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3938 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3939 | break; |
| 3940 | |
| 3941 | case L2CAP_CR_PEND: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3942 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3943 | break; |
| 3944 | |
| 3945 | default: |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3946 | l2cap_chan_del(chan, ECONNREFUSED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3947 | break; |
| 3948 | } |
| 3949 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3950 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3951 | |
| 3952 | unlock: |
| 3953 | mutex_unlock(&conn->chan_lock); |
| 3954 | |
| 3955 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | } |
| 3957 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3958 | static inline void set_default_fcs(struct l2cap_chan *chan) |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 3959 | { |
| 3960 | /* FCS is enabled only in ERTM or streaming mode, if one or both |
| 3961 | * sides request it. |
| 3962 | */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3963 | if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3964 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3965 | else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3966 | chan->fcs = L2CAP_FCS_CRC16; |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 3967 | } |
| 3968 | |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 3969 | static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data, |
| 3970 | u8 ident, u16 flags) |
| 3971 | { |
| 3972 | struct l2cap_conn *conn = chan->conn; |
| 3973 | |
| 3974 | BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident, |
| 3975 | flags); |
| 3976 | |
| 3977 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
| 3978 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
| 3979 | |
| 3980 | l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP, |
| 3981 | l2cap_build_conf_rsp(chan, data, |
| 3982 | L2CAP_CONF_SUCCESS, flags), data); |
| 3983 | } |
| 3984 | |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 3985 | static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident, |
| 3986 | u16 scid, u16 dcid) |
| 3987 | { |
| 3988 | struct l2cap_cmd_rej_cid rej; |
| 3989 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3990 | rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 3991 | rej.scid = __cpu_to_le16(scid); |
| 3992 | rej.dcid = __cpu_to_le16(dcid); |
| 3993 | |
| 3994 | l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); |
| 3995 | } |
| 3996 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3997 | static inline int l2cap_config_req(struct l2cap_conn *conn, |
| 3998 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3999 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4000 | { |
| 4001 | struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; |
| 4002 | u16 dcid, flags; |
| 4003 | u8 rsp[64]; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4004 | struct l2cap_chan *chan; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4005 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4006 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4007 | if (cmd_len < sizeof(*req)) |
| 4008 | return -EPROTO; |
| 4009 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4010 | dcid = __le16_to_cpu(req->dcid); |
| 4011 | flags = __le16_to_cpu(req->flags); |
| 4012 | |
| 4013 | BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); |
| 4014 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4015 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4016 | if (!chan) { |
| 4017 | cmd_reject_invalid_cid(conn, cmd->ident, dcid, 0); |
| 4018 | return 0; |
| 4019 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4020 | |
David S. Miller | 033b114 | 2011-07-21 13:38:42 -0700 | [diff] [blame] | 4021 | if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4022 | cmd_reject_invalid_cid(conn, cmd->ident, chan->scid, |
| 4023 | chan->dcid); |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 4024 | goto unlock; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 4025 | } |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 4026 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4027 | /* Reject if config buffer is too small. */ |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 4028 | len = cmd_len - sizeof(*req); |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4029 | if (chan->conf_len + len > sizeof(chan->conf_req)) { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4030 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4031 | l2cap_build_conf_rsp(chan, rsp, |
| 4032 | L2CAP_CONF_REJECT, flags), rsp); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4033 | goto unlock; |
| 4034 | } |
| 4035 | |
| 4036 | /* Store config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4037 | memcpy(chan->conf_req + chan->conf_len, req->data, len); |
| 4038 | chan->conf_len += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4039 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4040 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4041 | /* Incomplete config. Send empty response. */ |
| 4042 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4043 | l2cap_build_conf_rsp(chan, rsp, |
| 4044 | L2CAP_CONF_SUCCESS, flags), rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4045 | goto unlock; |
| 4046 | } |
| 4047 | |
| 4048 | /* Complete config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4049 | len = l2cap_parse_conf_req(chan, rsp); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4050 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4051 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | goto unlock; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4053 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4055 | chan->ident = cmd->ident; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4056 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4057 | chan->num_conf_rsp++; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4058 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4059 | /* Reset config buffer. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4060 | chan->conf_len = 0; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4061 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4062 | if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4063 | goto unlock; |
| 4064 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4065 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4066 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4067 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4068 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4069 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4070 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4071 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4072 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4073 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4074 | else |
| 4075 | l2cap_chan_ready(chan); |
| 4076 | |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4077 | goto unlock; |
| 4078 | } |
| 4079 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4080 | if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 4081 | u8 buf[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4082 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4083 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4084 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | } |
| 4086 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4087 | /* Got Conf Rsp PENDING from remote side and asume we sent |
| 4088 | Conf Rsp PENDING in the code above */ |
| 4089 | if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 4090 | test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4091 | |
| 4092 | /* check compatibility */ |
| 4093 | |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4094 | /* Send rsp for BR/EDR channel */ |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4095 | if (!chan->hs_hcon) |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4096 | l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags); |
| 4097 | else |
| 4098 | chan->ident = cmd->ident; |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4099 | } |
| 4100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | unlock: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4102 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4103 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4104 | } |
| 4105 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4106 | static inline int l2cap_config_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4107 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4108 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4109 | { |
| 4110 | struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; |
| 4111 | u16 scid, flags, result; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4112 | struct l2cap_chan *chan; |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4113 | int len = cmd_len - sizeof(*rsp); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4114 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4115 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4116 | if (cmd_len < sizeof(*rsp)) |
| 4117 | return -EPROTO; |
| 4118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | scid = __le16_to_cpu(rsp->scid); |
| 4120 | flags = __le16_to_cpu(rsp->flags); |
| 4121 | result = __le16_to_cpu(rsp->result); |
| 4122 | |
Andrei Emeltchenko | 61386cb | 2012-03-12 12:13:07 +0200 | [diff] [blame] | 4123 | BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags, |
| 4124 | result, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4126 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4127 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4128 | return 0; |
| 4129 | |
| 4130 | switch (result) { |
| 4131 | case L2CAP_CONF_SUCCESS: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4132 | l2cap_conf_rfc_get(chan, rsp->data, len); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4133 | clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4134 | break; |
| 4135 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4136 | case L2CAP_CONF_PENDING: |
| 4137 | set_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
| 4138 | |
| 4139 | if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
| 4140 | char buf[64]; |
| 4141 | |
| 4142 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4143 | buf, &result); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4144 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4145 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4146 | goto done; |
| 4147 | } |
| 4148 | |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4149 | if (!chan->hs_hcon) { |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4150 | l2cap_send_efs_conf_rsp(chan, buf, cmd->ident, |
| 4151 | 0); |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 4152 | } else { |
| 4153 | if (l2cap_check_efs(chan)) { |
| 4154 | amp_create_logical_link(chan); |
| 4155 | chan->ident = cmd->ident; |
| 4156 | } |
| 4157 | } |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4158 | } |
| 4159 | goto done; |
| 4160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | case L2CAP_CONF_UNACCEPT: |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4162 | if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4163 | char req[64]; |
| 4164 | |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4165 | if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4166 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4167 | goto done; |
| 4168 | } |
| 4169 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4170 | /* throw out any old stored conf requests */ |
| 4171 | result = L2CAP_CONF_SUCCESS; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 4172 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4173 | req, &result); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4174 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4175 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4176 | goto done; |
| 4177 | } |
| 4178 | |
| 4179 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4180 | L2CAP_CONF_REQ, len, req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4181 | chan->num_conf_req++; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4182 | if (result != L2CAP_CONF_SUCCESS) |
| 4183 | goto done; |
| 4184 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4185 | } |
| 4186 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 4187 | default: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4188 | l2cap_chan_set_err(chan, ECONNRESET); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 4189 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 4190 | __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4191 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4192 | goto done; |
| 4193 | } |
| 4194 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4195 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | goto done; |
| 4197 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4198 | set_bit(CONF_INPUT_DONE, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4200 | if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4201 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4202 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4203 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4204 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4205 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4206 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4207 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4208 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4209 | else |
| 4210 | l2cap_chan_ready(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4214 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4215 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | } |
| 4217 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4218 | static inline int l2cap_disconnect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4219 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4220 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | { |
| 4222 | struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; |
| 4223 | struct l2cap_disconn_rsp rsp; |
| 4224 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4225 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4227 | if (cmd_len != sizeof(*req)) |
| 4228 | return -EPROTO; |
| 4229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | scid = __le16_to_cpu(req->scid); |
| 4231 | dcid = __le16_to_cpu(req->dcid); |
| 4232 | |
| 4233 | BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); |
| 4234 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4235 | mutex_lock(&conn->chan_lock); |
| 4236 | |
| 4237 | chan = __l2cap_get_chan_by_scid(conn, dcid); |
| 4238 | if (!chan) { |
| 4239 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4240 | cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid); |
| 4241 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4243 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4244 | l2cap_chan_lock(chan); |
| 4245 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4246 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4247 | rsp.scid = cpu_to_le16(chan->dcid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); |
| 4249 | |
Gustavo Padovan | 5ec1bbe | 2013-10-15 19:24:48 -0300 | [diff] [blame] | 4250 | chan->ops->set_shutdown(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4252 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4253 | l2cap_chan_del(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4254 | |
| 4255 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4257 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4258 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4259 | |
| 4260 | mutex_unlock(&conn->chan_lock); |
| 4261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | return 0; |
| 4263 | } |
| 4264 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4265 | static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4266 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4267 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | { |
| 4269 | struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; |
| 4270 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4271 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4273 | if (cmd_len != sizeof(*rsp)) |
| 4274 | return -EPROTO; |
| 4275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | scid = __le16_to_cpu(rsp->scid); |
| 4277 | dcid = __le16_to_cpu(rsp->dcid); |
| 4278 | |
| 4279 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); |
| 4280 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4281 | mutex_lock(&conn->chan_lock); |
| 4282 | |
| 4283 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 4284 | if (!chan) { |
| 4285 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4287 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4288 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4289 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4290 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4291 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4292 | l2cap_chan_del(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4293 | |
| 4294 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4295 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4296 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4297 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4298 | |
| 4299 | mutex_unlock(&conn->chan_lock); |
| 4300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4301 | return 0; |
| 4302 | } |
| 4303 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4304 | static inline int l2cap_information_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4305 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4306 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | { |
| 4308 | struct l2cap_info_req *req = (struct l2cap_info_req *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | u16 type; |
| 4310 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4311 | if (cmd_len != sizeof(*req)) |
| 4312 | return -EPROTO; |
| 4313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | type = __le16_to_cpu(req->type); |
| 4315 | |
| 4316 | BT_DBG("type 0x%4.4x", type); |
| 4317 | |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4318 | if (type == L2CAP_IT_FEAT_MASK) { |
| 4319 | u8 buf[8]; |
Marcel Holtmann | 44dd46d | 2009-05-02 19:09:01 -0700 | [diff] [blame] | 4320 | u32 feat_mask = l2cap_feat_mask; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4321 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4322 | rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 4323 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 4324 | if (!disable_ertm) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4325 | feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4326 | | L2CAP_FEAT_FCS; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4327 | if (conn->hs_enabled) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 4328 | feat_mask |= L2CAP_FEAT_EXT_FLOW |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4329 | | L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 4330 | |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 4331 | put_unaligned_le32(feat_mask, rsp->data); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4332 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4333 | buf); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4334 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
| 4335 | u8 buf[12]; |
| 4336 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 4337 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4338 | if (conn->hs_enabled) |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 4339 | l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; |
| 4340 | else |
| 4341 | l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; |
| 4342 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4343 | rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 4344 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Andrei Emeltchenko | c6337ea | 2011-10-20 17:02:44 +0300 | [diff] [blame] | 4345 | memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan)); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4346 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4347 | buf); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4348 | } else { |
| 4349 | struct l2cap_info_rsp rsp; |
| 4350 | rsp.type = cpu_to_le16(type); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4351 | rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4352 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp), |
| 4353 | &rsp); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4354 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | |
| 4356 | return 0; |
| 4357 | } |
| 4358 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4359 | static inline int l2cap_information_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4360 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4361 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | { |
| 4363 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; |
| 4364 | u16 type, result; |
| 4365 | |
Jaganath Kanakkassery | 3f6fa3d | 2013-06-21 19:55:11 +0530 | [diff] [blame] | 4366 | if (cmd_len < sizeof(*rsp)) |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4367 | return -EPROTO; |
| 4368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | type = __le16_to_cpu(rsp->type); |
| 4370 | result = __le16_to_cpu(rsp->result); |
| 4371 | |
| 4372 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); |
| 4373 | |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4374 | /* L2CAP Info req/rsp are unbound to channels, add extra checks */ |
| 4375 | if (cmd->ident != conn->info_ident || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4376 | conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4377 | return 0; |
| 4378 | |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 4379 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4380 | |
Ville Tervo | adb08ed | 2010-08-04 09:43:33 +0300 | [diff] [blame] | 4381 | if (result != L2CAP_IR_SUCCESS) { |
| 4382 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4383 | conn->info_ident = 0; |
| 4384 | |
| 4385 | l2cap_conn_start(conn); |
| 4386 | |
| 4387 | return 0; |
| 4388 | } |
| 4389 | |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4390 | switch (type) { |
| 4391 | case L2CAP_IT_FEAT_MASK: |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 4392 | conn->feat_mask = get_unaligned_le32(rsp->data); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4393 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 4394 | if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4395 | struct l2cap_info_req req; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4396 | req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4397 | |
| 4398 | conn->info_ident = l2cap_get_ident(conn); |
| 4399 | |
| 4400 | l2cap_send_cmd(conn, conn->info_ident, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4401 | L2CAP_INFO_REQ, sizeof(req), &req); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4402 | } else { |
| 4403 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4404 | conn->info_ident = 0; |
| 4405 | |
| 4406 | l2cap_conn_start(conn); |
| 4407 | } |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4408 | break; |
| 4409 | |
| 4410 | case L2CAP_IT_FIXED_CHAN: |
| 4411 | conn->fixed_chan_mask = rsp->data[0]; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4412 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4413 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4414 | |
| 4415 | l2cap_conn_start(conn); |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4416 | break; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4417 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4419 | return 0; |
| 4420 | } |
| 4421 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4422 | static int l2cap_create_channel_req(struct l2cap_conn *conn, |
| 4423 | struct l2cap_cmd_hdr *cmd, |
| 4424 | u16 cmd_len, void *data) |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4425 | { |
| 4426 | struct l2cap_create_chan_req *req = data; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4427 | struct l2cap_create_chan_rsp rsp; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4428 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4429 | struct hci_dev *hdev; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4430 | u16 psm, scid; |
| 4431 | |
| 4432 | if (cmd_len != sizeof(*req)) |
| 4433 | return -EPROTO; |
| 4434 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4435 | if (!conn->hs_enabled) |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4436 | return -EINVAL; |
| 4437 | |
| 4438 | psm = le16_to_cpu(req->psm); |
| 4439 | scid = le16_to_cpu(req->scid); |
| 4440 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4441 | 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] | 4442 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4443 | /* For controller id 0 make BR/EDR connection */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4444 | if (req->amp_id == AMP_ID_BREDR) { |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4445 | l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4446 | req->amp_id); |
| 4447 | return 0; |
| 4448 | } |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4449 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4450 | /* Validate AMP controller id */ |
| 4451 | hdev = hci_dev_get(req->amp_id); |
| 4452 | if (!hdev) |
| 4453 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4454 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4455 | if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4456 | hci_dev_put(hdev); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4457 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4461 | req->amp_id); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4462 | if (chan) { |
| 4463 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
| 4464 | struct hci_conn *hs_hcon; |
| 4465 | |
Marcel Holtmann | 98e0f7e | 2013-10-13 02:23:40 -0700 | [diff] [blame] | 4466 | hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, |
| 4467 | &conn->hcon->dst); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4468 | if (!hs_hcon) { |
| 4469 | hci_dev_put(hdev); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4470 | cmd_reject_invalid_cid(conn, cmd->ident, chan->scid, |
| 4471 | chan->dcid); |
| 4472 | return 0; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon); |
| 4476 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4477 | mgr->bredr_chan = chan; |
| 4478 | chan->hs_hcon = hs_hcon; |
Andrei Emeltchenko | fd45bf4 | 2012-11-20 17:16:22 +0200 | [diff] [blame] | 4479 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4480 | conn->mtu = hdev->block_mtu; |
| 4481 | } |
| 4482 | |
| 4483 | hci_dev_put(hdev); |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4484 | |
| 4485 | return 0; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4486 | |
| 4487 | error: |
| 4488 | rsp.dcid = 0; |
| 4489 | rsp.scid = cpu_to_le16(scid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4490 | rsp.result = cpu_to_le16(L2CAP_CR_BAD_AMP); |
| 4491 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4492 | |
| 4493 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, |
| 4494 | sizeof(rsp), &rsp); |
| 4495 | |
Johan Hedberg | dc28080 | 2013-09-16 13:05:13 +0300 | [diff] [blame] | 4496 | return 0; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4497 | } |
| 4498 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4499 | static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id) |
| 4500 | { |
| 4501 | struct l2cap_move_chan_req req; |
| 4502 | u8 ident; |
| 4503 | |
| 4504 | BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id); |
| 4505 | |
| 4506 | ident = l2cap_get_ident(chan->conn); |
| 4507 | chan->ident = ident; |
| 4508 | |
| 4509 | req.icid = cpu_to_le16(chan->scid); |
| 4510 | req.dest_amp_id = dest_amp_id; |
| 4511 | |
| 4512 | l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req), |
| 4513 | &req); |
| 4514 | |
| 4515 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4516 | } |
| 4517 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4518 | 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] | 4519 | { |
| 4520 | struct l2cap_move_chan_rsp rsp; |
| 4521 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4522 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4523 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4524 | rsp.icid = cpu_to_le16(chan->dcid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4525 | rsp.result = cpu_to_le16(result); |
| 4526 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4527 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP, |
| 4528 | sizeof(rsp), &rsp); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4529 | } |
| 4530 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4531 | 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] | 4532 | { |
| 4533 | struct l2cap_move_chan_cfm cfm; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4534 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4535 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4536 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4537 | chan->ident = l2cap_get_ident(chan->conn); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4538 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4539 | cfm.icid = cpu_to_le16(chan->scid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4540 | cfm.result = cpu_to_le16(result); |
| 4541 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4542 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM, |
| 4543 | sizeof(cfm), &cfm); |
| 4544 | |
| 4545 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4546 | } |
| 4547 | |
| 4548 | static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid) |
| 4549 | { |
| 4550 | struct l2cap_move_chan_cfm cfm; |
| 4551 | |
| 4552 | BT_DBG("conn %p, icid 0x%4.4x", conn, icid); |
| 4553 | |
| 4554 | cfm.icid = cpu_to_le16(icid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4555 | cfm.result = cpu_to_le16(L2CAP_MC_UNCONFIRMED); |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4556 | |
| 4557 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM, |
| 4558 | sizeof(cfm), &cfm); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4559 | } |
| 4560 | |
| 4561 | 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] | 4562 | u16 icid) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4563 | { |
| 4564 | struct l2cap_move_chan_cfm_rsp rsp; |
| 4565 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4566 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4567 | |
| 4568 | rsp.icid = cpu_to_le16(icid); |
| 4569 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); |
| 4570 | } |
| 4571 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 4572 | static void __release_logical_link(struct l2cap_chan *chan) |
| 4573 | { |
| 4574 | chan->hs_hchan = NULL; |
| 4575 | chan->hs_hcon = NULL; |
| 4576 | |
| 4577 | /* Placeholder - release the logical link */ |
| 4578 | } |
| 4579 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4580 | static void l2cap_logical_fail(struct l2cap_chan *chan) |
| 4581 | { |
| 4582 | /* Logical link setup failed */ |
| 4583 | if (chan->state != BT_CONNECTED) { |
| 4584 | /* Create channel failure, disconnect */ |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4585 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4586 | return; |
| 4587 | } |
| 4588 | |
| 4589 | switch (chan->move_role) { |
| 4590 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4591 | l2cap_move_done(chan); |
| 4592 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP); |
| 4593 | break; |
| 4594 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4595 | if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP || |
| 4596 | chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) { |
| 4597 | /* Remote has only sent pending or |
| 4598 | * success responses, clean up |
| 4599 | */ |
| 4600 | l2cap_move_done(chan); |
| 4601 | } |
| 4602 | |
| 4603 | /* Other amp move states imply that the move |
| 4604 | * has already aborted |
| 4605 | */ |
| 4606 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 4607 | break; |
| 4608 | } |
| 4609 | } |
| 4610 | |
| 4611 | static void l2cap_logical_finish_create(struct l2cap_chan *chan, |
| 4612 | struct hci_chan *hchan) |
| 4613 | { |
| 4614 | struct l2cap_conf_rsp rsp; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4615 | |
Andrei Emeltchenko | 336178a | 2012-10-31 15:46:27 +0200 | [diff] [blame] | 4616 | chan->hs_hchan = hchan; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4617 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4618 | |
Andrei Emeltchenko | 35ba956 | 2012-10-25 15:20:43 +0300 | [diff] [blame] | 4619 | l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4620 | |
| 4621 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Andrei Emeltchenko | fe79c6f | 2012-10-25 15:20:42 +0300 | [diff] [blame] | 4622 | int err; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4623 | |
| 4624 | set_default_fcs(chan); |
| 4625 | |
| 4626 | err = l2cap_ertm_init(chan); |
| 4627 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4628 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4629 | else |
| 4630 | l2cap_chan_ready(chan); |
| 4631 | } |
| 4632 | } |
| 4633 | |
| 4634 | static void l2cap_logical_finish_move(struct l2cap_chan *chan, |
| 4635 | struct hci_chan *hchan) |
| 4636 | { |
| 4637 | chan->hs_hcon = hchan->conn; |
| 4638 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4639 | |
| 4640 | BT_DBG("move_state %d", chan->move_state); |
| 4641 | |
| 4642 | switch (chan->move_state) { |
| 4643 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 4644 | /* Move confirm will be sent after a success |
| 4645 | * response is received |
| 4646 | */ |
| 4647 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4648 | break; |
| 4649 | case L2CAP_MOVE_WAIT_LOGICAL_CFM: |
| 4650 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4651 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4652 | } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 4653 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 4654 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4655 | } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4656 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4657 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4658 | } |
| 4659 | break; |
| 4660 | default: |
| 4661 | /* Move was not in expected state, free the channel */ |
| 4662 | __release_logical_link(chan); |
| 4663 | |
| 4664 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4665 | } |
| 4666 | } |
| 4667 | |
| 4668 | /* Call with chan locked */ |
Andrei Emeltchenko | 27695fb | 2012-10-25 15:20:45 +0300 | [diff] [blame] | 4669 | void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan, |
| 4670 | u8 status) |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4671 | { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4672 | BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status); |
| 4673 | |
| 4674 | if (status) { |
| 4675 | l2cap_logical_fail(chan); |
| 4676 | __release_logical_link(chan); |
| 4677 | return; |
| 4678 | } |
| 4679 | |
| 4680 | if (chan->state != BT_CONNECTED) { |
| 4681 | /* Ignore logical link if channel is on BR/EDR */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4682 | if (chan->local_amp_id != AMP_ID_BREDR) |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4683 | l2cap_logical_finish_create(chan, hchan); |
| 4684 | } else { |
| 4685 | l2cap_logical_finish_move(chan, hchan); |
| 4686 | } |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4687 | } |
| 4688 | |
Mat Martineau | 3f7a56c | 2012-10-23 15:24:23 -0700 | [diff] [blame] | 4689 | void l2cap_move_start(struct l2cap_chan *chan) |
| 4690 | { |
| 4691 | BT_DBG("chan %p", chan); |
| 4692 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4693 | if (chan->local_amp_id == AMP_ID_BREDR) { |
Mat Martineau | 3f7a56c | 2012-10-23 15:24:23 -0700 | [diff] [blame] | 4694 | if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED) |
| 4695 | return; |
| 4696 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4697 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4698 | /* Placeholder - start physical link setup */ |
| 4699 | } else { |
| 4700 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4701 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4702 | chan->move_id = 0; |
| 4703 | l2cap_move_setup(chan); |
| 4704 | l2cap_send_move_chan_req(chan, 0); |
| 4705 | } |
| 4706 | } |
| 4707 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4708 | static void l2cap_do_create(struct l2cap_chan *chan, int result, |
| 4709 | u8 local_amp_id, u8 remote_amp_id) |
| 4710 | { |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4711 | BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state), |
| 4712 | local_amp_id, remote_amp_id); |
| 4713 | |
Andrei Emeltchenko | 12d6cc6 | 2012-11-14 17:39:31 +0200 | [diff] [blame] | 4714 | chan->fcs = L2CAP_FCS_NONE; |
| 4715 | |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4716 | /* Outgoing channel on AMP */ |
| 4717 | if (chan->state == BT_CONNECT) { |
| 4718 | if (result == L2CAP_CR_SUCCESS) { |
| 4719 | chan->local_amp_id = local_amp_id; |
| 4720 | l2cap_send_create_chan_req(chan, remote_amp_id); |
| 4721 | } else { |
| 4722 | /* Revert to BR/EDR connect */ |
| 4723 | l2cap_send_conn_req(chan); |
| 4724 | } |
| 4725 | |
| 4726 | return; |
| 4727 | } |
| 4728 | |
| 4729 | /* Incoming channel on AMP */ |
| 4730 | if (__l2cap_no_conn_pending(chan)) { |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4731 | struct l2cap_conn_rsp rsp; |
| 4732 | char buf[128]; |
| 4733 | rsp.scid = cpu_to_le16(chan->dcid); |
| 4734 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4735 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4736 | if (result == L2CAP_CR_SUCCESS) { |
| 4737 | /* Send successful response */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4738 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 4739 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4740 | } else { |
| 4741 | /* Send negative response */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4742 | rsp.result = cpu_to_le16(L2CAP_CR_NO_MEM); |
| 4743 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4744 | } |
| 4745 | |
| 4746 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP, |
| 4747 | sizeof(rsp), &rsp); |
| 4748 | |
| 4749 | if (result == L2CAP_CR_SUCCESS) { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 4750 | l2cap_state_change(chan, BT_CONFIG); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4751 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 4752 | l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn), |
| 4753 | L2CAP_CONF_REQ, |
| 4754 | l2cap_build_conf_req(chan, buf), buf); |
| 4755 | chan->num_conf_req++; |
| 4756 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4757 | } |
| 4758 | } |
| 4759 | |
| 4760 | static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id, |
| 4761 | u8 remote_amp_id) |
| 4762 | { |
| 4763 | l2cap_move_setup(chan); |
| 4764 | chan->move_id = local_amp_id; |
| 4765 | chan->move_state = L2CAP_MOVE_WAIT_RSP; |
| 4766 | |
| 4767 | l2cap_send_move_chan_req(chan, remote_amp_id); |
| 4768 | } |
| 4769 | |
| 4770 | static void l2cap_do_move_respond(struct l2cap_chan *chan, int result) |
| 4771 | { |
| 4772 | struct hci_chan *hchan = NULL; |
| 4773 | |
| 4774 | /* Placeholder - get hci_chan for logical link */ |
| 4775 | |
| 4776 | if (hchan) { |
| 4777 | if (hchan->state == BT_CONNECTED) { |
| 4778 | /* Logical link is ready to go */ |
| 4779 | chan->hs_hcon = hchan->conn; |
| 4780 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4781 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4782 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4783 | |
| 4784 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 4785 | } else { |
| 4786 | /* Wait for logical link to be ready */ |
| 4787 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4788 | } |
| 4789 | } else { |
| 4790 | /* Logical link not available */ |
| 4791 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED); |
| 4792 | } |
| 4793 | } |
| 4794 | |
| 4795 | static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result) |
| 4796 | { |
| 4797 | if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4798 | u8 rsp_result; |
| 4799 | if (result == -EINVAL) |
| 4800 | rsp_result = L2CAP_MR_BAD_ID; |
| 4801 | else |
| 4802 | rsp_result = L2CAP_MR_NOT_ALLOWED; |
| 4803 | |
| 4804 | l2cap_send_move_chan_rsp(chan, rsp_result); |
| 4805 | } |
| 4806 | |
| 4807 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 4808 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4809 | |
| 4810 | /* Restart data transmission */ |
| 4811 | l2cap_ertm_send(chan); |
| 4812 | } |
| 4813 | |
Andrei Emeltchenko | a514b17 | 2012-11-14 17:39:30 +0200 | [diff] [blame] | 4814 | /* Invoke with locked chan */ |
| 4815 | void __l2cap_physical_cfm(struct l2cap_chan *chan, int result) |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4816 | { |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4817 | u8 local_amp_id = chan->local_amp_id; |
Andrei Emeltchenko | fffadc0 | 2012-11-01 15:37:03 +0200 | [diff] [blame] | 4818 | u8 remote_amp_id = chan->remote_amp_id; |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4819 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4820 | BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d", |
| 4821 | chan, result, local_amp_id, remote_amp_id); |
| 4822 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4823 | if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) { |
| 4824 | l2cap_chan_unlock(chan); |
| 4825 | return; |
| 4826 | } |
| 4827 | |
| 4828 | if (chan->state != BT_CONNECTED) { |
| 4829 | l2cap_do_create(chan, result, local_amp_id, remote_amp_id); |
| 4830 | } else if (result != L2CAP_MR_SUCCESS) { |
| 4831 | l2cap_do_move_cancel(chan, result); |
| 4832 | } else { |
| 4833 | switch (chan->move_role) { |
| 4834 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4835 | l2cap_do_move_initiate(chan, local_amp_id, |
| 4836 | remote_amp_id); |
| 4837 | break; |
| 4838 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4839 | l2cap_do_move_respond(chan, result); |
| 4840 | break; |
| 4841 | default: |
| 4842 | l2cap_do_move_cancel(chan, result); |
| 4843 | break; |
| 4844 | } |
| 4845 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4846 | } |
| 4847 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4848 | static inline int l2cap_move_channel_req(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4849 | struct l2cap_cmd_hdr *cmd, |
| 4850 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4851 | { |
| 4852 | struct l2cap_move_chan_req *req = data; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4853 | struct l2cap_move_chan_rsp rsp; |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4854 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4855 | u16 icid = 0; |
| 4856 | u16 result = L2CAP_MR_NOT_ALLOWED; |
| 4857 | |
| 4858 | if (cmd_len != sizeof(*req)) |
| 4859 | return -EPROTO; |
| 4860 | |
| 4861 | icid = le16_to_cpu(req->icid); |
| 4862 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4863 | 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] | 4864 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4865 | if (!conn->hs_enabled) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4866 | return -EINVAL; |
| 4867 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4868 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 4869 | if (!chan) { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4870 | rsp.icid = cpu_to_le16(icid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4871 | rsp.result = cpu_to_le16(L2CAP_MR_NOT_ALLOWED); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4872 | l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP, |
| 4873 | sizeof(rsp), &rsp); |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4874 | return 0; |
| 4875 | } |
| 4876 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4877 | chan->ident = cmd->ident; |
| 4878 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4879 | if (chan->scid < L2CAP_CID_DYN_START || |
| 4880 | chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY || |
| 4881 | (chan->mode != L2CAP_MODE_ERTM && |
| 4882 | chan->mode != L2CAP_MODE_STREAMING)) { |
| 4883 | result = L2CAP_MR_NOT_ALLOWED; |
| 4884 | goto send_move_response; |
| 4885 | } |
| 4886 | |
| 4887 | if (chan->local_amp_id == req->dest_amp_id) { |
| 4888 | result = L2CAP_MR_SAME_ID; |
| 4889 | goto send_move_response; |
| 4890 | } |
| 4891 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4892 | if (req->dest_amp_id != AMP_ID_BREDR) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4893 | struct hci_dev *hdev; |
| 4894 | hdev = hci_dev_get(req->dest_amp_id); |
| 4895 | if (!hdev || hdev->dev_type != HCI_AMP || |
| 4896 | !test_bit(HCI_UP, &hdev->flags)) { |
| 4897 | if (hdev) |
| 4898 | hci_dev_put(hdev); |
| 4899 | |
| 4900 | result = L2CAP_MR_BAD_ID; |
| 4901 | goto send_move_response; |
| 4902 | } |
| 4903 | hci_dev_put(hdev); |
| 4904 | } |
| 4905 | |
| 4906 | /* Detect a move collision. Only send a collision response |
| 4907 | * if this side has "lost", otherwise proceed with the move. |
| 4908 | * The winner has the larger bd_addr. |
| 4909 | */ |
| 4910 | if ((__chan_is_moving(chan) || |
| 4911 | chan->move_role != L2CAP_MOVE_ROLE_NONE) && |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 4912 | bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4913 | result = L2CAP_MR_COLLISION; |
| 4914 | goto send_move_response; |
| 4915 | } |
| 4916 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4917 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 4918 | l2cap_move_setup(chan); |
| 4919 | chan->move_id = req->dest_amp_id; |
| 4920 | icid = chan->dcid; |
| 4921 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4922 | if (req->dest_amp_id == AMP_ID_BREDR) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4923 | /* Moving to BR/EDR */ |
| 4924 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4925 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4926 | result = L2CAP_MR_PEND; |
| 4927 | } else { |
| 4928 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4929 | result = L2CAP_MR_SUCCESS; |
| 4930 | } |
| 4931 | } else { |
| 4932 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4933 | /* Placeholder - uncomment when amp functions are available */ |
| 4934 | /*amp_accept_physical(chan, req->dest_amp_id);*/ |
| 4935 | result = L2CAP_MR_PEND; |
| 4936 | } |
| 4937 | |
| 4938 | send_move_response: |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4939 | l2cap_send_move_chan_rsp(chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4940 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4941 | l2cap_chan_unlock(chan); |
| 4942 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4943 | return 0; |
| 4944 | } |
| 4945 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4946 | static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result) |
| 4947 | { |
| 4948 | struct l2cap_chan *chan; |
| 4949 | struct hci_chan *hchan = NULL; |
| 4950 | |
| 4951 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 4952 | if (!chan) { |
| 4953 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 4954 | return; |
| 4955 | } |
| 4956 | |
| 4957 | __clear_chan_timer(chan); |
| 4958 | if (result == L2CAP_MR_PEND) |
| 4959 | __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT); |
| 4960 | |
| 4961 | switch (chan->move_state) { |
| 4962 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 4963 | /* Move confirm will be sent when logical link |
| 4964 | * is complete. |
| 4965 | */ |
| 4966 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4967 | break; |
| 4968 | case L2CAP_MOVE_WAIT_RSP_SUCCESS: |
| 4969 | if (result == L2CAP_MR_PEND) { |
| 4970 | break; |
| 4971 | } else if (test_bit(CONN_LOCAL_BUSY, |
| 4972 | &chan->conn_state)) { |
| 4973 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4974 | } else { |
| 4975 | /* Logical link is up or moving to BR/EDR, |
| 4976 | * proceed with move |
| 4977 | */ |
| 4978 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 4979 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4980 | } |
| 4981 | break; |
| 4982 | case L2CAP_MOVE_WAIT_RSP: |
| 4983 | /* Moving to AMP */ |
| 4984 | if (result == L2CAP_MR_SUCCESS) { |
| 4985 | /* Remote is ready, send confirm immediately |
| 4986 | * after logical link is ready |
| 4987 | */ |
| 4988 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4989 | } else { |
| 4990 | /* Both logical link and move success |
| 4991 | * are required to confirm |
| 4992 | */ |
| 4993 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP; |
| 4994 | } |
| 4995 | |
| 4996 | /* Placeholder - get hci_chan for logical link */ |
| 4997 | if (!hchan) { |
| 4998 | /* Logical link not available */ |
| 4999 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5000 | break; |
| 5001 | } |
| 5002 | |
| 5003 | /* If the logical link is not yet connected, do not |
| 5004 | * send confirmation. |
| 5005 | */ |
| 5006 | if (hchan->state != BT_CONNECTED) |
| 5007 | break; |
| 5008 | |
| 5009 | /* Logical link is already ready to go */ |
| 5010 | |
| 5011 | chan->hs_hcon = hchan->conn; |
| 5012 | chan->hs_hcon->l2cap_data = chan->conn; |
| 5013 | |
| 5014 | if (result == L2CAP_MR_SUCCESS) { |
| 5015 | /* Can confirm now */ |
| 5016 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 5017 | } else { |
| 5018 | /* Now only need move success |
| 5019 | * to confirm |
| 5020 | */ |
| 5021 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 5022 | } |
| 5023 | |
| 5024 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 5025 | break; |
| 5026 | default: |
| 5027 | /* Any other amp move state means the move failed. */ |
| 5028 | chan->move_id = chan->local_amp_id; |
| 5029 | l2cap_move_done(chan); |
| 5030 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5031 | } |
| 5032 | |
| 5033 | l2cap_chan_unlock(chan); |
| 5034 | } |
| 5035 | |
| 5036 | static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid, |
| 5037 | u16 result) |
| 5038 | { |
| 5039 | struct l2cap_chan *chan; |
| 5040 | |
| 5041 | chan = l2cap_get_chan_by_ident(conn, ident); |
| 5042 | if (!chan) { |
| 5043 | /* Could not locate channel, icid is best guess */ |
| 5044 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 5045 | return; |
| 5046 | } |
| 5047 | |
| 5048 | __clear_chan_timer(chan); |
| 5049 | |
| 5050 | if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 5051 | if (result == L2CAP_MR_COLLISION) { |
| 5052 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 5053 | } else { |
| 5054 | /* Cleanup - cancel move */ |
| 5055 | chan->move_id = chan->local_amp_id; |
| 5056 | l2cap_move_done(chan); |
| 5057 | } |
| 5058 | } |
| 5059 | |
| 5060 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5061 | |
| 5062 | l2cap_chan_unlock(chan); |
| 5063 | } |
| 5064 | |
| 5065 | static int l2cap_move_channel_rsp(struct l2cap_conn *conn, |
| 5066 | struct l2cap_cmd_hdr *cmd, |
| 5067 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5068 | { |
| 5069 | struct l2cap_move_chan_rsp *rsp = data; |
| 5070 | u16 icid, result; |
| 5071 | |
| 5072 | if (cmd_len != sizeof(*rsp)) |
| 5073 | return -EPROTO; |
| 5074 | |
| 5075 | icid = le16_to_cpu(rsp->icid); |
| 5076 | result = le16_to_cpu(rsp->result); |
| 5077 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5078 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5079 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 5080 | if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND) |
| 5081 | l2cap_move_continue(conn, icid, result); |
| 5082 | else |
| 5083 | l2cap_move_fail(conn, cmd->ident, icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5084 | |
| 5085 | return 0; |
| 5086 | } |
| 5087 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5088 | static int l2cap_move_channel_confirm(struct l2cap_conn *conn, |
| 5089 | struct l2cap_cmd_hdr *cmd, |
| 5090 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5091 | { |
| 5092 | struct l2cap_move_chan_cfm *cfm = data; |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5093 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5094 | u16 icid, result; |
| 5095 | |
| 5096 | if (cmd_len != sizeof(*cfm)) |
| 5097 | return -EPROTO; |
| 5098 | |
| 5099 | icid = le16_to_cpu(cfm->icid); |
| 5100 | result = le16_to_cpu(cfm->result); |
| 5101 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5102 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5103 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5104 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 5105 | if (!chan) { |
| 5106 | /* Spec requires a response even if the icid was not found */ |
| 5107 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5108 | return 0; |
| 5109 | } |
| 5110 | |
| 5111 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) { |
| 5112 | if (result == L2CAP_MC_CONFIRMED) { |
| 5113 | chan->local_amp_id = chan->move_id; |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 5114 | if (chan->local_amp_id == AMP_ID_BREDR) |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5115 | __release_logical_link(chan); |
| 5116 | } else { |
| 5117 | chan->move_id = chan->local_amp_id; |
| 5118 | } |
| 5119 | |
| 5120 | l2cap_move_done(chan); |
| 5121 | } |
| 5122 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5123 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5124 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5125 | l2cap_chan_unlock(chan); |
| 5126 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5127 | return 0; |
| 5128 | } |
| 5129 | |
| 5130 | static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5131 | struct l2cap_cmd_hdr *cmd, |
| 5132 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5133 | { |
| 5134 | struct l2cap_move_chan_cfm_rsp *rsp = data; |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5135 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5136 | u16 icid; |
| 5137 | |
| 5138 | if (cmd_len != sizeof(*rsp)) |
| 5139 | return -EPROTO; |
| 5140 | |
| 5141 | icid = le16_to_cpu(rsp->icid); |
| 5142 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5143 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5144 | |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5145 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 5146 | if (!chan) |
| 5147 | return 0; |
| 5148 | |
| 5149 | __clear_chan_timer(chan); |
| 5150 | |
| 5151 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) { |
| 5152 | chan->local_amp_id = chan->move_id; |
| 5153 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 5154 | if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan) |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5155 | __release_logical_link(chan); |
| 5156 | |
| 5157 | l2cap_move_done(chan); |
| 5158 | } |
| 5159 | |
| 5160 | l2cap_chan_unlock(chan); |
| 5161 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5162 | return 0; |
| 5163 | } |
| 5164 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5165 | static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5166 | struct l2cap_cmd_hdr *cmd, |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5167 | u16 cmd_len, u8 *data) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5168 | { |
| 5169 | struct hci_conn *hcon = conn->hcon; |
| 5170 | struct l2cap_conn_param_update_req *req; |
| 5171 | struct l2cap_conn_param_update_rsp rsp; |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5172 | u16 min, max, latency, to_multiplier; |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5173 | int err; |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5174 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 5175 | if (hcon->role != HCI_ROLE_MASTER) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5176 | return -EINVAL; |
| 5177 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5178 | if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) |
| 5179 | return -EPROTO; |
| 5180 | |
| 5181 | req = (struct l2cap_conn_param_update_req *) data; |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 5182 | min = __le16_to_cpu(req->min); |
| 5183 | max = __le16_to_cpu(req->max); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5184 | latency = __le16_to_cpu(req->latency); |
| 5185 | to_multiplier = __le16_to_cpu(req->to_multiplier); |
| 5186 | |
| 5187 | 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] | 5188 | min, max, latency, to_multiplier); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5189 | |
| 5190 | memset(&rsp, 0, sizeof(rsp)); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5191 | |
Andre Guedes | d4905f2 | 2014-06-25 21:52:52 -0300 | [diff] [blame] | 5192 | err = hci_check_conn_params(min, max, latency, to_multiplier); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5193 | if (err) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5194 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5195 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5196 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5197 | |
| 5198 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5199 | sizeof(rsp), &rsp); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5200 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5201 | if (!err) { |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5202 | u8 store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5203 | |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5204 | store_hint = hci_le_conn_update(hcon, min, max, latency, |
| 5205 | to_multiplier); |
| 5206 | mgmt_new_conn_param(hcon->hdev, &hcon->dst, hcon->dst_type, |
| 5207 | store_hint, min, max, latency, |
| 5208 | to_multiplier); |
| 5209 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5210 | } |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5211 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5212 | return 0; |
| 5213 | } |
| 5214 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5215 | static int l2cap_le_connect_rsp(struct l2cap_conn *conn, |
| 5216 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5217 | u8 *data) |
| 5218 | { |
| 5219 | struct l2cap_le_conn_rsp *rsp = (struct l2cap_le_conn_rsp *) data; |
| 5220 | u16 dcid, mtu, mps, credits, result; |
| 5221 | struct l2cap_chan *chan; |
| 5222 | int err; |
| 5223 | |
| 5224 | if (cmd_len < sizeof(*rsp)) |
| 5225 | return -EPROTO; |
| 5226 | |
| 5227 | dcid = __le16_to_cpu(rsp->dcid); |
| 5228 | mtu = __le16_to_cpu(rsp->mtu); |
| 5229 | mps = __le16_to_cpu(rsp->mps); |
| 5230 | credits = __le16_to_cpu(rsp->credits); |
| 5231 | result = __le16_to_cpu(rsp->result); |
| 5232 | |
| 5233 | if (result == L2CAP_CR_SUCCESS && (mtu < 23 || mps < 23)) |
| 5234 | return -EPROTO; |
| 5235 | |
| 5236 | BT_DBG("dcid 0x%4.4x mtu %u mps %u credits %u result 0x%2.2x", |
| 5237 | dcid, mtu, mps, credits, result); |
| 5238 | |
| 5239 | mutex_lock(&conn->chan_lock); |
| 5240 | |
| 5241 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 5242 | if (!chan) { |
| 5243 | err = -EBADSLT; |
| 5244 | goto unlock; |
| 5245 | } |
| 5246 | |
| 5247 | err = 0; |
| 5248 | |
| 5249 | l2cap_chan_lock(chan); |
| 5250 | |
| 5251 | switch (result) { |
| 5252 | case L2CAP_CR_SUCCESS: |
| 5253 | chan->ident = 0; |
| 5254 | chan->dcid = dcid; |
| 5255 | chan->omtu = mtu; |
| 5256 | chan->remote_mps = mps; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5257 | chan->tx_credits = credits; |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5258 | l2cap_chan_ready(chan); |
| 5259 | break; |
| 5260 | |
| 5261 | default: |
| 5262 | l2cap_chan_del(chan, ECONNREFUSED); |
| 5263 | break; |
| 5264 | } |
| 5265 | |
| 5266 | l2cap_chan_unlock(chan); |
| 5267 | |
| 5268 | unlock: |
| 5269 | mutex_unlock(&conn->chan_lock); |
| 5270 | |
| 5271 | return err; |
| 5272 | } |
| 5273 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5274 | static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5275 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5276 | u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5277 | { |
| 5278 | int err = 0; |
| 5279 | |
| 5280 | switch (cmd->code) { |
| 5281 | case L2CAP_COMMAND_REJ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5282 | l2cap_command_rej(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5283 | break; |
| 5284 | |
| 5285 | case L2CAP_CONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5286 | err = l2cap_connect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5287 | break; |
| 5288 | |
| 5289 | case L2CAP_CONN_RSP: |
Mat Martineau | f5a2598 | 2012-10-11 17:48:21 +0300 | [diff] [blame] | 5290 | case L2CAP_CREATE_CHAN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5291 | l2cap_connect_create_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5292 | break; |
| 5293 | |
| 5294 | case L2CAP_CONF_REQ: |
| 5295 | err = l2cap_config_req(conn, cmd, cmd_len, data); |
| 5296 | break; |
| 5297 | |
| 5298 | case L2CAP_CONF_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5299 | l2cap_config_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5300 | break; |
| 5301 | |
| 5302 | case L2CAP_DISCONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5303 | err = l2cap_disconnect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5304 | break; |
| 5305 | |
| 5306 | case L2CAP_DISCONN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5307 | l2cap_disconnect_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5308 | break; |
| 5309 | |
| 5310 | case L2CAP_ECHO_REQ: |
| 5311 | l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); |
| 5312 | break; |
| 5313 | |
| 5314 | case L2CAP_ECHO_RSP: |
| 5315 | break; |
| 5316 | |
| 5317 | case L2CAP_INFO_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5318 | err = l2cap_information_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5319 | break; |
| 5320 | |
| 5321 | case L2CAP_INFO_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5322 | l2cap_information_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5323 | break; |
| 5324 | |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 5325 | case L2CAP_CREATE_CHAN_REQ: |
| 5326 | err = l2cap_create_channel_req(conn, cmd, cmd_len, data); |
| 5327 | break; |
| 5328 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5329 | case L2CAP_MOVE_CHAN_REQ: |
| 5330 | err = l2cap_move_channel_req(conn, cmd, cmd_len, data); |
| 5331 | break; |
| 5332 | |
| 5333 | case L2CAP_MOVE_CHAN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5334 | l2cap_move_channel_rsp(conn, cmd, cmd_len, data); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5335 | break; |
| 5336 | |
| 5337 | case L2CAP_MOVE_CHAN_CFM: |
| 5338 | err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); |
| 5339 | break; |
| 5340 | |
| 5341 | case L2CAP_MOVE_CHAN_CFM_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5342 | l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5343 | break; |
| 5344 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5345 | default: |
| 5346 | BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); |
| 5347 | err = -EINVAL; |
| 5348 | break; |
| 5349 | } |
| 5350 | |
| 5351 | return err; |
| 5352 | } |
| 5353 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5354 | static int l2cap_le_connect_req(struct l2cap_conn *conn, |
| 5355 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5356 | u8 *data) |
| 5357 | { |
| 5358 | struct l2cap_le_conn_req *req = (struct l2cap_le_conn_req *) data; |
| 5359 | struct l2cap_le_conn_rsp rsp; |
| 5360 | struct l2cap_chan *chan, *pchan; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5361 | u16 dcid, scid, credits, mtu, mps; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5362 | __le16 psm; |
| 5363 | u8 result; |
| 5364 | |
| 5365 | if (cmd_len != sizeof(*req)) |
| 5366 | return -EPROTO; |
| 5367 | |
| 5368 | scid = __le16_to_cpu(req->scid); |
| 5369 | mtu = __le16_to_cpu(req->mtu); |
| 5370 | mps = __le16_to_cpu(req->mps); |
| 5371 | psm = req->psm; |
| 5372 | dcid = 0; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5373 | credits = 0; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5374 | |
| 5375 | if (mtu < 23 || mps < 23) |
| 5376 | return -EPROTO; |
| 5377 | |
| 5378 | BT_DBG("psm 0x%2.2x scid 0x%4.4x mtu %u mps %u", __le16_to_cpu(psm), |
| 5379 | scid, mtu, mps); |
| 5380 | |
| 5381 | /* Check if we have socket listening on psm */ |
| 5382 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src, |
| 5383 | &conn->hcon->dst, LE_LINK); |
| 5384 | if (!pchan) { |
| 5385 | result = L2CAP_CR_BAD_PSM; |
| 5386 | chan = NULL; |
| 5387 | goto response; |
| 5388 | } |
| 5389 | |
| 5390 | mutex_lock(&conn->chan_lock); |
| 5391 | l2cap_chan_lock(pchan); |
| 5392 | |
| 5393 | if (!smp_sufficient_security(conn->hcon, pchan->sec_level)) { |
| 5394 | result = L2CAP_CR_AUTHENTICATION; |
| 5395 | chan = NULL; |
| 5396 | goto response_unlock; |
| 5397 | } |
| 5398 | |
| 5399 | /* Check if we already have channel with that dcid */ |
| 5400 | if (__l2cap_get_chan_by_dcid(conn, scid)) { |
| 5401 | result = L2CAP_CR_NO_MEM; |
| 5402 | chan = NULL; |
| 5403 | goto response_unlock; |
| 5404 | } |
| 5405 | |
| 5406 | chan = pchan->ops->new_connection(pchan); |
| 5407 | if (!chan) { |
| 5408 | result = L2CAP_CR_NO_MEM; |
| 5409 | goto response_unlock; |
| 5410 | } |
| 5411 | |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 5412 | l2cap_le_flowctl_init(chan); |
| 5413 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5414 | bacpy(&chan->src, &conn->hcon->src); |
| 5415 | bacpy(&chan->dst, &conn->hcon->dst); |
| 5416 | chan->src_type = bdaddr_type(conn->hcon, conn->hcon->src_type); |
| 5417 | chan->dst_type = bdaddr_type(conn->hcon, conn->hcon->dst_type); |
| 5418 | chan->psm = psm; |
| 5419 | chan->dcid = scid; |
| 5420 | chan->omtu = mtu; |
| 5421 | chan->remote_mps = mps; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5422 | chan->tx_credits = __le16_to_cpu(req->credits); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5423 | |
| 5424 | __l2cap_chan_add(conn, chan); |
| 5425 | dcid = chan->scid; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5426 | credits = chan->rx_credits; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5427 | |
| 5428 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
| 5429 | |
| 5430 | chan->ident = cmd->ident; |
| 5431 | |
| 5432 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
| 5433 | l2cap_state_change(chan, BT_CONNECT2); |
Johan Hedberg | 434714d | 2014-09-01 09:45:03 +0300 | [diff] [blame] | 5434 | /* The following result value is actually not defined |
| 5435 | * for LE CoC but we use it to let the function know |
| 5436 | * that it should bail out after doing its cleanup |
| 5437 | * instead of sending a response. |
| 5438 | */ |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5439 | result = L2CAP_CR_PEND; |
| 5440 | chan->ops->defer(chan); |
| 5441 | } else { |
| 5442 | l2cap_chan_ready(chan); |
| 5443 | result = L2CAP_CR_SUCCESS; |
| 5444 | } |
| 5445 | |
| 5446 | response_unlock: |
| 5447 | l2cap_chan_unlock(pchan); |
| 5448 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 5449 | l2cap_chan_put(pchan); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5450 | |
| 5451 | if (result == L2CAP_CR_PEND) |
| 5452 | return 0; |
| 5453 | |
| 5454 | response: |
| 5455 | if (chan) { |
| 5456 | rsp.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 5457 | rsp.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5458 | } else { |
| 5459 | rsp.mtu = 0; |
| 5460 | rsp.mps = 0; |
| 5461 | } |
| 5462 | |
| 5463 | rsp.dcid = cpu_to_le16(dcid); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5464 | rsp.credits = cpu_to_le16(credits); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5465 | rsp.result = cpu_to_le16(result); |
| 5466 | |
| 5467 | l2cap_send_cmd(conn, cmd->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), &rsp); |
| 5468 | |
| 5469 | return 0; |
| 5470 | } |
| 5471 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5472 | static inline int l2cap_le_credits(struct l2cap_conn *conn, |
| 5473 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5474 | u8 *data) |
| 5475 | { |
| 5476 | struct l2cap_le_credits *pkt; |
| 5477 | struct l2cap_chan *chan; |
Johan Hedberg | 0f1bfe4 | 2014-01-27 15:11:35 -0800 | [diff] [blame] | 5478 | u16 cid, credits, max_credits; |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5479 | |
| 5480 | if (cmd_len != sizeof(*pkt)) |
| 5481 | return -EPROTO; |
| 5482 | |
| 5483 | pkt = (struct l2cap_le_credits *) data; |
| 5484 | cid = __le16_to_cpu(pkt->cid); |
| 5485 | credits = __le16_to_cpu(pkt->credits); |
| 5486 | |
| 5487 | BT_DBG("cid 0x%4.4x credits 0x%4.4x", cid, credits); |
| 5488 | |
| 5489 | chan = l2cap_get_chan_by_dcid(conn, cid); |
| 5490 | if (!chan) |
| 5491 | return -EBADSLT; |
| 5492 | |
Johan Hedberg | 0f1bfe4 | 2014-01-27 15:11:35 -0800 | [diff] [blame] | 5493 | max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits; |
| 5494 | if (credits > max_credits) { |
| 5495 | BT_ERR("LE credits overflow"); |
| 5496 | l2cap_send_disconn_req(chan, ECONNRESET); |
| 5497 | |
| 5498 | /* Return 0 so that we don't trigger an unnecessary |
| 5499 | * command reject packet. |
| 5500 | */ |
| 5501 | return 0; |
| 5502 | } |
| 5503 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5504 | chan->tx_credits += credits; |
| 5505 | |
| 5506 | while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { |
| 5507 | l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); |
| 5508 | chan->tx_credits--; |
| 5509 | } |
| 5510 | |
| 5511 | if (chan->tx_credits) |
| 5512 | chan->ops->resume(chan); |
| 5513 | |
| 5514 | l2cap_chan_unlock(chan); |
| 5515 | |
| 5516 | return 0; |
| 5517 | } |
| 5518 | |
Johan Hedberg | 71fb419 | 2013-12-10 10:52:48 +0200 | [diff] [blame] | 5519 | static inline int l2cap_le_command_rej(struct l2cap_conn *conn, |
| 5520 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5521 | u8 *data) |
| 5522 | { |
| 5523 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
| 5524 | struct l2cap_chan *chan; |
| 5525 | |
| 5526 | if (cmd_len < sizeof(*rej)) |
| 5527 | return -EPROTO; |
| 5528 | |
| 5529 | mutex_lock(&conn->chan_lock); |
| 5530 | |
| 5531 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 5532 | if (!chan) |
| 5533 | goto done; |
| 5534 | |
| 5535 | l2cap_chan_lock(chan); |
| 5536 | l2cap_chan_del(chan, ECONNREFUSED); |
| 5537 | l2cap_chan_unlock(chan); |
| 5538 | |
| 5539 | done: |
| 5540 | mutex_unlock(&conn->chan_lock); |
| 5541 | return 0; |
| 5542 | } |
| 5543 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5544 | static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5545 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5546 | u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5547 | { |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5548 | int err = 0; |
| 5549 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5550 | switch (cmd->code) { |
| 5551 | case L2CAP_COMMAND_REJ: |
Johan Hedberg | 71fb419 | 2013-12-10 10:52:48 +0200 | [diff] [blame] | 5552 | l2cap_le_command_rej(conn, cmd, cmd_len, data); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5553 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5554 | |
| 5555 | case L2CAP_CONN_PARAM_UPDATE_REQ: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5556 | err = l2cap_conn_param_update_req(conn, cmd, cmd_len, data); |
| 5557 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5558 | |
| 5559 | case L2CAP_CONN_PARAM_UPDATE_RSP: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5560 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5561 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5562 | case L2CAP_LE_CONN_RSP: |
| 5563 | l2cap_le_connect_rsp(conn, cmd, cmd_len, data); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5564 | break; |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5565 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5566 | case L2CAP_LE_CONN_REQ: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5567 | err = l2cap_le_connect_req(conn, cmd, cmd_len, data); |
| 5568 | break; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5569 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5570 | case L2CAP_LE_CREDITS: |
| 5571 | err = l2cap_le_credits(conn, cmd, cmd_len, data); |
| 5572 | break; |
| 5573 | |
Johan Hedberg | 3defe01 | 2013-05-15 10:16:06 +0300 | [diff] [blame] | 5574 | case L2CAP_DISCONN_REQ: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5575 | err = l2cap_disconnect_req(conn, cmd, cmd_len, data); |
| 5576 | break; |
Johan Hedberg | 3defe01 | 2013-05-15 10:16:06 +0300 | [diff] [blame] | 5577 | |
| 5578 | case L2CAP_DISCONN_RSP: |
| 5579 | l2cap_disconnect_rsp(conn, cmd, cmd_len, data); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5580 | break; |
Johan Hedberg | 3defe01 | 2013-05-15 10:16:06 +0300 | [diff] [blame] | 5581 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5582 | default: |
| 5583 | BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5584 | err = -EINVAL; |
| 5585 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5586 | } |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5587 | |
| 5588 | return err; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5589 | } |
| 5590 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5591 | static inline void l2cap_le_sig_channel(struct l2cap_conn *conn, |
| 5592 | struct sk_buff *skb) |
| 5593 | { |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5594 | struct hci_conn *hcon = conn->hcon; |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5595 | struct l2cap_cmd_hdr *cmd; |
| 5596 | u16 len; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5597 | int err; |
| 5598 | |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5599 | if (hcon->type != LE_LINK) |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5600 | goto drop; |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5601 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5602 | if (skb->len < L2CAP_CMD_HDR_SIZE) |
| 5603 | goto drop; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5604 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5605 | cmd = (void *) skb->data; |
| 5606 | skb_pull(skb, L2CAP_CMD_HDR_SIZE); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5607 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5608 | len = le16_to_cpu(cmd->len); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5609 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5610 | 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] | 5611 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5612 | if (len != skb->len || !cmd->ident) { |
| 5613 | BT_DBG("corrupted command"); |
| 5614 | goto drop; |
| 5615 | } |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5616 | |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5617 | err = l2cap_le_sig_cmd(conn, cmd, len, skb->data); |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5618 | if (err) { |
| 5619 | struct l2cap_cmd_rej_unk rej; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5620 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5621 | BT_ERR("Wrong link type (%d)", err); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5622 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5623 | rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5624 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
| 5625 | sizeof(rej), &rej); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5626 | } |
| 5627 | |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5628 | drop: |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5629 | kfree_skb(skb); |
| 5630 | } |
| 5631 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5632 | static inline void l2cap_sig_channel(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5633 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5634 | { |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5635 | struct hci_conn *hcon = conn->hcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5636 | u8 *data = skb->data; |
| 5637 | int len = skb->len; |
| 5638 | struct l2cap_cmd_hdr cmd; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5639 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5640 | |
| 5641 | l2cap_raw_recv(conn, skb); |
| 5642 | |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5643 | if (hcon->type != ACL_LINK) |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5644 | goto drop; |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5646 | while (len >= L2CAP_CMD_HDR_SIZE) { |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5647 | u16 cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5648 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 5649 | data += L2CAP_CMD_HDR_SIZE; |
| 5650 | len -= L2CAP_CMD_HDR_SIZE; |
| 5651 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5652 | cmd_len = le16_to_cpu(cmd.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5653 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5654 | BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, |
| 5655 | cmd.ident); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5657 | if (cmd_len > len || !cmd.ident) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5658 | BT_DBG("corrupted command"); |
| 5659 | break; |
| 5660 | } |
| 5661 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5662 | err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5663 | if (err) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 5664 | struct l2cap_cmd_rej_unk rej; |
Gustavo F. Padovan | 2c6d1a2 | 2011-03-23 14:38:32 -0300 | [diff] [blame] | 5665 | |
| 5666 | BT_ERR("Wrong link type (%d)", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5667 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5668 | rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5669 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, |
| 5670 | sizeof(rej), &rej); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5671 | } |
| 5672 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5673 | data += cmd_len; |
| 5674 | len -= cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5675 | } |
| 5676 | |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5677 | drop: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | kfree_skb(skb); |
| 5679 | } |
| 5680 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5681 | 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] | 5682 | { |
| 5683 | u16 our_fcs, rcv_fcs; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 5684 | int hdr_size; |
| 5685 | |
| 5686 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 5687 | hdr_size = L2CAP_EXT_HDR_SIZE; |
| 5688 | else |
| 5689 | hdr_size = L2CAP_ENH_HDR_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5690 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5691 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5692 | skb_trim(skb, skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5693 | rcv_fcs = get_unaligned_le16(skb->data + skb->len); |
| 5694 | our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); |
| 5695 | |
| 5696 | if (our_fcs != rcv_fcs) |
João Paulo Rechi Vita | 7a560e5 | 2010-06-22 13:56:27 -0300 | [diff] [blame] | 5697 | return -EBADMSG; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5698 | } |
| 5699 | return 0; |
| 5700 | } |
| 5701 | |
Mat Martineau | 6ea0048 | 2012-05-17 20:53:52 -0700 | [diff] [blame] | 5702 | 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] | 5703 | { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5704 | struct l2cap_ctrl control; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5705 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5706 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5707 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5708 | memset(&control, 0, sizeof(control)); |
| 5709 | control.sframe = 1; |
| 5710 | control.final = 1; |
| 5711 | control.reqseq = chan->buffer_seq; |
| 5712 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5713 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5714 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5715 | control.super = L2CAP_SUPER_RNR; |
| 5716 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5717 | } |
| 5718 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5719 | if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
| 5720 | chan->unacked_frames > 0) |
| 5721 | __set_retrans_timer(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5722 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5723 | /* Send pending iframes */ |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 5724 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5725 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5726 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5727 | test_bit(CONN_SEND_FBIT, &chan->conn_state)) { |
| 5728 | /* F-bit wasn't sent in an s-frame or i-frame yet, so |
| 5729 | * send it now. |
| 5730 | */ |
| 5731 | control.super = L2CAP_SUPER_RR; |
| 5732 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5733 | } |
| 5734 | } |
| 5735 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5736 | static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag, |
| 5737 | struct sk_buff **last_frag) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5738 | { |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5739 | /* skb->len reflects data in skb as well as all fragments |
| 5740 | * skb->data_len reflects only data in fragments |
| 5741 | */ |
| 5742 | if (!skb_has_frag_list(skb)) |
| 5743 | skb_shinfo(skb)->frag_list = new_frag; |
| 5744 | |
| 5745 | new_frag->next = NULL; |
| 5746 | |
| 5747 | (*last_frag)->next = new_frag; |
| 5748 | *last_frag = new_frag; |
| 5749 | |
| 5750 | skb->len += new_frag->len; |
| 5751 | skb->data_len += new_frag->len; |
| 5752 | skb->truesize += new_frag->truesize; |
| 5753 | } |
| 5754 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5755 | static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, |
| 5756 | struct l2cap_ctrl *control) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5757 | { |
| 5758 | int err = -EINVAL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5759 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5760 | switch (control->sar) { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5761 | case L2CAP_SAR_UNSEGMENTED: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5762 | if (chan->sdu) |
| 5763 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5764 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5765 | err = chan->ops->recv(chan, skb); |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5766 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5767 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5768 | case L2CAP_SAR_START: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5769 | if (chan->sdu) |
| 5770 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5771 | |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5772 | chan->sdu_len = get_unaligned_le16(skb->data); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5773 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5774 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5775 | if (chan->sdu_len > chan->imtu) { |
| 5776 | err = -EMSGSIZE; |
| 5777 | break; |
| 5778 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5779 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5780 | if (skb->len >= chan->sdu_len) |
| 5781 | break; |
| 5782 | |
| 5783 | chan->sdu = skb; |
| 5784 | chan->sdu_last_frag = skb; |
| 5785 | |
| 5786 | skb = NULL; |
| 5787 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5788 | break; |
| 5789 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5790 | case L2CAP_SAR_CONTINUE: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5791 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5792 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5793 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5794 | append_skb_frag(chan->sdu, skb, |
| 5795 | &chan->sdu_last_frag); |
| 5796 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5797 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5798 | if (chan->sdu->len >= chan->sdu_len) |
| 5799 | break; |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5800 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5801 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5802 | break; |
| 5803 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5804 | case L2CAP_SAR_END: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5805 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5806 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5807 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5808 | append_skb_frag(chan->sdu, skb, |
| 5809 | &chan->sdu_last_frag); |
| 5810 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5811 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5812 | if (chan->sdu->len != chan->sdu_len) |
| 5813 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5814 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5815 | err = chan->ops->recv(chan, chan->sdu); |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5816 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5817 | if (!err) { |
| 5818 | /* Reassembly complete */ |
| 5819 | chan->sdu = NULL; |
| 5820 | chan->sdu_last_frag = NULL; |
| 5821 | chan->sdu_len = 0; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5822 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5823 | break; |
| 5824 | } |
| 5825 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5826 | if (err) { |
| 5827 | kfree_skb(skb); |
| 5828 | kfree_skb(chan->sdu); |
| 5829 | chan->sdu = NULL; |
| 5830 | chan->sdu_last_frag = NULL; |
| 5831 | chan->sdu_len = 0; |
| 5832 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5833 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5834 | return err; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5835 | } |
| 5836 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 5837 | static int l2cap_resegment(struct l2cap_chan *chan) |
| 5838 | { |
| 5839 | /* Placeholder */ |
| 5840 | return 0; |
| 5841 | } |
| 5842 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 5843 | void l2cap_chan_busy(struct l2cap_chan *chan, int busy) |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 5844 | { |
Mat Martineau | 61aa4f5 | 2012-05-17 20:53:40 -0700 | [diff] [blame] | 5845 | u8 event; |
| 5846 | |
| 5847 | if (chan->mode != L2CAP_MODE_ERTM) |
| 5848 | return; |
| 5849 | |
| 5850 | event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR; |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 5851 | l2cap_tx(chan, NULL, NULL, event); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5852 | } |
| 5853 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5854 | static int l2cap_rx_queued_iframes(struct l2cap_chan *chan) |
| 5855 | { |
Mat Martineau | 6383872 | 2012-05-17 20:53:45 -0700 | [diff] [blame] | 5856 | int err = 0; |
| 5857 | /* Pass sequential frames to l2cap_reassemble_sdu() |
| 5858 | * until a gap is encountered. |
| 5859 | */ |
| 5860 | |
| 5861 | BT_DBG("chan %p", chan); |
| 5862 | |
| 5863 | while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5864 | struct sk_buff *skb; |
| 5865 | BT_DBG("Searching for skb with txseq %d (queue len %d)", |
| 5866 | chan->buffer_seq, skb_queue_len(&chan->srej_q)); |
| 5867 | |
| 5868 | skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq); |
| 5869 | |
| 5870 | if (!skb) |
| 5871 | break; |
| 5872 | |
| 5873 | skb_unlink(skb, &chan->srej_q); |
| 5874 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 5875 | err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->control); |
| 5876 | if (err) |
| 5877 | break; |
| 5878 | } |
| 5879 | |
| 5880 | if (skb_queue_empty(&chan->srej_q)) { |
| 5881 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 5882 | l2cap_send_ack(chan); |
| 5883 | } |
| 5884 | |
| 5885 | return err; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5886 | } |
| 5887 | |
| 5888 | static void l2cap_handle_srej(struct l2cap_chan *chan, |
| 5889 | struct l2cap_ctrl *control) |
| 5890 | { |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5891 | struct sk_buff *skb; |
| 5892 | |
| 5893 | BT_DBG("chan %p, control %p", chan, control); |
| 5894 | |
| 5895 | if (control->reqseq == chan->next_tx_seq) { |
| 5896 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5897 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5898 | return; |
| 5899 | } |
| 5900 | |
| 5901 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 5902 | |
| 5903 | if (skb == NULL) { |
| 5904 | BT_DBG("Seq %d not available for retransmission", |
| 5905 | control->reqseq); |
| 5906 | return; |
| 5907 | } |
| 5908 | |
| 5909 | if (chan->max_tx != 0 && bt_cb(skb)->control.retries >= chan->max_tx) { |
| 5910 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5911 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5912 | return; |
| 5913 | } |
| 5914 | |
| 5915 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5916 | |
| 5917 | if (control->poll) { |
| 5918 | l2cap_pass_to_tx(chan, control); |
| 5919 | |
| 5920 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 5921 | l2cap_retransmit(chan, control); |
| 5922 | l2cap_ertm_send(chan); |
| 5923 | |
| 5924 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 5925 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5926 | chan->srej_save_reqseq = control->reqseq; |
| 5927 | } |
| 5928 | } else { |
| 5929 | l2cap_pass_to_tx_fbit(chan, control); |
| 5930 | |
| 5931 | if (control->final) { |
| 5932 | if (chan->srej_save_reqseq != control->reqseq || |
| 5933 | !test_and_clear_bit(CONN_SREJ_ACT, |
| 5934 | &chan->conn_state)) |
| 5935 | l2cap_retransmit(chan, control); |
| 5936 | } else { |
| 5937 | l2cap_retransmit(chan, control); |
| 5938 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 5939 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5940 | chan->srej_save_reqseq = control->reqseq; |
| 5941 | } |
| 5942 | } |
| 5943 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5944 | } |
| 5945 | |
| 5946 | static void l2cap_handle_rej(struct l2cap_chan *chan, |
| 5947 | struct l2cap_ctrl *control) |
| 5948 | { |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5949 | struct sk_buff *skb; |
| 5950 | |
| 5951 | BT_DBG("chan %p, control %p", chan, control); |
| 5952 | |
| 5953 | if (control->reqseq == chan->next_tx_seq) { |
| 5954 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5955 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5956 | return; |
| 5957 | } |
| 5958 | |
| 5959 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 5960 | |
| 5961 | if (chan->max_tx && skb && |
| 5962 | bt_cb(skb)->control.retries >= chan->max_tx) { |
| 5963 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5964 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5965 | return; |
| 5966 | } |
| 5967 | |
| 5968 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5969 | |
| 5970 | l2cap_pass_to_tx(chan, control); |
| 5971 | |
| 5972 | if (control->final) { |
| 5973 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
| 5974 | l2cap_retransmit_all(chan, control); |
| 5975 | } else { |
| 5976 | l2cap_retransmit_all(chan, control); |
| 5977 | l2cap_ertm_send(chan); |
| 5978 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) |
| 5979 | set_bit(CONN_REJ_ACT, &chan->conn_state); |
| 5980 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5981 | } |
| 5982 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5983 | static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq) |
| 5984 | { |
| 5985 | BT_DBG("chan %p, txseq %d", chan, txseq); |
| 5986 | |
| 5987 | BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq, |
| 5988 | chan->expected_tx_seq); |
| 5989 | |
| 5990 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 5991 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5992 | chan->tx_win) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5993 | /* See notes below regarding "double poll" and |
| 5994 | * invalid packets. |
| 5995 | */ |
| 5996 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 5997 | BT_DBG("Invalid/Ignore - after SREJ"); |
| 5998 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 5999 | } else { |
| 6000 | BT_DBG("Invalid - in window after SREJ sent"); |
| 6001 | return L2CAP_TXSEQ_INVALID; |
| 6002 | } |
| 6003 | } |
| 6004 | |
| 6005 | if (chan->srej_list.head == txseq) { |
| 6006 | BT_DBG("Expected SREJ"); |
| 6007 | return L2CAP_TXSEQ_EXPECTED_SREJ; |
| 6008 | } |
| 6009 | |
| 6010 | if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) { |
| 6011 | BT_DBG("Duplicate SREJ - txseq already stored"); |
| 6012 | return L2CAP_TXSEQ_DUPLICATE_SREJ; |
| 6013 | } |
| 6014 | |
| 6015 | if (l2cap_seq_list_contains(&chan->srej_list, txseq)) { |
| 6016 | BT_DBG("Unexpected SREJ - not requested"); |
| 6017 | return L2CAP_TXSEQ_UNEXPECTED_SREJ; |
| 6018 | } |
| 6019 | } |
| 6020 | |
| 6021 | if (chan->expected_tx_seq == txseq) { |
| 6022 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
| 6023 | chan->tx_win) { |
| 6024 | BT_DBG("Invalid - txseq outside tx window"); |
| 6025 | return L2CAP_TXSEQ_INVALID; |
| 6026 | } else { |
| 6027 | BT_DBG("Expected"); |
| 6028 | return L2CAP_TXSEQ_EXPECTED; |
| 6029 | } |
| 6030 | } |
| 6031 | |
| 6032 | if (__seq_offset(chan, txseq, chan->last_acked_seq) < |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6033 | __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6034 | BT_DBG("Duplicate - expected_tx_seq later than txseq"); |
| 6035 | return L2CAP_TXSEQ_DUPLICATE; |
| 6036 | } |
| 6037 | |
| 6038 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) { |
| 6039 | /* A source of invalid packets is a "double poll" condition, |
| 6040 | * where delays cause us to send multiple poll packets. If |
| 6041 | * the remote stack receives and processes both polls, |
| 6042 | * sequence numbers can wrap around in such a way that a |
| 6043 | * resent frame has a sequence number that looks like new data |
| 6044 | * with a sequence gap. This would trigger an erroneous SREJ |
| 6045 | * request. |
| 6046 | * |
| 6047 | * Fortunately, this is impossible with a tx window that's |
| 6048 | * less than half of the maximum sequence number, which allows |
| 6049 | * invalid frames to be safely ignored. |
| 6050 | * |
| 6051 | * With tx window sizes greater than half of the tx window |
| 6052 | * maximum, the frame is invalid and cannot be ignored. This |
| 6053 | * causes a disconnect. |
| 6054 | */ |
| 6055 | |
| 6056 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 6057 | BT_DBG("Invalid/Ignore - txseq outside tx window"); |
| 6058 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 6059 | } else { |
| 6060 | BT_DBG("Invalid - txseq outside tx window"); |
| 6061 | return L2CAP_TXSEQ_INVALID; |
| 6062 | } |
| 6063 | } else { |
| 6064 | BT_DBG("Unexpected - txseq indicates missing frames"); |
| 6065 | return L2CAP_TXSEQ_UNEXPECTED; |
| 6066 | } |
| 6067 | } |
| 6068 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6069 | static int l2cap_rx_state_recv(struct l2cap_chan *chan, |
| 6070 | struct l2cap_ctrl *control, |
| 6071 | struct sk_buff *skb, u8 event) |
| 6072 | { |
| 6073 | int err = 0; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6074 | bool skb_in_use = false; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6075 | |
| 6076 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6077 | event); |
| 6078 | |
| 6079 | switch (event) { |
| 6080 | case L2CAP_EV_RECV_IFRAME: |
| 6081 | switch (l2cap_classify_txseq(chan, control->txseq)) { |
| 6082 | case L2CAP_TXSEQ_EXPECTED: |
| 6083 | l2cap_pass_to_tx(chan, control); |
| 6084 | |
| 6085 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 6086 | BT_DBG("Busy, discarding expected seq %d", |
| 6087 | control->txseq); |
| 6088 | break; |
| 6089 | } |
| 6090 | |
| 6091 | chan->expected_tx_seq = __next_seq(chan, |
| 6092 | control->txseq); |
| 6093 | |
| 6094 | chan->buffer_seq = chan->expected_tx_seq; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6095 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6096 | |
| 6097 | err = l2cap_reassemble_sdu(chan, skb, control); |
| 6098 | if (err) |
| 6099 | break; |
| 6100 | |
| 6101 | if (control->final) { |
| 6102 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 6103 | &chan->conn_state)) { |
| 6104 | control->final = 0; |
| 6105 | l2cap_retransmit_all(chan, control); |
| 6106 | l2cap_ertm_send(chan); |
| 6107 | } |
| 6108 | } |
| 6109 | |
| 6110 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 6111 | l2cap_send_ack(chan); |
| 6112 | break; |
| 6113 | case L2CAP_TXSEQ_UNEXPECTED: |
| 6114 | l2cap_pass_to_tx(chan, control); |
| 6115 | |
| 6116 | /* Can't issue SREJ frames in the local busy state. |
| 6117 | * Drop this frame, it will be seen as missing |
| 6118 | * when local busy is exited. |
| 6119 | */ |
| 6120 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 6121 | BT_DBG("Busy, discarding unexpected seq %d", |
| 6122 | control->txseq); |
| 6123 | break; |
| 6124 | } |
| 6125 | |
| 6126 | /* There was a gap in the sequence, so an SREJ |
| 6127 | * must be sent for each missing frame. The |
| 6128 | * current frame is stored for later use. |
| 6129 | */ |
| 6130 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6131 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6132 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6133 | skb_queue_len(&chan->srej_q)); |
| 6134 | |
| 6135 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 6136 | l2cap_seq_list_clear(&chan->srej_list); |
| 6137 | l2cap_send_srej(chan, control->txseq); |
| 6138 | |
| 6139 | chan->rx_state = L2CAP_RX_STATE_SREJ_SENT; |
| 6140 | break; |
| 6141 | case L2CAP_TXSEQ_DUPLICATE: |
| 6142 | l2cap_pass_to_tx(chan, control); |
| 6143 | break; |
| 6144 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 6145 | break; |
| 6146 | case L2CAP_TXSEQ_INVALID: |
| 6147 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6148 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6149 | break; |
| 6150 | } |
| 6151 | break; |
| 6152 | case L2CAP_EV_RECV_RR: |
| 6153 | l2cap_pass_to_tx(chan, control); |
| 6154 | if (control->final) { |
| 6155 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6156 | |
Mat Martineau | e6a3ee6 | 2012-10-23 15:24:22 -0700 | [diff] [blame] | 6157 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) && |
| 6158 | !__chan_is_moving(chan)) { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6159 | control->final = 0; |
| 6160 | l2cap_retransmit_all(chan, control); |
| 6161 | } |
| 6162 | |
| 6163 | l2cap_ertm_send(chan); |
| 6164 | } else if (control->poll) { |
| 6165 | l2cap_send_i_or_rr_or_rnr(chan); |
| 6166 | } else { |
| 6167 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6168 | &chan->conn_state) && |
| 6169 | chan->unacked_frames) |
| 6170 | __set_retrans_timer(chan); |
| 6171 | |
| 6172 | l2cap_ertm_send(chan); |
| 6173 | } |
| 6174 | break; |
| 6175 | case L2CAP_EV_RECV_RNR: |
| 6176 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6177 | l2cap_pass_to_tx(chan, control); |
| 6178 | if (control && control->poll) { |
| 6179 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6180 | l2cap_send_rr_or_rnr(chan, 0); |
| 6181 | } |
| 6182 | __clear_retrans_timer(chan); |
| 6183 | l2cap_seq_list_clear(&chan->retrans_list); |
| 6184 | break; |
| 6185 | case L2CAP_EV_RECV_REJ: |
| 6186 | l2cap_handle_rej(chan, control); |
| 6187 | break; |
| 6188 | case L2CAP_EV_RECV_SREJ: |
| 6189 | l2cap_handle_srej(chan, control); |
| 6190 | break; |
| 6191 | default: |
| 6192 | break; |
| 6193 | } |
| 6194 | |
| 6195 | if (skb && !skb_in_use) { |
| 6196 | BT_DBG("Freeing %p", skb); |
| 6197 | kfree_skb(skb); |
| 6198 | } |
| 6199 | |
| 6200 | return err; |
| 6201 | } |
| 6202 | |
| 6203 | static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan, |
| 6204 | struct l2cap_ctrl *control, |
| 6205 | struct sk_buff *skb, u8 event) |
| 6206 | { |
| 6207 | int err = 0; |
| 6208 | u16 txseq = control->txseq; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6209 | bool skb_in_use = false; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6210 | |
| 6211 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6212 | event); |
| 6213 | |
| 6214 | switch (event) { |
| 6215 | case L2CAP_EV_RECV_IFRAME: |
| 6216 | switch (l2cap_classify_txseq(chan, txseq)) { |
| 6217 | case L2CAP_TXSEQ_EXPECTED: |
| 6218 | /* Keep frame for reassembly later */ |
| 6219 | l2cap_pass_to_tx(chan, control); |
| 6220 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6221 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6222 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6223 | skb_queue_len(&chan->srej_q)); |
| 6224 | |
| 6225 | chan->expected_tx_seq = __next_seq(chan, txseq); |
| 6226 | break; |
| 6227 | case L2CAP_TXSEQ_EXPECTED_SREJ: |
| 6228 | l2cap_seq_list_pop(&chan->srej_list); |
| 6229 | |
| 6230 | l2cap_pass_to_tx(chan, control); |
| 6231 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6232 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6233 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6234 | skb_queue_len(&chan->srej_q)); |
| 6235 | |
| 6236 | err = l2cap_rx_queued_iframes(chan); |
| 6237 | if (err) |
| 6238 | break; |
| 6239 | |
| 6240 | break; |
| 6241 | case L2CAP_TXSEQ_UNEXPECTED: |
| 6242 | /* Got a frame that can't be reassembled yet. |
| 6243 | * Save it for later, and send SREJs to cover |
| 6244 | * the missing frames. |
| 6245 | */ |
| 6246 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6247 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6248 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6249 | skb_queue_len(&chan->srej_q)); |
| 6250 | |
| 6251 | l2cap_pass_to_tx(chan, control); |
| 6252 | l2cap_send_srej(chan, control->txseq); |
| 6253 | break; |
| 6254 | case L2CAP_TXSEQ_UNEXPECTED_SREJ: |
| 6255 | /* This frame was requested with an SREJ, but |
| 6256 | * some expected retransmitted frames are |
| 6257 | * missing. Request retransmission of missing |
| 6258 | * SREJ'd frames. |
| 6259 | */ |
| 6260 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6261 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6262 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6263 | skb_queue_len(&chan->srej_q)); |
| 6264 | |
| 6265 | l2cap_pass_to_tx(chan, control); |
| 6266 | l2cap_send_srej_list(chan, control->txseq); |
| 6267 | break; |
| 6268 | case L2CAP_TXSEQ_DUPLICATE_SREJ: |
| 6269 | /* We've already queued this frame. Drop this copy. */ |
| 6270 | l2cap_pass_to_tx(chan, control); |
| 6271 | break; |
| 6272 | case L2CAP_TXSEQ_DUPLICATE: |
| 6273 | /* Expecting a later sequence number, so this frame |
| 6274 | * was already received. Ignore it completely. |
| 6275 | */ |
| 6276 | break; |
| 6277 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 6278 | break; |
| 6279 | case L2CAP_TXSEQ_INVALID: |
| 6280 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6281 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6282 | break; |
| 6283 | } |
| 6284 | break; |
| 6285 | case L2CAP_EV_RECV_RR: |
| 6286 | l2cap_pass_to_tx(chan, control); |
| 6287 | if (control->final) { |
| 6288 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6289 | |
| 6290 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 6291 | &chan->conn_state)) { |
| 6292 | control->final = 0; |
| 6293 | l2cap_retransmit_all(chan, control); |
| 6294 | } |
| 6295 | |
| 6296 | l2cap_ertm_send(chan); |
| 6297 | } else if (control->poll) { |
| 6298 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6299 | &chan->conn_state) && |
| 6300 | chan->unacked_frames) { |
| 6301 | __set_retrans_timer(chan); |
| 6302 | } |
| 6303 | |
| 6304 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6305 | l2cap_send_srej_tail(chan); |
| 6306 | } else { |
| 6307 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6308 | &chan->conn_state) && |
| 6309 | chan->unacked_frames) |
| 6310 | __set_retrans_timer(chan); |
| 6311 | |
| 6312 | l2cap_send_ack(chan); |
| 6313 | } |
| 6314 | break; |
| 6315 | case L2CAP_EV_RECV_RNR: |
| 6316 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6317 | l2cap_pass_to_tx(chan, control); |
| 6318 | if (control->poll) { |
| 6319 | l2cap_send_srej_tail(chan); |
| 6320 | } else { |
| 6321 | struct l2cap_ctrl rr_control; |
| 6322 | memset(&rr_control, 0, sizeof(rr_control)); |
| 6323 | rr_control.sframe = 1; |
| 6324 | rr_control.super = L2CAP_SUPER_RR; |
| 6325 | rr_control.reqseq = chan->buffer_seq; |
| 6326 | l2cap_send_sframe(chan, &rr_control); |
| 6327 | } |
| 6328 | |
| 6329 | break; |
| 6330 | case L2CAP_EV_RECV_REJ: |
| 6331 | l2cap_handle_rej(chan, control); |
| 6332 | break; |
| 6333 | case L2CAP_EV_RECV_SREJ: |
| 6334 | l2cap_handle_srej(chan, control); |
| 6335 | break; |
| 6336 | } |
| 6337 | |
| 6338 | if (skb && !skb_in_use) { |
| 6339 | BT_DBG("Freeing %p", skb); |
| 6340 | kfree_skb(skb); |
| 6341 | } |
| 6342 | |
| 6343 | return err; |
| 6344 | } |
| 6345 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6346 | static int l2cap_finish_move(struct l2cap_chan *chan) |
| 6347 | { |
| 6348 | BT_DBG("chan %p", chan); |
| 6349 | |
| 6350 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6351 | |
| 6352 | if (chan->hs_hcon) |
| 6353 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6354 | else |
| 6355 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6356 | |
| 6357 | return l2cap_resegment(chan); |
| 6358 | } |
| 6359 | |
| 6360 | static int l2cap_rx_state_wait_p(struct l2cap_chan *chan, |
| 6361 | struct l2cap_ctrl *control, |
| 6362 | struct sk_buff *skb, u8 event) |
| 6363 | { |
| 6364 | int err; |
| 6365 | |
| 6366 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6367 | event); |
| 6368 | |
| 6369 | if (!control->poll) |
| 6370 | return -EPROTO; |
| 6371 | |
| 6372 | l2cap_process_reqseq(chan, control->reqseq); |
| 6373 | |
| 6374 | if (!skb_queue_empty(&chan->tx_q)) |
| 6375 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6376 | else |
| 6377 | chan->tx_send_head = NULL; |
| 6378 | |
| 6379 | /* Rewind next_tx_seq to the point expected |
| 6380 | * by the receiver. |
| 6381 | */ |
| 6382 | chan->next_tx_seq = control->reqseq; |
| 6383 | chan->unacked_frames = 0; |
| 6384 | |
| 6385 | err = l2cap_finish_move(chan); |
| 6386 | if (err) |
| 6387 | return err; |
| 6388 | |
| 6389 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6390 | l2cap_send_i_or_rr_or_rnr(chan); |
| 6391 | |
| 6392 | if (event == L2CAP_EV_RECV_IFRAME) |
| 6393 | return -EPROTO; |
| 6394 | |
| 6395 | return l2cap_rx_state_recv(chan, control, NULL, event); |
| 6396 | } |
| 6397 | |
| 6398 | static int l2cap_rx_state_wait_f(struct l2cap_chan *chan, |
| 6399 | struct l2cap_ctrl *control, |
| 6400 | struct sk_buff *skb, u8 event) |
| 6401 | { |
| 6402 | int err; |
| 6403 | |
| 6404 | if (!control->final) |
| 6405 | return -EPROTO; |
| 6406 | |
| 6407 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6408 | |
| 6409 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6410 | l2cap_process_reqseq(chan, control->reqseq); |
| 6411 | |
| 6412 | if (!skb_queue_empty(&chan->tx_q)) |
| 6413 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6414 | else |
| 6415 | chan->tx_send_head = NULL; |
| 6416 | |
| 6417 | /* Rewind next_tx_seq to the point expected |
| 6418 | * by the receiver. |
| 6419 | */ |
| 6420 | chan->next_tx_seq = control->reqseq; |
| 6421 | chan->unacked_frames = 0; |
| 6422 | |
| 6423 | if (chan->hs_hcon) |
| 6424 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6425 | else |
| 6426 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6427 | |
| 6428 | err = l2cap_resegment(chan); |
| 6429 | |
| 6430 | if (!err) |
| 6431 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6432 | |
| 6433 | return err; |
| 6434 | } |
| 6435 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6436 | static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 6437 | { |
| 6438 | /* Make sure reqseq is for a packet that has been sent but not acked */ |
| 6439 | u16 unacked; |
| 6440 | |
| 6441 | unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq); |
| 6442 | return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked; |
| 6443 | } |
| 6444 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6445 | static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6446 | struct sk_buff *skb, u8 event) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6447 | { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6448 | int err = 0; |
| 6449 | |
| 6450 | BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan, |
| 6451 | control, skb, event, chan->rx_state); |
| 6452 | |
| 6453 | if (__valid_reqseq(chan, control->reqseq)) { |
| 6454 | switch (chan->rx_state) { |
| 6455 | case L2CAP_RX_STATE_RECV: |
| 6456 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6457 | break; |
| 6458 | case L2CAP_RX_STATE_SREJ_SENT: |
| 6459 | err = l2cap_rx_state_srej_sent(chan, control, skb, |
| 6460 | event); |
| 6461 | break; |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6462 | case L2CAP_RX_STATE_WAIT_P: |
| 6463 | err = l2cap_rx_state_wait_p(chan, control, skb, event); |
| 6464 | break; |
| 6465 | case L2CAP_RX_STATE_WAIT_F: |
| 6466 | err = l2cap_rx_state_wait_f(chan, control, skb, event); |
| 6467 | break; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6468 | default: |
| 6469 | /* shut it down */ |
| 6470 | break; |
| 6471 | } |
| 6472 | } else { |
| 6473 | BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d", |
| 6474 | control->reqseq, chan->next_tx_seq, |
| 6475 | chan->expected_ack_seq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6476 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6477 | } |
| 6478 | |
| 6479 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6480 | } |
| 6481 | |
| 6482 | static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6483 | struct sk_buff *skb) |
| 6484 | { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6485 | int err = 0; |
| 6486 | |
| 6487 | BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb, |
| 6488 | chan->rx_state); |
| 6489 | |
| 6490 | if (l2cap_classify_txseq(chan, control->txseq) == |
| 6491 | L2CAP_TXSEQ_EXPECTED) { |
| 6492 | l2cap_pass_to_tx(chan, control); |
| 6493 | |
| 6494 | BT_DBG("buffer_seq %d->%d", chan->buffer_seq, |
| 6495 | __next_seq(chan, chan->buffer_seq)); |
| 6496 | |
| 6497 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 6498 | |
| 6499 | l2cap_reassemble_sdu(chan, skb, control); |
| 6500 | } else { |
| 6501 | if (chan->sdu) { |
| 6502 | kfree_skb(chan->sdu); |
| 6503 | chan->sdu = NULL; |
| 6504 | } |
| 6505 | chan->sdu_last_frag = NULL; |
| 6506 | chan->sdu_len = 0; |
| 6507 | |
| 6508 | if (skb) { |
| 6509 | BT_DBG("Freeing %p", skb); |
| 6510 | kfree_skb(skb); |
| 6511 | } |
| 6512 | } |
| 6513 | |
| 6514 | chan->last_acked_seq = control->txseq; |
| 6515 | chan->expected_tx_seq = __next_seq(chan, control->txseq); |
| 6516 | |
| 6517 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6518 | } |
| 6519 | |
| 6520 | static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) |
| 6521 | { |
| 6522 | struct l2cap_ctrl *control = &bt_cb(skb)->control; |
| 6523 | u16 len; |
| 6524 | u8 event; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6525 | |
Mat Martineau | b76bbd6 | 2012-04-11 10:48:43 -0700 | [diff] [blame] | 6526 | __unpack_control(chan, skb); |
| 6527 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6528 | len = skb->len; |
| 6529 | |
| 6530 | /* |
| 6531 | * We can just drop the corrupted I-frame here. |
| 6532 | * Receiver will miss it and start proper recovery |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6533 | * procedures and ask for retransmission. |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6534 | */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6535 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6536 | goto drop; |
| 6537 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6538 | if (!control->sframe && control->sar == L2CAP_SAR_START) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6539 | len -= L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6540 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6541 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6542 | len -= L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6543 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6544 | if (len > chan->mps) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6545 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6546 | goto drop; |
| 6547 | } |
| 6548 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6549 | if (!control->sframe) { |
| 6550 | int err; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6551 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6552 | BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d", |
| 6553 | control->sar, control->reqseq, control->final, |
| 6554 | control->txseq); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 6555 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6556 | /* Validate F-bit - F=0 always valid, F=1 only |
| 6557 | * valid in TX WAIT_F |
| 6558 | */ |
| 6559 | if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6560 | goto drop; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6561 | |
| 6562 | if (chan->mode != L2CAP_MODE_STREAMING) { |
| 6563 | event = L2CAP_EV_RECV_IFRAME; |
| 6564 | err = l2cap_rx(chan, control, skb, event); |
| 6565 | } else { |
| 6566 | err = l2cap_stream_rx(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6567 | } |
| 6568 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6569 | if (err) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6570 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6571 | } else { |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6572 | const u8 rx_func_to_event[4] = { |
| 6573 | L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ, |
| 6574 | L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ |
| 6575 | }; |
| 6576 | |
| 6577 | /* Only I-frames are expected in streaming mode */ |
| 6578 | if (chan->mode == L2CAP_MODE_STREAMING) |
| 6579 | goto drop; |
| 6580 | |
| 6581 | BT_DBG("sframe reqseq %d, final %d, poll %d, super %d", |
| 6582 | control->reqseq, control->final, control->poll, |
| 6583 | control->super); |
| 6584 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6585 | if (len != 0) { |
Andrei Emeltchenko | 1bb166e | 2012-11-20 17:16:21 +0200 | [diff] [blame] | 6586 | BT_ERR("Trailing bytes: %d in sframe", len); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6587 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6588 | goto drop; |
| 6589 | } |
| 6590 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6591 | /* Validate F and P bits */ |
| 6592 | if (control->final && (control->poll || |
| 6593 | chan->tx_state != L2CAP_TX_STATE_WAIT_F)) |
| 6594 | goto drop; |
| 6595 | |
| 6596 | event = rx_func_to_event[control->super]; |
| 6597 | if (l2cap_rx(chan, control, skb, event)) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6598 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6599 | } |
| 6600 | |
| 6601 | return 0; |
| 6602 | |
| 6603 | drop: |
| 6604 | kfree_skb(skb); |
| 6605 | return 0; |
| 6606 | } |
| 6607 | |
Johan Hedberg | b1c325c | 2013-12-05 09:43:34 +0200 | [diff] [blame] | 6608 | static void l2cap_chan_le_send_credits(struct l2cap_chan *chan) |
| 6609 | { |
| 6610 | struct l2cap_conn *conn = chan->conn; |
| 6611 | struct l2cap_le_credits pkt; |
| 6612 | u16 return_credits; |
| 6613 | |
| 6614 | /* We return more credits to the sender only after the amount of |
| 6615 | * credits falls below half of the initial amount. |
| 6616 | */ |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 6617 | if (chan->rx_credits >= (le_max_credits + 1) / 2) |
Johan Hedberg | b1c325c | 2013-12-05 09:43:34 +0200 | [diff] [blame] | 6618 | return; |
| 6619 | |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 6620 | return_credits = le_max_credits - chan->rx_credits; |
Johan Hedberg | b1c325c | 2013-12-05 09:43:34 +0200 | [diff] [blame] | 6621 | |
| 6622 | BT_DBG("chan %p returning %u credits to sender", chan, return_credits); |
| 6623 | |
| 6624 | chan->rx_credits += return_credits; |
| 6625 | |
| 6626 | pkt.cid = cpu_to_le16(chan->scid); |
| 6627 | pkt.credits = cpu_to_le16(return_credits); |
| 6628 | |
| 6629 | chan->ident = l2cap_get_ident(conn); |
| 6630 | |
| 6631 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt); |
| 6632 | } |
| 6633 | |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6634 | static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) |
| 6635 | { |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6636 | int err; |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6637 | |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6638 | if (!chan->rx_credits) { |
| 6639 | BT_ERR("No credits to receive LE L2CAP data"); |
Johan Hedberg | dfd9774 | 2014-01-27 15:11:34 -0800 | [diff] [blame] | 6640 | l2cap_send_disconn_req(chan, ECONNRESET); |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6641 | return -ENOBUFS; |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6642 | } |
| 6643 | |
| 6644 | if (chan->imtu < skb->len) { |
| 6645 | BT_ERR("Too big LE L2CAP PDU"); |
| 6646 | return -ENOBUFS; |
| 6647 | } |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6648 | |
| 6649 | chan->rx_credits--; |
| 6650 | BT_DBG("rx_credits %u -> %u", chan->rx_credits + 1, chan->rx_credits); |
| 6651 | |
| 6652 | l2cap_chan_le_send_credits(chan); |
| 6653 | |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6654 | err = 0; |
| 6655 | |
| 6656 | if (!chan->sdu) { |
| 6657 | u16 sdu_len; |
| 6658 | |
| 6659 | sdu_len = get_unaligned_le16(skb->data); |
| 6660 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
| 6661 | |
| 6662 | BT_DBG("Start of new SDU. sdu_len %u skb->len %u imtu %u", |
| 6663 | sdu_len, skb->len, chan->imtu); |
| 6664 | |
| 6665 | if (sdu_len > chan->imtu) { |
| 6666 | BT_ERR("Too big LE L2CAP SDU length received"); |
| 6667 | err = -EMSGSIZE; |
| 6668 | goto failed; |
| 6669 | } |
| 6670 | |
| 6671 | if (skb->len > sdu_len) { |
| 6672 | BT_ERR("Too much LE L2CAP data received"); |
| 6673 | err = -EINVAL; |
| 6674 | goto failed; |
| 6675 | } |
| 6676 | |
| 6677 | if (skb->len == sdu_len) |
| 6678 | return chan->ops->recv(chan, skb); |
| 6679 | |
| 6680 | chan->sdu = skb; |
| 6681 | chan->sdu_len = sdu_len; |
| 6682 | chan->sdu_last_frag = skb; |
| 6683 | |
| 6684 | return 0; |
| 6685 | } |
| 6686 | |
| 6687 | BT_DBG("SDU fragment. chan->sdu->len %u skb->len %u chan->sdu_len %u", |
| 6688 | chan->sdu->len, skb->len, chan->sdu_len); |
| 6689 | |
| 6690 | if (chan->sdu->len + skb->len > chan->sdu_len) { |
| 6691 | BT_ERR("Too much LE L2CAP data received"); |
| 6692 | err = -EINVAL; |
| 6693 | goto failed; |
| 6694 | } |
| 6695 | |
| 6696 | append_skb_frag(chan->sdu, skb, &chan->sdu_last_frag); |
| 6697 | skb = NULL; |
| 6698 | |
| 6699 | if (chan->sdu->len == chan->sdu_len) { |
| 6700 | err = chan->ops->recv(chan, chan->sdu); |
| 6701 | if (!err) { |
| 6702 | chan->sdu = NULL; |
| 6703 | chan->sdu_last_frag = NULL; |
| 6704 | chan->sdu_len = 0; |
| 6705 | } |
| 6706 | } |
| 6707 | |
| 6708 | failed: |
| 6709 | if (err) { |
| 6710 | kfree_skb(skb); |
| 6711 | kfree_skb(chan->sdu); |
| 6712 | chan->sdu = NULL; |
| 6713 | chan->sdu_last_frag = NULL; |
| 6714 | chan->sdu_len = 0; |
| 6715 | } |
| 6716 | |
| 6717 | /* We can't return an error here since we took care of the skb |
| 6718 | * freeing internally. An error return would cause the caller to |
| 6719 | * do a double-free of the skb. |
| 6720 | */ |
| 6721 | return 0; |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6722 | } |
| 6723 | |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6724 | static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid, |
| 6725 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6726 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6727 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6728 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 6729 | chan = l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6730 | if (!chan) { |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6731 | if (cid == L2CAP_CID_A2MP) { |
| 6732 | chan = a2mp_channel_create(conn, skb); |
| 6733 | if (!chan) { |
| 6734 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6735 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6736 | } |
| 6737 | |
| 6738 | l2cap_chan_lock(chan); |
| 6739 | } else { |
| 6740 | BT_DBG("unknown cid 0x%4.4x", cid); |
| 6741 | /* Drop packet and return */ |
| 6742 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6743 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6745 | } |
| 6746 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 6747 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6748 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6749 | if (chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6750 | goto drop; |
| 6751 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6752 | switch (chan->mode) { |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 6753 | case L2CAP_MODE_LE_FLOWCTL: |
Johan Hedberg | fad5fc89 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6754 | if (l2cap_le_data_rcv(chan, skb) < 0) |
| 6755 | goto drop; |
| 6756 | |
| 6757 | goto done; |
| 6758 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6759 | case L2CAP_MODE_BASIC: |
| 6760 | /* If socket recv buffers overflows we drop data here |
| 6761 | * which is *bad* because L2CAP has to be reliable. |
| 6762 | * But we don't have any other choice. L2CAP doesn't |
| 6763 | * provide flow control mechanism. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6764 | |
Szymon Janc | 2c96e03 | 2014-02-18 20:48:34 +0100 | [diff] [blame] | 6765 | if (chan->imtu < skb->len) { |
| 6766 | BT_ERR("Dropping L2CAP data: receive buffer overflow"); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6767 | goto drop; |
Szymon Janc | 2c96e03 | 2014-02-18 20:48:34 +0100 | [diff] [blame] | 6768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6769 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6770 | if (!chan->ops->recv(chan, skb)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6771 | goto done; |
| 6772 | break; |
| 6773 | |
| 6774 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6775 | case L2CAP_MODE_STREAMING: |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6776 | l2cap_data_rcv(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6777 | goto done; |
| 6778 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6779 | default: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6780 | 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] | 6781 | break; |
| 6782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6783 | |
| 6784 | drop: |
| 6785 | kfree_skb(skb); |
| 6786 | |
| 6787 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6788 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6789 | } |
| 6790 | |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6791 | static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, |
| 6792 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6793 | { |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6794 | struct hci_conn *hcon = conn->hcon; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6795 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6796 | |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6797 | if (hcon->type != ACL_LINK) |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6798 | goto free_skb; |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6799 | |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 6800 | chan = l2cap_global_chan_by_psm(0, psm, &hcon->src, &hcon->dst, |
| 6801 | ACL_LINK); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6802 | if (!chan) |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6803 | goto free_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6804 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 6805 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6806 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6807 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6808 | goto drop; |
| 6809 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 6810 | if (chan->imtu < skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6811 | goto drop; |
| 6812 | |
Marcel Holtmann | 2edf870 | 2013-10-13 12:55:29 -0700 | [diff] [blame] | 6813 | /* Store remote BD_ADDR and PSM for msg_name */ |
Marcel Holtmann | 06ae331 | 2013-10-18 03:43:00 -0700 | [diff] [blame] | 6814 | bacpy(&bt_cb(skb)->bdaddr, &hcon->dst); |
Marcel Holtmann | 2edf870 | 2013-10-13 12:55:29 -0700 | [diff] [blame] | 6815 | bt_cb(skb)->psm = psm; |
| 6816 | |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6817 | if (!chan->ops->recv(chan, skb)) { |
| 6818 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6819 | return; |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6820 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6821 | |
| 6822 | drop: |
Johan Hedberg | a24cce1 | 2014-08-07 22:56:42 +0300 | [diff] [blame] | 6823 | l2cap_chan_put(chan); |
| 6824 | free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6825 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6826 | } |
| 6827 | |
| 6828 | static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) |
| 6829 | { |
| 6830 | struct l2cap_hdr *lh = (void *) skb->data; |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6831 | struct hci_conn *hcon = conn->hcon; |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 6832 | u16 cid, len; |
| 6833 | __le16 psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6834 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6835 | if (hcon->state != BT_CONNECTED) { |
| 6836 | BT_DBG("queueing pending rx skb"); |
| 6837 | skb_queue_tail(&conn->pending_rx, skb); |
| 6838 | return; |
| 6839 | } |
| 6840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6841 | skb_pull(skb, L2CAP_HDR_SIZE); |
| 6842 | cid = __le16_to_cpu(lh->cid); |
| 6843 | len = __le16_to_cpu(lh->len); |
| 6844 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6845 | if (len != skb->len) { |
| 6846 | kfree_skb(skb); |
| 6847 | return; |
| 6848 | } |
| 6849 | |
Johan Hedberg | 9e1d7e1 | 2014-07-06 11:03:36 +0300 | [diff] [blame] | 6850 | /* Since we can't actively block incoming LE connections we must |
| 6851 | * at least ensure that we ignore incoming data from them. |
| 6852 | */ |
| 6853 | if (hcon->type == LE_LINK && |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 6854 | hci_bdaddr_list_lookup(&hcon->hdev->blacklist, &hcon->dst, |
| 6855 | bdaddr_type(hcon, hcon->dst_type))) { |
Johan Hedberg | e493150 | 2014-07-02 09:36:21 +0300 | [diff] [blame] | 6856 | kfree_skb(skb); |
| 6857 | return; |
| 6858 | } |
| 6859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6860 | BT_DBG("len %d, cid 0x%4.4x", len, cid); |
| 6861 | |
| 6862 | switch (cid) { |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6863 | case L2CAP_CID_SIGNALING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6864 | l2cap_sig_channel(conn, skb); |
| 6865 | break; |
| 6866 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6867 | case L2CAP_CID_CONN_LESS: |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 6868 | psm = get_unaligned((__le16 *) skb->data); |
Andrei Emeltchenko | 0181a70 | 2012-05-29 10:04:05 +0300 | [diff] [blame] | 6869 | skb_pull(skb, L2CAP_PSMLEN_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6870 | l2cap_conless_channel(conn, psm, skb); |
| 6871 | break; |
| 6872 | |
Marcel Holtmann | a287762 | 2013-10-02 23:46:54 -0700 | [diff] [blame] | 6873 | case L2CAP_CID_LE_SIGNALING: |
| 6874 | l2cap_le_sig_channel(conn, skb); |
| 6875 | break; |
| 6876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6877 | default: |
| 6878 | l2cap_data_channel(conn, cid, skb); |
| 6879 | break; |
| 6880 | } |
| 6881 | } |
| 6882 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6883 | static void process_pending_rx(struct work_struct *work) |
| 6884 | { |
| 6885 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
| 6886 | pending_rx_work); |
| 6887 | struct sk_buff *skb; |
| 6888 | |
| 6889 | BT_DBG(""); |
| 6890 | |
| 6891 | while ((skb = skb_dequeue(&conn->pending_rx))) |
| 6892 | l2cap_recv_frame(conn, skb); |
| 6893 | } |
| 6894 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6895 | static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) |
| 6896 | { |
| 6897 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 6898 | struct hci_chan *hchan; |
| 6899 | |
| 6900 | if (conn) |
| 6901 | return conn; |
| 6902 | |
| 6903 | hchan = hci_chan_create(hcon); |
| 6904 | if (!hchan) |
| 6905 | return NULL; |
| 6906 | |
Johan Hedberg | 27f70f3 | 2014-07-21 10:50:06 +0300 | [diff] [blame] | 6907 | conn = kzalloc(sizeof(*conn), GFP_KERNEL); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6908 | if (!conn) { |
| 6909 | hci_chan_del(hchan); |
| 6910 | return NULL; |
| 6911 | } |
| 6912 | |
| 6913 | kref_init(&conn->ref); |
| 6914 | hcon->l2cap_data = conn; |
Johan Hedberg | 51bb8457 | 2014-08-15 21:06:57 +0300 | [diff] [blame] | 6915 | conn->hcon = hci_conn_get(hcon); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6916 | conn->hchan = hchan; |
| 6917 | |
| 6918 | BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); |
| 6919 | |
| 6920 | switch (hcon->type) { |
| 6921 | case LE_LINK: |
| 6922 | if (hcon->hdev->le_mtu) { |
| 6923 | conn->mtu = hcon->hdev->le_mtu; |
| 6924 | break; |
| 6925 | } |
| 6926 | /* fall through */ |
| 6927 | default: |
| 6928 | conn->mtu = hcon->hdev->acl_mtu; |
| 6929 | break; |
| 6930 | } |
| 6931 | |
| 6932 | conn->feat_mask = 0; |
| 6933 | |
| 6934 | if (hcon->type == ACL_LINK) |
| 6935 | conn->hs_enabled = test_bit(HCI_HS_ENABLED, |
| 6936 | &hcon->hdev->dev_flags); |
| 6937 | |
Marcel Holtmann | 5a54e7c | 2014-07-13 20:50:15 +0200 | [diff] [blame] | 6938 | mutex_init(&conn->ident_lock); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6939 | mutex_init(&conn->chan_lock); |
| 6940 | |
| 6941 | INIT_LIST_HEAD(&conn->chan_l); |
| 6942 | INIT_LIST_HEAD(&conn->users); |
| 6943 | |
Johan Hedberg | 276d807 | 2014-08-11 22:06:41 +0300 | [diff] [blame] | 6944 | INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6945 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6946 | skb_queue_head_init(&conn->pending_rx); |
| 6947 | INIT_WORK(&conn->pending_rx_work, process_pending_rx); |
Johan Hedberg | f3d82d0 | 2014-09-05 22:19:50 +0300 | [diff] [blame] | 6948 | INIT_WORK(&conn->id_addr_update_work, l2cap_conn_update_id_addr); |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6949 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6950 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
| 6951 | |
| 6952 | return conn; |
| 6953 | } |
| 6954 | |
| 6955 | static bool is_valid_psm(u16 psm, u8 dst_type) { |
| 6956 | if (!psm) |
| 6957 | return false; |
| 6958 | |
| 6959 | if (bdaddr_type_is_le(dst_type)) |
| 6960 | return (psm <= 0x00ff); |
| 6961 | |
| 6962 | /* PSM must be odd and lsb of upper byte must be 0 */ |
| 6963 | return ((psm & 0x0101) == 0x0001); |
| 6964 | } |
| 6965 | |
| 6966 | int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, |
| 6967 | bdaddr_t *dst, u8 dst_type) |
| 6968 | { |
| 6969 | struct l2cap_conn *conn; |
| 6970 | struct hci_conn *hcon; |
| 6971 | struct hci_dev *hdev; |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6972 | int err; |
| 6973 | |
| 6974 | BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", &chan->src, dst, |
| 6975 | dst_type, __le16_to_cpu(psm)); |
| 6976 | |
| 6977 | hdev = hci_get_route(dst, &chan->src); |
| 6978 | if (!hdev) |
| 6979 | return -EHOSTUNREACH; |
| 6980 | |
| 6981 | hci_dev_lock(hdev); |
| 6982 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6983 | if (!is_valid_psm(__le16_to_cpu(psm), dst_type) && !cid && |
| 6984 | chan->chan_type != L2CAP_CHAN_RAW) { |
| 6985 | err = -EINVAL; |
| 6986 | goto done; |
| 6987 | } |
| 6988 | |
Johan Hedberg | 21626e6 | 2014-01-24 10:35:41 +0200 | [diff] [blame] | 6989 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !psm) { |
| 6990 | err = -EINVAL; |
| 6991 | goto done; |
| 6992 | } |
| 6993 | |
| 6994 | if (chan->chan_type == L2CAP_CHAN_FIXED && !cid) { |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6995 | err = -EINVAL; |
| 6996 | goto done; |
| 6997 | } |
| 6998 | |
| 6999 | switch (chan->mode) { |
| 7000 | case L2CAP_MODE_BASIC: |
| 7001 | break; |
| 7002 | case L2CAP_MODE_LE_FLOWCTL: |
| 7003 | l2cap_le_flowctl_init(chan); |
| 7004 | break; |
| 7005 | case L2CAP_MODE_ERTM: |
| 7006 | case L2CAP_MODE_STREAMING: |
| 7007 | if (!disable_ertm) |
| 7008 | break; |
| 7009 | /* fall through */ |
| 7010 | default: |
Johan Hedberg | beb19e4 | 2014-07-18 11:15:26 +0300 | [diff] [blame] | 7011 | err = -EOPNOTSUPP; |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7012 | goto done; |
| 7013 | } |
| 7014 | |
| 7015 | switch (chan->state) { |
| 7016 | case BT_CONNECT: |
| 7017 | case BT_CONNECT2: |
| 7018 | case BT_CONFIG: |
| 7019 | /* Already connecting */ |
| 7020 | err = 0; |
| 7021 | goto done; |
| 7022 | |
| 7023 | case BT_CONNECTED: |
| 7024 | /* Already connected */ |
| 7025 | err = -EISCONN; |
| 7026 | goto done; |
| 7027 | |
| 7028 | case BT_OPEN: |
| 7029 | case BT_BOUND: |
| 7030 | /* Can connect */ |
| 7031 | break; |
| 7032 | |
| 7033 | default: |
| 7034 | err = -EBADFD; |
| 7035 | goto done; |
| 7036 | } |
| 7037 | |
| 7038 | /* Set destination address and psm */ |
| 7039 | bacpy(&chan->dst, dst); |
| 7040 | chan->dst_type = dst_type; |
| 7041 | |
| 7042 | chan->psm = psm; |
| 7043 | chan->dcid = cid; |
| 7044 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7045 | if (bdaddr_type_is_le(dst_type)) { |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 7046 | u8 role; |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 7047 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7048 | /* Convert from L2CAP channel address type to HCI address type |
| 7049 | */ |
| 7050 | if (dst_type == BDADDR_LE_PUBLIC) |
| 7051 | dst_type = ADDR_LE_DEV_PUBLIC; |
| 7052 | else |
| 7053 | dst_type = ADDR_LE_DEV_RANDOM; |
| 7054 | |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 7055 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 7056 | role = HCI_ROLE_SLAVE; |
| 7057 | else |
| 7058 | role = HCI_ROLE_MASTER; |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 7059 | |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 7060 | hcon = hci_connect_le(hdev, dst, dst_type, chan->sec_level, |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 7061 | HCI_LE_CONN_TIMEOUT, role); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7062 | } else { |
Johan Hedberg | d93375a | 2014-07-07 15:02:27 +0300 | [diff] [blame] | 7063 | u8 auth_type = l2cap_get_auth_type(chan); |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 7064 | hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7065 | } |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7066 | |
| 7067 | if (IS_ERR(hcon)) { |
| 7068 | err = PTR_ERR(hcon); |
| 7069 | goto done; |
| 7070 | } |
| 7071 | |
| 7072 | conn = l2cap_conn_add(hcon); |
| 7073 | if (!conn) { |
| 7074 | hci_conn_drop(hcon); |
| 7075 | err = -ENOMEM; |
| 7076 | goto done; |
| 7077 | } |
| 7078 | |
Johan Hedberg | 02e246a | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7079 | mutex_lock(&conn->chan_lock); |
| 7080 | l2cap_chan_lock(chan); |
| 7081 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7082 | if (cid && __l2cap_get_chan_by_dcid(conn, cid)) { |
| 7083 | hci_conn_drop(hcon); |
| 7084 | err = -EBUSY; |
Johan Hedberg | 02e246a | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7085 | goto chan_unlock; |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7086 | } |
| 7087 | |
| 7088 | /* Update source addr of the socket */ |
| 7089 | bacpy(&chan->src, &hcon->src); |
| 7090 | chan->src_type = bdaddr_type(hcon, hcon->src_type); |
| 7091 | |
Johan Hedberg | 02e246a | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7092 | __l2cap_chan_add(conn, chan); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7093 | |
| 7094 | /* l2cap_chan_add takes its own ref so we can drop this one */ |
| 7095 | hci_conn_drop(hcon); |
| 7096 | |
| 7097 | l2cap_state_change(chan, BT_CONNECT); |
| 7098 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
| 7099 | |
Johan Hedberg | 61202e4 | 2014-01-28 15:16:48 -0800 | [diff] [blame] | 7100 | /* Release chan->sport so that it can be reused by other |
| 7101 | * sockets (as it's only used for listening sockets). |
| 7102 | */ |
| 7103 | write_lock(&chan_list_lock); |
| 7104 | chan->sport = 0; |
| 7105 | write_unlock(&chan_list_lock); |
| 7106 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7107 | if (hcon->state == BT_CONNECTED) { |
| 7108 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
| 7109 | __clear_chan_timer(chan); |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 7110 | if (l2cap_chan_check_security(chan, true)) |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7111 | l2cap_state_change(chan, BT_CONNECTED); |
| 7112 | } else |
| 7113 | l2cap_do_start(chan); |
| 7114 | } |
| 7115 | |
| 7116 | err = 0; |
| 7117 | |
Johan Hedberg | 02e246a | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7118 | chan_unlock: |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7119 | l2cap_chan_unlock(chan); |
Johan Hedberg | 02e246a | 2014-10-02 10:16:22 +0300 | [diff] [blame] | 7120 | mutex_unlock(&conn->chan_lock); |
| 7121 | done: |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7122 | hci_dev_unlock(hdev); |
| 7123 | hci_dev_put(hdev); |
| 7124 | return err; |
| 7125 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 7126 | EXPORT_SYMBOL_GPL(l2cap_chan_connect); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7128 | /* ---- L2CAP interface with lower layer (HCI) ---- */ |
| 7129 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7130 | int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7131 | { |
| 7132 | int exact = 0, lm1 = 0, lm2 = 0; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7133 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7134 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 7135 | BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7136 | |
| 7137 | /* Find listening sockets and check their link_mode */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7138 | read_lock(&chan_list_lock); |
| 7139 | list_for_each_entry(c, &chan_list, global_l) { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 7140 | if (c->state != BT_LISTEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7141 | continue; |
| 7142 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 7143 | if (!bacmp(&c->src, &hdev->bdaddr)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7144 | lm1 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 7145 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7146 | lm1 |= HCI_LM_MASTER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7147 | exact++; |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 7148 | } else if (!bacmp(&c->src, BDADDR_ANY)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7149 | lm2 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 7150 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7151 | lm2 |= HCI_LM_MASTER; |
| 7152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7153 | } |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7154 | read_unlock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7155 | |
| 7156 | return exact ? lm1 : lm2; |
| 7157 | } |
| 7158 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7159 | /* Find the next fixed channel in BT_LISTEN state, continue iteration |
| 7160 | * from an existing channel in the list or from the beginning of the |
| 7161 | * global list (by passing NULL as first parameter). |
| 7162 | */ |
| 7163 | static struct l2cap_chan *l2cap_global_fixed_chan(struct l2cap_chan *c, |
Johan Hedberg | 54a1b62 | 2014-08-07 22:56:48 +0300 | [diff] [blame] | 7164 | bdaddr_t *src, u8 link_type) |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7165 | { |
| 7166 | read_lock(&chan_list_lock); |
| 7167 | |
| 7168 | if (c) |
| 7169 | c = list_next_entry(c, global_l); |
| 7170 | else |
| 7171 | c = list_entry(chan_list.next, typeof(*c), global_l); |
| 7172 | |
| 7173 | list_for_each_entry_from(c, &chan_list, global_l) { |
| 7174 | if (c->chan_type != L2CAP_CHAN_FIXED) |
| 7175 | continue; |
| 7176 | if (c->state != BT_LISTEN) |
| 7177 | continue; |
| 7178 | if (bacmp(&c->src, src) && bacmp(&c->src, BDADDR_ANY)) |
| 7179 | continue; |
Johan Hedberg | 54a1b62 | 2014-08-07 22:56:48 +0300 | [diff] [blame] | 7180 | if (link_type == ACL_LINK && c->src_type != BDADDR_BREDR) |
| 7181 | continue; |
| 7182 | if (link_type == LE_LINK && c->src_type == BDADDR_BREDR) |
| 7183 | continue; |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7184 | |
| 7185 | l2cap_chan_hold(c); |
| 7186 | read_unlock(&chan_list_lock); |
| 7187 | return c; |
| 7188 | } |
| 7189 | |
| 7190 | read_unlock(&chan_list_lock); |
| 7191 | |
| 7192 | return NULL; |
| 7193 | } |
| 7194 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 7195 | void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7196 | { |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7197 | struct hci_dev *hdev = hcon->hdev; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7198 | struct l2cap_conn *conn; |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7199 | struct l2cap_chan *pchan; |
| 7200 | u8 dst_type; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7201 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 7202 | BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7203 | |
Johan Hedberg | dc0f508 | 2014-08-07 22:56:46 +0300 | [diff] [blame] | 7204 | if (status) { |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 7205 | l2cap_conn_del(hcon, bt_to_errno(status)); |
Johan Hedberg | dc0f508 | 2014-08-07 22:56:46 +0300 | [diff] [blame] | 7206 | return; |
Andrei Emeltchenko | ba6fc31 | 2012-10-31 15:46:26 +0200 | [diff] [blame] | 7207 | } |
Johan Hedberg | dc0f508 | 2014-08-07 22:56:46 +0300 | [diff] [blame] | 7208 | |
| 7209 | conn = l2cap_conn_add(hcon); |
| 7210 | if (!conn) |
| 7211 | return; |
| 7212 | |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7213 | dst_type = bdaddr_type(hcon, hcon->dst_type); |
| 7214 | |
| 7215 | /* If device is blocked, do not create channels for it */ |
| 7216 | if (hci_bdaddr_list_lookup(&hdev->blacklist, &hcon->dst, dst_type)) |
| 7217 | return; |
| 7218 | |
| 7219 | /* Find fixed channels and notify them of the new connection. We |
| 7220 | * use multiple individual lookups, continuing each time where |
| 7221 | * we left off, because the list lock would prevent calling the |
| 7222 | * potentially sleeping l2cap_chan_lock() function. |
| 7223 | */ |
Johan Hedberg | 54a1b62 | 2014-08-07 22:56:48 +0300 | [diff] [blame] | 7224 | pchan = l2cap_global_fixed_chan(NULL, &hdev->bdaddr, hcon->type); |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7225 | while (pchan) { |
| 7226 | struct l2cap_chan *chan, *next; |
| 7227 | |
| 7228 | /* Client fixed channels should override server ones */ |
| 7229 | if (__l2cap_get_chan_by_dcid(conn, pchan->scid)) |
| 7230 | goto next; |
| 7231 | |
| 7232 | l2cap_chan_lock(pchan); |
| 7233 | chan = pchan->ops->new_connection(pchan); |
| 7234 | if (chan) { |
| 7235 | bacpy(&chan->src, &hcon->src); |
| 7236 | bacpy(&chan->dst, &hcon->dst); |
| 7237 | chan->src_type = bdaddr_type(hcon, hcon->src_type); |
| 7238 | chan->dst_type = dst_type; |
| 7239 | |
| 7240 | __l2cap_chan_add(conn, chan); |
| 7241 | } |
| 7242 | |
| 7243 | l2cap_chan_unlock(pchan); |
| 7244 | next: |
Johan Hedberg | 54a1b62 | 2014-08-07 22:56:48 +0300 | [diff] [blame] | 7245 | next = l2cap_global_fixed_chan(pchan, &hdev->bdaddr, |
| 7246 | hcon->type); |
Johan Hedberg | e760ec1 | 2014-08-07 22:56:47 +0300 | [diff] [blame] | 7247 | l2cap_chan_put(pchan); |
| 7248 | pchan = next; |
| 7249 | } |
| 7250 | |
Johan Hedberg | dc0f508 | 2014-08-07 22:56:46 +0300 | [diff] [blame] | 7251 | l2cap_conn_ready(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7252 | } |
| 7253 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7254 | int l2cap_disconn_ind(struct hci_conn *hcon) |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 7255 | { |
| 7256 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 7257 | |
| 7258 | BT_DBG("hcon %p", hcon); |
| 7259 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7260 | if (!conn) |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 7261 | return HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 7262 | return conn->disc_reason; |
| 7263 | } |
| 7264 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 7265 | void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7266 | { |
| 7267 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 7268 | |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 7269 | l2cap_conn_del(hcon, bt_to_errno(reason)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7270 | } |
| 7271 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7272 | static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7273 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 7274 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
Marcel Holtmann | 255c760 | 2009-02-04 21:07:19 +0100 | [diff] [blame] | 7275 | return; |
| 7276 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7277 | if (encrypt == 0x00) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7278 | if (chan->sec_level == BT_SECURITY_MEDIUM) { |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 7279 | __set_chan_timer(chan, L2CAP_ENC_TIMEOUT); |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 7280 | } else if (chan->sec_level == BT_SECURITY_HIGH || |
| 7281 | chan->sec_level == BT_SECURITY_FIPS) |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 7282 | l2cap_chan_close(chan, ECONNREFUSED); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7283 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7284 | if (chan->sec_level == BT_SECURITY_MEDIUM) |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 7285 | __clear_chan_timer(chan); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7286 | } |
| 7287 | } |
| 7288 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7289 | int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7290 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 7291 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 7292 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7293 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7294 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7295 | return 0; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7296 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 7297 | 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] | 7298 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 7299 | mutex_lock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7300 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 7301 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7302 | l2cap_chan_lock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7303 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 7304 | BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, |
| 7305 | state_to_string(chan->state)); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 7306 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 7307 | if (chan->scid == L2CAP_CID_A2MP) { |
Andrei Emeltchenko | 78eb2f9 | 2012-07-19 17:03:47 +0300 | [diff] [blame] | 7308 | l2cap_chan_unlock(chan); |
| 7309 | continue; |
| 7310 | } |
| 7311 | |
Johan Hedberg | 191eb39 | 2014-08-07 22:56:45 +0300 | [diff] [blame] | 7312 | if (!status && encrypt) |
| 7313 | chan->sec_level = hcon->sec_level; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 7314 | |
Andrei Emeltchenko | 96eff46 | 2012-11-15 18:14:53 +0200 | [diff] [blame] | 7315 | if (!__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7316 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 7317 | continue; |
| 7318 | } |
| 7319 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 7320 | if (!status && (chan->state == BT_CONNECTED || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7321 | chan->state == BT_CONFIG)) { |
Marcel Holtmann | d97c899 | 2013-10-14 02:53:54 -0700 | [diff] [blame] | 7322 | chan->ops->resume(chan); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7323 | l2cap_check_encryption(chan, encrypt); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7324 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 9719f8a | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 7325 | continue; |
| 7326 | } |
| 7327 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 7328 | if (chan->state == BT_CONNECT) { |
Johan Hedberg | 6d3c15d | 2013-12-02 22:13:24 +0200 | [diff] [blame] | 7329 | if (!status) |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 7330 | l2cap_start_connection(chan); |
Johan Hedberg | 6d3c15d | 2013-12-02 22:13:24 +0200 | [diff] [blame] | 7331 | else |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 7332 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 7333 | } else if (chan->state == BT_CONNECT2) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7334 | struct l2cap_conn_rsp rsp; |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7335 | __u16 res, stat; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7336 | |
| 7337 | if (!status) { |
Marcel Holtmann | bdc2578 | 2013-10-14 02:45:34 -0700 | [diff] [blame] | 7338 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7339 | res = L2CAP_CR_PEND; |
| 7340 | stat = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 7341 | chan->ops->defer(chan); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7342 | } else { |
Gustavo Padovan | acdcabf | 2013-10-21 14:21:39 -0200 | [diff] [blame] | 7343 | l2cap_state_change(chan, BT_CONFIG); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7344 | res = L2CAP_CR_SUCCESS; |
| 7345 | stat = L2CAP_CS_NO_INFO; |
| 7346 | } |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7347 | } else { |
Gustavo Padovan | acdcabf | 2013-10-21 14:21:39 -0200 | [diff] [blame] | 7348 | l2cap_state_change(chan, BT_DISCONN); |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 7349 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7350 | res = L2CAP_CR_SEC_BLOCK; |
| 7351 | stat = L2CAP_CS_NO_INFO; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7352 | } |
| 7353 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 7354 | rsp.scid = cpu_to_le16(chan->dcid); |
| 7355 | rsp.dcid = cpu_to_le16(chan->scid); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7356 | rsp.result = cpu_to_le16(res); |
| 7357 | rsp.status = cpu_to_le16(stat); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 7358 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7359 | sizeof(rsp), &rsp); |
Mat Martineau | 2d36935 | 2012-05-23 14:59:30 -0700 | [diff] [blame] | 7360 | |
| 7361 | if (!test_bit(CONF_REQ_SENT, &chan->conf_state) && |
| 7362 | res == L2CAP_CR_SUCCESS) { |
| 7363 | char buf[128]; |
| 7364 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 7365 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 7366 | L2CAP_CONF_REQ, |
| 7367 | l2cap_build_conf_req(chan, buf), |
| 7368 | buf); |
| 7369 | chan->num_conf_req++; |
| 7370 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7371 | } |
| 7372 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7373 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7374 | } |
| 7375 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 7376 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7378 | return 0; |
| 7379 | } |
| 7380 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7381 | int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7382 | { |
| 7383 | struct l2cap_conn *conn = hcon->l2cap_data; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7384 | struct l2cap_hdr *hdr; |
| 7385 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7386 | |
Andrei Emeltchenko | 1d13a25 | 2012-10-15 11:58:41 +0300 | [diff] [blame] | 7387 | /* For AMP controller do not create l2cap conn */ |
| 7388 | if (!conn && hcon->hdev->dev_type != HCI_BREDR) |
| 7389 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7390 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 7391 | if (!conn) |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 7392 | conn = l2cap_conn_add(hcon); |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 7393 | |
| 7394 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7395 | goto drop; |
| 7396 | |
| 7397 | BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); |
| 7398 | |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7399 | switch (flags) { |
| 7400 | case ACL_START: |
| 7401 | case ACL_START_NO_FLUSH: |
| 7402 | case ACL_COMPLETE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7403 | if (conn->rx_len) { |
| 7404 | BT_ERR("Unexpected start frame (len %d)", skb->len); |
| 7405 | kfree_skb(conn->rx_skb); |
| 7406 | conn->rx_skb = NULL; |
| 7407 | conn->rx_len = 0; |
| 7408 | l2cap_conn_unreliable(conn, ECOMM); |
| 7409 | } |
| 7410 | |
Andrei Emeltchenko | aae7fe2 | 2010-09-15 14:28:43 +0300 | [diff] [blame] | 7411 | /* Start fragment always begin with Basic L2CAP header */ |
| 7412 | if (skb->len < L2CAP_HDR_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7413 | BT_ERR("Frame is too short (len %d)", skb->len); |
| 7414 | l2cap_conn_unreliable(conn, ECOMM); |
| 7415 | goto drop; |
| 7416 | } |
| 7417 | |
| 7418 | hdr = (struct l2cap_hdr *) skb->data; |
| 7419 | len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; |
| 7420 | |
| 7421 | if (len == skb->len) { |
| 7422 | /* Complete frame received */ |
| 7423 | l2cap_recv_frame(conn, skb); |
| 7424 | return 0; |
| 7425 | } |
| 7426 | |
| 7427 | BT_DBG("Start: total len %d, frag len %d", len, skb->len); |
| 7428 | |
| 7429 | if (skb->len > len) { |
| 7430 | BT_ERR("Frame is too long (len %d, expected len %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7431 | skb->len, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7432 | l2cap_conn_unreliable(conn, ECOMM); |
| 7433 | goto drop; |
| 7434 | } |
| 7435 | |
| 7436 | /* Allocate skb for the complete frame (with header) */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 7437 | conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 7438 | if (!conn->rx_skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7439 | goto drop; |
| 7440 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 7441 | 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] | 7442 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7443 | conn->rx_len = len - skb->len; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7444 | break; |
| 7445 | |
| 7446 | case ACL_CONT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7447 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
| 7448 | |
| 7449 | if (!conn->rx_len) { |
| 7450 | BT_ERR("Unexpected continuation frame (len %d)", skb->len); |
| 7451 | l2cap_conn_unreliable(conn, ECOMM); |
| 7452 | goto drop; |
| 7453 | } |
| 7454 | |
| 7455 | if (skb->len > conn->rx_len) { |
| 7456 | BT_ERR("Fragment is too long (len %d, expected %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7457 | skb->len, conn->rx_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7458 | kfree_skb(conn->rx_skb); |
| 7459 | conn->rx_skb = NULL; |
| 7460 | conn->rx_len = 0; |
| 7461 | l2cap_conn_unreliable(conn, ECOMM); |
| 7462 | goto drop; |
| 7463 | } |
| 7464 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 7465 | 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] | 7466 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7467 | conn->rx_len -= skb->len; |
| 7468 | |
| 7469 | if (!conn->rx_len) { |
Johan Hedberg | c4e5baf | 2013-10-10 13:33:37 +0200 | [diff] [blame] | 7470 | /* Complete frame received. l2cap_recv_frame |
| 7471 | * takes ownership of the skb so set the global |
| 7472 | * rx_skb pointer to NULL first. |
| 7473 | */ |
| 7474 | struct sk_buff *rx_skb = conn->rx_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7475 | conn->rx_skb = NULL; |
Johan Hedberg | c4e5baf | 2013-10-10 13:33:37 +0200 | [diff] [blame] | 7476 | l2cap_recv_frame(conn, rx_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7477 | } |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7478 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7479 | } |
| 7480 | |
| 7481 | drop: |
| 7482 | kfree_skb(skb); |
| 7483 | return 0; |
| 7484 | } |
| 7485 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7486 | static int l2cap_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7487 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7488 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7489 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 7490 | read_lock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7491 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7492 | list_for_each_entry(c, &chan_list, global_l) { |
Andrei Emeltchenko | fcb7333 | 2012-09-25 12:49:44 +0300 | [diff] [blame] | 7493 | seq_printf(f, "%pMR %pMR %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 7494 | &c->src, &c->dst, |
Andrei Emeltchenko | fcb7333 | 2012-09-25 12:49:44 +0300 | [diff] [blame] | 7495 | c->state, __le16_to_cpu(c->psm), |
| 7496 | c->scid, c->dcid, c->imtu, c->omtu, |
| 7497 | c->sec_level, c->mode); |
Andrei Emeltchenko | 61e1b4b | 2012-01-19 11:19:50 +0200 | [diff] [blame] | 7498 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7499 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 7500 | read_unlock(&chan_list_lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 7501 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7502 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7503 | } |
| 7504 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7505 | static int l2cap_debugfs_open(struct inode *inode, struct file *file) |
| 7506 | { |
| 7507 | return single_open(file, l2cap_debugfs_show, inode->i_private); |
| 7508 | } |
| 7509 | |
| 7510 | static const struct file_operations l2cap_debugfs_fops = { |
| 7511 | .open = l2cap_debugfs_open, |
| 7512 | .read = seq_read, |
| 7513 | .llseek = seq_lseek, |
| 7514 | .release = single_release, |
| 7515 | }; |
| 7516 | |
| 7517 | static struct dentry *l2cap_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7518 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 7519 | int __init l2cap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7520 | { |
| 7521 | int err; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 7522 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 7523 | err = l2cap_init_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7524 | if (err < 0) |
| 7525 | return err; |
| 7526 | |
Marcel Holtmann | 1120e4b | 2013-10-17 17:24:16 -0700 | [diff] [blame] | 7527 | if (IS_ERR_OR_NULL(bt_debugfs)) |
| 7528 | return 0; |
| 7529 | |
| 7530 | l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs, |
| 7531 | NULL, &l2cap_debugfs_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7532 | |
Samuel Ortiz | 40b9397 | 2014-05-14 17:53:35 +0200 | [diff] [blame] | 7533 | debugfs_create_u16("l2cap_le_max_credits", 0644, bt_debugfs, |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 7534 | &le_max_credits); |
Samuel Ortiz | 40b9397 | 2014-05-14 17:53:35 +0200 | [diff] [blame] | 7535 | debugfs_create_u16("l2cap_le_default_mps", 0644, bt_debugfs, |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 7536 | &le_default_mps); |
| 7537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7538 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7539 | } |
| 7540 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 7541 | void l2cap_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7542 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7543 | debugfs_remove(l2cap_debugfs); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 7544 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7545 | } |
| 7546 | |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 7547 | module_param(disable_ertm, bool, 0644); |
| 7548 | MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); |