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 | |
Mat Martineau | d1de6d4 | 2012-05-17 20:53:55 -0700 | [diff] [blame] | 44 | bool disable_ertm; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 45 | |
Marcel Holtmann | 547d103 | 2013-10-12 08:18:19 -0700 | [diff] [blame] | 46 | static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN | L2CAP_FEAT_UCD; |
Marcel Holtmann | d40bffb | 2013-10-12 08:18:18 -0700 | [diff] [blame] | 47 | static u8 l2cap_fixed_chan[8] = { L2CAP_FC_L2CAP | L2CAP_FC_CONNLESS, }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 49 | static LIST_HEAD(chan_list); |
| 50 | static DEFINE_RWLOCK(chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 53 | u8 code, u8 ident, u16 dlen, void *data); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 54 | 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] | 55 | void *data); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 56 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 57 | static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 59 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 60 | struct sk_buff_head *skbs, u8 event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 61 | |
Marcel Holtmann | 4f1654e | 2013-10-13 08:50:41 -0700 | [diff] [blame] | 62 | static inline __u8 bdaddr_type(struct hci_conn *hcon, __u8 type) |
| 63 | { |
| 64 | if (hcon->type == LE_LINK) { |
| 65 | if (type == ADDR_LE_DEV_PUBLIC) |
| 66 | return BDADDR_LE_PUBLIC; |
| 67 | else |
| 68 | return BDADDR_LE_RANDOM; |
| 69 | } |
| 70 | |
| 71 | return BDADDR_BREDR; |
| 72 | } |
| 73 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 74 | /* ---- L2CAP channels ---- */ |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 75 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 76 | static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, |
| 77 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 78 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 79 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 80 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 81 | list_for_each_entry(c, &conn->chan_l, list) { |
| 82 | if (c->dcid == cid) |
| 83 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 84 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 85 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 88 | static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, |
| 89 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 90 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 91 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 92 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 93 | list_for_each_entry(c, &conn->chan_l, list) { |
| 94 | if (c->scid == cid) |
| 95 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 96 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 97 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* Find channel with given SCID. |
Mat Martineau | ef191ad | 2012-05-02 09:42:00 -0700 | [diff] [blame] | 101 | * Returns locked channel. */ |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 102 | static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, |
| 103 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 104 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 105 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 106 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 107 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 108 | c = __l2cap_get_chan_by_scid(conn, cid); |
Mat Martineau | ef191ad | 2012-05-02 09:42:00 -0700 | [diff] [blame] | 109 | if (c) |
| 110 | l2cap_chan_lock(c); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 111 | mutex_unlock(&conn->chan_lock); |
| 112 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 113 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Mat Martineau | b1a130b | 2012-10-23 15:24:09 -0700 | [diff] [blame] | 116 | /* Find channel with given DCID. |
| 117 | * Returns locked channel. |
| 118 | */ |
| 119 | static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn, |
| 120 | u16 cid) |
| 121 | { |
| 122 | struct l2cap_chan *c; |
| 123 | |
| 124 | mutex_lock(&conn->chan_lock); |
| 125 | c = __l2cap_get_chan_by_dcid(conn, cid); |
| 126 | if (c) |
| 127 | l2cap_chan_lock(c); |
| 128 | mutex_unlock(&conn->chan_lock); |
| 129 | |
| 130 | return c; |
| 131 | } |
| 132 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 133 | static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, |
| 134 | u8 ident) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 135 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 136 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 137 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 138 | list_for_each_entry(c, &conn->chan_l, list) { |
| 139 | if (c->ident == ident) |
| 140 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 141 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 142 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 145 | static struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, |
| 146 | u8 ident) |
| 147 | { |
| 148 | struct l2cap_chan *c; |
| 149 | |
| 150 | mutex_lock(&conn->chan_lock); |
| 151 | c = __l2cap_get_chan_by_ident(conn, ident); |
| 152 | if (c) |
| 153 | l2cap_chan_lock(c); |
| 154 | mutex_unlock(&conn->chan_lock); |
| 155 | |
| 156 | return c; |
| 157 | } |
| 158 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 159 | 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] | 160 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 161 | struct l2cap_chan *c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 162 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 163 | list_for_each_entry(c, &chan_list, global_l) { |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 164 | if (c->sport == psm && !bacmp(&c->src, src)) |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 165 | return c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 166 | } |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 167 | return NULL; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) |
| 171 | { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 172 | int err; |
| 173 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 174 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 175 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 176 | if (psm && __l2cap_global_chan_by_addr(psm, src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 177 | err = -EADDRINUSE; |
| 178 | goto done; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 179 | } |
| 180 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 181 | if (psm) { |
| 182 | chan->psm = psm; |
| 183 | chan->sport = psm; |
| 184 | err = 0; |
| 185 | } else { |
| 186 | u16 p; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 187 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 188 | err = -EINVAL; |
| 189 | for (p = 0x1001; p < 0x1100; p += 2) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 190 | if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 191 | chan->psm = cpu_to_le16(p); |
| 192 | chan->sport = cpu_to_le16(p); |
| 193 | err = 0; |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | done: |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 199 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 200 | return err; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) |
| 204 | { |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 205 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 206 | |
| 207 | chan->scid = scid; |
| 208 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 209 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 214 | static u16 l2cap_alloc_cid(struct l2cap_conn *conn) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 215 | { |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 216 | u16 cid = L2CAP_CID_DYN_START; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 217 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 218 | for (; cid < L2CAP_CID_DYN_END; cid++) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 219 | if (!__l2cap_get_chan_by_scid(conn, cid)) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 220 | return cid; |
| 221 | } |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 226 | static void __l2cap_state_change(struct l2cap_chan *chan, int state) |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 227 | { |
Andrei Emeltchenko | 42d2d87 | 2012-02-17 11:40:57 +0200 | [diff] [blame] | 228 | BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 229 | state_to_string(state)); |
Gustavo F. Padovan | badaaa0 | 2011-11-23 20:11:46 -0200 | [diff] [blame] | 230 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 231 | chan->state = state; |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 232 | chan->ops->state_change(chan, state); |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 233 | } |
| 234 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 235 | static void l2cap_state_change(struct l2cap_chan *chan, int state) |
| 236 | { |
| 237 | struct sock *sk = chan->sk; |
| 238 | |
| 239 | lock_sock(sk); |
| 240 | __l2cap_state_change(chan, state); |
| 241 | release_sock(sk); |
| 242 | } |
| 243 | |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 244 | static inline void __l2cap_chan_set_err(struct l2cap_chan *chan, int err) |
| 245 | { |
| 246 | struct sock *sk = chan->sk; |
| 247 | |
| 248 | sk->sk_err = err; |
| 249 | } |
| 250 | |
| 251 | static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err) |
| 252 | { |
| 253 | struct sock *sk = chan->sk; |
| 254 | |
| 255 | lock_sock(sk); |
| 256 | __l2cap_chan_set_err(chan, err); |
| 257 | release_sock(sk); |
| 258 | } |
| 259 | |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 260 | static void __set_retrans_timer(struct l2cap_chan *chan) |
| 261 | { |
| 262 | if (!delayed_work_pending(&chan->monitor_timer) && |
| 263 | chan->retrans_timeout) { |
| 264 | l2cap_set_timer(chan, &chan->retrans_timer, |
| 265 | msecs_to_jiffies(chan->retrans_timeout)); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | static void __set_monitor_timer(struct l2cap_chan *chan) |
| 270 | { |
| 271 | __clear_retrans_timer(chan); |
| 272 | if (chan->monitor_timeout) { |
| 273 | l2cap_set_timer(chan, &chan->monitor_timer, |
| 274 | msecs_to_jiffies(chan->monitor_timeout)); |
| 275 | } |
| 276 | } |
| 277 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 278 | static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head, |
| 279 | u16 seq) |
| 280 | { |
| 281 | struct sk_buff *skb; |
| 282 | |
| 283 | skb_queue_walk(head, skb) { |
| 284 | if (bt_cb(skb)->control.txseq == seq) |
| 285 | return skb; |
| 286 | } |
| 287 | |
| 288 | return NULL; |
| 289 | } |
| 290 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 291 | /* ---- L2CAP sequence number lists ---- */ |
| 292 | |
| 293 | /* For ERTM, ordered lists of sequence numbers must be tracked for |
| 294 | * SREJ requests that are received and for frames that are to be |
| 295 | * retransmitted. These seq_list functions implement a singly-linked |
| 296 | * list in an array, where membership in the list can also be checked |
| 297 | * in constant time. Items can also be added to the tail of the list |
| 298 | * and removed from the head in constant time, without further memory |
| 299 | * allocs or frees. |
| 300 | */ |
| 301 | |
| 302 | static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size) |
| 303 | { |
| 304 | size_t alloc_size, i; |
| 305 | |
| 306 | /* Allocated size is a power of 2 to map sequence numbers |
| 307 | * (which may be up to 14 bits) in to a smaller array that is |
| 308 | * sized for the negotiated ERTM transmit windows. |
| 309 | */ |
| 310 | alloc_size = roundup_pow_of_two(size); |
| 311 | |
| 312 | seq_list->list = kmalloc(sizeof(u16) * alloc_size, GFP_KERNEL); |
| 313 | if (!seq_list->list) |
| 314 | return -ENOMEM; |
| 315 | |
| 316 | seq_list->mask = alloc_size - 1; |
| 317 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 318 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
| 319 | for (i = 0; i < alloc_size; i++) |
| 320 | seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list) |
| 326 | { |
| 327 | kfree(seq_list->list); |
| 328 | } |
| 329 | |
| 330 | static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list, |
| 331 | u16 seq) |
| 332 | { |
| 333 | /* Constant-time check for list membership */ |
| 334 | return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR; |
| 335 | } |
| 336 | |
| 337 | static u16 l2cap_seq_list_remove(struct l2cap_seq_list *seq_list, u16 seq) |
| 338 | { |
| 339 | u16 mask = seq_list->mask; |
| 340 | |
| 341 | if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) { |
| 342 | /* In case someone tries to pop the head of an empty list */ |
| 343 | return L2CAP_SEQ_LIST_CLEAR; |
| 344 | } else if (seq_list->head == seq) { |
| 345 | /* Head can be removed in constant time */ |
| 346 | seq_list->head = seq_list->list[seq & mask]; |
| 347 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR; |
| 348 | |
| 349 | if (seq_list->head == L2CAP_SEQ_LIST_TAIL) { |
| 350 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 351 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
| 352 | } |
| 353 | } else { |
| 354 | /* Walk the list to find the sequence number */ |
| 355 | u16 prev = seq_list->head; |
| 356 | while (seq_list->list[prev & mask] != seq) { |
| 357 | prev = seq_list->list[prev & mask]; |
| 358 | if (prev == L2CAP_SEQ_LIST_TAIL) |
| 359 | return L2CAP_SEQ_LIST_CLEAR; |
| 360 | } |
| 361 | |
| 362 | /* Unlink the number from the list and clear it */ |
| 363 | seq_list->list[prev & mask] = seq_list->list[seq & mask]; |
| 364 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR; |
| 365 | if (seq_list->tail == seq) |
| 366 | seq_list->tail = prev; |
| 367 | } |
| 368 | return seq; |
| 369 | } |
| 370 | |
| 371 | static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list) |
| 372 | { |
| 373 | /* Remove the head in constant time */ |
| 374 | return l2cap_seq_list_remove(seq_list, seq_list->head); |
| 375 | } |
| 376 | |
| 377 | static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list) |
| 378 | { |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 379 | u16 i; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 380 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 381 | if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) |
| 382 | return; |
| 383 | |
| 384 | for (i = 0; i <= seq_list->mask; i++) |
| 385 | seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; |
| 386 | |
| 387 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 388 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq) |
| 392 | { |
| 393 | u16 mask = seq_list->mask; |
| 394 | |
| 395 | /* All appends happen in constant time */ |
| 396 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 397 | if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR) |
| 398 | return; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 399 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 400 | if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR) |
| 401 | seq_list->head = seq; |
| 402 | else |
| 403 | seq_list->list[seq_list->tail & mask] = seq; |
| 404 | |
| 405 | seq_list->tail = seq; |
| 406 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 409 | static void l2cap_chan_timeout(struct work_struct *work) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 410 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 411 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 412 | chan_timer.work); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 413 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 414 | int reason; |
| 415 | |
Andrei Emeltchenko | e05dcc3 | 2012-02-17 11:40:56 +0200 | [diff] [blame] | 416 | 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] | 417 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 418 | mutex_lock(&conn->chan_lock); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 419 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 420 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 421 | if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 422 | reason = ECONNREFUSED; |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 423 | else if (chan->state == BT_CONNECT && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 424 | chan->sec_level != BT_SECURITY_SDP) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 425 | reason = ECONNREFUSED; |
| 426 | else |
| 427 | reason = ETIMEDOUT; |
| 428 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 429 | l2cap_chan_close(chan, reason); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 430 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 431 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 432 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 433 | chan->ops->close(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 434 | mutex_unlock(&conn->chan_lock); |
| 435 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 436 | l2cap_chan_put(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 437 | } |
| 438 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 439 | struct l2cap_chan *l2cap_chan_create(void) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 440 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 441 | struct l2cap_chan *chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 442 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 443 | chan = kzalloc(sizeof(*chan), GFP_ATOMIC); |
| 444 | if (!chan) |
| 445 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 446 | |
Andrei Emeltchenko | c03b355 | 2012-02-21 12:54:56 +0200 | [diff] [blame] | 447 | mutex_init(&chan->lock); |
| 448 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 449 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 450 | list_add(&chan->global_l, &chan_list); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 451 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 452 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 453 | INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 454 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 455 | chan->state = BT_OPEN; |
| 456 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 457 | kref_init(&chan->kref); |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 458 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 459 | /* This flag is cleared in l2cap_chan_ready() */ |
| 460 | set_bit(CONF_NOT_COMPLETE, &chan->conf_state); |
| 461 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 462 | BT_DBG("chan %p", chan); |
Szymon Janc | abc545b | 2011-11-03 16:05:44 +0100 | [diff] [blame] | 463 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 464 | return chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 465 | } |
| 466 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 467 | static void l2cap_chan_destroy(struct kref *kref) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 468 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 469 | struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref); |
| 470 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 471 | BT_DBG("chan %p", chan); |
| 472 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 473 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 474 | list_del(&chan->global_l); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 475 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 476 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 477 | kfree(chan); |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 478 | } |
| 479 | |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 480 | void l2cap_chan_hold(struct l2cap_chan *c) |
| 481 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 482 | 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] | 483 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 484 | kref_get(&c->kref); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | void l2cap_chan_put(struct l2cap_chan *c) |
| 488 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 489 | 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] | 490 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 491 | kref_put(&c->kref, l2cap_chan_destroy); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 492 | } |
| 493 | |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 494 | void l2cap_chan_set_defaults(struct l2cap_chan *chan) |
| 495 | { |
| 496 | chan->fcs = L2CAP_FCS_CRC16; |
| 497 | chan->max_tx = L2CAP_DEFAULT_MAX_TX; |
| 498 | chan->tx_win = L2CAP_DEFAULT_TX_WINDOW; |
| 499 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 500 | chan->ack_win = L2CAP_DEFAULT_TX_WINDOW; |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 501 | chan->sec_level = BT_SECURITY_LOW; |
| 502 | |
| 503 | set_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 504 | } |
| 505 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 506 | void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 507 | { |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 508 | 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] | 509 | __le16_to_cpu(chan->psm), chan->dcid); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 510 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 511 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 512 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 513 | chan->conn = conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 514 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 515 | switch (chan->chan_type) { |
| 516 | case L2CAP_CHAN_CONN_ORIENTED: |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 517 | if (conn->hcon->type == LE_LINK) { |
| 518 | /* LE connection */ |
Andre Guedes | 6fcb06a | 2012-05-31 17:01:33 -0300 | [diff] [blame] | 519 | chan->omtu = L2CAP_DEFAULT_MTU; |
Johan Hedberg | 9f22398c | 2013-04-29 19:35:38 +0300 | [diff] [blame] | 520 | if (chan->dcid == L2CAP_CID_ATT) |
| 521 | chan->scid = L2CAP_CID_ATT; |
| 522 | else |
| 523 | chan->scid = l2cap_alloc_cid(conn); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 524 | } else { |
| 525 | /* Alloc CID for connection-oriented socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 526 | chan->scid = l2cap_alloc_cid(conn); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 527 | chan->omtu = L2CAP_DEFAULT_MTU; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 528 | } |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 529 | break; |
| 530 | |
| 531 | case L2CAP_CHAN_CONN_LESS: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 532 | /* Connectionless socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 533 | chan->scid = L2CAP_CID_CONN_LESS; |
| 534 | chan->dcid = L2CAP_CID_CONN_LESS; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 535 | chan->omtu = L2CAP_DEFAULT_MTU; |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 536 | break; |
| 537 | |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 538 | case L2CAP_CHAN_CONN_FIX_A2MP: |
| 539 | chan->scid = L2CAP_CID_A2MP; |
| 540 | chan->dcid = L2CAP_CID_A2MP; |
| 541 | chan->omtu = L2CAP_A2MP_DEFAULT_MTU; |
| 542 | chan->imtu = L2CAP_A2MP_DEFAULT_MTU; |
| 543 | break; |
| 544 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 545 | default: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 546 | /* Raw socket can send/recv signalling messages only */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 547 | chan->scid = L2CAP_CID_SIGNALING; |
| 548 | chan->dcid = L2CAP_CID_SIGNALING; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 549 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 550 | } |
| 551 | |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 552 | chan->local_id = L2CAP_BESTEFFORT_ID; |
| 553 | chan->local_stype = L2CAP_SERV_BESTEFFORT; |
| 554 | chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; |
| 555 | chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; |
| 556 | chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 557 | chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO; |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 558 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 559 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 560 | |
Johan Hedberg | 5ee9891 | 2013-04-29 19:35:43 +0300 | [diff] [blame] | 561 | hci_conn_hold(conn->hcon); |
| 562 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 563 | list_add(&chan->list, &conn->chan_l); |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 564 | } |
| 565 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 566 | void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 567 | { |
| 568 | mutex_lock(&conn->chan_lock); |
| 569 | __l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 570 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 571 | } |
| 572 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 573 | void l2cap_chan_del(struct l2cap_chan *chan, int err) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 574 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 575 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 576 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 577 | __clear_chan_timer(chan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 578 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 579 | BT_DBG("chan %p, conn %p, err %d", chan, conn, err); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 580 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 581 | if (conn) { |
Andrei Emeltchenko | 56f6098 | 2012-10-15 11:58:44 +0300 | [diff] [blame] | 582 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 583 | /* Delete from channel list */ |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 584 | list_del(&chan->list); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 585 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 586 | l2cap_chan_put(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 587 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 588 | chan->conn = NULL; |
Andrei Emeltchenko | 3cabbfd | 2012-05-31 11:01:37 +0300 | [diff] [blame] | 589 | |
| 590 | if (chan->chan_type != L2CAP_CHAN_CONN_FIX_A2MP) |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 591 | hci_conn_drop(conn->hcon); |
Andrei Emeltchenko | 56f6098 | 2012-10-15 11:58:44 +0300 | [diff] [blame] | 592 | |
| 593 | if (mgr && mgr->bredr_chan == chan) |
| 594 | mgr->bredr_chan = NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Andrei Emeltchenko | 419e08c | 2012-10-31 15:46:34 +0200 | [diff] [blame] | 597 | if (chan->hs_hchan) { |
| 598 | struct hci_chan *hs_hchan = chan->hs_hchan; |
| 599 | |
| 600 | BT_DBG("chan %p disconnect hs_hchan %p", chan, hs_hchan); |
| 601 | amp_disconnect_logical_link(hs_hchan); |
| 602 | } |
| 603 | |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 604 | chan->ops->teardown(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 605 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 606 | if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 607 | return; |
Gustavo F. Padovan | 2ead70b | 2011-04-01 15:13:36 -0300 | [diff] [blame] | 608 | |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 609 | switch(chan->mode) { |
| 610 | case L2CAP_MODE_BASIC: |
| 611 | break; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 612 | |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 613 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 614 | __clear_retrans_timer(chan); |
| 615 | __clear_monitor_timer(chan); |
| 616 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 617 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 618 | skb_queue_purge(&chan->srej_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 619 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 620 | l2cap_seq_list_free(&chan->srej_list); |
| 621 | l2cap_seq_list_free(&chan->retrans_list); |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 622 | |
| 623 | /* fall through */ |
| 624 | |
| 625 | case L2CAP_MODE_STREAMING: |
| 626 | skb_queue_purge(&chan->tx_q); |
| 627 | break; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 628 | } |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 629 | |
| 630 | return; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 631 | } |
| 632 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 633 | void l2cap_chan_close(struct l2cap_chan *chan, int reason) |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 634 | { |
| 635 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 636 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 637 | 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] | 638 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 639 | switch (chan->state) { |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 640 | case BT_LISTEN: |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 641 | chan->ops->teardown(chan, 0); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 642 | break; |
| 643 | |
| 644 | case BT_CONNECTED: |
| 645 | case BT_CONFIG: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 646 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 647 | conn->hcon->type == ACL_LINK) { |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 648 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 649 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 650 | l2cap_send_disconn_req(chan, reason); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 651 | } else |
| 652 | l2cap_chan_del(chan, reason); |
| 653 | break; |
| 654 | |
| 655 | case BT_CONNECT2: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 656 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 657 | conn->hcon->type == ACL_LINK) { |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 658 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 659 | struct l2cap_conn_rsp rsp; |
| 660 | __u16 result; |
| 661 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 662 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 663 | result = L2CAP_CR_SEC_BLOCK; |
| 664 | else |
| 665 | result = L2CAP_CR_BAD_PSM; |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 666 | l2cap_state_change(chan, BT_DISCONN); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 667 | |
| 668 | rsp.scid = cpu_to_le16(chan->dcid); |
| 669 | rsp.dcid = cpu_to_le16(chan->scid); |
| 670 | rsp.result = cpu_to_le16(result); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 671 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 672 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 673 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | l2cap_chan_del(chan, reason); |
| 677 | break; |
| 678 | |
| 679 | case BT_CONNECT: |
| 680 | case BT_DISCONN: |
| 681 | l2cap_chan_del(chan, reason); |
| 682 | break; |
| 683 | |
| 684 | default: |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 685 | chan->ops->teardown(chan, 0); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 686 | break; |
| 687 | } |
| 688 | } |
| 689 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 690 | static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 691 | { |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 692 | switch (chan->chan_type) { |
| 693 | case L2CAP_CHAN_RAW: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 694 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 695 | case BT_SECURITY_HIGH: |
| 696 | return HCI_AT_DEDICATED_BONDING_MITM; |
| 697 | case BT_SECURITY_MEDIUM: |
| 698 | return HCI_AT_DEDICATED_BONDING; |
| 699 | default: |
| 700 | return HCI_AT_NO_BONDING; |
| 701 | } |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 702 | break; |
Marcel Holtmann | 3124b84 | 2013-10-12 07:19:32 -0700 | [diff] [blame] | 703 | case L2CAP_CHAN_CONN_LESS: |
| 704 | if (chan->psm == __constant_cpu_to_le16(L2CAP_PSM_3DSP)) { |
| 705 | if (chan->sec_level == BT_SECURITY_LOW) |
| 706 | chan->sec_level = BT_SECURITY_SDP; |
| 707 | } |
| 708 | if (chan->sec_level == BT_SECURITY_HIGH) |
| 709 | return HCI_AT_NO_BONDING_MITM; |
| 710 | else |
| 711 | return HCI_AT_NO_BONDING; |
| 712 | break; |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 713 | case L2CAP_CHAN_CONN_ORIENTED: |
| 714 | if (chan->psm == __constant_cpu_to_le16(L2CAP_PSM_SDP)) { |
| 715 | if (chan->sec_level == BT_SECURITY_LOW) |
| 716 | chan->sec_level = BT_SECURITY_SDP; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 717 | |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 718 | if (chan->sec_level == BT_SECURITY_HIGH) |
| 719 | return HCI_AT_NO_BONDING_MITM; |
| 720 | else |
| 721 | return HCI_AT_NO_BONDING; |
| 722 | } |
| 723 | /* fall through */ |
| 724 | default: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 725 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 726 | case BT_SECURITY_HIGH: |
| 727 | return HCI_AT_GENERAL_BONDING_MITM; |
| 728 | case BT_SECURITY_MEDIUM: |
| 729 | return HCI_AT_GENERAL_BONDING; |
| 730 | default: |
| 731 | return HCI_AT_NO_BONDING; |
| 732 | } |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 733 | break; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 737 | /* Service level security */ |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 738 | int l2cap_chan_check_security(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 739 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 740 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 741 | __u8 auth_type; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 742 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 743 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 744 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 745 | return hci_conn_security(conn->hcon, chan->sec_level, auth_type); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 746 | } |
| 747 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 748 | static u8 l2cap_get_ident(struct l2cap_conn *conn) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 749 | { |
| 750 | u8 id; |
| 751 | |
| 752 | /* Get next available identificator. |
| 753 | * 1 - 128 are used by kernel. |
| 754 | * 129 - 199 are reserved. |
| 755 | * 200 - 254 are used by utilities like l2ping, etc. |
| 756 | */ |
| 757 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 758 | spin_lock(&conn->lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 759 | |
| 760 | if (++conn->tx_ident > 128) |
| 761 | conn->tx_ident = 1; |
| 762 | |
| 763 | id = conn->tx_ident; |
| 764 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 765 | spin_unlock(&conn->lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 766 | |
| 767 | return id; |
| 768 | } |
| 769 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 770 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, |
| 771 | void *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 772 | { |
| 773 | struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 774 | u8 flags; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 775 | |
| 776 | BT_DBG("code 0x%2.2x", code); |
| 777 | |
| 778 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 779 | return; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 780 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 781 | if (lmp_no_flush_capable(conn->hcon->hdev)) |
| 782 | flags = ACL_START_NO_FLUSH; |
| 783 | else |
| 784 | flags = ACL_START; |
| 785 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 786 | bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 787 | skb->priority = HCI_PRIO_MAX; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 788 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 789 | hci_send_acl(conn->hchan, skb, flags); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 790 | } |
| 791 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 792 | static bool __chan_is_moving(struct l2cap_chan *chan) |
| 793 | { |
| 794 | return chan->move_state != L2CAP_MOVE_STABLE && |
| 795 | chan->move_state != L2CAP_MOVE_WAIT_PREPARE; |
| 796 | } |
| 797 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 798 | static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) |
| 799 | { |
| 800 | struct hci_conn *hcon = chan->conn->hcon; |
| 801 | u16 flags; |
| 802 | |
| 803 | 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] | 804 | skb->priority); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 805 | |
Mat Martineau | d5f8a75 | 2012-10-23 15:24:18 -0700 | [diff] [blame] | 806 | if (chan->hs_hcon && !__chan_is_moving(chan)) { |
| 807 | if (chan->hs_hchan) |
| 808 | hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE); |
| 809 | else |
| 810 | kfree_skb(skb); |
| 811 | |
| 812 | return; |
| 813 | } |
| 814 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 815 | if (!test_bit(FLAG_FLUSHABLE, &chan->flags) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 816 | lmp_no_flush_capable(hcon->hdev)) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 817 | flags = ACL_START_NO_FLUSH; |
| 818 | else |
| 819 | flags = ACL_START; |
| 820 | |
| 821 | bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 822 | hci_send_acl(chan->conn->hchan, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 825 | static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control) |
| 826 | { |
| 827 | control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT; |
| 828 | control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT; |
| 829 | |
| 830 | if (enh & L2CAP_CTRL_FRAME_TYPE) { |
| 831 | /* S-Frame */ |
| 832 | control->sframe = 1; |
| 833 | control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT; |
| 834 | control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT; |
| 835 | |
| 836 | control->sar = 0; |
| 837 | control->txseq = 0; |
| 838 | } else { |
| 839 | /* I-Frame */ |
| 840 | control->sframe = 0; |
| 841 | control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT; |
| 842 | control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT; |
| 843 | |
| 844 | control->poll = 0; |
| 845 | control->super = 0; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control) |
| 850 | { |
| 851 | control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT; |
| 852 | control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT; |
| 853 | |
| 854 | if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) { |
| 855 | /* S-Frame */ |
| 856 | control->sframe = 1; |
| 857 | control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT; |
| 858 | control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT; |
| 859 | |
| 860 | control->sar = 0; |
| 861 | control->txseq = 0; |
| 862 | } else { |
| 863 | /* I-Frame */ |
| 864 | control->sframe = 0; |
| 865 | control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT; |
| 866 | control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT; |
| 867 | |
| 868 | control->poll = 0; |
| 869 | control->super = 0; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | static inline void __unpack_control(struct l2cap_chan *chan, |
| 874 | struct sk_buff *skb) |
| 875 | { |
| 876 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 877 | __unpack_extended_control(get_unaligned_le32(skb->data), |
| 878 | &bt_cb(skb)->control); |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 879 | skb_pull(skb, L2CAP_EXT_CTRL_SIZE); |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 880 | } else { |
| 881 | __unpack_enhanced_control(get_unaligned_le16(skb->data), |
| 882 | &bt_cb(skb)->control); |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 883 | skb_pull(skb, L2CAP_ENH_CTRL_SIZE); |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | |
| 887 | static u32 __pack_extended_control(struct l2cap_ctrl *control) |
| 888 | { |
| 889 | u32 packed; |
| 890 | |
| 891 | packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT; |
| 892 | packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT; |
| 893 | |
| 894 | if (control->sframe) { |
| 895 | packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT; |
| 896 | packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT; |
| 897 | packed |= L2CAP_EXT_CTRL_FRAME_TYPE; |
| 898 | } else { |
| 899 | packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT; |
| 900 | packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT; |
| 901 | } |
| 902 | |
| 903 | return packed; |
| 904 | } |
| 905 | |
| 906 | static u16 __pack_enhanced_control(struct l2cap_ctrl *control) |
| 907 | { |
| 908 | u16 packed; |
| 909 | |
| 910 | packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT; |
| 911 | packed |= control->final << L2CAP_CTRL_FINAL_SHIFT; |
| 912 | |
| 913 | if (control->sframe) { |
| 914 | packed |= control->poll << L2CAP_CTRL_POLL_SHIFT; |
| 915 | packed |= control->super << L2CAP_CTRL_SUPER_SHIFT; |
| 916 | packed |= L2CAP_CTRL_FRAME_TYPE; |
| 917 | } else { |
| 918 | packed |= control->sar << L2CAP_CTRL_SAR_SHIFT; |
| 919 | packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT; |
| 920 | } |
| 921 | |
| 922 | return packed; |
| 923 | } |
| 924 | |
| 925 | static inline void __pack_control(struct l2cap_chan *chan, |
| 926 | struct l2cap_ctrl *control, |
| 927 | struct sk_buff *skb) |
| 928 | { |
| 929 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 930 | put_unaligned_le32(__pack_extended_control(control), |
| 931 | skb->data + L2CAP_HDR_SIZE); |
| 932 | } else { |
| 933 | put_unaligned_le16(__pack_enhanced_control(control), |
| 934 | skb->data + L2CAP_HDR_SIZE); |
| 935 | } |
| 936 | } |
| 937 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 938 | static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan) |
| 939 | { |
| 940 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 941 | return L2CAP_EXT_HDR_SIZE; |
| 942 | else |
| 943 | return L2CAP_ENH_HDR_SIZE; |
| 944 | } |
| 945 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 946 | static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan, |
| 947 | u32 control) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 948 | { |
| 949 | struct sk_buff *skb; |
| 950 | struct l2cap_hdr *lh; |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 951 | int hlen = __ertm_hdr_size(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 952 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 953 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 954 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 955 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 956 | skb = bt_skb_alloc(hlen, GFP_KERNEL); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 957 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 958 | if (!skb) |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 959 | return ERR_PTR(-ENOMEM); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 960 | |
| 961 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 962 | lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 963 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 964 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 965 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 966 | put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 967 | else |
| 968 | put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 969 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 970 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 971 | u16 fcs = crc16(0, (u8 *)skb->data, skb->len); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 972 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 973 | } |
| 974 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 975 | skb->priority = HCI_PRIO_MAX; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 976 | return skb; |
| 977 | } |
| 978 | |
| 979 | static void l2cap_send_sframe(struct l2cap_chan *chan, |
| 980 | struct l2cap_ctrl *control) |
| 981 | { |
| 982 | struct sk_buff *skb; |
| 983 | u32 control_field; |
| 984 | |
| 985 | BT_DBG("chan %p, control %p", chan, control); |
| 986 | |
| 987 | if (!control->sframe) |
| 988 | return; |
| 989 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 990 | if (__chan_is_moving(chan)) |
| 991 | return; |
| 992 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 993 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) && |
| 994 | !control->poll) |
| 995 | control->final = 1; |
| 996 | |
| 997 | if (control->super == L2CAP_SUPER_RR) |
| 998 | clear_bit(CONN_RNR_SENT, &chan->conn_state); |
| 999 | else if (control->super == L2CAP_SUPER_RNR) |
| 1000 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
| 1001 | |
| 1002 | if (control->super != L2CAP_SUPER_SREJ) { |
| 1003 | chan->last_acked_seq = control->reqseq; |
| 1004 | __clear_ack_timer(chan); |
| 1005 | } |
| 1006 | |
| 1007 | BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq, |
| 1008 | control->final, control->poll, control->super); |
| 1009 | |
| 1010 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 1011 | control_field = __pack_extended_control(control); |
| 1012 | else |
| 1013 | control_field = __pack_enhanced_control(control); |
| 1014 | |
| 1015 | skb = l2cap_create_sframe_pdu(chan, control_field); |
| 1016 | if (!IS_ERR(skb)) |
| 1017 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1018 | } |
| 1019 | |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1020 | 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] | 1021 | { |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1022 | struct l2cap_ctrl control; |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 1023 | |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1024 | BT_DBG("chan %p, poll %d", chan, poll); |
| 1025 | |
| 1026 | memset(&control, 0, sizeof(control)); |
| 1027 | control.sframe = 1; |
| 1028 | control.poll = poll; |
| 1029 | |
| 1030 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 1031 | control.super = L2CAP_SUPER_RNR; |
| 1032 | else |
| 1033 | control.super = L2CAP_SUPER_RR; |
| 1034 | |
| 1035 | control.reqseq = chan->buffer_seq; |
| 1036 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 1037 | } |
| 1038 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1039 | static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 1040 | { |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1041 | return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 1042 | } |
| 1043 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1044 | static bool __amp_capable(struct l2cap_chan *chan) |
| 1045 | { |
| 1046 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 1df7b17 | 2013-10-05 11:47:49 -0700 | [diff] [blame] | 1047 | struct hci_dev *hdev; |
| 1048 | bool amp_available = false; |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1049 | |
Marcel Holtmann | 1df7b17 | 2013-10-05 11:47:49 -0700 | [diff] [blame] | 1050 | if (!conn->hs_enabled) |
| 1051 | return false; |
| 1052 | |
| 1053 | if (!(conn->fixed_chan_mask & L2CAP_FC_A2MP)) |
| 1054 | return false; |
| 1055 | |
| 1056 | read_lock(&hci_dev_list_lock); |
| 1057 | list_for_each_entry(hdev, &hci_dev_list, list) { |
| 1058 | if (hdev->amp_type != AMP_TYPE_BREDR && |
| 1059 | test_bit(HCI_UP, &hdev->flags)) { |
| 1060 | amp_available = true; |
| 1061 | break; |
| 1062 | } |
| 1063 | } |
| 1064 | read_unlock(&hci_dev_list_lock); |
| 1065 | |
| 1066 | if (chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED) |
| 1067 | return amp_available; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 1068 | |
| 1069 | return false; |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1070 | } |
| 1071 | |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 1072 | static bool l2cap_check_efs(struct l2cap_chan *chan) |
| 1073 | { |
| 1074 | /* Check EFS parameters */ |
| 1075 | return true; |
| 1076 | } |
| 1077 | |
Andrei Emeltchenko | 2766be4 | 2012-09-27 17:26:21 +0300 | [diff] [blame] | 1078 | void l2cap_send_conn_req(struct l2cap_chan *chan) |
Andrei Emeltchenko | 9b27f35 | 2012-02-24 16:00:00 +0200 | [diff] [blame] | 1079 | { |
| 1080 | struct l2cap_conn *conn = chan->conn; |
| 1081 | struct l2cap_conn_req req; |
| 1082 | |
| 1083 | req.scid = cpu_to_le16(chan->scid); |
| 1084 | req.psm = chan->psm; |
| 1085 | |
| 1086 | chan->ident = l2cap_get_ident(conn); |
| 1087 | |
| 1088 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
| 1089 | |
| 1090 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req); |
| 1091 | } |
| 1092 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 1093 | static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id) |
| 1094 | { |
| 1095 | struct l2cap_create_chan_req req; |
| 1096 | req.scid = cpu_to_le16(chan->scid); |
| 1097 | req.psm = chan->psm; |
| 1098 | req.amp_id = amp_id; |
| 1099 | |
| 1100 | chan->ident = l2cap_get_ident(chan->conn); |
| 1101 | |
| 1102 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ, |
| 1103 | sizeof(req), &req); |
| 1104 | } |
| 1105 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 1106 | static void l2cap_move_setup(struct l2cap_chan *chan) |
| 1107 | { |
| 1108 | struct sk_buff *skb; |
| 1109 | |
| 1110 | BT_DBG("chan %p", chan); |
| 1111 | |
| 1112 | if (chan->mode != L2CAP_MODE_ERTM) |
| 1113 | return; |
| 1114 | |
| 1115 | __clear_retrans_timer(chan); |
| 1116 | __clear_monitor_timer(chan); |
| 1117 | __clear_ack_timer(chan); |
| 1118 | |
| 1119 | chan->retry_count = 0; |
| 1120 | skb_queue_walk(&chan->tx_q, skb) { |
| 1121 | if (bt_cb(skb)->control.retries) |
| 1122 | bt_cb(skb)->control.retries = 1; |
| 1123 | else |
| 1124 | break; |
| 1125 | } |
| 1126 | |
| 1127 | chan->expected_tx_seq = chan->buffer_seq; |
| 1128 | |
| 1129 | clear_bit(CONN_REJ_ACT, &chan->conn_state); |
| 1130 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 1131 | l2cap_seq_list_clear(&chan->retrans_list); |
| 1132 | l2cap_seq_list_clear(&chan->srej_list); |
| 1133 | skb_queue_purge(&chan->srej_q); |
| 1134 | |
| 1135 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 1136 | chan->rx_state = L2CAP_RX_STATE_MOVE; |
| 1137 | |
| 1138 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 1139 | } |
| 1140 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 1141 | static void l2cap_move_done(struct l2cap_chan *chan) |
| 1142 | { |
| 1143 | u8 move_role = chan->move_role; |
| 1144 | BT_DBG("chan %p", chan); |
| 1145 | |
| 1146 | chan->move_state = L2CAP_MOVE_STABLE; |
| 1147 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 1148 | |
| 1149 | if (chan->mode != L2CAP_MODE_ERTM) |
| 1150 | return; |
| 1151 | |
| 1152 | switch (move_role) { |
| 1153 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 1154 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL); |
| 1155 | chan->rx_state = L2CAP_RX_STATE_WAIT_F; |
| 1156 | break; |
| 1157 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 1158 | chan->rx_state = L2CAP_RX_STATE_WAIT_P; |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1163 | static void l2cap_chan_ready(struct l2cap_chan *chan) |
| 1164 | { |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 1165 | /* This clears all conf flags, including CONF_NOT_COMPLETE */ |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1166 | chan->conf_state = 0; |
| 1167 | __clear_chan_timer(chan); |
| 1168 | |
Andrei Emeltchenko | 54a59aa | 2012-05-27 22:27:53 -0300 | [diff] [blame] | 1169 | chan->state = BT_CONNECTED; |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1170 | |
Andrei Emeltchenko | fd83e2c | 2012-05-30 09:55:32 +0300 | [diff] [blame] | 1171 | chan->ops->ready(chan); |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1172 | } |
| 1173 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1174 | static void l2cap_start_connection(struct l2cap_chan *chan) |
| 1175 | { |
| 1176 | if (__amp_capable(chan)) { |
| 1177 | BT_DBG("chan %p AMP capable: discover AMPs", chan); |
| 1178 | a2mp_discover_amp(chan); |
| 1179 | } else { |
| 1180 | l2cap_send_conn_req(chan); |
| 1181 | } |
| 1182 | } |
| 1183 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1184 | static void l2cap_do_start(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1185 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1186 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1187 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1188 | if (conn->hcon->type == LE_LINK) { |
| 1189 | l2cap_chan_ready(chan); |
| 1190 | return; |
| 1191 | } |
| 1192 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1193 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1194 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) |
| 1195 | return; |
| 1196 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1197 | if (l2cap_chan_check_security(chan) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1198 | __l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1199 | l2cap_start_connection(chan); |
| 1200 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1201 | } else { |
| 1202 | struct l2cap_info_req req; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 1203 | req.type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1204 | |
| 1205 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 1206 | conn->info_ident = l2cap_get_ident(conn); |
| 1207 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 1208 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1209 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1210 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 1211 | sizeof(req), &req); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 1215 | static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) |
| 1216 | { |
| 1217 | u32 local_feat_mask = l2cap_feat_mask; |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 1218 | if (!disable_ertm) |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 1219 | local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; |
| 1220 | |
| 1221 | switch (mode) { |
| 1222 | case L2CAP_MODE_ERTM: |
| 1223 | return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; |
| 1224 | case L2CAP_MODE_STREAMING: |
| 1225 | return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; |
| 1226 | default: |
| 1227 | return 0x00; |
| 1228 | } |
| 1229 | } |
| 1230 | |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1231 | 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] | 1232 | { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1233 | struct sock *sk = chan->sk; |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1234 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1235 | struct l2cap_disconn_req req; |
| 1236 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1237 | if (!conn) |
| 1238 | return; |
| 1239 | |
Andrei Emeltchenko | aad3d0e | 2012-09-06 15:05:42 +0300 | [diff] [blame] | 1240 | if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1241 | __clear_retrans_timer(chan); |
| 1242 | __clear_monitor_timer(chan); |
| 1243 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1244 | } |
| 1245 | |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1246 | if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) { |
Gustavo Padovan | d117773 | 2012-10-06 11:47:38 +0100 | [diff] [blame] | 1247 | l2cap_state_change(chan, BT_DISCONN); |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1248 | return; |
| 1249 | } |
| 1250 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1251 | req.dcid = cpu_to_le16(chan->dcid); |
| 1252 | req.scid = cpu_to_le16(chan->scid); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1253 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ, |
| 1254 | sizeof(req), &req); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1255 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1256 | lock_sock(sk); |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 1257 | __l2cap_state_change(chan, BT_DISCONN); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 1258 | __l2cap_chan_set_err(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1259 | release_sock(sk); |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1260 | } |
| 1261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | /* ---- L2CAP connections ---- */ |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1263 | static void l2cap_conn_start(struct l2cap_conn *conn) |
| 1264 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1265 | struct l2cap_chan *chan, *tmp; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1266 | |
| 1267 | BT_DBG("conn %p", conn); |
| 1268 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1269 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1270 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1271 | list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1272 | l2cap_chan_lock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1273 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1274 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1275 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1276 | continue; |
| 1277 | } |
| 1278 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1279 | if (chan->state == BT_CONNECT) { |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1280 | if (!l2cap_chan_check_security(chan) || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1281 | !__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1282 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1283 | continue; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 1284 | } |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1285 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1286 | if (!l2cap_mode_supported(chan->mode, conn->feat_mask) |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1287 | && test_bit(CONF_STATE2_DEVICE, |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1288 | &chan->conf_state)) { |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1289 | l2cap_chan_close(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1290 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1291 | continue; |
| 1292 | } |
| 1293 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1294 | l2cap_start_connection(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1295 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1296 | } else if (chan->state == BT_CONNECT2) { |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1297 | struct sock *sk = chan->sk; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1298 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1299 | char buf[128]; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1300 | rsp.scid = cpu_to_le16(chan->dcid); |
| 1301 | rsp.dcid = cpu_to_le16(chan->scid); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1302 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1303 | if (l2cap_chan_check_security(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1304 | lock_sock(sk); |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 1305 | if (test_bit(BT_SK_DEFER_SETUP, |
| 1306 | &bt_sk(sk)->flags)) { |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 1307 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_PEND); |
| 1308 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_AUTHOR_PEND); |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 1309 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 1310 | |
| 1311 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 1312 | __l2cap_state_change(chan, BT_CONFIG); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 1313 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS); |
| 1314 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 1315 | } |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1316 | release_sock(sk); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1317 | } else { |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 1318 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_PEND); |
| 1319 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_AUTHEN_PEND); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1320 | } |
| 1321 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1322 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1323 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1324 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1325 | if (test_bit(CONF_REQ_SENT, &chan->conf_state) || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1326 | rsp.result != L2CAP_CR_SUCCESS) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1327 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1328 | continue; |
| 1329 | } |
| 1330 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1331 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1332 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1333 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1334 | chan->num_conf_req++; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1335 | } |
| 1336 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1337 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1338 | } |
| 1339 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1340 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1341 | } |
| 1342 | |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1343 | /* Find socket with cid and source/destination bdaddr. |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1344 | * Returns closest match, locked. |
| 1345 | */ |
Andrei Emeltchenko | d9b8870 | 2012-03-12 12:13:08 +0200 | [diff] [blame] | 1346 | 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] | 1347 | bdaddr_t *src, |
| 1348 | bdaddr_t *dst) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1349 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1350 | struct l2cap_chan *c, *c1 = NULL; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1351 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1352 | read_lock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1353 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1354 | list_for_each_entry(c, &chan_list, global_l) { |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1355 | if (state && c->state != state) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1356 | continue; |
| 1357 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1358 | if (c->scid == cid) { |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1359 | int src_match, dst_match; |
| 1360 | int src_any, dst_any; |
| 1361 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1362 | /* Exact match. */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1363 | src_match = !bacmp(&c->src, src); |
| 1364 | dst_match = !bacmp(&c->dst, dst); |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1365 | if (src_match && dst_match) { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1366 | read_unlock(&chan_list_lock); |
| 1367 | return c; |
| 1368 | } |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1369 | |
| 1370 | /* Closest match */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1371 | src_any = !bacmp(&c->src, BDADDR_ANY); |
| 1372 | dst_any = !bacmp(&c->dst, BDADDR_ANY); |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1373 | if ((src_match && dst_any) || (src_any && dst_match) || |
| 1374 | (src_any && dst_any)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1375 | c1 = c; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1376 | } |
| 1377 | } |
Gustavo F. Padovan | 280f294 | 2011-04-13 19:01:22 -0300 | [diff] [blame] | 1378 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1379 | read_unlock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1380 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1381 | return c1; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | static void l2cap_le_conn_ready(struct l2cap_conn *conn) |
| 1385 | { |
Johan Hedberg | 60bac184 | 2013-04-29 19:35:41 +0300 | [diff] [blame] | 1386 | struct sock *parent; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1387 | struct l2cap_chan *chan, *pchan; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1388 | |
| 1389 | BT_DBG(""); |
| 1390 | |
| 1391 | /* Check if we have socket listening on cid */ |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame] | 1392 | pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_ATT, |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 1393 | &conn->hcon->src, &conn->hcon->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1394 | if (!pchan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1395 | return; |
| 1396 | |
Johan Hedberg | 44f3b0f | 2013-04-29 19:35:45 +0300 | [diff] [blame] | 1397 | /* Client ATT sockets should override the server one */ |
| 1398 | if (__l2cap_get_chan_by_dcid(conn, L2CAP_CID_ATT)) |
| 1399 | return; |
| 1400 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1401 | parent = pchan->sk; |
| 1402 | |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 1403 | lock_sock(parent); |
Gustavo F. Padovan | 62f3a2c | 2011-04-14 18:34:34 -0300 | [diff] [blame] | 1404 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 1405 | chan = pchan->ops->new_connection(pchan); |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1406 | if (!chan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1407 | goto clean; |
| 1408 | |
Johan Hedberg | 9f22398c | 2013-04-29 19:35:38 +0300 | [diff] [blame] | 1409 | chan->dcid = L2CAP_CID_ATT; |
| 1410 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1411 | bacpy(&chan->src, &conn->hcon->src); |
| 1412 | bacpy(&chan->dst, &conn->hcon->dst); |
Marcel Holtmann | 4f1654e | 2013-10-13 08:50:41 -0700 | [diff] [blame] | 1413 | chan->src_type = bdaddr_type(conn->hcon, conn->hcon->src_type); |
| 1414 | chan->dst_type = bdaddr_type(conn->hcon, conn->hcon->dst_type); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1415 | |
Johan Hedberg | 44f3b0f | 2013-04-29 19:35:45 +0300 | [diff] [blame] | 1416 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1417 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1418 | clean: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 1419 | release_sock(parent); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1420 | } |
| 1421 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1422 | static void l2cap_conn_ready(struct l2cap_conn *conn) |
| 1423 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1424 | struct l2cap_chan *chan; |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1425 | struct hci_conn *hcon = conn->hcon; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1426 | |
| 1427 | BT_DBG("conn %p", conn); |
| 1428 | |
Johan Hedberg | d872992 | 2013-04-29 19:35:39 +0300 | [diff] [blame] | 1429 | /* For outgoing pairing which doesn't necessarily have an |
| 1430 | * associated socket (e.g. mgmt_pair_device). |
| 1431 | */ |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1432 | if (hcon->out && hcon->type == LE_LINK) |
| 1433 | smp_conn_security(hcon, hcon->pending_sec_level); |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 1434 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1435 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1436 | |
Johan Hedberg | 44f3b0f | 2013-04-29 19:35:45 +0300 | [diff] [blame] | 1437 | if (hcon->type == LE_LINK) |
| 1438 | l2cap_le_conn_ready(conn); |
| 1439 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1440 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1441 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1442 | l2cap_chan_lock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1443 | |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1444 | if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) { |
| 1445 | l2cap_chan_unlock(chan); |
| 1446 | continue; |
| 1447 | } |
| 1448 | |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1449 | if (hcon->type == LE_LINK) { |
| 1450 | if (smp_conn_security(hcon, chan->sec_level)) |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 1451 | l2cap_chan_ready(chan); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1452 | |
Vinicius Costa Gomes | 6312845 | 2011-06-17 22:46:26 -0300 | [diff] [blame] | 1453 | } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1454 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1455 | __clear_chan_timer(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1456 | lock_sock(sk); |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 1457 | __l2cap_state_change(chan, BT_CONNECTED); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1458 | sk->sk_state_change(sk); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1459 | release_sock(sk); |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 1460 | |
Gustavo Padovan | 1c244f7 | 2012-12-07 03:29:10 -0200 | [diff] [blame] | 1461 | } else if (chan->state == BT_CONNECT) { |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1462 | l2cap_do_start(chan); |
Gustavo Padovan | 1c244f7 | 2012-12-07 03:29:10 -0200 | [diff] [blame] | 1463 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1464 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1465 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1466 | } |
| 1467 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1468 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1469 | } |
| 1470 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1471 | /* Notify sockets that we cannot guaranty reliability anymore */ |
| 1472 | static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) |
| 1473 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1474 | struct l2cap_chan *chan; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1475 | |
| 1476 | BT_DBG("conn %p", conn); |
| 1477 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1478 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1479 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1480 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | ecf61bd | 2011-10-11 14:04:32 +0300 | [diff] [blame] | 1481 | if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) |
Gustavo Padovan | 1d8b1fd | 2012-10-06 11:34:52 +0100 | [diff] [blame] | 1482 | l2cap_chan_set_err(chan, err); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1483 | } |
| 1484 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1485 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1486 | } |
| 1487 | |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1488 | static void l2cap_info_timeout(struct work_struct *work) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1489 | { |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1490 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1491 | info_timer.work); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1492 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1493 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 1494 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1495 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1496 | l2cap_conn_start(conn); |
| 1497 | } |
| 1498 | |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1499 | /* |
| 1500 | * l2cap_user |
| 1501 | * External modules can register l2cap_user objects on l2cap_conn. The ->probe |
| 1502 | * callback is called during registration. The ->remove callback is called |
| 1503 | * during unregistration. |
| 1504 | * An l2cap_user object can either be explicitly unregistered or when the |
| 1505 | * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon, |
| 1506 | * l2cap->hchan, .. are valid as long as the remove callback hasn't been called. |
| 1507 | * External modules must own a reference to the l2cap_conn object if they intend |
| 1508 | * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at |
| 1509 | * any time if they don't. |
| 1510 | */ |
| 1511 | |
| 1512 | int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user) |
| 1513 | { |
| 1514 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1515 | int ret; |
| 1516 | |
| 1517 | /* We need to check whether l2cap_conn is registered. If it is not, we |
| 1518 | * must not register the l2cap_user. l2cap_conn_del() is unregisters |
| 1519 | * l2cap_conn objects, but doesn't provide its own locking. Instead, it |
| 1520 | * relies on the parent hci_conn object to be locked. This itself relies |
| 1521 | * on the hci_dev object to be locked. So we must lock the hci device |
| 1522 | * here, too. */ |
| 1523 | |
| 1524 | hci_dev_lock(hdev); |
| 1525 | |
| 1526 | if (user->list.next || user->list.prev) { |
| 1527 | ret = -EINVAL; |
| 1528 | goto out_unlock; |
| 1529 | } |
| 1530 | |
| 1531 | /* conn->hchan is NULL after l2cap_conn_del() was called */ |
| 1532 | if (!conn->hchan) { |
| 1533 | ret = -ENODEV; |
| 1534 | goto out_unlock; |
| 1535 | } |
| 1536 | |
| 1537 | ret = user->probe(conn, user); |
| 1538 | if (ret) |
| 1539 | goto out_unlock; |
| 1540 | |
| 1541 | list_add(&user->list, &conn->users); |
| 1542 | ret = 0; |
| 1543 | |
| 1544 | out_unlock: |
| 1545 | hci_dev_unlock(hdev); |
| 1546 | return ret; |
| 1547 | } |
| 1548 | EXPORT_SYMBOL(l2cap_register_user); |
| 1549 | |
| 1550 | void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user) |
| 1551 | { |
| 1552 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1553 | |
| 1554 | hci_dev_lock(hdev); |
| 1555 | |
| 1556 | if (!user->list.next || !user->list.prev) |
| 1557 | goto out_unlock; |
| 1558 | |
| 1559 | list_del(&user->list); |
| 1560 | user->list.next = NULL; |
| 1561 | user->list.prev = NULL; |
| 1562 | user->remove(conn, user); |
| 1563 | |
| 1564 | out_unlock: |
| 1565 | hci_dev_unlock(hdev); |
| 1566 | } |
| 1567 | EXPORT_SYMBOL(l2cap_unregister_user); |
| 1568 | |
| 1569 | static void l2cap_unregister_all_users(struct l2cap_conn *conn) |
| 1570 | { |
| 1571 | struct l2cap_user *user; |
| 1572 | |
| 1573 | while (!list_empty(&conn->users)) { |
| 1574 | user = list_first_entry(&conn->users, struct l2cap_user, list); |
| 1575 | list_del(&user->list); |
| 1576 | user->list.next = NULL; |
| 1577 | user->list.prev = NULL; |
| 1578 | user->remove(conn, user); |
| 1579 | } |
| 1580 | } |
| 1581 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1582 | static void l2cap_conn_del(struct hci_conn *hcon, int err) |
| 1583 | { |
| 1584 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 1585 | struct l2cap_chan *chan, *l; |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1586 | |
| 1587 | if (!conn) |
| 1588 | return; |
| 1589 | |
| 1590 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 1591 | |
| 1592 | kfree_skb(conn->rx_skb); |
| 1593 | |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1594 | l2cap_unregister_all_users(conn); |
| 1595 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1596 | mutex_lock(&conn->chan_lock); |
| 1597 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1598 | /* Kill channels */ |
| 1599 | list_for_each_entry_safe(chan, l, &conn->chan_l, list) { |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1600 | l2cap_chan_hold(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1601 | l2cap_chan_lock(chan); |
| 1602 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1603 | l2cap_chan_del(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1604 | |
| 1605 | l2cap_chan_unlock(chan); |
| 1606 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 1607 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1608 | l2cap_chan_put(chan); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1609 | } |
| 1610 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1611 | mutex_unlock(&conn->chan_lock); |
| 1612 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1613 | hci_chan_del(conn->hchan); |
| 1614 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1615 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1616 | cancel_delayed_work_sync(&conn->info_timer); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1617 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1618 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) { |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1619 | cancel_delayed_work_sync(&conn->security_timer); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1620 | smp_chan_destroy(conn); |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1621 | } |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1622 | |
| 1623 | hcon->l2cap_data = NULL; |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1624 | conn->hchan = NULL; |
| 1625 | l2cap_conn_put(conn); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1626 | } |
| 1627 | |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1628 | static void security_timeout(struct work_struct *work) |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1629 | { |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1630 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1631 | security_timer.work); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1632 | |
Johan Hedberg | d06cc41 | 2012-06-06 18:44:11 +0800 | [diff] [blame] | 1633 | BT_DBG("conn %p", conn); |
| 1634 | |
| 1635 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) { |
| 1636 | smp_chan_destroy(conn); |
| 1637 | l2cap_conn_del(conn->hcon, ETIMEDOUT); |
| 1638 | } |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1639 | } |
| 1640 | |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 1641 | static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1643 | struct l2cap_conn *conn = hcon->l2cap_data; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1644 | struct hci_chan *hchan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 1646 | if (conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | return conn; |
| 1648 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1649 | hchan = hci_chan_create(hcon); |
| 1650 | if (!hchan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 1653 | conn = kzalloc(sizeof(struct l2cap_conn), GFP_KERNEL); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1654 | if (!conn) { |
| 1655 | hci_chan_del(hchan); |
| 1656 | return NULL; |
| 1657 | } |
| 1658 | |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1659 | kref_init(&conn->ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | hcon->l2cap_data = conn; |
| 1661 | conn->hcon = hcon; |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1662 | hci_conn_get(conn->hcon); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1663 | conn->hchan = hchan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1665 | BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1666 | |
Andrei Emeltchenko | dcc042d | 2012-10-05 16:56:57 +0300 | [diff] [blame] | 1667 | switch (hcon->type) { |
Andrei Emeltchenko | dcc042d | 2012-10-05 16:56:57 +0300 | [diff] [blame] | 1668 | case LE_LINK: |
| 1669 | if (hcon->hdev->le_mtu) { |
| 1670 | conn->mtu = hcon->hdev->le_mtu; |
| 1671 | break; |
| 1672 | } |
| 1673 | /* fall through */ |
Andrei Emeltchenko | dcc042d | 2012-10-05 16:56:57 +0300 | [diff] [blame] | 1674 | default: |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1675 | conn->mtu = hcon->hdev->acl_mtu; |
Andrei Emeltchenko | dcc042d | 2012-10-05 16:56:57 +0300 | [diff] [blame] | 1676 | break; |
| 1677 | } |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1678 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1679 | conn->feat_mask = 0; |
| 1680 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 1681 | if (hcon->type == ACL_LINK) |
| 1682 | conn->hs_enabled = test_bit(HCI_HS_ENABLED, |
| 1683 | &hcon->hdev->dev_flags); |
| 1684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | spin_lock_init(&conn->lock); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1686 | mutex_init(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1687 | |
| 1688 | INIT_LIST_HEAD(&conn->chan_l); |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1689 | INIT_LIST_HEAD(&conn->users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1691 | if (hcon->type == LE_LINK) |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1692 | INIT_DELAYED_WORK(&conn->security_timer, security_timeout); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1693 | else |
Gustavo F. Padovan | 030013d | 2011-12-20 10:57:28 -0200 | [diff] [blame] | 1694 | INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); |
Dave Young | 45054dc | 2009-10-18 20:28:30 +0000 | [diff] [blame] | 1695 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 1696 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 1697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | return conn; |
| 1699 | } |
| 1700 | |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1701 | static void l2cap_conn_free(struct kref *ref) |
| 1702 | { |
| 1703 | struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref); |
| 1704 | |
| 1705 | hci_conn_put(conn->hcon); |
| 1706 | kfree(conn); |
| 1707 | } |
| 1708 | |
| 1709 | void l2cap_conn_get(struct l2cap_conn *conn) |
| 1710 | { |
| 1711 | kref_get(&conn->ref); |
| 1712 | } |
| 1713 | EXPORT_SYMBOL(l2cap_conn_get); |
| 1714 | |
| 1715 | void l2cap_conn_put(struct l2cap_conn *conn) |
| 1716 | { |
| 1717 | kref_put(&conn->ref, l2cap_conn_free); |
| 1718 | } |
| 1719 | EXPORT_SYMBOL(l2cap_conn_put); |
| 1720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | /* ---- Socket interface ---- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1723 | /* Find socket with psm and source / destination bdaddr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | * Returns closest match. |
| 1725 | */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1726 | static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, |
| 1727 | bdaddr_t *src, |
| 1728 | bdaddr_t *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1730 | struct l2cap_chan *c, *c1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1732 | read_lock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1733 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1734 | list_for_each_entry(c, &chan_list, global_l) { |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1735 | if (state && c->state != state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | continue; |
| 1737 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1738 | if (c->psm == psm) { |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1739 | int src_match, dst_match; |
| 1740 | int src_any, dst_any; |
| 1741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | /* Exact match. */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1743 | src_match = !bacmp(&c->src, src); |
| 1744 | dst_match = !bacmp(&c->dst, dst); |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1745 | if (src_match && dst_match) { |
Johannes Berg | a7567b2 | 2011-06-01 08:29:54 +0200 | [diff] [blame] | 1746 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1747 | return c; |
| 1748 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | |
| 1750 | /* Closest match */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1751 | src_any = !bacmp(&c->src, BDADDR_ANY); |
| 1752 | dst_any = !bacmp(&c->dst, BDADDR_ANY); |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1753 | if ((src_match && dst_any) || (src_any && dst_match) || |
| 1754 | (src_any && dst_any)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1755 | c1 = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | } |
| 1757 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1759 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1760 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1761 | return c1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
Andre Guedes | 8e9f989 | 2012-04-24 21:02:55 -0300 | [diff] [blame] | 1764 | int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, |
| 1765 | bdaddr_t *dst, u8 dst_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | { |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 1767 | struct sock *sk = chan->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | struct l2cap_conn *conn; |
| 1769 | struct hci_conn *hcon; |
| 1770 | struct hci_dev *hdev; |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1771 | __u8 auth_type; |
Marcel Holtmann | 44d0e48 | 2009-04-20 07:09:16 +0200 | [diff] [blame] | 1772 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1774 | BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", &chan->src, dst, |
Syam Sidhardhan | ab19516 | 2012-07-27 23:51:22 +0530 | [diff] [blame] | 1775 | dst_type, __le16_to_cpu(psm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1777 | hdev = hci_get_route(dst, &chan->src); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1778 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | return -EHOSTUNREACH; |
| 1780 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1781 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1783 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1784 | |
| 1785 | /* PSM must be odd and lsb of upper byte must be 0 */ |
| 1786 | if ((__le16_to_cpu(psm) & 0x0101) != 0x0001 && !cid && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1787 | chan->chan_type != L2CAP_CHAN_RAW) { |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1788 | err = -EINVAL; |
| 1789 | goto done; |
| 1790 | } |
| 1791 | |
| 1792 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !(psm || cid)) { |
| 1793 | err = -EINVAL; |
| 1794 | goto done; |
| 1795 | } |
| 1796 | |
| 1797 | switch (chan->mode) { |
| 1798 | case L2CAP_MODE_BASIC: |
| 1799 | break; |
| 1800 | case L2CAP_MODE_ERTM: |
| 1801 | case L2CAP_MODE_STREAMING: |
| 1802 | if (!disable_ertm) |
| 1803 | break; |
| 1804 | /* fall through */ |
| 1805 | default: |
| 1806 | err = -ENOTSUPP; |
| 1807 | goto done; |
| 1808 | } |
| 1809 | |
Gustavo Padovan | 0797e01 | 2012-05-27 22:27:54 -0300 | [diff] [blame] | 1810 | switch (chan->state) { |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1811 | case BT_CONNECT: |
| 1812 | case BT_CONNECT2: |
| 1813 | case BT_CONFIG: |
| 1814 | /* Already connecting */ |
| 1815 | err = 0; |
| 1816 | goto done; |
| 1817 | |
| 1818 | case BT_CONNECTED: |
| 1819 | /* Already connected */ |
| 1820 | err = -EISCONN; |
| 1821 | goto done; |
| 1822 | |
| 1823 | case BT_OPEN: |
| 1824 | case BT_BOUND: |
| 1825 | /* Can connect */ |
| 1826 | break; |
| 1827 | |
| 1828 | default: |
| 1829 | err = -EBADFD; |
| 1830 | goto done; |
| 1831 | } |
| 1832 | |
| 1833 | /* Set destination address and psm */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1834 | bacpy(&chan->dst, dst); |
Marcel Holtmann | 4f1654e | 2013-10-13 08:50:41 -0700 | [diff] [blame] | 1835 | chan->dst_type = dst_type; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1836 | |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1837 | chan->psm = psm; |
| 1838 | chan->dcid = cid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1840 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1841 | |
Johan Hedberg | f224ca5 | 2013-04-29 19:35:36 +0300 | [diff] [blame] | 1842 | if (bdaddr_type_is_le(dst_type)) |
Andre Guedes | 8e9f989 | 2012-04-24 21:02:55 -0300 | [diff] [blame] | 1843 | hcon = hci_connect(hdev, LE_LINK, dst, dst_type, |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 1844 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1845 | else |
Andre Guedes | 8e9f989 | 2012-04-24 21:02:55 -0300 | [diff] [blame] | 1846 | hcon = hci_connect(hdev, ACL_LINK, dst, dst_type, |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 1847 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1848 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1849 | if (IS_ERR(hcon)) { |
| 1850 | err = PTR_ERR(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | goto done; |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 1854 | conn = l2cap_conn_add(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | if (!conn) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 1856 | hci_conn_drop(hcon); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1857 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | goto done; |
| 1859 | } |
| 1860 | |
Johan Hedberg | 141d570 | 2013-04-29 19:35:37 +0300 | [diff] [blame] | 1861 | if (cid && __l2cap_get_chan_by_dcid(conn, cid)) { |
| 1862 | hci_conn_drop(hcon); |
| 1863 | err = -EBUSY; |
| 1864 | goto done; |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1865 | } |
| 1866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | /* Update source addr of the socket */ |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 1868 | bacpy(&chan->src, &hcon->src); |
Marcel Holtmann | 4f1654e | 2013-10-13 08:50:41 -0700 | [diff] [blame] | 1869 | chan->src_type = bdaddr_type(hcon, hcon->src_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1871 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1872 | l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1873 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1874 | |
Johan Hedberg | 5ee9891 | 2013-04-29 19:35:43 +0300 | [diff] [blame] | 1875 | /* l2cap_chan_add takes its own ref so we can drop this one */ |
| 1876 | hci_conn_drop(hcon); |
| 1877 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1878 | l2cap_state_change(chan, BT_CONNECT); |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1879 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | |
| 1881 | if (hcon->state == BT_CONNECTED) { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1882 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1883 | __clear_chan_timer(chan); |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1884 | if (l2cap_chan_check_security(chan)) |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1885 | l2cap_state_change(chan, BT_CONNECTED); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1886 | } else |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1887 | l2cap_do_start(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | } |
| 1889 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1890 | err = 0; |
| 1891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1893 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1894 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | hci_dev_put(hdev); |
| 1896 | return err; |
| 1897 | } |
| 1898 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1899 | int __l2cap_wait_ack(struct sock *sk) |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1900 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1901 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1902 | DECLARE_WAITQUEUE(wait, current); |
| 1903 | int err = 0; |
| 1904 | int timeo = HZ/5; |
| 1905 | |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1906 | add_wait_queue(sk_sleep(sk), &wait); |
Peter Hurley | a71a0cf | 2011-07-25 18:36:26 -0400 | [diff] [blame] | 1907 | set_current_state(TASK_INTERRUPTIBLE); |
| 1908 | while (chan->unacked_frames > 0 && chan->conn) { |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1909 | if (!timeo) |
| 1910 | timeo = HZ/5; |
| 1911 | |
| 1912 | if (signal_pending(current)) { |
| 1913 | err = sock_intr_errno(timeo); |
| 1914 | break; |
| 1915 | } |
| 1916 | |
| 1917 | release_sock(sk); |
| 1918 | timeo = schedule_timeout(timeo); |
| 1919 | lock_sock(sk); |
Peter Hurley | a71a0cf | 2011-07-25 18:36:26 -0400 | [diff] [blame] | 1920 | set_current_state(TASK_INTERRUPTIBLE); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1921 | |
| 1922 | err = sock_error(sk); |
| 1923 | if (err) |
| 1924 | break; |
| 1925 | } |
| 1926 | set_current_state(TASK_RUNNING); |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1927 | remove_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1928 | return err; |
| 1929 | } |
| 1930 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1931 | static void l2cap_monitor_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1932 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1933 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1934 | monitor_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1935 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1936 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1937 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1938 | l2cap_chan_lock(chan); |
| 1939 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1940 | if (!chan->conn) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1941 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1942 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1943 | return; |
| 1944 | } |
| 1945 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1946 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1947 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1948 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1949 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1950 | } |
| 1951 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1952 | static void l2cap_retrans_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1953 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1954 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1955 | retrans_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1956 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1957 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1958 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1959 | l2cap_chan_lock(chan); |
| 1960 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1961 | if (!chan->conn) { |
| 1962 | l2cap_chan_unlock(chan); |
| 1963 | l2cap_chan_put(chan); |
| 1964 | return; |
| 1965 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1966 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1967 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1968 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1969 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1970 | } |
| 1971 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 1972 | static void l2cap_streaming_send(struct l2cap_chan *chan, |
| 1973 | struct sk_buff_head *skbs) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1974 | { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1975 | struct sk_buff *skb; |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1976 | struct l2cap_ctrl *control; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1977 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1978 | BT_DBG("chan %p, skbs %p", chan, skbs); |
| 1979 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1980 | if (__chan_is_moving(chan)) |
| 1981 | return; |
| 1982 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1983 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 1984 | |
| 1985 | while (!skb_queue_empty(&chan->tx_q)) { |
| 1986 | |
| 1987 | skb = skb_dequeue(&chan->tx_q); |
| 1988 | |
| 1989 | bt_cb(skb)->control.retries = 1; |
| 1990 | control = &bt_cb(skb)->control; |
| 1991 | |
| 1992 | control->reqseq = 0; |
| 1993 | control->txseq = chan->next_tx_seq; |
| 1994 | |
| 1995 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1996 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1997 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1998 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 1999 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2000 | } |
| 2001 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 2002 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 2003 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2004 | BT_DBG("Sent txseq %u", control->txseq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2005 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2006 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2007 | chan->frames_sent++; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 2008 | } |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 2009 | } |
| 2010 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 2011 | static int l2cap_ertm_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2012 | { |
| 2013 | struct sk_buff *skb, *tx_skb; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2014 | struct l2cap_ctrl *control; |
| 2015 | int sent = 0; |
| 2016 | |
| 2017 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2018 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 2019 | if (chan->state != BT_CONNECTED) |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 2020 | return -ENOTCONN; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2021 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2022 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 2023 | return 0; |
| 2024 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 2025 | if (__chan_is_moving(chan)) |
| 2026 | return 0; |
| 2027 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2028 | while (chan->tx_send_head && |
| 2029 | chan->unacked_frames < chan->remote_tx_win && |
| 2030 | chan->tx_state == L2CAP_TX_STATE_XMIT) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2031 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2032 | skb = chan->tx_send_head; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2033 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2034 | bt_cb(skb)->control.retries = 1; |
| 2035 | control = &bt_cb(skb)->control; |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 2036 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 2037 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2038 | control->final = 1; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 2039 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2040 | control->reqseq = chan->buffer_seq; |
| 2041 | chan->last_acked_seq = chan->buffer_seq; |
| 2042 | control->txseq = chan->next_tx_seq; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2043 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2044 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2045 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2046 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2047 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 2048 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2049 | } |
| 2050 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2051 | /* Clone after data has been modified. Data is assumed to be |
| 2052 | read-only (for locking purposes) on cloned sk_buffs. |
| 2053 | */ |
| 2054 | tx_skb = skb_clone(skb, GFP_KERNEL); |
| 2055 | |
| 2056 | if (!tx_skb) |
| 2057 | break; |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 2058 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 2059 | __set_retrans_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2060 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2061 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2062 | chan->unacked_frames++; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 2063 | chan->frames_sent++; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2064 | sent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2065 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2066 | if (skb_queue_is_last(&chan->tx_q, skb)) |
| 2067 | chan->tx_send_head = NULL; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2068 | else |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2069 | chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2070 | |
| 2071 | l2cap_do_send(chan, tx_skb); |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2072 | BT_DBG("Sent txseq %u", control->txseq); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2073 | } |
| 2074 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2075 | BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent, |
| 2076 | chan->unacked_frames, skb_queue_len(&chan->tx_q)); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2077 | |
| 2078 | return sent; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 2079 | } |
| 2080 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2081 | static void l2cap_ertm_resend(struct l2cap_chan *chan) |
| 2082 | { |
| 2083 | struct l2cap_ctrl control; |
| 2084 | struct sk_buff *skb; |
| 2085 | struct sk_buff *tx_skb; |
| 2086 | u16 seq; |
| 2087 | |
| 2088 | BT_DBG("chan %p", chan); |
| 2089 | |
| 2090 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 2091 | return; |
| 2092 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 2093 | if (__chan_is_moving(chan)) |
| 2094 | return; |
| 2095 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2096 | while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) { |
| 2097 | seq = l2cap_seq_list_pop(&chan->retrans_list); |
| 2098 | |
| 2099 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq); |
| 2100 | if (!skb) { |
| 2101 | BT_DBG("Error: Can't retransmit seq %d, frame missing", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2102 | seq); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2103 | continue; |
| 2104 | } |
| 2105 | |
| 2106 | bt_cb(skb)->control.retries++; |
| 2107 | control = bt_cb(skb)->control; |
| 2108 | |
| 2109 | if (chan->max_tx != 0 && |
| 2110 | bt_cb(skb)->control.retries > chan->max_tx) { |
| 2111 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 2112 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2113 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2114 | break; |
| 2115 | } |
| 2116 | |
| 2117 | control.reqseq = chan->buffer_seq; |
| 2118 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
| 2119 | control.final = 1; |
| 2120 | else |
| 2121 | control.final = 0; |
| 2122 | |
| 2123 | if (skb_cloned(skb)) { |
| 2124 | /* Cloned sk_buffs are read-only, so we need a |
| 2125 | * writeable copy |
| 2126 | */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2127 | tx_skb = skb_copy(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2128 | } else { |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2129 | tx_skb = skb_clone(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | if (!tx_skb) { |
| 2133 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2134 | break; |
| 2135 | } |
| 2136 | |
| 2137 | /* Update skb contents */ |
| 2138 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 2139 | put_unaligned_le32(__pack_extended_control(&control), |
| 2140 | tx_skb->data + L2CAP_HDR_SIZE); |
| 2141 | } else { |
| 2142 | put_unaligned_le16(__pack_enhanced_control(&control), |
| 2143 | tx_skb->data + L2CAP_HDR_SIZE); |
| 2144 | } |
| 2145 | |
| 2146 | if (chan->fcs == L2CAP_FCS_CRC16) { |
| 2147 | u16 fcs = crc16(0, (u8 *) tx_skb->data, tx_skb->len); |
| 2148 | put_unaligned_le16(fcs, skb_put(tx_skb, |
| 2149 | L2CAP_FCS_SIZE)); |
| 2150 | } |
| 2151 | |
| 2152 | l2cap_do_send(chan, tx_skb); |
| 2153 | |
| 2154 | BT_DBG("Resent txseq %d", control.txseq); |
| 2155 | |
| 2156 | chan->last_acked_seq = chan->buffer_seq; |
| 2157 | } |
| 2158 | } |
| 2159 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2160 | static void l2cap_retransmit(struct l2cap_chan *chan, |
| 2161 | struct l2cap_ctrl *control) |
| 2162 | { |
| 2163 | BT_DBG("chan %p, control %p", chan, control); |
| 2164 | |
| 2165 | l2cap_seq_list_append(&chan->retrans_list, control->reqseq); |
| 2166 | l2cap_ertm_resend(chan); |
| 2167 | } |
| 2168 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2169 | static void l2cap_retransmit_all(struct l2cap_chan *chan, |
| 2170 | struct l2cap_ctrl *control) |
| 2171 | { |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2172 | struct sk_buff *skb; |
| 2173 | |
| 2174 | BT_DBG("chan %p, control %p", chan, control); |
| 2175 | |
| 2176 | if (control->poll) |
| 2177 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 2178 | |
| 2179 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2180 | |
| 2181 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 2182 | return; |
| 2183 | |
| 2184 | if (chan->unacked_frames) { |
| 2185 | skb_queue_walk(&chan->tx_q, skb) { |
| 2186 | if (bt_cb(skb)->control.txseq == control->reqseq || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2187 | skb == chan->tx_send_head) |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2188 | break; |
| 2189 | } |
| 2190 | |
| 2191 | skb_queue_walk_from(&chan->tx_q, skb) { |
| 2192 | if (skb == chan->tx_send_head) |
| 2193 | break; |
| 2194 | |
| 2195 | l2cap_seq_list_append(&chan->retrans_list, |
| 2196 | bt_cb(skb)->control.txseq); |
| 2197 | } |
| 2198 | |
| 2199 | l2cap_ertm_resend(chan); |
| 2200 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2201 | } |
| 2202 | |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2203 | static void l2cap_send_ack(struct l2cap_chan *chan) |
| 2204 | { |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2205 | struct l2cap_ctrl control; |
| 2206 | u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 2207 | chan->last_acked_seq); |
| 2208 | int threshold; |
| 2209 | |
| 2210 | BT_DBG("chan %p last_acked_seq %d buffer_seq %d", |
| 2211 | chan, chan->last_acked_seq, chan->buffer_seq); |
| 2212 | |
| 2213 | memset(&control, 0, sizeof(control)); |
| 2214 | control.sframe = 1; |
| 2215 | |
| 2216 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
| 2217 | chan->rx_state == L2CAP_RX_STATE_RECV) { |
| 2218 | __clear_ack_timer(chan); |
| 2219 | control.super = L2CAP_SUPER_RNR; |
| 2220 | control.reqseq = chan->buffer_seq; |
| 2221 | l2cap_send_sframe(chan, &control); |
| 2222 | } else { |
| 2223 | if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) { |
| 2224 | l2cap_ertm_send(chan); |
| 2225 | /* If any i-frames were sent, they included an ack */ |
| 2226 | if (chan->buffer_seq == chan->last_acked_seq) |
| 2227 | frames_to_ack = 0; |
| 2228 | } |
| 2229 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2230 | /* Ack now if the window is 3/4ths full. |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2231 | * Calculate without mul or div |
| 2232 | */ |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2233 | threshold = chan->ack_win; |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2234 | threshold += threshold << 1; |
| 2235 | threshold >>= 2; |
| 2236 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2237 | BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack, |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2238 | threshold); |
| 2239 | |
| 2240 | if (frames_to_ack >= threshold) { |
| 2241 | __clear_ack_timer(chan); |
| 2242 | control.super = L2CAP_SUPER_RR; |
| 2243 | control.reqseq = chan->buffer_seq; |
| 2244 | l2cap_send_sframe(chan, &control); |
| 2245 | frames_to_ack = 0; |
| 2246 | } |
| 2247 | |
| 2248 | if (frames_to_ack) |
| 2249 | __set_ack_timer(chan); |
| 2250 | } |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2251 | } |
| 2252 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2253 | static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, |
| 2254 | struct msghdr *msg, int len, |
| 2255 | int count, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | { |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2257 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2258 | struct sk_buff **frag; |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2259 | int sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | |
Gustavo F. Padovan | 59203a2 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 2261 | if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2262 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | |
| 2264 | sent += count; |
| 2265 | len -= count; |
| 2266 | |
| 2267 | /* Continuation fragments (no L2CAP header) */ |
| 2268 | frag = &skb_shinfo(skb)->frag_list; |
| 2269 | while (len) { |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2270 | struct sk_buff *tmp; |
| 2271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | count = min_t(unsigned int, conn->mtu, len); |
| 2273 | |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2274 | tmp = chan->ops->alloc_skb(chan, count, |
| 2275 | msg->msg_flags & MSG_DONTWAIT); |
| 2276 | if (IS_ERR(tmp)) |
| 2277 | return PTR_ERR(tmp); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2278 | |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2279 | *frag = tmp; |
| 2280 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2281 | if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) |
| 2282 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2284 | (*frag)->priority = skb->priority; |
| 2285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | sent += count; |
| 2287 | len -= count; |
| 2288 | |
Gustavo Padovan | 2d0ed3d | 2012-05-11 13:16:12 -0300 | [diff] [blame] | 2289 | skb->len += (*frag)->len; |
| 2290 | skb->data_len += (*frag)->len; |
| 2291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | frag = &(*frag)->next; |
| 2293 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | |
| 2295 | return sent; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2298 | static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2299 | struct msghdr *msg, size_t len, |
| 2300 | u32 priority) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2301 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2302 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2303 | struct sk_buff *skb; |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2304 | int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2305 | struct l2cap_hdr *lh; |
| 2306 | |
Marcel Holtmann | 43b1b8d | 2013-10-12 06:01:26 -0700 | [diff] [blame] | 2307 | BT_DBG("chan %p psm 0x%2.2x len %zu priority %u", chan, |
| 2308 | __le16_to_cpu(chan->psm), len, priority); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2309 | |
| 2310 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2311 | |
| 2312 | skb = chan->ops->alloc_skb(chan, count + hlen, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2313 | msg->msg_flags & MSG_DONTWAIT); |
| 2314 | if (IS_ERR(skb)) |
| 2315 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2316 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2317 | skb->priority = priority; |
| 2318 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2319 | /* Create L2CAP header */ |
| 2320 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2321 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | daf6a78c | 2012-05-03 10:55:52 +0300 | [diff] [blame] | 2322 | lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE); |
Marcel Holtmann | 43b1b8d | 2013-10-12 06:01:26 -0700 | [diff] [blame] | 2323 | put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2324 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2325 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2326 | if (unlikely(err < 0)) { |
| 2327 | kfree_skb(skb); |
| 2328 | return ERR_PTR(err); |
| 2329 | } |
| 2330 | return skb; |
| 2331 | } |
| 2332 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2333 | static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2334 | struct msghdr *msg, size_t len, |
| 2335 | u32 priority) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2336 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2337 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2338 | struct sk_buff *skb; |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2339 | int err, count; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2340 | struct l2cap_hdr *lh; |
| 2341 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2342 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2343 | |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2344 | count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2345 | |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2346 | skb = chan->ops->alloc_skb(chan, count + L2CAP_HDR_SIZE, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2347 | msg->msg_flags & MSG_DONTWAIT); |
| 2348 | if (IS_ERR(skb)) |
| 2349 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2350 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2351 | skb->priority = priority; |
| 2352 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2353 | /* Create L2CAP header */ |
| 2354 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2355 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo Padovan | 6ff9b5e | 2012-05-02 11:56:17 -0300 | [diff] [blame] | 2356 | lh->len = cpu_to_le16(len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2357 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2358 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2359 | if (unlikely(err < 0)) { |
| 2360 | kfree_skb(skb); |
| 2361 | return ERR_PTR(err); |
| 2362 | } |
| 2363 | return skb; |
| 2364 | } |
| 2365 | |
Luiz Augusto von Dentz | ab0ff76 | 2011-09-12 20:00:50 +0300 | [diff] [blame] | 2366 | static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2367 | struct msghdr *msg, size_t len, |
| 2368 | u16 sdulen) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2369 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2370 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2371 | struct sk_buff *skb; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2372 | int err, count, hlen; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2373 | struct l2cap_hdr *lh; |
| 2374 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2375 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2376 | |
Gustavo F. Padovan | 0ee0d20 | 2010-05-01 16:15:41 -0300 | [diff] [blame] | 2377 | if (!conn) |
| 2378 | return ERR_PTR(-ENOTCONN); |
| 2379 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2380 | hlen = __ertm_hdr_size(chan); |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2381 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2382 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2383 | hlen += L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2384 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2385 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2386 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2387 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2388 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2389 | |
| 2390 | skb = chan->ops->alloc_skb(chan, count + hlen, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2391 | msg->msg_flags & MSG_DONTWAIT); |
| 2392 | if (IS_ERR(skb)) |
| 2393 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2394 | |
| 2395 | /* Create L2CAP header */ |
| 2396 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2397 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2398 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2399 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2400 | /* Control header is populated later */ |
| 2401 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 2402 | put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 2403 | else |
| 2404 | put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2405 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2406 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2407 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2408 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2409 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2410 | if (unlikely(err < 0)) { |
| 2411 | kfree_skb(skb); |
| 2412 | return ERR_PTR(err); |
| 2413 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2414 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2415 | bt_cb(skb)->control.fcs = chan->fcs; |
Mat Martineau | 3ce3514 | 2012-04-25 16:36:14 -0700 | [diff] [blame] | 2416 | bt_cb(skb)->control.retries = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2417 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | } |
| 2419 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2420 | static int l2cap_segment_sdu(struct l2cap_chan *chan, |
| 2421 | struct sk_buff_head *seg_queue, |
| 2422 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2423 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2424 | struct sk_buff *skb; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2425 | u16 sdu_len; |
| 2426 | size_t pdu_len; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2427 | u8 sar; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2428 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2429 | BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2430 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2431 | /* It is critical that ERTM PDUs fit in a single HCI fragment, |
| 2432 | * so fragmented skbs are not used. The HCI layer's handling |
| 2433 | * of fragmented skbs is not compatible with ERTM's queueing. |
| 2434 | */ |
| 2435 | |
| 2436 | /* PDU size is derived from the HCI MTU */ |
| 2437 | pdu_len = chan->conn->mtu; |
| 2438 | |
Mat Martineau | a549574 | 2012-10-23 15:24:21 -0700 | [diff] [blame] | 2439 | /* Constrain PDU size for BR/EDR connections */ |
| 2440 | if (!chan->hs_hcon) |
| 2441 | pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2442 | |
| 2443 | /* Adjust for largest possible L2CAP overhead. */ |
Gustavo Padovan | 35d401d | 2012-05-25 18:57:05 -0300 | [diff] [blame] | 2444 | if (chan->fcs) |
| 2445 | pdu_len -= L2CAP_FCS_SIZE; |
| 2446 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2447 | pdu_len -= __ertm_hdr_size(chan); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2448 | |
| 2449 | /* Remote device may have requested smaller PDUs */ |
| 2450 | pdu_len = min_t(size_t, pdu_len, chan->remote_mps); |
| 2451 | |
| 2452 | if (len <= pdu_len) { |
| 2453 | sar = L2CAP_SAR_UNSEGMENTED; |
| 2454 | sdu_len = 0; |
| 2455 | pdu_len = len; |
| 2456 | } else { |
| 2457 | sar = L2CAP_SAR_START; |
| 2458 | sdu_len = len; |
| 2459 | pdu_len -= L2CAP_SDULEN_SIZE; |
| 2460 | } |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2461 | |
| 2462 | while (len > 0) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2463 | skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2464 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2465 | if (IS_ERR(skb)) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2466 | __skb_queue_purge(seg_queue); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2467 | return PTR_ERR(skb); |
| 2468 | } |
| 2469 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2470 | bt_cb(skb)->control.sar = sar; |
| 2471 | __skb_queue_tail(seg_queue, skb); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2472 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2473 | len -= pdu_len; |
| 2474 | if (sdu_len) { |
| 2475 | sdu_len = 0; |
| 2476 | pdu_len += L2CAP_SDULEN_SIZE; |
| 2477 | } |
| 2478 | |
| 2479 | if (len <= pdu_len) { |
| 2480 | sar = L2CAP_SAR_END; |
| 2481 | pdu_len = len; |
| 2482 | } else { |
| 2483 | sar = L2CAP_SAR_CONTINUE; |
| 2484 | } |
| 2485 | } |
| 2486 | |
Gustavo Padovan | f0f6279 | 2012-05-29 13:29:17 -0300 | [diff] [blame] | 2487 | return 0; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2488 | } |
| 2489 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2490 | int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2491 | u32 priority) |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2492 | { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2493 | struct sk_buff *skb; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2494 | int err; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2495 | struct sk_buff_head seg_queue; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2496 | |
| 2497 | /* Connectionless channel */ |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2498 | if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2499 | skb = l2cap_create_connless_pdu(chan, msg, len, priority); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2500 | if (IS_ERR(skb)) |
| 2501 | return PTR_ERR(skb); |
| 2502 | |
| 2503 | l2cap_do_send(chan, skb); |
| 2504 | return len; |
| 2505 | } |
| 2506 | |
| 2507 | switch (chan->mode) { |
| 2508 | case L2CAP_MODE_BASIC: |
| 2509 | /* Check outgoing MTU */ |
| 2510 | if (len > chan->omtu) |
| 2511 | return -EMSGSIZE; |
| 2512 | |
| 2513 | /* Create a basic PDU */ |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2514 | skb = l2cap_create_basic_pdu(chan, msg, len, priority); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2515 | if (IS_ERR(skb)) |
| 2516 | return PTR_ERR(skb); |
| 2517 | |
| 2518 | l2cap_do_send(chan, skb); |
| 2519 | err = len; |
| 2520 | break; |
| 2521 | |
| 2522 | case L2CAP_MODE_ERTM: |
| 2523 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2524 | /* Check outgoing MTU */ |
| 2525 | if (len > chan->omtu) { |
| 2526 | err = -EMSGSIZE; |
| 2527 | break; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2528 | } |
| 2529 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2530 | __skb_queue_head_init(&seg_queue); |
| 2531 | |
| 2532 | /* Do segmentation before calling in to the state machine, |
| 2533 | * since it's possible to block while waiting for memory |
| 2534 | * allocation. |
| 2535 | */ |
| 2536 | err = l2cap_segment_sdu(chan, &seg_queue, msg, len); |
| 2537 | |
| 2538 | /* The channel could have been closed while segmenting, |
| 2539 | * check that it is still connected. |
| 2540 | */ |
| 2541 | if (chan->state != BT_CONNECTED) { |
| 2542 | __skb_queue_purge(&seg_queue); |
| 2543 | err = -ENOTCONN; |
| 2544 | } |
| 2545 | |
| 2546 | if (err) |
| 2547 | break; |
| 2548 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2549 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2550 | l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2551 | else |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2552 | l2cap_streaming_send(chan, &seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2553 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2554 | err = len; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2555 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2556 | /* If the skbs were not queued for sending, they'll still be in |
| 2557 | * seg_queue and need to be purged. |
| 2558 | */ |
| 2559 | __skb_queue_purge(&seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2560 | break; |
| 2561 | |
| 2562 | default: |
| 2563 | BT_DBG("bad state %1.1x", chan->mode); |
| 2564 | err = -EBADFD; |
| 2565 | } |
| 2566 | |
| 2567 | return err; |
| 2568 | } |
| 2569 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2570 | static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq) |
| 2571 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2572 | struct l2cap_ctrl control; |
| 2573 | u16 seq; |
| 2574 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2575 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2576 | |
| 2577 | memset(&control, 0, sizeof(control)); |
| 2578 | control.sframe = 1; |
| 2579 | control.super = L2CAP_SUPER_SREJ; |
| 2580 | |
| 2581 | for (seq = chan->expected_tx_seq; seq != txseq; |
| 2582 | seq = __next_seq(chan, seq)) { |
| 2583 | if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) { |
| 2584 | control.reqseq = seq; |
| 2585 | l2cap_send_sframe(chan, &control); |
| 2586 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2587 | } |
| 2588 | } |
| 2589 | |
| 2590 | chan->expected_tx_seq = __next_seq(chan, txseq); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | static void l2cap_send_srej_tail(struct l2cap_chan *chan) |
| 2594 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2595 | struct l2cap_ctrl control; |
| 2596 | |
| 2597 | BT_DBG("chan %p", chan); |
| 2598 | |
| 2599 | if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR) |
| 2600 | return; |
| 2601 | |
| 2602 | memset(&control, 0, sizeof(control)); |
| 2603 | control.sframe = 1; |
| 2604 | control.super = L2CAP_SUPER_SREJ; |
| 2605 | control.reqseq = chan->srej_list.tail; |
| 2606 | l2cap_send_sframe(chan, &control); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq) |
| 2610 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2611 | struct l2cap_ctrl control; |
| 2612 | u16 initial_head; |
| 2613 | u16 seq; |
| 2614 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2615 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2616 | |
| 2617 | memset(&control, 0, sizeof(control)); |
| 2618 | control.sframe = 1; |
| 2619 | control.super = L2CAP_SUPER_SREJ; |
| 2620 | |
| 2621 | /* Capture initial list head to allow only one pass through the list. */ |
| 2622 | initial_head = chan->srej_list.head; |
| 2623 | |
| 2624 | do { |
| 2625 | seq = l2cap_seq_list_pop(&chan->srej_list); |
| 2626 | if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR) |
| 2627 | break; |
| 2628 | |
| 2629 | control.reqseq = seq; |
| 2630 | l2cap_send_sframe(chan, &control); |
| 2631 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2632 | } while (chan->srej_list.head != initial_head); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2635 | static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 2636 | { |
| 2637 | struct sk_buff *acked_skb; |
| 2638 | u16 ackseq; |
| 2639 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2640 | BT_DBG("chan %p, reqseq %u", chan, reqseq); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2641 | |
| 2642 | if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq) |
| 2643 | return; |
| 2644 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2645 | BT_DBG("expected_ack_seq %u, unacked_frames %u", |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2646 | chan->expected_ack_seq, chan->unacked_frames); |
| 2647 | |
| 2648 | for (ackseq = chan->expected_ack_seq; ackseq != reqseq; |
| 2649 | ackseq = __next_seq(chan, ackseq)) { |
| 2650 | |
| 2651 | acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq); |
| 2652 | if (acked_skb) { |
| 2653 | skb_unlink(acked_skb, &chan->tx_q); |
| 2654 | kfree_skb(acked_skb); |
| 2655 | chan->unacked_frames--; |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | chan->expected_ack_seq = reqseq; |
| 2660 | |
| 2661 | if (chan->unacked_frames == 0) |
| 2662 | __clear_retrans_timer(chan); |
| 2663 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2664 | BT_DBG("unacked_frames %u", chan->unacked_frames); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan) |
| 2668 | { |
| 2669 | BT_DBG("chan %p", chan); |
| 2670 | |
| 2671 | chan->expected_tx_seq = chan->buffer_seq; |
| 2672 | l2cap_seq_list_clear(&chan->srej_list); |
| 2673 | skb_queue_purge(&chan->srej_q); |
| 2674 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 2675 | } |
| 2676 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2677 | static void l2cap_tx_state_xmit(struct l2cap_chan *chan, |
| 2678 | struct l2cap_ctrl *control, |
| 2679 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2680 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2681 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2682 | event); |
| 2683 | |
| 2684 | switch (event) { |
| 2685 | case L2CAP_EV_DATA_REQUEST: |
| 2686 | if (chan->tx_send_head == NULL) |
| 2687 | chan->tx_send_head = skb_peek(skbs); |
| 2688 | |
| 2689 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2690 | l2cap_ertm_send(chan); |
| 2691 | break; |
| 2692 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2693 | BT_DBG("Enter LOCAL_BUSY"); |
| 2694 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2695 | |
| 2696 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2697 | /* The SREJ_SENT state must be aborted if we are to |
| 2698 | * enter the LOCAL_BUSY state. |
| 2699 | */ |
| 2700 | l2cap_abort_rx_srej_sent(chan); |
| 2701 | } |
| 2702 | |
| 2703 | l2cap_send_ack(chan); |
| 2704 | |
| 2705 | break; |
| 2706 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2707 | BT_DBG("Exit LOCAL_BUSY"); |
| 2708 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2709 | |
| 2710 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2711 | struct l2cap_ctrl local_control; |
| 2712 | |
| 2713 | memset(&local_control, 0, sizeof(local_control)); |
| 2714 | local_control.sframe = 1; |
| 2715 | local_control.super = L2CAP_SUPER_RR; |
| 2716 | local_control.poll = 1; |
| 2717 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2718 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2719 | |
| 2720 | chan->retry_count = 1; |
| 2721 | __set_monitor_timer(chan); |
| 2722 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2723 | } |
| 2724 | break; |
| 2725 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2726 | l2cap_process_reqseq(chan, control->reqseq); |
| 2727 | break; |
| 2728 | case L2CAP_EV_EXPLICIT_POLL: |
| 2729 | l2cap_send_rr_or_rnr(chan, 1); |
| 2730 | chan->retry_count = 1; |
| 2731 | __set_monitor_timer(chan); |
| 2732 | __clear_ack_timer(chan); |
| 2733 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2734 | break; |
| 2735 | case L2CAP_EV_RETRANS_TO: |
| 2736 | l2cap_send_rr_or_rnr(chan, 1); |
| 2737 | chan->retry_count = 1; |
| 2738 | __set_monitor_timer(chan); |
| 2739 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2740 | break; |
| 2741 | case L2CAP_EV_RECV_FBIT: |
| 2742 | /* Nothing to process */ |
| 2743 | break; |
| 2744 | default: |
| 2745 | break; |
| 2746 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2747 | } |
| 2748 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2749 | static void l2cap_tx_state_wait_f(struct l2cap_chan *chan, |
| 2750 | struct l2cap_ctrl *control, |
| 2751 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2752 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2753 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2754 | event); |
| 2755 | |
| 2756 | switch (event) { |
| 2757 | case L2CAP_EV_DATA_REQUEST: |
| 2758 | if (chan->tx_send_head == NULL) |
| 2759 | chan->tx_send_head = skb_peek(skbs); |
| 2760 | /* Queue data, but don't send. */ |
| 2761 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2762 | break; |
| 2763 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2764 | BT_DBG("Enter LOCAL_BUSY"); |
| 2765 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2766 | |
| 2767 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2768 | /* The SREJ_SENT state must be aborted if we are to |
| 2769 | * enter the LOCAL_BUSY state. |
| 2770 | */ |
| 2771 | l2cap_abort_rx_srej_sent(chan); |
| 2772 | } |
| 2773 | |
| 2774 | l2cap_send_ack(chan); |
| 2775 | |
| 2776 | break; |
| 2777 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2778 | BT_DBG("Exit LOCAL_BUSY"); |
| 2779 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2780 | |
| 2781 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2782 | struct l2cap_ctrl local_control; |
| 2783 | memset(&local_control, 0, sizeof(local_control)); |
| 2784 | local_control.sframe = 1; |
| 2785 | local_control.super = L2CAP_SUPER_RR; |
| 2786 | local_control.poll = 1; |
| 2787 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2788 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2789 | |
| 2790 | chan->retry_count = 1; |
| 2791 | __set_monitor_timer(chan); |
| 2792 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2793 | } |
| 2794 | break; |
| 2795 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2796 | l2cap_process_reqseq(chan, control->reqseq); |
| 2797 | |
| 2798 | /* Fall through */ |
| 2799 | |
| 2800 | case L2CAP_EV_RECV_FBIT: |
| 2801 | if (control && control->final) { |
| 2802 | __clear_monitor_timer(chan); |
| 2803 | if (chan->unacked_frames > 0) |
| 2804 | __set_retrans_timer(chan); |
| 2805 | chan->retry_count = 0; |
| 2806 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 2807 | BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state); |
| 2808 | } |
| 2809 | break; |
| 2810 | case L2CAP_EV_EXPLICIT_POLL: |
| 2811 | /* Ignore */ |
| 2812 | break; |
| 2813 | case L2CAP_EV_MONITOR_TO: |
| 2814 | if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) { |
| 2815 | l2cap_send_rr_or_rnr(chan, 1); |
| 2816 | __set_monitor_timer(chan); |
| 2817 | chan->retry_count++; |
| 2818 | } else { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 2819 | l2cap_send_disconn_req(chan, ECONNABORTED); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2820 | } |
| 2821 | break; |
| 2822 | default: |
| 2823 | break; |
| 2824 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2825 | } |
| 2826 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2827 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 2828 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2829 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2830 | BT_DBG("chan %p, control %p, skbs %p, event %d, state %d", |
| 2831 | chan, control, skbs, event, chan->tx_state); |
| 2832 | |
| 2833 | switch (chan->tx_state) { |
| 2834 | case L2CAP_TX_STATE_XMIT: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2835 | l2cap_tx_state_xmit(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2836 | break; |
| 2837 | case L2CAP_TX_STATE_WAIT_F: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2838 | l2cap_tx_state_wait_f(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2839 | break; |
| 2840 | default: |
| 2841 | /* Ignore event */ |
| 2842 | break; |
| 2843 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2844 | } |
| 2845 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2846 | static void l2cap_pass_to_tx(struct l2cap_chan *chan, |
| 2847 | struct l2cap_ctrl *control) |
| 2848 | { |
| 2849 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2850 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT); |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2851 | } |
| 2852 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2853 | static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan, |
| 2854 | struct l2cap_ctrl *control) |
| 2855 | { |
| 2856 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2857 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2858 | } |
| 2859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | /* Copy frame to all raw sockets on that connection */ |
| 2861 | static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) |
| 2862 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | struct sk_buff *nskb; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2864 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | |
| 2866 | BT_DBG("conn %p", conn); |
| 2867 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2868 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2869 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2870 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2871 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2872 | if (chan->chan_type != L2CAP_CHAN_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | continue; |
| 2874 | |
| 2875 | /* Don't send frame to the socket it came from */ |
| 2876 | if (skb->sk == sk) |
| 2877 | continue; |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2878 | nskb = skb_clone(skb, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 2879 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2880 | continue; |
| 2881 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 2882 | if (chan->ops->recv(chan, nskb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | kfree_skb(nskb); |
| 2884 | } |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2885 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2886 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | /* ---- L2CAP signalling commands ---- */ |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2890 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code, |
| 2891 | u8 ident, u16 dlen, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | { |
| 2893 | struct sk_buff *skb, **frag; |
| 2894 | struct l2cap_cmd_hdr *cmd; |
| 2895 | struct l2cap_hdr *lh; |
| 2896 | int len, count; |
| 2897 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2898 | BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u", |
| 2899 | conn, code, ident, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2900 | |
Anderson Lizardo | 300b962 | 2013-06-02 16:30:40 -0400 | [diff] [blame] | 2901 | if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE) |
| 2902 | return NULL; |
| 2903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2904 | len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; |
| 2905 | count = min_t(unsigned int, conn->mtu, len); |
| 2906 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2907 | skb = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | if (!skb) |
| 2909 | return NULL; |
| 2910 | |
| 2911 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2912 | lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2913 | |
| 2914 | if (conn->hcon->type == LE_LINK) |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 2915 | lh->cid = __constant_cpu_to_le16(L2CAP_CID_LE_SIGNALING); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2916 | else |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 2917 | lh->cid = __constant_cpu_to_le16(L2CAP_CID_SIGNALING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | |
| 2919 | cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); |
| 2920 | cmd->code = code; |
| 2921 | cmd->ident = ident; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2922 | cmd->len = cpu_to_le16(dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | |
| 2924 | if (dlen) { |
| 2925 | count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; |
| 2926 | memcpy(skb_put(skb, count), data, count); |
| 2927 | data += count; |
| 2928 | } |
| 2929 | |
| 2930 | len -= skb->len; |
| 2931 | |
| 2932 | /* Continuation fragments (no L2CAP header) */ |
| 2933 | frag = &skb_shinfo(skb)->frag_list; |
| 2934 | while (len) { |
| 2935 | count = min_t(unsigned int, conn->mtu, len); |
| 2936 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2937 | *frag = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2938 | if (!*frag) |
| 2939 | goto fail; |
| 2940 | |
| 2941 | memcpy(skb_put(*frag, count), data, count); |
| 2942 | |
| 2943 | len -= count; |
| 2944 | data += count; |
| 2945 | |
| 2946 | frag = &(*frag)->next; |
| 2947 | } |
| 2948 | |
| 2949 | return skb; |
| 2950 | |
| 2951 | fail: |
| 2952 | kfree_skb(skb); |
| 2953 | return NULL; |
| 2954 | } |
| 2955 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2956 | static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, |
| 2957 | unsigned long *val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | { |
| 2959 | struct l2cap_conf_opt *opt = *ptr; |
| 2960 | int len; |
| 2961 | |
| 2962 | len = L2CAP_CONF_OPT_SIZE + opt->len; |
| 2963 | *ptr += len; |
| 2964 | |
| 2965 | *type = opt->type; |
| 2966 | *olen = opt->len; |
| 2967 | |
| 2968 | switch (opt->len) { |
| 2969 | case 1: |
| 2970 | *val = *((u8 *) opt->val); |
| 2971 | break; |
| 2972 | |
| 2973 | case 2: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2974 | *val = get_unaligned_le16(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | break; |
| 2976 | |
| 2977 | case 4: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2978 | *val = get_unaligned_le32(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | break; |
| 2980 | |
| 2981 | default: |
| 2982 | *val = (unsigned long) opt->val; |
| 2983 | break; |
| 2984 | } |
| 2985 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2986 | BT_DBG("type 0x%2.2x len %u val 0x%lx", *type, opt->len, *val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | return len; |
| 2988 | } |
| 2989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) |
| 2991 | { |
| 2992 | struct l2cap_conf_opt *opt = *ptr; |
| 2993 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2994 | BT_DBG("type 0x%2.2x len %u val 0x%lx", type, len, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | |
| 2996 | opt->type = type; |
| 2997 | opt->len = len; |
| 2998 | |
| 2999 | switch (len) { |
| 3000 | case 1: |
| 3001 | *((u8 *) opt->val) = val; |
| 3002 | break; |
| 3003 | |
| 3004 | case 2: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 3005 | put_unaligned_le16(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | break; |
| 3007 | |
| 3008 | case 4: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 3009 | put_unaligned_le32(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3010 | break; |
| 3011 | |
| 3012 | default: |
| 3013 | memcpy(opt->val, (void *) val, len); |
| 3014 | break; |
| 3015 | } |
| 3016 | |
| 3017 | *ptr += L2CAP_CONF_OPT_SIZE + len; |
| 3018 | } |
| 3019 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3020 | static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) |
| 3021 | { |
| 3022 | struct l2cap_conf_efs efs; |
| 3023 | |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 3024 | switch (chan->mode) { |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3025 | case L2CAP_MODE_ERTM: |
| 3026 | efs.id = chan->local_id; |
| 3027 | efs.stype = chan->local_stype; |
| 3028 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 3029 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 3030 | efs.acc_lat = __constant_cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 3031 | efs.flush_to = __constant_cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3032 | break; |
| 3033 | |
| 3034 | case L2CAP_MODE_STREAMING: |
| 3035 | efs.id = 1; |
| 3036 | efs.stype = L2CAP_SERV_BESTEFFORT; |
| 3037 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 3038 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
| 3039 | efs.acc_lat = 0; |
| 3040 | efs.flush_to = 0; |
| 3041 | break; |
| 3042 | |
| 3043 | default: |
| 3044 | return; |
| 3045 | } |
| 3046 | |
| 3047 | l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 3048 | (unsigned long) &efs); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3049 | } |
| 3050 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3051 | static void l2cap_ack_timeout(struct work_struct *work) |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3052 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3053 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3054 | ack_timer.work); |
| 3055 | u16 frames_to_ack; |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3056 | |
Gustavo F. Padovan | 2fb9b3d | 2011-12-22 16:56:05 -0200 | [diff] [blame] | 3057 | BT_DBG("chan %p", chan); |
| 3058 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3059 | l2cap_chan_lock(chan); |
| 3060 | |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3061 | frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 3062 | chan->last_acked_seq); |
| 3063 | |
| 3064 | if (frames_to_ack) |
| 3065 | l2cap_send_rr_or_rnr(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3066 | |
| 3067 | l2cap_chan_unlock(chan); |
Szymon Janc | 09bfb2e | 2012-01-11 10:59:49 +0100 | [diff] [blame] | 3068 | l2cap_chan_put(chan); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3069 | } |
| 3070 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 3071 | int l2cap_ertm_init(struct l2cap_chan *chan) |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3072 | { |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3073 | int err; |
| 3074 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3075 | chan->next_tx_seq = 0; |
| 3076 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3077 | chan->expected_ack_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3078 | chan->unacked_frames = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3079 | chan->buffer_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3080 | chan->frames_sent = 0; |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3081 | chan->last_acked_seq = 0; |
| 3082 | chan->sdu = NULL; |
| 3083 | chan->sdu_last_frag = NULL; |
| 3084 | chan->sdu_len = 0; |
| 3085 | |
Mat Martineau | d34c34f | 2012-05-14 14:49:27 -0700 | [diff] [blame] | 3086 | skb_queue_head_init(&chan->tx_q); |
| 3087 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3088 | chan->local_amp_id = AMP_ID_BREDR; |
| 3089 | chan->move_id = AMP_ID_BREDR; |
Mat Martineau | 0833328 | 2012-10-23 15:24:06 -0700 | [diff] [blame] | 3090 | chan->move_state = L2CAP_MOVE_STABLE; |
| 3091 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 3092 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3093 | if (chan->mode != L2CAP_MODE_ERTM) |
| 3094 | return 0; |
| 3095 | |
| 3096 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 3097 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3098 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3099 | INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); |
| 3100 | INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); |
| 3101 | INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3102 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3103 | skb_queue_head_init(&chan->srej_q); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3104 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3105 | err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win); |
| 3106 | if (err < 0) |
| 3107 | return err; |
| 3108 | |
Mat Martineau | 9dc9aff | 2012-05-17 16:20:14 -0700 | [diff] [blame] | 3109 | err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win); |
| 3110 | if (err < 0) |
| 3111 | l2cap_seq_list_free(&chan->srej_list); |
| 3112 | |
| 3113 | return err; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3114 | } |
| 3115 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3116 | static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) |
| 3117 | { |
| 3118 | switch (mode) { |
| 3119 | case L2CAP_MODE_STREAMING: |
| 3120 | case L2CAP_MODE_ERTM: |
| 3121 | if (l2cap_mode_supported(mode, remote_feat_mask)) |
| 3122 | return mode; |
| 3123 | /* fall through */ |
| 3124 | default: |
| 3125 | return L2CAP_MODE_BASIC; |
| 3126 | } |
| 3127 | } |
| 3128 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3129 | static inline bool __l2cap_ews_supported(struct l2cap_conn *conn) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3130 | { |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3131 | return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3132 | } |
| 3133 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3134 | static inline bool __l2cap_efs_supported(struct l2cap_conn *conn) |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3135 | { |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3136 | return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_FLOW; |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3137 | } |
| 3138 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3139 | static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan, |
| 3140 | struct l2cap_conf_rfc *rfc) |
| 3141 | { |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3142 | if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) { |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3143 | u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to; |
| 3144 | |
| 3145 | /* Class 1 devices have must have ERTM timeouts |
| 3146 | * exceeding the Link Supervision Timeout. The |
| 3147 | * default Link Supervision Timeout for AMP |
| 3148 | * controllers is 10 seconds. |
| 3149 | * |
| 3150 | * Class 1 devices use 0xffffffff for their |
| 3151 | * best-effort flush timeout, so the clamping logic |
| 3152 | * will result in a timeout that meets the above |
| 3153 | * requirement. ERTM timeouts are 16-bit values, so |
| 3154 | * the maximum timeout is 65.535 seconds. |
| 3155 | */ |
| 3156 | |
| 3157 | /* Convert timeout to milliseconds and round */ |
| 3158 | ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000); |
| 3159 | |
| 3160 | /* This is the recommended formula for class 2 devices |
| 3161 | * that start ERTM timers when packets are sent to the |
| 3162 | * controller. |
| 3163 | */ |
| 3164 | ertm_to = 3 * ertm_to + 500; |
| 3165 | |
| 3166 | if (ertm_to > 0xffff) |
| 3167 | ertm_to = 0xffff; |
| 3168 | |
| 3169 | rfc->retrans_timeout = cpu_to_le16((u16) ertm_to); |
| 3170 | rfc->monitor_timeout = rfc->retrans_timeout; |
| 3171 | } else { |
| 3172 | rfc->retrans_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); |
| 3173 | rfc->monitor_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); |
| 3174 | } |
| 3175 | } |
| 3176 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3177 | static inline void l2cap_txwin_setup(struct l2cap_chan *chan) |
| 3178 | { |
| 3179 | if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3180 | __l2cap_ews_supported(chan->conn)) { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3181 | /* use extended control field */ |
| 3182 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3183 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
| 3184 | } else { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3185 | chan->tx_win = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3186 | L2CAP_DEFAULT_TX_WINDOW); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3187 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
| 3188 | } |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3189 | chan->ack_win = chan->tx_win; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3190 | } |
| 3191 | |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3192 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | struct l2cap_conf_req *req = data; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3195 | struct l2cap_conf_rfc rfc = { .mode = chan->mode }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3196 | void *ptr = req->data; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3197 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3198 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3199 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3201 | if (chan->num_conf_req || chan->num_conf_rsp) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3202 | goto done; |
| 3203 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3204 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3205 | case L2CAP_MODE_STREAMING: |
| 3206 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3207 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3208 | break; |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3209 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3210 | if (__l2cap_efs_supported(chan->conn)) |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3211 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3212 | |
Gustavo F. Padovan | 2ba13ed | 2010-06-09 16:39:05 -0300 | [diff] [blame] | 3213 | /* fall through */ |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3214 | default: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3215 | chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3216 | break; |
| 3217 | } |
| 3218 | |
| 3219 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3220 | if (chan->imtu != L2CAP_DEFAULT_MTU) |
| 3221 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | 7990681c | 2011-01-24 16:01:43 -0200 | [diff] [blame] | 3222 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3223 | switch (chan->mode) { |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3224 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3225 | if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3226 | !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3227 | break; |
| 3228 | |
Gustavo F. Padovan | 6254775 | 2010-06-08 20:05:31 -0300 | [diff] [blame] | 3229 | rfc.mode = L2CAP_MODE_BASIC; |
| 3230 | rfc.txwin_size = 0; |
| 3231 | rfc.max_transmit = 0; |
| 3232 | rfc.retrans_timeout = 0; |
| 3233 | rfc.monitor_timeout = 0; |
| 3234 | rfc.max_pdu_size = 0; |
| 3235 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3236 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3237 | (unsigned long) &rfc); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3238 | break; |
| 3239 | |
| 3240 | case L2CAP_MODE_ERTM: |
| 3241 | rfc.mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3242 | rfc.max_transmit = chan->max_tx; |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3243 | |
| 3244 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3245 | |
| 3246 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3247 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3248 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3249 | rfc.max_pdu_size = cpu_to_le16(size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3250 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3251 | l2cap_txwin_setup(chan); |
| 3252 | |
| 3253 | rfc.txwin_size = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3254 | L2CAP_DEFAULT_TX_WINDOW); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3255 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3256 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3257 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3258 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3259 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3260 | l2cap_add_opt_efs(&ptr, chan); |
| 3261 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3262 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3263 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3264 | chan->tx_win); |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3265 | |
| 3266 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3267 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3268 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3269 | chan->fcs = L2CAP_FCS_NONE; |
| 3270 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3271 | chan->fcs); |
| 3272 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3273 | break; |
| 3274 | |
| 3275 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 273759e | 2012-05-17 20:53:53 -0700 | [diff] [blame] | 3276 | l2cap_txwin_setup(chan); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3277 | rfc.mode = L2CAP_MODE_STREAMING; |
| 3278 | rfc.txwin_size = 0; |
| 3279 | rfc.max_transmit = 0; |
| 3280 | rfc.retrans_timeout = 0; |
| 3281 | rfc.monitor_timeout = 0; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3282 | |
| 3283 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3284 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3285 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3286 | rfc.max_pdu_size = cpu_to_le16(size); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3287 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3288 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3289 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3290 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3291 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3292 | l2cap_add_opt_efs(&ptr, chan); |
| 3293 | |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3294 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3295 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3296 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3297 | chan->fcs = L2CAP_FCS_NONE; |
| 3298 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3299 | chan->fcs); |
| 3300 | } |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3301 | break; |
| 3302 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3304 | req->dcid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 3305 | req->flags = __constant_cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3306 | |
| 3307 | return ptr - data; |
| 3308 | } |
| 3309 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3310 | static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3311 | { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3312 | struct l2cap_conf_rsp *rsp = data; |
| 3313 | void *ptr = rsp->data; |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3314 | void *req = chan->conf_req; |
| 3315 | int len = chan->conf_len; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3316 | int type, hint, olen; |
| 3317 | unsigned long val; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3318 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3319 | struct l2cap_conf_efs efs; |
| 3320 | u8 remote_efs = 0; |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3321 | u16 mtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3322 | u16 result = L2CAP_CONF_SUCCESS; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3323 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3324 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3325 | BT_DBG("chan %p", chan); |
Marcel Holtmann | 820ae1b | 2006-11-18 22:15:00 +0100 | [diff] [blame] | 3326 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3327 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3328 | len -= l2cap_get_conf_opt(&req, &type, &olen, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | |
Gustavo F. Padovan | 589d274 | 2009-04-20 01:31:07 -0300 | [diff] [blame] | 3330 | hint = type & L2CAP_CONF_HINT; |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 3331 | type &= L2CAP_CONF_MASK; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3332 | |
| 3333 | switch (type) { |
| 3334 | case L2CAP_CONF_MTU: |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3335 | mtu = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3336 | break; |
| 3337 | |
| 3338 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3339 | chan->flush_to = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3340 | break; |
| 3341 | |
| 3342 | case L2CAP_CONF_QOS: |
| 3343 | break; |
| 3344 | |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3345 | case L2CAP_CONF_RFC: |
| 3346 | if (olen == sizeof(rfc)) |
| 3347 | memcpy(&rfc, (void *) val, olen); |
| 3348 | break; |
| 3349 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3350 | case L2CAP_CONF_FCS: |
| 3351 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3352 | set_bit(CONF_RECV_NO_FCS, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3353 | break; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3354 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3355 | case L2CAP_CONF_EFS: |
| 3356 | remote_efs = 1; |
| 3357 | if (olen == sizeof(efs)) |
| 3358 | memcpy(&efs, (void *) val, olen); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3359 | break; |
| 3360 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3361 | case L2CAP_CONF_EWS: |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3362 | if (!chan->conn->hs_enabled) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3363 | return -ECONNREFUSED; |
| 3364 | |
| 3365 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
| 3366 | set_bit(CONF_EWS_RECV, &chan->conf_state); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3367 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3368 | chan->remote_tx_win = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3369 | break; |
| 3370 | |
| 3371 | default: |
| 3372 | if (hint) |
| 3373 | break; |
| 3374 | |
| 3375 | result = L2CAP_CONF_UNKNOWN; |
| 3376 | *((u8 *) ptr++) = type; |
| 3377 | break; |
| 3378 | } |
| 3379 | } |
| 3380 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3381 | if (chan->num_conf_rsp || chan->num_conf_req > 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3382 | goto done; |
| 3383 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3384 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3385 | case L2CAP_MODE_STREAMING: |
| 3386 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3387 | if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3388 | chan->mode = l2cap_select_mode(rfc.mode, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3389 | chan->conn->feat_mask); |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3390 | break; |
| 3391 | } |
| 3392 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3393 | if (remote_efs) { |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3394 | if (__l2cap_efs_supported(chan->conn)) |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3395 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3396 | else |
| 3397 | return -ECONNREFUSED; |
| 3398 | } |
| 3399 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3400 | if (chan->mode != rfc.mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3401 | return -ECONNREFUSED; |
Gustavo F. Padovan | 742e519 | 2010-06-08 19:09:48 -0300 | [diff] [blame] | 3402 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3403 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3404 | } |
| 3405 | |
| 3406 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3407 | if (chan->mode != rfc.mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3408 | result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3409 | rfc.mode = chan->mode; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3410 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3411 | if (chan->num_conf_rsp == 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3412 | return -ECONNREFUSED; |
| 3413 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3414 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3415 | (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3416 | } |
| 3417 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3418 | if (result == L2CAP_CONF_SUCCESS) { |
| 3419 | /* Configure output options and let the other side know |
| 3420 | * which ones we don't like. */ |
| 3421 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3422 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
| 3423 | result = L2CAP_CONF_UNACCEPT; |
| 3424 | else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3425 | chan->omtu = mtu; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3426 | set_bit(CONF_MTU_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3427 | } |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3428 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3429 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3430 | if (remote_efs) { |
| 3431 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3432 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3433 | efs.stype != chan->local_stype) { |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3434 | |
| 3435 | result = L2CAP_CONF_UNACCEPT; |
| 3436 | |
| 3437 | if (chan->num_conf_req >= 1) |
| 3438 | return -ECONNREFUSED; |
| 3439 | |
| 3440 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3441 | sizeof(efs), |
| 3442 | (unsigned long) &efs); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3443 | } else { |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3444 | /* Send PENDING Conf Rsp */ |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3445 | result = L2CAP_CONF_PENDING; |
| 3446 | set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3447 | } |
| 3448 | } |
| 3449 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3450 | switch (rfc.mode) { |
| 3451 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3452 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3453 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3454 | break; |
| 3455 | |
| 3456 | case L2CAP_MODE_ERTM: |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3457 | if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) |
| 3458 | chan->remote_tx_win = rfc.txwin_size; |
| 3459 | else |
| 3460 | rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; |
| 3461 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 3462 | chan->remote_max_tx = rfc.max_transmit; |
Mat Martineau | 86b1b26 | 2010-08-05 15:54:22 -0700 | [diff] [blame] | 3463 | |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3464 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3465 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3466 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3467 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3468 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3469 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3470 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3471 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3472 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3473 | |
| 3474 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3475 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3476 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3477 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3478 | chan->remote_id = efs.id; |
| 3479 | chan->remote_stype = efs.stype; |
| 3480 | chan->remote_msdu = le16_to_cpu(efs.msdu); |
| 3481 | chan->remote_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3482 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3483 | chan->remote_acc_lat = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3484 | le32_to_cpu(efs.acc_lat); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3485 | chan->remote_sdu_itime = |
| 3486 | le32_to_cpu(efs.sdu_itime); |
| 3487 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3488 | sizeof(efs), |
| 3489 | (unsigned long) &efs); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3490 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3491 | break; |
| 3492 | |
| 3493 | case L2CAP_MODE_STREAMING: |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3494 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3495 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3496 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3497 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3498 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3499 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3500 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3501 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3502 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3503 | (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3504 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3505 | break; |
| 3506 | |
| 3507 | default: |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3508 | result = L2CAP_CONF_UNACCEPT; |
| 3509 | |
| 3510 | memset(&rfc, 0, sizeof(rfc)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3511 | rfc.mode = chan->mode; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3512 | } |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3513 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3514 | if (result == L2CAP_CONF_SUCCESS) |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3515 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3516 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3517 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3518 | rsp->result = cpu_to_le16(result); |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 3519 | rsp->flags = __constant_cpu_to_le16(0); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3520 | |
| 3521 | return ptr - data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3522 | } |
| 3523 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3524 | static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, |
| 3525 | void *data, u16 *result) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3526 | { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3527 | struct l2cap_conf_req *req = data; |
| 3528 | void *ptr = req->data; |
| 3529 | int type, olen; |
| 3530 | unsigned long val; |
Mat Martineau | 36e999a | 2011-12-08 17:23:21 -0800 | [diff] [blame] | 3531 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3532 | struct l2cap_conf_efs efs; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3533 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3534 | 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] | 3535 | |
| 3536 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3537 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3538 | |
| 3539 | switch (type) { |
| 3540 | case L2CAP_CONF_MTU: |
| 3541 | if (val < L2CAP_DEFAULT_MIN_MTU) { |
| 3542 | *result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3543 | chan->imtu = L2CAP_DEFAULT_MIN_MTU; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3544 | } else |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3545 | chan->imtu = val; |
| 3546 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3547 | break; |
| 3548 | |
| 3549 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3550 | chan->flush_to = val; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3551 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3552 | 2, chan->flush_to); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3553 | break; |
| 3554 | |
| 3555 | case L2CAP_CONF_RFC: |
| 3556 | if (olen == sizeof(rfc)) |
| 3557 | memcpy(&rfc, (void *)val, olen); |
| 3558 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3559 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3560 | rfc.mode != chan->mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3561 | return -ECONNREFUSED; |
| 3562 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3563 | chan->fcs = 0; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3564 | |
| 3565 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3566 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3567 | break; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3568 | |
| 3569 | case L2CAP_CONF_EWS: |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3570 | chan->ack_win = min_t(u16, val, chan->ack_win); |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3571 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3572 | chan->tx_win); |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3573 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3574 | |
| 3575 | case L2CAP_CONF_EFS: |
| 3576 | if (olen == sizeof(efs)) |
| 3577 | memcpy(&efs, (void *)val, olen); |
| 3578 | |
| 3579 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3580 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3581 | efs.stype != chan->local_stype) |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3582 | return -ECONNREFUSED; |
| 3583 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3584 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs), |
| 3585 | (unsigned long) &efs); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3586 | break; |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3587 | |
| 3588 | case L2CAP_CONF_FCS: |
| 3589 | if (*result == L2CAP_CONF_PENDING) |
| 3590 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3591 | set_bit(CONF_RECV_NO_FCS, |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3592 | &chan->conf_state); |
| 3593 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3594 | } |
| 3595 | } |
| 3596 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3597 | if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3598 | return -ECONNREFUSED; |
| 3599 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3600 | chan->mode = rfc.mode; |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3601 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3602 | if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3603 | switch (rfc.mode) { |
| 3604 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3605 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3606 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 3607 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3608 | if (!test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3609 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3610 | rfc.txwin_size); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3611 | |
| 3612 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3613 | chan->local_msdu = le16_to_cpu(efs.msdu); |
| 3614 | chan->local_sdu_itime = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3615 | le32_to_cpu(efs.sdu_itime); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3616 | chan->local_acc_lat = le32_to_cpu(efs.acc_lat); |
| 3617 | chan->local_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3618 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3619 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3620 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3621 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3622 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3623 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3624 | } |
| 3625 | } |
| 3626 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3627 | req->dcid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 3628 | req->flags = __constant_cpu_to_le16(0); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3629 | |
| 3630 | return ptr - data; |
| 3631 | } |
| 3632 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3633 | static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, |
| 3634 | u16 result, u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | { |
| 3636 | struct l2cap_conf_rsp *rsp = data; |
| 3637 | void *ptr = rsp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3638 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3639 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3641 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3642 | rsp->result = cpu_to_le16(result); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3643 | rsp->flags = cpu_to_le16(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3644 | |
| 3645 | return ptr - data; |
| 3646 | } |
| 3647 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3648 | void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3649 | { |
| 3650 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3651 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3652 | u8 buf[128]; |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3653 | u8 rsp_code; |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3654 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3655 | rsp.scid = cpu_to_le16(chan->dcid); |
| 3656 | rsp.dcid = cpu_to_le16(chan->scid); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 3657 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS); |
| 3658 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3659 | |
| 3660 | if (chan->hs_hcon) |
| 3661 | rsp_code = L2CAP_CREATE_CHAN_RSP; |
| 3662 | else |
| 3663 | rsp_code = L2CAP_CONN_RSP; |
| 3664 | |
| 3665 | BT_DBG("chan %p rsp_code %u", chan, rsp_code); |
| 3666 | |
| 3667 | l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3668 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3669 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3670 | return; |
| 3671 | |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3672 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3673 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3674 | chan->num_conf_req++; |
| 3675 | } |
| 3676 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3677 | 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] | 3678 | { |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3679 | int type, olen; |
| 3680 | unsigned long val; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3681 | /* Use sane default values in case a misbehaving remote device |
| 3682 | * did not send an RFC or extended window size option. |
| 3683 | */ |
| 3684 | u16 txwin_ext = chan->ack_win; |
| 3685 | struct l2cap_conf_rfc rfc = { |
| 3686 | .mode = chan->mode, |
| 3687 | .retrans_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO), |
| 3688 | .monitor_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO), |
| 3689 | .max_pdu_size = cpu_to_le16(chan->imtu), |
| 3690 | .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW), |
| 3691 | }; |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3692 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3693 | BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3694 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3695 | if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3696 | return; |
| 3697 | |
| 3698 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3699 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3700 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3701 | switch (type) { |
| 3702 | case L2CAP_CONF_RFC: |
| 3703 | if (olen == sizeof(rfc)) |
| 3704 | memcpy(&rfc, (void *)val, olen); |
Szymon Janc | 8f321f8 | 2012-06-08 11:33:33 +0200 | [diff] [blame] | 3705 | break; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3706 | case L2CAP_CONF_EWS: |
| 3707 | txwin_ext = val; |
| 3708 | break; |
| 3709 | } |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3710 | } |
| 3711 | |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3712 | switch (rfc.mode) { |
| 3713 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3714 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3715 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3716 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
| 3717 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3718 | chan->ack_win = min_t(u16, chan->ack_win, txwin_ext); |
| 3719 | else |
| 3720 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3721 | rfc.txwin_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3722 | break; |
| 3723 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3724 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3725 | } |
| 3726 | } |
| 3727 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3728 | static inline int l2cap_command_rej(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3729 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3730 | u8 *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3731 | { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3732 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3733 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3734 | if (cmd_len < sizeof(*rej)) |
| 3735 | return -EPROTO; |
| 3736 | |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3737 | if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3738 | return 0; |
| 3739 | |
| 3740 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3741 | cmd->ident == conn->info_ident) { |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 3742 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3743 | |
| 3744 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3745 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3746 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3747 | l2cap_conn_start(conn); |
| 3748 | } |
| 3749 | |
| 3750 | return 0; |
| 3751 | } |
| 3752 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3753 | static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn, |
| 3754 | struct l2cap_cmd_hdr *cmd, |
| 3755 | u8 *data, u8 rsp_code, u8 amp_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3757 | struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; |
| 3758 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3759 | struct l2cap_chan *chan = NULL, *pchan; |
Nathan Holstein | d793fe8 | 2010-10-15 11:54:02 -0400 | [diff] [blame] | 3760 | struct sock *parent, *sk = NULL; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3761 | int result, status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3762 | |
| 3763 | u16 dcid = 0, scid = __le16_to_cpu(req->scid); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3764 | __le16 psm = req->psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 3766 | 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] | 3767 | |
| 3768 | /* Check if we have socket listening on psm */ |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 3769 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src, |
Marcel Holtmann | 98e0f7e | 2013-10-13 02:23:40 -0700 | [diff] [blame] | 3770 | &conn->hcon->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3771 | if (!pchan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | result = L2CAP_CR_BAD_PSM; |
| 3773 | goto sendresp; |
| 3774 | } |
| 3775 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3776 | parent = pchan->sk; |
| 3777 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3778 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 3779 | lock_sock(parent); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 3780 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3781 | /* Check if the ACL is secure enough (if not SDP) */ |
Andrei Emeltchenko | 2983fd6 | 2012-05-24 15:42:50 +0300 | [diff] [blame] | 3782 | if (psm != __constant_cpu_to_le16(L2CAP_PSM_SDP) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3783 | !hci_conn_check_link_mode(conn->hcon)) { |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 3784 | conn->disc_reason = HCI_ERROR_AUTH_FAILURE; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3785 | result = L2CAP_CR_SEC_BLOCK; |
| 3786 | goto response; |
| 3787 | } |
| 3788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | result = L2CAP_CR_NO_MEM; |
| 3790 | |
Gustavo Padovan | 2dfa100 | 2012-05-27 22:27:58 -0300 | [diff] [blame] | 3791 | /* Check if we already have channel with that dcid */ |
| 3792 | if (__l2cap_get_chan_by_dcid(conn, scid)) |
| 3793 | goto response; |
| 3794 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 3795 | chan = pchan->ops->new_connection(pchan); |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 3796 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3797 | goto response; |
| 3798 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 3799 | sk = chan->sk; |
| 3800 | |
Syam Sidhardhan | 330b6c1 | 2013-08-06 01:59:12 +0900 | [diff] [blame] | 3801 | /* For certain devices (ex: HID mouse), support for authentication, |
| 3802 | * pairing and bonding is optional. For such devices, inorder to avoid |
| 3803 | * the ACL alive for too long after L2CAP disconnection, reset the ACL |
| 3804 | * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect. |
| 3805 | */ |
| 3806 | conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT; |
| 3807 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 3808 | bacpy(&chan->src, &conn->hcon->src); |
| 3809 | bacpy(&chan->dst, &conn->hcon->dst); |
Marcel Holtmann | 4f1654e | 2013-10-13 08:50:41 -0700 | [diff] [blame] | 3810 | chan->src_type = bdaddr_type(conn->hcon, conn->hcon->src_type); |
| 3811 | chan->dst_type = bdaddr_type(conn->hcon, conn->hcon->dst_type); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3812 | chan->psm = psm; |
| 3813 | chan->dcid = scid; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3814 | chan->local_amp_id = amp_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3815 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3816 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3817 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3818 | dcid = chan->scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 3820 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3821 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3822 | chan->ident = cmd->ident; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3823 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3824 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 3825 | if (l2cap_chan_check_security(chan)) { |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 3826 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 3827 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3828 | result = L2CAP_CR_PEND; |
| 3829 | status = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 3830 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3831 | } else { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3832 | /* Force pending result for AMP controllers. |
| 3833 | * The connection will succeed after the |
| 3834 | * physical link is up. |
| 3835 | */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3836 | if (amp_id == AMP_ID_BREDR) { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3837 | __l2cap_state_change(chan, BT_CONFIG); |
| 3838 | result = L2CAP_CR_SUCCESS; |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3839 | } else { |
| 3840 | __l2cap_state_change(chan, BT_CONNECT2); |
| 3841 | result = L2CAP_CR_PEND; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3842 | } |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3843 | status = L2CAP_CS_NO_INFO; |
| 3844 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3845 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 3846 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3847 | result = L2CAP_CR_PEND; |
| 3848 | status = L2CAP_CS_AUTHEN_PEND; |
| 3849 | } |
| 3850 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 3851 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3852 | result = L2CAP_CR_PEND; |
| 3853 | status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3854 | } |
| 3855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3856 | response: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 3857 | release_sock(parent); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3858 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3859 | |
| 3860 | sendresp: |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3861 | rsp.scid = cpu_to_le16(scid); |
| 3862 | rsp.dcid = cpu_to_le16(dcid); |
| 3863 | rsp.result = cpu_to_le16(result); |
| 3864 | rsp.status = cpu_to_le16(status); |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3865 | l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3866 | |
| 3867 | if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { |
| 3868 | struct l2cap_info_req info; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 3869 | info.type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3870 | |
| 3871 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 3872 | conn->info_ident = l2cap_get_ident(conn); |
| 3873 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 3874 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3875 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3876 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 3877 | sizeof(info), &info); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3878 | } |
| 3879 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3880 | if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3881 | result == L2CAP_CR_SUCCESS) { |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3882 | u8 buf[128]; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3883 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3884 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3885 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3886 | chan->num_conf_req++; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3887 | } |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3888 | |
| 3889 | return chan; |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3890 | } |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3891 | |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3892 | static int l2cap_connect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3893 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3894 | { |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3895 | struct hci_dev *hdev = conn->hcon->hdev; |
| 3896 | struct hci_conn *hcon = conn->hcon; |
| 3897 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3898 | if (cmd_len < sizeof(struct l2cap_conn_req)) |
| 3899 | return -EPROTO; |
| 3900 | |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3901 | hci_dev_lock(hdev); |
| 3902 | if (test_bit(HCI_MGMT, &hdev->dev_flags) && |
| 3903 | !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags)) |
| 3904 | mgmt_device_connected(hdev, &hcon->dst, hcon->type, |
| 3905 | hcon->dst_type, 0, NULL, 0, |
| 3906 | hcon->dev_class); |
| 3907 | hci_dev_unlock(hdev); |
| 3908 | |
Gustavo Padovan | 300229f | 2012-10-12 19:40:40 +0800 | [diff] [blame] | 3909 | l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3910 | return 0; |
| 3911 | } |
| 3912 | |
Mat Martineau | 5909cf3 | 2012-10-23 15:24:08 -0700 | [diff] [blame] | 3913 | static int l2cap_connect_create_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3914 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3915 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3916 | { |
| 3917 | struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; |
| 3918 | u16 scid, dcid, result, status; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3919 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3920 | u8 req[128]; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3921 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3922 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3923 | if (cmd_len < sizeof(*rsp)) |
| 3924 | return -EPROTO; |
| 3925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3926 | scid = __le16_to_cpu(rsp->scid); |
| 3927 | dcid = __le16_to_cpu(rsp->dcid); |
| 3928 | result = __le16_to_cpu(rsp->result); |
| 3929 | status = __le16_to_cpu(rsp->status); |
| 3930 | |
Andrei Emeltchenko | 1b009c9 | 2012-02-21 12:54:54 +0200 | [diff] [blame] | 3931 | 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] | 3932 | dcid, scid, result, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3934 | mutex_lock(&conn->chan_lock); |
| 3935 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3936 | if (scid) { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3937 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 3938 | if (!chan) { |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 3939 | err = -EBADSLT; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3940 | goto unlock; |
| 3941 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3942 | } else { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3943 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 3944 | if (!chan) { |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 3945 | err = -EBADSLT; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3946 | goto unlock; |
| 3947 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | } |
| 3949 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3950 | err = 0; |
| 3951 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3952 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3954 | switch (result) { |
| 3955 | case L2CAP_CR_SUCCESS: |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 3956 | l2cap_state_change(chan, BT_CONFIG); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3957 | chan->ident = 0; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3958 | chan->dcid = dcid; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3959 | clear_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 3960 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3961 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3962 | break; |
| 3963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3964 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3965 | l2cap_build_conf_req(chan, req), req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3966 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | break; |
| 3968 | |
| 3969 | case L2CAP_CR_PEND: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3970 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3971 | break; |
| 3972 | |
| 3973 | default: |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3974 | l2cap_chan_del(chan, ECONNREFUSED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3975 | break; |
| 3976 | } |
| 3977 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3978 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3979 | |
| 3980 | unlock: |
| 3981 | mutex_unlock(&conn->chan_lock); |
| 3982 | |
| 3983 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3984 | } |
| 3985 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3986 | static inline void set_default_fcs(struct l2cap_chan *chan) |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 3987 | { |
| 3988 | /* FCS is enabled only in ERTM or streaming mode, if one or both |
| 3989 | * sides request it. |
| 3990 | */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3991 | if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3992 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3993 | else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3994 | chan->fcs = L2CAP_FCS_CRC16; |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 3995 | } |
| 3996 | |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 3997 | static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data, |
| 3998 | u8 ident, u16 flags) |
| 3999 | { |
| 4000 | struct l2cap_conn *conn = chan->conn; |
| 4001 | |
| 4002 | BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident, |
| 4003 | flags); |
| 4004 | |
| 4005 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
| 4006 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
| 4007 | |
| 4008 | l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP, |
| 4009 | l2cap_build_conf_rsp(chan, data, |
| 4010 | L2CAP_CONF_SUCCESS, flags), data); |
| 4011 | } |
| 4012 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4013 | static inline int l2cap_config_req(struct l2cap_conn *conn, |
| 4014 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4015 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | { |
| 4017 | struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; |
| 4018 | u16 dcid, flags; |
| 4019 | u8 rsp[64]; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4020 | struct l2cap_chan *chan; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4021 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4022 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4023 | if (cmd_len < sizeof(*req)) |
| 4024 | return -EPROTO; |
| 4025 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4026 | dcid = __le16_to_cpu(req->dcid); |
| 4027 | flags = __le16_to_cpu(req->flags); |
| 4028 | |
| 4029 | BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); |
| 4030 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4031 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4032 | if (!chan) |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 4033 | return -EBADSLT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4034 | |
David S. Miller | 033b114 | 2011-07-21 13:38:42 -0700 | [diff] [blame] | 4035 | if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 4036 | struct l2cap_cmd_rej_cid rej; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 4037 | |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4038 | rej.reason = __constant_cpu_to_le16(L2CAP_REJ_INVALID_CID); |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 4039 | rej.scid = cpu_to_le16(chan->scid); |
| 4040 | rej.dcid = cpu_to_le16(chan->dcid); |
| 4041 | |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 4042 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4043 | sizeof(rej), &rej); |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 4044 | goto unlock; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 4045 | } |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 4046 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4047 | /* Reject if config buffer is too small. */ |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 4048 | len = cmd_len - sizeof(*req); |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4049 | if (chan->conf_len + len > sizeof(chan->conf_req)) { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4050 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4051 | l2cap_build_conf_rsp(chan, rsp, |
| 4052 | L2CAP_CONF_REJECT, flags), rsp); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4053 | goto unlock; |
| 4054 | } |
| 4055 | |
| 4056 | /* Store config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4057 | memcpy(chan->conf_req + chan->conf_len, req->data, len); |
| 4058 | chan->conf_len += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4059 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4060 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | /* Incomplete config. Send empty response. */ |
| 4062 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4063 | l2cap_build_conf_rsp(chan, rsp, |
| 4064 | L2CAP_CONF_SUCCESS, flags), rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4065 | goto unlock; |
| 4066 | } |
| 4067 | |
| 4068 | /* Complete config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4069 | len = l2cap_parse_conf_req(chan, rsp); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4070 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4071 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4072 | goto unlock; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4073 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4075 | chan->ident = cmd->ident; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4076 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4077 | chan->num_conf_rsp++; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4078 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4079 | /* Reset config buffer. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4080 | chan->conf_len = 0; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4081 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4082 | if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4083 | goto unlock; |
| 4084 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4085 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4086 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4087 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4088 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4089 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4090 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4091 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4092 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4093 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4094 | else |
| 4095 | l2cap_chan_ready(chan); |
| 4096 | |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4097 | goto unlock; |
| 4098 | } |
| 4099 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4100 | if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 4101 | u8 buf[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4102 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4103 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4104 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4105 | } |
| 4106 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4107 | /* Got Conf Rsp PENDING from remote side and asume we sent |
| 4108 | Conf Rsp PENDING in the code above */ |
| 4109 | if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 4110 | test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4111 | |
| 4112 | /* check compatibility */ |
| 4113 | |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4114 | /* Send rsp for BR/EDR channel */ |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4115 | if (!chan->hs_hcon) |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4116 | l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags); |
| 4117 | else |
| 4118 | chan->ident = cmd->ident; |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4119 | } |
| 4120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | unlock: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4122 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4123 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4124 | } |
| 4125 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4126 | static inline int l2cap_config_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4127 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4128 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | { |
| 4130 | struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; |
| 4131 | u16 scid, flags, result; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4132 | struct l2cap_chan *chan; |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4133 | int len = cmd_len - sizeof(*rsp); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4134 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4135 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4136 | if (cmd_len < sizeof(*rsp)) |
| 4137 | return -EPROTO; |
| 4138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | scid = __le16_to_cpu(rsp->scid); |
| 4140 | flags = __le16_to_cpu(rsp->flags); |
| 4141 | result = __le16_to_cpu(rsp->result); |
| 4142 | |
Andrei Emeltchenko | 61386cb | 2012-03-12 12:13:07 +0200 | [diff] [blame] | 4143 | BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags, |
| 4144 | result, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4146 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4147 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4148 | return 0; |
| 4149 | |
| 4150 | switch (result) { |
| 4151 | case L2CAP_CONF_SUCCESS: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4152 | l2cap_conf_rfc_get(chan, rsp->data, len); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4153 | clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | break; |
| 4155 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4156 | case L2CAP_CONF_PENDING: |
| 4157 | set_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
| 4158 | |
| 4159 | if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
| 4160 | char buf[64]; |
| 4161 | |
| 4162 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4163 | buf, &result); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4164 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4165 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4166 | goto done; |
| 4167 | } |
| 4168 | |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4169 | if (!chan->hs_hcon) { |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4170 | l2cap_send_efs_conf_rsp(chan, buf, cmd->ident, |
| 4171 | 0); |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 4172 | } else { |
| 4173 | if (l2cap_check_efs(chan)) { |
| 4174 | amp_create_logical_link(chan); |
| 4175 | chan->ident = cmd->ident; |
| 4176 | } |
| 4177 | } |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4178 | } |
| 4179 | goto done; |
| 4180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | case L2CAP_CONF_UNACCEPT: |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4182 | if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4183 | char req[64]; |
| 4184 | |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4185 | if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4186 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4187 | goto done; |
| 4188 | } |
| 4189 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4190 | /* throw out any old stored conf requests */ |
| 4191 | result = L2CAP_CONF_SUCCESS; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 4192 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4193 | req, &result); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4194 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4195 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4196 | goto done; |
| 4197 | } |
| 4198 | |
| 4199 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4200 | L2CAP_CONF_REQ, len, req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4201 | chan->num_conf_req++; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4202 | if (result != L2CAP_CONF_SUCCESS) |
| 4203 | goto done; |
| 4204 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | } |
| 4206 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 4207 | default: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4208 | l2cap_chan_set_err(chan, ECONNRESET); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 4209 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 4210 | __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4211 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4212 | goto done; |
| 4213 | } |
| 4214 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4215 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | goto done; |
| 4217 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4218 | set_bit(CONF_INPUT_DONE, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4220 | if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4221 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4222 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4223 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4224 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4225 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4226 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4227 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4228 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4229 | else |
| 4230 | l2cap_chan_ready(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4234 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4235 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | } |
| 4237 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4238 | static inline int l2cap_disconnect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4239 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4240 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | { |
| 4242 | struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; |
| 4243 | struct l2cap_disconn_rsp rsp; |
| 4244 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4245 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4246 | struct sock *sk; |
| 4247 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4248 | if (cmd_len != sizeof(*req)) |
| 4249 | return -EPROTO; |
| 4250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | scid = __le16_to_cpu(req->scid); |
| 4252 | dcid = __le16_to_cpu(req->dcid); |
| 4253 | |
| 4254 | BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); |
| 4255 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4256 | mutex_lock(&conn->chan_lock); |
| 4257 | |
| 4258 | chan = __l2cap_get_chan_by_scid(conn, dcid); |
| 4259 | if (!chan) { |
| 4260 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | c4ea249 | 2013-09-16 13:05:15 +0300 | [diff] [blame] | 4261 | return -EBADSLT; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4262 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4264 | l2cap_chan_lock(chan); |
| 4265 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4266 | sk = chan->sk; |
| 4267 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4268 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4269 | rsp.scid = cpu_to_le16(chan->dcid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); |
| 4271 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4272 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | sk->sk_shutdown = SHUTDOWN_MASK; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4274 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4276 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4277 | l2cap_chan_del(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4278 | |
| 4279 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4281 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4282 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4283 | |
| 4284 | mutex_unlock(&conn->chan_lock); |
| 4285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | return 0; |
| 4287 | } |
| 4288 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4289 | static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4290 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4291 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | { |
| 4293 | struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; |
| 4294 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4295 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4297 | if (cmd_len != sizeof(*rsp)) |
| 4298 | return -EPROTO; |
| 4299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | scid = __le16_to_cpu(rsp->scid); |
| 4301 | dcid = __le16_to_cpu(rsp->dcid); |
| 4302 | |
| 4303 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); |
| 4304 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4305 | mutex_lock(&conn->chan_lock); |
| 4306 | |
| 4307 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 4308 | if (!chan) { |
| 4309 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4313 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4314 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4315 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4316 | l2cap_chan_del(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4317 | |
| 4318 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4320 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4321 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4322 | |
| 4323 | mutex_unlock(&conn->chan_lock); |
| 4324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | return 0; |
| 4326 | } |
| 4327 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4328 | static inline int l2cap_information_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4329 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4330 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | { |
| 4332 | struct l2cap_info_req *req = (struct l2cap_info_req *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | u16 type; |
| 4334 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4335 | if (cmd_len != sizeof(*req)) |
| 4336 | return -EPROTO; |
| 4337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | type = __le16_to_cpu(req->type); |
| 4339 | |
| 4340 | BT_DBG("type 0x%4.4x", type); |
| 4341 | |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4342 | if (type == L2CAP_IT_FEAT_MASK) { |
| 4343 | u8 buf[8]; |
Marcel Holtmann | 44dd46d | 2009-05-02 19:09:01 -0700 | [diff] [blame] | 4344 | u32 feat_mask = l2cap_feat_mask; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4345 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4346 | rsp->type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 4347 | rsp->result = __constant_cpu_to_le16(L2CAP_IR_SUCCESS); |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 4348 | if (!disable_ertm) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4349 | feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4350 | | L2CAP_FEAT_FCS; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4351 | if (conn->hs_enabled) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 4352 | feat_mask |= L2CAP_FEAT_EXT_FLOW |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4353 | | L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 4354 | |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 4355 | put_unaligned_le32(feat_mask, rsp->data); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4356 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4357 | buf); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4358 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
| 4359 | u8 buf[12]; |
| 4360 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 4361 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4362 | if (conn->hs_enabled) |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 4363 | l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; |
| 4364 | else |
| 4365 | l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; |
| 4366 | |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4367 | rsp->type = __constant_cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 4368 | rsp->result = __constant_cpu_to_le16(L2CAP_IR_SUCCESS); |
Andrei Emeltchenko | c6337ea | 2011-10-20 17:02:44 +0300 | [diff] [blame] | 4369 | memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan)); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4370 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4371 | buf); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4372 | } else { |
| 4373 | struct l2cap_info_rsp rsp; |
| 4374 | rsp.type = cpu_to_le16(type); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4375 | rsp.result = __constant_cpu_to_le16(L2CAP_IR_NOTSUPP); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4376 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp), |
| 4377 | &rsp); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | |
| 4380 | return 0; |
| 4381 | } |
| 4382 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4383 | static inline int l2cap_information_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4384 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4385 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | { |
| 4387 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; |
| 4388 | u16 type, result; |
| 4389 | |
Jaganath Kanakkassery | 3f6fa3d | 2013-06-21 19:55:11 +0530 | [diff] [blame] | 4390 | if (cmd_len < sizeof(*rsp)) |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4391 | return -EPROTO; |
| 4392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4393 | type = __le16_to_cpu(rsp->type); |
| 4394 | result = __le16_to_cpu(rsp->result); |
| 4395 | |
| 4396 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); |
| 4397 | |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4398 | /* L2CAP Info req/rsp are unbound to channels, add extra checks */ |
| 4399 | if (cmd->ident != conn->info_ident || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4400 | conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4401 | return 0; |
| 4402 | |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 4403 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4404 | |
Ville Tervo | adb08ed | 2010-08-04 09:43:33 +0300 | [diff] [blame] | 4405 | if (result != L2CAP_IR_SUCCESS) { |
| 4406 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4407 | conn->info_ident = 0; |
| 4408 | |
| 4409 | l2cap_conn_start(conn); |
| 4410 | |
| 4411 | return 0; |
| 4412 | } |
| 4413 | |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4414 | switch (type) { |
| 4415 | case L2CAP_IT_FEAT_MASK: |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 4416 | conn->feat_mask = get_unaligned_le32(rsp->data); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4417 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 4418 | if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4419 | struct l2cap_info_req req; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4420 | req.type = __constant_cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4421 | |
| 4422 | conn->info_ident = l2cap_get_ident(conn); |
| 4423 | |
| 4424 | l2cap_send_cmd(conn, conn->info_ident, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4425 | L2CAP_INFO_REQ, sizeof(req), &req); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4426 | } else { |
| 4427 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4428 | conn->info_ident = 0; |
| 4429 | |
| 4430 | l2cap_conn_start(conn); |
| 4431 | } |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4432 | break; |
| 4433 | |
| 4434 | case L2CAP_IT_FIXED_CHAN: |
| 4435 | conn->fixed_chan_mask = rsp->data[0]; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4436 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4437 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4438 | |
| 4439 | l2cap_conn_start(conn); |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4440 | break; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4441 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4443 | return 0; |
| 4444 | } |
| 4445 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4446 | static int l2cap_create_channel_req(struct l2cap_conn *conn, |
| 4447 | struct l2cap_cmd_hdr *cmd, |
| 4448 | u16 cmd_len, void *data) |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4449 | { |
| 4450 | struct l2cap_create_chan_req *req = data; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4451 | struct l2cap_create_chan_rsp rsp; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4452 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4453 | struct hci_dev *hdev; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4454 | u16 psm, scid; |
| 4455 | |
| 4456 | if (cmd_len != sizeof(*req)) |
| 4457 | return -EPROTO; |
| 4458 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4459 | if (!conn->hs_enabled) |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4460 | return -EINVAL; |
| 4461 | |
| 4462 | psm = le16_to_cpu(req->psm); |
| 4463 | scid = le16_to_cpu(req->scid); |
| 4464 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4465 | 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] | 4466 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4467 | /* For controller id 0 make BR/EDR connection */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4468 | if (req->amp_id == AMP_ID_BREDR) { |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4469 | l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4470 | req->amp_id); |
| 4471 | return 0; |
| 4472 | } |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4473 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4474 | /* Validate AMP controller id */ |
| 4475 | hdev = hci_dev_get(req->amp_id); |
| 4476 | if (!hdev) |
| 4477 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4478 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4479 | if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4480 | hci_dev_put(hdev); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4481 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4482 | } |
| 4483 | |
| 4484 | chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4485 | req->amp_id); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4486 | if (chan) { |
| 4487 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
| 4488 | struct hci_conn *hs_hcon; |
| 4489 | |
Marcel Holtmann | 98e0f7e | 2013-10-13 02:23:40 -0700 | [diff] [blame] | 4490 | hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, |
| 4491 | &conn->hcon->dst); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4492 | if (!hs_hcon) { |
| 4493 | hci_dev_put(hdev); |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 4494 | return -EBADSLT; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon); |
| 4498 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4499 | mgr->bredr_chan = chan; |
| 4500 | chan->hs_hcon = hs_hcon; |
Andrei Emeltchenko | fd45bf4 | 2012-11-20 17:16:22 +0200 | [diff] [blame] | 4501 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4502 | conn->mtu = hdev->block_mtu; |
| 4503 | } |
| 4504 | |
| 4505 | hci_dev_put(hdev); |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4506 | |
| 4507 | return 0; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4508 | |
| 4509 | error: |
| 4510 | rsp.dcid = 0; |
| 4511 | rsp.scid = cpu_to_le16(scid); |
| 4512 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_BAD_AMP); |
| 4513 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
| 4514 | |
| 4515 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, |
| 4516 | sizeof(rsp), &rsp); |
| 4517 | |
Johan Hedberg | dc28080 | 2013-09-16 13:05:13 +0300 | [diff] [blame] | 4518 | return 0; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4519 | } |
| 4520 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4521 | static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id) |
| 4522 | { |
| 4523 | struct l2cap_move_chan_req req; |
| 4524 | u8 ident; |
| 4525 | |
| 4526 | BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id); |
| 4527 | |
| 4528 | ident = l2cap_get_ident(chan->conn); |
| 4529 | chan->ident = ident; |
| 4530 | |
| 4531 | req.icid = cpu_to_le16(chan->scid); |
| 4532 | req.dest_amp_id = dest_amp_id; |
| 4533 | |
| 4534 | l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req), |
| 4535 | &req); |
| 4536 | |
| 4537 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4538 | } |
| 4539 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4540 | 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] | 4541 | { |
| 4542 | struct l2cap_move_chan_rsp rsp; |
| 4543 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4544 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4545 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4546 | rsp.icid = cpu_to_le16(chan->dcid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4547 | rsp.result = cpu_to_le16(result); |
| 4548 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4549 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP, |
| 4550 | sizeof(rsp), &rsp); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4551 | } |
| 4552 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4553 | 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] | 4554 | { |
| 4555 | struct l2cap_move_chan_cfm cfm; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4556 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4557 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4558 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4559 | chan->ident = l2cap_get_ident(chan->conn); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4560 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4561 | cfm.icid = cpu_to_le16(chan->scid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4562 | cfm.result = cpu_to_le16(result); |
| 4563 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4564 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM, |
| 4565 | sizeof(cfm), &cfm); |
| 4566 | |
| 4567 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4568 | } |
| 4569 | |
| 4570 | static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid) |
| 4571 | { |
| 4572 | struct l2cap_move_chan_cfm cfm; |
| 4573 | |
| 4574 | BT_DBG("conn %p, icid 0x%4.4x", conn, icid); |
| 4575 | |
| 4576 | cfm.icid = cpu_to_le16(icid); |
| 4577 | cfm.result = __constant_cpu_to_le16(L2CAP_MC_UNCONFIRMED); |
| 4578 | |
| 4579 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM, |
| 4580 | sizeof(cfm), &cfm); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4581 | } |
| 4582 | |
| 4583 | 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] | 4584 | u16 icid) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4585 | { |
| 4586 | struct l2cap_move_chan_cfm_rsp rsp; |
| 4587 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4588 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4589 | |
| 4590 | rsp.icid = cpu_to_le16(icid); |
| 4591 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); |
| 4592 | } |
| 4593 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 4594 | static void __release_logical_link(struct l2cap_chan *chan) |
| 4595 | { |
| 4596 | chan->hs_hchan = NULL; |
| 4597 | chan->hs_hcon = NULL; |
| 4598 | |
| 4599 | /* Placeholder - release the logical link */ |
| 4600 | } |
| 4601 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4602 | static void l2cap_logical_fail(struct l2cap_chan *chan) |
| 4603 | { |
| 4604 | /* Logical link setup failed */ |
| 4605 | if (chan->state != BT_CONNECTED) { |
| 4606 | /* Create channel failure, disconnect */ |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4607 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4608 | return; |
| 4609 | } |
| 4610 | |
| 4611 | switch (chan->move_role) { |
| 4612 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4613 | l2cap_move_done(chan); |
| 4614 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP); |
| 4615 | break; |
| 4616 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4617 | if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP || |
| 4618 | chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) { |
| 4619 | /* Remote has only sent pending or |
| 4620 | * success responses, clean up |
| 4621 | */ |
| 4622 | l2cap_move_done(chan); |
| 4623 | } |
| 4624 | |
| 4625 | /* Other amp move states imply that the move |
| 4626 | * has already aborted |
| 4627 | */ |
| 4628 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 4629 | break; |
| 4630 | } |
| 4631 | } |
| 4632 | |
| 4633 | static void l2cap_logical_finish_create(struct l2cap_chan *chan, |
| 4634 | struct hci_chan *hchan) |
| 4635 | { |
| 4636 | struct l2cap_conf_rsp rsp; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4637 | |
Andrei Emeltchenko | 336178a | 2012-10-31 15:46:27 +0200 | [diff] [blame] | 4638 | chan->hs_hchan = hchan; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4639 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4640 | |
Andrei Emeltchenko | 35ba956 | 2012-10-25 15:20:43 +0300 | [diff] [blame] | 4641 | l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4642 | |
| 4643 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Andrei Emeltchenko | fe79c6f | 2012-10-25 15:20:42 +0300 | [diff] [blame] | 4644 | int err; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4645 | |
| 4646 | set_default_fcs(chan); |
| 4647 | |
| 4648 | err = l2cap_ertm_init(chan); |
| 4649 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4650 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4651 | else |
| 4652 | l2cap_chan_ready(chan); |
| 4653 | } |
| 4654 | } |
| 4655 | |
| 4656 | static void l2cap_logical_finish_move(struct l2cap_chan *chan, |
| 4657 | struct hci_chan *hchan) |
| 4658 | { |
| 4659 | chan->hs_hcon = hchan->conn; |
| 4660 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4661 | |
| 4662 | BT_DBG("move_state %d", chan->move_state); |
| 4663 | |
| 4664 | switch (chan->move_state) { |
| 4665 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 4666 | /* Move confirm will be sent after a success |
| 4667 | * response is received |
| 4668 | */ |
| 4669 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4670 | break; |
| 4671 | case L2CAP_MOVE_WAIT_LOGICAL_CFM: |
| 4672 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4673 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4674 | } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 4675 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 4676 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4677 | } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4678 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4679 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4680 | } |
| 4681 | break; |
| 4682 | default: |
| 4683 | /* Move was not in expected state, free the channel */ |
| 4684 | __release_logical_link(chan); |
| 4685 | |
| 4686 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4687 | } |
| 4688 | } |
| 4689 | |
| 4690 | /* Call with chan locked */ |
Andrei Emeltchenko | 27695fb | 2012-10-25 15:20:45 +0300 | [diff] [blame] | 4691 | void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan, |
| 4692 | u8 status) |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4693 | { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4694 | BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status); |
| 4695 | |
| 4696 | if (status) { |
| 4697 | l2cap_logical_fail(chan); |
| 4698 | __release_logical_link(chan); |
| 4699 | return; |
| 4700 | } |
| 4701 | |
| 4702 | if (chan->state != BT_CONNECTED) { |
| 4703 | /* Ignore logical link if channel is on BR/EDR */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4704 | if (chan->local_amp_id != AMP_ID_BREDR) |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4705 | l2cap_logical_finish_create(chan, hchan); |
| 4706 | } else { |
| 4707 | l2cap_logical_finish_move(chan, hchan); |
| 4708 | } |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4709 | } |
| 4710 | |
Mat Martineau | 3f7a56c | 2012-10-23 15:24:23 -0700 | [diff] [blame] | 4711 | void l2cap_move_start(struct l2cap_chan *chan) |
| 4712 | { |
| 4713 | BT_DBG("chan %p", chan); |
| 4714 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4715 | if (chan->local_amp_id == AMP_ID_BREDR) { |
Mat Martineau | 3f7a56c | 2012-10-23 15:24:23 -0700 | [diff] [blame] | 4716 | if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED) |
| 4717 | return; |
| 4718 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4719 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4720 | /* Placeholder - start physical link setup */ |
| 4721 | } else { |
| 4722 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4723 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4724 | chan->move_id = 0; |
| 4725 | l2cap_move_setup(chan); |
| 4726 | l2cap_send_move_chan_req(chan, 0); |
| 4727 | } |
| 4728 | } |
| 4729 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4730 | static void l2cap_do_create(struct l2cap_chan *chan, int result, |
| 4731 | u8 local_amp_id, u8 remote_amp_id) |
| 4732 | { |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4733 | BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state), |
| 4734 | local_amp_id, remote_amp_id); |
| 4735 | |
Andrei Emeltchenko | 12d6cc6 | 2012-11-14 17:39:31 +0200 | [diff] [blame] | 4736 | chan->fcs = L2CAP_FCS_NONE; |
| 4737 | |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4738 | /* Outgoing channel on AMP */ |
| 4739 | if (chan->state == BT_CONNECT) { |
| 4740 | if (result == L2CAP_CR_SUCCESS) { |
| 4741 | chan->local_amp_id = local_amp_id; |
| 4742 | l2cap_send_create_chan_req(chan, remote_amp_id); |
| 4743 | } else { |
| 4744 | /* Revert to BR/EDR connect */ |
| 4745 | l2cap_send_conn_req(chan); |
| 4746 | } |
| 4747 | |
| 4748 | return; |
| 4749 | } |
| 4750 | |
| 4751 | /* Incoming channel on AMP */ |
| 4752 | if (__l2cap_no_conn_pending(chan)) { |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4753 | struct l2cap_conn_rsp rsp; |
| 4754 | char buf[128]; |
| 4755 | rsp.scid = cpu_to_le16(chan->dcid); |
| 4756 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4757 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4758 | if (result == L2CAP_CR_SUCCESS) { |
| 4759 | /* Send successful response */ |
Andrei Emeltchenko | 62cd50e | 2012-11-14 17:39:32 +0200 | [diff] [blame] | 4760 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS); |
| 4761 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4762 | } else { |
| 4763 | /* Send negative response */ |
Andrei Emeltchenko | 62cd50e | 2012-11-14 17:39:32 +0200 | [diff] [blame] | 4764 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_NO_MEM); |
| 4765 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4766 | } |
| 4767 | |
| 4768 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP, |
| 4769 | sizeof(rsp), &rsp); |
| 4770 | |
| 4771 | if (result == L2CAP_CR_SUCCESS) { |
| 4772 | __l2cap_state_change(chan, BT_CONFIG); |
| 4773 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 4774 | l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn), |
| 4775 | L2CAP_CONF_REQ, |
| 4776 | l2cap_build_conf_req(chan, buf), buf); |
| 4777 | chan->num_conf_req++; |
| 4778 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4779 | } |
| 4780 | } |
| 4781 | |
| 4782 | static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id, |
| 4783 | u8 remote_amp_id) |
| 4784 | { |
| 4785 | l2cap_move_setup(chan); |
| 4786 | chan->move_id = local_amp_id; |
| 4787 | chan->move_state = L2CAP_MOVE_WAIT_RSP; |
| 4788 | |
| 4789 | l2cap_send_move_chan_req(chan, remote_amp_id); |
| 4790 | } |
| 4791 | |
| 4792 | static void l2cap_do_move_respond(struct l2cap_chan *chan, int result) |
| 4793 | { |
| 4794 | struct hci_chan *hchan = NULL; |
| 4795 | |
| 4796 | /* Placeholder - get hci_chan for logical link */ |
| 4797 | |
| 4798 | if (hchan) { |
| 4799 | if (hchan->state == BT_CONNECTED) { |
| 4800 | /* Logical link is ready to go */ |
| 4801 | chan->hs_hcon = hchan->conn; |
| 4802 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4803 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4804 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4805 | |
| 4806 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 4807 | } else { |
| 4808 | /* Wait for logical link to be ready */ |
| 4809 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4810 | } |
| 4811 | } else { |
| 4812 | /* Logical link not available */ |
| 4813 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED); |
| 4814 | } |
| 4815 | } |
| 4816 | |
| 4817 | static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result) |
| 4818 | { |
| 4819 | if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4820 | u8 rsp_result; |
| 4821 | if (result == -EINVAL) |
| 4822 | rsp_result = L2CAP_MR_BAD_ID; |
| 4823 | else |
| 4824 | rsp_result = L2CAP_MR_NOT_ALLOWED; |
| 4825 | |
| 4826 | l2cap_send_move_chan_rsp(chan, rsp_result); |
| 4827 | } |
| 4828 | |
| 4829 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 4830 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4831 | |
| 4832 | /* Restart data transmission */ |
| 4833 | l2cap_ertm_send(chan); |
| 4834 | } |
| 4835 | |
Andrei Emeltchenko | a514b17 | 2012-11-14 17:39:30 +0200 | [diff] [blame] | 4836 | /* Invoke with locked chan */ |
| 4837 | void __l2cap_physical_cfm(struct l2cap_chan *chan, int result) |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4838 | { |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4839 | u8 local_amp_id = chan->local_amp_id; |
Andrei Emeltchenko | fffadc0 | 2012-11-01 15:37:03 +0200 | [diff] [blame] | 4840 | u8 remote_amp_id = chan->remote_amp_id; |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4841 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4842 | BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d", |
| 4843 | chan, result, local_amp_id, remote_amp_id); |
| 4844 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4845 | if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) { |
| 4846 | l2cap_chan_unlock(chan); |
| 4847 | return; |
| 4848 | } |
| 4849 | |
| 4850 | if (chan->state != BT_CONNECTED) { |
| 4851 | l2cap_do_create(chan, result, local_amp_id, remote_amp_id); |
| 4852 | } else if (result != L2CAP_MR_SUCCESS) { |
| 4853 | l2cap_do_move_cancel(chan, result); |
| 4854 | } else { |
| 4855 | switch (chan->move_role) { |
| 4856 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4857 | l2cap_do_move_initiate(chan, local_amp_id, |
| 4858 | remote_amp_id); |
| 4859 | break; |
| 4860 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4861 | l2cap_do_move_respond(chan, result); |
| 4862 | break; |
| 4863 | default: |
| 4864 | l2cap_do_move_cancel(chan, result); |
| 4865 | break; |
| 4866 | } |
| 4867 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4868 | } |
| 4869 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4870 | static inline int l2cap_move_channel_req(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4871 | struct l2cap_cmd_hdr *cmd, |
| 4872 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4873 | { |
| 4874 | struct l2cap_move_chan_req *req = data; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4875 | struct l2cap_move_chan_rsp rsp; |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4876 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4877 | u16 icid = 0; |
| 4878 | u16 result = L2CAP_MR_NOT_ALLOWED; |
| 4879 | |
| 4880 | if (cmd_len != sizeof(*req)) |
| 4881 | return -EPROTO; |
| 4882 | |
| 4883 | icid = le16_to_cpu(req->icid); |
| 4884 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4885 | 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] | 4886 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4887 | if (!conn->hs_enabled) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4888 | return -EINVAL; |
| 4889 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4890 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 4891 | if (!chan) { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4892 | rsp.icid = cpu_to_le16(icid); |
| 4893 | rsp.result = __constant_cpu_to_le16(L2CAP_MR_NOT_ALLOWED); |
| 4894 | l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP, |
| 4895 | sizeof(rsp), &rsp); |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4896 | return 0; |
| 4897 | } |
| 4898 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4899 | chan->ident = cmd->ident; |
| 4900 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4901 | if (chan->scid < L2CAP_CID_DYN_START || |
| 4902 | chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY || |
| 4903 | (chan->mode != L2CAP_MODE_ERTM && |
| 4904 | chan->mode != L2CAP_MODE_STREAMING)) { |
| 4905 | result = L2CAP_MR_NOT_ALLOWED; |
| 4906 | goto send_move_response; |
| 4907 | } |
| 4908 | |
| 4909 | if (chan->local_amp_id == req->dest_amp_id) { |
| 4910 | result = L2CAP_MR_SAME_ID; |
| 4911 | goto send_move_response; |
| 4912 | } |
| 4913 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4914 | if (req->dest_amp_id != AMP_ID_BREDR) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4915 | struct hci_dev *hdev; |
| 4916 | hdev = hci_dev_get(req->dest_amp_id); |
| 4917 | if (!hdev || hdev->dev_type != HCI_AMP || |
| 4918 | !test_bit(HCI_UP, &hdev->flags)) { |
| 4919 | if (hdev) |
| 4920 | hci_dev_put(hdev); |
| 4921 | |
| 4922 | result = L2CAP_MR_BAD_ID; |
| 4923 | goto send_move_response; |
| 4924 | } |
| 4925 | hci_dev_put(hdev); |
| 4926 | } |
| 4927 | |
| 4928 | /* Detect a move collision. Only send a collision response |
| 4929 | * if this side has "lost", otherwise proceed with the move. |
| 4930 | * The winner has the larger bd_addr. |
| 4931 | */ |
| 4932 | if ((__chan_is_moving(chan) || |
| 4933 | chan->move_role != L2CAP_MOVE_ROLE_NONE) && |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 4934 | bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4935 | result = L2CAP_MR_COLLISION; |
| 4936 | goto send_move_response; |
| 4937 | } |
| 4938 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4939 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 4940 | l2cap_move_setup(chan); |
| 4941 | chan->move_id = req->dest_amp_id; |
| 4942 | icid = chan->dcid; |
| 4943 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4944 | if (req->dest_amp_id == AMP_ID_BREDR) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4945 | /* Moving to BR/EDR */ |
| 4946 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4947 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4948 | result = L2CAP_MR_PEND; |
| 4949 | } else { |
| 4950 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4951 | result = L2CAP_MR_SUCCESS; |
| 4952 | } |
| 4953 | } else { |
| 4954 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4955 | /* Placeholder - uncomment when amp functions are available */ |
| 4956 | /*amp_accept_physical(chan, req->dest_amp_id);*/ |
| 4957 | result = L2CAP_MR_PEND; |
| 4958 | } |
| 4959 | |
| 4960 | send_move_response: |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4961 | l2cap_send_move_chan_rsp(chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4962 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4963 | l2cap_chan_unlock(chan); |
| 4964 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4965 | return 0; |
| 4966 | } |
| 4967 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4968 | static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result) |
| 4969 | { |
| 4970 | struct l2cap_chan *chan; |
| 4971 | struct hci_chan *hchan = NULL; |
| 4972 | |
| 4973 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 4974 | if (!chan) { |
| 4975 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 4976 | return; |
| 4977 | } |
| 4978 | |
| 4979 | __clear_chan_timer(chan); |
| 4980 | if (result == L2CAP_MR_PEND) |
| 4981 | __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT); |
| 4982 | |
| 4983 | switch (chan->move_state) { |
| 4984 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 4985 | /* Move confirm will be sent when logical link |
| 4986 | * is complete. |
| 4987 | */ |
| 4988 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4989 | break; |
| 4990 | case L2CAP_MOVE_WAIT_RSP_SUCCESS: |
| 4991 | if (result == L2CAP_MR_PEND) { |
| 4992 | break; |
| 4993 | } else if (test_bit(CONN_LOCAL_BUSY, |
| 4994 | &chan->conn_state)) { |
| 4995 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4996 | } else { |
| 4997 | /* Logical link is up or moving to BR/EDR, |
| 4998 | * proceed with move |
| 4999 | */ |
| 5000 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 5001 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 5002 | } |
| 5003 | break; |
| 5004 | case L2CAP_MOVE_WAIT_RSP: |
| 5005 | /* Moving to AMP */ |
| 5006 | if (result == L2CAP_MR_SUCCESS) { |
| 5007 | /* Remote is ready, send confirm immediately |
| 5008 | * after logical link is ready |
| 5009 | */ |
| 5010 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 5011 | } else { |
| 5012 | /* Both logical link and move success |
| 5013 | * are required to confirm |
| 5014 | */ |
| 5015 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP; |
| 5016 | } |
| 5017 | |
| 5018 | /* Placeholder - get hci_chan for logical link */ |
| 5019 | if (!hchan) { |
| 5020 | /* Logical link not available */ |
| 5021 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5022 | break; |
| 5023 | } |
| 5024 | |
| 5025 | /* If the logical link is not yet connected, do not |
| 5026 | * send confirmation. |
| 5027 | */ |
| 5028 | if (hchan->state != BT_CONNECTED) |
| 5029 | break; |
| 5030 | |
| 5031 | /* Logical link is already ready to go */ |
| 5032 | |
| 5033 | chan->hs_hcon = hchan->conn; |
| 5034 | chan->hs_hcon->l2cap_data = chan->conn; |
| 5035 | |
| 5036 | if (result == L2CAP_MR_SUCCESS) { |
| 5037 | /* Can confirm now */ |
| 5038 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 5039 | } else { |
| 5040 | /* Now only need move success |
| 5041 | * to confirm |
| 5042 | */ |
| 5043 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 5044 | } |
| 5045 | |
| 5046 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 5047 | break; |
| 5048 | default: |
| 5049 | /* Any other amp move state means the move failed. */ |
| 5050 | chan->move_id = chan->local_amp_id; |
| 5051 | l2cap_move_done(chan); |
| 5052 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5053 | } |
| 5054 | |
| 5055 | l2cap_chan_unlock(chan); |
| 5056 | } |
| 5057 | |
| 5058 | static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid, |
| 5059 | u16 result) |
| 5060 | { |
| 5061 | struct l2cap_chan *chan; |
| 5062 | |
| 5063 | chan = l2cap_get_chan_by_ident(conn, ident); |
| 5064 | if (!chan) { |
| 5065 | /* Could not locate channel, icid is best guess */ |
| 5066 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 5067 | return; |
| 5068 | } |
| 5069 | |
| 5070 | __clear_chan_timer(chan); |
| 5071 | |
| 5072 | if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 5073 | if (result == L2CAP_MR_COLLISION) { |
| 5074 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 5075 | } else { |
| 5076 | /* Cleanup - cancel move */ |
| 5077 | chan->move_id = chan->local_amp_id; |
| 5078 | l2cap_move_done(chan); |
| 5079 | } |
| 5080 | } |
| 5081 | |
| 5082 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5083 | |
| 5084 | l2cap_chan_unlock(chan); |
| 5085 | } |
| 5086 | |
| 5087 | static int l2cap_move_channel_rsp(struct l2cap_conn *conn, |
| 5088 | struct l2cap_cmd_hdr *cmd, |
| 5089 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5090 | { |
| 5091 | struct l2cap_move_chan_rsp *rsp = data; |
| 5092 | u16 icid, result; |
| 5093 | |
| 5094 | if (cmd_len != sizeof(*rsp)) |
| 5095 | return -EPROTO; |
| 5096 | |
| 5097 | icid = le16_to_cpu(rsp->icid); |
| 5098 | result = le16_to_cpu(rsp->result); |
| 5099 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5100 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5101 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 5102 | if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND) |
| 5103 | l2cap_move_continue(conn, icid, result); |
| 5104 | else |
| 5105 | l2cap_move_fail(conn, cmd->ident, icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5106 | |
| 5107 | return 0; |
| 5108 | } |
| 5109 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5110 | static int l2cap_move_channel_confirm(struct l2cap_conn *conn, |
| 5111 | struct l2cap_cmd_hdr *cmd, |
| 5112 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5113 | { |
| 5114 | struct l2cap_move_chan_cfm *cfm = data; |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5115 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5116 | u16 icid, result; |
| 5117 | |
| 5118 | if (cmd_len != sizeof(*cfm)) |
| 5119 | return -EPROTO; |
| 5120 | |
| 5121 | icid = le16_to_cpu(cfm->icid); |
| 5122 | result = le16_to_cpu(cfm->result); |
| 5123 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5124 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5125 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5126 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 5127 | if (!chan) { |
| 5128 | /* Spec requires a response even if the icid was not found */ |
| 5129 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5130 | return 0; |
| 5131 | } |
| 5132 | |
| 5133 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) { |
| 5134 | if (result == L2CAP_MC_CONFIRMED) { |
| 5135 | chan->local_amp_id = chan->move_id; |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 5136 | if (chan->local_amp_id == AMP_ID_BREDR) |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5137 | __release_logical_link(chan); |
| 5138 | } else { |
| 5139 | chan->move_id = chan->local_amp_id; |
| 5140 | } |
| 5141 | |
| 5142 | l2cap_move_done(chan); |
| 5143 | } |
| 5144 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5145 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5146 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5147 | l2cap_chan_unlock(chan); |
| 5148 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5149 | return 0; |
| 5150 | } |
| 5151 | |
| 5152 | static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5153 | struct l2cap_cmd_hdr *cmd, |
| 5154 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5155 | { |
| 5156 | struct l2cap_move_chan_cfm_rsp *rsp = data; |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5157 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5158 | u16 icid; |
| 5159 | |
| 5160 | if (cmd_len != sizeof(*rsp)) |
| 5161 | return -EPROTO; |
| 5162 | |
| 5163 | icid = le16_to_cpu(rsp->icid); |
| 5164 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5165 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5166 | |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5167 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 5168 | if (!chan) |
| 5169 | return 0; |
| 5170 | |
| 5171 | __clear_chan_timer(chan); |
| 5172 | |
| 5173 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) { |
| 5174 | chan->local_amp_id = chan->move_id; |
| 5175 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 5176 | if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan) |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5177 | __release_logical_link(chan); |
| 5178 | |
| 5179 | l2cap_move_done(chan); |
| 5180 | } |
| 5181 | |
| 5182 | l2cap_chan_unlock(chan); |
| 5183 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5184 | return 0; |
| 5185 | } |
| 5186 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 5187 | static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5188 | u16 to_multiplier) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5189 | { |
| 5190 | u16 max_latency; |
| 5191 | |
| 5192 | if (min > max || min < 6 || max > 3200) |
| 5193 | return -EINVAL; |
| 5194 | |
| 5195 | if (to_multiplier < 10 || to_multiplier > 3200) |
| 5196 | return -EINVAL; |
| 5197 | |
| 5198 | if (max >= to_multiplier * 8) |
| 5199 | return -EINVAL; |
| 5200 | |
| 5201 | max_latency = (to_multiplier * 8 / max) - 1; |
| 5202 | if (latency > 499 || latency > max_latency) |
| 5203 | return -EINVAL; |
| 5204 | |
| 5205 | return 0; |
| 5206 | } |
| 5207 | |
| 5208 | static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5209 | struct l2cap_cmd_hdr *cmd, |
| 5210 | u8 *data) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5211 | { |
| 5212 | struct hci_conn *hcon = conn->hcon; |
| 5213 | struct l2cap_conn_param_update_req *req; |
| 5214 | struct l2cap_conn_param_update_rsp rsp; |
| 5215 | u16 min, max, latency, to_multiplier, cmd_len; |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5216 | int err; |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5217 | |
| 5218 | if (!(hcon->link_mode & HCI_LM_MASTER)) |
| 5219 | return -EINVAL; |
| 5220 | |
| 5221 | cmd_len = __le16_to_cpu(cmd->len); |
| 5222 | if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) |
| 5223 | return -EPROTO; |
| 5224 | |
| 5225 | req = (struct l2cap_conn_param_update_req *) data; |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 5226 | min = __le16_to_cpu(req->min); |
| 5227 | max = __le16_to_cpu(req->max); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5228 | latency = __le16_to_cpu(req->latency); |
| 5229 | to_multiplier = __le16_to_cpu(req->to_multiplier); |
| 5230 | |
| 5231 | 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] | 5232 | min, max, latency, to_multiplier); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5233 | |
| 5234 | memset(&rsp, 0, sizeof(rsp)); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5235 | |
| 5236 | err = l2cap_check_conn_param(min, max, latency, to_multiplier); |
| 5237 | if (err) |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 5238 | rsp.result = __constant_cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5239 | else |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 5240 | rsp.result = __constant_cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5241 | |
| 5242 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5243 | sizeof(rsp), &rsp); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5244 | |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5245 | if (!err) |
| 5246 | hci_le_conn_update(hcon, min, max, latency, to_multiplier); |
| 5247 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5248 | return 0; |
| 5249 | } |
| 5250 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5251 | static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5252 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5253 | u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5254 | { |
| 5255 | int err = 0; |
| 5256 | |
| 5257 | switch (cmd->code) { |
| 5258 | case L2CAP_COMMAND_REJ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5259 | l2cap_command_rej(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5260 | break; |
| 5261 | |
| 5262 | case L2CAP_CONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5263 | err = l2cap_connect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5264 | break; |
| 5265 | |
| 5266 | case L2CAP_CONN_RSP: |
Mat Martineau | f5a2598 | 2012-10-11 17:48:21 +0300 | [diff] [blame] | 5267 | case L2CAP_CREATE_CHAN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5268 | l2cap_connect_create_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5269 | break; |
| 5270 | |
| 5271 | case L2CAP_CONF_REQ: |
| 5272 | err = l2cap_config_req(conn, cmd, cmd_len, data); |
| 5273 | break; |
| 5274 | |
| 5275 | case L2CAP_CONF_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5276 | l2cap_config_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5277 | break; |
| 5278 | |
| 5279 | case L2CAP_DISCONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5280 | err = l2cap_disconnect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5281 | break; |
| 5282 | |
| 5283 | case L2CAP_DISCONN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5284 | l2cap_disconnect_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5285 | break; |
| 5286 | |
| 5287 | case L2CAP_ECHO_REQ: |
| 5288 | l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); |
| 5289 | break; |
| 5290 | |
| 5291 | case L2CAP_ECHO_RSP: |
| 5292 | break; |
| 5293 | |
| 5294 | case L2CAP_INFO_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5295 | err = l2cap_information_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5296 | break; |
| 5297 | |
| 5298 | case L2CAP_INFO_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5299 | l2cap_information_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5300 | break; |
| 5301 | |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 5302 | case L2CAP_CREATE_CHAN_REQ: |
| 5303 | err = l2cap_create_channel_req(conn, cmd, cmd_len, data); |
| 5304 | break; |
| 5305 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5306 | case L2CAP_MOVE_CHAN_REQ: |
| 5307 | err = l2cap_move_channel_req(conn, cmd, cmd_len, data); |
| 5308 | break; |
| 5309 | |
| 5310 | case L2CAP_MOVE_CHAN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5311 | l2cap_move_channel_rsp(conn, cmd, cmd_len, data); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5312 | break; |
| 5313 | |
| 5314 | case L2CAP_MOVE_CHAN_CFM: |
| 5315 | err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); |
| 5316 | break; |
| 5317 | |
| 5318 | case L2CAP_MOVE_CHAN_CFM_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5319 | l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5320 | break; |
| 5321 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5322 | default: |
| 5323 | BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); |
| 5324 | err = -EINVAL; |
| 5325 | break; |
| 5326 | } |
| 5327 | |
| 5328 | return err; |
| 5329 | } |
| 5330 | |
| 5331 | static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5332 | struct l2cap_cmd_hdr *cmd, u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5333 | { |
| 5334 | switch (cmd->code) { |
| 5335 | case L2CAP_COMMAND_REJ: |
| 5336 | return 0; |
| 5337 | |
| 5338 | case L2CAP_CONN_PARAM_UPDATE_REQ: |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5339 | return l2cap_conn_param_update_req(conn, cmd, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5340 | |
| 5341 | case L2CAP_CONN_PARAM_UPDATE_RSP: |
| 5342 | return 0; |
| 5343 | |
| 5344 | default: |
| 5345 | BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); |
| 5346 | return -EINVAL; |
| 5347 | } |
| 5348 | } |
| 5349 | |
Johan Hedberg | 7c2005d | 2013-09-16 13:05:16 +0300 | [diff] [blame] | 5350 | static __le16 l2cap_err_to_reason(int err) |
| 5351 | { |
| 5352 | switch (err) { |
| 5353 | case -EBADSLT: |
| 5354 | return __constant_cpu_to_le16(L2CAP_REJ_INVALID_CID); |
| 5355 | case -EMSGSIZE: |
| 5356 | return __constant_cpu_to_le16(L2CAP_REJ_MTU_EXCEEDED); |
| 5357 | case -EINVAL: |
| 5358 | case -EPROTO: |
| 5359 | default: |
| 5360 | return __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
| 5361 | } |
| 5362 | } |
| 5363 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5364 | static inline void l2cap_le_sig_channel(struct l2cap_conn *conn, |
| 5365 | struct sk_buff *skb) |
| 5366 | { |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5367 | struct hci_conn *hcon = conn->hcon; |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5368 | struct l2cap_cmd_hdr *cmd; |
| 5369 | u16 len; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5370 | int err; |
| 5371 | |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5372 | if (hcon->type != LE_LINK) |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5373 | goto drop; |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5374 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5375 | if (skb->len < L2CAP_CMD_HDR_SIZE) |
| 5376 | goto drop; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5377 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5378 | cmd = (void *) skb->data; |
| 5379 | skb_pull(skb, L2CAP_CMD_HDR_SIZE); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5380 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5381 | len = le16_to_cpu(cmd->len); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5382 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5383 | 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] | 5384 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5385 | if (len != skb->len || !cmd->ident) { |
| 5386 | BT_DBG("corrupted command"); |
| 5387 | goto drop; |
| 5388 | } |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5389 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5390 | err = l2cap_le_sig_cmd(conn, cmd, skb->data); |
| 5391 | if (err) { |
| 5392 | struct l2cap_cmd_rej_unk rej; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5393 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5394 | BT_ERR("Wrong link type (%d)", err); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5395 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5396 | rej.reason = l2cap_err_to_reason(err); |
| 5397 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
| 5398 | sizeof(rej), &rej); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5399 | } |
| 5400 | |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5401 | drop: |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5402 | kfree_skb(skb); |
| 5403 | } |
| 5404 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5405 | static inline void l2cap_sig_channel(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5406 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5407 | { |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5408 | struct hci_conn *hcon = conn->hcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | u8 *data = skb->data; |
| 5410 | int len = skb->len; |
| 5411 | struct l2cap_cmd_hdr cmd; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5412 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5413 | |
| 5414 | l2cap_raw_recv(conn, skb); |
| 5415 | |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5416 | if (hcon->type != ACL_LINK) |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5417 | goto drop; |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | while (len >= L2CAP_CMD_HDR_SIZE) { |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5420 | u16 cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 5422 | data += L2CAP_CMD_HDR_SIZE; |
| 5423 | len -= L2CAP_CMD_HDR_SIZE; |
| 5424 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5425 | cmd_len = le16_to_cpu(cmd.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5427 | BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, |
| 5428 | cmd.ident); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5429 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5430 | if (cmd_len > len || !cmd.ident) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | BT_DBG("corrupted command"); |
| 5432 | break; |
| 5433 | } |
| 5434 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5435 | err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | if (err) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 5437 | struct l2cap_cmd_rej_unk rej; |
Gustavo F. Padovan | 2c6d1a2 | 2011-03-23 14:38:32 -0300 | [diff] [blame] | 5438 | |
| 5439 | BT_ERR("Wrong link type (%d)", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5440 | |
Johan Hedberg | 7c2005d | 2013-09-16 13:05:16 +0300 | [diff] [blame] | 5441 | rej.reason = l2cap_err_to_reason(err); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5442 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, |
| 5443 | sizeof(rej), &rej); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | } |
| 5445 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5446 | data += cmd_len; |
| 5447 | len -= cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5448 | } |
| 5449 | |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5450 | drop: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | kfree_skb(skb); |
| 5452 | } |
| 5453 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5454 | 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] | 5455 | { |
| 5456 | u16 our_fcs, rcv_fcs; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 5457 | int hdr_size; |
| 5458 | |
| 5459 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 5460 | hdr_size = L2CAP_EXT_HDR_SIZE; |
| 5461 | else |
| 5462 | hdr_size = L2CAP_ENH_HDR_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5463 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5464 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5465 | skb_trim(skb, skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5466 | rcv_fcs = get_unaligned_le16(skb->data + skb->len); |
| 5467 | our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); |
| 5468 | |
| 5469 | if (our_fcs != rcv_fcs) |
João Paulo Rechi Vita | 7a560e5 | 2010-06-22 13:56:27 -0300 | [diff] [blame] | 5470 | return -EBADMSG; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5471 | } |
| 5472 | return 0; |
| 5473 | } |
| 5474 | |
Mat Martineau | 6ea0048 | 2012-05-17 20:53:52 -0700 | [diff] [blame] | 5475 | 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] | 5476 | { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5477 | struct l2cap_ctrl control; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5478 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5479 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5480 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5481 | memset(&control, 0, sizeof(control)); |
| 5482 | control.sframe = 1; |
| 5483 | control.final = 1; |
| 5484 | control.reqseq = chan->buffer_seq; |
| 5485 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5486 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5487 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5488 | control.super = L2CAP_SUPER_RNR; |
| 5489 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5490 | } |
| 5491 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5492 | if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
| 5493 | chan->unacked_frames > 0) |
| 5494 | __set_retrans_timer(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5495 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5496 | /* Send pending iframes */ |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 5497 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5498 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5499 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5500 | test_bit(CONN_SEND_FBIT, &chan->conn_state)) { |
| 5501 | /* F-bit wasn't sent in an s-frame or i-frame yet, so |
| 5502 | * send it now. |
| 5503 | */ |
| 5504 | control.super = L2CAP_SUPER_RR; |
| 5505 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5506 | } |
| 5507 | } |
| 5508 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5509 | static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag, |
| 5510 | struct sk_buff **last_frag) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5511 | { |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5512 | /* skb->len reflects data in skb as well as all fragments |
| 5513 | * skb->data_len reflects only data in fragments |
| 5514 | */ |
| 5515 | if (!skb_has_frag_list(skb)) |
| 5516 | skb_shinfo(skb)->frag_list = new_frag; |
| 5517 | |
| 5518 | new_frag->next = NULL; |
| 5519 | |
| 5520 | (*last_frag)->next = new_frag; |
| 5521 | *last_frag = new_frag; |
| 5522 | |
| 5523 | skb->len += new_frag->len; |
| 5524 | skb->data_len += new_frag->len; |
| 5525 | skb->truesize += new_frag->truesize; |
| 5526 | } |
| 5527 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5528 | static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, |
| 5529 | struct l2cap_ctrl *control) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5530 | { |
| 5531 | int err = -EINVAL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5532 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5533 | switch (control->sar) { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5534 | case L2CAP_SAR_UNSEGMENTED: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5535 | if (chan->sdu) |
| 5536 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5537 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5538 | err = chan->ops->recv(chan, skb); |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5539 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5540 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5541 | case L2CAP_SAR_START: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5542 | if (chan->sdu) |
| 5543 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5544 | |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5545 | chan->sdu_len = get_unaligned_le16(skb->data); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5546 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5547 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5548 | if (chan->sdu_len > chan->imtu) { |
| 5549 | err = -EMSGSIZE; |
| 5550 | break; |
| 5551 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5552 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5553 | if (skb->len >= chan->sdu_len) |
| 5554 | break; |
| 5555 | |
| 5556 | chan->sdu = skb; |
| 5557 | chan->sdu_last_frag = skb; |
| 5558 | |
| 5559 | skb = NULL; |
| 5560 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5561 | break; |
| 5562 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5563 | case L2CAP_SAR_CONTINUE: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5564 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5565 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5566 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5567 | append_skb_frag(chan->sdu, skb, |
| 5568 | &chan->sdu_last_frag); |
| 5569 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5570 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5571 | if (chan->sdu->len >= chan->sdu_len) |
| 5572 | break; |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5573 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5574 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5575 | break; |
| 5576 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5577 | case L2CAP_SAR_END: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5578 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5579 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5580 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5581 | append_skb_frag(chan->sdu, skb, |
| 5582 | &chan->sdu_last_frag); |
| 5583 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5584 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5585 | if (chan->sdu->len != chan->sdu_len) |
| 5586 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5587 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5588 | err = chan->ops->recv(chan, chan->sdu); |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5589 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5590 | if (!err) { |
| 5591 | /* Reassembly complete */ |
| 5592 | chan->sdu = NULL; |
| 5593 | chan->sdu_last_frag = NULL; |
| 5594 | chan->sdu_len = 0; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5595 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5596 | break; |
| 5597 | } |
| 5598 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5599 | if (err) { |
| 5600 | kfree_skb(skb); |
| 5601 | kfree_skb(chan->sdu); |
| 5602 | chan->sdu = NULL; |
| 5603 | chan->sdu_last_frag = NULL; |
| 5604 | chan->sdu_len = 0; |
| 5605 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5606 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5607 | return err; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5608 | } |
| 5609 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 5610 | static int l2cap_resegment(struct l2cap_chan *chan) |
| 5611 | { |
| 5612 | /* Placeholder */ |
| 5613 | return 0; |
| 5614 | } |
| 5615 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 5616 | void l2cap_chan_busy(struct l2cap_chan *chan, int busy) |
Gustavo F. Padovan | 712132eb | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 5617 | { |
Mat Martineau | 61aa4f5 | 2012-05-17 20:53:40 -0700 | [diff] [blame] | 5618 | u8 event; |
| 5619 | |
| 5620 | if (chan->mode != L2CAP_MODE_ERTM) |
| 5621 | return; |
| 5622 | |
| 5623 | event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR; |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 5624 | l2cap_tx(chan, NULL, NULL, event); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5625 | } |
| 5626 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5627 | static int l2cap_rx_queued_iframes(struct l2cap_chan *chan) |
| 5628 | { |
Mat Martineau | 6383872 | 2012-05-17 20:53:45 -0700 | [diff] [blame] | 5629 | int err = 0; |
| 5630 | /* Pass sequential frames to l2cap_reassemble_sdu() |
| 5631 | * until a gap is encountered. |
| 5632 | */ |
| 5633 | |
| 5634 | BT_DBG("chan %p", chan); |
| 5635 | |
| 5636 | while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5637 | struct sk_buff *skb; |
| 5638 | BT_DBG("Searching for skb with txseq %d (queue len %d)", |
| 5639 | chan->buffer_seq, skb_queue_len(&chan->srej_q)); |
| 5640 | |
| 5641 | skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq); |
| 5642 | |
| 5643 | if (!skb) |
| 5644 | break; |
| 5645 | |
| 5646 | skb_unlink(skb, &chan->srej_q); |
| 5647 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 5648 | err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->control); |
| 5649 | if (err) |
| 5650 | break; |
| 5651 | } |
| 5652 | |
| 5653 | if (skb_queue_empty(&chan->srej_q)) { |
| 5654 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 5655 | l2cap_send_ack(chan); |
| 5656 | } |
| 5657 | |
| 5658 | return err; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5659 | } |
| 5660 | |
| 5661 | static void l2cap_handle_srej(struct l2cap_chan *chan, |
| 5662 | struct l2cap_ctrl *control) |
| 5663 | { |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5664 | struct sk_buff *skb; |
| 5665 | |
| 5666 | BT_DBG("chan %p, control %p", chan, control); |
| 5667 | |
| 5668 | if (control->reqseq == chan->next_tx_seq) { |
| 5669 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5670 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5671 | return; |
| 5672 | } |
| 5673 | |
| 5674 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 5675 | |
| 5676 | if (skb == NULL) { |
| 5677 | BT_DBG("Seq %d not available for retransmission", |
| 5678 | control->reqseq); |
| 5679 | return; |
| 5680 | } |
| 5681 | |
| 5682 | if (chan->max_tx != 0 && bt_cb(skb)->control.retries >= chan->max_tx) { |
| 5683 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5684 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5685 | return; |
| 5686 | } |
| 5687 | |
| 5688 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5689 | |
| 5690 | if (control->poll) { |
| 5691 | l2cap_pass_to_tx(chan, control); |
| 5692 | |
| 5693 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 5694 | l2cap_retransmit(chan, control); |
| 5695 | l2cap_ertm_send(chan); |
| 5696 | |
| 5697 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 5698 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5699 | chan->srej_save_reqseq = control->reqseq; |
| 5700 | } |
| 5701 | } else { |
| 5702 | l2cap_pass_to_tx_fbit(chan, control); |
| 5703 | |
| 5704 | if (control->final) { |
| 5705 | if (chan->srej_save_reqseq != control->reqseq || |
| 5706 | !test_and_clear_bit(CONN_SREJ_ACT, |
| 5707 | &chan->conn_state)) |
| 5708 | l2cap_retransmit(chan, control); |
| 5709 | } else { |
| 5710 | l2cap_retransmit(chan, control); |
| 5711 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 5712 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5713 | chan->srej_save_reqseq = control->reqseq; |
| 5714 | } |
| 5715 | } |
| 5716 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5717 | } |
| 5718 | |
| 5719 | static void l2cap_handle_rej(struct l2cap_chan *chan, |
| 5720 | struct l2cap_ctrl *control) |
| 5721 | { |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5722 | struct sk_buff *skb; |
| 5723 | |
| 5724 | BT_DBG("chan %p, control %p", chan, control); |
| 5725 | |
| 5726 | if (control->reqseq == chan->next_tx_seq) { |
| 5727 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5728 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5729 | return; |
| 5730 | } |
| 5731 | |
| 5732 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 5733 | |
| 5734 | if (chan->max_tx && skb && |
| 5735 | bt_cb(skb)->control.retries >= chan->max_tx) { |
| 5736 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5737 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5738 | return; |
| 5739 | } |
| 5740 | |
| 5741 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5742 | |
| 5743 | l2cap_pass_to_tx(chan, control); |
| 5744 | |
| 5745 | if (control->final) { |
| 5746 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
| 5747 | l2cap_retransmit_all(chan, control); |
| 5748 | } else { |
| 5749 | l2cap_retransmit_all(chan, control); |
| 5750 | l2cap_ertm_send(chan); |
| 5751 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) |
| 5752 | set_bit(CONN_REJ_ACT, &chan->conn_state); |
| 5753 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5754 | } |
| 5755 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5756 | static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq) |
| 5757 | { |
| 5758 | BT_DBG("chan %p, txseq %d", chan, txseq); |
| 5759 | |
| 5760 | BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq, |
| 5761 | chan->expected_tx_seq); |
| 5762 | |
| 5763 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 5764 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5765 | chan->tx_win) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5766 | /* See notes below regarding "double poll" and |
| 5767 | * invalid packets. |
| 5768 | */ |
| 5769 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 5770 | BT_DBG("Invalid/Ignore - after SREJ"); |
| 5771 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 5772 | } else { |
| 5773 | BT_DBG("Invalid - in window after SREJ sent"); |
| 5774 | return L2CAP_TXSEQ_INVALID; |
| 5775 | } |
| 5776 | } |
| 5777 | |
| 5778 | if (chan->srej_list.head == txseq) { |
| 5779 | BT_DBG("Expected SREJ"); |
| 5780 | return L2CAP_TXSEQ_EXPECTED_SREJ; |
| 5781 | } |
| 5782 | |
| 5783 | if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) { |
| 5784 | BT_DBG("Duplicate SREJ - txseq already stored"); |
| 5785 | return L2CAP_TXSEQ_DUPLICATE_SREJ; |
| 5786 | } |
| 5787 | |
| 5788 | if (l2cap_seq_list_contains(&chan->srej_list, txseq)) { |
| 5789 | BT_DBG("Unexpected SREJ - not requested"); |
| 5790 | return L2CAP_TXSEQ_UNEXPECTED_SREJ; |
| 5791 | } |
| 5792 | } |
| 5793 | |
| 5794 | if (chan->expected_tx_seq == txseq) { |
| 5795 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
| 5796 | chan->tx_win) { |
| 5797 | BT_DBG("Invalid - txseq outside tx window"); |
| 5798 | return L2CAP_TXSEQ_INVALID; |
| 5799 | } else { |
| 5800 | BT_DBG("Expected"); |
| 5801 | return L2CAP_TXSEQ_EXPECTED; |
| 5802 | } |
| 5803 | } |
| 5804 | |
| 5805 | if (__seq_offset(chan, txseq, chan->last_acked_seq) < |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5806 | __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5807 | BT_DBG("Duplicate - expected_tx_seq later than txseq"); |
| 5808 | return L2CAP_TXSEQ_DUPLICATE; |
| 5809 | } |
| 5810 | |
| 5811 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) { |
| 5812 | /* A source of invalid packets is a "double poll" condition, |
| 5813 | * where delays cause us to send multiple poll packets. If |
| 5814 | * the remote stack receives and processes both polls, |
| 5815 | * sequence numbers can wrap around in such a way that a |
| 5816 | * resent frame has a sequence number that looks like new data |
| 5817 | * with a sequence gap. This would trigger an erroneous SREJ |
| 5818 | * request. |
| 5819 | * |
| 5820 | * Fortunately, this is impossible with a tx window that's |
| 5821 | * less than half of the maximum sequence number, which allows |
| 5822 | * invalid frames to be safely ignored. |
| 5823 | * |
| 5824 | * With tx window sizes greater than half of the tx window |
| 5825 | * maximum, the frame is invalid and cannot be ignored. This |
| 5826 | * causes a disconnect. |
| 5827 | */ |
| 5828 | |
| 5829 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 5830 | BT_DBG("Invalid/Ignore - txseq outside tx window"); |
| 5831 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 5832 | } else { |
| 5833 | BT_DBG("Invalid - txseq outside tx window"); |
| 5834 | return L2CAP_TXSEQ_INVALID; |
| 5835 | } |
| 5836 | } else { |
| 5837 | BT_DBG("Unexpected - txseq indicates missing frames"); |
| 5838 | return L2CAP_TXSEQ_UNEXPECTED; |
| 5839 | } |
| 5840 | } |
| 5841 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5842 | static int l2cap_rx_state_recv(struct l2cap_chan *chan, |
| 5843 | struct l2cap_ctrl *control, |
| 5844 | struct sk_buff *skb, u8 event) |
| 5845 | { |
| 5846 | int err = 0; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 5847 | bool skb_in_use = false; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5848 | |
| 5849 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 5850 | event); |
| 5851 | |
| 5852 | switch (event) { |
| 5853 | case L2CAP_EV_RECV_IFRAME: |
| 5854 | switch (l2cap_classify_txseq(chan, control->txseq)) { |
| 5855 | case L2CAP_TXSEQ_EXPECTED: |
| 5856 | l2cap_pass_to_tx(chan, control); |
| 5857 | |
| 5858 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5859 | BT_DBG("Busy, discarding expected seq %d", |
| 5860 | control->txseq); |
| 5861 | break; |
| 5862 | } |
| 5863 | |
| 5864 | chan->expected_tx_seq = __next_seq(chan, |
| 5865 | control->txseq); |
| 5866 | |
| 5867 | chan->buffer_seq = chan->expected_tx_seq; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 5868 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5869 | |
| 5870 | err = l2cap_reassemble_sdu(chan, skb, control); |
| 5871 | if (err) |
| 5872 | break; |
| 5873 | |
| 5874 | if (control->final) { |
| 5875 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 5876 | &chan->conn_state)) { |
| 5877 | control->final = 0; |
| 5878 | l2cap_retransmit_all(chan, control); |
| 5879 | l2cap_ertm_send(chan); |
| 5880 | } |
| 5881 | } |
| 5882 | |
| 5883 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 5884 | l2cap_send_ack(chan); |
| 5885 | break; |
| 5886 | case L2CAP_TXSEQ_UNEXPECTED: |
| 5887 | l2cap_pass_to_tx(chan, control); |
| 5888 | |
| 5889 | /* Can't issue SREJ frames in the local busy state. |
| 5890 | * Drop this frame, it will be seen as missing |
| 5891 | * when local busy is exited. |
| 5892 | */ |
| 5893 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5894 | BT_DBG("Busy, discarding unexpected seq %d", |
| 5895 | control->txseq); |
| 5896 | break; |
| 5897 | } |
| 5898 | |
| 5899 | /* There was a gap in the sequence, so an SREJ |
| 5900 | * must be sent for each missing frame. The |
| 5901 | * current frame is stored for later use. |
| 5902 | */ |
| 5903 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 5904 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5905 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5906 | skb_queue_len(&chan->srej_q)); |
| 5907 | |
| 5908 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5909 | l2cap_seq_list_clear(&chan->srej_list); |
| 5910 | l2cap_send_srej(chan, control->txseq); |
| 5911 | |
| 5912 | chan->rx_state = L2CAP_RX_STATE_SREJ_SENT; |
| 5913 | break; |
| 5914 | case L2CAP_TXSEQ_DUPLICATE: |
| 5915 | l2cap_pass_to_tx(chan, control); |
| 5916 | break; |
| 5917 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 5918 | break; |
| 5919 | case L2CAP_TXSEQ_INVALID: |
| 5920 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5921 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5922 | break; |
| 5923 | } |
| 5924 | break; |
| 5925 | case L2CAP_EV_RECV_RR: |
| 5926 | l2cap_pass_to_tx(chan, control); |
| 5927 | if (control->final) { |
| 5928 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5929 | |
Mat Martineau | e6a3ee6 | 2012-10-23 15:24:22 -0700 | [diff] [blame] | 5930 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) && |
| 5931 | !__chan_is_moving(chan)) { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5932 | control->final = 0; |
| 5933 | l2cap_retransmit_all(chan, control); |
| 5934 | } |
| 5935 | |
| 5936 | l2cap_ertm_send(chan); |
| 5937 | } else if (control->poll) { |
| 5938 | l2cap_send_i_or_rr_or_rnr(chan); |
| 5939 | } else { |
| 5940 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 5941 | &chan->conn_state) && |
| 5942 | chan->unacked_frames) |
| 5943 | __set_retrans_timer(chan); |
| 5944 | |
| 5945 | l2cap_ertm_send(chan); |
| 5946 | } |
| 5947 | break; |
| 5948 | case L2CAP_EV_RECV_RNR: |
| 5949 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5950 | l2cap_pass_to_tx(chan, control); |
| 5951 | if (control && control->poll) { |
| 5952 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 5953 | l2cap_send_rr_or_rnr(chan, 0); |
| 5954 | } |
| 5955 | __clear_retrans_timer(chan); |
| 5956 | l2cap_seq_list_clear(&chan->retrans_list); |
| 5957 | break; |
| 5958 | case L2CAP_EV_RECV_REJ: |
| 5959 | l2cap_handle_rej(chan, control); |
| 5960 | break; |
| 5961 | case L2CAP_EV_RECV_SREJ: |
| 5962 | l2cap_handle_srej(chan, control); |
| 5963 | break; |
| 5964 | default: |
| 5965 | break; |
| 5966 | } |
| 5967 | |
| 5968 | if (skb && !skb_in_use) { |
| 5969 | BT_DBG("Freeing %p", skb); |
| 5970 | kfree_skb(skb); |
| 5971 | } |
| 5972 | |
| 5973 | return err; |
| 5974 | } |
| 5975 | |
| 5976 | static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan, |
| 5977 | struct l2cap_ctrl *control, |
| 5978 | struct sk_buff *skb, u8 event) |
| 5979 | { |
| 5980 | int err = 0; |
| 5981 | u16 txseq = control->txseq; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 5982 | bool skb_in_use = false; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5983 | |
| 5984 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 5985 | event); |
| 5986 | |
| 5987 | switch (event) { |
| 5988 | case L2CAP_EV_RECV_IFRAME: |
| 5989 | switch (l2cap_classify_txseq(chan, txseq)) { |
| 5990 | case L2CAP_TXSEQ_EXPECTED: |
| 5991 | /* Keep frame for reassembly later */ |
| 5992 | l2cap_pass_to_tx(chan, control); |
| 5993 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 5994 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5995 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5996 | skb_queue_len(&chan->srej_q)); |
| 5997 | |
| 5998 | chan->expected_tx_seq = __next_seq(chan, txseq); |
| 5999 | break; |
| 6000 | case L2CAP_TXSEQ_EXPECTED_SREJ: |
| 6001 | l2cap_seq_list_pop(&chan->srej_list); |
| 6002 | |
| 6003 | l2cap_pass_to_tx(chan, control); |
| 6004 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6005 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6006 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6007 | skb_queue_len(&chan->srej_q)); |
| 6008 | |
| 6009 | err = l2cap_rx_queued_iframes(chan); |
| 6010 | if (err) |
| 6011 | break; |
| 6012 | |
| 6013 | break; |
| 6014 | case L2CAP_TXSEQ_UNEXPECTED: |
| 6015 | /* Got a frame that can't be reassembled yet. |
| 6016 | * Save it for later, and send SREJs to cover |
| 6017 | * the missing frames. |
| 6018 | */ |
| 6019 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6020 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6021 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6022 | skb_queue_len(&chan->srej_q)); |
| 6023 | |
| 6024 | l2cap_pass_to_tx(chan, control); |
| 6025 | l2cap_send_srej(chan, control->txseq); |
| 6026 | break; |
| 6027 | case L2CAP_TXSEQ_UNEXPECTED_SREJ: |
| 6028 | /* This frame was requested with an SREJ, but |
| 6029 | * some expected retransmitted frames are |
| 6030 | * missing. Request retransmission of missing |
| 6031 | * SREJ'd frames. |
| 6032 | */ |
| 6033 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6034 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6035 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6036 | skb_queue_len(&chan->srej_q)); |
| 6037 | |
| 6038 | l2cap_pass_to_tx(chan, control); |
| 6039 | l2cap_send_srej_list(chan, control->txseq); |
| 6040 | break; |
| 6041 | case L2CAP_TXSEQ_DUPLICATE_SREJ: |
| 6042 | /* We've already queued this frame. Drop this copy. */ |
| 6043 | l2cap_pass_to_tx(chan, control); |
| 6044 | break; |
| 6045 | case L2CAP_TXSEQ_DUPLICATE: |
| 6046 | /* Expecting a later sequence number, so this frame |
| 6047 | * was already received. Ignore it completely. |
| 6048 | */ |
| 6049 | break; |
| 6050 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 6051 | break; |
| 6052 | case L2CAP_TXSEQ_INVALID: |
| 6053 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6054 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6055 | break; |
| 6056 | } |
| 6057 | break; |
| 6058 | case L2CAP_EV_RECV_RR: |
| 6059 | l2cap_pass_to_tx(chan, control); |
| 6060 | if (control->final) { |
| 6061 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6062 | |
| 6063 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 6064 | &chan->conn_state)) { |
| 6065 | control->final = 0; |
| 6066 | l2cap_retransmit_all(chan, control); |
| 6067 | } |
| 6068 | |
| 6069 | l2cap_ertm_send(chan); |
| 6070 | } else if (control->poll) { |
| 6071 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6072 | &chan->conn_state) && |
| 6073 | chan->unacked_frames) { |
| 6074 | __set_retrans_timer(chan); |
| 6075 | } |
| 6076 | |
| 6077 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6078 | l2cap_send_srej_tail(chan); |
| 6079 | } else { |
| 6080 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6081 | &chan->conn_state) && |
| 6082 | chan->unacked_frames) |
| 6083 | __set_retrans_timer(chan); |
| 6084 | |
| 6085 | l2cap_send_ack(chan); |
| 6086 | } |
| 6087 | break; |
| 6088 | case L2CAP_EV_RECV_RNR: |
| 6089 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6090 | l2cap_pass_to_tx(chan, control); |
| 6091 | if (control->poll) { |
| 6092 | l2cap_send_srej_tail(chan); |
| 6093 | } else { |
| 6094 | struct l2cap_ctrl rr_control; |
| 6095 | memset(&rr_control, 0, sizeof(rr_control)); |
| 6096 | rr_control.sframe = 1; |
| 6097 | rr_control.super = L2CAP_SUPER_RR; |
| 6098 | rr_control.reqseq = chan->buffer_seq; |
| 6099 | l2cap_send_sframe(chan, &rr_control); |
| 6100 | } |
| 6101 | |
| 6102 | break; |
| 6103 | case L2CAP_EV_RECV_REJ: |
| 6104 | l2cap_handle_rej(chan, control); |
| 6105 | break; |
| 6106 | case L2CAP_EV_RECV_SREJ: |
| 6107 | l2cap_handle_srej(chan, control); |
| 6108 | break; |
| 6109 | } |
| 6110 | |
| 6111 | if (skb && !skb_in_use) { |
| 6112 | BT_DBG("Freeing %p", skb); |
| 6113 | kfree_skb(skb); |
| 6114 | } |
| 6115 | |
| 6116 | return err; |
| 6117 | } |
| 6118 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6119 | static int l2cap_finish_move(struct l2cap_chan *chan) |
| 6120 | { |
| 6121 | BT_DBG("chan %p", chan); |
| 6122 | |
| 6123 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6124 | |
| 6125 | if (chan->hs_hcon) |
| 6126 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6127 | else |
| 6128 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6129 | |
| 6130 | return l2cap_resegment(chan); |
| 6131 | } |
| 6132 | |
| 6133 | static int l2cap_rx_state_wait_p(struct l2cap_chan *chan, |
| 6134 | struct l2cap_ctrl *control, |
| 6135 | struct sk_buff *skb, u8 event) |
| 6136 | { |
| 6137 | int err; |
| 6138 | |
| 6139 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6140 | event); |
| 6141 | |
| 6142 | if (!control->poll) |
| 6143 | return -EPROTO; |
| 6144 | |
| 6145 | l2cap_process_reqseq(chan, control->reqseq); |
| 6146 | |
| 6147 | if (!skb_queue_empty(&chan->tx_q)) |
| 6148 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6149 | else |
| 6150 | chan->tx_send_head = NULL; |
| 6151 | |
| 6152 | /* Rewind next_tx_seq to the point expected |
| 6153 | * by the receiver. |
| 6154 | */ |
| 6155 | chan->next_tx_seq = control->reqseq; |
| 6156 | chan->unacked_frames = 0; |
| 6157 | |
| 6158 | err = l2cap_finish_move(chan); |
| 6159 | if (err) |
| 6160 | return err; |
| 6161 | |
| 6162 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6163 | l2cap_send_i_or_rr_or_rnr(chan); |
| 6164 | |
| 6165 | if (event == L2CAP_EV_RECV_IFRAME) |
| 6166 | return -EPROTO; |
| 6167 | |
| 6168 | return l2cap_rx_state_recv(chan, control, NULL, event); |
| 6169 | } |
| 6170 | |
| 6171 | static int l2cap_rx_state_wait_f(struct l2cap_chan *chan, |
| 6172 | struct l2cap_ctrl *control, |
| 6173 | struct sk_buff *skb, u8 event) |
| 6174 | { |
| 6175 | int err; |
| 6176 | |
| 6177 | if (!control->final) |
| 6178 | return -EPROTO; |
| 6179 | |
| 6180 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6181 | |
| 6182 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6183 | l2cap_process_reqseq(chan, control->reqseq); |
| 6184 | |
| 6185 | if (!skb_queue_empty(&chan->tx_q)) |
| 6186 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6187 | else |
| 6188 | chan->tx_send_head = NULL; |
| 6189 | |
| 6190 | /* Rewind next_tx_seq to the point expected |
| 6191 | * by the receiver. |
| 6192 | */ |
| 6193 | chan->next_tx_seq = control->reqseq; |
| 6194 | chan->unacked_frames = 0; |
| 6195 | |
| 6196 | if (chan->hs_hcon) |
| 6197 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6198 | else |
| 6199 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6200 | |
| 6201 | err = l2cap_resegment(chan); |
| 6202 | |
| 6203 | if (!err) |
| 6204 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6205 | |
| 6206 | return err; |
| 6207 | } |
| 6208 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6209 | static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 6210 | { |
| 6211 | /* Make sure reqseq is for a packet that has been sent but not acked */ |
| 6212 | u16 unacked; |
| 6213 | |
| 6214 | unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq); |
| 6215 | return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked; |
| 6216 | } |
| 6217 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6218 | static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6219 | struct sk_buff *skb, u8 event) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6220 | { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6221 | int err = 0; |
| 6222 | |
| 6223 | BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan, |
| 6224 | control, skb, event, chan->rx_state); |
| 6225 | |
| 6226 | if (__valid_reqseq(chan, control->reqseq)) { |
| 6227 | switch (chan->rx_state) { |
| 6228 | case L2CAP_RX_STATE_RECV: |
| 6229 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6230 | break; |
| 6231 | case L2CAP_RX_STATE_SREJ_SENT: |
| 6232 | err = l2cap_rx_state_srej_sent(chan, control, skb, |
| 6233 | event); |
| 6234 | break; |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6235 | case L2CAP_RX_STATE_WAIT_P: |
| 6236 | err = l2cap_rx_state_wait_p(chan, control, skb, event); |
| 6237 | break; |
| 6238 | case L2CAP_RX_STATE_WAIT_F: |
| 6239 | err = l2cap_rx_state_wait_f(chan, control, skb, event); |
| 6240 | break; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6241 | default: |
| 6242 | /* shut it down */ |
| 6243 | break; |
| 6244 | } |
| 6245 | } else { |
| 6246 | BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d", |
| 6247 | control->reqseq, chan->next_tx_seq, |
| 6248 | chan->expected_ack_seq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6249 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6250 | } |
| 6251 | |
| 6252 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6253 | } |
| 6254 | |
| 6255 | static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6256 | struct sk_buff *skb) |
| 6257 | { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6258 | int err = 0; |
| 6259 | |
| 6260 | BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb, |
| 6261 | chan->rx_state); |
| 6262 | |
| 6263 | if (l2cap_classify_txseq(chan, control->txseq) == |
| 6264 | L2CAP_TXSEQ_EXPECTED) { |
| 6265 | l2cap_pass_to_tx(chan, control); |
| 6266 | |
| 6267 | BT_DBG("buffer_seq %d->%d", chan->buffer_seq, |
| 6268 | __next_seq(chan, chan->buffer_seq)); |
| 6269 | |
| 6270 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 6271 | |
| 6272 | l2cap_reassemble_sdu(chan, skb, control); |
| 6273 | } else { |
| 6274 | if (chan->sdu) { |
| 6275 | kfree_skb(chan->sdu); |
| 6276 | chan->sdu = NULL; |
| 6277 | } |
| 6278 | chan->sdu_last_frag = NULL; |
| 6279 | chan->sdu_len = 0; |
| 6280 | |
| 6281 | if (skb) { |
| 6282 | BT_DBG("Freeing %p", skb); |
| 6283 | kfree_skb(skb); |
| 6284 | } |
| 6285 | } |
| 6286 | |
| 6287 | chan->last_acked_seq = control->txseq; |
| 6288 | chan->expected_tx_seq = __next_seq(chan, control->txseq); |
| 6289 | |
| 6290 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6291 | } |
| 6292 | |
| 6293 | static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) |
| 6294 | { |
| 6295 | struct l2cap_ctrl *control = &bt_cb(skb)->control; |
| 6296 | u16 len; |
| 6297 | u8 event; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6298 | |
Mat Martineau | b76bbd6 | 2012-04-11 10:48:43 -0700 | [diff] [blame] | 6299 | __unpack_control(chan, skb); |
| 6300 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6301 | len = skb->len; |
| 6302 | |
| 6303 | /* |
| 6304 | * We can just drop the corrupted I-frame here. |
| 6305 | * Receiver will miss it and start proper recovery |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6306 | * procedures and ask for retransmission. |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6307 | */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6308 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6309 | goto drop; |
| 6310 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6311 | if (!control->sframe && control->sar == L2CAP_SAR_START) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6312 | len -= L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6313 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6314 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6315 | len -= L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6316 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6317 | if (len > chan->mps) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6318 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6319 | goto drop; |
| 6320 | } |
| 6321 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6322 | if (!control->sframe) { |
| 6323 | int err; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6324 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6325 | BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d", |
| 6326 | control->sar, control->reqseq, control->final, |
| 6327 | control->txseq); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 6328 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6329 | /* Validate F-bit - F=0 always valid, F=1 only |
| 6330 | * valid in TX WAIT_F |
| 6331 | */ |
| 6332 | if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6333 | goto drop; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6334 | |
| 6335 | if (chan->mode != L2CAP_MODE_STREAMING) { |
| 6336 | event = L2CAP_EV_RECV_IFRAME; |
| 6337 | err = l2cap_rx(chan, control, skb, event); |
| 6338 | } else { |
| 6339 | err = l2cap_stream_rx(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6340 | } |
| 6341 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6342 | if (err) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6343 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6344 | } else { |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6345 | const u8 rx_func_to_event[4] = { |
| 6346 | L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ, |
| 6347 | L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ |
| 6348 | }; |
| 6349 | |
| 6350 | /* Only I-frames are expected in streaming mode */ |
| 6351 | if (chan->mode == L2CAP_MODE_STREAMING) |
| 6352 | goto drop; |
| 6353 | |
| 6354 | BT_DBG("sframe reqseq %d, final %d, poll %d, super %d", |
| 6355 | control->reqseq, control->final, control->poll, |
| 6356 | control->super); |
| 6357 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6358 | if (len != 0) { |
Andrei Emeltchenko | 1bb166e | 2012-11-20 17:16:21 +0200 | [diff] [blame] | 6359 | BT_ERR("Trailing bytes: %d in sframe", len); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6360 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6361 | goto drop; |
| 6362 | } |
| 6363 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6364 | /* Validate F and P bits */ |
| 6365 | if (control->final && (control->poll || |
| 6366 | chan->tx_state != L2CAP_TX_STATE_WAIT_F)) |
| 6367 | goto drop; |
| 6368 | |
| 6369 | event = rx_func_to_event[control->super]; |
| 6370 | if (l2cap_rx(chan, control, skb, event)) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6371 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6372 | } |
| 6373 | |
| 6374 | return 0; |
| 6375 | |
| 6376 | drop: |
| 6377 | kfree_skb(skb); |
| 6378 | return 0; |
| 6379 | } |
| 6380 | |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6381 | static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid, |
| 6382 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6384 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6385 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 6386 | chan = l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6387 | if (!chan) { |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6388 | if (cid == L2CAP_CID_A2MP) { |
| 6389 | chan = a2mp_channel_create(conn, skb); |
| 6390 | if (!chan) { |
| 6391 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6392 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6393 | } |
| 6394 | |
| 6395 | l2cap_chan_lock(chan); |
| 6396 | } else { |
| 6397 | BT_DBG("unknown cid 0x%4.4x", cid); |
| 6398 | /* Drop packet and return */ |
| 6399 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6400 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6402 | } |
| 6403 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 6404 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6405 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6406 | if (chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6407 | goto drop; |
| 6408 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6409 | switch (chan->mode) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6410 | case L2CAP_MODE_BASIC: |
| 6411 | /* If socket recv buffers overflows we drop data here |
| 6412 | * which is *bad* because L2CAP has to be reliable. |
| 6413 | * But we don't have any other choice. L2CAP doesn't |
| 6414 | * provide flow control mechanism. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6415 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6416 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6417 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6418 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6419 | if (!chan->ops->recv(chan, skb)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6420 | goto done; |
| 6421 | break; |
| 6422 | |
| 6423 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6424 | case L2CAP_MODE_STREAMING: |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6425 | l2cap_data_rcv(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6426 | goto done; |
| 6427 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6428 | default: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6429 | 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] | 6430 | break; |
| 6431 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | |
| 6433 | drop: |
| 6434 | kfree_skb(skb); |
| 6435 | |
| 6436 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6437 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6438 | } |
| 6439 | |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6440 | static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, |
| 6441 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6442 | { |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6443 | struct hci_conn *hcon = conn->hcon; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6444 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6445 | |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6446 | if (hcon->type != ACL_LINK) |
| 6447 | goto drop; |
| 6448 | |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 6449 | chan = l2cap_global_chan_by_psm(0, psm, &conn->hcon->src, |
Marcel Holtmann | 98e0f7e | 2013-10-13 02:23:40 -0700 | [diff] [blame] | 6450 | &conn->hcon->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6451 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6452 | goto drop; |
| 6453 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 6454 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6455 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6456 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6457 | goto drop; |
| 6458 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 6459 | if (chan->imtu < skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6460 | goto drop; |
| 6461 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6462 | if (!chan->ops->recv(chan, skb)) |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6463 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6464 | |
| 6465 | drop: |
| 6466 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | } |
| 6468 | |
Marcel Holtmann | 72f7835 | 2013-04-23 00:59:00 -0700 | [diff] [blame] | 6469 | static void l2cap_att_channel(struct l2cap_conn *conn, |
Andrei Emeltchenko | 6810fca | 2012-05-31 11:18:57 +0300 | [diff] [blame] | 6470 | struct sk_buff *skb) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6471 | { |
Marcel Holtmann | b99707d | 2013-10-03 02:54:11 -0700 | [diff] [blame] | 6472 | struct hci_conn *hcon = conn->hcon; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6473 | struct l2cap_chan *chan; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6474 | |
Marcel Holtmann | b99707d | 2013-10-03 02:54:11 -0700 | [diff] [blame] | 6475 | if (hcon->type != LE_LINK) |
| 6476 | goto drop; |
| 6477 | |
Johan Hedberg | af1c013 | 2013-04-29 19:35:42 +0300 | [diff] [blame] | 6478 | chan = l2cap_global_chan_by_scid(BT_CONNECTED, L2CAP_CID_ATT, |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 6479 | &conn->hcon->src, &conn->hcon->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6480 | if (!chan) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6481 | goto drop; |
| 6482 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 6483 | BT_DBG("chan %p, len %d", chan, skb->len); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6484 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 6485 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6486 | goto drop; |
| 6487 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6488 | if (!chan->ops->recv(chan, skb)) |
Andrei Emeltchenko | 6810fca | 2012-05-31 11:18:57 +0300 | [diff] [blame] | 6489 | return; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6490 | |
| 6491 | drop: |
| 6492 | kfree_skb(skb); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6493 | } |
| 6494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) |
| 6496 | { |
| 6497 | struct l2cap_hdr *lh = (void *) skb->data; |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 6498 | u16 cid, len; |
| 6499 | __le16 psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | |
| 6501 | skb_pull(skb, L2CAP_HDR_SIZE); |
| 6502 | cid = __le16_to_cpu(lh->cid); |
| 6503 | len = __le16_to_cpu(lh->len); |
| 6504 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6505 | if (len != skb->len) { |
| 6506 | kfree_skb(skb); |
| 6507 | return; |
| 6508 | } |
| 6509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6510 | BT_DBG("len %d, cid 0x%4.4x", len, cid); |
| 6511 | |
| 6512 | switch (cid) { |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6513 | case L2CAP_CID_SIGNALING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6514 | l2cap_sig_channel(conn, skb); |
| 6515 | break; |
| 6516 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6517 | case L2CAP_CID_CONN_LESS: |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 6518 | psm = get_unaligned((__le16 *) skb->data); |
Andrei Emeltchenko | 0181a70 | 2012-05-29 10:04:05 +0300 | [diff] [blame] | 6519 | skb_pull(skb, L2CAP_PSMLEN_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6520 | l2cap_conless_channel(conn, psm, skb); |
| 6521 | break; |
| 6522 | |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame] | 6523 | case L2CAP_CID_ATT: |
Marcel Holtmann | 72f7835 | 2013-04-23 00:59:00 -0700 | [diff] [blame] | 6524 | l2cap_att_channel(conn, skb); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6525 | break; |
| 6526 | |
Marcel Holtmann | a287762 | 2013-10-02 23:46:54 -0700 | [diff] [blame] | 6527 | case L2CAP_CID_LE_SIGNALING: |
| 6528 | l2cap_le_sig_channel(conn, skb); |
| 6529 | break; |
| 6530 | |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 6531 | case L2CAP_CID_SMP: |
| 6532 | if (smp_sig_channel(conn, skb)) |
| 6533 | l2cap_conn_del(conn->hcon, EACCES); |
| 6534 | break; |
| 6535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6536 | default: |
| 6537 | l2cap_data_channel(conn, cid, skb); |
| 6538 | break; |
| 6539 | } |
| 6540 | } |
| 6541 | |
| 6542 | /* ---- L2CAP interface with lower layer (HCI) ---- */ |
| 6543 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6544 | int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6545 | { |
| 6546 | int exact = 0, lm1 = 0, lm2 = 0; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6547 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6548 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 6549 | BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6550 | |
| 6551 | /* Find listening sockets and check their link_mode */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6552 | read_lock(&chan_list_lock); |
| 6553 | list_for_each_entry(c, &chan_list, global_l) { |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6554 | if (c->state != BT_LISTEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6555 | continue; |
| 6556 | |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 6557 | if (!bacmp(&c->src, &hdev->bdaddr)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6558 | lm1 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 6559 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6560 | lm1 |= HCI_LM_MASTER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | exact++; |
Marcel Holtmann | 7eafc59 | 2013-10-13 08:12:47 -0700 | [diff] [blame] | 6562 | } else if (!bacmp(&c->src, BDADDR_ANY)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6563 | lm2 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 6564 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6565 | lm2 |= HCI_LM_MASTER; |
| 6566 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6567 | } |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6568 | read_unlock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6569 | |
| 6570 | return exact ? lm1 : lm2; |
| 6571 | } |
| 6572 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 6573 | void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6574 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 6575 | struct l2cap_conn *conn; |
| 6576 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 6577 | BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6579 | if (!status) { |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 6580 | conn = l2cap_conn_add(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6581 | if (conn) |
| 6582 | l2cap_conn_ready(conn); |
Andrei Emeltchenko | ba6fc31 | 2012-10-31 15:46:26 +0200 | [diff] [blame] | 6583 | } else { |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 6584 | l2cap_conn_del(hcon, bt_to_errno(status)); |
Andrei Emeltchenko | ba6fc31 | 2012-10-31 15:46:26 +0200 | [diff] [blame] | 6585 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6586 | } |
| 6587 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6588 | int l2cap_disconn_ind(struct hci_conn *hcon) |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 6589 | { |
| 6590 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 6591 | |
| 6592 | BT_DBG("hcon %p", hcon); |
| 6593 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6594 | if (!conn) |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 6595 | return HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 6596 | return conn->disc_reason; |
| 6597 | } |
| 6598 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 6599 | void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6600 | { |
| 6601 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 6602 | |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 6603 | l2cap_conn_del(hcon, bt_to_errno(reason)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6604 | } |
| 6605 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6606 | static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6607 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 6608 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
Marcel Holtmann | 255c760 | 2009-02-04 21:07:19 +0100 | [diff] [blame] | 6609 | return; |
| 6610 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6611 | if (encrypt == 0x00) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6612 | if (chan->sec_level == BT_SECURITY_MEDIUM) { |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 6613 | __set_chan_timer(chan, L2CAP_ENC_TIMEOUT); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6614 | } else if (chan->sec_level == BT_SECURITY_HIGH) |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 6615 | l2cap_chan_close(chan, ECONNREFUSED); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6616 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6617 | if (chan->sec_level == BT_SECURITY_MEDIUM) |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 6618 | __clear_chan_timer(chan); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6619 | } |
| 6620 | } |
| 6621 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6622 | int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6623 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 6624 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6625 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6626 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 6627 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6628 | return 0; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 6629 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 6630 | 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] | 6631 | |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 6632 | if (hcon->type == LE_LINK) { |
Hemant Gupta | 35d4adcc | 2012-04-18 14:46:26 +0530 | [diff] [blame] | 6633 | if (!status && encrypt) |
| 6634 | smp_distribute_keys(conn, 0); |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 6635 | cancel_delayed_work(&conn->security_timer); |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 6636 | } |
| 6637 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 6638 | mutex_lock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6639 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 6640 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6641 | l2cap_chan_lock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6642 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 6643 | BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, |
| 6644 | state_to_string(chan->state)); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6645 | |
Andrei Emeltchenko | 78eb2f9 | 2012-07-19 17:03:47 +0300 | [diff] [blame] | 6646 | if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) { |
| 6647 | l2cap_chan_unlock(chan); |
| 6648 | continue; |
| 6649 | } |
| 6650 | |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame] | 6651 | if (chan->scid == L2CAP_CID_ATT) { |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6652 | if (!status && encrypt) { |
| 6653 | chan->sec_level = hcon->sec_level; |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 6654 | l2cap_chan_ready(chan); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6655 | } |
| 6656 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6657 | l2cap_chan_unlock(chan); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6658 | continue; |
| 6659 | } |
| 6660 | |
Andrei Emeltchenko | 96eff46 | 2012-11-15 18:14:53 +0200 | [diff] [blame] | 6661 | if (!__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6662 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 6663 | continue; |
| 6664 | } |
| 6665 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6666 | if (!status && (chan->state == BT_CONNECTED || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6667 | chan->state == BT_CONFIG)) { |
Gustavo Padovan | a7d7723 | 2012-05-13 03:20:07 -0300 | [diff] [blame] | 6668 | struct sock *sk = chan->sk; |
| 6669 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 6670 | clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); |
Gustavo Padovan | a7d7723 | 2012-05-13 03:20:07 -0300 | [diff] [blame] | 6671 | sk->sk_state_change(sk); |
| 6672 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6673 | l2cap_check_encryption(chan, encrypt); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6674 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 9719f8a | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 6675 | continue; |
| 6676 | } |
| 6677 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6678 | if (chan->state == BT_CONNECT) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6679 | if (!status) { |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 6680 | l2cap_start_connection(chan); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6681 | } else { |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 6682 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6683 | } |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6684 | } else if (chan->state == BT_CONNECT2) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6685 | struct sock *sk = chan->sk; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6686 | struct l2cap_conn_rsp rsp; |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6687 | __u16 res, stat; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6688 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6689 | lock_sock(sk); |
| 6690 | |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6691 | if (!status) { |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 6692 | if (test_bit(BT_SK_DEFER_SETUP, |
| 6693 | &bt_sk(sk)->flags)) { |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6694 | res = L2CAP_CR_PEND; |
| 6695 | stat = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 6696 | chan->ops->defer(chan); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6697 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 6698 | __l2cap_state_change(chan, BT_CONFIG); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6699 | res = L2CAP_CR_SUCCESS; |
| 6700 | stat = L2CAP_CS_NO_INFO; |
| 6701 | } |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6702 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 6703 | __l2cap_state_change(chan, BT_DISCONN); |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 6704 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6705 | res = L2CAP_CR_SEC_BLOCK; |
| 6706 | stat = L2CAP_CS_NO_INFO; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6707 | } |
| 6708 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6709 | release_sock(sk); |
| 6710 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 6711 | rsp.scid = cpu_to_le16(chan->dcid); |
| 6712 | rsp.dcid = cpu_to_le16(chan->scid); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6713 | rsp.result = cpu_to_le16(res); |
| 6714 | rsp.status = cpu_to_le16(stat); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 6715 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6716 | sizeof(rsp), &rsp); |
Mat Martineau | 2d36935 | 2012-05-23 14:59:30 -0700 | [diff] [blame] | 6717 | |
| 6718 | if (!test_bit(CONF_REQ_SENT, &chan->conf_state) && |
| 6719 | res == L2CAP_CR_SUCCESS) { |
| 6720 | char buf[128]; |
| 6721 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 6722 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 6723 | L2CAP_CONF_REQ, |
| 6724 | l2cap_build_conf_req(chan, buf), |
| 6725 | buf); |
| 6726 | chan->num_conf_req++; |
| 6727 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6728 | } |
| 6729 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6730 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6731 | } |
| 6732 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 6733 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | return 0; |
| 6736 | } |
| 6737 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6738 | 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] | 6739 | { |
| 6740 | struct l2cap_conn *conn = hcon->l2cap_data; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6741 | struct l2cap_hdr *hdr; |
| 6742 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6743 | |
Andrei Emeltchenko | 1d13a25 | 2012-10-15 11:58:41 +0300 | [diff] [blame] | 6744 | /* For AMP controller do not create l2cap conn */ |
| 6745 | if (!conn && hcon->hdev->dev_type != HCI_BREDR) |
| 6746 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6747 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 6748 | if (!conn) |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 6749 | conn = l2cap_conn_add(hcon); |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 6750 | |
| 6751 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | goto drop; |
| 6753 | |
| 6754 | BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); |
| 6755 | |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6756 | switch (flags) { |
| 6757 | case ACL_START: |
| 6758 | case ACL_START_NO_FLUSH: |
| 6759 | case ACL_COMPLETE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6760 | if (conn->rx_len) { |
| 6761 | BT_ERR("Unexpected start frame (len %d)", skb->len); |
| 6762 | kfree_skb(conn->rx_skb); |
| 6763 | conn->rx_skb = NULL; |
| 6764 | conn->rx_len = 0; |
| 6765 | l2cap_conn_unreliable(conn, ECOMM); |
| 6766 | } |
| 6767 | |
Andrei Emeltchenko | aae7fe2 | 2010-09-15 14:28:43 +0300 | [diff] [blame] | 6768 | /* Start fragment always begin with Basic L2CAP header */ |
| 6769 | if (skb->len < L2CAP_HDR_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6770 | BT_ERR("Frame is too short (len %d)", skb->len); |
| 6771 | l2cap_conn_unreliable(conn, ECOMM); |
| 6772 | goto drop; |
| 6773 | } |
| 6774 | |
| 6775 | hdr = (struct l2cap_hdr *) skb->data; |
| 6776 | len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; |
| 6777 | |
| 6778 | if (len == skb->len) { |
| 6779 | /* Complete frame received */ |
| 6780 | l2cap_recv_frame(conn, skb); |
| 6781 | return 0; |
| 6782 | } |
| 6783 | |
| 6784 | BT_DBG("Start: total len %d, frag len %d", len, skb->len); |
| 6785 | |
| 6786 | if (skb->len > len) { |
| 6787 | BT_ERR("Frame is too long (len %d, expected len %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6788 | skb->len, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6789 | l2cap_conn_unreliable(conn, ECOMM); |
| 6790 | goto drop; |
| 6791 | } |
| 6792 | |
| 6793 | /* Allocate skb for the complete frame (with header) */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 6794 | conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 6795 | if (!conn->rx_skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6796 | goto drop; |
| 6797 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 6798 | 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] | 6799 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6800 | conn->rx_len = len - skb->len; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6801 | break; |
| 6802 | |
| 6803 | case ACL_CONT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6804 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
| 6805 | |
| 6806 | if (!conn->rx_len) { |
| 6807 | BT_ERR("Unexpected continuation frame (len %d)", skb->len); |
| 6808 | l2cap_conn_unreliable(conn, ECOMM); |
| 6809 | goto drop; |
| 6810 | } |
| 6811 | |
| 6812 | if (skb->len > conn->rx_len) { |
| 6813 | BT_ERR("Fragment is too long (len %d, expected %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6814 | skb->len, conn->rx_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6815 | kfree_skb(conn->rx_skb); |
| 6816 | conn->rx_skb = NULL; |
| 6817 | conn->rx_len = 0; |
| 6818 | l2cap_conn_unreliable(conn, ECOMM); |
| 6819 | goto drop; |
| 6820 | } |
| 6821 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 6822 | 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] | 6823 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6824 | conn->rx_len -= skb->len; |
| 6825 | |
| 6826 | if (!conn->rx_len) { |
Johan Hedberg | c4e5baf | 2013-10-10 13:33:37 +0200 | [diff] [blame] | 6827 | /* Complete frame received. l2cap_recv_frame |
| 6828 | * takes ownership of the skb so set the global |
| 6829 | * rx_skb pointer to NULL first. |
| 6830 | */ |
| 6831 | struct sk_buff *rx_skb = conn->rx_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6832 | conn->rx_skb = NULL; |
Johan Hedberg | c4e5baf | 2013-10-10 13:33:37 +0200 | [diff] [blame] | 6833 | l2cap_recv_frame(conn, rx_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6834 | } |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6835 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6836 | } |
| 6837 | |
| 6838 | drop: |
| 6839 | kfree_skb(skb); |
| 6840 | return 0; |
| 6841 | } |
| 6842 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6843 | static int l2cap_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6844 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6845 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6846 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 6847 | read_lock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6848 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6849 | list_for_each_entry(c, &chan_list, global_l) { |
Andrei Emeltchenko | fcb7333 | 2012-09-25 12:49:44 +0300 | [diff] [blame] | 6850 | 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] | 6851 | &c->src, &c->dst, |
Andrei Emeltchenko | fcb7333 | 2012-09-25 12:49:44 +0300 | [diff] [blame] | 6852 | c->state, __le16_to_cpu(c->psm), |
| 6853 | c->scid, c->dcid, c->imtu, c->omtu, |
| 6854 | c->sec_level, c->mode); |
Andrei Emeltchenko | 61e1b4b | 2012-01-19 11:19:50 +0200 | [diff] [blame] | 6855 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6856 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 6857 | read_unlock(&chan_list_lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 6858 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6859 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6860 | } |
| 6861 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6862 | static int l2cap_debugfs_open(struct inode *inode, struct file *file) |
| 6863 | { |
| 6864 | return single_open(file, l2cap_debugfs_show, inode->i_private); |
| 6865 | } |
| 6866 | |
| 6867 | static const struct file_operations l2cap_debugfs_fops = { |
| 6868 | .open = l2cap_debugfs_open, |
| 6869 | .read = seq_read, |
| 6870 | .llseek = seq_lseek, |
| 6871 | .release = single_release, |
| 6872 | }; |
| 6873 | |
| 6874 | static struct dentry *l2cap_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6875 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 6876 | int __init l2cap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6877 | { |
| 6878 | int err; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 6879 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 6880 | err = l2cap_init_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6881 | if (err < 0) |
| 6882 | return err; |
| 6883 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6884 | if (bt_debugfs) { |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6885 | l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs, |
| 6886 | NULL, &l2cap_debugfs_fops); |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6887 | if (!l2cap_debugfs) |
| 6888 | BT_ERR("Failed to create L2CAP debug file"); |
| 6889 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6890 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6891 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | } |
| 6893 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 6894 | void l2cap_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6895 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6896 | debugfs_remove(l2cap_debugfs); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 6897 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6898 | } |
| 6899 | |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 6900 | module_param(disable_ertm, bool, 0644); |
| 6901 | MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); |