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 | 710f9b0a | 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 | 89bc500 | 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 | 89bc500 | 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 | 89bc500 | 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 | 89bc500 | 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 | 89bc500 | 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 | 89bc500 | 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 | 89bc500 | 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 | 89bc500 | 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 | 89bc500 | 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 | 89bc500 | 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 | 89bc500 | 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 | 89bc500 | 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 | 89bc500 | 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 | 89bc500 | 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 | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame^] | 1795 | if (chan->dcid == L2CAP_CID_ATT) |
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 | |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1814 | if (hcon->type == LE_LINK) { |
| 1815 | err = 0; |
| 1816 | |
| 1817 | if (!list_empty(&conn->chan_l)) { |
| 1818 | err = -EBUSY; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 1819 | hci_conn_drop(hcon); |
Vinicius Costa Gomes | 9f0caeb | 2012-04-20 15:46:08 -0300 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | if (err) |
| 1823 | goto done; |
| 1824 | } |
| 1825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | /* Update source addr of the socket */ |
| 1827 | bacpy(src, conn->src); |
| 1828 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1829 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1830 | l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1831 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1832 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1833 | l2cap_state_change(chan, BT_CONNECT); |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1834 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | |
| 1836 | if (hcon->state == BT_CONNECTED) { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1837 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1838 | __clear_chan_timer(chan); |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1839 | if (l2cap_chan_check_security(chan)) |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1840 | l2cap_state_change(chan, BT_CONNECTED); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1841 | } else |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1842 | l2cap_do_start(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1845 | err = 0; |
| 1846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1848 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1849 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | hci_dev_put(hdev); |
| 1851 | return err; |
| 1852 | } |
| 1853 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1854 | int __l2cap_wait_ack(struct sock *sk) |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1855 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1856 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1857 | DECLARE_WAITQUEUE(wait, current); |
| 1858 | int err = 0; |
| 1859 | int timeo = HZ/5; |
| 1860 | |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1861 | add_wait_queue(sk_sleep(sk), &wait); |
Peter Hurley | a71a0cf | 2011-07-25 18:36:26 -0400 | [diff] [blame] | 1862 | set_current_state(TASK_INTERRUPTIBLE); |
| 1863 | while (chan->unacked_frames > 0 && chan->conn) { |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1864 | if (!timeo) |
| 1865 | timeo = HZ/5; |
| 1866 | |
| 1867 | if (signal_pending(current)) { |
| 1868 | err = sock_intr_errno(timeo); |
| 1869 | break; |
| 1870 | } |
| 1871 | |
| 1872 | release_sock(sk); |
| 1873 | timeo = schedule_timeout(timeo); |
| 1874 | lock_sock(sk); |
Peter Hurley | a71a0cf | 2011-07-25 18:36:26 -0400 | [diff] [blame] | 1875 | set_current_state(TASK_INTERRUPTIBLE); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1876 | |
| 1877 | err = sock_error(sk); |
| 1878 | if (err) |
| 1879 | break; |
| 1880 | } |
| 1881 | set_current_state(TASK_RUNNING); |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1882 | remove_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1883 | return err; |
| 1884 | } |
| 1885 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1886 | static void l2cap_monitor_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1887 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1888 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1889 | monitor_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1890 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1891 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1892 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1893 | l2cap_chan_lock(chan); |
| 1894 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1895 | if (!chan->conn) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1896 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1897 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1898 | return; |
| 1899 | } |
| 1900 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1901 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_MONITOR_TO); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1902 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1903 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1904 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1905 | } |
| 1906 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1907 | static void l2cap_retrans_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1908 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1909 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 4239d16 | 2012-05-17 20:53:49 -0700 | [diff] [blame] | 1910 | retrans_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1911 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1912 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1913 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1914 | l2cap_chan_lock(chan); |
| 1915 | |
Mat Martineau | 80909e0 | 2012-05-17 20:53:50 -0700 | [diff] [blame] | 1916 | if (!chan->conn) { |
| 1917 | l2cap_chan_unlock(chan); |
| 1918 | l2cap_chan_put(chan); |
| 1919 | return; |
| 1920 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1921 | |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 1922 | l2cap_tx(chan, NULL, NULL, L2CAP_EV_RETRANS_TO); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1923 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1924 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1925 | } |
| 1926 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 1927 | static void l2cap_streaming_send(struct l2cap_chan *chan, |
| 1928 | struct sk_buff_head *skbs) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1929 | { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1930 | struct sk_buff *skb; |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1931 | struct l2cap_ctrl *control; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1932 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1933 | BT_DBG("chan %p, skbs %p", chan, skbs); |
| 1934 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1935 | if (__chan_is_moving(chan)) |
| 1936 | return; |
| 1937 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1938 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 1939 | |
| 1940 | while (!skb_queue_empty(&chan->tx_q)) { |
| 1941 | |
| 1942 | skb = skb_dequeue(&chan->tx_q); |
| 1943 | |
| 1944 | bt_cb(skb)->control.retries = 1; |
| 1945 | control = &bt_cb(skb)->control; |
| 1946 | |
| 1947 | control->reqseq = 0; |
| 1948 | control->txseq = chan->next_tx_seq; |
| 1949 | |
| 1950 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1951 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1952 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1953 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 1954 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1955 | } |
| 1956 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1957 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1958 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 1959 | BT_DBG("Sent txseq %u", control->txseq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1960 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1961 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 1962 | chan->frames_sent++; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1963 | } |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1964 | } |
| 1965 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1966 | static int l2cap_ertm_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1967 | { |
| 1968 | struct sk_buff *skb, *tx_skb; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1969 | struct l2cap_ctrl *control; |
| 1970 | int sent = 0; |
| 1971 | |
| 1972 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1973 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1974 | if (chan->state != BT_CONNECTED) |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1975 | return -ENOTCONN; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1976 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 1977 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 1978 | return 0; |
| 1979 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 1980 | if (__chan_is_moving(chan)) |
| 1981 | return 0; |
| 1982 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1983 | while (chan->tx_send_head && |
| 1984 | chan->unacked_frames < chan->remote_tx_win && |
| 1985 | chan->tx_state == L2CAP_TX_STATE_XMIT) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1986 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1987 | skb = chan->tx_send_head; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1988 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1989 | bt_cb(skb)->control.retries = 1; |
| 1990 | control = &bt_cb(skb)->control; |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1991 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1992 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1993 | control->final = 1; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1994 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1995 | control->reqseq = chan->buffer_seq; |
| 1996 | chan->last_acked_seq = chan->buffer_seq; |
| 1997 | control->txseq = chan->next_tx_seq; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1998 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 1999 | __pack_control(chan, control, skb); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2000 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2001 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2002 | u16 fcs = crc16(0, (u8 *) skb->data, skb->len); |
| 2003 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2004 | } |
| 2005 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2006 | /* Clone after data has been modified. Data is assumed to be |
| 2007 | read-only (for locking purposes) on cloned sk_buffs. |
| 2008 | */ |
| 2009 | tx_skb = skb_clone(skb, GFP_KERNEL); |
| 2010 | |
| 2011 | if (!tx_skb) |
| 2012 | break; |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 2013 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 2014 | __set_retrans_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2015 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2016 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2017 | chan->unacked_frames++; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 2018 | chan->frames_sent++; |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2019 | sent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2020 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2021 | if (skb_queue_is_last(&chan->tx_q, skb)) |
| 2022 | chan->tx_send_head = NULL; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2023 | else |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2024 | chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2025 | |
| 2026 | l2cap_do_send(chan, tx_skb); |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2027 | BT_DBG("Sent txseq %u", control->txseq); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2028 | } |
| 2029 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2030 | BT_DBG("Sent %d, %u unacked, %u in ERTM queue", sent, |
| 2031 | chan->unacked_frames, skb_queue_len(&chan->tx_q)); |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2032 | |
| 2033 | return sent; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 2034 | } |
| 2035 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2036 | static void l2cap_ertm_resend(struct l2cap_chan *chan) |
| 2037 | { |
| 2038 | struct l2cap_ctrl control; |
| 2039 | struct sk_buff *skb; |
| 2040 | struct sk_buff *tx_skb; |
| 2041 | u16 seq; |
| 2042 | |
| 2043 | BT_DBG("chan %p", chan); |
| 2044 | |
| 2045 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 2046 | return; |
| 2047 | |
Mat Martineau | b99e13a | 2012-10-23 15:24:19 -0700 | [diff] [blame] | 2048 | if (__chan_is_moving(chan)) |
| 2049 | return; |
| 2050 | |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2051 | while (chan->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) { |
| 2052 | seq = l2cap_seq_list_pop(&chan->retrans_list); |
| 2053 | |
| 2054 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, seq); |
| 2055 | if (!skb) { |
| 2056 | BT_DBG("Error: Can't retransmit seq %d, frame missing", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2057 | seq); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2058 | continue; |
| 2059 | } |
| 2060 | |
| 2061 | bt_cb(skb)->control.retries++; |
| 2062 | control = bt_cb(skb)->control; |
| 2063 | |
| 2064 | if (chan->max_tx != 0 && |
| 2065 | bt_cb(skb)->control.retries > chan->max_tx) { |
| 2066 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 2067 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2068 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2069 | break; |
| 2070 | } |
| 2071 | |
| 2072 | control.reqseq = chan->buffer_seq; |
| 2073 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
| 2074 | control.final = 1; |
| 2075 | else |
| 2076 | control.final = 0; |
| 2077 | |
| 2078 | if (skb_cloned(skb)) { |
| 2079 | /* Cloned sk_buffs are read-only, so we need a |
| 2080 | * writeable copy |
| 2081 | */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2082 | tx_skb = skb_copy(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2083 | } else { |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2084 | tx_skb = skb_clone(skb, GFP_KERNEL); |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | if (!tx_skb) { |
| 2088 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2089 | break; |
| 2090 | } |
| 2091 | |
| 2092 | /* Update skb contents */ |
| 2093 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { |
| 2094 | put_unaligned_le32(__pack_extended_control(&control), |
| 2095 | tx_skb->data + L2CAP_HDR_SIZE); |
| 2096 | } else { |
| 2097 | put_unaligned_le16(__pack_enhanced_control(&control), |
| 2098 | tx_skb->data + L2CAP_HDR_SIZE); |
| 2099 | } |
| 2100 | |
| 2101 | if (chan->fcs == L2CAP_FCS_CRC16) { |
| 2102 | u16 fcs = crc16(0, (u8 *) tx_skb->data, tx_skb->len); |
| 2103 | put_unaligned_le16(fcs, skb_put(tx_skb, |
| 2104 | L2CAP_FCS_SIZE)); |
| 2105 | } |
| 2106 | |
| 2107 | l2cap_do_send(chan, tx_skb); |
| 2108 | |
| 2109 | BT_DBG("Resent txseq %d", control.txseq); |
| 2110 | |
| 2111 | chan->last_acked_seq = chan->buffer_seq; |
| 2112 | } |
| 2113 | } |
| 2114 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2115 | static void l2cap_retransmit(struct l2cap_chan *chan, |
| 2116 | struct l2cap_ctrl *control) |
| 2117 | { |
| 2118 | BT_DBG("chan %p, control %p", chan, control); |
| 2119 | |
| 2120 | l2cap_seq_list_append(&chan->retrans_list, control->reqseq); |
| 2121 | l2cap_ertm_resend(chan); |
| 2122 | } |
| 2123 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2124 | static void l2cap_retransmit_all(struct l2cap_chan *chan, |
| 2125 | struct l2cap_ctrl *control) |
| 2126 | { |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2127 | struct sk_buff *skb; |
| 2128 | |
| 2129 | BT_DBG("chan %p, control %p", chan, control); |
| 2130 | |
| 2131 | if (control->poll) |
| 2132 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 2133 | |
| 2134 | l2cap_seq_list_clear(&chan->retrans_list); |
| 2135 | |
| 2136 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
| 2137 | return; |
| 2138 | |
| 2139 | if (chan->unacked_frames) { |
| 2140 | skb_queue_walk(&chan->tx_q, skb) { |
| 2141 | if (bt_cb(skb)->control.txseq == control->reqseq || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2142 | skb == chan->tx_send_head) |
Mat Martineau | e1fbd4c | 2012-05-17 20:53:43 -0700 | [diff] [blame] | 2143 | break; |
| 2144 | } |
| 2145 | |
| 2146 | skb_queue_walk_from(&chan->tx_q, skb) { |
| 2147 | if (skb == chan->tx_send_head) |
| 2148 | break; |
| 2149 | |
| 2150 | l2cap_seq_list_append(&chan->retrans_list, |
| 2151 | bt_cb(skb)->control.txseq); |
| 2152 | } |
| 2153 | |
| 2154 | l2cap_ertm_resend(chan); |
| 2155 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2156 | } |
| 2157 | |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2158 | static void l2cap_send_ack(struct l2cap_chan *chan) |
| 2159 | { |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2160 | struct l2cap_ctrl control; |
| 2161 | u16 frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 2162 | chan->last_acked_seq); |
| 2163 | int threshold; |
| 2164 | |
| 2165 | BT_DBG("chan %p last_acked_seq %d buffer_seq %d", |
| 2166 | chan, chan->last_acked_seq, chan->buffer_seq); |
| 2167 | |
| 2168 | memset(&control, 0, sizeof(control)); |
| 2169 | control.sframe = 1; |
| 2170 | |
| 2171 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
| 2172 | chan->rx_state == L2CAP_RX_STATE_RECV) { |
| 2173 | __clear_ack_timer(chan); |
| 2174 | control.super = L2CAP_SUPER_RNR; |
| 2175 | control.reqseq = chan->buffer_seq; |
| 2176 | l2cap_send_sframe(chan, &control); |
| 2177 | } else { |
| 2178 | if (!test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) { |
| 2179 | l2cap_ertm_send(chan); |
| 2180 | /* If any i-frames were sent, they included an ack */ |
| 2181 | if (chan->buffer_seq == chan->last_acked_seq) |
| 2182 | frames_to_ack = 0; |
| 2183 | } |
| 2184 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2185 | /* Ack now if the window is 3/4ths full. |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2186 | * Calculate without mul or div |
| 2187 | */ |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 2188 | threshold = chan->ack_win; |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2189 | threshold += threshold << 1; |
| 2190 | threshold >>= 2; |
| 2191 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2192 | BT_DBG("frames_to_ack %u, threshold %d", frames_to_ack, |
Mat Martineau | 0a0aba4 | 2012-05-17 20:53:39 -0700 | [diff] [blame] | 2193 | threshold); |
| 2194 | |
| 2195 | if (frames_to_ack >= threshold) { |
| 2196 | __clear_ack_timer(chan); |
| 2197 | control.super = L2CAP_SUPER_RR; |
| 2198 | control.reqseq = chan->buffer_seq; |
| 2199 | l2cap_send_sframe(chan, &control); |
| 2200 | frames_to_ack = 0; |
| 2201 | } |
| 2202 | |
| 2203 | if (frames_to_ack) |
| 2204 | __set_ack_timer(chan); |
| 2205 | } |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2206 | } |
| 2207 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2208 | static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, |
| 2209 | struct msghdr *msg, int len, |
| 2210 | int count, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | { |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2212 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2213 | struct sk_buff **frag; |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2214 | int sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | |
Gustavo F. Padovan | 59203a2 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 2216 | if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2217 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | |
| 2219 | sent += count; |
| 2220 | len -= count; |
| 2221 | |
| 2222 | /* Continuation fragments (no L2CAP header) */ |
| 2223 | frag = &skb_shinfo(skb)->frag_list; |
| 2224 | while (len) { |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2225 | struct sk_buff *tmp; |
| 2226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | count = min_t(unsigned int, conn->mtu, len); |
| 2228 | |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2229 | tmp = chan->ops->alloc_skb(chan, count, |
| 2230 | msg->msg_flags & MSG_DONTWAIT); |
| 2231 | if (IS_ERR(tmp)) |
| 2232 | return PTR_ERR(tmp); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2233 | |
Gustavo Padovan | fbe0070 | 2012-05-15 13:22:55 -0300 | [diff] [blame] | 2234 | *frag = tmp; |
| 2235 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2236 | if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) |
| 2237 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2239 | (*frag)->priority = skb->priority; |
| 2240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | sent += count; |
| 2242 | len -= count; |
| 2243 | |
Gustavo Padovan | 2d0ed3d | 2012-05-11 13:16:12 -0300 | [diff] [blame] | 2244 | skb->len += (*frag)->len; |
| 2245 | skb->data_len += (*frag)->len; |
| 2246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | frag = &(*frag)->next; |
| 2248 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | |
| 2250 | return sent; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2253 | static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2254 | struct msghdr *msg, size_t len, |
| 2255 | u32 priority) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2256 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2257 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2258 | struct sk_buff *skb; |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2259 | int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2260 | struct l2cap_hdr *lh; |
| 2261 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2262 | BT_DBG("chan %p len %zu priority %u", chan, len, priority); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2263 | |
| 2264 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2265 | |
| 2266 | skb = chan->ops->alloc_skb(chan, count + hlen, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2267 | msg->msg_flags & MSG_DONTWAIT); |
| 2268 | if (IS_ERR(skb)) |
| 2269 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2270 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2271 | skb->priority = priority; |
| 2272 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2273 | /* Create L2CAP header */ |
| 2274 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2275 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | daf6a78c | 2012-05-03 10:55:52 +0300 | [diff] [blame] | 2276 | lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE); |
| 2277 | put_unaligned(chan->psm, skb_put(skb, L2CAP_PSMLEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2278 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2279 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2280 | if (unlikely(err < 0)) { |
| 2281 | kfree_skb(skb); |
| 2282 | return ERR_PTR(err); |
| 2283 | } |
| 2284 | return skb; |
| 2285 | } |
| 2286 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2287 | static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2288 | struct msghdr *msg, size_t len, |
| 2289 | u32 priority) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2290 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2291 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2292 | struct sk_buff *skb; |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2293 | int err, count; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2294 | struct l2cap_hdr *lh; |
| 2295 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2296 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2297 | |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2298 | count = min_t(unsigned int, (conn->mtu - L2CAP_HDR_SIZE), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2299 | |
Gustavo Padovan | f2ba7fa | 2012-05-03 04:54:21 -0300 | [diff] [blame] | 2300 | skb = chan->ops->alloc_skb(chan, count + L2CAP_HDR_SIZE, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2301 | msg->msg_flags & MSG_DONTWAIT); |
| 2302 | if (IS_ERR(skb)) |
| 2303 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2304 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2305 | skb->priority = priority; |
| 2306 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2307 | /* Create L2CAP header */ |
| 2308 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2309 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo Padovan | 6ff9b5e | 2012-05-02 11:56:17 -0300 | [diff] [blame] | 2310 | lh->len = cpu_to_le16(len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2311 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2312 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2313 | if (unlikely(err < 0)) { |
| 2314 | kfree_skb(skb); |
| 2315 | return ERR_PTR(err); |
| 2316 | } |
| 2317 | return skb; |
| 2318 | } |
| 2319 | |
Luiz Augusto von Dentz | ab0ff76 | 2011-09-12 20:00:50 +0300 | [diff] [blame] | 2320 | static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2321 | struct msghdr *msg, size_t len, |
| 2322 | u16 sdulen) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2323 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2324 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2325 | struct sk_buff *skb; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2326 | int err, count, hlen; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2327 | struct l2cap_hdr *lh; |
| 2328 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2329 | BT_DBG("chan %p len %zu", chan, len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2330 | |
Gustavo F. Padovan | 0ee0d20 | 2010-05-01 16:15:41 -0300 | [diff] [blame] | 2331 | if (!conn) |
| 2332 | return ERR_PTR(-ENOTCONN); |
| 2333 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2334 | hlen = __ertm_hdr_size(chan); |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 2335 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2336 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2337 | hlen += L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2338 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2339 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2340 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2341 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2342 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 2343 | |
| 2344 | skb = chan->ops->alloc_skb(chan, count + hlen, |
Gustavo Padovan | 9033894 | 2012-04-06 20:15:47 -0300 | [diff] [blame] | 2345 | msg->msg_flags & MSG_DONTWAIT); |
| 2346 | if (IS_ERR(skb)) |
| 2347 | return skb; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2348 | |
| 2349 | /* Create L2CAP header */ |
| 2350 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2351 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2352 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2353 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2354 | /* Control header is populated later */ |
| 2355 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 2356 | put_unaligned_le32(0, skb_put(skb, L2CAP_EXT_CTRL_SIZE)); |
| 2357 | else |
| 2358 | put_unaligned_le16(0, skb_put(skb, L2CAP_ENH_CTRL_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 2359 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2360 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 2361 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2362 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 2363 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2364 | if (unlikely(err < 0)) { |
| 2365 | kfree_skb(skb); |
| 2366 | return ERR_PTR(err); |
| 2367 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 2368 | |
Mat Martineau | 18a48e7 | 2012-05-17 20:53:34 -0700 | [diff] [blame] | 2369 | bt_cb(skb)->control.fcs = chan->fcs; |
Mat Martineau | 3ce3514 | 2012-04-25 16:36:14 -0700 | [diff] [blame] | 2370 | bt_cb(skb)->control.retries = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 2371 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | } |
| 2373 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2374 | static int l2cap_segment_sdu(struct l2cap_chan *chan, |
| 2375 | struct sk_buff_head *seg_queue, |
| 2376 | struct msghdr *msg, size_t len) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2377 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2378 | struct sk_buff *skb; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2379 | u16 sdu_len; |
| 2380 | size_t pdu_len; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2381 | u8 sar; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2382 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2383 | BT_DBG("chan %p, msg %p, len %zu", chan, msg, len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2384 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2385 | /* It is critical that ERTM PDUs fit in a single HCI fragment, |
| 2386 | * so fragmented skbs are not used. The HCI layer's handling |
| 2387 | * of fragmented skbs is not compatible with ERTM's queueing. |
| 2388 | */ |
| 2389 | |
| 2390 | /* PDU size is derived from the HCI MTU */ |
| 2391 | pdu_len = chan->conn->mtu; |
| 2392 | |
Mat Martineau | a549574 | 2012-10-23 15:24:21 -0700 | [diff] [blame] | 2393 | /* Constrain PDU size for BR/EDR connections */ |
| 2394 | if (!chan->hs_hcon) |
| 2395 | pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2396 | |
| 2397 | /* Adjust for largest possible L2CAP overhead. */ |
Gustavo Padovan | 35d401d | 2012-05-25 18:57:05 -0300 | [diff] [blame] | 2398 | if (chan->fcs) |
| 2399 | pdu_len -= L2CAP_FCS_SIZE; |
| 2400 | |
Gustavo Padovan | ba7aa64 | 2012-05-29 13:29:16 -0300 | [diff] [blame] | 2401 | pdu_len -= __ertm_hdr_size(chan); |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2402 | |
| 2403 | /* Remote device may have requested smaller PDUs */ |
| 2404 | pdu_len = min_t(size_t, pdu_len, chan->remote_mps); |
| 2405 | |
| 2406 | if (len <= pdu_len) { |
| 2407 | sar = L2CAP_SAR_UNSEGMENTED; |
| 2408 | sdu_len = 0; |
| 2409 | pdu_len = len; |
| 2410 | } else { |
| 2411 | sar = L2CAP_SAR_START; |
| 2412 | sdu_len = len; |
| 2413 | pdu_len -= L2CAP_SDULEN_SIZE; |
| 2414 | } |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2415 | |
| 2416 | while (len > 0) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2417 | skb = l2cap_create_iframe_pdu(chan, msg, pdu_len, sdu_len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2418 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2419 | if (IS_ERR(skb)) { |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2420 | __skb_queue_purge(seg_queue); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2421 | return PTR_ERR(skb); |
| 2422 | } |
| 2423 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2424 | bt_cb(skb)->control.sar = sar; |
| 2425 | __skb_queue_tail(seg_queue, skb); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2426 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2427 | len -= pdu_len; |
| 2428 | if (sdu_len) { |
| 2429 | sdu_len = 0; |
| 2430 | pdu_len += L2CAP_SDULEN_SIZE; |
| 2431 | } |
| 2432 | |
| 2433 | if (len <= pdu_len) { |
| 2434 | sar = L2CAP_SAR_END; |
| 2435 | pdu_len = len; |
| 2436 | } else { |
| 2437 | sar = L2CAP_SAR_CONTINUE; |
| 2438 | } |
| 2439 | } |
| 2440 | |
Gustavo Padovan | f0f6279 | 2012-05-29 13:29:17 -0300 | [diff] [blame] | 2441 | return 0; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 2442 | } |
| 2443 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2444 | 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] | 2445 | u32 priority) |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2446 | { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2447 | struct sk_buff *skb; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2448 | int err; |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2449 | struct sk_buff_head seg_queue; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2450 | |
| 2451 | /* Connectionless channel */ |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2452 | if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2453 | skb = l2cap_create_connless_pdu(chan, msg, len, priority); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2454 | if (IS_ERR(skb)) |
| 2455 | return PTR_ERR(skb); |
| 2456 | |
| 2457 | l2cap_do_send(chan, skb); |
| 2458 | return len; |
| 2459 | } |
| 2460 | |
| 2461 | switch (chan->mode) { |
| 2462 | case L2CAP_MODE_BASIC: |
| 2463 | /* Check outgoing MTU */ |
| 2464 | if (len > chan->omtu) |
| 2465 | return -EMSGSIZE; |
| 2466 | |
| 2467 | /* Create a basic PDU */ |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 2468 | skb = l2cap_create_basic_pdu(chan, msg, len, priority); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2469 | if (IS_ERR(skb)) |
| 2470 | return PTR_ERR(skb); |
| 2471 | |
| 2472 | l2cap_do_send(chan, skb); |
| 2473 | err = len; |
| 2474 | break; |
| 2475 | |
| 2476 | case L2CAP_MODE_ERTM: |
| 2477 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2478 | /* Check outgoing MTU */ |
| 2479 | if (len > chan->omtu) { |
| 2480 | err = -EMSGSIZE; |
| 2481 | break; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2482 | } |
| 2483 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2484 | __skb_queue_head_init(&seg_queue); |
| 2485 | |
| 2486 | /* Do segmentation before calling in to the state machine, |
| 2487 | * since it's possible to block while waiting for memory |
| 2488 | * allocation. |
| 2489 | */ |
| 2490 | err = l2cap_segment_sdu(chan, &seg_queue, msg, len); |
| 2491 | |
| 2492 | /* The channel could have been closed while segmenting, |
| 2493 | * check that it is still connected. |
| 2494 | */ |
| 2495 | if (chan->state != BT_CONNECTED) { |
| 2496 | __skb_queue_purge(&seg_queue); |
| 2497 | err = -ENOTCONN; |
| 2498 | } |
| 2499 | |
| 2500 | if (err) |
| 2501 | break; |
| 2502 | |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2503 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2504 | l2cap_tx(chan, NULL, &seg_queue, L2CAP_EV_DATA_REQUEST); |
Mat Martineau | 3733937 | 2012-05-17 20:53:33 -0700 | [diff] [blame] | 2505 | else |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2506 | l2cap_streaming_send(chan, &seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2507 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2508 | err = len; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2509 | |
Mat Martineau | 94122bb | 2012-05-02 09:42:02 -0700 | [diff] [blame] | 2510 | /* If the skbs were not queued for sending, they'll still be in |
| 2511 | * seg_queue and need to be purged. |
| 2512 | */ |
| 2513 | __skb_queue_purge(&seg_queue); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 2514 | break; |
| 2515 | |
| 2516 | default: |
| 2517 | BT_DBG("bad state %1.1x", chan->mode); |
| 2518 | err = -EBADFD; |
| 2519 | } |
| 2520 | |
| 2521 | return err; |
| 2522 | } |
| 2523 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2524 | static void l2cap_send_srej(struct l2cap_chan *chan, u16 txseq) |
| 2525 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2526 | struct l2cap_ctrl control; |
| 2527 | u16 seq; |
| 2528 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2529 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2530 | |
| 2531 | memset(&control, 0, sizeof(control)); |
| 2532 | control.sframe = 1; |
| 2533 | control.super = L2CAP_SUPER_SREJ; |
| 2534 | |
| 2535 | for (seq = chan->expected_tx_seq; seq != txseq; |
| 2536 | seq = __next_seq(chan, seq)) { |
| 2537 | if (!l2cap_ertm_seq_in_queue(&chan->srej_q, seq)) { |
| 2538 | control.reqseq = seq; |
| 2539 | l2cap_send_sframe(chan, &control); |
| 2540 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2541 | } |
| 2542 | } |
| 2543 | |
| 2544 | chan->expected_tx_seq = __next_seq(chan, txseq); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | static void l2cap_send_srej_tail(struct l2cap_chan *chan) |
| 2548 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2549 | struct l2cap_ctrl control; |
| 2550 | |
| 2551 | BT_DBG("chan %p", chan); |
| 2552 | |
| 2553 | if (chan->srej_list.tail == L2CAP_SEQ_LIST_CLEAR) |
| 2554 | return; |
| 2555 | |
| 2556 | memset(&control, 0, sizeof(control)); |
| 2557 | control.sframe = 1; |
| 2558 | control.super = L2CAP_SUPER_SREJ; |
| 2559 | control.reqseq = chan->srej_list.tail; |
| 2560 | l2cap_send_sframe(chan, &control); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | static void l2cap_send_srej_list(struct l2cap_chan *chan, u16 txseq) |
| 2564 | { |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2565 | struct l2cap_ctrl control; |
| 2566 | u16 initial_head; |
| 2567 | u16 seq; |
| 2568 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2569 | BT_DBG("chan %p, txseq %u", chan, txseq); |
Mat Martineau | bed68bd | 2012-05-17 20:53:44 -0700 | [diff] [blame] | 2570 | |
| 2571 | memset(&control, 0, sizeof(control)); |
| 2572 | control.sframe = 1; |
| 2573 | control.super = L2CAP_SUPER_SREJ; |
| 2574 | |
| 2575 | /* Capture initial list head to allow only one pass through the list. */ |
| 2576 | initial_head = chan->srej_list.head; |
| 2577 | |
| 2578 | do { |
| 2579 | seq = l2cap_seq_list_pop(&chan->srej_list); |
| 2580 | if (seq == txseq || seq == L2CAP_SEQ_LIST_CLEAR) |
| 2581 | break; |
| 2582 | |
| 2583 | control.reqseq = seq; |
| 2584 | l2cap_send_sframe(chan, &control); |
| 2585 | l2cap_seq_list_append(&chan->srej_list, seq); |
| 2586 | } while (chan->srej_list.head != initial_head); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 2587 | } |
| 2588 | |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2589 | static void l2cap_process_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 2590 | { |
| 2591 | struct sk_buff *acked_skb; |
| 2592 | u16 ackseq; |
| 2593 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2594 | BT_DBG("chan %p, reqseq %u", chan, reqseq); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2595 | |
| 2596 | if (chan->unacked_frames == 0 || reqseq == chan->expected_ack_seq) |
| 2597 | return; |
| 2598 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2599 | BT_DBG("expected_ack_seq %u, unacked_frames %u", |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2600 | chan->expected_ack_seq, chan->unacked_frames); |
| 2601 | |
| 2602 | for (ackseq = chan->expected_ack_seq; ackseq != reqseq; |
| 2603 | ackseq = __next_seq(chan, ackseq)) { |
| 2604 | |
| 2605 | acked_skb = l2cap_ertm_seq_in_queue(&chan->tx_q, ackseq); |
| 2606 | if (acked_skb) { |
| 2607 | skb_unlink(acked_skb, &chan->tx_q); |
| 2608 | kfree_skb(acked_skb); |
| 2609 | chan->unacked_frames--; |
| 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | chan->expected_ack_seq = reqseq; |
| 2614 | |
| 2615 | if (chan->unacked_frames == 0) |
| 2616 | __clear_retrans_timer(chan); |
| 2617 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2618 | BT_DBG("unacked_frames %u", chan->unacked_frames); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | static void l2cap_abort_rx_srej_sent(struct l2cap_chan *chan) |
| 2622 | { |
| 2623 | BT_DBG("chan %p", chan); |
| 2624 | |
| 2625 | chan->expected_tx_seq = chan->buffer_seq; |
| 2626 | l2cap_seq_list_clear(&chan->srej_list); |
| 2627 | skb_queue_purge(&chan->srej_q); |
| 2628 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 2629 | } |
| 2630 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2631 | static void l2cap_tx_state_xmit(struct l2cap_chan *chan, |
| 2632 | struct l2cap_ctrl *control, |
| 2633 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2634 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2635 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2636 | event); |
| 2637 | |
| 2638 | switch (event) { |
| 2639 | case L2CAP_EV_DATA_REQUEST: |
| 2640 | if (chan->tx_send_head == NULL) |
| 2641 | chan->tx_send_head = skb_peek(skbs); |
| 2642 | |
| 2643 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2644 | l2cap_ertm_send(chan); |
| 2645 | break; |
| 2646 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2647 | BT_DBG("Enter LOCAL_BUSY"); |
| 2648 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2649 | |
| 2650 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2651 | /* The SREJ_SENT state must be aborted if we are to |
| 2652 | * enter the LOCAL_BUSY state. |
| 2653 | */ |
| 2654 | l2cap_abort_rx_srej_sent(chan); |
| 2655 | } |
| 2656 | |
| 2657 | l2cap_send_ack(chan); |
| 2658 | |
| 2659 | break; |
| 2660 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2661 | BT_DBG("Exit LOCAL_BUSY"); |
| 2662 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2663 | |
| 2664 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2665 | struct l2cap_ctrl local_control; |
| 2666 | |
| 2667 | memset(&local_control, 0, sizeof(local_control)); |
| 2668 | local_control.sframe = 1; |
| 2669 | local_control.super = L2CAP_SUPER_RR; |
| 2670 | local_control.poll = 1; |
| 2671 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2672 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2673 | |
| 2674 | chan->retry_count = 1; |
| 2675 | __set_monitor_timer(chan); |
| 2676 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2677 | } |
| 2678 | break; |
| 2679 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2680 | l2cap_process_reqseq(chan, control->reqseq); |
| 2681 | break; |
| 2682 | case L2CAP_EV_EXPLICIT_POLL: |
| 2683 | l2cap_send_rr_or_rnr(chan, 1); |
| 2684 | chan->retry_count = 1; |
| 2685 | __set_monitor_timer(chan); |
| 2686 | __clear_ack_timer(chan); |
| 2687 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2688 | break; |
| 2689 | case L2CAP_EV_RETRANS_TO: |
| 2690 | l2cap_send_rr_or_rnr(chan, 1); |
| 2691 | chan->retry_count = 1; |
| 2692 | __set_monitor_timer(chan); |
| 2693 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2694 | break; |
| 2695 | case L2CAP_EV_RECV_FBIT: |
| 2696 | /* Nothing to process */ |
| 2697 | break; |
| 2698 | default: |
| 2699 | break; |
| 2700 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2703 | static void l2cap_tx_state_wait_f(struct l2cap_chan *chan, |
| 2704 | struct l2cap_ctrl *control, |
| 2705 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2706 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2707 | BT_DBG("chan %p, control %p, skbs %p, event %d", chan, control, skbs, |
| 2708 | event); |
| 2709 | |
| 2710 | switch (event) { |
| 2711 | case L2CAP_EV_DATA_REQUEST: |
| 2712 | if (chan->tx_send_head == NULL) |
| 2713 | chan->tx_send_head = skb_peek(skbs); |
| 2714 | /* Queue data, but don't send. */ |
| 2715 | skb_queue_splice_tail_init(skbs, &chan->tx_q); |
| 2716 | break; |
| 2717 | case L2CAP_EV_LOCAL_BUSY_DETECTED: |
| 2718 | BT_DBG("Enter LOCAL_BUSY"); |
| 2719 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2720 | |
| 2721 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 2722 | /* The SREJ_SENT state must be aborted if we are to |
| 2723 | * enter the LOCAL_BUSY state. |
| 2724 | */ |
| 2725 | l2cap_abort_rx_srej_sent(chan); |
| 2726 | } |
| 2727 | |
| 2728 | l2cap_send_ack(chan); |
| 2729 | |
| 2730 | break; |
| 2731 | case L2CAP_EV_LOCAL_BUSY_CLEAR: |
| 2732 | BT_DBG("Exit LOCAL_BUSY"); |
| 2733 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 2734 | |
| 2735 | if (test_bit(CONN_RNR_SENT, &chan->conn_state)) { |
| 2736 | struct l2cap_ctrl local_control; |
| 2737 | memset(&local_control, 0, sizeof(local_control)); |
| 2738 | local_control.sframe = 1; |
| 2739 | local_control.super = L2CAP_SUPER_RR; |
| 2740 | local_control.poll = 1; |
| 2741 | local_control.reqseq = chan->buffer_seq; |
Mat Martineau | a67d7f6 | 2012-05-17 20:53:35 -0700 | [diff] [blame] | 2742 | l2cap_send_sframe(chan, &local_control); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2743 | |
| 2744 | chan->retry_count = 1; |
| 2745 | __set_monitor_timer(chan); |
| 2746 | chan->tx_state = L2CAP_TX_STATE_WAIT_F; |
| 2747 | } |
| 2748 | break; |
| 2749 | case L2CAP_EV_RECV_REQSEQ_AND_FBIT: |
| 2750 | l2cap_process_reqseq(chan, control->reqseq); |
| 2751 | |
| 2752 | /* Fall through */ |
| 2753 | |
| 2754 | case L2CAP_EV_RECV_FBIT: |
| 2755 | if (control && control->final) { |
| 2756 | __clear_monitor_timer(chan); |
| 2757 | if (chan->unacked_frames > 0) |
| 2758 | __set_retrans_timer(chan); |
| 2759 | chan->retry_count = 0; |
| 2760 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
| 2761 | BT_DBG("recv fbit tx_state 0x2.2%x", chan->tx_state); |
| 2762 | } |
| 2763 | break; |
| 2764 | case L2CAP_EV_EXPLICIT_POLL: |
| 2765 | /* Ignore */ |
| 2766 | break; |
| 2767 | case L2CAP_EV_MONITOR_TO: |
| 2768 | if (chan->max_tx == 0 || chan->retry_count < chan->max_tx) { |
| 2769 | l2cap_send_rr_or_rnr(chan, 1); |
| 2770 | __set_monitor_timer(chan); |
| 2771 | chan->retry_count++; |
| 2772 | } else { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 2773 | l2cap_send_disconn_req(chan, ECONNABORTED); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2774 | } |
| 2775 | break; |
| 2776 | default: |
| 2777 | break; |
| 2778 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2779 | } |
| 2780 | |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2781 | static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 2782 | struct sk_buff_head *skbs, u8 event) |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2783 | { |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2784 | BT_DBG("chan %p, control %p, skbs %p, event %d, state %d", |
| 2785 | chan, control, skbs, event, chan->tx_state); |
| 2786 | |
| 2787 | switch (chan->tx_state) { |
| 2788 | case L2CAP_TX_STATE_XMIT: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2789 | l2cap_tx_state_xmit(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2790 | break; |
| 2791 | case L2CAP_TX_STATE_WAIT_F: |
Gustavo Padovan | d660366 | 2012-05-21 13:58:22 -0300 | [diff] [blame] | 2792 | l2cap_tx_state_wait_f(chan, control, skbs, event); |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2793 | break; |
| 2794 | default: |
| 2795 | /* Ignore event */ |
| 2796 | break; |
| 2797 | } |
Mat Martineau | 608bcc6 | 2012-05-17 20:53:32 -0700 | [diff] [blame] | 2798 | } |
| 2799 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2800 | static void l2cap_pass_to_tx(struct l2cap_chan *chan, |
| 2801 | struct l2cap_ctrl *control) |
| 2802 | { |
| 2803 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2804 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_REQSEQ_AND_FBIT); |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 2805 | } |
| 2806 | |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2807 | static void l2cap_pass_to_tx_fbit(struct l2cap_chan *chan, |
| 2808 | struct l2cap_ctrl *control) |
| 2809 | { |
| 2810 | BT_DBG("chan %p, control %p", chan, control); |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 2811 | l2cap_tx(chan, control, NULL, L2CAP_EV_RECV_FBIT); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 2812 | } |
| 2813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | /* Copy frame to all raw sockets on that connection */ |
| 2815 | static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) |
| 2816 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | struct sk_buff *nskb; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2818 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | |
| 2820 | BT_DBG("conn %p", conn); |
| 2821 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2822 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2823 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2824 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2825 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 2826 | if (chan->chan_type != L2CAP_CHAN_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | continue; |
| 2828 | |
| 2829 | /* Don't send frame to the socket it came from */ |
| 2830 | if (skb->sk == sk) |
| 2831 | continue; |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2832 | nskb = skb_clone(skb, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 2833 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | continue; |
| 2835 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 2836 | if (chan->ops->recv(chan, nskb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | kfree_skb(nskb); |
| 2838 | } |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 2839 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2840 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | /* ---- L2CAP signalling commands ---- */ |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2844 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, u8 code, |
| 2845 | u8 ident, u16 dlen, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | { |
| 2847 | struct sk_buff *skb, **frag; |
| 2848 | struct l2cap_cmd_hdr *cmd; |
| 2849 | struct l2cap_hdr *lh; |
| 2850 | int len, count; |
| 2851 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2852 | BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %u", |
| 2853 | conn, code, ident, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | |
Anderson Lizardo | 300b962 | 2013-06-02 16:30:40 -0400 | [diff] [blame] | 2855 | if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE) |
| 2856 | return NULL; |
| 2857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; |
| 2859 | count = min_t(unsigned int, conn->mtu, len); |
| 2860 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2861 | skb = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | if (!skb) |
| 2863 | return NULL; |
| 2864 | |
| 2865 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2866 | lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2867 | |
| 2868 | if (conn->hcon->type == LE_LINK) |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 2869 | lh->cid = __constant_cpu_to_le16(L2CAP_CID_LE_SIGNALING); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2870 | else |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 2871 | lh->cid = __constant_cpu_to_le16(L2CAP_CID_SIGNALING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | |
| 2873 | cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); |
| 2874 | cmd->code = code; |
| 2875 | cmd->ident = ident; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2876 | cmd->len = cpu_to_le16(dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | |
| 2878 | if (dlen) { |
| 2879 | count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; |
| 2880 | memcpy(skb_put(skb, count), data, count); |
| 2881 | data += count; |
| 2882 | } |
| 2883 | |
| 2884 | len -= skb->len; |
| 2885 | |
| 2886 | /* Continuation fragments (no L2CAP header) */ |
| 2887 | frag = &skb_shinfo(skb)->frag_list; |
| 2888 | while (len) { |
| 2889 | count = min_t(unsigned int, conn->mtu, len); |
| 2890 | |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 2891 | *frag = bt_skb_alloc(count, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | if (!*frag) |
| 2893 | goto fail; |
| 2894 | |
| 2895 | memcpy(skb_put(*frag, count), data, count); |
| 2896 | |
| 2897 | len -= count; |
| 2898 | data += count; |
| 2899 | |
| 2900 | frag = &(*frag)->next; |
| 2901 | } |
| 2902 | |
| 2903 | return skb; |
| 2904 | |
| 2905 | fail: |
| 2906 | kfree_skb(skb); |
| 2907 | return NULL; |
| 2908 | } |
| 2909 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 2910 | static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, |
| 2911 | unsigned long *val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | { |
| 2913 | struct l2cap_conf_opt *opt = *ptr; |
| 2914 | int len; |
| 2915 | |
| 2916 | len = L2CAP_CONF_OPT_SIZE + opt->len; |
| 2917 | *ptr += len; |
| 2918 | |
| 2919 | *type = opt->type; |
| 2920 | *olen = opt->len; |
| 2921 | |
| 2922 | switch (opt->len) { |
| 2923 | case 1: |
| 2924 | *val = *((u8 *) opt->val); |
| 2925 | break; |
| 2926 | |
| 2927 | case 2: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2928 | *val = get_unaligned_le16(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | break; |
| 2930 | |
| 2931 | case 4: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 2932 | *val = get_unaligned_le32(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | break; |
| 2934 | |
| 2935 | default: |
| 2936 | *val = (unsigned long) opt->val; |
| 2937 | break; |
| 2938 | } |
| 2939 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2940 | 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] | 2941 | return len; |
| 2942 | } |
| 2943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) |
| 2945 | { |
| 2946 | struct l2cap_conf_opt *opt = *ptr; |
| 2947 | |
Andrei Emeltchenko | b440067 | 2012-07-10 15:27:49 +0300 | [diff] [blame] | 2948 | 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] | 2949 | |
| 2950 | opt->type = type; |
| 2951 | opt->len = len; |
| 2952 | |
| 2953 | switch (len) { |
| 2954 | case 1: |
| 2955 | *((u8 *) opt->val) = val; |
| 2956 | break; |
| 2957 | |
| 2958 | case 2: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 2959 | put_unaligned_le16(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2960 | break; |
| 2961 | |
| 2962 | case 4: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 2963 | put_unaligned_le32(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2964 | break; |
| 2965 | |
| 2966 | default: |
| 2967 | memcpy(opt->val, (void *) val, len); |
| 2968 | break; |
| 2969 | } |
| 2970 | |
| 2971 | *ptr += L2CAP_CONF_OPT_SIZE + len; |
| 2972 | } |
| 2973 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2974 | static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) |
| 2975 | { |
| 2976 | struct l2cap_conf_efs efs; |
| 2977 | |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 2978 | switch (chan->mode) { |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2979 | case L2CAP_MODE_ERTM: |
| 2980 | efs.id = chan->local_id; |
| 2981 | efs.stype = chan->local_stype; |
| 2982 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 2983 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 2984 | efs.acc_lat = __constant_cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 2985 | efs.flush_to = __constant_cpu_to_le32(L2CAP_EFS_DEFAULT_FLUSH_TO); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2986 | break; |
| 2987 | |
| 2988 | case L2CAP_MODE_STREAMING: |
| 2989 | efs.id = 1; |
| 2990 | efs.stype = L2CAP_SERV_BESTEFFORT; |
| 2991 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 2992 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
| 2993 | efs.acc_lat = 0; |
| 2994 | efs.flush_to = 0; |
| 2995 | break; |
| 2996 | |
| 2997 | default: |
| 2998 | return; |
| 2999 | } |
| 3000 | |
| 3001 | l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), |
Andrei Emeltchenko | 8936fa6 | 2012-10-08 11:14:41 +0300 | [diff] [blame] | 3002 | (unsigned long) &efs); |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3003 | } |
| 3004 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3005 | static void l2cap_ack_timeout(struct work_struct *work) |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3006 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3007 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3008 | ack_timer.work); |
| 3009 | u16 frames_to_ack; |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3010 | |
Gustavo F. Padovan | 2fb9b3d | 2011-12-22 16:56:05 -0200 | [diff] [blame] | 3011 | BT_DBG("chan %p", chan); |
| 3012 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3013 | l2cap_chan_lock(chan); |
| 3014 | |
Mat Martineau | 0362520 | 2012-05-17 20:53:51 -0700 | [diff] [blame] | 3015 | frames_to_ack = __seq_offset(chan, chan->buffer_seq, |
| 3016 | chan->last_acked_seq); |
| 3017 | |
| 3018 | if (frames_to_ack) |
| 3019 | l2cap_send_rr_or_rnr(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3020 | |
| 3021 | l2cap_chan_unlock(chan); |
Szymon Janc | 09bfb2e | 2012-01-11 10:59:49 +0100 | [diff] [blame] | 3022 | l2cap_chan_put(chan); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3023 | } |
| 3024 | |
Andrei Emeltchenko | 466f800 | 2012-05-29 13:59:01 +0300 | [diff] [blame] | 3025 | int l2cap_ertm_init(struct l2cap_chan *chan) |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3026 | { |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3027 | int err; |
| 3028 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3029 | chan->next_tx_seq = 0; |
| 3030 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3031 | chan->expected_ack_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3032 | chan->unacked_frames = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3033 | chan->buffer_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3034 | chan->frames_sent = 0; |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3035 | chan->last_acked_seq = 0; |
| 3036 | chan->sdu = NULL; |
| 3037 | chan->sdu_last_frag = NULL; |
| 3038 | chan->sdu_len = 0; |
| 3039 | |
Mat Martineau | d34c34f | 2012-05-14 14:49:27 -0700 | [diff] [blame] | 3040 | skb_queue_head_init(&chan->tx_q); |
| 3041 | |
Mat Martineau | 0833328 | 2012-10-23 15:24:06 -0700 | [diff] [blame] | 3042 | chan->local_amp_id = 0; |
| 3043 | chan->move_id = 0; |
| 3044 | chan->move_state = L2CAP_MOVE_STABLE; |
| 3045 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 3046 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 3047 | if (chan->mode != L2CAP_MODE_ERTM) |
| 3048 | return 0; |
| 3049 | |
| 3050 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 3051 | chan->tx_state = L2CAP_TX_STATE_XMIT; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3052 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 3053 | INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); |
| 3054 | INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); |
| 3055 | INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3056 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3057 | skb_queue_head_init(&chan->srej_q); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3058 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3059 | err = l2cap_seq_list_init(&chan->srej_list, chan->tx_win); |
| 3060 | if (err < 0) |
| 3061 | return err; |
| 3062 | |
Mat Martineau | 9dc9aff | 2012-05-17 16:20:14 -0700 | [diff] [blame] | 3063 | err = l2cap_seq_list_init(&chan->retrans_list, chan->remote_tx_win); |
| 3064 | if (err < 0) |
| 3065 | l2cap_seq_list_free(&chan->srej_list); |
| 3066 | |
| 3067 | return err; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3068 | } |
| 3069 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3070 | static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) |
| 3071 | { |
| 3072 | switch (mode) { |
| 3073 | case L2CAP_MODE_STREAMING: |
| 3074 | case L2CAP_MODE_ERTM: |
| 3075 | if (l2cap_mode_supported(mode, remote_feat_mask)) |
| 3076 | return mode; |
| 3077 | /* fall through */ |
| 3078 | default: |
| 3079 | return L2CAP_MODE_BASIC; |
| 3080 | } |
| 3081 | } |
| 3082 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3083 | static inline bool __l2cap_ews_supported(struct l2cap_chan *chan) |
| 3084 | { |
| 3085 | return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; |
| 3086 | } |
| 3087 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3088 | static inline bool __l2cap_efs_supported(struct l2cap_chan *chan) |
| 3089 | { |
| 3090 | return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW; |
| 3091 | } |
| 3092 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3093 | static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan, |
| 3094 | struct l2cap_conf_rfc *rfc) |
| 3095 | { |
| 3096 | if (chan->local_amp_id && chan->hs_hcon) { |
| 3097 | u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to; |
| 3098 | |
| 3099 | /* Class 1 devices have must have ERTM timeouts |
| 3100 | * exceeding the Link Supervision Timeout. The |
| 3101 | * default Link Supervision Timeout for AMP |
| 3102 | * controllers is 10 seconds. |
| 3103 | * |
| 3104 | * Class 1 devices use 0xffffffff for their |
| 3105 | * best-effort flush timeout, so the clamping logic |
| 3106 | * will result in a timeout that meets the above |
| 3107 | * requirement. ERTM timeouts are 16-bit values, so |
| 3108 | * the maximum timeout is 65.535 seconds. |
| 3109 | */ |
| 3110 | |
| 3111 | /* Convert timeout to milliseconds and round */ |
| 3112 | ertm_to = DIV_ROUND_UP_ULL(ertm_to, 1000); |
| 3113 | |
| 3114 | /* This is the recommended formula for class 2 devices |
| 3115 | * that start ERTM timers when packets are sent to the |
| 3116 | * controller. |
| 3117 | */ |
| 3118 | ertm_to = 3 * ertm_to + 500; |
| 3119 | |
| 3120 | if (ertm_to > 0xffff) |
| 3121 | ertm_to = 0xffff; |
| 3122 | |
| 3123 | rfc->retrans_timeout = cpu_to_le16((u16) ertm_to); |
| 3124 | rfc->monitor_timeout = rfc->retrans_timeout; |
| 3125 | } else { |
| 3126 | rfc->retrans_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); |
| 3127 | rfc->monitor_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); |
| 3128 | } |
| 3129 | } |
| 3130 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3131 | static inline void l2cap_txwin_setup(struct l2cap_chan *chan) |
| 3132 | { |
| 3133 | if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3134 | __l2cap_ews_supported(chan)) { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3135 | /* use extended control field */ |
| 3136 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3137 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
| 3138 | } else { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3139 | chan->tx_win = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3140 | L2CAP_DEFAULT_TX_WINDOW); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3141 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
| 3142 | } |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3143 | chan->ack_win = chan->tx_win; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3144 | } |
| 3145 | |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3146 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | struct l2cap_conf_req *req = data; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3149 | struct l2cap_conf_rfc rfc = { .mode = chan->mode }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | void *ptr = req->data; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3151 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3153 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3154 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3155 | if (chan->num_conf_req || chan->num_conf_rsp) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3156 | goto done; |
| 3157 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3158 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3159 | case L2CAP_MODE_STREAMING: |
| 3160 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3161 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3162 | break; |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3163 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3164 | if (__l2cap_efs_supported(chan)) |
| 3165 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3166 | |
Gustavo F. Padovan | 2ba13ed | 2010-06-09 16:39:05 -0300 | [diff] [blame] | 3167 | /* fall through */ |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3168 | default: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3169 | chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3170 | break; |
| 3171 | } |
| 3172 | |
| 3173 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3174 | if (chan->imtu != L2CAP_DEFAULT_MTU) |
| 3175 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | 7990681c | 2011-01-24 16:01:43 -0200 | [diff] [blame] | 3176 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3177 | switch (chan->mode) { |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3178 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3179 | if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3180 | !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3181 | break; |
| 3182 | |
Gustavo F. Padovan | 6254775 | 2010-06-08 20:05:31 -0300 | [diff] [blame] | 3183 | rfc.mode = L2CAP_MODE_BASIC; |
| 3184 | rfc.txwin_size = 0; |
| 3185 | rfc.max_transmit = 0; |
| 3186 | rfc.retrans_timeout = 0; |
| 3187 | rfc.monitor_timeout = 0; |
| 3188 | rfc.max_pdu_size = 0; |
| 3189 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3190 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3191 | (unsigned long) &rfc); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3192 | break; |
| 3193 | |
| 3194 | case L2CAP_MODE_ERTM: |
| 3195 | rfc.mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3196 | rfc.max_transmit = chan->max_tx; |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3197 | |
| 3198 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3199 | |
| 3200 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3201 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3202 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3203 | rfc.max_pdu_size = cpu_to_le16(size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3204 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3205 | l2cap_txwin_setup(chan); |
| 3206 | |
| 3207 | rfc.txwin_size = min_t(u16, chan->tx_win, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3208 | L2CAP_DEFAULT_TX_WINDOW); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3209 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3210 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3211 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3212 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3213 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3214 | l2cap_add_opt_efs(&ptr, chan); |
| 3215 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3216 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3217 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3218 | chan->tx_win); |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3219 | |
| 3220 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3221 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3222 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3223 | chan->fcs = L2CAP_FCS_NONE; |
| 3224 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3225 | chan->fcs); |
| 3226 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3227 | break; |
| 3228 | |
| 3229 | case L2CAP_MODE_STREAMING: |
Mat Martineau | 273759e | 2012-05-17 20:53:53 -0700 | [diff] [blame] | 3230 | l2cap_txwin_setup(chan); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3231 | rfc.mode = L2CAP_MODE_STREAMING; |
| 3232 | rfc.txwin_size = 0; |
| 3233 | rfc.max_transmit = 0; |
| 3234 | rfc.retrans_timeout = 0; |
| 3235 | rfc.monitor_timeout = 0; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3236 | |
| 3237 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3238 | L2CAP_EXT_HDR_SIZE - L2CAP_SDULEN_SIZE - |
| 3239 | L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3240 | rfc.max_pdu_size = cpu_to_le16(size); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3241 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3242 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3243 | (unsigned long) &rfc); |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 3244 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 3245 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 3246 | l2cap_add_opt_efs(&ptr, chan); |
| 3247 | |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3248 | if (chan->conn->feat_mask & L2CAP_FEAT_FCS) |
| 3249 | if (chan->fcs == L2CAP_FCS_NONE || |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3250 | test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) { |
Andrei Emeltchenko | 6091891 | 2012-11-29 17:46:06 +0200 | [diff] [blame] | 3251 | chan->fcs = L2CAP_FCS_NONE; |
| 3252 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, |
| 3253 | chan->fcs); |
| 3254 | } |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 3255 | break; |
| 3256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3257 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3258 | req->dcid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 3259 | req->flags = __constant_cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | |
| 3261 | return ptr - data; |
| 3262 | } |
| 3263 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3264 | static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3266 | struct l2cap_conf_rsp *rsp = data; |
| 3267 | void *ptr = rsp->data; |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3268 | void *req = chan->conf_req; |
| 3269 | int len = chan->conf_len; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3270 | int type, hint, olen; |
| 3271 | unsigned long val; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3272 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3273 | struct l2cap_conf_efs efs; |
| 3274 | u8 remote_efs = 0; |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3275 | u16 mtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3276 | u16 result = L2CAP_CONF_SUCCESS; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3277 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3279 | BT_DBG("chan %p", chan); |
Marcel Holtmann | 820ae1b | 2006-11-18 22:15:00 +0100 | [diff] [blame] | 3280 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3281 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3282 | len -= l2cap_get_conf_opt(&req, &type, &olen, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | |
Gustavo F. Padovan | 589d274 | 2009-04-20 01:31:07 -0300 | [diff] [blame] | 3284 | hint = type & L2CAP_CONF_HINT; |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 3285 | type &= L2CAP_CONF_MASK; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3286 | |
| 3287 | switch (type) { |
| 3288 | case L2CAP_CONF_MTU: |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 3289 | mtu = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3290 | break; |
| 3291 | |
| 3292 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3293 | chan->flush_to = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3294 | break; |
| 3295 | |
| 3296 | case L2CAP_CONF_QOS: |
| 3297 | break; |
| 3298 | |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3299 | case L2CAP_CONF_RFC: |
| 3300 | if (olen == sizeof(rfc)) |
| 3301 | memcpy(&rfc, (void *) val, olen); |
| 3302 | break; |
| 3303 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3304 | case L2CAP_CONF_FCS: |
| 3305 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3306 | set_bit(CONF_RECV_NO_FCS, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3307 | break; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3308 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3309 | case L2CAP_CONF_EFS: |
| 3310 | remote_efs = 1; |
| 3311 | if (olen == sizeof(efs)) |
| 3312 | memcpy(&efs, (void *) val, olen); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3313 | break; |
| 3314 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3315 | case L2CAP_CONF_EWS: |
| 3316 | if (!enable_hs) |
| 3317 | return -ECONNREFUSED; |
| 3318 | |
| 3319 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
| 3320 | set_bit(CONF_EWS_RECV, &chan->conf_state); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3321 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3322 | chan->remote_tx_win = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3323 | break; |
| 3324 | |
| 3325 | default: |
| 3326 | if (hint) |
| 3327 | break; |
| 3328 | |
| 3329 | result = L2CAP_CONF_UNKNOWN; |
| 3330 | *((u8 *) ptr++) = type; |
| 3331 | break; |
| 3332 | } |
| 3333 | } |
| 3334 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3335 | if (chan->num_conf_rsp || chan->num_conf_req > 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3336 | goto done; |
| 3337 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3338 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3339 | case L2CAP_MODE_STREAMING: |
| 3340 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3341 | if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3342 | chan->mode = l2cap_select_mode(rfc.mode, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3343 | chan->conn->feat_mask); |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 3344 | break; |
| 3345 | } |
| 3346 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3347 | if (remote_efs) { |
| 3348 | if (__l2cap_efs_supported(chan)) |
| 3349 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 3350 | else |
| 3351 | return -ECONNREFUSED; |
| 3352 | } |
| 3353 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3354 | if (chan->mode != rfc.mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3355 | return -ECONNREFUSED; |
Gustavo F. Padovan | 742e519 | 2010-06-08 19:09:48 -0300 | [diff] [blame] | 3356 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3357 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3361 | if (chan->mode != rfc.mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3362 | result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3363 | rfc.mode = chan->mode; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3364 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3365 | if (chan->num_conf_rsp == 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3366 | return -ECONNREFUSED; |
| 3367 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3368 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3369 | (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3370 | } |
| 3371 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3372 | if (result == L2CAP_CONF_SUCCESS) { |
| 3373 | /* Configure output options and let the other side know |
| 3374 | * which ones we don't like. */ |
| 3375 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3376 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
| 3377 | result = L2CAP_CONF_UNACCEPT; |
| 3378 | else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3379 | chan->omtu = mtu; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3380 | set_bit(CONF_MTU_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3381 | } |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3382 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3383 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3384 | if (remote_efs) { |
| 3385 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3386 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3387 | efs.stype != chan->local_stype) { |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3388 | |
| 3389 | result = L2CAP_CONF_UNACCEPT; |
| 3390 | |
| 3391 | if (chan->num_conf_req >= 1) |
| 3392 | return -ECONNREFUSED; |
| 3393 | |
| 3394 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3395 | sizeof(efs), |
| 3396 | (unsigned long) &efs); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3397 | } else { |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3398 | /* Send PENDING Conf Rsp */ |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3399 | result = L2CAP_CONF_PENDING; |
| 3400 | set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3401 | } |
| 3402 | } |
| 3403 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3404 | switch (rfc.mode) { |
| 3405 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3406 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3407 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3408 | break; |
| 3409 | |
| 3410 | case L2CAP_MODE_ERTM: |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3411 | if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) |
| 3412 | chan->remote_tx_win = rfc.txwin_size; |
| 3413 | else |
| 3414 | rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; |
| 3415 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 3416 | chan->remote_max_tx = rfc.max_transmit; |
Mat Martineau | 86b1b26 | 2010-08-05 15:54:22 -0700 | [diff] [blame] | 3417 | |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3418 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3419 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3420 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3421 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3422 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3423 | |
Mat Martineau | 36c86c8 | 2012-10-23 15:24:20 -0700 | [diff] [blame] | 3424 | __l2cap_set_ertm_timeouts(chan, &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3425 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3426 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3427 | |
| 3428 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3429 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3430 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3431 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3432 | chan->remote_id = efs.id; |
| 3433 | chan->remote_stype = efs.stype; |
| 3434 | chan->remote_msdu = le16_to_cpu(efs.msdu); |
| 3435 | chan->remote_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3436 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3437 | chan->remote_acc_lat = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3438 | le32_to_cpu(efs.acc_lat); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3439 | chan->remote_sdu_itime = |
| 3440 | le32_to_cpu(efs.sdu_itime); |
| 3441 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3442 | sizeof(efs), |
| 3443 | (unsigned long) &efs); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 3444 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3445 | break; |
| 3446 | |
| 3447 | case L2CAP_MODE_STREAMING: |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3448 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3449 | chan->conn->mtu - L2CAP_EXT_HDR_SIZE - |
| 3450 | L2CAP_SDULEN_SIZE - L2CAP_FCS_SIZE); |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 3451 | rfc.max_pdu_size = cpu_to_le16(size); |
| 3452 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3453 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3454 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3455 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3456 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 3457 | (unsigned long) &rfc); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 3458 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3459 | break; |
| 3460 | |
| 3461 | default: |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3462 | result = L2CAP_CONF_UNACCEPT; |
| 3463 | |
| 3464 | memset(&rfc, 0, sizeof(rfc)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3465 | rfc.mode = chan->mode; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 3466 | } |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3467 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3468 | if (result == L2CAP_CONF_SUCCESS) |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3469 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3470 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3471 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3472 | rsp->result = cpu_to_le16(result); |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 3473 | rsp->flags = __constant_cpu_to_le16(0); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3474 | |
| 3475 | return ptr - data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3476 | } |
| 3477 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3478 | static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, |
| 3479 | void *data, u16 *result) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3480 | { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3481 | struct l2cap_conf_req *req = data; |
| 3482 | void *ptr = req->data; |
| 3483 | int type, olen; |
| 3484 | unsigned long val; |
Mat Martineau | 36e999a | 2011-12-08 17:23:21 -0800 | [diff] [blame] | 3485 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3486 | struct l2cap_conf_efs efs; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3487 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3488 | 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] | 3489 | |
| 3490 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3491 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3492 | |
| 3493 | switch (type) { |
| 3494 | case L2CAP_CONF_MTU: |
| 3495 | if (val < L2CAP_DEFAULT_MIN_MTU) { |
| 3496 | *result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3497 | chan->imtu = L2CAP_DEFAULT_MIN_MTU; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3498 | } else |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3499 | chan->imtu = val; |
| 3500 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3501 | break; |
| 3502 | |
| 3503 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3504 | chan->flush_to = val; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3505 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3506 | 2, chan->flush_to); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3507 | break; |
| 3508 | |
| 3509 | case L2CAP_CONF_RFC: |
| 3510 | if (olen == sizeof(rfc)) |
| 3511 | memcpy(&rfc, (void *)val, olen); |
| 3512 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3513 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3514 | rfc.mode != chan->mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3515 | return -ECONNREFUSED; |
| 3516 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3517 | chan->fcs = 0; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3518 | |
| 3519 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3520 | sizeof(rfc), (unsigned long) &rfc); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3521 | break; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3522 | |
| 3523 | case L2CAP_CONF_EWS: |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3524 | chan->ack_win = min_t(u16, val, chan->ack_win); |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3525 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3526 | chan->tx_win); |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3527 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3528 | |
| 3529 | case L2CAP_CONF_EFS: |
| 3530 | if (olen == sizeof(efs)) |
| 3531 | memcpy(&efs, (void *)val, olen); |
| 3532 | |
| 3533 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3534 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 3535 | efs.stype != chan->local_stype) |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3536 | return -ECONNREFUSED; |
| 3537 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3538 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, sizeof(efs), |
| 3539 | (unsigned long) &efs); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3540 | break; |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3541 | |
| 3542 | case L2CAP_CONF_FCS: |
| 3543 | if (*result == L2CAP_CONF_PENDING) |
| 3544 | if (val == L2CAP_FCS_NONE) |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3545 | set_bit(CONF_RECV_NO_FCS, |
Andrei Emeltchenko | cbabee7 | 2012-11-29 17:46:07 +0200 | [diff] [blame] | 3546 | &chan->conf_state); |
| 3547 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3548 | } |
| 3549 | } |
| 3550 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3551 | if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3552 | return -ECONNREFUSED; |
| 3553 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3554 | chan->mode = rfc.mode; |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 3555 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3556 | if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3557 | switch (rfc.mode) { |
| 3558 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3559 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3560 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 3561 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3562 | if (!test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3563 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3564 | rfc.txwin_size); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3565 | |
| 3566 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 3567 | chan->local_msdu = le16_to_cpu(efs.msdu); |
| 3568 | chan->local_sdu_itime = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3569 | le32_to_cpu(efs.sdu_itime); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3570 | chan->local_acc_lat = le32_to_cpu(efs.acc_lat); |
| 3571 | chan->local_flush_to = |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3572 | le32_to_cpu(efs.flush_to); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3573 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3574 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 3575 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3576 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3577 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3578 | } |
| 3579 | } |
| 3580 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3581 | req->dcid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 3582 | req->flags = __constant_cpu_to_le16(0); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3583 | |
| 3584 | return ptr - data; |
| 3585 | } |
| 3586 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3587 | static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, |
| 3588 | u16 result, u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3589 | { |
| 3590 | struct l2cap_conf_rsp *rsp = data; |
| 3591 | void *ptr = rsp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3592 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3593 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3595 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3596 | rsp->result = cpu_to_le16(result); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3597 | rsp->flags = cpu_to_le16(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3598 | |
| 3599 | return ptr - data; |
| 3600 | } |
| 3601 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3602 | void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3603 | { |
| 3604 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3605 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3606 | u8 buf[128]; |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3607 | u8 rsp_code; |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3608 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3609 | rsp.scid = cpu_to_le16(chan->dcid); |
| 3610 | rsp.dcid = cpu_to_le16(chan->scid); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 3611 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS); |
| 3612 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Andrei Emeltchenko | 439f34a | 2012-10-31 15:46:28 +0200 | [diff] [blame] | 3613 | |
| 3614 | if (chan->hs_hcon) |
| 3615 | rsp_code = L2CAP_CREATE_CHAN_RSP; |
| 3616 | else |
| 3617 | rsp_code = L2CAP_CONN_RSP; |
| 3618 | |
| 3619 | BT_DBG("chan %p rsp_code %u", chan, rsp_code); |
| 3620 | |
| 3621 | l2cap_send_cmd(conn, chan->ident, rsp_code, sizeof(rsp), &rsp); |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3622 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3623 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3624 | return; |
| 3625 | |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3626 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3627 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 3628 | chan->num_conf_req++; |
| 3629 | } |
| 3630 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3631 | 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] | 3632 | { |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3633 | int type, olen; |
| 3634 | unsigned long val; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3635 | /* Use sane default values in case a misbehaving remote device |
| 3636 | * did not send an RFC or extended window size option. |
| 3637 | */ |
| 3638 | u16 txwin_ext = chan->ack_win; |
| 3639 | struct l2cap_conf_rfc rfc = { |
| 3640 | .mode = chan->mode, |
| 3641 | .retrans_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO), |
| 3642 | .monitor_timeout = __constant_cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO), |
| 3643 | .max_pdu_size = cpu_to_le16(chan->imtu), |
| 3644 | .txwin_size = min_t(u16, chan->ack_win, L2CAP_DEFAULT_TX_WINDOW), |
| 3645 | }; |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3646 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3647 | BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3648 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3649 | if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3650 | return; |
| 3651 | |
| 3652 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 3653 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 3654 | |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3655 | switch (type) { |
| 3656 | case L2CAP_CONF_RFC: |
| 3657 | if (olen == sizeof(rfc)) |
| 3658 | memcpy(&rfc, (void *)val, olen); |
Szymon Janc | 8f321f8 | 2012-06-08 11:33:33 +0200 | [diff] [blame] | 3659 | break; |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3660 | case L2CAP_CONF_EWS: |
| 3661 | txwin_ext = val; |
| 3662 | break; |
| 3663 | } |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3664 | } |
| 3665 | |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3666 | switch (rfc.mode) { |
| 3667 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3668 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 3669 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
Mat Martineau | c20f8e3 | 2012-07-10 05:47:07 -0700 | [diff] [blame] | 3670 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
| 3671 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3672 | chan->ack_win = min_t(u16, chan->ack_win, txwin_ext); |
| 3673 | else |
| 3674 | chan->ack_win = min_t(u16, chan->ack_win, |
| 3675 | rfc.txwin_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3676 | break; |
| 3677 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3678 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3679 | } |
| 3680 | } |
| 3681 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3682 | static inline int l2cap_command_rej(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3683 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3684 | u8 *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3685 | { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3686 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3687 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3688 | if (cmd_len < sizeof(*rej)) |
| 3689 | return -EPROTO; |
| 3690 | |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3691 | if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3692 | return 0; |
| 3693 | |
| 3694 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3695 | cmd->ident == conn->info_ident) { |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 3696 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3697 | |
| 3698 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3699 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3700 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3701 | l2cap_conn_start(conn); |
| 3702 | } |
| 3703 | |
| 3704 | return 0; |
| 3705 | } |
| 3706 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3707 | static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn, |
| 3708 | struct l2cap_cmd_hdr *cmd, |
| 3709 | u8 *data, u8 rsp_code, u8 amp_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3710 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3711 | struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; |
| 3712 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3713 | struct l2cap_chan *chan = NULL, *pchan; |
Nathan Holstein | d793fe8 | 2010-10-15 11:54:02 -0400 | [diff] [blame] | 3714 | struct sock *parent, *sk = NULL; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3715 | int result, status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | |
| 3717 | u16 dcid = 0, scid = __le16_to_cpu(req->scid); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3718 | __le16 psm = req->psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3719 | |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 3720 | 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] | 3721 | |
| 3722 | /* Check if we have socket listening on psm */ |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 3723 | 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] | 3724 | if (!pchan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3725 | result = L2CAP_CR_BAD_PSM; |
| 3726 | goto sendresp; |
| 3727 | } |
| 3728 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3729 | parent = pchan->sk; |
| 3730 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3731 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 3732 | lock_sock(parent); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 3733 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3734 | /* Check if the ACL is secure enough (if not SDP) */ |
Andrei Emeltchenko | 2983fd6 | 2012-05-24 15:42:50 +0300 | [diff] [blame] | 3735 | if (psm != __constant_cpu_to_le16(L2CAP_PSM_SDP) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3736 | !hci_conn_check_link_mode(conn->hcon)) { |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 3737 | conn->disc_reason = HCI_ERROR_AUTH_FAILURE; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 3738 | result = L2CAP_CR_SEC_BLOCK; |
| 3739 | goto response; |
| 3740 | } |
| 3741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3742 | result = L2CAP_CR_NO_MEM; |
| 3743 | |
Gustavo Padovan | 2dfa100 | 2012-05-27 22:27:58 -0300 | [diff] [blame] | 3744 | /* Check if we already have channel with that dcid */ |
| 3745 | if (__l2cap_get_chan_by_dcid(conn, scid)) |
| 3746 | goto response; |
| 3747 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 3748 | chan = pchan->ops->new_connection(pchan); |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 3749 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | goto response; |
| 3751 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 3752 | sk = chan->sk; |
| 3753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3754 | hci_conn_hold(conn->hcon); |
| 3755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | bacpy(&bt_sk(sk)->src, conn->src); |
| 3757 | bacpy(&bt_sk(sk)->dst, conn->dst); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3758 | chan->psm = psm; |
| 3759 | chan->dcid = scid; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3760 | chan->local_amp_id = amp_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3762 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3763 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3764 | dcid = chan->scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3765 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 3766 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3767 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3768 | chan->ident = cmd->ident; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3769 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3770 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 3771 | if (l2cap_chan_check_security(chan)) { |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 3772 | if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 3773 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3774 | result = L2CAP_CR_PEND; |
| 3775 | status = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 3776 | chan->ops->defer(chan); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3777 | } else { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3778 | /* Force pending result for AMP controllers. |
| 3779 | * The connection will succeed after the |
| 3780 | * physical link is up. |
| 3781 | */ |
| 3782 | if (amp_id) { |
| 3783 | __l2cap_state_change(chan, BT_CONNECT2); |
| 3784 | result = L2CAP_CR_PEND; |
| 3785 | } else { |
| 3786 | __l2cap_state_change(chan, BT_CONFIG); |
| 3787 | result = L2CAP_CR_SUCCESS; |
| 3788 | } |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 3789 | status = L2CAP_CS_NO_INFO; |
| 3790 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3791 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 3792 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3793 | result = L2CAP_CR_PEND; |
| 3794 | status = L2CAP_CS_AUTHEN_PEND; |
| 3795 | } |
| 3796 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 3797 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3798 | result = L2CAP_CR_PEND; |
| 3799 | status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3800 | } |
| 3801 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3802 | response: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 3803 | release_sock(parent); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3804 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3805 | |
| 3806 | sendresp: |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 3807 | rsp.scid = cpu_to_le16(scid); |
| 3808 | rsp.dcid = cpu_to_le16(dcid); |
| 3809 | rsp.result = cpu_to_le16(result); |
| 3810 | rsp.status = cpu_to_le16(status); |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3811 | l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3812 | |
| 3813 | if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { |
| 3814 | struct l2cap_info_req info; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 3815 | info.type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3816 | |
| 3817 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 3818 | conn->info_ident = l2cap_get_ident(conn); |
| 3819 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 3820 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3821 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3822 | l2cap_send_cmd(conn, conn->info_ident, L2CAP_INFO_REQ, |
| 3823 | sizeof(info), &info); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 3824 | } |
| 3825 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3826 | if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3827 | result == L2CAP_CR_SUCCESS) { |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3828 | u8 buf[128]; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3829 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3830 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3831 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3832 | chan->num_conf_req++; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3833 | } |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 3834 | |
| 3835 | return chan; |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3836 | } |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3837 | |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3838 | static int l2cap_connect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3839 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
Mat Martineau | 4c89b6a | 2012-10-11 17:48:22 +0300 | [diff] [blame] | 3840 | { |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3841 | struct hci_dev *hdev = conn->hcon->hdev; |
| 3842 | struct hci_conn *hcon = conn->hcon; |
| 3843 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3844 | if (cmd_len < sizeof(struct l2cap_conn_req)) |
| 3845 | return -EPROTO; |
| 3846 | |
Jaganath Kanakkassery | 7b064ed | 2013-01-10 10:28:35 +0530 | [diff] [blame] | 3847 | hci_dev_lock(hdev); |
| 3848 | if (test_bit(HCI_MGMT, &hdev->dev_flags) && |
| 3849 | !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags)) |
| 3850 | mgmt_device_connected(hdev, &hcon->dst, hcon->type, |
| 3851 | hcon->dst_type, 0, NULL, 0, |
| 3852 | hcon->dev_class); |
| 3853 | hci_dev_unlock(hdev); |
| 3854 | |
Gustavo Padovan | 300229f | 2012-10-12 19:40:40 +0800 | [diff] [blame] | 3855 | l2cap_connect(conn, cmd, data, L2CAP_CONN_RSP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3856 | return 0; |
| 3857 | } |
| 3858 | |
Mat Martineau | 5909cf3 | 2012-10-23 15:24:08 -0700 | [diff] [blame] | 3859 | static int l2cap_connect_create_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3860 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3861 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3862 | { |
| 3863 | struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; |
| 3864 | u16 scid, dcid, result, status; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3865 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3866 | u8 req[128]; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3867 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3868 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3869 | if (cmd_len < sizeof(*rsp)) |
| 3870 | return -EPROTO; |
| 3871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3872 | scid = __le16_to_cpu(rsp->scid); |
| 3873 | dcid = __le16_to_cpu(rsp->dcid); |
| 3874 | result = __le16_to_cpu(rsp->result); |
| 3875 | status = __le16_to_cpu(rsp->status); |
| 3876 | |
Andrei Emeltchenko | 1b009c9 | 2012-02-21 12:54:54 +0200 | [diff] [blame] | 3877 | 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] | 3878 | dcid, scid, result, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3879 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3880 | mutex_lock(&conn->chan_lock); |
| 3881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3882 | if (scid) { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3883 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 3884 | if (!chan) { |
| 3885 | err = -EFAULT; |
| 3886 | goto unlock; |
| 3887 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3888 | } else { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3889 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 3890 | if (!chan) { |
| 3891 | err = -EFAULT; |
| 3892 | goto unlock; |
| 3893 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3894 | } |
| 3895 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3896 | err = 0; |
| 3897 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3898 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3899 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | switch (result) { |
| 3901 | case L2CAP_CR_SUCCESS: |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 3902 | l2cap_state_change(chan, BT_CONFIG); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3903 | chan->ident = 0; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3904 | chan->dcid = dcid; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3905 | clear_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 3906 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3907 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 3908 | break; |
| 3909 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3910 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3911 | l2cap_build_conf_req(chan, req), req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3912 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3913 | break; |
| 3914 | |
| 3915 | case L2CAP_CR_PEND: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3916 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3917 | break; |
| 3918 | |
| 3919 | default: |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3920 | l2cap_chan_del(chan, ECONNREFUSED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3921 | break; |
| 3922 | } |
| 3923 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3924 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3925 | |
| 3926 | unlock: |
| 3927 | mutex_unlock(&conn->chan_lock); |
| 3928 | |
| 3929 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3930 | } |
| 3931 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3932 | static inline void set_default_fcs(struct l2cap_chan *chan) |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 3933 | { |
| 3934 | /* FCS is enabled only in ERTM or streaming mode, if one or both |
| 3935 | * sides request it. |
| 3936 | */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3937 | if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3938 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | f2592d3 | 2012-11-29 17:46:08 +0200 | [diff] [blame] | 3939 | else if (!test_bit(CONF_RECV_NO_FCS, &chan->conf_state)) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3940 | chan->fcs = L2CAP_FCS_CRC16; |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 3941 | } |
| 3942 | |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 3943 | static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data, |
| 3944 | u8 ident, u16 flags) |
| 3945 | { |
| 3946 | struct l2cap_conn *conn = chan->conn; |
| 3947 | |
| 3948 | BT_DBG("conn %p chan %p ident %d flags 0x%4.4x", conn, chan, ident, |
| 3949 | flags); |
| 3950 | |
| 3951 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
| 3952 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
| 3953 | |
| 3954 | l2cap_send_cmd(conn, ident, L2CAP_CONF_RSP, |
| 3955 | l2cap_build_conf_rsp(chan, data, |
| 3956 | L2CAP_CONF_SUCCESS, flags), data); |
| 3957 | } |
| 3958 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3959 | static inline int l2cap_config_req(struct l2cap_conn *conn, |
| 3960 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3961 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3962 | { |
| 3963 | struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; |
| 3964 | u16 dcid, flags; |
| 3965 | u8 rsp[64]; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3966 | struct l2cap_chan *chan; |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 3967 | int len, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3969 | if (cmd_len < sizeof(*req)) |
| 3970 | return -EPROTO; |
| 3971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3972 | dcid = __le16_to_cpu(req->dcid); |
| 3973 | flags = __le16_to_cpu(req->flags); |
| 3974 | |
| 3975 | BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); |
| 3976 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 3977 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3978 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | return -ENOENT; |
| 3980 | |
David S. Miller | 033b114 | 2011-07-21 13:38:42 -0700 | [diff] [blame] | 3981 | if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3982 | struct l2cap_cmd_rej_cid rej; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 3983 | |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 3984 | rej.reason = __constant_cpu_to_le16(L2CAP_REJ_INVALID_CID); |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3985 | rej.scid = cpu_to_le16(chan->scid); |
| 3986 | rej.dcid = cpu_to_le16(chan->dcid); |
| 3987 | |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 3988 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3989 | sizeof(rej), &rej); |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 3990 | goto unlock; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 3991 | } |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 3992 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3993 | /* Reject if config buffer is too small. */ |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3994 | len = cmd_len - sizeof(*req); |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 3995 | if (chan->conf_len + len > sizeof(chan->conf_req)) { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3996 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 3997 | l2cap_build_conf_rsp(chan, rsp, |
| 3998 | L2CAP_CONF_REJECT, flags), rsp); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 3999 | goto unlock; |
| 4000 | } |
| 4001 | |
| 4002 | /* Store config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4003 | memcpy(chan->conf_req + chan->conf_len, req->data, len); |
| 4004 | chan->conf_len += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4006 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | /* Incomplete config. Send empty response. */ |
| 4008 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4009 | l2cap_build_conf_rsp(chan, rsp, |
| 4010 | L2CAP_CONF_SUCCESS, flags), rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | goto unlock; |
| 4012 | } |
| 4013 | |
| 4014 | /* Complete config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4015 | len = l2cap_parse_conf_req(chan, rsp); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4016 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4017 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4018 | goto unlock; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4019 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4020 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4021 | chan->ident = cmd->ident; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4022 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4023 | chan->num_conf_rsp++; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4024 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4025 | /* Reset config buffer. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4026 | chan->conf_len = 0; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 4027 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4028 | if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4029 | goto unlock; |
| 4030 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4031 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4032 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4033 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4034 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4035 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4036 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4037 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4038 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4039 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4040 | else |
| 4041 | l2cap_chan_ready(chan); |
| 4042 | |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 4043 | goto unlock; |
| 4044 | } |
| 4045 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4046 | if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 4047 | u8 buf[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4048 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4049 | l2cap_build_conf_req(chan, buf), buf); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4050 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4051 | } |
| 4052 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4053 | /* Got Conf Rsp PENDING from remote side and asume we sent |
| 4054 | Conf Rsp PENDING in the code above */ |
| 4055 | if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && |
Andrei Emeltchenko | 29d8a59 | 2012-09-21 12:30:05 +0300 | [diff] [blame] | 4056 | test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4057 | |
| 4058 | /* check compatibility */ |
| 4059 | |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4060 | /* Send rsp for BR/EDR channel */ |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4061 | if (!chan->hs_hcon) |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4062 | l2cap_send_efs_conf_rsp(chan, rsp, cmd->ident, flags); |
| 4063 | else |
| 4064 | chan->ident = cmd->ident; |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4065 | } |
| 4066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | unlock: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4068 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4069 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4070 | } |
| 4071 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4072 | static inline int l2cap_config_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4073 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4074 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4075 | { |
| 4076 | struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; |
| 4077 | u16 scid, flags, result; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4078 | struct l2cap_chan *chan; |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4079 | int len = cmd_len - sizeof(*rsp); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4080 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4081 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4082 | if (cmd_len < sizeof(*rsp)) |
| 4083 | return -EPROTO; |
| 4084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4085 | scid = __le16_to_cpu(rsp->scid); |
| 4086 | flags = __le16_to_cpu(rsp->flags); |
| 4087 | result = __le16_to_cpu(rsp->result); |
| 4088 | |
Andrei Emeltchenko | 61386cb | 2012-03-12 12:13:07 +0200 | [diff] [blame] | 4089 | BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags, |
| 4090 | result, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4091 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4092 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4093 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | return 0; |
| 4095 | |
| 4096 | switch (result) { |
| 4097 | case L2CAP_CONF_SUCCESS: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4098 | l2cap_conf_rfc_get(chan, rsp->data, len); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4099 | clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4100 | break; |
| 4101 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4102 | case L2CAP_CONF_PENDING: |
| 4103 | set_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
| 4104 | |
| 4105 | if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
| 4106 | char buf[64]; |
| 4107 | |
| 4108 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4109 | buf, &result); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4110 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4111 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4112 | goto done; |
| 4113 | } |
| 4114 | |
Andrei Emeltchenko | f351bc7 | 2012-10-31 15:46:35 +0200 | [diff] [blame] | 4115 | if (!chan->hs_hcon) { |
Andrei Emeltchenko | 79de886 | 2012-10-15 11:58:42 +0300 | [diff] [blame] | 4116 | l2cap_send_efs_conf_rsp(chan, buf, cmd->ident, |
| 4117 | 0); |
Andrei Emeltchenko | 5ce66b5 | 2012-10-31 15:46:30 +0200 | [diff] [blame] | 4118 | } else { |
| 4119 | if (l2cap_check_efs(chan)) { |
| 4120 | amp_create_logical_link(chan); |
| 4121 | chan->ident = cmd->ident; |
| 4122 | } |
| 4123 | } |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 4124 | } |
| 4125 | goto done; |
| 4126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4127 | case L2CAP_CONF_UNACCEPT: |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4128 | if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4129 | char req[64]; |
| 4130 | |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4131 | if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4132 | l2cap_send_disconn_req(chan, ECONNRESET); |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 4133 | goto done; |
| 4134 | } |
| 4135 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4136 | /* throw out any old stored conf requests */ |
| 4137 | result = L2CAP_CONF_SUCCESS; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 4138 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4139 | req, &result); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4140 | if (len < 0) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4141 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4142 | goto done; |
| 4143 | } |
| 4144 | |
| 4145 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4146 | L2CAP_CONF_REQ, len, req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 4147 | chan->num_conf_req++; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 4148 | if (result != L2CAP_CONF_SUCCESS) |
| 4149 | goto done; |
| 4150 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | } |
| 4152 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 4153 | default: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4154 | l2cap_chan_set_err(chan, ECONNRESET); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 4155 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 4156 | __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4157 | l2cap_send_disconn_req(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | goto done; |
| 4159 | } |
| 4160 | |
Andrei Emeltchenko | 59e54bd | 2012-05-23 15:44:06 +0300 | [diff] [blame] | 4161 | if (flags & L2CAP_CONF_FLAG_CONTINUATION) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4162 | goto done; |
| 4163 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4164 | set_bit(CONF_INPUT_DONE, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4166 | if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4167 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4168 | |
Mat Martineau | 105bdf9 | 2012-04-27 16:50:48 -0700 | [diff] [blame] | 4169 | if (chan->mode == L2CAP_MODE_ERTM || |
| 4170 | chan->mode == L2CAP_MODE_STREAMING) |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4171 | err = l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 4172 | |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4173 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4174 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4175 | else |
| 4176 | l2cap_chan_ready(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4180 | l2cap_chan_unlock(chan); |
Mat Martineau | 3c58819 | 2012-04-11 10:48:42 -0700 | [diff] [blame] | 4181 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | } |
| 4183 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4184 | static inline int l2cap_disconnect_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4185 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4186 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4187 | { |
| 4188 | struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; |
| 4189 | struct l2cap_disconn_rsp rsp; |
| 4190 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4191 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4192 | struct sock *sk; |
| 4193 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4194 | if (cmd_len != sizeof(*req)) |
| 4195 | return -EPROTO; |
| 4196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | scid = __le16_to_cpu(req->scid); |
| 4198 | dcid = __le16_to_cpu(req->dcid); |
| 4199 | |
| 4200 | BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); |
| 4201 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4202 | mutex_lock(&conn->chan_lock); |
| 4203 | |
| 4204 | chan = __l2cap_get_chan_by_scid(conn, dcid); |
| 4205 | if (!chan) { |
| 4206 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4207 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4208 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4209 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4210 | l2cap_chan_lock(chan); |
| 4211 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4212 | sk = chan->sk; |
| 4213 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4214 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4215 | rsp.scid = cpu_to_le16(chan->dcid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4216 | l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); |
| 4217 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4218 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | sk->sk_shutdown = SHUTDOWN_MASK; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4220 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4222 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4223 | l2cap_chan_del(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4224 | |
| 4225 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4227 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4228 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4229 | |
| 4230 | mutex_unlock(&conn->chan_lock); |
| 4231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4232 | return 0; |
| 4233 | } |
| 4234 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4235 | static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4236 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4237 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | { |
| 4239 | struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; |
| 4240 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4241 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4243 | if (cmd_len != sizeof(*rsp)) |
| 4244 | return -EPROTO; |
| 4245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4246 | scid = __le16_to_cpu(rsp->scid); |
| 4247 | dcid = __le16_to_cpu(rsp->dcid); |
| 4248 | |
| 4249 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); |
| 4250 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4251 | mutex_lock(&conn->chan_lock); |
| 4252 | |
| 4253 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 4254 | if (!chan) { |
| 4255 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4256 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4259 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4260 | |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4261 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4262 | l2cap_chan_del(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4263 | |
| 4264 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 4266 | chan->ops->close(chan); |
Mat Martineau | 61d6ef3 | 2012-04-27 16:50:50 -0700 | [diff] [blame] | 4267 | l2cap_chan_put(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4268 | |
| 4269 | mutex_unlock(&conn->chan_lock); |
| 4270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4271 | return 0; |
| 4272 | } |
| 4273 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4274 | static inline int l2cap_information_req(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4275 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4276 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | { |
| 4278 | struct l2cap_info_req *req = (struct l2cap_info_req *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | u16 type; |
| 4280 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4281 | if (cmd_len != sizeof(*req)) |
| 4282 | return -EPROTO; |
| 4283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4284 | type = __le16_to_cpu(req->type); |
| 4285 | |
| 4286 | BT_DBG("type 0x%4.4x", type); |
| 4287 | |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4288 | if (type == L2CAP_IT_FEAT_MASK) { |
| 4289 | u8 buf[8]; |
Marcel Holtmann | 44dd46d | 2009-05-02 19:09:01 -0700 | [diff] [blame] | 4290 | u32 feat_mask = l2cap_feat_mask; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4291 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4292 | rsp->type = __constant_cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 4293 | rsp->result = __constant_cpu_to_le16(L2CAP_IR_SUCCESS); |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 4294 | if (!disable_ertm) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4295 | feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4296 | | L2CAP_FEAT_FCS; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 4297 | if (enable_hs) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 4298 | feat_mask |= L2CAP_FEAT_EXT_FLOW |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4299 | | L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 4300 | |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 4301 | put_unaligned_le32(feat_mask, rsp->data); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4302 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4303 | buf); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4304 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
| 4305 | u8 buf[12]; |
| 4306 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 4307 | |
| 4308 | if (enable_hs) |
| 4309 | l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; |
| 4310 | else |
| 4311 | l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; |
| 4312 | |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4313 | rsp->type = __constant_cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 4314 | rsp->result = __constant_cpu_to_le16(L2CAP_IR_SUCCESS); |
Andrei Emeltchenko | c6337ea | 2011-10-20 17:02:44 +0300 | [diff] [blame] | 4315 | memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan)); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4316 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(buf), |
| 4317 | buf); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4318 | } else { |
| 4319 | struct l2cap_info_rsp rsp; |
| 4320 | rsp.type = cpu_to_le16(type); |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4321 | rsp.result = __constant_cpu_to_le16(L2CAP_IR_NOTSUPP); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4322 | l2cap_send_cmd(conn, cmd->ident, L2CAP_INFO_RSP, sizeof(rsp), |
| 4323 | &rsp); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 4324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4325 | |
| 4326 | return 0; |
| 4327 | } |
| 4328 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4329 | static inline int l2cap_information_rsp(struct l2cap_conn *conn, |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4330 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 4331 | u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | { |
| 4333 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; |
| 4334 | u16 type, result; |
| 4335 | |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 4336 | if (cmd_len != sizeof(*rsp)) |
| 4337 | return -EPROTO; |
| 4338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | type = __le16_to_cpu(rsp->type); |
| 4340 | result = __le16_to_cpu(rsp->result); |
| 4341 | |
| 4342 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); |
| 4343 | |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4344 | /* L2CAP Info req/rsp are unbound to channels, add extra checks */ |
| 4345 | if (cmd->ident != conn->info_ident || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4346 | conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 4347 | return 0; |
| 4348 | |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 4349 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4350 | |
Ville Tervo | adb08ed | 2010-08-04 09:43:33 +0300 | [diff] [blame] | 4351 | if (result != L2CAP_IR_SUCCESS) { |
| 4352 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4353 | conn->info_ident = 0; |
| 4354 | |
| 4355 | l2cap_conn_start(conn); |
| 4356 | |
| 4357 | return 0; |
| 4358 | } |
| 4359 | |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4360 | switch (type) { |
| 4361 | case L2CAP_IT_FEAT_MASK: |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 4362 | conn->feat_mask = get_unaligned_le32(rsp->data); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4363 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 4364 | if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4365 | struct l2cap_info_req req; |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 4366 | req.type = __constant_cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4367 | |
| 4368 | conn->info_ident = l2cap_get_ident(conn); |
| 4369 | |
| 4370 | l2cap_send_cmd(conn, conn->info_ident, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 4371 | L2CAP_INFO_REQ, sizeof(req), &req); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4372 | } else { |
| 4373 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 4374 | conn->info_ident = 0; |
| 4375 | |
| 4376 | l2cap_conn_start(conn); |
| 4377 | } |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4378 | break; |
| 4379 | |
| 4380 | case L2CAP_IT_FIXED_CHAN: |
| 4381 | conn->fixed_chan_mask = rsp->data[0]; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4382 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4383 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4384 | |
| 4385 | l2cap_conn_start(conn); |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 4386 | break; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 4387 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 4388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | return 0; |
| 4390 | } |
| 4391 | |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4392 | static int l2cap_create_channel_req(struct l2cap_conn *conn, |
| 4393 | struct l2cap_cmd_hdr *cmd, |
| 4394 | u16 cmd_len, void *data) |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4395 | { |
| 4396 | struct l2cap_create_chan_req *req = data; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4397 | struct l2cap_create_chan_rsp rsp; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4398 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4399 | struct hci_dev *hdev; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4400 | u16 psm, scid; |
| 4401 | |
| 4402 | if (cmd_len != sizeof(*req)) |
| 4403 | return -EPROTO; |
| 4404 | |
| 4405 | if (!enable_hs) |
| 4406 | return -EINVAL; |
| 4407 | |
| 4408 | psm = le16_to_cpu(req->psm); |
| 4409 | scid = le16_to_cpu(req->scid); |
| 4410 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4411 | 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] | 4412 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4413 | /* For controller id 0 make BR/EDR connection */ |
| 4414 | if (req->amp_id == HCI_BREDR_ID) { |
| 4415 | l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4416 | req->amp_id); |
| 4417 | return 0; |
| 4418 | } |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4419 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4420 | /* Validate AMP controller id */ |
| 4421 | hdev = hci_dev_get(req->amp_id); |
| 4422 | if (!hdev) |
| 4423 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4424 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4425 | if (hdev->dev_type != HCI_AMP || !test_bit(HCI_UP, &hdev->flags)) { |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4426 | hci_dev_put(hdev); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4427 | goto error; |
Mat Martineau | 1700915 | 2012-10-23 15:24:07 -0700 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | chan = l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, |
| 4431 | req->amp_id); |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4432 | if (chan) { |
| 4433 | struct amp_mgr *mgr = conn->hcon->amp_mgr; |
| 4434 | struct hci_conn *hs_hcon; |
| 4435 | |
| 4436 | hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, conn->dst); |
| 4437 | if (!hs_hcon) { |
| 4438 | hci_dev_put(hdev); |
| 4439 | return -EFAULT; |
| 4440 | } |
| 4441 | |
| 4442 | BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon); |
| 4443 | |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4444 | mgr->bredr_chan = chan; |
| 4445 | chan->hs_hcon = hs_hcon; |
Andrei Emeltchenko | fd45bf4 | 2012-11-20 17:16:22 +0200 | [diff] [blame] | 4446 | chan->fcs = L2CAP_FCS_NONE; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4447 | conn->mtu = hdev->block_mtu; |
| 4448 | } |
| 4449 | |
| 4450 | hci_dev_put(hdev); |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4451 | |
| 4452 | return 0; |
Andrei Emeltchenko | 6e1df6a | 2012-11-01 15:37:02 +0200 | [diff] [blame] | 4453 | |
| 4454 | error: |
| 4455 | rsp.dcid = 0; |
| 4456 | rsp.scid = cpu_to_le16(scid); |
| 4457 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_BAD_AMP); |
| 4458 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
| 4459 | |
| 4460 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, |
| 4461 | sizeof(rsp), &rsp); |
| 4462 | |
| 4463 | return -EFAULT; |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 4464 | } |
| 4465 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4466 | static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id) |
| 4467 | { |
| 4468 | struct l2cap_move_chan_req req; |
| 4469 | u8 ident; |
| 4470 | |
| 4471 | BT_DBG("chan %p, dest_amp_id %d", chan, dest_amp_id); |
| 4472 | |
| 4473 | ident = l2cap_get_ident(chan->conn); |
| 4474 | chan->ident = ident; |
| 4475 | |
| 4476 | req.icid = cpu_to_le16(chan->scid); |
| 4477 | req.dest_amp_id = dest_amp_id; |
| 4478 | |
| 4479 | l2cap_send_cmd(chan->conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req), |
| 4480 | &req); |
| 4481 | |
| 4482 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4483 | } |
| 4484 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4485 | 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] | 4486 | { |
| 4487 | struct l2cap_move_chan_rsp rsp; |
| 4488 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4489 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4490 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4491 | rsp.icid = cpu_to_le16(chan->dcid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4492 | rsp.result = cpu_to_le16(result); |
| 4493 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4494 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_RSP, |
| 4495 | sizeof(rsp), &rsp); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4496 | } |
| 4497 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4498 | 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] | 4499 | { |
| 4500 | struct l2cap_move_chan_cfm cfm; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4501 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4502 | BT_DBG("chan %p, result 0x%4.4x", chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4503 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4504 | chan->ident = l2cap_get_ident(chan->conn); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4505 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4506 | cfm.icid = cpu_to_le16(chan->scid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4507 | cfm.result = cpu_to_le16(result); |
| 4508 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4509 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_MOVE_CHAN_CFM, |
| 4510 | sizeof(cfm), &cfm); |
| 4511 | |
| 4512 | __set_chan_timer(chan, L2CAP_MOVE_TIMEOUT); |
| 4513 | } |
| 4514 | |
| 4515 | static void l2cap_send_move_chan_cfm_icid(struct l2cap_conn *conn, u16 icid) |
| 4516 | { |
| 4517 | struct l2cap_move_chan_cfm cfm; |
| 4518 | |
| 4519 | BT_DBG("conn %p, icid 0x%4.4x", conn, icid); |
| 4520 | |
| 4521 | cfm.icid = cpu_to_le16(icid); |
| 4522 | cfm.result = __constant_cpu_to_le16(L2CAP_MC_UNCONFIRMED); |
| 4523 | |
| 4524 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_MOVE_CHAN_CFM, |
| 4525 | sizeof(cfm), &cfm); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | 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] | 4529 | u16 icid) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4530 | { |
| 4531 | struct l2cap_move_chan_cfm_rsp rsp; |
| 4532 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4533 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4534 | |
| 4535 | rsp.icid = cpu_to_le16(icid); |
| 4536 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); |
| 4537 | } |
| 4538 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 4539 | static void __release_logical_link(struct l2cap_chan *chan) |
| 4540 | { |
| 4541 | chan->hs_hchan = NULL; |
| 4542 | chan->hs_hcon = NULL; |
| 4543 | |
| 4544 | /* Placeholder - release the logical link */ |
| 4545 | } |
| 4546 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4547 | static void l2cap_logical_fail(struct l2cap_chan *chan) |
| 4548 | { |
| 4549 | /* Logical link setup failed */ |
| 4550 | if (chan->state != BT_CONNECTED) { |
| 4551 | /* Create channel failure, disconnect */ |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4552 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4553 | return; |
| 4554 | } |
| 4555 | |
| 4556 | switch (chan->move_role) { |
| 4557 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4558 | l2cap_move_done(chan); |
| 4559 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_SUPP); |
| 4560 | break; |
| 4561 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4562 | if (chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_COMP || |
| 4563 | chan->move_state == L2CAP_MOVE_WAIT_LOGICAL_CFM) { |
| 4564 | /* Remote has only sent pending or |
| 4565 | * success responses, clean up |
| 4566 | */ |
| 4567 | l2cap_move_done(chan); |
| 4568 | } |
| 4569 | |
| 4570 | /* Other amp move states imply that the move |
| 4571 | * has already aborted |
| 4572 | */ |
| 4573 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 4574 | break; |
| 4575 | } |
| 4576 | } |
| 4577 | |
| 4578 | static void l2cap_logical_finish_create(struct l2cap_chan *chan, |
| 4579 | struct hci_chan *hchan) |
| 4580 | { |
| 4581 | struct l2cap_conf_rsp rsp; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4582 | |
Andrei Emeltchenko | 336178a | 2012-10-31 15:46:27 +0200 | [diff] [blame] | 4583 | chan->hs_hchan = hchan; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4584 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4585 | |
Andrei Emeltchenko | 35ba956 | 2012-10-25 15:20:43 +0300 | [diff] [blame] | 4586 | l2cap_send_efs_conf_rsp(chan, &rsp, chan->ident, 0); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4587 | |
| 4588 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Andrei Emeltchenko | fe79c6f | 2012-10-25 15:20:42 +0300 | [diff] [blame] | 4589 | int err; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4590 | |
| 4591 | set_default_fcs(chan); |
| 4592 | |
| 4593 | err = l2cap_ertm_init(chan); |
| 4594 | if (err < 0) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 4595 | l2cap_send_disconn_req(chan, -err); |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4596 | else |
| 4597 | l2cap_chan_ready(chan); |
| 4598 | } |
| 4599 | } |
| 4600 | |
| 4601 | static void l2cap_logical_finish_move(struct l2cap_chan *chan, |
| 4602 | struct hci_chan *hchan) |
| 4603 | { |
| 4604 | chan->hs_hcon = hchan->conn; |
| 4605 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4606 | |
| 4607 | BT_DBG("move_state %d", chan->move_state); |
| 4608 | |
| 4609 | switch (chan->move_state) { |
| 4610 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 4611 | /* Move confirm will be sent after a success |
| 4612 | * response is received |
| 4613 | */ |
| 4614 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4615 | break; |
| 4616 | case L2CAP_MOVE_WAIT_LOGICAL_CFM: |
| 4617 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4618 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4619 | } else if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 4620 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 4621 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4622 | } else if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4623 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4624 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4625 | } |
| 4626 | break; |
| 4627 | default: |
| 4628 | /* Move was not in expected state, free the channel */ |
| 4629 | __release_logical_link(chan); |
| 4630 | |
| 4631 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4632 | } |
| 4633 | } |
| 4634 | |
| 4635 | /* Call with chan locked */ |
Andrei Emeltchenko | 27695fb | 2012-10-25 15:20:45 +0300 | [diff] [blame] | 4636 | void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan, |
| 4637 | u8 status) |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4638 | { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4639 | BT_DBG("chan %p, hchan %p, status %d", chan, hchan, status); |
| 4640 | |
| 4641 | if (status) { |
| 4642 | l2cap_logical_fail(chan); |
| 4643 | __release_logical_link(chan); |
| 4644 | return; |
| 4645 | } |
| 4646 | |
| 4647 | if (chan->state != BT_CONNECTED) { |
| 4648 | /* Ignore logical link if channel is on BR/EDR */ |
| 4649 | if (chan->local_amp_id) |
| 4650 | l2cap_logical_finish_create(chan, hchan); |
| 4651 | } else { |
| 4652 | l2cap_logical_finish_move(chan, hchan); |
| 4653 | } |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4654 | } |
| 4655 | |
Mat Martineau | 3f7a56c | 2012-10-23 15:24:23 -0700 | [diff] [blame] | 4656 | void l2cap_move_start(struct l2cap_chan *chan) |
| 4657 | { |
| 4658 | BT_DBG("chan %p", chan); |
| 4659 | |
| 4660 | if (chan->local_amp_id == HCI_BREDR_ID) { |
| 4661 | if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED) |
| 4662 | return; |
| 4663 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4664 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4665 | /* Placeholder - start physical link setup */ |
| 4666 | } else { |
| 4667 | chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; |
| 4668 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4669 | chan->move_id = 0; |
| 4670 | l2cap_move_setup(chan); |
| 4671 | l2cap_send_move_chan_req(chan, 0); |
| 4672 | } |
| 4673 | } |
| 4674 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4675 | static void l2cap_do_create(struct l2cap_chan *chan, int result, |
| 4676 | u8 local_amp_id, u8 remote_amp_id) |
| 4677 | { |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4678 | BT_DBG("chan %p state %s %u -> %u", chan, state_to_string(chan->state), |
| 4679 | local_amp_id, remote_amp_id); |
| 4680 | |
Andrei Emeltchenko | 12d6cc6 | 2012-11-14 17:39:31 +0200 | [diff] [blame] | 4681 | chan->fcs = L2CAP_FCS_NONE; |
| 4682 | |
Andrei Emeltchenko | 62748ca | 2012-11-20 17:16:19 +0200 | [diff] [blame] | 4683 | /* Outgoing channel on AMP */ |
| 4684 | if (chan->state == BT_CONNECT) { |
| 4685 | if (result == L2CAP_CR_SUCCESS) { |
| 4686 | chan->local_amp_id = local_amp_id; |
| 4687 | l2cap_send_create_chan_req(chan, remote_amp_id); |
| 4688 | } else { |
| 4689 | /* Revert to BR/EDR connect */ |
| 4690 | l2cap_send_conn_req(chan); |
| 4691 | } |
| 4692 | |
| 4693 | return; |
| 4694 | } |
| 4695 | |
| 4696 | /* Incoming channel on AMP */ |
| 4697 | if (__l2cap_no_conn_pending(chan)) { |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4698 | struct l2cap_conn_rsp rsp; |
| 4699 | char buf[128]; |
| 4700 | rsp.scid = cpu_to_le16(chan->dcid); |
| 4701 | rsp.dcid = cpu_to_le16(chan->scid); |
| 4702 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4703 | if (result == L2CAP_CR_SUCCESS) { |
| 4704 | /* Send successful response */ |
Andrei Emeltchenko | 62cd50e | 2012-11-14 17:39:32 +0200 | [diff] [blame] | 4705 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS); |
| 4706 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4707 | } else { |
| 4708 | /* Send negative response */ |
Andrei Emeltchenko | 62cd50e | 2012-11-14 17:39:32 +0200 | [diff] [blame] | 4709 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_NO_MEM); |
| 4710 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | l2cap_send_cmd(chan->conn, chan->ident, L2CAP_CREATE_CHAN_RSP, |
| 4714 | sizeof(rsp), &rsp); |
| 4715 | |
| 4716 | if (result == L2CAP_CR_SUCCESS) { |
| 4717 | __l2cap_state_change(chan, BT_CONFIG); |
| 4718 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 4719 | l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn), |
| 4720 | L2CAP_CONF_REQ, |
| 4721 | l2cap_build_conf_req(chan, buf), buf); |
| 4722 | chan->num_conf_req++; |
| 4723 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4724 | } |
| 4725 | } |
| 4726 | |
| 4727 | static void l2cap_do_move_initiate(struct l2cap_chan *chan, u8 local_amp_id, |
| 4728 | u8 remote_amp_id) |
| 4729 | { |
| 4730 | l2cap_move_setup(chan); |
| 4731 | chan->move_id = local_amp_id; |
| 4732 | chan->move_state = L2CAP_MOVE_WAIT_RSP; |
| 4733 | |
| 4734 | l2cap_send_move_chan_req(chan, remote_amp_id); |
| 4735 | } |
| 4736 | |
| 4737 | static void l2cap_do_move_respond(struct l2cap_chan *chan, int result) |
| 4738 | { |
| 4739 | struct hci_chan *hchan = NULL; |
| 4740 | |
| 4741 | /* Placeholder - get hci_chan for logical link */ |
| 4742 | |
| 4743 | if (hchan) { |
| 4744 | if (hchan->state == BT_CONNECTED) { |
| 4745 | /* Logical link is ready to go */ |
| 4746 | chan->hs_hcon = hchan->conn; |
| 4747 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4748 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4749 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_SUCCESS); |
| 4750 | |
| 4751 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 4752 | } else { |
| 4753 | /* Wait for logical link to be ready */ |
| 4754 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4755 | } |
| 4756 | } else { |
| 4757 | /* Logical link not available */ |
| 4758 | l2cap_send_move_chan_rsp(chan, L2CAP_MR_NOT_ALLOWED); |
| 4759 | } |
| 4760 | } |
| 4761 | |
| 4762 | static void l2cap_do_move_cancel(struct l2cap_chan *chan, int result) |
| 4763 | { |
| 4764 | if (chan->move_role == L2CAP_MOVE_ROLE_RESPONDER) { |
| 4765 | u8 rsp_result; |
| 4766 | if (result == -EINVAL) |
| 4767 | rsp_result = L2CAP_MR_BAD_ID; |
| 4768 | else |
| 4769 | rsp_result = L2CAP_MR_NOT_ALLOWED; |
| 4770 | |
| 4771 | l2cap_send_move_chan_rsp(chan, rsp_result); |
| 4772 | } |
| 4773 | |
| 4774 | chan->move_role = L2CAP_MOVE_ROLE_NONE; |
| 4775 | chan->move_state = L2CAP_MOVE_STABLE; |
| 4776 | |
| 4777 | /* Restart data transmission */ |
| 4778 | l2cap_ertm_send(chan); |
| 4779 | } |
| 4780 | |
Andrei Emeltchenko | a514b17 | 2012-11-14 17:39:30 +0200 | [diff] [blame] | 4781 | /* Invoke with locked chan */ |
| 4782 | void __l2cap_physical_cfm(struct l2cap_chan *chan, int result) |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4783 | { |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4784 | u8 local_amp_id = chan->local_amp_id; |
Andrei Emeltchenko | fffadc0 | 2012-11-01 15:37:03 +0200 | [diff] [blame] | 4785 | u8 remote_amp_id = chan->remote_amp_id; |
Andrei Emeltchenko | 770bfef | 2012-10-31 15:46:29 +0200 | [diff] [blame] | 4786 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4787 | BT_DBG("chan %p, result %d, local_amp_id %d, remote_amp_id %d", |
| 4788 | chan, result, local_amp_id, remote_amp_id); |
| 4789 | |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4790 | if (chan->state == BT_DISCONN || chan->state == BT_CLOSED) { |
| 4791 | l2cap_chan_unlock(chan); |
| 4792 | return; |
| 4793 | } |
| 4794 | |
| 4795 | if (chan->state != BT_CONNECTED) { |
| 4796 | l2cap_do_create(chan, result, local_amp_id, remote_amp_id); |
| 4797 | } else if (result != L2CAP_MR_SUCCESS) { |
| 4798 | l2cap_do_move_cancel(chan, result); |
| 4799 | } else { |
| 4800 | switch (chan->move_role) { |
| 4801 | case L2CAP_MOVE_ROLE_INITIATOR: |
| 4802 | l2cap_do_move_initiate(chan, local_amp_id, |
| 4803 | remote_amp_id); |
| 4804 | break; |
| 4805 | case L2CAP_MOVE_ROLE_RESPONDER: |
| 4806 | l2cap_do_move_respond(chan, result); |
| 4807 | break; |
| 4808 | default: |
| 4809 | l2cap_do_move_cancel(chan, result); |
| 4810 | break; |
| 4811 | } |
| 4812 | } |
Mat Martineau | 8eb200b | 2012-10-23 15:24:17 -0700 | [diff] [blame] | 4813 | } |
| 4814 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4815 | static inline int l2cap_move_channel_req(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4816 | struct l2cap_cmd_hdr *cmd, |
| 4817 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4818 | { |
| 4819 | struct l2cap_move_chan_req *req = data; |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4820 | struct l2cap_move_chan_rsp rsp; |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4821 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4822 | u16 icid = 0; |
| 4823 | u16 result = L2CAP_MR_NOT_ALLOWED; |
| 4824 | |
| 4825 | if (cmd_len != sizeof(*req)) |
| 4826 | return -EPROTO; |
| 4827 | |
| 4828 | icid = le16_to_cpu(req->icid); |
| 4829 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 4830 | 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] | 4831 | |
| 4832 | if (!enable_hs) |
| 4833 | return -EINVAL; |
| 4834 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4835 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 4836 | if (!chan) { |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4837 | rsp.icid = cpu_to_le16(icid); |
| 4838 | rsp.result = __constant_cpu_to_le16(L2CAP_MR_NOT_ALLOWED); |
| 4839 | l2cap_send_cmd(conn, cmd->ident, L2CAP_MOVE_CHAN_RSP, |
| 4840 | sizeof(rsp), &rsp); |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4841 | return 0; |
| 4842 | } |
| 4843 | |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4844 | chan->ident = cmd->ident; |
| 4845 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4846 | if (chan->scid < L2CAP_CID_DYN_START || |
| 4847 | chan->chan_policy == BT_CHANNEL_POLICY_BREDR_ONLY || |
| 4848 | (chan->mode != L2CAP_MODE_ERTM && |
| 4849 | chan->mode != L2CAP_MODE_STREAMING)) { |
| 4850 | result = L2CAP_MR_NOT_ALLOWED; |
| 4851 | goto send_move_response; |
| 4852 | } |
| 4853 | |
| 4854 | if (chan->local_amp_id == req->dest_amp_id) { |
| 4855 | result = L2CAP_MR_SAME_ID; |
| 4856 | goto send_move_response; |
| 4857 | } |
| 4858 | |
| 4859 | if (req->dest_amp_id) { |
| 4860 | struct hci_dev *hdev; |
| 4861 | hdev = hci_dev_get(req->dest_amp_id); |
| 4862 | if (!hdev || hdev->dev_type != HCI_AMP || |
| 4863 | !test_bit(HCI_UP, &hdev->flags)) { |
| 4864 | if (hdev) |
| 4865 | hci_dev_put(hdev); |
| 4866 | |
| 4867 | result = L2CAP_MR_BAD_ID; |
| 4868 | goto send_move_response; |
| 4869 | } |
| 4870 | hci_dev_put(hdev); |
| 4871 | } |
| 4872 | |
| 4873 | /* Detect a move collision. Only send a collision response |
| 4874 | * if this side has "lost", otherwise proceed with the move. |
| 4875 | * The winner has the larger bd_addr. |
| 4876 | */ |
| 4877 | if ((__chan_is_moving(chan) || |
| 4878 | chan->move_role != L2CAP_MOVE_ROLE_NONE) && |
| 4879 | bacmp(conn->src, conn->dst) > 0) { |
| 4880 | result = L2CAP_MR_COLLISION; |
| 4881 | goto send_move_response; |
| 4882 | } |
| 4883 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4884 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 4885 | l2cap_move_setup(chan); |
| 4886 | chan->move_id = req->dest_amp_id; |
| 4887 | icid = chan->dcid; |
| 4888 | |
| 4889 | if (!req->dest_amp_id) { |
| 4890 | /* Moving to BR/EDR */ |
| 4891 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 4892 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4893 | result = L2CAP_MR_PEND; |
| 4894 | } else { |
| 4895 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM; |
| 4896 | result = L2CAP_MR_SUCCESS; |
| 4897 | } |
| 4898 | } else { |
| 4899 | chan->move_state = L2CAP_MOVE_WAIT_PREPARE; |
| 4900 | /* Placeholder - uncomment when amp functions are available */ |
| 4901 | /*amp_accept_physical(chan, req->dest_amp_id);*/ |
| 4902 | result = L2CAP_MR_PEND; |
| 4903 | } |
| 4904 | |
| 4905 | send_move_response: |
Mat Martineau | 1500109b | 2012-10-23 15:24:15 -0700 | [diff] [blame] | 4906 | l2cap_send_move_chan_rsp(chan, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4907 | |
Mat Martineau | 02b0fbb | 2012-10-23 15:24:10 -0700 | [diff] [blame] | 4908 | l2cap_chan_unlock(chan); |
| 4909 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 4910 | return 0; |
| 4911 | } |
| 4912 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 4913 | static void l2cap_move_continue(struct l2cap_conn *conn, u16 icid, u16 result) |
| 4914 | { |
| 4915 | struct l2cap_chan *chan; |
| 4916 | struct hci_chan *hchan = NULL; |
| 4917 | |
| 4918 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 4919 | if (!chan) { |
| 4920 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 4921 | return; |
| 4922 | } |
| 4923 | |
| 4924 | __clear_chan_timer(chan); |
| 4925 | if (result == L2CAP_MR_PEND) |
| 4926 | __set_chan_timer(chan, L2CAP_MOVE_ERTX_TIMEOUT); |
| 4927 | |
| 4928 | switch (chan->move_state) { |
| 4929 | case L2CAP_MOVE_WAIT_LOGICAL_COMP: |
| 4930 | /* Move confirm will be sent when logical link |
| 4931 | * is complete. |
| 4932 | */ |
| 4933 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4934 | break; |
| 4935 | case L2CAP_MOVE_WAIT_RSP_SUCCESS: |
| 4936 | if (result == L2CAP_MR_PEND) { |
| 4937 | break; |
| 4938 | } else if (test_bit(CONN_LOCAL_BUSY, |
| 4939 | &chan->conn_state)) { |
| 4940 | chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; |
| 4941 | } else { |
| 4942 | /* Logical link is up or moving to BR/EDR, |
| 4943 | * proceed with move |
| 4944 | */ |
| 4945 | chan->move_state = L2CAP_MOVE_WAIT_CONFIRM_RSP; |
| 4946 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4947 | } |
| 4948 | break; |
| 4949 | case L2CAP_MOVE_WAIT_RSP: |
| 4950 | /* Moving to AMP */ |
| 4951 | if (result == L2CAP_MR_SUCCESS) { |
| 4952 | /* Remote is ready, send confirm immediately |
| 4953 | * after logical link is ready |
| 4954 | */ |
| 4955 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_CFM; |
| 4956 | } else { |
| 4957 | /* Both logical link and move success |
| 4958 | * are required to confirm |
| 4959 | */ |
| 4960 | chan->move_state = L2CAP_MOVE_WAIT_LOGICAL_COMP; |
| 4961 | } |
| 4962 | |
| 4963 | /* Placeholder - get hci_chan for logical link */ |
| 4964 | if (!hchan) { |
| 4965 | /* Logical link not available */ |
| 4966 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 4967 | break; |
| 4968 | } |
| 4969 | |
| 4970 | /* If the logical link is not yet connected, do not |
| 4971 | * send confirmation. |
| 4972 | */ |
| 4973 | if (hchan->state != BT_CONNECTED) |
| 4974 | break; |
| 4975 | |
| 4976 | /* Logical link is already ready to go */ |
| 4977 | |
| 4978 | chan->hs_hcon = hchan->conn; |
| 4979 | chan->hs_hcon->l2cap_data = chan->conn; |
| 4980 | |
| 4981 | if (result == L2CAP_MR_SUCCESS) { |
| 4982 | /* Can confirm now */ |
| 4983 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_CONFIRMED); |
| 4984 | } else { |
| 4985 | /* Now only need move success |
| 4986 | * to confirm |
| 4987 | */ |
| 4988 | chan->move_state = L2CAP_MOVE_WAIT_RSP_SUCCESS; |
| 4989 | } |
| 4990 | |
| 4991 | l2cap_logical_cfm(chan, hchan, L2CAP_MR_SUCCESS); |
| 4992 | break; |
| 4993 | default: |
| 4994 | /* Any other amp move state means the move failed. */ |
| 4995 | chan->move_id = chan->local_amp_id; |
| 4996 | l2cap_move_done(chan); |
| 4997 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 4998 | } |
| 4999 | |
| 5000 | l2cap_chan_unlock(chan); |
| 5001 | } |
| 5002 | |
| 5003 | static void l2cap_move_fail(struct l2cap_conn *conn, u8 ident, u16 icid, |
| 5004 | u16 result) |
| 5005 | { |
| 5006 | struct l2cap_chan *chan; |
| 5007 | |
| 5008 | chan = l2cap_get_chan_by_ident(conn, ident); |
| 5009 | if (!chan) { |
| 5010 | /* Could not locate channel, icid is best guess */ |
| 5011 | l2cap_send_move_chan_cfm_icid(conn, icid); |
| 5012 | return; |
| 5013 | } |
| 5014 | |
| 5015 | __clear_chan_timer(chan); |
| 5016 | |
| 5017 | if (chan->move_role == L2CAP_MOVE_ROLE_INITIATOR) { |
| 5018 | if (result == L2CAP_MR_COLLISION) { |
| 5019 | chan->move_role = L2CAP_MOVE_ROLE_RESPONDER; |
| 5020 | } else { |
| 5021 | /* Cleanup - cancel move */ |
| 5022 | chan->move_id = chan->local_amp_id; |
| 5023 | l2cap_move_done(chan); |
| 5024 | } |
| 5025 | } |
| 5026 | |
| 5027 | l2cap_send_move_chan_cfm(chan, L2CAP_MC_UNCONFIRMED); |
| 5028 | |
| 5029 | l2cap_chan_unlock(chan); |
| 5030 | } |
| 5031 | |
| 5032 | static int l2cap_move_channel_rsp(struct l2cap_conn *conn, |
| 5033 | struct l2cap_cmd_hdr *cmd, |
| 5034 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5035 | { |
| 5036 | struct l2cap_move_chan_rsp *rsp = data; |
| 5037 | u16 icid, result; |
| 5038 | |
| 5039 | if (cmd_len != sizeof(*rsp)) |
| 5040 | return -EPROTO; |
| 5041 | |
| 5042 | icid = le16_to_cpu(rsp->icid); |
| 5043 | result = le16_to_cpu(rsp->result); |
| 5044 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5045 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5046 | |
Mat Martineau | 5b155ef | 2012-10-23 15:24:14 -0700 | [diff] [blame] | 5047 | if (result == L2CAP_MR_SUCCESS || result == L2CAP_MR_PEND) |
| 5048 | l2cap_move_continue(conn, icid, result); |
| 5049 | else |
| 5050 | l2cap_move_fail(conn, cmd->ident, icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5051 | |
| 5052 | return 0; |
| 5053 | } |
| 5054 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5055 | static int l2cap_move_channel_confirm(struct l2cap_conn *conn, |
| 5056 | struct l2cap_cmd_hdr *cmd, |
| 5057 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5058 | { |
| 5059 | struct l2cap_move_chan_cfm *cfm = data; |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5060 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5061 | u16 icid, result; |
| 5062 | |
| 5063 | if (cmd_len != sizeof(*cfm)) |
| 5064 | return -EPROTO; |
| 5065 | |
| 5066 | icid = le16_to_cpu(cfm->icid); |
| 5067 | result = le16_to_cpu(cfm->result); |
| 5068 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5069 | BT_DBG("icid 0x%4.4x, result 0x%4.4x", icid, result); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5070 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5071 | chan = l2cap_get_chan_by_dcid(conn, icid); |
| 5072 | if (!chan) { |
| 5073 | /* Spec requires a response even if the icid was not found */ |
| 5074 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5075 | return 0; |
| 5076 | } |
| 5077 | |
| 5078 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) { |
| 5079 | if (result == L2CAP_MC_CONFIRMED) { |
| 5080 | chan->local_amp_id = chan->move_id; |
| 5081 | if (!chan->local_amp_id) |
| 5082 | __release_logical_link(chan); |
| 5083 | } else { |
| 5084 | chan->move_id = chan->local_amp_id; |
| 5085 | } |
| 5086 | |
| 5087 | l2cap_move_done(chan); |
| 5088 | } |
| 5089 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5090 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 5091 | |
Mat Martineau | 5f3847a | 2012-10-23 15:24:12 -0700 | [diff] [blame] | 5092 | l2cap_chan_unlock(chan); |
| 5093 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5094 | return 0; |
| 5095 | } |
| 5096 | |
| 5097 | static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5098 | struct l2cap_cmd_hdr *cmd, |
| 5099 | u16 cmd_len, void *data) |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5100 | { |
| 5101 | struct l2cap_move_chan_cfm_rsp *rsp = data; |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5102 | struct l2cap_chan *chan; |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5103 | u16 icid; |
| 5104 | |
| 5105 | if (cmd_len != sizeof(*rsp)) |
| 5106 | return -EPROTO; |
| 5107 | |
| 5108 | icid = le16_to_cpu(rsp->icid); |
| 5109 | |
Andrei Emeltchenko | ad0ac6c | 2012-07-10 15:27:50 +0300 | [diff] [blame] | 5110 | BT_DBG("icid 0x%4.4x", icid); |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5111 | |
Mat Martineau | 3fd71a0 | 2012-10-23 15:24:16 -0700 | [diff] [blame] | 5112 | chan = l2cap_get_chan_by_scid(conn, icid); |
| 5113 | if (!chan) |
| 5114 | return 0; |
| 5115 | |
| 5116 | __clear_chan_timer(chan); |
| 5117 | |
| 5118 | if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) { |
| 5119 | chan->local_amp_id = chan->move_id; |
| 5120 | |
| 5121 | if (!chan->local_amp_id && chan->hs_hchan) |
| 5122 | __release_logical_link(chan); |
| 5123 | |
| 5124 | l2cap_move_done(chan); |
| 5125 | } |
| 5126 | |
| 5127 | l2cap_chan_unlock(chan); |
| 5128 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5129 | return 0; |
| 5130 | } |
| 5131 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 5132 | 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] | 5133 | u16 to_multiplier) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5134 | { |
| 5135 | u16 max_latency; |
| 5136 | |
| 5137 | if (min > max || min < 6 || max > 3200) |
| 5138 | return -EINVAL; |
| 5139 | |
| 5140 | if (to_multiplier < 10 || to_multiplier > 3200) |
| 5141 | return -EINVAL; |
| 5142 | |
| 5143 | if (max >= to_multiplier * 8) |
| 5144 | return -EINVAL; |
| 5145 | |
| 5146 | max_latency = (to_multiplier * 8 / max) - 1; |
| 5147 | if (latency > 499 || latency > max_latency) |
| 5148 | return -EINVAL; |
| 5149 | |
| 5150 | return 0; |
| 5151 | } |
| 5152 | |
| 5153 | static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5154 | struct l2cap_cmd_hdr *cmd, |
| 5155 | u8 *data) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5156 | { |
| 5157 | struct hci_conn *hcon = conn->hcon; |
| 5158 | struct l2cap_conn_param_update_req *req; |
| 5159 | struct l2cap_conn_param_update_rsp rsp; |
| 5160 | u16 min, max, latency, to_multiplier, cmd_len; |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5161 | int err; |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5162 | |
| 5163 | if (!(hcon->link_mode & HCI_LM_MASTER)) |
| 5164 | return -EINVAL; |
| 5165 | |
| 5166 | cmd_len = __le16_to_cpu(cmd->len); |
| 5167 | if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) |
| 5168 | return -EPROTO; |
| 5169 | |
| 5170 | req = (struct l2cap_conn_param_update_req *) data; |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 5171 | min = __le16_to_cpu(req->min); |
| 5172 | max = __le16_to_cpu(req->max); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5173 | latency = __le16_to_cpu(req->latency); |
| 5174 | to_multiplier = __le16_to_cpu(req->to_multiplier); |
| 5175 | |
| 5176 | 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] | 5177 | min, max, latency, to_multiplier); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5178 | |
| 5179 | memset(&rsp, 0, sizeof(rsp)); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5180 | |
| 5181 | err = l2cap_check_conn_param(min, max, latency, to_multiplier); |
| 5182 | if (err) |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 5183 | rsp.result = __constant_cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5184 | else |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 5185 | rsp.result = __constant_cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5186 | |
| 5187 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5188 | sizeof(rsp), &rsp); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5189 | |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 5190 | if (!err) |
| 5191 | hci_le_conn_update(hcon, min, max, latency, to_multiplier); |
| 5192 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5193 | return 0; |
| 5194 | } |
| 5195 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5196 | static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5197 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 5198 | u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5199 | { |
| 5200 | int err = 0; |
| 5201 | |
| 5202 | switch (cmd->code) { |
| 5203 | case L2CAP_COMMAND_REJ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5204 | l2cap_command_rej(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5205 | break; |
| 5206 | |
| 5207 | case L2CAP_CONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5208 | err = l2cap_connect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5209 | break; |
| 5210 | |
| 5211 | case L2CAP_CONN_RSP: |
Mat Martineau | f5a2598 | 2012-10-11 17:48:21 +0300 | [diff] [blame] | 5212 | case L2CAP_CREATE_CHAN_RSP: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5213 | err = l2cap_connect_create_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5214 | break; |
| 5215 | |
| 5216 | case L2CAP_CONF_REQ: |
| 5217 | err = l2cap_config_req(conn, cmd, cmd_len, data); |
| 5218 | break; |
| 5219 | |
| 5220 | case L2CAP_CONF_RSP: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5221 | err = l2cap_config_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5222 | break; |
| 5223 | |
| 5224 | case L2CAP_DISCONN_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5225 | err = l2cap_disconnect_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5226 | break; |
| 5227 | |
| 5228 | case L2CAP_DISCONN_RSP: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5229 | err = l2cap_disconnect_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5230 | break; |
| 5231 | |
| 5232 | case L2CAP_ECHO_REQ: |
| 5233 | l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); |
| 5234 | break; |
| 5235 | |
| 5236 | case L2CAP_ECHO_RSP: |
| 5237 | break; |
| 5238 | |
| 5239 | case L2CAP_INFO_REQ: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5240 | err = l2cap_information_req(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5241 | break; |
| 5242 | |
| 5243 | case L2CAP_INFO_RSP: |
Johan Hedberg | cb3b315 | 2013-05-28 13:46:30 +0300 | [diff] [blame] | 5244 | err = l2cap_information_rsp(conn, cmd, cmd_len, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5245 | break; |
| 5246 | |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 5247 | case L2CAP_CREATE_CHAN_REQ: |
| 5248 | err = l2cap_create_channel_req(conn, cmd, cmd_len, data); |
| 5249 | break; |
| 5250 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 5251 | case L2CAP_MOVE_CHAN_REQ: |
| 5252 | err = l2cap_move_channel_req(conn, cmd, cmd_len, data); |
| 5253 | break; |
| 5254 | |
| 5255 | case L2CAP_MOVE_CHAN_RSP: |
| 5256 | err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data); |
| 5257 | break; |
| 5258 | |
| 5259 | case L2CAP_MOVE_CHAN_CFM: |
| 5260 | err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); |
| 5261 | break; |
| 5262 | |
| 5263 | case L2CAP_MOVE_CHAN_CFM_RSP: |
| 5264 | err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); |
| 5265 | break; |
| 5266 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5267 | default: |
| 5268 | BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); |
| 5269 | err = -EINVAL; |
| 5270 | break; |
| 5271 | } |
| 5272 | |
| 5273 | return err; |
| 5274 | } |
| 5275 | |
| 5276 | static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5277 | struct l2cap_cmd_hdr *cmd, u8 *data) |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5278 | { |
| 5279 | switch (cmd->code) { |
| 5280 | case L2CAP_COMMAND_REJ: |
| 5281 | return 0; |
| 5282 | |
| 5283 | case L2CAP_CONN_PARAM_UPDATE_REQ: |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 5284 | return l2cap_conn_param_update_req(conn, cmd, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5285 | |
| 5286 | case L2CAP_CONN_PARAM_UPDATE_RSP: |
| 5287 | return 0; |
| 5288 | |
| 5289 | default: |
| 5290 | BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); |
| 5291 | return -EINVAL; |
| 5292 | } |
| 5293 | } |
| 5294 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5295 | static inline void l2cap_le_sig_channel(struct l2cap_conn *conn, |
| 5296 | struct sk_buff *skb) |
| 5297 | { |
| 5298 | u8 *data = skb->data; |
| 5299 | int len = skb->len; |
| 5300 | struct l2cap_cmd_hdr cmd; |
| 5301 | int err; |
| 5302 | |
| 5303 | l2cap_raw_recv(conn, skb); |
| 5304 | |
| 5305 | while (len >= L2CAP_CMD_HDR_SIZE) { |
| 5306 | u16 cmd_len; |
| 5307 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 5308 | data += L2CAP_CMD_HDR_SIZE; |
| 5309 | len -= L2CAP_CMD_HDR_SIZE; |
| 5310 | |
| 5311 | cmd_len = le16_to_cpu(cmd.len); |
| 5312 | |
| 5313 | BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, |
| 5314 | cmd.ident); |
| 5315 | |
| 5316 | if (cmd_len > len || !cmd.ident) { |
| 5317 | BT_DBG("corrupted command"); |
| 5318 | break; |
| 5319 | } |
| 5320 | |
| 5321 | err = l2cap_le_sig_cmd(conn, &cmd, data); |
| 5322 | if (err) { |
| 5323 | struct l2cap_cmd_rej_unk rej; |
| 5324 | |
| 5325 | BT_ERR("Wrong link type (%d)", err); |
| 5326 | |
| 5327 | /* FIXME: Map err to a valid reason */ |
| 5328 | rej.reason = __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
| 5329 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, |
| 5330 | sizeof(rej), &rej); |
| 5331 | } |
| 5332 | |
| 5333 | data += cmd_len; |
| 5334 | len -= cmd_len; |
| 5335 | } |
| 5336 | |
| 5337 | kfree_skb(skb); |
| 5338 | } |
| 5339 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5340 | static inline void l2cap_sig_channel(struct l2cap_conn *conn, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5341 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | { |
| 5343 | u8 *data = skb->data; |
| 5344 | int len = skb->len; |
| 5345 | struct l2cap_cmd_hdr cmd; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 5346 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5347 | |
| 5348 | l2cap_raw_recv(conn, skb); |
| 5349 | |
| 5350 | while (len >= L2CAP_CMD_HDR_SIZE) { |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5351 | u16 cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 5353 | data += L2CAP_CMD_HDR_SIZE; |
| 5354 | len -= L2CAP_CMD_HDR_SIZE; |
| 5355 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5356 | cmd_len = le16_to_cpu(cmd.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5358 | BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, |
| 5359 | cmd.ident); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5360 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5361 | if (cmd_len > len || !cmd.ident) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5362 | BT_DBG("corrupted command"); |
| 5363 | break; |
| 5364 | } |
| 5365 | |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 5366 | err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5367 | if (err) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 5368 | struct l2cap_cmd_rej_unk rej; |
Gustavo F. Padovan | 2c6d1a2 | 2011-03-23 14:38:32 -0300 | [diff] [blame] | 5369 | |
| 5370 | BT_ERR("Wrong link type (%d)", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5371 | |
| 5372 | /* FIXME: Map err to a valid reason */ |
Andrei Emeltchenko | ac73498 | 2012-05-24 15:42:51 +0300 | [diff] [blame] | 5373 | rej.reason = __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5374 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, |
| 5375 | sizeof(rej), &rej); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5376 | } |
| 5377 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 5378 | data += cmd_len; |
| 5379 | len -= cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | } |
| 5381 | |
| 5382 | kfree_skb(skb); |
| 5383 | } |
| 5384 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5385 | 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] | 5386 | { |
| 5387 | u16 our_fcs, rcv_fcs; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 5388 | int hdr_size; |
| 5389 | |
| 5390 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 5391 | hdr_size = L2CAP_EXT_HDR_SIZE; |
| 5392 | else |
| 5393 | hdr_size = L2CAP_ENH_HDR_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5394 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 5395 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5396 | skb_trim(skb, skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5397 | rcv_fcs = get_unaligned_le16(skb->data + skb->len); |
| 5398 | our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); |
| 5399 | |
| 5400 | if (our_fcs != rcv_fcs) |
João Paulo Rechi Vita | 7a560e5 | 2010-06-22 13:56:27 -0300 | [diff] [blame] | 5401 | return -EBADMSG; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 5402 | } |
| 5403 | return 0; |
| 5404 | } |
| 5405 | |
Mat Martineau | 6ea0048 | 2012-05-17 20:53:52 -0700 | [diff] [blame] | 5406 | 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] | 5407 | { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5408 | struct l2cap_ctrl control; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5409 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5410 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5411 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5412 | memset(&control, 0, sizeof(control)); |
| 5413 | control.sframe = 1; |
| 5414 | control.final = 1; |
| 5415 | control.reqseq = chan->buffer_seq; |
| 5416 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5417 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5418 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5419 | control.super = L2CAP_SUPER_RNR; |
| 5420 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5421 | } |
| 5422 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5423 | if (test_and_clear_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
| 5424 | chan->unacked_frames > 0) |
| 5425 | __set_retrans_timer(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5426 | |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5427 | /* Send pending iframes */ |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 5428 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5429 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 5430 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
Mat Martineau | e31f763 | 2012-05-17 20:53:41 -0700 | [diff] [blame] | 5431 | test_bit(CONN_SEND_FBIT, &chan->conn_state)) { |
| 5432 | /* F-bit wasn't sent in an s-frame or i-frame yet, so |
| 5433 | * send it now. |
| 5434 | */ |
| 5435 | control.super = L2CAP_SUPER_RR; |
| 5436 | l2cap_send_sframe(chan, &control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 5437 | } |
| 5438 | } |
| 5439 | |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5440 | static void append_skb_frag(struct sk_buff *skb, struct sk_buff *new_frag, |
| 5441 | struct sk_buff **last_frag) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5442 | { |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5443 | /* skb->len reflects data in skb as well as all fragments |
| 5444 | * skb->data_len reflects only data in fragments |
| 5445 | */ |
| 5446 | if (!skb_has_frag_list(skb)) |
| 5447 | skb_shinfo(skb)->frag_list = new_frag; |
| 5448 | |
| 5449 | new_frag->next = NULL; |
| 5450 | |
| 5451 | (*last_frag)->next = new_frag; |
| 5452 | *last_frag = new_frag; |
| 5453 | |
| 5454 | skb->len += new_frag->len; |
| 5455 | skb->data_len += new_frag->len; |
| 5456 | skb->truesize += new_frag->truesize; |
| 5457 | } |
| 5458 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5459 | static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, |
| 5460 | struct l2cap_ctrl *control) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5461 | { |
| 5462 | int err = -EINVAL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5463 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5464 | switch (control->sar) { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5465 | case L2CAP_SAR_UNSEGMENTED: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5466 | if (chan->sdu) |
| 5467 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5468 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5469 | err = chan->ops->recv(chan, skb); |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5470 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5471 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5472 | case L2CAP_SAR_START: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5473 | if (chan->sdu) |
| 5474 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5475 | |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5476 | chan->sdu_len = get_unaligned_le16(skb->data); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 5477 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5478 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5479 | if (chan->sdu_len > chan->imtu) { |
| 5480 | err = -EMSGSIZE; |
| 5481 | break; |
| 5482 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5483 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5484 | if (skb->len >= chan->sdu_len) |
| 5485 | break; |
| 5486 | |
| 5487 | chan->sdu = skb; |
| 5488 | chan->sdu_last_frag = skb; |
| 5489 | |
| 5490 | skb = NULL; |
| 5491 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5492 | break; |
| 5493 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5494 | case L2CAP_SAR_CONTINUE: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5495 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5496 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5497 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5498 | append_skb_frag(chan->sdu, skb, |
| 5499 | &chan->sdu_last_frag); |
| 5500 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5501 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5502 | if (chan->sdu->len >= chan->sdu_len) |
| 5503 | break; |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5504 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5505 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5506 | break; |
| 5507 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 5508 | case L2CAP_SAR_END: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 5509 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5510 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5511 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5512 | append_skb_frag(chan->sdu, skb, |
| 5513 | &chan->sdu_last_frag); |
| 5514 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5515 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5516 | if (chan->sdu->len != chan->sdu_len) |
| 5517 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5518 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 5519 | err = chan->ops->recv(chan, chan->sdu); |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 5520 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5521 | if (!err) { |
| 5522 | /* Reassembly complete */ |
| 5523 | chan->sdu = NULL; |
| 5524 | chan->sdu_last_frag = NULL; |
| 5525 | chan->sdu_len = 0; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5526 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5527 | break; |
| 5528 | } |
| 5529 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5530 | if (err) { |
| 5531 | kfree_skb(skb); |
| 5532 | kfree_skb(chan->sdu); |
| 5533 | chan->sdu = NULL; |
| 5534 | chan->sdu_last_frag = NULL; |
| 5535 | chan->sdu_len = 0; |
| 5536 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5537 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 5538 | return err; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5539 | } |
| 5540 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 5541 | static int l2cap_resegment(struct l2cap_chan *chan) |
| 5542 | { |
| 5543 | /* Placeholder */ |
| 5544 | return 0; |
| 5545 | } |
| 5546 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 5547 | void l2cap_chan_busy(struct l2cap_chan *chan, int busy) |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 5548 | { |
Mat Martineau | 61aa4f5 | 2012-05-17 20:53:40 -0700 | [diff] [blame] | 5549 | u8 event; |
| 5550 | |
| 5551 | if (chan->mode != L2CAP_MODE_ERTM) |
| 5552 | return; |
| 5553 | |
| 5554 | event = busy ? L2CAP_EV_LOCAL_BUSY_DETECTED : L2CAP_EV_LOCAL_BUSY_CLEAR; |
Andrei Emeltchenko | 401bb1f | 2012-05-21 15:47:46 +0300 | [diff] [blame] | 5555 | l2cap_tx(chan, NULL, NULL, event); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 5556 | } |
| 5557 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5558 | static int l2cap_rx_queued_iframes(struct l2cap_chan *chan) |
| 5559 | { |
Mat Martineau | 6383872 | 2012-05-17 20:53:45 -0700 | [diff] [blame] | 5560 | int err = 0; |
| 5561 | /* Pass sequential frames to l2cap_reassemble_sdu() |
| 5562 | * until a gap is encountered. |
| 5563 | */ |
| 5564 | |
| 5565 | BT_DBG("chan %p", chan); |
| 5566 | |
| 5567 | while (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5568 | struct sk_buff *skb; |
| 5569 | BT_DBG("Searching for skb with txseq %d (queue len %d)", |
| 5570 | chan->buffer_seq, skb_queue_len(&chan->srej_q)); |
| 5571 | |
| 5572 | skb = l2cap_ertm_seq_in_queue(&chan->srej_q, chan->buffer_seq); |
| 5573 | |
| 5574 | if (!skb) |
| 5575 | break; |
| 5576 | |
| 5577 | skb_unlink(skb, &chan->srej_q); |
| 5578 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 5579 | err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->control); |
| 5580 | if (err) |
| 5581 | break; |
| 5582 | } |
| 5583 | |
| 5584 | if (skb_queue_empty(&chan->srej_q)) { |
| 5585 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 5586 | l2cap_send_ack(chan); |
| 5587 | } |
| 5588 | |
| 5589 | return err; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5590 | } |
| 5591 | |
| 5592 | static void l2cap_handle_srej(struct l2cap_chan *chan, |
| 5593 | struct l2cap_ctrl *control) |
| 5594 | { |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5595 | struct sk_buff *skb; |
| 5596 | |
| 5597 | BT_DBG("chan %p, control %p", chan, control); |
| 5598 | |
| 5599 | if (control->reqseq == chan->next_tx_seq) { |
| 5600 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5601 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5602 | return; |
| 5603 | } |
| 5604 | |
| 5605 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 5606 | |
| 5607 | if (skb == NULL) { |
| 5608 | BT_DBG("Seq %d not available for retransmission", |
| 5609 | control->reqseq); |
| 5610 | return; |
| 5611 | } |
| 5612 | |
| 5613 | if (chan->max_tx != 0 && bt_cb(skb)->control.retries >= chan->max_tx) { |
| 5614 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5615 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | f80842a | 2012-05-17 20:53:46 -0700 | [diff] [blame] | 5616 | return; |
| 5617 | } |
| 5618 | |
| 5619 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5620 | |
| 5621 | if (control->poll) { |
| 5622 | l2cap_pass_to_tx(chan, control); |
| 5623 | |
| 5624 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 5625 | l2cap_retransmit(chan, control); |
| 5626 | l2cap_ertm_send(chan); |
| 5627 | |
| 5628 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 5629 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5630 | chan->srej_save_reqseq = control->reqseq; |
| 5631 | } |
| 5632 | } else { |
| 5633 | l2cap_pass_to_tx_fbit(chan, control); |
| 5634 | |
| 5635 | if (control->final) { |
| 5636 | if (chan->srej_save_reqseq != control->reqseq || |
| 5637 | !test_and_clear_bit(CONN_SREJ_ACT, |
| 5638 | &chan->conn_state)) |
| 5639 | l2cap_retransmit(chan, control); |
| 5640 | } else { |
| 5641 | l2cap_retransmit(chan, control); |
| 5642 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) { |
| 5643 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5644 | chan->srej_save_reqseq = control->reqseq; |
| 5645 | } |
| 5646 | } |
| 5647 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5648 | } |
| 5649 | |
| 5650 | static void l2cap_handle_rej(struct l2cap_chan *chan, |
| 5651 | struct l2cap_ctrl *control) |
| 5652 | { |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5653 | struct sk_buff *skb; |
| 5654 | |
| 5655 | BT_DBG("chan %p, control %p", chan, control); |
| 5656 | |
| 5657 | if (control->reqseq == chan->next_tx_seq) { |
| 5658 | BT_DBG("Invalid reqseq %d, disconnecting", control->reqseq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5659 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5660 | return; |
| 5661 | } |
| 5662 | |
| 5663 | skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); |
| 5664 | |
| 5665 | if (chan->max_tx && skb && |
| 5666 | bt_cb(skb)->control.retries >= chan->max_tx) { |
| 5667 | BT_DBG("Retry limit exceeded (%d)", chan->max_tx); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5668 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | fcd289d | 2012-05-17 20:53:47 -0700 | [diff] [blame] | 5669 | return; |
| 5670 | } |
| 5671 | |
| 5672 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5673 | |
| 5674 | l2cap_pass_to_tx(chan, control); |
| 5675 | |
| 5676 | if (control->final) { |
| 5677 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
| 5678 | l2cap_retransmit_all(chan, control); |
| 5679 | } else { |
| 5680 | l2cap_retransmit_all(chan, control); |
| 5681 | l2cap_ertm_send(chan); |
| 5682 | if (chan->tx_state == L2CAP_TX_STATE_WAIT_F) |
| 5683 | set_bit(CONN_REJ_ACT, &chan->conn_state); |
| 5684 | } |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5685 | } |
| 5686 | |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5687 | static u8 l2cap_classify_txseq(struct l2cap_chan *chan, u16 txseq) |
| 5688 | { |
| 5689 | BT_DBG("chan %p, txseq %d", chan, txseq); |
| 5690 | |
| 5691 | BT_DBG("last_acked_seq %d, expected_tx_seq %d", chan->last_acked_seq, |
| 5692 | chan->expected_tx_seq); |
| 5693 | |
| 5694 | if (chan->rx_state == L2CAP_RX_STATE_SREJ_SENT) { |
| 5695 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5696 | chan->tx_win) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5697 | /* See notes below regarding "double poll" and |
| 5698 | * invalid packets. |
| 5699 | */ |
| 5700 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 5701 | BT_DBG("Invalid/Ignore - after SREJ"); |
| 5702 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 5703 | } else { |
| 5704 | BT_DBG("Invalid - in window after SREJ sent"); |
| 5705 | return L2CAP_TXSEQ_INVALID; |
| 5706 | } |
| 5707 | } |
| 5708 | |
| 5709 | if (chan->srej_list.head == txseq) { |
| 5710 | BT_DBG("Expected SREJ"); |
| 5711 | return L2CAP_TXSEQ_EXPECTED_SREJ; |
| 5712 | } |
| 5713 | |
| 5714 | if (l2cap_ertm_seq_in_queue(&chan->srej_q, txseq)) { |
| 5715 | BT_DBG("Duplicate SREJ - txseq already stored"); |
| 5716 | return L2CAP_TXSEQ_DUPLICATE_SREJ; |
| 5717 | } |
| 5718 | |
| 5719 | if (l2cap_seq_list_contains(&chan->srej_list, txseq)) { |
| 5720 | BT_DBG("Unexpected SREJ - not requested"); |
| 5721 | return L2CAP_TXSEQ_UNEXPECTED_SREJ; |
| 5722 | } |
| 5723 | } |
| 5724 | |
| 5725 | if (chan->expected_tx_seq == txseq) { |
| 5726 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= |
| 5727 | chan->tx_win) { |
| 5728 | BT_DBG("Invalid - txseq outside tx window"); |
| 5729 | return L2CAP_TXSEQ_INVALID; |
| 5730 | } else { |
| 5731 | BT_DBG("Expected"); |
| 5732 | return L2CAP_TXSEQ_EXPECTED; |
| 5733 | } |
| 5734 | } |
| 5735 | |
| 5736 | if (__seq_offset(chan, txseq, chan->last_acked_seq) < |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 5737 | __seq_offset(chan, chan->expected_tx_seq, chan->last_acked_seq)) { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 5738 | BT_DBG("Duplicate - expected_tx_seq later than txseq"); |
| 5739 | return L2CAP_TXSEQ_DUPLICATE; |
| 5740 | } |
| 5741 | |
| 5742 | if (__seq_offset(chan, txseq, chan->last_acked_seq) >= chan->tx_win) { |
| 5743 | /* A source of invalid packets is a "double poll" condition, |
| 5744 | * where delays cause us to send multiple poll packets. If |
| 5745 | * the remote stack receives and processes both polls, |
| 5746 | * sequence numbers can wrap around in such a way that a |
| 5747 | * resent frame has a sequence number that looks like new data |
| 5748 | * with a sequence gap. This would trigger an erroneous SREJ |
| 5749 | * request. |
| 5750 | * |
| 5751 | * Fortunately, this is impossible with a tx window that's |
| 5752 | * less than half of the maximum sequence number, which allows |
| 5753 | * invalid frames to be safely ignored. |
| 5754 | * |
| 5755 | * With tx window sizes greater than half of the tx window |
| 5756 | * maximum, the frame is invalid and cannot be ignored. This |
| 5757 | * causes a disconnect. |
| 5758 | */ |
| 5759 | |
| 5760 | if (chan->tx_win <= ((chan->tx_win_max + 1) >> 1)) { |
| 5761 | BT_DBG("Invalid/Ignore - txseq outside tx window"); |
| 5762 | return L2CAP_TXSEQ_INVALID_IGNORE; |
| 5763 | } else { |
| 5764 | BT_DBG("Invalid - txseq outside tx window"); |
| 5765 | return L2CAP_TXSEQ_INVALID; |
| 5766 | } |
| 5767 | } else { |
| 5768 | BT_DBG("Unexpected - txseq indicates missing frames"); |
| 5769 | return L2CAP_TXSEQ_UNEXPECTED; |
| 5770 | } |
| 5771 | } |
| 5772 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5773 | static int l2cap_rx_state_recv(struct l2cap_chan *chan, |
| 5774 | struct l2cap_ctrl *control, |
| 5775 | struct sk_buff *skb, u8 event) |
| 5776 | { |
| 5777 | int err = 0; |
| 5778 | bool skb_in_use = 0; |
| 5779 | |
| 5780 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 5781 | event); |
| 5782 | |
| 5783 | switch (event) { |
| 5784 | case L2CAP_EV_RECV_IFRAME: |
| 5785 | switch (l2cap_classify_txseq(chan, control->txseq)) { |
| 5786 | case L2CAP_TXSEQ_EXPECTED: |
| 5787 | l2cap_pass_to_tx(chan, control); |
| 5788 | |
| 5789 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5790 | BT_DBG("Busy, discarding expected seq %d", |
| 5791 | control->txseq); |
| 5792 | break; |
| 5793 | } |
| 5794 | |
| 5795 | chan->expected_tx_seq = __next_seq(chan, |
| 5796 | control->txseq); |
| 5797 | |
| 5798 | chan->buffer_seq = chan->expected_tx_seq; |
| 5799 | skb_in_use = 1; |
| 5800 | |
| 5801 | err = l2cap_reassemble_sdu(chan, skb, control); |
| 5802 | if (err) |
| 5803 | break; |
| 5804 | |
| 5805 | if (control->final) { |
| 5806 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 5807 | &chan->conn_state)) { |
| 5808 | control->final = 0; |
| 5809 | l2cap_retransmit_all(chan, control); |
| 5810 | l2cap_ertm_send(chan); |
| 5811 | } |
| 5812 | } |
| 5813 | |
| 5814 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
| 5815 | l2cap_send_ack(chan); |
| 5816 | break; |
| 5817 | case L2CAP_TXSEQ_UNEXPECTED: |
| 5818 | l2cap_pass_to_tx(chan, control); |
| 5819 | |
| 5820 | /* Can't issue SREJ frames in the local busy state. |
| 5821 | * Drop this frame, it will be seen as missing |
| 5822 | * when local busy is exited. |
| 5823 | */ |
| 5824 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 5825 | BT_DBG("Busy, discarding unexpected seq %d", |
| 5826 | control->txseq); |
| 5827 | break; |
| 5828 | } |
| 5829 | |
| 5830 | /* There was a gap in the sequence, so an SREJ |
| 5831 | * must be sent for each missing frame. The |
| 5832 | * current frame is stored for later use. |
| 5833 | */ |
| 5834 | skb_queue_tail(&chan->srej_q, skb); |
| 5835 | skb_in_use = 1; |
| 5836 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5837 | skb_queue_len(&chan->srej_q)); |
| 5838 | |
| 5839 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
| 5840 | l2cap_seq_list_clear(&chan->srej_list); |
| 5841 | l2cap_send_srej(chan, control->txseq); |
| 5842 | |
| 5843 | chan->rx_state = L2CAP_RX_STATE_SREJ_SENT; |
| 5844 | break; |
| 5845 | case L2CAP_TXSEQ_DUPLICATE: |
| 5846 | l2cap_pass_to_tx(chan, control); |
| 5847 | break; |
| 5848 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 5849 | break; |
| 5850 | case L2CAP_TXSEQ_INVALID: |
| 5851 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5852 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5853 | break; |
| 5854 | } |
| 5855 | break; |
| 5856 | case L2CAP_EV_RECV_RR: |
| 5857 | l2cap_pass_to_tx(chan, control); |
| 5858 | if (control->final) { |
| 5859 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5860 | |
Mat Martineau | e6a3ee6 | 2012-10-23 15:24:22 -0700 | [diff] [blame] | 5861 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state) && |
| 5862 | !__chan_is_moving(chan)) { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5863 | control->final = 0; |
| 5864 | l2cap_retransmit_all(chan, control); |
| 5865 | } |
| 5866 | |
| 5867 | l2cap_ertm_send(chan); |
| 5868 | } else if (control->poll) { |
| 5869 | l2cap_send_i_or_rr_or_rnr(chan); |
| 5870 | } else { |
| 5871 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 5872 | &chan->conn_state) && |
| 5873 | chan->unacked_frames) |
| 5874 | __set_retrans_timer(chan); |
| 5875 | |
| 5876 | l2cap_ertm_send(chan); |
| 5877 | } |
| 5878 | break; |
| 5879 | case L2CAP_EV_RECV_RNR: |
| 5880 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5881 | l2cap_pass_to_tx(chan, control); |
| 5882 | if (control && control->poll) { |
| 5883 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 5884 | l2cap_send_rr_or_rnr(chan, 0); |
| 5885 | } |
| 5886 | __clear_retrans_timer(chan); |
| 5887 | l2cap_seq_list_clear(&chan->retrans_list); |
| 5888 | break; |
| 5889 | case L2CAP_EV_RECV_REJ: |
| 5890 | l2cap_handle_rej(chan, control); |
| 5891 | break; |
| 5892 | case L2CAP_EV_RECV_SREJ: |
| 5893 | l2cap_handle_srej(chan, control); |
| 5894 | break; |
| 5895 | default: |
| 5896 | break; |
| 5897 | } |
| 5898 | |
| 5899 | if (skb && !skb_in_use) { |
| 5900 | BT_DBG("Freeing %p", skb); |
| 5901 | kfree_skb(skb); |
| 5902 | } |
| 5903 | |
| 5904 | return err; |
| 5905 | } |
| 5906 | |
| 5907 | static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan, |
| 5908 | struct l2cap_ctrl *control, |
| 5909 | struct sk_buff *skb, u8 event) |
| 5910 | { |
| 5911 | int err = 0; |
| 5912 | u16 txseq = control->txseq; |
| 5913 | bool skb_in_use = 0; |
| 5914 | |
| 5915 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 5916 | event); |
| 5917 | |
| 5918 | switch (event) { |
| 5919 | case L2CAP_EV_RECV_IFRAME: |
| 5920 | switch (l2cap_classify_txseq(chan, txseq)) { |
| 5921 | case L2CAP_TXSEQ_EXPECTED: |
| 5922 | /* Keep frame for reassembly later */ |
| 5923 | l2cap_pass_to_tx(chan, control); |
| 5924 | skb_queue_tail(&chan->srej_q, skb); |
| 5925 | skb_in_use = 1; |
| 5926 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5927 | skb_queue_len(&chan->srej_q)); |
| 5928 | |
| 5929 | chan->expected_tx_seq = __next_seq(chan, txseq); |
| 5930 | break; |
| 5931 | case L2CAP_TXSEQ_EXPECTED_SREJ: |
| 5932 | l2cap_seq_list_pop(&chan->srej_list); |
| 5933 | |
| 5934 | l2cap_pass_to_tx(chan, control); |
| 5935 | skb_queue_tail(&chan->srej_q, skb); |
| 5936 | skb_in_use = 1; |
| 5937 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5938 | skb_queue_len(&chan->srej_q)); |
| 5939 | |
| 5940 | err = l2cap_rx_queued_iframes(chan); |
| 5941 | if (err) |
| 5942 | break; |
| 5943 | |
| 5944 | break; |
| 5945 | case L2CAP_TXSEQ_UNEXPECTED: |
| 5946 | /* Got a frame that can't be reassembled yet. |
| 5947 | * Save it for later, and send SREJs to cover |
| 5948 | * the missing frames. |
| 5949 | */ |
| 5950 | skb_queue_tail(&chan->srej_q, skb); |
| 5951 | skb_in_use = 1; |
| 5952 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5953 | skb_queue_len(&chan->srej_q)); |
| 5954 | |
| 5955 | l2cap_pass_to_tx(chan, control); |
| 5956 | l2cap_send_srej(chan, control->txseq); |
| 5957 | break; |
| 5958 | case L2CAP_TXSEQ_UNEXPECTED_SREJ: |
| 5959 | /* This frame was requested with an SREJ, but |
| 5960 | * some expected retransmitted frames are |
| 5961 | * missing. Request retransmission of missing |
| 5962 | * SREJ'd frames. |
| 5963 | */ |
| 5964 | skb_queue_tail(&chan->srej_q, skb); |
| 5965 | skb_in_use = 1; |
| 5966 | BT_DBG("Queued %p (queue len %d)", skb, |
| 5967 | skb_queue_len(&chan->srej_q)); |
| 5968 | |
| 5969 | l2cap_pass_to_tx(chan, control); |
| 5970 | l2cap_send_srej_list(chan, control->txseq); |
| 5971 | break; |
| 5972 | case L2CAP_TXSEQ_DUPLICATE_SREJ: |
| 5973 | /* We've already queued this frame. Drop this copy. */ |
| 5974 | l2cap_pass_to_tx(chan, control); |
| 5975 | break; |
| 5976 | case L2CAP_TXSEQ_DUPLICATE: |
| 5977 | /* Expecting a later sequence number, so this frame |
| 5978 | * was already received. Ignore it completely. |
| 5979 | */ |
| 5980 | break; |
| 5981 | case L2CAP_TXSEQ_INVALID_IGNORE: |
| 5982 | break; |
| 5983 | case L2CAP_TXSEQ_INVALID: |
| 5984 | default: |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 5985 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 5986 | break; |
| 5987 | } |
| 5988 | break; |
| 5989 | case L2CAP_EV_RECV_RR: |
| 5990 | l2cap_pass_to_tx(chan, control); |
| 5991 | if (control->final) { |
| 5992 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 5993 | |
| 5994 | if (!test_and_clear_bit(CONN_REJ_ACT, |
| 5995 | &chan->conn_state)) { |
| 5996 | control->final = 0; |
| 5997 | l2cap_retransmit_all(chan, control); |
| 5998 | } |
| 5999 | |
| 6000 | l2cap_ertm_send(chan); |
| 6001 | } else if (control->poll) { |
| 6002 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6003 | &chan->conn_state) && |
| 6004 | chan->unacked_frames) { |
| 6005 | __set_retrans_timer(chan); |
| 6006 | } |
| 6007 | |
| 6008 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6009 | l2cap_send_srej_tail(chan); |
| 6010 | } else { |
| 6011 | if (test_and_clear_bit(CONN_REMOTE_BUSY, |
| 6012 | &chan->conn_state) && |
| 6013 | chan->unacked_frames) |
| 6014 | __set_retrans_timer(chan); |
| 6015 | |
| 6016 | l2cap_send_ack(chan); |
| 6017 | } |
| 6018 | break; |
| 6019 | case L2CAP_EV_RECV_RNR: |
| 6020 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6021 | l2cap_pass_to_tx(chan, control); |
| 6022 | if (control->poll) { |
| 6023 | l2cap_send_srej_tail(chan); |
| 6024 | } else { |
| 6025 | struct l2cap_ctrl rr_control; |
| 6026 | memset(&rr_control, 0, sizeof(rr_control)); |
| 6027 | rr_control.sframe = 1; |
| 6028 | rr_control.super = L2CAP_SUPER_RR; |
| 6029 | rr_control.reqseq = chan->buffer_seq; |
| 6030 | l2cap_send_sframe(chan, &rr_control); |
| 6031 | } |
| 6032 | |
| 6033 | break; |
| 6034 | case L2CAP_EV_RECV_REJ: |
| 6035 | l2cap_handle_rej(chan, control); |
| 6036 | break; |
| 6037 | case L2CAP_EV_RECV_SREJ: |
| 6038 | l2cap_handle_srej(chan, control); |
| 6039 | break; |
| 6040 | } |
| 6041 | |
| 6042 | if (skb && !skb_in_use) { |
| 6043 | BT_DBG("Freeing %p", skb); |
| 6044 | kfree_skb(skb); |
| 6045 | } |
| 6046 | |
| 6047 | return err; |
| 6048 | } |
| 6049 | |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6050 | static int l2cap_finish_move(struct l2cap_chan *chan) |
| 6051 | { |
| 6052 | BT_DBG("chan %p", chan); |
| 6053 | |
| 6054 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6055 | |
| 6056 | if (chan->hs_hcon) |
| 6057 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6058 | else |
| 6059 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6060 | |
| 6061 | return l2cap_resegment(chan); |
| 6062 | } |
| 6063 | |
| 6064 | static int l2cap_rx_state_wait_p(struct l2cap_chan *chan, |
| 6065 | struct l2cap_ctrl *control, |
| 6066 | struct sk_buff *skb, u8 event) |
| 6067 | { |
| 6068 | int err; |
| 6069 | |
| 6070 | BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, |
| 6071 | event); |
| 6072 | |
| 6073 | if (!control->poll) |
| 6074 | return -EPROTO; |
| 6075 | |
| 6076 | l2cap_process_reqseq(chan, control->reqseq); |
| 6077 | |
| 6078 | if (!skb_queue_empty(&chan->tx_q)) |
| 6079 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6080 | else |
| 6081 | chan->tx_send_head = NULL; |
| 6082 | |
| 6083 | /* Rewind next_tx_seq to the point expected |
| 6084 | * by the receiver. |
| 6085 | */ |
| 6086 | chan->next_tx_seq = control->reqseq; |
| 6087 | chan->unacked_frames = 0; |
| 6088 | |
| 6089 | err = l2cap_finish_move(chan); |
| 6090 | if (err) |
| 6091 | return err; |
| 6092 | |
| 6093 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 6094 | l2cap_send_i_or_rr_or_rnr(chan); |
| 6095 | |
| 6096 | if (event == L2CAP_EV_RECV_IFRAME) |
| 6097 | return -EPROTO; |
| 6098 | |
| 6099 | return l2cap_rx_state_recv(chan, control, NULL, event); |
| 6100 | } |
| 6101 | |
| 6102 | static int l2cap_rx_state_wait_f(struct l2cap_chan *chan, |
| 6103 | struct l2cap_ctrl *control, |
| 6104 | struct sk_buff *skb, u8 event) |
| 6105 | { |
| 6106 | int err; |
| 6107 | |
| 6108 | if (!control->final) |
| 6109 | return -EPROTO; |
| 6110 | |
| 6111 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 6112 | |
| 6113 | chan->rx_state = L2CAP_RX_STATE_RECV; |
| 6114 | l2cap_process_reqseq(chan, control->reqseq); |
| 6115 | |
| 6116 | if (!skb_queue_empty(&chan->tx_q)) |
| 6117 | chan->tx_send_head = skb_peek(&chan->tx_q); |
| 6118 | else |
| 6119 | chan->tx_send_head = NULL; |
| 6120 | |
| 6121 | /* Rewind next_tx_seq to the point expected |
| 6122 | * by the receiver. |
| 6123 | */ |
| 6124 | chan->next_tx_seq = control->reqseq; |
| 6125 | chan->unacked_frames = 0; |
| 6126 | |
| 6127 | if (chan->hs_hcon) |
| 6128 | chan->conn->mtu = chan->hs_hcon->hdev->block_mtu; |
| 6129 | else |
| 6130 | chan->conn->mtu = chan->conn->hcon->hdev->acl_mtu; |
| 6131 | |
| 6132 | err = l2cap_resegment(chan); |
| 6133 | |
| 6134 | if (!err) |
| 6135 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6136 | |
| 6137 | return err; |
| 6138 | } |
| 6139 | |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6140 | static bool __valid_reqseq(struct l2cap_chan *chan, u16 reqseq) |
| 6141 | { |
| 6142 | /* Make sure reqseq is for a packet that has been sent but not acked */ |
| 6143 | u16 unacked; |
| 6144 | |
| 6145 | unacked = __seq_offset(chan, chan->next_tx_seq, chan->expected_ack_seq); |
| 6146 | return __seq_offset(chan, chan->next_tx_seq, reqseq) <= unacked; |
| 6147 | } |
| 6148 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6149 | static int l2cap_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6150 | struct sk_buff *skb, u8 event) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6151 | { |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6152 | int err = 0; |
| 6153 | |
| 6154 | BT_DBG("chan %p, control %p, skb %p, event %d, state %d", chan, |
| 6155 | control, skb, event, chan->rx_state); |
| 6156 | |
| 6157 | if (__valid_reqseq(chan, control->reqseq)) { |
| 6158 | switch (chan->rx_state) { |
| 6159 | case L2CAP_RX_STATE_RECV: |
| 6160 | err = l2cap_rx_state_recv(chan, control, skb, event); |
| 6161 | break; |
| 6162 | case L2CAP_RX_STATE_SREJ_SENT: |
| 6163 | err = l2cap_rx_state_srej_sent(chan, control, skb, |
| 6164 | event); |
| 6165 | break; |
Mat Martineau | 32b3273 | 2012-10-23 15:24:11 -0700 | [diff] [blame] | 6166 | case L2CAP_RX_STATE_WAIT_P: |
| 6167 | err = l2cap_rx_state_wait_p(chan, control, skb, event); |
| 6168 | break; |
| 6169 | case L2CAP_RX_STATE_WAIT_F: |
| 6170 | err = l2cap_rx_state_wait_f(chan, control, skb, event); |
| 6171 | break; |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6172 | default: |
| 6173 | /* shut it down */ |
| 6174 | break; |
| 6175 | } |
| 6176 | } else { |
| 6177 | BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d", |
| 6178 | control->reqseq, chan->next_tx_seq, |
| 6179 | chan->expected_ack_seq); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6180 | l2cap_send_disconn_req(chan, ECONNRESET); |
Mat Martineau | d2a7ac5 | 2012-05-17 20:53:42 -0700 | [diff] [blame] | 6181 | } |
| 6182 | |
| 6183 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6184 | } |
| 6185 | |
| 6186 | static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control, |
| 6187 | struct sk_buff *skb) |
| 6188 | { |
Mat Martineau | 4b51dae9 | 2012-05-17 20:53:37 -0700 | [diff] [blame] | 6189 | int err = 0; |
| 6190 | |
| 6191 | BT_DBG("chan %p, control %p, skb %p, state %d", chan, control, skb, |
| 6192 | chan->rx_state); |
| 6193 | |
| 6194 | if (l2cap_classify_txseq(chan, control->txseq) == |
| 6195 | L2CAP_TXSEQ_EXPECTED) { |
| 6196 | l2cap_pass_to_tx(chan, control); |
| 6197 | |
| 6198 | BT_DBG("buffer_seq %d->%d", chan->buffer_seq, |
| 6199 | __next_seq(chan, chan->buffer_seq)); |
| 6200 | |
| 6201 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 6202 | |
| 6203 | l2cap_reassemble_sdu(chan, skb, control); |
| 6204 | } else { |
| 6205 | if (chan->sdu) { |
| 6206 | kfree_skb(chan->sdu); |
| 6207 | chan->sdu = NULL; |
| 6208 | } |
| 6209 | chan->sdu_last_frag = NULL; |
| 6210 | chan->sdu_len = 0; |
| 6211 | |
| 6212 | if (skb) { |
| 6213 | BT_DBG("Freeing %p", skb); |
| 6214 | kfree_skb(skb); |
| 6215 | } |
| 6216 | } |
| 6217 | |
| 6218 | chan->last_acked_seq = control->txseq; |
| 6219 | chan->expected_tx_seq = __next_seq(chan, control->txseq); |
| 6220 | |
| 6221 | return err; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) |
| 6225 | { |
| 6226 | struct l2cap_ctrl *control = &bt_cb(skb)->control; |
| 6227 | u16 len; |
| 6228 | u8 event; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6229 | |
Mat Martineau | b76bbd6 | 2012-04-11 10:48:43 -0700 | [diff] [blame] | 6230 | __unpack_control(chan, skb); |
| 6231 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6232 | len = skb->len; |
| 6233 | |
| 6234 | /* |
| 6235 | * We can just drop the corrupted I-frame here. |
| 6236 | * Receiver will miss it and start proper recovery |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6237 | * procedures and ask for retransmission. |
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 (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6240 | goto drop; |
| 6241 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6242 | if (!control->sframe && control->sar == L2CAP_SAR_START) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6243 | len -= L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6244 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6245 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 6246 | len -= L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6247 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 6248 | if (len > chan->mps) { |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6249 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6250 | goto drop; |
| 6251 | } |
| 6252 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6253 | if (!control->sframe) { |
| 6254 | int err; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6255 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6256 | BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d", |
| 6257 | control->sar, control->reqseq, control->final, |
| 6258 | control->txseq); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 6259 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6260 | /* Validate F-bit - F=0 always valid, F=1 only |
| 6261 | * valid in TX WAIT_F |
| 6262 | */ |
| 6263 | if (control->final && chan->tx_state != L2CAP_TX_STATE_WAIT_F) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6264 | goto drop; |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6265 | |
| 6266 | if (chan->mode != L2CAP_MODE_STREAMING) { |
| 6267 | event = L2CAP_EV_RECV_IFRAME; |
| 6268 | err = l2cap_rx(chan, control, skb, event); |
| 6269 | } else { |
| 6270 | err = l2cap_stream_rx(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6271 | } |
| 6272 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6273 | if (err) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6274 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6275 | } else { |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6276 | const u8 rx_func_to_event[4] = { |
| 6277 | L2CAP_EV_RECV_RR, L2CAP_EV_RECV_REJ, |
| 6278 | L2CAP_EV_RECV_RNR, L2CAP_EV_RECV_SREJ |
| 6279 | }; |
| 6280 | |
| 6281 | /* Only I-frames are expected in streaming mode */ |
| 6282 | if (chan->mode == L2CAP_MODE_STREAMING) |
| 6283 | goto drop; |
| 6284 | |
| 6285 | BT_DBG("sframe reqseq %d, final %d, poll %d, super %d", |
| 6286 | control->reqseq, control->final, control->poll, |
| 6287 | control->super); |
| 6288 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6289 | if (len != 0) { |
Andrei Emeltchenko | 1bb166e | 2012-11-20 17:16:21 +0200 | [diff] [blame] | 6290 | BT_ERR("Trailing bytes: %d in sframe", len); |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6291 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6292 | goto drop; |
| 6293 | } |
| 6294 | |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6295 | /* Validate F and P bits */ |
| 6296 | if (control->final && (control->poll || |
| 6297 | chan->tx_state != L2CAP_TX_STATE_WAIT_F)) |
| 6298 | goto drop; |
| 6299 | |
| 6300 | event = rx_func_to_event[control->super]; |
| 6301 | if (l2cap_rx(chan, control, skb, event)) |
Andrei Emeltchenko | 5e4e397 | 2012-11-28 17:59:39 +0200 | [diff] [blame] | 6302 | l2cap_send_disconn_req(chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 6303 | } |
| 6304 | |
| 6305 | return 0; |
| 6306 | |
| 6307 | drop: |
| 6308 | kfree_skb(skb); |
| 6309 | return 0; |
| 6310 | } |
| 6311 | |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6312 | static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid, |
| 6313 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6314 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6315 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6316 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 6317 | chan = l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6318 | if (!chan) { |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6319 | if (cid == L2CAP_CID_A2MP) { |
| 6320 | chan = a2mp_channel_create(conn, skb); |
| 6321 | if (!chan) { |
| 6322 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6323 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6324 | } |
| 6325 | |
| 6326 | l2cap_chan_lock(chan); |
| 6327 | } else { |
| 6328 | BT_DBG("unknown cid 0x%4.4x", cid); |
| 6329 | /* Drop packet and return */ |
| 6330 | kfree_skb(skb); |
Andrei Emeltchenko | 13ca56e | 2012-05-31 11:18:55 +0300 | [diff] [blame] | 6331 | return; |
Andrei Emeltchenko | 97e8e89 | 2012-05-29 13:59:17 +0300 | [diff] [blame] | 6332 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6333 | } |
| 6334 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 6335 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6336 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6337 | if (chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6338 | goto drop; |
| 6339 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6340 | switch (chan->mode) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6341 | case L2CAP_MODE_BASIC: |
| 6342 | /* If socket recv buffers overflows we drop data here |
| 6343 | * which is *bad* because L2CAP has to be reliable. |
| 6344 | * But we don't have any other choice. L2CAP doesn't |
| 6345 | * provide flow control mechanism. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6346 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6347 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6348 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6349 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6350 | if (!chan->ops->recv(chan, skb)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6351 | goto done; |
| 6352 | break; |
| 6353 | |
| 6354 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6355 | case L2CAP_MODE_STREAMING: |
Mat Martineau | cec8ab6e | 2012-05-17 20:53:36 -0700 | [diff] [blame] | 6356 | l2cap_data_rcv(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 6357 | goto done; |
| 6358 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6359 | default: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 6360 | 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] | 6361 | break; |
| 6362 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6363 | |
| 6364 | drop: |
| 6365 | kfree_skb(skb); |
| 6366 | |
| 6367 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6368 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6369 | } |
| 6370 | |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6371 | static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, |
| 6372 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6373 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6374 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | |
Ido Yariv | c228768 | 2012-04-20 15:46:07 -0300 | [diff] [blame] | 6376 | 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] | 6377 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6378 | goto drop; |
| 6379 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 6380 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6381 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6382 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | goto drop; |
| 6384 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 6385 | if (chan->imtu < skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6386 | goto drop; |
| 6387 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6388 | if (!chan->ops->recv(chan, skb)) |
Andrei Emeltchenko | 84104b2 | 2012-05-31 11:18:56 +0300 | [diff] [blame] | 6389 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6390 | |
| 6391 | drop: |
| 6392 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6393 | } |
| 6394 | |
Marcel Holtmann | 72f7835 | 2013-04-23 00:59:00 -0700 | [diff] [blame] | 6395 | static void l2cap_att_channel(struct l2cap_conn *conn, |
Andrei Emeltchenko | 6810fca | 2012-05-31 11:18:57 +0300 | [diff] [blame] | 6396 | struct sk_buff *skb) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6397 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6398 | struct l2cap_chan *chan; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6399 | |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame^] | 6400 | chan = l2cap_global_chan_by_scid(0, L2CAP_CID_ATT, |
Marcel Holtmann | 72f7835 | 2013-04-23 00:59:00 -0700 | [diff] [blame] | 6401 | conn->src, conn->dst); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6402 | if (!chan) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6403 | goto drop; |
| 6404 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 6405 | BT_DBG("chan %p, len %d", chan, skb->len); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6406 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6407 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6408 | goto drop; |
| 6409 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 6410 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6411 | goto drop; |
| 6412 | |
Gustavo Padovan | 80b9802 | 2012-05-27 22:27:51 -0300 | [diff] [blame] | 6413 | if (!chan->ops->recv(chan, skb)) |
Andrei Emeltchenko | 6810fca | 2012-05-31 11:18:57 +0300 | [diff] [blame] | 6414 | return; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6415 | |
| 6416 | drop: |
| 6417 | kfree_skb(skb); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6418 | } |
| 6419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6420 | static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) |
| 6421 | { |
| 6422 | struct l2cap_hdr *lh = (void *) skb->data; |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 6423 | u16 cid, len; |
| 6424 | __le16 psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6425 | |
| 6426 | skb_pull(skb, L2CAP_HDR_SIZE); |
| 6427 | cid = __le16_to_cpu(lh->cid); |
| 6428 | len = __le16_to_cpu(lh->len); |
| 6429 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 6430 | if (len != skb->len) { |
| 6431 | kfree_skb(skb); |
| 6432 | return; |
| 6433 | } |
| 6434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6435 | BT_DBG("len %d, cid 0x%4.4x", len, cid); |
| 6436 | |
| 6437 | switch (cid) { |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 6438 | case L2CAP_CID_LE_SIGNALING: |
Johan Hedberg | c562355 | 2013-04-29 19:35:33 +0300 | [diff] [blame] | 6439 | l2cap_le_sig_channel(conn, skb); |
| 6440 | break; |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6441 | case L2CAP_CID_SIGNALING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6442 | l2cap_sig_channel(conn, skb); |
| 6443 | break; |
| 6444 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 6445 | case L2CAP_CID_CONN_LESS: |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 6446 | psm = get_unaligned((__le16 *) skb->data); |
Andrei Emeltchenko | 0181a70 | 2012-05-29 10:04:05 +0300 | [diff] [blame] | 6447 | skb_pull(skb, L2CAP_PSMLEN_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6448 | l2cap_conless_channel(conn, psm, skb); |
| 6449 | break; |
| 6450 | |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame^] | 6451 | case L2CAP_CID_ATT: |
Marcel Holtmann | 72f7835 | 2013-04-23 00:59:00 -0700 | [diff] [blame] | 6452 | l2cap_att_channel(conn, skb); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 6453 | break; |
| 6454 | |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 6455 | case L2CAP_CID_SMP: |
| 6456 | if (smp_sig_channel(conn, skb)) |
| 6457 | l2cap_conn_del(conn->hcon, EACCES); |
| 6458 | break; |
| 6459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6460 | default: |
| 6461 | l2cap_data_channel(conn, cid, skb); |
| 6462 | break; |
| 6463 | } |
| 6464 | } |
| 6465 | |
| 6466 | /* ---- L2CAP interface with lower layer (HCI) ---- */ |
| 6467 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6468 | int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6469 | { |
| 6470 | int exact = 0, lm1 = 0, lm2 = 0; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6471 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 6473 | BT_DBG("hdev %s, bdaddr %pMR", hdev->name, bdaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6474 | |
| 6475 | /* Find listening sockets and check their link_mode */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6476 | read_lock(&chan_list_lock); |
| 6477 | list_for_each_entry(c, &chan_list, global_l) { |
| 6478 | struct sock *sk = c->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6479 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6480 | if (c->state != BT_LISTEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6481 | continue; |
| 6482 | |
| 6483 | if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6484 | lm1 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 6485 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6486 | lm1 |= HCI_LM_MASTER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6487 | exact++; |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6488 | } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { |
| 6489 | lm2 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 6490 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 6491 | lm2 |= HCI_LM_MASTER; |
| 6492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6493 | } |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6494 | read_unlock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | |
| 6496 | return exact ? lm1 : lm2; |
| 6497 | } |
| 6498 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 6499 | void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 6501 | struct l2cap_conn *conn; |
| 6502 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 6503 | BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6505 | if (!status) { |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 6506 | conn = l2cap_conn_add(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6507 | if (conn) |
| 6508 | l2cap_conn_ready(conn); |
Andrei Emeltchenko | ba6fc31 | 2012-10-31 15:46:26 +0200 | [diff] [blame] | 6509 | } else { |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 6510 | l2cap_conn_del(hcon, bt_to_errno(status)); |
Andrei Emeltchenko | ba6fc31 | 2012-10-31 15:46:26 +0200 | [diff] [blame] | 6511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6512 | } |
| 6513 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6514 | int l2cap_disconn_ind(struct hci_conn *hcon) |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 6515 | { |
| 6516 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 6517 | |
| 6518 | BT_DBG("hcon %p", hcon); |
| 6519 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6520 | if (!conn) |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 6521 | return HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 6522 | return conn->disc_reason; |
| 6523 | } |
| 6524 | |
Andrei Emeltchenko | 9e66463 | 2012-07-24 16:06:15 +0300 | [diff] [blame] | 6525 | void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6526 | { |
| 6527 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 6528 | |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 6529 | l2cap_conn_del(hcon, bt_to_errno(reason)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6530 | } |
| 6531 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6532 | static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6533 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 6534 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
Marcel Holtmann | 255c760 | 2009-02-04 21:07:19 +0100 | [diff] [blame] | 6535 | return; |
| 6536 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6537 | if (encrypt == 0x00) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6538 | if (chan->sec_level == BT_SECURITY_MEDIUM) { |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 6539 | __set_chan_timer(chan, L2CAP_ENC_TIMEOUT); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6540 | } else if (chan->sec_level == BT_SECURITY_HIGH) |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 6541 | l2cap_chan_close(chan, ECONNREFUSED); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6542 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6543 | if (chan->sec_level == BT_SECURITY_MEDIUM) |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 6544 | __clear_chan_timer(chan); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 6545 | } |
| 6546 | } |
| 6547 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6548 | int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6549 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 6550 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 6551 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6552 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 6553 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6554 | return 0; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 6555 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 6556 | 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] | 6557 | |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 6558 | if (hcon->type == LE_LINK) { |
Hemant Gupta | 35d4adcc | 2012-04-18 14:46:26 +0530 | [diff] [blame] | 6559 | if (!status && encrypt) |
| 6560 | smp_distribute_keys(conn, 0); |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 6561 | cancel_delayed_work(&conn->security_timer); |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 6562 | } |
| 6563 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 6564 | mutex_lock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6565 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 6566 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6567 | l2cap_chan_lock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6568 | |
Andrei Emeltchenko | 89d8b40 | 2012-07-10 15:27:51 +0300 | [diff] [blame] | 6569 | BT_DBG("chan %p scid 0x%4.4x state %s", chan, chan->scid, |
| 6570 | state_to_string(chan->state)); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6571 | |
Andrei Emeltchenko | 78eb2f9 | 2012-07-19 17:03:47 +0300 | [diff] [blame] | 6572 | if (chan->chan_type == L2CAP_CHAN_CONN_FIX_A2MP) { |
| 6573 | l2cap_chan_unlock(chan); |
| 6574 | continue; |
| 6575 | } |
| 6576 | |
Johan Hedberg | 073d1cf | 2013-04-29 19:35:35 +0300 | [diff] [blame^] | 6577 | if (chan->scid == L2CAP_CID_ATT) { |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6578 | if (!status && encrypt) { |
| 6579 | chan->sec_level = hcon->sec_level; |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 6580 | l2cap_chan_ready(chan); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6581 | } |
| 6582 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6583 | l2cap_chan_unlock(chan); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 6584 | continue; |
| 6585 | } |
| 6586 | |
Andrei Emeltchenko | 96eff46 | 2012-11-15 18:14:53 +0200 | [diff] [blame] | 6587 | if (!__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6588 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 6589 | continue; |
| 6590 | } |
| 6591 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6592 | if (!status && (chan->state == BT_CONNECTED || |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6593 | chan->state == BT_CONFIG)) { |
Gustavo Padovan | a7d7723 | 2012-05-13 03:20:07 -0300 | [diff] [blame] | 6594 | struct sock *sk = chan->sk; |
| 6595 | |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 6596 | clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags); |
Gustavo Padovan | a7d7723 | 2012-05-13 03:20:07 -0300 | [diff] [blame] | 6597 | sk->sk_state_change(sk); |
| 6598 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 6599 | l2cap_check_encryption(chan, encrypt); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6600 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 9719f8a | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 6601 | continue; |
| 6602 | } |
| 6603 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6604 | if (chan->state == BT_CONNECT) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6605 | if (!status) { |
Andrei Emeltchenko | 93c3e8f | 2012-09-27 17:26:16 +0300 | [diff] [blame] | 6606 | l2cap_start_connection(chan); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6607 | } else { |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 6608 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6609 | } |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 6610 | } else if (chan->state == BT_CONNECT2) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6611 | struct sock *sk = chan->sk; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6612 | struct l2cap_conn_rsp rsp; |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6613 | __u16 res, stat; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6614 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6615 | lock_sock(sk); |
| 6616 | |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6617 | if (!status) { |
Gustavo Padovan | c5daa68 | 2012-05-16 12:17:10 -0300 | [diff] [blame] | 6618 | if (test_bit(BT_SK_DEFER_SETUP, |
| 6619 | &bt_sk(sk)->flags)) { |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6620 | res = L2CAP_CR_PEND; |
| 6621 | stat = L2CAP_CS_AUTHOR_PEND; |
Gustavo Padovan | 2dc4e51 | 2012-10-12 19:35:24 +0800 | [diff] [blame] | 6622 | chan->ops->defer(chan); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6623 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 6624 | __l2cap_state_change(chan, BT_CONFIG); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6625 | res = L2CAP_CR_SUCCESS; |
| 6626 | stat = L2CAP_CS_NO_INFO; |
| 6627 | } |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6628 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 6629 | __l2cap_state_change(chan, BT_DISCONN); |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 6630 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6631 | res = L2CAP_CR_SEC_BLOCK; |
| 6632 | stat = L2CAP_CS_NO_INFO; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6633 | } |
| 6634 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6635 | release_sock(sk); |
| 6636 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 6637 | rsp.scid = cpu_to_le16(chan->dcid); |
| 6638 | rsp.dcid = cpu_to_le16(chan->scid); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 6639 | rsp.result = cpu_to_le16(res); |
| 6640 | rsp.status = cpu_to_le16(stat); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 6641 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6642 | sizeof(rsp), &rsp); |
Mat Martineau | 2d36935 | 2012-05-23 14:59:30 -0700 | [diff] [blame] | 6643 | |
| 6644 | if (!test_bit(CONF_REQ_SENT, &chan->conf_state) && |
| 6645 | res == L2CAP_CR_SUCCESS) { |
| 6646 | char buf[128]; |
| 6647 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
| 6648 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 6649 | L2CAP_CONF_REQ, |
| 6650 | l2cap_build_conf_req(chan, buf), |
| 6651 | buf); |
| 6652 | chan->num_conf_req++; |
| 6653 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6654 | } |
| 6655 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 6656 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6657 | } |
| 6658 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 6659 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 6660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6661 | return 0; |
| 6662 | } |
| 6663 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 6664 | 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] | 6665 | { |
| 6666 | struct l2cap_conn *conn = hcon->l2cap_data; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6667 | struct l2cap_hdr *hdr; |
| 6668 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6669 | |
Andrei Emeltchenko | 1d13a25 | 2012-10-15 11:58:41 +0300 | [diff] [blame] | 6670 | /* For AMP controller do not create l2cap conn */ |
| 6671 | if (!conn && hcon->hdev->dev_type != HCI_BREDR) |
| 6672 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6673 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 6674 | if (!conn) |
Claudio Takahasi | baf4325 | 2013-04-11 13:55:50 -0300 | [diff] [blame] | 6675 | conn = l2cap_conn_add(hcon); |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 6676 | |
| 6677 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6678 | goto drop; |
| 6679 | |
| 6680 | BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); |
| 6681 | |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6682 | switch (flags) { |
| 6683 | case ACL_START: |
| 6684 | case ACL_START_NO_FLUSH: |
| 6685 | case ACL_COMPLETE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6686 | if (conn->rx_len) { |
| 6687 | BT_ERR("Unexpected start frame (len %d)", skb->len); |
| 6688 | kfree_skb(conn->rx_skb); |
| 6689 | conn->rx_skb = NULL; |
| 6690 | conn->rx_len = 0; |
| 6691 | l2cap_conn_unreliable(conn, ECOMM); |
| 6692 | } |
| 6693 | |
Andrei Emeltchenko | aae7fe2 | 2010-09-15 14:28:43 +0300 | [diff] [blame] | 6694 | /* Start fragment always begin with Basic L2CAP header */ |
| 6695 | if (skb->len < L2CAP_HDR_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6696 | BT_ERR("Frame is too short (len %d)", skb->len); |
| 6697 | l2cap_conn_unreliable(conn, ECOMM); |
| 6698 | goto drop; |
| 6699 | } |
| 6700 | |
| 6701 | hdr = (struct l2cap_hdr *) skb->data; |
| 6702 | len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; |
| 6703 | |
| 6704 | if (len == skb->len) { |
| 6705 | /* Complete frame received */ |
| 6706 | l2cap_recv_frame(conn, skb); |
| 6707 | return 0; |
| 6708 | } |
| 6709 | |
| 6710 | BT_DBG("Start: total len %d, frag len %d", len, skb->len); |
| 6711 | |
| 6712 | if (skb->len > len) { |
| 6713 | BT_ERR("Frame is too long (len %d, expected len %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6714 | skb->len, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6715 | l2cap_conn_unreliable(conn, ECOMM); |
| 6716 | goto drop; |
| 6717 | } |
| 6718 | |
| 6719 | /* Allocate skb for the complete frame (with header) */ |
Gustavo Padovan | 8bcde1f | 2012-05-28 19:18:14 -0300 | [diff] [blame] | 6720 | conn->rx_skb = bt_skb_alloc(len, GFP_KERNEL); |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 6721 | if (!conn->rx_skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6722 | goto drop; |
| 6723 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 6724 | 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] | 6725 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6726 | conn->rx_len = len - skb->len; |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6727 | break; |
| 6728 | |
| 6729 | case ACL_CONT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6730 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
| 6731 | |
| 6732 | if (!conn->rx_len) { |
| 6733 | BT_ERR("Unexpected continuation frame (len %d)", skb->len); |
| 6734 | l2cap_conn_unreliable(conn, ECOMM); |
| 6735 | goto drop; |
| 6736 | } |
| 6737 | |
| 6738 | if (skb->len > conn->rx_len) { |
| 6739 | BT_ERR("Fragment is too long (len %d, expected %d)", |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6740 | skb->len, conn->rx_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6741 | kfree_skb(conn->rx_skb); |
| 6742 | conn->rx_skb = NULL; |
| 6743 | conn->rx_len = 0; |
| 6744 | l2cap_conn_unreliable(conn, ECOMM); |
| 6745 | goto drop; |
| 6746 | } |
| 6747 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 6748 | 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] | 6749 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6750 | conn->rx_len -= skb->len; |
| 6751 | |
| 6752 | if (!conn->rx_len) { |
| 6753 | /* Complete frame received */ |
| 6754 | l2cap_recv_frame(conn, conn->rx_skb); |
| 6755 | conn->rx_skb = NULL; |
| 6756 | } |
Andrei Emeltchenko | d73a098 | 2012-10-15 11:58:40 +0300 | [diff] [blame] | 6757 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6758 | } |
| 6759 | |
| 6760 | drop: |
| 6761 | kfree_skb(skb); |
| 6762 | return 0; |
| 6763 | } |
| 6764 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6765 | static int l2cap_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6766 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6767 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6768 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 6769 | read_lock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6770 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 6771 | list_for_each_entry(c, &chan_list, global_l) { |
| 6772 | struct sock *sk = c->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6773 | |
Andrei Emeltchenko | fcb7333 | 2012-09-25 12:49:44 +0300 | [diff] [blame] | 6774 | seq_printf(f, "%pMR %pMR %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", |
| 6775 | &bt_sk(sk)->src, &bt_sk(sk)->dst, |
| 6776 | c->state, __le16_to_cpu(c->psm), |
| 6777 | c->scid, c->dcid, c->imtu, c->omtu, |
| 6778 | c->sec_level, c->mode); |
Andrei Emeltchenko | 61e1b4b | 2012-01-19 11:19:50 +0200 | [diff] [blame] | 6779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6780 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 6781 | read_unlock(&chan_list_lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 6782 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6783 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6784 | } |
| 6785 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6786 | static int l2cap_debugfs_open(struct inode *inode, struct file *file) |
| 6787 | { |
| 6788 | return single_open(file, l2cap_debugfs_show, inode->i_private); |
| 6789 | } |
| 6790 | |
| 6791 | static const struct file_operations l2cap_debugfs_fops = { |
| 6792 | .open = l2cap_debugfs_open, |
| 6793 | .read = seq_read, |
| 6794 | .llseek = seq_lseek, |
| 6795 | .release = single_release, |
| 6796 | }; |
| 6797 | |
| 6798 | static struct dentry *l2cap_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6799 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 6800 | int __init l2cap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6801 | { |
| 6802 | int err; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 6803 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 6804 | err = l2cap_init_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6805 | if (err < 0) |
| 6806 | return err; |
| 6807 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6808 | if (bt_debugfs) { |
Gustavo Padovan | 2d79281 | 2012-10-06 10:07:01 +0100 | [diff] [blame] | 6809 | l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs, |
| 6810 | NULL, &l2cap_debugfs_fops); |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6811 | if (!l2cap_debugfs) |
| 6812 | BT_ERR("Failed to create L2CAP debug file"); |
| 6813 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6815 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6816 | } |
| 6817 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 6818 | void l2cap_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6819 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 6820 | debugfs_remove(l2cap_debugfs); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 6821 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | } |
| 6823 | |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 6824 | module_param(disable_ertm, bool, 0644); |
| 6825 | MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); |