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 | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 61 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 62 | static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 64 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 65 | struct sk_buff_head *skbs, u8 event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 66 | |
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 | |
| 213 | chan->scid = scid; |
| 214 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 215 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 220 | static u16 l2cap_alloc_cid(struct l2cap_conn *conn) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 221 | { |
Johan Hedberg | e77af75 | 2013-10-08 10:31:00 +0200 | [diff] [blame] | 222 | u16 cid, dyn_end; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 223 | |
Johan Hedberg | e77af75 | 2013-10-08 10:31:00 +0200 | [diff] [blame] | 224 | if (conn->hcon->type == LE_LINK) |
| 225 | dyn_end = L2CAP_CID_LE_DYN_END; |
| 226 | else |
| 227 | dyn_end = L2CAP_CID_DYN_END; |
| 228 | |
| 229 | for (cid = L2CAP_CID_DYN_START; cid < dyn_end; cid++) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 230 | if (!__l2cap_get_chan_by_scid(conn, cid)) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 231 | return cid; |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 237 | static void l2cap_state_change(struct l2cap_chan *chan, int state) |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 238 | { |
Andrei Emeltchenko | 42d2d87 | 2012-02-17 11:40:57 +0200 | [diff] [blame] | 239 | BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 240 | state_to_string(state)); |
Gustavo F. Padovan | badaaa0 | 2011-11-23 20:11:46 -0200 | [diff] [blame] | 241 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 242 | chan->state = state; |
Gustavo Padovan | 53f5212 | 2013-10-15 19:24:45 -0300 | [diff] [blame] | 243 | chan->ops->state_change(chan, state, 0); |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 244 | } |
| 245 | |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 246 | static inline void l2cap_state_change_and_error(struct l2cap_chan *chan, |
| 247 | int state, int err) |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 248 | { |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 249 | chan->state = state; |
Gustavo Padovan | 53f5212 | 2013-10-15 19:24:45 -0300 | [diff] [blame] | 250 | chan->ops->state_change(chan, chan->state, err); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err) |
| 254 | { |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 255 | chan->ops->state_change(chan, chan->state, err); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 258 | static void __set_retrans_timer(struct l2cap_chan *chan) |
| 259 | { |
| 260 | if (!delayed_work_pending(&chan->monitor_timer) && |
| 261 | chan->retrans_timeout) { |
| 262 | l2cap_set_timer(chan, &chan->retrans_timer, |
| 263 | msecs_to_jiffies(chan->retrans_timeout)); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | static void __set_monitor_timer(struct l2cap_chan *chan) |
| 268 | { |
| 269 | __clear_retrans_timer(chan); |
| 270 | if (chan->monitor_timeout) { |
| 271 | l2cap_set_timer(chan, &chan->monitor_timer, |
| 272 | msecs_to_jiffies(chan->monitor_timeout)); |
| 273 | } |
| 274 | } |
| 275 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 276 | static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head, |
| 277 | u16 seq) |
| 278 | { |
| 279 | struct sk_buff *skb; |
| 280 | |
| 281 | skb_queue_walk(head, skb) { |
| 282 | if (bt_cb(skb)->control.txseq == seq) |
| 283 | return skb; |
| 284 | } |
| 285 | |
| 286 | return NULL; |
| 287 | } |
| 288 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 289 | /* ---- L2CAP sequence number lists ---- */ |
| 290 | |
| 291 | /* For ERTM, ordered lists of sequence numbers must be tracked for |
| 292 | * SREJ requests that are received and for frames that are to be |
| 293 | * retransmitted. These seq_list functions implement a singly-linked |
| 294 | * list in an array, where membership in the list can also be checked |
| 295 | * in constant time. Items can also be added to the tail of the list |
| 296 | * and removed from the head in constant time, without further memory |
| 297 | * allocs or frees. |
| 298 | */ |
| 299 | |
| 300 | static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size) |
| 301 | { |
| 302 | size_t alloc_size, i; |
| 303 | |
| 304 | /* Allocated size is a power of 2 to map sequence numbers |
| 305 | * (which may be up to 14 bits) in to a smaller array that is |
| 306 | * sized for the negotiated ERTM transmit windows. |
| 307 | */ |
| 308 | alloc_size = roundup_pow_of_two(size); |
| 309 | |
| 310 | seq_list->list = kmalloc(sizeof(u16) * alloc_size, GFP_KERNEL); |
| 311 | if (!seq_list->list) |
| 312 | return -ENOMEM; |
| 313 | |
| 314 | seq_list->mask = alloc_size - 1; |
| 315 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 316 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
| 317 | for (i = 0; i < alloc_size; i++) |
| 318 | seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list) |
| 324 | { |
| 325 | kfree(seq_list->list); |
| 326 | } |
| 327 | |
| 328 | static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list, |
| 329 | u16 seq) |
| 330 | { |
| 331 | /* Constant-time check for list membership */ |
| 332 | return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR; |
| 333 | } |
| 334 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 335 | static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list) |
| 336 | { |
Johan Hedberg | 03a0c5d | 2014-01-18 21:32:59 +0200 | [diff] [blame] | 337 | u16 seq = seq_list->head; |
| 338 | u16 mask = seq_list->mask; |
| 339 | |
| 340 | seq_list->head = seq_list->list[seq & mask]; |
| 341 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR; |
| 342 | |
| 343 | if (seq_list->head == L2CAP_SEQ_LIST_TAIL) { |
| 344 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 345 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
| 346 | } |
| 347 | |
| 348 | return seq; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list) |
| 352 | { |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 353 | u16 i; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 354 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 355 | if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) |
| 356 | return; |
| 357 | |
| 358 | for (i = 0; i <= seq_list->mask; i++) |
| 359 | seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; |
| 360 | |
| 361 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 362 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq) |
| 366 | { |
| 367 | u16 mask = seq_list->mask; |
| 368 | |
| 369 | /* All appends happen in constant time */ |
| 370 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 371 | if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR) |
| 372 | return; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 373 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 374 | if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR) |
| 375 | seq_list->head = seq; |
| 376 | else |
| 377 | seq_list->list[seq_list->tail & mask] = seq; |
| 378 | |
| 379 | seq_list->tail = seq; |
| 380 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 383 | static void l2cap_chan_timeout(struct work_struct *work) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 384 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 385 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 386 | chan_timer.work); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 387 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 388 | int reason; |
| 389 | |
Andrei Emeltchenko | e05dcc3 | 2012-02-17 11:40:56 +0200 | [diff] [blame] | 390 | 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] | 391 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 392 | mutex_lock(&conn->chan_lock); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 393 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 394 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 395 | if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 396 | reason = ECONNREFUSED; |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 397 | else if (chan->state == BT_CONNECT && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 398 | chan->sec_level != BT_SECURITY_SDP) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 399 | reason = ECONNREFUSED; |
| 400 | else |
| 401 | reason = ETIMEDOUT; |
| 402 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 403 | l2cap_chan_close(chan, reason); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 404 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 405 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 406 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 407 | chan->ops->close(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 408 | mutex_unlock(&conn->chan_lock); |
| 409 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 410 | l2cap_chan_put(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 411 | } |
| 412 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 413 | struct l2cap_chan *l2cap_chan_create(void) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 414 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 415 | struct l2cap_chan *chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 416 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 417 | chan = kzalloc(sizeof(*chan), GFP_ATOMIC); |
| 418 | if (!chan) |
| 419 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 420 | |
Andrei Emeltchenko | c03b355 | 2012-02-21 12:54:56 +0200 | [diff] [blame] | 421 | mutex_init(&chan->lock); |
| 422 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 423 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 424 | list_add(&chan->global_l, &chan_list); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 425 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 426 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 427 | INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 428 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 429 | chan->state = BT_OPEN; |
| 430 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 431 | kref_init(&chan->kref); |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 432 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 433 | /* This flag is cleared in l2cap_chan_ready() */ |
| 434 | set_bit(CONF_NOT_COMPLETE, &chan->conf_state); |
| 435 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 436 | BT_DBG("chan %p", chan); |
Szymon Janc | abc545b | 2011-11-03 16:05:44 +0100 | [diff] [blame] | 437 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 438 | return chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 439 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 440 | EXPORT_SYMBOL_GPL(l2cap_chan_create); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 441 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 442 | static void l2cap_chan_destroy(struct kref *kref) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 443 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 444 | struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref); |
| 445 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 446 | BT_DBG("chan %p", chan); |
| 447 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 448 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 449 | list_del(&chan->global_l); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 450 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 451 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 452 | kfree(chan); |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 453 | } |
| 454 | |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 455 | void l2cap_chan_hold(struct l2cap_chan *c) |
| 456 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 457 | 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] | 458 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 459 | kref_get(&c->kref); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | void l2cap_chan_put(struct l2cap_chan *c) |
| 463 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 464 | 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] | 465 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 466 | kref_put(&c->kref, l2cap_chan_destroy); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 467 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 468 | EXPORT_SYMBOL_GPL(l2cap_chan_put); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 469 | |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 470 | void l2cap_chan_set_defaults(struct l2cap_chan *chan) |
| 471 | { |
| 472 | chan->fcs = L2CAP_FCS_CRC16; |
| 473 | chan->max_tx = L2CAP_DEFAULT_MAX_TX; |
| 474 | chan->tx_win = L2CAP_DEFAULT_TX_WINDOW; |
| 475 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
Jukka Rissanen | 6a5e816 | 2014-05-28 14:43:04 +0300 | [diff] [blame] | 476 | chan->remote_max_tx = chan->max_tx; |
| 477 | chan->remote_tx_win = chan->tx_win; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 478 | chan->ack_win = L2CAP_DEFAULT_TX_WINDOW; |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 479 | chan->sec_level = BT_SECURITY_LOW; |
Jukka Rissanen | 6a5e816 | 2014-05-28 14:43:04 +0300 | [diff] [blame] | 480 | chan->flush_to = L2CAP_DEFAULT_FLUSH_TO; |
| 481 | chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO; |
| 482 | chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO; |
| 483 | chan->conf_state = 0; |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 484 | |
| 485 | set_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 486 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 487 | EXPORT_SYMBOL_GPL(l2cap_chan_set_defaults); |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 488 | |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 489 | static void l2cap_le_flowctl_init(struct l2cap_chan *chan) |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 490 | { |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 491 | chan->sdu = NULL; |
| 492 | chan->sdu_last_frag = NULL; |
| 493 | chan->sdu_len = 0; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 494 | chan->tx_credits = 0; |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 495 | chan->rx_credits = le_max_credits; |
Johan Hedberg | d1d7941 | 2014-01-27 15:11:33 -0800 | [diff] [blame] | 496 | chan->mps = min_t(u16, chan->imtu, le_default_mps); |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 497 | |
| 498 | skb_queue_head_init(&chan->tx_q); |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 499 | } |
| 500 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 501 | void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 502 | { |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 503 | 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] | 504 | __le16_to_cpu(chan->psm), chan->dcid); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 505 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 506 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 507 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 508 | chan->conn = conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 509 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 510 | switch (chan->chan_type) { |
| 511 | case L2CAP_CHAN_CONN_ORIENTED: |
Johan Hedberg | 21626e6 | 2014-01-24 10:35:41 +0200 | [diff] [blame] | 512 | /* Alloc CID for connection-oriented socket */ |
| 513 | chan->scid = l2cap_alloc_cid(conn); |
| 514 | if (conn->hcon->type == ACL_LINK) |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 515 | chan->omtu = L2CAP_DEFAULT_MTU; |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 516 | break; |
| 517 | |
| 518 | case L2CAP_CHAN_CONN_LESS: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 519 | /* Connectionless socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 520 | chan->scid = L2CAP_CID_CONN_LESS; |
| 521 | chan->dcid = L2CAP_CID_CONN_LESS; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 522 | chan->omtu = L2CAP_DEFAULT_MTU; |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 523 | break; |
| 524 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 525 | case L2CAP_CHAN_FIXED: |
| 526 | /* Caller will set CID and CID specific MTU values */ |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 527 | break; |
| 528 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 529 | default: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 530 | /* Raw socket can send/recv signalling messages only */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 531 | chan->scid = L2CAP_CID_SIGNALING; |
| 532 | chan->dcid = L2CAP_CID_SIGNALING; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 533 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 534 | } |
| 535 | |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 536 | chan->local_id = L2CAP_BESTEFFORT_ID; |
| 537 | chan->local_stype = L2CAP_SERV_BESTEFFORT; |
| 538 | chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; |
| 539 | chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; |
| 540 | chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 541 | chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO; |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 542 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 543 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 544 | |
Johan Hedberg | 5ee9891 | 2013-04-29 19:35:43 +0300 | [diff] [blame] | 545 | hci_conn_hold(conn->hcon); |
| 546 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 547 | list_add(&chan->list, &conn->chan_l); |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 548 | } |
| 549 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 550 | void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 551 | { |
| 552 | mutex_lock(&conn->chan_lock); |
| 553 | __l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 554 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 555 | } |
| 556 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 557 | void l2cap_chan_del(struct l2cap_chan *chan, int err) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 558 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 559 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 560 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 561 | __clear_chan_timer(chan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 562 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 563 | BT_DBG("chan %p, conn %p, err %d", chan, conn, err); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 564 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 565 | if (conn) { |
Andrei Emeltchenko | 56f6098 | 2012-10-15 11:58:44 +0300 | [diff] [blame] | 566 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 567 | /* Delete from channel list */ |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 568 | list_del(&chan->list); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 569 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 570 | l2cap_chan_put(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 571 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 572 | chan->conn = NULL; |
Andrei Emeltchenko | 3cabbfd | 2012-05-31 11:01:37 +0300 | [diff] [blame] | 573 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 574 | if (chan->scid != L2CAP_CID_A2MP) |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 575 | hci_conn_drop(conn->hcon); |
Andrei Emeltchenko | 56f6098 | 2012-10-15 11:58:44 +0300 | [diff] [blame] | 576 | |
| 577 | if (mgr && mgr->bredr_chan == chan) |
| 578 | mgr->bredr_chan = NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 579 | } |
| 580 | |
Andrei Emeltchenko | 419e08c | 2012-10-31 15:46:34 +0200 | [diff] [blame] | 581 | if (chan->hs_hchan) { |
| 582 | struct hci_chan *hs_hchan = chan->hs_hchan; |
| 583 | |
| 584 | BT_DBG("chan %p disconnect hs_hchan %p", chan, hs_hchan); |
| 585 | amp_disconnect_logical_link(hs_hchan); |
| 586 | } |
| 587 | |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 588 | chan->ops->teardown(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 589 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 590 | if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 591 | return; |
Gustavo F. Padovan | 2ead70b | 2011-04-01 15:13:36 -0300 | [diff] [blame] | 592 | |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 593 | switch(chan->mode) { |
| 594 | case L2CAP_MODE_BASIC: |
| 595 | break; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 596 | |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 597 | case L2CAP_MODE_LE_FLOWCTL: |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 598 | skb_queue_purge(&chan->tx_q); |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 599 | break; |
| 600 | |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 601 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 602 | __clear_retrans_timer(chan); |
| 603 | __clear_monitor_timer(chan); |
| 604 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 605 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 606 | skb_queue_purge(&chan->srej_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 607 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 608 | l2cap_seq_list_free(&chan->srej_list); |
| 609 | l2cap_seq_list_free(&chan->retrans_list); |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 610 | |
| 611 | /* fall through */ |
| 612 | |
| 613 | case L2CAP_MODE_STREAMING: |
| 614 | skb_queue_purge(&chan->tx_q); |
| 615 | break; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 616 | } |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 617 | |
| 618 | return; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 619 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 620 | EXPORT_SYMBOL_GPL(l2cap_chan_del); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 621 | |
Johan Hedberg | 387a33e | 2014-02-18 21:41:33 +0200 | [diff] [blame] | 622 | void l2cap_conn_update_id_addr(struct hci_conn *hcon) |
| 623 | { |
| 624 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 625 | struct l2cap_chan *chan; |
| 626 | |
| 627 | mutex_lock(&conn->chan_lock); |
| 628 | |
| 629 | list_for_each_entry(chan, &conn->chan_l, list) { |
| 630 | l2cap_chan_lock(chan); |
| 631 | bacpy(&chan->dst, &hcon->dst); |
| 632 | chan->dst_type = bdaddr_type(hcon, hcon->dst_type); |
| 633 | l2cap_chan_unlock(chan); |
| 634 | } |
| 635 | |
| 636 | mutex_unlock(&conn->chan_lock); |
| 637 | } |
| 638 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 639 | static void l2cap_chan_le_connect_reject(struct l2cap_chan *chan) |
| 640 | { |
| 641 | struct l2cap_conn *conn = chan->conn; |
| 642 | struct l2cap_le_conn_rsp rsp; |
| 643 | u16 result; |
| 644 | |
| 645 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) |
| 646 | result = L2CAP_CR_AUTHORIZATION; |
| 647 | else |
| 648 | result = L2CAP_CR_BAD_PSM; |
| 649 | |
| 650 | l2cap_state_change(chan, BT_DISCONN); |
| 651 | |
| 652 | rsp.dcid = cpu_to_le16(chan->scid); |
| 653 | rsp.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 654 | rsp.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 655 | rsp.credits = cpu_to_le16(chan->rx_credits); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 656 | rsp.result = cpu_to_le16(result); |
| 657 | |
| 658 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), |
| 659 | &rsp); |
| 660 | } |
| 661 | |
Johan Hedberg | 791d60f | 2013-05-14 22:24:44 +0300 | [diff] [blame] | 662 | static void l2cap_chan_connect_reject(struct l2cap_chan *chan) |
| 663 | { |
| 664 | struct l2cap_conn *conn = chan->conn; |
| 665 | struct l2cap_conn_rsp rsp; |
| 666 | u16 result; |
| 667 | |
| 668 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) |
| 669 | result = L2CAP_CR_SEC_BLOCK; |
| 670 | else |
| 671 | result = L2CAP_CR_BAD_PSM; |
| 672 | |
| 673 | l2cap_state_change(chan, BT_DISCONN); |
| 674 | |
| 675 | rsp.scid = cpu_to_le16(chan->dcid); |
| 676 | rsp.dcid = cpu_to_le16(chan->scid); |
| 677 | rsp.result = cpu_to_le16(result); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 678 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Johan Hedberg | 791d60f | 2013-05-14 22:24:44 +0300 | [diff] [blame] | 679 | |
| 680 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
| 681 | } |
| 682 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 683 | void l2cap_chan_close(struct l2cap_chan *chan, int reason) |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 684 | { |
| 685 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 686 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 687 | 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] | 688 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 689 | switch (chan->state) { |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 690 | case BT_LISTEN: |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 691 | chan->ops->teardown(chan, 0); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 692 | break; |
| 693 | |
| 694 | case BT_CONNECTED: |
| 695 | case BT_CONFIG: |
Johan Hedberg | 7b25c9b | 2014-01-28 15:28:04 -0800 | [diff] [blame] | 696 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { |
Gustavo Padovan | 8d836d7 | 2013-10-15 19:24:47 -0300 | [diff] [blame] | 697 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 698 | l2cap_send_disconn_req(chan, reason); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 699 | } else |
| 700 | l2cap_chan_del(chan, reason); |
| 701 | break; |
| 702 | |
| 703 | case BT_CONNECT2: |
Johan Hedberg | 791d60f | 2013-05-14 22:24:44 +0300 | [diff] [blame] | 704 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { |
| 705 | if (conn->hcon->type == ACL_LINK) |
| 706 | l2cap_chan_connect_reject(chan); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 707 | else if (conn->hcon->type == LE_LINK) |
| 708 | l2cap_chan_le_connect_reject(chan); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | l2cap_chan_del(chan, reason); |
| 712 | break; |
| 713 | |
| 714 | case BT_CONNECT: |
| 715 | case BT_DISCONN: |
| 716 | l2cap_chan_del(chan, reason); |
| 717 | break; |
| 718 | |
| 719 | default: |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 720 | chan->ops->teardown(chan, 0); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 721 | break; |
| 722 | } |
| 723 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 724 | EXPORT_SYMBOL(l2cap_chan_close); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 725 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 726 | static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 727 | { |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 728 | switch (chan->chan_type) { |
| 729 | case L2CAP_CHAN_RAW: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 730 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 731 | case BT_SECURITY_HIGH: |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 732 | case BT_SECURITY_FIPS: |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 733 | return HCI_AT_DEDICATED_BONDING_MITM; |
| 734 | case BT_SECURITY_MEDIUM: |
| 735 | return HCI_AT_DEDICATED_BONDING; |
| 736 | default: |
| 737 | return HCI_AT_NO_BONDING; |
| 738 | } |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 739 | break; |
Marcel Holtmann | 3124b84 | 2013-10-12 07:19:32 -0700 | [diff] [blame] | 740 | case L2CAP_CHAN_CONN_LESS: |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 741 | if (chan->psm == cpu_to_le16(L2CAP_PSM_3DSP)) { |
Marcel Holtmann | 3124b84 | 2013-10-12 07:19:32 -0700 | [diff] [blame] | 742 | if (chan->sec_level == BT_SECURITY_LOW) |
| 743 | chan->sec_level = BT_SECURITY_SDP; |
| 744 | } |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 745 | if (chan->sec_level == BT_SECURITY_HIGH || |
| 746 | chan->sec_level == BT_SECURITY_FIPS) |
Marcel Holtmann | 3124b84 | 2013-10-12 07:19:32 -0700 | [diff] [blame] | 747 | return HCI_AT_NO_BONDING_MITM; |
| 748 | else |
| 749 | return HCI_AT_NO_BONDING; |
| 750 | break; |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 751 | case L2CAP_CHAN_CONN_ORIENTED: |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 752 | if (chan->psm == cpu_to_le16(L2CAP_PSM_SDP)) { |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 753 | if (chan->sec_level == BT_SECURITY_LOW) |
| 754 | chan->sec_level = BT_SECURITY_SDP; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 755 | |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 756 | if (chan->sec_level == BT_SECURITY_HIGH || |
| 757 | chan->sec_level == BT_SECURITY_FIPS) |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 758 | return HCI_AT_NO_BONDING_MITM; |
| 759 | else |
| 760 | return HCI_AT_NO_BONDING; |
| 761 | } |
| 762 | /* fall through */ |
| 763 | default: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 764 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 765 | case BT_SECURITY_HIGH: |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 766 | case BT_SECURITY_FIPS: |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 767 | return HCI_AT_GENERAL_BONDING_MITM; |
| 768 | case BT_SECURITY_MEDIUM: |
| 769 | return HCI_AT_GENERAL_BONDING; |
| 770 | default: |
| 771 | return HCI_AT_NO_BONDING; |
| 772 | } |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 773 | break; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 777 | /* Service level security */ |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 778 | int l2cap_chan_check_security(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 779 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 780 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 781 | __u8 auth_type; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 782 | |
Johan Hedberg | a17de2f | 2013-05-14 13:25:37 +0300 | [diff] [blame] | 783 | if (conn->hcon->type == LE_LINK) |
| 784 | return smp_conn_security(conn->hcon, chan->sec_level); |
| 785 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 786 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 787 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 788 | return hci_conn_security(conn->hcon, chan->sec_level, auth_type); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 789 | } |
| 790 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 791 | static u8 l2cap_get_ident(struct l2cap_conn *conn) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 792 | { |
| 793 | u8 id; |
| 794 | |
| 795 | /* Get next available identificator. |
| 796 | * 1 - 128 are used by kernel. |
| 797 | * 129 - 199 are reserved. |
| 798 | * 200 - 254 are used by utilities like l2ping, etc. |
| 799 | */ |
| 800 | |
Marcel Holtmann | 5a54e7c | 2014-07-13 20:50:15 +0200 | [diff] [blame] | 801 | mutex_lock(&conn->ident_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 802 | |
| 803 | if (++conn->tx_ident > 128) |
| 804 | conn->tx_ident = 1; |
| 805 | |
| 806 | id = conn->tx_ident; |
| 807 | |
Marcel Holtmann | 5a54e7c | 2014-07-13 20:50:15 +0200 | [diff] [blame] | 808 | mutex_unlock(&conn->ident_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 809 | |
| 810 | return id; |
| 811 | } |
| 812 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 813 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, |
| 814 | void *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 815 | { |
| 816 | struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 817 | u8 flags; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 818 | |
| 819 | BT_DBG("code 0x%2.2x", code); |
| 820 | |
| 821 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 822 | return; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 823 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 824 | if (lmp_no_flush_capable(conn->hcon->hdev)) |
| 825 | flags = ACL_START_NO_FLUSH; |
| 826 | else |
| 827 | flags = ACL_START; |
| 828 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 829 | bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 830 | skb->priority = HCI_PRIO_MAX; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 831 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 832 | hci_send_acl(conn->hchan, skb, flags); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 833 | } |
| 834 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 835 | static bool __chan_is_moving(struct l2cap_chan *chan) |
| 836 | { |
| 837 | return chan->move_state != L2CAP_MOVE_STABLE && |
| 838 | chan->move_state != L2CAP_MOVE_WAIT_PREPARE; |
| 839 | } |
| 840 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 841 | static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) |
| 842 | { |
| 843 | struct hci_conn *hcon = chan->conn->hcon; |
| 844 | u16 flags; |
| 845 | |
| 846 | 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] | 847 | skb->priority); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 848 | |
Mat Martineau | d5f8a75 | 2012-10-23 15:24:18 -0700 | [diff] [blame] | 849 | if (chan->hs_hcon && !__chan_is_moving(chan)) { |
| 850 | if (chan->hs_hchan) |
| 851 | hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE); |
| 852 | else |
| 853 | kfree_skb(skb); |
| 854 | |
| 855 | return; |
| 856 | } |
| 857 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 858 | if (!test_bit(FLAG_FLUSHABLE, &chan->flags) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 859 | lmp_no_flush_capable(hcon->hdev)) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 860 | flags = ACL_START_NO_FLUSH; |
| 861 | else |
| 862 | flags = ACL_START; |
| 863 | |
| 864 | bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 865 | hci_send_acl(chan->conn->hchan, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 868 | static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control) |
| 869 | { |
| 870 | control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT; |
| 871 | control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT; |
| 872 | |
| 873 | if (enh & L2CAP_CTRL_FRAME_TYPE) { |
| 874 | /* S-Frame */ |
| 875 | control->sframe = 1; |
| 876 | control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT; |
| 877 | control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT; |
| 878 | |
| 879 | control->sar = 0; |
| 880 | control->txseq = 0; |
| 881 | } else { |
| 882 | /* I-Frame */ |
| 883 | control->sframe = 0; |
| 884 | control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT; |
| 885 | control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT; |
| 886 | |
| 887 | control->poll = 0; |
| 888 | control->super = 0; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control) |
| 893 | { |
| 894 | control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT; |
| 895 | control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT; |
| 896 | |
| 897 | if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) { |
| 898 | /* S-Frame */ |
| 899 | control->sframe = 1; |
| 900 | control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT; |
| 901 | control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT; |
| 902 | |
| 903 | control->sar = 0; |
| 904 | control->txseq = 0; |
| 905 | } else { |
| 906 | /* I-Frame */ |
| 907 | control->sframe = 0; |
| 908 | control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT; |
| 909 | control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT; |
| 910 | |
| 911 | control->poll = 0; |
| 912 | control->super = 0; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | static inline void __unpack_control(struct l2cap_chan *chan, |
| 917 | struct sk_buff *skb) |
| 918 | { |
| 919 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 920 | __unpack_extended_control(get_unaligned_le32(skb->data), |
| 921 | &bt_cb(skb)->control); |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 922 | skb_pull(skb, L2CAP_EXT_CTRL_SIZE); |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 923 | } else { |
| 924 | __unpack_enhanced_control(get_unaligned_le16(skb->data), |
| 925 | &bt_cb(skb)->control); |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 926 | skb_pull(skb, L2CAP_ENH_CTRL_SIZE); |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | |
| 930 | static u32 __pack_extended_control(struct l2cap_ctrl *control) |
| 931 | { |
| 932 | u32 packed; |
| 933 | |
| 934 | packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT; |
| 935 | packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT; |
| 936 | |
| 937 | if (control->sframe) { |
| 938 | packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT; |
| 939 | packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT; |
| 940 | packed |= L2CAP_EXT_CTRL_FRAME_TYPE; |
| 941 | } else { |
| 942 | packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT; |
| 943 | packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT; |
| 944 | } |
| 945 | |
| 946 | return packed; |
| 947 | } |
| 948 | |
| 949 | static u16 __pack_enhanced_control(struct l2cap_ctrl *control) |
| 950 | { |
| 951 | u16 packed; |
| 952 | |
| 953 | packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT; |
| 954 | packed |= control->final << L2CAP_CTRL_FINAL_SHIFT; |
| 955 | |
| 956 | if (control->sframe) { |
| 957 | packed |= control->poll << L2CAP_CTRL_POLL_SHIFT; |
| 958 | packed |= control->super << L2CAP_CTRL_SUPER_SHIFT; |
| 959 | packed |= L2CAP_CTRL_FRAME_TYPE; |
| 960 | } else { |
| 961 | packed |= control->sar << L2CAP_CTRL_SAR_SHIFT; |
| 962 | packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT; |
| 963 | } |
| 964 | |
| 965 | return packed; |
| 966 | } |
| 967 | |
| 968 | static inline void __pack_control(struct l2cap_chan *chan, |
| 969 | struct l2cap_ctrl *control, |
| 970 | struct sk_buff *skb) |
| 971 | { |
| 972 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 973 | put_unaligned_le32(__pack_extended_control(control), |
| 974 | skb->data + L2CAP_HDR_SIZE); |
| 975 | } else { |
| 976 | put_unaligned_le16(__pack_enhanced_control(control), |
| 977 | skb->data + L2CAP_HDR_SIZE); |
| 978 | } |
| 979 | } |
| 980 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 981 | static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan) |
| 982 | { |
| 983 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 984 | return L2CAP_EXT_HDR_SIZE; |
| 985 | else |
| 986 | return L2CAP_ENH_HDR_SIZE; |
| 987 | } |
| 988 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 989 | static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan, |
| 990 | u32 control) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 991 | { |
| 992 | struct sk_buff *skb; |
| 993 | struct l2cap_hdr *lh; |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 994 | int hlen = __ertm_hdr_size(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 995 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 996 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 997 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 998 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 999 | skb = bt_skb_alloc(hlen, GFP_KERNEL); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1000 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1001 | if (!skb) |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1002 | return ERR_PTR(-ENOMEM); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1003 | |
| 1004 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1005 | lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1006 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1007 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1008 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 1009 | put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 1010 | else |
| 1011 | put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1012 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1013 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1014 | u16 fcs = crc16(0, (u8 *)skb->data, skb->len); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1015 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1016 | } |
| 1017 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1018 | skb->priority = HCI_PRIO_MAX; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1019 | return skb; |
| 1020 | } |
| 1021 | |
| 1022 | static void l2cap_send_sframe(struct l2cap_chan *chan, |
| 1023 | struct l2cap_ctrl *control) |
| 1024 | { |
| 1025 | struct sk_buff *skb; |
| 1026 | u32 control_field; |
| 1027 | |
| 1028 | BT_DBG("chan %p, control %p", chan, control); |
| 1029 | |
| 1030 | if (!control->sframe) |
| 1031 | return; |
| 1032 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1033 | if (__chan_is_moving(chan)) |
| 1034 | return; |
| 1035 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 1036 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) && |
| 1037 | !control->poll) |
| 1038 | control->final = 1; |
| 1039 | |
| 1040 | if (control->super == L2CAP_SUPER_RR) |
| 1041 | clear_bit(CONN_RNR_SENT, &chan->conn_state); |
| 1042 | else if (control->super == L2CAP_SUPER_RNR) |
| 1043 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
| 1044 | |
| 1045 | if (control->super != L2CAP_SUPER_SREJ) { |
| 1046 | chan->last_acked_seq = control->reqseq; |
| 1047 | __clear_ack_timer(chan); |
| 1048 | } |
| 1049 | |
| 1050 | BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq, |
| 1051 | control->final, control->poll, control->super); |
| 1052 | |
| 1053 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 1054 | control_field = __pack_extended_control(control); |
| 1055 | else |
| 1056 | control_field = __pack_enhanced_control(control); |
| 1057 | |
| 1058 | skb = l2cap_create_sframe_pdu(chan, control_field); |
| 1059 | if (!IS_ERR(skb)) |
| 1060 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1061 | } |
| 1062 | |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1063 | 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] | 1064 | { |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1065 | struct l2cap_ctrl control; |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 1066 | |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1067 | BT_DBG("chan %p, poll %d", chan, poll); |
| 1068 | |
| 1069 | memset(&control, 0, sizeof(control)); |
| 1070 | control.sframe = 1; |
| 1071 | control.poll = poll; |
| 1072 | |
| 1073 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 1074 | control.super = L2CAP_SUPER_RNR; |
| 1075 | else |
| 1076 | control.super = L2CAP_SUPER_RR; |
| 1077 | |
| 1078 | control.reqseq = chan->buffer_seq; |
| 1079 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 1080 | } |
| 1081 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1082 | static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 1083 | { |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1084 | return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 1085 | } |
| 1086 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1087 | static bool __amp_capable(struct l2cap_chan *chan) |
| 1088 | { |
| 1089 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 1df7b17 | 2013-10-05 11:47:49 -0700 | [diff] [blame] | 1090 | struct hci_dev *hdev; |
| 1091 | bool amp_available = false; |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1092 | |
Marcel Holtmann | 1df7b17 | 2013-10-05 11:47:49 -0700 | [diff] [blame] | 1093 | if (!conn->hs_enabled) |
| 1094 | return false; |
| 1095 | |
| 1096 | if (!(conn->fixed_chan_mask & L2CAP_FC_A2MP)) |
| 1097 | return false; |
| 1098 | |
| 1099 | read_lock(&hci_dev_list_lock); |
| 1100 | list_for_each_entry(hdev, &hci_dev_list, list) { |
| 1101 | if (hdev->amp_type != AMP_TYPE_BREDR && |
| 1102 | test_bit(HCI_UP, &hdev->flags)) { |
| 1103 | amp_available = true; |
| 1104 | break; |
| 1105 | } |
| 1106 | } |
| 1107 | read_unlock(&hci_dev_list_lock); |
| 1108 | |
| 1109 | if (chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED) |
| 1110 | return amp_available; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 1111 | |
| 1112 | return false; |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1113 | } |
| 1114 | |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 1115 | static bool l2cap_check_efs(struct l2cap_chan *chan) |
| 1116 | { |
| 1117 | /* Check EFS parameters */ |
| 1118 | return true; |
| 1119 | } |
| 1120 | |
Andrei Emeltchenko | 2766be4 | 2012-09-27 17:26:21 +0300 | [diff] [blame] | 1121 | void l2cap_send_conn_req(struct l2cap_chan *chan) |
Andrei Emeltchenko | 9b27f35 | 2012-02-24 16:00:00 +0200 | [diff] [blame] | 1122 | { |
| 1123 | struct l2cap_conn *conn = chan->conn; |
| 1124 | struct l2cap_conn_req req; |
| 1125 | |
| 1126 | req.scid = cpu_to_le16(chan->scid); |
| 1127 | req.psm = chan->psm; |
| 1128 | |
| 1129 | chan->ident = l2cap_get_ident(conn); |
| 1130 | |
| 1131 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
| 1132 | |
| 1133 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req); |
| 1134 | } |
| 1135 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 1136 | static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id) |
| 1137 | { |
| 1138 | struct l2cap_create_chan_req req; |
| 1139 | req.scid = cpu_to_le16(chan->scid); |
| 1140 | req.psm = chan->psm; |
| 1141 | req.amp_id = amp_id; |
| 1142 | |
| 1143 | chan->ident = l2cap_get_ident(chan->conn); |
| 1144 | |
| 1145 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ, |
| 1146 | sizeof(req), &req); |
| 1147 | } |
| 1148 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 1149 | static void l2cap_move_setup(struct l2cap_chan *chan) |
| 1150 | { |
| 1151 | struct sk_buff *skb; |
| 1152 | |
| 1153 | BT_DBG("chan %p", chan); |
| 1154 | |
| 1155 | if (chan->mode != L2CAP_MODE_ERTM) |
| 1156 | return; |
| 1157 | |
| 1158 | __clear_retrans_timer(chan); |
| 1159 | __clear_monitor_timer(chan); |
| 1160 | __clear_ack_timer(chan); |
| 1161 | |
| 1162 | chan->retry_count = 0; |
| 1163 | skb_queue_walk(&chan->tx_q, skb) { |
| 1164 | if (bt_cb(skb)->control.retries) |
| 1165 | bt_cb(skb)->control.retries = 1; |
| 1166 | else |
| 1167 | break; |
| 1168 | } |
| 1169 | |
| 1170 | chan->expected_tx_seq = chan->buffer_seq; |
| 1171 | |
| 1172 | clear_bit(CONN_REJ_ACT, &chan->conn_state); |
| 1173 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 1174 | l2cap_seq_list_clear(&chan->retrans_list); |
| 1175 | l2cap_seq_list_clear(&chan->srej_list); |
| 1176 | skb_queue_purge(&chan->srej_q); |
| 1177 | |
| 1178 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 1179 | chan->rx_state = L2CAP_RX_STATE_MOVE; |
| 1180 | |
| 1181 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 1182 | } |
| 1183 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 1184 | static void l2cap_move_done(struct l2cap_chan *chan) |
| 1185 | { |
| 1186 | u8 move_role = chan->move_role; |
| 1187 | BT_DBG("chan %p", chan); |
| 1188 | |
| 1189 | chan->move_state = L2CAP_MOVE_STABLE; |
| 1190 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 1191 | |
| 1192 | if (chan->mode != L2CAP_MODE_ERTM) |
| 1193 | return; |
| 1194 | |
| 1195 | switch (move_role) { |
| 1196 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 1197 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL); |
| 1198 | chan->rx_state = L2CAP_RX_STATE_WAIT_F; |
| 1199 | break; |
| 1200 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 1201 | chan->rx_state = L2CAP_RX_STATE_WAIT_P; |
| 1202 | break; |
| 1203 | } |
| 1204 | } |
| 1205 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1206 | static void l2cap_chan_ready(struct l2cap_chan *chan) |
| 1207 | { |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 1208 | /* This clears all conf flags, including CONF_NOT_COMPLETE */ |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1209 | chan->conf_state = 0; |
| 1210 | __clear_chan_timer(chan); |
| 1211 | |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 1212 | if (chan->mode == L2CAP_MODE_LE_FLOWCTL && !chan->tx_credits) |
| 1213 | chan->ops->suspend(chan); |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 1214 | |
Andrei Emeltchenko | 54a59aa | 2012-05-27 22:27:53 -0300 | [diff] [blame] | 1215 | chan->state = BT_CONNECTED; |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1216 | |
Andrei Emeltchenko | fd83e2c | 2012-05-30 09:55:32 +0300 | [diff] [blame] | 1217 | chan->ops->ready(chan); |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1218 | } |
| 1219 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1220 | static void l2cap_le_connect(struct l2cap_chan *chan) |
| 1221 | { |
| 1222 | struct l2cap_conn *conn = chan->conn; |
| 1223 | struct l2cap_le_conn_req req; |
| 1224 | |
Johan Hedberg | 595177f | 2013-12-02 22:12:22 +0200 | [diff] [blame] | 1225 | if (test_and_set_bit(FLAG_LE_CONN_REQ_SENT, &chan->flags)) |
| 1226 | return; |
| 1227 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1228 | req.psm = chan->psm; |
| 1229 | req.scid = cpu_to_le16(chan->scid); |
| 1230 | req.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 1231 | req.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 1232 | req.credits = cpu_to_le16(chan->rx_credits); |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1233 | |
| 1234 | chan->ident = l2cap_get_ident(conn); |
| 1235 | |
| 1236 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_REQ, |
| 1237 | sizeof(req), &req); |
| 1238 | } |
| 1239 | |
| 1240 | static void l2cap_le_start(struct l2cap_chan *chan) |
| 1241 | { |
| 1242 | struct l2cap_conn *conn = chan->conn; |
| 1243 | |
| 1244 | if (!smp_conn_security(conn->hcon, chan->sec_level)) |
| 1245 | return; |
| 1246 | |
| 1247 | if (!chan->psm) { |
| 1248 | l2cap_chan_ready(chan); |
| 1249 | return; |
| 1250 | } |
| 1251 | |
| 1252 | if (chan->state == BT_CONNECT) |
| 1253 | l2cap_le_connect(chan); |
| 1254 | } |
| 1255 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1256 | static void l2cap_start_connection(struct l2cap_chan *chan) |
| 1257 | { |
| 1258 | if (__amp_capable(chan)) { |
| 1259 | BT_DBG("chan %p AMP capable: discover AMPs", chan); |
| 1260 | a2mp_discover_amp(chan); |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1261 | } else if (chan->conn->hcon->type == LE_LINK) { |
| 1262 | l2cap_le_start(chan); |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1263 | } else { |
| 1264 | l2cap_send_conn_req(chan); |
| 1265 | } |
| 1266 | } |
| 1267 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1268 | static void l2cap_do_start(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1269 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1270 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1271 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1272 | if (conn->hcon->type == LE_LINK) { |
Johan Hedberg | 96ac34f | 2013-05-13 11:15:07 +0300 | [diff] [blame] | 1273 | l2cap_le_start(chan); |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1274 | return; |
| 1275 | } |
| 1276 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1277 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1278 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) |
| 1279 | return; |
| 1280 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1281 | if (l2cap_chan_check_security(chan) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1282 | __l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1283 | l2cap_start_connection(chan); |
| 1284 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1285 | } else { |
| 1286 | struct l2cap_info_req req; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1287 | req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1288 | |
| 1289 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 1290 | conn->info_ident = l2cap_get_ident(conn); |
| 1291 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 1292 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1293 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1294 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 1295 | sizeof(req), &req); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 1299 | static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) |
| 1300 | { |
| 1301 | u32 local_feat_mask = l2cap_feat_mask; |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 1302 | if (!disable_ertm) |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 1303 | local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; |
| 1304 | |
| 1305 | switch (mode) { |
| 1306 | case L2CAP_MODE_ERTM: |
| 1307 | return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; |
| 1308 | case L2CAP_MODE_STREAMING: |
| 1309 | return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; |
| 1310 | default: |
| 1311 | return 0x00; |
| 1312 | } |
| 1313 | } |
| 1314 | |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1315 | 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] | 1316 | { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1317 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1318 | struct l2cap_disconn_req req; |
| 1319 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1320 | if (!conn) |
| 1321 | return; |
| 1322 | |
Andrei Emeltchenko | aad3d0e | 2012-09-06 15:05:42 +0300 | [diff] [blame] | 1323 | if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1324 | __clear_retrans_timer(chan); |
| 1325 | __clear_monitor_timer(chan); |
| 1326 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1327 | } |
| 1328 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 1329 | if (chan->scid == L2CAP_CID_A2MP) { |
Gustavo Padovan | d117773 | 2012-10-06 11:47:38 +0100 | [diff] [blame] | 1330 | l2cap_state_change(chan, BT_DISCONN); |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1331 | return; |
| 1332 | } |
| 1333 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1334 | req.dcid = cpu_to_le16(chan->dcid); |
| 1335 | req.scid = cpu_to_le16(chan->scid); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1336 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ, |
| 1337 | sizeof(req), &req); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1338 | |
Gustavo Padovan | f8e7301 | 2013-10-15 19:24:46 -0300 | [diff] [blame] | 1339 | l2cap_state_change_and_error(chan, BT_DISCONN, err); |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1340 | } |
| 1341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | /* ---- L2CAP connections ---- */ |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1343 | static void l2cap_conn_start(struct l2cap_conn *conn) |
| 1344 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1345 | struct l2cap_chan *chan, *tmp; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1346 | |
| 1347 | BT_DBG("conn %p", conn); |
| 1348 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1349 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1350 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1351 | list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1352 | l2cap_chan_lock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1353 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1354 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1355 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1356 | continue; |
| 1357 | } |
| 1358 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1359 | if (chan->state == BT_CONNECT) { |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1360 | if (!l2cap_chan_check_security(chan) || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1361 | !__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1362 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1363 | continue; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 1364 | } |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1365 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1366 | if (!l2cap_mode_supported(chan->mode, conn->feat_mask) |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1367 | && test_bit(CONF_STATE2_DEVICE, |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1368 | &chan->conf_state)) { |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1369 | l2cap_chan_close(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1370 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1371 | continue; |
| 1372 | } |
| 1373 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1374 | l2cap_start_connection(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1375 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1376 | } else if (chan->state == BT_CONNECT2) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1377 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1378 | char buf[128]; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1379 | rsp.scid = cpu_to_le16(chan->dcid); |
| 1380 | rsp.dcid = cpu_to_le16(chan->scid); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1381 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1382 | if (l2cap_chan_check_security(chan)) { |
Marcel Holtmann | bdc2578 | 2013-10-14 02:45:34 -0700 | [diff] [blame] | 1383 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1384 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 1385 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 1386 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 1387 | |
| 1388 | } else { |
Gustavo Padovan | acdcabf | 2013-10-21 14:21:39 -0200 | [diff] [blame] | 1389 | l2cap_state_change(chan, BT_CONFIG); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1390 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 1391 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 1392 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1393 | } else { |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1394 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 1395 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1396 | } |
| 1397 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1398 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1399 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1400 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1401 | if (test_bit(CONF_REQ_SENT, &chan->conf_state) || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1402 | rsp.result != L2CAP_CR_SUCCESS) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1403 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1404 | continue; |
| 1405 | } |
| 1406 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1407 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1408 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1409 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1410 | chan->num_conf_req++; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1411 | } |
| 1412 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1413 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1414 | } |
| 1415 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1416 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1417 | } |
| 1418 | |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1419 | /* Find socket with cid and source/destination bdaddr. |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1420 | * Returns closest match, locked. |
| 1421 | */ |
Andrei Emeltchenko | d9b8870 | 2012-03-12 12:13:08 +0200 | [diff] [blame] | 1422 | static struct l2cap_chan *l2cap_global_chan_by_scid(int state, u16 cid, |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1423 | bdaddr_t *src, |
| 1424 | bdaddr_t *dst) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1425 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1426 | struct l2cap_chan *c, *c1 = NULL; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1427 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1428 | read_lock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1429 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1430 | list_for_each_entry(c, &chan_list, global_l) { |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1431 | if (state && c->state != state) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1432 | continue; |
| 1433 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1434 | if (c->scid == cid) { |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1435 | int src_match, dst_match; |
| 1436 | int src_any, dst_any; |
| 1437 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1438 | /* Exact match. */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1439 | src_match = !bacmp(&c->src, src); |
| 1440 | dst_match = !bacmp(&c->dst, dst); |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1441 | if (src_match && dst_match) { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1442 | read_unlock(&chan_list_lock); |
| 1443 | return c; |
| 1444 | } |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1445 | |
| 1446 | /* Closest match */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1447 | src_any = !bacmp(&c->src, BDADDR_ANY); |
| 1448 | dst_any = !bacmp(&c->dst, BDADDR_ANY); |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1449 | if ((src_match && dst_any) || (src_any && dst_match) || |
| 1450 | (src_any && dst_any)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1451 | c1 = c; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1452 | } |
| 1453 | } |
Gustavo F. Padovan | 280f294 | 2011-04-13 19:01:22 -0300 | [diff] [blame] | 1454 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1455 | read_unlock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1456 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1457 | return c1; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | static void l2cap_le_conn_ready(struct l2cap_conn *conn) |
| 1461 | { |
Marcel Holtmann | cc8dba2 | 2013-10-18 03:43:01 -0700 | [diff] [blame] | 1462 | struct hci_conn *hcon = conn->hcon; |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 1463 | struct hci_dev *hdev = hcon->hdev; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1464 | struct l2cap_chan *chan, *pchan; |
Marcel Holtmann | cc8dba2 | 2013-10-18 03:43:01 -0700 | [diff] [blame] | 1465 | u8 dst_type; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1466 | |
| 1467 | BT_DBG(""); |
| 1468 | |
| 1469 | /* Check if we have socket listening on cid */ |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame] | 1470 | pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_ATT, |
Marcel Holtmann | cc8dba2 | 2013-10-18 03:43:01 -0700 | [diff] [blame] | 1471 | &hcon->src, &hcon->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1472 | if (!pchan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1473 | return; |
| 1474 | |
Johan Hedberg | 44f3b0f | 2013-04-29 19:35:45 +0300 | [diff] [blame] | 1475 | /* Client ATT sockets should override the server one */ |
| 1476 | if (__l2cap_get_chan_by_dcid(conn, L2CAP_CID_ATT)) |
| 1477 | return; |
| 1478 | |
Marcel Holtmann | cc8dba2 | 2013-10-18 03:43:01 -0700 | [diff] [blame] | 1479 | dst_type = bdaddr_type(hcon, hcon->dst_type); |
| 1480 | |
| 1481 | /* If device is blocked, do not create a channel for it */ |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 1482 | if (hci_bdaddr_list_lookup(&hdev->blacklist, &hcon->dst, dst_type)) |
Marcel Holtmann | cc8dba2 | 2013-10-18 03:43:01 -0700 | [diff] [blame] | 1483 | return; |
| 1484 | |
Marcel Holtmann | 80afeb6 | 2014-06-23 12:18:51 +0200 | [diff] [blame] | 1485 | /* For LE slave connections, make sure the connection interval |
| 1486 | * is in the range of the minium and maximum interval that has |
| 1487 | * been configured for this connection. If not, then trigger |
| 1488 | * the connection update procedure. |
| 1489 | */ |
Johan Hedberg | 4dae279 | 2014-06-24 17:03:50 +0300 | [diff] [blame] | 1490 | if (!test_bit(HCI_CONN_MASTER, &hcon->flags) && |
Marcel Holtmann | 80afeb6 | 2014-06-23 12:18:51 +0200 | [diff] [blame] | 1491 | (hcon->le_conn_interval < hcon->le_conn_min_interval || |
| 1492 | hcon->le_conn_interval > hcon->le_conn_max_interval)) { |
| 1493 | struct l2cap_conn_param_update_req req; |
| 1494 | |
| 1495 | req.min = cpu_to_le16(hcon->le_conn_min_interval); |
| 1496 | req.max = cpu_to_le16(hcon->le_conn_max_interval); |
| 1497 | req.latency = cpu_to_le16(hcon->le_conn_latency); |
| 1498 | req.to_multiplier = cpu_to_le16(hcon->le_supv_timeout); |
| 1499 | |
| 1500 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 1501 | L2CAP_CONN_PARAM_UPDATE_REQ, sizeof(req), &req); |
| 1502 | } |
| 1503 | |
Gustavo Padovan | 8ffb929 | 2013-10-21 14:21:41 -0200 | [diff] [blame] | 1504 | l2cap_chan_lock(pchan); |
Gustavo F. Padovan | 62f3a2c | 2011-04-14 18:34:34 -0300 | [diff] [blame] | 1505 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 1506 | chan = pchan->ops->new_connection(pchan); |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1507 | if (!chan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1508 | goto clean; |
| 1509 | |
Marcel Holtmann | cc8dba2 | 2013-10-18 03:43:01 -0700 | [diff] [blame] | 1510 | bacpy(&chan->src, &hcon->src); |
| 1511 | bacpy(&chan->dst, &hcon->dst); |
| 1512 | chan->src_type = bdaddr_type(hcon, hcon->src_type); |
| 1513 | chan->dst_type = dst_type; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1514 | |
Johan Hedberg | 44f3b0f | 2013-04-29 19:35:45 +0300 | [diff] [blame] | 1515 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1516 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1517 | clean: |
Gustavo Padovan | 8ffb929 | 2013-10-21 14:21:41 -0200 | [diff] [blame] | 1518 | l2cap_chan_unlock(pchan); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1519 | } |
| 1520 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1521 | static void l2cap_conn_ready(struct l2cap_conn *conn) |
| 1522 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1523 | struct l2cap_chan *chan; |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1524 | struct hci_conn *hcon = conn->hcon; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1525 | |
| 1526 | BT_DBG("conn %p", conn); |
| 1527 | |
Johan Hedberg | d872992 | 2013-04-29 19:35:39 +0300 | [diff] [blame] | 1528 | /* For outgoing pairing which doesn't necessarily have an |
| 1529 | * associated socket (e.g. mgmt_pair_device). |
| 1530 | */ |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1531 | if (hcon->out && hcon->type == LE_LINK) |
| 1532 | smp_conn_security(hcon, hcon->pending_sec_level); |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 1533 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1534 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1535 | |
Johan Hedberg | 44f3b0f | 2013-04-29 19:35:45 +0300 | [diff] [blame] | 1536 | if (hcon->type == LE_LINK) |
| 1537 | l2cap_le_conn_ready(conn); |
| 1538 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1539 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1540 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1541 | l2cap_chan_lock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1542 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 1543 | if (chan->scid == L2CAP_CID_A2MP) { |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1544 | l2cap_chan_unlock(chan); |
| 1545 | continue; |
| 1546 | } |
| 1547 | |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1548 | if (hcon->type == LE_LINK) { |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 1549 | l2cap_le_start(chan); |
Vinicius Costa Gomes | 6312845 | 2011-06-17 22:46:26 -0300 | [diff] [blame] | 1550 | } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Gustavo Padovan | 74e7574 | 2013-10-15 19:24:51 -0300 | [diff] [blame] | 1551 | l2cap_chan_ready(chan); |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 1552 | |
Gustavo Padovan | 1c244f7 | 2012-12-07 03:29:10 -0200 | [diff] [blame] | 1553 | } else if (chan->state == BT_CONNECT) { |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1554 | l2cap_do_start(chan); |
Gustavo Padovan | 1c244f7 | 2012-12-07 03:29:10 -0200 | [diff] [blame] | 1555 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1556 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1557 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1558 | } |
| 1559 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1560 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1561 | |
| 1562 | queue_work(hcon->hdev->workqueue, &conn->pending_rx_work); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1563 | } |
| 1564 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1565 | /* Notify sockets that we cannot guaranty reliability anymore */ |
| 1566 | static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) |
| 1567 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1568 | struct l2cap_chan *chan; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1569 | |
| 1570 | BT_DBG("conn %p", conn); |
| 1571 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1572 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1573 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1574 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | ecf61bd | 2011-10-11 14:04:32 +0300 | [diff] [blame] | 1575 | if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) |
Gustavo Padovan | 1d8b1fd | 2012-10-06 11:34:52 +0100 | [diff] [blame] | 1576 | l2cap_chan_set_err(chan, err); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1577 | } |
| 1578 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1579 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1580 | } |
| 1581 | |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1582 | static void l2cap_info_timeout(struct work_struct *work) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1583 | { |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1584 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1585 | info_timer.work); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1586 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1587 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 1588 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1589 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1590 | l2cap_conn_start(conn); |
| 1591 | } |
| 1592 | |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1593 | /* |
| 1594 | * l2cap_user |
| 1595 | * External modules can register l2cap_user objects on l2cap_conn. The ->probe |
| 1596 | * callback is called during registration. The ->remove callback is called |
| 1597 | * during unregistration. |
| 1598 | * An l2cap_user object can either be explicitly unregistered or when the |
| 1599 | * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon, |
| 1600 | * l2cap->hchan, .. are valid as long as the remove callback hasn't been called. |
| 1601 | * External modules must own a reference to the l2cap_conn object if they intend |
| 1602 | * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at |
| 1603 | * any time if they don't. |
| 1604 | */ |
| 1605 | |
| 1606 | int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user) |
| 1607 | { |
| 1608 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1609 | int ret; |
| 1610 | |
| 1611 | /* We need to check whether l2cap_conn is registered. If it is not, we |
| 1612 | * must not register the l2cap_user. l2cap_conn_del() is unregisters |
| 1613 | * l2cap_conn objects, but doesn't provide its own locking. Instead, it |
| 1614 | * relies on the parent hci_conn object to be locked. This itself relies |
| 1615 | * on the hci_dev object to be locked. So we must lock the hci device |
| 1616 | * here, too. */ |
| 1617 | |
| 1618 | hci_dev_lock(hdev); |
| 1619 | |
| 1620 | if (user->list.next || user->list.prev) { |
| 1621 | ret = -EINVAL; |
| 1622 | goto out_unlock; |
| 1623 | } |
| 1624 | |
| 1625 | /* conn->hchan is NULL after l2cap_conn_del() was called */ |
| 1626 | if (!conn->hchan) { |
| 1627 | ret = -ENODEV; |
| 1628 | goto out_unlock; |
| 1629 | } |
| 1630 | |
| 1631 | ret = user->probe(conn, user); |
| 1632 | if (ret) |
| 1633 | goto out_unlock; |
| 1634 | |
| 1635 | list_add(&user->list, &conn->users); |
| 1636 | ret = 0; |
| 1637 | |
| 1638 | out_unlock: |
| 1639 | hci_dev_unlock(hdev); |
| 1640 | return ret; |
| 1641 | } |
| 1642 | EXPORT_SYMBOL(l2cap_register_user); |
| 1643 | |
| 1644 | void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user) |
| 1645 | { |
| 1646 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1647 | |
| 1648 | hci_dev_lock(hdev); |
| 1649 | |
| 1650 | if (!user->list.next || !user->list.prev) |
| 1651 | goto out_unlock; |
| 1652 | |
| 1653 | list_del(&user->list); |
| 1654 | user->list.next = NULL; |
| 1655 | user->list.prev = NULL; |
| 1656 | user->remove(conn, user); |
| 1657 | |
| 1658 | out_unlock: |
| 1659 | hci_dev_unlock(hdev); |
| 1660 | } |
| 1661 | EXPORT_SYMBOL(l2cap_unregister_user); |
| 1662 | |
| 1663 | static void l2cap_unregister_all_users(struct l2cap_conn *conn) |
| 1664 | { |
| 1665 | struct l2cap_user *user; |
| 1666 | |
| 1667 | while (!list_empty(&conn->users)) { |
| 1668 | user = list_first_entry(&conn->users, struct l2cap_user, list); |
| 1669 | list_del(&user->list); |
| 1670 | user->list.next = NULL; |
| 1671 | user->list.prev = NULL; |
| 1672 | user->remove(conn, user); |
| 1673 | } |
| 1674 | } |
| 1675 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1676 | static void l2cap_conn_del(struct hci_conn *hcon, int err) |
| 1677 | { |
| 1678 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 1679 | struct l2cap_chan *chan, *l; |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1680 | |
| 1681 | if (!conn) |
| 1682 | return; |
| 1683 | |
| 1684 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 1685 | |
| 1686 | kfree_skb(conn->rx_skb); |
| 1687 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1688 | skb_queue_purge(&conn->pending_rx); |
Jukka Taimisto | 7ab56c3 | 2014-06-12 10:15:13 +0000 | [diff] [blame] | 1689 | |
| 1690 | /* We can not call flush_work(&conn->pending_rx_work) here since we |
| 1691 | * might block if we are running on a worker from the same workqueue |
| 1692 | * pending_rx_work is waiting on. |
| 1693 | */ |
| 1694 | if (work_pending(&conn->pending_rx_work)) |
| 1695 | cancel_work_sync(&conn->pending_rx_work); |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 1696 | |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1697 | l2cap_unregister_all_users(conn); |
| 1698 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1699 | mutex_lock(&conn->chan_lock); |
| 1700 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1701 | /* Kill channels */ |
| 1702 | list_for_each_entry_safe(chan, l, &conn->chan_l, list) { |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1703 | l2cap_chan_hold(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1704 | l2cap_chan_lock(chan); |
| 1705 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1706 | l2cap_chan_del(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1707 | |
| 1708 | l2cap_chan_unlock(chan); |
| 1709 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 1710 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1711 | l2cap_chan_put(chan); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1712 | } |
| 1713 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1714 | mutex_unlock(&conn->chan_lock); |
| 1715 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1716 | hci_chan_del(conn->hchan); |
| 1717 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1718 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1719 | cancel_delayed_work_sync(&conn->info_timer); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1720 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1721 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) { |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1722 | cancel_delayed_work_sync(&conn->security_timer); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1723 | smp_chan_destroy(conn); |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1724 | } |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1725 | |
| 1726 | hcon->l2cap_data = NULL; |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1727 | conn->hchan = NULL; |
| 1728 | l2cap_conn_put(conn); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1729 | } |
| 1730 | |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1731 | static void security_timeout(struct work_struct *work) |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1732 | { |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1733 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1734 | security_timer.work); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1735 | |
Johan Hedberg | d06cc41 | 2012-06-06 18:44:11 +0800 | [diff] [blame] | 1736 | BT_DBG("conn %p", conn); |
| 1737 | |
| 1738 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) { |
| 1739 | smp_chan_destroy(conn); |
| 1740 | l2cap_conn_del(conn->hcon, ETIMEDOUT); |
| 1741 | } |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1742 | } |
| 1743 | |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1744 | static void l2cap_conn_free(struct kref *ref) |
| 1745 | { |
| 1746 | struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref); |
| 1747 | |
| 1748 | hci_conn_put(conn->hcon); |
| 1749 | kfree(conn); |
| 1750 | } |
| 1751 | |
| 1752 | void l2cap_conn_get(struct l2cap_conn *conn) |
| 1753 | { |
| 1754 | kref_get(&conn->ref); |
| 1755 | } |
| 1756 | EXPORT_SYMBOL(l2cap_conn_get); |
| 1757 | |
| 1758 | void l2cap_conn_put(struct l2cap_conn *conn) |
| 1759 | { |
| 1760 | kref_put(&conn->ref, l2cap_conn_free); |
| 1761 | } |
| 1762 | EXPORT_SYMBOL(l2cap_conn_put); |
| 1763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | /* ---- Socket interface ---- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1766 | /* Find socket with psm and source / destination bdaddr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | * Returns closest match. |
| 1768 | */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1769 | static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, |
| 1770 | bdaddr_t *src, |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 1771 | bdaddr_t *dst, |
| 1772 | u8 link_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1774 | struct l2cap_chan *c, *c1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1776 | read_lock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1777 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1778 | list_for_each_entry(c, &chan_list, global_l) { |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1779 | if (state && c->state != state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | continue; |
| 1781 | |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 1782 | if (link_type == ACL_LINK && c->src_type != BDADDR_BREDR) |
| 1783 | continue; |
| 1784 | |
| 1785 | if (link_type == LE_LINK && c->src_type == BDADDR_BREDR) |
| 1786 | continue; |
| 1787 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1788 | if (c->psm == psm) { |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1789 | int src_match, dst_match; |
| 1790 | int src_any, dst_any; |
| 1791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | /* Exact match. */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1793 | src_match = !bacmp(&c->src, src); |
| 1794 | dst_match = !bacmp(&c->dst, dst); |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1795 | if (src_match && dst_match) { |
Johannes Berg | a7567b2 | 2011-06-01 08:29:54 +0200 | [diff] [blame] | 1796 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1797 | return c; |
| 1798 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | |
| 1800 | /* Closest match */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1801 | src_any = !bacmp(&c->src, BDADDR_ANY); |
| 1802 | dst_any = !bacmp(&c->dst, BDADDR_ANY); |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1803 | if ((src_match && dst_any) || (src_any && dst_match) || |
| 1804 | (src_any && dst_any)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1805 | c1 = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | } |
| 1807 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1809 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1810 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1811 | return c1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | } |
| 1813 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1814 | static void l2cap_monitor_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1815 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1816 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1817 | monitor_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1818 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1819 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1820 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1821 | l2cap_chan_lock(chan); |
| 1822 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1823 | if (!chan->conn) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1824 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1825 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1826 | return; |
| 1827 | } |
| 1828 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1829 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1830 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1831 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1832 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1833 | } |
| 1834 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1835 | static void l2cap_retrans_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1836 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1837 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1838 | retrans_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1839 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1840 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1841 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1842 | l2cap_chan_lock(chan); |
| 1843 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1844 | if (!chan->conn) { |
| 1845 | l2cap_chan_unlock(chan); |
| 1846 | l2cap_chan_put(chan); |
| 1847 | return; |
| 1848 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1849 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1850 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1851 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1852 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1853 | } |
| 1854 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 1855 | static void l2cap_streaming_send(struct l2cap_chan *chan, |
| 1856 | struct sk_buff_head *skbs) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1857 | { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1858 | struct sk_buff *skb; |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1859 | struct l2cap_ctrl *control; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1860 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1861 | BT_DBG("chan %p, skbs %p", chan, skbs); |
| 1862 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1863 | if (__chan_is_moving(chan)) |
| 1864 | return; |
| 1865 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1866 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 1867 | |
| 1868 | while (!skb_queue_empty(&chan->tx_q)) { |
| 1869 | |
| 1870 | skb = skb_dequeue(&chan->tx_q); |
| 1871 | |
| 1872 | bt_cb(skb)->control.retries = 1; |
| 1873 | control = &bt_cb(skb)->control; |
| 1874 | |
| 1875 | control->reqseq = 0; |
| 1876 | control->txseq = chan->next_tx_seq; |
| 1877 | |
| 1878 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1879 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1880 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1881 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 1882 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1883 | } |
| 1884 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1885 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1886 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1887 | BT_DBG("Sent txseq %u", control->txseq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1888 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1889 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1890 | chan->frames_sent++; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1891 | } |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1892 | } |
| 1893 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1894 | static int l2cap_ertm_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1895 | { |
| 1896 | struct sk_buff *skb, *tx_skb; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1897 | struct l2cap_ctrl *control; |
| 1898 | int sent = 0; |
| 1899 | |
| 1900 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1901 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1902 | if (chan->state != BT_CONNECTED) |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1903 | return -ENOTCONN; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1904 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 1905 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 1906 | return 0; |
| 1907 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1908 | if (__chan_is_moving(chan)) |
| 1909 | return 0; |
| 1910 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1911 | while (chan->tx_send_head && |
| 1912 | chan->unacked_frames < chan->remote_tx_win && |
| 1913 | chan->tx_state == L2CAP_TX_STATE_XMIT) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1914 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1915 | skb = chan->tx_send_head; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1916 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1917 | bt_cb(skb)->control.retries = 1; |
| 1918 | control = &bt_cb(skb)->control; |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1919 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1920 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1921 | control->final = 1; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1922 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1923 | control->reqseq = chan->buffer_seq; |
| 1924 | chan->last_acked_seq = chan->buffer_seq; |
| 1925 | control->txseq = chan->next_tx_seq; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1926 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1927 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1928 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1929 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1930 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 1931 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1932 | } |
| 1933 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1934 | /* Clone after data has been modified. Data is assumed to be |
| 1935 | read-only (for locking purposes) on cloned sk_buffs. |
| 1936 | */ |
| 1937 | tx_skb = skb_clone(skb, GFP_KERNEL); |
| 1938 | |
| 1939 | if (!tx_skb) |
| 1940 | break; |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1941 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1942 | __set_retrans_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1943 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1944 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1945 | chan->unacked_frames++; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1946 | chan->frames_sent++; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1947 | sent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1948 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1949 | if (skb_queue_is_last(&chan->tx_q, skb)) |
| 1950 | chan->tx_send_head = NULL; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1951 | else |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1952 | chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1953 | |
| 1954 | l2cap_do_send(chan, tx_skb); |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1955 | BT_DBG("Sent txseq %u", control->txseq); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1956 | } |
| 1957 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1958 | BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent, |
| 1959 | chan->unacked_frames, skb_queue_len(&chan->tx_q)); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1960 | |
| 1961 | return sent; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1962 | } |
| 1963 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1964 | static void l2cap_ertm_resend(struct l2cap_chan *chan) |
| 1965 | { |
| 1966 | struct l2cap_ctrl control; |
| 1967 | struct sk_buff *skb; |
| 1968 | struct sk_buff *tx_skb; |
| 1969 | u16 seq; |
| 1970 | |
| 1971 | BT_DBG("chan %p", chan); |
| 1972 | |
| 1973 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 1974 | return; |
| 1975 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1976 | if (__chan_is_moving(chan)) |
| 1977 | return; |
| 1978 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1979 | while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) { |
| 1980 | seq = l2cap_seq_list_pop(&chan->retrans_list); |
| 1981 | |
| 1982 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq); |
| 1983 | if (!skb) { |
| 1984 | BT_DBG("Error: Can't retransmit seq %d, frame missing", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1985 | seq); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1986 | continue; |
| 1987 | } |
| 1988 | |
| 1989 | bt_cb(skb)->control.retries++; |
| 1990 | control = bt_cb(skb)->control; |
| 1991 | |
| 1992 | if (chan->max_tx != 0 && |
| 1993 | bt_cb(skb)->control.retries > chan->max_tx) { |
| 1994 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1995 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 1996 | l2cap_seq_list_clear(&chan->retrans_list); |
| 1997 | break; |
| 1998 | } |
| 1999 | |
| 2000 | control.reqseq = chan->buffer_seq; |
| 2001 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
| 2002 | control.final = 1; |
| 2003 | else |
| 2004 | control.final = 0; |
| 2005 | |
| 2006 | if (skb_cloned(skb)) { |
| 2007 | /* Cloned sk_buffs are read-only, so we need a |
| 2008 | * writeable copy |
| 2009 | */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2010 | tx_skb = skb_copy(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2011 | } else { |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2012 | tx_skb = skb_clone(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | if (!tx_skb) { |
| 2016 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2017 | break; |
| 2018 | } |
| 2019 | |
| 2020 | /* Update skb contents */ |
| 2021 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 2022 | put_unaligned_le32(__pack_extended_control(&control), |
| 2023 | tx_skb->data + L2CAP_HDR_SIZE); |
| 2024 | } else { |
| 2025 | put_unaligned_le16(__pack_enhanced_control(&control), |
| 2026 | tx_skb->data + L2CAP_HDR_SIZE); |
| 2027 | } |
| 2028 | |
| 2029 | if (chan->fcs == L2CAP_FCS_CRC16) { |
| 2030 | u16 fcs = crc16(0, (u8 *) tx_skb->data, tx_skb->len); |
| 2031 | put_unaligned_le16(fcs, skb_put(tx_skb, |
| 2032 | L2CAP_FCS_SIZE)); |
| 2033 | } |
| 2034 | |
| 2035 | l2cap_do_send(chan, tx_skb); |
| 2036 | |
| 2037 | BT_DBG("Resent txseq %d", control.txseq); |
| 2038 | |
| 2039 | chan->last_acked_seq = chan->buffer_seq; |
| 2040 | } |
| 2041 | } |
| 2042 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2043 | static void l2cap_retransmit(struct l2cap_chan *chan, |
| 2044 | struct l2cap_ctrl *control) |
| 2045 | { |
| 2046 | BT_DBG("chan %p, control %p", chan, control); |
| 2047 | |
| 2048 | l2cap_seq_list_append(&chan->retrans_list, control->reqseq); |
| 2049 | l2cap_ertm_resend(chan); |
| 2050 | } |
| 2051 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2052 | static void l2cap_retransmit_all(struct l2cap_chan *chan, |
| 2053 | struct l2cap_ctrl *control) |
| 2054 | { |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2055 | struct sk_buff *skb; |
| 2056 | |
| 2057 | BT_DBG("chan %p, control %p", chan, control); |
| 2058 | |
| 2059 | if (control->poll) |
| 2060 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 2061 | |
| 2062 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2063 | |
| 2064 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 2065 | return; |
| 2066 | |
| 2067 | if (chan->unacked_frames) { |
| 2068 | skb_queue_walk(&chan->tx_q, skb) { |
| 2069 | if (bt_cb(skb)->control.txseq == control->reqseq || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2070 | skb == chan->tx_send_head) |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2071 | break; |
| 2072 | } |
| 2073 | |
| 2074 | skb_queue_walk_from(&chan->tx_q, skb) { |
| 2075 | if (skb == chan->tx_send_head) |
| 2076 | break; |
| 2077 | |
| 2078 | l2cap_seq_list_append(&chan->retrans_list, |
| 2079 | bt_cb(skb)->control.txseq); |
| 2080 | } |
| 2081 | |
| 2082 | l2cap_ertm_resend(chan); |
| 2083 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2084 | } |
| 2085 | |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2086 | static void l2cap_send_ack(struct l2cap_chan *chan) |
| 2087 | { |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2088 | struct l2cap_ctrl control; |
| 2089 | u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 2090 | chan->last_acked_seq); |
| 2091 | int threshold; |
| 2092 | |
| 2093 | BT_DBG("chan %p last_acked_seq %d buffer_seq %d", |
| 2094 | chan, chan->last_acked_seq, chan->buffer_seq); |
| 2095 | |
| 2096 | memset(&control, 0, sizeof(control)); |
| 2097 | control.sframe = 1; |
| 2098 | |
| 2099 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
| 2100 | chan->rx_state == L2CAP_RX_STATE_RECV) { |
| 2101 | __clear_ack_timer(chan); |
| 2102 | control.super = L2CAP_SUPER_RNR; |
| 2103 | control.reqseq = chan->buffer_seq; |
| 2104 | l2cap_send_sframe(chan, &control); |
| 2105 | } else { |
| 2106 | if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) { |
| 2107 | l2cap_ertm_send(chan); |
| 2108 | /* If any i-frames were sent, they included an ack */ |
| 2109 | if (chan->buffer_seq == chan->last_acked_seq) |
| 2110 | frames_to_ack = 0; |
| 2111 | } |
| 2112 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2113 | /* Ack now if the window is 3/4ths full. |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2114 | * Calculate without mul or div |
| 2115 | */ |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2116 | threshold = chan->ack_win; |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2117 | threshold += threshold << 1; |
| 2118 | threshold >>= 2; |
| 2119 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2120 | BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack, |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2121 | threshold); |
| 2122 | |
| 2123 | if (frames_to_ack >= threshold) { |
| 2124 | __clear_ack_timer(chan); |
| 2125 | control.super = L2CAP_SUPER_RR; |
| 2126 | control.reqseq = chan->buffer_seq; |
| 2127 | l2cap_send_sframe(chan, &control); |
| 2128 | frames_to_ack = 0; |
| 2129 | } |
| 2130 | |
| 2131 | if (frames_to_ack) |
| 2132 | __set_ack_timer(chan); |
| 2133 | } |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2134 | } |
| 2135 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2136 | static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, |
| 2137 | struct msghdr *msg, int len, |
| 2138 | int count, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | { |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2140 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2141 | struct sk_buff **frag; |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2142 | int sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | |
Jukka Rissanen | 0498878 | 2014-06-18 16:37:07 +0300 | [diff] [blame] | 2144 | if (chan->ops->memcpy_fromiovec(chan, skb_put(skb, count), |
| 2145 | msg->msg_iov, count)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2146 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | |
| 2148 | sent += count; |
| 2149 | len -= count; |
| 2150 | |
| 2151 | /* Continuation fragments (no L2CAP header) */ |
| 2152 | frag = &skb_shinfo(skb)->frag_list; |
| 2153 | while (len) { |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2154 | struct sk_buff *tmp; |
| 2155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | count = min_t(unsigned int, conn->mtu, len); |
| 2157 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2158 | tmp = chan->ops->alloc_skb(chan, 0, count, |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2159 | msg->msg_flags & MSG_DONTWAIT); |
| 2160 | if (IS_ERR(tmp)) |
| 2161 | return PTR_ERR(tmp); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2162 | |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2163 | *frag = tmp; |
| 2164 | |
Jukka Rissanen | 0498878 | 2014-06-18 16:37:07 +0300 | [diff] [blame] | 2165 | if (chan->ops->memcpy_fromiovec(chan, skb_put(*frag, count), |
| 2166 | msg->msg_iov, count)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2167 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | |
| 2169 | sent += count; |
| 2170 | len -= count; |
| 2171 | |
Gustavo Padovan | 2d0ed3d | 2012-05-11 13:16:12 -0300 | [diff] [blame] | 2172 | skb->len += (*frag)->len; |
| 2173 | skb->data_len += (*frag)->len; |
| 2174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | frag = &(*frag)->next; |
| 2176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | |
| 2178 | return sent; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2179 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2181 | static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2182 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2183 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2184 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2185 | struct sk_buff *skb; |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2186 | int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2187 | struct l2cap_hdr *lh; |
| 2188 | |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2189 | BT_DBG("chan %p psm 0x%2.2x len %zu", chan, |
| 2190 | __le16_to_cpu(chan->psm), len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2191 | |
| 2192 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2193 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2194 | skb = chan->ops->alloc_skb(chan, hlen, count, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2195 | msg->msg_flags & MSG_DONTWAIT); |
| 2196 | if (IS_ERR(skb)) |
| 2197 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2198 | |
| 2199 | /* Create L2CAP header */ |
| 2200 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2201 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | daf6a78c | 2012-05-03 10:55:52 +0300 | [diff] [blame] | 2202 | lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE); |
Marcel Holtmann | 43b1b8d | 2013-10-12 06:01:26 -0700 | [diff] [blame] | 2203 | put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2204 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2205 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2206 | if (unlikely(err < 0)) { |
| 2207 | kfree_skb(skb); |
| 2208 | return ERR_PTR(err); |
| 2209 | } |
| 2210 | return skb; |
| 2211 | } |
| 2212 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2213 | static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2214 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2215 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2216 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2217 | struct sk_buff *skb; |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2218 | int err, count; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2219 | struct l2cap_hdr *lh; |
| 2220 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2221 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2222 | |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2223 | count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2224 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2225 | skb = chan->ops->alloc_skb(chan, L2CAP_HDR_SIZE, count, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2226 | msg->msg_flags & MSG_DONTWAIT); |
| 2227 | if (IS_ERR(skb)) |
| 2228 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2229 | |
| 2230 | /* Create L2CAP header */ |
| 2231 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2232 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo Padovan | 6ff9b5e | 2012-05-02 11:56:17 -0300 | [diff] [blame] | 2233 | lh->len = cpu_to_le16(len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2234 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2235 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2236 | if (unlikely(err < 0)) { |
| 2237 | kfree_skb(skb); |
| 2238 | return ERR_PTR(err); |
| 2239 | } |
| 2240 | return skb; |
| 2241 | } |
| 2242 | |
Luiz Augusto von Dentz | ab0ff76 | 2011-09-12 20:00:50 +0300 | [diff] [blame] | 2243 | static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2244 | struct msghdr *msg, size_t len, |
| 2245 | u16 sdulen) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2246 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2247 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2248 | struct sk_buff *skb; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2249 | int err, count, hlen; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2250 | struct l2cap_hdr *lh; |
| 2251 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2252 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2253 | |
Gustavo F. Padovan | 0ee0d20 | 2010-05-01 16:15:41 -0300 | [diff] [blame] | 2254 | if (!conn) |
| 2255 | return ERR_PTR(-ENOTCONN); |
| 2256 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2257 | hlen = __ertm_hdr_size(chan); |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2258 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2259 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2260 | hlen += L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2261 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2262 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2263 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2264 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2265 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2266 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2267 | skb = chan->ops->alloc_skb(chan, hlen, count, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2268 | msg->msg_flags & MSG_DONTWAIT); |
| 2269 | if (IS_ERR(skb)) |
| 2270 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2271 | |
| 2272 | /* Create L2CAP header */ |
| 2273 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2274 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2275 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2276 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2277 | /* Control header is populated later */ |
| 2278 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 2279 | put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 2280 | else |
| 2281 | put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2282 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2283 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2284 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2285 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2286 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2287 | if (unlikely(err < 0)) { |
| 2288 | kfree_skb(skb); |
| 2289 | return ERR_PTR(err); |
| 2290 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2291 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2292 | bt_cb(skb)->control.fcs = chan->fcs; |
Mat Martineau | 3ce3514 | 2012-04-25 16:36:14 -0700 | [diff] [blame] | 2293 | bt_cb(skb)->control.retries = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2294 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | } |
| 2296 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2297 | static int l2cap_segment_sdu(struct l2cap_chan *chan, |
| 2298 | struct sk_buff_head *seg_queue, |
| 2299 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2300 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2301 | struct sk_buff *skb; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2302 | u16 sdu_len; |
| 2303 | size_t pdu_len; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2304 | u8 sar; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2305 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2306 | BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2307 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2308 | /* It is critical that ERTM PDUs fit in a single HCI fragment, |
| 2309 | * so fragmented skbs are not used. The HCI layer's handling |
| 2310 | * of fragmented skbs is not compatible with ERTM's queueing. |
| 2311 | */ |
| 2312 | |
| 2313 | /* PDU size is derived from the HCI MTU */ |
| 2314 | pdu_len = chan->conn->mtu; |
| 2315 | |
Mat Martineau | a549574 | 2012-10-23 15:24:21 -0700 | [diff] [blame] | 2316 | /* Constrain PDU size for BR/EDR connections */ |
| 2317 | if (!chan->hs_hcon) |
| 2318 | pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2319 | |
| 2320 | /* Adjust for largest possible L2CAP overhead. */ |
Gustavo Padovan | 35d401d | 2012-05-25 18:57:05 -0300 | [diff] [blame] | 2321 | if (chan->fcs) |
| 2322 | pdu_len -= L2CAP_FCS_SIZE; |
| 2323 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2324 | pdu_len -= __ertm_hdr_size(chan); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2325 | |
| 2326 | /* Remote device may have requested smaller PDUs */ |
| 2327 | pdu_len = min_t(size_t, pdu_len, chan->remote_mps); |
| 2328 | |
| 2329 | if (len <= pdu_len) { |
| 2330 | sar = L2CAP_SAR_UNSEGMENTED; |
| 2331 | sdu_len = 0; |
| 2332 | pdu_len = len; |
| 2333 | } else { |
| 2334 | sar = L2CAP_SAR_START; |
| 2335 | sdu_len = len; |
| 2336 | pdu_len -= L2CAP_SDULEN_SIZE; |
| 2337 | } |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2338 | |
| 2339 | while (len > 0) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2340 | skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2341 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2342 | if (IS_ERR(skb)) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2343 | __skb_queue_purge(seg_queue); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2344 | return PTR_ERR(skb); |
| 2345 | } |
| 2346 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2347 | bt_cb(skb)->control.sar = sar; |
| 2348 | __skb_queue_tail(seg_queue, skb); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2349 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2350 | len -= pdu_len; |
| 2351 | if (sdu_len) { |
| 2352 | sdu_len = 0; |
| 2353 | pdu_len += L2CAP_SDULEN_SIZE; |
| 2354 | } |
| 2355 | |
| 2356 | if (len <= pdu_len) { |
| 2357 | sar = L2CAP_SAR_END; |
| 2358 | pdu_len = len; |
| 2359 | } else { |
| 2360 | sar = L2CAP_SAR_CONTINUE; |
| 2361 | } |
| 2362 | } |
| 2363 | |
Gustavo Padovan | f0f6279 | 2012-05-29 13:29:17 -0300 | [diff] [blame] | 2364 | return 0; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2365 | } |
| 2366 | |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2367 | static struct sk_buff *l2cap_create_le_flowctl_pdu(struct l2cap_chan *chan, |
| 2368 | struct msghdr *msg, |
| 2369 | size_t len, u16 sdulen) |
| 2370 | { |
| 2371 | struct l2cap_conn *conn = chan->conn; |
| 2372 | struct sk_buff *skb; |
| 2373 | int err, count, hlen; |
| 2374 | struct l2cap_hdr *lh; |
| 2375 | |
| 2376 | BT_DBG("chan %p len %zu", chan, len); |
| 2377 | |
| 2378 | if (!conn) |
| 2379 | return ERR_PTR(-ENOTCONN); |
| 2380 | |
| 2381 | hlen = L2CAP_HDR_SIZE; |
| 2382 | |
| 2383 | if (sdulen) |
| 2384 | hlen += L2CAP_SDULEN_SIZE; |
| 2385 | |
| 2386 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
| 2387 | |
Marcel Holtmann | d9fbd02 | 2014-06-08 11:22:28 +0200 | [diff] [blame] | 2388 | skb = chan->ops->alloc_skb(chan, hlen, count, |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2389 | msg->msg_flags & MSG_DONTWAIT); |
| 2390 | if (IS_ERR(skb)) |
| 2391 | return skb; |
| 2392 | |
| 2393 | /* Create L2CAP header */ |
| 2394 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
| 2395 | lh->cid = cpu_to_le16(chan->dcid); |
| 2396 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
| 2397 | |
| 2398 | if (sdulen) |
| 2399 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
| 2400 | |
| 2401 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
| 2402 | if (unlikely(err < 0)) { |
| 2403 | kfree_skb(skb); |
| 2404 | return ERR_PTR(err); |
| 2405 | } |
| 2406 | |
| 2407 | return skb; |
| 2408 | } |
| 2409 | |
| 2410 | static int l2cap_segment_le_sdu(struct l2cap_chan *chan, |
| 2411 | struct sk_buff_head *seg_queue, |
| 2412 | struct msghdr *msg, size_t len) |
| 2413 | { |
| 2414 | struct sk_buff *skb; |
| 2415 | size_t pdu_len; |
| 2416 | u16 sdu_len; |
| 2417 | |
| 2418 | BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); |
| 2419 | |
| 2420 | pdu_len = chan->conn->mtu - L2CAP_HDR_SIZE; |
| 2421 | |
| 2422 | pdu_len = min_t(size_t, pdu_len, chan->remote_mps); |
| 2423 | |
| 2424 | sdu_len = len; |
| 2425 | pdu_len -= L2CAP_SDULEN_SIZE; |
| 2426 | |
| 2427 | while (len > 0) { |
| 2428 | if (len <= pdu_len) |
| 2429 | pdu_len = len; |
| 2430 | |
| 2431 | skb = l2cap_create_le_flowctl_pdu(chan, msg, pdu_len, sdu_len); |
| 2432 | if (IS_ERR(skb)) { |
| 2433 | __skb_queue_purge(seg_queue); |
| 2434 | return PTR_ERR(skb); |
| 2435 | } |
| 2436 | |
| 2437 | __skb_queue_tail(seg_queue, skb); |
| 2438 | |
| 2439 | len -= pdu_len; |
| 2440 | |
| 2441 | if (sdu_len) { |
| 2442 | sdu_len = 0; |
| 2443 | pdu_len += L2CAP_SDULEN_SIZE; |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | return 0; |
| 2448 | } |
| 2449 | |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2450 | 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] | 2451 | { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2452 | struct sk_buff *skb; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2453 | int err; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2454 | struct sk_buff_head seg_queue; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2455 | |
Seung-Woo Kim | 31e8ce8 | 2013-11-05 18:46:33 +0900 | [diff] [blame] | 2456 | if (!chan->conn) |
| 2457 | return -ENOTCONN; |
| 2458 | |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2459 | /* Connectionless channel */ |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2460 | if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2461 | skb = l2cap_create_connless_pdu(chan, msg, len); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2462 | if (IS_ERR(skb)) |
| 2463 | return PTR_ERR(skb); |
| 2464 | |
Andrzej Kaczmarek | ede81a2 | 2014-02-25 17:16:22 +0100 | [diff] [blame] | 2465 | /* Channel lock is released before requesting new skb and then |
| 2466 | * reacquired thus we need to recheck channel state. |
| 2467 | */ |
| 2468 | if (chan->state != BT_CONNECTED) { |
| 2469 | kfree_skb(skb); |
| 2470 | return -ENOTCONN; |
| 2471 | } |
| 2472 | |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2473 | l2cap_do_send(chan, skb); |
| 2474 | return len; |
| 2475 | } |
| 2476 | |
| 2477 | switch (chan->mode) { |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 2478 | case L2CAP_MODE_LE_FLOWCTL: |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2479 | /* Check outgoing MTU */ |
| 2480 | if (len > chan->omtu) |
| 2481 | return -EMSGSIZE; |
| 2482 | |
Johan Hedberg | fad5fc8 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 2483 | if (!chan->tx_credits) |
| 2484 | return -EAGAIN; |
| 2485 | |
Johan Hedberg | 177f8f2 | 2013-05-31 17:54:51 +0300 | [diff] [blame] | 2486 | __skb_queue_head_init(&seg_queue); |
| 2487 | |
| 2488 | err = l2cap_segment_le_sdu(chan, &seg_queue, msg, len); |
| 2489 | |
| 2490 | if (chan->state != BT_CONNECTED) { |
| 2491 | __skb_queue_purge(&seg_queue); |
| 2492 | err = -ENOTCONN; |
| 2493 | } |
| 2494 | |
| 2495 | if (err) |
| 2496 | return err; |
| 2497 | |
| 2498 | skb_queue_splice_tail_init(&seg_queue, &chan->tx_q); |
| 2499 | |
| 2500 | while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { |
| 2501 | l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); |
| 2502 | chan->tx_credits--; |
| 2503 | } |
| 2504 | |
| 2505 | if (!chan->tx_credits) |
| 2506 | chan->ops->suspend(chan); |
| 2507 | |
| 2508 | err = len; |
| 2509 | |
| 2510 | break; |
| 2511 | |
Johan Hedberg | fad5fc8 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 2512 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2513 | /* Check outgoing MTU */ |
| 2514 | if (len > chan->omtu) |
| 2515 | return -EMSGSIZE; |
| 2516 | |
| 2517 | /* Create a basic PDU */ |
Marcel Holtmann | 8d46321 | 2014-06-05 15:22:51 +0200 | [diff] [blame] | 2518 | skb = l2cap_create_basic_pdu(chan, msg, len); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2519 | if (IS_ERR(skb)) |
| 2520 | return PTR_ERR(skb); |
| 2521 | |
Andrzej Kaczmarek | ede81a2 | 2014-02-25 17:16:22 +0100 | [diff] [blame] | 2522 | /* Channel lock is released before requesting new skb and then |
| 2523 | * reacquired thus we need to recheck channel state. |
| 2524 | */ |
| 2525 | if (chan->state != BT_CONNECTED) { |
| 2526 | kfree_skb(skb); |
| 2527 | return -ENOTCONN; |
| 2528 | } |
| 2529 | |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2530 | l2cap_do_send(chan, skb); |
| 2531 | err = len; |
| 2532 | break; |
| 2533 | |
| 2534 | case L2CAP_MODE_ERTM: |
| 2535 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2536 | /* Check outgoing MTU */ |
| 2537 | if (len > chan->omtu) { |
| 2538 | err = -EMSGSIZE; |
| 2539 | break; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2540 | } |
| 2541 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2542 | __skb_queue_head_init(&seg_queue); |
| 2543 | |
| 2544 | /* Do segmentation before calling in to the state machine, |
| 2545 | * since it's possible to block while waiting for memory |
| 2546 | * allocation. |
| 2547 | */ |
| 2548 | err = l2cap_segment_sdu(chan, &seg_queue, msg, len); |
| 2549 | |
| 2550 | /* The channel could have been closed while segmenting, |
| 2551 | * check that it is still connected. |
| 2552 | */ |
| 2553 | if (chan->state != BT_CONNECTED) { |
| 2554 | __skb_queue_purge(&seg_queue); |
| 2555 | err = -ENOTCONN; |
| 2556 | } |
| 2557 | |
| 2558 | if (err) |
| 2559 | break; |
| 2560 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2561 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2562 | l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2563 | else |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2564 | l2cap_streaming_send(chan, &seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2565 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2566 | err = len; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2567 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2568 | /* If the skbs were not queued for sending, they'll still be in |
| 2569 | * seg_queue and need to be purged. |
| 2570 | */ |
| 2571 | __skb_queue_purge(&seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2572 | break; |
| 2573 | |
| 2574 | default: |
| 2575 | BT_DBG("bad state %1.1x", chan->mode); |
| 2576 | err = -EBADFD; |
| 2577 | } |
| 2578 | |
| 2579 | return err; |
| 2580 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 2581 | EXPORT_SYMBOL_GPL(l2cap_chan_send); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2582 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2583 | static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq) |
| 2584 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2585 | struct l2cap_ctrl control; |
| 2586 | u16 seq; |
| 2587 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2588 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2589 | |
| 2590 | memset(&control, 0, sizeof(control)); |
| 2591 | control.sframe = 1; |
| 2592 | control.super = L2CAP_SUPER_SREJ; |
| 2593 | |
| 2594 | for (seq = chan->expected_tx_seq; seq != txseq; |
| 2595 | seq = __next_seq(chan, seq)) { |
| 2596 | if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) { |
| 2597 | control.reqseq = seq; |
| 2598 | l2cap_send_sframe(chan, &control); |
| 2599 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2600 | } |
| 2601 | } |
| 2602 | |
| 2603 | chan->expected_tx_seq = __next_seq(chan, txseq); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | static void l2cap_send_srej_tail(struct l2cap_chan *chan) |
| 2607 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2608 | struct l2cap_ctrl control; |
| 2609 | |
| 2610 | BT_DBG("chan %p", chan); |
| 2611 | |
| 2612 | if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR) |
| 2613 | return; |
| 2614 | |
| 2615 | memset(&control, 0, sizeof(control)); |
| 2616 | control.sframe = 1; |
| 2617 | control.super = L2CAP_SUPER_SREJ; |
| 2618 | control.reqseq = chan->srej_list.tail; |
| 2619 | l2cap_send_sframe(chan, &control); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq) |
| 2623 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2624 | struct l2cap_ctrl control; |
| 2625 | u16 initial_head; |
| 2626 | u16 seq; |
| 2627 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2628 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2629 | |
| 2630 | memset(&control, 0, sizeof(control)); |
| 2631 | control.sframe = 1; |
| 2632 | control.super = L2CAP_SUPER_SREJ; |
| 2633 | |
| 2634 | /* Capture initial list head to allow only one pass through the list. */ |
| 2635 | initial_head = chan->srej_list.head; |
| 2636 | |
| 2637 | do { |
| 2638 | seq = l2cap_seq_list_pop(&chan->srej_list); |
| 2639 | if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR) |
| 2640 | break; |
| 2641 | |
| 2642 | control.reqseq = seq; |
| 2643 | l2cap_send_sframe(chan, &control); |
| 2644 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2645 | } while (chan->srej_list.head != initial_head); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2646 | } |
| 2647 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2648 | static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 2649 | { |
| 2650 | struct sk_buff *acked_skb; |
| 2651 | u16 ackseq; |
| 2652 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2653 | BT_DBG("chan %p, reqseq %u", chan, reqseq); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2654 | |
| 2655 | if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq) |
| 2656 | return; |
| 2657 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2658 | BT_DBG("expected_ack_seq %u, unacked_frames %u", |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2659 | chan->expected_ack_seq, chan->unacked_frames); |
| 2660 | |
| 2661 | for (ackseq = chan->expected_ack_seq; ackseq != reqseq; |
| 2662 | ackseq = __next_seq(chan, ackseq)) { |
| 2663 | |
| 2664 | acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq); |
| 2665 | if (acked_skb) { |
| 2666 | skb_unlink(acked_skb, &chan->tx_q); |
| 2667 | kfree_skb(acked_skb); |
| 2668 | chan->unacked_frames--; |
| 2669 | } |
| 2670 | } |
| 2671 | |
| 2672 | chan->expected_ack_seq = reqseq; |
| 2673 | |
| 2674 | if (chan->unacked_frames == 0) |
| 2675 | __clear_retrans_timer(chan); |
| 2676 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2677 | BT_DBG("unacked_frames %u", chan->unacked_frames); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan) |
| 2681 | { |
| 2682 | BT_DBG("chan %p", chan); |
| 2683 | |
| 2684 | chan->expected_tx_seq = chan->buffer_seq; |
| 2685 | l2cap_seq_list_clear(&chan->srej_list); |
| 2686 | skb_queue_purge(&chan->srej_q); |
| 2687 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 2688 | } |
| 2689 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2690 | static void l2cap_tx_state_xmit(struct l2cap_chan *chan, |
| 2691 | struct l2cap_ctrl *control, |
| 2692 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2693 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2694 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2695 | event); |
| 2696 | |
| 2697 | switch (event) { |
| 2698 | case L2CAP_EV_DATA_REQUEST: |
| 2699 | if (chan->tx_send_head == NULL) |
| 2700 | chan->tx_send_head = skb_peek(skbs); |
| 2701 | |
| 2702 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2703 | l2cap_ertm_send(chan); |
| 2704 | break; |
| 2705 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2706 | BT_DBG("Enter LOCAL_BUSY"); |
| 2707 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2708 | |
| 2709 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2710 | /* The SREJ_SENT state must be aborted if we are to |
| 2711 | * enter the LOCAL_BUSY state. |
| 2712 | */ |
| 2713 | l2cap_abort_rx_srej_sent(chan); |
| 2714 | } |
| 2715 | |
| 2716 | l2cap_send_ack(chan); |
| 2717 | |
| 2718 | break; |
| 2719 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2720 | BT_DBG("Exit LOCAL_BUSY"); |
| 2721 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2722 | |
| 2723 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2724 | struct l2cap_ctrl local_control; |
| 2725 | |
| 2726 | memset(&local_control, 0, sizeof(local_control)); |
| 2727 | local_control.sframe = 1; |
| 2728 | local_control.super = L2CAP_SUPER_RR; |
| 2729 | local_control.poll = 1; |
| 2730 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2731 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2732 | |
| 2733 | chan->retry_count = 1; |
| 2734 | __set_monitor_timer(chan); |
| 2735 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2736 | } |
| 2737 | break; |
| 2738 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2739 | l2cap_process_reqseq(chan, control->reqseq); |
| 2740 | break; |
| 2741 | case L2CAP_EV_EXPLICIT_POLL: |
| 2742 | l2cap_send_rr_or_rnr(chan, 1); |
| 2743 | chan->retry_count = 1; |
| 2744 | __set_monitor_timer(chan); |
| 2745 | __clear_ack_timer(chan); |
| 2746 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2747 | break; |
| 2748 | case L2CAP_EV_RETRANS_TO: |
| 2749 | l2cap_send_rr_or_rnr(chan, 1); |
| 2750 | chan->retry_count = 1; |
| 2751 | __set_monitor_timer(chan); |
| 2752 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2753 | break; |
| 2754 | case L2CAP_EV_RECV_FBIT: |
| 2755 | /* Nothing to process */ |
| 2756 | break; |
| 2757 | default: |
| 2758 | break; |
| 2759 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2760 | } |
| 2761 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2762 | static void l2cap_tx_state_wait_f(struct l2cap_chan *chan, |
| 2763 | struct l2cap_ctrl *control, |
| 2764 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2765 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2766 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2767 | event); |
| 2768 | |
| 2769 | switch (event) { |
| 2770 | case L2CAP_EV_DATA_REQUEST: |
| 2771 | if (chan->tx_send_head == NULL) |
| 2772 | chan->tx_send_head = skb_peek(skbs); |
| 2773 | /* Queue data, but don't send. */ |
| 2774 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2775 | break; |
| 2776 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2777 | BT_DBG("Enter LOCAL_BUSY"); |
| 2778 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2779 | |
| 2780 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2781 | /* The SREJ_SENT state must be aborted if we are to |
| 2782 | * enter the LOCAL_BUSY state. |
| 2783 | */ |
| 2784 | l2cap_abort_rx_srej_sent(chan); |
| 2785 | } |
| 2786 | |
| 2787 | l2cap_send_ack(chan); |
| 2788 | |
| 2789 | break; |
| 2790 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2791 | BT_DBG("Exit LOCAL_BUSY"); |
| 2792 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2793 | |
| 2794 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2795 | struct l2cap_ctrl local_control; |
| 2796 | memset(&local_control, 0, sizeof(local_control)); |
| 2797 | local_control.sframe = 1; |
| 2798 | local_control.super = L2CAP_SUPER_RR; |
| 2799 | local_control.poll = 1; |
| 2800 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2801 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2802 | |
| 2803 | chan->retry_count = 1; |
| 2804 | __set_monitor_timer(chan); |
| 2805 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2806 | } |
| 2807 | break; |
| 2808 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2809 | l2cap_process_reqseq(chan, control->reqseq); |
| 2810 | |
| 2811 | /* Fall through */ |
| 2812 | |
| 2813 | case L2CAP_EV_RECV_FBIT: |
| 2814 | if (control && control->final) { |
| 2815 | __clear_monitor_timer(chan); |
| 2816 | if (chan->unacked_frames > 0) |
| 2817 | __set_retrans_timer(chan); |
| 2818 | chan->retry_count = 0; |
| 2819 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 2820 | BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state); |
| 2821 | } |
| 2822 | break; |
| 2823 | case L2CAP_EV_EXPLICIT_POLL: |
| 2824 | /* Ignore */ |
| 2825 | break; |
| 2826 | case L2CAP_EV_MONITOR_TO: |
| 2827 | if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) { |
| 2828 | l2cap_send_rr_or_rnr(chan, 1); |
| 2829 | __set_monitor_timer(chan); |
| 2830 | chan->retry_count++; |
| 2831 | } else { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 2832 | l2cap_send_disconn_req(chan, ECONNABORTED); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2833 | } |
| 2834 | break; |
| 2835 | default: |
| 2836 | break; |
| 2837 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2838 | } |
| 2839 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2840 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 2841 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2842 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2843 | BT_DBG("chan %p, control %p, skbs %p, event %d, state %d", |
| 2844 | chan, control, skbs, event, chan->tx_state); |
| 2845 | |
| 2846 | switch (chan->tx_state) { |
| 2847 | case L2CAP_TX_STATE_XMIT: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2848 | l2cap_tx_state_xmit(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2849 | break; |
| 2850 | case L2CAP_TX_STATE_WAIT_F: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2851 | l2cap_tx_state_wait_f(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2852 | break; |
| 2853 | default: |
| 2854 | /* Ignore event */ |
| 2855 | break; |
| 2856 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2857 | } |
| 2858 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2859 | static void l2cap_pass_to_tx(struct l2cap_chan *chan, |
| 2860 | struct l2cap_ctrl *control) |
| 2861 | { |
| 2862 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2863 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT); |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2864 | } |
| 2865 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2866 | static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan, |
| 2867 | struct l2cap_ctrl *control) |
| 2868 | { |
| 2869 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2870 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2871 | } |
| 2872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | /* Copy frame to all raw sockets on that connection */ |
| 2874 | static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) |
| 2875 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | struct sk_buff *nskb; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2877 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2878 | |
| 2879 | BT_DBG("conn %p", conn); |
| 2880 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2881 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2882 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2883 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2884 | if (chan->chan_type != L2CAP_CHAN_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | continue; |
| 2886 | |
Gustavo Padovan | 7f5396a | 2013-10-21 18:22:25 -0200 | [diff] [blame] | 2887 | /* Don't send frame to the channel it came from */ |
| 2888 | if (bt_cb(skb)->chan == chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2889 | continue; |
Gustavo Padovan | 7f5396a | 2013-10-21 18:22:25 -0200 | [diff] [blame] | 2890 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2891 | nskb = skb_clone(skb, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 2892 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2893 | continue; |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 2894 | if (chan->ops->recv(chan, nskb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | kfree_skb(nskb); |
| 2896 | } |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2897 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2898 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | } |
| 2900 | |
| 2901 | /* ---- L2CAP signalling commands ---- */ |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2902 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code, |
| 2903 | u8 ident, u16 dlen, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | { |
| 2905 | struct sk_buff *skb, **frag; |
| 2906 | struct l2cap_cmd_hdr *cmd; |
| 2907 | struct l2cap_hdr *lh; |
| 2908 | int len, count; |
| 2909 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2910 | BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u", |
| 2911 | conn, code, ident, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | |
Anderson Lizardo | 300b962 | 2013-06-02 16:30:40 -0400 | [diff] [blame] | 2913 | if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE) |
| 2914 | return NULL; |
| 2915 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; |
| 2917 | count = min_t(unsigned int, conn->mtu, len); |
| 2918 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2919 | skb = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | if (!skb) |
| 2921 | return NULL; |
| 2922 | |
| 2923 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2924 | lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2925 | |
| 2926 | if (conn->hcon->type == LE_LINK) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 2927 | lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2928 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 2929 | lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | |
| 2931 | cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); |
| 2932 | cmd->code = code; |
| 2933 | cmd->ident = ident; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2934 | cmd->len = cpu_to_le16(dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | |
| 2936 | if (dlen) { |
| 2937 | count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; |
| 2938 | memcpy(skb_put(skb, count), data, count); |
| 2939 | data += count; |
| 2940 | } |
| 2941 | |
| 2942 | len -= skb->len; |
| 2943 | |
| 2944 | /* Continuation fragments (no L2CAP header) */ |
| 2945 | frag = &skb_shinfo(skb)->frag_list; |
| 2946 | while (len) { |
| 2947 | count = min_t(unsigned int, conn->mtu, len); |
| 2948 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2949 | *frag = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | if (!*frag) |
| 2951 | goto fail; |
| 2952 | |
| 2953 | memcpy(skb_put(*frag, count), data, count); |
| 2954 | |
| 2955 | len -= count; |
| 2956 | data += count; |
| 2957 | |
| 2958 | frag = &(*frag)->next; |
| 2959 | } |
| 2960 | |
| 2961 | return skb; |
| 2962 | |
| 2963 | fail: |
| 2964 | kfree_skb(skb); |
| 2965 | return NULL; |
| 2966 | } |
| 2967 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2968 | static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, |
| 2969 | unsigned long *val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | { |
| 2971 | struct l2cap_conf_opt *opt = *ptr; |
| 2972 | int len; |
| 2973 | |
| 2974 | len = L2CAP_CONF_OPT_SIZE + opt->len; |
| 2975 | *ptr += len; |
| 2976 | |
| 2977 | *type = opt->type; |
| 2978 | *olen = opt->len; |
| 2979 | |
| 2980 | switch (opt->len) { |
| 2981 | case 1: |
| 2982 | *val = *((u8 *) opt->val); |
| 2983 | break; |
| 2984 | |
| 2985 | case 2: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2986 | *val = get_unaligned_le16(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | break; |
| 2988 | |
| 2989 | case 4: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2990 | *val = get_unaligned_le32(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2991 | break; |
| 2992 | |
| 2993 | default: |
| 2994 | *val = (unsigned long) opt->val; |
| 2995 | break; |
| 2996 | } |
| 2997 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2998 | 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] | 2999 | return len; |
| 3000 | } |
| 3001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) |
| 3003 | { |
| 3004 | struct l2cap_conf_opt *opt = *ptr; |
| 3005 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 3006 | 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] | 3007 | |
| 3008 | opt->type = type; |
| 3009 | opt->len = len; |
| 3010 | |
| 3011 | switch (len) { |
| 3012 | case 1: |
| 3013 | *((u8 *) opt->val) = val; |
| 3014 | break; |
| 3015 | |
| 3016 | case 2: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 3017 | put_unaligned_le16(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3018 | break; |
| 3019 | |
| 3020 | case 4: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 3021 | put_unaligned_le32(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | break; |
| 3023 | |
| 3024 | default: |
| 3025 | memcpy(opt->val, (void *) val, len); |
| 3026 | break; |
| 3027 | } |
| 3028 | |
| 3029 | *ptr += L2CAP_CONF_OPT_SIZE + len; |
| 3030 | } |
| 3031 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3032 | static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) |
| 3033 | { |
| 3034 | struct l2cap_conf_efs efs; |
| 3035 | |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 3036 | switch (chan->mode) { |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3037 | case L2CAP_MODE_ERTM: |
| 3038 | efs.id = chan->local_id; |
| 3039 | efs.stype = chan->local_stype; |
| 3040 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 3041 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3042 | efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); |
| 3043 | efs.flush_to = cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3044 | break; |
| 3045 | |
| 3046 | case L2CAP_MODE_STREAMING: |
| 3047 | efs.id = 1; |
| 3048 | efs.stype = L2CAP_SERV_BESTEFFORT; |
| 3049 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 3050 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
| 3051 | efs.acc_lat = 0; |
| 3052 | efs.flush_to = 0; |
| 3053 | break; |
| 3054 | |
| 3055 | default: |
| 3056 | return; |
| 3057 | } |
| 3058 | |
| 3059 | l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 3060 | (unsigned long) &efs); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3061 | } |
| 3062 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3063 | static void l2cap_ack_timeout(struct work_struct *work) |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3064 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3065 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3066 | ack_timer.work); |
| 3067 | u16 frames_to_ack; |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3068 | |
Gustavo F. Padovan | 2fb9b3d | 2011-12-22 16:56:05 -0200 | [diff] [blame] | 3069 | BT_DBG("chan %p", chan); |
| 3070 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3071 | l2cap_chan_lock(chan); |
| 3072 | |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3073 | frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 3074 | chan->last_acked_seq); |
| 3075 | |
| 3076 | if (frames_to_ack) |
| 3077 | l2cap_send_rr_or_rnr(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3078 | |
| 3079 | l2cap_chan_unlock(chan); |
Szymon Janc | 09bfb2e | 2012-01-11 10:59:49 +0100 | [diff] [blame] | 3080 | l2cap_chan_put(chan); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3081 | } |
| 3082 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 3083 | int l2cap_ertm_init(struct l2cap_chan *chan) |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3084 | { |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3085 | int err; |
| 3086 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3087 | chan->next_tx_seq = 0; |
| 3088 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3089 | chan->expected_ack_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3090 | chan->unacked_frames = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3091 | chan->buffer_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3092 | chan->frames_sent = 0; |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3093 | chan->last_acked_seq = 0; |
| 3094 | chan->sdu = NULL; |
| 3095 | chan->sdu_last_frag = NULL; |
| 3096 | chan->sdu_len = 0; |
| 3097 | |
Mat Martineau | d34c34f | 2012-05-14 14:49:27 -0700 | [diff] [blame] | 3098 | skb_queue_head_init(&chan->tx_q); |
| 3099 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3100 | chan->local_amp_id = AMP_ID_BREDR; |
| 3101 | chan->move_id = AMP_ID_BREDR; |
Mat Martineau | 0833328 | 2012-10-23 15:24:06 -0700 | [diff] [blame] | 3102 | chan->move_state = L2CAP_MOVE_STABLE; |
| 3103 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 3104 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3105 | if (chan->mode != L2CAP_MODE_ERTM) |
| 3106 | return 0; |
| 3107 | |
| 3108 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 3109 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3110 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3111 | INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); |
| 3112 | INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); |
| 3113 | INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3114 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3115 | skb_queue_head_init(&chan->srej_q); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3116 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3117 | err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win); |
| 3118 | if (err < 0) |
| 3119 | return err; |
| 3120 | |
Mat Martineau | 9dc9aff | 2012-05-17 16:20:14 -0700 | [diff] [blame] | 3121 | err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win); |
| 3122 | if (err < 0) |
| 3123 | l2cap_seq_list_free(&chan->srej_list); |
| 3124 | |
| 3125 | return err; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3126 | } |
| 3127 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3128 | static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) |
| 3129 | { |
| 3130 | switch (mode) { |
| 3131 | case L2CAP_MODE_STREAMING: |
| 3132 | case L2CAP_MODE_ERTM: |
| 3133 | if (l2cap_mode_supported(mode, remote_feat_mask)) |
| 3134 | return mode; |
| 3135 | /* fall through */ |
| 3136 | default: |
| 3137 | return L2CAP_MODE_BASIC; |
| 3138 | } |
| 3139 | } |
| 3140 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3141 | static inline bool __l2cap_ews_supported(struct l2cap_conn *conn) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3142 | { |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3143 | return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3144 | } |
| 3145 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3146 | static inline bool __l2cap_efs_supported(struct l2cap_conn *conn) |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3147 | { |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3148 | return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_FLOW; |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3149 | } |
| 3150 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3151 | static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan, |
| 3152 | struct l2cap_conf_rfc *rfc) |
| 3153 | { |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3154 | if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) { |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3155 | u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to; |
| 3156 | |
| 3157 | /* Class 1 devices have must have ERTM timeouts |
| 3158 | * exceeding the Link Supervision Timeout. The |
| 3159 | * default Link Supervision Timeout for AMP |
| 3160 | * controllers is 10 seconds. |
| 3161 | * |
| 3162 | * Class 1 devices use 0xffffffff for their |
| 3163 | * best-effort flush timeout, so the clamping logic |
| 3164 | * will result in a timeout that meets the above |
| 3165 | * requirement. ERTM timeouts are 16-bit values, so |
| 3166 | * the maximum timeout is 65.535 seconds. |
| 3167 | */ |
| 3168 | |
| 3169 | /* Convert timeout to milliseconds and round */ |
| 3170 | ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000); |
| 3171 | |
| 3172 | /* This is the recommended formula for class 2 devices |
| 3173 | * that start ERTM timers when packets are sent to the |
| 3174 | * controller. |
| 3175 | */ |
| 3176 | ertm_to = 3 * ertm_to + 500; |
| 3177 | |
| 3178 | if (ertm_to > 0xffff) |
| 3179 | ertm_to = 0xffff; |
| 3180 | |
| 3181 | rfc->retrans_timeout = cpu_to_le16((u16) ertm_to); |
| 3182 | rfc->monitor_timeout = rfc->retrans_timeout; |
| 3183 | } else { |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3184 | rfc->retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); |
| 3185 | rfc->monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3186 | } |
| 3187 | } |
| 3188 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3189 | static inline void l2cap_txwin_setup(struct l2cap_chan *chan) |
| 3190 | { |
| 3191 | if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3192 | __l2cap_ews_supported(chan->conn)) { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3193 | /* use extended control field */ |
| 3194 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3195 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
| 3196 | } else { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3197 | chan->tx_win = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3198 | L2CAP_DEFAULT_TX_WINDOW); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3199 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
| 3200 | } |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3201 | chan->ack_win = chan->tx_win; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3202 | } |
| 3203 | |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3204 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3205 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | struct l2cap_conf_req *req = data; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3207 | struct l2cap_conf_rfc rfc = { .mode = chan->mode }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | void *ptr = req->data; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3209 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3211 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3213 | if (chan->num_conf_req || chan->num_conf_rsp) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3214 | goto done; |
| 3215 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3216 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3217 | case L2CAP_MODE_STREAMING: |
| 3218 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3219 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3220 | break; |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3221 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3222 | if (__l2cap_efs_supported(chan->conn)) |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3223 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3224 | |
Gustavo F. Padovan | 2ba13ed | 2010-06-09 16:39:05 -0300 | [diff] [blame] | 3225 | /* fall through */ |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3226 | default: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3227 | chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3228 | break; |
| 3229 | } |
| 3230 | |
| 3231 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3232 | if (chan->imtu != L2CAP_DEFAULT_MTU) |
| 3233 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | 7990681c | 2011-01-24 16:01:43 -0200 | [diff] [blame] | 3234 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3235 | switch (chan->mode) { |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3236 | case L2CAP_MODE_BASIC: |
Marcel Holtmann | 6fea7ad | 2014-07-09 11:53:35 +0200 | [diff] [blame] | 3237 | if (disable_ertm) |
| 3238 | break; |
| 3239 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3240 | if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3241 | !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3242 | break; |
| 3243 | |
Gustavo F. Padovan | 6254775 | 2010-06-08 20:05:31 -0300 | [diff] [blame] | 3244 | rfc.mode = L2CAP_MODE_BASIC; |
| 3245 | rfc.txwin_size = 0; |
| 3246 | rfc.max_transmit = 0; |
| 3247 | rfc.retrans_timeout = 0; |
| 3248 | rfc.monitor_timeout = 0; |
| 3249 | rfc.max_pdu_size = 0; |
| 3250 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3251 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3252 | (unsigned long) &rfc); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3253 | break; |
| 3254 | |
| 3255 | case L2CAP_MODE_ERTM: |
| 3256 | rfc.mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3257 | rfc.max_transmit = chan->max_tx; |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3258 | |
| 3259 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3260 | |
| 3261 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3262 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3263 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3264 | rfc.max_pdu_size = cpu_to_le16(size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3265 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3266 | l2cap_txwin_setup(chan); |
| 3267 | |
| 3268 | rfc.txwin_size = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3269 | L2CAP_DEFAULT_TX_WINDOW); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3270 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3271 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3272 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3273 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3274 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3275 | l2cap_add_opt_efs(&ptr, chan); |
| 3276 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3277 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3278 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3279 | chan->tx_win); |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3280 | |
| 3281 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3282 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3283 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3284 | chan->fcs = L2CAP_FCS_NONE; |
| 3285 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3286 | chan->fcs); |
| 3287 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3288 | break; |
| 3289 | |
| 3290 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 273759e | 2012-05-17 20:53:53 -0700 | [diff] [blame] | 3291 | l2cap_txwin_setup(chan); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3292 | rfc.mode = L2CAP_MODE_STREAMING; |
| 3293 | rfc.txwin_size = 0; |
| 3294 | rfc.max_transmit = 0; |
| 3295 | rfc.retrans_timeout = 0; |
| 3296 | rfc.monitor_timeout = 0; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3297 | |
| 3298 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3299 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3300 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3301 | rfc.max_pdu_size = cpu_to_le16(size); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3302 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3303 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3304 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3305 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3306 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3307 | l2cap_add_opt_efs(&ptr, chan); |
| 3308 | |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3309 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3310 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3311 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3312 | chan->fcs = L2CAP_FCS_NONE; |
| 3313 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3314 | chan->fcs); |
| 3315 | } |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3316 | break; |
| 3317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3318 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3319 | req->dcid = cpu_to_le16(chan->dcid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3320 | req->flags = cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3321 | |
| 3322 | return ptr - data; |
| 3323 | } |
| 3324 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3325 | static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3327 | struct l2cap_conf_rsp *rsp = data; |
| 3328 | void *ptr = rsp->data; |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3329 | void *req = chan->conf_req; |
| 3330 | int len = chan->conf_len; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3331 | int type, hint, olen; |
| 3332 | unsigned long val; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3333 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3334 | struct l2cap_conf_efs efs; |
| 3335 | u8 remote_efs = 0; |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3336 | u16 mtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3337 | u16 result = L2CAP_CONF_SUCCESS; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3338 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3339 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3340 | BT_DBG("chan %p", chan); |
Marcel Holtmann | 820ae1b | 2006-11-18 22:15:00 +0100 | [diff] [blame] | 3341 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3342 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3343 | len -= l2cap_get_conf_opt(&req, &type, &olen, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3344 | |
Gustavo F. Padovan | 589d274 | 2009-04-20 01:31:07 -0300 | [diff] [blame] | 3345 | hint = type & L2CAP_CONF_HINT; |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 3346 | type &= L2CAP_CONF_MASK; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3347 | |
| 3348 | switch (type) { |
| 3349 | case L2CAP_CONF_MTU: |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3350 | mtu = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3351 | break; |
| 3352 | |
| 3353 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3354 | chan->flush_to = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3355 | break; |
| 3356 | |
| 3357 | case L2CAP_CONF_QOS: |
| 3358 | break; |
| 3359 | |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3360 | case L2CAP_CONF_RFC: |
| 3361 | if (olen == sizeof(rfc)) |
| 3362 | memcpy(&rfc, (void *) val, olen); |
| 3363 | break; |
| 3364 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3365 | case L2CAP_CONF_FCS: |
| 3366 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3367 | set_bit(CONF_RECV_NO_FCS, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3368 | break; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3369 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3370 | case L2CAP_CONF_EFS: |
| 3371 | remote_efs = 1; |
| 3372 | if (olen == sizeof(efs)) |
| 3373 | memcpy(&efs, (void *) val, olen); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3374 | break; |
| 3375 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3376 | case L2CAP_CONF_EWS: |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3377 | if (!chan->conn->hs_enabled) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3378 | return -ECONNREFUSED; |
| 3379 | |
| 3380 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
| 3381 | set_bit(CONF_EWS_RECV, &chan->conf_state); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3382 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3383 | chan->remote_tx_win = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3384 | break; |
| 3385 | |
| 3386 | default: |
| 3387 | if (hint) |
| 3388 | break; |
| 3389 | |
| 3390 | result = L2CAP_CONF_UNKNOWN; |
| 3391 | *((u8 *) ptr++) = type; |
| 3392 | break; |
| 3393 | } |
| 3394 | } |
| 3395 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3396 | if (chan->num_conf_rsp || chan->num_conf_req > 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3397 | goto done; |
| 3398 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3399 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3400 | case L2CAP_MODE_STREAMING: |
| 3401 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3402 | if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3403 | chan->mode = l2cap_select_mode(rfc.mode, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3404 | chan->conn->feat_mask); |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3405 | break; |
| 3406 | } |
| 3407 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3408 | if (remote_efs) { |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3409 | if (__l2cap_efs_supported(chan->conn)) |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3410 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3411 | else |
| 3412 | return -ECONNREFUSED; |
| 3413 | } |
| 3414 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3415 | if (chan->mode != rfc.mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3416 | return -ECONNREFUSED; |
Gustavo F. Padovan | 742e519 | 2010-06-08 19:09:48 -0300 | [diff] [blame] | 3417 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3418 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3419 | } |
| 3420 | |
| 3421 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3422 | if (chan->mode != rfc.mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3423 | result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3424 | rfc.mode = chan->mode; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3425 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3426 | if (chan->num_conf_rsp == 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3427 | return -ECONNREFUSED; |
| 3428 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3429 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3430 | (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3431 | } |
| 3432 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3433 | if (result == L2CAP_CONF_SUCCESS) { |
| 3434 | /* Configure output options and let the other side know |
| 3435 | * which ones we don't like. */ |
| 3436 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3437 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
| 3438 | result = L2CAP_CONF_UNACCEPT; |
| 3439 | else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3440 | chan->omtu = mtu; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3441 | set_bit(CONF_MTU_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3442 | } |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3443 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3444 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3445 | if (remote_efs) { |
| 3446 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3447 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3448 | efs.stype != chan->local_stype) { |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3449 | |
| 3450 | result = L2CAP_CONF_UNACCEPT; |
| 3451 | |
| 3452 | if (chan->num_conf_req >= 1) |
| 3453 | return -ECONNREFUSED; |
| 3454 | |
| 3455 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3456 | sizeof(efs), |
| 3457 | (unsigned long) &efs); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3458 | } else { |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3459 | /* Send PENDING Conf Rsp */ |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3460 | result = L2CAP_CONF_PENDING; |
| 3461 | set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3462 | } |
| 3463 | } |
| 3464 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3465 | switch (rfc.mode) { |
| 3466 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3467 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3468 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3469 | break; |
| 3470 | |
| 3471 | case L2CAP_MODE_ERTM: |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3472 | if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) |
| 3473 | chan->remote_tx_win = rfc.txwin_size; |
| 3474 | else |
| 3475 | rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; |
| 3476 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 3477 | chan->remote_max_tx = rfc.max_transmit; |
Mat Martineau | 86b1b26 | 2010-08-05 15:54:22 -0700 | [diff] [blame] | 3478 | |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3479 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3480 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3481 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3482 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3483 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3484 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3485 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3486 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3487 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3488 | |
| 3489 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3490 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3491 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3492 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3493 | chan->remote_id = efs.id; |
| 3494 | chan->remote_stype = efs.stype; |
| 3495 | chan->remote_msdu = le16_to_cpu(efs.msdu); |
| 3496 | chan->remote_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3497 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3498 | chan->remote_acc_lat = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3499 | le32_to_cpu(efs.acc_lat); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3500 | chan->remote_sdu_itime = |
| 3501 | le32_to_cpu(efs.sdu_itime); |
| 3502 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3503 | sizeof(efs), |
| 3504 | (unsigned long) &efs); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3505 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3506 | break; |
| 3507 | |
| 3508 | case L2CAP_MODE_STREAMING: |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3509 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3510 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3511 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3512 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3513 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3514 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3515 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3516 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3517 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3518 | (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3519 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3520 | break; |
| 3521 | |
| 3522 | default: |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3523 | result = L2CAP_CONF_UNACCEPT; |
| 3524 | |
| 3525 | memset(&rfc, 0, sizeof(rfc)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3526 | rfc.mode = chan->mode; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3527 | } |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3528 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3529 | if (result == L2CAP_CONF_SUCCESS) |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3530 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3531 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3532 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3533 | rsp->result = cpu_to_le16(result); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3534 | rsp->flags = cpu_to_le16(0); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3535 | |
| 3536 | return ptr - data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | } |
| 3538 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3539 | static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, |
| 3540 | void *data, u16 *result) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3541 | { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3542 | struct l2cap_conf_req *req = data; |
| 3543 | void *ptr = req->data; |
| 3544 | int type, olen; |
| 3545 | unsigned long val; |
Mat Martineau | 36e999a | 2011-12-08 17:23:21 -0800 | [diff] [blame] | 3546 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3547 | struct l2cap_conf_efs efs; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3548 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3549 | 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] | 3550 | |
| 3551 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3552 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3553 | |
| 3554 | switch (type) { |
| 3555 | case L2CAP_CONF_MTU: |
| 3556 | if (val < L2CAP_DEFAULT_MIN_MTU) { |
| 3557 | *result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3558 | chan->imtu = L2CAP_DEFAULT_MIN_MTU; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3559 | } else |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3560 | chan->imtu = val; |
| 3561 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3562 | break; |
| 3563 | |
| 3564 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3565 | chan->flush_to = val; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3566 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3567 | 2, chan->flush_to); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3568 | break; |
| 3569 | |
| 3570 | case L2CAP_CONF_RFC: |
| 3571 | if (olen == sizeof(rfc)) |
| 3572 | memcpy(&rfc, (void *)val, olen); |
| 3573 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3574 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3575 | rfc.mode != chan->mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3576 | return -ECONNREFUSED; |
| 3577 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3578 | chan->fcs = 0; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3579 | |
| 3580 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3581 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3582 | break; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3583 | |
| 3584 | case L2CAP_CONF_EWS: |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3585 | chan->ack_win = min_t(u16, val, chan->ack_win); |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3586 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3587 | chan->tx_win); |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3588 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3589 | |
| 3590 | case L2CAP_CONF_EFS: |
| 3591 | if (olen == sizeof(efs)) |
| 3592 | memcpy(&efs, (void *)val, olen); |
| 3593 | |
| 3594 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3595 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3596 | efs.stype != chan->local_stype) |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3597 | return -ECONNREFUSED; |
| 3598 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3599 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs), |
| 3600 | (unsigned long) &efs); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3601 | break; |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3602 | |
| 3603 | case L2CAP_CONF_FCS: |
| 3604 | if (*result == L2CAP_CONF_PENDING) |
| 3605 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3606 | set_bit(CONF_RECV_NO_FCS, |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3607 | &chan->conf_state); |
| 3608 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3609 | } |
| 3610 | } |
| 3611 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3612 | if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3613 | return -ECONNREFUSED; |
| 3614 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3615 | chan->mode = rfc.mode; |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3616 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3617 | if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3618 | switch (rfc.mode) { |
| 3619 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3620 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3621 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 3622 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3623 | if (!test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3624 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3625 | rfc.txwin_size); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3626 | |
| 3627 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3628 | chan->local_msdu = le16_to_cpu(efs.msdu); |
| 3629 | chan->local_sdu_itime = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3630 | le32_to_cpu(efs.sdu_itime); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3631 | chan->local_acc_lat = le32_to_cpu(efs.acc_lat); |
| 3632 | chan->local_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3633 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3634 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3635 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3636 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3637 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3638 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3639 | } |
| 3640 | } |
| 3641 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3642 | req->dcid = cpu_to_le16(chan->dcid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3643 | req->flags = cpu_to_le16(0); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3644 | |
| 3645 | return ptr - data; |
| 3646 | } |
| 3647 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3648 | static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, |
| 3649 | u16 result, u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3650 | { |
| 3651 | struct l2cap_conf_rsp *rsp = data; |
| 3652 | void *ptr = rsp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3654 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3655 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3656 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3657 | rsp->result = cpu_to_le16(result); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3658 | rsp->flags = cpu_to_le16(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3659 | |
| 3660 | return ptr - data; |
| 3661 | } |
| 3662 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 3663 | void __l2cap_le_connect_rsp_defer(struct l2cap_chan *chan) |
| 3664 | { |
| 3665 | struct l2cap_le_conn_rsp rsp; |
| 3666 | struct l2cap_conn *conn = chan->conn; |
| 3667 | |
| 3668 | BT_DBG("chan %p", chan); |
| 3669 | |
| 3670 | rsp.dcid = cpu_to_le16(chan->scid); |
| 3671 | rsp.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 3672 | rsp.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 3673 | rsp.credits = cpu_to_le16(chan->rx_credits); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3674 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 3675 | |
| 3676 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), |
| 3677 | &rsp); |
| 3678 | } |
| 3679 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3680 | void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3681 | { |
| 3682 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3683 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3684 | u8 buf[128]; |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3685 | u8 rsp_code; |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3686 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3687 | rsp.scid = cpu_to_le16(chan->dcid); |
| 3688 | rsp.dcid = cpu_to_le16(chan->scid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3689 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 3690 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3691 | |
| 3692 | if (chan->hs_hcon) |
| 3693 | rsp_code = L2CAP_CREATE_CHAN_RSP; |
| 3694 | else |
| 3695 | rsp_code = L2CAP_CONN_RSP; |
| 3696 | |
| 3697 | BT_DBG("chan %p rsp_code %u", chan, rsp_code); |
| 3698 | |
| 3699 | l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3700 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3701 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3702 | return; |
| 3703 | |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3704 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3705 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3706 | chan->num_conf_req++; |
| 3707 | } |
| 3708 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3709 | 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] | 3710 | { |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3711 | int type, olen; |
| 3712 | unsigned long val; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3713 | /* Use sane default values in case a misbehaving remote device |
| 3714 | * did not send an RFC or extended window size option. |
| 3715 | */ |
| 3716 | u16 txwin_ext = chan->ack_win; |
| 3717 | struct l2cap_conf_rfc rfc = { |
| 3718 | .mode = chan->mode, |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3719 | .retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO), |
| 3720 | .monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO), |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3721 | .max_pdu_size = cpu_to_le16(chan->imtu), |
| 3722 | .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW), |
| 3723 | }; |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3724 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3725 | BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3726 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3727 | if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3728 | return; |
| 3729 | |
| 3730 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3731 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3732 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3733 | switch (type) { |
| 3734 | case L2CAP_CONF_RFC: |
| 3735 | if (olen == sizeof(rfc)) |
| 3736 | memcpy(&rfc, (void *)val, olen); |
Szymon Janc | 8f321f8 | 2012-06-08 11:33:33 +0200 | [diff] [blame] | 3737 | break; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3738 | case L2CAP_CONF_EWS: |
| 3739 | txwin_ext = val; |
| 3740 | break; |
| 3741 | } |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3742 | } |
| 3743 | |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3744 | switch (rfc.mode) { |
| 3745 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3746 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3747 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3748 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
| 3749 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3750 | chan->ack_win = min_t(u16, chan->ack_win, txwin_ext); |
| 3751 | else |
| 3752 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3753 | rfc.txwin_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3754 | break; |
| 3755 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3756 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3757 | } |
| 3758 | } |
| 3759 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3760 | static inline int l2cap_command_rej(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3761 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3762 | u8 *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3763 | { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3764 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3765 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3766 | if (cmd_len < sizeof(*rej)) |
| 3767 | return -EPROTO; |
| 3768 | |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3769 | if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3770 | return 0; |
| 3771 | |
| 3772 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3773 | cmd->ident == conn->info_ident) { |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 3774 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3775 | |
| 3776 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3777 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3778 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3779 | l2cap_conn_start(conn); |
| 3780 | } |
| 3781 | |
| 3782 | return 0; |
| 3783 | } |
| 3784 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3785 | static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn, |
| 3786 | struct l2cap_cmd_hdr *cmd, |
| 3787 | u8 *data, u8 rsp_code, u8 amp_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3788 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; |
| 3790 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3791 | struct l2cap_chan *chan = NULL, *pchan; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3792 | int result, status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3793 | |
| 3794 | u16 dcid = 0, scid = __le16_to_cpu(req->scid); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3795 | __le16 psm = req->psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 3797 | 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] | 3798 | |
| 3799 | /* Check if we have socket listening on psm */ |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 3800 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src, |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 3801 | &conn->hcon->dst, ACL_LINK); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3802 | if (!pchan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3803 | result = L2CAP_CR_BAD_PSM; |
| 3804 | goto sendresp; |
| 3805 | } |
| 3806 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3807 | mutex_lock(&conn->chan_lock); |
Gustavo Padovan | 8ffb929 | 2013-10-21 14:21:41 -0200 | [diff] [blame] | 3808 | l2cap_chan_lock(pchan); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 3809 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3810 | /* Check if the ACL is secure enough (if not SDP) */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3811 | if (psm != cpu_to_le16(L2CAP_PSM_SDP) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3812 | !hci_conn_check_link_mode(conn->hcon)) { |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 3813 | conn->disc_reason = HCI_ERROR_AUTH_FAILURE; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3814 | result = L2CAP_CR_SEC_BLOCK; |
| 3815 | goto response; |
| 3816 | } |
| 3817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3818 | result = L2CAP_CR_NO_MEM; |
| 3819 | |
Gustavo Padovan | 2dfa100 | 2012-05-27 22:27:58 -0300 | [diff] [blame] | 3820 | /* Check if we already have channel with that dcid */ |
| 3821 | if (__l2cap_get_chan_by_dcid(conn, scid)) |
| 3822 | goto response; |
| 3823 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 3824 | chan = pchan->ops->new_connection(pchan); |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 3825 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3826 | goto response; |
| 3827 | |
Syam Sidhardhan | 330b6c1 | 2013-08-06 01:59:12 +0900 | [diff] [blame] | 3828 | /* For certain devices (ex: HID mouse), support for authentication, |
| 3829 | * pairing and bonding is optional. For such devices, inorder to avoid |
| 3830 | * the ACL alive for too long after L2CAP disconnection, reset the ACL |
| 3831 | * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect. |
| 3832 | */ |
| 3833 | conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT; |
| 3834 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 3835 | bacpy(&chan->src, &conn->hcon->src); |
| 3836 | bacpy(&chan->dst, &conn->hcon->dst); |
Marcel Holtmann | 4f1654e | 2013-10-13 08:50:41 -0700 | [diff] [blame] | 3837 | chan->src_type = bdaddr_type(conn->hcon, conn->hcon->src_type); |
| 3838 | chan->dst_type = bdaddr_type(conn->hcon, conn->hcon->dst_type); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3839 | chan->psm = psm; |
| 3840 | chan->dcid = scid; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3841 | chan->local_amp_id = amp_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3842 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3843 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3844 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3845 | dcid = chan->scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3846 | |
Gustavo Padovan | 8d836d7 | 2013-10-15 19:24:47 -0300 | [diff] [blame] | 3847 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3848 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3849 | chan->ident = cmd->ident; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3850 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3851 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 3852 | if (l2cap_chan_check_security(chan)) { |
Marcel Holtmann | bdc2578 | 2013-10-14 02:45:34 -0700 | [diff] [blame] | 3853 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3854 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3855 | result = L2CAP_CR_PEND; |
| 3856 | status = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 3857 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3858 | } else { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3859 | /* Force pending result for AMP controllers. |
| 3860 | * The connection will succeed after the |
| 3861 | * physical link is up. |
| 3862 | */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3863 | if (amp_id == AMP_ID_BREDR) { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3864 | l2cap_state_change(chan, BT_CONFIG); |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3865 | result = L2CAP_CR_SUCCESS; |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3866 | } else { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3867 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3868 | result = L2CAP_CR_PEND; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3869 | } |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3870 | status = L2CAP_CS_NO_INFO; |
| 3871 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3872 | } else { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3873 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3874 | result = L2CAP_CR_PEND; |
| 3875 | status = L2CAP_CS_AUTHEN_PEND; |
| 3876 | } |
| 3877 | } else { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 3878 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3879 | result = L2CAP_CR_PEND; |
| 3880 | status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | } |
| 3882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3883 | response: |
Gustavo Padovan | 8ffb929 | 2013-10-21 14:21:41 -0200 | [diff] [blame] | 3884 | l2cap_chan_unlock(pchan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3885 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3886 | |
| 3887 | sendresp: |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3888 | rsp.scid = cpu_to_le16(scid); |
| 3889 | rsp.dcid = cpu_to_le16(dcid); |
| 3890 | rsp.result = cpu_to_le16(result); |
| 3891 | rsp.status = cpu_to_le16(status); |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3892 | l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3893 | |
| 3894 | if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { |
| 3895 | struct l2cap_info_req info; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 3896 | info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3897 | |
| 3898 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 3899 | conn->info_ident = l2cap_get_ident(conn); |
| 3900 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 3901 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3902 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3903 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 3904 | sizeof(info), &info); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3905 | } |
| 3906 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3907 | if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3908 | result == L2CAP_CR_SUCCESS) { |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3909 | u8 buf[128]; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3910 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3911 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3912 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3913 | chan->num_conf_req++; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3914 | } |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3915 | |
| 3916 | return chan; |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3917 | } |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3918 | |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3919 | static int l2cap_connect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3920 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3921 | { |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3922 | struct hci_dev *hdev = conn->hcon->hdev; |
| 3923 | struct hci_conn *hcon = conn->hcon; |
| 3924 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3925 | if (cmd_len < sizeof(struct l2cap_conn_req)) |
| 3926 | return -EPROTO; |
| 3927 | |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3928 | hci_dev_lock(hdev); |
| 3929 | if (test_bit(HCI_MGMT, &hdev->dev_flags) && |
| 3930 | !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags)) |
| 3931 | mgmt_device_connected(hdev, &hcon->dst, hcon->type, |
| 3932 | hcon->dst_type, 0, NULL, 0, |
| 3933 | hcon->dev_class); |
| 3934 | hci_dev_unlock(hdev); |
| 3935 | |
Gustavo Padovan | 300229f | 2012-10-12 19:40:40 +0800 | [diff] [blame] | 3936 | l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3937 | return 0; |
| 3938 | } |
| 3939 | |
Mat Martineau | 5909cf3 | 2012-10-23 15:24:08 -0700 | [diff] [blame] | 3940 | static int l2cap_connect_create_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3941 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3942 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3943 | { |
| 3944 | struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; |
| 3945 | u16 scid, dcid, result, status; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3946 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3947 | u8 req[128]; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3948 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3949 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3950 | if (cmd_len < sizeof(*rsp)) |
| 3951 | return -EPROTO; |
| 3952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | scid = __le16_to_cpu(rsp->scid); |
| 3954 | dcid = __le16_to_cpu(rsp->dcid); |
| 3955 | result = __le16_to_cpu(rsp->result); |
| 3956 | status = __le16_to_cpu(rsp->status); |
| 3957 | |
Andrei Emeltchenko | 1b009c9 | 2012-02-21 12:54:54 +0200 | [diff] [blame] | 3958 | 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] | 3959 | dcid, scid, result, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3961 | mutex_lock(&conn->chan_lock); |
| 3962 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | if (scid) { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3964 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 3965 | if (!chan) { |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 3966 | err = -EBADSLT; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3967 | goto unlock; |
| 3968 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3969 | } else { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3970 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 3971 | if (!chan) { |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 3972 | err = -EBADSLT; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3973 | goto unlock; |
| 3974 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3975 | } |
| 3976 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3977 | err = 0; |
| 3978 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3979 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3981 | switch (result) { |
| 3982 | case L2CAP_CR_SUCCESS: |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 3983 | l2cap_state_change(chan, BT_CONFIG); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3984 | chan->ident = 0; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3985 | chan->dcid = dcid; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3986 | clear_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 3987 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3988 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3989 | break; |
| 3990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3992 | l2cap_build_conf_req(chan, req), req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3993 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3994 | break; |
| 3995 | |
| 3996 | case L2CAP_CR_PEND: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3997 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3998 | break; |
| 3999 | |
| 4000 | default: |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4001 | l2cap_chan_del(chan, ECONNREFUSED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4002 | break; |
| 4003 | } |
| 4004 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4005 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4006 | |
| 4007 | unlock: |
| 4008 | mutex_unlock(&conn->chan_lock); |
| 4009 | |
| 4010 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | } |
| 4012 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4013 | static inline void set_default_fcs(struct l2cap_chan *chan) |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 4014 | { |
| 4015 | /* FCS is enabled only in ERTM or streaming mode, if one or both |
| 4016 | * sides request it. |
| 4017 | */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4018 | if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4019 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 4020 | else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4021 | chan->fcs = L2CAP_FCS_CRC16; |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 4022 | } |
| 4023 | |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 4024 | static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data, |
| 4025 | u8 ident, u16 flags) |
| 4026 | { |
| 4027 | struct l2cap_conn *conn = chan->conn; |
| 4028 | |
| 4029 | BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident, |
| 4030 | flags); |
| 4031 | |
| 4032 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
| 4033 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
| 4034 | |
| 4035 | l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP, |
| 4036 | l2cap_build_conf_rsp(chan, data, |
| 4037 | L2CAP_CONF_SUCCESS, flags), data); |
| 4038 | } |
| 4039 | |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4040 | static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident, |
| 4041 | u16 scid, u16 dcid) |
| 4042 | { |
| 4043 | struct l2cap_cmd_rej_cid rej; |
| 4044 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4045 | rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4046 | rej.scid = __cpu_to_le16(scid); |
| 4047 | rej.dcid = __cpu_to_le16(dcid); |
| 4048 | |
| 4049 | l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); |
| 4050 | } |
| 4051 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4052 | static inline int l2cap_config_req(struct l2cap_conn *conn, |
| 4053 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4054 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4055 | { |
| 4056 | struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; |
| 4057 | u16 dcid, flags; |
| 4058 | u8 rsp[64]; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4059 | struct l2cap_chan *chan; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4060 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4062 | if (cmd_len < sizeof(*req)) |
| 4063 | return -EPROTO; |
| 4064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4065 | dcid = __le16_to_cpu(req->dcid); |
| 4066 | flags = __le16_to_cpu(req->flags); |
| 4067 | |
| 4068 | BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); |
| 4069 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4070 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4071 | if (!chan) { |
| 4072 | cmd_reject_invalid_cid(conn, cmd->ident, dcid, 0); |
| 4073 | return 0; |
| 4074 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4075 | |
David S. Miller | 033b114 | 2011-07-21 13:38:42 -0700 | [diff] [blame] | 4076 | if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4077 | cmd_reject_invalid_cid(conn, cmd->ident, chan->scid, |
| 4078 | chan->dcid); |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 4079 | goto unlock; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 4080 | } |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 4081 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4082 | /* Reject if config buffer is too small. */ |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 4083 | len = cmd_len - sizeof(*req); |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4084 | if (chan->conf_len + len > sizeof(chan->conf_req)) { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4085 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4086 | l2cap_build_conf_rsp(chan, rsp, |
| 4087 | L2CAP_CONF_REJECT, flags), rsp); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4088 | goto unlock; |
| 4089 | } |
| 4090 | |
| 4091 | /* Store config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4092 | memcpy(chan->conf_req + chan->conf_len, req->data, len); |
| 4093 | chan->conf_len += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4095 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4096 | /* Incomplete config. Send empty response. */ |
| 4097 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4098 | l2cap_build_conf_rsp(chan, rsp, |
| 4099 | L2CAP_CONF_SUCCESS, flags), rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4100 | goto unlock; |
| 4101 | } |
| 4102 | |
| 4103 | /* Complete config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4104 | len = l2cap_parse_conf_req(chan, rsp); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4105 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4106 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4107 | goto unlock; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4109 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4110 | chan->ident = cmd->ident; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4111 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4112 | chan->num_conf_rsp++; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4113 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4114 | /* Reset config buffer. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4115 | chan->conf_len = 0; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4116 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4117 | if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4118 | goto unlock; |
| 4119 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4120 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4121 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4122 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4123 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4124 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4125 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4126 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4127 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4128 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4129 | else |
| 4130 | l2cap_chan_ready(chan); |
| 4131 | |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4132 | goto unlock; |
| 4133 | } |
| 4134 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4135 | if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 4136 | u8 buf[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4137 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4138 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4139 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | } |
| 4141 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4142 | /* Got Conf Rsp PENDING from remote side and asume we sent |
| 4143 | Conf Rsp PENDING in the code above */ |
| 4144 | if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 4145 | test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4146 | |
| 4147 | /* check compatibility */ |
| 4148 | |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4149 | /* Send rsp for BR/EDR channel */ |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4150 | if (!chan->hs_hcon) |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4151 | l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags); |
| 4152 | else |
| 4153 | chan->ident = cmd->ident; |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4154 | } |
| 4155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | unlock: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4157 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4158 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | } |
| 4160 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4161 | static inline int l2cap_config_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4162 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4163 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4164 | { |
| 4165 | struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; |
| 4166 | u16 scid, flags, result; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4167 | struct l2cap_chan *chan; |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4168 | int len = cmd_len - sizeof(*rsp); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4169 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4171 | if (cmd_len < sizeof(*rsp)) |
| 4172 | return -EPROTO; |
| 4173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4174 | scid = __le16_to_cpu(rsp->scid); |
| 4175 | flags = __le16_to_cpu(rsp->flags); |
| 4176 | result = __le16_to_cpu(rsp->result); |
| 4177 | |
Andrei Emeltchenko | 61386cb | 2012-03-12 12:13:07 +0200 | [diff] [blame] | 4178 | BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags, |
| 4179 | result, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4181 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4182 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4183 | return 0; |
| 4184 | |
| 4185 | switch (result) { |
| 4186 | case L2CAP_CONF_SUCCESS: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4187 | l2cap_conf_rfc_get(chan, rsp->data, len); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4188 | clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | break; |
| 4190 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4191 | case L2CAP_CONF_PENDING: |
| 4192 | set_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
| 4193 | |
| 4194 | if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
| 4195 | char buf[64]; |
| 4196 | |
| 4197 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4198 | buf, &result); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4199 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4200 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4201 | goto done; |
| 4202 | } |
| 4203 | |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4204 | if (!chan->hs_hcon) { |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4205 | l2cap_send_efs_conf_rsp(chan, buf, cmd->ident, |
| 4206 | 0); |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 4207 | } else { |
| 4208 | if (l2cap_check_efs(chan)) { |
| 4209 | amp_create_logical_link(chan); |
| 4210 | chan->ident = cmd->ident; |
| 4211 | } |
| 4212 | } |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4213 | } |
| 4214 | goto done; |
| 4215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | case L2CAP_CONF_UNACCEPT: |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4217 | if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4218 | char req[64]; |
| 4219 | |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4220 | if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4221 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4222 | goto done; |
| 4223 | } |
| 4224 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4225 | /* throw out any old stored conf requests */ |
| 4226 | result = L2CAP_CONF_SUCCESS; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 4227 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4228 | req, &result); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4229 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4230 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4231 | goto done; |
| 4232 | } |
| 4233 | |
| 4234 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4235 | L2CAP_CONF_REQ, len, req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4236 | chan->num_conf_req++; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4237 | if (result != L2CAP_CONF_SUCCESS) |
| 4238 | goto done; |
| 4239 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | } |
| 4241 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 4242 | default: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4243 | l2cap_chan_set_err(chan, ECONNRESET); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 4244 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 4245 | __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4246 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4247 | goto done; |
| 4248 | } |
| 4249 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4250 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | goto done; |
| 4252 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4253 | set_bit(CONF_INPUT_DONE, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4255 | if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4256 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4257 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4258 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4259 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4260 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4261 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4262 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4263 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4264 | else |
| 4265 | l2cap_chan_ready(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4266 | } |
| 4267 | |
| 4268 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4269 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4270 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | } |
| 4272 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4273 | static inline int l2cap_disconnect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4274 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4275 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | { |
| 4277 | struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; |
| 4278 | struct l2cap_disconn_rsp rsp; |
| 4279 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4280 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4282 | if (cmd_len != sizeof(*req)) |
| 4283 | return -EPROTO; |
| 4284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4285 | scid = __le16_to_cpu(req->scid); |
| 4286 | dcid = __le16_to_cpu(req->dcid); |
| 4287 | |
| 4288 | BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); |
| 4289 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4290 | mutex_lock(&conn->chan_lock); |
| 4291 | |
| 4292 | chan = __l2cap_get_chan_by_scid(conn, dcid); |
| 4293 | if (!chan) { |
| 4294 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4295 | cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid); |
| 4296 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4297 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4299 | l2cap_chan_lock(chan); |
| 4300 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4301 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4302 | rsp.scid = cpu_to_le16(chan->dcid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4303 | l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); |
| 4304 | |
Gustavo Padovan | 5ec1bbe | 2013-10-15 19:24:48 -0300 | [diff] [blame] | 4305 | chan->ops->set_shutdown(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4307 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4308 | l2cap_chan_del(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4309 | |
| 4310 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4312 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4313 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4314 | |
| 4315 | mutex_unlock(&conn->chan_lock); |
| 4316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | return 0; |
| 4318 | } |
| 4319 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4320 | static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4321 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4322 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | { |
| 4324 | struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; |
| 4325 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4326 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4328 | if (cmd_len != sizeof(*rsp)) |
| 4329 | return -EPROTO; |
| 4330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | scid = __le16_to_cpu(rsp->scid); |
| 4332 | dcid = __le16_to_cpu(rsp->dcid); |
| 4333 | |
| 4334 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); |
| 4335 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4336 | mutex_lock(&conn->chan_lock); |
| 4337 | |
| 4338 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 4339 | if (!chan) { |
| 4340 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4341 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4344 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4345 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4346 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4347 | l2cap_chan_del(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4348 | |
| 4349 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4350 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4351 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4352 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4353 | |
| 4354 | mutex_unlock(&conn->chan_lock); |
| 4355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4356 | return 0; |
| 4357 | } |
| 4358 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4359 | static inline int l2cap_information_req(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_req *req = (struct l2cap_info_req *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4364 | u16 type; |
| 4365 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4366 | if (cmd_len != sizeof(*req)) |
| 4367 | return -EPROTO; |
| 4368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | type = __le16_to_cpu(req->type); |
| 4370 | |
| 4371 | BT_DBG("type 0x%4.4x", type); |
| 4372 | |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4373 | if (type == L2CAP_IT_FEAT_MASK) { |
| 4374 | u8 buf[8]; |
Marcel Holtmann | 44dd46d | 2009-05-02 19:09:01 -0700 | [diff] [blame] | 4375 | u32 feat_mask = l2cap_feat_mask; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4376 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4377 | rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 4378 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 4379 | if (!disable_ertm) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4380 | feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4381 | | L2CAP_FEAT_FCS; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4382 | if (conn->hs_enabled) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 4383 | feat_mask |= L2CAP_FEAT_EXT_FLOW |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4384 | | L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 4385 | |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 4386 | put_unaligned_le32(feat_mask, rsp->data); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4387 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4388 | buf); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4389 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
| 4390 | u8 buf[12]; |
| 4391 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 4392 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4393 | if (conn->hs_enabled) |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 4394 | l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; |
| 4395 | else |
| 4396 | l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; |
| 4397 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4398 | rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 4399 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Andrei Emeltchenko | c6337ea | 2011-10-20 17:02:44 +0300 | [diff] [blame] | 4400 | memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan)); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4401 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4402 | buf); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4403 | } else { |
| 4404 | struct l2cap_info_rsp rsp; |
| 4405 | rsp.type = cpu_to_le16(type); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4406 | rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4407 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp), |
| 4408 | &rsp); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4409 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | |
| 4411 | return 0; |
| 4412 | } |
| 4413 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4414 | static inline int l2cap_information_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4415 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4416 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | { |
| 4418 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; |
| 4419 | u16 type, result; |
| 4420 | |
Jaganath Kanakkassery | 3f6fa3d | 2013-06-21 19:55:11 +0530 | [diff] [blame] | 4421 | if (cmd_len < sizeof(*rsp)) |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4422 | return -EPROTO; |
| 4423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4424 | type = __le16_to_cpu(rsp->type); |
| 4425 | result = __le16_to_cpu(rsp->result); |
| 4426 | |
| 4427 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); |
| 4428 | |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4429 | /* L2CAP Info req/rsp are unbound to channels, add extra checks */ |
| 4430 | if (cmd->ident != conn->info_ident || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4431 | conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4432 | return 0; |
| 4433 | |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 4434 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4435 | |
Ville Tervo | adb08ed | 2010-08-04 09:43:33 +0300 | [diff] [blame] | 4436 | if (result != L2CAP_IR_SUCCESS) { |
| 4437 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4438 | conn->info_ident = 0; |
| 4439 | |
| 4440 | l2cap_conn_start(conn); |
| 4441 | |
| 4442 | return 0; |
| 4443 | } |
| 4444 | |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4445 | switch (type) { |
| 4446 | case L2CAP_IT_FEAT_MASK: |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 4447 | conn->feat_mask = get_unaligned_le32(rsp->data); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4448 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 4449 | if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4450 | struct l2cap_info_req req; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4451 | req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4452 | |
| 4453 | conn->info_ident = l2cap_get_ident(conn); |
| 4454 | |
| 4455 | l2cap_send_cmd(conn, conn->info_ident, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4456 | L2CAP_INFO_REQ, sizeof(req), &req); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4457 | } else { |
| 4458 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4459 | conn->info_ident = 0; |
| 4460 | |
| 4461 | l2cap_conn_start(conn); |
| 4462 | } |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4463 | break; |
| 4464 | |
| 4465 | case L2CAP_IT_FIXED_CHAN: |
| 4466 | conn->fixed_chan_mask = rsp->data[0]; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4467 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4468 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4469 | |
| 4470 | l2cap_conn_start(conn); |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4471 | break; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4472 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4474 | return 0; |
| 4475 | } |
| 4476 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4477 | static int l2cap_create_channel_req(struct l2cap_conn *conn, |
| 4478 | struct l2cap_cmd_hdr *cmd, |
| 4479 | u16 cmd_len, void *data) |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4480 | { |
| 4481 | struct l2cap_create_chan_req *req = data; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4482 | struct l2cap_create_chan_rsp rsp; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4483 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4484 | struct hci_dev *hdev; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4485 | u16 psm, scid; |
| 4486 | |
| 4487 | if (cmd_len != sizeof(*req)) |
| 4488 | return -EPROTO; |
| 4489 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4490 | if (!conn->hs_enabled) |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4491 | return -EINVAL; |
| 4492 | |
| 4493 | psm = le16_to_cpu(req->psm); |
| 4494 | scid = le16_to_cpu(req->scid); |
| 4495 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4496 | 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] | 4497 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4498 | /* For controller id 0 make BR/EDR connection */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4499 | if (req->amp_id == AMP_ID_BREDR) { |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4500 | l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4501 | req->amp_id); |
| 4502 | return 0; |
| 4503 | } |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4504 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4505 | /* Validate AMP controller id */ |
| 4506 | hdev = hci_dev_get(req->amp_id); |
| 4507 | if (!hdev) |
| 4508 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4509 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4510 | if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4511 | hci_dev_put(hdev); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4512 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4513 | } |
| 4514 | |
| 4515 | chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4516 | req->amp_id); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4517 | if (chan) { |
| 4518 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
| 4519 | struct hci_conn *hs_hcon; |
| 4520 | |
Marcel Holtmann | 98e0f7e | 2013-10-13 02:23:40 -0700 | [diff] [blame] | 4521 | hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, |
| 4522 | &conn->hcon->dst); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4523 | if (!hs_hcon) { |
| 4524 | hci_dev_put(hdev); |
Johan Hedberg | 662d652 | 2013-10-16 11:20:47 +0300 | [diff] [blame] | 4525 | cmd_reject_invalid_cid(conn, cmd->ident, chan->scid, |
| 4526 | chan->dcid); |
| 4527 | return 0; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon); |
| 4531 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4532 | mgr->bredr_chan = chan; |
| 4533 | chan->hs_hcon = hs_hcon; |
Andrei Emeltchenko | fd45bf4 | 2012-11-20 17:16:22 +0200 | [diff] [blame] | 4534 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4535 | conn->mtu = hdev->block_mtu; |
| 4536 | } |
| 4537 | |
| 4538 | hci_dev_put(hdev); |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4539 | |
| 4540 | return 0; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4541 | |
| 4542 | error: |
| 4543 | rsp.dcid = 0; |
| 4544 | rsp.scid = cpu_to_le16(scid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4545 | rsp.result = cpu_to_le16(L2CAP_CR_BAD_AMP); |
| 4546 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4547 | |
| 4548 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, |
| 4549 | sizeof(rsp), &rsp); |
| 4550 | |
Johan Hedberg | dc28080 | 2013-09-16 13:05:13 +0300 | [diff] [blame] | 4551 | return 0; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4552 | } |
| 4553 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4554 | static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id) |
| 4555 | { |
| 4556 | struct l2cap_move_chan_req req; |
| 4557 | u8 ident; |
| 4558 | |
| 4559 | BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id); |
| 4560 | |
| 4561 | ident = l2cap_get_ident(chan->conn); |
| 4562 | chan->ident = ident; |
| 4563 | |
| 4564 | req.icid = cpu_to_le16(chan->scid); |
| 4565 | req.dest_amp_id = dest_amp_id; |
| 4566 | |
| 4567 | l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req), |
| 4568 | &req); |
| 4569 | |
| 4570 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4571 | } |
| 4572 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4573 | 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] | 4574 | { |
| 4575 | struct l2cap_move_chan_rsp rsp; |
| 4576 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4577 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4578 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4579 | rsp.icid = cpu_to_le16(chan->dcid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4580 | rsp.result = cpu_to_le16(result); |
| 4581 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4582 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP, |
| 4583 | sizeof(rsp), &rsp); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4584 | } |
| 4585 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4586 | 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] | 4587 | { |
| 4588 | struct l2cap_move_chan_cfm cfm; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4589 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4590 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4591 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4592 | chan->ident = l2cap_get_ident(chan->conn); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4593 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4594 | cfm.icid = cpu_to_le16(chan->scid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4595 | cfm.result = cpu_to_le16(result); |
| 4596 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4597 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM, |
| 4598 | sizeof(cfm), &cfm); |
| 4599 | |
| 4600 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4601 | } |
| 4602 | |
| 4603 | static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid) |
| 4604 | { |
| 4605 | struct l2cap_move_chan_cfm cfm; |
| 4606 | |
| 4607 | BT_DBG("conn %p, icid 0x%4.4x", conn, icid); |
| 4608 | |
| 4609 | cfm.icid = cpu_to_le16(icid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4610 | cfm.result = cpu_to_le16(L2CAP_MC_UNCONFIRMED); |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4611 | |
| 4612 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM, |
| 4613 | sizeof(cfm), &cfm); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4614 | } |
| 4615 | |
| 4616 | 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] | 4617 | u16 icid) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4618 | { |
| 4619 | struct l2cap_move_chan_cfm_rsp rsp; |
| 4620 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4621 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4622 | |
| 4623 | rsp.icid = cpu_to_le16(icid); |
| 4624 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); |
| 4625 | } |
| 4626 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 4627 | static void __release_logical_link(struct l2cap_chan *chan) |
| 4628 | { |
| 4629 | chan->hs_hchan = NULL; |
| 4630 | chan->hs_hcon = NULL; |
| 4631 | |
| 4632 | /* Placeholder - release the logical link */ |
| 4633 | } |
| 4634 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4635 | static void l2cap_logical_fail(struct l2cap_chan *chan) |
| 4636 | { |
| 4637 | /* Logical link setup failed */ |
| 4638 | if (chan->state != BT_CONNECTED) { |
| 4639 | /* Create channel failure, disconnect */ |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4640 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4641 | return; |
| 4642 | } |
| 4643 | |
| 4644 | switch (chan->move_role) { |
| 4645 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4646 | l2cap_move_done(chan); |
| 4647 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP); |
| 4648 | break; |
| 4649 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4650 | if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP || |
| 4651 | chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) { |
| 4652 | /* Remote has only sent pending or |
| 4653 | * success responses, clean up |
| 4654 | */ |
| 4655 | l2cap_move_done(chan); |
| 4656 | } |
| 4657 | |
| 4658 | /* Other amp move states imply that the move |
| 4659 | * has already aborted |
| 4660 | */ |
| 4661 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 4662 | break; |
| 4663 | } |
| 4664 | } |
| 4665 | |
| 4666 | static void l2cap_logical_finish_create(struct l2cap_chan *chan, |
| 4667 | struct hci_chan *hchan) |
| 4668 | { |
| 4669 | struct l2cap_conf_rsp rsp; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4670 | |
Andrei Emeltchenko | 336178a | 2012-10-31 15:46:27 +0200 | [diff] [blame] | 4671 | chan->hs_hchan = hchan; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4672 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4673 | |
Andrei Emeltchenko | 35ba956 | 2012-10-25 15:20:43 +0300 | [diff] [blame] | 4674 | l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4675 | |
| 4676 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Andrei Emeltchenko | fe79c6f | 2012-10-25 15:20:42 +0300 | [diff] [blame] | 4677 | int err; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4678 | |
| 4679 | set_default_fcs(chan); |
| 4680 | |
| 4681 | err = l2cap_ertm_init(chan); |
| 4682 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4683 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4684 | else |
| 4685 | l2cap_chan_ready(chan); |
| 4686 | } |
| 4687 | } |
| 4688 | |
| 4689 | static void l2cap_logical_finish_move(struct l2cap_chan *chan, |
| 4690 | struct hci_chan *hchan) |
| 4691 | { |
| 4692 | chan->hs_hcon = hchan->conn; |
| 4693 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4694 | |
| 4695 | BT_DBG("move_state %d", chan->move_state); |
| 4696 | |
| 4697 | switch (chan->move_state) { |
| 4698 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 4699 | /* Move confirm will be sent after a success |
| 4700 | * response is received |
| 4701 | */ |
| 4702 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4703 | break; |
| 4704 | case L2CAP_MOVE_WAIT_LOGICAL_CFM: |
| 4705 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4706 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4707 | } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 4708 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 4709 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4710 | } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4711 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4712 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4713 | } |
| 4714 | break; |
| 4715 | default: |
| 4716 | /* Move was not in expected state, free the channel */ |
| 4717 | __release_logical_link(chan); |
| 4718 | |
| 4719 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4720 | } |
| 4721 | } |
| 4722 | |
| 4723 | /* Call with chan locked */ |
Andrei Emeltchenko | 27695fb | 2012-10-25 15:20:45 +0300 | [diff] [blame] | 4724 | void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan, |
| 4725 | u8 status) |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4726 | { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4727 | BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status); |
| 4728 | |
| 4729 | if (status) { |
| 4730 | l2cap_logical_fail(chan); |
| 4731 | __release_logical_link(chan); |
| 4732 | return; |
| 4733 | } |
| 4734 | |
| 4735 | if (chan->state != BT_CONNECTED) { |
| 4736 | /* Ignore logical link if channel is on BR/EDR */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4737 | if (chan->local_amp_id != AMP_ID_BREDR) |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4738 | l2cap_logical_finish_create(chan, hchan); |
| 4739 | } else { |
| 4740 | l2cap_logical_finish_move(chan, hchan); |
| 4741 | } |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4742 | } |
| 4743 | |
Mat Martineau | 3f7a56c | 2012-10-23 15:24:23 -0700 | [diff] [blame] | 4744 | void l2cap_move_start(struct l2cap_chan *chan) |
| 4745 | { |
| 4746 | BT_DBG("chan %p", chan); |
| 4747 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4748 | if (chan->local_amp_id == AMP_ID_BREDR) { |
Mat Martineau | 3f7a56c | 2012-10-23 15:24:23 -0700 | [diff] [blame] | 4749 | if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED) |
| 4750 | return; |
| 4751 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4752 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4753 | /* Placeholder - start physical link setup */ |
| 4754 | } else { |
| 4755 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4756 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4757 | chan->move_id = 0; |
| 4758 | l2cap_move_setup(chan); |
| 4759 | l2cap_send_move_chan_req(chan, 0); |
| 4760 | } |
| 4761 | } |
| 4762 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4763 | static void l2cap_do_create(struct l2cap_chan *chan, int result, |
| 4764 | u8 local_amp_id, u8 remote_amp_id) |
| 4765 | { |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4766 | BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state), |
| 4767 | local_amp_id, remote_amp_id); |
| 4768 | |
Andrei Emeltchenko | 12d6cc6 | 2012-11-14 17:39:31 +0200 | [diff] [blame] | 4769 | chan->fcs = L2CAP_FCS_NONE; |
| 4770 | |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4771 | /* Outgoing channel on AMP */ |
| 4772 | if (chan->state == BT_CONNECT) { |
| 4773 | if (result == L2CAP_CR_SUCCESS) { |
| 4774 | chan->local_amp_id = local_amp_id; |
| 4775 | l2cap_send_create_chan_req(chan, remote_amp_id); |
| 4776 | } else { |
| 4777 | /* Revert to BR/EDR connect */ |
| 4778 | l2cap_send_conn_req(chan); |
| 4779 | } |
| 4780 | |
| 4781 | return; |
| 4782 | } |
| 4783 | |
| 4784 | /* Incoming channel on AMP */ |
| 4785 | if (__l2cap_no_conn_pending(chan)) { |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4786 | struct l2cap_conn_rsp rsp; |
| 4787 | char buf[128]; |
| 4788 | rsp.scid = cpu_to_le16(chan->dcid); |
| 4789 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4790 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4791 | if (result == L2CAP_CR_SUCCESS) { |
| 4792 | /* Send successful response */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4793 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 4794 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4795 | } else { |
| 4796 | /* Send negative response */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4797 | rsp.result = cpu_to_le16(L2CAP_CR_NO_MEM); |
| 4798 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4799 | } |
| 4800 | |
| 4801 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP, |
| 4802 | sizeof(rsp), &rsp); |
| 4803 | |
| 4804 | if (result == L2CAP_CR_SUCCESS) { |
Gustavo Padovan | f93fa27 | 2013-10-21 14:21:40 -0200 | [diff] [blame] | 4805 | l2cap_state_change(chan, BT_CONFIG); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4806 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 4807 | l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn), |
| 4808 | L2CAP_CONF_REQ, |
| 4809 | l2cap_build_conf_req(chan, buf), buf); |
| 4810 | chan->num_conf_req++; |
| 4811 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4812 | } |
| 4813 | } |
| 4814 | |
| 4815 | static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id, |
| 4816 | u8 remote_amp_id) |
| 4817 | { |
| 4818 | l2cap_move_setup(chan); |
| 4819 | chan->move_id = local_amp_id; |
| 4820 | chan->move_state = L2CAP_MOVE_WAIT_RSP; |
| 4821 | |
| 4822 | l2cap_send_move_chan_req(chan, remote_amp_id); |
| 4823 | } |
| 4824 | |
| 4825 | static void l2cap_do_move_respond(struct l2cap_chan *chan, int result) |
| 4826 | { |
| 4827 | struct hci_chan *hchan = NULL; |
| 4828 | |
| 4829 | /* Placeholder - get hci_chan for logical link */ |
| 4830 | |
| 4831 | if (hchan) { |
| 4832 | if (hchan->state == BT_CONNECTED) { |
| 4833 | /* Logical link is ready to go */ |
| 4834 | chan->hs_hcon = hchan->conn; |
| 4835 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4836 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4837 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4838 | |
| 4839 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 4840 | } else { |
| 4841 | /* Wait for logical link to be ready */ |
| 4842 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4843 | } |
| 4844 | } else { |
| 4845 | /* Logical link not available */ |
| 4846 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED); |
| 4847 | } |
| 4848 | } |
| 4849 | |
| 4850 | static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result) |
| 4851 | { |
| 4852 | if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4853 | u8 rsp_result; |
| 4854 | if (result == -EINVAL) |
| 4855 | rsp_result = L2CAP_MR_BAD_ID; |
| 4856 | else |
| 4857 | rsp_result = L2CAP_MR_NOT_ALLOWED; |
| 4858 | |
| 4859 | l2cap_send_move_chan_rsp(chan, rsp_result); |
| 4860 | } |
| 4861 | |
| 4862 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 4863 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4864 | |
| 4865 | /* Restart data transmission */ |
| 4866 | l2cap_ertm_send(chan); |
| 4867 | } |
| 4868 | |
Andrei Emeltchenko | a514b17 | 2012-11-14 17:39:30 +0200 | [diff] [blame] | 4869 | /* Invoke with locked chan */ |
| 4870 | void __l2cap_physical_cfm(struct l2cap_chan *chan, int result) |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4871 | { |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4872 | u8 local_amp_id = chan->local_amp_id; |
Andrei Emeltchenko | fffadc0 | 2012-11-01 15:37:03 +0200 | [diff] [blame] | 4873 | u8 remote_amp_id = chan->remote_amp_id; |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4874 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4875 | BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d", |
| 4876 | chan, result, local_amp_id, remote_amp_id); |
| 4877 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4878 | if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) { |
| 4879 | l2cap_chan_unlock(chan); |
| 4880 | return; |
| 4881 | } |
| 4882 | |
| 4883 | if (chan->state != BT_CONNECTED) { |
| 4884 | l2cap_do_create(chan, result, local_amp_id, remote_amp_id); |
| 4885 | } else if (result != L2CAP_MR_SUCCESS) { |
| 4886 | l2cap_do_move_cancel(chan, result); |
| 4887 | } else { |
| 4888 | switch (chan->move_role) { |
| 4889 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4890 | l2cap_do_move_initiate(chan, local_amp_id, |
| 4891 | remote_amp_id); |
| 4892 | break; |
| 4893 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4894 | l2cap_do_move_respond(chan, result); |
| 4895 | break; |
| 4896 | default: |
| 4897 | l2cap_do_move_cancel(chan, result); |
| 4898 | break; |
| 4899 | } |
| 4900 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4901 | } |
| 4902 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4903 | static inline int l2cap_move_channel_req(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4904 | struct l2cap_cmd_hdr *cmd, |
| 4905 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4906 | { |
| 4907 | struct l2cap_move_chan_req *req = data; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4908 | struct l2cap_move_chan_rsp rsp; |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4909 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4910 | u16 icid = 0; |
| 4911 | u16 result = L2CAP_MR_NOT_ALLOWED; |
| 4912 | |
| 4913 | if (cmd_len != sizeof(*req)) |
| 4914 | return -EPROTO; |
| 4915 | |
| 4916 | icid = le16_to_cpu(req->icid); |
| 4917 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4918 | 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] | 4919 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4920 | if (!conn->hs_enabled) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4921 | return -EINVAL; |
| 4922 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4923 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 4924 | if (!chan) { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4925 | rsp.icid = cpu_to_le16(icid); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 4926 | rsp.result = cpu_to_le16(L2CAP_MR_NOT_ALLOWED); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4927 | l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP, |
| 4928 | sizeof(rsp), &rsp); |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4929 | return 0; |
| 4930 | } |
| 4931 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4932 | chan->ident = cmd->ident; |
| 4933 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4934 | if (chan->scid < L2CAP_CID_DYN_START || |
| 4935 | chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY || |
| 4936 | (chan->mode != L2CAP_MODE_ERTM && |
| 4937 | chan->mode != L2CAP_MODE_STREAMING)) { |
| 4938 | result = L2CAP_MR_NOT_ALLOWED; |
| 4939 | goto send_move_response; |
| 4940 | } |
| 4941 | |
| 4942 | if (chan->local_amp_id == req->dest_amp_id) { |
| 4943 | result = L2CAP_MR_SAME_ID; |
| 4944 | goto send_move_response; |
| 4945 | } |
| 4946 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4947 | if (req->dest_amp_id != AMP_ID_BREDR) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4948 | struct hci_dev *hdev; |
| 4949 | hdev = hci_dev_get(req->dest_amp_id); |
| 4950 | if (!hdev || hdev->dev_type != HCI_AMP || |
| 4951 | !test_bit(HCI_UP, &hdev->flags)) { |
| 4952 | if (hdev) |
| 4953 | hci_dev_put(hdev); |
| 4954 | |
| 4955 | result = L2CAP_MR_BAD_ID; |
| 4956 | goto send_move_response; |
| 4957 | } |
| 4958 | hci_dev_put(hdev); |
| 4959 | } |
| 4960 | |
| 4961 | /* Detect a move collision. Only send a collision response |
| 4962 | * if this side has "lost", otherwise proceed with the move. |
| 4963 | * The winner has the larger bd_addr. |
| 4964 | */ |
| 4965 | if ((__chan_is_moving(chan) || |
| 4966 | chan->move_role != L2CAP_MOVE_ROLE_NONE) && |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 4967 | bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4968 | result = L2CAP_MR_COLLISION; |
| 4969 | goto send_move_response; |
| 4970 | } |
| 4971 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4972 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 4973 | l2cap_move_setup(chan); |
| 4974 | chan->move_id = req->dest_amp_id; |
| 4975 | icid = chan->dcid; |
| 4976 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4977 | if (req->dest_amp_id == AMP_ID_BREDR) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4978 | /* Moving to BR/EDR */ |
| 4979 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4980 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4981 | result = L2CAP_MR_PEND; |
| 4982 | } else { |
| 4983 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4984 | result = L2CAP_MR_SUCCESS; |
| 4985 | } |
| 4986 | } else { |
| 4987 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4988 | /* Placeholder - uncomment when amp functions are available */ |
| 4989 | /*amp_accept_physical(chan, req->dest_amp_id);*/ |
| 4990 | result = L2CAP_MR_PEND; |
| 4991 | } |
| 4992 | |
| 4993 | send_move_response: |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4994 | l2cap_send_move_chan_rsp(chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4995 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4996 | l2cap_chan_unlock(chan); |
| 4997 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4998 | return 0; |
| 4999 | } |
| 5000 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 5001 | static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result) |
| 5002 | { |
| 5003 | struct l2cap_chan *chan; |
| 5004 | struct hci_chan *hchan = NULL; |
| 5005 | |
| 5006 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 5007 | if (!chan) { |
| 5008 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 5009 | return; |
| 5010 | } |
| 5011 | |
| 5012 | __clear_chan_timer(chan); |
| 5013 | if (result == L2CAP_MR_PEND) |
| 5014 | __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT); |
| 5015 | |
| 5016 | switch (chan->move_state) { |
| 5017 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 5018 | /* Move confirm will be sent when logical link |
| 5019 | * is complete. |
| 5020 | */ |
| 5021 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 5022 | break; |
| 5023 | case L2CAP_MOVE_WAIT_RSP_SUCCESS: |
| 5024 | if (result == L2CAP_MR_PEND) { |
| 5025 | break; |
| 5026 | } else if (test_bit(CONN_LOCAL_BUSY, |
| 5027 | &chan->conn_state)) { |
| 5028 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 5029 | } else { |
| 5030 | /* Logical link is up or moving to BR/EDR, |
| 5031 | * proceed with move |
| 5032 | */ |
| 5033 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 5034 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 5035 | } |
| 5036 | break; |
| 5037 | case L2CAP_MOVE_WAIT_RSP: |
| 5038 | /* Moving to AMP */ |
| 5039 | if (result == L2CAP_MR_SUCCESS) { |
| 5040 | /* Remote is ready, send confirm immediately |
| 5041 | * after logical link is ready |
| 5042 | */ |
| 5043 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 5044 | } else { |
| 5045 | /* Both logical link and move success |
| 5046 | * are required to confirm |
| 5047 | */ |
| 5048 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP; |
| 5049 | } |
| 5050 | |
| 5051 | /* Placeholder - get hci_chan for logical link */ |
| 5052 | if (!hchan) { |
| 5053 | /* Logical link not available */ |
| 5054 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5055 | break; |
| 5056 | } |
| 5057 | |
| 5058 | /* If the logical link is not yet connected, do not |
| 5059 | * send confirmation. |
| 5060 | */ |
| 5061 | if (hchan->state != BT_CONNECTED) |
| 5062 | break; |
| 5063 | |
| 5064 | /* Logical link is already ready to go */ |
| 5065 | |
| 5066 | chan->hs_hcon = hchan->conn; |
| 5067 | chan->hs_hcon->l2cap_data = chan->conn; |
| 5068 | |
| 5069 | if (result == L2CAP_MR_SUCCESS) { |
| 5070 | /* Can confirm now */ |
| 5071 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 5072 | } else { |
| 5073 | /* Now only need move success |
| 5074 | * to confirm |
| 5075 | */ |
| 5076 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 5077 | } |
| 5078 | |
| 5079 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 5080 | break; |
| 5081 | default: |
| 5082 | /* Any other amp move state means the move failed. */ |
| 5083 | chan->move_id = chan->local_amp_id; |
| 5084 | l2cap_move_done(chan); |
| 5085 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5086 | } |
| 5087 | |
| 5088 | l2cap_chan_unlock(chan); |
| 5089 | } |
| 5090 | |
| 5091 | static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid, |
| 5092 | u16 result) |
| 5093 | { |
| 5094 | struct l2cap_chan *chan; |
| 5095 | |
| 5096 | chan = l2cap_get_chan_by_ident(conn, ident); |
| 5097 | if (!chan) { |
| 5098 | /* Could not locate channel, icid is best guess */ |
| 5099 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 5100 | return; |
| 5101 | } |
| 5102 | |
| 5103 | __clear_chan_timer(chan); |
| 5104 | |
| 5105 | if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 5106 | if (result == L2CAP_MR_COLLISION) { |
| 5107 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 5108 | } else { |
| 5109 | /* Cleanup - cancel move */ |
| 5110 | chan->move_id = chan->local_amp_id; |
| 5111 | l2cap_move_done(chan); |
| 5112 | } |
| 5113 | } |
| 5114 | |
| 5115 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5116 | |
| 5117 | l2cap_chan_unlock(chan); |
| 5118 | } |
| 5119 | |
| 5120 | static int l2cap_move_channel_rsp(struct l2cap_conn *conn, |
| 5121 | struct l2cap_cmd_hdr *cmd, |
| 5122 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5123 | { |
| 5124 | struct l2cap_move_chan_rsp *rsp = data; |
| 5125 | u16 icid, result; |
| 5126 | |
| 5127 | if (cmd_len != sizeof(*rsp)) |
| 5128 | return -EPROTO; |
| 5129 | |
| 5130 | icid = le16_to_cpu(rsp->icid); |
| 5131 | result = le16_to_cpu(rsp->result); |
| 5132 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5133 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5134 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 5135 | if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND) |
| 5136 | l2cap_move_continue(conn, icid, result); |
| 5137 | else |
| 5138 | l2cap_move_fail(conn, cmd->ident, icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5139 | |
| 5140 | return 0; |
| 5141 | } |
| 5142 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5143 | static int l2cap_move_channel_confirm(struct l2cap_conn *conn, |
| 5144 | struct l2cap_cmd_hdr *cmd, |
| 5145 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5146 | { |
| 5147 | struct l2cap_move_chan_cfm *cfm = data; |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5148 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5149 | u16 icid, result; |
| 5150 | |
| 5151 | if (cmd_len != sizeof(*cfm)) |
| 5152 | return -EPROTO; |
| 5153 | |
| 5154 | icid = le16_to_cpu(cfm->icid); |
| 5155 | result = le16_to_cpu(cfm->result); |
| 5156 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5157 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5158 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5159 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 5160 | if (!chan) { |
| 5161 | /* Spec requires a response even if the icid was not found */ |
| 5162 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5163 | return 0; |
| 5164 | } |
| 5165 | |
| 5166 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) { |
| 5167 | if (result == L2CAP_MC_CONFIRMED) { |
| 5168 | chan->local_amp_id = chan->move_id; |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 5169 | if (chan->local_amp_id == AMP_ID_BREDR) |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5170 | __release_logical_link(chan); |
| 5171 | } else { |
| 5172 | chan->move_id = chan->local_amp_id; |
| 5173 | } |
| 5174 | |
| 5175 | l2cap_move_done(chan); |
| 5176 | } |
| 5177 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5178 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5179 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5180 | l2cap_chan_unlock(chan); |
| 5181 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5182 | return 0; |
| 5183 | } |
| 5184 | |
| 5185 | static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5186 | struct l2cap_cmd_hdr *cmd, |
| 5187 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5188 | { |
| 5189 | struct l2cap_move_chan_cfm_rsp *rsp = data; |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5190 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5191 | u16 icid; |
| 5192 | |
| 5193 | if (cmd_len != sizeof(*rsp)) |
| 5194 | return -EPROTO; |
| 5195 | |
| 5196 | icid = le16_to_cpu(rsp->icid); |
| 5197 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5198 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5199 | |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5200 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 5201 | if (!chan) |
| 5202 | return 0; |
| 5203 | |
| 5204 | __clear_chan_timer(chan); |
| 5205 | |
| 5206 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) { |
| 5207 | chan->local_amp_id = chan->move_id; |
| 5208 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 5209 | if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan) |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5210 | __release_logical_link(chan); |
| 5211 | |
| 5212 | l2cap_move_done(chan); |
| 5213 | } |
| 5214 | |
| 5215 | l2cap_chan_unlock(chan); |
| 5216 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5217 | return 0; |
| 5218 | } |
| 5219 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5220 | static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5221 | struct l2cap_cmd_hdr *cmd, |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5222 | u16 cmd_len, u8 *data) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5223 | { |
| 5224 | struct hci_conn *hcon = conn->hcon; |
| 5225 | struct l2cap_conn_param_update_req *req; |
| 5226 | struct l2cap_conn_param_update_rsp rsp; |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5227 | u16 min, max, latency, to_multiplier; |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5228 | int err; |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5229 | |
Johan Hedberg | 4dae279 | 2014-06-24 17:03:50 +0300 | [diff] [blame] | 5230 | if (!test_bit(HCI_CONN_MASTER, &hcon->flags)) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5231 | return -EINVAL; |
| 5232 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5233 | if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) |
| 5234 | return -EPROTO; |
| 5235 | |
| 5236 | req = (struct l2cap_conn_param_update_req *) data; |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 5237 | min = __le16_to_cpu(req->min); |
| 5238 | max = __le16_to_cpu(req->max); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5239 | latency = __le16_to_cpu(req->latency); |
| 5240 | to_multiplier = __le16_to_cpu(req->to_multiplier); |
| 5241 | |
| 5242 | 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] | 5243 | min, max, latency, to_multiplier); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5244 | |
| 5245 | memset(&rsp, 0, sizeof(rsp)); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5246 | |
Andre Guedes | d4905f2 | 2014-06-25 21:52:52 -0300 | [diff] [blame] | 5247 | err = hci_check_conn_params(min, max, latency, to_multiplier); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5248 | if (err) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5249 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5250 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5251 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5252 | |
| 5253 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5254 | sizeof(rsp), &rsp); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5255 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5256 | if (!err) { |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5257 | u8 store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5258 | |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5259 | store_hint = hci_le_conn_update(hcon, min, max, latency, |
| 5260 | to_multiplier); |
| 5261 | mgmt_new_conn_param(hcon->hdev, &hcon->dst, hcon->dst_type, |
| 5262 | store_hint, min, max, latency, |
| 5263 | to_multiplier); |
| 5264 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5265 | } |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5266 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5267 | return 0; |
| 5268 | } |
| 5269 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5270 | static int l2cap_le_connect_rsp(struct l2cap_conn *conn, |
| 5271 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5272 | u8 *data) |
| 5273 | { |
| 5274 | struct l2cap_le_conn_rsp *rsp = (struct l2cap_le_conn_rsp *) data; |
| 5275 | u16 dcid, mtu, mps, credits, result; |
| 5276 | struct l2cap_chan *chan; |
| 5277 | int err; |
| 5278 | |
| 5279 | if (cmd_len < sizeof(*rsp)) |
| 5280 | return -EPROTO; |
| 5281 | |
| 5282 | dcid = __le16_to_cpu(rsp->dcid); |
| 5283 | mtu = __le16_to_cpu(rsp->mtu); |
| 5284 | mps = __le16_to_cpu(rsp->mps); |
| 5285 | credits = __le16_to_cpu(rsp->credits); |
| 5286 | result = __le16_to_cpu(rsp->result); |
| 5287 | |
| 5288 | if (result == L2CAP_CR_SUCCESS && (mtu < 23 || mps < 23)) |
| 5289 | return -EPROTO; |
| 5290 | |
| 5291 | BT_DBG("dcid 0x%4.4x mtu %u mps %u credits %u result 0x%2.2x", |
| 5292 | dcid, mtu, mps, credits, result); |
| 5293 | |
| 5294 | mutex_lock(&conn->chan_lock); |
| 5295 | |
| 5296 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 5297 | if (!chan) { |
| 5298 | err = -EBADSLT; |
| 5299 | goto unlock; |
| 5300 | } |
| 5301 | |
| 5302 | err = 0; |
| 5303 | |
| 5304 | l2cap_chan_lock(chan); |
| 5305 | |
| 5306 | switch (result) { |
| 5307 | case L2CAP_CR_SUCCESS: |
| 5308 | chan->ident = 0; |
| 5309 | chan->dcid = dcid; |
| 5310 | chan->omtu = mtu; |
| 5311 | chan->remote_mps = mps; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5312 | chan->tx_credits = credits; |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5313 | l2cap_chan_ready(chan); |
| 5314 | break; |
| 5315 | |
| 5316 | default: |
| 5317 | l2cap_chan_del(chan, ECONNREFUSED); |
| 5318 | break; |
| 5319 | } |
| 5320 | |
| 5321 | l2cap_chan_unlock(chan); |
| 5322 | |
| 5323 | unlock: |
| 5324 | mutex_unlock(&conn->chan_lock); |
| 5325 | |
| 5326 | return err; |
| 5327 | } |
| 5328 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5329 | static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5330 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5331 | u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5332 | { |
| 5333 | int err = 0; |
| 5334 | |
| 5335 | switch (cmd->code) { |
| 5336 | case L2CAP_COMMAND_REJ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5337 | l2cap_command_rej(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5338 | break; |
| 5339 | |
| 5340 | case L2CAP_CONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5341 | err = l2cap_connect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5342 | break; |
| 5343 | |
| 5344 | case L2CAP_CONN_RSP: |
Mat Martineau | f5a2598 | 2012-10-11 17:48:21 +0300 | [diff] [blame] | 5345 | case L2CAP_CREATE_CHAN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5346 | l2cap_connect_create_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5347 | break; |
| 5348 | |
| 5349 | case L2CAP_CONF_REQ: |
| 5350 | err = l2cap_config_req(conn, cmd, cmd_len, data); |
| 5351 | break; |
| 5352 | |
| 5353 | case L2CAP_CONF_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5354 | l2cap_config_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5355 | break; |
| 5356 | |
| 5357 | case L2CAP_DISCONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5358 | err = l2cap_disconnect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5359 | break; |
| 5360 | |
| 5361 | case L2CAP_DISCONN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5362 | l2cap_disconnect_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5363 | break; |
| 5364 | |
| 5365 | case L2CAP_ECHO_REQ: |
| 5366 | l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); |
| 5367 | break; |
| 5368 | |
| 5369 | case L2CAP_ECHO_RSP: |
| 5370 | break; |
| 5371 | |
| 5372 | case L2CAP_INFO_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5373 | err = l2cap_information_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5374 | break; |
| 5375 | |
| 5376 | case L2CAP_INFO_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5377 | l2cap_information_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5378 | break; |
| 5379 | |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 5380 | case L2CAP_CREATE_CHAN_REQ: |
| 5381 | err = l2cap_create_channel_req(conn, cmd, cmd_len, data); |
| 5382 | break; |
| 5383 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5384 | case L2CAP_MOVE_CHAN_REQ: |
| 5385 | err = l2cap_move_channel_req(conn, cmd, cmd_len, data); |
| 5386 | break; |
| 5387 | |
| 5388 | case L2CAP_MOVE_CHAN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5389 | l2cap_move_channel_rsp(conn, cmd, cmd_len, data); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5390 | break; |
| 5391 | |
| 5392 | case L2CAP_MOVE_CHAN_CFM: |
| 5393 | err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); |
| 5394 | break; |
| 5395 | |
| 5396 | case L2CAP_MOVE_CHAN_CFM_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5397 | l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5398 | break; |
| 5399 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5400 | default: |
| 5401 | BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); |
| 5402 | err = -EINVAL; |
| 5403 | break; |
| 5404 | } |
| 5405 | |
| 5406 | return err; |
| 5407 | } |
| 5408 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5409 | static int l2cap_le_connect_req(struct l2cap_conn *conn, |
| 5410 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5411 | u8 *data) |
| 5412 | { |
| 5413 | struct l2cap_le_conn_req *req = (struct l2cap_le_conn_req *) data; |
| 5414 | struct l2cap_le_conn_rsp rsp; |
| 5415 | struct l2cap_chan *chan, *pchan; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5416 | u16 dcid, scid, credits, mtu, mps; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5417 | __le16 psm; |
| 5418 | u8 result; |
| 5419 | |
| 5420 | if (cmd_len != sizeof(*req)) |
| 5421 | return -EPROTO; |
| 5422 | |
| 5423 | scid = __le16_to_cpu(req->scid); |
| 5424 | mtu = __le16_to_cpu(req->mtu); |
| 5425 | mps = __le16_to_cpu(req->mps); |
| 5426 | psm = req->psm; |
| 5427 | dcid = 0; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5428 | credits = 0; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5429 | |
| 5430 | if (mtu < 23 || mps < 23) |
| 5431 | return -EPROTO; |
| 5432 | |
| 5433 | BT_DBG("psm 0x%2.2x scid 0x%4.4x mtu %u mps %u", __le16_to_cpu(psm), |
| 5434 | scid, mtu, mps); |
| 5435 | |
| 5436 | /* Check if we have socket listening on psm */ |
| 5437 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src, |
| 5438 | &conn->hcon->dst, LE_LINK); |
| 5439 | if (!pchan) { |
| 5440 | result = L2CAP_CR_BAD_PSM; |
| 5441 | chan = NULL; |
| 5442 | goto response; |
| 5443 | } |
| 5444 | |
| 5445 | mutex_lock(&conn->chan_lock); |
| 5446 | l2cap_chan_lock(pchan); |
| 5447 | |
| 5448 | if (!smp_sufficient_security(conn->hcon, pchan->sec_level)) { |
| 5449 | result = L2CAP_CR_AUTHENTICATION; |
| 5450 | chan = NULL; |
| 5451 | goto response_unlock; |
| 5452 | } |
| 5453 | |
| 5454 | /* Check if we already have channel with that dcid */ |
| 5455 | if (__l2cap_get_chan_by_dcid(conn, scid)) { |
| 5456 | result = L2CAP_CR_NO_MEM; |
| 5457 | chan = NULL; |
| 5458 | goto response_unlock; |
| 5459 | } |
| 5460 | |
| 5461 | chan = pchan->ops->new_connection(pchan); |
| 5462 | if (!chan) { |
| 5463 | result = L2CAP_CR_NO_MEM; |
| 5464 | goto response_unlock; |
| 5465 | } |
| 5466 | |
Johan Hedberg | 0ce43ce | 2013-12-05 14:55:33 +0200 | [diff] [blame] | 5467 | l2cap_le_flowctl_init(chan); |
| 5468 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5469 | bacpy(&chan->src, &conn->hcon->src); |
| 5470 | bacpy(&chan->dst, &conn->hcon->dst); |
| 5471 | chan->src_type = bdaddr_type(conn->hcon, conn->hcon->src_type); |
| 5472 | chan->dst_type = bdaddr_type(conn->hcon, conn->hcon->dst_type); |
| 5473 | chan->psm = psm; |
| 5474 | chan->dcid = scid; |
| 5475 | chan->omtu = mtu; |
| 5476 | chan->remote_mps = mps; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5477 | chan->tx_credits = __le16_to_cpu(req->credits); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5478 | |
| 5479 | __l2cap_chan_add(conn, chan); |
| 5480 | dcid = chan->scid; |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5481 | credits = chan->rx_credits; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5482 | |
| 5483 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
| 5484 | |
| 5485 | chan->ident = cmd->ident; |
| 5486 | |
| 5487 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
| 5488 | l2cap_state_change(chan, BT_CONNECT2); |
| 5489 | result = L2CAP_CR_PEND; |
| 5490 | chan->ops->defer(chan); |
| 5491 | } else { |
| 5492 | l2cap_chan_ready(chan); |
| 5493 | result = L2CAP_CR_SUCCESS; |
| 5494 | } |
| 5495 | |
| 5496 | response_unlock: |
| 5497 | l2cap_chan_unlock(pchan); |
| 5498 | mutex_unlock(&conn->chan_lock); |
| 5499 | |
| 5500 | if (result == L2CAP_CR_PEND) |
| 5501 | return 0; |
| 5502 | |
| 5503 | response: |
| 5504 | if (chan) { |
| 5505 | rsp.mtu = cpu_to_le16(chan->imtu); |
Johan Hedberg | 3916aed | 2013-10-07 15:35:26 +0200 | [diff] [blame] | 5506 | rsp.mps = cpu_to_le16(chan->mps); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5507 | } else { |
| 5508 | rsp.mtu = 0; |
| 5509 | rsp.mps = 0; |
| 5510 | } |
| 5511 | |
| 5512 | rsp.dcid = cpu_to_le16(dcid); |
Johan Hedberg | 0cd75f7 | 2013-05-17 13:09:05 +0300 | [diff] [blame] | 5513 | rsp.credits = cpu_to_le16(credits); |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5514 | rsp.result = cpu_to_le16(result); |
| 5515 | |
| 5516 | l2cap_send_cmd(conn, cmd->ident, L2CAP_LE_CONN_RSP, sizeof(rsp), &rsp); |
| 5517 | |
| 5518 | return 0; |
| 5519 | } |
| 5520 | |
Johan Hedberg | fad5fc8 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5521 | static inline int l2cap_le_credits(struct l2cap_conn *conn, |
| 5522 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5523 | u8 *data) |
| 5524 | { |
| 5525 | struct l2cap_le_credits *pkt; |
| 5526 | struct l2cap_chan *chan; |
Johan Hedberg | 0f1bfe4 | 2014-01-27 15:11:35 -0800 | [diff] [blame] | 5527 | u16 cid, credits, max_credits; |
Johan Hedberg | fad5fc8 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5528 | |
| 5529 | if (cmd_len != sizeof(*pkt)) |
| 5530 | return -EPROTO; |
| 5531 | |
| 5532 | pkt = (struct l2cap_le_credits *) data; |
| 5533 | cid = __le16_to_cpu(pkt->cid); |
| 5534 | credits = __le16_to_cpu(pkt->credits); |
| 5535 | |
| 5536 | BT_DBG("cid 0x%4.4x credits 0x%4.4x", cid, credits); |
| 5537 | |
| 5538 | chan = l2cap_get_chan_by_dcid(conn, cid); |
| 5539 | if (!chan) |
| 5540 | return -EBADSLT; |
| 5541 | |
Johan Hedberg | 0f1bfe4 | 2014-01-27 15:11:35 -0800 | [diff] [blame] | 5542 | max_credits = LE_FLOWCTL_MAX_CREDITS - chan->tx_credits; |
| 5543 | if (credits > max_credits) { |
| 5544 | BT_ERR("LE credits overflow"); |
| 5545 | l2cap_send_disconn_req(chan, ECONNRESET); |
| 5546 | |
| 5547 | /* Return 0 so that we don't trigger an unnecessary |
| 5548 | * command reject packet. |
| 5549 | */ |
| 5550 | return 0; |
| 5551 | } |
| 5552 | |
Johan Hedberg | fad5fc8 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5553 | chan->tx_credits += credits; |
| 5554 | |
| 5555 | while (chan->tx_credits && !skb_queue_empty(&chan->tx_q)) { |
| 5556 | l2cap_do_send(chan, skb_dequeue(&chan->tx_q)); |
| 5557 | chan->tx_credits--; |
| 5558 | } |
| 5559 | |
| 5560 | if (chan->tx_credits) |
| 5561 | chan->ops->resume(chan); |
| 5562 | |
| 5563 | l2cap_chan_unlock(chan); |
| 5564 | |
| 5565 | return 0; |
| 5566 | } |
| 5567 | |
Johan Hedberg | 71fb419 | 2013-12-10 10:52:48 +0200 | [diff] [blame] | 5568 | static inline int l2cap_le_command_rej(struct l2cap_conn *conn, |
| 5569 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5570 | u8 *data) |
| 5571 | { |
| 5572 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
| 5573 | struct l2cap_chan *chan; |
| 5574 | |
| 5575 | if (cmd_len < sizeof(*rej)) |
| 5576 | return -EPROTO; |
| 5577 | |
| 5578 | mutex_lock(&conn->chan_lock); |
| 5579 | |
| 5580 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 5581 | if (!chan) |
| 5582 | goto done; |
| 5583 | |
| 5584 | l2cap_chan_lock(chan); |
| 5585 | l2cap_chan_del(chan, ECONNREFUSED); |
| 5586 | l2cap_chan_unlock(chan); |
| 5587 | |
| 5588 | done: |
| 5589 | mutex_unlock(&conn->chan_lock); |
| 5590 | return 0; |
| 5591 | } |
| 5592 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5593 | static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5594 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5595 | u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5596 | { |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5597 | int err = 0; |
| 5598 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5599 | switch (cmd->code) { |
| 5600 | case L2CAP_COMMAND_REJ: |
Johan Hedberg | 71fb419 | 2013-12-10 10:52:48 +0200 | [diff] [blame] | 5601 | l2cap_le_command_rej(conn, cmd, cmd_len, data); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5602 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5603 | |
| 5604 | case L2CAP_CONN_PARAM_UPDATE_REQ: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5605 | err = l2cap_conn_param_update_req(conn, cmd, cmd_len, data); |
| 5606 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5607 | |
| 5608 | case L2CAP_CONN_PARAM_UPDATE_RSP: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5609 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5610 | |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5611 | case L2CAP_LE_CONN_RSP: |
| 5612 | l2cap_le_connect_rsp(conn, cmd, cmd_len, data); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5613 | break; |
Johan Hedberg | f1496de | 2013-05-13 14:15:56 +0300 | [diff] [blame] | 5614 | |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5615 | case L2CAP_LE_CONN_REQ: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5616 | err = l2cap_le_connect_req(conn, cmd, cmd_len, data); |
| 5617 | break; |
Johan Hedberg | 27e2d4c | 2013-05-14 13:27:21 +0300 | [diff] [blame] | 5618 | |
Johan Hedberg | fad5fc8 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 5619 | case L2CAP_LE_CREDITS: |
| 5620 | err = l2cap_le_credits(conn, cmd, cmd_len, data); |
| 5621 | break; |
| 5622 | |
Johan Hedberg | 3defe01 | 2013-05-15 10:16:06 +0300 | [diff] [blame] | 5623 | case L2CAP_DISCONN_REQ: |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5624 | err = l2cap_disconnect_req(conn, cmd, cmd_len, data); |
| 5625 | break; |
Johan Hedberg | 3defe01 | 2013-05-15 10:16:06 +0300 | [diff] [blame] | 5626 | |
| 5627 | case L2CAP_DISCONN_RSP: |
| 5628 | l2cap_disconnect_rsp(conn, cmd, cmd_len, data); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5629 | break; |
Johan Hedberg | 3defe01 | 2013-05-15 10:16:06 +0300 | [diff] [blame] | 5630 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5631 | default: |
| 5632 | BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5633 | err = -EINVAL; |
| 5634 | break; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5635 | } |
Johan Hedberg | b5ecba6 | 2013-12-02 12:21:29 +0200 | [diff] [blame] | 5636 | |
| 5637 | return err; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5638 | } |
| 5639 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5640 | static inline void l2cap_le_sig_channel(struct l2cap_conn *conn, |
| 5641 | struct sk_buff *skb) |
| 5642 | { |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5643 | struct hci_conn *hcon = conn->hcon; |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5644 | struct l2cap_cmd_hdr *cmd; |
| 5645 | u16 len; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5646 | int err; |
| 5647 | |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5648 | if (hcon->type != LE_LINK) |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5649 | goto drop; |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5650 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5651 | if (skb->len < L2CAP_CMD_HDR_SIZE) |
| 5652 | goto drop; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5653 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5654 | cmd = (void *) skb->data; |
| 5655 | skb_pull(skb, L2CAP_CMD_HDR_SIZE); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5656 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5657 | len = le16_to_cpu(cmd->len); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5658 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5659 | 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] | 5660 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5661 | if (len != skb->len || !cmd->ident) { |
| 5662 | BT_DBG("corrupted command"); |
| 5663 | goto drop; |
| 5664 | } |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5665 | |
Johan Hedberg | 203e639 | 2013-05-15 10:07:15 +0300 | [diff] [blame] | 5666 | err = l2cap_le_sig_cmd(conn, cmd, len, skb->data); |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5667 | if (err) { |
| 5668 | struct l2cap_cmd_rej_unk rej; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5669 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5670 | BT_ERR("Wrong link type (%d)", err); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5671 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5672 | rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5673 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
| 5674 | sizeof(rej), &rej); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5675 | } |
| 5676 | |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5677 | drop: |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5678 | kfree_skb(skb); |
| 5679 | } |
| 5680 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5681 | static inline void l2cap_sig_channel(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5682 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5683 | { |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5684 | struct hci_conn *hcon = conn->hcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5685 | u8 *data = skb->data; |
| 5686 | int len = skb->len; |
| 5687 | struct l2cap_cmd_hdr cmd; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5688 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5689 | |
| 5690 | l2cap_raw_recv(conn, skb); |
| 5691 | |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5692 | if (hcon->type != ACL_LINK) |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5693 | goto drop; |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5695 | while (len >= L2CAP_CMD_HDR_SIZE) { |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5696 | u16 cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5697 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 5698 | data += L2CAP_CMD_HDR_SIZE; |
| 5699 | len -= L2CAP_CMD_HDR_SIZE; |
| 5700 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5701 | cmd_len = le16_to_cpu(cmd.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5703 | BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, |
| 5704 | cmd.ident); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5705 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5706 | if (cmd_len > len || !cmd.ident) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5707 | BT_DBG("corrupted command"); |
| 5708 | break; |
| 5709 | } |
| 5710 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5711 | err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5712 | if (err) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 5713 | struct l2cap_cmd_rej_unk rej; |
Gustavo F. Padovan | 2c6d1a2 | 2011-03-23 14:38:32 -0300 | [diff] [blame] | 5714 | |
| 5715 | BT_ERR("Wrong link type (%d)", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5716 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5717 | rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5718 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, |
| 5719 | sizeof(rej), &rej); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5720 | } |
| 5721 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5722 | data += cmd_len; |
| 5723 | len -= cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | } |
| 5725 | |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5726 | drop: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5727 | kfree_skb(skb); |
| 5728 | } |
| 5729 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5730 | 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] | 5731 | { |
| 5732 | u16 our_fcs, rcv_fcs; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 5733 | int hdr_size; |
| 5734 | |
| 5735 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 5736 | hdr_size = L2CAP_EXT_HDR_SIZE; |
| 5737 | else |
| 5738 | hdr_size = L2CAP_ENH_HDR_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5739 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5740 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5741 | skb_trim(skb, skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5742 | rcv_fcs = get_unaligned_le16(skb->data + skb->len); |
| 5743 | our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); |
| 5744 | |
| 5745 | if (our_fcs != rcv_fcs) |
João Paulo Rechi Vita | 7a560e5 | 2010-06-22 13:56:27 -0300 | [diff] [blame] | 5746 | return -EBADMSG; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5747 | } |
| 5748 | return 0; |
| 5749 | } |
| 5750 | |
Mat Martineau | 6ea0048 | 2012-05-17 20:53:52 -0700 | [diff] [blame] | 5751 | 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] | 5752 | { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5753 | struct l2cap_ctrl control; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5754 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5755 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5756 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5757 | memset(&control, 0, sizeof(control)); |
| 5758 | control.sframe = 1; |
| 5759 | control.final = 1; |
| 5760 | control.reqseq = chan->buffer_seq; |
| 5761 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5762 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5763 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5764 | control.super = L2CAP_SUPER_RNR; |
| 5765 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5766 | } |
| 5767 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5768 | if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
| 5769 | chan->unacked_frames > 0) |
| 5770 | __set_retrans_timer(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5771 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5772 | /* Send pending iframes */ |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 5773 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5774 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5775 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5776 | test_bit(CONN_SEND_FBIT, &chan->conn_state)) { |
| 5777 | /* F-bit wasn't sent in an s-frame or i-frame yet, so |
| 5778 | * send it now. |
| 5779 | */ |
| 5780 | control.super = L2CAP_SUPER_RR; |
| 5781 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5782 | } |
| 5783 | } |
| 5784 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5785 | static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag, |
| 5786 | struct sk_buff **last_frag) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5787 | { |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5788 | /* skb->len reflects data in skb as well as all fragments |
| 5789 | * skb->data_len reflects only data in fragments |
| 5790 | */ |
| 5791 | if (!skb_has_frag_list(skb)) |
| 5792 | skb_shinfo(skb)->frag_list = new_frag; |
| 5793 | |
| 5794 | new_frag->next = NULL; |
| 5795 | |
| 5796 | (*last_frag)->next = new_frag; |
| 5797 | *last_frag = new_frag; |
| 5798 | |
| 5799 | skb->len += new_frag->len; |
| 5800 | skb->data_len += new_frag->len; |
| 5801 | skb->truesize += new_frag->truesize; |
| 5802 | } |
| 5803 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5804 | static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, |
| 5805 | struct l2cap_ctrl *control) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5806 | { |
| 5807 | int err = -EINVAL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5808 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5809 | switch (control->sar) { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5810 | case L2CAP_SAR_UNSEGMENTED: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5811 | if (chan->sdu) |
| 5812 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5813 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5814 | err = chan->ops->recv(chan, skb); |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5815 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5816 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5817 | case L2CAP_SAR_START: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5818 | if (chan->sdu) |
| 5819 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5820 | |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5821 | chan->sdu_len = get_unaligned_le16(skb->data); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5822 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5823 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5824 | if (chan->sdu_len > chan->imtu) { |
| 5825 | err = -EMSGSIZE; |
| 5826 | break; |
| 5827 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5828 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5829 | if (skb->len >= chan->sdu_len) |
| 5830 | break; |
| 5831 | |
| 5832 | chan->sdu = skb; |
| 5833 | chan->sdu_last_frag = skb; |
| 5834 | |
| 5835 | skb = NULL; |
| 5836 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5837 | break; |
| 5838 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5839 | case L2CAP_SAR_CONTINUE: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5840 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5841 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5842 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5843 | append_skb_frag(chan->sdu, skb, |
| 5844 | &chan->sdu_last_frag); |
| 5845 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5846 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5847 | if (chan->sdu->len >= chan->sdu_len) |
| 5848 | break; |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5849 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5850 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5851 | break; |
| 5852 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5853 | case L2CAP_SAR_END: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5854 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5855 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5856 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5857 | append_skb_frag(chan->sdu, skb, |
| 5858 | &chan->sdu_last_frag); |
| 5859 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5860 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5861 | if (chan->sdu->len != chan->sdu_len) |
| 5862 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5863 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5864 | err = chan->ops->recv(chan, chan->sdu); |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5865 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5866 | if (!err) { |
| 5867 | /* Reassembly complete */ |
| 5868 | chan->sdu = NULL; |
| 5869 | chan->sdu_last_frag = NULL; |
| 5870 | chan->sdu_len = 0; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5871 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5872 | break; |
| 5873 | } |
| 5874 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5875 | if (err) { |
| 5876 | kfree_skb(skb); |
| 5877 | kfree_skb(chan->sdu); |
| 5878 | chan->sdu = NULL; |
| 5879 | chan->sdu_last_frag = NULL; |
| 5880 | chan->sdu_len = 0; |
| 5881 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5882 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5883 | return err; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5884 | } |
| 5885 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 5886 | static int l2cap_resegment(struct l2cap_chan *chan) |
| 5887 | { |
| 5888 | /* Placeholder */ |
| 5889 | return 0; |
| 5890 | } |
| 5891 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 5892 | void l2cap_chan_busy(struct l2cap_chan *chan, int busy) |
Gustavo F. Padovan | 712132eb | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 5893 | { |
Mat Martineau | 61aa4f5 | 2012-05-17 20:53:40 -0700 | [diff] [blame] | 5894 | u8 event; |
| 5895 | |
| 5896 | if (chan->mode != L2CAP_MODE_ERTM) |
| 5897 | return; |
| 5898 | |
| 5899 | event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR; |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 5900 | l2cap_tx(chan, NULL, NULL, event); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5901 | } |
| 5902 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5903 | static int l2cap_rx_queued_iframes(struct l2cap_chan *chan) |
| 5904 | { |
Mat Martineau | 6383872 | 2012-05-17 20:53:45 -0700 | [diff] [blame] | 5905 | int err = 0; |
| 5906 | /* Pass sequential frames to l2cap_reassemble_sdu() |
| 5907 | * until a gap is encountered. |
| 5908 | */ |
| 5909 | |
| 5910 | BT_DBG("chan %p", chan); |
| 5911 | |
| 5912 | while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5913 | struct sk_buff *skb; |
| 5914 | BT_DBG("Searching for skb with txseq %d (queue len %d)", |
| 5915 | chan->buffer_seq, skb_queue_len(&chan->srej_q)); |
| 5916 | |
| 5917 | skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq); |
| 5918 | |
| 5919 | if (!skb) |
| 5920 | break; |
| 5921 | |
| 5922 | skb_unlink(skb, &chan->srej_q); |
| 5923 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 5924 | err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->control); |
| 5925 | if (err) |
| 5926 | break; |
| 5927 | } |
| 5928 | |
| 5929 | if (skb_queue_empty(&chan->srej_q)) { |
| 5930 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 5931 | l2cap_send_ack(chan); |
| 5932 | } |
| 5933 | |
| 5934 | return err; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5935 | } |
| 5936 | |
| 5937 | static void l2cap_handle_srej(struct l2cap_chan *chan, |
| 5938 | struct l2cap_ctrl *control) |
| 5939 | { |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5940 | struct sk_buff *skb; |
| 5941 | |
| 5942 | BT_DBG("chan %p, control %p", chan, control); |
| 5943 | |
| 5944 | if (control->reqseq == chan->next_tx_seq) { |
| 5945 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5946 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5947 | return; |
| 5948 | } |
| 5949 | |
| 5950 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 5951 | |
| 5952 | if (skb == NULL) { |
| 5953 | BT_DBG("Seq %d not available for retransmission", |
| 5954 | control->reqseq); |
| 5955 | return; |
| 5956 | } |
| 5957 | |
| 5958 | if (chan->max_tx != 0 && bt_cb(skb)->control.retries >= chan->max_tx) { |
| 5959 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5960 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5961 | return; |
| 5962 | } |
| 5963 | |
| 5964 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5965 | |
| 5966 | if (control->poll) { |
| 5967 | l2cap_pass_to_tx(chan, control); |
| 5968 | |
| 5969 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 5970 | l2cap_retransmit(chan, control); |
| 5971 | l2cap_ertm_send(chan); |
| 5972 | |
| 5973 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 5974 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5975 | chan->srej_save_reqseq = control->reqseq; |
| 5976 | } |
| 5977 | } else { |
| 5978 | l2cap_pass_to_tx_fbit(chan, control); |
| 5979 | |
| 5980 | if (control->final) { |
| 5981 | if (chan->srej_save_reqseq != control->reqseq || |
| 5982 | !test_and_clear_bit(CONN_SREJ_ACT, |
| 5983 | &chan->conn_state)) |
| 5984 | l2cap_retransmit(chan, control); |
| 5985 | } else { |
| 5986 | l2cap_retransmit(chan, control); |
| 5987 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 5988 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5989 | chan->srej_save_reqseq = control->reqseq; |
| 5990 | } |
| 5991 | } |
| 5992 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5993 | } |
| 5994 | |
| 5995 | static void l2cap_handle_rej(struct l2cap_chan *chan, |
| 5996 | struct l2cap_ctrl *control) |
| 5997 | { |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5998 | struct sk_buff *skb; |
| 5999 | |
| 6000 | BT_DBG("chan %p, control %p", chan, control); |
| 6001 | |
| 6002 | if (control->reqseq == chan->next_tx_seq) { |
| 6003 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6004 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 6005 | return; |
| 6006 | } |
| 6007 | |
| 6008 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 6009 | |
| 6010 | if (chan->max_tx && skb && |
| 6011 | bt_cb(skb)->control.retries >= chan->max_tx) { |
| 6012 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6013 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 6014 | return; |
| 6015 | } |
| 6016 | |
| 6017 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6018 | |
| 6019 | l2cap_pass_to_tx(chan, control); |
| 6020 | |
| 6021 | if (control->final) { |
| 6022 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
| 6023 | l2cap_retransmit_all(chan, control); |
| 6024 | } else { |
| 6025 | l2cap_retransmit_all(chan, control); |
| 6026 | l2cap_ertm_send(chan); |
| 6027 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) |
| 6028 | set_bit(CONN_REJ_ACT, &chan->conn_state); |
| 6029 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6030 | } |
| 6031 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6032 | static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq) |
| 6033 | { |
| 6034 | BT_DBG("chan %p, txseq %d", chan, txseq); |
| 6035 | |
| 6036 | BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq, |
| 6037 | chan->expected_tx_seq); |
| 6038 | |
| 6039 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 6040 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6041 | chan->tx_win) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6042 | /* See notes below regarding "double poll" and |
| 6043 | * invalid packets. |
| 6044 | */ |
| 6045 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 6046 | BT_DBG("Invalid/Ignore - after SREJ"); |
| 6047 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 6048 | } else { |
| 6049 | BT_DBG("Invalid - in window after SREJ sent"); |
| 6050 | return L2CAP_TXSEQ_INVALID; |
| 6051 | } |
| 6052 | } |
| 6053 | |
| 6054 | if (chan->srej_list.head == txseq) { |
| 6055 | BT_DBG("Expected SREJ"); |
| 6056 | return L2CAP_TXSEQ_EXPECTED_SREJ; |
| 6057 | } |
| 6058 | |
| 6059 | if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) { |
| 6060 | BT_DBG("Duplicate SREJ - txseq already stored"); |
| 6061 | return L2CAP_TXSEQ_DUPLICATE_SREJ; |
| 6062 | } |
| 6063 | |
| 6064 | if (l2cap_seq_list_contains(&chan->srej_list, txseq)) { |
| 6065 | BT_DBG("Unexpected SREJ - not requested"); |
| 6066 | return L2CAP_TXSEQ_UNEXPECTED_SREJ; |
| 6067 | } |
| 6068 | } |
| 6069 | |
| 6070 | if (chan->expected_tx_seq == txseq) { |
| 6071 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
| 6072 | chan->tx_win) { |
| 6073 | BT_DBG("Invalid - txseq outside tx window"); |
| 6074 | return L2CAP_TXSEQ_INVALID; |
| 6075 | } else { |
| 6076 | BT_DBG("Expected"); |
| 6077 | return L2CAP_TXSEQ_EXPECTED; |
| 6078 | } |
| 6079 | } |
| 6080 | |
| 6081 | if (__seq_offset(chan, txseq, chan->last_acked_seq) < |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6082 | __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6083 | BT_DBG("Duplicate - expected_tx_seq later than txseq"); |
| 6084 | return L2CAP_TXSEQ_DUPLICATE; |
| 6085 | } |
| 6086 | |
| 6087 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) { |
| 6088 | /* A source of invalid packets is a "double poll" condition, |
| 6089 | * where delays cause us to send multiple poll packets. If |
| 6090 | * the remote stack receives and processes both polls, |
| 6091 | * sequence numbers can wrap around in such a way that a |
| 6092 | * resent frame has a sequence number that looks like new data |
| 6093 | * with a sequence gap. This would trigger an erroneous SREJ |
| 6094 | * request. |
| 6095 | * |
| 6096 | * Fortunately, this is impossible with a tx window that's |
| 6097 | * less than half of the maximum sequence number, which allows |
| 6098 | * invalid frames to be safely ignored. |
| 6099 | * |
| 6100 | * With tx window sizes greater than half of the tx window |
| 6101 | * maximum, the frame is invalid and cannot be ignored. This |
| 6102 | * causes a disconnect. |
| 6103 | */ |
| 6104 | |
| 6105 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 6106 | BT_DBG("Invalid/Ignore - txseq outside tx window"); |
| 6107 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 6108 | } else { |
| 6109 | BT_DBG("Invalid - txseq outside tx window"); |
| 6110 | return L2CAP_TXSEQ_INVALID; |
| 6111 | } |
| 6112 | } else { |
| 6113 | BT_DBG("Unexpected - txseq indicates missing frames"); |
| 6114 | return L2CAP_TXSEQ_UNEXPECTED; |
| 6115 | } |
| 6116 | } |
| 6117 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6118 | static int l2cap_rx_state_recv(struct l2cap_chan *chan, |
| 6119 | struct l2cap_ctrl *control, |
| 6120 | struct sk_buff *skb, u8 event) |
| 6121 | { |
| 6122 | int err = 0; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6123 | bool skb_in_use = false; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6124 | |
| 6125 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6126 | event); |
| 6127 | |
| 6128 | switch (event) { |
| 6129 | case L2CAP_EV_RECV_IFRAME: |
| 6130 | switch (l2cap_classify_txseq(chan, control->txseq)) { |
| 6131 | case L2CAP_TXSEQ_EXPECTED: |
| 6132 | l2cap_pass_to_tx(chan, control); |
| 6133 | |
| 6134 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 6135 | BT_DBG("Busy, discarding expected seq %d", |
| 6136 | control->txseq); |
| 6137 | break; |
| 6138 | } |
| 6139 | |
| 6140 | chan->expected_tx_seq = __next_seq(chan, |
| 6141 | control->txseq); |
| 6142 | |
| 6143 | chan->buffer_seq = chan->expected_tx_seq; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6144 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6145 | |
| 6146 | err = l2cap_reassemble_sdu(chan, skb, control); |
| 6147 | if (err) |
| 6148 | break; |
| 6149 | |
| 6150 | if (control->final) { |
| 6151 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 6152 | &chan->conn_state)) { |
| 6153 | control->final = 0; |
| 6154 | l2cap_retransmit_all(chan, control); |
| 6155 | l2cap_ertm_send(chan); |
| 6156 | } |
| 6157 | } |
| 6158 | |
| 6159 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 6160 | l2cap_send_ack(chan); |
| 6161 | break; |
| 6162 | case L2CAP_TXSEQ_UNEXPECTED: |
| 6163 | l2cap_pass_to_tx(chan, control); |
| 6164 | |
| 6165 | /* Can't issue SREJ frames in the local busy state. |
| 6166 | * Drop this frame, it will be seen as missing |
| 6167 | * when local busy is exited. |
| 6168 | */ |
| 6169 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 6170 | BT_DBG("Busy, discarding unexpected seq %d", |
| 6171 | control->txseq); |
| 6172 | break; |
| 6173 | } |
| 6174 | |
| 6175 | /* There was a gap in the sequence, so an SREJ |
| 6176 | * must be sent for each missing frame. The |
| 6177 | * current frame is stored for later use. |
| 6178 | */ |
| 6179 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6180 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6181 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6182 | skb_queue_len(&chan->srej_q)); |
| 6183 | |
| 6184 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 6185 | l2cap_seq_list_clear(&chan->srej_list); |
| 6186 | l2cap_send_srej(chan, control->txseq); |
| 6187 | |
| 6188 | chan->rx_state = L2CAP_RX_STATE_SREJ_SENT; |
| 6189 | break; |
| 6190 | case L2CAP_TXSEQ_DUPLICATE: |
| 6191 | l2cap_pass_to_tx(chan, control); |
| 6192 | break; |
| 6193 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 6194 | break; |
| 6195 | case L2CAP_TXSEQ_INVALID: |
| 6196 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6197 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6198 | break; |
| 6199 | } |
| 6200 | break; |
| 6201 | case L2CAP_EV_RECV_RR: |
| 6202 | l2cap_pass_to_tx(chan, control); |
| 6203 | if (control->final) { |
| 6204 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6205 | |
Mat Martineau | e6a3ee6 | 2012-10-23 15:24:22 -0700 | [diff] [blame] | 6206 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) && |
| 6207 | !__chan_is_moving(chan)) { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6208 | control->final = 0; |
| 6209 | l2cap_retransmit_all(chan, control); |
| 6210 | } |
| 6211 | |
| 6212 | l2cap_ertm_send(chan); |
| 6213 | } else if (control->poll) { |
| 6214 | l2cap_send_i_or_rr_or_rnr(chan); |
| 6215 | } else { |
| 6216 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6217 | &chan->conn_state) && |
| 6218 | chan->unacked_frames) |
| 6219 | __set_retrans_timer(chan); |
| 6220 | |
| 6221 | l2cap_ertm_send(chan); |
| 6222 | } |
| 6223 | break; |
| 6224 | case L2CAP_EV_RECV_RNR: |
| 6225 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6226 | l2cap_pass_to_tx(chan, control); |
| 6227 | if (control && control->poll) { |
| 6228 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6229 | l2cap_send_rr_or_rnr(chan, 0); |
| 6230 | } |
| 6231 | __clear_retrans_timer(chan); |
| 6232 | l2cap_seq_list_clear(&chan->retrans_list); |
| 6233 | break; |
| 6234 | case L2CAP_EV_RECV_REJ: |
| 6235 | l2cap_handle_rej(chan, control); |
| 6236 | break; |
| 6237 | case L2CAP_EV_RECV_SREJ: |
| 6238 | l2cap_handle_srej(chan, control); |
| 6239 | break; |
| 6240 | default: |
| 6241 | break; |
| 6242 | } |
| 6243 | |
| 6244 | if (skb && !skb_in_use) { |
| 6245 | BT_DBG("Freeing %p", skb); |
| 6246 | kfree_skb(skb); |
| 6247 | } |
| 6248 | |
| 6249 | return err; |
| 6250 | } |
| 6251 | |
| 6252 | static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan, |
| 6253 | struct l2cap_ctrl *control, |
| 6254 | struct sk_buff *skb, u8 event) |
| 6255 | { |
| 6256 | int err = 0; |
| 6257 | u16 txseq = control->txseq; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6258 | bool skb_in_use = false; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6259 | |
| 6260 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6261 | event); |
| 6262 | |
| 6263 | switch (event) { |
| 6264 | case L2CAP_EV_RECV_IFRAME: |
| 6265 | switch (l2cap_classify_txseq(chan, txseq)) { |
| 6266 | case L2CAP_TXSEQ_EXPECTED: |
| 6267 | /* Keep frame for reassembly later */ |
| 6268 | l2cap_pass_to_tx(chan, control); |
| 6269 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6270 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6271 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6272 | skb_queue_len(&chan->srej_q)); |
| 6273 | |
| 6274 | chan->expected_tx_seq = __next_seq(chan, txseq); |
| 6275 | break; |
| 6276 | case L2CAP_TXSEQ_EXPECTED_SREJ: |
| 6277 | l2cap_seq_list_pop(&chan->srej_list); |
| 6278 | |
| 6279 | l2cap_pass_to_tx(chan, control); |
| 6280 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6281 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6282 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6283 | skb_queue_len(&chan->srej_q)); |
| 6284 | |
| 6285 | err = l2cap_rx_queued_iframes(chan); |
| 6286 | if (err) |
| 6287 | break; |
| 6288 | |
| 6289 | break; |
| 6290 | case L2CAP_TXSEQ_UNEXPECTED: |
| 6291 | /* Got a frame that can't be reassembled yet. |
| 6292 | * Save it for later, and send SREJs to cover |
| 6293 | * the missing frames. |
| 6294 | */ |
| 6295 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6296 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6297 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6298 | skb_queue_len(&chan->srej_q)); |
| 6299 | |
| 6300 | l2cap_pass_to_tx(chan, control); |
| 6301 | l2cap_send_srej(chan, control->txseq); |
| 6302 | break; |
| 6303 | case L2CAP_TXSEQ_UNEXPECTED_SREJ: |
| 6304 | /* This frame was requested with an SREJ, but |
| 6305 | * some expected retransmitted frames are |
| 6306 | * missing. Request retransmission of missing |
| 6307 | * SREJ'd frames. |
| 6308 | */ |
| 6309 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6310 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6311 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6312 | skb_queue_len(&chan->srej_q)); |
| 6313 | |
| 6314 | l2cap_pass_to_tx(chan, control); |
| 6315 | l2cap_send_srej_list(chan, control->txseq); |
| 6316 | break; |
| 6317 | case L2CAP_TXSEQ_DUPLICATE_SREJ: |
| 6318 | /* We've already queued this frame. Drop this copy. */ |
| 6319 | l2cap_pass_to_tx(chan, control); |
| 6320 | break; |
| 6321 | case L2CAP_TXSEQ_DUPLICATE: |
| 6322 | /* Expecting a later sequence number, so this frame |
| 6323 | * was already received. Ignore it completely. |
| 6324 | */ |
| 6325 | break; |
| 6326 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 6327 | break; |
| 6328 | case L2CAP_TXSEQ_INVALID: |
| 6329 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6330 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6331 | break; |
| 6332 | } |
| 6333 | break; |
| 6334 | case L2CAP_EV_RECV_RR: |
| 6335 | l2cap_pass_to_tx(chan, control); |
| 6336 | if (control->final) { |
| 6337 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6338 | |
| 6339 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 6340 | &chan->conn_state)) { |
| 6341 | control->final = 0; |
| 6342 | l2cap_retransmit_all(chan, control); |
| 6343 | } |
| 6344 | |
| 6345 | l2cap_ertm_send(chan); |
| 6346 | } else if (control->poll) { |
| 6347 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6348 | &chan->conn_state) && |
| 6349 | chan->unacked_frames) { |
| 6350 | __set_retrans_timer(chan); |
| 6351 | } |
| 6352 | |
| 6353 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6354 | l2cap_send_srej_tail(chan); |
| 6355 | } else { |
| 6356 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6357 | &chan->conn_state) && |
| 6358 | chan->unacked_frames) |
| 6359 | __set_retrans_timer(chan); |
| 6360 | |
| 6361 | l2cap_send_ack(chan); |
| 6362 | } |
| 6363 | break; |
| 6364 | case L2CAP_EV_RECV_RNR: |
| 6365 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6366 | l2cap_pass_to_tx(chan, control); |
| 6367 | if (control->poll) { |
| 6368 | l2cap_send_srej_tail(chan); |
| 6369 | } else { |
| 6370 | struct l2cap_ctrl rr_control; |
| 6371 | memset(&rr_control, 0, sizeof(rr_control)); |
| 6372 | rr_control.sframe = 1; |
| 6373 | rr_control.super = L2CAP_SUPER_RR; |
| 6374 | rr_control.reqseq = chan->buffer_seq; |
| 6375 | l2cap_send_sframe(chan, &rr_control); |
| 6376 | } |
| 6377 | |
| 6378 | break; |
| 6379 | case L2CAP_EV_RECV_REJ: |
| 6380 | l2cap_handle_rej(chan, control); |
| 6381 | break; |
| 6382 | case L2CAP_EV_RECV_SREJ: |
| 6383 | l2cap_handle_srej(chan, control); |
| 6384 | break; |
| 6385 | } |
| 6386 | |
| 6387 | if (skb && !skb_in_use) { |
| 6388 | BT_DBG("Freeing %p", skb); |
| 6389 | kfree_skb(skb); |
| 6390 | } |
| 6391 | |
| 6392 | return err; |
| 6393 | } |
| 6394 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6395 | static int l2cap_finish_move(struct l2cap_chan *chan) |
| 6396 | { |
| 6397 | BT_DBG("chan %p", chan); |
| 6398 | |
| 6399 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6400 | |
| 6401 | if (chan->hs_hcon) |
| 6402 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6403 | else |
| 6404 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6405 | |
| 6406 | return l2cap_resegment(chan); |
| 6407 | } |
| 6408 | |
| 6409 | static int l2cap_rx_state_wait_p(struct l2cap_chan *chan, |
| 6410 | struct l2cap_ctrl *control, |
| 6411 | struct sk_buff *skb, u8 event) |
| 6412 | { |
| 6413 | int err; |
| 6414 | |
| 6415 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6416 | event); |
| 6417 | |
| 6418 | if (!control->poll) |
| 6419 | return -EPROTO; |
| 6420 | |
| 6421 | l2cap_process_reqseq(chan, control->reqseq); |
| 6422 | |
| 6423 | if (!skb_queue_empty(&chan->tx_q)) |
| 6424 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6425 | else |
| 6426 | chan->tx_send_head = NULL; |
| 6427 | |
| 6428 | /* Rewind next_tx_seq to the point expected |
| 6429 | * by the receiver. |
| 6430 | */ |
| 6431 | chan->next_tx_seq = control->reqseq; |
| 6432 | chan->unacked_frames = 0; |
| 6433 | |
| 6434 | err = l2cap_finish_move(chan); |
| 6435 | if (err) |
| 6436 | return err; |
| 6437 | |
| 6438 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6439 | l2cap_send_i_or_rr_or_rnr(chan); |
| 6440 | |
| 6441 | if (event == L2CAP_EV_RECV_IFRAME) |
| 6442 | return -EPROTO; |
| 6443 | |
| 6444 | return l2cap_rx_state_recv(chan, control, NULL, event); |
| 6445 | } |
| 6446 | |
| 6447 | static int l2cap_rx_state_wait_f(struct l2cap_chan *chan, |
| 6448 | struct l2cap_ctrl *control, |
| 6449 | struct sk_buff *skb, u8 event) |
| 6450 | { |
| 6451 | int err; |
| 6452 | |
| 6453 | if (!control->final) |
| 6454 | return -EPROTO; |
| 6455 | |
| 6456 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6457 | |
| 6458 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6459 | l2cap_process_reqseq(chan, control->reqseq); |
| 6460 | |
| 6461 | if (!skb_queue_empty(&chan->tx_q)) |
| 6462 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6463 | else |
| 6464 | chan->tx_send_head = NULL; |
| 6465 | |
| 6466 | /* Rewind next_tx_seq to the point expected |
| 6467 | * by the receiver. |
| 6468 | */ |
| 6469 | chan->next_tx_seq = control->reqseq; |
| 6470 | chan->unacked_frames = 0; |
| 6471 | |
| 6472 | if (chan->hs_hcon) |
| 6473 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6474 | else |
| 6475 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6476 | |
| 6477 | err = l2cap_resegment(chan); |
| 6478 | |
| 6479 | if (!err) |
| 6480 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6481 | |
| 6482 | return err; |
| 6483 | } |
| 6484 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6485 | static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 6486 | { |
| 6487 | /* Make sure reqseq is for a packet that has been sent but not acked */ |
| 6488 | u16 unacked; |
| 6489 | |
| 6490 | unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq); |
| 6491 | return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked; |
| 6492 | } |
| 6493 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6494 | static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6495 | struct sk_buff *skb, u8 event) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6496 | { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6497 | int err = 0; |
| 6498 | |
| 6499 | BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan, |
| 6500 | control, skb, event, chan->rx_state); |
| 6501 | |
| 6502 | if (__valid_reqseq(chan, control->reqseq)) { |
| 6503 | switch (chan->rx_state) { |
| 6504 | case L2CAP_RX_STATE_RECV: |
| 6505 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6506 | break; |
| 6507 | case L2CAP_RX_STATE_SREJ_SENT: |
| 6508 | err = l2cap_rx_state_srej_sent(chan, control, skb, |
| 6509 | event); |
| 6510 | break; |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6511 | case L2CAP_RX_STATE_WAIT_P: |
| 6512 | err = l2cap_rx_state_wait_p(chan, control, skb, event); |
| 6513 | break; |
| 6514 | case L2CAP_RX_STATE_WAIT_F: |
| 6515 | err = l2cap_rx_state_wait_f(chan, control, skb, event); |
| 6516 | break; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6517 | default: |
| 6518 | /* shut it down */ |
| 6519 | break; |
| 6520 | } |
| 6521 | } else { |
| 6522 | BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d", |
| 6523 | control->reqseq, chan->next_tx_seq, |
| 6524 | chan->expected_ack_seq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6525 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6526 | } |
| 6527 | |
| 6528 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6529 | } |
| 6530 | |
| 6531 | static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6532 | struct sk_buff *skb) |
| 6533 | { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6534 | int err = 0; |
| 6535 | |
| 6536 | BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb, |
| 6537 | chan->rx_state); |
| 6538 | |
| 6539 | if (l2cap_classify_txseq(chan, control->txseq) == |
| 6540 | L2CAP_TXSEQ_EXPECTED) { |
| 6541 | l2cap_pass_to_tx(chan, control); |
| 6542 | |
| 6543 | BT_DBG("buffer_seq %d->%d", chan->buffer_seq, |
| 6544 | __next_seq(chan, chan->buffer_seq)); |
| 6545 | |
| 6546 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 6547 | |
| 6548 | l2cap_reassemble_sdu(chan, skb, control); |
| 6549 | } else { |
| 6550 | if (chan->sdu) { |
| 6551 | kfree_skb(chan->sdu); |
| 6552 | chan->sdu = NULL; |
| 6553 | } |
| 6554 | chan->sdu_last_frag = NULL; |
| 6555 | chan->sdu_len = 0; |
| 6556 | |
| 6557 | if (skb) { |
| 6558 | BT_DBG("Freeing %p", skb); |
| 6559 | kfree_skb(skb); |
| 6560 | } |
| 6561 | } |
| 6562 | |
| 6563 | chan->last_acked_seq = control->txseq; |
| 6564 | chan->expected_tx_seq = __next_seq(chan, control->txseq); |
| 6565 | |
| 6566 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6567 | } |
| 6568 | |
| 6569 | static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) |
| 6570 | { |
| 6571 | struct l2cap_ctrl *control = &bt_cb(skb)->control; |
| 6572 | u16 len; |
| 6573 | u8 event; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6574 | |
Mat Martineau | b76bbd6 | 2012-04-11 10:48:43 -0700 | [diff] [blame] | 6575 | __unpack_control(chan, skb); |
| 6576 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6577 | len = skb->len; |
| 6578 | |
| 6579 | /* |
| 6580 | * We can just drop the corrupted I-frame here. |
| 6581 | * Receiver will miss it and start proper recovery |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6582 | * procedures and ask for retransmission. |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6583 | */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6584 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6585 | goto drop; |
| 6586 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6587 | if (!control->sframe && control->sar == L2CAP_SAR_START) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6588 | len -= L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6589 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6590 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6591 | len -= L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6592 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6593 | if (len > chan->mps) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6594 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6595 | goto drop; |
| 6596 | } |
| 6597 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6598 | if (!control->sframe) { |
| 6599 | int err; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6600 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6601 | BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d", |
| 6602 | control->sar, control->reqseq, control->final, |
| 6603 | control->txseq); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 6604 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6605 | /* Validate F-bit - F=0 always valid, F=1 only |
| 6606 | * valid in TX WAIT_F |
| 6607 | */ |
| 6608 | if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6609 | goto drop; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6610 | |
| 6611 | if (chan->mode != L2CAP_MODE_STREAMING) { |
| 6612 | event = L2CAP_EV_RECV_IFRAME; |
| 6613 | err = l2cap_rx(chan, control, skb, event); |
| 6614 | } else { |
| 6615 | err = l2cap_stream_rx(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6616 | } |
| 6617 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6618 | if (err) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6619 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6620 | } else { |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6621 | const u8 rx_func_to_event[4] = { |
| 6622 | L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ, |
| 6623 | L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ |
| 6624 | }; |
| 6625 | |
| 6626 | /* Only I-frames are expected in streaming mode */ |
| 6627 | if (chan->mode == L2CAP_MODE_STREAMING) |
| 6628 | goto drop; |
| 6629 | |
| 6630 | BT_DBG("sframe reqseq %d, final %d, poll %d, super %d", |
| 6631 | control->reqseq, control->final, control->poll, |
| 6632 | control->super); |
| 6633 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6634 | if (len != 0) { |
Andrei Emeltchenko | 1bb166e | 2012-11-20 17:16:21 +0200 | [diff] [blame] | 6635 | BT_ERR("Trailing bytes: %d in sframe", len); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6636 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6637 | goto drop; |
| 6638 | } |
| 6639 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6640 | /* Validate F and P bits */ |
| 6641 | if (control->final && (control->poll || |
| 6642 | chan->tx_state != L2CAP_TX_STATE_WAIT_F)) |
| 6643 | goto drop; |
| 6644 | |
| 6645 | event = rx_func_to_event[control->super]; |
| 6646 | if (l2cap_rx(chan, control, skb, event)) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6647 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6648 | } |
| 6649 | |
| 6650 | return 0; |
| 6651 | |
| 6652 | drop: |
| 6653 | kfree_skb(skb); |
| 6654 | return 0; |
| 6655 | } |
| 6656 | |
Johan Hedberg | b1c325c | 2013-12-05 09:43:34 +0200 | [diff] [blame] | 6657 | static void l2cap_chan_le_send_credits(struct l2cap_chan *chan) |
| 6658 | { |
| 6659 | struct l2cap_conn *conn = chan->conn; |
| 6660 | struct l2cap_le_credits pkt; |
| 6661 | u16 return_credits; |
| 6662 | |
| 6663 | /* We return more credits to the sender only after the amount of |
| 6664 | * credits falls below half of the initial amount. |
| 6665 | */ |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 6666 | if (chan->rx_credits >= (le_max_credits + 1) / 2) |
Johan Hedberg | b1c325c | 2013-12-05 09:43:34 +0200 | [diff] [blame] | 6667 | return; |
| 6668 | |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 6669 | return_credits = le_max_credits - chan->rx_credits; |
Johan Hedberg | b1c325c | 2013-12-05 09:43:34 +0200 | [diff] [blame] | 6670 | |
| 6671 | BT_DBG("chan %p returning %u credits to sender", chan, return_credits); |
| 6672 | |
| 6673 | chan->rx_credits += return_credits; |
| 6674 | |
| 6675 | pkt.cid = cpu_to_le16(chan->scid); |
| 6676 | pkt.credits = cpu_to_le16(return_credits); |
| 6677 | |
| 6678 | chan->ident = l2cap_get_ident(conn); |
| 6679 | |
| 6680 | l2cap_send_cmd(conn, chan->ident, L2CAP_LE_CREDITS, sizeof(pkt), &pkt); |
| 6681 | } |
| 6682 | |
Johan Hedberg | fad5fc8 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6683 | static int l2cap_le_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) |
| 6684 | { |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6685 | int err; |
Johan Hedberg | fad5fc8 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6686 | |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6687 | if (!chan->rx_credits) { |
| 6688 | BT_ERR("No credits to receive LE L2CAP data"); |
Johan Hedberg | dfd9774 | 2014-01-27 15:11:34 -0800 | [diff] [blame] | 6689 | l2cap_send_disconn_req(chan, ECONNRESET); |
Johan Hedberg | fad5fc8 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6690 | return -ENOBUFS; |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6691 | } |
| 6692 | |
| 6693 | if (chan->imtu < skb->len) { |
| 6694 | BT_ERR("Too big LE L2CAP PDU"); |
| 6695 | return -ENOBUFS; |
| 6696 | } |
Johan Hedberg | fad5fc8 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6697 | |
| 6698 | chan->rx_credits--; |
| 6699 | BT_DBG("rx_credits %u -> %u", chan->rx_credits + 1, chan->rx_credits); |
| 6700 | |
| 6701 | l2cap_chan_le_send_credits(chan); |
| 6702 | |
Johan Hedberg | aac23bf | 2013-06-01 10:14:57 +0300 | [diff] [blame] | 6703 | err = 0; |
| 6704 | |
| 6705 | if (!chan->sdu) { |
| 6706 | u16 sdu_len; |
| 6707 | |
| 6708 | sdu_len = get_unaligned_le16(skb->data); |
| 6709 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
| 6710 | |
| 6711 | BT_DBG("Start of new SDU. sdu_len %u skb->len %u imtu %u", |
| 6712 | sdu_len, skb->len, chan->imtu); |
| 6713 | |
| 6714 | if (sdu_len > chan->imtu) { |
| 6715 | BT_ERR("Too big LE L2CAP SDU length received"); |
| 6716 | err = -EMSGSIZE; |
| 6717 | goto failed; |
| 6718 | } |
| 6719 | |
| 6720 | if (skb->len > sdu_len) { |
| 6721 | BT_ERR("Too much LE L2CAP data received"); |
| 6722 | err = -EINVAL; |
| 6723 | goto failed; |
| 6724 | } |
| 6725 | |
| 6726 | if (skb->len == sdu_len) |
| 6727 | return chan->ops->recv(chan, skb); |
| 6728 | |
| 6729 | chan->sdu = skb; |
| 6730 | chan->sdu_len = sdu_len; |
| 6731 | chan->sdu_last_frag = skb; |
| 6732 | |
| 6733 | return 0; |
| 6734 | } |
| 6735 | |
| 6736 | BT_DBG("SDU fragment. chan->sdu->len %u skb->len %u chan->sdu_len %u", |
| 6737 | chan->sdu->len, skb->len, chan->sdu_len); |
| 6738 | |
| 6739 | if (chan->sdu->len + skb->len > chan->sdu_len) { |
| 6740 | BT_ERR("Too much LE L2CAP data received"); |
| 6741 | err = -EINVAL; |
| 6742 | goto failed; |
| 6743 | } |
| 6744 | |
| 6745 | append_skb_frag(chan->sdu, skb, &chan->sdu_last_frag); |
| 6746 | skb = NULL; |
| 6747 | |
| 6748 | if (chan->sdu->len == chan->sdu_len) { |
| 6749 | err = chan->ops->recv(chan, chan->sdu); |
| 6750 | if (!err) { |
| 6751 | chan->sdu = NULL; |
| 6752 | chan->sdu_last_frag = NULL; |
| 6753 | chan->sdu_len = 0; |
| 6754 | } |
| 6755 | } |
| 6756 | |
| 6757 | failed: |
| 6758 | if (err) { |
| 6759 | kfree_skb(skb); |
| 6760 | kfree_skb(chan->sdu); |
| 6761 | chan->sdu = NULL; |
| 6762 | chan->sdu_last_frag = NULL; |
| 6763 | chan->sdu_len = 0; |
| 6764 | } |
| 6765 | |
| 6766 | /* We can't return an error here since we took care of the skb |
| 6767 | * freeing internally. An error return would cause the caller to |
| 6768 | * do a double-free of the skb. |
| 6769 | */ |
| 6770 | return 0; |
Johan Hedberg | fad5fc8 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6771 | } |
| 6772 | |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6773 | static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid, |
| 6774 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6775 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6776 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6777 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 6778 | chan = l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6779 | if (!chan) { |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6780 | if (cid == L2CAP_CID_A2MP) { |
| 6781 | chan = a2mp_channel_create(conn, skb); |
| 6782 | if (!chan) { |
| 6783 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6784 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6785 | } |
| 6786 | |
| 6787 | l2cap_chan_lock(chan); |
| 6788 | } else { |
| 6789 | BT_DBG("unknown cid 0x%4.4x", cid); |
| 6790 | /* Drop packet and return */ |
| 6791 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6792 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6793 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6794 | } |
| 6795 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 6796 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6797 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6798 | if (chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6799 | goto drop; |
| 6800 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6801 | switch (chan->mode) { |
Johan Hedberg | 3831971 | 2013-05-17 12:49:23 +0300 | [diff] [blame] | 6802 | case L2CAP_MODE_LE_FLOWCTL: |
Johan Hedberg | fad5fc8 | 2013-12-05 09:45:01 +0200 | [diff] [blame] | 6803 | if (l2cap_le_data_rcv(chan, skb) < 0) |
| 6804 | goto drop; |
| 6805 | |
| 6806 | goto done; |
| 6807 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6808 | case L2CAP_MODE_BASIC: |
| 6809 | /* If socket recv buffers overflows we drop data here |
| 6810 | * which is *bad* because L2CAP has to be reliable. |
| 6811 | * But we don't have any other choice. L2CAP doesn't |
| 6812 | * provide flow control mechanism. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6813 | |
Szymon Janc | 2c96e03 | 2014-02-18 20:48:34 +0100 | [diff] [blame] | 6814 | if (chan->imtu < skb->len) { |
| 6815 | BT_ERR("Dropping L2CAP data: receive buffer overflow"); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6816 | goto drop; |
Szymon Janc | 2c96e03 | 2014-02-18 20:48:34 +0100 | [diff] [blame] | 6817 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6818 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6819 | if (!chan->ops->recv(chan, skb)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6820 | goto done; |
| 6821 | break; |
| 6822 | |
| 6823 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6824 | case L2CAP_MODE_STREAMING: |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6825 | l2cap_data_rcv(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6826 | goto done; |
| 6827 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6828 | default: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6829 | 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] | 6830 | break; |
| 6831 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6832 | |
| 6833 | drop: |
| 6834 | kfree_skb(skb); |
| 6835 | |
| 6836 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6837 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6838 | } |
| 6839 | |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6840 | static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, |
| 6841 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6842 | { |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6843 | struct hci_conn *hcon = conn->hcon; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6844 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6845 | |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6846 | if (hcon->type != ACL_LINK) |
| 6847 | goto drop; |
| 6848 | |
Johan Hedberg | bf20fd4 | 2013-05-14 13:23:13 +0300 | [diff] [blame] | 6849 | chan = l2cap_global_chan_by_psm(0, psm, &hcon->src, &hcon->dst, |
| 6850 | ACL_LINK); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6851 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6852 | goto drop; |
| 6853 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 6854 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6855 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6856 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6857 | goto drop; |
| 6858 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 6859 | if (chan->imtu < skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6860 | goto drop; |
| 6861 | |
Marcel Holtmann | 2edf870 | 2013-10-13 12:55:29 -0700 | [diff] [blame] | 6862 | /* Store remote BD_ADDR and PSM for msg_name */ |
Marcel Holtmann | 06ae331 | 2013-10-18 03:43:00 -0700 | [diff] [blame] | 6863 | bacpy(&bt_cb(skb)->bdaddr, &hcon->dst); |
Marcel Holtmann | 2edf870 | 2013-10-13 12:55:29 -0700 | [diff] [blame] | 6864 | bt_cb(skb)->psm = psm; |
| 6865 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6866 | if (!chan->ops->recv(chan, skb)) |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6867 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6868 | |
| 6869 | drop: |
| 6870 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6871 | } |
| 6872 | |
Marcel Holtmann | 72f7835 | 2013-04-23 00:59:00 -0700 | [diff] [blame] | 6873 | static void l2cap_att_channel(struct l2cap_conn *conn, |
Andrei Emeltchenko | 6810fca | 2012-05-31 11:18:57 +0300 | [diff] [blame] | 6874 | struct sk_buff *skb) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6875 | { |
Marcel Holtmann | b99707d | 2013-10-03 02:54:11 -0700 | [diff] [blame] | 6876 | struct hci_conn *hcon = conn->hcon; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6877 | struct l2cap_chan *chan; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6878 | |
Marcel Holtmann | b99707d | 2013-10-03 02:54:11 -0700 | [diff] [blame] | 6879 | if (hcon->type != LE_LINK) |
| 6880 | goto drop; |
| 6881 | |
Johan Hedberg | af1c013 | 2013-04-29 19:35:42 +0300 | [diff] [blame] | 6882 | chan = l2cap_global_chan_by_scid(BT_CONNECTED, L2CAP_CID_ATT, |
Marcel Holtmann | 06ae331 | 2013-10-18 03:43:00 -0700 | [diff] [blame] | 6883 | &hcon->src, &hcon->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6884 | if (!chan) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6885 | goto drop; |
| 6886 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 6887 | BT_DBG("chan %p, len %d", chan, skb->len); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6888 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 6889 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6890 | goto drop; |
| 6891 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6892 | if (!chan->ops->recv(chan, skb)) |
Andrei Emeltchenko | 6810fca | 2012-05-31 11:18:57 +0300 | [diff] [blame] | 6893 | return; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6894 | |
| 6895 | drop: |
| 6896 | kfree_skb(skb); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6897 | } |
| 6898 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6899 | static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) |
| 6900 | { |
| 6901 | struct l2cap_hdr *lh = (void *) skb->data; |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6902 | struct hci_conn *hcon = conn->hcon; |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 6903 | u16 cid, len; |
| 6904 | __le16 psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6905 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6906 | if (hcon->state != BT_CONNECTED) { |
| 6907 | BT_DBG("queueing pending rx skb"); |
| 6908 | skb_queue_tail(&conn->pending_rx, skb); |
| 6909 | return; |
| 6910 | } |
| 6911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6912 | skb_pull(skb, L2CAP_HDR_SIZE); |
| 6913 | cid = __le16_to_cpu(lh->cid); |
| 6914 | len = __le16_to_cpu(lh->len); |
| 6915 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6916 | if (len != skb->len) { |
| 6917 | kfree_skb(skb); |
| 6918 | return; |
| 6919 | } |
| 6920 | |
Johan Hedberg | 9e1d7e1 | 2014-07-06 11:03:36 +0300 | [diff] [blame] | 6921 | /* Since we can't actively block incoming LE connections we must |
| 6922 | * at least ensure that we ignore incoming data from them. |
| 6923 | */ |
| 6924 | if (hcon->type == LE_LINK && |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 6925 | hci_bdaddr_list_lookup(&hcon->hdev->blacklist, &hcon->dst, |
| 6926 | bdaddr_type(hcon, hcon->dst_type))) { |
Johan Hedberg | e493150 | 2014-07-02 09:36:21 +0300 | [diff] [blame] | 6927 | kfree_skb(skb); |
| 6928 | return; |
| 6929 | } |
| 6930 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6931 | BT_DBG("len %d, cid 0x%4.4x", len, cid); |
| 6932 | |
| 6933 | switch (cid) { |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6934 | case L2CAP_CID_SIGNALING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6935 | l2cap_sig_channel(conn, skb); |
| 6936 | break; |
| 6937 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6938 | case L2CAP_CID_CONN_LESS: |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 6939 | psm = get_unaligned((__le16 *) skb->data); |
Andrei Emeltchenko | 0181a70 | 2012-05-29 10:04:05 +0300 | [diff] [blame] | 6940 | skb_pull(skb, L2CAP_PSMLEN_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6941 | l2cap_conless_channel(conn, psm, skb); |
| 6942 | break; |
| 6943 | |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame] | 6944 | case L2CAP_CID_ATT: |
Marcel Holtmann | 72f7835 | 2013-04-23 00:59:00 -0700 | [diff] [blame] | 6945 | l2cap_att_channel(conn, skb); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6946 | break; |
| 6947 | |
Marcel Holtmann | a287762 | 2013-10-02 23:46:54 -0700 | [diff] [blame] | 6948 | case L2CAP_CID_LE_SIGNALING: |
| 6949 | l2cap_le_sig_channel(conn, skb); |
| 6950 | break; |
| 6951 | |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 6952 | case L2CAP_CID_SMP: |
| 6953 | if (smp_sig_channel(conn, skb)) |
| 6954 | l2cap_conn_del(conn->hcon, EACCES); |
| 6955 | break; |
| 6956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6957 | default: |
| 6958 | l2cap_data_channel(conn, cid, skb); |
| 6959 | break; |
| 6960 | } |
| 6961 | } |
| 6962 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 6963 | static void process_pending_rx(struct work_struct *work) |
| 6964 | { |
| 6965 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
| 6966 | pending_rx_work); |
| 6967 | struct sk_buff *skb; |
| 6968 | |
| 6969 | BT_DBG(""); |
| 6970 | |
| 6971 | while ((skb = skb_dequeue(&conn->pending_rx))) |
| 6972 | l2cap_recv_frame(conn, skb); |
| 6973 | } |
| 6974 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 6975 | static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) |
| 6976 | { |
| 6977 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 6978 | struct hci_chan *hchan; |
| 6979 | |
| 6980 | if (conn) |
| 6981 | return conn; |
| 6982 | |
| 6983 | hchan = hci_chan_create(hcon); |
| 6984 | if (!hchan) |
| 6985 | return NULL; |
| 6986 | |
| 6987 | conn = kzalloc(sizeof(struct l2cap_conn), GFP_KERNEL); |
| 6988 | if (!conn) { |
| 6989 | hci_chan_del(hchan); |
| 6990 | return NULL; |
| 6991 | } |
| 6992 | |
| 6993 | kref_init(&conn->ref); |
| 6994 | hcon->l2cap_data = conn; |
| 6995 | conn->hcon = hcon; |
| 6996 | hci_conn_get(conn->hcon); |
| 6997 | conn->hchan = hchan; |
| 6998 | |
| 6999 | BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); |
| 7000 | |
| 7001 | switch (hcon->type) { |
| 7002 | case LE_LINK: |
| 7003 | if (hcon->hdev->le_mtu) { |
| 7004 | conn->mtu = hcon->hdev->le_mtu; |
| 7005 | break; |
| 7006 | } |
| 7007 | /* fall through */ |
| 7008 | default: |
| 7009 | conn->mtu = hcon->hdev->acl_mtu; |
| 7010 | break; |
| 7011 | } |
| 7012 | |
| 7013 | conn->feat_mask = 0; |
| 7014 | |
| 7015 | if (hcon->type == ACL_LINK) |
| 7016 | conn->hs_enabled = test_bit(HCI_HS_ENABLED, |
| 7017 | &hcon->hdev->dev_flags); |
| 7018 | |
Marcel Holtmann | 5a54e7c | 2014-07-13 20:50:15 +0200 | [diff] [blame] | 7019 | mutex_init(&conn->ident_lock); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7020 | mutex_init(&conn->chan_lock); |
| 7021 | |
| 7022 | INIT_LIST_HEAD(&conn->chan_l); |
| 7023 | INIT_LIST_HEAD(&conn->users); |
| 7024 | |
| 7025 | if (hcon->type == LE_LINK) |
| 7026 | INIT_DELAYED_WORK(&conn->security_timer, security_timeout); |
| 7027 | else |
| 7028 | INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); |
| 7029 | |
Johan Hedberg | 61a939c | 2014-01-17 20:45:11 +0200 | [diff] [blame] | 7030 | skb_queue_head_init(&conn->pending_rx); |
| 7031 | INIT_WORK(&conn->pending_rx_work, process_pending_rx); |
| 7032 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7033 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
| 7034 | |
| 7035 | return conn; |
| 7036 | } |
| 7037 | |
| 7038 | static bool is_valid_psm(u16 psm, u8 dst_type) { |
| 7039 | if (!psm) |
| 7040 | return false; |
| 7041 | |
| 7042 | if (bdaddr_type_is_le(dst_type)) |
| 7043 | return (psm <= 0x00ff); |
| 7044 | |
| 7045 | /* PSM must be odd and lsb of upper byte must be 0 */ |
| 7046 | return ((psm & 0x0101) == 0x0001); |
| 7047 | } |
| 7048 | |
| 7049 | int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, |
| 7050 | bdaddr_t *dst, u8 dst_type) |
| 7051 | { |
| 7052 | struct l2cap_conn *conn; |
| 7053 | struct hci_conn *hcon; |
| 7054 | struct hci_dev *hdev; |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7055 | int err; |
| 7056 | |
| 7057 | BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", &chan->src, dst, |
| 7058 | dst_type, __le16_to_cpu(psm)); |
| 7059 | |
| 7060 | hdev = hci_get_route(dst, &chan->src); |
| 7061 | if (!hdev) |
| 7062 | return -EHOSTUNREACH; |
| 7063 | |
| 7064 | hci_dev_lock(hdev); |
| 7065 | |
| 7066 | l2cap_chan_lock(chan); |
| 7067 | |
| 7068 | if (!is_valid_psm(__le16_to_cpu(psm), dst_type) && !cid && |
| 7069 | chan->chan_type != L2CAP_CHAN_RAW) { |
| 7070 | err = -EINVAL; |
| 7071 | goto done; |
| 7072 | } |
| 7073 | |
Johan Hedberg | 21626e6 | 2014-01-24 10:35:41 +0200 | [diff] [blame] | 7074 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !psm) { |
| 7075 | err = -EINVAL; |
| 7076 | goto done; |
| 7077 | } |
| 7078 | |
| 7079 | if (chan->chan_type == L2CAP_CHAN_FIXED && !cid) { |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7080 | err = -EINVAL; |
| 7081 | goto done; |
| 7082 | } |
| 7083 | |
| 7084 | switch (chan->mode) { |
| 7085 | case L2CAP_MODE_BASIC: |
| 7086 | break; |
| 7087 | case L2CAP_MODE_LE_FLOWCTL: |
| 7088 | l2cap_le_flowctl_init(chan); |
| 7089 | break; |
| 7090 | case L2CAP_MODE_ERTM: |
| 7091 | case L2CAP_MODE_STREAMING: |
| 7092 | if (!disable_ertm) |
| 7093 | break; |
| 7094 | /* fall through */ |
| 7095 | default: |
| 7096 | err = -ENOTSUPP; |
| 7097 | goto done; |
| 7098 | } |
| 7099 | |
| 7100 | switch (chan->state) { |
| 7101 | case BT_CONNECT: |
| 7102 | case BT_CONNECT2: |
| 7103 | case BT_CONFIG: |
| 7104 | /* Already connecting */ |
| 7105 | err = 0; |
| 7106 | goto done; |
| 7107 | |
| 7108 | case BT_CONNECTED: |
| 7109 | /* Already connected */ |
| 7110 | err = -EISCONN; |
| 7111 | goto done; |
| 7112 | |
| 7113 | case BT_OPEN: |
| 7114 | case BT_BOUND: |
| 7115 | /* Can connect */ |
| 7116 | break; |
| 7117 | |
| 7118 | default: |
| 7119 | err = -EBADFD; |
| 7120 | goto done; |
| 7121 | } |
| 7122 | |
| 7123 | /* Set destination address and psm */ |
| 7124 | bacpy(&chan->dst, dst); |
| 7125 | chan->dst_type = dst_type; |
| 7126 | |
| 7127 | chan->psm = psm; |
| 7128 | chan->dcid = cid; |
| 7129 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7130 | if (bdaddr_type_is_le(dst_type)) { |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 7131 | bool master; |
| 7132 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7133 | /* Convert from L2CAP channel address type to HCI address type |
| 7134 | */ |
| 7135 | if (dst_type == BDADDR_LE_PUBLIC) |
| 7136 | dst_type = ADDR_LE_DEV_PUBLIC; |
| 7137 | else |
| 7138 | dst_type = ADDR_LE_DEV_RANDOM; |
| 7139 | |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 7140 | master = !test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 7141 | |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 7142 | hcon = hci_connect_le(hdev, dst, dst_type, chan->sec_level, |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 7143 | HCI_LE_CONN_TIMEOUT, master); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7144 | } else { |
Johan Hedberg | d93375a | 2014-07-07 15:02:27 +0300 | [diff] [blame] | 7145 | u8 auth_type = l2cap_get_auth_type(chan); |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 7146 | hcon = hci_connect_acl(hdev, dst, chan->sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 7147 | } |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7148 | |
| 7149 | if (IS_ERR(hcon)) { |
| 7150 | err = PTR_ERR(hcon); |
| 7151 | goto done; |
| 7152 | } |
| 7153 | |
| 7154 | conn = l2cap_conn_add(hcon); |
| 7155 | if (!conn) { |
| 7156 | hci_conn_drop(hcon); |
| 7157 | err = -ENOMEM; |
| 7158 | goto done; |
| 7159 | } |
| 7160 | |
| 7161 | if (cid && __l2cap_get_chan_by_dcid(conn, cid)) { |
| 7162 | hci_conn_drop(hcon); |
| 7163 | err = -EBUSY; |
| 7164 | goto done; |
| 7165 | } |
| 7166 | |
| 7167 | /* Update source addr of the socket */ |
| 7168 | bacpy(&chan->src, &hcon->src); |
| 7169 | chan->src_type = bdaddr_type(hcon, hcon->src_type); |
| 7170 | |
| 7171 | l2cap_chan_unlock(chan); |
| 7172 | l2cap_chan_add(conn, chan); |
| 7173 | l2cap_chan_lock(chan); |
| 7174 | |
| 7175 | /* l2cap_chan_add takes its own ref so we can drop this one */ |
| 7176 | hci_conn_drop(hcon); |
| 7177 | |
| 7178 | l2cap_state_change(chan, BT_CONNECT); |
| 7179 | __set_chan_timer(chan, chan->ops->get_sndtimeo(chan)); |
| 7180 | |
Johan Hedberg | 61202e4 | 2014-01-28 15:16:48 -0800 | [diff] [blame] | 7181 | /* Release chan->sport so that it can be reused by other |
| 7182 | * sockets (as it's only used for listening sockets). |
| 7183 | */ |
| 7184 | write_lock(&chan_list_lock); |
| 7185 | chan->sport = 0; |
| 7186 | write_unlock(&chan_list_lock); |
| 7187 | |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7188 | if (hcon->state == BT_CONNECTED) { |
| 7189 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
| 7190 | __clear_chan_timer(chan); |
| 7191 | if (l2cap_chan_check_security(chan)) |
| 7192 | l2cap_state_change(chan, BT_CONNECTED); |
| 7193 | } else |
| 7194 | l2cap_do_start(chan); |
| 7195 | } |
| 7196 | |
| 7197 | err = 0; |
| 7198 | |
| 7199 | done: |
| 7200 | l2cap_chan_unlock(chan); |
| 7201 | hci_dev_unlock(hdev); |
| 7202 | hci_dev_put(hdev); |
| 7203 | return err; |
| 7204 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 7205 | EXPORT_SYMBOL_GPL(l2cap_chan_connect); |
Johan Hedberg | 162b49e | 2014-01-17 20:45:10 +0200 | [diff] [blame] | 7206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7207 | /* ---- L2CAP interface with lower layer (HCI) ---- */ |
| 7208 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7209 | int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7210 | { |
| 7211 | int exact = 0, lm1 = 0, lm2 = 0; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7212 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7213 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 7214 | BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7215 | |
| 7216 | /* Find listening sockets and check their link_mode */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7217 | read_lock(&chan_list_lock); |
| 7218 | list_for_each_entry(c, &chan_list, global_l) { |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 7219 | if (c->state != BT_LISTEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7220 | continue; |
| 7221 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 7222 | if (!bacmp(&c->src, &hdev->bdaddr)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7223 | lm1 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 7224 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7225 | lm1 |= HCI_LM_MASTER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7226 | exact++; |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 7227 | } else if (!bacmp(&c->src, BDADDR_ANY)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7228 | lm2 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 7229 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 7230 | lm2 |= HCI_LM_MASTER; |
| 7231 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7232 | } |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7233 | read_unlock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7234 | |
| 7235 | return exact ? lm1 : lm2; |
| 7236 | } |
| 7237 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 7238 | void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7239 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7240 | struct l2cap_conn *conn; |
| 7241 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 7242 | BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7244 | if (!status) { |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 7245 | conn = l2cap_conn_add(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7246 | if (conn) |
| 7247 | l2cap_conn_ready(conn); |
Andrei Emeltchenko | ba6fc31 | 2012-10-31 15:46:26 +0200 | [diff] [blame] | 7248 | } else { |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 7249 | l2cap_conn_del(hcon, bt_to_errno(status)); |
Andrei Emeltchenko | ba6fc31 | 2012-10-31 15:46:26 +0200 | [diff] [blame] | 7250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7251 | } |
| 7252 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7253 | int l2cap_disconn_ind(struct hci_conn *hcon) |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 7254 | { |
| 7255 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 7256 | |
| 7257 | BT_DBG("hcon %p", hcon); |
| 7258 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7259 | if (!conn) |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 7260 | return HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 7261 | return conn->disc_reason; |
| 7262 | } |
| 7263 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 7264 | void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7265 | { |
| 7266 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 7267 | |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 7268 | l2cap_conn_del(hcon, bt_to_errno(reason)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7269 | } |
| 7270 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7271 | static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7272 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 7273 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
Marcel Holtmann | 255c760 | 2009-02-04 21:07:19 +0100 | [diff] [blame] | 7274 | return; |
| 7275 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7276 | if (encrypt == 0x00) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7277 | if (chan->sec_level == BT_SECURITY_MEDIUM) { |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 7278 | __set_chan_timer(chan, L2CAP_ENC_TIMEOUT); |
Marcel Holtmann | 7d513e9 | 2014-01-15 22:37:40 -0800 | [diff] [blame] | 7279 | } else if (chan->sec_level == BT_SECURITY_HIGH || |
| 7280 | chan->sec_level == BT_SECURITY_FIPS) |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 7281 | l2cap_chan_close(chan, ECONNREFUSED); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7282 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7283 | if (chan->sec_level == BT_SECURITY_MEDIUM) |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 7284 | __clear_chan_timer(chan); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 7285 | } |
| 7286 | } |
| 7287 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7288 | int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7289 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 7290 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 7291 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7292 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7293 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7294 | return 0; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 7295 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 7296 | 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] | 7297 | |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 7298 | if (hcon->type == LE_LINK) { |
Hemant Gupta | 35d4adcc | 2012-04-18 14:46:26 +0530 | [diff] [blame] | 7299 | if (!status && encrypt) |
Johan Hedberg | 4bd6d38 | 2014-02-26 23:33:45 +0200 | [diff] [blame] | 7300 | smp_distribute_keys(conn); |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 7301 | cancel_delayed_work(&conn->security_timer); |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 7302 | } |
| 7303 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 7304 | mutex_lock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7305 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 7306 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7307 | l2cap_chan_lock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7308 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 7309 | BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, |
| 7310 | state_to_string(chan->state)); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 7311 | |
Johan Hedberg | 2338a7e | 2014-01-24 10:35:40 +0200 | [diff] [blame] | 7312 | if (chan->scid == L2CAP_CID_A2MP) { |
Andrei Emeltchenko | 78eb2f9 | 2012-07-19 17:03:47 +0300 | [diff] [blame] | 7313 | l2cap_chan_unlock(chan); |
| 7314 | continue; |
| 7315 | } |
| 7316 | |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame] | 7317 | if (chan->scid == L2CAP_CID_ATT) { |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 7318 | if (!status && encrypt) { |
| 7319 | chan->sec_level = hcon->sec_level; |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 7320 | l2cap_chan_ready(chan); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 7321 | } |
| 7322 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7323 | l2cap_chan_unlock(chan); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 7324 | continue; |
| 7325 | } |
| 7326 | |
Andrei Emeltchenko | 96eff46 | 2012-11-15 18:14:53 +0200 | [diff] [blame] | 7327 | if (!__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7328 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 7329 | continue; |
| 7330 | } |
| 7331 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 7332 | if (!status && (chan->state == BT_CONNECTED || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7333 | chan->state == BT_CONFIG)) { |
Marcel Holtmann | d97c899 | 2013-10-14 02:53:54 -0700 | [diff] [blame] | 7334 | chan->ops->resume(chan); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 7335 | l2cap_check_encryption(chan, encrypt); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7336 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 9719f8a | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 7337 | continue; |
| 7338 | } |
| 7339 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 7340 | if (chan->state == BT_CONNECT) { |
Johan Hedberg | 6d3c15d | 2013-12-02 22:13:24 +0200 | [diff] [blame] | 7341 | if (!status) |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 7342 | l2cap_start_connection(chan); |
Johan Hedberg | 6d3c15d | 2013-12-02 22:13:24 +0200 | [diff] [blame] | 7343 | else |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 7344 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 7345 | } else if (chan->state == BT_CONNECT2) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7346 | struct l2cap_conn_rsp rsp; |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7347 | __u16 res, stat; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7348 | |
| 7349 | if (!status) { |
Marcel Holtmann | bdc2578 | 2013-10-14 02:45:34 -0700 | [diff] [blame] | 7350 | if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) { |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7351 | res = L2CAP_CR_PEND; |
| 7352 | stat = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 7353 | chan->ops->defer(chan); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7354 | } else { |
Gustavo Padovan | acdcabf | 2013-10-21 14:21:39 -0200 | [diff] [blame] | 7355 | l2cap_state_change(chan, BT_CONFIG); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7356 | res = L2CAP_CR_SUCCESS; |
| 7357 | stat = L2CAP_CS_NO_INFO; |
| 7358 | } |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7359 | } else { |
Gustavo Padovan | acdcabf | 2013-10-21 14:21:39 -0200 | [diff] [blame] | 7360 | l2cap_state_change(chan, BT_DISCONN); |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 7361 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7362 | res = L2CAP_CR_SEC_BLOCK; |
| 7363 | stat = L2CAP_CS_NO_INFO; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7364 | } |
| 7365 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 7366 | rsp.scid = cpu_to_le16(chan->dcid); |
| 7367 | rsp.dcid = cpu_to_le16(chan->scid); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 7368 | rsp.result = cpu_to_le16(res); |
| 7369 | rsp.status = cpu_to_le16(stat); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 7370 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7371 | sizeof(rsp), &rsp); |
Mat Martineau | 2d36935 | 2012-05-23 14:59:30 -0700 | [diff] [blame] | 7372 | |
| 7373 | if (!test_bit(CONF_REQ_SENT, &chan->conf_state) && |
| 7374 | res == L2CAP_CR_SUCCESS) { |
| 7375 | char buf[128]; |
| 7376 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 7377 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 7378 | L2CAP_CONF_REQ, |
| 7379 | l2cap_build_conf_req(chan, buf), |
| 7380 | buf); |
| 7381 | chan->num_conf_req++; |
| 7382 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7383 | } |
| 7384 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 7385 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7386 | } |
| 7387 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 7388 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 7389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7390 | return 0; |
| 7391 | } |
| 7392 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 7393 | 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] | 7394 | { |
| 7395 | struct l2cap_conn *conn = hcon->l2cap_data; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7396 | struct l2cap_hdr *hdr; |
| 7397 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7398 | |
Andrei Emeltchenko | 1d13a25 | 2012-10-15 11:58:41 +0300 | [diff] [blame] | 7399 | /* For AMP controller do not create l2cap conn */ |
| 7400 | if (!conn && hcon->hdev->dev_type != HCI_BREDR) |
| 7401 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7402 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 7403 | if (!conn) |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 7404 | conn = l2cap_conn_add(hcon); |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 7405 | |
| 7406 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7407 | goto drop; |
| 7408 | |
| 7409 | BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); |
| 7410 | |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7411 | switch (flags) { |
| 7412 | case ACL_START: |
| 7413 | case ACL_START_NO_FLUSH: |
| 7414 | case ACL_COMPLETE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7415 | if (conn->rx_len) { |
| 7416 | BT_ERR("Unexpected start frame (len %d)", skb->len); |
| 7417 | kfree_skb(conn->rx_skb); |
| 7418 | conn->rx_skb = NULL; |
| 7419 | conn->rx_len = 0; |
| 7420 | l2cap_conn_unreliable(conn, ECOMM); |
| 7421 | } |
| 7422 | |
Andrei Emeltchenko | aae7fe2 | 2010-09-15 14:28:43 +0300 | [diff] [blame] | 7423 | /* Start fragment always begin with Basic L2CAP header */ |
| 7424 | if (skb->len < L2CAP_HDR_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7425 | BT_ERR("Frame is too short (len %d)", skb->len); |
| 7426 | l2cap_conn_unreliable(conn, ECOMM); |
| 7427 | goto drop; |
| 7428 | } |
| 7429 | |
| 7430 | hdr = (struct l2cap_hdr *) skb->data; |
| 7431 | len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; |
| 7432 | |
| 7433 | if (len == skb->len) { |
| 7434 | /* Complete frame received */ |
| 7435 | l2cap_recv_frame(conn, skb); |
| 7436 | return 0; |
| 7437 | } |
| 7438 | |
| 7439 | BT_DBG("Start: total len %d, frag len %d", len, skb->len); |
| 7440 | |
| 7441 | if (skb->len > len) { |
| 7442 | BT_ERR("Frame is too long (len %d, expected len %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7443 | skb->len, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7444 | l2cap_conn_unreliable(conn, ECOMM); |
| 7445 | goto drop; |
| 7446 | } |
| 7447 | |
| 7448 | /* Allocate skb for the complete frame (with header) */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 7449 | conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 7450 | if (!conn->rx_skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7451 | goto drop; |
| 7452 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 7453 | 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] | 7454 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7455 | conn->rx_len = len - skb->len; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7456 | break; |
| 7457 | |
| 7458 | case ACL_CONT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7459 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
| 7460 | |
| 7461 | if (!conn->rx_len) { |
| 7462 | BT_ERR("Unexpected continuation frame (len %d)", skb->len); |
| 7463 | l2cap_conn_unreliable(conn, ECOMM); |
| 7464 | goto drop; |
| 7465 | } |
| 7466 | |
| 7467 | if (skb->len > conn->rx_len) { |
| 7468 | BT_ERR("Fragment is too long (len %d, expected %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 7469 | skb->len, conn->rx_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7470 | kfree_skb(conn->rx_skb); |
| 7471 | conn->rx_skb = NULL; |
| 7472 | conn->rx_len = 0; |
| 7473 | l2cap_conn_unreliable(conn, ECOMM); |
| 7474 | goto drop; |
| 7475 | } |
| 7476 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 7477 | 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] | 7478 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7479 | conn->rx_len -= skb->len; |
| 7480 | |
| 7481 | if (!conn->rx_len) { |
Johan Hedberg | c4e5baf | 2013-10-10 13:33:37 +0200 | [diff] [blame] | 7482 | /* Complete frame received. l2cap_recv_frame |
| 7483 | * takes ownership of the skb so set the global |
| 7484 | * rx_skb pointer to NULL first. |
| 7485 | */ |
| 7486 | struct sk_buff *rx_skb = conn->rx_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7487 | conn->rx_skb = NULL; |
Johan Hedberg | c4e5baf | 2013-10-10 13:33:37 +0200 | [diff] [blame] | 7488 | l2cap_recv_frame(conn, rx_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7489 | } |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 7490 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7491 | } |
| 7492 | |
| 7493 | drop: |
| 7494 | kfree_skb(skb); |
| 7495 | return 0; |
| 7496 | } |
| 7497 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7498 | static int l2cap_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7499 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7500 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7501 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 7502 | read_lock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7503 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 7504 | list_for_each_entry(c, &chan_list, global_l) { |
Andrei Emeltchenko | fcb7333 | 2012-09-25 12:49:44 +0300 | [diff] [blame] | 7505 | 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] | 7506 | &c->src, &c->dst, |
Andrei Emeltchenko | fcb7333 | 2012-09-25 12:49:44 +0300 | [diff] [blame] | 7507 | c->state, __le16_to_cpu(c->psm), |
| 7508 | c->scid, c->dcid, c->imtu, c->omtu, |
| 7509 | c->sec_level, c->mode); |
Andrei Emeltchenko | 61e1b4b | 2012-01-19 11:19:50 +0200 | [diff] [blame] | 7510 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7511 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 7512 | read_unlock(&chan_list_lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 7513 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7514 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7515 | } |
| 7516 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7517 | static int l2cap_debugfs_open(struct inode *inode, struct file *file) |
| 7518 | { |
| 7519 | return single_open(file, l2cap_debugfs_show, inode->i_private); |
| 7520 | } |
| 7521 | |
| 7522 | static const struct file_operations l2cap_debugfs_fops = { |
| 7523 | .open = l2cap_debugfs_open, |
| 7524 | .read = seq_read, |
| 7525 | .llseek = seq_lseek, |
| 7526 | .release = single_release, |
| 7527 | }; |
| 7528 | |
| 7529 | static struct dentry *l2cap_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7530 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 7531 | int __init l2cap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7532 | { |
| 7533 | int err; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 7534 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 7535 | err = l2cap_init_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7536 | if (err < 0) |
| 7537 | return err; |
| 7538 | |
Marcel Holtmann | 1120e4b | 2013-10-17 17:24:16 -0700 | [diff] [blame] | 7539 | if (IS_ERR_OR_NULL(bt_debugfs)) |
| 7540 | return 0; |
| 7541 | |
| 7542 | l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs, |
| 7543 | NULL, &l2cap_debugfs_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7544 | |
Samuel Ortiz | 40b9397 | 2014-05-14 17:53:35 +0200 | [diff] [blame] | 7545 | debugfs_create_u16("l2cap_le_max_credits", 0644, bt_debugfs, |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 7546 | &le_max_credits); |
Samuel Ortiz | 40b9397 | 2014-05-14 17:53:35 +0200 | [diff] [blame] | 7547 | debugfs_create_u16("l2cap_le_default_mps", 0644, bt_debugfs, |
Johan Hedberg | f15b8ec | 2013-12-03 15:08:25 +0200 | [diff] [blame] | 7548 | &le_default_mps); |
| 7549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7550 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7551 | } |
| 7552 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 7553 | void l2cap_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7554 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 7555 | debugfs_remove(l2cap_debugfs); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 7556 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7557 | } |
| 7558 | |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 7559 | module_param(disable_ertm, bool, 0644); |
| 7560 | MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); |