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 | 710f9b0a | 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 | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 62 | /* ---- L2CAP channels ---- */ |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 63 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 64 | static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, |
| 65 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 66 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 67 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 68 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 69 | list_for_each_entry(c, &conn->chan_l, list) { |
| 70 | if (c->dcid == cid) |
| 71 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 72 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 73 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 76 | static struct l2cap_chan *__l2cap_get_chan_by_scid(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->scid == 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 | |
| 88 | /* Find channel with given SCID. |
Mat Martineau | ef191ad | 2012-05-02 09:42:00 -0700 | [diff] [blame] | 89 | * Returns locked channel. */ |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 90 | static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, |
| 91 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 92 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 93 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 94 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 95 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 96 | c = __l2cap_get_chan_by_scid(conn, cid); |
Mat Martineau | ef191ad | 2012-05-02 09:42:00 -0700 | [diff] [blame] | 97 | if (c) |
| 98 | l2cap_chan_lock(c); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 99 | mutex_unlock(&conn->chan_lock); |
| 100 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 101 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Mat Martineau | b1a130b | 2012-10-23 15:24:09 -0700 | [diff] [blame] | 104 | /* Find channel with given DCID. |
| 105 | * Returns locked channel. |
| 106 | */ |
| 107 | static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn, |
| 108 | u16 cid) |
| 109 | { |
| 110 | struct l2cap_chan *c; |
| 111 | |
| 112 | mutex_lock(&conn->chan_lock); |
| 113 | c = __l2cap_get_chan_by_dcid(conn, cid); |
| 114 | if (c) |
| 115 | l2cap_chan_lock(c); |
| 116 | mutex_unlock(&conn->chan_lock); |
| 117 | |
| 118 | return c; |
| 119 | } |
| 120 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 121 | static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, |
| 122 | u8 ident) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 123 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 124 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 125 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 126 | list_for_each_entry(c, &conn->chan_l, list) { |
| 127 | if (c->ident == ident) |
| 128 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 129 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 130 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 133 | static struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, |
| 134 | u8 ident) |
| 135 | { |
| 136 | struct l2cap_chan *c; |
| 137 | |
| 138 | mutex_lock(&conn->chan_lock); |
| 139 | c = __l2cap_get_chan_by_ident(conn, ident); |
| 140 | if (c) |
| 141 | l2cap_chan_lock(c); |
| 142 | mutex_unlock(&conn->chan_lock); |
| 143 | |
| 144 | return c; |
| 145 | } |
| 146 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 147 | 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] | 148 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 149 | struct l2cap_chan *c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 150 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 151 | list_for_each_entry(c, &chan_list, global_l) { |
| 152 | if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src)) |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 153 | return c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 154 | } |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 155 | return NULL; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) |
| 159 | { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 160 | int err; |
| 161 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 162 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 163 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 164 | if (psm && __l2cap_global_chan_by_addr(psm, src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 165 | err = -EADDRINUSE; |
| 166 | goto done; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 167 | } |
| 168 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 169 | if (psm) { |
| 170 | chan->psm = psm; |
| 171 | chan->sport = psm; |
| 172 | err = 0; |
| 173 | } else { |
| 174 | u16 p; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 175 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 176 | err = -EINVAL; |
| 177 | for (p = 0x1001; p < 0x1100; p += 2) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 178 | if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 179 | chan->psm = cpu_to_le16(p); |
| 180 | chan->sport = cpu_to_le16(p); |
| 181 | err = 0; |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | done: |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 187 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 188 | return err; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) |
| 192 | { |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 193 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 194 | |
| 195 | chan->scid = scid; |
| 196 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 197 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 202 | static u16 l2cap_alloc_cid(struct l2cap_conn *conn) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 203 | { |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 204 | u16 cid = L2CAP_CID_DYN_START; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 205 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 206 | for (; cid < L2CAP_CID_DYN_END; cid++) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 207 | if (!__l2cap_get_chan_by_scid(conn, cid)) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 208 | return cid; |
| 209 | } |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 214 | static void __l2cap_state_change(struct l2cap_chan *chan, int state) |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 215 | { |
Andrei Emeltchenko | 42d2d87 | 2012-02-17 11:40:57 +0200 | [diff] [blame] | 216 | BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 217 | state_to_string(state)); |
Gustavo F. Padovan | badaaa0 | 2011-11-23 20:11:46 -0200 | [diff] [blame] | 218 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 219 | chan->state = state; |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 220 | chan->ops->state_change(chan, state); |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 221 | } |
| 222 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 223 | static void l2cap_state_change(struct l2cap_chan *chan, int state) |
| 224 | { |
| 225 | struct sock *sk = chan->sk; |
| 226 | |
| 227 | lock_sock(sk); |
| 228 | __l2cap_state_change(chan, state); |
| 229 | release_sock(sk); |
| 230 | } |
| 231 | |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 232 | static inline void __l2cap_chan_set_err(struct l2cap_chan *chan, int err) |
| 233 | { |
| 234 | struct sock *sk = chan->sk; |
| 235 | |
| 236 | sk->sk_err = err; |
| 237 | } |
| 238 | |
| 239 | static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err) |
| 240 | { |
| 241 | struct sock *sk = chan->sk; |
| 242 | |
| 243 | lock_sock(sk); |
| 244 | __l2cap_chan_set_err(chan, err); |
| 245 | release_sock(sk); |
| 246 | } |
| 247 | |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 248 | static void __set_retrans_timer(struct l2cap_chan *chan) |
| 249 | { |
| 250 | if (!delayed_work_pending(&chan->monitor_timer) && |
| 251 | chan->retrans_timeout) { |
| 252 | l2cap_set_timer(chan, &chan->retrans_timer, |
| 253 | msecs_to_jiffies(chan->retrans_timeout)); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | static void __set_monitor_timer(struct l2cap_chan *chan) |
| 258 | { |
| 259 | __clear_retrans_timer(chan); |
| 260 | if (chan->monitor_timeout) { |
| 261 | l2cap_set_timer(chan, &chan->monitor_timer, |
| 262 | msecs_to_jiffies(chan->monitor_timeout)); |
| 263 | } |
| 264 | } |
| 265 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 266 | static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head, |
| 267 | u16 seq) |
| 268 | { |
| 269 | struct sk_buff *skb; |
| 270 | |
| 271 | skb_queue_walk(head, skb) { |
| 272 | if (bt_cb(skb)->control.txseq == seq) |
| 273 | return skb; |
| 274 | } |
| 275 | |
| 276 | return NULL; |
| 277 | } |
| 278 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 279 | /* ---- L2CAP sequence number lists ---- */ |
| 280 | |
| 281 | /* For ERTM, ordered lists of sequence numbers must be tracked for |
| 282 | * SREJ requests that are received and for frames that are to be |
| 283 | * retransmitted. These seq_list functions implement a singly-linked |
| 284 | * list in an array, where membership in the list can also be checked |
| 285 | * in constant time. Items can also be added to the tail of the list |
| 286 | * and removed from the head in constant time, without further memory |
| 287 | * allocs or frees. |
| 288 | */ |
| 289 | |
| 290 | static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size) |
| 291 | { |
| 292 | size_t alloc_size, i; |
| 293 | |
| 294 | /* Allocated size is a power of 2 to map sequence numbers |
| 295 | * (which may be up to 14 bits) in to a smaller array that is |
| 296 | * sized for the negotiated ERTM transmit windows. |
| 297 | */ |
| 298 | alloc_size = roundup_pow_of_two(size); |
| 299 | |
| 300 | seq_list->list = kmalloc(sizeof(u16) * alloc_size, GFP_KERNEL); |
| 301 | if (!seq_list->list) |
| 302 | return -ENOMEM; |
| 303 | |
| 304 | seq_list->mask = alloc_size - 1; |
| 305 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 306 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
| 307 | for (i = 0; i < alloc_size; i++) |
| 308 | seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list) |
| 314 | { |
| 315 | kfree(seq_list->list); |
| 316 | } |
| 317 | |
| 318 | static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list, |
| 319 | u16 seq) |
| 320 | { |
| 321 | /* Constant-time check for list membership */ |
| 322 | return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR; |
| 323 | } |
| 324 | |
| 325 | static u16 l2cap_seq_list_remove(struct l2cap_seq_list *seq_list, u16 seq) |
| 326 | { |
| 327 | u16 mask = seq_list->mask; |
| 328 | |
| 329 | if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) { |
| 330 | /* In case someone tries to pop the head of an empty list */ |
| 331 | return L2CAP_SEQ_LIST_CLEAR; |
| 332 | } else if (seq_list->head == seq) { |
| 333 | /* Head can be removed in constant time */ |
| 334 | seq_list->head = seq_list->list[seq & mask]; |
| 335 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR; |
| 336 | |
| 337 | if (seq_list->head == L2CAP_SEQ_LIST_TAIL) { |
| 338 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 339 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
| 340 | } |
| 341 | } else { |
| 342 | /* Walk the list to find the sequence number */ |
| 343 | u16 prev = seq_list->head; |
| 344 | while (seq_list->list[prev & mask] != seq) { |
| 345 | prev = seq_list->list[prev & mask]; |
| 346 | if (prev == L2CAP_SEQ_LIST_TAIL) |
| 347 | return L2CAP_SEQ_LIST_CLEAR; |
| 348 | } |
| 349 | |
| 350 | /* Unlink the number from the list and clear it */ |
| 351 | seq_list->list[prev & mask] = seq_list->list[seq & mask]; |
| 352 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR; |
| 353 | if (seq_list->tail == seq) |
| 354 | seq_list->tail = prev; |
| 355 | } |
| 356 | return seq; |
| 357 | } |
| 358 | |
| 359 | static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list) |
| 360 | { |
| 361 | /* Remove the head in constant time */ |
| 362 | return l2cap_seq_list_remove(seq_list, seq_list->head); |
| 363 | } |
| 364 | |
| 365 | static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list) |
| 366 | { |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 367 | u16 i; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 368 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 369 | if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) |
| 370 | return; |
| 371 | |
| 372 | for (i = 0; i <= seq_list->mask; i++) |
| 373 | seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; |
| 374 | |
| 375 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 376 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq) |
| 380 | { |
| 381 | u16 mask = seq_list->mask; |
| 382 | |
| 383 | /* All appends happen in constant time */ |
| 384 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 385 | if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR) |
| 386 | return; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 387 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 388 | if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR) |
| 389 | seq_list->head = seq; |
| 390 | else |
| 391 | seq_list->list[seq_list->tail & mask] = seq; |
| 392 | |
| 393 | seq_list->tail = seq; |
| 394 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 397 | static void l2cap_chan_timeout(struct work_struct *work) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 398 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 399 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 400 | chan_timer.work); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 401 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 402 | int reason; |
| 403 | |
Andrei Emeltchenko | e05dcc3 | 2012-02-17 11:40:56 +0200 | [diff] [blame] | 404 | 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] | 405 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 406 | mutex_lock(&conn->chan_lock); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 407 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 408 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 409 | if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 410 | reason = ECONNREFUSED; |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 411 | else if (chan->state == BT_CONNECT && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 412 | chan->sec_level != BT_SECURITY_SDP) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 413 | reason = ECONNREFUSED; |
| 414 | else |
| 415 | reason = ETIMEDOUT; |
| 416 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 417 | l2cap_chan_close(chan, reason); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 418 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 419 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 420 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 421 | chan->ops->close(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 422 | mutex_unlock(&conn->chan_lock); |
| 423 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 424 | l2cap_chan_put(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 425 | } |
| 426 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 427 | struct l2cap_chan *l2cap_chan_create(void) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 428 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 429 | struct l2cap_chan *chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 430 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 431 | chan = kzalloc(sizeof(*chan), GFP_ATOMIC); |
| 432 | if (!chan) |
| 433 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 434 | |
Andrei Emeltchenko | c03b355 | 2012-02-21 12:54:56 +0200 | [diff] [blame] | 435 | mutex_init(&chan->lock); |
| 436 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 437 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 438 | list_add(&chan->global_l, &chan_list); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 439 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 440 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 441 | INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 442 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 443 | chan->state = BT_OPEN; |
| 444 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 445 | kref_init(&chan->kref); |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 446 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 447 | /* This flag is cleared in l2cap_chan_ready() */ |
| 448 | set_bit(CONF_NOT_COMPLETE, &chan->conf_state); |
| 449 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 450 | BT_DBG("chan %p", chan); |
Szymon Janc | abc545b | 2011-11-03 16:05:44 +0100 | [diff] [blame] | 451 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 452 | return chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 455 | static void l2cap_chan_destroy(struct kref *kref) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 456 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 457 | struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref); |
| 458 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 459 | BT_DBG("chan %p", chan); |
| 460 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 461 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 462 | list_del(&chan->global_l); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 463 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 464 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 465 | kfree(chan); |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 466 | } |
| 467 | |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 468 | void l2cap_chan_hold(struct l2cap_chan *c) |
| 469 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 470 | 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] | 471 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 472 | kref_get(&c->kref); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | void l2cap_chan_put(struct l2cap_chan *c) |
| 476 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 477 | 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] | 478 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 479 | kref_put(&c->kref, l2cap_chan_destroy); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 480 | } |
| 481 | |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 482 | void l2cap_chan_set_defaults(struct l2cap_chan *chan) |
| 483 | { |
| 484 | chan->fcs = L2CAP_FCS_CRC16; |
| 485 | chan->max_tx = L2CAP_DEFAULT_MAX_TX; |
| 486 | chan->tx_win = L2CAP_DEFAULT_TX_WINDOW; |
| 487 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 488 | chan->ack_win = L2CAP_DEFAULT_TX_WINDOW; |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 489 | chan->sec_level = BT_SECURITY_LOW; |
| 490 | |
| 491 | set_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 492 | } |
| 493 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 494 | void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 495 | { |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 496 | 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] | 497 | __le16_to_cpu(chan->psm), chan->dcid); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 498 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 499 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 500 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 501 | chan->conn = conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 502 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 503 | switch (chan->chan_type) { |
| 504 | case L2CAP_CHAN_CONN_ORIENTED: |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 505 | if (conn->hcon->type == LE_LINK) { |
| 506 | /* LE connection */ |
Andre Guedes | 6fcb06a | 2012-05-31 17:01:33 -0300 | [diff] [blame] | 507 | chan->omtu = L2CAP_DEFAULT_MTU; |
Johan Hedberg | 9f22398c | 2013-04-29 19:35:38 +0300 | [diff] [blame] | 508 | if (chan->dcid == L2CAP_CID_ATT) |
| 509 | chan->scid = L2CAP_CID_ATT; |
| 510 | else |
| 511 | chan->scid = l2cap_alloc_cid(conn); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 512 | } else { |
| 513 | /* Alloc CID for connection-oriented socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 514 | chan->scid = l2cap_alloc_cid(conn); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 515 | chan->omtu = L2CAP_DEFAULT_MTU; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 516 | } |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 517 | break; |
| 518 | |
| 519 | case L2CAP_CHAN_CONN_LESS: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 520 | /* Connectionless socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 521 | chan->scid = L2CAP_CID_CONN_LESS; |
| 522 | chan->dcid = L2CAP_CID_CONN_LESS; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 523 | chan->omtu = L2CAP_DEFAULT_MTU; |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 524 | break; |
| 525 | |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 526 | case L2CAP_CHAN_CONN_FIX_A2MP: |
| 527 | chan->scid = L2CAP_CID_A2MP; |
| 528 | chan->dcid = L2CAP_CID_A2MP; |
| 529 | chan->omtu = L2CAP_A2MP_DEFAULT_MTU; |
| 530 | chan->imtu = L2CAP_A2MP_DEFAULT_MTU; |
| 531 | break; |
| 532 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 533 | default: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 534 | /* Raw socket can send/recv signalling messages only */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 535 | chan->scid = L2CAP_CID_SIGNALING; |
| 536 | chan->dcid = L2CAP_CID_SIGNALING; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 537 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 540 | chan->local_id = L2CAP_BESTEFFORT_ID; |
| 541 | chan->local_stype = L2CAP_SERV_BESTEFFORT; |
| 542 | chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; |
| 543 | chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; |
| 544 | chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 545 | chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO; |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 546 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 547 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 548 | |
Johan Hedberg | 5ee9891 | 2013-04-29 19:35:43 +0300 | [diff] [blame] | 549 | hci_conn_hold(conn->hcon); |
| 550 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 551 | list_add(&chan->list, &conn->chan_l); |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 552 | } |
| 553 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 554 | void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 555 | { |
| 556 | mutex_lock(&conn->chan_lock); |
| 557 | __l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 558 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 561 | void l2cap_chan_del(struct l2cap_chan *chan, int err) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 562 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 563 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 564 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 565 | __clear_chan_timer(chan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 566 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 567 | BT_DBG("chan %p, conn %p, err %d", chan, conn, err); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 568 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 569 | if (conn) { |
Andrei Emeltchenko | 56f6098 | 2012-10-15 11:58:44 +0300 | [diff] [blame] | 570 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 571 | /* Delete from channel list */ |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 572 | list_del(&chan->list); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 573 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 574 | l2cap_chan_put(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 575 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 576 | chan->conn = NULL; |
Andrei Emeltchenko | 3cabbfd | 2012-05-31 11:01:37 +0300 | [diff] [blame] | 577 | |
| 578 | if (chan->chan_type != L2CAP_CHAN_CONN_FIX_A2MP) |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 579 | hci_conn_drop(conn->hcon); |
Andrei Emeltchenko | 56f6098 | 2012-10-15 11:58:44 +0300 | [diff] [blame] | 580 | |
| 581 | if (mgr && mgr->bredr_chan == chan) |
| 582 | mgr->bredr_chan = NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Andrei Emeltchenko | 419e08c | 2012-10-31 15:46:34 +0200 | [diff] [blame] | 585 | if (chan->hs_hchan) { |
| 586 | struct hci_chan *hs_hchan = chan->hs_hchan; |
| 587 | |
| 588 | BT_DBG("chan %p disconnect hs_hchan %p", chan, hs_hchan); |
| 589 | amp_disconnect_logical_link(hs_hchan); |
| 590 | } |
| 591 | |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 592 | chan->ops->teardown(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 593 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 594 | if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 595 | return; |
Gustavo F. Padovan | 2ead70b | 2011-04-01 15:13:36 -0300 | [diff] [blame] | 596 | |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 597 | switch(chan->mode) { |
| 598 | case L2CAP_MODE_BASIC: |
| 599 | break; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 600 | |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 601 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 602 | __clear_retrans_timer(chan); |
| 603 | __clear_monitor_timer(chan); |
| 604 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 605 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 606 | skb_queue_purge(&chan->srej_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 607 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 608 | l2cap_seq_list_free(&chan->srej_list); |
| 609 | l2cap_seq_list_free(&chan->retrans_list); |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 610 | |
| 611 | /* fall through */ |
| 612 | |
| 613 | case L2CAP_MODE_STREAMING: |
| 614 | skb_queue_purge(&chan->tx_q); |
| 615 | break; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 616 | } |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 617 | |
| 618 | return; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 619 | } |
| 620 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 621 | void l2cap_chan_close(struct l2cap_chan *chan, int reason) |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 622 | { |
| 623 | struct l2cap_conn *conn = chan->conn; |
| 624 | struct sock *sk = chan->sk; |
| 625 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 626 | BT_DBG("chan %p state %s sk %p", chan, state_to_string(chan->state), |
| 627 | sk); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 628 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 629 | switch (chan->state) { |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 630 | case BT_LISTEN: |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 631 | chan->ops->teardown(chan, 0); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 632 | break; |
| 633 | |
| 634 | case BT_CONNECTED: |
| 635 | case BT_CONFIG: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 636 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 637 | conn->hcon->type == ACL_LINK) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 638 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 639 | l2cap_send_disconn_req(chan, reason); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 640 | } else |
| 641 | l2cap_chan_del(chan, reason); |
| 642 | break; |
| 643 | |
| 644 | case BT_CONNECT2: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 645 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 646 | conn->hcon->type == ACL_LINK) { |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 647 | struct l2cap_conn_rsp rsp; |
| 648 | __u16 result; |
| 649 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 650 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 651 | result = L2CAP_CR_SEC_BLOCK; |
| 652 | else |
| 653 | result = L2CAP_CR_BAD_PSM; |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 654 | l2cap_state_change(chan, BT_DISCONN); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 655 | |
| 656 | rsp.scid = cpu_to_le16(chan->dcid); |
| 657 | rsp.dcid = cpu_to_le16(chan->scid); |
| 658 | rsp.result = cpu_to_le16(result); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 659 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 660 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 661 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | l2cap_chan_del(chan, reason); |
| 665 | break; |
| 666 | |
| 667 | case BT_CONNECT: |
| 668 | case BT_DISCONN: |
| 669 | l2cap_chan_del(chan, reason); |
| 670 | break; |
| 671 | |
| 672 | default: |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 673 | chan->ops->teardown(chan, 0); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 674 | break; |
| 675 | } |
| 676 | } |
| 677 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 678 | static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 679 | { |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 680 | switch (chan->chan_type) { |
| 681 | case L2CAP_CHAN_RAW: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 682 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 683 | case BT_SECURITY_HIGH: |
| 684 | return HCI_AT_DEDICATED_BONDING_MITM; |
| 685 | case BT_SECURITY_MEDIUM: |
| 686 | return HCI_AT_DEDICATED_BONDING; |
| 687 | default: |
| 688 | return HCI_AT_NO_BONDING; |
| 689 | } |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 690 | break; |
Marcel Holtmann | 3124b84 | 2013-10-12 07:19:32 -0700 | [diff] [blame] | 691 | case L2CAP_CHAN_CONN_LESS: |
| 692 | if (chan->psm == __constant_cpu_to_le16(L2CAP_PSM_3DSP)) { |
| 693 | if (chan->sec_level == BT_SECURITY_LOW) |
| 694 | chan->sec_level = BT_SECURITY_SDP; |
| 695 | } |
| 696 | if (chan->sec_level == BT_SECURITY_HIGH) |
| 697 | return HCI_AT_NO_BONDING_MITM; |
| 698 | else |
| 699 | return HCI_AT_NO_BONDING; |
| 700 | break; |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 701 | case L2CAP_CHAN_CONN_ORIENTED: |
| 702 | if (chan->psm == __constant_cpu_to_le16(L2CAP_PSM_SDP)) { |
| 703 | if (chan->sec_level == BT_SECURITY_LOW) |
| 704 | chan->sec_level = BT_SECURITY_SDP; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 705 | |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 706 | if (chan->sec_level == BT_SECURITY_HIGH) |
| 707 | return HCI_AT_NO_BONDING_MITM; |
| 708 | else |
| 709 | return HCI_AT_NO_BONDING; |
| 710 | } |
| 711 | /* fall through */ |
| 712 | default: |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 713 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 714 | case BT_SECURITY_HIGH: |
| 715 | return HCI_AT_GENERAL_BONDING_MITM; |
| 716 | case BT_SECURITY_MEDIUM: |
| 717 | return HCI_AT_GENERAL_BONDING; |
| 718 | default: |
| 719 | return HCI_AT_NO_BONDING; |
| 720 | } |
Marcel Holtmann | 6a974b5 | 2013-10-12 07:19:31 -0700 | [diff] [blame] | 721 | break; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 722 | } |
| 723 | } |
| 724 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 725 | /* Service level security */ |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 726 | int l2cap_chan_check_security(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 727 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 728 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 729 | __u8 auth_type; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 730 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 731 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 732 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 733 | return hci_conn_security(conn->hcon, chan->sec_level, auth_type); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 734 | } |
| 735 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 736 | static u8 l2cap_get_ident(struct l2cap_conn *conn) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 737 | { |
| 738 | u8 id; |
| 739 | |
| 740 | /* Get next available identificator. |
| 741 | * 1 - 128 are used by kernel. |
| 742 | * 129 - 199 are reserved. |
| 743 | * 200 - 254 are used by utilities like l2ping, etc. |
| 744 | */ |
| 745 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 746 | spin_lock(&conn->lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 747 | |
| 748 | if (++conn->tx_ident > 128) |
| 749 | conn->tx_ident = 1; |
| 750 | |
| 751 | id = conn->tx_ident; |
| 752 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 753 | spin_unlock(&conn->lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 754 | |
| 755 | return id; |
| 756 | } |
| 757 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 758 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, |
| 759 | void *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 760 | { |
| 761 | struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 762 | u8 flags; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 763 | |
| 764 | BT_DBG("code 0x%2.2x", code); |
| 765 | |
| 766 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 767 | return; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 768 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 769 | if (lmp_no_flush_capable(conn->hcon->hdev)) |
| 770 | flags = ACL_START_NO_FLUSH; |
| 771 | else |
| 772 | flags = ACL_START; |
| 773 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 774 | bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 775 | skb->priority = HCI_PRIO_MAX; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 776 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 777 | hci_send_acl(conn->hchan, skb, flags); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 778 | } |
| 779 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 780 | static bool __chan_is_moving(struct l2cap_chan *chan) |
| 781 | { |
| 782 | return chan->move_state != L2CAP_MOVE_STABLE && |
| 783 | chan->move_state != L2CAP_MOVE_WAIT_PREPARE; |
| 784 | } |
| 785 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 786 | static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) |
| 787 | { |
| 788 | struct hci_conn *hcon = chan->conn->hcon; |
| 789 | u16 flags; |
| 790 | |
| 791 | 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] | 792 | skb->priority); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 793 | |
Mat Martineau | d5f8a75 | 2012-10-23 15:24:18 -0700 | [diff] [blame] | 794 | if (chan->hs_hcon && !__chan_is_moving(chan)) { |
| 795 | if (chan->hs_hchan) |
| 796 | hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE); |
| 797 | else |
| 798 | kfree_skb(skb); |
| 799 | |
| 800 | return; |
| 801 | } |
| 802 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 803 | if (!test_bit(FLAG_FLUSHABLE, &chan->flags) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 804 | lmp_no_flush_capable(hcon->hdev)) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 805 | flags = ACL_START_NO_FLUSH; |
| 806 | else |
| 807 | flags = ACL_START; |
| 808 | |
| 809 | bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 810 | hci_send_acl(chan->conn->hchan, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 813 | static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control) |
| 814 | { |
| 815 | control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT; |
| 816 | control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT; |
| 817 | |
| 818 | if (enh & L2CAP_CTRL_FRAME_TYPE) { |
| 819 | /* S-Frame */ |
| 820 | control->sframe = 1; |
| 821 | control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT; |
| 822 | control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT; |
| 823 | |
| 824 | control->sar = 0; |
| 825 | control->txseq = 0; |
| 826 | } else { |
| 827 | /* I-Frame */ |
| 828 | control->sframe = 0; |
| 829 | control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT; |
| 830 | control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT; |
| 831 | |
| 832 | control->poll = 0; |
| 833 | control->super = 0; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control) |
| 838 | { |
| 839 | control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT; |
| 840 | control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT; |
| 841 | |
| 842 | if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) { |
| 843 | /* S-Frame */ |
| 844 | control->sframe = 1; |
| 845 | control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT; |
| 846 | control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT; |
| 847 | |
| 848 | control->sar = 0; |
| 849 | control->txseq = 0; |
| 850 | } else { |
| 851 | /* I-Frame */ |
| 852 | control->sframe = 0; |
| 853 | control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT; |
| 854 | control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT; |
| 855 | |
| 856 | control->poll = 0; |
| 857 | control->super = 0; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | static inline void __unpack_control(struct l2cap_chan *chan, |
| 862 | struct sk_buff *skb) |
| 863 | { |
| 864 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 865 | __unpack_extended_control(get_unaligned_le32(skb->data), |
| 866 | &bt_cb(skb)->control); |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 867 | skb_pull(skb, L2CAP_EXT_CTRL_SIZE); |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 868 | } else { |
| 869 | __unpack_enhanced_control(get_unaligned_le16(skb->data), |
| 870 | &bt_cb(skb)->control); |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 871 | skb_pull(skb, L2CAP_ENH_CTRL_SIZE); |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
| 875 | static u32 __pack_extended_control(struct l2cap_ctrl *control) |
| 876 | { |
| 877 | u32 packed; |
| 878 | |
| 879 | packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT; |
| 880 | packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT; |
| 881 | |
| 882 | if (control->sframe) { |
| 883 | packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT; |
| 884 | packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT; |
| 885 | packed |= L2CAP_EXT_CTRL_FRAME_TYPE; |
| 886 | } else { |
| 887 | packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT; |
| 888 | packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT; |
| 889 | } |
| 890 | |
| 891 | return packed; |
| 892 | } |
| 893 | |
| 894 | static u16 __pack_enhanced_control(struct l2cap_ctrl *control) |
| 895 | { |
| 896 | u16 packed; |
| 897 | |
| 898 | packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT; |
| 899 | packed |= control->final << L2CAP_CTRL_FINAL_SHIFT; |
| 900 | |
| 901 | if (control->sframe) { |
| 902 | packed |= control->poll << L2CAP_CTRL_POLL_SHIFT; |
| 903 | packed |= control->super << L2CAP_CTRL_SUPER_SHIFT; |
| 904 | packed |= L2CAP_CTRL_FRAME_TYPE; |
| 905 | } else { |
| 906 | packed |= control->sar << L2CAP_CTRL_SAR_SHIFT; |
| 907 | packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT; |
| 908 | } |
| 909 | |
| 910 | return packed; |
| 911 | } |
| 912 | |
| 913 | static inline void __pack_control(struct l2cap_chan *chan, |
| 914 | struct l2cap_ctrl *control, |
| 915 | struct sk_buff *skb) |
| 916 | { |
| 917 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 918 | put_unaligned_le32(__pack_extended_control(control), |
| 919 | skb->data + L2CAP_HDR_SIZE); |
| 920 | } else { |
| 921 | put_unaligned_le16(__pack_enhanced_control(control), |
| 922 | skb->data + L2CAP_HDR_SIZE); |
| 923 | } |
| 924 | } |
| 925 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 926 | static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan) |
| 927 | { |
| 928 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 929 | return L2CAP_EXT_HDR_SIZE; |
| 930 | else |
| 931 | return L2CAP_ENH_HDR_SIZE; |
| 932 | } |
| 933 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 934 | static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan, |
| 935 | u32 control) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 936 | { |
| 937 | struct sk_buff *skb; |
| 938 | struct l2cap_hdr *lh; |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 939 | int hlen = __ertm_hdr_size(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 940 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 941 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 942 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 943 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 944 | skb = bt_skb_alloc(hlen, GFP_KERNEL); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 945 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 946 | if (!skb) |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 947 | return ERR_PTR(-ENOMEM); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 948 | |
| 949 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 950 | lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 951 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 952 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 953 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 954 | put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 955 | else |
| 956 | put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 957 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 958 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 959 | u16 fcs = crc16(0, (u8 *)skb->data, skb->len); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 960 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 961 | } |
| 962 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 963 | skb->priority = HCI_PRIO_MAX; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 964 | return skb; |
| 965 | } |
| 966 | |
| 967 | static void l2cap_send_sframe(struct l2cap_chan *chan, |
| 968 | struct l2cap_ctrl *control) |
| 969 | { |
| 970 | struct sk_buff *skb; |
| 971 | u32 control_field; |
| 972 | |
| 973 | BT_DBG("chan %p, control %p", chan, control); |
| 974 | |
| 975 | if (!control->sframe) |
| 976 | return; |
| 977 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 978 | if (__chan_is_moving(chan)) |
| 979 | return; |
| 980 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 981 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) && |
| 982 | !control->poll) |
| 983 | control->final = 1; |
| 984 | |
| 985 | if (control->super == L2CAP_SUPER_RR) |
| 986 | clear_bit(CONN_RNR_SENT, &chan->conn_state); |
| 987 | else if (control->super == L2CAP_SUPER_RNR) |
| 988 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
| 989 | |
| 990 | if (control->super != L2CAP_SUPER_SREJ) { |
| 991 | chan->last_acked_seq = control->reqseq; |
| 992 | __clear_ack_timer(chan); |
| 993 | } |
| 994 | |
| 995 | BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq, |
| 996 | control->final, control->poll, control->super); |
| 997 | |
| 998 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 999 | control_field = __pack_extended_control(control); |
| 1000 | else |
| 1001 | control_field = __pack_enhanced_control(control); |
| 1002 | |
| 1003 | skb = l2cap_create_sframe_pdu(chan, control_field); |
| 1004 | if (!IS_ERR(skb)) |
| 1005 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1006 | } |
| 1007 | |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1008 | 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] | 1009 | { |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1010 | struct l2cap_ctrl control; |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 1011 | |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 1012 | BT_DBG("chan %p, poll %d", chan, poll); |
| 1013 | |
| 1014 | memset(&control, 0, sizeof(control)); |
| 1015 | control.sframe = 1; |
| 1016 | control.poll = poll; |
| 1017 | |
| 1018 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 1019 | control.super = L2CAP_SUPER_RNR; |
| 1020 | else |
| 1021 | control.super = L2CAP_SUPER_RR; |
| 1022 | |
| 1023 | control.reqseq = chan->buffer_seq; |
| 1024 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 1025 | } |
| 1026 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1027 | static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 1028 | { |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1029 | return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 1030 | } |
| 1031 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1032 | static bool __amp_capable(struct l2cap_chan *chan) |
| 1033 | { |
| 1034 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 1df7b17 | 2013-10-05 11:47:49 -0700 | [diff] [blame] | 1035 | struct hci_dev *hdev; |
| 1036 | bool amp_available = false; |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1037 | |
Marcel Holtmann | 1df7b17 | 2013-10-05 11:47:49 -0700 | [diff] [blame] | 1038 | if (!conn->hs_enabled) |
| 1039 | return false; |
| 1040 | |
| 1041 | if (!(conn->fixed_chan_mask & L2CAP_FC_A2MP)) |
| 1042 | return false; |
| 1043 | |
| 1044 | read_lock(&hci_dev_list_lock); |
| 1045 | list_for_each_entry(hdev, &hci_dev_list, list) { |
| 1046 | if (hdev->amp_type != AMP_TYPE_BREDR && |
| 1047 | test_bit(HCI_UP, &hdev->flags)) { |
| 1048 | amp_available = true; |
| 1049 | break; |
| 1050 | } |
| 1051 | } |
| 1052 | read_unlock(&hci_dev_list_lock); |
| 1053 | |
| 1054 | if (chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED) |
| 1055 | return amp_available; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 1056 | |
| 1057 | return false; |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1058 | } |
| 1059 | |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 1060 | static bool l2cap_check_efs(struct l2cap_chan *chan) |
| 1061 | { |
| 1062 | /* Check EFS parameters */ |
| 1063 | return true; |
| 1064 | } |
| 1065 | |
Andrei Emeltchenko | 2766be4 | 2012-09-27 17:26:21 +0300 | [diff] [blame] | 1066 | void l2cap_send_conn_req(struct l2cap_chan *chan) |
Andrei Emeltchenko | 9b27f35 | 2012-02-24 16:00:00 +0200 | [diff] [blame] | 1067 | { |
| 1068 | struct l2cap_conn *conn = chan->conn; |
| 1069 | struct l2cap_conn_req req; |
| 1070 | |
| 1071 | req.scid = cpu_to_le16(chan->scid); |
| 1072 | req.psm = chan->psm; |
| 1073 | |
| 1074 | chan->ident = l2cap_get_ident(conn); |
| 1075 | |
| 1076 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
| 1077 | |
| 1078 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req); |
| 1079 | } |
| 1080 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 1081 | static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id) |
| 1082 | { |
| 1083 | struct l2cap_create_chan_req req; |
| 1084 | req.scid = cpu_to_le16(chan->scid); |
| 1085 | req.psm = chan->psm; |
| 1086 | req.amp_id = amp_id; |
| 1087 | |
| 1088 | chan->ident = l2cap_get_ident(chan->conn); |
| 1089 | |
| 1090 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ, |
| 1091 | sizeof(req), &req); |
| 1092 | } |
| 1093 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 1094 | static void l2cap_move_setup(struct l2cap_chan *chan) |
| 1095 | { |
| 1096 | struct sk_buff *skb; |
| 1097 | |
| 1098 | BT_DBG("chan %p", chan); |
| 1099 | |
| 1100 | if (chan->mode != L2CAP_MODE_ERTM) |
| 1101 | return; |
| 1102 | |
| 1103 | __clear_retrans_timer(chan); |
| 1104 | __clear_monitor_timer(chan); |
| 1105 | __clear_ack_timer(chan); |
| 1106 | |
| 1107 | chan->retry_count = 0; |
| 1108 | skb_queue_walk(&chan->tx_q, skb) { |
| 1109 | if (bt_cb(skb)->control.retries) |
| 1110 | bt_cb(skb)->control.retries = 1; |
| 1111 | else |
| 1112 | break; |
| 1113 | } |
| 1114 | |
| 1115 | chan->expected_tx_seq = chan->buffer_seq; |
| 1116 | |
| 1117 | clear_bit(CONN_REJ_ACT, &chan->conn_state); |
| 1118 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 1119 | l2cap_seq_list_clear(&chan->retrans_list); |
| 1120 | l2cap_seq_list_clear(&chan->srej_list); |
| 1121 | skb_queue_purge(&chan->srej_q); |
| 1122 | |
| 1123 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 1124 | chan->rx_state = L2CAP_RX_STATE_MOVE; |
| 1125 | |
| 1126 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 1127 | } |
| 1128 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 1129 | static void l2cap_move_done(struct l2cap_chan *chan) |
| 1130 | { |
| 1131 | u8 move_role = chan->move_role; |
| 1132 | BT_DBG("chan %p", chan); |
| 1133 | |
| 1134 | chan->move_state = L2CAP_MOVE_STABLE; |
| 1135 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 1136 | |
| 1137 | if (chan->mode != L2CAP_MODE_ERTM) |
| 1138 | return; |
| 1139 | |
| 1140 | switch (move_role) { |
| 1141 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 1142 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL); |
| 1143 | chan->rx_state = L2CAP_RX_STATE_WAIT_F; |
| 1144 | break; |
| 1145 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 1146 | chan->rx_state = L2CAP_RX_STATE_WAIT_P; |
| 1147 | break; |
| 1148 | } |
| 1149 | } |
| 1150 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1151 | static void l2cap_chan_ready(struct l2cap_chan *chan) |
| 1152 | { |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 1153 | /* This clears all conf flags, including CONF_NOT_COMPLETE */ |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1154 | chan->conf_state = 0; |
| 1155 | __clear_chan_timer(chan); |
| 1156 | |
Andrei Emeltchenko | 54a59aa | 2012-05-27 22:27:53 -0300 | [diff] [blame] | 1157 | chan->state = BT_CONNECTED; |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1158 | |
Andrei Emeltchenko | fd83e2c | 2012-05-30 09:55:32 +0300 | [diff] [blame] | 1159 | chan->ops->ready(chan); |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1160 | } |
| 1161 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1162 | static void l2cap_start_connection(struct l2cap_chan *chan) |
| 1163 | { |
| 1164 | if (__amp_capable(chan)) { |
| 1165 | BT_DBG("chan %p AMP capable: discover AMPs", chan); |
| 1166 | a2mp_discover_amp(chan); |
| 1167 | } else { |
| 1168 | l2cap_send_conn_req(chan); |
| 1169 | } |
| 1170 | } |
| 1171 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1172 | static void l2cap_do_start(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1173 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1174 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1175 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1176 | if (conn->hcon->type == LE_LINK) { |
| 1177 | l2cap_chan_ready(chan); |
| 1178 | return; |
| 1179 | } |
| 1180 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1181 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1182 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) |
| 1183 | return; |
| 1184 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1185 | if (l2cap_chan_check_security(chan) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1186 | __l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1187 | l2cap_start_connection(chan); |
| 1188 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1189 | } else { |
| 1190 | struct l2cap_info_req req; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 1191 | req.type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1192 | |
| 1193 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 1194 | conn->info_ident = l2cap_get_ident(conn); |
| 1195 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 1196 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1197 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1198 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 1199 | sizeof(req), &req); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 1203 | static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) |
| 1204 | { |
| 1205 | u32 local_feat_mask = l2cap_feat_mask; |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 1206 | if (!disable_ertm) |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 1207 | local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; |
| 1208 | |
| 1209 | switch (mode) { |
| 1210 | case L2CAP_MODE_ERTM: |
| 1211 | return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; |
| 1212 | case L2CAP_MODE_STREAMING: |
| 1213 | return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; |
| 1214 | default: |
| 1215 | return 0x00; |
| 1216 | } |
| 1217 | } |
| 1218 | |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1219 | 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] | 1220 | { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1221 | struct sock *sk = chan->sk; |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1222 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1223 | struct l2cap_disconn_req req; |
| 1224 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1225 | if (!conn) |
| 1226 | return; |
| 1227 | |
Andrei Emeltchenko | aad3d0e | 2012-09-06 15:05:42 +0300 | [diff] [blame] | 1228 | if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1229 | __clear_retrans_timer(chan); |
| 1230 | __clear_monitor_timer(chan); |
| 1231 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1232 | } |
| 1233 | |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1234 | if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) { |
Gustavo Padovan | d117773 | 2012-10-06 11:47:38 +0100 | [diff] [blame] | 1235 | l2cap_state_change(chan, BT_DISCONN); |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1236 | return; |
| 1237 | } |
| 1238 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1239 | req.dcid = cpu_to_le16(chan->dcid); |
| 1240 | req.scid = cpu_to_le16(chan->scid); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1241 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ, |
| 1242 | sizeof(req), &req); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1243 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1244 | lock_sock(sk); |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 1245 | __l2cap_state_change(chan, BT_DISCONN); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 1246 | __l2cap_chan_set_err(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1247 | release_sock(sk); |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1248 | } |
| 1249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | /* ---- L2CAP connections ---- */ |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1251 | static void l2cap_conn_start(struct l2cap_conn *conn) |
| 1252 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1253 | struct l2cap_chan *chan, *tmp; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1254 | |
| 1255 | BT_DBG("conn %p", conn); |
| 1256 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1257 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1258 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1259 | list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1260 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1261 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1262 | l2cap_chan_lock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1263 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1264 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1265 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1266 | continue; |
| 1267 | } |
| 1268 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1269 | if (chan->state == BT_CONNECT) { |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1270 | if (!l2cap_chan_check_security(chan) || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1271 | !__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1272 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1273 | continue; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 1274 | } |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1275 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1276 | if (!l2cap_mode_supported(chan->mode, conn->feat_mask) |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1277 | && test_bit(CONF_STATE2_DEVICE, |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1278 | &chan->conf_state)) { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1279 | l2cap_chan_close(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1280 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1281 | continue; |
| 1282 | } |
| 1283 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1284 | l2cap_start_connection(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1285 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1286 | } else if (chan->state == BT_CONNECT2) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1287 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1288 | char buf[128]; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1289 | rsp.scid = cpu_to_le16(chan->dcid); |
| 1290 | rsp.dcid = cpu_to_le16(chan->scid); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1291 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1292 | if (l2cap_chan_check_security(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1293 | lock_sock(sk); |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 1294 | if (test_bit(BT_SK_DEFER_SETUP, |
| 1295 | &bt_sk(sk)->flags)) { |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 1296 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_PEND); |
| 1297 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_AUTHOR_PEND); |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 1298 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 1299 | |
| 1300 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 1301 | __l2cap_state_change(chan, BT_CONFIG); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 1302 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS); |
| 1303 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 1304 | } |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1305 | release_sock(sk); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1306 | } else { |
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_AUTHEN_PEND); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1309 | } |
| 1310 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1311 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1312 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1313 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1314 | if (test_bit(CONF_REQ_SENT, &chan->conf_state) || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1315 | rsp.result != L2CAP_CR_SUCCESS) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1316 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1317 | continue; |
| 1318 | } |
| 1319 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1320 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1321 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1322 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1323 | chan->num_conf_req++; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1324 | } |
| 1325 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1326 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1327 | } |
| 1328 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1329 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1330 | } |
| 1331 | |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1332 | /* Find socket with cid and source/destination bdaddr. |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1333 | * Returns closest match, locked. |
| 1334 | */ |
Andrei Emeltchenko | d9b8870 | 2012-03-12 12:13:08 +0200 | [diff] [blame] | 1335 | 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] | 1336 | bdaddr_t *src, |
| 1337 | bdaddr_t *dst) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1338 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1339 | struct l2cap_chan *c, *c1 = NULL; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1340 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1341 | read_lock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1342 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1343 | list_for_each_entry(c, &chan_list, global_l) { |
| 1344 | struct sock *sk = c->sk; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1345 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1346 | if (state && c->state != state) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1347 | continue; |
| 1348 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1349 | if (c->scid == cid) { |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1350 | int src_match, dst_match; |
| 1351 | int src_any, dst_any; |
| 1352 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1353 | /* Exact match. */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1354 | src_match = !bacmp(&bt_sk(sk)->src, src); |
| 1355 | dst_match = !bacmp(&bt_sk(sk)->dst, dst); |
| 1356 | if (src_match && dst_match) { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1357 | read_unlock(&chan_list_lock); |
| 1358 | return c; |
| 1359 | } |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1360 | |
| 1361 | /* Closest match */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1362 | src_any = !bacmp(&bt_sk(sk)->src, BDADDR_ANY); |
| 1363 | dst_any = !bacmp(&bt_sk(sk)->dst, BDADDR_ANY); |
| 1364 | if ((src_match && dst_any) || (src_any && dst_match) || |
| 1365 | (src_any && dst_any)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1366 | c1 = c; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1367 | } |
| 1368 | } |
Gustavo F. Padovan | 280f294 | 2011-04-13 19:01:22 -0300 | [diff] [blame] | 1369 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1370 | read_unlock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1371 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1372 | return c1; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | static void l2cap_le_conn_ready(struct l2cap_conn *conn) |
| 1376 | { |
Johan Hedberg | 60bac18 | 2013-04-29 19:35:41 +0300 | [diff] [blame] | 1377 | struct sock *parent; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1378 | struct l2cap_chan *chan, *pchan; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1379 | |
| 1380 | BT_DBG(""); |
| 1381 | |
| 1382 | /* Check if we have socket listening on cid */ |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame] | 1383 | pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_ATT, |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 1384 | &conn->hcon->src, &conn->hcon->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1385 | if (!pchan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1386 | return; |
| 1387 | |
Johan Hedberg | 44f3b0f | 2013-04-29 19:35:45 +0300 | [diff] [blame] | 1388 | /* Client ATT sockets should override the server one */ |
| 1389 | if (__l2cap_get_chan_by_dcid(conn, L2CAP_CID_ATT)) |
| 1390 | return; |
| 1391 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1392 | parent = pchan->sk; |
| 1393 | |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 1394 | lock_sock(parent); |
Gustavo F. Padovan | 62f3a2c | 2011-04-14 18:34:34 -0300 | [diff] [blame] | 1395 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 1396 | chan = pchan->ops->new_connection(pchan); |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1397 | if (!chan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1398 | goto clean; |
| 1399 | |
Johan Hedberg | 9f22398c | 2013-04-29 19:35:38 +0300 | [diff] [blame] | 1400 | chan->dcid = L2CAP_CID_ATT; |
| 1401 | |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 1402 | bacpy(&bt_sk(chan->sk)->src, &conn->hcon->src); |
Marcel Holtmann | 98e0f7e | 2013-10-13 02:23:40 -0700 | [diff] [blame] | 1403 | bacpy(&bt_sk(chan->sk)->dst, &conn->hcon->dst); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1404 | |
Johan Hedberg | 44f3b0f | 2013-04-29 19:35:45 +0300 | [diff] [blame] | 1405 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1406 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1407 | clean: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 1408 | release_sock(parent); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1409 | } |
| 1410 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1411 | static void l2cap_conn_ready(struct l2cap_conn *conn) |
| 1412 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1413 | struct l2cap_chan *chan; |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1414 | struct hci_conn *hcon = conn->hcon; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1415 | |
| 1416 | BT_DBG("conn %p", conn); |
| 1417 | |
Johan Hedberg | d872992 | 2013-04-29 19:35:39 +0300 | [diff] [blame] | 1418 | /* For outgoing pairing which doesn't necessarily have an |
| 1419 | * associated socket (e.g. mgmt_pair_device). |
| 1420 | */ |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1421 | if (hcon->out && hcon->type == LE_LINK) |
| 1422 | smp_conn_security(hcon, hcon->pending_sec_level); |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 1423 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1424 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1425 | |
Johan Hedberg | 44f3b0f | 2013-04-29 19:35:45 +0300 | [diff] [blame] | 1426 | if (hcon->type == LE_LINK) |
| 1427 | l2cap_le_conn_ready(conn); |
| 1428 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1429 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1430 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1431 | l2cap_chan_lock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1432 | |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1433 | if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) { |
| 1434 | l2cap_chan_unlock(chan); |
| 1435 | continue; |
| 1436 | } |
| 1437 | |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1438 | if (hcon->type == LE_LINK) { |
| 1439 | if (smp_conn_security(hcon, chan->sec_level)) |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 1440 | l2cap_chan_ready(chan); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1441 | |
Vinicius Costa Gomes | 6312845 | 2011-06-17 22:46:26 -0300 | [diff] [blame] | 1442 | } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1443 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1444 | __clear_chan_timer(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1445 | lock_sock(sk); |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 1446 | __l2cap_state_change(chan, BT_CONNECTED); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1447 | sk->sk_state_change(sk); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1448 | release_sock(sk); |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 1449 | |
Gustavo Padovan | 1c244f7 | 2012-12-07 03:29:10 -0200 | [diff] [blame] | 1450 | } else if (chan->state == BT_CONNECT) { |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1451 | l2cap_do_start(chan); |
Gustavo Padovan | 1c244f7 | 2012-12-07 03:29:10 -0200 | [diff] [blame] | 1452 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1453 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1454 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1455 | } |
| 1456 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1457 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1458 | } |
| 1459 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1460 | /* Notify sockets that we cannot guaranty reliability anymore */ |
| 1461 | static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) |
| 1462 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1463 | struct l2cap_chan *chan; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1464 | |
| 1465 | BT_DBG("conn %p", conn); |
| 1466 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1467 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1468 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1469 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | ecf61bd | 2011-10-11 14:04:32 +0300 | [diff] [blame] | 1470 | if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) |
Gustavo Padovan | 1d8b1fd | 2012-10-06 11:34:52 +0100 | [diff] [blame] | 1471 | l2cap_chan_set_err(chan, err); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1472 | } |
| 1473 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1474 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1475 | } |
| 1476 | |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1477 | static void l2cap_info_timeout(struct work_struct *work) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1478 | { |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1479 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1480 | info_timer.work); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1481 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1482 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 1483 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1484 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1485 | l2cap_conn_start(conn); |
| 1486 | } |
| 1487 | |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1488 | /* |
| 1489 | * l2cap_user |
| 1490 | * External modules can register l2cap_user objects on l2cap_conn. The ->probe |
| 1491 | * callback is called during registration. The ->remove callback is called |
| 1492 | * during unregistration. |
| 1493 | * An l2cap_user object can either be explicitly unregistered or when the |
| 1494 | * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon, |
| 1495 | * l2cap->hchan, .. are valid as long as the remove callback hasn't been called. |
| 1496 | * External modules must own a reference to the l2cap_conn object if they intend |
| 1497 | * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at |
| 1498 | * any time if they don't. |
| 1499 | */ |
| 1500 | |
| 1501 | int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user) |
| 1502 | { |
| 1503 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1504 | int ret; |
| 1505 | |
| 1506 | /* We need to check whether l2cap_conn is registered. If it is not, we |
| 1507 | * must not register the l2cap_user. l2cap_conn_del() is unregisters |
| 1508 | * l2cap_conn objects, but doesn't provide its own locking. Instead, it |
| 1509 | * relies on the parent hci_conn object to be locked. This itself relies |
| 1510 | * on the hci_dev object to be locked. So we must lock the hci device |
| 1511 | * here, too. */ |
| 1512 | |
| 1513 | hci_dev_lock(hdev); |
| 1514 | |
| 1515 | if (user->list.next || user->list.prev) { |
| 1516 | ret = -EINVAL; |
| 1517 | goto out_unlock; |
| 1518 | } |
| 1519 | |
| 1520 | /* conn->hchan is NULL after l2cap_conn_del() was called */ |
| 1521 | if (!conn->hchan) { |
| 1522 | ret = -ENODEV; |
| 1523 | goto out_unlock; |
| 1524 | } |
| 1525 | |
| 1526 | ret = user->probe(conn, user); |
| 1527 | if (ret) |
| 1528 | goto out_unlock; |
| 1529 | |
| 1530 | list_add(&user->list, &conn->users); |
| 1531 | ret = 0; |
| 1532 | |
| 1533 | out_unlock: |
| 1534 | hci_dev_unlock(hdev); |
| 1535 | return ret; |
| 1536 | } |
| 1537 | EXPORT_SYMBOL(l2cap_register_user); |
| 1538 | |
| 1539 | void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user) |
| 1540 | { |
| 1541 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1542 | |
| 1543 | hci_dev_lock(hdev); |
| 1544 | |
| 1545 | if (!user->list.next || !user->list.prev) |
| 1546 | goto out_unlock; |
| 1547 | |
| 1548 | list_del(&user->list); |
| 1549 | user->list.next = NULL; |
| 1550 | user->list.prev = NULL; |
| 1551 | user->remove(conn, user); |
| 1552 | |
| 1553 | out_unlock: |
| 1554 | hci_dev_unlock(hdev); |
| 1555 | } |
| 1556 | EXPORT_SYMBOL(l2cap_unregister_user); |
| 1557 | |
| 1558 | static void l2cap_unregister_all_users(struct l2cap_conn *conn) |
| 1559 | { |
| 1560 | struct l2cap_user *user; |
| 1561 | |
| 1562 | while (!list_empty(&conn->users)) { |
| 1563 | user = list_first_entry(&conn->users, struct l2cap_user, list); |
| 1564 | list_del(&user->list); |
| 1565 | user->list.next = NULL; |
| 1566 | user->list.prev = NULL; |
| 1567 | user->remove(conn, user); |
| 1568 | } |
| 1569 | } |
| 1570 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1571 | static void l2cap_conn_del(struct hci_conn *hcon, int err) |
| 1572 | { |
| 1573 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 1574 | struct l2cap_chan *chan, *l; |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1575 | |
| 1576 | if (!conn) |
| 1577 | return; |
| 1578 | |
| 1579 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 1580 | |
| 1581 | kfree_skb(conn->rx_skb); |
| 1582 | |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1583 | l2cap_unregister_all_users(conn); |
| 1584 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1585 | mutex_lock(&conn->chan_lock); |
| 1586 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1587 | /* Kill channels */ |
| 1588 | list_for_each_entry_safe(chan, l, &conn->chan_l, list) { |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1589 | l2cap_chan_hold(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1590 | l2cap_chan_lock(chan); |
| 1591 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1592 | l2cap_chan_del(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1593 | |
| 1594 | l2cap_chan_unlock(chan); |
| 1595 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 1596 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1597 | l2cap_chan_put(chan); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1598 | } |
| 1599 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1600 | mutex_unlock(&conn->chan_lock); |
| 1601 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1602 | hci_chan_del(conn->hchan); |
| 1603 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1604 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1605 | cancel_delayed_work_sync(&conn->info_timer); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1606 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1607 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) { |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1608 | cancel_delayed_work_sync(&conn->security_timer); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1609 | smp_chan_destroy(conn); |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1610 | } |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1611 | |
| 1612 | hcon->l2cap_data = NULL; |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1613 | conn->hchan = NULL; |
| 1614 | l2cap_conn_put(conn); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1615 | } |
| 1616 | |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1617 | static void security_timeout(struct work_struct *work) |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1618 | { |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1619 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1620 | security_timer.work); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1621 | |
Johan Hedberg | d06cc41 | 2012-06-06 18:44:11 +0800 | [diff] [blame] | 1622 | BT_DBG("conn %p", conn); |
| 1623 | |
| 1624 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) { |
| 1625 | smp_chan_destroy(conn); |
| 1626 | l2cap_conn_del(conn->hcon, ETIMEDOUT); |
| 1627 | } |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1628 | } |
| 1629 | |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 1630 | static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1632 | struct l2cap_conn *conn = hcon->l2cap_data; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1633 | struct hci_chan *hchan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 1635 | if (conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | return conn; |
| 1637 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1638 | hchan = hci_chan_create(hcon); |
| 1639 | if (!hchan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 1642 | conn = kzalloc(sizeof(struct l2cap_conn), GFP_KERNEL); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1643 | if (!conn) { |
| 1644 | hci_chan_del(hchan); |
| 1645 | return NULL; |
| 1646 | } |
| 1647 | |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1648 | kref_init(&conn->ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | hcon->l2cap_data = conn; |
| 1650 | conn->hcon = hcon; |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1651 | hci_conn_get(conn->hcon); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1652 | conn->hchan = hchan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1654 | BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1655 | |
Andrei Emeltchenko | dcc042d | 2012-10-05 16:56:57 +0300 | [diff] [blame] | 1656 | switch (hcon->type) { |
Andrei Emeltchenko | dcc042d | 2012-10-05 16:56:57 +0300 | [diff] [blame] | 1657 | case LE_LINK: |
| 1658 | if (hcon->hdev->le_mtu) { |
| 1659 | conn->mtu = hcon->hdev->le_mtu; |
| 1660 | break; |
| 1661 | } |
| 1662 | /* fall through */ |
Andrei Emeltchenko | dcc042d | 2012-10-05 16:56:57 +0300 | [diff] [blame] | 1663 | default: |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1664 | conn->mtu = hcon->hdev->acl_mtu; |
Andrei Emeltchenko | dcc042d | 2012-10-05 16:56:57 +0300 | [diff] [blame] | 1665 | break; |
| 1666 | } |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1667 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1668 | conn->feat_mask = 0; |
| 1669 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 1670 | if (hcon->type == ACL_LINK) |
| 1671 | conn->hs_enabled = test_bit(HCI_HS_ENABLED, |
| 1672 | &hcon->hdev->dev_flags); |
| 1673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | spin_lock_init(&conn->lock); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1675 | mutex_init(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1676 | |
| 1677 | INIT_LIST_HEAD(&conn->chan_l); |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1678 | INIT_LIST_HEAD(&conn->users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1680 | if (hcon->type == LE_LINK) |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1681 | INIT_DELAYED_WORK(&conn->security_timer, security_timeout); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1682 | else |
Gustavo F. Padovan | 030013d | 2011-12-20 10:57:28 -0200 | [diff] [blame] | 1683 | INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); |
Dave Young | 45054dc | 2009-10-18 20:28:30 +0000 | [diff] [blame] | 1684 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 1685 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 1686 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | return conn; |
| 1688 | } |
| 1689 | |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1690 | static void l2cap_conn_free(struct kref *ref) |
| 1691 | { |
| 1692 | struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref); |
| 1693 | |
| 1694 | hci_conn_put(conn->hcon); |
| 1695 | kfree(conn); |
| 1696 | } |
| 1697 | |
| 1698 | void l2cap_conn_get(struct l2cap_conn *conn) |
| 1699 | { |
| 1700 | kref_get(&conn->ref); |
| 1701 | } |
| 1702 | EXPORT_SYMBOL(l2cap_conn_get); |
| 1703 | |
| 1704 | void l2cap_conn_put(struct l2cap_conn *conn) |
| 1705 | { |
| 1706 | kref_put(&conn->ref, l2cap_conn_free); |
| 1707 | } |
| 1708 | EXPORT_SYMBOL(l2cap_conn_put); |
| 1709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | /* ---- Socket interface ---- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1712 | /* Find socket with psm and source / destination bdaddr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | * Returns closest match. |
| 1714 | */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1715 | static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, |
| 1716 | bdaddr_t *src, |
| 1717 | bdaddr_t *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1719 | struct l2cap_chan *c, *c1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1721 | read_lock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1722 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1723 | list_for_each_entry(c, &chan_list, global_l) { |
| 1724 | struct sock *sk = c->sk; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1725 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1726 | if (state && c->state != state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | continue; |
| 1728 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1729 | if (c->psm == psm) { |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1730 | int src_match, dst_match; |
| 1731 | int src_any, dst_any; |
| 1732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | /* Exact match. */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1734 | src_match = !bacmp(&bt_sk(sk)->src, src); |
| 1735 | dst_match = !bacmp(&bt_sk(sk)->dst, dst); |
| 1736 | if (src_match && dst_match) { |
Johannes Berg | a7567b2 | 2011-06-01 08:29:54 +0200 | [diff] [blame] | 1737 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1738 | return c; |
| 1739 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | |
| 1741 | /* Closest match */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1742 | src_any = !bacmp(&bt_sk(sk)->src, BDADDR_ANY); |
| 1743 | dst_any = !bacmp(&bt_sk(sk)->dst, BDADDR_ANY); |
| 1744 | if ((src_match && dst_any) || (src_any && dst_match) || |
| 1745 | (src_any && dst_any)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1746 | c1 = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | } |
| 1748 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1750 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1751 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1752 | return c1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | } |
| 1754 | |
Andre Guedes | 8e9f989 | 2012-04-24 21:02:55 -0300 | [diff] [blame] | 1755 | int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, |
| 1756 | bdaddr_t *dst, u8 dst_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | { |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 1758 | struct sock *sk = chan->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | bdaddr_t *src = &bt_sk(sk)->src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | struct l2cap_conn *conn; |
| 1761 | struct hci_conn *hcon; |
| 1762 | struct hci_dev *hdev; |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1763 | __u8 auth_type; |
Marcel Holtmann | 44d0e48 | 2009-04-20 07:09:16 +0200 | [diff] [blame] | 1764 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1766 | BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", src, dst, |
Syam Sidhardhan | ab19516 | 2012-07-27 23:51:22 +0530 | [diff] [blame] | 1767 | dst_type, __le16_to_cpu(psm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1769 | hdev = hci_get_route(dst, src); |
| 1770 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | return -EHOSTUNREACH; |
| 1772 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1773 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1775 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1776 | |
| 1777 | /* PSM must be odd and lsb of upper byte must be 0 */ |
| 1778 | if ((__le16_to_cpu(psm) & 0x0101) != 0x0001 && !cid && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1779 | chan->chan_type != L2CAP_CHAN_RAW) { |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1780 | err = -EINVAL; |
| 1781 | goto done; |
| 1782 | } |
| 1783 | |
| 1784 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !(psm || cid)) { |
| 1785 | err = -EINVAL; |
| 1786 | goto done; |
| 1787 | } |
| 1788 | |
| 1789 | switch (chan->mode) { |
| 1790 | case L2CAP_MODE_BASIC: |
| 1791 | break; |
| 1792 | case L2CAP_MODE_ERTM: |
| 1793 | case L2CAP_MODE_STREAMING: |
| 1794 | if (!disable_ertm) |
| 1795 | break; |
| 1796 | /* fall through */ |
| 1797 | default: |
| 1798 | err = -ENOTSUPP; |
| 1799 | goto done; |
| 1800 | } |
| 1801 | |
Gustavo Padovan | 0797e01 | 2012-05-27 22:27:54 -0300 | [diff] [blame] | 1802 | switch (chan->state) { |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1803 | case BT_CONNECT: |
| 1804 | case BT_CONNECT2: |
| 1805 | case BT_CONFIG: |
| 1806 | /* Already connecting */ |
| 1807 | err = 0; |
| 1808 | goto done; |
| 1809 | |
| 1810 | case BT_CONNECTED: |
| 1811 | /* Already connected */ |
| 1812 | err = -EISCONN; |
| 1813 | goto done; |
| 1814 | |
| 1815 | case BT_OPEN: |
| 1816 | case BT_BOUND: |
| 1817 | /* Can connect */ |
| 1818 | break; |
| 1819 | |
| 1820 | default: |
| 1821 | err = -EBADFD; |
| 1822 | goto done; |
| 1823 | } |
| 1824 | |
| 1825 | /* Set destination address and psm */ |
Gustavo Padovan | 0797e01 | 2012-05-27 22:27:54 -0300 | [diff] [blame] | 1826 | lock_sock(sk); |
Gustavo F. Padovan | 9219b2a | 2012-01-02 20:08:04 -0200 | [diff] [blame] | 1827 | bacpy(&bt_sk(sk)->dst, dst); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1828 | release_sock(sk); |
| 1829 | |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1830 | chan->psm = psm; |
| 1831 | chan->dcid = cid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1833 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1834 | |
Johan Hedberg | f224ca5 | 2013-04-29 19:35:36 +0300 | [diff] [blame] | 1835 | if (bdaddr_type_is_le(dst_type)) |
Andre Guedes | 8e9f989 | 2012-04-24 21:02:55 -0300 | [diff] [blame] | 1836 | hcon = hci_connect(hdev, LE_LINK, dst, dst_type, |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 1837 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1838 | else |
Andre Guedes | 8e9f989 | 2012-04-24 21:02:55 -0300 | [diff] [blame] | 1839 | hcon = hci_connect(hdev, ACL_LINK, dst, dst_type, |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 1840 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1841 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1842 | if (IS_ERR(hcon)) { |
| 1843 | err = PTR_ERR(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | goto done; |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1845 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 1847 | conn = l2cap_conn_add(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | if (!conn) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 1849 | hci_conn_drop(hcon); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1850 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | goto done; |
| 1852 | } |
| 1853 | |
Johan Hedberg | 141d570 | 2013-04-29 19:35:37 +0300 | [diff] [blame] | 1854 | if (cid && __l2cap_get_chan_by_dcid(conn, cid)) { |
| 1855 | hci_conn_drop(hcon); |
| 1856 | err = -EBUSY; |
| 1857 | goto done; |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1858 | } |
| 1859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | /* Update source addr of the socket */ |
Marcel Holtmann | 98e0f7e | 2013-10-13 02:23:40 -0700 | [diff] [blame] | 1861 | bacpy(src, &hdev->bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1863 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1864 | l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1865 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1866 | |
Johan Hedberg | 5ee9891 | 2013-04-29 19:35:43 +0300 | [diff] [blame] | 1867 | /* l2cap_chan_add takes its own ref so we can drop this one */ |
| 1868 | hci_conn_drop(hcon); |
| 1869 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1870 | l2cap_state_change(chan, BT_CONNECT); |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1871 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | |
| 1873 | if (hcon->state == BT_CONNECTED) { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1874 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1875 | __clear_chan_timer(chan); |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1876 | if (l2cap_chan_check_security(chan)) |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1877 | l2cap_state_change(chan, BT_CONNECTED); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1878 | } else |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1879 | l2cap_do_start(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1882 | err = 0; |
| 1883 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1885 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1886 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | hci_dev_put(hdev); |
| 1888 | return err; |
| 1889 | } |
| 1890 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1891 | int __l2cap_wait_ack(struct sock *sk) |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1892 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1893 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1894 | DECLARE_WAITQUEUE(wait, current); |
| 1895 | int err = 0; |
| 1896 | int timeo = HZ/5; |
| 1897 | |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1898 | add_wait_queue(sk_sleep(sk), &wait); |
Peter Hurley | a71a0cf | 2011-07-25 18:36:26 -0400 | [diff] [blame] | 1899 | set_current_state(TASK_INTERRUPTIBLE); |
| 1900 | while (chan->unacked_frames > 0 && chan->conn) { |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1901 | if (!timeo) |
| 1902 | timeo = HZ/5; |
| 1903 | |
| 1904 | if (signal_pending(current)) { |
| 1905 | err = sock_intr_errno(timeo); |
| 1906 | break; |
| 1907 | } |
| 1908 | |
| 1909 | release_sock(sk); |
| 1910 | timeo = schedule_timeout(timeo); |
| 1911 | lock_sock(sk); |
Peter Hurley | a71a0cf | 2011-07-25 18:36:26 -0400 | [diff] [blame] | 1912 | set_current_state(TASK_INTERRUPTIBLE); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1913 | |
| 1914 | err = sock_error(sk); |
| 1915 | if (err) |
| 1916 | break; |
| 1917 | } |
| 1918 | set_current_state(TASK_RUNNING); |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1919 | remove_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1920 | return err; |
| 1921 | } |
| 1922 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1923 | static void l2cap_monitor_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1924 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1925 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1926 | monitor_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1927 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1928 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1929 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1930 | l2cap_chan_lock(chan); |
| 1931 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1932 | if (!chan->conn) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1933 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1934 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1935 | return; |
| 1936 | } |
| 1937 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1938 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1939 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1940 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1941 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1942 | } |
| 1943 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1944 | static void l2cap_retrans_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1945 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1946 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1947 | retrans_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1948 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1949 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1950 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1951 | l2cap_chan_lock(chan); |
| 1952 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1953 | if (!chan->conn) { |
| 1954 | l2cap_chan_unlock(chan); |
| 1955 | l2cap_chan_put(chan); |
| 1956 | return; |
| 1957 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1958 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1959 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1960 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1961 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1962 | } |
| 1963 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 1964 | static void l2cap_streaming_send(struct l2cap_chan *chan, |
| 1965 | struct sk_buff_head *skbs) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1966 | { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1967 | struct sk_buff *skb; |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1968 | struct l2cap_ctrl *control; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1969 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1970 | BT_DBG("chan %p, skbs %p", chan, skbs); |
| 1971 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1972 | if (__chan_is_moving(chan)) |
| 1973 | return; |
| 1974 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1975 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 1976 | |
| 1977 | while (!skb_queue_empty(&chan->tx_q)) { |
| 1978 | |
| 1979 | skb = skb_dequeue(&chan->tx_q); |
| 1980 | |
| 1981 | bt_cb(skb)->control.retries = 1; |
| 1982 | control = &bt_cb(skb)->control; |
| 1983 | |
| 1984 | control->reqseq = 0; |
| 1985 | control->txseq = chan->next_tx_seq; |
| 1986 | |
| 1987 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1988 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1989 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1990 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 1991 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1992 | } |
| 1993 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1994 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1995 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1996 | BT_DBG("Sent txseq %u", control->txseq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1997 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1998 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1999 | chan->frames_sent++; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 2000 | } |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 2001 | } |
| 2002 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 2003 | static int l2cap_ertm_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2004 | { |
| 2005 | struct sk_buff *skb, *tx_skb; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2006 | struct l2cap_ctrl *control; |
| 2007 | int sent = 0; |
| 2008 | |
| 2009 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2010 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 2011 | if (chan->state != BT_CONNECTED) |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 2012 | return -ENOTCONN; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2013 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2014 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 2015 | return 0; |
| 2016 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 2017 | if (__chan_is_moving(chan)) |
| 2018 | return 0; |
| 2019 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2020 | while (chan->tx_send_head && |
| 2021 | chan->unacked_frames < chan->remote_tx_win && |
| 2022 | chan->tx_state == L2CAP_TX_STATE_XMIT) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2023 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2024 | skb = chan->tx_send_head; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2025 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2026 | bt_cb(skb)->control.retries = 1; |
| 2027 | control = &bt_cb(skb)->control; |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 2028 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 2029 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2030 | control->final = 1; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 2031 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2032 | control->reqseq = chan->buffer_seq; |
| 2033 | chan->last_acked_seq = chan->buffer_seq; |
| 2034 | control->txseq = chan->next_tx_seq; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2035 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2036 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2037 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2038 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2039 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 2040 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2041 | } |
| 2042 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2043 | /* Clone after data has been modified. Data is assumed to be |
| 2044 | read-only (for locking purposes) on cloned sk_buffs. |
| 2045 | */ |
| 2046 | tx_skb = skb_clone(skb, GFP_KERNEL); |
| 2047 | |
| 2048 | if (!tx_skb) |
| 2049 | break; |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 2050 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 2051 | __set_retrans_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2052 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2053 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2054 | chan->unacked_frames++; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 2055 | chan->frames_sent++; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2056 | sent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2057 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2058 | if (skb_queue_is_last(&chan->tx_q, skb)) |
| 2059 | chan->tx_send_head = NULL; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2060 | else |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2061 | chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2062 | |
| 2063 | l2cap_do_send(chan, tx_skb); |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2064 | BT_DBG("Sent txseq %u", control->txseq); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2065 | } |
| 2066 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2067 | BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent, |
| 2068 | chan->unacked_frames, skb_queue_len(&chan->tx_q)); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2069 | |
| 2070 | return sent; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 2071 | } |
| 2072 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2073 | static void l2cap_ertm_resend(struct l2cap_chan *chan) |
| 2074 | { |
| 2075 | struct l2cap_ctrl control; |
| 2076 | struct sk_buff *skb; |
| 2077 | struct sk_buff *tx_skb; |
| 2078 | u16 seq; |
| 2079 | |
| 2080 | BT_DBG("chan %p", chan); |
| 2081 | |
| 2082 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 2083 | return; |
| 2084 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 2085 | if (__chan_is_moving(chan)) |
| 2086 | return; |
| 2087 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2088 | while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) { |
| 2089 | seq = l2cap_seq_list_pop(&chan->retrans_list); |
| 2090 | |
| 2091 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq); |
| 2092 | if (!skb) { |
| 2093 | BT_DBG("Error: Can't retransmit seq %d, frame missing", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2094 | seq); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2095 | continue; |
| 2096 | } |
| 2097 | |
| 2098 | bt_cb(skb)->control.retries++; |
| 2099 | control = bt_cb(skb)->control; |
| 2100 | |
| 2101 | if (chan->max_tx != 0 && |
| 2102 | bt_cb(skb)->control.retries > chan->max_tx) { |
| 2103 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 2104 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2105 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2106 | break; |
| 2107 | } |
| 2108 | |
| 2109 | control.reqseq = chan->buffer_seq; |
| 2110 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
| 2111 | control.final = 1; |
| 2112 | else |
| 2113 | control.final = 0; |
| 2114 | |
| 2115 | if (skb_cloned(skb)) { |
| 2116 | /* Cloned sk_buffs are read-only, so we need a |
| 2117 | * writeable copy |
| 2118 | */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2119 | tx_skb = skb_copy(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2120 | } else { |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2121 | tx_skb = skb_clone(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | if (!tx_skb) { |
| 2125 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2126 | break; |
| 2127 | } |
| 2128 | |
| 2129 | /* Update skb contents */ |
| 2130 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 2131 | put_unaligned_le32(__pack_extended_control(&control), |
| 2132 | tx_skb->data + L2CAP_HDR_SIZE); |
| 2133 | } else { |
| 2134 | put_unaligned_le16(__pack_enhanced_control(&control), |
| 2135 | tx_skb->data + L2CAP_HDR_SIZE); |
| 2136 | } |
| 2137 | |
| 2138 | if (chan->fcs == L2CAP_FCS_CRC16) { |
| 2139 | u16 fcs = crc16(0, (u8 *) tx_skb->data, tx_skb->len); |
| 2140 | put_unaligned_le16(fcs, skb_put(tx_skb, |
| 2141 | L2CAP_FCS_SIZE)); |
| 2142 | } |
| 2143 | |
| 2144 | l2cap_do_send(chan, tx_skb); |
| 2145 | |
| 2146 | BT_DBG("Resent txseq %d", control.txseq); |
| 2147 | |
| 2148 | chan->last_acked_seq = chan->buffer_seq; |
| 2149 | } |
| 2150 | } |
| 2151 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2152 | static void l2cap_retransmit(struct l2cap_chan *chan, |
| 2153 | struct l2cap_ctrl *control) |
| 2154 | { |
| 2155 | BT_DBG("chan %p, control %p", chan, control); |
| 2156 | |
| 2157 | l2cap_seq_list_append(&chan->retrans_list, control->reqseq); |
| 2158 | l2cap_ertm_resend(chan); |
| 2159 | } |
| 2160 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2161 | static void l2cap_retransmit_all(struct l2cap_chan *chan, |
| 2162 | struct l2cap_ctrl *control) |
| 2163 | { |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2164 | struct sk_buff *skb; |
| 2165 | |
| 2166 | BT_DBG("chan %p, control %p", chan, control); |
| 2167 | |
| 2168 | if (control->poll) |
| 2169 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 2170 | |
| 2171 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2172 | |
| 2173 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 2174 | return; |
| 2175 | |
| 2176 | if (chan->unacked_frames) { |
| 2177 | skb_queue_walk(&chan->tx_q, skb) { |
| 2178 | if (bt_cb(skb)->control.txseq == control->reqseq || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2179 | skb == chan->tx_send_head) |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2180 | break; |
| 2181 | } |
| 2182 | |
| 2183 | skb_queue_walk_from(&chan->tx_q, skb) { |
| 2184 | if (skb == chan->tx_send_head) |
| 2185 | break; |
| 2186 | |
| 2187 | l2cap_seq_list_append(&chan->retrans_list, |
| 2188 | bt_cb(skb)->control.txseq); |
| 2189 | } |
| 2190 | |
| 2191 | l2cap_ertm_resend(chan); |
| 2192 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2193 | } |
| 2194 | |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2195 | static void l2cap_send_ack(struct l2cap_chan *chan) |
| 2196 | { |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2197 | struct l2cap_ctrl control; |
| 2198 | u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 2199 | chan->last_acked_seq); |
| 2200 | int threshold; |
| 2201 | |
| 2202 | BT_DBG("chan %p last_acked_seq %d buffer_seq %d", |
| 2203 | chan, chan->last_acked_seq, chan->buffer_seq); |
| 2204 | |
| 2205 | memset(&control, 0, sizeof(control)); |
| 2206 | control.sframe = 1; |
| 2207 | |
| 2208 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
| 2209 | chan->rx_state == L2CAP_RX_STATE_RECV) { |
| 2210 | __clear_ack_timer(chan); |
| 2211 | control.super = L2CAP_SUPER_RNR; |
| 2212 | control.reqseq = chan->buffer_seq; |
| 2213 | l2cap_send_sframe(chan, &control); |
| 2214 | } else { |
| 2215 | if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) { |
| 2216 | l2cap_ertm_send(chan); |
| 2217 | /* If any i-frames were sent, they included an ack */ |
| 2218 | if (chan->buffer_seq == chan->last_acked_seq) |
| 2219 | frames_to_ack = 0; |
| 2220 | } |
| 2221 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2222 | /* Ack now if the window is 3/4ths full. |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2223 | * Calculate without mul or div |
| 2224 | */ |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2225 | threshold = chan->ack_win; |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2226 | threshold += threshold << 1; |
| 2227 | threshold >>= 2; |
| 2228 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2229 | BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack, |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2230 | threshold); |
| 2231 | |
| 2232 | if (frames_to_ack >= threshold) { |
| 2233 | __clear_ack_timer(chan); |
| 2234 | control.super = L2CAP_SUPER_RR; |
| 2235 | control.reqseq = chan->buffer_seq; |
| 2236 | l2cap_send_sframe(chan, &control); |
| 2237 | frames_to_ack = 0; |
| 2238 | } |
| 2239 | |
| 2240 | if (frames_to_ack) |
| 2241 | __set_ack_timer(chan); |
| 2242 | } |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2243 | } |
| 2244 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2245 | static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, |
| 2246 | struct msghdr *msg, int len, |
| 2247 | int count, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | { |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2249 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2250 | struct sk_buff **frag; |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2251 | int sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | |
Gustavo F. Padovan | 59203a2 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 2253 | if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2254 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | |
| 2256 | sent += count; |
| 2257 | len -= count; |
| 2258 | |
| 2259 | /* Continuation fragments (no L2CAP header) */ |
| 2260 | frag = &skb_shinfo(skb)->frag_list; |
| 2261 | while (len) { |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2262 | struct sk_buff *tmp; |
| 2263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | count = min_t(unsigned int, conn->mtu, len); |
| 2265 | |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2266 | tmp = chan->ops->alloc_skb(chan, count, |
| 2267 | msg->msg_flags & MSG_DONTWAIT); |
| 2268 | if (IS_ERR(tmp)) |
| 2269 | return PTR_ERR(tmp); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2270 | |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2271 | *frag = tmp; |
| 2272 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2273 | if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) |
| 2274 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2276 | (*frag)->priority = skb->priority; |
| 2277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | sent += count; |
| 2279 | len -= count; |
| 2280 | |
Gustavo Padovan | 2d0ed3d | 2012-05-11 13:16:12 -0300 | [diff] [blame] | 2281 | skb->len += (*frag)->len; |
| 2282 | skb->data_len += (*frag)->len; |
| 2283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | frag = &(*frag)->next; |
| 2285 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | |
| 2287 | return sent; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2290 | static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2291 | struct msghdr *msg, size_t len, |
| 2292 | u32 priority) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2293 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2294 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2295 | struct sk_buff *skb; |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2296 | int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2297 | struct l2cap_hdr *lh; |
| 2298 | |
Marcel Holtmann | 43b1b8d | 2013-10-12 06:01:26 -0700 | [diff] [blame] | 2299 | BT_DBG("chan %p psm 0x%2.2x len %zu priority %u", chan, |
| 2300 | __le16_to_cpu(chan->psm), len, priority); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2301 | |
| 2302 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2303 | |
| 2304 | skb = chan->ops->alloc_skb(chan, count + hlen, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2305 | msg->msg_flags & MSG_DONTWAIT); |
| 2306 | if (IS_ERR(skb)) |
| 2307 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2308 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2309 | skb->priority = priority; |
| 2310 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2311 | /* Create L2CAP header */ |
| 2312 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2313 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | daf6a78c | 2012-05-03 10:55:52 +0300 | [diff] [blame] | 2314 | lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE); |
Marcel Holtmann | 43b1b8d | 2013-10-12 06:01:26 -0700 | [diff] [blame] | 2315 | put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2316 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2317 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2318 | if (unlikely(err < 0)) { |
| 2319 | kfree_skb(skb); |
| 2320 | return ERR_PTR(err); |
| 2321 | } |
| 2322 | return skb; |
| 2323 | } |
| 2324 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2325 | static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2326 | struct msghdr *msg, size_t len, |
| 2327 | u32 priority) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2328 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2329 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2330 | struct sk_buff *skb; |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2331 | int err, count; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2332 | struct l2cap_hdr *lh; |
| 2333 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2334 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2335 | |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2336 | count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2337 | |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2338 | skb = chan->ops->alloc_skb(chan, count + L2CAP_HDR_SIZE, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2339 | msg->msg_flags & MSG_DONTWAIT); |
| 2340 | if (IS_ERR(skb)) |
| 2341 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2342 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2343 | skb->priority = priority; |
| 2344 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2345 | /* Create L2CAP header */ |
| 2346 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2347 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo Padovan | 6ff9b5e | 2012-05-02 11:56:17 -0300 | [diff] [blame] | 2348 | lh->len = cpu_to_le16(len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2349 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2350 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2351 | if (unlikely(err < 0)) { |
| 2352 | kfree_skb(skb); |
| 2353 | return ERR_PTR(err); |
| 2354 | } |
| 2355 | return skb; |
| 2356 | } |
| 2357 | |
Luiz Augusto von Dentz | ab0ff76 | 2011-09-12 20:00:50 +0300 | [diff] [blame] | 2358 | static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2359 | struct msghdr *msg, size_t len, |
| 2360 | u16 sdulen) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2361 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2362 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2363 | struct sk_buff *skb; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2364 | int err, count, hlen; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2365 | struct l2cap_hdr *lh; |
| 2366 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2367 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2368 | |
Gustavo F. Padovan | 0ee0d20 | 2010-05-01 16:15:41 -0300 | [diff] [blame] | 2369 | if (!conn) |
| 2370 | return ERR_PTR(-ENOTCONN); |
| 2371 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2372 | hlen = __ertm_hdr_size(chan); |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2373 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2374 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2375 | hlen += L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2376 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2377 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2378 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2379 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2380 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2381 | |
| 2382 | skb = chan->ops->alloc_skb(chan, count + hlen, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2383 | msg->msg_flags & MSG_DONTWAIT); |
| 2384 | if (IS_ERR(skb)) |
| 2385 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2386 | |
| 2387 | /* Create L2CAP header */ |
| 2388 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2389 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2390 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2391 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2392 | /* Control header is populated later */ |
| 2393 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 2394 | put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 2395 | else |
| 2396 | put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2397 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2398 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2399 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2400 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2401 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2402 | if (unlikely(err < 0)) { |
| 2403 | kfree_skb(skb); |
| 2404 | return ERR_PTR(err); |
| 2405 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2406 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2407 | bt_cb(skb)->control.fcs = chan->fcs; |
Mat Martineau | 3ce3514 | 2012-04-25 16:36:14 -0700 | [diff] [blame] | 2408 | bt_cb(skb)->control.retries = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2409 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | } |
| 2411 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2412 | static int l2cap_segment_sdu(struct l2cap_chan *chan, |
| 2413 | struct sk_buff_head *seg_queue, |
| 2414 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2415 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2416 | struct sk_buff *skb; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2417 | u16 sdu_len; |
| 2418 | size_t pdu_len; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2419 | u8 sar; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2420 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2421 | BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2422 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2423 | /* It is critical that ERTM PDUs fit in a single HCI fragment, |
| 2424 | * so fragmented skbs are not used. The HCI layer's handling |
| 2425 | * of fragmented skbs is not compatible with ERTM's queueing. |
| 2426 | */ |
| 2427 | |
| 2428 | /* PDU size is derived from the HCI MTU */ |
| 2429 | pdu_len = chan->conn->mtu; |
| 2430 | |
Mat Martineau | a549574 | 2012-10-23 15:24:21 -0700 | [diff] [blame] | 2431 | /* Constrain PDU size for BR/EDR connections */ |
| 2432 | if (!chan->hs_hcon) |
| 2433 | pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2434 | |
| 2435 | /* Adjust for largest possible L2CAP overhead. */ |
Gustavo Padovan | 35d401d | 2012-05-25 18:57:05 -0300 | [diff] [blame] | 2436 | if (chan->fcs) |
| 2437 | pdu_len -= L2CAP_FCS_SIZE; |
| 2438 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2439 | pdu_len -= __ertm_hdr_size(chan); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2440 | |
| 2441 | /* Remote device may have requested smaller PDUs */ |
| 2442 | pdu_len = min_t(size_t, pdu_len, chan->remote_mps); |
| 2443 | |
| 2444 | if (len <= pdu_len) { |
| 2445 | sar = L2CAP_SAR_UNSEGMENTED; |
| 2446 | sdu_len = 0; |
| 2447 | pdu_len = len; |
| 2448 | } else { |
| 2449 | sar = L2CAP_SAR_START; |
| 2450 | sdu_len = len; |
| 2451 | pdu_len -= L2CAP_SDULEN_SIZE; |
| 2452 | } |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2453 | |
| 2454 | while (len > 0) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2455 | skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2456 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2457 | if (IS_ERR(skb)) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2458 | __skb_queue_purge(seg_queue); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2459 | return PTR_ERR(skb); |
| 2460 | } |
| 2461 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2462 | bt_cb(skb)->control.sar = sar; |
| 2463 | __skb_queue_tail(seg_queue, skb); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2464 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2465 | len -= pdu_len; |
| 2466 | if (sdu_len) { |
| 2467 | sdu_len = 0; |
| 2468 | pdu_len += L2CAP_SDULEN_SIZE; |
| 2469 | } |
| 2470 | |
| 2471 | if (len <= pdu_len) { |
| 2472 | sar = L2CAP_SAR_END; |
| 2473 | pdu_len = len; |
| 2474 | } else { |
| 2475 | sar = L2CAP_SAR_CONTINUE; |
| 2476 | } |
| 2477 | } |
| 2478 | |
Gustavo Padovan | f0f6279 | 2012-05-29 13:29:17 -0300 | [diff] [blame] | 2479 | return 0; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2480 | } |
| 2481 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2482 | 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] | 2483 | u32 priority) |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2484 | { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2485 | struct sk_buff *skb; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2486 | int err; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2487 | struct sk_buff_head seg_queue; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2488 | |
| 2489 | /* Connectionless channel */ |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2490 | if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2491 | skb = l2cap_create_connless_pdu(chan, msg, len, priority); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2492 | if (IS_ERR(skb)) |
| 2493 | return PTR_ERR(skb); |
| 2494 | |
| 2495 | l2cap_do_send(chan, skb); |
| 2496 | return len; |
| 2497 | } |
| 2498 | |
| 2499 | switch (chan->mode) { |
| 2500 | case L2CAP_MODE_BASIC: |
| 2501 | /* Check outgoing MTU */ |
| 2502 | if (len > chan->omtu) |
| 2503 | return -EMSGSIZE; |
| 2504 | |
| 2505 | /* Create a basic PDU */ |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2506 | skb = l2cap_create_basic_pdu(chan, msg, len, priority); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2507 | if (IS_ERR(skb)) |
| 2508 | return PTR_ERR(skb); |
| 2509 | |
| 2510 | l2cap_do_send(chan, skb); |
| 2511 | err = len; |
| 2512 | break; |
| 2513 | |
| 2514 | case L2CAP_MODE_ERTM: |
| 2515 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2516 | /* Check outgoing MTU */ |
| 2517 | if (len > chan->omtu) { |
| 2518 | err = -EMSGSIZE; |
| 2519 | break; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2520 | } |
| 2521 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2522 | __skb_queue_head_init(&seg_queue); |
| 2523 | |
| 2524 | /* Do segmentation before calling in to the state machine, |
| 2525 | * since it's possible to block while waiting for memory |
| 2526 | * allocation. |
| 2527 | */ |
| 2528 | err = l2cap_segment_sdu(chan, &seg_queue, msg, len); |
| 2529 | |
| 2530 | /* The channel could have been closed while segmenting, |
| 2531 | * check that it is still connected. |
| 2532 | */ |
| 2533 | if (chan->state != BT_CONNECTED) { |
| 2534 | __skb_queue_purge(&seg_queue); |
| 2535 | err = -ENOTCONN; |
| 2536 | } |
| 2537 | |
| 2538 | if (err) |
| 2539 | break; |
| 2540 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2541 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2542 | l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2543 | else |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2544 | l2cap_streaming_send(chan, &seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2545 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2546 | err = len; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2547 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2548 | /* If the skbs were not queued for sending, they'll still be in |
| 2549 | * seg_queue and need to be purged. |
| 2550 | */ |
| 2551 | __skb_queue_purge(&seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2552 | break; |
| 2553 | |
| 2554 | default: |
| 2555 | BT_DBG("bad state %1.1x", chan->mode); |
| 2556 | err = -EBADFD; |
| 2557 | } |
| 2558 | |
| 2559 | return err; |
| 2560 | } |
| 2561 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2562 | static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq) |
| 2563 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2564 | struct l2cap_ctrl control; |
| 2565 | u16 seq; |
| 2566 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2567 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2568 | |
| 2569 | memset(&control, 0, sizeof(control)); |
| 2570 | control.sframe = 1; |
| 2571 | control.super = L2CAP_SUPER_SREJ; |
| 2572 | |
| 2573 | for (seq = chan->expected_tx_seq; seq != txseq; |
| 2574 | seq = __next_seq(chan, seq)) { |
| 2575 | if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) { |
| 2576 | control.reqseq = seq; |
| 2577 | l2cap_send_sframe(chan, &control); |
| 2578 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | chan->expected_tx_seq = __next_seq(chan, txseq); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | static void l2cap_send_srej_tail(struct l2cap_chan *chan) |
| 2586 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2587 | struct l2cap_ctrl control; |
| 2588 | |
| 2589 | BT_DBG("chan %p", chan); |
| 2590 | |
| 2591 | if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR) |
| 2592 | return; |
| 2593 | |
| 2594 | memset(&control, 0, sizeof(control)); |
| 2595 | control.sframe = 1; |
| 2596 | control.super = L2CAP_SUPER_SREJ; |
| 2597 | control.reqseq = chan->srej_list.tail; |
| 2598 | l2cap_send_sframe(chan, &control); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq) |
| 2602 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2603 | struct l2cap_ctrl control; |
| 2604 | u16 initial_head; |
| 2605 | u16 seq; |
| 2606 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2607 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2608 | |
| 2609 | memset(&control, 0, sizeof(control)); |
| 2610 | control.sframe = 1; |
| 2611 | control.super = L2CAP_SUPER_SREJ; |
| 2612 | |
| 2613 | /* Capture initial list head to allow only one pass through the list. */ |
| 2614 | initial_head = chan->srej_list.head; |
| 2615 | |
| 2616 | do { |
| 2617 | seq = l2cap_seq_list_pop(&chan->srej_list); |
| 2618 | if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR) |
| 2619 | break; |
| 2620 | |
| 2621 | control.reqseq = seq; |
| 2622 | l2cap_send_sframe(chan, &control); |
| 2623 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2624 | } while (chan->srej_list.head != initial_head); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2625 | } |
| 2626 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2627 | static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 2628 | { |
| 2629 | struct sk_buff *acked_skb; |
| 2630 | u16 ackseq; |
| 2631 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2632 | BT_DBG("chan %p, reqseq %u", chan, reqseq); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2633 | |
| 2634 | if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq) |
| 2635 | return; |
| 2636 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2637 | BT_DBG("expected_ack_seq %u, unacked_frames %u", |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2638 | chan->expected_ack_seq, chan->unacked_frames); |
| 2639 | |
| 2640 | for (ackseq = chan->expected_ack_seq; ackseq != reqseq; |
| 2641 | ackseq = __next_seq(chan, ackseq)) { |
| 2642 | |
| 2643 | acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq); |
| 2644 | if (acked_skb) { |
| 2645 | skb_unlink(acked_skb, &chan->tx_q); |
| 2646 | kfree_skb(acked_skb); |
| 2647 | chan->unacked_frames--; |
| 2648 | } |
| 2649 | } |
| 2650 | |
| 2651 | chan->expected_ack_seq = reqseq; |
| 2652 | |
| 2653 | if (chan->unacked_frames == 0) |
| 2654 | __clear_retrans_timer(chan); |
| 2655 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2656 | BT_DBG("unacked_frames %u", chan->unacked_frames); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan) |
| 2660 | { |
| 2661 | BT_DBG("chan %p", chan); |
| 2662 | |
| 2663 | chan->expected_tx_seq = chan->buffer_seq; |
| 2664 | l2cap_seq_list_clear(&chan->srej_list); |
| 2665 | skb_queue_purge(&chan->srej_q); |
| 2666 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 2667 | } |
| 2668 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2669 | static void l2cap_tx_state_xmit(struct l2cap_chan *chan, |
| 2670 | struct l2cap_ctrl *control, |
| 2671 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2672 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2673 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2674 | event); |
| 2675 | |
| 2676 | switch (event) { |
| 2677 | case L2CAP_EV_DATA_REQUEST: |
| 2678 | if (chan->tx_send_head == NULL) |
| 2679 | chan->tx_send_head = skb_peek(skbs); |
| 2680 | |
| 2681 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2682 | l2cap_ertm_send(chan); |
| 2683 | break; |
| 2684 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2685 | BT_DBG("Enter LOCAL_BUSY"); |
| 2686 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2687 | |
| 2688 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2689 | /* The SREJ_SENT state must be aborted if we are to |
| 2690 | * enter the LOCAL_BUSY state. |
| 2691 | */ |
| 2692 | l2cap_abort_rx_srej_sent(chan); |
| 2693 | } |
| 2694 | |
| 2695 | l2cap_send_ack(chan); |
| 2696 | |
| 2697 | break; |
| 2698 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2699 | BT_DBG("Exit LOCAL_BUSY"); |
| 2700 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2701 | |
| 2702 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2703 | struct l2cap_ctrl local_control; |
| 2704 | |
| 2705 | memset(&local_control, 0, sizeof(local_control)); |
| 2706 | local_control.sframe = 1; |
| 2707 | local_control.super = L2CAP_SUPER_RR; |
| 2708 | local_control.poll = 1; |
| 2709 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2710 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2711 | |
| 2712 | chan->retry_count = 1; |
| 2713 | __set_monitor_timer(chan); |
| 2714 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2715 | } |
| 2716 | break; |
| 2717 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2718 | l2cap_process_reqseq(chan, control->reqseq); |
| 2719 | break; |
| 2720 | case L2CAP_EV_EXPLICIT_POLL: |
| 2721 | l2cap_send_rr_or_rnr(chan, 1); |
| 2722 | chan->retry_count = 1; |
| 2723 | __set_monitor_timer(chan); |
| 2724 | __clear_ack_timer(chan); |
| 2725 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2726 | break; |
| 2727 | case L2CAP_EV_RETRANS_TO: |
| 2728 | l2cap_send_rr_or_rnr(chan, 1); |
| 2729 | chan->retry_count = 1; |
| 2730 | __set_monitor_timer(chan); |
| 2731 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2732 | break; |
| 2733 | case L2CAP_EV_RECV_FBIT: |
| 2734 | /* Nothing to process */ |
| 2735 | break; |
| 2736 | default: |
| 2737 | break; |
| 2738 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2739 | } |
| 2740 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2741 | static void l2cap_tx_state_wait_f(struct l2cap_chan *chan, |
| 2742 | struct l2cap_ctrl *control, |
| 2743 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2744 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2745 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2746 | event); |
| 2747 | |
| 2748 | switch (event) { |
| 2749 | case L2CAP_EV_DATA_REQUEST: |
| 2750 | if (chan->tx_send_head == NULL) |
| 2751 | chan->tx_send_head = skb_peek(skbs); |
| 2752 | /* Queue data, but don't send. */ |
| 2753 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2754 | break; |
| 2755 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2756 | BT_DBG("Enter LOCAL_BUSY"); |
| 2757 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2758 | |
| 2759 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2760 | /* The SREJ_SENT state must be aborted if we are to |
| 2761 | * enter the LOCAL_BUSY state. |
| 2762 | */ |
| 2763 | l2cap_abort_rx_srej_sent(chan); |
| 2764 | } |
| 2765 | |
| 2766 | l2cap_send_ack(chan); |
| 2767 | |
| 2768 | break; |
| 2769 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2770 | BT_DBG("Exit LOCAL_BUSY"); |
| 2771 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2772 | |
| 2773 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2774 | struct l2cap_ctrl local_control; |
| 2775 | memset(&local_control, 0, sizeof(local_control)); |
| 2776 | local_control.sframe = 1; |
| 2777 | local_control.super = L2CAP_SUPER_RR; |
| 2778 | local_control.poll = 1; |
| 2779 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2780 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2781 | |
| 2782 | chan->retry_count = 1; |
| 2783 | __set_monitor_timer(chan); |
| 2784 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2785 | } |
| 2786 | break; |
| 2787 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2788 | l2cap_process_reqseq(chan, control->reqseq); |
| 2789 | |
| 2790 | /* Fall through */ |
| 2791 | |
| 2792 | case L2CAP_EV_RECV_FBIT: |
| 2793 | if (control && control->final) { |
| 2794 | __clear_monitor_timer(chan); |
| 2795 | if (chan->unacked_frames > 0) |
| 2796 | __set_retrans_timer(chan); |
| 2797 | chan->retry_count = 0; |
| 2798 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 2799 | BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state); |
| 2800 | } |
| 2801 | break; |
| 2802 | case L2CAP_EV_EXPLICIT_POLL: |
| 2803 | /* Ignore */ |
| 2804 | break; |
| 2805 | case L2CAP_EV_MONITOR_TO: |
| 2806 | if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) { |
| 2807 | l2cap_send_rr_or_rnr(chan, 1); |
| 2808 | __set_monitor_timer(chan); |
| 2809 | chan->retry_count++; |
| 2810 | } else { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 2811 | l2cap_send_disconn_req(chan, ECONNABORTED); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2812 | } |
| 2813 | break; |
| 2814 | default: |
| 2815 | break; |
| 2816 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2817 | } |
| 2818 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2819 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 2820 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2821 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2822 | BT_DBG("chan %p, control %p, skbs %p, event %d, state %d", |
| 2823 | chan, control, skbs, event, chan->tx_state); |
| 2824 | |
| 2825 | switch (chan->tx_state) { |
| 2826 | case L2CAP_TX_STATE_XMIT: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2827 | l2cap_tx_state_xmit(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2828 | break; |
| 2829 | case L2CAP_TX_STATE_WAIT_F: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2830 | l2cap_tx_state_wait_f(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2831 | break; |
| 2832 | default: |
| 2833 | /* Ignore event */ |
| 2834 | break; |
| 2835 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2836 | } |
| 2837 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2838 | static void l2cap_pass_to_tx(struct l2cap_chan *chan, |
| 2839 | struct l2cap_ctrl *control) |
| 2840 | { |
| 2841 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2842 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT); |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2843 | } |
| 2844 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2845 | static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan, |
| 2846 | struct l2cap_ctrl *control) |
| 2847 | { |
| 2848 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2849 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2850 | } |
| 2851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | /* Copy frame to all raw sockets on that connection */ |
| 2853 | static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) |
| 2854 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2855 | struct sk_buff *nskb; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2856 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | |
| 2858 | BT_DBG("conn %p", conn); |
| 2859 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2860 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2861 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2862 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2863 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2864 | if (chan->chan_type != L2CAP_CHAN_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | continue; |
| 2866 | |
| 2867 | /* Don't send frame to the socket it came from */ |
| 2868 | if (skb->sk == sk) |
| 2869 | continue; |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2870 | nskb = skb_clone(skb, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 2871 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | continue; |
| 2873 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 2874 | if (chan->ops->recv(chan, nskb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | kfree_skb(nskb); |
| 2876 | } |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2877 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2878 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | /* ---- L2CAP signalling commands ---- */ |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2882 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code, |
| 2883 | u8 ident, u16 dlen, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2884 | { |
| 2885 | struct sk_buff *skb, **frag; |
| 2886 | struct l2cap_cmd_hdr *cmd; |
| 2887 | struct l2cap_hdr *lh; |
| 2888 | int len, count; |
| 2889 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2890 | BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u", |
| 2891 | conn, code, ident, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | |
Anderson Lizardo | 300b962 | 2013-06-02 16:30:40 -0400 | [diff] [blame] | 2893 | if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE) |
| 2894 | return NULL; |
| 2895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2896 | len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; |
| 2897 | count = min_t(unsigned int, conn->mtu, len); |
| 2898 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2899 | skb = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2900 | if (!skb) |
| 2901 | return NULL; |
| 2902 | |
| 2903 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2904 | lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2905 | |
| 2906 | if (conn->hcon->type == LE_LINK) |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 2907 | lh->cid = __constant_cpu_to_le16(L2CAP_CID_LE_SIGNALING); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2908 | else |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 2909 | lh->cid = __constant_cpu_to_le16(L2CAP_CID_SIGNALING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | |
| 2911 | cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); |
| 2912 | cmd->code = code; |
| 2913 | cmd->ident = ident; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2914 | cmd->len = cpu_to_le16(dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | |
| 2916 | if (dlen) { |
| 2917 | count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; |
| 2918 | memcpy(skb_put(skb, count), data, count); |
| 2919 | data += count; |
| 2920 | } |
| 2921 | |
| 2922 | len -= skb->len; |
| 2923 | |
| 2924 | /* Continuation fragments (no L2CAP header) */ |
| 2925 | frag = &skb_shinfo(skb)->frag_list; |
| 2926 | while (len) { |
| 2927 | count = min_t(unsigned int, conn->mtu, len); |
| 2928 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2929 | *frag = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | if (!*frag) |
| 2931 | goto fail; |
| 2932 | |
| 2933 | memcpy(skb_put(*frag, count), data, count); |
| 2934 | |
| 2935 | len -= count; |
| 2936 | data += count; |
| 2937 | |
| 2938 | frag = &(*frag)->next; |
| 2939 | } |
| 2940 | |
| 2941 | return skb; |
| 2942 | |
| 2943 | fail: |
| 2944 | kfree_skb(skb); |
| 2945 | return NULL; |
| 2946 | } |
| 2947 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2948 | static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, |
| 2949 | unsigned long *val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | { |
| 2951 | struct l2cap_conf_opt *opt = *ptr; |
| 2952 | int len; |
| 2953 | |
| 2954 | len = L2CAP_CONF_OPT_SIZE + opt->len; |
| 2955 | *ptr += len; |
| 2956 | |
| 2957 | *type = opt->type; |
| 2958 | *olen = opt->len; |
| 2959 | |
| 2960 | switch (opt->len) { |
| 2961 | case 1: |
| 2962 | *val = *((u8 *) opt->val); |
| 2963 | break; |
| 2964 | |
| 2965 | case 2: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2966 | *val = get_unaligned_le16(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | break; |
| 2968 | |
| 2969 | case 4: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2970 | *val = get_unaligned_le32(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | break; |
| 2972 | |
| 2973 | default: |
| 2974 | *val = (unsigned long) opt->val; |
| 2975 | break; |
| 2976 | } |
| 2977 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2978 | 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] | 2979 | return len; |
| 2980 | } |
| 2981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) |
| 2983 | { |
| 2984 | struct l2cap_conf_opt *opt = *ptr; |
| 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, len, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | |
| 2988 | opt->type = type; |
| 2989 | opt->len = len; |
| 2990 | |
| 2991 | switch (len) { |
| 2992 | case 1: |
| 2993 | *((u8 *) opt->val) = val; |
| 2994 | break; |
| 2995 | |
| 2996 | case 2: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 2997 | put_unaligned_le16(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | break; |
| 2999 | |
| 3000 | case 4: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 3001 | put_unaligned_le32(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | break; |
| 3003 | |
| 3004 | default: |
| 3005 | memcpy(opt->val, (void *) val, len); |
| 3006 | break; |
| 3007 | } |
| 3008 | |
| 3009 | *ptr += L2CAP_CONF_OPT_SIZE + len; |
| 3010 | } |
| 3011 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3012 | static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) |
| 3013 | { |
| 3014 | struct l2cap_conf_efs efs; |
| 3015 | |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 3016 | switch (chan->mode) { |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3017 | case L2CAP_MODE_ERTM: |
| 3018 | efs.id = chan->local_id; |
| 3019 | efs.stype = chan->local_stype; |
| 3020 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 3021 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 3022 | efs.acc_lat = __constant_cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 3023 | efs.flush_to = __constant_cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3024 | break; |
| 3025 | |
| 3026 | case L2CAP_MODE_STREAMING: |
| 3027 | efs.id = 1; |
| 3028 | efs.stype = L2CAP_SERV_BESTEFFORT; |
| 3029 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 3030 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
| 3031 | efs.acc_lat = 0; |
| 3032 | efs.flush_to = 0; |
| 3033 | break; |
| 3034 | |
| 3035 | default: |
| 3036 | return; |
| 3037 | } |
| 3038 | |
| 3039 | l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 3040 | (unsigned long) &efs); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3041 | } |
| 3042 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3043 | static void l2cap_ack_timeout(struct work_struct *work) |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3044 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3045 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3046 | ack_timer.work); |
| 3047 | u16 frames_to_ack; |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3048 | |
Gustavo F. Padovan | 2fb9b3d | 2011-12-22 16:56:05 -0200 | [diff] [blame] | 3049 | BT_DBG("chan %p", chan); |
| 3050 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3051 | l2cap_chan_lock(chan); |
| 3052 | |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3053 | frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 3054 | chan->last_acked_seq); |
| 3055 | |
| 3056 | if (frames_to_ack) |
| 3057 | l2cap_send_rr_or_rnr(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3058 | |
| 3059 | l2cap_chan_unlock(chan); |
Szymon Janc | 09bfb2e | 2012-01-11 10:59:49 +0100 | [diff] [blame] | 3060 | l2cap_chan_put(chan); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3061 | } |
| 3062 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 3063 | int l2cap_ertm_init(struct l2cap_chan *chan) |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3064 | { |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3065 | int err; |
| 3066 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3067 | chan->next_tx_seq = 0; |
| 3068 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3069 | chan->expected_ack_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3070 | chan->unacked_frames = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3071 | chan->buffer_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3072 | chan->frames_sent = 0; |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3073 | chan->last_acked_seq = 0; |
| 3074 | chan->sdu = NULL; |
| 3075 | chan->sdu_last_frag = NULL; |
| 3076 | chan->sdu_len = 0; |
| 3077 | |
Mat Martineau | d34c34f | 2012-05-14 14:49:27 -0700 | [diff] [blame] | 3078 | skb_queue_head_init(&chan->tx_q); |
| 3079 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3080 | chan->local_amp_id = AMP_ID_BREDR; |
| 3081 | chan->move_id = AMP_ID_BREDR; |
Mat Martineau | 0833328 | 2012-10-23 15:24:06 -0700 | [diff] [blame] | 3082 | chan->move_state = L2CAP_MOVE_STABLE; |
| 3083 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 3084 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3085 | if (chan->mode != L2CAP_MODE_ERTM) |
| 3086 | return 0; |
| 3087 | |
| 3088 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 3089 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3090 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3091 | INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); |
| 3092 | INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); |
| 3093 | INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3094 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3095 | skb_queue_head_init(&chan->srej_q); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3096 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3097 | err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win); |
| 3098 | if (err < 0) |
| 3099 | return err; |
| 3100 | |
Mat Martineau | 9dc9aff | 2012-05-17 16:20:14 -0700 | [diff] [blame] | 3101 | err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win); |
| 3102 | if (err < 0) |
| 3103 | l2cap_seq_list_free(&chan->srej_list); |
| 3104 | |
| 3105 | return err; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3106 | } |
| 3107 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3108 | static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) |
| 3109 | { |
| 3110 | switch (mode) { |
| 3111 | case L2CAP_MODE_STREAMING: |
| 3112 | case L2CAP_MODE_ERTM: |
| 3113 | if (l2cap_mode_supported(mode, remote_feat_mask)) |
| 3114 | return mode; |
| 3115 | /* fall through */ |
| 3116 | default: |
| 3117 | return L2CAP_MODE_BASIC; |
| 3118 | } |
| 3119 | } |
| 3120 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3121 | static inline bool __l2cap_ews_supported(struct l2cap_conn *conn) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3122 | { |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3123 | return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3124 | } |
| 3125 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3126 | static inline bool __l2cap_efs_supported(struct l2cap_conn *conn) |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3127 | { |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3128 | return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_FLOW; |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3129 | } |
| 3130 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3131 | static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan, |
| 3132 | struct l2cap_conf_rfc *rfc) |
| 3133 | { |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3134 | if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) { |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3135 | u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to; |
| 3136 | |
| 3137 | /* Class 1 devices have must have ERTM timeouts |
| 3138 | * exceeding the Link Supervision Timeout. The |
| 3139 | * default Link Supervision Timeout for AMP |
| 3140 | * controllers is 10 seconds. |
| 3141 | * |
| 3142 | * Class 1 devices use 0xffffffff for their |
| 3143 | * best-effort flush timeout, so the clamping logic |
| 3144 | * will result in a timeout that meets the above |
| 3145 | * requirement. ERTM timeouts are 16-bit values, so |
| 3146 | * the maximum timeout is 65.535 seconds. |
| 3147 | */ |
| 3148 | |
| 3149 | /* Convert timeout to milliseconds and round */ |
| 3150 | ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000); |
| 3151 | |
| 3152 | /* This is the recommended formula for class 2 devices |
| 3153 | * that start ERTM timers when packets are sent to the |
| 3154 | * controller. |
| 3155 | */ |
| 3156 | ertm_to = 3 * ertm_to + 500; |
| 3157 | |
| 3158 | if (ertm_to > 0xffff) |
| 3159 | ertm_to = 0xffff; |
| 3160 | |
| 3161 | rfc->retrans_timeout = cpu_to_le16((u16) ertm_to); |
| 3162 | rfc->monitor_timeout = rfc->retrans_timeout; |
| 3163 | } else { |
| 3164 | rfc->retrans_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); |
| 3165 | rfc->monitor_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); |
| 3166 | } |
| 3167 | } |
| 3168 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3169 | static inline void l2cap_txwin_setup(struct l2cap_chan *chan) |
| 3170 | { |
| 3171 | if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3172 | __l2cap_ews_supported(chan->conn)) { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3173 | /* use extended control field */ |
| 3174 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3175 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
| 3176 | } else { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3177 | chan->tx_win = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3178 | L2CAP_DEFAULT_TX_WINDOW); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3179 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
| 3180 | } |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3181 | chan->ack_win = chan->tx_win; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3182 | } |
| 3183 | |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3184 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3186 | struct l2cap_conf_req *req = data; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3187 | struct l2cap_conf_rfc rfc = { .mode = chan->mode }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3188 | void *ptr = req->data; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3189 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3191 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3192 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3193 | if (chan->num_conf_req || chan->num_conf_rsp) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3194 | goto done; |
| 3195 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3196 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3197 | case L2CAP_MODE_STREAMING: |
| 3198 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3199 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3200 | break; |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3201 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3202 | if (__l2cap_efs_supported(chan->conn)) |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3203 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3204 | |
Gustavo F. Padovan | 2ba13ed | 2010-06-09 16:39:05 -0300 | [diff] [blame] | 3205 | /* fall through */ |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3206 | default: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3207 | chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3208 | break; |
| 3209 | } |
| 3210 | |
| 3211 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3212 | if (chan->imtu != L2CAP_DEFAULT_MTU) |
| 3213 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | 7990681c | 2011-01-24 16:01:43 -0200 | [diff] [blame] | 3214 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3215 | switch (chan->mode) { |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3216 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3217 | if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3218 | !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3219 | break; |
| 3220 | |
Gustavo F. Padovan | 6254775 | 2010-06-08 20:05:31 -0300 | [diff] [blame] | 3221 | rfc.mode = L2CAP_MODE_BASIC; |
| 3222 | rfc.txwin_size = 0; |
| 3223 | rfc.max_transmit = 0; |
| 3224 | rfc.retrans_timeout = 0; |
| 3225 | rfc.monitor_timeout = 0; |
| 3226 | rfc.max_pdu_size = 0; |
| 3227 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3228 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3229 | (unsigned long) &rfc); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3230 | break; |
| 3231 | |
| 3232 | case L2CAP_MODE_ERTM: |
| 3233 | rfc.mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3234 | rfc.max_transmit = chan->max_tx; |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3235 | |
| 3236 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3237 | |
| 3238 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3239 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3240 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3241 | rfc.max_pdu_size = cpu_to_le16(size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3242 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3243 | l2cap_txwin_setup(chan); |
| 3244 | |
| 3245 | rfc.txwin_size = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3246 | L2CAP_DEFAULT_TX_WINDOW); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3247 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3248 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3249 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3250 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3251 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3252 | l2cap_add_opt_efs(&ptr, chan); |
| 3253 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3254 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3255 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3256 | chan->tx_win); |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3257 | |
| 3258 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3259 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3260 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3261 | chan->fcs = L2CAP_FCS_NONE; |
| 3262 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3263 | chan->fcs); |
| 3264 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3265 | break; |
| 3266 | |
| 3267 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 273759e | 2012-05-17 20:53:53 -0700 | [diff] [blame] | 3268 | l2cap_txwin_setup(chan); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3269 | rfc.mode = L2CAP_MODE_STREAMING; |
| 3270 | rfc.txwin_size = 0; |
| 3271 | rfc.max_transmit = 0; |
| 3272 | rfc.retrans_timeout = 0; |
| 3273 | rfc.monitor_timeout = 0; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3274 | |
| 3275 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3276 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3277 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3278 | rfc.max_pdu_size = cpu_to_le16(size); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3279 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3280 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3281 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3282 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3283 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3284 | l2cap_add_opt_efs(&ptr, chan); |
| 3285 | |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3286 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3287 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3288 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3289 | chan->fcs = L2CAP_FCS_NONE; |
| 3290 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3291 | chan->fcs); |
| 3292 | } |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3293 | break; |
| 3294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3295 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3296 | req->dcid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 3297 | req->flags = __constant_cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | |
| 3299 | return ptr - data; |
| 3300 | } |
| 3301 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3302 | static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3304 | struct l2cap_conf_rsp *rsp = data; |
| 3305 | void *ptr = rsp->data; |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3306 | void *req = chan->conf_req; |
| 3307 | int len = chan->conf_len; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3308 | int type, hint, olen; |
| 3309 | unsigned long val; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3310 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3311 | struct l2cap_conf_efs efs; |
| 3312 | u8 remote_efs = 0; |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3313 | u16 mtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3314 | u16 result = L2CAP_CONF_SUCCESS; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3315 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3317 | BT_DBG("chan %p", chan); |
Marcel Holtmann | 820ae1b | 2006-11-18 22:15:00 +0100 | [diff] [blame] | 3318 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3319 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3320 | len -= l2cap_get_conf_opt(&req, &type, &olen, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3321 | |
Gustavo F. Padovan | 589d274 | 2009-04-20 01:31:07 -0300 | [diff] [blame] | 3322 | hint = type & L2CAP_CONF_HINT; |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 3323 | type &= L2CAP_CONF_MASK; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3324 | |
| 3325 | switch (type) { |
| 3326 | case L2CAP_CONF_MTU: |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3327 | mtu = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3328 | break; |
| 3329 | |
| 3330 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3331 | chan->flush_to = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3332 | break; |
| 3333 | |
| 3334 | case L2CAP_CONF_QOS: |
| 3335 | break; |
| 3336 | |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3337 | case L2CAP_CONF_RFC: |
| 3338 | if (olen == sizeof(rfc)) |
| 3339 | memcpy(&rfc, (void *) val, olen); |
| 3340 | break; |
| 3341 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3342 | case L2CAP_CONF_FCS: |
| 3343 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3344 | set_bit(CONF_RECV_NO_FCS, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3345 | break; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3346 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3347 | case L2CAP_CONF_EFS: |
| 3348 | remote_efs = 1; |
| 3349 | if (olen == sizeof(efs)) |
| 3350 | memcpy(&efs, (void *) val, olen); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3351 | break; |
| 3352 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3353 | case L2CAP_CONF_EWS: |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3354 | if (!chan->conn->hs_enabled) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3355 | return -ECONNREFUSED; |
| 3356 | |
| 3357 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
| 3358 | set_bit(CONF_EWS_RECV, &chan->conf_state); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3359 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3360 | chan->remote_tx_win = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3361 | break; |
| 3362 | |
| 3363 | default: |
| 3364 | if (hint) |
| 3365 | break; |
| 3366 | |
| 3367 | result = L2CAP_CONF_UNKNOWN; |
| 3368 | *((u8 *) ptr++) = type; |
| 3369 | break; |
| 3370 | } |
| 3371 | } |
| 3372 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3373 | if (chan->num_conf_rsp || chan->num_conf_req > 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3374 | goto done; |
| 3375 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3376 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3377 | case L2CAP_MODE_STREAMING: |
| 3378 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3379 | if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3380 | chan->mode = l2cap_select_mode(rfc.mode, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3381 | chan->conn->feat_mask); |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3382 | break; |
| 3383 | } |
| 3384 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3385 | if (remote_efs) { |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 3386 | if (__l2cap_efs_supported(chan->conn)) |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3387 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3388 | else |
| 3389 | return -ECONNREFUSED; |
| 3390 | } |
| 3391 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3392 | if (chan->mode != rfc.mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3393 | return -ECONNREFUSED; |
Gustavo F. Padovan | 742e519 | 2010-06-08 19:09:48 -0300 | [diff] [blame] | 3394 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3395 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3399 | if (chan->mode != rfc.mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3400 | result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3401 | rfc.mode = chan->mode; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3402 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3403 | if (chan->num_conf_rsp == 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3404 | return -ECONNREFUSED; |
| 3405 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3406 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3407 | (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3408 | } |
| 3409 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3410 | if (result == L2CAP_CONF_SUCCESS) { |
| 3411 | /* Configure output options and let the other side know |
| 3412 | * which ones we don't like. */ |
| 3413 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3414 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
| 3415 | result = L2CAP_CONF_UNACCEPT; |
| 3416 | else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3417 | chan->omtu = mtu; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3418 | set_bit(CONF_MTU_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3419 | } |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3420 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3421 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3422 | if (remote_efs) { |
| 3423 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3424 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3425 | efs.stype != chan->local_stype) { |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3426 | |
| 3427 | result = L2CAP_CONF_UNACCEPT; |
| 3428 | |
| 3429 | if (chan->num_conf_req >= 1) |
| 3430 | return -ECONNREFUSED; |
| 3431 | |
| 3432 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3433 | sizeof(efs), |
| 3434 | (unsigned long) &efs); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3435 | } else { |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3436 | /* Send PENDING Conf Rsp */ |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3437 | result = L2CAP_CONF_PENDING; |
| 3438 | set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3439 | } |
| 3440 | } |
| 3441 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3442 | switch (rfc.mode) { |
| 3443 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3444 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3445 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3446 | break; |
| 3447 | |
| 3448 | case L2CAP_MODE_ERTM: |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3449 | if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) |
| 3450 | chan->remote_tx_win = rfc.txwin_size; |
| 3451 | else |
| 3452 | rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; |
| 3453 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 3454 | chan->remote_max_tx = rfc.max_transmit; |
Mat Martineau | 86b1b26 | 2010-08-05 15:54:22 -0700 | [diff] [blame] | 3455 | |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3456 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3457 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3458 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3459 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3460 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3461 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3462 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3463 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3464 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3465 | |
| 3466 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3467 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3468 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3469 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3470 | chan->remote_id = efs.id; |
| 3471 | chan->remote_stype = efs.stype; |
| 3472 | chan->remote_msdu = le16_to_cpu(efs.msdu); |
| 3473 | chan->remote_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3474 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3475 | chan->remote_acc_lat = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3476 | le32_to_cpu(efs.acc_lat); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3477 | chan->remote_sdu_itime = |
| 3478 | le32_to_cpu(efs.sdu_itime); |
| 3479 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3480 | sizeof(efs), |
| 3481 | (unsigned long) &efs); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3482 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3483 | break; |
| 3484 | |
| 3485 | case L2CAP_MODE_STREAMING: |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3486 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3487 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3488 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3489 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3490 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3491 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3492 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3493 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3494 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3495 | (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3496 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3497 | break; |
| 3498 | |
| 3499 | default: |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3500 | result = L2CAP_CONF_UNACCEPT; |
| 3501 | |
| 3502 | memset(&rfc, 0, sizeof(rfc)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3503 | rfc.mode = chan->mode; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3504 | } |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3505 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3506 | if (result == L2CAP_CONF_SUCCESS) |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3507 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3508 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3509 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3510 | rsp->result = cpu_to_le16(result); |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 3511 | rsp->flags = __constant_cpu_to_le16(0); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3512 | |
| 3513 | return ptr - data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3514 | } |
| 3515 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3516 | static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, |
| 3517 | void *data, u16 *result) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3518 | { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3519 | struct l2cap_conf_req *req = data; |
| 3520 | void *ptr = req->data; |
| 3521 | int type, olen; |
| 3522 | unsigned long val; |
Mat Martineau | 36e999a | 2011-12-08 17:23:21 -0800 | [diff] [blame] | 3523 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3524 | struct l2cap_conf_efs efs; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3525 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3526 | 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] | 3527 | |
| 3528 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3529 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3530 | |
| 3531 | switch (type) { |
| 3532 | case L2CAP_CONF_MTU: |
| 3533 | if (val < L2CAP_DEFAULT_MIN_MTU) { |
| 3534 | *result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3535 | chan->imtu = L2CAP_DEFAULT_MIN_MTU; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3536 | } else |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3537 | chan->imtu = val; |
| 3538 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3539 | break; |
| 3540 | |
| 3541 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3542 | chan->flush_to = val; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3543 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3544 | 2, chan->flush_to); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3545 | break; |
| 3546 | |
| 3547 | case L2CAP_CONF_RFC: |
| 3548 | if (olen == sizeof(rfc)) |
| 3549 | memcpy(&rfc, (void *)val, olen); |
| 3550 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3551 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3552 | rfc.mode != chan->mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3553 | return -ECONNREFUSED; |
| 3554 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3555 | chan->fcs = 0; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3556 | |
| 3557 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3558 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3559 | break; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3560 | |
| 3561 | case L2CAP_CONF_EWS: |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3562 | chan->ack_win = min_t(u16, val, chan->ack_win); |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3563 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3564 | chan->tx_win); |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3565 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3566 | |
| 3567 | case L2CAP_CONF_EFS: |
| 3568 | if (olen == sizeof(efs)) |
| 3569 | memcpy(&efs, (void *)val, olen); |
| 3570 | |
| 3571 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3572 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3573 | efs.stype != chan->local_stype) |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3574 | return -ECONNREFUSED; |
| 3575 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3576 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs), |
| 3577 | (unsigned long) &efs); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3578 | break; |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3579 | |
| 3580 | case L2CAP_CONF_FCS: |
| 3581 | if (*result == L2CAP_CONF_PENDING) |
| 3582 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3583 | set_bit(CONF_RECV_NO_FCS, |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3584 | &chan->conf_state); |
| 3585 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3586 | } |
| 3587 | } |
| 3588 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3589 | if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3590 | return -ECONNREFUSED; |
| 3591 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3592 | chan->mode = rfc.mode; |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3593 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3594 | if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3595 | switch (rfc.mode) { |
| 3596 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3597 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3598 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 3599 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3600 | if (!test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3601 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3602 | rfc.txwin_size); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3603 | |
| 3604 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3605 | chan->local_msdu = le16_to_cpu(efs.msdu); |
| 3606 | chan->local_sdu_itime = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3607 | le32_to_cpu(efs.sdu_itime); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3608 | chan->local_acc_lat = le32_to_cpu(efs.acc_lat); |
| 3609 | chan->local_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3610 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3611 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3612 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3613 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3614 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3615 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3616 | } |
| 3617 | } |
| 3618 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3619 | req->dcid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 3620 | req->flags = __constant_cpu_to_le16(0); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3621 | |
| 3622 | return ptr - data; |
| 3623 | } |
| 3624 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3625 | static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, |
| 3626 | u16 result, u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | { |
| 3628 | struct l2cap_conf_rsp *rsp = data; |
| 3629 | void *ptr = rsp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3631 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3632 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3633 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3634 | rsp->result = cpu_to_le16(result); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3635 | rsp->flags = cpu_to_le16(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3636 | |
| 3637 | return ptr - data; |
| 3638 | } |
| 3639 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3640 | void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3641 | { |
| 3642 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3643 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3644 | u8 buf[128]; |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3645 | u8 rsp_code; |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3646 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3647 | rsp.scid = cpu_to_le16(chan->dcid); |
| 3648 | rsp.dcid = cpu_to_le16(chan->scid); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 3649 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS); |
| 3650 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3651 | |
| 3652 | if (chan->hs_hcon) |
| 3653 | rsp_code = L2CAP_CREATE_CHAN_RSP; |
| 3654 | else |
| 3655 | rsp_code = L2CAP_CONN_RSP; |
| 3656 | |
| 3657 | BT_DBG("chan %p rsp_code %u", chan, rsp_code); |
| 3658 | |
| 3659 | l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp); |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3660 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3661 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3662 | return; |
| 3663 | |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3664 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3665 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3666 | chan->num_conf_req++; |
| 3667 | } |
| 3668 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3669 | 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] | 3670 | { |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3671 | int type, olen; |
| 3672 | unsigned long val; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3673 | /* Use sane default values in case a misbehaving remote device |
| 3674 | * did not send an RFC or extended window size option. |
| 3675 | */ |
| 3676 | u16 txwin_ext = chan->ack_win; |
| 3677 | struct l2cap_conf_rfc rfc = { |
| 3678 | .mode = chan->mode, |
| 3679 | .retrans_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO), |
| 3680 | .monitor_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO), |
| 3681 | .max_pdu_size = cpu_to_le16(chan->imtu), |
| 3682 | .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW), |
| 3683 | }; |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3684 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3685 | BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3686 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3687 | if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3688 | return; |
| 3689 | |
| 3690 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3691 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3692 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3693 | switch (type) { |
| 3694 | case L2CAP_CONF_RFC: |
| 3695 | if (olen == sizeof(rfc)) |
| 3696 | memcpy(&rfc, (void *)val, olen); |
Szymon Janc | 8f321f8 | 2012-06-08 11:33:33 +0200 | [diff] [blame] | 3697 | break; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3698 | case L2CAP_CONF_EWS: |
| 3699 | txwin_ext = val; |
| 3700 | break; |
| 3701 | } |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3702 | } |
| 3703 | |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3704 | switch (rfc.mode) { |
| 3705 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3706 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3707 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3708 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
| 3709 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3710 | chan->ack_win = min_t(u16, chan->ack_win, txwin_ext); |
| 3711 | else |
| 3712 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3713 | rfc.txwin_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3714 | break; |
| 3715 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3716 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3717 | } |
| 3718 | } |
| 3719 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3720 | static inline int l2cap_command_rej(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3721 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3722 | u8 *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3723 | { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3724 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3725 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3726 | if (cmd_len < sizeof(*rej)) |
| 3727 | return -EPROTO; |
| 3728 | |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3729 | if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3730 | return 0; |
| 3731 | |
| 3732 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3733 | cmd->ident == conn->info_ident) { |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 3734 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3735 | |
| 3736 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3737 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3738 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3739 | l2cap_conn_start(conn); |
| 3740 | } |
| 3741 | |
| 3742 | return 0; |
| 3743 | } |
| 3744 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3745 | static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn, |
| 3746 | struct l2cap_cmd_hdr *cmd, |
| 3747 | u8 *data, u8 rsp_code, u8 amp_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3748 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3749 | struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; |
| 3750 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3751 | struct l2cap_chan *chan = NULL, *pchan; |
Nathan Holstein | d793fe8 | 2010-10-15 11:54:02 -0400 | [diff] [blame] | 3752 | struct sock *parent, *sk = NULL; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3753 | int result, status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3754 | |
| 3755 | u16 dcid = 0, scid = __le16_to_cpu(req->scid); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3756 | __le16 psm = req->psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3757 | |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 3758 | 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] | 3759 | |
| 3760 | /* Check if we have socket listening on psm */ |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 3761 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src, |
Marcel Holtmann | 98e0f7e | 2013-10-13 02:23:40 -0700 | [diff] [blame] | 3762 | &conn->hcon->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3763 | if (!pchan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | result = L2CAP_CR_BAD_PSM; |
| 3765 | goto sendresp; |
| 3766 | } |
| 3767 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3768 | parent = pchan->sk; |
| 3769 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3770 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 3771 | lock_sock(parent); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 3772 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3773 | /* Check if the ACL is secure enough (if not SDP) */ |
Andrei Emeltchenko | 2983fd6 | 2012-05-24 15:42:50 +0300 | [diff] [blame] | 3774 | if (psm != __constant_cpu_to_le16(L2CAP_PSM_SDP) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3775 | !hci_conn_check_link_mode(conn->hcon)) { |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 3776 | conn->disc_reason = HCI_ERROR_AUTH_FAILURE; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3777 | result = L2CAP_CR_SEC_BLOCK; |
| 3778 | goto response; |
| 3779 | } |
| 3780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3781 | result = L2CAP_CR_NO_MEM; |
| 3782 | |
Gustavo Padovan | 2dfa100 | 2012-05-27 22:27:58 -0300 | [diff] [blame] | 3783 | /* Check if we already have channel with that dcid */ |
| 3784 | if (__l2cap_get_chan_by_dcid(conn, scid)) |
| 3785 | goto response; |
| 3786 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 3787 | chan = pchan->ops->new_connection(pchan); |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 3788 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3789 | goto response; |
| 3790 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 3791 | sk = chan->sk; |
| 3792 | |
Syam Sidhardhan | 330b6c1 | 2013-08-06 01:59:12 +0900 | [diff] [blame] | 3793 | /* For certain devices (ex: HID mouse), support for authentication, |
| 3794 | * pairing and bonding is optional. For such devices, inorder to avoid |
| 3795 | * the ACL alive for too long after L2CAP disconnection, reset the ACL |
| 3796 | * disc_timeout back to HCI_DISCONN_TIMEOUT during L2CAP connect. |
| 3797 | */ |
| 3798 | conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT; |
| 3799 | |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 3800 | bacpy(&bt_sk(sk)->src, &conn->hcon->src); |
Marcel Holtmann | 98e0f7e | 2013-10-13 02:23:40 -0700 | [diff] [blame] | 3801 | bacpy(&bt_sk(sk)->dst, &conn->hcon->dst); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3802 | chan->psm = psm; |
| 3803 | chan->dcid = scid; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3804 | chan->local_amp_id = amp_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3805 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3806 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3807 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3808 | dcid = chan->scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3809 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 3810 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3811 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3812 | chan->ident = cmd->ident; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3813 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3814 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 3815 | if (l2cap_chan_check_security(chan)) { |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 3816 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 3817 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3818 | result = L2CAP_CR_PEND; |
| 3819 | status = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 3820 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3821 | } else { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3822 | /* Force pending result for AMP controllers. |
| 3823 | * The connection will succeed after the |
| 3824 | * physical link is up. |
| 3825 | */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3826 | if (amp_id == AMP_ID_BREDR) { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3827 | __l2cap_state_change(chan, BT_CONFIG); |
| 3828 | result = L2CAP_CR_SUCCESS; |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 3829 | } else { |
| 3830 | __l2cap_state_change(chan, BT_CONNECT2); |
| 3831 | result = L2CAP_CR_PEND; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3832 | } |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3833 | status = L2CAP_CS_NO_INFO; |
| 3834 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3835 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 3836 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3837 | result = L2CAP_CR_PEND; |
| 3838 | status = L2CAP_CS_AUTHEN_PEND; |
| 3839 | } |
| 3840 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 3841 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3842 | result = L2CAP_CR_PEND; |
| 3843 | status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3844 | } |
| 3845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3846 | response: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 3847 | release_sock(parent); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3848 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3849 | |
| 3850 | sendresp: |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3851 | rsp.scid = cpu_to_le16(scid); |
| 3852 | rsp.dcid = cpu_to_le16(dcid); |
| 3853 | rsp.result = cpu_to_le16(result); |
| 3854 | rsp.status = cpu_to_le16(status); |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3855 | l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3856 | |
| 3857 | if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { |
| 3858 | struct l2cap_info_req info; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 3859 | info.type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3860 | |
| 3861 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 3862 | conn->info_ident = l2cap_get_ident(conn); |
| 3863 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 3864 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3865 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3866 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 3867 | sizeof(info), &info); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3868 | } |
| 3869 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3870 | if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3871 | result == L2CAP_CR_SUCCESS) { |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3872 | u8 buf[128]; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3873 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3874 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3875 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3876 | chan->num_conf_req++; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3877 | } |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3878 | |
| 3879 | return chan; |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3880 | } |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3881 | |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3882 | static int l2cap_connect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3883 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3884 | { |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3885 | struct hci_dev *hdev = conn->hcon->hdev; |
| 3886 | struct hci_conn *hcon = conn->hcon; |
| 3887 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3888 | if (cmd_len < sizeof(struct l2cap_conn_req)) |
| 3889 | return -EPROTO; |
| 3890 | |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3891 | hci_dev_lock(hdev); |
| 3892 | if (test_bit(HCI_MGMT, &hdev->dev_flags) && |
| 3893 | !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags)) |
| 3894 | mgmt_device_connected(hdev, &hcon->dst, hcon->type, |
| 3895 | hcon->dst_type, 0, NULL, 0, |
| 3896 | hcon->dev_class); |
| 3897 | hci_dev_unlock(hdev); |
| 3898 | |
Gustavo Padovan | 300229f | 2012-10-12 19:40:40 +0800 | [diff] [blame] | 3899 | l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | return 0; |
| 3901 | } |
| 3902 | |
Mat Martineau | 5909cf3 | 2012-10-23 15:24:08 -0700 | [diff] [blame] | 3903 | static int l2cap_connect_create_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3904 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3905 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3906 | { |
| 3907 | struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; |
| 3908 | u16 scid, dcid, result, status; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3909 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3910 | u8 req[128]; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3911 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3912 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3913 | if (cmd_len < sizeof(*rsp)) |
| 3914 | return -EPROTO; |
| 3915 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3916 | scid = __le16_to_cpu(rsp->scid); |
| 3917 | dcid = __le16_to_cpu(rsp->dcid); |
| 3918 | result = __le16_to_cpu(rsp->result); |
| 3919 | status = __le16_to_cpu(rsp->status); |
| 3920 | |
Andrei Emeltchenko | 1b009c9 | 2012-02-21 12:54:54 +0200 | [diff] [blame] | 3921 | 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] | 3922 | dcid, scid, result, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3923 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3924 | mutex_lock(&conn->chan_lock); |
| 3925 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3926 | if (scid) { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3927 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 3928 | if (!chan) { |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 3929 | err = -EBADSLT; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3930 | goto unlock; |
| 3931 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3932 | } else { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3933 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 3934 | if (!chan) { |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 3935 | err = -EBADSLT; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3936 | goto unlock; |
| 3937 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3938 | } |
| 3939 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3940 | err = 0; |
| 3941 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3942 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | switch (result) { |
| 3945 | case L2CAP_CR_SUCCESS: |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 3946 | l2cap_state_change(chan, BT_CONFIG); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3947 | chan->ident = 0; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3948 | chan->dcid = dcid; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3949 | clear_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 3950 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3951 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3952 | break; |
| 3953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3954 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3955 | l2cap_build_conf_req(chan, req), req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3956 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3957 | break; |
| 3958 | |
| 3959 | case L2CAP_CR_PEND: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3960 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3961 | break; |
| 3962 | |
| 3963 | default: |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3964 | l2cap_chan_del(chan, ECONNREFUSED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | break; |
| 3966 | } |
| 3967 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3968 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3969 | |
| 3970 | unlock: |
| 3971 | mutex_unlock(&conn->chan_lock); |
| 3972 | |
| 3973 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | } |
| 3975 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3976 | static inline void set_default_fcs(struct l2cap_chan *chan) |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 3977 | { |
| 3978 | /* FCS is enabled only in ERTM or streaming mode, if one or both |
| 3979 | * sides request it. |
| 3980 | */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3981 | if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3982 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3983 | else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3984 | chan->fcs = L2CAP_FCS_CRC16; |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 3985 | } |
| 3986 | |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 3987 | static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data, |
| 3988 | u8 ident, u16 flags) |
| 3989 | { |
| 3990 | struct l2cap_conn *conn = chan->conn; |
| 3991 | |
| 3992 | BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident, |
| 3993 | flags); |
| 3994 | |
| 3995 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
| 3996 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
| 3997 | |
| 3998 | l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP, |
| 3999 | l2cap_build_conf_rsp(chan, data, |
| 4000 | L2CAP_CONF_SUCCESS, flags), data); |
| 4001 | } |
| 4002 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4003 | static inline int l2cap_config_req(struct l2cap_conn *conn, |
| 4004 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4005 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4006 | { |
| 4007 | struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; |
| 4008 | u16 dcid, flags; |
| 4009 | u8 rsp[64]; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4010 | struct l2cap_chan *chan; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4011 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4013 | if (cmd_len < sizeof(*req)) |
| 4014 | return -EPROTO; |
| 4015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | dcid = __le16_to_cpu(req->dcid); |
| 4017 | flags = __le16_to_cpu(req->flags); |
| 4018 | |
| 4019 | BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); |
| 4020 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4021 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4022 | if (!chan) |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 4023 | return -EBADSLT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | |
David S. Miller | 033b114 | 2011-07-21 13:38:42 -0700 | [diff] [blame] | 4025 | if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 4026 | struct l2cap_cmd_rej_cid rej; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 4027 | |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4028 | rej.reason = __constant_cpu_to_le16(L2CAP_REJ_INVALID_CID); |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 4029 | rej.scid = cpu_to_le16(chan->scid); |
| 4030 | rej.dcid = cpu_to_le16(chan->dcid); |
| 4031 | |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 4032 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4033 | sizeof(rej), &rej); |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 4034 | goto unlock; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 4035 | } |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 4036 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4037 | /* Reject if config buffer is too small. */ |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 4038 | len = cmd_len - sizeof(*req); |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4039 | if (chan->conf_len + len > sizeof(chan->conf_req)) { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4040 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4041 | l2cap_build_conf_rsp(chan, rsp, |
| 4042 | L2CAP_CONF_REJECT, flags), rsp); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4043 | goto unlock; |
| 4044 | } |
| 4045 | |
| 4046 | /* Store config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4047 | memcpy(chan->conf_req + chan->conf_len, req->data, len); |
| 4048 | chan->conf_len += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4049 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4050 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4051 | /* Incomplete config. Send empty response. */ |
| 4052 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4053 | l2cap_build_conf_rsp(chan, rsp, |
| 4054 | L2CAP_CONF_SUCCESS, flags), rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4055 | goto unlock; |
| 4056 | } |
| 4057 | |
| 4058 | /* Complete config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4059 | len = l2cap_parse_conf_req(chan, rsp); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4060 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4061 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | goto unlock; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4063 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4064 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4065 | chan->ident = cmd->ident; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4066 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4067 | chan->num_conf_rsp++; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4068 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4069 | /* Reset config buffer. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4070 | chan->conf_len = 0; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4071 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4072 | if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4073 | goto unlock; |
| 4074 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4075 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4076 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4077 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4078 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4079 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4080 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4081 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4082 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4083 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4084 | else |
| 4085 | l2cap_chan_ready(chan); |
| 4086 | |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4087 | goto unlock; |
| 4088 | } |
| 4089 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4090 | if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 4091 | u8 buf[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4092 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4093 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4094 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4095 | } |
| 4096 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4097 | /* Got Conf Rsp PENDING from remote side and asume we sent |
| 4098 | Conf Rsp PENDING in the code above */ |
| 4099 | if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 4100 | test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4101 | |
| 4102 | /* check compatibility */ |
| 4103 | |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4104 | /* Send rsp for BR/EDR channel */ |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4105 | if (!chan->hs_hcon) |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4106 | l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags); |
| 4107 | else |
| 4108 | chan->ident = cmd->ident; |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4109 | } |
| 4110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4111 | unlock: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4112 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4113 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4114 | } |
| 4115 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4116 | static inline int l2cap_config_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4117 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4118 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | { |
| 4120 | struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; |
| 4121 | u16 scid, flags, result; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4122 | struct l2cap_chan *chan; |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4123 | int len = cmd_len - sizeof(*rsp); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4124 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4126 | if (cmd_len < sizeof(*rsp)) |
| 4127 | return -EPROTO; |
| 4128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | scid = __le16_to_cpu(rsp->scid); |
| 4130 | flags = __le16_to_cpu(rsp->flags); |
| 4131 | result = __le16_to_cpu(rsp->result); |
| 4132 | |
Andrei Emeltchenko | 61386cb | 2012-03-12 12:13:07 +0200 | [diff] [blame] | 4133 | BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags, |
| 4134 | result, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4135 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4136 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4137 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4138 | return 0; |
| 4139 | |
| 4140 | switch (result) { |
| 4141 | case L2CAP_CONF_SUCCESS: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4142 | l2cap_conf_rfc_get(chan, rsp->data, len); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4143 | clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4144 | break; |
| 4145 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4146 | case L2CAP_CONF_PENDING: |
| 4147 | set_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
| 4148 | |
| 4149 | if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
| 4150 | char buf[64]; |
| 4151 | |
| 4152 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4153 | buf, &result); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4154 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4155 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4156 | goto done; |
| 4157 | } |
| 4158 | |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4159 | if (!chan->hs_hcon) { |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4160 | l2cap_send_efs_conf_rsp(chan, buf, cmd->ident, |
| 4161 | 0); |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 4162 | } else { |
| 4163 | if (l2cap_check_efs(chan)) { |
| 4164 | amp_create_logical_link(chan); |
| 4165 | chan->ident = cmd->ident; |
| 4166 | } |
| 4167 | } |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4168 | } |
| 4169 | goto done; |
| 4170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | case L2CAP_CONF_UNACCEPT: |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4172 | if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4173 | char req[64]; |
| 4174 | |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4175 | if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4176 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4177 | goto done; |
| 4178 | } |
| 4179 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4180 | /* throw out any old stored conf requests */ |
| 4181 | result = L2CAP_CONF_SUCCESS; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 4182 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4183 | req, &result); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4184 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4185 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4186 | goto done; |
| 4187 | } |
| 4188 | |
| 4189 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4190 | L2CAP_CONF_REQ, len, req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4191 | chan->num_conf_req++; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4192 | if (result != L2CAP_CONF_SUCCESS) |
| 4193 | goto done; |
| 4194 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | } |
| 4196 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 4197 | default: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4198 | l2cap_chan_set_err(chan, ECONNRESET); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 4199 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 4200 | __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4201 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | goto done; |
| 4203 | } |
| 4204 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4205 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4206 | goto done; |
| 4207 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4208 | set_bit(CONF_INPUT_DONE, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4209 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4210 | if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4211 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4212 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4213 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4214 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4215 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4216 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4217 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4218 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4219 | else |
| 4220 | l2cap_chan_ready(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | } |
| 4222 | |
| 4223 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4224 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4225 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | } |
| 4227 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4228 | static inline int l2cap_disconnect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4229 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4230 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | { |
| 4232 | struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; |
| 4233 | struct l2cap_disconn_rsp rsp; |
| 4234 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4235 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | struct sock *sk; |
| 4237 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4238 | if (cmd_len != sizeof(*req)) |
| 4239 | return -EPROTO; |
| 4240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | scid = __le16_to_cpu(req->scid); |
| 4242 | dcid = __le16_to_cpu(req->dcid); |
| 4243 | |
| 4244 | BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); |
| 4245 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4246 | mutex_lock(&conn->chan_lock); |
| 4247 | |
| 4248 | chan = __l2cap_get_chan_by_scid(conn, dcid); |
| 4249 | if (!chan) { |
| 4250 | mutex_unlock(&conn->chan_lock); |
Johan Hedberg | c4ea249 | 2013-09-16 13:05:15 +0300 | [diff] [blame] | 4251 | return -EBADSLT; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4252 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4253 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4254 | l2cap_chan_lock(chan); |
| 4255 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4256 | sk = chan->sk; |
| 4257 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4258 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4259 | rsp.scid = cpu_to_le16(chan->dcid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); |
| 4261 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4262 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | sk->sk_shutdown = SHUTDOWN_MASK; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4264 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4266 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4267 | l2cap_chan_del(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4268 | |
| 4269 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4271 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4272 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4273 | |
| 4274 | mutex_unlock(&conn->chan_lock); |
| 4275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4276 | return 0; |
| 4277 | } |
| 4278 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4279 | static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4280 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4281 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | { |
| 4283 | struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; |
| 4284 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4285 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4287 | if (cmd_len != sizeof(*rsp)) |
| 4288 | return -EPROTO; |
| 4289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4290 | scid = __le16_to_cpu(rsp->scid); |
| 4291 | dcid = __le16_to_cpu(rsp->dcid); |
| 4292 | |
| 4293 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); |
| 4294 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4295 | mutex_lock(&conn->chan_lock); |
| 4296 | |
| 4297 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 4298 | if (!chan) { |
| 4299 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4300 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4302 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4303 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4304 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4305 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4306 | l2cap_chan_del(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4307 | |
| 4308 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4309 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4310 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4311 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4312 | |
| 4313 | mutex_unlock(&conn->chan_lock); |
| 4314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4315 | return 0; |
| 4316 | } |
| 4317 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4318 | static inline int l2cap_information_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4319 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4320 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | { |
| 4322 | struct l2cap_info_req *req = (struct l2cap_info_req *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | u16 type; |
| 4324 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4325 | if (cmd_len != sizeof(*req)) |
| 4326 | return -EPROTO; |
| 4327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4328 | type = __le16_to_cpu(req->type); |
| 4329 | |
| 4330 | BT_DBG("type 0x%4.4x", type); |
| 4331 | |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4332 | if (type == L2CAP_IT_FEAT_MASK) { |
| 4333 | u8 buf[8]; |
Marcel Holtmann | 44dd46d | 2009-05-02 19:09:01 -0700 | [diff] [blame] | 4334 | u32 feat_mask = l2cap_feat_mask; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4335 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4336 | rsp->type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 4337 | rsp->result = __constant_cpu_to_le16(L2CAP_IR_SUCCESS); |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 4338 | if (!disable_ertm) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4339 | feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4340 | | L2CAP_FEAT_FCS; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4341 | if (conn->hs_enabled) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 4342 | feat_mask |= L2CAP_FEAT_EXT_FLOW |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4343 | | L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 4344 | |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 4345 | put_unaligned_le32(feat_mask, rsp->data); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4346 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4347 | buf); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4348 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
| 4349 | u8 buf[12]; |
| 4350 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 4351 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4352 | if (conn->hs_enabled) |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 4353 | l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; |
| 4354 | else |
| 4355 | l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; |
| 4356 | |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4357 | rsp->type = __constant_cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 4358 | rsp->result = __constant_cpu_to_le16(L2CAP_IR_SUCCESS); |
Andrei Emeltchenko | c6337ea | 2011-10-20 17:02:44 +0300 | [diff] [blame] | 4359 | memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan)); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4360 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4361 | buf); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4362 | } else { |
| 4363 | struct l2cap_info_rsp rsp; |
| 4364 | rsp.type = cpu_to_le16(type); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4365 | rsp.result = __constant_cpu_to_le16(L2CAP_IR_NOTSUPP); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4366 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp), |
| 4367 | &rsp); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | |
| 4370 | return 0; |
| 4371 | } |
| 4372 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4373 | static inline int l2cap_information_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4374 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4375 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | { |
| 4377 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; |
| 4378 | u16 type, result; |
| 4379 | |
Jaganath Kanakkassery | 3f6fa3d | 2013-06-21 19:55:11 +0530 | [diff] [blame] | 4380 | if (cmd_len < sizeof(*rsp)) |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4381 | return -EPROTO; |
| 4382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | type = __le16_to_cpu(rsp->type); |
| 4384 | result = __le16_to_cpu(rsp->result); |
| 4385 | |
| 4386 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); |
| 4387 | |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4388 | /* L2CAP Info req/rsp are unbound to channels, add extra checks */ |
| 4389 | if (cmd->ident != conn->info_ident || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4390 | conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4391 | return 0; |
| 4392 | |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 4393 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4394 | |
Ville Tervo | adb08ed | 2010-08-04 09:43:33 +0300 | [diff] [blame] | 4395 | if (result != L2CAP_IR_SUCCESS) { |
| 4396 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4397 | conn->info_ident = 0; |
| 4398 | |
| 4399 | l2cap_conn_start(conn); |
| 4400 | |
| 4401 | return 0; |
| 4402 | } |
| 4403 | |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4404 | switch (type) { |
| 4405 | case L2CAP_IT_FEAT_MASK: |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 4406 | conn->feat_mask = get_unaligned_le32(rsp->data); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4407 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 4408 | if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4409 | struct l2cap_info_req req; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4410 | req.type = __constant_cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4411 | |
| 4412 | conn->info_ident = l2cap_get_ident(conn); |
| 4413 | |
| 4414 | l2cap_send_cmd(conn, conn->info_ident, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4415 | L2CAP_INFO_REQ, sizeof(req), &req); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4416 | } else { |
| 4417 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4418 | conn->info_ident = 0; |
| 4419 | |
| 4420 | l2cap_conn_start(conn); |
| 4421 | } |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4422 | break; |
| 4423 | |
| 4424 | case L2CAP_IT_FIXED_CHAN: |
| 4425 | conn->fixed_chan_mask = rsp->data[0]; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4426 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4427 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4428 | |
| 4429 | l2cap_conn_start(conn); |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4430 | break; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4431 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4433 | return 0; |
| 4434 | } |
| 4435 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4436 | static int l2cap_create_channel_req(struct l2cap_conn *conn, |
| 4437 | struct l2cap_cmd_hdr *cmd, |
| 4438 | u16 cmd_len, void *data) |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4439 | { |
| 4440 | struct l2cap_create_chan_req *req = data; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4441 | struct l2cap_create_chan_rsp rsp; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4442 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4443 | struct hci_dev *hdev; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4444 | u16 psm, scid; |
| 4445 | |
| 4446 | if (cmd_len != sizeof(*req)) |
| 4447 | return -EPROTO; |
| 4448 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4449 | if (!conn->hs_enabled) |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4450 | return -EINVAL; |
| 4451 | |
| 4452 | psm = le16_to_cpu(req->psm); |
| 4453 | scid = le16_to_cpu(req->scid); |
| 4454 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4455 | 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] | 4456 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4457 | /* For controller id 0 make BR/EDR connection */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4458 | if (req->amp_id == AMP_ID_BREDR) { |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4459 | l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4460 | req->amp_id); |
| 4461 | return 0; |
| 4462 | } |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4463 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4464 | /* Validate AMP controller id */ |
| 4465 | hdev = hci_dev_get(req->amp_id); |
| 4466 | if (!hdev) |
| 4467 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4468 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4469 | if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4470 | hci_dev_put(hdev); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4471 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4472 | } |
| 4473 | |
| 4474 | chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4475 | req->amp_id); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4476 | if (chan) { |
| 4477 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
| 4478 | struct hci_conn *hs_hcon; |
| 4479 | |
Marcel Holtmann | 98e0f7e | 2013-10-13 02:23:40 -0700 | [diff] [blame] | 4480 | hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, |
| 4481 | &conn->hcon->dst); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4482 | if (!hs_hcon) { |
| 4483 | hci_dev_put(hdev); |
Johan Hedberg | 21870b5 | 2013-09-16 13:05:14 +0300 | [diff] [blame] | 4484 | return -EBADSLT; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4485 | } |
| 4486 | |
| 4487 | BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon); |
| 4488 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4489 | mgr->bredr_chan = chan; |
| 4490 | chan->hs_hcon = hs_hcon; |
Andrei Emeltchenko | fd45bf4 | 2012-11-20 17:16:22 +0200 | [diff] [blame] | 4491 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4492 | conn->mtu = hdev->block_mtu; |
| 4493 | } |
| 4494 | |
| 4495 | hci_dev_put(hdev); |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4496 | |
| 4497 | return 0; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4498 | |
| 4499 | error: |
| 4500 | rsp.dcid = 0; |
| 4501 | rsp.scid = cpu_to_le16(scid); |
| 4502 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_BAD_AMP); |
| 4503 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
| 4504 | |
| 4505 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, |
| 4506 | sizeof(rsp), &rsp); |
| 4507 | |
Johan Hedberg | dc28080 | 2013-09-16 13:05:13 +0300 | [diff] [blame] | 4508 | return 0; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4509 | } |
| 4510 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4511 | static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id) |
| 4512 | { |
| 4513 | struct l2cap_move_chan_req req; |
| 4514 | u8 ident; |
| 4515 | |
| 4516 | BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id); |
| 4517 | |
| 4518 | ident = l2cap_get_ident(chan->conn); |
| 4519 | chan->ident = ident; |
| 4520 | |
| 4521 | req.icid = cpu_to_le16(chan->scid); |
| 4522 | req.dest_amp_id = dest_amp_id; |
| 4523 | |
| 4524 | l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req), |
| 4525 | &req); |
| 4526 | |
| 4527 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4528 | } |
| 4529 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4530 | 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] | 4531 | { |
| 4532 | struct l2cap_move_chan_rsp rsp; |
| 4533 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4534 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4535 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4536 | rsp.icid = cpu_to_le16(chan->dcid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4537 | rsp.result = cpu_to_le16(result); |
| 4538 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4539 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP, |
| 4540 | sizeof(rsp), &rsp); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4541 | } |
| 4542 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4543 | 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] | 4544 | { |
| 4545 | struct l2cap_move_chan_cfm cfm; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4546 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4547 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4548 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4549 | chan->ident = l2cap_get_ident(chan->conn); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4550 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4551 | cfm.icid = cpu_to_le16(chan->scid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4552 | cfm.result = cpu_to_le16(result); |
| 4553 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4554 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM, |
| 4555 | sizeof(cfm), &cfm); |
| 4556 | |
| 4557 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4558 | } |
| 4559 | |
| 4560 | static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid) |
| 4561 | { |
| 4562 | struct l2cap_move_chan_cfm cfm; |
| 4563 | |
| 4564 | BT_DBG("conn %p, icid 0x%4.4x", conn, icid); |
| 4565 | |
| 4566 | cfm.icid = cpu_to_le16(icid); |
| 4567 | cfm.result = __constant_cpu_to_le16(L2CAP_MC_UNCONFIRMED); |
| 4568 | |
| 4569 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM, |
| 4570 | sizeof(cfm), &cfm); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | 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] | 4574 | u16 icid) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4575 | { |
| 4576 | struct l2cap_move_chan_cfm_rsp rsp; |
| 4577 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4578 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4579 | |
| 4580 | rsp.icid = cpu_to_le16(icid); |
| 4581 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); |
| 4582 | } |
| 4583 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 4584 | static void __release_logical_link(struct l2cap_chan *chan) |
| 4585 | { |
| 4586 | chan->hs_hchan = NULL; |
| 4587 | chan->hs_hcon = NULL; |
| 4588 | |
| 4589 | /* Placeholder - release the logical link */ |
| 4590 | } |
| 4591 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4592 | static void l2cap_logical_fail(struct l2cap_chan *chan) |
| 4593 | { |
| 4594 | /* Logical link setup failed */ |
| 4595 | if (chan->state != BT_CONNECTED) { |
| 4596 | /* Create channel failure, disconnect */ |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4597 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4598 | return; |
| 4599 | } |
| 4600 | |
| 4601 | switch (chan->move_role) { |
| 4602 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4603 | l2cap_move_done(chan); |
| 4604 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP); |
| 4605 | break; |
| 4606 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4607 | if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP || |
| 4608 | chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) { |
| 4609 | /* Remote has only sent pending or |
| 4610 | * success responses, clean up |
| 4611 | */ |
| 4612 | l2cap_move_done(chan); |
| 4613 | } |
| 4614 | |
| 4615 | /* Other amp move states imply that the move |
| 4616 | * has already aborted |
| 4617 | */ |
| 4618 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 4619 | break; |
| 4620 | } |
| 4621 | } |
| 4622 | |
| 4623 | static void l2cap_logical_finish_create(struct l2cap_chan *chan, |
| 4624 | struct hci_chan *hchan) |
| 4625 | { |
| 4626 | struct l2cap_conf_rsp rsp; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4627 | |
Andrei Emeltchenko | 336178a | 2012-10-31 15:46:27 +0200 | [diff] [blame] | 4628 | chan->hs_hchan = hchan; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4629 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4630 | |
Andrei Emeltchenko | 35ba956 | 2012-10-25 15:20:43 +0300 | [diff] [blame] | 4631 | l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4632 | |
| 4633 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Andrei Emeltchenko | fe79c6f | 2012-10-25 15:20:42 +0300 | [diff] [blame] | 4634 | int err; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4635 | |
| 4636 | set_default_fcs(chan); |
| 4637 | |
| 4638 | err = l2cap_ertm_init(chan); |
| 4639 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4640 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4641 | else |
| 4642 | l2cap_chan_ready(chan); |
| 4643 | } |
| 4644 | } |
| 4645 | |
| 4646 | static void l2cap_logical_finish_move(struct l2cap_chan *chan, |
| 4647 | struct hci_chan *hchan) |
| 4648 | { |
| 4649 | chan->hs_hcon = hchan->conn; |
| 4650 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4651 | |
| 4652 | BT_DBG("move_state %d", chan->move_state); |
| 4653 | |
| 4654 | switch (chan->move_state) { |
| 4655 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 4656 | /* Move confirm will be sent after a success |
| 4657 | * response is received |
| 4658 | */ |
| 4659 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4660 | break; |
| 4661 | case L2CAP_MOVE_WAIT_LOGICAL_CFM: |
| 4662 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4663 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4664 | } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 4665 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 4666 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4667 | } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4668 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4669 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4670 | } |
| 4671 | break; |
| 4672 | default: |
| 4673 | /* Move was not in expected state, free the channel */ |
| 4674 | __release_logical_link(chan); |
| 4675 | |
| 4676 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4677 | } |
| 4678 | } |
| 4679 | |
| 4680 | /* Call with chan locked */ |
Andrei Emeltchenko | 27695fb | 2012-10-25 15:20:45 +0300 | [diff] [blame] | 4681 | void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan, |
| 4682 | u8 status) |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4683 | { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4684 | BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status); |
| 4685 | |
| 4686 | if (status) { |
| 4687 | l2cap_logical_fail(chan); |
| 4688 | __release_logical_link(chan); |
| 4689 | return; |
| 4690 | } |
| 4691 | |
| 4692 | if (chan->state != BT_CONNECTED) { |
| 4693 | /* Ignore logical link if channel is on BR/EDR */ |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4694 | if (chan->local_amp_id != AMP_ID_BREDR) |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4695 | l2cap_logical_finish_create(chan, hchan); |
| 4696 | } else { |
| 4697 | l2cap_logical_finish_move(chan, hchan); |
| 4698 | } |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4699 | } |
| 4700 | |
Mat Martineau | 3f7a56c | 2012-10-23 15:24:23 -0700 | [diff] [blame] | 4701 | void l2cap_move_start(struct l2cap_chan *chan) |
| 4702 | { |
| 4703 | BT_DBG("chan %p", chan); |
| 4704 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4705 | if (chan->local_amp_id == AMP_ID_BREDR) { |
Mat Martineau | 3f7a56c | 2012-10-23 15:24:23 -0700 | [diff] [blame] | 4706 | if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED) |
| 4707 | return; |
| 4708 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4709 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4710 | /* Placeholder - start physical link setup */ |
| 4711 | } else { |
| 4712 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4713 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4714 | chan->move_id = 0; |
| 4715 | l2cap_move_setup(chan); |
| 4716 | l2cap_send_move_chan_req(chan, 0); |
| 4717 | } |
| 4718 | } |
| 4719 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4720 | static void l2cap_do_create(struct l2cap_chan *chan, int result, |
| 4721 | u8 local_amp_id, u8 remote_amp_id) |
| 4722 | { |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4723 | BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state), |
| 4724 | local_amp_id, remote_amp_id); |
| 4725 | |
Andrei Emeltchenko | 12d6cc6 | 2012-11-14 17:39:31 +0200 | [diff] [blame] | 4726 | chan->fcs = L2CAP_FCS_NONE; |
| 4727 | |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4728 | /* Outgoing channel on AMP */ |
| 4729 | if (chan->state == BT_CONNECT) { |
| 4730 | if (result == L2CAP_CR_SUCCESS) { |
| 4731 | chan->local_amp_id = local_amp_id; |
| 4732 | l2cap_send_create_chan_req(chan, remote_amp_id); |
| 4733 | } else { |
| 4734 | /* Revert to BR/EDR connect */ |
| 4735 | l2cap_send_conn_req(chan); |
| 4736 | } |
| 4737 | |
| 4738 | return; |
| 4739 | } |
| 4740 | |
| 4741 | /* Incoming channel on AMP */ |
| 4742 | if (__l2cap_no_conn_pending(chan)) { |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4743 | struct l2cap_conn_rsp rsp; |
| 4744 | char buf[128]; |
| 4745 | rsp.scid = cpu_to_le16(chan->dcid); |
| 4746 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4747 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4748 | if (result == L2CAP_CR_SUCCESS) { |
| 4749 | /* Send successful response */ |
Andrei Emeltchenko | 62cd50e | 2012-11-14 17:39:32 +0200 | [diff] [blame] | 4750 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS); |
| 4751 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4752 | } else { |
| 4753 | /* Send negative response */ |
Andrei Emeltchenko | 62cd50e | 2012-11-14 17:39:32 +0200 | [diff] [blame] | 4754 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_NO_MEM); |
| 4755 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP, |
| 4759 | sizeof(rsp), &rsp); |
| 4760 | |
| 4761 | if (result == L2CAP_CR_SUCCESS) { |
| 4762 | __l2cap_state_change(chan, BT_CONFIG); |
| 4763 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 4764 | l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn), |
| 4765 | L2CAP_CONF_REQ, |
| 4766 | l2cap_build_conf_req(chan, buf), buf); |
| 4767 | chan->num_conf_req++; |
| 4768 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4769 | } |
| 4770 | } |
| 4771 | |
| 4772 | static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id, |
| 4773 | u8 remote_amp_id) |
| 4774 | { |
| 4775 | l2cap_move_setup(chan); |
| 4776 | chan->move_id = local_amp_id; |
| 4777 | chan->move_state = L2CAP_MOVE_WAIT_RSP; |
| 4778 | |
| 4779 | l2cap_send_move_chan_req(chan, remote_amp_id); |
| 4780 | } |
| 4781 | |
| 4782 | static void l2cap_do_move_respond(struct l2cap_chan *chan, int result) |
| 4783 | { |
| 4784 | struct hci_chan *hchan = NULL; |
| 4785 | |
| 4786 | /* Placeholder - get hci_chan for logical link */ |
| 4787 | |
| 4788 | if (hchan) { |
| 4789 | if (hchan->state == BT_CONNECTED) { |
| 4790 | /* Logical link is ready to go */ |
| 4791 | chan->hs_hcon = hchan->conn; |
| 4792 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4793 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4794 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4795 | |
| 4796 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 4797 | } else { |
| 4798 | /* Wait for logical link to be ready */ |
| 4799 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4800 | } |
| 4801 | } else { |
| 4802 | /* Logical link not available */ |
| 4803 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED); |
| 4804 | } |
| 4805 | } |
| 4806 | |
| 4807 | static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result) |
| 4808 | { |
| 4809 | if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4810 | u8 rsp_result; |
| 4811 | if (result == -EINVAL) |
| 4812 | rsp_result = L2CAP_MR_BAD_ID; |
| 4813 | else |
| 4814 | rsp_result = L2CAP_MR_NOT_ALLOWED; |
| 4815 | |
| 4816 | l2cap_send_move_chan_rsp(chan, rsp_result); |
| 4817 | } |
| 4818 | |
| 4819 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 4820 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4821 | |
| 4822 | /* Restart data transmission */ |
| 4823 | l2cap_ertm_send(chan); |
| 4824 | } |
| 4825 | |
Andrei Emeltchenko | a514b17 | 2012-11-14 17:39:30 +0200 | [diff] [blame] | 4826 | /* Invoke with locked chan */ |
| 4827 | void __l2cap_physical_cfm(struct l2cap_chan *chan, int result) |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4828 | { |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4829 | u8 local_amp_id = chan->local_amp_id; |
Andrei Emeltchenko | fffadc0 | 2012-11-01 15:37:03 +0200 | [diff] [blame] | 4830 | u8 remote_amp_id = chan->remote_amp_id; |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4831 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4832 | BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d", |
| 4833 | chan, result, local_amp_id, remote_amp_id); |
| 4834 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4835 | if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) { |
| 4836 | l2cap_chan_unlock(chan); |
| 4837 | return; |
| 4838 | } |
| 4839 | |
| 4840 | if (chan->state != BT_CONNECTED) { |
| 4841 | l2cap_do_create(chan, result, local_amp_id, remote_amp_id); |
| 4842 | } else if (result != L2CAP_MR_SUCCESS) { |
| 4843 | l2cap_do_move_cancel(chan, result); |
| 4844 | } else { |
| 4845 | switch (chan->move_role) { |
| 4846 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4847 | l2cap_do_move_initiate(chan, local_amp_id, |
| 4848 | remote_amp_id); |
| 4849 | break; |
| 4850 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4851 | l2cap_do_move_respond(chan, result); |
| 4852 | break; |
| 4853 | default: |
| 4854 | l2cap_do_move_cancel(chan, result); |
| 4855 | break; |
| 4856 | } |
| 4857 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4858 | } |
| 4859 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4860 | static inline int l2cap_move_channel_req(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4861 | struct l2cap_cmd_hdr *cmd, |
| 4862 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4863 | { |
| 4864 | struct l2cap_move_chan_req *req = data; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4865 | struct l2cap_move_chan_rsp rsp; |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4866 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4867 | u16 icid = 0; |
| 4868 | u16 result = L2CAP_MR_NOT_ALLOWED; |
| 4869 | |
| 4870 | if (cmd_len != sizeof(*req)) |
| 4871 | return -EPROTO; |
| 4872 | |
| 4873 | icid = le16_to_cpu(req->icid); |
| 4874 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4875 | 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] | 4876 | |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 4877 | if (!conn->hs_enabled) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4878 | return -EINVAL; |
| 4879 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4880 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 4881 | if (!chan) { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4882 | rsp.icid = cpu_to_le16(icid); |
| 4883 | rsp.result = __constant_cpu_to_le16(L2CAP_MR_NOT_ALLOWED); |
| 4884 | l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP, |
| 4885 | sizeof(rsp), &rsp); |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4886 | return 0; |
| 4887 | } |
| 4888 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4889 | chan->ident = cmd->ident; |
| 4890 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4891 | if (chan->scid < L2CAP_CID_DYN_START || |
| 4892 | chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY || |
| 4893 | (chan->mode != L2CAP_MODE_ERTM && |
| 4894 | chan->mode != L2CAP_MODE_STREAMING)) { |
| 4895 | result = L2CAP_MR_NOT_ALLOWED; |
| 4896 | goto send_move_response; |
| 4897 | } |
| 4898 | |
| 4899 | if (chan->local_amp_id == req->dest_amp_id) { |
| 4900 | result = L2CAP_MR_SAME_ID; |
| 4901 | goto send_move_response; |
| 4902 | } |
| 4903 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4904 | if (req->dest_amp_id != AMP_ID_BREDR) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4905 | struct hci_dev *hdev; |
| 4906 | hdev = hci_dev_get(req->dest_amp_id); |
| 4907 | if (!hdev || hdev->dev_type != HCI_AMP || |
| 4908 | !test_bit(HCI_UP, &hdev->flags)) { |
| 4909 | if (hdev) |
| 4910 | hci_dev_put(hdev); |
| 4911 | |
| 4912 | result = L2CAP_MR_BAD_ID; |
| 4913 | goto send_move_response; |
| 4914 | } |
| 4915 | hci_dev_put(hdev); |
| 4916 | } |
| 4917 | |
| 4918 | /* Detect a move collision. Only send a collision response |
| 4919 | * if this side has "lost", otherwise proceed with the move. |
| 4920 | * The winner has the larger bd_addr. |
| 4921 | */ |
| 4922 | if ((__chan_is_moving(chan) || |
| 4923 | chan->move_role != L2CAP_MOVE_ROLE_NONE) && |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 4924 | bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4925 | result = L2CAP_MR_COLLISION; |
| 4926 | goto send_move_response; |
| 4927 | } |
| 4928 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4929 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 4930 | l2cap_move_setup(chan); |
| 4931 | chan->move_id = req->dest_amp_id; |
| 4932 | icid = chan->dcid; |
| 4933 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 4934 | if (req->dest_amp_id == AMP_ID_BREDR) { |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4935 | /* Moving to BR/EDR */ |
| 4936 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4937 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4938 | result = L2CAP_MR_PEND; |
| 4939 | } else { |
| 4940 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4941 | result = L2CAP_MR_SUCCESS; |
| 4942 | } |
| 4943 | } else { |
| 4944 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4945 | /* Placeholder - uncomment when amp functions are available */ |
| 4946 | /*amp_accept_physical(chan, req->dest_amp_id);*/ |
| 4947 | result = L2CAP_MR_PEND; |
| 4948 | } |
| 4949 | |
| 4950 | send_move_response: |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4951 | l2cap_send_move_chan_rsp(chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4952 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4953 | l2cap_chan_unlock(chan); |
| 4954 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4955 | return 0; |
| 4956 | } |
| 4957 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4958 | static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result) |
| 4959 | { |
| 4960 | struct l2cap_chan *chan; |
| 4961 | struct hci_chan *hchan = NULL; |
| 4962 | |
| 4963 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 4964 | if (!chan) { |
| 4965 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 4966 | return; |
| 4967 | } |
| 4968 | |
| 4969 | __clear_chan_timer(chan); |
| 4970 | if (result == L2CAP_MR_PEND) |
| 4971 | __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT); |
| 4972 | |
| 4973 | switch (chan->move_state) { |
| 4974 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 4975 | /* Move confirm will be sent when logical link |
| 4976 | * is complete. |
| 4977 | */ |
| 4978 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4979 | break; |
| 4980 | case L2CAP_MOVE_WAIT_RSP_SUCCESS: |
| 4981 | if (result == L2CAP_MR_PEND) { |
| 4982 | break; |
| 4983 | } else if (test_bit(CONN_LOCAL_BUSY, |
| 4984 | &chan->conn_state)) { |
| 4985 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4986 | } else { |
| 4987 | /* Logical link is up or moving to BR/EDR, |
| 4988 | * proceed with move |
| 4989 | */ |
| 4990 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 4991 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4992 | } |
| 4993 | break; |
| 4994 | case L2CAP_MOVE_WAIT_RSP: |
| 4995 | /* Moving to AMP */ |
| 4996 | if (result == L2CAP_MR_SUCCESS) { |
| 4997 | /* Remote is ready, send confirm immediately |
| 4998 | * after logical link is ready |
| 4999 | */ |
| 5000 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 5001 | } else { |
| 5002 | /* Both logical link and move success |
| 5003 | * are required to confirm |
| 5004 | */ |
| 5005 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP; |
| 5006 | } |
| 5007 | |
| 5008 | /* Placeholder - get hci_chan for logical link */ |
| 5009 | if (!hchan) { |
| 5010 | /* Logical link not available */ |
| 5011 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5012 | break; |
| 5013 | } |
| 5014 | |
| 5015 | /* If the logical link is not yet connected, do not |
| 5016 | * send confirmation. |
| 5017 | */ |
| 5018 | if (hchan->state != BT_CONNECTED) |
| 5019 | break; |
| 5020 | |
| 5021 | /* Logical link is already ready to go */ |
| 5022 | |
| 5023 | chan->hs_hcon = hchan->conn; |
| 5024 | chan->hs_hcon->l2cap_data = chan->conn; |
| 5025 | |
| 5026 | if (result == L2CAP_MR_SUCCESS) { |
| 5027 | /* Can confirm now */ |
| 5028 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 5029 | } else { |
| 5030 | /* Now only need move success |
| 5031 | * to confirm |
| 5032 | */ |
| 5033 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 5034 | } |
| 5035 | |
| 5036 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 5037 | break; |
| 5038 | default: |
| 5039 | /* Any other amp move state means the move failed. */ |
| 5040 | chan->move_id = chan->local_amp_id; |
| 5041 | l2cap_move_done(chan); |
| 5042 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5043 | } |
| 5044 | |
| 5045 | l2cap_chan_unlock(chan); |
| 5046 | } |
| 5047 | |
| 5048 | static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid, |
| 5049 | u16 result) |
| 5050 | { |
| 5051 | struct l2cap_chan *chan; |
| 5052 | |
| 5053 | chan = l2cap_get_chan_by_ident(conn, ident); |
| 5054 | if (!chan) { |
| 5055 | /* Could not locate channel, icid is best guess */ |
| 5056 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 5057 | return; |
| 5058 | } |
| 5059 | |
| 5060 | __clear_chan_timer(chan); |
| 5061 | |
| 5062 | if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 5063 | if (result == L2CAP_MR_COLLISION) { |
| 5064 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 5065 | } else { |
| 5066 | /* Cleanup - cancel move */ |
| 5067 | chan->move_id = chan->local_amp_id; |
| 5068 | l2cap_move_done(chan); |
| 5069 | } |
| 5070 | } |
| 5071 | |
| 5072 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5073 | |
| 5074 | l2cap_chan_unlock(chan); |
| 5075 | } |
| 5076 | |
| 5077 | static int l2cap_move_channel_rsp(struct l2cap_conn *conn, |
| 5078 | struct l2cap_cmd_hdr *cmd, |
| 5079 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5080 | { |
| 5081 | struct l2cap_move_chan_rsp *rsp = data; |
| 5082 | u16 icid, result; |
| 5083 | |
| 5084 | if (cmd_len != sizeof(*rsp)) |
| 5085 | return -EPROTO; |
| 5086 | |
| 5087 | icid = le16_to_cpu(rsp->icid); |
| 5088 | result = le16_to_cpu(rsp->result); |
| 5089 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5090 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5091 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 5092 | if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND) |
| 5093 | l2cap_move_continue(conn, icid, result); |
| 5094 | else |
| 5095 | l2cap_move_fail(conn, cmd->ident, icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5096 | |
| 5097 | return 0; |
| 5098 | } |
| 5099 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5100 | static int l2cap_move_channel_confirm(struct l2cap_conn *conn, |
| 5101 | struct l2cap_cmd_hdr *cmd, |
| 5102 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5103 | { |
| 5104 | struct l2cap_move_chan_cfm *cfm = data; |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5105 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5106 | u16 icid, result; |
| 5107 | |
| 5108 | if (cmd_len != sizeof(*cfm)) |
| 5109 | return -EPROTO; |
| 5110 | |
| 5111 | icid = le16_to_cpu(cfm->icid); |
| 5112 | result = le16_to_cpu(cfm->result); |
| 5113 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5114 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5115 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5116 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 5117 | if (!chan) { |
| 5118 | /* Spec requires a response even if the icid was not found */ |
| 5119 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5120 | return 0; |
| 5121 | } |
| 5122 | |
| 5123 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) { |
| 5124 | if (result == L2CAP_MC_CONFIRMED) { |
| 5125 | chan->local_amp_id = chan->move_id; |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 5126 | if (chan->local_amp_id == AMP_ID_BREDR) |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5127 | __release_logical_link(chan); |
| 5128 | } else { |
| 5129 | chan->move_id = chan->local_amp_id; |
| 5130 | } |
| 5131 | |
| 5132 | l2cap_move_done(chan); |
| 5133 | } |
| 5134 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5135 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5136 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5137 | l2cap_chan_unlock(chan); |
| 5138 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5139 | return 0; |
| 5140 | } |
| 5141 | |
| 5142 | static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5143 | struct l2cap_cmd_hdr *cmd, |
| 5144 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5145 | { |
| 5146 | struct l2cap_move_chan_cfm_rsp *rsp = data; |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5147 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5148 | u16 icid; |
| 5149 | |
| 5150 | if (cmd_len != sizeof(*rsp)) |
| 5151 | return -EPROTO; |
| 5152 | |
| 5153 | icid = le16_to_cpu(rsp->icid); |
| 5154 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5155 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5156 | |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5157 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 5158 | if (!chan) |
| 5159 | return 0; |
| 5160 | |
| 5161 | __clear_chan_timer(chan); |
| 5162 | |
| 5163 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) { |
| 5164 | chan->local_amp_id = chan->move_id; |
| 5165 | |
Marcel Holtmann | 6ed971c | 2013-10-05 11:47:44 -0700 | [diff] [blame] | 5166 | if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan) |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5167 | __release_logical_link(chan); |
| 5168 | |
| 5169 | l2cap_move_done(chan); |
| 5170 | } |
| 5171 | |
| 5172 | l2cap_chan_unlock(chan); |
| 5173 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5174 | return 0; |
| 5175 | } |
| 5176 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 5177 | 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] | 5178 | u16 to_multiplier) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5179 | { |
| 5180 | u16 max_latency; |
| 5181 | |
| 5182 | if (min > max || min < 6 || max > 3200) |
| 5183 | return -EINVAL; |
| 5184 | |
| 5185 | if (to_multiplier < 10 || to_multiplier > 3200) |
| 5186 | return -EINVAL; |
| 5187 | |
| 5188 | if (max >= to_multiplier * 8) |
| 5189 | return -EINVAL; |
| 5190 | |
| 5191 | max_latency = (to_multiplier * 8 / max) - 1; |
| 5192 | if (latency > 499 || latency > max_latency) |
| 5193 | return -EINVAL; |
| 5194 | |
| 5195 | return 0; |
| 5196 | } |
| 5197 | |
| 5198 | static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5199 | struct l2cap_cmd_hdr *cmd, |
| 5200 | u8 *data) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5201 | { |
| 5202 | struct hci_conn *hcon = conn->hcon; |
| 5203 | struct l2cap_conn_param_update_req *req; |
| 5204 | struct l2cap_conn_param_update_rsp rsp; |
| 5205 | u16 min, max, latency, to_multiplier, cmd_len; |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5206 | int err; |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5207 | |
| 5208 | if (!(hcon->link_mode & HCI_LM_MASTER)) |
| 5209 | return -EINVAL; |
| 5210 | |
| 5211 | cmd_len = __le16_to_cpu(cmd->len); |
| 5212 | if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) |
| 5213 | return -EPROTO; |
| 5214 | |
| 5215 | req = (struct l2cap_conn_param_update_req *) data; |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 5216 | min = __le16_to_cpu(req->min); |
| 5217 | max = __le16_to_cpu(req->max); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5218 | latency = __le16_to_cpu(req->latency); |
| 5219 | to_multiplier = __le16_to_cpu(req->to_multiplier); |
| 5220 | |
| 5221 | 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] | 5222 | min, max, latency, to_multiplier); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5223 | |
| 5224 | memset(&rsp, 0, sizeof(rsp)); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5225 | |
| 5226 | err = l2cap_check_conn_param(min, max, latency, to_multiplier); |
| 5227 | if (err) |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 5228 | rsp.result = __constant_cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5229 | else |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 5230 | rsp.result = __constant_cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5231 | |
| 5232 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5233 | sizeof(rsp), &rsp); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5234 | |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5235 | if (!err) |
| 5236 | hci_le_conn_update(hcon, min, max, latency, to_multiplier); |
| 5237 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5238 | return 0; |
| 5239 | } |
| 5240 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5241 | static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5242 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5243 | u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5244 | { |
| 5245 | int err = 0; |
| 5246 | |
| 5247 | switch (cmd->code) { |
| 5248 | case L2CAP_COMMAND_REJ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5249 | l2cap_command_rej(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5250 | break; |
| 5251 | |
| 5252 | case L2CAP_CONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5253 | err = l2cap_connect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5254 | break; |
| 5255 | |
| 5256 | case L2CAP_CONN_RSP: |
Mat Martineau | f5a2598 | 2012-10-11 17:48:21 +0300 | [diff] [blame] | 5257 | case L2CAP_CREATE_CHAN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5258 | l2cap_connect_create_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5259 | break; |
| 5260 | |
| 5261 | case L2CAP_CONF_REQ: |
| 5262 | err = l2cap_config_req(conn, cmd, cmd_len, data); |
| 5263 | break; |
| 5264 | |
| 5265 | case L2CAP_CONF_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5266 | l2cap_config_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5267 | break; |
| 5268 | |
| 5269 | case L2CAP_DISCONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5270 | err = l2cap_disconnect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5271 | break; |
| 5272 | |
| 5273 | case L2CAP_DISCONN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5274 | l2cap_disconnect_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5275 | break; |
| 5276 | |
| 5277 | case L2CAP_ECHO_REQ: |
| 5278 | l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); |
| 5279 | break; |
| 5280 | |
| 5281 | case L2CAP_ECHO_RSP: |
| 5282 | break; |
| 5283 | |
| 5284 | case L2CAP_INFO_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5285 | err = l2cap_information_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5286 | break; |
| 5287 | |
| 5288 | case L2CAP_INFO_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5289 | l2cap_information_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5290 | break; |
| 5291 | |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 5292 | case L2CAP_CREATE_CHAN_REQ: |
| 5293 | err = l2cap_create_channel_req(conn, cmd, cmd_len, data); |
| 5294 | break; |
| 5295 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5296 | case L2CAP_MOVE_CHAN_REQ: |
| 5297 | err = l2cap_move_channel_req(conn, cmd, cmd_len, data); |
| 5298 | break; |
| 5299 | |
| 5300 | case L2CAP_MOVE_CHAN_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5301 | l2cap_move_channel_rsp(conn, cmd, cmd_len, data); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5302 | break; |
| 5303 | |
| 5304 | case L2CAP_MOVE_CHAN_CFM: |
| 5305 | err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); |
| 5306 | break; |
| 5307 | |
| 5308 | case L2CAP_MOVE_CHAN_CFM_RSP: |
Johan Hedberg | 9245e73 | 2013-09-16 13:05:17 +0300 | [diff] [blame] | 5309 | l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5310 | break; |
| 5311 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5312 | default: |
| 5313 | BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); |
| 5314 | err = -EINVAL; |
| 5315 | break; |
| 5316 | } |
| 5317 | |
| 5318 | return err; |
| 5319 | } |
| 5320 | |
| 5321 | static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5322 | struct l2cap_cmd_hdr *cmd, u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5323 | { |
| 5324 | switch (cmd->code) { |
| 5325 | case L2CAP_COMMAND_REJ: |
| 5326 | return 0; |
| 5327 | |
| 5328 | case L2CAP_CONN_PARAM_UPDATE_REQ: |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5329 | return l2cap_conn_param_update_req(conn, cmd, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5330 | |
| 5331 | case L2CAP_CONN_PARAM_UPDATE_RSP: |
| 5332 | return 0; |
| 5333 | |
| 5334 | default: |
| 5335 | BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); |
| 5336 | return -EINVAL; |
| 5337 | } |
| 5338 | } |
| 5339 | |
Johan Hedberg | 7c2005d | 2013-09-16 13:05:16 +0300 | [diff] [blame] | 5340 | static __le16 l2cap_err_to_reason(int err) |
| 5341 | { |
| 5342 | switch (err) { |
| 5343 | case -EBADSLT: |
| 5344 | return __constant_cpu_to_le16(L2CAP_REJ_INVALID_CID); |
| 5345 | case -EMSGSIZE: |
| 5346 | return __constant_cpu_to_le16(L2CAP_REJ_MTU_EXCEEDED); |
| 5347 | case -EINVAL: |
| 5348 | case -EPROTO: |
| 5349 | default: |
| 5350 | return __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
| 5351 | } |
| 5352 | } |
| 5353 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5354 | static inline void l2cap_le_sig_channel(struct l2cap_conn *conn, |
| 5355 | struct sk_buff *skb) |
| 5356 | { |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5357 | struct hci_conn *hcon = conn->hcon; |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5358 | struct l2cap_cmd_hdr *cmd; |
| 5359 | u16 len; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5360 | int err; |
| 5361 | |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5362 | if (hcon->type != LE_LINK) |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5363 | goto drop; |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5364 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5365 | if (skb->len < L2CAP_CMD_HDR_SIZE) |
| 5366 | goto drop; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5367 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5368 | cmd = (void *) skb->data; |
| 5369 | skb_pull(skb, L2CAP_CMD_HDR_SIZE); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5370 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5371 | len = le16_to_cpu(cmd->len); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5372 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5373 | 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] | 5374 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5375 | if (len != skb->len || !cmd->ident) { |
| 5376 | BT_DBG("corrupted command"); |
| 5377 | goto drop; |
| 5378 | } |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5379 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5380 | err = l2cap_le_sig_cmd(conn, cmd, skb->data); |
| 5381 | if (err) { |
| 5382 | struct l2cap_cmd_rej_unk rej; |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5383 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5384 | BT_ERR("Wrong link type (%d)", err); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5385 | |
Marcel Holtmann | 4f3e219 | 2013-10-03 01:26:37 -0700 | [diff] [blame] | 5386 | rej.reason = l2cap_err_to_reason(err); |
| 5387 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
| 5388 | sizeof(rej), &rej); |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5389 | } |
| 5390 | |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5391 | drop: |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5392 | kfree_skb(skb); |
| 5393 | } |
| 5394 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5395 | static inline void l2cap_sig_channel(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5396 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | { |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5398 | struct hci_conn *hcon = conn->hcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | u8 *data = skb->data; |
| 5400 | int len = skb->len; |
| 5401 | struct l2cap_cmd_hdr cmd; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5402 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5403 | |
| 5404 | l2cap_raw_recv(conn, skb); |
| 5405 | |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5406 | if (hcon->type != ACL_LINK) |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5407 | goto drop; |
Johan Hedberg | 69c4e4e | 2013-09-16 13:05:18 +0300 | [diff] [blame] | 5408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5409 | while (len >= L2CAP_CMD_HDR_SIZE) { |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5410 | u16 cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 5412 | data += L2CAP_CMD_HDR_SIZE; |
| 5413 | len -= L2CAP_CMD_HDR_SIZE; |
| 5414 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5415 | cmd_len = le16_to_cpu(cmd.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5416 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5417 | BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, |
| 5418 | cmd.ident); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5420 | if (cmd_len > len || !cmd.ident) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | BT_DBG("corrupted command"); |
| 5422 | break; |
| 5423 | } |
| 5424 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5425 | err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | if (err) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 5427 | struct l2cap_cmd_rej_unk rej; |
Gustavo F. Padovan | 2c6d1a2 | 2011-03-23 14:38:32 -0300 | [diff] [blame] | 5428 | |
| 5429 | BT_ERR("Wrong link type (%d)", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5430 | |
Johan Hedberg | 7c2005d | 2013-09-16 13:05:16 +0300 | [diff] [blame] | 5431 | rej.reason = l2cap_err_to_reason(err); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5432 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, |
| 5433 | sizeof(rej), &rej); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | } |
| 5435 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5436 | data += cmd_len; |
| 5437 | len -= cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5438 | } |
| 5439 | |
Marcel Holtmann | 3b16629 | 2013-10-02 08:28:21 -0700 | [diff] [blame] | 5440 | drop: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5441 | kfree_skb(skb); |
| 5442 | } |
| 5443 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5444 | 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] | 5445 | { |
| 5446 | u16 our_fcs, rcv_fcs; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 5447 | int hdr_size; |
| 5448 | |
| 5449 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 5450 | hdr_size = L2CAP_EXT_HDR_SIZE; |
| 5451 | else |
| 5452 | hdr_size = L2CAP_ENH_HDR_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5453 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5454 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5455 | skb_trim(skb, skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5456 | rcv_fcs = get_unaligned_le16(skb->data + skb->len); |
| 5457 | our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); |
| 5458 | |
| 5459 | if (our_fcs != rcv_fcs) |
João Paulo Rechi Vita | 7a560e5 | 2010-06-22 13:56:27 -0300 | [diff] [blame] | 5460 | return -EBADMSG; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5461 | } |
| 5462 | return 0; |
| 5463 | } |
| 5464 | |
Mat Martineau | 6ea0048 | 2012-05-17 20:53:52 -0700 | [diff] [blame] | 5465 | 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] | 5466 | { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5467 | struct l2cap_ctrl control; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5468 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5469 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5470 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5471 | memset(&control, 0, sizeof(control)); |
| 5472 | control.sframe = 1; |
| 5473 | control.final = 1; |
| 5474 | control.reqseq = chan->buffer_seq; |
| 5475 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5476 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5477 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5478 | control.super = L2CAP_SUPER_RNR; |
| 5479 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5480 | } |
| 5481 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5482 | if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
| 5483 | chan->unacked_frames > 0) |
| 5484 | __set_retrans_timer(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5485 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5486 | /* Send pending iframes */ |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 5487 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5488 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5489 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5490 | test_bit(CONN_SEND_FBIT, &chan->conn_state)) { |
| 5491 | /* F-bit wasn't sent in an s-frame or i-frame yet, so |
| 5492 | * send it now. |
| 5493 | */ |
| 5494 | control.super = L2CAP_SUPER_RR; |
| 5495 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5496 | } |
| 5497 | } |
| 5498 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5499 | static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag, |
| 5500 | struct sk_buff **last_frag) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5501 | { |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5502 | /* skb->len reflects data in skb as well as all fragments |
| 5503 | * skb->data_len reflects only data in fragments |
| 5504 | */ |
| 5505 | if (!skb_has_frag_list(skb)) |
| 5506 | skb_shinfo(skb)->frag_list = new_frag; |
| 5507 | |
| 5508 | new_frag->next = NULL; |
| 5509 | |
| 5510 | (*last_frag)->next = new_frag; |
| 5511 | *last_frag = new_frag; |
| 5512 | |
| 5513 | skb->len += new_frag->len; |
| 5514 | skb->data_len += new_frag->len; |
| 5515 | skb->truesize += new_frag->truesize; |
| 5516 | } |
| 5517 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5518 | static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, |
| 5519 | struct l2cap_ctrl *control) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5520 | { |
| 5521 | int err = -EINVAL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5522 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5523 | switch (control->sar) { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5524 | case L2CAP_SAR_UNSEGMENTED: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5525 | if (chan->sdu) |
| 5526 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5527 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5528 | err = chan->ops->recv(chan, skb); |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5529 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5530 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5531 | case L2CAP_SAR_START: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5532 | if (chan->sdu) |
| 5533 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5534 | |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5535 | chan->sdu_len = get_unaligned_le16(skb->data); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5536 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5537 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5538 | if (chan->sdu_len > chan->imtu) { |
| 5539 | err = -EMSGSIZE; |
| 5540 | break; |
| 5541 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5542 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5543 | if (skb->len >= chan->sdu_len) |
| 5544 | break; |
| 5545 | |
| 5546 | chan->sdu = skb; |
| 5547 | chan->sdu_last_frag = skb; |
| 5548 | |
| 5549 | skb = NULL; |
| 5550 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5551 | break; |
| 5552 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5553 | case L2CAP_SAR_CONTINUE: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5554 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5555 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5556 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5557 | append_skb_frag(chan->sdu, skb, |
| 5558 | &chan->sdu_last_frag); |
| 5559 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5560 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5561 | if (chan->sdu->len >= chan->sdu_len) |
| 5562 | break; |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5563 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5564 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5565 | break; |
| 5566 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5567 | case L2CAP_SAR_END: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5568 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5569 | break; |
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 | append_skb_frag(chan->sdu, skb, |
| 5572 | &chan->sdu_last_frag); |
| 5573 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5574 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5575 | if (chan->sdu->len != chan->sdu_len) |
| 5576 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5577 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5578 | err = chan->ops->recv(chan, chan->sdu); |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5579 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5580 | if (!err) { |
| 5581 | /* Reassembly complete */ |
| 5582 | chan->sdu = NULL; |
| 5583 | chan->sdu_last_frag = NULL; |
| 5584 | chan->sdu_len = 0; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5585 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5586 | break; |
| 5587 | } |
| 5588 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5589 | if (err) { |
| 5590 | kfree_skb(skb); |
| 5591 | kfree_skb(chan->sdu); |
| 5592 | chan->sdu = NULL; |
| 5593 | chan->sdu_last_frag = NULL; |
| 5594 | chan->sdu_len = 0; |
| 5595 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5596 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5597 | return err; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5598 | } |
| 5599 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 5600 | static int l2cap_resegment(struct l2cap_chan *chan) |
| 5601 | { |
| 5602 | /* Placeholder */ |
| 5603 | return 0; |
| 5604 | } |
| 5605 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 5606 | void l2cap_chan_busy(struct l2cap_chan *chan, int busy) |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 5607 | { |
Mat Martineau | 61aa4f5 | 2012-05-17 20:53:40 -0700 | [diff] [blame] | 5608 | u8 event; |
| 5609 | |
| 5610 | if (chan->mode != L2CAP_MODE_ERTM) |
| 5611 | return; |
| 5612 | |
| 5613 | event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR; |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 5614 | l2cap_tx(chan, NULL, NULL, event); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5615 | } |
| 5616 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5617 | static int l2cap_rx_queued_iframes(struct l2cap_chan *chan) |
| 5618 | { |
Mat Martineau | 6383872 | 2012-05-17 20:53:45 -0700 | [diff] [blame] | 5619 | int err = 0; |
| 5620 | /* Pass sequential frames to l2cap_reassemble_sdu() |
| 5621 | * until a gap is encountered. |
| 5622 | */ |
| 5623 | |
| 5624 | BT_DBG("chan %p", chan); |
| 5625 | |
| 5626 | while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5627 | struct sk_buff *skb; |
| 5628 | BT_DBG("Searching for skb with txseq %d (queue len %d)", |
| 5629 | chan->buffer_seq, skb_queue_len(&chan->srej_q)); |
| 5630 | |
| 5631 | skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq); |
| 5632 | |
| 5633 | if (!skb) |
| 5634 | break; |
| 5635 | |
| 5636 | skb_unlink(skb, &chan->srej_q); |
| 5637 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 5638 | err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->control); |
| 5639 | if (err) |
| 5640 | break; |
| 5641 | } |
| 5642 | |
| 5643 | if (skb_queue_empty(&chan->srej_q)) { |
| 5644 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 5645 | l2cap_send_ack(chan); |
| 5646 | } |
| 5647 | |
| 5648 | return err; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5649 | } |
| 5650 | |
| 5651 | static void l2cap_handle_srej(struct l2cap_chan *chan, |
| 5652 | struct l2cap_ctrl *control) |
| 5653 | { |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5654 | struct sk_buff *skb; |
| 5655 | |
| 5656 | BT_DBG("chan %p, control %p", chan, control); |
| 5657 | |
| 5658 | if (control->reqseq == chan->next_tx_seq) { |
| 5659 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5660 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5661 | return; |
| 5662 | } |
| 5663 | |
| 5664 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 5665 | |
| 5666 | if (skb == NULL) { |
| 5667 | BT_DBG("Seq %d not available for retransmission", |
| 5668 | control->reqseq); |
| 5669 | return; |
| 5670 | } |
| 5671 | |
| 5672 | if (chan->max_tx != 0 && bt_cb(skb)->control.retries >= chan->max_tx) { |
| 5673 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5674 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5675 | return; |
| 5676 | } |
| 5677 | |
| 5678 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5679 | |
| 5680 | if (control->poll) { |
| 5681 | l2cap_pass_to_tx(chan, control); |
| 5682 | |
| 5683 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 5684 | l2cap_retransmit(chan, control); |
| 5685 | l2cap_ertm_send(chan); |
| 5686 | |
| 5687 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 5688 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5689 | chan->srej_save_reqseq = control->reqseq; |
| 5690 | } |
| 5691 | } else { |
| 5692 | l2cap_pass_to_tx_fbit(chan, control); |
| 5693 | |
| 5694 | if (control->final) { |
| 5695 | if (chan->srej_save_reqseq != control->reqseq || |
| 5696 | !test_and_clear_bit(CONN_SREJ_ACT, |
| 5697 | &chan->conn_state)) |
| 5698 | l2cap_retransmit(chan, control); |
| 5699 | } else { |
| 5700 | l2cap_retransmit(chan, control); |
| 5701 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 5702 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5703 | chan->srej_save_reqseq = control->reqseq; |
| 5704 | } |
| 5705 | } |
| 5706 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5707 | } |
| 5708 | |
| 5709 | static void l2cap_handle_rej(struct l2cap_chan *chan, |
| 5710 | struct l2cap_ctrl *control) |
| 5711 | { |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5712 | struct sk_buff *skb; |
| 5713 | |
| 5714 | BT_DBG("chan %p, control %p", chan, control); |
| 5715 | |
| 5716 | if (control->reqseq == chan->next_tx_seq) { |
| 5717 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5718 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5719 | return; |
| 5720 | } |
| 5721 | |
| 5722 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 5723 | |
| 5724 | if (chan->max_tx && skb && |
| 5725 | bt_cb(skb)->control.retries >= chan->max_tx) { |
| 5726 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5727 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5728 | return; |
| 5729 | } |
| 5730 | |
| 5731 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5732 | |
| 5733 | l2cap_pass_to_tx(chan, control); |
| 5734 | |
| 5735 | if (control->final) { |
| 5736 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
| 5737 | l2cap_retransmit_all(chan, control); |
| 5738 | } else { |
| 5739 | l2cap_retransmit_all(chan, control); |
| 5740 | l2cap_ertm_send(chan); |
| 5741 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) |
| 5742 | set_bit(CONN_REJ_ACT, &chan->conn_state); |
| 5743 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5744 | } |
| 5745 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5746 | static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq) |
| 5747 | { |
| 5748 | BT_DBG("chan %p, txseq %d", chan, txseq); |
| 5749 | |
| 5750 | BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq, |
| 5751 | chan->expected_tx_seq); |
| 5752 | |
| 5753 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 5754 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5755 | chan->tx_win) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5756 | /* See notes below regarding "double poll" and |
| 5757 | * invalid packets. |
| 5758 | */ |
| 5759 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 5760 | BT_DBG("Invalid/Ignore - after SREJ"); |
| 5761 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 5762 | } else { |
| 5763 | BT_DBG("Invalid - in window after SREJ sent"); |
| 5764 | return L2CAP_TXSEQ_INVALID; |
| 5765 | } |
| 5766 | } |
| 5767 | |
| 5768 | if (chan->srej_list.head == txseq) { |
| 5769 | BT_DBG("Expected SREJ"); |
| 5770 | return L2CAP_TXSEQ_EXPECTED_SREJ; |
| 5771 | } |
| 5772 | |
| 5773 | if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) { |
| 5774 | BT_DBG("Duplicate SREJ - txseq already stored"); |
| 5775 | return L2CAP_TXSEQ_DUPLICATE_SREJ; |
| 5776 | } |
| 5777 | |
| 5778 | if (l2cap_seq_list_contains(&chan->srej_list, txseq)) { |
| 5779 | BT_DBG("Unexpected SREJ - not requested"); |
| 5780 | return L2CAP_TXSEQ_UNEXPECTED_SREJ; |
| 5781 | } |
| 5782 | } |
| 5783 | |
| 5784 | if (chan->expected_tx_seq == txseq) { |
| 5785 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
| 5786 | chan->tx_win) { |
| 5787 | BT_DBG("Invalid - txseq outside tx window"); |
| 5788 | return L2CAP_TXSEQ_INVALID; |
| 5789 | } else { |
| 5790 | BT_DBG("Expected"); |
| 5791 | return L2CAP_TXSEQ_EXPECTED; |
| 5792 | } |
| 5793 | } |
| 5794 | |
| 5795 | if (__seq_offset(chan, txseq, chan->last_acked_seq) < |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5796 | __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5797 | BT_DBG("Duplicate - expected_tx_seq later than txseq"); |
| 5798 | return L2CAP_TXSEQ_DUPLICATE; |
| 5799 | } |
| 5800 | |
| 5801 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) { |
| 5802 | /* A source of invalid packets is a "double poll" condition, |
| 5803 | * where delays cause us to send multiple poll packets. If |
| 5804 | * the remote stack receives and processes both polls, |
| 5805 | * sequence numbers can wrap around in such a way that a |
| 5806 | * resent frame has a sequence number that looks like new data |
| 5807 | * with a sequence gap. This would trigger an erroneous SREJ |
| 5808 | * request. |
| 5809 | * |
| 5810 | * Fortunately, this is impossible with a tx window that's |
| 5811 | * less than half of the maximum sequence number, which allows |
| 5812 | * invalid frames to be safely ignored. |
| 5813 | * |
| 5814 | * With tx window sizes greater than half of the tx window |
| 5815 | * maximum, the frame is invalid and cannot be ignored. This |
| 5816 | * causes a disconnect. |
| 5817 | */ |
| 5818 | |
| 5819 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 5820 | BT_DBG("Invalid/Ignore - txseq outside tx window"); |
| 5821 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 5822 | } else { |
| 5823 | BT_DBG("Invalid - txseq outside tx window"); |
| 5824 | return L2CAP_TXSEQ_INVALID; |
| 5825 | } |
| 5826 | } else { |
| 5827 | BT_DBG("Unexpected - txseq indicates missing frames"); |
| 5828 | return L2CAP_TXSEQ_UNEXPECTED; |
| 5829 | } |
| 5830 | } |
| 5831 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5832 | static int l2cap_rx_state_recv(struct l2cap_chan *chan, |
| 5833 | struct l2cap_ctrl *control, |
| 5834 | struct sk_buff *skb, u8 event) |
| 5835 | { |
| 5836 | int err = 0; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 5837 | bool skb_in_use = false; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5838 | |
| 5839 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 5840 | event); |
| 5841 | |
| 5842 | switch (event) { |
| 5843 | case L2CAP_EV_RECV_IFRAME: |
| 5844 | switch (l2cap_classify_txseq(chan, control->txseq)) { |
| 5845 | case L2CAP_TXSEQ_EXPECTED: |
| 5846 | l2cap_pass_to_tx(chan, control); |
| 5847 | |
| 5848 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5849 | BT_DBG("Busy, discarding expected seq %d", |
| 5850 | control->txseq); |
| 5851 | break; |
| 5852 | } |
| 5853 | |
| 5854 | chan->expected_tx_seq = __next_seq(chan, |
| 5855 | control->txseq); |
| 5856 | |
| 5857 | chan->buffer_seq = chan->expected_tx_seq; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 5858 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5859 | |
| 5860 | err = l2cap_reassemble_sdu(chan, skb, control); |
| 5861 | if (err) |
| 5862 | break; |
| 5863 | |
| 5864 | if (control->final) { |
| 5865 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 5866 | &chan->conn_state)) { |
| 5867 | control->final = 0; |
| 5868 | l2cap_retransmit_all(chan, control); |
| 5869 | l2cap_ertm_send(chan); |
| 5870 | } |
| 5871 | } |
| 5872 | |
| 5873 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 5874 | l2cap_send_ack(chan); |
| 5875 | break; |
| 5876 | case L2CAP_TXSEQ_UNEXPECTED: |
| 5877 | l2cap_pass_to_tx(chan, control); |
| 5878 | |
| 5879 | /* Can't issue SREJ frames in the local busy state. |
| 5880 | * Drop this frame, it will be seen as missing |
| 5881 | * when local busy is exited. |
| 5882 | */ |
| 5883 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5884 | BT_DBG("Busy, discarding unexpected seq %d", |
| 5885 | control->txseq); |
| 5886 | break; |
| 5887 | } |
| 5888 | |
| 5889 | /* There was a gap in the sequence, so an SREJ |
| 5890 | * must be sent for each missing frame. The |
| 5891 | * current frame is stored for later use. |
| 5892 | */ |
| 5893 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 5894 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5895 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5896 | skb_queue_len(&chan->srej_q)); |
| 5897 | |
| 5898 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5899 | l2cap_seq_list_clear(&chan->srej_list); |
| 5900 | l2cap_send_srej(chan, control->txseq); |
| 5901 | |
| 5902 | chan->rx_state = L2CAP_RX_STATE_SREJ_SENT; |
| 5903 | break; |
| 5904 | case L2CAP_TXSEQ_DUPLICATE: |
| 5905 | l2cap_pass_to_tx(chan, control); |
| 5906 | break; |
| 5907 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 5908 | break; |
| 5909 | case L2CAP_TXSEQ_INVALID: |
| 5910 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5911 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5912 | break; |
| 5913 | } |
| 5914 | break; |
| 5915 | case L2CAP_EV_RECV_RR: |
| 5916 | l2cap_pass_to_tx(chan, control); |
| 5917 | if (control->final) { |
| 5918 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5919 | |
Mat Martineau | e6a3ee6 | 2012-10-23 15:24:22 -0700 | [diff] [blame] | 5920 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) && |
| 5921 | !__chan_is_moving(chan)) { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5922 | control->final = 0; |
| 5923 | l2cap_retransmit_all(chan, control); |
| 5924 | } |
| 5925 | |
| 5926 | l2cap_ertm_send(chan); |
| 5927 | } else if (control->poll) { |
| 5928 | l2cap_send_i_or_rr_or_rnr(chan); |
| 5929 | } else { |
| 5930 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 5931 | &chan->conn_state) && |
| 5932 | chan->unacked_frames) |
| 5933 | __set_retrans_timer(chan); |
| 5934 | |
| 5935 | l2cap_ertm_send(chan); |
| 5936 | } |
| 5937 | break; |
| 5938 | case L2CAP_EV_RECV_RNR: |
| 5939 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5940 | l2cap_pass_to_tx(chan, control); |
| 5941 | if (control && control->poll) { |
| 5942 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 5943 | l2cap_send_rr_or_rnr(chan, 0); |
| 5944 | } |
| 5945 | __clear_retrans_timer(chan); |
| 5946 | l2cap_seq_list_clear(&chan->retrans_list); |
| 5947 | break; |
| 5948 | case L2CAP_EV_RECV_REJ: |
| 5949 | l2cap_handle_rej(chan, control); |
| 5950 | break; |
| 5951 | case L2CAP_EV_RECV_SREJ: |
| 5952 | l2cap_handle_srej(chan, control); |
| 5953 | break; |
| 5954 | default: |
| 5955 | break; |
| 5956 | } |
| 5957 | |
| 5958 | if (skb && !skb_in_use) { |
| 5959 | BT_DBG("Freeing %p", skb); |
| 5960 | kfree_skb(skb); |
| 5961 | } |
| 5962 | |
| 5963 | return err; |
| 5964 | } |
| 5965 | |
| 5966 | static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan, |
| 5967 | struct l2cap_ctrl *control, |
| 5968 | struct sk_buff *skb, u8 event) |
| 5969 | { |
| 5970 | int err = 0; |
| 5971 | u16 txseq = control->txseq; |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 5972 | bool skb_in_use = false; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5973 | |
| 5974 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 5975 | event); |
| 5976 | |
| 5977 | switch (event) { |
| 5978 | case L2CAP_EV_RECV_IFRAME: |
| 5979 | switch (l2cap_classify_txseq(chan, txseq)) { |
| 5980 | case L2CAP_TXSEQ_EXPECTED: |
| 5981 | /* Keep frame for reassembly later */ |
| 5982 | l2cap_pass_to_tx(chan, control); |
| 5983 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 5984 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5985 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5986 | skb_queue_len(&chan->srej_q)); |
| 5987 | |
| 5988 | chan->expected_tx_seq = __next_seq(chan, txseq); |
| 5989 | break; |
| 5990 | case L2CAP_TXSEQ_EXPECTED_SREJ: |
| 5991 | l2cap_seq_list_pop(&chan->srej_list); |
| 5992 | |
| 5993 | l2cap_pass_to_tx(chan, control); |
| 5994 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 5995 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5996 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5997 | skb_queue_len(&chan->srej_q)); |
| 5998 | |
| 5999 | err = l2cap_rx_queued_iframes(chan); |
| 6000 | if (err) |
| 6001 | break; |
| 6002 | |
| 6003 | break; |
| 6004 | case L2CAP_TXSEQ_UNEXPECTED: |
| 6005 | /* Got a frame that can't be reassembled yet. |
| 6006 | * Save it for later, and send SREJs to cover |
| 6007 | * the missing frames. |
| 6008 | */ |
| 6009 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6010 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6011 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6012 | skb_queue_len(&chan->srej_q)); |
| 6013 | |
| 6014 | l2cap_pass_to_tx(chan, control); |
| 6015 | l2cap_send_srej(chan, control->txseq); |
| 6016 | break; |
| 6017 | case L2CAP_TXSEQ_UNEXPECTED_SREJ: |
| 6018 | /* This frame was requested with an SREJ, but |
| 6019 | * some expected retransmitted frames are |
| 6020 | * missing. Request retransmission of missing |
| 6021 | * SREJ'd frames. |
| 6022 | */ |
| 6023 | skb_queue_tail(&chan->srej_q, skb); |
Peter Senna Tschudin | 941247f | 2013-09-22 20:44:10 +0200 | [diff] [blame] | 6024 | skb_in_use = true; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6025 | BT_DBG("Queued %p (queue len %d)", skb, |
| 6026 | skb_queue_len(&chan->srej_q)); |
| 6027 | |
| 6028 | l2cap_pass_to_tx(chan, control); |
| 6029 | l2cap_send_srej_list(chan, control->txseq); |
| 6030 | break; |
| 6031 | case L2CAP_TXSEQ_DUPLICATE_SREJ: |
| 6032 | /* We've already queued this frame. Drop this copy. */ |
| 6033 | l2cap_pass_to_tx(chan, control); |
| 6034 | break; |
| 6035 | case L2CAP_TXSEQ_DUPLICATE: |
| 6036 | /* Expecting a later sequence number, so this frame |
| 6037 | * was already received. Ignore it completely. |
| 6038 | */ |
| 6039 | break; |
| 6040 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 6041 | break; |
| 6042 | case L2CAP_TXSEQ_INVALID: |
| 6043 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6044 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6045 | break; |
| 6046 | } |
| 6047 | break; |
| 6048 | case L2CAP_EV_RECV_RR: |
| 6049 | l2cap_pass_to_tx(chan, control); |
| 6050 | if (control->final) { |
| 6051 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6052 | |
| 6053 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 6054 | &chan->conn_state)) { |
| 6055 | control->final = 0; |
| 6056 | l2cap_retransmit_all(chan, control); |
| 6057 | } |
| 6058 | |
| 6059 | l2cap_ertm_send(chan); |
| 6060 | } else if (control->poll) { |
| 6061 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6062 | &chan->conn_state) && |
| 6063 | chan->unacked_frames) { |
| 6064 | __set_retrans_timer(chan); |
| 6065 | } |
| 6066 | |
| 6067 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6068 | l2cap_send_srej_tail(chan); |
| 6069 | } else { |
| 6070 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6071 | &chan->conn_state) && |
| 6072 | chan->unacked_frames) |
| 6073 | __set_retrans_timer(chan); |
| 6074 | |
| 6075 | l2cap_send_ack(chan); |
| 6076 | } |
| 6077 | break; |
| 6078 | case L2CAP_EV_RECV_RNR: |
| 6079 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6080 | l2cap_pass_to_tx(chan, control); |
| 6081 | if (control->poll) { |
| 6082 | l2cap_send_srej_tail(chan); |
| 6083 | } else { |
| 6084 | struct l2cap_ctrl rr_control; |
| 6085 | memset(&rr_control, 0, sizeof(rr_control)); |
| 6086 | rr_control.sframe = 1; |
| 6087 | rr_control.super = L2CAP_SUPER_RR; |
| 6088 | rr_control.reqseq = chan->buffer_seq; |
| 6089 | l2cap_send_sframe(chan, &rr_control); |
| 6090 | } |
| 6091 | |
| 6092 | break; |
| 6093 | case L2CAP_EV_RECV_REJ: |
| 6094 | l2cap_handle_rej(chan, control); |
| 6095 | break; |
| 6096 | case L2CAP_EV_RECV_SREJ: |
| 6097 | l2cap_handle_srej(chan, control); |
| 6098 | break; |
| 6099 | } |
| 6100 | |
| 6101 | if (skb && !skb_in_use) { |
| 6102 | BT_DBG("Freeing %p", skb); |
| 6103 | kfree_skb(skb); |
| 6104 | } |
| 6105 | |
| 6106 | return err; |
| 6107 | } |
| 6108 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6109 | static int l2cap_finish_move(struct l2cap_chan *chan) |
| 6110 | { |
| 6111 | BT_DBG("chan %p", chan); |
| 6112 | |
| 6113 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6114 | |
| 6115 | if (chan->hs_hcon) |
| 6116 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6117 | else |
| 6118 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6119 | |
| 6120 | return l2cap_resegment(chan); |
| 6121 | } |
| 6122 | |
| 6123 | static int l2cap_rx_state_wait_p(struct l2cap_chan *chan, |
| 6124 | struct l2cap_ctrl *control, |
| 6125 | struct sk_buff *skb, u8 event) |
| 6126 | { |
| 6127 | int err; |
| 6128 | |
| 6129 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6130 | event); |
| 6131 | |
| 6132 | if (!control->poll) |
| 6133 | return -EPROTO; |
| 6134 | |
| 6135 | l2cap_process_reqseq(chan, control->reqseq); |
| 6136 | |
| 6137 | if (!skb_queue_empty(&chan->tx_q)) |
| 6138 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6139 | else |
| 6140 | chan->tx_send_head = NULL; |
| 6141 | |
| 6142 | /* Rewind next_tx_seq to the point expected |
| 6143 | * by the receiver. |
| 6144 | */ |
| 6145 | chan->next_tx_seq = control->reqseq; |
| 6146 | chan->unacked_frames = 0; |
| 6147 | |
| 6148 | err = l2cap_finish_move(chan); |
| 6149 | if (err) |
| 6150 | return err; |
| 6151 | |
| 6152 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6153 | l2cap_send_i_or_rr_or_rnr(chan); |
| 6154 | |
| 6155 | if (event == L2CAP_EV_RECV_IFRAME) |
| 6156 | return -EPROTO; |
| 6157 | |
| 6158 | return l2cap_rx_state_recv(chan, control, NULL, event); |
| 6159 | } |
| 6160 | |
| 6161 | static int l2cap_rx_state_wait_f(struct l2cap_chan *chan, |
| 6162 | struct l2cap_ctrl *control, |
| 6163 | struct sk_buff *skb, u8 event) |
| 6164 | { |
| 6165 | int err; |
| 6166 | |
| 6167 | if (!control->final) |
| 6168 | return -EPROTO; |
| 6169 | |
| 6170 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6171 | |
| 6172 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6173 | l2cap_process_reqseq(chan, control->reqseq); |
| 6174 | |
| 6175 | if (!skb_queue_empty(&chan->tx_q)) |
| 6176 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6177 | else |
| 6178 | chan->tx_send_head = NULL; |
| 6179 | |
| 6180 | /* Rewind next_tx_seq to the point expected |
| 6181 | * by the receiver. |
| 6182 | */ |
| 6183 | chan->next_tx_seq = control->reqseq; |
| 6184 | chan->unacked_frames = 0; |
| 6185 | |
| 6186 | if (chan->hs_hcon) |
| 6187 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6188 | else |
| 6189 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6190 | |
| 6191 | err = l2cap_resegment(chan); |
| 6192 | |
| 6193 | if (!err) |
| 6194 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6195 | |
| 6196 | return err; |
| 6197 | } |
| 6198 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6199 | static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 6200 | { |
| 6201 | /* Make sure reqseq is for a packet that has been sent but not acked */ |
| 6202 | u16 unacked; |
| 6203 | |
| 6204 | unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq); |
| 6205 | return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked; |
| 6206 | } |
| 6207 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6208 | static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6209 | struct sk_buff *skb, u8 event) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6210 | { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6211 | int err = 0; |
| 6212 | |
| 6213 | BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan, |
| 6214 | control, skb, event, chan->rx_state); |
| 6215 | |
| 6216 | if (__valid_reqseq(chan, control->reqseq)) { |
| 6217 | switch (chan->rx_state) { |
| 6218 | case L2CAP_RX_STATE_RECV: |
| 6219 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6220 | break; |
| 6221 | case L2CAP_RX_STATE_SREJ_SENT: |
| 6222 | err = l2cap_rx_state_srej_sent(chan, control, skb, |
| 6223 | event); |
| 6224 | break; |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6225 | case L2CAP_RX_STATE_WAIT_P: |
| 6226 | err = l2cap_rx_state_wait_p(chan, control, skb, event); |
| 6227 | break; |
| 6228 | case L2CAP_RX_STATE_WAIT_F: |
| 6229 | err = l2cap_rx_state_wait_f(chan, control, skb, event); |
| 6230 | break; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6231 | default: |
| 6232 | /* shut it down */ |
| 6233 | break; |
| 6234 | } |
| 6235 | } else { |
| 6236 | BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d", |
| 6237 | control->reqseq, chan->next_tx_seq, |
| 6238 | chan->expected_ack_seq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6239 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6240 | } |
| 6241 | |
| 6242 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6243 | } |
| 6244 | |
| 6245 | static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6246 | struct sk_buff *skb) |
| 6247 | { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6248 | int err = 0; |
| 6249 | |
| 6250 | BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb, |
| 6251 | chan->rx_state); |
| 6252 | |
| 6253 | if (l2cap_classify_txseq(chan, control->txseq) == |
| 6254 | L2CAP_TXSEQ_EXPECTED) { |
| 6255 | l2cap_pass_to_tx(chan, control); |
| 6256 | |
| 6257 | BT_DBG("buffer_seq %d->%d", chan->buffer_seq, |
| 6258 | __next_seq(chan, chan->buffer_seq)); |
| 6259 | |
| 6260 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 6261 | |
| 6262 | l2cap_reassemble_sdu(chan, skb, control); |
| 6263 | } else { |
| 6264 | if (chan->sdu) { |
| 6265 | kfree_skb(chan->sdu); |
| 6266 | chan->sdu = NULL; |
| 6267 | } |
| 6268 | chan->sdu_last_frag = NULL; |
| 6269 | chan->sdu_len = 0; |
| 6270 | |
| 6271 | if (skb) { |
| 6272 | BT_DBG("Freeing %p", skb); |
| 6273 | kfree_skb(skb); |
| 6274 | } |
| 6275 | } |
| 6276 | |
| 6277 | chan->last_acked_seq = control->txseq; |
| 6278 | chan->expected_tx_seq = __next_seq(chan, control->txseq); |
| 6279 | |
| 6280 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6281 | } |
| 6282 | |
| 6283 | static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) |
| 6284 | { |
| 6285 | struct l2cap_ctrl *control = &bt_cb(skb)->control; |
| 6286 | u16 len; |
| 6287 | u8 event; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6288 | |
Mat Martineau | b76bbd6 | 2012-04-11 10:48:43 -0700 | [diff] [blame] | 6289 | __unpack_control(chan, skb); |
| 6290 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6291 | len = skb->len; |
| 6292 | |
| 6293 | /* |
| 6294 | * We can just drop the corrupted I-frame here. |
| 6295 | * Receiver will miss it and start proper recovery |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6296 | * procedures and ask for retransmission. |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6297 | */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6298 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6299 | goto drop; |
| 6300 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6301 | if (!control->sframe && control->sar == L2CAP_SAR_START) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6302 | len -= L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6303 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6304 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6305 | len -= L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6306 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6307 | if (len > chan->mps) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6308 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6309 | goto drop; |
| 6310 | } |
| 6311 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6312 | if (!control->sframe) { |
| 6313 | int err; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6314 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6315 | BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d", |
| 6316 | control->sar, control->reqseq, control->final, |
| 6317 | control->txseq); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 6318 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6319 | /* Validate F-bit - F=0 always valid, F=1 only |
| 6320 | * valid in TX WAIT_F |
| 6321 | */ |
| 6322 | if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6323 | goto drop; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6324 | |
| 6325 | if (chan->mode != L2CAP_MODE_STREAMING) { |
| 6326 | event = L2CAP_EV_RECV_IFRAME; |
| 6327 | err = l2cap_rx(chan, control, skb, event); |
| 6328 | } else { |
| 6329 | err = l2cap_stream_rx(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6330 | } |
| 6331 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6332 | if (err) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6333 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6334 | } else { |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6335 | const u8 rx_func_to_event[4] = { |
| 6336 | L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ, |
| 6337 | L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ |
| 6338 | }; |
| 6339 | |
| 6340 | /* Only I-frames are expected in streaming mode */ |
| 6341 | if (chan->mode == L2CAP_MODE_STREAMING) |
| 6342 | goto drop; |
| 6343 | |
| 6344 | BT_DBG("sframe reqseq %d, final %d, poll %d, super %d", |
| 6345 | control->reqseq, control->final, control->poll, |
| 6346 | control->super); |
| 6347 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6348 | if (len != 0) { |
Andrei Emeltchenko | 1bb166e | 2012-11-20 17:16:21 +0200 | [diff] [blame] | 6349 | BT_ERR("Trailing bytes: %d in sframe", len); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6350 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6351 | goto drop; |
| 6352 | } |
| 6353 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6354 | /* Validate F and P bits */ |
| 6355 | if (control->final && (control->poll || |
| 6356 | chan->tx_state != L2CAP_TX_STATE_WAIT_F)) |
| 6357 | goto drop; |
| 6358 | |
| 6359 | event = rx_func_to_event[control->super]; |
| 6360 | if (l2cap_rx(chan, control, skb, event)) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6361 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6362 | } |
| 6363 | |
| 6364 | return 0; |
| 6365 | |
| 6366 | drop: |
| 6367 | kfree_skb(skb); |
| 6368 | return 0; |
| 6369 | } |
| 6370 | |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6371 | static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid, |
| 6372 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6373 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6374 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 6376 | chan = l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6377 | if (!chan) { |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6378 | if (cid == L2CAP_CID_A2MP) { |
| 6379 | chan = a2mp_channel_create(conn, skb); |
| 6380 | if (!chan) { |
| 6381 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6382 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6383 | } |
| 6384 | |
| 6385 | l2cap_chan_lock(chan); |
| 6386 | } else { |
| 6387 | BT_DBG("unknown cid 0x%4.4x", cid); |
| 6388 | /* Drop packet and return */ |
| 6389 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6390 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6391 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6392 | } |
| 6393 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 6394 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6395 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6396 | if (chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6397 | goto drop; |
| 6398 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6399 | switch (chan->mode) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6400 | case L2CAP_MODE_BASIC: |
| 6401 | /* If socket recv buffers overflows we drop data here |
| 6402 | * which is *bad* because L2CAP has to be reliable. |
| 6403 | * But we don't have any other choice. L2CAP doesn't |
| 6404 | * provide flow control mechanism. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6405 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6406 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6407 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6408 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6409 | if (!chan->ops->recv(chan, skb)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6410 | goto done; |
| 6411 | break; |
| 6412 | |
| 6413 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6414 | case L2CAP_MODE_STREAMING: |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6415 | l2cap_data_rcv(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6416 | goto done; |
| 6417 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6418 | default: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6419 | 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] | 6420 | break; |
| 6421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6422 | |
| 6423 | drop: |
| 6424 | kfree_skb(skb); |
| 6425 | |
| 6426 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6427 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6428 | } |
| 6429 | |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6430 | static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, |
| 6431 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | { |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6433 | struct hci_conn *hcon = conn->hcon; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6434 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6435 | |
Marcel Holtmann | ae4fd2d | 2013-10-03 00:03:39 -0700 | [diff] [blame] | 6436 | if (hcon->type != ACL_LINK) |
| 6437 | goto drop; |
| 6438 | |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 6439 | chan = l2cap_global_chan_by_psm(0, psm, &conn->hcon->src, |
Marcel Holtmann | 98e0f7e | 2013-10-13 02:23:40 -0700 | [diff] [blame] | 6440 | &conn->hcon->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6441 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6442 | goto drop; |
| 6443 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 6444 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6445 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6446 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6447 | goto drop; |
| 6448 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 6449 | if (chan->imtu < skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | goto drop; |
| 6451 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6452 | if (!chan->ops->recv(chan, skb)) |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6453 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6454 | |
| 6455 | drop: |
| 6456 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6457 | } |
| 6458 | |
Marcel Holtmann | 72f7835 | 2013-04-23 00:59:00 -0700 | [diff] [blame] | 6459 | static void l2cap_att_channel(struct l2cap_conn *conn, |
Andrei Emeltchenko | 6810fca | 2012-05-31 11:18:57 +0300 | [diff] [blame] | 6460 | struct sk_buff *skb) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6461 | { |
Marcel Holtmann | b99707d | 2013-10-03 02:54:11 -0700 | [diff] [blame] | 6462 | struct hci_conn *hcon = conn->hcon; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6463 | struct l2cap_chan *chan; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6464 | |
Marcel Holtmann | b99707d | 2013-10-03 02:54:11 -0700 | [diff] [blame] | 6465 | if (hcon->type != LE_LINK) |
| 6466 | goto drop; |
| 6467 | |
Johan Hedberg | af1c013 | 2013-04-29 19:35:42 +0300 | [diff] [blame] | 6468 | chan = l2cap_global_chan_by_scid(BT_CONNECTED, L2CAP_CID_ATT, |
Marcel Holtmann | 6f59b90 | 2013-10-13 05:24:01 -0700 | [diff] [blame] | 6469 | &conn->hcon->src, &conn->hcon->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6470 | if (!chan) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6471 | goto drop; |
| 6472 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 6473 | BT_DBG("chan %p, len %d", chan, skb->len); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6474 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 6475 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6476 | goto drop; |
| 6477 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6478 | if (!chan->ops->recv(chan, skb)) |
Andrei Emeltchenko | 6810fca | 2012-05-31 11:18:57 +0300 | [diff] [blame] | 6479 | return; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6480 | |
| 6481 | drop: |
| 6482 | kfree_skb(skb); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6483 | } |
| 6484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6485 | static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) |
| 6486 | { |
| 6487 | struct l2cap_hdr *lh = (void *) skb->data; |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 6488 | u16 cid, len; |
| 6489 | __le16 psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6490 | |
| 6491 | skb_pull(skb, L2CAP_HDR_SIZE); |
| 6492 | cid = __le16_to_cpu(lh->cid); |
| 6493 | len = __le16_to_cpu(lh->len); |
| 6494 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6495 | if (len != skb->len) { |
| 6496 | kfree_skb(skb); |
| 6497 | return; |
| 6498 | } |
| 6499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | BT_DBG("len %d, cid 0x%4.4x", len, cid); |
| 6501 | |
| 6502 | switch (cid) { |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6503 | case L2CAP_CID_SIGNALING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6504 | l2cap_sig_channel(conn, skb); |
| 6505 | break; |
| 6506 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6507 | case L2CAP_CID_CONN_LESS: |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 6508 | psm = get_unaligned((__le16 *) skb->data); |
Andrei Emeltchenko | 0181a70 | 2012-05-29 10:04:05 +0300 | [diff] [blame] | 6509 | skb_pull(skb, L2CAP_PSMLEN_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6510 | l2cap_conless_channel(conn, psm, skb); |
| 6511 | break; |
| 6512 | |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame] | 6513 | case L2CAP_CID_ATT: |
Marcel Holtmann | 72f7835 | 2013-04-23 00:59:00 -0700 | [diff] [blame] | 6514 | l2cap_att_channel(conn, skb); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6515 | break; |
| 6516 | |
Marcel Holtmann | a287762 | 2013-10-02 23:46:54 -0700 | [diff] [blame] | 6517 | case L2CAP_CID_LE_SIGNALING: |
| 6518 | l2cap_le_sig_channel(conn, skb); |
| 6519 | break; |
| 6520 | |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 6521 | case L2CAP_CID_SMP: |
| 6522 | if (smp_sig_channel(conn, skb)) |
| 6523 | l2cap_conn_del(conn->hcon, EACCES); |
| 6524 | break; |
| 6525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6526 | default: |
| 6527 | l2cap_data_channel(conn, cid, skb); |
| 6528 | break; |
| 6529 | } |
| 6530 | } |
| 6531 | |
| 6532 | /* ---- L2CAP interface with lower layer (HCI) ---- */ |
| 6533 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6534 | int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | { |
| 6536 | int exact = 0, lm1 = 0, lm2 = 0; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6537 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6538 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 6539 | BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6540 | |
| 6541 | /* Find listening sockets and check their link_mode */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6542 | read_lock(&chan_list_lock); |
| 6543 | list_for_each_entry(c, &chan_list, global_l) { |
| 6544 | struct sock *sk = c->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6545 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6546 | if (c->state != BT_LISTEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6547 | continue; |
| 6548 | |
| 6549 | if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6550 | lm1 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 6551 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6552 | lm1 |= HCI_LM_MASTER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6553 | exact++; |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6554 | } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { |
| 6555 | lm2 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 6556 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6557 | lm2 |= HCI_LM_MASTER; |
| 6558 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6559 | } |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6560 | read_unlock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | |
| 6562 | return exact ? lm1 : lm2; |
| 6563 | } |
| 6564 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 6565 | void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6566 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 6567 | struct l2cap_conn *conn; |
| 6568 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 6569 | BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6571 | if (!status) { |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 6572 | conn = l2cap_conn_add(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6573 | if (conn) |
| 6574 | l2cap_conn_ready(conn); |
Andrei Emeltchenko | ba6fc31 | 2012-10-31 15:46:26 +0200 | [diff] [blame] | 6575 | } else { |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 6576 | l2cap_conn_del(hcon, bt_to_errno(status)); |
Andrei Emeltchenko | ba6fc31 | 2012-10-31 15:46:26 +0200 | [diff] [blame] | 6577 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6578 | } |
| 6579 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6580 | int l2cap_disconn_ind(struct hci_conn *hcon) |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 6581 | { |
| 6582 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 6583 | |
| 6584 | BT_DBG("hcon %p", hcon); |
| 6585 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6586 | if (!conn) |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 6587 | return HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 6588 | return conn->disc_reason; |
| 6589 | } |
| 6590 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 6591 | void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6592 | { |
| 6593 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 6594 | |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 6595 | l2cap_conn_del(hcon, bt_to_errno(reason)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6596 | } |
| 6597 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6598 | static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6599 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 6600 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
Marcel Holtmann | 255c760 | 2009-02-04 21:07:19 +0100 | [diff] [blame] | 6601 | return; |
| 6602 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6603 | if (encrypt == 0x00) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6604 | if (chan->sec_level == BT_SECURITY_MEDIUM) { |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 6605 | __set_chan_timer(chan, L2CAP_ENC_TIMEOUT); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6606 | } else if (chan->sec_level == BT_SECURITY_HIGH) |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 6607 | l2cap_chan_close(chan, ECONNREFUSED); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6608 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6609 | if (chan->sec_level == BT_SECURITY_MEDIUM) |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 6610 | __clear_chan_timer(chan); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6611 | } |
| 6612 | } |
| 6613 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6614 | int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6615 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 6616 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6617 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6618 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 6619 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6620 | return 0; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 6621 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 6622 | 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] | 6623 | |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 6624 | if (hcon->type == LE_LINK) { |
Hemant Gupta | 35d4adcc | 2012-04-18 14:46:26 +0530 | [diff] [blame] | 6625 | if (!status && encrypt) |
| 6626 | smp_distribute_keys(conn, 0); |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 6627 | cancel_delayed_work(&conn->security_timer); |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 6628 | } |
| 6629 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 6630 | mutex_lock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6631 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 6632 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6633 | l2cap_chan_lock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6634 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 6635 | BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, |
| 6636 | state_to_string(chan->state)); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6637 | |
Andrei Emeltchenko | 78eb2f9 | 2012-07-19 17:03:47 +0300 | [diff] [blame] | 6638 | if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) { |
| 6639 | l2cap_chan_unlock(chan); |
| 6640 | continue; |
| 6641 | } |
| 6642 | |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame] | 6643 | if (chan->scid == L2CAP_CID_ATT) { |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6644 | if (!status && encrypt) { |
| 6645 | chan->sec_level = hcon->sec_level; |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 6646 | l2cap_chan_ready(chan); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6647 | } |
| 6648 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6649 | l2cap_chan_unlock(chan); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6650 | continue; |
| 6651 | } |
| 6652 | |
Andrei Emeltchenko | 96eff46 | 2012-11-15 18:14:53 +0200 | [diff] [blame] | 6653 | if (!__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6654 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 6655 | continue; |
| 6656 | } |
| 6657 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6658 | if (!status && (chan->state == BT_CONNECTED || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6659 | chan->state == BT_CONFIG)) { |
Gustavo Padovan | a7d7723 | 2012-05-13 03:20:07 -0300 | [diff] [blame] | 6660 | struct sock *sk = chan->sk; |
| 6661 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 6662 | clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); |
Gustavo Padovan | a7d7723 | 2012-05-13 03:20:07 -0300 | [diff] [blame] | 6663 | sk->sk_state_change(sk); |
| 6664 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6665 | l2cap_check_encryption(chan, encrypt); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6666 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 9719f8a | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 6667 | continue; |
| 6668 | } |
| 6669 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6670 | if (chan->state == BT_CONNECT) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6671 | if (!status) { |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 6672 | l2cap_start_connection(chan); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6673 | } else { |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 6674 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6675 | } |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6676 | } else if (chan->state == BT_CONNECT2) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6677 | struct sock *sk = chan->sk; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6678 | struct l2cap_conn_rsp rsp; |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6679 | __u16 res, stat; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6680 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6681 | lock_sock(sk); |
| 6682 | |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6683 | if (!status) { |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 6684 | if (test_bit(BT_SK_DEFER_SETUP, |
| 6685 | &bt_sk(sk)->flags)) { |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6686 | res = L2CAP_CR_PEND; |
| 6687 | stat = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 6688 | chan->ops->defer(chan); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6689 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 6690 | __l2cap_state_change(chan, BT_CONFIG); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6691 | res = L2CAP_CR_SUCCESS; |
| 6692 | stat = L2CAP_CS_NO_INFO; |
| 6693 | } |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6694 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 6695 | __l2cap_state_change(chan, BT_DISCONN); |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 6696 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6697 | res = L2CAP_CR_SEC_BLOCK; |
| 6698 | stat = L2CAP_CS_NO_INFO; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6699 | } |
| 6700 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6701 | release_sock(sk); |
| 6702 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 6703 | rsp.scid = cpu_to_le16(chan->dcid); |
| 6704 | rsp.dcid = cpu_to_le16(chan->scid); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6705 | rsp.result = cpu_to_le16(res); |
| 6706 | rsp.status = cpu_to_le16(stat); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 6707 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6708 | sizeof(rsp), &rsp); |
Mat Martineau | 2d36935 | 2012-05-23 14:59:30 -0700 | [diff] [blame] | 6709 | |
| 6710 | if (!test_bit(CONF_REQ_SENT, &chan->conf_state) && |
| 6711 | res == L2CAP_CR_SUCCESS) { |
| 6712 | char buf[128]; |
| 6713 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 6714 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 6715 | L2CAP_CONF_REQ, |
| 6716 | l2cap_build_conf_req(chan, buf), |
| 6717 | buf); |
| 6718 | chan->num_conf_req++; |
| 6719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6720 | } |
| 6721 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6722 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6723 | } |
| 6724 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 6725 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6727 | return 0; |
| 6728 | } |
| 6729 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6730 | 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] | 6731 | { |
| 6732 | struct l2cap_conn *conn = hcon->l2cap_data; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6733 | struct l2cap_hdr *hdr; |
| 6734 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | |
Andrei Emeltchenko | 1d13a25 | 2012-10-15 11:58:41 +0300 | [diff] [blame] | 6736 | /* For AMP controller do not create l2cap conn */ |
| 6737 | if (!conn && hcon->hdev->dev_type != HCI_BREDR) |
| 6738 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6739 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 6740 | if (!conn) |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 6741 | conn = l2cap_conn_add(hcon); |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 6742 | |
| 6743 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6744 | goto drop; |
| 6745 | |
| 6746 | BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); |
| 6747 | |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6748 | switch (flags) { |
| 6749 | case ACL_START: |
| 6750 | case ACL_START_NO_FLUSH: |
| 6751 | case ACL_COMPLETE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | if (conn->rx_len) { |
| 6753 | BT_ERR("Unexpected start frame (len %d)", skb->len); |
| 6754 | kfree_skb(conn->rx_skb); |
| 6755 | conn->rx_skb = NULL; |
| 6756 | conn->rx_len = 0; |
| 6757 | l2cap_conn_unreliable(conn, ECOMM); |
| 6758 | } |
| 6759 | |
Andrei Emeltchenko | aae7fe2 | 2010-09-15 14:28:43 +0300 | [diff] [blame] | 6760 | /* Start fragment always begin with Basic L2CAP header */ |
| 6761 | if (skb->len < L2CAP_HDR_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | BT_ERR("Frame is too short (len %d)", skb->len); |
| 6763 | l2cap_conn_unreliable(conn, ECOMM); |
| 6764 | goto drop; |
| 6765 | } |
| 6766 | |
| 6767 | hdr = (struct l2cap_hdr *) skb->data; |
| 6768 | len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; |
| 6769 | |
| 6770 | if (len == skb->len) { |
| 6771 | /* Complete frame received */ |
| 6772 | l2cap_recv_frame(conn, skb); |
| 6773 | return 0; |
| 6774 | } |
| 6775 | |
| 6776 | BT_DBG("Start: total len %d, frag len %d", len, skb->len); |
| 6777 | |
| 6778 | if (skb->len > len) { |
| 6779 | BT_ERR("Frame is too long (len %d, expected len %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6780 | skb->len, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6781 | l2cap_conn_unreliable(conn, ECOMM); |
| 6782 | goto drop; |
| 6783 | } |
| 6784 | |
| 6785 | /* Allocate skb for the complete frame (with header) */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 6786 | conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 6787 | if (!conn->rx_skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6788 | goto drop; |
| 6789 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 6790 | 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] | 6791 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6792 | conn->rx_len = len - skb->len; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6793 | break; |
| 6794 | |
| 6795 | case ACL_CONT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6796 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
| 6797 | |
| 6798 | if (!conn->rx_len) { |
| 6799 | BT_ERR("Unexpected continuation frame (len %d)", skb->len); |
| 6800 | l2cap_conn_unreliable(conn, ECOMM); |
| 6801 | goto drop; |
| 6802 | } |
| 6803 | |
| 6804 | if (skb->len > conn->rx_len) { |
| 6805 | BT_ERR("Fragment is too long (len %d, expected %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6806 | skb->len, conn->rx_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6807 | kfree_skb(conn->rx_skb); |
| 6808 | conn->rx_skb = NULL; |
| 6809 | conn->rx_len = 0; |
| 6810 | l2cap_conn_unreliable(conn, ECOMM); |
| 6811 | goto drop; |
| 6812 | } |
| 6813 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 6814 | 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] | 6815 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6816 | conn->rx_len -= skb->len; |
| 6817 | |
| 6818 | if (!conn->rx_len) { |
Johan Hedberg | c4e5baf | 2013-10-10 13:33:37 +0200 | [diff] [blame] | 6819 | /* Complete frame received. l2cap_recv_frame |
| 6820 | * takes ownership of the skb so set the global |
| 6821 | * rx_skb pointer to NULL first. |
| 6822 | */ |
| 6823 | struct sk_buff *rx_skb = conn->rx_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6824 | conn->rx_skb = NULL; |
Johan Hedberg | c4e5baf | 2013-10-10 13:33:37 +0200 | [diff] [blame] | 6825 | l2cap_recv_frame(conn, rx_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6826 | } |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6827 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6828 | } |
| 6829 | |
| 6830 | drop: |
| 6831 | kfree_skb(skb); |
| 6832 | return 0; |
| 6833 | } |
| 6834 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6835 | static int l2cap_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6836 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6837 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6838 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 6839 | read_lock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6840 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6841 | list_for_each_entry(c, &chan_list, global_l) { |
| 6842 | struct sock *sk = c->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6843 | |
Andrei Emeltchenko | fcb7333 | 2012-09-25 12:49:44 +0300 | [diff] [blame] | 6844 | seq_printf(f, "%pMR %pMR %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", |
| 6845 | &bt_sk(sk)->src, &bt_sk(sk)->dst, |
| 6846 | c->state, __le16_to_cpu(c->psm), |
| 6847 | c->scid, c->dcid, c->imtu, c->omtu, |
| 6848 | c->sec_level, c->mode); |
Andrei Emeltchenko | 61e1b4b | 2012-01-19 11:19:50 +0200 | [diff] [blame] | 6849 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6850 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 6851 | read_unlock(&chan_list_lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 6852 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6853 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6854 | } |
| 6855 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6856 | static int l2cap_debugfs_open(struct inode *inode, struct file *file) |
| 6857 | { |
| 6858 | return single_open(file, l2cap_debugfs_show, inode->i_private); |
| 6859 | } |
| 6860 | |
| 6861 | static const struct file_operations l2cap_debugfs_fops = { |
| 6862 | .open = l2cap_debugfs_open, |
| 6863 | .read = seq_read, |
| 6864 | .llseek = seq_lseek, |
| 6865 | .release = single_release, |
| 6866 | }; |
| 6867 | |
| 6868 | static struct dentry *l2cap_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6869 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 6870 | int __init l2cap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6871 | { |
| 6872 | int err; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 6873 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 6874 | err = l2cap_init_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6875 | if (err < 0) |
| 6876 | return err; |
| 6877 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6878 | if (bt_debugfs) { |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6879 | l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs, |
| 6880 | NULL, &l2cap_debugfs_fops); |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6881 | if (!l2cap_debugfs) |
| 6882 | BT_ERR("Failed to create L2CAP debug file"); |
| 6883 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6885 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6886 | } |
| 6887 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 6888 | void l2cap_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6889 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6890 | debugfs_remove(l2cap_debugfs); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 6891 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | } |
| 6893 | |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 6894 | module_param(disable_ertm, bool, 0644); |
| 6895 | MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); |