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> |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 39 | #include <net/bluetooth/smp.h> |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 40 | #include <net/bluetooth/a2mp.h> |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 41 | #include <net/bluetooth/amp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Mat Martineau | d1de6d4 | 2012-05-17 20:53:55 -0700 | [diff] [blame] | 43 | bool disable_ertm; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 44 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 45 | static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN; |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 46 | static u8 l2cap_fixed_chan[8] = { L2CAP_FC_L2CAP, }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 48 | static LIST_HEAD(chan_list); |
| 49 | static DEFINE_RWLOCK(chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 52 | u8 code, u8 ident, u16 dlen, void *data); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 53 | 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] | 54 | void *data); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 55 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 56 | static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 58 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 59 | struct sk_buff_head *skbs, u8 event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 60 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 61 | /* ---- L2CAP channels ---- */ |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 62 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 63 | static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, |
| 64 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 65 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 66 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 67 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 68 | list_for_each_entry(c, &conn->chan_l, list) { |
| 69 | if (c->dcid == cid) |
| 70 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 71 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 72 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 75 | static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, |
| 76 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 77 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 78 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 79 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 80 | list_for_each_entry(c, &conn->chan_l, list) { |
| 81 | if (c->scid == cid) |
| 82 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 83 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 84 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* Find channel with given SCID. |
Mat Martineau | ef191ad | 2012-05-02 09:42:00 -0700 | [diff] [blame] | 88 | * Returns locked channel. */ |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 89 | static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, |
| 90 | u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 91 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 92 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 93 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 94 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 95 | c = __l2cap_get_chan_by_scid(conn, cid); |
Mat Martineau | ef191ad | 2012-05-02 09:42:00 -0700 | [diff] [blame] | 96 | if (c) |
| 97 | l2cap_chan_lock(c); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 98 | mutex_unlock(&conn->chan_lock); |
| 99 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 100 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Mat Martineau | b1a130b | 2012-10-23 15:24:09 -0700 | [diff] [blame] | 103 | /* Find channel with given DCID. |
| 104 | * Returns locked channel. |
| 105 | */ |
| 106 | static struct l2cap_chan *l2cap_get_chan_by_dcid(struct l2cap_conn *conn, |
| 107 | u16 cid) |
| 108 | { |
| 109 | struct l2cap_chan *c; |
| 110 | |
| 111 | mutex_lock(&conn->chan_lock); |
| 112 | c = __l2cap_get_chan_by_dcid(conn, cid); |
| 113 | if (c) |
| 114 | l2cap_chan_lock(c); |
| 115 | mutex_unlock(&conn->chan_lock); |
| 116 | |
| 117 | return c; |
| 118 | } |
| 119 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 120 | static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, |
| 121 | u8 ident) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 122 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 123 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 124 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 125 | list_for_each_entry(c, &conn->chan_l, list) { |
| 126 | if (c->ident == ident) |
| 127 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 128 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 129 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 132 | static struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, |
| 133 | u8 ident) |
| 134 | { |
| 135 | struct l2cap_chan *c; |
| 136 | |
| 137 | mutex_lock(&conn->chan_lock); |
| 138 | c = __l2cap_get_chan_by_ident(conn, ident); |
| 139 | if (c) |
| 140 | l2cap_chan_lock(c); |
| 141 | mutex_unlock(&conn->chan_lock); |
| 142 | |
| 143 | return c; |
| 144 | } |
| 145 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 146 | 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] | 147 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 148 | struct l2cap_chan *c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 149 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 150 | list_for_each_entry(c, &chan_list, global_l) { |
| 151 | if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src)) |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 152 | return c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 153 | } |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 154 | return NULL; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) |
| 158 | { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 159 | int err; |
| 160 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 161 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 162 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 163 | if (psm && __l2cap_global_chan_by_addr(psm, src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 164 | err = -EADDRINUSE; |
| 165 | goto done; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 166 | } |
| 167 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 168 | if (psm) { |
| 169 | chan->psm = psm; |
| 170 | chan->sport = psm; |
| 171 | err = 0; |
| 172 | } else { |
| 173 | u16 p; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 174 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 175 | err = -EINVAL; |
| 176 | for (p = 0x1001; p < 0x1100; p += 2) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 177 | if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 178 | chan->psm = cpu_to_le16(p); |
| 179 | chan->sport = cpu_to_le16(p); |
| 180 | err = 0; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | done: |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 186 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 187 | return err; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) |
| 191 | { |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 192 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 193 | |
| 194 | chan->scid = scid; |
| 195 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 196 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 201 | static u16 l2cap_alloc_cid(struct l2cap_conn *conn) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 202 | { |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 203 | u16 cid = L2CAP_CID_DYN_START; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 204 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 205 | for (; cid < L2CAP_CID_DYN_END; cid++) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 206 | if (!__l2cap_get_chan_by_scid(conn, cid)) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 207 | return cid; |
| 208 | } |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 213 | static void __l2cap_state_change(struct l2cap_chan *chan, int state) |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 214 | { |
Andrei Emeltchenko | 42d2d87 | 2012-02-17 11:40:57 +0200 | [diff] [blame] | 215 | BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 216 | state_to_string(state)); |
Gustavo F. Padovan | badaaa0 | 2011-11-23 20:11:46 -0200 | [diff] [blame] | 217 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 218 | chan->state = state; |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 219 | chan->ops->state_change(chan, state); |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 220 | } |
| 221 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 222 | static void l2cap_state_change(struct l2cap_chan *chan, int state) |
| 223 | { |
| 224 | struct sock *sk = chan->sk; |
| 225 | |
| 226 | lock_sock(sk); |
| 227 | __l2cap_state_change(chan, state); |
| 228 | release_sock(sk); |
| 229 | } |
| 230 | |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 231 | static inline void __l2cap_chan_set_err(struct l2cap_chan *chan, int err) |
| 232 | { |
| 233 | struct sock *sk = chan->sk; |
| 234 | |
| 235 | sk->sk_err = err; |
| 236 | } |
| 237 | |
| 238 | static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err) |
| 239 | { |
| 240 | struct sock *sk = chan->sk; |
| 241 | |
| 242 | lock_sock(sk); |
| 243 | __l2cap_chan_set_err(chan, err); |
| 244 | release_sock(sk); |
| 245 | } |
| 246 | |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 247 | static void __set_retrans_timer(struct l2cap_chan *chan) |
| 248 | { |
| 249 | if (!delayed_work_pending(&chan->monitor_timer) && |
| 250 | chan->retrans_timeout) { |
| 251 | l2cap_set_timer(chan, &chan->retrans_timer, |
| 252 | msecs_to_jiffies(chan->retrans_timeout)); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | static void __set_monitor_timer(struct l2cap_chan *chan) |
| 257 | { |
| 258 | __clear_retrans_timer(chan); |
| 259 | if (chan->monitor_timeout) { |
| 260 | l2cap_set_timer(chan, &chan->monitor_timer, |
| 261 | msecs_to_jiffies(chan->monitor_timeout)); |
| 262 | } |
| 263 | } |
| 264 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 265 | static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head, |
| 266 | u16 seq) |
| 267 | { |
| 268 | struct sk_buff *skb; |
| 269 | |
| 270 | skb_queue_walk(head, skb) { |
| 271 | if (bt_cb(skb)->control.txseq == seq) |
| 272 | return skb; |
| 273 | } |
| 274 | |
| 275 | return NULL; |
| 276 | } |
| 277 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 278 | /* ---- L2CAP sequence number lists ---- */ |
| 279 | |
| 280 | /* For ERTM, ordered lists of sequence numbers must be tracked for |
| 281 | * SREJ requests that are received and for frames that are to be |
| 282 | * retransmitted. These seq_list functions implement a singly-linked |
| 283 | * list in an array, where membership in the list can also be checked |
| 284 | * in constant time. Items can also be added to the tail of the list |
| 285 | * and removed from the head in constant time, without further memory |
| 286 | * allocs or frees. |
| 287 | */ |
| 288 | |
| 289 | static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size) |
| 290 | { |
| 291 | size_t alloc_size, i; |
| 292 | |
| 293 | /* Allocated size is a power of 2 to map sequence numbers |
| 294 | * (which may be up to 14 bits) in to a smaller array that is |
| 295 | * sized for the negotiated ERTM transmit windows. |
| 296 | */ |
| 297 | alloc_size = roundup_pow_of_two(size); |
| 298 | |
| 299 | seq_list->list = kmalloc(sizeof(u16) * alloc_size, GFP_KERNEL); |
| 300 | if (!seq_list->list) |
| 301 | return -ENOMEM; |
| 302 | |
| 303 | seq_list->mask = alloc_size - 1; |
| 304 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 305 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
| 306 | for (i = 0; i < alloc_size; i++) |
| 307 | seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list) |
| 313 | { |
| 314 | kfree(seq_list->list); |
| 315 | } |
| 316 | |
| 317 | static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list, |
| 318 | u16 seq) |
| 319 | { |
| 320 | /* Constant-time check for list membership */ |
| 321 | return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR; |
| 322 | } |
| 323 | |
| 324 | static u16 l2cap_seq_list_remove(struct l2cap_seq_list *seq_list, u16 seq) |
| 325 | { |
| 326 | u16 mask = seq_list->mask; |
| 327 | |
| 328 | if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) { |
| 329 | /* In case someone tries to pop the head of an empty list */ |
| 330 | return L2CAP_SEQ_LIST_CLEAR; |
| 331 | } else if (seq_list->head == seq) { |
| 332 | /* Head can be removed in constant time */ |
| 333 | seq_list->head = seq_list->list[seq & mask]; |
| 334 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR; |
| 335 | |
| 336 | if (seq_list->head == L2CAP_SEQ_LIST_TAIL) { |
| 337 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 338 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
| 339 | } |
| 340 | } else { |
| 341 | /* Walk the list to find the sequence number */ |
| 342 | u16 prev = seq_list->head; |
| 343 | while (seq_list->list[prev & mask] != seq) { |
| 344 | prev = seq_list->list[prev & mask]; |
| 345 | if (prev == L2CAP_SEQ_LIST_TAIL) |
| 346 | return L2CAP_SEQ_LIST_CLEAR; |
| 347 | } |
| 348 | |
| 349 | /* Unlink the number from the list and clear it */ |
| 350 | seq_list->list[prev & mask] = seq_list->list[seq & mask]; |
| 351 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR; |
| 352 | if (seq_list->tail == seq) |
| 353 | seq_list->tail = prev; |
| 354 | } |
| 355 | return seq; |
| 356 | } |
| 357 | |
| 358 | static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list) |
| 359 | { |
| 360 | /* Remove the head in constant time */ |
| 361 | return l2cap_seq_list_remove(seq_list, seq_list->head); |
| 362 | } |
| 363 | |
| 364 | static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list) |
| 365 | { |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 366 | u16 i; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 367 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 368 | if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) |
| 369 | return; |
| 370 | |
| 371 | for (i = 0; i <= seq_list->mask; i++) |
| 372 | seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR; |
| 373 | |
| 374 | seq_list->head = L2CAP_SEQ_LIST_CLEAR; |
| 375 | seq_list->tail = L2CAP_SEQ_LIST_CLEAR; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq) |
| 379 | { |
| 380 | u16 mask = seq_list->mask; |
| 381 | |
| 382 | /* All appends happen in constant time */ |
| 383 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 384 | if (seq_list->list[seq & mask] != L2CAP_SEQ_LIST_CLEAR) |
| 385 | return; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 386 | |
Gustavo Padovan | f522ae3 | 2012-05-09 18:28:00 -0300 | [diff] [blame] | 387 | if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR) |
| 388 | seq_list->head = seq; |
| 389 | else |
| 390 | seq_list->list[seq_list->tail & mask] = seq; |
| 391 | |
| 392 | seq_list->tail = seq; |
| 393 | seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 396 | static void l2cap_chan_timeout(struct work_struct *work) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 397 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 398 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 399 | chan_timer.work); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 400 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 401 | int reason; |
| 402 | |
Andrei Emeltchenko | e05dcc3 | 2012-02-17 11:40:56 +0200 | [diff] [blame] | 403 | 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] | 404 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 405 | mutex_lock(&conn->chan_lock); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 406 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 407 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 408 | if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 409 | reason = ECONNREFUSED; |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 410 | else if (chan->state == BT_CONNECT && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 411 | chan->sec_level != BT_SECURITY_SDP) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 412 | reason = ECONNREFUSED; |
| 413 | else |
| 414 | reason = ETIMEDOUT; |
| 415 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 416 | l2cap_chan_close(chan, reason); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 417 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 418 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 419 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 420 | chan->ops->close(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 421 | mutex_unlock(&conn->chan_lock); |
| 422 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 423 | l2cap_chan_put(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 424 | } |
| 425 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 426 | struct l2cap_chan *l2cap_chan_create(void) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 427 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 428 | struct l2cap_chan *chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 429 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 430 | chan = kzalloc(sizeof(*chan), GFP_ATOMIC); |
| 431 | if (!chan) |
| 432 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 433 | |
Andrei Emeltchenko | c03b355 | 2012-02-21 12:54:56 +0200 | [diff] [blame] | 434 | mutex_init(&chan->lock); |
| 435 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 436 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 437 | list_add(&chan->global_l, &chan_list); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 438 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 439 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 440 | INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 441 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 442 | chan->state = BT_OPEN; |
| 443 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 444 | kref_init(&chan->kref); |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 445 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 446 | /* This flag is cleared in l2cap_chan_ready() */ |
| 447 | set_bit(CONF_NOT_COMPLETE, &chan->conf_state); |
| 448 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 449 | BT_DBG("chan %p", chan); |
Szymon Janc | abc545b | 2011-11-03 16:05:44 +0100 | [diff] [blame] | 450 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 451 | return chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 454 | static void l2cap_chan_destroy(struct kref *kref) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 455 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 456 | struct l2cap_chan *chan = container_of(kref, struct l2cap_chan, kref); |
| 457 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 458 | BT_DBG("chan %p", chan); |
| 459 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 460 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 461 | list_del(&chan->global_l); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 462 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 463 | |
Jaganath Kanakkassery | 4af66c6 | 2012-07-13 18:17:55 +0530 | [diff] [blame] | 464 | kfree(chan); |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 465 | } |
| 466 | |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 467 | void l2cap_chan_hold(struct l2cap_chan *c) |
| 468 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 469 | 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] | 470 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 471 | kref_get(&c->kref); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | void l2cap_chan_put(struct l2cap_chan *c) |
| 475 | { |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 476 | 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] | 477 | |
Syam Sidhardhan | 144ad33 | 2012-07-27 23:51:21 +0530 | [diff] [blame] | 478 | kref_put(&c->kref, l2cap_chan_destroy); |
Jaganath Kanakkassery | 3064837 | 2012-07-13 18:17:54 +0530 | [diff] [blame] | 479 | } |
| 480 | |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 481 | void l2cap_chan_set_defaults(struct l2cap_chan *chan) |
| 482 | { |
| 483 | chan->fcs = L2CAP_FCS_CRC16; |
| 484 | chan->max_tx = L2CAP_DEFAULT_MAX_TX; |
| 485 | chan->tx_win = L2CAP_DEFAULT_TX_WINDOW; |
| 486 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 487 | chan->ack_win = L2CAP_DEFAULT_TX_WINDOW; |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame] | 488 | chan->sec_level = BT_SECURITY_LOW; |
| 489 | |
| 490 | set_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 491 | } |
| 492 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 493 | void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 494 | { |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 495 | 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] | 496 | __le16_to_cpu(chan->psm), chan->dcid); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 497 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 498 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 499 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 500 | chan->conn = conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 501 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 502 | switch (chan->chan_type) { |
| 503 | case L2CAP_CHAN_CONN_ORIENTED: |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 504 | if (conn->hcon->type == LE_LINK) { |
| 505 | /* LE connection */ |
Andre Guedes | 6fcb06a | 2012-05-31 17:01:33 -0300 | [diff] [blame] | 506 | chan->omtu = L2CAP_DEFAULT_MTU; |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame] | 507 | chan->scid = L2CAP_CID_ATT; |
| 508 | chan->dcid = L2CAP_CID_ATT; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 509 | } else { |
| 510 | /* Alloc CID for connection-oriented socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 511 | chan->scid = l2cap_alloc_cid(conn); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 512 | chan->omtu = L2CAP_DEFAULT_MTU; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 513 | } |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 514 | break; |
| 515 | |
| 516 | case L2CAP_CHAN_CONN_LESS: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 517 | /* Connectionless socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 518 | chan->scid = L2CAP_CID_CONN_LESS; |
| 519 | chan->dcid = L2CAP_CID_CONN_LESS; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 520 | chan->omtu = L2CAP_DEFAULT_MTU; |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 521 | break; |
| 522 | |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 523 | case L2CAP_CHAN_CONN_FIX_A2MP: |
| 524 | chan->scid = L2CAP_CID_A2MP; |
| 525 | chan->dcid = L2CAP_CID_A2MP; |
| 526 | chan->omtu = L2CAP_A2MP_DEFAULT_MTU; |
| 527 | chan->imtu = L2CAP_A2MP_DEFAULT_MTU; |
| 528 | break; |
| 529 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 530 | default: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 531 | /* Raw socket can send/recv signalling messages only */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 532 | chan->scid = L2CAP_CID_SIGNALING; |
| 533 | chan->dcid = L2CAP_CID_SIGNALING; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 534 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 537 | chan->local_id = L2CAP_BESTEFFORT_ID; |
| 538 | chan->local_stype = L2CAP_SERV_BESTEFFORT; |
| 539 | chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; |
| 540 | chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; |
| 541 | chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 542 | chan->local_flush_to = L2CAP_EFS_DEFAULT_FLUSH_TO; |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 543 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 544 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 545 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 546 | list_add(&chan->list, &conn->chan_l); |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 547 | } |
| 548 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 549 | void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 550 | { |
| 551 | mutex_lock(&conn->chan_lock); |
| 552 | __l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 553 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 556 | void l2cap_chan_del(struct l2cap_chan *chan, int err) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 557 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 558 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 559 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 560 | __clear_chan_timer(chan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 561 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 562 | BT_DBG("chan %p, conn %p, err %d", chan, conn, err); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 563 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 564 | if (conn) { |
Andrei Emeltchenko | 56f6098 | 2012-10-15 11:58:44 +0300 | [diff] [blame] | 565 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 566 | /* Delete from channel list */ |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 567 | list_del(&chan->list); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 568 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 569 | l2cap_chan_put(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 570 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 571 | chan->conn = NULL; |
Andrei Emeltchenko | 3cabbfd | 2012-05-31 11:01:37 +0300 | [diff] [blame] | 572 | |
| 573 | if (chan->chan_type != L2CAP_CHAN_CONN_FIX_A2MP) |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 574 | hci_conn_drop(conn->hcon); |
Andrei Emeltchenko | 56f6098 | 2012-10-15 11:58:44 +0300 | [diff] [blame] | 575 | |
| 576 | if (mgr && mgr->bredr_chan == chan) |
| 577 | mgr->bredr_chan = NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Andrei Emeltchenko | 419e08c | 2012-10-31 15:46:34 +0200 | [diff] [blame] | 580 | if (chan->hs_hchan) { |
| 581 | struct hci_chan *hs_hchan = chan->hs_hchan; |
| 582 | |
| 583 | BT_DBG("chan %p disconnect hs_hchan %p", chan, hs_hchan); |
| 584 | amp_disconnect_logical_link(hs_hchan); |
| 585 | } |
| 586 | |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 587 | chan->ops->teardown(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 588 | |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 589 | if (test_bit(CONF_NOT_COMPLETE, &chan->conf_state)) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 590 | return; |
Gustavo F. Padovan | 2ead70b | 2011-04-01 15:13:36 -0300 | [diff] [blame] | 591 | |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 592 | switch(chan->mode) { |
| 593 | case L2CAP_MODE_BASIC: |
| 594 | break; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 595 | |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 596 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 597 | __clear_retrans_timer(chan); |
| 598 | __clear_monitor_timer(chan); |
| 599 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 600 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 601 | skb_queue_purge(&chan->srej_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 602 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 603 | l2cap_seq_list_free(&chan->srej_list); |
| 604 | l2cap_seq_list_free(&chan->retrans_list); |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 605 | |
| 606 | /* fall through */ |
| 607 | |
| 608 | case L2CAP_MODE_STREAMING: |
| 609 | skb_queue_purge(&chan->tx_q); |
| 610 | break; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 611 | } |
Gustavo Padovan | ee556f6 | 2012-05-18 20:22:38 -0300 | [diff] [blame] | 612 | |
| 613 | return; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 614 | } |
| 615 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 616 | void l2cap_chan_close(struct l2cap_chan *chan, int reason) |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 617 | { |
| 618 | struct l2cap_conn *conn = chan->conn; |
| 619 | struct sock *sk = chan->sk; |
| 620 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 621 | BT_DBG("chan %p state %s sk %p", chan, state_to_string(chan->state), |
| 622 | sk); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 623 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 624 | switch (chan->state) { |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 625 | case BT_LISTEN: |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 626 | chan->ops->teardown(chan, 0); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 627 | break; |
| 628 | |
| 629 | case BT_CONNECTED: |
| 630 | case BT_CONFIG: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 631 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 632 | conn->hcon->type == ACL_LINK) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 633 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 634 | l2cap_send_disconn_req(chan, reason); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 635 | } else |
| 636 | l2cap_chan_del(chan, reason); |
| 637 | break; |
| 638 | |
| 639 | case BT_CONNECT2: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 640 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 641 | conn->hcon->type == ACL_LINK) { |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 642 | struct l2cap_conn_rsp rsp; |
| 643 | __u16 result; |
| 644 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 645 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 646 | result = L2CAP_CR_SEC_BLOCK; |
| 647 | else |
| 648 | result = L2CAP_CR_BAD_PSM; |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 649 | l2cap_state_change(chan, BT_DISCONN); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 650 | |
| 651 | rsp.scid = cpu_to_le16(chan->dcid); |
| 652 | rsp.dcid = cpu_to_le16(chan->scid); |
| 653 | rsp.result = cpu_to_le16(result); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 654 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 655 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 656 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | l2cap_chan_del(chan, reason); |
| 660 | break; |
| 661 | |
| 662 | case BT_CONNECT: |
| 663 | case BT_DISCONN: |
| 664 | l2cap_chan_del(chan, reason); |
| 665 | break; |
| 666 | |
| 667 | default: |
Gustavo Padovan | b699ec0 | 2012-10-06 11:51:54 +0100 | [diff] [blame] | 668 | chan->ops->teardown(chan, 0); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 669 | break; |
| 670 | } |
| 671 | } |
| 672 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 673 | static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 674 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 675 | if (chan->chan_type == L2CAP_CHAN_RAW) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 676 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 677 | case BT_SECURITY_HIGH: |
| 678 | return HCI_AT_DEDICATED_BONDING_MITM; |
| 679 | case BT_SECURITY_MEDIUM: |
| 680 | return HCI_AT_DEDICATED_BONDING; |
| 681 | default: |
| 682 | return HCI_AT_NO_BONDING; |
| 683 | } |
Andrei Emeltchenko | 2983fd6 | 2012-05-24 15:42:50 +0300 | [diff] [blame] | 684 | } else if (chan->psm == __constant_cpu_to_le16(L2CAP_PSM_SDP)) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 685 | if (chan->sec_level == BT_SECURITY_LOW) |
| 686 | chan->sec_level = BT_SECURITY_SDP; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 687 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 688 | if (chan->sec_level == BT_SECURITY_HIGH) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 689 | return HCI_AT_NO_BONDING_MITM; |
| 690 | else |
| 691 | return HCI_AT_NO_BONDING; |
| 692 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 693 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 694 | case BT_SECURITY_HIGH: |
| 695 | return HCI_AT_GENERAL_BONDING_MITM; |
| 696 | case BT_SECURITY_MEDIUM: |
| 697 | return HCI_AT_GENERAL_BONDING; |
| 698 | default: |
| 699 | return HCI_AT_NO_BONDING; |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 704 | /* Service level security */ |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 705 | int l2cap_chan_check_security(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 706 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 707 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 708 | __u8 auth_type; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 709 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 710 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 711 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 712 | return hci_conn_security(conn->hcon, chan->sec_level, auth_type); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 713 | } |
| 714 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 715 | static u8 l2cap_get_ident(struct l2cap_conn *conn) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 716 | { |
| 717 | u8 id; |
| 718 | |
| 719 | /* Get next available identificator. |
| 720 | * 1 - 128 are used by kernel. |
| 721 | * 129 - 199 are reserved. |
| 722 | * 200 - 254 are used by utilities like l2ping, etc. |
| 723 | */ |
| 724 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 725 | spin_lock(&conn->lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 726 | |
| 727 | if (++conn->tx_ident > 128) |
| 728 | conn->tx_ident = 1; |
| 729 | |
| 730 | id = conn->tx_ident; |
| 731 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 732 | spin_unlock(&conn->lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 733 | |
| 734 | return id; |
| 735 | } |
| 736 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 737 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, |
| 738 | void *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 739 | { |
| 740 | struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 741 | u8 flags; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 742 | |
| 743 | BT_DBG("code 0x%2.2x", code); |
| 744 | |
| 745 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 746 | return; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 747 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 748 | if (lmp_no_flush_capable(conn->hcon->hdev)) |
| 749 | flags = ACL_START_NO_FLUSH; |
| 750 | else |
| 751 | flags = ACL_START; |
| 752 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 753 | bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 754 | skb->priority = HCI_PRIO_MAX; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 755 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 756 | hci_send_acl(conn->hchan, skb, flags); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 757 | } |
| 758 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 759 | static bool __chan_is_moving(struct l2cap_chan *chan) |
| 760 | { |
| 761 | return chan->move_state != L2CAP_MOVE_STABLE && |
| 762 | chan->move_state != L2CAP_MOVE_WAIT_PREPARE; |
| 763 | } |
| 764 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 765 | static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) |
| 766 | { |
| 767 | struct hci_conn *hcon = chan->conn->hcon; |
| 768 | u16 flags; |
| 769 | |
| 770 | 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] | 771 | skb->priority); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 772 | |
Mat Martineau | d5f8a75 | 2012-10-23 15:24:18 -0700 | [diff] [blame] | 773 | if (chan->hs_hcon && !__chan_is_moving(chan)) { |
| 774 | if (chan->hs_hchan) |
| 775 | hci_send_acl(chan->hs_hchan, skb, ACL_COMPLETE); |
| 776 | else |
| 777 | kfree_skb(skb); |
| 778 | |
| 779 | return; |
| 780 | } |
| 781 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 782 | if (!test_bit(FLAG_FLUSHABLE, &chan->flags) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 783 | lmp_no_flush_capable(hcon->hdev)) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 784 | flags = ACL_START_NO_FLUSH; |
| 785 | else |
| 786 | flags = ACL_START; |
| 787 | |
| 788 | bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 789 | hci_send_acl(chan->conn->hchan, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 792 | static void __unpack_enhanced_control(u16 enh, struct l2cap_ctrl *control) |
| 793 | { |
| 794 | control->reqseq = (enh & L2CAP_CTRL_REQSEQ) >> L2CAP_CTRL_REQSEQ_SHIFT; |
| 795 | control->final = (enh & L2CAP_CTRL_FINAL) >> L2CAP_CTRL_FINAL_SHIFT; |
| 796 | |
| 797 | if (enh & L2CAP_CTRL_FRAME_TYPE) { |
| 798 | /* S-Frame */ |
| 799 | control->sframe = 1; |
| 800 | control->poll = (enh & L2CAP_CTRL_POLL) >> L2CAP_CTRL_POLL_SHIFT; |
| 801 | control->super = (enh & L2CAP_CTRL_SUPERVISE) >> L2CAP_CTRL_SUPER_SHIFT; |
| 802 | |
| 803 | control->sar = 0; |
| 804 | control->txseq = 0; |
| 805 | } else { |
| 806 | /* I-Frame */ |
| 807 | control->sframe = 0; |
| 808 | control->sar = (enh & L2CAP_CTRL_SAR) >> L2CAP_CTRL_SAR_SHIFT; |
| 809 | control->txseq = (enh & L2CAP_CTRL_TXSEQ) >> L2CAP_CTRL_TXSEQ_SHIFT; |
| 810 | |
| 811 | control->poll = 0; |
| 812 | control->super = 0; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | static void __unpack_extended_control(u32 ext, struct l2cap_ctrl *control) |
| 817 | { |
| 818 | control->reqseq = (ext & L2CAP_EXT_CTRL_REQSEQ) >> L2CAP_EXT_CTRL_REQSEQ_SHIFT; |
| 819 | control->final = (ext & L2CAP_EXT_CTRL_FINAL) >> L2CAP_EXT_CTRL_FINAL_SHIFT; |
| 820 | |
| 821 | if (ext & L2CAP_EXT_CTRL_FRAME_TYPE) { |
| 822 | /* S-Frame */ |
| 823 | control->sframe = 1; |
| 824 | control->poll = (ext & L2CAP_EXT_CTRL_POLL) >> L2CAP_EXT_CTRL_POLL_SHIFT; |
| 825 | control->super = (ext & L2CAP_EXT_CTRL_SUPERVISE) >> L2CAP_EXT_CTRL_SUPER_SHIFT; |
| 826 | |
| 827 | control->sar = 0; |
| 828 | control->txseq = 0; |
| 829 | } else { |
| 830 | /* I-Frame */ |
| 831 | control->sframe = 0; |
| 832 | control->sar = (ext & L2CAP_EXT_CTRL_SAR) >> L2CAP_EXT_CTRL_SAR_SHIFT; |
| 833 | control->txseq = (ext & L2CAP_EXT_CTRL_TXSEQ) >> L2CAP_EXT_CTRL_TXSEQ_SHIFT; |
| 834 | |
| 835 | control->poll = 0; |
| 836 | control->super = 0; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | static inline void __unpack_control(struct l2cap_chan *chan, |
| 841 | struct sk_buff *skb) |
| 842 | { |
| 843 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 844 | __unpack_extended_control(get_unaligned_le32(skb->data), |
| 845 | &bt_cb(skb)->control); |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 846 | skb_pull(skb, L2CAP_EXT_CTRL_SIZE); |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 847 | } else { |
| 848 | __unpack_enhanced_control(get_unaligned_le16(skb->data), |
| 849 | &bt_cb(skb)->control); |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 850 | skb_pull(skb, L2CAP_ENH_CTRL_SIZE); |
Mat Martineau | b5c6aae | 2012-04-25 16:36:15 -0700 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
| 854 | static u32 __pack_extended_control(struct l2cap_ctrl *control) |
| 855 | { |
| 856 | u32 packed; |
| 857 | |
| 858 | packed = control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT; |
| 859 | packed |= control->final << L2CAP_EXT_CTRL_FINAL_SHIFT; |
| 860 | |
| 861 | if (control->sframe) { |
| 862 | packed |= control->poll << L2CAP_EXT_CTRL_POLL_SHIFT; |
| 863 | packed |= control->super << L2CAP_EXT_CTRL_SUPER_SHIFT; |
| 864 | packed |= L2CAP_EXT_CTRL_FRAME_TYPE; |
| 865 | } else { |
| 866 | packed |= control->sar << L2CAP_EXT_CTRL_SAR_SHIFT; |
| 867 | packed |= control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT; |
| 868 | } |
| 869 | |
| 870 | return packed; |
| 871 | } |
| 872 | |
| 873 | static u16 __pack_enhanced_control(struct l2cap_ctrl *control) |
| 874 | { |
| 875 | u16 packed; |
| 876 | |
| 877 | packed = control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT; |
| 878 | packed |= control->final << L2CAP_CTRL_FINAL_SHIFT; |
| 879 | |
| 880 | if (control->sframe) { |
| 881 | packed |= control->poll << L2CAP_CTRL_POLL_SHIFT; |
| 882 | packed |= control->super << L2CAP_CTRL_SUPER_SHIFT; |
| 883 | packed |= L2CAP_CTRL_FRAME_TYPE; |
| 884 | } else { |
| 885 | packed |= control->sar << L2CAP_CTRL_SAR_SHIFT; |
| 886 | packed |= control->txseq << L2CAP_CTRL_TXSEQ_SHIFT; |
| 887 | } |
| 888 | |
| 889 | return packed; |
| 890 | } |
| 891 | |
| 892 | static inline void __pack_control(struct l2cap_chan *chan, |
| 893 | struct l2cap_ctrl *control, |
| 894 | struct sk_buff *skb) |
| 895 | { |
| 896 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 897 | put_unaligned_le32(__pack_extended_control(control), |
| 898 | skb->data + L2CAP_HDR_SIZE); |
| 899 | } else { |
| 900 | put_unaligned_le16(__pack_enhanced_control(control), |
| 901 | skb->data + L2CAP_HDR_SIZE); |
| 902 | } |
| 903 | } |
| 904 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 905 | static inline unsigned int __ertm_hdr_size(struct l2cap_chan *chan) |
| 906 | { |
| 907 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 908 | return L2CAP_EXT_HDR_SIZE; |
| 909 | else |
| 910 | return L2CAP_ENH_HDR_SIZE; |
| 911 | } |
| 912 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 913 | static struct sk_buff *l2cap_create_sframe_pdu(struct l2cap_chan *chan, |
| 914 | u32 control) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 915 | { |
| 916 | struct sk_buff *skb; |
| 917 | struct l2cap_hdr *lh; |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 918 | int hlen = __ertm_hdr_size(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 919 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 920 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 921 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 922 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 923 | skb = bt_skb_alloc(hlen, GFP_KERNEL); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 924 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 925 | if (!skb) |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 926 | return ERR_PTR(-ENOMEM); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 927 | |
| 928 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 929 | lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 930 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 931 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 932 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 933 | put_unaligned_le32(control, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 934 | else |
| 935 | put_unaligned_le16(control, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 936 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 937 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 938 | u16 fcs = crc16(0, (u8 *)skb->data, skb->len); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 939 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 940 | } |
| 941 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 942 | skb->priority = HCI_PRIO_MAX; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 943 | return skb; |
| 944 | } |
| 945 | |
| 946 | static void l2cap_send_sframe(struct l2cap_chan *chan, |
| 947 | struct l2cap_ctrl *control) |
| 948 | { |
| 949 | struct sk_buff *skb; |
| 950 | u32 control_field; |
| 951 | |
| 952 | BT_DBG("chan %p, control %p", chan, control); |
| 953 | |
| 954 | if (!control->sframe) |
| 955 | return; |
| 956 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 957 | if (__chan_is_moving(chan)) |
| 958 | return; |
| 959 | |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 960 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state) && |
| 961 | !control->poll) |
| 962 | control->final = 1; |
| 963 | |
| 964 | if (control->super == L2CAP_SUPER_RR) |
| 965 | clear_bit(CONN_RNR_SENT, &chan->conn_state); |
| 966 | else if (control->super == L2CAP_SUPER_RNR) |
| 967 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
| 968 | |
| 969 | if (control->super != L2CAP_SUPER_SREJ) { |
| 970 | chan->last_acked_seq = control->reqseq; |
| 971 | __clear_ack_timer(chan); |
| 972 | } |
| 973 | |
| 974 | BT_DBG("reqseq %d, final %d, poll %d, super %d", control->reqseq, |
| 975 | control->final, control->poll, control->super); |
| 976 | |
| 977 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 978 | control_field = __pack_extended_control(control); |
| 979 | else |
| 980 | control_field = __pack_enhanced_control(control); |
| 981 | |
| 982 | skb = l2cap_create_sframe_pdu(chan, control_field); |
| 983 | if (!IS_ERR(skb)) |
| 984 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 985 | } |
| 986 | |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 987 | 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] | 988 | { |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 989 | struct l2cap_ctrl control; |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 990 | |
Mat Martineau | c9e3d5e | 2012-05-17 20:53:48 -0700 | [diff] [blame] | 991 | BT_DBG("chan %p, poll %d", chan, poll); |
| 992 | |
| 993 | memset(&control, 0, sizeof(control)); |
| 994 | control.sframe = 1; |
| 995 | control.poll = poll; |
| 996 | |
| 997 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 998 | control.super = L2CAP_SUPER_RNR; |
| 999 | else |
| 1000 | control.super = L2CAP_SUPER_RR; |
| 1001 | |
| 1002 | control.reqseq = chan->buffer_seq; |
| 1003 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 1004 | } |
| 1005 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1006 | static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 1007 | { |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1008 | return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 1009 | } |
| 1010 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1011 | static bool __amp_capable(struct l2cap_chan *chan) |
| 1012 | { |
| 1013 | struct l2cap_conn *conn = chan->conn; |
| 1014 | |
| 1015 | if (enable_hs && |
Andrei Emeltchenko | 5d05416 | 2012-11-29 17:46:05 +0200 | [diff] [blame] | 1016 | hci_amp_capable() && |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1017 | chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED && |
| 1018 | conn->fixed_chan_mask & L2CAP_FC_A2MP) |
| 1019 | return true; |
| 1020 | else |
| 1021 | return false; |
| 1022 | } |
| 1023 | |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 1024 | static bool l2cap_check_efs(struct l2cap_chan *chan) |
| 1025 | { |
| 1026 | /* Check EFS parameters */ |
| 1027 | return true; |
| 1028 | } |
| 1029 | |
Andrei Emeltchenko | 2766be4 | 2012-09-27 17:26:21 +0300 | [diff] [blame] | 1030 | void l2cap_send_conn_req(struct l2cap_chan *chan) |
Andrei Emeltchenko | 9b27f35 | 2012-02-24 16:00:00 +0200 | [diff] [blame] | 1031 | { |
| 1032 | struct l2cap_conn *conn = chan->conn; |
| 1033 | struct l2cap_conn_req req; |
| 1034 | |
| 1035 | req.scid = cpu_to_le16(chan->scid); |
| 1036 | req.psm = chan->psm; |
| 1037 | |
| 1038 | chan->ident = l2cap_get_ident(conn); |
| 1039 | |
| 1040 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
| 1041 | |
| 1042 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req); |
| 1043 | } |
| 1044 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 1045 | static void l2cap_send_create_chan_req(struct l2cap_chan *chan, u8 amp_id) |
| 1046 | { |
| 1047 | struct l2cap_create_chan_req req; |
| 1048 | req.scid = cpu_to_le16(chan->scid); |
| 1049 | req.psm = chan->psm; |
| 1050 | req.amp_id = amp_id; |
| 1051 | |
| 1052 | chan->ident = l2cap_get_ident(chan->conn); |
| 1053 | |
| 1054 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_REQ, |
| 1055 | sizeof(req), &req); |
| 1056 | } |
| 1057 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 1058 | static void l2cap_move_setup(struct l2cap_chan *chan) |
| 1059 | { |
| 1060 | struct sk_buff *skb; |
| 1061 | |
| 1062 | BT_DBG("chan %p", chan); |
| 1063 | |
| 1064 | if (chan->mode != L2CAP_MODE_ERTM) |
| 1065 | return; |
| 1066 | |
| 1067 | __clear_retrans_timer(chan); |
| 1068 | __clear_monitor_timer(chan); |
| 1069 | __clear_ack_timer(chan); |
| 1070 | |
| 1071 | chan->retry_count = 0; |
| 1072 | skb_queue_walk(&chan->tx_q, skb) { |
| 1073 | if (bt_cb(skb)->control.retries) |
| 1074 | bt_cb(skb)->control.retries = 1; |
| 1075 | else |
| 1076 | break; |
| 1077 | } |
| 1078 | |
| 1079 | chan->expected_tx_seq = chan->buffer_seq; |
| 1080 | |
| 1081 | clear_bit(CONN_REJ_ACT, &chan->conn_state); |
| 1082 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 1083 | l2cap_seq_list_clear(&chan->retrans_list); |
| 1084 | l2cap_seq_list_clear(&chan->srej_list); |
| 1085 | skb_queue_purge(&chan->srej_q); |
| 1086 | |
| 1087 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 1088 | chan->rx_state = L2CAP_RX_STATE_MOVE; |
| 1089 | |
| 1090 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 1091 | } |
| 1092 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 1093 | static void l2cap_move_done(struct l2cap_chan *chan) |
| 1094 | { |
| 1095 | u8 move_role = chan->move_role; |
| 1096 | BT_DBG("chan %p", chan); |
| 1097 | |
| 1098 | chan->move_state = L2CAP_MOVE_STABLE; |
| 1099 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 1100 | |
| 1101 | if (chan->mode != L2CAP_MODE_ERTM) |
| 1102 | return; |
| 1103 | |
| 1104 | switch (move_role) { |
| 1105 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 1106 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_EXPLICIT_POLL); |
| 1107 | chan->rx_state = L2CAP_RX_STATE_WAIT_F; |
| 1108 | break; |
| 1109 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 1110 | chan->rx_state = L2CAP_RX_STATE_WAIT_P; |
| 1111 | break; |
| 1112 | } |
| 1113 | } |
| 1114 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1115 | static void l2cap_chan_ready(struct l2cap_chan *chan) |
| 1116 | { |
Mat Martineau | 2827011 | 2012-05-17 21:14:09 -0700 | [diff] [blame] | 1117 | /* This clears all conf flags, including CONF_NOT_COMPLETE */ |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1118 | chan->conf_state = 0; |
| 1119 | __clear_chan_timer(chan); |
| 1120 | |
Andrei Emeltchenko | 54a59aa | 2012-05-27 22:27:53 -0300 | [diff] [blame] | 1121 | chan->state = BT_CONNECTED; |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1122 | |
Andrei Emeltchenko | fd83e2c | 2012-05-30 09:55:32 +0300 | [diff] [blame] | 1123 | chan->ops->ready(chan); |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1124 | } |
| 1125 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1126 | static void l2cap_start_connection(struct l2cap_chan *chan) |
| 1127 | { |
| 1128 | if (__amp_capable(chan)) { |
| 1129 | BT_DBG("chan %p AMP capable: discover AMPs", chan); |
| 1130 | a2mp_discover_amp(chan); |
| 1131 | } else { |
| 1132 | l2cap_send_conn_req(chan); |
| 1133 | } |
| 1134 | } |
| 1135 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1136 | static void l2cap_do_start(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1137 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1138 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1139 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1140 | if (conn->hcon->type == LE_LINK) { |
| 1141 | l2cap_chan_ready(chan); |
| 1142 | return; |
| 1143 | } |
| 1144 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1145 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1146 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) |
| 1147 | return; |
| 1148 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1149 | if (l2cap_chan_check_security(chan) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1150 | __l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1151 | l2cap_start_connection(chan); |
| 1152 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1153 | } else { |
| 1154 | struct l2cap_info_req req; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 1155 | req.type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1156 | |
| 1157 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 1158 | conn->info_ident = l2cap_get_ident(conn); |
| 1159 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 1160 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1161 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1162 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 1163 | sizeof(req), &req); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 1167 | static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) |
| 1168 | { |
| 1169 | u32 local_feat_mask = l2cap_feat_mask; |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 1170 | if (!disable_ertm) |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 1171 | local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; |
| 1172 | |
| 1173 | switch (mode) { |
| 1174 | case L2CAP_MODE_ERTM: |
| 1175 | return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; |
| 1176 | case L2CAP_MODE_STREAMING: |
| 1177 | return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; |
| 1178 | default: |
| 1179 | return 0x00; |
| 1180 | } |
| 1181 | } |
| 1182 | |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1183 | 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] | 1184 | { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1185 | struct sock *sk = chan->sk; |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 1186 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1187 | struct l2cap_disconn_req req; |
| 1188 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1189 | if (!conn) |
| 1190 | return; |
| 1191 | |
Andrei Emeltchenko | aad3d0e | 2012-09-06 15:05:42 +0300 | [diff] [blame] | 1192 | if (chan->mode == L2CAP_MODE_ERTM && chan->state == BT_CONNECTED) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1193 | __clear_retrans_timer(chan); |
| 1194 | __clear_monitor_timer(chan); |
| 1195 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1196 | } |
| 1197 | |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1198 | if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) { |
Gustavo Padovan | d117773 | 2012-10-06 11:47:38 +0100 | [diff] [blame] | 1199 | l2cap_state_change(chan, BT_DISCONN); |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1200 | return; |
| 1201 | } |
| 1202 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1203 | req.dcid = cpu_to_le16(chan->dcid); |
| 1204 | req.scid = cpu_to_le16(chan->scid); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1205 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ, |
| 1206 | sizeof(req), &req); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1207 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1208 | lock_sock(sk); |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 1209 | __l2cap_state_change(chan, BT_DISCONN); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 1210 | __l2cap_chan_set_err(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1211 | release_sock(sk); |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 1212 | } |
| 1213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | /* ---- L2CAP connections ---- */ |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1215 | static void l2cap_conn_start(struct l2cap_conn *conn) |
| 1216 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1217 | struct l2cap_chan *chan, *tmp; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1218 | |
| 1219 | BT_DBG("conn %p", conn); |
| 1220 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1221 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1222 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1223 | list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1224 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1225 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1226 | l2cap_chan_lock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1227 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1228 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1229 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1230 | continue; |
| 1231 | } |
| 1232 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1233 | if (chan->state == BT_CONNECT) { |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1234 | if (!l2cap_chan_check_security(chan) || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1235 | !__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1236 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1237 | continue; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 1238 | } |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1239 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1240 | if (!l2cap_mode_supported(chan->mode, conn->feat_mask) |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1241 | && test_bit(CONF_STATE2_DEVICE, |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1242 | &chan->conf_state)) { |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1243 | l2cap_chan_close(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1244 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1245 | continue; |
| 1246 | } |
| 1247 | |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 1248 | l2cap_start_connection(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 1249 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1250 | } else if (chan->state == BT_CONNECT2) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1251 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1252 | char buf[128]; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1253 | rsp.scid = cpu_to_le16(chan->dcid); |
| 1254 | rsp.dcid = cpu_to_le16(chan->scid); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1255 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1256 | if (l2cap_chan_check_security(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1257 | lock_sock(sk); |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 1258 | if (test_bit(BT_SK_DEFER_SETUP, |
| 1259 | &bt_sk(sk)->flags)) { |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 1260 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_PEND); |
| 1261 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_AUTHOR_PEND); |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 1262 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 1263 | |
| 1264 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 1265 | __l2cap_state_change(chan, BT_CONFIG); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 1266 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS); |
| 1267 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 1268 | } |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1269 | release_sock(sk); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1270 | } else { |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 1271 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_PEND); |
| 1272 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_AUTHEN_PEND); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1273 | } |
| 1274 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1275 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1276 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1277 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1278 | if (test_bit(CONF_REQ_SENT, &chan->conf_state) || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1279 | rsp.result != L2CAP_CR_SUCCESS) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1280 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1281 | continue; |
| 1282 | } |
| 1283 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 1284 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 1285 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1286 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1287 | chan->num_conf_req++; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1288 | } |
| 1289 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1290 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1291 | } |
| 1292 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1293 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1294 | } |
| 1295 | |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1296 | /* Find socket with cid and source/destination bdaddr. |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1297 | * Returns closest match, locked. |
| 1298 | */ |
Andrei Emeltchenko | d9b8870 | 2012-03-12 12:13:08 +0200 | [diff] [blame] | 1299 | 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] | 1300 | bdaddr_t *src, |
| 1301 | bdaddr_t *dst) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1302 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1303 | struct l2cap_chan *c, *c1 = NULL; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1304 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1305 | read_lock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1306 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1307 | list_for_each_entry(c, &chan_list, global_l) { |
| 1308 | struct sock *sk = c->sk; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1309 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1310 | if (state && c->state != state) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1311 | continue; |
| 1312 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1313 | if (c->scid == cid) { |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1314 | int src_match, dst_match; |
| 1315 | int src_any, dst_any; |
| 1316 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1317 | /* Exact match. */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1318 | src_match = !bacmp(&bt_sk(sk)->src, src); |
| 1319 | dst_match = !bacmp(&bt_sk(sk)->dst, dst); |
| 1320 | if (src_match && dst_match) { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1321 | read_unlock(&chan_list_lock); |
| 1322 | return c; |
| 1323 | } |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1324 | |
| 1325 | /* Closest match */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1326 | src_any = !bacmp(&bt_sk(sk)->src, BDADDR_ANY); |
| 1327 | dst_any = !bacmp(&bt_sk(sk)->dst, BDADDR_ANY); |
| 1328 | if ((src_match && dst_any) || (src_any && dst_match) || |
| 1329 | (src_any && dst_any)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1330 | c1 = c; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1331 | } |
| 1332 | } |
Gustavo F. Padovan | 280f294 | 2011-04-13 19:01:22 -0300 | [diff] [blame] | 1333 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1334 | read_unlock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1335 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1336 | return c1; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | static void l2cap_le_conn_ready(struct l2cap_conn *conn) |
| 1340 | { |
Gustavo F. Padovan | c916fbe | 2011-04-04 16:00:55 -0300 | [diff] [blame] | 1341 | struct sock *parent, *sk; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1342 | struct l2cap_chan *chan, *pchan; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1343 | |
| 1344 | BT_DBG(""); |
| 1345 | |
| 1346 | /* Check if we have socket listening on cid */ |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame] | 1347 | pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_ATT, |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1348 | conn->src, conn->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1349 | if (!pchan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1350 | return; |
| 1351 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1352 | parent = pchan->sk; |
| 1353 | |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 1354 | lock_sock(parent); |
Gustavo F. Padovan | 62f3a2c | 2011-04-14 18:34:34 -0300 | [diff] [blame] | 1355 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 1356 | chan = pchan->ops->new_connection(pchan); |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1357 | if (!chan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1358 | goto clean; |
| 1359 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 1360 | sk = chan->sk; |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 1361 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1362 | hci_conn_hold(conn->hcon); |
Szymon Janc | a9ea3ed | 2012-07-19 14:46:08 +0200 | [diff] [blame] | 1363 | conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1364 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1365 | bacpy(&bt_sk(sk)->src, conn->src); |
| 1366 | bacpy(&bt_sk(sk)->dst, conn->dst); |
| 1367 | |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 1368 | l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1369 | |
Gustavo Padovan | 6754e0d | 2012-05-25 09:30:56 -0300 | [diff] [blame] | 1370 | l2cap_chan_ready(chan); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1371 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1372 | clean: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 1373 | release_sock(parent); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1374 | } |
| 1375 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1376 | static void l2cap_conn_ready(struct l2cap_conn *conn) |
| 1377 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1378 | struct l2cap_chan *chan; |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1379 | struct hci_conn *hcon = conn->hcon; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1380 | |
| 1381 | BT_DBG("conn %p", conn); |
| 1382 | |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1383 | if (!hcon->out && hcon->type == LE_LINK) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 1384 | l2cap_le_conn_ready(conn); |
| 1385 | |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1386 | if (hcon->out && hcon->type == LE_LINK) |
| 1387 | smp_conn_security(hcon, hcon->pending_sec_level); |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 1388 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1389 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1390 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1391 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1392 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1393 | l2cap_chan_lock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1394 | |
Andrei Emeltchenko | 416fa75 | 2012-05-29 13:59:16 +0300 | [diff] [blame] | 1395 | if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) { |
| 1396 | l2cap_chan_unlock(chan); |
| 1397 | continue; |
| 1398 | } |
| 1399 | |
Vinicius Costa Gomes | cc11092 | 2012-08-23 21:32:43 -0300 | [diff] [blame] | 1400 | if (hcon->type == LE_LINK) { |
| 1401 | if (smp_conn_security(hcon, chan->sec_level)) |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 1402 | l2cap_chan_ready(chan); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1403 | |
Vinicius Costa Gomes | 6312845 | 2011-06-17 22:46:26 -0300 | [diff] [blame] | 1404 | } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1405 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1406 | __clear_chan_timer(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1407 | lock_sock(sk); |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 1408 | __l2cap_state_change(chan, BT_CONNECTED); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1409 | sk->sk_state_change(sk); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1410 | release_sock(sk); |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 1411 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1412 | } else if (chan->state == BT_CONNECT) |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1413 | l2cap_do_start(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1414 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1415 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1416 | } |
| 1417 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1418 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1419 | } |
| 1420 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1421 | /* Notify sockets that we cannot guaranty reliability anymore */ |
| 1422 | static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) |
| 1423 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1424 | struct l2cap_chan *chan; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1425 | |
| 1426 | BT_DBG("conn %p", conn); |
| 1427 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1428 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1429 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1430 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | ecf61bd | 2011-10-11 14:04:32 +0300 | [diff] [blame] | 1431 | if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) |
Gustavo Padovan | 1d8b1fd | 2012-10-06 11:34:52 +0100 | [diff] [blame] | 1432 | l2cap_chan_set_err(chan, err); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1433 | } |
| 1434 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1435 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1436 | } |
| 1437 | |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1438 | static void l2cap_info_timeout(struct work_struct *work) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1439 | { |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1440 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1441 | info_timer.work); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1442 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1443 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 1444 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1445 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1446 | l2cap_conn_start(conn); |
| 1447 | } |
| 1448 | |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1449 | /* |
| 1450 | * l2cap_user |
| 1451 | * External modules can register l2cap_user objects on l2cap_conn. The ->probe |
| 1452 | * callback is called during registration. The ->remove callback is called |
| 1453 | * during unregistration. |
| 1454 | * An l2cap_user object can either be explicitly unregistered or when the |
| 1455 | * underlying l2cap_conn object is deleted. This guarantees that l2cap->hcon, |
| 1456 | * l2cap->hchan, .. are valid as long as the remove callback hasn't been called. |
| 1457 | * External modules must own a reference to the l2cap_conn object if they intend |
| 1458 | * to call l2cap_unregister_user(). The l2cap_conn object might get destroyed at |
| 1459 | * any time if they don't. |
| 1460 | */ |
| 1461 | |
| 1462 | int l2cap_register_user(struct l2cap_conn *conn, struct l2cap_user *user) |
| 1463 | { |
| 1464 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1465 | int ret; |
| 1466 | |
| 1467 | /* We need to check whether l2cap_conn is registered. If it is not, we |
| 1468 | * must not register the l2cap_user. l2cap_conn_del() is unregisters |
| 1469 | * l2cap_conn objects, but doesn't provide its own locking. Instead, it |
| 1470 | * relies on the parent hci_conn object to be locked. This itself relies |
| 1471 | * on the hci_dev object to be locked. So we must lock the hci device |
| 1472 | * here, too. */ |
| 1473 | |
| 1474 | hci_dev_lock(hdev); |
| 1475 | |
| 1476 | if (user->list.next || user->list.prev) { |
| 1477 | ret = -EINVAL; |
| 1478 | goto out_unlock; |
| 1479 | } |
| 1480 | |
| 1481 | /* conn->hchan is NULL after l2cap_conn_del() was called */ |
| 1482 | if (!conn->hchan) { |
| 1483 | ret = -ENODEV; |
| 1484 | goto out_unlock; |
| 1485 | } |
| 1486 | |
| 1487 | ret = user->probe(conn, user); |
| 1488 | if (ret) |
| 1489 | goto out_unlock; |
| 1490 | |
| 1491 | list_add(&user->list, &conn->users); |
| 1492 | ret = 0; |
| 1493 | |
| 1494 | out_unlock: |
| 1495 | hci_dev_unlock(hdev); |
| 1496 | return ret; |
| 1497 | } |
| 1498 | EXPORT_SYMBOL(l2cap_register_user); |
| 1499 | |
| 1500 | void l2cap_unregister_user(struct l2cap_conn *conn, struct l2cap_user *user) |
| 1501 | { |
| 1502 | struct hci_dev *hdev = conn->hcon->hdev; |
| 1503 | |
| 1504 | hci_dev_lock(hdev); |
| 1505 | |
| 1506 | if (!user->list.next || !user->list.prev) |
| 1507 | goto out_unlock; |
| 1508 | |
| 1509 | list_del(&user->list); |
| 1510 | user->list.next = NULL; |
| 1511 | user->list.prev = NULL; |
| 1512 | user->remove(conn, user); |
| 1513 | |
| 1514 | out_unlock: |
| 1515 | hci_dev_unlock(hdev); |
| 1516 | } |
| 1517 | EXPORT_SYMBOL(l2cap_unregister_user); |
| 1518 | |
| 1519 | static void l2cap_unregister_all_users(struct l2cap_conn *conn) |
| 1520 | { |
| 1521 | struct l2cap_user *user; |
| 1522 | |
| 1523 | while (!list_empty(&conn->users)) { |
| 1524 | user = list_first_entry(&conn->users, struct l2cap_user, list); |
| 1525 | list_del(&user->list); |
| 1526 | user->list.next = NULL; |
| 1527 | user->list.prev = NULL; |
| 1528 | user->remove(conn, user); |
| 1529 | } |
| 1530 | } |
| 1531 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1532 | static void l2cap_conn_del(struct hci_conn *hcon, int err) |
| 1533 | { |
| 1534 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 1535 | struct l2cap_chan *chan, *l; |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1536 | |
| 1537 | if (!conn) |
| 1538 | return; |
| 1539 | |
| 1540 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 1541 | |
| 1542 | kfree_skb(conn->rx_skb); |
| 1543 | |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1544 | l2cap_unregister_all_users(conn); |
| 1545 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1546 | mutex_lock(&conn->chan_lock); |
| 1547 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1548 | /* Kill channels */ |
| 1549 | list_for_each_entry_safe(chan, l, &conn->chan_l, list) { |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1550 | l2cap_chan_hold(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1551 | l2cap_chan_lock(chan); |
| 1552 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1553 | l2cap_chan_del(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1554 | |
| 1555 | l2cap_chan_unlock(chan); |
| 1556 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 1557 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 1558 | l2cap_chan_put(chan); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1559 | } |
| 1560 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1561 | mutex_unlock(&conn->chan_lock); |
| 1562 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1563 | hci_chan_del(conn->hchan); |
| 1564 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1565 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1566 | cancel_delayed_work_sync(&conn->info_timer); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1567 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1568 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) { |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1569 | cancel_delayed_work_sync(&conn->security_timer); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1570 | smp_chan_destroy(conn); |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1571 | } |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1572 | |
| 1573 | hcon->l2cap_data = NULL; |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1574 | conn->hchan = NULL; |
| 1575 | l2cap_conn_put(conn); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1576 | } |
| 1577 | |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1578 | static void security_timeout(struct work_struct *work) |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1579 | { |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1580 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1581 | security_timer.work); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1582 | |
Johan Hedberg | d06cc41 | 2012-06-06 18:44:11 +0800 | [diff] [blame] | 1583 | BT_DBG("conn %p", conn); |
| 1584 | |
| 1585 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) { |
| 1586 | smp_chan_destroy(conn); |
| 1587 | l2cap_conn_del(conn->hcon, ETIMEDOUT); |
| 1588 | } |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1589 | } |
| 1590 | |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 1591 | static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1593 | struct l2cap_conn *conn = hcon->l2cap_data; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1594 | struct hci_chan *hchan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 1596 | if (conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | return conn; |
| 1598 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1599 | hchan = hci_chan_create(hcon); |
| 1600 | if (!hchan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 1603 | conn = kzalloc(sizeof(struct l2cap_conn), GFP_KERNEL); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1604 | if (!conn) { |
| 1605 | hci_chan_del(hchan); |
| 1606 | return NULL; |
| 1607 | } |
| 1608 | |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1609 | kref_init(&conn->ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | hcon->l2cap_data = conn; |
| 1611 | conn->hcon = hcon; |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1612 | hci_conn_get(conn->hcon); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1613 | conn->hchan = hchan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1615 | BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1616 | |
Andrei Emeltchenko | dcc042d | 2012-10-05 16:56:57 +0300 | [diff] [blame] | 1617 | switch (hcon->type) { |
Andrei Emeltchenko | dcc042d | 2012-10-05 16:56:57 +0300 | [diff] [blame] | 1618 | case LE_LINK: |
| 1619 | if (hcon->hdev->le_mtu) { |
| 1620 | conn->mtu = hcon->hdev->le_mtu; |
| 1621 | break; |
| 1622 | } |
| 1623 | /* fall through */ |
Andrei Emeltchenko | dcc042d | 2012-10-05 16:56:57 +0300 | [diff] [blame] | 1624 | default: |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1625 | conn->mtu = hcon->hdev->acl_mtu; |
Andrei Emeltchenko | dcc042d | 2012-10-05 16:56:57 +0300 | [diff] [blame] | 1626 | break; |
| 1627 | } |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | conn->src = &hcon->hdev->bdaddr; |
| 1630 | conn->dst = &hcon->dst; |
| 1631 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1632 | conn->feat_mask = 0; |
| 1633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | spin_lock_init(&conn->lock); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1635 | mutex_init(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1636 | |
| 1637 | INIT_LIST_HEAD(&conn->chan_l); |
David Herrmann | 2c8e141 | 2013-04-06 20:28:45 +0200 | [diff] [blame] | 1638 | INIT_LIST_HEAD(&conn->users); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1640 | if (hcon->type == LE_LINK) |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1641 | INIT_DELAYED_WORK(&conn->security_timer, security_timeout); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1642 | else |
Gustavo F. Padovan | 030013d | 2011-12-20 10:57:28 -0200 | [diff] [blame] | 1643 | INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); |
Dave Young | 45054dc | 2009-10-18 20:28:30 +0000 | [diff] [blame] | 1644 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 1645 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 1646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | return conn; |
| 1648 | } |
| 1649 | |
David Herrmann | 9c903e3 | 2013-04-06 20:28:44 +0200 | [diff] [blame] | 1650 | static void l2cap_conn_free(struct kref *ref) |
| 1651 | { |
| 1652 | struct l2cap_conn *conn = container_of(ref, struct l2cap_conn, ref); |
| 1653 | |
| 1654 | hci_conn_put(conn->hcon); |
| 1655 | kfree(conn); |
| 1656 | } |
| 1657 | |
| 1658 | void l2cap_conn_get(struct l2cap_conn *conn) |
| 1659 | { |
| 1660 | kref_get(&conn->ref); |
| 1661 | } |
| 1662 | EXPORT_SYMBOL(l2cap_conn_get); |
| 1663 | |
| 1664 | void l2cap_conn_put(struct l2cap_conn *conn) |
| 1665 | { |
| 1666 | kref_put(&conn->ref, l2cap_conn_free); |
| 1667 | } |
| 1668 | EXPORT_SYMBOL(l2cap_conn_put); |
| 1669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | /* ---- Socket interface ---- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1672 | /* Find socket with psm and source / destination bdaddr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | * Returns closest match. |
| 1674 | */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1675 | static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, |
| 1676 | bdaddr_t *src, |
| 1677 | bdaddr_t *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1679 | struct l2cap_chan *c, *c1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1681 | read_lock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1682 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1683 | list_for_each_entry(c, &chan_list, global_l) { |
| 1684 | struct sock *sk = c->sk; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1685 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1686 | if (state && c->state != state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | continue; |
| 1688 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1689 | if (c->psm == psm) { |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1690 | int src_match, dst_match; |
| 1691 | int src_any, dst_any; |
| 1692 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | /* Exact match. */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1694 | src_match = !bacmp(&bt_sk(sk)->src, src); |
| 1695 | dst_match = !bacmp(&bt_sk(sk)->dst, dst); |
| 1696 | if (src_match && dst_match) { |
Johannes Berg | a7567b2 | 2011-06-01 08:29:54 +0200 | [diff] [blame] | 1697 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1698 | return c; |
| 1699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | |
| 1701 | /* Closest match */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 1702 | src_any = !bacmp(&bt_sk(sk)->src, BDADDR_ANY); |
| 1703 | dst_any = !bacmp(&bt_sk(sk)->dst, BDADDR_ANY); |
| 1704 | if ((src_match && dst_any) || (src_any && dst_match) || |
| 1705 | (src_any && dst_any)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1706 | c1 = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | } |
| 1708 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1710 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1711 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1712 | return c1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | } |
| 1714 | |
Andre Guedes | 8e9f989 | 2012-04-24 21:02:55 -0300 | [diff] [blame] | 1715 | int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, |
| 1716 | bdaddr_t *dst, u8 dst_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | { |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 1718 | struct sock *sk = chan->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | bdaddr_t *src = &bt_sk(sk)->src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | struct l2cap_conn *conn; |
| 1721 | struct hci_conn *hcon; |
| 1722 | struct hci_dev *hdev; |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1723 | __u8 auth_type; |
Marcel Holtmann | 44d0e48 | 2009-04-20 07:09:16 +0200 | [diff] [blame] | 1724 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1725 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 1726 | BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", src, dst, |
Syam Sidhardhan | ab19516 | 2012-07-27 23:51:22 +0530 | [diff] [blame] | 1727 | dst_type, __le16_to_cpu(psm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1729 | hdev = hci_get_route(dst, src); |
| 1730 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | return -EHOSTUNREACH; |
| 1732 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1733 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1735 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1736 | |
| 1737 | /* PSM must be odd and lsb of upper byte must be 0 */ |
| 1738 | if ((__le16_to_cpu(psm) & 0x0101) != 0x0001 && !cid && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 1739 | chan->chan_type != L2CAP_CHAN_RAW) { |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1740 | err = -EINVAL; |
| 1741 | goto done; |
| 1742 | } |
| 1743 | |
| 1744 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !(psm || cid)) { |
| 1745 | err = -EINVAL; |
| 1746 | goto done; |
| 1747 | } |
| 1748 | |
| 1749 | switch (chan->mode) { |
| 1750 | case L2CAP_MODE_BASIC: |
| 1751 | break; |
| 1752 | case L2CAP_MODE_ERTM: |
| 1753 | case L2CAP_MODE_STREAMING: |
| 1754 | if (!disable_ertm) |
| 1755 | break; |
| 1756 | /* fall through */ |
| 1757 | default: |
| 1758 | err = -ENOTSUPP; |
| 1759 | goto done; |
| 1760 | } |
| 1761 | |
Gustavo Padovan | 0797e01 | 2012-05-27 22:27:54 -0300 | [diff] [blame] | 1762 | switch (chan->state) { |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1763 | case BT_CONNECT: |
| 1764 | case BT_CONNECT2: |
| 1765 | case BT_CONFIG: |
| 1766 | /* Already connecting */ |
| 1767 | err = 0; |
| 1768 | goto done; |
| 1769 | |
| 1770 | case BT_CONNECTED: |
| 1771 | /* Already connected */ |
| 1772 | err = -EISCONN; |
| 1773 | goto done; |
| 1774 | |
| 1775 | case BT_OPEN: |
| 1776 | case BT_BOUND: |
| 1777 | /* Can connect */ |
| 1778 | break; |
| 1779 | |
| 1780 | default: |
| 1781 | err = -EBADFD; |
| 1782 | goto done; |
| 1783 | } |
| 1784 | |
| 1785 | /* Set destination address and psm */ |
Gustavo Padovan | 0797e01 | 2012-05-27 22:27:54 -0300 | [diff] [blame] | 1786 | lock_sock(sk); |
Gustavo F. Padovan | 9219b2a | 2012-01-02 20:08:04 -0200 | [diff] [blame] | 1787 | bacpy(&bt_sk(sk)->dst, dst); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1788 | release_sock(sk); |
| 1789 | |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1790 | chan->psm = psm; |
| 1791 | chan->dcid = cid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1793 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1794 | |
Johan Hedberg | f224ca5 | 2013-04-29 19:35:36 +0300 | [diff] [blame] | 1795 | if (bdaddr_type_is_le(dst_type)) |
Andre Guedes | 8e9f989 | 2012-04-24 21:02:55 -0300 | [diff] [blame] | 1796 | hcon = hci_connect(hdev, LE_LINK, dst, dst_type, |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 1797 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1798 | else |
Andre Guedes | 8e9f989 | 2012-04-24 21:02:55 -0300 | [diff] [blame] | 1799 | hcon = hci_connect(hdev, ACL_LINK, dst, dst_type, |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 1800 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1801 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1802 | if (IS_ERR(hcon)) { |
| 1803 | err = PTR_ERR(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | goto done; |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1805 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 1807 | conn = l2cap_conn_add(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | if (!conn) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 1809 | hci_conn_drop(hcon); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1810 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | goto done; |
| 1812 | } |
| 1813 | |
Johan Hedberg | 141d570 | 2013-04-29 19:35:37 +0300 | [diff] [blame^] | 1814 | if (cid && __l2cap_get_chan_by_dcid(conn, cid)) { |
| 1815 | hci_conn_drop(hcon); |
| 1816 | err = -EBUSY; |
| 1817 | goto done; |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1818 | } |
| 1819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | /* Update source addr of the socket */ |
| 1821 | bacpy(src, conn->src); |
| 1822 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1823 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1824 | l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1825 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1826 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1827 | l2cap_state_change(chan, BT_CONNECT); |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1828 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | |
| 1830 | if (hcon->state == BT_CONNECTED) { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1831 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1832 | __clear_chan_timer(chan); |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1833 | if (l2cap_chan_check_security(chan)) |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1834 | l2cap_state_change(chan, BT_CONNECTED); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1835 | } else |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1836 | l2cap_do_start(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1839 | err = 0; |
| 1840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1842 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1843 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | hci_dev_put(hdev); |
| 1845 | return err; |
| 1846 | } |
| 1847 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1848 | int __l2cap_wait_ack(struct sock *sk) |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1849 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1850 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1851 | DECLARE_WAITQUEUE(wait, current); |
| 1852 | int err = 0; |
| 1853 | int timeo = HZ/5; |
| 1854 | |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1855 | add_wait_queue(sk_sleep(sk), &wait); |
Peter Hurley | a71a0cf | 2011-07-25 18:36:26 -0400 | [diff] [blame] | 1856 | set_current_state(TASK_INTERRUPTIBLE); |
| 1857 | while (chan->unacked_frames > 0 && chan->conn) { |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1858 | if (!timeo) |
| 1859 | timeo = HZ/5; |
| 1860 | |
| 1861 | if (signal_pending(current)) { |
| 1862 | err = sock_intr_errno(timeo); |
| 1863 | break; |
| 1864 | } |
| 1865 | |
| 1866 | release_sock(sk); |
| 1867 | timeo = schedule_timeout(timeo); |
| 1868 | lock_sock(sk); |
Peter Hurley | a71a0cf | 2011-07-25 18:36:26 -0400 | [diff] [blame] | 1869 | set_current_state(TASK_INTERRUPTIBLE); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1870 | |
| 1871 | err = sock_error(sk); |
| 1872 | if (err) |
| 1873 | break; |
| 1874 | } |
| 1875 | set_current_state(TASK_RUNNING); |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1876 | remove_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1877 | return err; |
| 1878 | } |
| 1879 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1880 | static void l2cap_monitor_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1881 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1882 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1883 | monitor_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1884 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1885 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1886 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1887 | l2cap_chan_lock(chan); |
| 1888 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1889 | if (!chan->conn) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1890 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1891 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1892 | return; |
| 1893 | } |
| 1894 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1895 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1896 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1897 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1898 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1899 | } |
| 1900 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1901 | static void l2cap_retrans_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1902 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1903 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1904 | retrans_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1905 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1906 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1907 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1908 | l2cap_chan_lock(chan); |
| 1909 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1910 | if (!chan->conn) { |
| 1911 | l2cap_chan_unlock(chan); |
| 1912 | l2cap_chan_put(chan); |
| 1913 | return; |
| 1914 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1915 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1916 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1917 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1918 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1919 | } |
| 1920 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 1921 | static void l2cap_streaming_send(struct l2cap_chan *chan, |
| 1922 | struct sk_buff_head *skbs) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1923 | { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1924 | struct sk_buff *skb; |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1925 | struct l2cap_ctrl *control; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1926 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1927 | BT_DBG("chan %p, skbs %p", chan, skbs); |
| 1928 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1929 | if (__chan_is_moving(chan)) |
| 1930 | return; |
| 1931 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1932 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 1933 | |
| 1934 | while (!skb_queue_empty(&chan->tx_q)) { |
| 1935 | |
| 1936 | skb = skb_dequeue(&chan->tx_q); |
| 1937 | |
| 1938 | bt_cb(skb)->control.retries = 1; |
| 1939 | control = &bt_cb(skb)->control; |
| 1940 | |
| 1941 | control->reqseq = 0; |
| 1942 | control->txseq = chan->next_tx_seq; |
| 1943 | |
| 1944 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1945 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1946 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1947 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 1948 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1949 | } |
| 1950 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1951 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1952 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1953 | BT_DBG("Sent txseq %u", control->txseq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1954 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1955 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1956 | chan->frames_sent++; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1957 | } |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1958 | } |
| 1959 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1960 | static int l2cap_ertm_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1961 | { |
| 1962 | struct sk_buff *skb, *tx_skb; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1963 | struct l2cap_ctrl *control; |
| 1964 | int sent = 0; |
| 1965 | |
| 1966 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1967 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1968 | if (chan->state != BT_CONNECTED) |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1969 | return -ENOTCONN; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1970 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 1971 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 1972 | return 0; |
| 1973 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1974 | if (__chan_is_moving(chan)) |
| 1975 | return 0; |
| 1976 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1977 | while (chan->tx_send_head && |
| 1978 | chan->unacked_frames < chan->remote_tx_win && |
| 1979 | chan->tx_state == L2CAP_TX_STATE_XMIT) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1980 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1981 | skb = chan->tx_send_head; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1982 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1983 | bt_cb(skb)->control.retries = 1; |
| 1984 | control = &bt_cb(skb)->control; |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1985 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1986 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1987 | control->final = 1; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1988 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1989 | control->reqseq = chan->buffer_seq; |
| 1990 | chan->last_acked_seq = chan->buffer_seq; |
| 1991 | control->txseq = chan->next_tx_seq; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1992 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1993 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1994 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1995 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1996 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 1997 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1998 | } |
| 1999 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2000 | /* Clone after data has been modified. Data is assumed to be |
| 2001 | read-only (for locking purposes) on cloned sk_buffs. |
| 2002 | */ |
| 2003 | tx_skb = skb_clone(skb, GFP_KERNEL); |
| 2004 | |
| 2005 | if (!tx_skb) |
| 2006 | break; |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 2007 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 2008 | __set_retrans_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2009 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2010 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2011 | chan->unacked_frames++; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 2012 | chan->frames_sent++; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2013 | sent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2014 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2015 | if (skb_queue_is_last(&chan->tx_q, skb)) |
| 2016 | chan->tx_send_head = NULL; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2017 | else |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2018 | chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2019 | |
| 2020 | l2cap_do_send(chan, tx_skb); |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2021 | BT_DBG("Sent txseq %u", control->txseq); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2022 | } |
| 2023 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2024 | BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent, |
| 2025 | chan->unacked_frames, skb_queue_len(&chan->tx_q)); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2026 | |
| 2027 | return sent; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 2028 | } |
| 2029 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2030 | static void l2cap_ertm_resend(struct l2cap_chan *chan) |
| 2031 | { |
| 2032 | struct l2cap_ctrl control; |
| 2033 | struct sk_buff *skb; |
| 2034 | struct sk_buff *tx_skb; |
| 2035 | u16 seq; |
| 2036 | |
| 2037 | BT_DBG("chan %p", chan); |
| 2038 | |
| 2039 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 2040 | return; |
| 2041 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 2042 | if (__chan_is_moving(chan)) |
| 2043 | return; |
| 2044 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2045 | while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) { |
| 2046 | seq = l2cap_seq_list_pop(&chan->retrans_list); |
| 2047 | |
| 2048 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq); |
| 2049 | if (!skb) { |
| 2050 | BT_DBG("Error: Can't retransmit seq %d, frame missing", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2051 | seq); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2052 | continue; |
| 2053 | } |
| 2054 | |
| 2055 | bt_cb(skb)->control.retries++; |
| 2056 | control = bt_cb(skb)->control; |
| 2057 | |
| 2058 | if (chan->max_tx != 0 && |
| 2059 | bt_cb(skb)->control.retries > chan->max_tx) { |
| 2060 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 2061 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2062 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2063 | break; |
| 2064 | } |
| 2065 | |
| 2066 | control.reqseq = chan->buffer_seq; |
| 2067 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
| 2068 | control.final = 1; |
| 2069 | else |
| 2070 | control.final = 0; |
| 2071 | |
| 2072 | if (skb_cloned(skb)) { |
| 2073 | /* Cloned sk_buffs are read-only, so we need a |
| 2074 | * writeable copy |
| 2075 | */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2076 | tx_skb = skb_copy(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2077 | } else { |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2078 | tx_skb = skb_clone(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | if (!tx_skb) { |
| 2082 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2083 | break; |
| 2084 | } |
| 2085 | |
| 2086 | /* Update skb contents */ |
| 2087 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 2088 | put_unaligned_le32(__pack_extended_control(&control), |
| 2089 | tx_skb->data + L2CAP_HDR_SIZE); |
| 2090 | } else { |
| 2091 | put_unaligned_le16(__pack_enhanced_control(&control), |
| 2092 | tx_skb->data + L2CAP_HDR_SIZE); |
| 2093 | } |
| 2094 | |
| 2095 | if (chan->fcs == L2CAP_FCS_CRC16) { |
| 2096 | u16 fcs = crc16(0, (u8 *) tx_skb->data, tx_skb->len); |
| 2097 | put_unaligned_le16(fcs, skb_put(tx_skb, |
| 2098 | L2CAP_FCS_SIZE)); |
| 2099 | } |
| 2100 | |
| 2101 | l2cap_do_send(chan, tx_skb); |
| 2102 | |
| 2103 | BT_DBG("Resent txseq %d", control.txseq); |
| 2104 | |
| 2105 | chan->last_acked_seq = chan->buffer_seq; |
| 2106 | } |
| 2107 | } |
| 2108 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2109 | static void l2cap_retransmit(struct l2cap_chan *chan, |
| 2110 | struct l2cap_ctrl *control) |
| 2111 | { |
| 2112 | BT_DBG("chan %p, control %p", chan, control); |
| 2113 | |
| 2114 | l2cap_seq_list_append(&chan->retrans_list, control->reqseq); |
| 2115 | l2cap_ertm_resend(chan); |
| 2116 | } |
| 2117 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2118 | static void l2cap_retransmit_all(struct l2cap_chan *chan, |
| 2119 | struct l2cap_ctrl *control) |
| 2120 | { |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2121 | struct sk_buff *skb; |
| 2122 | |
| 2123 | BT_DBG("chan %p, control %p", chan, control); |
| 2124 | |
| 2125 | if (control->poll) |
| 2126 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 2127 | |
| 2128 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2129 | |
| 2130 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 2131 | return; |
| 2132 | |
| 2133 | if (chan->unacked_frames) { |
| 2134 | skb_queue_walk(&chan->tx_q, skb) { |
| 2135 | if (bt_cb(skb)->control.txseq == control->reqseq || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2136 | skb == chan->tx_send_head) |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2137 | break; |
| 2138 | } |
| 2139 | |
| 2140 | skb_queue_walk_from(&chan->tx_q, skb) { |
| 2141 | if (skb == chan->tx_send_head) |
| 2142 | break; |
| 2143 | |
| 2144 | l2cap_seq_list_append(&chan->retrans_list, |
| 2145 | bt_cb(skb)->control.txseq); |
| 2146 | } |
| 2147 | |
| 2148 | l2cap_ertm_resend(chan); |
| 2149 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2150 | } |
| 2151 | |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2152 | static void l2cap_send_ack(struct l2cap_chan *chan) |
| 2153 | { |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2154 | struct l2cap_ctrl control; |
| 2155 | u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 2156 | chan->last_acked_seq); |
| 2157 | int threshold; |
| 2158 | |
| 2159 | BT_DBG("chan %p last_acked_seq %d buffer_seq %d", |
| 2160 | chan, chan->last_acked_seq, chan->buffer_seq); |
| 2161 | |
| 2162 | memset(&control, 0, sizeof(control)); |
| 2163 | control.sframe = 1; |
| 2164 | |
| 2165 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
| 2166 | chan->rx_state == L2CAP_RX_STATE_RECV) { |
| 2167 | __clear_ack_timer(chan); |
| 2168 | control.super = L2CAP_SUPER_RNR; |
| 2169 | control.reqseq = chan->buffer_seq; |
| 2170 | l2cap_send_sframe(chan, &control); |
| 2171 | } else { |
| 2172 | if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) { |
| 2173 | l2cap_ertm_send(chan); |
| 2174 | /* If any i-frames were sent, they included an ack */ |
| 2175 | if (chan->buffer_seq == chan->last_acked_seq) |
| 2176 | frames_to_ack = 0; |
| 2177 | } |
| 2178 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2179 | /* Ack now if the window is 3/4ths full. |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2180 | * Calculate without mul or div |
| 2181 | */ |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2182 | threshold = chan->ack_win; |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2183 | threshold += threshold << 1; |
| 2184 | threshold >>= 2; |
| 2185 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2186 | BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack, |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2187 | threshold); |
| 2188 | |
| 2189 | if (frames_to_ack >= threshold) { |
| 2190 | __clear_ack_timer(chan); |
| 2191 | control.super = L2CAP_SUPER_RR; |
| 2192 | control.reqseq = chan->buffer_seq; |
| 2193 | l2cap_send_sframe(chan, &control); |
| 2194 | frames_to_ack = 0; |
| 2195 | } |
| 2196 | |
| 2197 | if (frames_to_ack) |
| 2198 | __set_ack_timer(chan); |
| 2199 | } |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2200 | } |
| 2201 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2202 | static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, |
| 2203 | struct msghdr *msg, int len, |
| 2204 | int count, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | { |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2206 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2207 | struct sk_buff **frag; |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2208 | int sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | |
Gustavo F. Padovan | 59203a2 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 2210 | if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2211 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | |
| 2213 | sent += count; |
| 2214 | len -= count; |
| 2215 | |
| 2216 | /* Continuation fragments (no L2CAP header) */ |
| 2217 | frag = &skb_shinfo(skb)->frag_list; |
| 2218 | while (len) { |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2219 | struct sk_buff *tmp; |
| 2220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | count = min_t(unsigned int, conn->mtu, len); |
| 2222 | |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2223 | tmp = chan->ops->alloc_skb(chan, count, |
| 2224 | msg->msg_flags & MSG_DONTWAIT); |
| 2225 | if (IS_ERR(tmp)) |
| 2226 | return PTR_ERR(tmp); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2227 | |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2228 | *frag = tmp; |
| 2229 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2230 | if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) |
| 2231 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2233 | (*frag)->priority = skb->priority; |
| 2234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | sent += count; |
| 2236 | len -= count; |
| 2237 | |
Gustavo Padovan | 2d0ed3d | 2012-05-11 13:16:12 -0300 | [diff] [blame] | 2238 | skb->len += (*frag)->len; |
| 2239 | skb->data_len += (*frag)->len; |
| 2240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | frag = &(*frag)->next; |
| 2242 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | |
| 2244 | return sent; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2247 | static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2248 | struct msghdr *msg, size_t len, |
| 2249 | u32 priority) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2250 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2251 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2252 | struct sk_buff *skb; |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2253 | int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2254 | struct l2cap_hdr *lh; |
| 2255 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2256 | BT_DBG("chan %p len %zu priority %u", chan, len, priority); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2257 | |
| 2258 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2259 | |
| 2260 | skb = chan->ops->alloc_skb(chan, count + hlen, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2261 | msg->msg_flags & MSG_DONTWAIT); |
| 2262 | if (IS_ERR(skb)) |
| 2263 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2264 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2265 | skb->priority = priority; |
| 2266 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2267 | /* Create L2CAP header */ |
| 2268 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2269 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | daf6a78c | 2012-05-03 10:55:52 +0300 | [diff] [blame] | 2270 | lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE); |
| 2271 | put_unaligned(chan->psm, skb_put(skb, L2CAP_PSMLEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2272 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2273 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2274 | if (unlikely(err < 0)) { |
| 2275 | kfree_skb(skb); |
| 2276 | return ERR_PTR(err); |
| 2277 | } |
| 2278 | return skb; |
| 2279 | } |
| 2280 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2281 | static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2282 | struct msghdr *msg, size_t len, |
| 2283 | u32 priority) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2284 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2285 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2286 | struct sk_buff *skb; |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2287 | int err, count; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2288 | struct l2cap_hdr *lh; |
| 2289 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2290 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2291 | |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2292 | count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2293 | |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2294 | skb = chan->ops->alloc_skb(chan, count + L2CAP_HDR_SIZE, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2295 | msg->msg_flags & MSG_DONTWAIT); |
| 2296 | if (IS_ERR(skb)) |
| 2297 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2298 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2299 | skb->priority = priority; |
| 2300 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2301 | /* Create L2CAP header */ |
| 2302 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2303 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo Padovan | 6ff9b5e | 2012-05-02 11:56:17 -0300 | [diff] [blame] | 2304 | lh->len = cpu_to_le16(len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2305 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2306 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2307 | if (unlikely(err < 0)) { |
| 2308 | kfree_skb(skb); |
| 2309 | return ERR_PTR(err); |
| 2310 | } |
| 2311 | return skb; |
| 2312 | } |
| 2313 | |
Luiz Augusto von Dentz | ab0ff76 | 2011-09-12 20:00:50 +0300 | [diff] [blame] | 2314 | static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2315 | struct msghdr *msg, size_t len, |
| 2316 | u16 sdulen) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2317 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2318 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2319 | struct sk_buff *skb; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2320 | int err, count, hlen; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2321 | struct l2cap_hdr *lh; |
| 2322 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2323 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2324 | |
Gustavo F. Padovan | 0ee0d20 | 2010-05-01 16:15:41 -0300 | [diff] [blame] | 2325 | if (!conn) |
| 2326 | return ERR_PTR(-ENOTCONN); |
| 2327 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2328 | hlen = __ertm_hdr_size(chan); |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2329 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2330 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2331 | hlen += L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2332 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2333 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2334 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2335 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2336 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2337 | |
| 2338 | skb = chan->ops->alloc_skb(chan, count + hlen, |
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 | |
| 2343 | /* Create L2CAP header */ |
| 2344 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2345 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2346 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2347 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2348 | /* Control header is populated later */ |
| 2349 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 2350 | put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 2351 | else |
| 2352 | put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2353 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2354 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2355 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2356 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2357 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2358 | if (unlikely(err < 0)) { |
| 2359 | kfree_skb(skb); |
| 2360 | return ERR_PTR(err); |
| 2361 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2362 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2363 | bt_cb(skb)->control.fcs = chan->fcs; |
Mat Martineau | 3ce3514 | 2012-04-25 16:36:14 -0700 | [diff] [blame] | 2364 | bt_cb(skb)->control.retries = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2365 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | } |
| 2367 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2368 | static int l2cap_segment_sdu(struct l2cap_chan *chan, |
| 2369 | struct sk_buff_head *seg_queue, |
| 2370 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2371 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2372 | struct sk_buff *skb; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2373 | u16 sdu_len; |
| 2374 | size_t pdu_len; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2375 | u8 sar; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2376 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2377 | BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2378 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2379 | /* It is critical that ERTM PDUs fit in a single HCI fragment, |
| 2380 | * so fragmented skbs are not used. The HCI layer's handling |
| 2381 | * of fragmented skbs is not compatible with ERTM's queueing. |
| 2382 | */ |
| 2383 | |
| 2384 | /* PDU size is derived from the HCI MTU */ |
| 2385 | pdu_len = chan->conn->mtu; |
| 2386 | |
Mat Martineau | a549574 | 2012-10-23 15:24:21 -0700 | [diff] [blame] | 2387 | /* Constrain PDU size for BR/EDR connections */ |
| 2388 | if (!chan->hs_hcon) |
| 2389 | pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2390 | |
| 2391 | /* Adjust for largest possible L2CAP overhead. */ |
Gustavo Padovan | 35d401d | 2012-05-25 18:57:05 -0300 | [diff] [blame] | 2392 | if (chan->fcs) |
| 2393 | pdu_len -= L2CAP_FCS_SIZE; |
| 2394 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2395 | pdu_len -= __ertm_hdr_size(chan); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2396 | |
| 2397 | /* Remote device may have requested smaller PDUs */ |
| 2398 | pdu_len = min_t(size_t, pdu_len, chan->remote_mps); |
| 2399 | |
| 2400 | if (len <= pdu_len) { |
| 2401 | sar = L2CAP_SAR_UNSEGMENTED; |
| 2402 | sdu_len = 0; |
| 2403 | pdu_len = len; |
| 2404 | } else { |
| 2405 | sar = L2CAP_SAR_START; |
| 2406 | sdu_len = len; |
| 2407 | pdu_len -= L2CAP_SDULEN_SIZE; |
| 2408 | } |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2409 | |
| 2410 | while (len > 0) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2411 | skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2412 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2413 | if (IS_ERR(skb)) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2414 | __skb_queue_purge(seg_queue); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2415 | return PTR_ERR(skb); |
| 2416 | } |
| 2417 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2418 | bt_cb(skb)->control.sar = sar; |
| 2419 | __skb_queue_tail(seg_queue, skb); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2420 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2421 | len -= pdu_len; |
| 2422 | if (sdu_len) { |
| 2423 | sdu_len = 0; |
| 2424 | pdu_len += L2CAP_SDULEN_SIZE; |
| 2425 | } |
| 2426 | |
| 2427 | if (len <= pdu_len) { |
| 2428 | sar = L2CAP_SAR_END; |
| 2429 | pdu_len = len; |
| 2430 | } else { |
| 2431 | sar = L2CAP_SAR_CONTINUE; |
| 2432 | } |
| 2433 | } |
| 2434 | |
Gustavo Padovan | f0f6279 | 2012-05-29 13:29:17 -0300 | [diff] [blame] | 2435 | return 0; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2436 | } |
| 2437 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2438 | 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] | 2439 | u32 priority) |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2440 | { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2441 | struct sk_buff *skb; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2442 | int err; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2443 | struct sk_buff_head seg_queue; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2444 | |
| 2445 | /* Connectionless channel */ |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2446 | if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2447 | skb = l2cap_create_connless_pdu(chan, msg, len, priority); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2448 | if (IS_ERR(skb)) |
| 2449 | return PTR_ERR(skb); |
| 2450 | |
| 2451 | l2cap_do_send(chan, skb); |
| 2452 | return len; |
| 2453 | } |
| 2454 | |
| 2455 | switch (chan->mode) { |
| 2456 | case L2CAP_MODE_BASIC: |
| 2457 | /* Check outgoing MTU */ |
| 2458 | if (len > chan->omtu) |
| 2459 | return -EMSGSIZE; |
| 2460 | |
| 2461 | /* Create a basic PDU */ |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2462 | skb = l2cap_create_basic_pdu(chan, msg, len, priority); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2463 | if (IS_ERR(skb)) |
| 2464 | return PTR_ERR(skb); |
| 2465 | |
| 2466 | l2cap_do_send(chan, skb); |
| 2467 | err = len; |
| 2468 | break; |
| 2469 | |
| 2470 | case L2CAP_MODE_ERTM: |
| 2471 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2472 | /* Check outgoing MTU */ |
| 2473 | if (len > chan->omtu) { |
| 2474 | err = -EMSGSIZE; |
| 2475 | break; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2476 | } |
| 2477 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2478 | __skb_queue_head_init(&seg_queue); |
| 2479 | |
| 2480 | /* Do segmentation before calling in to the state machine, |
| 2481 | * since it's possible to block while waiting for memory |
| 2482 | * allocation. |
| 2483 | */ |
| 2484 | err = l2cap_segment_sdu(chan, &seg_queue, msg, len); |
| 2485 | |
| 2486 | /* The channel could have been closed while segmenting, |
| 2487 | * check that it is still connected. |
| 2488 | */ |
| 2489 | if (chan->state != BT_CONNECTED) { |
| 2490 | __skb_queue_purge(&seg_queue); |
| 2491 | err = -ENOTCONN; |
| 2492 | } |
| 2493 | |
| 2494 | if (err) |
| 2495 | break; |
| 2496 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2497 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2498 | l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2499 | else |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2500 | l2cap_streaming_send(chan, &seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2501 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2502 | err = len; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2503 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2504 | /* If the skbs were not queued for sending, they'll still be in |
| 2505 | * seg_queue and need to be purged. |
| 2506 | */ |
| 2507 | __skb_queue_purge(&seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2508 | break; |
| 2509 | |
| 2510 | default: |
| 2511 | BT_DBG("bad state %1.1x", chan->mode); |
| 2512 | err = -EBADFD; |
| 2513 | } |
| 2514 | |
| 2515 | return err; |
| 2516 | } |
| 2517 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2518 | static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq) |
| 2519 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2520 | struct l2cap_ctrl control; |
| 2521 | u16 seq; |
| 2522 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2523 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2524 | |
| 2525 | memset(&control, 0, sizeof(control)); |
| 2526 | control.sframe = 1; |
| 2527 | control.super = L2CAP_SUPER_SREJ; |
| 2528 | |
| 2529 | for (seq = chan->expected_tx_seq; seq != txseq; |
| 2530 | seq = __next_seq(chan, seq)) { |
| 2531 | if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) { |
| 2532 | control.reqseq = seq; |
| 2533 | l2cap_send_sframe(chan, &control); |
| 2534 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | chan->expected_tx_seq = __next_seq(chan, txseq); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | static void l2cap_send_srej_tail(struct l2cap_chan *chan) |
| 2542 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2543 | struct l2cap_ctrl control; |
| 2544 | |
| 2545 | BT_DBG("chan %p", chan); |
| 2546 | |
| 2547 | if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR) |
| 2548 | return; |
| 2549 | |
| 2550 | memset(&control, 0, sizeof(control)); |
| 2551 | control.sframe = 1; |
| 2552 | control.super = L2CAP_SUPER_SREJ; |
| 2553 | control.reqseq = chan->srej_list.tail; |
| 2554 | l2cap_send_sframe(chan, &control); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq) |
| 2558 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2559 | struct l2cap_ctrl control; |
| 2560 | u16 initial_head; |
| 2561 | u16 seq; |
| 2562 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2563 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2564 | |
| 2565 | memset(&control, 0, sizeof(control)); |
| 2566 | control.sframe = 1; |
| 2567 | control.super = L2CAP_SUPER_SREJ; |
| 2568 | |
| 2569 | /* Capture initial list head to allow only one pass through the list. */ |
| 2570 | initial_head = chan->srej_list.head; |
| 2571 | |
| 2572 | do { |
| 2573 | seq = l2cap_seq_list_pop(&chan->srej_list); |
| 2574 | if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR) |
| 2575 | break; |
| 2576 | |
| 2577 | control.reqseq = seq; |
| 2578 | l2cap_send_sframe(chan, &control); |
| 2579 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2580 | } while (chan->srej_list.head != initial_head); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2581 | } |
| 2582 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2583 | static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 2584 | { |
| 2585 | struct sk_buff *acked_skb; |
| 2586 | u16 ackseq; |
| 2587 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2588 | BT_DBG("chan %p, reqseq %u", chan, reqseq); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2589 | |
| 2590 | if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq) |
| 2591 | return; |
| 2592 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2593 | BT_DBG("expected_ack_seq %u, unacked_frames %u", |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2594 | chan->expected_ack_seq, chan->unacked_frames); |
| 2595 | |
| 2596 | for (ackseq = chan->expected_ack_seq; ackseq != reqseq; |
| 2597 | ackseq = __next_seq(chan, ackseq)) { |
| 2598 | |
| 2599 | acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq); |
| 2600 | if (acked_skb) { |
| 2601 | skb_unlink(acked_skb, &chan->tx_q); |
| 2602 | kfree_skb(acked_skb); |
| 2603 | chan->unacked_frames--; |
| 2604 | } |
| 2605 | } |
| 2606 | |
| 2607 | chan->expected_ack_seq = reqseq; |
| 2608 | |
| 2609 | if (chan->unacked_frames == 0) |
| 2610 | __clear_retrans_timer(chan); |
| 2611 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2612 | BT_DBG("unacked_frames %u", chan->unacked_frames); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan) |
| 2616 | { |
| 2617 | BT_DBG("chan %p", chan); |
| 2618 | |
| 2619 | chan->expected_tx_seq = chan->buffer_seq; |
| 2620 | l2cap_seq_list_clear(&chan->srej_list); |
| 2621 | skb_queue_purge(&chan->srej_q); |
| 2622 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 2623 | } |
| 2624 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2625 | static void l2cap_tx_state_xmit(struct l2cap_chan *chan, |
| 2626 | struct l2cap_ctrl *control, |
| 2627 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2628 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2629 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2630 | event); |
| 2631 | |
| 2632 | switch (event) { |
| 2633 | case L2CAP_EV_DATA_REQUEST: |
| 2634 | if (chan->tx_send_head == NULL) |
| 2635 | chan->tx_send_head = skb_peek(skbs); |
| 2636 | |
| 2637 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2638 | l2cap_ertm_send(chan); |
| 2639 | break; |
| 2640 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2641 | BT_DBG("Enter LOCAL_BUSY"); |
| 2642 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2643 | |
| 2644 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2645 | /* The SREJ_SENT state must be aborted if we are to |
| 2646 | * enter the LOCAL_BUSY state. |
| 2647 | */ |
| 2648 | l2cap_abort_rx_srej_sent(chan); |
| 2649 | } |
| 2650 | |
| 2651 | l2cap_send_ack(chan); |
| 2652 | |
| 2653 | break; |
| 2654 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2655 | BT_DBG("Exit LOCAL_BUSY"); |
| 2656 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2657 | |
| 2658 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2659 | struct l2cap_ctrl local_control; |
| 2660 | |
| 2661 | memset(&local_control, 0, sizeof(local_control)); |
| 2662 | local_control.sframe = 1; |
| 2663 | local_control.super = L2CAP_SUPER_RR; |
| 2664 | local_control.poll = 1; |
| 2665 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2666 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2667 | |
| 2668 | chan->retry_count = 1; |
| 2669 | __set_monitor_timer(chan); |
| 2670 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2671 | } |
| 2672 | break; |
| 2673 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2674 | l2cap_process_reqseq(chan, control->reqseq); |
| 2675 | break; |
| 2676 | case L2CAP_EV_EXPLICIT_POLL: |
| 2677 | l2cap_send_rr_or_rnr(chan, 1); |
| 2678 | chan->retry_count = 1; |
| 2679 | __set_monitor_timer(chan); |
| 2680 | __clear_ack_timer(chan); |
| 2681 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2682 | break; |
| 2683 | case L2CAP_EV_RETRANS_TO: |
| 2684 | l2cap_send_rr_or_rnr(chan, 1); |
| 2685 | chan->retry_count = 1; |
| 2686 | __set_monitor_timer(chan); |
| 2687 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2688 | break; |
| 2689 | case L2CAP_EV_RECV_FBIT: |
| 2690 | /* Nothing to process */ |
| 2691 | break; |
| 2692 | default: |
| 2693 | break; |
| 2694 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2695 | } |
| 2696 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2697 | static void l2cap_tx_state_wait_f(struct l2cap_chan *chan, |
| 2698 | struct l2cap_ctrl *control, |
| 2699 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2700 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2701 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2702 | event); |
| 2703 | |
| 2704 | switch (event) { |
| 2705 | case L2CAP_EV_DATA_REQUEST: |
| 2706 | if (chan->tx_send_head == NULL) |
| 2707 | chan->tx_send_head = skb_peek(skbs); |
| 2708 | /* Queue data, but don't send. */ |
| 2709 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2710 | break; |
| 2711 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2712 | BT_DBG("Enter LOCAL_BUSY"); |
| 2713 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2714 | |
| 2715 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2716 | /* The SREJ_SENT state must be aborted if we are to |
| 2717 | * enter the LOCAL_BUSY state. |
| 2718 | */ |
| 2719 | l2cap_abort_rx_srej_sent(chan); |
| 2720 | } |
| 2721 | |
| 2722 | l2cap_send_ack(chan); |
| 2723 | |
| 2724 | break; |
| 2725 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2726 | BT_DBG("Exit LOCAL_BUSY"); |
| 2727 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2728 | |
| 2729 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2730 | struct l2cap_ctrl local_control; |
| 2731 | memset(&local_control, 0, sizeof(local_control)); |
| 2732 | local_control.sframe = 1; |
| 2733 | local_control.super = L2CAP_SUPER_RR; |
| 2734 | local_control.poll = 1; |
| 2735 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2736 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2737 | |
| 2738 | chan->retry_count = 1; |
| 2739 | __set_monitor_timer(chan); |
| 2740 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2741 | } |
| 2742 | break; |
| 2743 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2744 | l2cap_process_reqseq(chan, control->reqseq); |
| 2745 | |
| 2746 | /* Fall through */ |
| 2747 | |
| 2748 | case L2CAP_EV_RECV_FBIT: |
| 2749 | if (control && control->final) { |
| 2750 | __clear_monitor_timer(chan); |
| 2751 | if (chan->unacked_frames > 0) |
| 2752 | __set_retrans_timer(chan); |
| 2753 | chan->retry_count = 0; |
| 2754 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 2755 | BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state); |
| 2756 | } |
| 2757 | break; |
| 2758 | case L2CAP_EV_EXPLICIT_POLL: |
| 2759 | /* Ignore */ |
| 2760 | break; |
| 2761 | case L2CAP_EV_MONITOR_TO: |
| 2762 | if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) { |
| 2763 | l2cap_send_rr_or_rnr(chan, 1); |
| 2764 | __set_monitor_timer(chan); |
| 2765 | chan->retry_count++; |
| 2766 | } else { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 2767 | l2cap_send_disconn_req(chan, ECONNABORTED); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2768 | } |
| 2769 | break; |
| 2770 | default: |
| 2771 | break; |
| 2772 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2773 | } |
| 2774 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2775 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 2776 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2777 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2778 | BT_DBG("chan %p, control %p, skbs %p, event %d, state %d", |
| 2779 | chan, control, skbs, event, chan->tx_state); |
| 2780 | |
| 2781 | switch (chan->tx_state) { |
| 2782 | case L2CAP_TX_STATE_XMIT: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2783 | l2cap_tx_state_xmit(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2784 | break; |
| 2785 | case L2CAP_TX_STATE_WAIT_F: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2786 | l2cap_tx_state_wait_f(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2787 | break; |
| 2788 | default: |
| 2789 | /* Ignore event */ |
| 2790 | break; |
| 2791 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2792 | } |
| 2793 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2794 | static void l2cap_pass_to_tx(struct l2cap_chan *chan, |
| 2795 | struct l2cap_ctrl *control) |
| 2796 | { |
| 2797 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2798 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT); |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2799 | } |
| 2800 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2801 | static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan, |
| 2802 | struct l2cap_ctrl *control) |
| 2803 | { |
| 2804 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2805 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2806 | } |
| 2807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | /* Copy frame to all raw sockets on that connection */ |
| 2809 | static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) |
| 2810 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | struct sk_buff *nskb; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2812 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | |
| 2814 | BT_DBG("conn %p", conn); |
| 2815 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2816 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2817 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2818 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2819 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2820 | if (chan->chan_type != L2CAP_CHAN_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | continue; |
| 2822 | |
| 2823 | /* Don't send frame to the socket it came from */ |
| 2824 | if (skb->sk == sk) |
| 2825 | continue; |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2826 | nskb = skb_clone(skb, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 2827 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | continue; |
| 2829 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 2830 | if (chan->ops->recv(chan, nskb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | kfree_skb(nskb); |
| 2832 | } |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2833 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2834 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | /* ---- L2CAP signalling commands ---- */ |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2838 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code, |
| 2839 | u8 ident, u16 dlen, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | { |
| 2841 | struct sk_buff *skb, **frag; |
| 2842 | struct l2cap_cmd_hdr *cmd; |
| 2843 | struct l2cap_hdr *lh; |
| 2844 | int len, count; |
| 2845 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2846 | BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u", |
| 2847 | conn, code, ident, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | |
Anderson Lizardo | 300b962 | 2013-06-02 16:30:40 -0400 | [diff] [blame] | 2849 | if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE) |
| 2850 | return NULL; |
| 2851 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; |
| 2853 | count = min_t(unsigned int, conn->mtu, len); |
| 2854 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2855 | skb = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | if (!skb) |
| 2857 | return NULL; |
| 2858 | |
| 2859 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2860 | lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2861 | |
| 2862 | if (conn->hcon->type == LE_LINK) |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 2863 | lh->cid = __constant_cpu_to_le16(L2CAP_CID_LE_SIGNALING); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2864 | else |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 2865 | lh->cid = __constant_cpu_to_le16(L2CAP_CID_SIGNALING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | |
| 2867 | cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); |
| 2868 | cmd->code = code; |
| 2869 | cmd->ident = ident; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2870 | cmd->len = cpu_to_le16(dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | |
| 2872 | if (dlen) { |
| 2873 | count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; |
| 2874 | memcpy(skb_put(skb, count), data, count); |
| 2875 | data += count; |
| 2876 | } |
| 2877 | |
| 2878 | len -= skb->len; |
| 2879 | |
| 2880 | /* Continuation fragments (no L2CAP header) */ |
| 2881 | frag = &skb_shinfo(skb)->frag_list; |
| 2882 | while (len) { |
| 2883 | count = min_t(unsigned int, conn->mtu, len); |
| 2884 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2885 | *frag = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2886 | if (!*frag) |
| 2887 | goto fail; |
| 2888 | |
| 2889 | memcpy(skb_put(*frag, count), data, count); |
| 2890 | |
| 2891 | len -= count; |
| 2892 | data += count; |
| 2893 | |
| 2894 | frag = &(*frag)->next; |
| 2895 | } |
| 2896 | |
| 2897 | return skb; |
| 2898 | |
| 2899 | fail: |
| 2900 | kfree_skb(skb); |
| 2901 | return NULL; |
| 2902 | } |
| 2903 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2904 | static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, |
| 2905 | unsigned long *val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | { |
| 2907 | struct l2cap_conf_opt *opt = *ptr; |
| 2908 | int len; |
| 2909 | |
| 2910 | len = L2CAP_CONF_OPT_SIZE + opt->len; |
| 2911 | *ptr += len; |
| 2912 | |
| 2913 | *type = opt->type; |
| 2914 | *olen = opt->len; |
| 2915 | |
| 2916 | switch (opt->len) { |
| 2917 | case 1: |
| 2918 | *val = *((u8 *) opt->val); |
| 2919 | break; |
| 2920 | |
| 2921 | case 2: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2922 | *val = get_unaligned_le16(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | break; |
| 2924 | |
| 2925 | case 4: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2926 | *val = get_unaligned_le32(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2927 | break; |
| 2928 | |
| 2929 | default: |
| 2930 | *val = (unsigned long) opt->val; |
| 2931 | break; |
| 2932 | } |
| 2933 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2934 | 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] | 2935 | return len; |
| 2936 | } |
| 2937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2938 | static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) |
| 2939 | { |
| 2940 | struct l2cap_conf_opt *opt = *ptr; |
| 2941 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2942 | BT_DBG("type 0x%2.2x len %u val 0x%lx", type, len, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | |
| 2944 | opt->type = type; |
| 2945 | opt->len = len; |
| 2946 | |
| 2947 | switch (len) { |
| 2948 | case 1: |
| 2949 | *((u8 *) opt->val) = val; |
| 2950 | break; |
| 2951 | |
| 2952 | case 2: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 2953 | put_unaligned_le16(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | break; |
| 2955 | |
| 2956 | case 4: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 2957 | put_unaligned_le32(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | break; |
| 2959 | |
| 2960 | default: |
| 2961 | memcpy(opt->val, (void *) val, len); |
| 2962 | break; |
| 2963 | } |
| 2964 | |
| 2965 | *ptr += L2CAP_CONF_OPT_SIZE + len; |
| 2966 | } |
| 2967 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2968 | static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) |
| 2969 | { |
| 2970 | struct l2cap_conf_efs efs; |
| 2971 | |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 2972 | switch (chan->mode) { |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2973 | case L2CAP_MODE_ERTM: |
| 2974 | efs.id = chan->local_id; |
| 2975 | efs.stype = chan->local_stype; |
| 2976 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 2977 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 2978 | efs.acc_lat = __constant_cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 2979 | efs.flush_to = __constant_cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2980 | break; |
| 2981 | |
| 2982 | case L2CAP_MODE_STREAMING: |
| 2983 | efs.id = 1; |
| 2984 | efs.stype = L2CAP_SERV_BESTEFFORT; |
| 2985 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 2986 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
| 2987 | efs.acc_lat = 0; |
| 2988 | efs.flush_to = 0; |
| 2989 | break; |
| 2990 | |
| 2991 | default: |
| 2992 | return; |
| 2993 | } |
| 2994 | |
| 2995 | l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 2996 | (unsigned long) &efs); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2997 | } |
| 2998 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 2999 | static void l2cap_ack_timeout(struct work_struct *work) |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3000 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3001 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3002 | ack_timer.work); |
| 3003 | u16 frames_to_ack; |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3004 | |
Gustavo F. Padovan | 2fb9b3d | 2011-12-22 16:56:05 -0200 | [diff] [blame] | 3005 | BT_DBG("chan %p", chan); |
| 3006 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3007 | l2cap_chan_lock(chan); |
| 3008 | |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3009 | frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 3010 | chan->last_acked_seq); |
| 3011 | |
| 3012 | if (frames_to_ack) |
| 3013 | l2cap_send_rr_or_rnr(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3014 | |
| 3015 | l2cap_chan_unlock(chan); |
Szymon Janc | 09bfb2e | 2012-01-11 10:59:49 +0100 | [diff] [blame] | 3016 | l2cap_chan_put(chan); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3017 | } |
| 3018 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 3019 | int l2cap_ertm_init(struct l2cap_chan *chan) |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3020 | { |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3021 | int err; |
| 3022 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3023 | chan->next_tx_seq = 0; |
| 3024 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3025 | chan->expected_ack_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3026 | chan->unacked_frames = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3027 | chan->buffer_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3028 | chan->frames_sent = 0; |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3029 | chan->last_acked_seq = 0; |
| 3030 | chan->sdu = NULL; |
| 3031 | chan->sdu_last_frag = NULL; |
| 3032 | chan->sdu_len = 0; |
| 3033 | |
Mat Martineau | d34c34f | 2012-05-14 14:49:27 -0700 | [diff] [blame] | 3034 | skb_queue_head_init(&chan->tx_q); |
| 3035 | |
Mat Martineau | 0833328 | 2012-10-23 15:24:06 -0700 | [diff] [blame] | 3036 | chan->local_amp_id = 0; |
| 3037 | chan->move_id = 0; |
| 3038 | chan->move_state = L2CAP_MOVE_STABLE; |
| 3039 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 3040 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3041 | if (chan->mode != L2CAP_MODE_ERTM) |
| 3042 | return 0; |
| 3043 | |
| 3044 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 3045 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3046 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3047 | INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); |
| 3048 | INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); |
| 3049 | INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3050 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3051 | skb_queue_head_init(&chan->srej_q); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3052 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3053 | err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win); |
| 3054 | if (err < 0) |
| 3055 | return err; |
| 3056 | |
Mat Martineau | 9dc9aff | 2012-05-17 16:20:14 -0700 | [diff] [blame] | 3057 | err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win); |
| 3058 | if (err < 0) |
| 3059 | l2cap_seq_list_free(&chan->srej_list); |
| 3060 | |
| 3061 | return err; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3062 | } |
| 3063 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3064 | static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) |
| 3065 | { |
| 3066 | switch (mode) { |
| 3067 | case L2CAP_MODE_STREAMING: |
| 3068 | case L2CAP_MODE_ERTM: |
| 3069 | if (l2cap_mode_supported(mode, remote_feat_mask)) |
| 3070 | return mode; |
| 3071 | /* fall through */ |
| 3072 | default: |
| 3073 | return L2CAP_MODE_BASIC; |
| 3074 | } |
| 3075 | } |
| 3076 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3077 | static inline bool __l2cap_ews_supported(struct l2cap_chan *chan) |
| 3078 | { |
| 3079 | return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; |
| 3080 | } |
| 3081 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3082 | static inline bool __l2cap_efs_supported(struct l2cap_chan *chan) |
| 3083 | { |
| 3084 | return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW; |
| 3085 | } |
| 3086 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3087 | static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan, |
| 3088 | struct l2cap_conf_rfc *rfc) |
| 3089 | { |
| 3090 | if (chan->local_amp_id && chan->hs_hcon) { |
| 3091 | u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to; |
| 3092 | |
| 3093 | /* Class 1 devices have must have ERTM timeouts |
| 3094 | * exceeding the Link Supervision Timeout. The |
| 3095 | * default Link Supervision Timeout for AMP |
| 3096 | * controllers is 10 seconds. |
| 3097 | * |
| 3098 | * Class 1 devices use 0xffffffff for their |
| 3099 | * best-effort flush timeout, so the clamping logic |
| 3100 | * will result in a timeout that meets the above |
| 3101 | * requirement. ERTM timeouts are 16-bit values, so |
| 3102 | * the maximum timeout is 65.535 seconds. |
| 3103 | */ |
| 3104 | |
| 3105 | /* Convert timeout to milliseconds and round */ |
| 3106 | ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000); |
| 3107 | |
| 3108 | /* This is the recommended formula for class 2 devices |
| 3109 | * that start ERTM timers when packets are sent to the |
| 3110 | * controller. |
| 3111 | */ |
| 3112 | ertm_to = 3 * ertm_to + 500; |
| 3113 | |
| 3114 | if (ertm_to > 0xffff) |
| 3115 | ertm_to = 0xffff; |
| 3116 | |
| 3117 | rfc->retrans_timeout = cpu_to_le16((u16) ertm_to); |
| 3118 | rfc->monitor_timeout = rfc->retrans_timeout; |
| 3119 | } else { |
| 3120 | rfc->retrans_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); |
| 3121 | rfc->monitor_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); |
| 3122 | } |
| 3123 | } |
| 3124 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3125 | static inline void l2cap_txwin_setup(struct l2cap_chan *chan) |
| 3126 | { |
| 3127 | if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3128 | __l2cap_ews_supported(chan)) { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3129 | /* use extended control field */ |
| 3130 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3131 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
| 3132 | } else { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3133 | chan->tx_win = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3134 | L2CAP_DEFAULT_TX_WINDOW); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3135 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
| 3136 | } |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3137 | chan->ack_win = chan->tx_win; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3138 | } |
| 3139 | |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3140 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | struct l2cap_conf_req *req = data; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3143 | struct l2cap_conf_rfc rfc = { .mode = chan->mode }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | void *ptr = req->data; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3145 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3147 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3149 | if (chan->num_conf_req || chan->num_conf_rsp) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3150 | goto done; |
| 3151 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3152 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3153 | case L2CAP_MODE_STREAMING: |
| 3154 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3155 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3156 | break; |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3157 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3158 | if (__l2cap_efs_supported(chan)) |
| 3159 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3160 | |
Gustavo F. Padovan | 2ba13ed | 2010-06-09 16:39:05 -0300 | [diff] [blame] | 3161 | /* fall through */ |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3162 | default: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3163 | chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3164 | break; |
| 3165 | } |
| 3166 | |
| 3167 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3168 | if (chan->imtu != L2CAP_DEFAULT_MTU) |
| 3169 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | 7990681c | 2011-01-24 16:01:43 -0200 | [diff] [blame] | 3170 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3171 | switch (chan->mode) { |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3172 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3173 | if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3174 | !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3175 | break; |
| 3176 | |
Gustavo F. Padovan | 6254775 | 2010-06-08 20:05:31 -0300 | [diff] [blame] | 3177 | rfc.mode = L2CAP_MODE_BASIC; |
| 3178 | rfc.txwin_size = 0; |
| 3179 | rfc.max_transmit = 0; |
| 3180 | rfc.retrans_timeout = 0; |
| 3181 | rfc.monitor_timeout = 0; |
| 3182 | rfc.max_pdu_size = 0; |
| 3183 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3184 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3185 | (unsigned long) &rfc); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3186 | break; |
| 3187 | |
| 3188 | case L2CAP_MODE_ERTM: |
| 3189 | rfc.mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3190 | rfc.max_transmit = chan->max_tx; |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3191 | |
| 3192 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3193 | |
| 3194 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3195 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3196 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3197 | rfc.max_pdu_size = cpu_to_le16(size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3198 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3199 | l2cap_txwin_setup(chan); |
| 3200 | |
| 3201 | rfc.txwin_size = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3202 | L2CAP_DEFAULT_TX_WINDOW); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3203 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3204 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3205 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3206 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3207 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3208 | l2cap_add_opt_efs(&ptr, chan); |
| 3209 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3210 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3211 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3212 | chan->tx_win); |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3213 | |
| 3214 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3215 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3216 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3217 | chan->fcs = L2CAP_FCS_NONE; |
| 3218 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3219 | chan->fcs); |
| 3220 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3221 | break; |
| 3222 | |
| 3223 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 273759e | 2012-05-17 20:53:53 -0700 | [diff] [blame] | 3224 | l2cap_txwin_setup(chan); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3225 | rfc.mode = L2CAP_MODE_STREAMING; |
| 3226 | rfc.txwin_size = 0; |
| 3227 | rfc.max_transmit = 0; |
| 3228 | rfc.retrans_timeout = 0; |
| 3229 | rfc.monitor_timeout = 0; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3230 | |
| 3231 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3232 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3233 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3234 | rfc.max_pdu_size = cpu_to_le16(size); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3235 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3236 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3237 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3238 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3239 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3240 | l2cap_add_opt_efs(&ptr, chan); |
| 3241 | |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3242 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3243 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3244 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3245 | chan->fcs = L2CAP_FCS_NONE; |
| 3246 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3247 | chan->fcs); |
| 3248 | } |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3249 | break; |
| 3250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3251 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3252 | req->dcid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 3253 | req->flags = __constant_cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3254 | |
| 3255 | return ptr - data; |
| 3256 | } |
| 3257 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3258 | static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3259 | { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3260 | struct l2cap_conf_rsp *rsp = data; |
| 3261 | void *ptr = rsp->data; |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3262 | void *req = chan->conf_req; |
| 3263 | int len = chan->conf_len; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3264 | int type, hint, olen; |
| 3265 | unsigned long val; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3266 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3267 | struct l2cap_conf_efs efs; |
| 3268 | u8 remote_efs = 0; |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3269 | u16 mtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3270 | u16 result = L2CAP_CONF_SUCCESS; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3271 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3273 | BT_DBG("chan %p", chan); |
Marcel Holtmann | 820ae1b | 2006-11-18 22:15:00 +0100 | [diff] [blame] | 3274 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3275 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3276 | len -= l2cap_get_conf_opt(&req, &type, &olen, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3277 | |
Gustavo F. Padovan | 589d274 | 2009-04-20 01:31:07 -0300 | [diff] [blame] | 3278 | hint = type & L2CAP_CONF_HINT; |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 3279 | type &= L2CAP_CONF_MASK; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3280 | |
| 3281 | switch (type) { |
| 3282 | case L2CAP_CONF_MTU: |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3283 | mtu = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3284 | break; |
| 3285 | |
| 3286 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3287 | chan->flush_to = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3288 | break; |
| 3289 | |
| 3290 | case L2CAP_CONF_QOS: |
| 3291 | break; |
| 3292 | |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3293 | case L2CAP_CONF_RFC: |
| 3294 | if (olen == sizeof(rfc)) |
| 3295 | memcpy(&rfc, (void *) val, olen); |
| 3296 | break; |
| 3297 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3298 | case L2CAP_CONF_FCS: |
| 3299 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3300 | set_bit(CONF_RECV_NO_FCS, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3301 | break; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3302 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3303 | case L2CAP_CONF_EFS: |
| 3304 | remote_efs = 1; |
| 3305 | if (olen == sizeof(efs)) |
| 3306 | memcpy(&efs, (void *) val, olen); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3307 | break; |
| 3308 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3309 | case L2CAP_CONF_EWS: |
| 3310 | if (!enable_hs) |
| 3311 | return -ECONNREFUSED; |
| 3312 | |
| 3313 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
| 3314 | set_bit(CONF_EWS_RECV, &chan->conf_state); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3315 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3316 | chan->remote_tx_win = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3317 | break; |
| 3318 | |
| 3319 | default: |
| 3320 | if (hint) |
| 3321 | break; |
| 3322 | |
| 3323 | result = L2CAP_CONF_UNKNOWN; |
| 3324 | *((u8 *) ptr++) = type; |
| 3325 | break; |
| 3326 | } |
| 3327 | } |
| 3328 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3329 | if (chan->num_conf_rsp || chan->num_conf_req > 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3330 | goto done; |
| 3331 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3332 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3333 | case L2CAP_MODE_STREAMING: |
| 3334 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3335 | if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3336 | chan->mode = l2cap_select_mode(rfc.mode, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3337 | chan->conn->feat_mask); |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3338 | break; |
| 3339 | } |
| 3340 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3341 | if (remote_efs) { |
| 3342 | if (__l2cap_efs_supported(chan)) |
| 3343 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3344 | else |
| 3345 | return -ECONNREFUSED; |
| 3346 | } |
| 3347 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3348 | if (chan->mode != rfc.mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3349 | return -ECONNREFUSED; |
Gustavo F. Padovan | 742e519 | 2010-06-08 19:09:48 -0300 | [diff] [blame] | 3350 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3351 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3355 | if (chan->mode != rfc.mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3356 | result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3357 | rfc.mode = chan->mode; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3358 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3359 | if (chan->num_conf_rsp == 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3360 | return -ECONNREFUSED; |
| 3361 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3362 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3363 | (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3364 | } |
| 3365 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3366 | if (result == L2CAP_CONF_SUCCESS) { |
| 3367 | /* Configure output options and let the other side know |
| 3368 | * which ones we don't like. */ |
| 3369 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3370 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
| 3371 | result = L2CAP_CONF_UNACCEPT; |
| 3372 | else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3373 | chan->omtu = mtu; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3374 | set_bit(CONF_MTU_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3375 | } |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3376 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3377 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3378 | if (remote_efs) { |
| 3379 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3380 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3381 | efs.stype != chan->local_stype) { |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3382 | |
| 3383 | result = L2CAP_CONF_UNACCEPT; |
| 3384 | |
| 3385 | if (chan->num_conf_req >= 1) |
| 3386 | return -ECONNREFUSED; |
| 3387 | |
| 3388 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3389 | sizeof(efs), |
| 3390 | (unsigned long) &efs); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3391 | } else { |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3392 | /* Send PENDING Conf Rsp */ |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3393 | result = L2CAP_CONF_PENDING; |
| 3394 | set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3395 | } |
| 3396 | } |
| 3397 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3398 | switch (rfc.mode) { |
| 3399 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3400 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3401 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3402 | break; |
| 3403 | |
| 3404 | case L2CAP_MODE_ERTM: |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3405 | if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) |
| 3406 | chan->remote_tx_win = rfc.txwin_size; |
| 3407 | else |
| 3408 | rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; |
| 3409 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 3410 | chan->remote_max_tx = rfc.max_transmit; |
Mat Martineau | 86b1b26 | 2010-08-05 15:54:22 -0700 | [diff] [blame] | 3411 | |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3412 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3413 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3414 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3415 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3416 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3417 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3418 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3419 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3420 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3421 | |
| 3422 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3423 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3424 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3425 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3426 | chan->remote_id = efs.id; |
| 3427 | chan->remote_stype = efs.stype; |
| 3428 | chan->remote_msdu = le16_to_cpu(efs.msdu); |
| 3429 | chan->remote_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3430 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3431 | chan->remote_acc_lat = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3432 | le32_to_cpu(efs.acc_lat); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3433 | chan->remote_sdu_itime = |
| 3434 | le32_to_cpu(efs.sdu_itime); |
| 3435 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3436 | sizeof(efs), |
| 3437 | (unsigned long) &efs); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3438 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3439 | break; |
| 3440 | |
| 3441 | case L2CAP_MODE_STREAMING: |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3442 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3443 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3444 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3445 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3446 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3447 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3448 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3449 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3450 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3451 | (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3452 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3453 | break; |
| 3454 | |
| 3455 | default: |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3456 | result = L2CAP_CONF_UNACCEPT; |
| 3457 | |
| 3458 | memset(&rfc, 0, sizeof(rfc)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3459 | rfc.mode = chan->mode; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3460 | } |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3461 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3462 | if (result == L2CAP_CONF_SUCCESS) |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3463 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3464 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3465 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3466 | rsp->result = cpu_to_le16(result); |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 3467 | rsp->flags = __constant_cpu_to_le16(0); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3468 | |
| 3469 | return ptr - data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | } |
| 3471 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3472 | static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, |
| 3473 | void *data, u16 *result) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3474 | { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3475 | struct l2cap_conf_req *req = data; |
| 3476 | void *ptr = req->data; |
| 3477 | int type, olen; |
| 3478 | unsigned long val; |
Mat Martineau | 36e999a | 2011-12-08 17:23:21 -0800 | [diff] [blame] | 3479 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3480 | struct l2cap_conf_efs efs; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3481 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3482 | 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] | 3483 | |
| 3484 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3485 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3486 | |
| 3487 | switch (type) { |
| 3488 | case L2CAP_CONF_MTU: |
| 3489 | if (val < L2CAP_DEFAULT_MIN_MTU) { |
| 3490 | *result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3491 | chan->imtu = L2CAP_DEFAULT_MIN_MTU; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3492 | } else |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3493 | chan->imtu = val; |
| 3494 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3495 | break; |
| 3496 | |
| 3497 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3498 | chan->flush_to = val; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3499 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3500 | 2, chan->flush_to); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3501 | break; |
| 3502 | |
| 3503 | case L2CAP_CONF_RFC: |
| 3504 | if (olen == sizeof(rfc)) |
| 3505 | memcpy(&rfc, (void *)val, olen); |
| 3506 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3507 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3508 | rfc.mode != chan->mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3509 | return -ECONNREFUSED; |
| 3510 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3511 | chan->fcs = 0; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3512 | |
| 3513 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3514 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3515 | break; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3516 | |
| 3517 | case L2CAP_CONF_EWS: |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3518 | chan->ack_win = min_t(u16, val, chan->ack_win); |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3519 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3520 | chan->tx_win); |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3521 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3522 | |
| 3523 | case L2CAP_CONF_EFS: |
| 3524 | if (olen == sizeof(efs)) |
| 3525 | memcpy(&efs, (void *)val, olen); |
| 3526 | |
| 3527 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3528 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3529 | efs.stype != chan->local_stype) |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3530 | return -ECONNREFUSED; |
| 3531 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3532 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs), |
| 3533 | (unsigned long) &efs); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3534 | break; |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3535 | |
| 3536 | case L2CAP_CONF_FCS: |
| 3537 | if (*result == L2CAP_CONF_PENDING) |
| 3538 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3539 | set_bit(CONF_RECV_NO_FCS, |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3540 | &chan->conf_state); |
| 3541 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3542 | } |
| 3543 | } |
| 3544 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3545 | if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3546 | return -ECONNREFUSED; |
| 3547 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3548 | chan->mode = rfc.mode; |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3549 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3550 | if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3551 | switch (rfc.mode) { |
| 3552 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3553 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3554 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 3555 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3556 | if (!test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3557 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3558 | rfc.txwin_size); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3559 | |
| 3560 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3561 | chan->local_msdu = le16_to_cpu(efs.msdu); |
| 3562 | chan->local_sdu_itime = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3563 | le32_to_cpu(efs.sdu_itime); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3564 | chan->local_acc_lat = le32_to_cpu(efs.acc_lat); |
| 3565 | chan->local_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3566 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3567 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3568 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3569 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3570 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3571 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3572 | } |
| 3573 | } |
| 3574 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3575 | req->dcid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 3576 | req->flags = __constant_cpu_to_le16(0); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3577 | |
| 3578 | return ptr - data; |
| 3579 | } |
| 3580 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3581 | static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, |
| 3582 | u16 result, u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3583 | { |
| 3584 | struct l2cap_conf_rsp *rsp = data; |
| 3585 | void *ptr = rsp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3586 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3587 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3588 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3589 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3590 | rsp->result = cpu_to_le16(result); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3591 | rsp->flags = cpu_to_le16(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3592 | |
| 3593 | return ptr - data; |
| 3594 | } |
| 3595 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3596 | void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3597 | { |
| 3598 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3599 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3600 | u8 buf[128]; |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3601 | u8 rsp_code; |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3602 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3603 | rsp.scid = cpu_to_le16(chan->dcid); |
| 3604 | rsp.dcid = cpu_to_le16(chan->scid); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 3605 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS); |
| 3606 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3607 | |
| 3608 | if (chan->hs_hcon) |
| 3609 | rsp_code = L2CAP_CREATE_CHAN_RSP; |
| 3610 | else |
| 3611 | rsp_code = L2CAP_CONN_RSP; |
| 3612 | |
| 3613 | BT_DBG("chan %p rsp_code %u", chan, rsp_code); |
| 3614 | |
| 3615 | l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3616 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3617 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3618 | return; |
| 3619 | |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3620 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3621 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3622 | chan->num_conf_req++; |
| 3623 | } |
| 3624 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3625 | 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] | 3626 | { |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3627 | int type, olen; |
| 3628 | unsigned long val; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3629 | /* Use sane default values in case a misbehaving remote device |
| 3630 | * did not send an RFC or extended window size option. |
| 3631 | */ |
| 3632 | u16 txwin_ext = chan->ack_win; |
| 3633 | struct l2cap_conf_rfc rfc = { |
| 3634 | .mode = chan->mode, |
| 3635 | .retrans_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO), |
| 3636 | .monitor_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO), |
| 3637 | .max_pdu_size = cpu_to_le16(chan->imtu), |
| 3638 | .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW), |
| 3639 | }; |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3640 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3641 | BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3642 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3643 | if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3644 | return; |
| 3645 | |
| 3646 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3647 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3648 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3649 | switch (type) { |
| 3650 | case L2CAP_CONF_RFC: |
| 3651 | if (olen == sizeof(rfc)) |
| 3652 | memcpy(&rfc, (void *)val, olen); |
Szymon Janc | 8f321f8 | 2012-06-08 11:33:33 +0200 | [diff] [blame] | 3653 | break; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3654 | case L2CAP_CONF_EWS: |
| 3655 | txwin_ext = val; |
| 3656 | break; |
| 3657 | } |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3658 | } |
| 3659 | |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3660 | switch (rfc.mode) { |
| 3661 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3662 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3663 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3664 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
| 3665 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3666 | chan->ack_win = min_t(u16, chan->ack_win, txwin_ext); |
| 3667 | else |
| 3668 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3669 | rfc.txwin_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3670 | break; |
| 3671 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3672 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3673 | } |
| 3674 | } |
| 3675 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3676 | static inline int l2cap_command_rej(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3677 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3678 | u8 *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3679 | { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3680 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3681 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3682 | if (cmd_len < sizeof(*rej)) |
| 3683 | return -EPROTO; |
| 3684 | |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3685 | if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3686 | return 0; |
| 3687 | |
| 3688 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3689 | cmd->ident == conn->info_ident) { |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 3690 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3691 | |
| 3692 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3693 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3694 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3695 | l2cap_conn_start(conn); |
| 3696 | } |
| 3697 | |
| 3698 | return 0; |
| 3699 | } |
| 3700 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3701 | static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn, |
| 3702 | struct l2cap_cmd_hdr *cmd, |
| 3703 | u8 *data, u8 rsp_code, u8 amp_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3705 | struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; |
| 3706 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3707 | struct l2cap_chan *chan = NULL, *pchan; |
Nathan Holstein | d793fe8 | 2010-10-15 11:54:02 -0400 | [diff] [blame] | 3708 | struct sock *parent, *sk = NULL; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3709 | int result, status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3710 | |
| 3711 | u16 dcid = 0, scid = __le16_to_cpu(req->scid); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3712 | __le16 psm = req->psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 3714 | 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] | 3715 | |
| 3716 | /* Check if we have socket listening on psm */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 3717 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src, conn->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3718 | if (!pchan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3719 | result = L2CAP_CR_BAD_PSM; |
| 3720 | goto sendresp; |
| 3721 | } |
| 3722 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3723 | parent = pchan->sk; |
| 3724 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3725 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 3726 | lock_sock(parent); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 3727 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3728 | /* Check if the ACL is secure enough (if not SDP) */ |
Andrei Emeltchenko | 2983fd6 | 2012-05-24 15:42:50 +0300 | [diff] [blame] | 3729 | if (psm != __constant_cpu_to_le16(L2CAP_PSM_SDP) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3730 | !hci_conn_check_link_mode(conn->hcon)) { |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 3731 | conn->disc_reason = HCI_ERROR_AUTH_FAILURE; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3732 | result = L2CAP_CR_SEC_BLOCK; |
| 3733 | goto response; |
| 3734 | } |
| 3735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3736 | result = L2CAP_CR_NO_MEM; |
| 3737 | |
Gustavo Padovan | 2dfa100 | 2012-05-27 22:27:58 -0300 | [diff] [blame] | 3738 | /* Check if we already have channel with that dcid */ |
| 3739 | if (__l2cap_get_chan_by_dcid(conn, scid)) |
| 3740 | goto response; |
| 3741 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 3742 | chan = pchan->ops->new_connection(pchan); |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 3743 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3744 | goto response; |
| 3745 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 3746 | sk = chan->sk; |
| 3747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3748 | hci_conn_hold(conn->hcon); |
| 3749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | bacpy(&bt_sk(sk)->src, conn->src); |
| 3751 | bacpy(&bt_sk(sk)->dst, conn->dst); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3752 | chan->psm = psm; |
| 3753 | chan->dcid = scid; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3754 | chan->local_amp_id = amp_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3756 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3757 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3758 | dcid = chan->scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3759 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 3760 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3762 | chan->ident = cmd->ident; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3763 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3764 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 3765 | if (l2cap_chan_check_security(chan)) { |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 3766 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 3767 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3768 | result = L2CAP_CR_PEND; |
| 3769 | status = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 3770 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3771 | } else { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3772 | /* Force pending result for AMP controllers. |
| 3773 | * The connection will succeed after the |
| 3774 | * physical link is up. |
| 3775 | */ |
| 3776 | if (amp_id) { |
| 3777 | __l2cap_state_change(chan, BT_CONNECT2); |
| 3778 | result = L2CAP_CR_PEND; |
| 3779 | } else { |
| 3780 | __l2cap_state_change(chan, BT_CONFIG); |
| 3781 | result = L2CAP_CR_SUCCESS; |
| 3782 | } |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3783 | status = L2CAP_CS_NO_INFO; |
| 3784 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3785 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 3786 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3787 | result = L2CAP_CR_PEND; |
| 3788 | status = L2CAP_CS_AUTHEN_PEND; |
| 3789 | } |
| 3790 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 3791 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3792 | result = L2CAP_CR_PEND; |
| 3793 | status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3794 | } |
| 3795 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | response: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 3797 | release_sock(parent); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3798 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3799 | |
| 3800 | sendresp: |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3801 | rsp.scid = cpu_to_le16(scid); |
| 3802 | rsp.dcid = cpu_to_le16(dcid); |
| 3803 | rsp.result = cpu_to_le16(result); |
| 3804 | rsp.status = cpu_to_le16(status); |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3805 | l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3806 | |
| 3807 | if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { |
| 3808 | struct l2cap_info_req info; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 3809 | info.type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3810 | |
| 3811 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 3812 | conn->info_ident = l2cap_get_ident(conn); |
| 3813 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 3814 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3815 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3816 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 3817 | sizeof(info), &info); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3818 | } |
| 3819 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3820 | if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3821 | result == L2CAP_CR_SUCCESS) { |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3822 | u8 buf[128]; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3823 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3824 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3825 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3826 | chan->num_conf_req++; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3827 | } |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3828 | |
| 3829 | return chan; |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3830 | } |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3831 | |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3832 | static int l2cap_connect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3833 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3834 | { |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3835 | struct hci_dev *hdev = conn->hcon->hdev; |
| 3836 | struct hci_conn *hcon = conn->hcon; |
| 3837 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3838 | if (cmd_len < sizeof(struct l2cap_conn_req)) |
| 3839 | return -EPROTO; |
| 3840 | |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3841 | hci_dev_lock(hdev); |
| 3842 | if (test_bit(HCI_MGMT, &hdev->dev_flags) && |
| 3843 | !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags)) |
| 3844 | mgmt_device_connected(hdev, &hcon->dst, hcon->type, |
| 3845 | hcon->dst_type, 0, NULL, 0, |
| 3846 | hcon->dev_class); |
| 3847 | hci_dev_unlock(hdev); |
| 3848 | |
Gustavo Padovan | 300229f | 2012-10-12 19:40:40 +0800 | [diff] [blame] | 3849 | l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3850 | return 0; |
| 3851 | } |
| 3852 | |
Mat Martineau | 5909cf3 | 2012-10-23 15:24:08 -0700 | [diff] [blame] | 3853 | static int l2cap_connect_create_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3854 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3855 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3856 | { |
| 3857 | struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; |
| 3858 | u16 scid, dcid, result, status; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3859 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3860 | u8 req[128]; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3861 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3862 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3863 | if (cmd_len < sizeof(*rsp)) |
| 3864 | return -EPROTO; |
| 3865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3866 | scid = __le16_to_cpu(rsp->scid); |
| 3867 | dcid = __le16_to_cpu(rsp->dcid); |
| 3868 | result = __le16_to_cpu(rsp->result); |
| 3869 | status = __le16_to_cpu(rsp->status); |
| 3870 | |
Andrei Emeltchenko | 1b009c9 | 2012-02-21 12:54:54 +0200 | [diff] [blame] | 3871 | 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] | 3872 | dcid, scid, result, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3874 | mutex_lock(&conn->chan_lock); |
| 3875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3876 | if (scid) { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3877 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 3878 | if (!chan) { |
| 3879 | err = -EFAULT; |
| 3880 | goto unlock; |
| 3881 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3882 | } else { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3883 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 3884 | if (!chan) { |
| 3885 | err = -EFAULT; |
| 3886 | goto unlock; |
| 3887 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3888 | } |
| 3889 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3890 | err = 0; |
| 3891 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3892 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3894 | switch (result) { |
| 3895 | case L2CAP_CR_SUCCESS: |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 3896 | l2cap_state_change(chan, BT_CONFIG); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3897 | chan->ident = 0; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3898 | chan->dcid = dcid; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3899 | clear_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 3900 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3901 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3902 | break; |
| 3903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3904 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3905 | l2cap_build_conf_req(chan, req), req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3906 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3907 | break; |
| 3908 | |
| 3909 | case L2CAP_CR_PEND: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3910 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | break; |
| 3912 | |
| 3913 | default: |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3914 | l2cap_chan_del(chan, ECONNREFUSED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3915 | break; |
| 3916 | } |
| 3917 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3918 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3919 | |
| 3920 | unlock: |
| 3921 | mutex_unlock(&conn->chan_lock); |
| 3922 | |
| 3923 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3924 | } |
| 3925 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3926 | static inline void set_default_fcs(struct l2cap_chan *chan) |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 3927 | { |
| 3928 | /* FCS is enabled only in ERTM or streaming mode, if one or both |
| 3929 | * sides request it. |
| 3930 | */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3931 | if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3932 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3933 | else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3934 | chan->fcs = L2CAP_FCS_CRC16; |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 3935 | } |
| 3936 | |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 3937 | static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data, |
| 3938 | u8 ident, u16 flags) |
| 3939 | { |
| 3940 | struct l2cap_conn *conn = chan->conn; |
| 3941 | |
| 3942 | BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident, |
| 3943 | flags); |
| 3944 | |
| 3945 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
| 3946 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
| 3947 | |
| 3948 | l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP, |
| 3949 | l2cap_build_conf_rsp(chan, data, |
| 3950 | L2CAP_CONF_SUCCESS, flags), data); |
| 3951 | } |
| 3952 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3953 | static inline int l2cap_config_req(struct l2cap_conn *conn, |
| 3954 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3955 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | { |
| 3957 | struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; |
| 3958 | u16 dcid, flags; |
| 3959 | u8 rsp[64]; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3960 | struct l2cap_chan *chan; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3961 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3962 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3963 | if (cmd_len < sizeof(*req)) |
| 3964 | return -EPROTO; |
| 3965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | dcid = __le16_to_cpu(req->dcid); |
| 3967 | flags = __le16_to_cpu(req->flags); |
| 3968 | |
| 3969 | BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); |
| 3970 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 3971 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3972 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3973 | return -ENOENT; |
| 3974 | |
David S. Miller | 033b114 | 2011-07-21 13:38:42 -0700 | [diff] [blame] | 3975 | if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3976 | struct l2cap_cmd_rej_cid rej; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 3977 | |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 3978 | rej.reason = __constant_cpu_to_le16(L2CAP_REJ_INVALID_CID); |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3979 | rej.scid = cpu_to_le16(chan->scid); |
| 3980 | rej.dcid = cpu_to_le16(chan->dcid); |
| 3981 | |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 3982 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3983 | sizeof(rej), &rej); |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 3984 | goto unlock; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 3985 | } |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 3986 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3987 | /* Reject if config buffer is too small. */ |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3988 | len = cmd_len - sizeof(*req); |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3989 | if (chan->conf_len + len > sizeof(chan->conf_req)) { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3990 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3991 | l2cap_build_conf_rsp(chan, rsp, |
| 3992 | L2CAP_CONF_REJECT, flags), rsp); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3993 | goto unlock; |
| 3994 | } |
| 3995 | |
| 3996 | /* Store config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3997 | memcpy(chan->conf_req + chan->conf_len, req->data, len); |
| 3998 | chan->conf_len += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3999 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4000 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | /* Incomplete config. Send empty response. */ |
| 4002 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4003 | l2cap_build_conf_rsp(chan, rsp, |
| 4004 | L2CAP_CONF_SUCCESS, flags), rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | goto unlock; |
| 4006 | } |
| 4007 | |
| 4008 | /* Complete config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4009 | len = l2cap_parse_conf_req(chan, rsp); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4010 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4011 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4012 | goto unlock; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4013 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4015 | chan->ident = cmd->ident; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4016 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4017 | chan->num_conf_rsp++; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4018 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4019 | /* Reset config buffer. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4020 | chan->conf_len = 0; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4021 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4022 | if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4023 | goto unlock; |
| 4024 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4025 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4026 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4027 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4028 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4029 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4030 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4031 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4032 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4033 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4034 | else |
| 4035 | l2cap_chan_ready(chan); |
| 4036 | |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4037 | goto unlock; |
| 4038 | } |
| 4039 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4040 | if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 4041 | u8 buf[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4043 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4044 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4045 | } |
| 4046 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4047 | /* Got Conf Rsp PENDING from remote side and asume we sent |
| 4048 | Conf Rsp PENDING in the code above */ |
| 4049 | if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 4050 | test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4051 | |
| 4052 | /* check compatibility */ |
| 4053 | |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4054 | /* Send rsp for BR/EDR channel */ |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4055 | if (!chan->hs_hcon) |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4056 | l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags); |
| 4057 | else |
| 4058 | chan->ident = cmd->ident; |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4059 | } |
| 4060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | unlock: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4062 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4063 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4064 | } |
| 4065 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4066 | static inline int l2cap_config_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4067 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4068 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4069 | { |
| 4070 | struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; |
| 4071 | u16 scid, flags, result; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4072 | struct l2cap_chan *chan; |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4073 | int len = cmd_len - sizeof(*rsp); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4074 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4075 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4076 | if (cmd_len < sizeof(*rsp)) |
| 4077 | return -EPROTO; |
| 4078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | scid = __le16_to_cpu(rsp->scid); |
| 4080 | flags = __le16_to_cpu(rsp->flags); |
| 4081 | result = __le16_to_cpu(rsp->result); |
| 4082 | |
Andrei Emeltchenko | 61386cb | 2012-03-12 12:13:07 +0200 | [diff] [blame] | 4083 | BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags, |
| 4084 | result, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4086 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4087 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4088 | return 0; |
| 4089 | |
| 4090 | switch (result) { |
| 4091 | case L2CAP_CONF_SUCCESS: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4092 | l2cap_conf_rfc_get(chan, rsp->data, len); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4093 | clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | break; |
| 4095 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4096 | case L2CAP_CONF_PENDING: |
| 4097 | set_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
| 4098 | |
| 4099 | if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
| 4100 | char buf[64]; |
| 4101 | |
| 4102 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4103 | buf, &result); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4104 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4105 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4106 | goto done; |
| 4107 | } |
| 4108 | |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4109 | if (!chan->hs_hcon) { |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4110 | l2cap_send_efs_conf_rsp(chan, buf, cmd->ident, |
| 4111 | 0); |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 4112 | } else { |
| 4113 | if (l2cap_check_efs(chan)) { |
| 4114 | amp_create_logical_link(chan); |
| 4115 | chan->ident = cmd->ident; |
| 4116 | } |
| 4117 | } |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4118 | } |
| 4119 | goto done; |
| 4120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | case L2CAP_CONF_UNACCEPT: |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4122 | if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4123 | char req[64]; |
| 4124 | |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4125 | if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4126 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4127 | goto done; |
| 4128 | } |
| 4129 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4130 | /* throw out any old stored conf requests */ |
| 4131 | result = L2CAP_CONF_SUCCESS; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 4132 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4133 | req, &result); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4134 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4135 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4136 | goto done; |
| 4137 | } |
| 4138 | |
| 4139 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4140 | L2CAP_CONF_REQ, len, req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4141 | chan->num_conf_req++; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4142 | if (result != L2CAP_CONF_SUCCESS) |
| 4143 | goto done; |
| 4144 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | } |
| 4146 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 4147 | default: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4148 | l2cap_chan_set_err(chan, ECONNRESET); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 4149 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 4150 | __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4151 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | goto done; |
| 4153 | } |
| 4154 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4155 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | goto done; |
| 4157 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4158 | set_bit(CONF_INPUT_DONE, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4160 | if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4161 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4162 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4163 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4164 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4165 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4166 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4167 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4168 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4169 | else |
| 4170 | l2cap_chan_ready(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4174 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4175 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4176 | } |
| 4177 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4178 | static inline int l2cap_disconnect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4179 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4180 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | { |
| 4182 | struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; |
| 4183 | struct l2cap_disconn_rsp rsp; |
| 4184 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4185 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | struct sock *sk; |
| 4187 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4188 | if (cmd_len != sizeof(*req)) |
| 4189 | return -EPROTO; |
| 4190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | scid = __le16_to_cpu(req->scid); |
| 4192 | dcid = __le16_to_cpu(req->dcid); |
| 4193 | |
| 4194 | BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); |
| 4195 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4196 | mutex_lock(&conn->chan_lock); |
| 4197 | |
| 4198 | chan = __l2cap_get_chan_by_scid(conn, dcid); |
| 4199 | if (!chan) { |
| 4200 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4201 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4203 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4204 | l2cap_chan_lock(chan); |
| 4205 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4206 | sk = chan->sk; |
| 4207 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4208 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4209 | rsp.scid = cpu_to_le16(chan->dcid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4210 | l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); |
| 4211 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4212 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | sk->sk_shutdown = SHUTDOWN_MASK; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4214 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4215 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4216 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4217 | l2cap_chan_del(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4218 | |
| 4219 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4220 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4221 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4222 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4223 | |
| 4224 | mutex_unlock(&conn->chan_lock); |
| 4225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | return 0; |
| 4227 | } |
| 4228 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4229 | static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4230 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4231 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4232 | { |
| 4233 | struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; |
| 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 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4237 | if (cmd_len != sizeof(*rsp)) |
| 4238 | return -EPROTO; |
| 4239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4240 | scid = __le16_to_cpu(rsp->scid); |
| 4241 | dcid = __le16_to_cpu(rsp->dcid); |
| 4242 | |
| 4243 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); |
| 4244 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4245 | mutex_lock(&conn->chan_lock); |
| 4246 | |
| 4247 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 4248 | if (!chan) { |
| 4249 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4253 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4254 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4255 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4256 | l2cap_chan_del(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4257 | |
| 4258 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4260 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4261 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4262 | |
| 4263 | mutex_unlock(&conn->chan_lock); |
| 4264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | return 0; |
| 4266 | } |
| 4267 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4268 | static inline int l2cap_information_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4269 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4270 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | { |
| 4272 | struct l2cap_info_req *req = (struct l2cap_info_req *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4273 | u16 type; |
| 4274 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4275 | if (cmd_len != sizeof(*req)) |
| 4276 | return -EPROTO; |
| 4277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4278 | type = __le16_to_cpu(req->type); |
| 4279 | |
| 4280 | BT_DBG("type 0x%4.4x", type); |
| 4281 | |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4282 | if (type == L2CAP_IT_FEAT_MASK) { |
| 4283 | u8 buf[8]; |
Marcel Holtmann | 44dd46d | 2009-05-02 19:09:01 -0700 | [diff] [blame] | 4284 | u32 feat_mask = l2cap_feat_mask; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4285 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4286 | rsp->type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 4287 | rsp->result = __constant_cpu_to_le16(L2CAP_IR_SUCCESS); |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 4288 | if (!disable_ertm) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4289 | feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4290 | | L2CAP_FEAT_FCS; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 4291 | if (enable_hs) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 4292 | feat_mask |= L2CAP_FEAT_EXT_FLOW |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4293 | | L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 4294 | |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 4295 | put_unaligned_le32(feat_mask, rsp->data); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4296 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4297 | buf); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4298 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
| 4299 | u8 buf[12]; |
| 4300 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 4301 | |
| 4302 | if (enable_hs) |
| 4303 | l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; |
| 4304 | else |
| 4305 | l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; |
| 4306 | |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4307 | rsp->type = __constant_cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 4308 | rsp->result = __constant_cpu_to_le16(L2CAP_IR_SUCCESS); |
Andrei Emeltchenko | c6337ea | 2011-10-20 17:02:44 +0300 | [diff] [blame] | 4309 | memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan)); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4310 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4311 | buf); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4312 | } else { |
| 4313 | struct l2cap_info_rsp rsp; |
| 4314 | rsp.type = cpu_to_le16(type); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4315 | rsp.result = __constant_cpu_to_le16(L2CAP_IR_NOTSUPP); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4316 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp), |
| 4317 | &rsp); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4318 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | |
| 4320 | return 0; |
| 4321 | } |
| 4322 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4323 | static inline int l2cap_information_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4324 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4325 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4326 | { |
| 4327 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; |
| 4328 | u16 type, result; |
| 4329 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4330 | if (cmd_len != sizeof(*rsp)) |
| 4331 | return -EPROTO; |
| 4332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | type = __le16_to_cpu(rsp->type); |
| 4334 | result = __le16_to_cpu(rsp->result); |
| 4335 | |
| 4336 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); |
| 4337 | |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4338 | /* L2CAP Info req/rsp are unbound to channels, add extra checks */ |
| 4339 | if (cmd->ident != conn->info_ident || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4340 | conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4341 | return 0; |
| 4342 | |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 4343 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4344 | |
Ville Tervo | adb08ed | 2010-08-04 09:43:33 +0300 | [diff] [blame] | 4345 | if (result != L2CAP_IR_SUCCESS) { |
| 4346 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4347 | conn->info_ident = 0; |
| 4348 | |
| 4349 | l2cap_conn_start(conn); |
| 4350 | |
| 4351 | return 0; |
| 4352 | } |
| 4353 | |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4354 | switch (type) { |
| 4355 | case L2CAP_IT_FEAT_MASK: |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 4356 | conn->feat_mask = get_unaligned_le32(rsp->data); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4357 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 4358 | if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4359 | struct l2cap_info_req req; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4360 | req.type = __constant_cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4361 | |
| 4362 | conn->info_ident = l2cap_get_ident(conn); |
| 4363 | |
| 4364 | l2cap_send_cmd(conn, conn->info_ident, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4365 | L2CAP_INFO_REQ, sizeof(req), &req); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4366 | } else { |
| 4367 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4368 | conn->info_ident = 0; |
| 4369 | |
| 4370 | l2cap_conn_start(conn); |
| 4371 | } |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4372 | break; |
| 4373 | |
| 4374 | case L2CAP_IT_FIXED_CHAN: |
| 4375 | conn->fixed_chan_mask = rsp->data[0]; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4376 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4377 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4378 | |
| 4379 | l2cap_conn_start(conn); |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4380 | break; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4381 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | return 0; |
| 4384 | } |
| 4385 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4386 | static int l2cap_create_channel_req(struct l2cap_conn *conn, |
| 4387 | struct l2cap_cmd_hdr *cmd, |
| 4388 | u16 cmd_len, void *data) |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4389 | { |
| 4390 | struct l2cap_create_chan_req *req = data; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4391 | struct l2cap_create_chan_rsp rsp; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4392 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4393 | struct hci_dev *hdev; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4394 | u16 psm, scid; |
| 4395 | |
| 4396 | if (cmd_len != sizeof(*req)) |
| 4397 | return -EPROTO; |
| 4398 | |
| 4399 | if (!enable_hs) |
| 4400 | return -EINVAL; |
| 4401 | |
| 4402 | psm = le16_to_cpu(req->psm); |
| 4403 | scid = le16_to_cpu(req->scid); |
| 4404 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4405 | 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] | 4406 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4407 | /* For controller id 0 make BR/EDR connection */ |
| 4408 | if (req->amp_id == HCI_BREDR_ID) { |
| 4409 | l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4410 | req->amp_id); |
| 4411 | return 0; |
| 4412 | } |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4413 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4414 | /* Validate AMP controller id */ |
| 4415 | hdev = hci_dev_get(req->amp_id); |
| 4416 | if (!hdev) |
| 4417 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4418 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4419 | if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4420 | hci_dev_put(hdev); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4421 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4425 | req->amp_id); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4426 | if (chan) { |
| 4427 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
| 4428 | struct hci_conn *hs_hcon; |
| 4429 | |
| 4430 | hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, conn->dst); |
| 4431 | if (!hs_hcon) { |
| 4432 | hci_dev_put(hdev); |
| 4433 | return -EFAULT; |
| 4434 | } |
| 4435 | |
| 4436 | BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon); |
| 4437 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4438 | mgr->bredr_chan = chan; |
| 4439 | chan->hs_hcon = hs_hcon; |
Andrei Emeltchenko | fd45bf4 | 2012-11-20 17:16:22 +0200 | [diff] [blame] | 4440 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4441 | conn->mtu = hdev->block_mtu; |
| 4442 | } |
| 4443 | |
| 4444 | hci_dev_put(hdev); |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4445 | |
| 4446 | return 0; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4447 | |
| 4448 | error: |
| 4449 | rsp.dcid = 0; |
| 4450 | rsp.scid = cpu_to_le16(scid); |
| 4451 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_BAD_AMP); |
| 4452 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
| 4453 | |
| 4454 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, |
| 4455 | sizeof(rsp), &rsp); |
| 4456 | |
| 4457 | return -EFAULT; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4458 | } |
| 4459 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4460 | static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id) |
| 4461 | { |
| 4462 | struct l2cap_move_chan_req req; |
| 4463 | u8 ident; |
| 4464 | |
| 4465 | BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id); |
| 4466 | |
| 4467 | ident = l2cap_get_ident(chan->conn); |
| 4468 | chan->ident = ident; |
| 4469 | |
| 4470 | req.icid = cpu_to_le16(chan->scid); |
| 4471 | req.dest_amp_id = dest_amp_id; |
| 4472 | |
| 4473 | l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req), |
| 4474 | &req); |
| 4475 | |
| 4476 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4477 | } |
| 4478 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4479 | 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] | 4480 | { |
| 4481 | struct l2cap_move_chan_rsp rsp; |
| 4482 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4483 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4484 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4485 | rsp.icid = cpu_to_le16(chan->dcid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4486 | rsp.result = cpu_to_le16(result); |
| 4487 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4488 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP, |
| 4489 | sizeof(rsp), &rsp); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4490 | } |
| 4491 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4492 | 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] | 4493 | { |
| 4494 | struct l2cap_move_chan_cfm cfm; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4495 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4496 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4497 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4498 | chan->ident = l2cap_get_ident(chan->conn); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4499 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4500 | cfm.icid = cpu_to_le16(chan->scid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4501 | cfm.result = cpu_to_le16(result); |
| 4502 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4503 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM, |
| 4504 | sizeof(cfm), &cfm); |
| 4505 | |
| 4506 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4507 | } |
| 4508 | |
| 4509 | static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid) |
| 4510 | { |
| 4511 | struct l2cap_move_chan_cfm cfm; |
| 4512 | |
| 4513 | BT_DBG("conn %p, icid 0x%4.4x", conn, icid); |
| 4514 | |
| 4515 | cfm.icid = cpu_to_le16(icid); |
| 4516 | cfm.result = __constant_cpu_to_le16(L2CAP_MC_UNCONFIRMED); |
| 4517 | |
| 4518 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM, |
| 4519 | sizeof(cfm), &cfm); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4520 | } |
| 4521 | |
| 4522 | 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] | 4523 | u16 icid) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4524 | { |
| 4525 | struct l2cap_move_chan_cfm_rsp rsp; |
| 4526 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4527 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4528 | |
| 4529 | rsp.icid = cpu_to_le16(icid); |
| 4530 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); |
| 4531 | } |
| 4532 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 4533 | static void __release_logical_link(struct l2cap_chan *chan) |
| 4534 | { |
| 4535 | chan->hs_hchan = NULL; |
| 4536 | chan->hs_hcon = NULL; |
| 4537 | |
| 4538 | /* Placeholder - release the logical link */ |
| 4539 | } |
| 4540 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4541 | static void l2cap_logical_fail(struct l2cap_chan *chan) |
| 4542 | { |
| 4543 | /* Logical link setup failed */ |
| 4544 | if (chan->state != BT_CONNECTED) { |
| 4545 | /* Create channel failure, disconnect */ |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4546 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4547 | return; |
| 4548 | } |
| 4549 | |
| 4550 | switch (chan->move_role) { |
| 4551 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4552 | l2cap_move_done(chan); |
| 4553 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP); |
| 4554 | break; |
| 4555 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4556 | if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP || |
| 4557 | chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) { |
| 4558 | /* Remote has only sent pending or |
| 4559 | * success responses, clean up |
| 4560 | */ |
| 4561 | l2cap_move_done(chan); |
| 4562 | } |
| 4563 | |
| 4564 | /* Other amp move states imply that the move |
| 4565 | * has already aborted |
| 4566 | */ |
| 4567 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 4568 | break; |
| 4569 | } |
| 4570 | } |
| 4571 | |
| 4572 | static void l2cap_logical_finish_create(struct l2cap_chan *chan, |
| 4573 | struct hci_chan *hchan) |
| 4574 | { |
| 4575 | struct l2cap_conf_rsp rsp; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4576 | |
Andrei Emeltchenko | 336178a | 2012-10-31 15:46:27 +0200 | [diff] [blame] | 4577 | chan->hs_hchan = hchan; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4578 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4579 | |
Andrei Emeltchenko | 35ba956 | 2012-10-25 15:20:43 +0300 | [diff] [blame] | 4580 | l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4581 | |
| 4582 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Andrei Emeltchenko | fe79c6f | 2012-10-25 15:20:42 +0300 | [diff] [blame] | 4583 | int err; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4584 | |
| 4585 | set_default_fcs(chan); |
| 4586 | |
| 4587 | err = l2cap_ertm_init(chan); |
| 4588 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4589 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4590 | else |
| 4591 | l2cap_chan_ready(chan); |
| 4592 | } |
| 4593 | } |
| 4594 | |
| 4595 | static void l2cap_logical_finish_move(struct l2cap_chan *chan, |
| 4596 | struct hci_chan *hchan) |
| 4597 | { |
| 4598 | chan->hs_hcon = hchan->conn; |
| 4599 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4600 | |
| 4601 | BT_DBG("move_state %d", chan->move_state); |
| 4602 | |
| 4603 | switch (chan->move_state) { |
| 4604 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 4605 | /* Move confirm will be sent after a success |
| 4606 | * response is received |
| 4607 | */ |
| 4608 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4609 | break; |
| 4610 | case L2CAP_MOVE_WAIT_LOGICAL_CFM: |
| 4611 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4612 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4613 | } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 4614 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 4615 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4616 | } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4617 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4618 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4619 | } |
| 4620 | break; |
| 4621 | default: |
| 4622 | /* Move was not in expected state, free the channel */ |
| 4623 | __release_logical_link(chan); |
| 4624 | |
| 4625 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4626 | } |
| 4627 | } |
| 4628 | |
| 4629 | /* Call with chan locked */ |
Andrei Emeltchenko | 27695fb | 2012-10-25 15:20:45 +0300 | [diff] [blame] | 4630 | void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan, |
| 4631 | u8 status) |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4632 | { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4633 | BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status); |
| 4634 | |
| 4635 | if (status) { |
| 4636 | l2cap_logical_fail(chan); |
| 4637 | __release_logical_link(chan); |
| 4638 | return; |
| 4639 | } |
| 4640 | |
| 4641 | if (chan->state != BT_CONNECTED) { |
| 4642 | /* Ignore logical link if channel is on BR/EDR */ |
| 4643 | if (chan->local_amp_id) |
| 4644 | l2cap_logical_finish_create(chan, hchan); |
| 4645 | } else { |
| 4646 | l2cap_logical_finish_move(chan, hchan); |
| 4647 | } |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4648 | } |
| 4649 | |
Mat Martineau | 3f7a56c | 2012-10-23 15:24:23 -0700 | [diff] [blame] | 4650 | void l2cap_move_start(struct l2cap_chan *chan) |
| 4651 | { |
| 4652 | BT_DBG("chan %p", chan); |
| 4653 | |
| 4654 | if (chan->local_amp_id == HCI_BREDR_ID) { |
| 4655 | if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED) |
| 4656 | return; |
| 4657 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4658 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4659 | /* Placeholder - start physical link setup */ |
| 4660 | } else { |
| 4661 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4662 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4663 | chan->move_id = 0; |
| 4664 | l2cap_move_setup(chan); |
| 4665 | l2cap_send_move_chan_req(chan, 0); |
| 4666 | } |
| 4667 | } |
| 4668 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4669 | static void l2cap_do_create(struct l2cap_chan *chan, int result, |
| 4670 | u8 local_amp_id, u8 remote_amp_id) |
| 4671 | { |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4672 | BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state), |
| 4673 | local_amp_id, remote_amp_id); |
| 4674 | |
Andrei Emeltchenko | 12d6cc6 | 2012-11-14 17:39:31 +0200 | [diff] [blame] | 4675 | chan->fcs = L2CAP_FCS_NONE; |
| 4676 | |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4677 | /* Outgoing channel on AMP */ |
| 4678 | if (chan->state == BT_CONNECT) { |
| 4679 | if (result == L2CAP_CR_SUCCESS) { |
| 4680 | chan->local_amp_id = local_amp_id; |
| 4681 | l2cap_send_create_chan_req(chan, remote_amp_id); |
| 4682 | } else { |
| 4683 | /* Revert to BR/EDR connect */ |
| 4684 | l2cap_send_conn_req(chan); |
| 4685 | } |
| 4686 | |
| 4687 | return; |
| 4688 | } |
| 4689 | |
| 4690 | /* Incoming channel on AMP */ |
| 4691 | if (__l2cap_no_conn_pending(chan)) { |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4692 | struct l2cap_conn_rsp rsp; |
| 4693 | char buf[128]; |
| 4694 | rsp.scid = cpu_to_le16(chan->dcid); |
| 4695 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4696 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4697 | if (result == L2CAP_CR_SUCCESS) { |
| 4698 | /* Send successful response */ |
Andrei Emeltchenko | 62cd50e | 2012-11-14 17:39:32 +0200 | [diff] [blame] | 4699 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS); |
| 4700 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4701 | } else { |
| 4702 | /* Send negative response */ |
Andrei Emeltchenko | 62cd50e | 2012-11-14 17:39:32 +0200 | [diff] [blame] | 4703 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_NO_MEM); |
| 4704 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4705 | } |
| 4706 | |
| 4707 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP, |
| 4708 | sizeof(rsp), &rsp); |
| 4709 | |
| 4710 | if (result == L2CAP_CR_SUCCESS) { |
| 4711 | __l2cap_state_change(chan, BT_CONFIG); |
| 4712 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 4713 | l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn), |
| 4714 | L2CAP_CONF_REQ, |
| 4715 | l2cap_build_conf_req(chan, buf), buf); |
| 4716 | chan->num_conf_req++; |
| 4717 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4718 | } |
| 4719 | } |
| 4720 | |
| 4721 | static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id, |
| 4722 | u8 remote_amp_id) |
| 4723 | { |
| 4724 | l2cap_move_setup(chan); |
| 4725 | chan->move_id = local_amp_id; |
| 4726 | chan->move_state = L2CAP_MOVE_WAIT_RSP; |
| 4727 | |
| 4728 | l2cap_send_move_chan_req(chan, remote_amp_id); |
| 4729 | } |
| 4730 | |
| 4731 | static void l2cap_do_move_respond(struct l2cap_chan *chan, int result) |
| 4732 | { |
| 4733 | struct hci_chan *hchan = NULL; |
| 4734 | |
| 4735 | /* Placeholder - get hci_chan for logical link */ |
| 4736 | |
| 4737 | if (hchan) { |
| 4738 | if (hchan->state == BT_CONNECTED) { |
| 4739 | /* Logical link is ready to go */ |
| 4740 | chan->hs_hcon = hchan->conn; |
| 4741 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4742 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4743 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4744 | |
| 4745 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 4746 | } else { |
| 4747 | /* Wait for logical link to be ready */ |
| 4748 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4749 | } |
| 4750 | } else { |
| 4751 | /* Logical link not available */ |
| 4752 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED); |
| 4753 | } |
| 4754 | } |
| 4755 | |
| 4756 | static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result) |
| 4757 | { |
| 4758 | if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4759 | u8 rsp_result; |
| 4760 | if (result == -EINVAL) |
| 4761 | rsp_result = L2CAP_MR_BAD_ID; |
| 4762 | else |
| 4763 | rsp_result = L2CAP_MR_NOT_ALLOWED; |
| 4764 | |
| 4765 | l2cap_send_move_chan_rsp(chan, rsp_result); |
| 4766 | } |
| 4767 | |
| 4768 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 4769 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4770 | |
| 4771 | /* Restart data transmission */ |
| 4772 | l2cap_ertm_send(chan); |
| 4773 | } |
| 4774 | |
Andrei Emeltchenko | a514b17 | 2012-11-14 17:39:30 +0200 | [diff] [blame] | 4775 | /* Invoke with locked chan */ |
| 4776 | void __l2cap_physical_cfm(struct l2cap_chan *chan, int result) |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4777 | { |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4778 | u8 local_amp_id = chan->local_amp_id; |
Andrei Emeltchenko | fffadc0 | 2012-11-01 15:37:03 +0200 | [diff] [blame] | 4779 | u8 remote_amp_id = chan->remote_amp_id; |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4780 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4781 | BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d", |
| 4782 | chan, result, local_amp_id, remote_amp_id); |
| 4783 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4784 | if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) { |
| 4785 | l2cap_chan_unlock(chan); |
| 4786 | return; |
| 4787 | } |
| 4788 | |
| 4789 | if (chan->state != BT_CONNECTED) { |
| 4790 | l2cap_do_create(chan, result, local_amp_id, remote_amp_id); |
| 4791 | } else if (result != L2CAP_MR_SUCCESS) { |
| 4792 | l2cap_do_move_cancel(chan, result); |
| 4793 | } else { |
| 4794 | switch (chan->move_role) { |
| 4795 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4796 | l2cap_do_move_initiate(chan, local_amp_id, |
| 4797 | remote_amp_id); |
| 4798 | break; |
| 4799 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4800 | l2cap_do_move_respond(chan, result); |
| 4801 | break; |
| 4802 | default: |
| 4803 | l2cap_do_move_cancel(chan, result); |
| 4804 | break; |
| 4805 | } |
| 4806 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4807 | } |
| 4808 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4809 | static inline int l2cap_move_channel_req(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4810 | struct l2cap_cmd_hdr *cmd, |
| 4811 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4812 | { |
| 4813 | struct l2cap_move_chan_req *req = data; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4814 | struct l2cap_move_chan_rsp rsp; |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4815 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4816 | u16 icid = 0; |
| 4817 | u16 result = L2CAP_MR_NOT_ALLOWED; |
| 4818 | |
| 4819 | if (cmd_len != sizeof(*req)) |
| 4820 | return -EPROTO; |
| 4821 | |
| 4822 | icid = le16_to_cpu(req->icid); |
| 4823 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4824 | 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] | 4825 | |
| 4826 | if (!enable_hs) |
| 4827 | return -EINVAL; |
| 4828 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4829 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 4830 | if (!chan) { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4831 | rsp.icid = cpu_to_le16(icid); |
| 4832 | rsp.result = __constant_cpu_to_le16(L2CAP_MR_NOT_ALLOWED); |
| 4833 | l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP, |
| 4834 | sizeof(rsp), &rsp); |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4835 | return 0; |
| 4836 | } |
| 4837 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4838 | chan->ident = cmd->ident; |
| 4839 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4840 | if (chan->scid < L2CAP_CID_DYN_START || |
| 4841 | chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY || |
| 4842 | (chan->mode != L2CAP_MODE_ERTM && |
| 4843 | chan->mode != L2CAP_MODE_STREAMING)) { |
| 4844 | result = L2CAP_MR_NOT_ALLOWED; |
| 4845 | goto send_move_response; |
| 4846 | } |
| 4847 | |
| 4848 | if (chan->local_amp_id == req->dest_amp_id) { |
| 4849 | result = L2CAP_MR_SAME_ID; |
| 4850 | goto send_move_response; |
| 4851 | } |
| 4852 | |
| 4853 | if (req->dest_amp_id) { |
| 4854 | struct hci_dev *hdev; |
| 4855 | hdev = hci_dev_get(req->dest_amp_id); |
| 4856 | if (!hdev || hdev->dev_type != HCI_AMP || |
| 4857 | !test_bit(HCI_UP, &hdev->flags)) { |
| 4858 | if (hdev) |
| 4859 | hci_dev_put(hdev); |
| 4860 | |
| 4861 | result = L2CAP_MR_BAD_ID; |
| 4862 | goto send_move_response; |
| 4863 | } |
| 4864 | hci_dev_put(hdev); |
| 4865 | } |
| 4866 | |
| 4867 | /* Detect a move collision. Only send a collision response |
| 4868 | * if this side has "lost", otherwise proceed with the move. |
| 4869 | * The winner has the larger bd_addr. |
| 4870 | */ |
| 4871 | if ((__chan_is_moving(chan) || |
| 4872 | chan->move_role != L2CAP_MOVE_ROLE_NONE) && |
| 4873 | bacmp(conn->src, conn->dst) > 0) { |
| 4874 | result = L2CAP_MR_COLLISION; |
| 4875 | goto send_move_response; |
| 4876 | } |
| 4877 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4878 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 4879 | l2cap_move_setup(chan); |
| 4880 | chan->move_id = req->dest_amp_id; |
| 4881 | icid = chan->dcid; |
| 4882 | |
| 4883 | if (!req->dest_amp_id) { |
| 4884 | /* Moving to BR/EDR */ |
| 4885 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4886 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4887 | result = L2CAP_MR_PEND; |
| 4888 | } else { |
| 4889 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4890 | result = L2CAP_MR_SUCCESS; |
| 4891 | } |
| 4892 | } else { |
| 4893 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4894 | /* Placeholder - uncomment when amp functions are available */ |
| 4895 | /*amp_accept_physical(chan, req->dest_amp_id);*/ |
| 4896 | result = L2CAP_MR_PEND; |
| 4897 | } |
| 4898 | |
| 4899 | send_move_response: |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4900 | l2cap_send_move_chan_rsp(chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4901 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4902 | l2cap_chan_unlock(chan); |
| 4903 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4904 | return 0; |
| 4905 | } |
| 4906 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4907 | static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result) |
| 4908 | { |
| 4909 | struct l2cap_chan *chan; |
| 4910 | struct hci_chan *hchan = NULL; |
| 4911 | |
| 4912 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 4913 | if (!chan) { |
| 4914 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 4915 | return; |
| 4916 | } |
| 4917 | |
| 4918 | __clear_chan_timer(chan); |
| 4919 | if (result == L2CAP_MR_PEND) |
| 4920 | __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT); |
| 4921 | |
| 4922 | switch (chan->move_state) { |
| 4923 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 4924 | /* Move confirm will be sent when logical link |
| 4925 | * is complete. |
| 4926 | */ |
| 4927 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4928 | break; |
| 4929 | case L2CAP_MOVE_WAIT_RSP_SUCCESS: |
| 4930 | if (result == L2CAP_MR_PEND) { |
| 4931 | break; |
| 4932 | } else if (test_bit(CONN_LOCAL_BUSY, |
| 4933 | &chan->conn_state)) { |
| 4934 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4935 | } else { |
| 4936 | /* Logical link is up or moving to BR/EDR, |
| 4937 | * proceed with move |
| 4938 | */ |
| 4939 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 4940 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4941 | } |
| 4942 | break; |
| 4943 | case L2CAP_MOVE_WAIT_RSP: |
| 4944 | /* Moving to AMP */ |
| 4945 | if (result == L2CAP_MR_SUCCESS) { |
| 4946 | /* Remote is ready, send confirm immediately |
| 4947 | * after logical link is ready |
| 4948 | */ |
| 4949 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4950 | } else { |
| 4951 | /* Both logical link and move success |
| 4952 | * are required to confirm |
| 4953 | */ |
| 4954 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP; |
| 4955 | } |
| 4956 | |
| 4957 | /* Placeholder - get hci_chan for logical link */ |
| 4958 | if (!hchan) { |
| 4959 | /* Logical link not available */ |
| 4960 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 4961 | break; |
| 4962 | } |
| 4963 | |
| 4964 | /* If the logical link is not yet connected, do not |
| 4965 | * send confirmation. |
| 4966 | */ |
| 4967 | if (hchan->state != BT_CONNECTED) |
| 4968 | break; |
| 4969 | |
| 4970 | /* Logical link is already ready to go */ |
| 4971 | |
| 4972 | chan->hs_hcon = hchan->conn; |
| 4973 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4974 | |
| 4975 | if (result == L2CAP_MR_SUCCESS) { |
| 4976 | /* Can confirm now */ |
| 4977 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4978 | } else { |
| 4979 | /* Now only need move success |
| 4980 | * to confirm |
| 4981 | */ |
| 4982 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4983 | } |
| 4984 | |
| 4985 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 4986 | break; |
| 4987 | default: |
| 4988 | /* Any other amp move state means the move failed. */ |
| 4989 | chan->move_id = chan->local_amp_id; |
| 4990 | l2cap_move_done(chan); |
| 4991 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 4992 | } |
| 4993 | |
| 4994 | l2cap_chan_unlock(chan); |
| 4995 | } |
| 4996 | |
| 4997 | static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid, |
| 4998 | u16 result) |
| 4999 | { |
| 5000 | struct l2cap_chan *chan; |
| 5001 | |
| 5002 | chan = l2cap_get_chan_by_ident(conn, ident); |
| 5003 | if (!chan) { |
| 5004 | /* Could not locate channel, icid is best guess */ |
| 5005 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 5006 | return; |
| 5007 | } |
| 5008 | |
| 5009 | __clear_chan_timer(chan); |
| 5010 | |
| 5011 | if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 5012 | if (result == L2CAP_MR_COLLISION) { |
| 5013 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 5014 | } else { |
| 5015 | /* Cleanup - cancel move */ |
| 5016 | chan->move_id = chan->local_amp_id; |
| 5017 | l2cap_move_done(chan); |
| 5018 | } |
| 5019 | } |
| 5020 | |
| 5021 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5022 | |
| 5023 | l2cap_chan_unlock(chan); |
| 5024 | } |
| 5025 | |
| 5026 | static int l2cap_move_channel_rsp(struct l2cap_conn *conn, |
| 5027 | struct l2cap_cmd_hdr *cmd, |
| 5028 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5029 | { |
| 5030 | struct l2cap_move_chan_rsp *rsp = data; |
| 5031 | u16 icid, result; |
| 5032 | |
| 5033 | if (cmd_len != sizeof(*rsp)) |
| 5034 | return -EPROTO; |
| 5035 | |
| 5036 | icid = le16_to_cpu(rsp->icid); |
| 5037 | result = le16_to_cpu(rsp->result); |
| 5038 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5039 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5040 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 5041 | if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND) |
| 5042 | l2cap_move_continue(conn, icid, result); |
| 5043 | else |
| 5044 | l2cap_move_fail(conn, cmd->ident, icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5045 | |
| 5046 | return 0; |
| 5047 | } |
| 5048 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5049 | static int l2cap_move_channel_confirm(struct l2cap_conn *conn, |
| 5050 | struct l2cap_cmd_hdr *cmd, |
| 5051 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5052 | { |
| 5053 | struct l2cap_move_chan_cfm *cfm = data; |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5054 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5055 | u16 icid, result; |
| 5056 | |
| 5057 | if (cmd_len != sizeof(*cfm)) |
| 5058 | return -EPROTO; |
| 5059 | |
| 5060 | icid = le16_to_cpu(cfm->icid); |
| 5061 | result = le16_to_cpu(cfm->result); |
| 5062 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5063 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5064 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5065 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 5066 | if (!chan) { |
| 5067 | /* Spec requires a response even if the icid was not found */ |
| 5068 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5069 | return 0; |
| 5070 | } |
| 5071 | |
| 5072 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) { |
| 5073 | if (result == L2CAP_MC_CONFIRMED) { |
| 5074 | chan->local_amp_id = chan->move_id; |
| 5075 | if (!chan->local_amp_id) |
| 5076 | __release_logical_link(chan); |
| 5077 | } else { |
| 5078 | chan->move_id = chan->local_amp_id; |
| 5079 | } |
| 5080 | |
| 5081 | l2cap_move_done(chan); |
| 5082 | } |
| 5083 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5084 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5085 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5086 | l2cap_chan_unlock(chan); |
| 5087 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5088 | return 0; |
| 5089 | } |
| 5090 | |
| 5091 | static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5092 | struct l2cap_cmd_hdr *cmd, |
| 5093 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5094 | { |
| 5095 | struct l2cap_move_chan_cfm_rsp *rsp = data; |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5096 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5097 | u16 icid; |
| 5098 | |
| 5099 | if (cmd_len != sizeof(*rsp)) |
| 5100 | return -EPROTO; |
| 5101 | |
| 5102 | icid = le16_to_cpu(rsp->icid); |
| 5103 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5104 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5105 | |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5106 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 5107 | if (!chan) |
| 5108 | return 0; |
| 5109 | |
| 5110 | __clear_chan_timer(chan); |
| 5111 | |
| 5112 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) { |
| 5113 | chan->local_amp_id = chan->move_id; |
| 5114 | |
| 5115 | if (!chan->local_amp_id && chan->hs_hchan) |
| 5116 | __release_logical_link(chan); |
| 5117 | |
| 5118 | l2cap_move_done(chan); |
| 5119 | } |
| 5120 | |
| 5121 | l2cap_chan_unlock(chan); |
| 5122 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5123 | return 0; |
| 5124 | } |
| 5125 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 5126 | 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] | 5127 | u16 to_multiplier) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5128 | { |
| 5129 | u16 max_latency; |
| 5130 | |
| 5131 | if (min > max || min < 6 || max > 3200) |
| 5132 | return -EINVAL; |
| 5133 | |
| 5134 | if (to_multiplier < 10 || to_multiplier > 3200) |
| 5135 | return -EINVAL; |
| 5136 | |
| 5137 | if (max >= to_multiplier * 8) |
| 5138 | return -EINVAL; |
| 5139 | |
| 5140 | max_latency = (to_multiplier * 8 / max) - 1; |
| 5141 | if (latency > 499 || latency > max_latency) |
| 5142 | return -EINVAL; |
| 5143 | |
| 5144 | return 0; |
| 5145 | } |
| 5146 | |
| 5147 | static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5148 | struct l2cap_cmd_hdr *cmd, |
| 5149 | u8 *data) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5150 | { |
| 5151 | struct hci_conn *hcon = conn->hcon; |
| 5152 | struct l2cap_conn_param_update_req *req; |
| 5153 | struct l2cap_conn_param_update_rsp rsp; |
| 5154 | u16 min, max, latency, to_multiplier, cmd_len; |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5155 | int err; |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5156 | |
| 5157 | if (!(hcon->link_mode & HCI_LM_MASTER)) |
| 5158 | return -EINVAL; |
| 5159 | |
| 5160 | cmd_len = __le16_to_cpu(cmd->len); |
| 5161 | if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) |
| 5162 | return -EPROTO; |
| 5163 | |
| 5164 | req = (struct l2cap_conn_param_update_req *) data; |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 5165 | min = __le16_to_cpu(req->min); |
| 5166 | max = __le16_to_cpu(req->max); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5167 | latency = __le16_to_cpu(req->latency); |
| 5168 | to_multiplier = __le16_to_cpu(req->to_multiplier); |
| 5169 | |
| 5170 | 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] | 5171 | min, max, latency, to_multiplier); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5172 | |
| 5173 | memset(&rsp, 0, sizeof(rsp)); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5174 | |
| 5175 | err = l2cap_check_conn_param(min, max, latency, to_multiplier); |
| 5176 | if (err) |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 5177 | rsp.result = __constant_cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5178 | else |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 5179 | rsp.result = __constant_cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5180 | |
| 5181 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5182 | sizeof(rsp), &rsp); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5183 | |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5184 | if (!err) |
| 5185 | hci_le_conn_update(hcon, min, max, latency, to_multiplier); |
| 5186 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5187 | return 0; |
| 5188 | } |
| 5189 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5190 | static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5191 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5192 | u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5193 | { |
| 5194 | int err = 0; |
| 5195 | |
| 5196 | switch (cmd->code) { |
| 5197 | case L2CAP_COMMAND_REJ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5198 | l2cap_command_rej(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5199 | break; |
| 5200 | |
| 5201 | case L2CAP_CONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5202 | err = l2cap_connect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5203 | break; |
| 5204 | |
| 5205 | case L2CAP_CONN_RSP: |
Mat Martineau | f5a2598 | 2012-10-11 17:48:21 +0300 | [diff] [blame] | 5206 | case L2CAP_CREATE_CHAN_RSP: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5207 | err = l2cap_connect_create_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5208 | break; |
| 5209 | |
| 5210 | case L2CAP_CONF_REQ: |
| 5211 | err = l2cap_config_req(conn, cmd, cmd_len, data); |
| 5212 | break; |
| 5213 | |
| 5214 | case L2CAP_CONF_RSP: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5215 | err = l2cap_config_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5216 | break; |
| 5217 | |
| 5218 | case L2CAP_DISCONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5219 | err = l2cap_disconnect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5220 | break; |
| 5221 | |
| 5222 | case L2CAP_DISCONN_RSP: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5223 | err = l2cap_disconnect_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5224 | break; |
| 5225 | |
| 5226 | case L2CAP_ECHO_REQ: |
| 5227 | l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); |
| 5228 | break; |
| 5229 | |
| 5230 | case L2CAP_ECHO_RSP: |
| 5231 | break; |
| 5232 | |
| 5233 | case L2CAP_INFO_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5234 | err = l2cap_information_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5235 | break; |
| 5236 | |
| 5237 | case L2CAP_INFO_RSP: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5238 | err = l2cap_information_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5239 | break; |
| 5240 | |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 5241 | case L2CAP_CREATE_CHAN_REQ: |
| 5242 | err = l2cap_create_channel_req(conn, cmd, cmd_len, data); |
| 5243 | break; |
| 5244 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5245 | case L2CAP_MOVE_CHAN_REQ: |
| 5246 | err = l2cap_move_channel_req(conn, cmd, cmd_len, data); |
| 5247 | break; |
| 5248 | |
| 5249 | case L2CAP_MOVE_CHAN_RSP: |
| 5250 | err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data); |
| 5251 | break; |
| 5252 | |
| 5253 | case L2CAP_MOVE_CHAN_CFM: |
| 5254 | err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); |
| 5255 | break; |
| 5256 | |
| 5257 | case L2CAP_MOVE_CHAN_CFM_RSP: |
| 5258 | err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); |
| 5259 | break; |
| 5260 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5261 | default: |
| 5262 | BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); |
| 5263 | err = -EINVAL; |
| 5264 | break; |
| 5265 | } |
| 5266 | |
| 5267 | return err; |
| 5268 | } |
| 5269 | |
| 5270 | static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5271 | struct l2cap_cmd_hdr *cmd, u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5272 | { |
| 5273 | switch (cmd->code) { |
| 5274 | case L2CAP_COMMAND_REJ: |
| 5275 | return 0; |
| 5276 | |
| 5277 | case L2CAP_CONN_PARAM_UPDATE_REQ: |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5278 | return l2cap_conn_param_update_req(conn, cmd, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5279 | |
| 5280 | case L2CAP_CONN_PARAM_UPDATE_RSP: |
| 5281 | return 0; |
| 5282 | |
| 5283 | default: |
| 5284 | BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); |
| 5285 | return -EINVAL; |
| 5286 | } |
| 5287 | } |
| 5288 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5289 | static inline void l2cap_le_sig_channel(struct l2cap_conn *conn, |
| 5290 | struct sk_buff *skb) |
| 5291 | { |
| 5292 | u8 *data = skb->data; |
| 5293 | int len = skb->len; |
| 5294 | struct l2cap_cmd_hdr cmd; |
| 5295 | int err; |
| 5296 | |
| 5297 | l2cap_raw_recv(conn, skb); |
| 5298 | |
| 5299 | while (len >= L2CAP_CMD_HDR_SIZE) { |
| 5300 | u16 cmd_len; |
| 5301 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 5302 | data += L2CAP_CMD_HDR_SIZE; |
| 5303 | len -= L2CAP_CMD_HDR_SIZE; |
| 5304 | |
| 5305 | cmd_len = le16_to_cpu(cmd.len); |
| 5306 | |
| 5307 | BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, |
| 5308 | cmd.ident); |
| 5309 | |
| 5310 | if (cmd_len > len || !cmd.ident) { |
| 5311 | BT_DBG("corrupted command"); |
| 5312 | break; |
| 5313 | } |
| 5314 | |
| 5315 | err = l2cap_le_sig_cmd(conn, &cmd, data); |
| 5316 | if (err) { |
| 5317 | struct l2cap_cmd_rej_unk rej; |
| 5318 | |
| 5319 | BT_ERR("Wrong link type (%d)", err); |
| 5320 | |
| 5321 | /* FIXME: Map err to a valid reason */ |
| 5322 | rej.reason = __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
| 5323 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, |
| 5324 | sizeof(rej), &rej); |
| 5325 | } |
| 5326 | |
| 5327 | data += cmd_len; |
| 5328 | len -= cmd_len; |
| 5329 | } |
| 5330 | |
| 5331 | kfree_skb(skb); |
| 5332 | } |
| 5333 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5334 | static inline void l2cap_sig_channel(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5335 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5336 | { |
| 5337 | u8 *data = skb->data; |
| 5338 | int len = skb->len; |
| 5339 | struct l2cap_cmd_hdr cmd; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5340 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | |
| 5342 | l2cap_raw_recv(conn, skb); |
| 5343 | |
| 5344 | while (len >= L2CAP_CMD_HDR_SIZE) { |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5345 | u16 cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5346 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 5347 | data += L2CAP_CMD_HDR_SIZE; |
| 5348 | len -= L2CAP_CMD_HDR_SIZE; |
| 5349 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5350 | cmd_len = le16_to_cpu(cmd.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5351 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5352 | BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, |
| 5353 | cmd.ident); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5354 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5355 | if (cmd_len > len || !cmd.ident) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5356 | BT_DBG("corrupted command"); |
| 5357 | break; |
| 5358 | } |
| 5359 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5360 | err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5361 | if (err) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 5362 | struct l2cap_cmd_rej_unk rej; |
Gustavo F. Padovan | 2c6d1a2 | 2011-03-23 14:38:32 -0300 | [diff] [blame] | 5363 | |
| 5364 | BT_ERR("Wrong link type (%d)", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5365 | |
| 5366 | /* FIXME: Map err to a valid reason */ |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 5367 | rej.reason = __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5368 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, |
| 5369 | sizeof(rej), &rej); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5370 | } |
| 5371 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5372 | data += cmd_len; |
| 5373 | len -= cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5374 | } |
| 5375 | |
| 5376 | kfree_skb(skb); |
| 5377 | } |
| 5378 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5379 | 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] | 5380 | { |
| 5381 | u16 our_fcs, rcv_fcs; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 5382 | int hdr_size; |
| 5383 | |
| 5384 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 5385 | hdr_size = L2CAP_EXT_HDR_SIZE; |
| 5386 | else |
| 5387 | hdr_size = L2CAP_ENH_HDR_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5388 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5389 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5390 | skb_trim(skb, skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5391 | rcv_fcs = get_unaligned_le16(skb->data + skb->len); |
| 5392 | our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); |
| 5393 | |
| 5394 | if (our_fcs != rcv_fcs) |
João Paulo Rechi Vita | 7a560e5 | 2010-06-22 13:56:27 -0300 | [diff] [blame] | 5395 | return -EBADMSG; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5396 | } |
| 5397 | return 0; |
| 5398 | } |
| 5399 | |
Mat Martineau | 6ea0048 | 2012-05-17 20:53:52 -0700 | [diff] [blame] | 5400 | 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] | 5401 | { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5402 | struct l2cap_ctrl control; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5403 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5404 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5405 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5406 | memset(&control, 0, sizeof(control)); |
| 5407 | control.sframe = 1; |
| 5408 | control.final = 1; |
| 5409 | control.reqseq = chan->buffer_seq; |
| 5410 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5411 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5412 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5413 | control.super = L2CAP_SUPER_RNR; |
| 5414 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5415 | } |
| 5416 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5417 | if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
| 5418 | chan->unacked_frames > 0) |
| 5419 | __set_retrans_timer(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5420 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5421 | /* Send pending iframes */ |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 5422 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5423 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5424 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5425 | test_bit(CONN_SEND_FBIT, &chan->conn_state)) { |
| 5426 | /* F-bit wasn't sent in an s-frame or i-frame yet, so |
| 5427 | * send it now. |
| 5428 | */ |
| 5429 | control.super = L2CAP_SUPER_RR; |
| 5430 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5431 | } |
| 5432 | } |
| 5433 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5434 | static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag, |
| 5435 | struct sk_buff **last_frag) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5436 | { |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5437 | /* skb->len reflects data in skb as well as all fragments |
| 5438 | * skb->data_len reflects only data in fragments |
| 5439 | */ |
| 5440 | if (!skb_has_frag_list(skb)) |
| 5441 | skb_shinfo(skb)->frag_list = new_frag; |
| 5442 | |
| 5443 | new_frag->next = NULL; |
| 5444 | |
| 5445 | (*last_frag)->next = new_frag; |
| 5446 | *last_frag = new_frag; |
| 5447 | |
| 5448 | skb->len += new_frag->len; |
| 5449 | skb->data_len += new_frag->len; |
| 5450 | skb->truesize += new_frag->truesize; |
| 5451 | } |
| 5452 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5453 | static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, |
| 5454 | struct l2cap_ctrl *control) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5455 | { |
| 5456 | int err = -EINVAL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5457 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5458 | switch (control->sar) { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5459 | case L2CAP_SAR_UNSEGMENTED: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5460 | if (chan->sdu) |
| 5461 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5462 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5463 | err = chan->ops->recv(chan, skb); |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5464 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5465 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5466 | case L2CAP_SAR_START: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5467 | if (chan->sdu) |
| 5468 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5469 | |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5470 | chan->sdu_len = get_unaligned_le16(skb->data); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5471 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5472 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5473 | if (chan->sdu_len > chan->imtu) { |
| 5474 | err = -EMSGSIZE; |
| 5475 | break; |
| 5476 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5477 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5478 | if (skb->len >= chan->sdu_len) |
| 5479 | break; |
| 5480 | |
| 5481 | chan->sdu = skb; |
| 5482 | chan->sdu_last_frag = skb; |
| 5483 | |
| 5484 | skb = NULL; |
| 5485 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5486 | break; |
| 5487 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5488 | case L2CAP_SAR_CONTINUE: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5489 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5490 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5491 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5492 | append_skb_frag(chan->sdu, skb, |
| 5493 | &chan->sdu_last_frag); |
| 5494 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5495 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5496 | if (chan->sdu->len >= chan->sdu_len) |
| 5497 | break; |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5498 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5499 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5500 | break; |
| 5501 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5502 | case L2CAP_SAR_END: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5503 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5504 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5505 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5506 | append_skb_frag(chan->sdu, skb, |
| 5507 | &chan->sdu_last_frag); |
| 5508 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5509 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5510 | if (chan->sdu->len != chan->sdu_len) |
| 5511 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5512 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5513 | err = chan->ops->recv(chan, chan->sdu); |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5514 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5515 | if (!err) { |
| 5516 | /* Reassembly complete */ |
| 5517 | chan->sdu = NULL; |
| 5518 | chan->sdu_last_frag = NULL; |
| 5519 | chan->sdu_len = 0; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5520 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5521 | break; |
| 5522 | } |
| 5523 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5524 | if (err) { |
| 5525 | kfree_skb(skb); |
| 5526 | kfree_skb(chan->sdu); |
| 5527 | chan->sdu = NULL; |
| 5528 | chan->sdu_last_frag = NULL; |
| 5529 | chan->sdu_len = 0; |
| 5530 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5531 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5532 | return err; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5533 | } |
| 5534 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 5535 | static int l2cap_resegment(struct l2cap_chan *chan) |
| 5536 | { |
| 5537 | /* Placeholder */ |
| 5538 | return 0; |
| 5539 | } |
| 5540 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 5541 | void l2cap_chan_busy(struct l2cap_chan *chan, int busy) |
Gustavo F. Padovan | 712132eb | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 5542 | { |
Mat Martineau | 61aa4f5 | 2012-05-17 20:53:40 -0700 | [diff] [blame] | 5543 | u8 event; |
| 5544 | |
| 5545 | if (chan->mode != L2CAP_MODE_ERTM) |
| 5546 | return; |
| 5547 | |
| 5548 | event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR; |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 5549 | l2cap_tx(chan, NULL, NULL, event); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5550 | } |
| 5551 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5552 | static int l2cap_rx_queued_iframes(struct l2cap_chan *chan) |
| 5553 | { |
Mat Martineau | 6383872 | 2012-05-17 20:53:45 -0700 | [diff] [blame] | 5554 | int err = 0; |
| 5555 | /* Pass sequential frames to l2cap_reassemble_sdu() |
| 5556 | * until a gap is encountered. |
| 5557 | */ |
| 5558 | |
| 5559 | BT_DBG("chan %p", chan); |
| 5560 | |
| 5561 | while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5562 | struct sk_buff *skb; |
| 5563 | BT_DBG("Searching for skb with txseq %d (queue len %d)", |
| 5564 | chan->buffer_seq, skb_queue_len(&chan->srej_q)); |
| 5565 | |
| 5566 | skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq); |
| 5567 | |
| 5568 | if (!skb) |
| 5569 | break; |
| 5570 | |
| 5571 | skb_unlink(skb, &chan->srej_q); |
| 5572 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 5573 | err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->control); |
| 5574 | if (err) |
| 5575 | break; |
| 5576 | } |
| 5577 | |
| 5578 | if (skb_queue_empty(&chan->srej_q)) { |
| 5579 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 5580 | l2cap_send_ack(chan); |
| 5581 | } |
| 5582 | |
| 5583 | return err; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5584 | } |
| 5585 | |
| 5586 | static void l2cap_handle_srej(struct l2cap_chan *chan, |
| 5587 | struct l2cap_ctrl *control) |
| 5588 | { |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5589 | struct sk_buff *skb; |
| 5590 | |
| 5591 | BT_DBG("chan %p, control %p", chan, control); |
| 5592 | |
| 5593 | if (control->reqseq == chan->next_tx_seq) { |
| 5594 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5595 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5596 | return; |
| 5597 | } |
| 5598 | |
| 5599 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 5600 | |
| 5601 | if (skb == NULL) { |
| 5602 | BT_DBG("Seq %d not available for retransmission", |
| 5603 | control->reqseq); |
| 5604 | return; |
| 5605 | } |
| 5606 | |
| 5607 | if (chan->max_tx != 0 && bt_cb(skb)->control.retries >= chan->max_tx) { |
| 5608 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5609 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5610 | return; |
| 5611 | } |
| 5612 | |
| 5613 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5614 | |
| 5615 | if (control->poll) { |
| 5616 | l2cap_pass_to_tx(chan, control); |
| 5617 | |
| 5618 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 5619 | l2cap_retransmit(chan, control); |
| 5620 | l2cap_ertm_send(chan); |
| 5621 | |
| 5622 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 5623 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5624 | chan->srej_save_reqseq = control->reqseq; |
| 5625 | } |
| 5626 | } else { |
| 5627 | l2cap_pass_to_tx_fbit(chan, control); |
| 5628 | |
| 5629 | if (control->final) { |
| 5630 | if (chan->srej_save_reqseq != control->reqseq || |
| 5631 | !test_and_clear_bit(CONN_SREJ_ACT, |
| 5632 | &chan->conn_state)) |
| 5633 | l2cap_retransmit(chan, control); |
| 5634 | } else { |
| 5635 | l2cap_retransmit(chan, control); |
| 5636 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 5637 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5638 | chan->srej_save_reqseq = control->reqseq; |
| 5639 | } |
| 5640 | } |
| 5641 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5642 | } |
| 5643 | |
| 5644 | static void l2cap_handle_rej(struct l2cap_chan *chan, |
| 5645 | struct l2cap_ctrl *control) |
| 5646 | { |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5647 | struct sk_buff *skb; |
| 5648 | |
| 5649 | BT_DBG("chan %p, control %p", chan, control); |
| 5650 | |
| 5651 | if (control->reqseq == chan->next_tx_seq) { |
| 5652 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5653 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5654 | return; |
| 5655 | } |
| 5656 | |
| 5657 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 5658 | |
| 5659 | if (chan->max_tx && skb && |
| 5660 | bt_cb(skb)->control.retries >= chan->max_tx) { |
| 5661 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5662 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5663 | return; |
| 5664 | } |
| 5665 | |
| 5666 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5667 | |
| 5668 | l2cap_pass_to_tx(chan, control); |
| 5669 | |
| 5670 | if (control->final) { |
| 5671 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
| 5672 | l2cap_retransmit_all(chan, control); |
| 5673 | } else { |
| 5674 | l2cap_retransmit_all(chan, control); |
| 5675 | l2cap_ertm_send(chan); |
| 5676 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) |
| 5677 | set_bit(CONN_REJ_ACT, &chan->conn_state); |
| 5678 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5679 | } |
| 5680 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5681 | static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq) |
| 5682 | { |
| 5683 | BT_DBG("chan %p, txseq %d", chan, txseq); |
| 5684 | |
| 5685 | BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq, |
| 5686 | chan->expected_tx_seq); |
| 5687 | |
| 5688 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 5689 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5690 | chan->tx_win) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5691 | /* See notes below regarding "double poll" and |
| 5692 | * invalid packets. |
| 5693 | */ |
| 5694 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 5695 | BT_DBG("Invalid/Ignore - after SREJ"); |
| 5696 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 5697 | } else { |
| 5698 | BT_DBG("Invalid - in window after SREJ sent"); |
| 5699 | return L2CAP_TXSEQ_INVALID; |
| 5700 | } |
| 5701 | } |
| 5702 | |
| 5703 | if (chan->srej_list.head == txseq) { |
| 5704 | BT_DBG("Expected SREJ"); |
| 5705 | return L2CAP_TXSEQ_EXPECTED_SREJ; |
| 5706 | } |
| 5707 | |
| 5708 | if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) { |
| 5709 | BT_DBG("Duplicate SREJ - txseq already stored"); |
| 5710 | return L2CAP_TXSEQ_DUPLICATE_SREJ; |
| 5711 | } |
| 5712 | |
| 5713 | if (l2cap_seq_list_contains(&chan->srej_list, txseq)) { |
| 5714 | BT_DBG("Unexpected SREJ - not requested"); |
| 5715 | return L2CAP_TXSEQ_UNEXPECTED_SREJ; |
| 5716 | } |
| 5717 | } |
| 5718 | |
| 5719 | if (chan->expected_tx_seq == txseq) { |
| 5720 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
| 5721 | chan->tx_win) { |
| 5722 | BT_DBG("Invalid - txseq outside tx window"); |
| 5723 | return L2CAP_TXSEQ_INVALID; |
| 5724 | } else { |
| 5725 | BT_DBG("Expected"); |
| 5726 | return L2CAP_TXSEQ_EXPECTED; |
| 5727 | } |
| 5728 | } |
| 5729 | |
| 5730 | if (__seq_offset(chan, txseq, chan->last_acked_seq) < |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5731 | __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5732 | BT_DBG("Duplicate - expected_tx_seq later than txseq"); |
| 5733 | return L2CAP_TXSEQ_DUPLICATE; |
| 5734 | } |
| 5735 | |
| 5736 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) { |
| 5737 | /* A source of invalid packets is a "double poll" condition, |
| 5738 | * where delays cause us to send multiple poll packets. If |
| 5739 | * the remote stack receives and processes both polls, |
| 5740 | * sequence numbers can wrap around in such a way that a |
| 5741 | * resent frame has a sequence number that looks like new data |
| 5742 | * with a sequence gap. This would trigger an erroneous SREJ |
| 5743 | * request. |
| 5744 | * |
| 5745 | * Fortunately, this is impossible with a tx window that's |
| 5746 | * less than half of the maximum sequence number, which allows |
| 5747 | * invalid frames to be safely ignored. |
| 5748 | * |
| 5749 | * With tx window sizes greater than half of the tx window |
| 5750 | * maximum, the frame is invalid and cannot be ignored. This |
| 5751 | * causes a disconnect. |
| 5752 | */ |
| 5753 | |
| 5754 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 5755 | BT_DBG("Invalid/Ignore - txseq outside tx window"); |
| 5756 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 5757 | } else { |
| 5758 | BT_DBG("Invalid - txseq outside tx window"); |
| 5759 | return L2CAP_TXSEQ_INVALID; |
| 5760 | } |
| 5761 | } else { |
| 5762 | BT_DBG("Unexpected - txseq indicates missing frames"); |
| 5763 | return L2CAP_TXSEQ_UNEXPECTED; |
| 5764 | } |
| 5765 | } |
| 5766 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5767 | static int l2cap_rx_state_recv(struct l2cap_chan *chan, |
| 5768 | struct l2cap_ctrl *control, |
| 5769 | struct sk_buff *skb, u8 event) |
| 5770 | { |
| 5771 | int err = 0; |
| 5772 | bool skb_in_use = 0; |
| 5773 | |
| 5774 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 5775 | event); |
| 5776 | |
| 5777 | switch (event) { |
| 5778 | case L2CAP_EV_RECV_IFRAME: |
| 5779 | switch (l2cap_classify_txseq(chan, control->txseq)) { |
| 5780 | case L2CAP_TXSEQ_EXPECTED: |
| 5781 | l2cap_pass_to_tx(chan, control); |
| 5782 | |
| 5783 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5784 | BT_DBG("Busy, discarding expected seq %d", |
| 5785 | control->txseq); |
| 5786 | break; |
| 5787 | } |
| 5788 | |
| 5789 | chan->expected_tx_seq = __next_seq(chan, |
| 5790 | control->txseq); |
| 5791 | |
| 5792 | chan->buffer_seq = chan->expected_tx_seq; |
| 5793 | skb_in_use = 1; |
| 5794 | |
| 5795 | err = l2cap_reassemble_sdu(chan, skb, control); |
| 5796 | if (err) |
| 5797 | break; |
| 5798 | |
| 5799 | if (control->final) { |
| 5800 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 5801 | &chan->conn_state)) { |
| 5802 | control->final = 0; |
| 5803 | l2cap_retransmit_all(chan, control); |
| 5804 | l2cap_ertm_send(chan); |
| 5805 | } |
| 5806 | } |
| 5807 | |
| 5808 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 5809 | l2cap_send_ack(chan); |
| 5810 | break; |
| 5811 | case L2CAP_TXSEQ_UNEXPECTED: |
| 5812 | l2cap_pass_to_tx(chan, control); |
| 5813 | |
| 5814 | /* Can't issue SREJ frames in the local busy state. |
| 5815 | * Drop this frame, it will be seen as missing |
| 5816 | * when local busy is exited. |
| 5817 | */ |
| 5818 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5819 | BT_DBG("Busy, discarding unexpected seq %d", |
| 5820 | control->txseq); |
| 5821 | break; |
| 5822 | } |
| 5823 | |
| 5824 | /* There was a gap in the sequence, so an SREJ |
| 5825 | * must be sent for each missing frame. The |
| 5826 | * current frame is stored for later use. |
| 5827 | */ |
| 5828 | skb_queue_tail(&chan->srej_q, skb); |
| 5829 | skb_in_use = 1; |
| 5830 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5831 | skb_queue_len(&chan->srej_q)); |
| 5832 | |
| 5833 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5834 | l2cap_seq_list_clear(&chan->srej_list); |
| 5835 | l2cap_send_srej(chan, control->txseq); |
| 5836 | |
| 5837 | chan->rx_state = L2CAP_RX_STATE_SREJ_SENT; |
| 5838 | break; |
| 5839 | case L2CAP_TXSEQ_DUPLICATE: |
| 5840 | l2cap_pass_to_tx(chan, control); |
| 5841 | break; |
| 5842 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 5843 | break; |
| 5844 | case L2CAP_TXSEQ_INVALID: |
| 5845 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5846 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5847 | break; |
| 5848 | } |
| 5849 | break; |
| 5850 | case L2CAP_EV_RECV_RR: |
| 5851 | l2cap_pass_to_tx(chan, control); |
| 5852 | if (control->final) { |
| 5853 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5854 | |
Mat Martineau | e6a3ee6 | 2012-10-23 15:24:22 -0700 | [diff] [blame] | 5855 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) && |
| 5856 | !__chan_is_moving(chan)) { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5857 | control->final = 0; |
| 5858 | l2cap_retransmit_all(chan, control); |
| 5859 | } |
| 5860 | |
| 5861 | l2cap_ertm_send(chan); |
| 5862 | } else if (control->poll) { |
| 5863 | l2cap_send_i_or_rr_or_rnr(chan); |
| 5864 | } else { |
| 5865 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 5866 | &chan->conn_state) && |
| 5867 | chan->unacked_frames) |
| 5868 | __set_retrans_timer(chan); |
| 5869 | |
| 5870 | l2cap_ertm_send(chan); |
| 5871 | } |
| 5872 | break; |
| 5873 | case L2CAP_EV_RECV_RNR: |
| 5874 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5875 | l2cap_pass_to_tx(chan, control); |
| 5876 | if (control && control->poll) { |
| 5877 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 5878 | l2cap_send_rr_or_rnr(chan, 0); |
| 5879 | } |
| 5880 | __clear_retrans_timer(chan); |
| 5881 | l2cap_seq_list_clear(&chan->retrans_list); |
| 5882 | break; |
| 5883 | case L2CAP_EV_RECV_REJ: |
| 5884 | l2cap_handle_rej(chan, control); |
| 5885 | break; |
| 5886 | case L2CAP_EV_RECV_SREJ: |
| 5887 | l2cap_handle_srej(chan, control); |
| 5888 | break; |
| 5889 | default: |
| 5890 | break; |
| 5891 | } |
| 5892 | |
| 5893 | if (skb && !skb_in_use) { |
| 5894 | BT_DBG("Freeing %p", skb); |
| 5895 | kfree_skb(skb); |
| 5896 | } |
| 5897 | |
| 5898 | return err; |
| 5899 | } |
| 5900 | |
| 5901 | static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan, |
| 5902 | struct l2cap_ctrl *control, |
| 5903 | struct sk_buff *skb, u8 event) |
| 5904 | { |
| 5905 | int err = 0; |
| 5906 | u16 txseq = control->txseq; |
| 5907 | bool skb_in_use = 0; |
| 5908 | |
| 5909 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 5910 | event); |
| 5911 | |
| 5912 | switch (event) { |
| 5913 | case L2CAP_EV_RECV_IFRAME: |
| 5914 | switch (l2cap_classify_txseq(chan, txseq)) { |
| 5915 | case L2CAP_TXSEQ_EXPECTED: |
| 5916 | /* Keep frame for reassembly later */ |
| 5917 | l2cap_pass_to_tx(chan, control); |
| 5918 | skb_queue_tail(&chan->srej_q, skb); |
| 5919 | skb_in_use = 1; |
| 5920 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5921 | skb_queue_len(&chan->srej_q)); |
| 5922 | |
| 5923 | chan->expected_tx_seq = __next_seq(chan, txseq); |
| 5924 | break; |
| 5925 | case L2CAP_TXSEQ_EXPECTED_SREJ: |
| 5926 | l2cap_seq_list_pop(&chan->srej_list); |
| 5927 | |
| 5928 | l2cap_pass_to_tx(chan, control); |
| 5929 | skb_queue_tail(&chan->srej_q, skb); |
| 5930 | skb_in_use = 1; |
| 5931 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5932 | skb_queue_len(&chan->srej_q)); |
| 5933 | |
| 5934 | err = l2cap_rx_queued_iframes(chan); |
| 5935 | if (err) |
| 5936 | break; |
| 5937 | |
| 5938 | break; |
| 5939 | case L2CAP_TXSEQ_UNEXPECTED: |
| 5940 | /* Got a frame that can't be reassembled yet. |
| 5941 | * Save it for later, and send SREJs to cover |
| 5942 | * the missing frames. |
| 5943 | */ |
| 5944 | skb_queue_tail(&chan->srej_q, skb); |
| 5945 | skb_in_use = 1; |
| 5946 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5947 | skb_queue_len(&chan->srej_q)); |
| 5948 | |
| 5949 | l2cap_pass_to_tx(chan, control); |
| 5950 | l2cap_send_srej(chan, control->txseq); |
| 5951 | break; |
| 5952 | case L2CAP_TXSEQ_UNEXPECTED_SREJ: |
| 5953 | /* This frame was requested with an SREJ, but |
| 5954 | * some expected retransmitted frames are |
| 5955 | * missing. Request retransmission of missing |
| 5956 | * SREJ'd frames. |
| 5957 | */ |
| 5958 | skb_queue_tail(&chan->srej_q, skb); |
| 5959 | skb_in_use = 1; |
| 5960 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5961 | skb_queue_len(&chan->srej_q)); |
| 5962 | |
| 5963 | l2cap_pass_to_tx(chan, control); |
| 5964 | l2cap_send_srej_list(chan, control->txseq); |
| 5965 | break; |
| 5966 | case L2CAP_TXSEQ_DUPLICATE_SREJ: |
| 5967 | /* We've already queued this frame. Drop this copy. */ |
| 5968 | l2cap_pass_to_tx(chan, control); |
| 5969 | break; |
| 5970 | case L2CAP_TXSEQ_DUPLICATE: |
| 5971 | /* Expecting a later sequence number, so this frame |
| 5972 | * was already received. Ignore it completely. |
| 5973 | */ |
| 5974 | break; |
| 5975 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 5976 | break; |
| 5977 | case L2CAP_TXSEQ_INVALID: |
| 5978 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5979 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5980 | break; |
| 5981 | } |
| 5982 | break; |
| 5983 | case L2CAP_EV_RECV_RR: |
| 5984 | l2cap_pass_to_tx(chan, control); |
| 5985 | if (control->final) { |
| 5986 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5987 | |
| 5988 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 5989 | &chan->conn_state)) { |
| 5990 | control->final = 0; |
| 5991 | l2cap_retransmit_all(chan, control); |
| 5992 | } |
| 5993 | |
| 5994 | l2cap_ertm_send(chan); |
| 5995 | } else if (control->poll) { |
| 5996 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 5997 | &chan->conn_state) && |
| 5998 | chan->unacked_frames) { |
| 5999 | __set_retrans_timer(chan); |
| 6000 | } |
| 6001 | |
| 6002 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6003 | l2cap_send_srej_tail(chan); |
| 6004 | } else { |
| 6005 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6006 | &chan->conn_state) && |
| 6007 | chan->unacked_frames) |
| 6008 | __set_retrans_timer(chan); |
| 6009 | |
| 6010 | l2cap_send_ack(chan); |
| 6011 | } |
| 6012 | break; |
| 6013 | case L2CAP_EV_RECV_RNR: |
| 6014 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6015 | l2cap_pass_to_tx(chan, control); |
| 6016 | if (control->poll) { |
| 6017 | l2cap_send_srej_tail(chan); |
| 6018 | } else { |
| 6019 | struct l2cap_ctrl rr_control; |
| 6020 | memset(&rr_control, 0, sizeof(rr_control)); |
| 6021 | rr_control.sframe = 1; |
| 6022 | rr_control.super = L2CAP_SUPER_RR; |
| 6023 | rr_control.reqseq = chan->buffer_seq; |
| 6024 | l2cap_send_sframe(chan, &rr_control); |
| 6025 | } |
| 6026 | |
| 6027 | break; |
| 6028 | case L2CAP_EV_RECV_REJ: |
| 6029 | l2cap_handle_rej(chan, control); |
| 6030 | break; |
| 6031 | case L2CAP_EV_RECV_SREJ: |
| 6032 | l2cap_handle_srej(chan, control); |
| 6033 | break; |
| 6034 | } |
| 6035 | |
| 6036 | if (skb && !skb_in_use) { |
| 6037 | BT_DBG("Freeing %p", skb); |
| 6038 | kfree_skb(skb); |
| 6039 | } |
| 6040 | |
| 6041 | return err; |
| 6042 | } |
| 6043 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6044 | static int l2cap_finish_move(struct l2cap_chan *chan) |
| 6045 | { |
| 6046 | BT_DBG("chan %p", chan); |
| 6047 | |
| 6048 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6049 | |
| 6050 | if (chan->hs_hcon) |
| 6051 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6052 | else |
| 6053 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6054 | |
| 6055 | return l2cap_resegment(chan); |
| 6056 | } |
| 6057 | |
| 6058 | static int l2cap_rx_state_wait_p(struct l2cap_chan *chan, |
| 6059 | struct l2cap_ctrl *control, |
| 6060 | struct sk_buff *skb, u8 event) |
| 6061 | { |
| 6062 | int err; |
| 6063 | |
| 6064 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6065 | event); |
| 6066 | |
| 6067 | if (!control->poll) |
| 6068 | return -EPROTO; |
| 6069 | |
| 6070 | l2cap_process_reqseq(chan, control->reqseq); |
| 6071 | |
| 6072 | if (!skb_queue_empty(&chan->tx_q)) |
| 6073 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6074 | else |
| 6075 | chan->tx_send_head = NULL; |
| 6076 | |
| 6077 | /* Rewind next_tx_seq to the point expected |
| 6078 | * by the receiver. |
| 6079 | */ |
| 6080 | chan->next_tx_seq = control->reqseq; |
| 6081 | chan->unacked_frames = 0; |
| 6082 | |
| 6083 | err = l2cap_finish_move(chan); |
| 6084 | if (err) |
| 6085 | return err; |
| 6086 | |
| 6087 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6088 | l2cap_send_i_or_rr_or_rnr(chan); |
| 6089 | |
| 6090 | if (event == L2CAP_EV_RECV_IFRAME) |
| 6091 | return -EPROTO; |
| 6092 | |
| 6093 | return l2cap_rx_state_recv(chan, control, NULL, event); |
| 6094 | } |
| 6095 | |
| 6096 | static int l2cap_rx_state_wait_f(struct l2cap_chan *chan, |
| 6097 | struct l2cap_ctrl *control, |
| 6098 | struct sk_buff *skb, u8 event) |
| 6099 | { |
| 6100 | int err; |
| 6101 | |
| 6102 | if (!control->final) |
| 6103 | return -EPROTO; |
| 6104 | |
| 6105 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6106 | |
| 6107 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6108 | l2cap_process_reqseq(chan, control->reqseq); |
| 6109 | |
| 6110 | if (!skb_queue_empty(&chan->tx_q)) |
| 6111 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6112 | else |
| 6113 | chan->tx_send_head = NULL; |
| 6114 | |
| 6115 | /* Rewind next_tx_seq to the point expected |
| 6116 | * by the receiver. |
| 6117 | */ |
| 6118 | chan->next_tx_seq = control->reqseq; |
| 6119 | chan->unacked_frames = 0; |
| 6120 | |
| 6121 | if (chan->hs_hcon) |
| 6122 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6123 | else |
| 6124 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6125 | |
| 6126 | err = l2cap_resegment(chan); |
| 6127 | |
| 6128 | if (!err) |
| 6129 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6130 | |
| 6131 | return err; |
| 6132 | } |
| 6133 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6134 | static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 6135 | { |
| 6136 | /* Make sure reqseq is for a packet that has been sent but not acked */ |
| 6137 | u16 unacked; |
| 6138 | |
| 6139 | unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq); |
| 6140 | return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked; |
| 6141 | } |
| 6142 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6143 | static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6144 | struct sk_buff *skb, u8 event) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6145 | { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6146 | int err = 0; |
| 6147 | |
| 6148 | BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan, |
| 6149 | control, skb, event, chan->rx_state); |
| 6150 | |
| 6151 | if (__valid_reqseq(chan, control->reqseq)) { |
| 6152 | switch (chan->rx_state) { |
| 6153 | case L2CAP_RX_STATE_RECV: |
| 6154 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6155 | break; |
| 6156 | case L2CAP_RX_STATE_SREJ_SENT: |
| 6157 | err = l2cap_rx_state_srej_sent(chan, control, skb, |
| 6158 | event); |
| 6159 | break; |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6160 | case L2CAP_RX_STATE_WAIT_P: |
| 6161 | err = l2cap_rx_state_wait_p(chan, control, skb, event); |
| 6162 | break; |
| 6163 | case L2CAP_RX_STATE_WAIT_F: |
| 6164 | err = l2cap_rx_state_wait_f(chan, control, skb, event); |
| 6165 | break; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6166 | default: |
| 6167 | /* shut it down */ |
| 6168 | break; |
| 6169 | } |
| 6170 | } else { |
| 6171 | BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d", |
| 6172 | control->reqseq, chan->next_tx_seq, |
| 6173 | chan->expected_ack_seq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6174 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6178 | } |
| 6179 | |
| 6180 | static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6181 | struct sk_buff *skb) |
| 6182 | { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6183 | int err = 0; |
| 6184 | |
| 6185 | BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb, |
| 6186 | chan->rx_state); |
| 6187 | |
| 6188 | if (l2cap_classify_txseq(chan, control->txseq) == |
| 6189 | L2CAP_TXSEQ_EXPECTED) { |
| 6190 | l2cap_pass_to_tx(chan, control); |
| 6191 | |
| 6192 | BT_DBG("buffer_seq %d->%d", chan->buffer_seq, |
| 6193 | __next_seq(chan, chan->buffer_seq)); |
| 6194 | |
| 6195 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 6196 | |
| 6197 | l2cap_reassemble_sdu(chan, skb, control); |
| 6198 | } else { |
| 6199 | if (chan->sdu) { |
| 6200 | kfree_skb(chan->sdu); |
| 6201 | chan->sdu = NULL; |
| 6202 | } |
| 6203 | chan->sdu_last_frag = NULL; |
| 6204 | chan->sdu_len = 0; |
| 6205 | |
| 6206 | if (skb) { |
| 6207 | BT_DBG("Freeing %p", skb); |
| 6208 | kfree_skb(skb); |
| 6209 | } |
| 6210 | } |
| 6211 | |
| 6212 | chan->last_acked_seq = control->txseq; |
| 6213 | chan->expected_tx_seq = __next_seq(chan, control->txseq); |
| 6214 | |
| 6215 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6216 | } |
| 6217 | |
| 6218 | static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) |
| 6219 | { |
| 6220 | struct l2cap_ctrl *control = &bt_cb(skb)->control; |
| 6221 | u16 len; |
| 6222 | u8 event; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6223 | |
Mat Martineau | b76bbd6 | 2012-04-11 10:48:43 -0700 | [diff] [blame] | 6224 | __unpack_control(chan, skb); |
| 6225 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6226 | len = skb->len; |
| 6227 | |
| 6228 | /* |
| 6229 | * We can just drop the corrupted I-frame here. |
| 6230 | * Receiver will miss it and start proper recovery |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6231 | * procedures and ask for retransmission. |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6232 | */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6233 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6234 | goto drop; |
| 6235 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6236 | if (!control->sframe && control->sar == L2CAP_SAR_START) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6237 | len -= L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6238 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6239 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6240 | len -= L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6241 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6242 | if (len > chan->mps) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6243 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6244 | goto drop; |
| 6245 | } |
| 6246 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6247 | if (!control->sframe) { |
| 6248 | int err; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6249 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6250 | BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d", |
| 6251 | control->sar, control->reqseq, control->final, |
| 6252 | control->txseq); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 6253 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6254 | /* Validate F-bit - F=0 always valid, F=1 only |
| 6255 | * valid in TX WAIT_F |
| 6256 | */ |
| 6257 | if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6258 | goto drop; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6259 | |
| 6260 | if (chan->mode != L2CAP_MODE_STREAMING) { |
| 6261 | event = L2CAP_EV_RECV_IFRAME; |
| 6262 | err = l2cap_rx(chan, control, skb, event); |
| 6263 | } else { |
| 6264 | err = l2cap_stream_rx(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6265 | } |
| 6266 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6267 | if (err) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6268 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6269 | } else { |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6270 | const u8 rx_func_to_event[4] = { |
| 6271 | L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ, |
| 6272 | L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ |
| 6273 | }; |
| 6274 | |
| 6275 | /* Only I-frames are expected in streaming mode */ |
| 6276 | if (chan->mode == L2CAP_MODE_STREAMING) |
| 6277 | goto drop; |
| 6278 | |
| 6279 | BT_DBG("sframe reqseq %d, final %d, poll %d, super %d", |
| 6280 | control->reqseq, control->final, control->poll, |
| 6281 | control->super); |
| 6282 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6283 | if (len != 0) { |
Andrei Emeltchenko | 1bb166e | 2012-11-20 17:16:21 +0200 | [diff] [blame] | 6284 | BT_ERR("Trailing bytes: %d in sframe", len); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6285 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6286 | goto drop; |
| 6287 | } |
| 6288 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6289 | /* Validate F and P bits */ |
| 6290 | if (control->final && (control->poll || |
| 6291 | chan->tx_state != L2CAP_TX_STATE_WAIT_F)) |
| 6292 | goto drop; |
| 6293 | |
| 6294 | event = rx_func_to_event[control->super]; |
| 6295 | if (l2cap_rx(chan, control, skb, event)) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6296 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6297 | } |
| 6298 | |
| 6299 | return 0; |
| 6300 | |
| 6301 | drop: |
| 6302 | kfree_skb(skb); |
| 6303 | return 0; |
| 6304 | } |
| 6305 | |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6306 | static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid, |
| 6307 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6308 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6309 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6310 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 6311 | chan = l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6312 | if (!chan) { |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6313 | if (cid == L2CAP_CID_A2MP) { |
| 6314 | chan = a2mp_channel_create(conn, skb); |
| 6315 | if (!chan) { |
| 6316 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6317 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6318 | } |
| 6319 | |
| 6320 | l2cap_chan_lock(chan); |
| 6321 | } else { |
| 6322 | BT_DBG("unknown cid 0x%4.4x", cid); |
| 6323 | /* Drop packet and return */ |
| 6324 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6325 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6327 | } |
| 6328 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 6329 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6330 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6331 | if (chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6332 | goto drop; |
| 6333 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6334 | switch (chan->mode) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6335 | case L2CAP_MODE_BASIC: |
| 6336 | /* If socket recv buffers overflows we drop data here |
| 6337 | * which is *bad* because L2CAP has to be reliable. |
| 6338 | * But we don't have any other choice. L2CAP doesn't |
| 6339 | * provide flow control mechanism. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6340 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6341 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6342 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6343 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6344 | if (!chan->ops->recv(chan, skb)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6345 | goto done; |
| 6346 | break; |
| 6347 | |
| 6348 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6349 | case L2CAP_MODE_STREAMING: |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6350 | l2cap_data_rcv(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6351 | goto done; |
| 6352 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6353 | default: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6354 | 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] | 6355 | break; |
| 6356 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6357 | |
| 6358 | drop: |
| 6359 | kfree_skb(skb); |
| 6360 | |
| 6361 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6362 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6363 | } |
| 6364 | |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6365 | static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, |
| 6366 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6367 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6368 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6369 | |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 6370 | chan = l2cap_global_chan_by_psm(0, psm, conn->src, conn->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6371 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6372 | goto drop; |
| 6373 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 6374 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6376 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6377 | goto drop; |
| 6378 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 6379 | if (chan->imtu < skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6380 | goto drop; |
| 6381 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6382 | if (!chan->ops->recv(chan, skb)) |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6383 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6384 | |
| 6385 | drop: |
| 6386 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6387 | } |
| 6388 | |
Marcel Holtmann | 72f7835 | 2013-04-23 00:59:00 -0700 | [diff] [blame] | 6389 | static void l2cap_att_channel(struct l2cap_conn *conn, |
Andrei Emeltchenko | 6810fca | 2012-05-31 11:18:57 +0300 | [diff] [blame] | 6390 | struct sk_buff *skb) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6391 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6392 | struct l2cap_chan *chan; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6393 | |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame] | 6394 | chan = l2cap_global_chan_by_scid(0, L2CAP_CID_ATT, |
Marcel Holtmann | 72f7835 | 2013-04-23 00:59:00 -0700 | [diff] [blame] | 6395 | conn->src, conn->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6396 | if (!chan) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6397 | goto drop; |
| 6398 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 6399 | BT_DBG("chan %p, len %d", chan, skb->len); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6400 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6401 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6402 | goto drop; |
| 6403 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 6404 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6405 | goto drop; |
| 6406 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6407 | if (!chan->ops->recv(chan, skb)) |
Andrei Emeltchenko | 6810fca | 2012-05-31 11:18:57 +0300 | [diff] [blame] | 6408 | return; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6409 | |
| 6410 | drop: |
| 6411 | kfree_skb(skb); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6412 | } |
| 6413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6414 | static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) |
| 6415 | { |
| 6416 | struct l2cap_hdr *lh = (void *) skb->data; |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 6417 | u16 cid, len; |
| 6418 | __le16 psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6419 | |
| 6420 | skb_pull(skb, L2CAP_HDR_SIZE); |
| 6421 | cid = __le16_to_cpu(lh->cid); |
| 6422 | len = __le16_to_cpu(lh->len); |
| 6423 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6424 | if (len != skb->len) { |
| 6425 | kfree_skb(skb); |
| 6426 | return; |
| 6427 | } |
| 6428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6429 | BT_DBG("len %d, cid 0x%4.4x", len, cid); |
| 6430 | |
| 6431 | switch (cid) { |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 6432 | case L2CAP_CID_LE_SIGNALING: |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 6433 | l2cap_le_sig_channel(conn, skb); |
| 6434 | break; |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6435 | case L2CAP_CID_SIGNALING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6436 | l2cap_sig_channel(conn, skb); |
| 6437 | break; |
| 6438 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6439 | case L2CAP_CID_CONN_LESS: |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 6440 | psm = get_unaligned((__le16 *) skb->data); |
Andrei Emeltchenko | 0181a70 | 2012-05-29 10:04:05 +0300 | [diff] [blame] | 6441 | skb_pull(skb, L2CAP_PSMLEN_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6442 | l2cap_conless_channel(conn, psm, skb); |
| 6443 | break; |
| 6444 | |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame] | 6445 | case L2CAP_CID_ATT: |
Marcel Holtmann | 72f7835 | 2013-04-23 00:59:00 -0700 | [diff] [blame] | 6446 | l2cap_att_channel(conn, skb); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6447 | break; |
| 6448 | |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 6449 | case L2CAP_CID_SMP: |
| 6450 | if (smp_sig_channel(conn, skb)) |
| 6451 | l2cap_conn_del(conn->hcon, EACCES); |
| 6452 | break; |
| 6453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6454 | default: |
| 6455 | l2cap_data_channel(conn, cid, skb); |
| 6456 | break; |
| 6457 | } |
| 6458 | } |
| 6459 | |
| 6460 | /* ---- L2CAP interface with lower layer (HCI) ---- */ |
| 6461 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6462 | int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6463 | { |
| 6464 | int exact = 0, lm1 = 0, lm2 = 0; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6465 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6466 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 6467 | BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6468 | |
| 6469 | /* Find listening sockets and check their link_mode */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6470 | read_lock(&chan_list_lock); |
| 6471 | list_for_each_entry(c, &chan_list, global_l) { |
| 6472 | struct sock *sk = c->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6473 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6474 | if (c->state != BT_LISTEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6475 | continue; |
| 6476 | |
| 6477 | if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6478 | lm1 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 6479 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6480 | lm1 |= HCI_LM_MASTER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6481 | exact++; |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6482 | } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { |
| 6483 | lm2 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 6484 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6485 | lm2 |= HCI_LM_MASTER; |
| 6486 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6487 | } |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6488 | read_unlock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6489 | |
| 6490 | return exact ? lm1 : lm2; |
| 6491 | } |
| 6492 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 6493 | void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6494 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 6495 | struct l2cap_conn *conn; |
| 6496 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 6497 | BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6499 | if (!status) { |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 6500 | conn = l2cap_conn_add(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6501 | if (conn) |
| 6502 | l2cap_conn_ready(conn); |
Andrei Emeltchenko | ba6fc31 | 2012-10-31 15:46:26 +0200 | [diff] [blame] | 6503 | } else { |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 6504 | l2cap_conn_del(hcon, bt_to_errno(status)); |
Andrei Emeltchenko | ba6fc31 | 2012-10-31 15:46:26 +0200 | [diff] [blame] | 6505 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6506 | } |
| 6507 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6508 | int l2cap_disconn_ind(struct hci_conn *hcon) |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 6509 | { |
| 6510 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 6511 | |
| 6512 | BT_DBG("hcon %p", hcon); |
| 6513 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6514 | if (!conn) |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 6515 | return HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 6516 | return conn->disc_reason; |
| 6517 | } |
| 6518 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 6519 | void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6520 | { |
| 6521 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 6522 | |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 6523 | l2cap_conn_del(hcon, bt_to_errno(reason)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6524 | } |
| 6525 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6526 | static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6527 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 6528 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
Marcel Holtmann | 255c760 | 2009-02-04 21:07:19 +0100 | [diff] [blame] | 6529 | return; |
| 6530 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6531 | if (encrypt == 0x00) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6532 | if (chan->sec_level == BT_SECURITY_MEDIUM) { |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 6533 | __set_chan_timer(chan, L2CAP_ENC_TIMEOUT); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6534 | } else if (chan->sec_level == BT_SECURITY_HIGH) |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 6535 | l2cap_chan_close(chan, ECONNREFUSED); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6536 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6537 | if (chan->sec_level == BT_SECURITY_MEDIUM) |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 6538 | __clear_chan_timer(chan); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6539 | } |
| 6540 | } |
| 6541 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6542 | int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6543 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 6544 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6545 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6546 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 6547 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6548 | return 0; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 6549 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 6550 | 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] | 6551 | |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 6552 | if (hcon->type == LE_LINK) { |
Hemant Gupta | 35d4adcc | 2012-04-18 14:46:26 +0530 | [diff] [blame] | 6553 | if (!status && encrypt) |
| 6554 | smp_distribute_keys(conn, 0); |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 6555 | cancel_delayed_work(&conn->security_timer); |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 6556 | } |
| 6557 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 6558 | mutex_lock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6559 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 6560 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6561 | l2cap_chan_lock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6562 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 6563 | BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, |
| 6564 | state_to_string(chan->state)); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6565 | |
Andrei Emeltchenko | 78eb2f9 | 2012-07-19 17:03:47 +0300 | [diff] [blame] | 6566 | if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) { |
| 6567 | l2cap_chan_unlock(chan); |
| 6568 | continue; |
| 6569 | } |
| 6570 | |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame] | 6571 | if (chan->scid == L2CAP_CID_ATT) { |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6572 | if (!status && encrypt) { |
| 6573 | chan->sec_level = hcon->sec_level; |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 6574 | l2cap_chan_ready(chan); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6575 | } |
| 6576 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6577 | l2cap_chan_unlock(chan); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6578 | continue; |
| 6579 | } |
| 6580 | |
Andrei Emeltchenko | 96eff46 | 2012-11-15 18:14:53 +0200 | [diff] [blame] | 6581 | if (!__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6582 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 6583 | continue; |
| 6584 | } |
| 6585 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6586 | if (!status && (chan->state == BT_CONNECTED || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6587 | chan->state == BT_CONFIG)) { |
Gustavo Padovan | a7d7723 | 2012-05-13 03:20:07 -0300 | [diff] [blame] | 6588 | struct sock *sk = chan->sk; |
| 6589 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 6590 | clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); |
Gustavo Padovan | a7d7723 | 2012-05-13 03:20:07 -0300 | [diff] [blame] | 6591 | sk->sk_state_change(sk); |
| 6592 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6593 | l2cap_check_encryption(chan, encrypt); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6594 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 9719f8a | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 6595 | continue; |
| 6596 | } |
| 6597 | |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6598 | if (chan->state == BT_CONNECT) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6599 | if (!status) { |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 6600 | l2cap_start_connection(chan); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6601 | } else { |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 6602 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6603 | } |
Gustavo F. Padovan | 89bc500e | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6604 | } else if (chan->state == BT_CONNECT2) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6605 | struct sock *sk = chan->sk; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6606 | struct l2cap_conn_rsp rsp; |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6607 | __u16 res, stat; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6608 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6609 | lock_sock(sk); |
| 6610 | |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6611 | if (!status) { |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 6612 | if (test_bit(BT_SK_DEFER_SETUP, |
| 6613 | &bt_sk(sk)->flags)) { |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6614 | res = L2CAP_CR_PEND; |
| 6615 | stat = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 6616 | chan->ops->defer(chan); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6617 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 6618 | __l2cap_state_change(chan, BT_CONFIG); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6619 | res = L2CAP_CR_SUCCESS; |
| 6620 | stat = L2CAP_CS_NO_INFO; |
| 6621 | } |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6622 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 6623 | __l2cap_state_change(chan, BT_DISCONN); |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 6624 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6625 | res = L2CAP_CR_SEC_BLOCK; |
| 6626 | stat = L2CAP_CS_NO_INFO; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6627 | } |
| 6628 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6629 | release_sock(sk); |
| 6630 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 6631 | rsp.scid = cpu_to_le16(chan->dcid); |
| 6632 | rsp.dcid = cpu_to_le16(chan->scid); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6633 | rsp.result = cpu_to_le16(res); |
| 6634 | rsp.status = cpu_to_le16(stat); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 6635 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6636 | sizeof(rsp), &rsp); |
Mat Martineau | 2d36935 | 2012-05-23 14:59:30 -0700 | [diff] [blame] | 6637 | |
| 6638 | if (!test_bit(CONF_REQ_SENT, &chan->conf_state) && |
| 6639 | res == L2CAP_CR_SUCCESS) { |
| 6640 | char buf[128]; |
| 6641 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 6642 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 6643 | L2CAP_CONF_REQ, |
| 6644 | l2cap_build_conf_req(chan, buf), |
| 6645 | buf); |
| 6646 | chan->num_conf_req++; |
| 6647 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | } |
| 6649 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6650 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6651 | } |
| 6652 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 6653 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6655 | return 0; |
| 6656 | } |
| 6657 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6658 | 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] | 6659 | { |
| 6660 | struct l2cap_conn *conn = hcon->l2cap_data; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6661 | struct l2cap_hdr *hdr; |
| 6662 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6663 | |
Andrei Emeltchenko | 1d13a25 | 2012-10-15 11:58:41 +0300 | [diff] [blame] | 6664 | /* For AMP controller do not create l2cap conn */ |
| 6665 | if (!conn && hcon->hdev->dev_type != HCI_BREDR) |
| 6666 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6667 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 6668 | if (!conn) |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 6669 | conn = l2cap_conn_add(hcon); |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 6670 | |
| 6671 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6672 | goto drop; |
| 6673 | |
| 6674 | BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); |
| 6675 | |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6676 | switch (flags) { |
| 6677 | case ACL_START: |
| 6678 | case ACL_START_NO_FLUSH: |
| 6679 | case ACL_COMPLETE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6680 | if (conn->rx_len) { |
| 6681 | BT_ERR("Unexpected start frame (len %d)", skb->len); |
| 6682 | kfree_skb(conn->rx_skb); |
| 6683 | conn->rx_skb = NULL; |
| 6684 | conn->rx_len = 0; |
| 6685 | l2cap_conn_unreliable(conn, ECOMM); |
| 6686 | } |
| 6687 | |
Andrei Emeltchenko | aae7fe2 | 2010-09-15 14:28:43 +0300 | [diff] [blame] | 6688 | /* Start fragment always begin with Basic L2CAP header */ |
| 6689 | if (skb->len < L2CAP_HDR_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6690 | BT_ERR("Frame is too short (len %d)", skb->len); |
| 6691 | l2cap_conn_unreliable(conn, ECOMM); |
| 6692 | goto drop; |
| 6693 | } |
| 6694 | |
| 6695 | hdr = (struct l2cap_hdr *) skb->data; |
| 6696 | len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; |
| 6697 | |
| 6698 | if (len == skb->len) { |
| 6699 | /* Complete frame received */ |
| 6700 | l2cap_recv_frame(conn, skb); |
| 6701 | return 0; |
| 6702 | } |
| 6703 | |
| 6704 | BT_DBG("Start: total len %d, frag len %d", len, skb->len); |
| 6705 | |
| 6706 | if (skb->len > len) { |
| 6707 | BT_ERR("Frame is too long (len %d, expected len %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6708 | skb->len, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6709 | l2cap_conn_unreliable(conn, ECOMM); |
| 6710 | goto drop; |
| 6711 | } |
| 6712 | |
| 6713 | /* Allocate skb for the complete frame (with header) */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 6714 | conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 6715 | if (!conn->rx_skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6716 | goto drop; |
| 6717 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 6718 | 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] | 6719 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6720 | conn->rx_len = len - skb->len; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6721 | break; |
| 6722 | |
| 6723 | case ACL_CONT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6724 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
| 6725 | |
| 6726 | if (!conn->rx_len) { |
| 6727 | BT_ERR("Unexpected continuation frame (len %d)", skb->len); |
| 6728 | l2cap_conn_unreliable(conn, ECOMM); |
| 6729 | goto drop; |
| 6730 | } |
| 6731 | |
| 6732 | if (skb->len > conn->rx_len) { |
| 6733 | BT_ERR("Fragment is too long (len %d, expected %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6734 | skb->len, conn->rx_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6735 | kfree_skb(conn->rx_skb); |
| 6736 | conn->rx_skb = NULL; |
| 6737 | conn->rx_len = 0; |
| 6738 | l2cap_conn_unreliable(conn, ECOMM); |
| 6739 | goto drop; |
| 6740 | } |
| 6741 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 6742 | 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] | 6743 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6744 | conn->rx_len -= skb->len; |
| 6745 | |
| 6746 | if (!conn->rx_len) { |
| 6747 | /* Complete frame received */ |
| 6748 | l2cap_recv_frame(conn, conn->rx_skb); |
| 6749 | conn->rx_skb = NULL; |
| 6750 | } |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6751 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | } |
| 6753 | |
| 6754 | drop: |
| 6755 | kfree_skb(skb); |
| 6756 | return 0; |
| 6757 | } |
| 6758 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6759 | static int l2cap_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6760 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6761 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6762 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 6763 | read_lock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6764 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6765 | list_for_each_entry(c, &chan_list, global_l) { |
| 6766 | struct sock *sk = c->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6767 | |
Andrei Emeltchenko | fcb7333 | 2012-09-25 12:49:44 +0300 | [diff] [blame] | 6768 | seq_printf(f, "%pMR %pMR %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", |
| 6769 | &bt_sk(sk)->src, &bt_sk(sk)->dst, |
| 6770 | c->state, __le16_to_cpu(c->psm), |
| 6771 | c->scid, c->dcid, c->imtu, c->omtu, |
| 6772 | c->sec_level, c->mode); |
Andrei Emeltchenko | 61e1b4b | 2012-01-19 11:19:50 +0200 | [diff] [blame] | 6773 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6774 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 6775 | read_unlock(&chan_list_lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 6776 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6777 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6778 | } |
| 6779 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6780 | static int l2cap_debugfs_open(struct inode *inode, struct file *file) |
| 6781 | { |
| 6782 | return single_open(file, l2cap_debugfs_show, inode->i_private); |
| 6783 | } |
| 6784 | |
| 6785 | static const struct file_operations l2cap_debugfs_fops = { |
| 6786 | .open = l2cap_debugfs_open, |
| 6787 | .read = seq_read, |
| 6788 | .llseek = seq_lseek, |
| 6789 | .release = single_release, |
| 6790 | }; |
| 6791 | |
| 6792 | static struct dentry *l2cap_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6793 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 6794 | int __init l2cap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6795 | { |
| 6796 | int err; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 6797 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 6798 | err = l2cap_init_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6799 | if (err < 0) |
| 6800 | return err; |
| 6801 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6802 | if (bt_debugfs) { |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6803 | l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs, |
| 6804 | NULL, &l2cap_debugfs_fops); |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6805 | if (!l2cap_debugfs) |
| 6806 | BT_ERR("Failed to create L2CAP debug file"); |
| 6807 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6809 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6810 | } |
| 6811 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 6812 | void l2cap_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6813 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6814 | debugfs_remove(l2cap_debugfs); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 6815 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6816 | } |
| 6817 | |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 6818 | module_param(disable_ertm, bool, 0644); |
| 6819 | MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); |