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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
| 8 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License version 2 as |
| 12 | published by the Free Software Foundation; |
| 13 | |
| 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 17 | 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] | 18 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 23 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 24 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | SOFTWARE IS DISCLAIMED. |
| 26 | */ |
| 27 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 28 | /* Bluetooth L2CAP core. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/module.h> |
| 31 | |
| 32 | #include <linux/types.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 33 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/errno.h> |
| 35 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/sched.h> |
| 37 | #include <linux/slab.h> |
| 38 | #include <linux/poll.h> |
| 39 | #include <linux/fcntl.h> |
| 40 | #include <linux/init.h> |
| 41 | #include <linux/interrupt.h> |
| 42 | #include <linux/socket.h> |
| 43 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/list.h> |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 45 | #include <linux/device.h> |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 46 | #include <linux/debugfs.h> |
| 47 | #include <linux/seq_file.h> |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 48 | #include <linux/uaccess.h> |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 49 | #include <linux/crc16.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <net/sock.h> |
| 51 | |
| 52 | #include <asm/system.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include <asm/unaligned.h> |
| 54 | |
| 55 | #include <net/bluetooth/bluetooth.h> |
| 56 | #include <net/bluetooth/hci_core.h> |
| 57 | #include <net/bluetooth/l2cap.h> |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 58 | #include <net/bluetooth/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Rusty Russell | eb93992 | 2011-12-19 14:08:01 +0000 | [diff] [blame] | 60 | bool disable_ertm; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 61 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 62 | static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN; |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 63 | static u8 l2cap_fixed_chan[8] = { L2CAP_FC_L2CAP, }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 65 | static LIST_HEAD(chan_list); |
| 66 | static DEFINE_RWLOCK(chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, |
| 69 | u8 code, u8 ident, u16 dlen, void *data); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 70 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, |
| 71 | void *data); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 72 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 73 | static void l2cap_send_disconn_req(struct l2cap_conn *conn, |
| 74 | struct l2cap_chan *chan, int err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 76 | /* ---- L2CAP channels ---- */ |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 77 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 78 | static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 79 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 80 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 81 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 82 | list_for_each_entry(c, &conn->chan_l, list) { |
| 83 | if (c->dcid == cid) |
| 84 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 85 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 86 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 89 | static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 90 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 91 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 92 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 93 | list_for_each_entry(c, &conn->chan_l, list) { |
| 94 | if (c->scid == cid) |
| 95 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 96 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 97 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* Find channel with given SCID. |
| 101 | * Returns locked socket */ |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 102 | static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 103 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 104 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 105 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 106 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 107 | c = __l2cap_get_chan_by_scid(conn, cid); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 108 | mutex_unlock(&conn->chan_lock); |
| 109 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 110 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 113 | static struct l2cap_chan *__l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 114 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 115 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 116 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 117 | list_for_each_entry(c, &conn->chan_l, list) { |
| 118 | if (c->ident == ident) |
| 119 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 120 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 121 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 124 | static inline struct l2cap_chan *l2cap_get_chan_by_ident(struct l2cap_conn *conn, u8 ident) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 125 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 126 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 127 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 128 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 129 | c = __l2cap_get_chan_by_ident(conn, ident); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 130 | mutex_unlock(&conn->chan_lock); |
| 131 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 132 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 135 | 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] | 136 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 137 | struct l2cap_chan *c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 138 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 139 | list_for_each_entry(c, &chan_list, global_l) { |
| 140 | if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src)) |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 141 | return c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 142 | } |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 143 | return NULL; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) |
| 147 | { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 148 | int err; |
| 149 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 150 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 151 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 152 | if (psm && __l2cap_global_chan_by_addr(psm, src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 153 | err = -EADDRINUSE; |
| 154 | goto done; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 155 | } |
| 156 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 157 | if (psm) { |
| 158 | chan->psm = psm; |
| 159 | chan->sport = psm; |
| 160 | err = 0; |
| 161 | } else { |
| 162 | u16 p; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 163 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 164 | err = -EINVAL; |
| 165 | for (p = 0x1001; p < 0x1100; p += 2) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 166 | if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 167 | chan->psm = cpu_to_le16(p); |
| 168 | chan->sport = cpu_to_le16(p); |
| 169 | err = 0; |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | done: |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 175 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 176 | return err; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) |
| 180 | { |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 181 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 182 | |
| 183 | chan->scid = scid; |
| 184 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 185 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 190 | static u16 l2cap_alloc_cid(struct l2cap_conn *conn) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 191 | { |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 192 | u16 cid = L2CAP_CID_DYN_START; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 193 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 194 | for (; cid < L2CAP_CID_DYN_END; cid++) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 195 | if (!__l2cap_get_chan_by_scid(conn, cid)) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 196 | return cid; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 202 | static void __l2cap_state_change(struct l2cap_chan *chan, int state) |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 203 | { |
Andrei Emeltchenko | 42d2d87 | 2012-02-17 11:40:57 +0200 | [diff] [blame] | 204 | BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state), |
Gustavo F. Padovan | badaaa0 | 2011-11-23 20:11:46 -0200 | [diff] [blame] | 205 | state_to_string(state)); |
| 206 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 207 | chan->state = state; |
| 208 | chan->ops->state_change(chan->data, state); |
| 209 | } |
| 210 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 211 | static void l2cap_state_change(struct l2cap_chan *chan, int state) |
| 212 | { |
| 213 | struct sock *sk = chan->sk; |
| 214 | |
| 215 | lock_sock(sk); |
| 216 | __l2cap_state_change(chan, state); |
| 217 | release_sock(sk); |
| 218 | } |
| 219 | |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 220 | static inline void __l2cap_chan_set_err(struct l2cap_chan *chan, int err) |
| 221 | { |
| 222 | struct sock *sk = chan->sk; |
| 223 | |
| 224 | sk->sk_err = err; |
| 225 | } |
| 226 | |
| 227 | static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err) |
| 228 | { |
| 229 | struct sock *sk = chan->sk; |
| 230 | |
| 231 | lock_sock(sk); |
| 232 | __l2cap_chan_set_err(chan, err); |
| 233 | release_sock(sk); |
| 234 | } |
| 235 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 236 | static void l2cap_chan_timeout(struct work_struct *work) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 237 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 238 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
| 239 | chan_timer.work); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 240 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 241 | int reason; |
| 242 | |
Andrei Emeltchenko | e05dcc3 | 2012-02-17 11:40:56 +0200 | [diff] [blame] | 243 | 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] | 244 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 245 | mutex_lock(&conn->chan_lock); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 246 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 247 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 248 | if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 249 | reason = ECONNREFUSED; |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 250 | else if (chan->state == BT_CONNECT && |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 251 | chan->sec_level != BT_SECURITY_SDP) |
| 252 | reason = ECONNREFUSED; |
| 253 | else |
| 254 | reason = ETIMEDOUT; |
| 255 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 256 | l2cap_chan_close(chan, reason); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 257 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 258 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 259 | |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 260 | chan->ops->close(chan->data); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 261 | mutex_unlock(&conn->chan_lock); |
| 262 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 263 | l2cap_chan_put(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 264 | } |
| 265 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 266 | struct l2cap_chan *l2cap_chan_create(struct sock *sk) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 267 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 268 | struct l2cap_chan *chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 269 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 270 | chan = kzalloc(sizeof(*chan), GFP_ATOMIC); |
| 271 | if (!chan) |
| 272 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 273 | |
Andrei Emeltchenko | c03b355 | 2012-02-21 12:54:56 +0200 | [diff] [blame] | 274 | mutex_init(&chan->lock); |
| 275 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 276 | chan->sk = sk; |
| 277 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 278 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 279 | list_add(&chan->global_l, &chan_list); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 280 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 281 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 282 | INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 283 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 284 | chan->state = BT_OPEN; |
| 285 | |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 286 | atomic_set(&chan->refcnt, 1); |
| 287 | |
Szymon Janc | abc545b | 2011-11-03 16:05:44 +0100 | [diff] [blame] | 288 | BT_DBG("sk %p chan %p", sk, chan); |
| 289 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 290 | return chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 293 | void l2cap_chan_destroy(struct l2cap_chan *chan) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 294 | { |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 295 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 296 | list_del(&chan->global_l); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 297 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 298 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 299 | l2cap_chan_put(chan); |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 300 | } |
| 301 | |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 302 | void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 303 | { |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 304 | BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 305 | chan->psm, chan->dcid); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 306 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 307 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 308 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 309 | chan->conn = conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 310 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 311 | switch (chan->chan_type) { |
| 312 | case L2CAP_CHAN_CONN_ORIENTED: |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 313 | if (conn->hcon->type == LE_LINK) { |
| 314 | /* LE connection */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 315 | chan->omtu = L2CAP_LE_DEFAULT_MTU; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 316 | chan->scid = L2CAP_CID_LE_DATA; |
| 317 | chan->dcid = L2CAP_CID_LE_DATA; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 318 | } else { |
| 319 | /* Alloc CID for connection-oriented socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 320 | chan->scid = l2cap_alloc_cid(conn); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 321 | chan->omtu = L2CAP_DEFAULT_MTU; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 322 | } |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 323 | break; |
| 324 | |
| 325 | case L2CAP_CHAN_CONN_LESS: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 326 | /* Connectionless socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 327 | chan->scid = L2CAP_CID_CONN_LESS; |
| 328 | chan->dcid = L2CAP_CID_CONN_LESS; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 329 | chan->omtu = L2CAP_DEFAULT_MTU; |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 330 | break; |
| 331 | |
| 332 | default: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 333 | /* Raw socket can send/recv signalling messages only */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 334 | chan->scid = L2CAP_CID_SIGNALING; |
| 335 | chan->dcid = L2CAP_CID_SIGNALING; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 336 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 337 | } |
| 338 | |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 339 | chan->local_id = L2CAP_BESTEFFORT_ID; |
| 340 | chan->local_stype = L2CAP_SERV_BESTEFFORT; |
| 341 | chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; |
| 342 | chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; |
| 343 | chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; |
| 344 | chan->local_flush_to = L2CAP_DEFAULT_FLUSH_TO; |
| 345 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 346 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 347 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 348 | list_add(&chan->list, &conn->chan_l); |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
| 352 | { |
| 353 | mutex_lock(&conn->chan_lock); |
| 354 | __l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 355 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 358 | static void l2cap_chan_del(struct l2cap_chan *chan, int err) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 359 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 360 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 361 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 362 | struct sock *parent = bt_sk(sk)->parent; |
| 363 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 364 | __clear_chan_timer(chan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 365 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 366 | BT_DBG("chan %p, conn %p, err %d", chan, conn, err); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 367 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 368 | if (conn) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 369 | /* Delete from channel list */ |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 370 | list_del(&chan->list); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 371 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 372 | l2cap_chan_put(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 373 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 374 | chan->conn = NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 375 | hci_conn_put(conn->hcon); |
| 376 | } |
| 377 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 378 | lock_sock(sk); |
| 379 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 380 | __l2cap_state_change(chan, BT_CLOSED); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 381 | sock_set_flag(sk, SOCK_ZAPPED); |
| 382 | |
| 383 | if (err) |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 384 | __l2cap_chan_set_err(chan, err); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 385 | |
| 386 | if (parent) { |
| 387 | bt_accept_unlink(sk); |
| 388 | parent->sk_data_ready(parent, 0); |
| 389 | } else |
| 390 | sk->sk_state_change(sk); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 391 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 392 | release_sock(sk); |
| 393 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 394 | if (!(test_bit(CONF_OUTPUT_DONE, &chan->conf_state) && |
| 395 | test_bit(CONF_INPUT_DONE, &chan->conf_state))) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 396 | return; |
Gustavo F. Padovan | 2ead70b | 2011-04-01 15:13:36 -0300 | [diff] [blame] | 397 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 398 | skb_queue_purge(&chan->tx_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 399 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 400 | if (chan->mode == L2CAP_MODE_ERTM) { |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 401 | struct srej_list *l, *tmp; |
| 402 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 403 | __clear_retrans_timer(chan); |
| 404 | __clear_monitor_timer(chan); |
| 405 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 406 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 407 | skb_queue_purge(&chan->srej_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 408 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 409 | list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 410 | list_del(&l->list); |
| 411 | kfree(l); |
| 412 | } |
| 413 | } |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 416 | static void l2cap_chan_cleanup_listen(struct sock *parent) |
| 417 | { |
| 418 | struct sock *sk; |
| 419 | |
| 420 | BT_DBG("parent %p", parent); |
| 421 | |
| 422 | /* Close not yet accepted channels */ |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 423 | while ((sk = bt_accept_dequeue(parent, NULL))) { |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 424 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 425 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 426 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 427 | __clear_chan_timer(chan); |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 428 | l2cap_chan_close(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 429 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 430 | |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 431 | chan->ops->close(chan->data); |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 432 | } |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 433 | } |
| 434 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 435 | void l2cap_chan_close(struct l2cap_chan *chan, int reason) |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 436 | { |
| 437 | struct l2cap_conn *conn = chan->conn; |
| 438 | struct sock *sk = chan->sk; |
| 439 | |
Andrei Emeltchenko | e05dcc3 | 2012-02-17 11:40:56 +0200 | [diff] [blame] | 440 | BT_DBG("chan %p state %s sk %p", chan, |
| 441 | state_to_string(chan->state), sk); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 442 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 443 | switch (chan->state) { |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 444 | case BT_LISTEN: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 445 | lock_sock(sk); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 446 | l2cap_chan_cleanup_listen(sk); |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 447 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 448 | __l2cap_state_change(chan, BT_CLOSED); |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 449 | sock_set_flag(sk, SOCK_ZAPPED); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 450 | release_sock(sk); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 451 | break; |
| 452 | |
| 453 | case BT_CONNECTED: |
| 454 | case BT_CONFIG: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 455 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 456 | conn->hcon->type == ACL_LINK) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 457 | __clear_chan_timer(chan); |
| 458 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 459 | l2cap_send_disconn_req(conn, chan, reason); |
| 460 | } else |
| 461 | l2cap_chan_del(chan, reason); |
| 462 | break; |
| 463 | |
| 464 | case BT_CONNECT2: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 465 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 466 | conn->hcon->type == ACL_LINK) { |
| 467 | struct l2cap_conn_rsp rsp; |
| 468 | __u16 result; |
| 469 | |
| 470 | if (bt_sk(sk)->defer_setup) |
| 471 | result = L2CAP_CR_SEC_BLOCK; |
| 472 | else |
| 473 | result = L2CAP_CR_BAD_PSM; |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 474 | l2cap_state_change(chan, BT_DISCONN); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 475 | |
| 476 | rsp.scid = cpu_to_le16(chan->dcid); |
| 477 | rsp.dcid = cpu_to_le16(chan->scid); |
| 478 | rsp.result = cpu_to_le16(result); |
| 479 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 480 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
| 481 | sizeof(rsp), &rsp); |
| 482 | } |
| 483 | |
| 484 | l2cap_chan_del(chan, reason); |
| 485 | break; |
| 486 | |
| 487 | case BT_CONNECT: |
| 488 | case BT_DISCONN: |
| 489 | l2cap_chan_del(chan, reason); |
| 490 | break; |
| 491 | |
| 492 | default: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 493 | lock_sock(sk); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 494 | sock_set_flag(sk, SOCK_ZAPPED); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 495 | release_sock(sk); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 496 | break; |
| 497 | } |
| 498 | } |
| 499 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 500 | static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 501 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 502 | if (chan->chan_type == L2CAP_CHAN_RAW) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 503 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 504 | case BT_SECURITY_HIGH: |
| 505 | return HCI_AT_DEDICATED_BONDING_MITM; |
| 506 | case BT_SECURITY_MEDIUM: |
| 507 | return HCI_AT_DEDICATED_BONDING; |
| 508 | default: |
| 509 | return HCI_AT_NO_BONDING; |
| 510 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 511 | } else if (chan->psm == cpu_to_le16(0x0001)) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 512 | if (chan->sec_level == BT_SECURITY_LOW) |
| 513 | chan->sec_level = BT_SECURITY_SDP; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 514 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 515 | if (chan->sec_level == BT_SECURITY_HIGH) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 516 | return HCI_AT_NO_BONDING_MITM; |
| 517 | else |
| 518 | return HCI_AT_NO_BONDING; |
| 519 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 520 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 521 | case BT_SECURITY_HIGH: |
| 522 | return HCI_AT_GENERAL_BONDING_MITM; |
| 523 | case BT_SECURITY_MEDIUM: |
| 524 | return HCI_AT_GENERAL_BONDING; |
| 525 | default: |
| 526 | return HCI_AT_NO_BONDING; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 531 | /* Service level security */ |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 532 | int l2cap_chan_check_security(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 533 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 534 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 535 | __u8 auth_type; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 536 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 537 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 538 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 539 | return hci_conn_security(conn->hcon, chan->sec_level, auth_type); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 542 | static u8 l2cap_get_ident(struct l2cap_conn *conn) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 543 | { |
| 544 | u8 id; |
| 545 | |
| 546 | /* Get next available identificator. |
| 547 | * 1 - 128 are used by kernel. |
| 548 | * 129 - 199 are reserved. |
| 549 | * 200 - 254 are used by utilities like l2ping, etc. |
| 550 | */ |
| 551 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 552 | spin_lock(&conn->lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 553 | |
| 554 | if (++conn->tx_ident > 128) |
| 555 | conn->tx_ident = 1; |
| 556 | |
| 557 | id = conn->tx_ident; |
| 558 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 559 | spin_unlock(&conn->lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 560 | |
| 561 | return id; |
| 562 | } |
| 563 | |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 564 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 565 | { |
| 566 | struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 567 | u8 flags; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 568 | |
| 569 | BT_DBG("code 0x%2.2x", code); |
| 570 | |
| 571 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 572 | return; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 573 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 574 | if (lmp_no_flush_capable(conn->hcon->hdev)) |
| 575 | flags = ACL_START_NO_FLUSH; |
| 576 | else |
| 577 | flags = ACL_START; |
| 578 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 579 | bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 580 | skb->priority = HCI_PRIO_MAX; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 581 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 582 | hci_send_acl(conn->hchan, skb, flags); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 585 | static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) |
| 586 | { |
| 587 | struct hci_conn *hcon = chan->conn->hcon; |
| 588 | u16 flags; |
| 589 | |
| 590 | BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len, |
| 591 | skb->priority); |
| 592 | |
| 593 | if (!test_bit(FLAG_FLUSHABLE, &chan->flags) && |
| 594 | lmp_no_flush_capable(hcon->hdev)) |
| 595 | flags = ACL_START_NO_FLUSH; |
| 596 | else |
| 597 | flags = ACL_START; |
| 598 | |
| 599 | bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 600 | hci_send_acl(chan->conn->hchan, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 603 | static inline void l2cap_send_sframe(struct l2cap_chan *chan, u32 control) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 604 | { |
| 605 | struct sk_buff *skb; |
| 606 | struct l2cap_hdr *lh; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 607 | struct l2cap_conn *conn = chan->conn; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 608 | int count, hlen; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 609 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 610 | if (chan->state != BT_CONNECTED) |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 611 | return; |
| 612 | |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 613 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 614 | hlen = L2CAP_EXT_HDR_SIZE; |
| 615 | else |
| 616 | hlen = L2CAP_ENH_HDR_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 617 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 618 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 619 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 620 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 621 | BT_DBG("chan %p, control 0x%8.8x", chan, control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 622 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 623 | count = min_t(unsigned int, conn->mtu, hlen); |
Andrei Emeltchenko | 793c2f1 | 2011-10-11 13:37:48 +0300 | [diff] [blame] | 624 | |
| 625 | control |= __set_sframe(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 626 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 627 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 628 | control |= __set_ctrl_final(chan); |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 629 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 630 | if (test_and_clear_bit(CONN_SEND_PBIT, &chan->conn_state)) |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 631 | control |= __set_ctrl_poll(chan); |
Gustavo F. Padovan | f0946cc | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 632 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 633 | skb = bt_skb_alloc(count, GFP_ATOMIC); |
| 634 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 635 | return; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 636 | |
| 637 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 638 | lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 639 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 640 | |
| 641 | __put_control(chan, control, skb_put(skb, __ctrl_size(chan))); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 642 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 643 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 644 | u16 fcs = crc16(0, (u8 *)lh, count - L2CAP_FCS_SIZE); |
| 645 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 646 | } |
| 647 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 648 | skb->priority = HCI_PRIO_MAX; |
| 649 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 650 | } |
| 651 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 652 | static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u32 control) |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 653 | { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 654 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 655 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 656 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 657 | } else |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 658 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 659 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 660 | control |= __set_reqseq(chan, chan->buffer_seq); |
Gustavo F. Padovan | 2ab25cd | 2009-10-03 02:34:40 -0300 | [diff] [blame] | 661 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 662 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 663 | } |
| 664 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 665 | static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 666 | { |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 667 | return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 668 | } |
| 669 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 670 | static void l2cap_do_start(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 671 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 672 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 673 | |
| 674 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 675 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) |
| 676 | return; |
| 677 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 678 | if (l2cap_chan_check_security(chan) && |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 679 | __l2cap_no_conn_pending(chan)) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 680 | struct l2cap_conn_req req; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 681 | req.scid = cpu_to_le16(chan->scid); |
| 682 | req.psm = chan->psm; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 683 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 684 | chan->ident = l2cap_get_ident(conn); |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 685 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 686 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 687 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, |
| 688 | sizeof(req), &req); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 689 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 690 | } else { |
| 691 | struct l2cap_info_req req; |
| 692 | req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 693 | |
| 694 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 695 | conn->info_ident = l2cap_get_ident(conn); |
| 696 | |
Gustavo F. Padovan | 030013d | 2011-12-20 10:57:28 -0200 | [diff] [blame] | 697 | schedule_delayed_work(&conn->info_timer, |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 698 | msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); |
| 699 | |
| 700 | l2cap_send_cmd(conn, conn->info_ident, |
| 701 | L2CAP_INFO_REQ, sizeof(req), &req); |
| 702 | } |
| 703 | } |
| 704 | |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 705 | static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) |
| 706 | { |
| 707 | u32 local_feat_mask = l2cap_feat_mask; |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 708 | if (!disable_ertm) |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 709 | local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; |
| 710 | |
| 711 | switch (mode) { |
| 712 | case L2CAP_MODE_ERTM: |
| 713 | return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; |
| 714 | case L2CAP_MODE_STREAMING: |
| 715 | return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; |
| 716 | default: |
| 717 | return 0x00; |
| 718 | } |
| 719 | } |
| 720 | |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 721 | static void l2cap_send_disconn_req(struct l2cap_conn *conn, struct l2cap_chan *chan, int err) |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 722 | { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 723 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 724 | struct l2cap_disconn_req req; |
| 725 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 726 | if (!conn) |
| 727 | return; |
| 728 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 729 | if (chan->mode == L2CAP_MODE_ERTM) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 730 | __clear_retrans_timer(chan); |
| 731 | __clear_monitor_timer(chan); |
| 732 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 733 | } |
| 734 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 735 | req.dcid = cpu_to_le16(chan->dcid); |
| 736 | req.scid = cpu_to_le16(chan->scid); |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 737 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 738 | L2CAP_DISCONN_REQ, sizeof(req), &req); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 739 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 740 | lock_sock(sk); |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 741 | __l2cap_state_change(chan, BT_DISCONN); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 742 | __l2cap_chan_set_err(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 743 | release_sock(sk); |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 744 | } |
| 745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | /* ---- L2CAP connections ---- */ |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 747 | static void l2cap_conn_start(struct l2cap_conn *conn) |
| 748 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 749 | struct l2cap_chan *chan, *tmp; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 750 | |
| 751 | BT_DBG("conn %p", conn); |
| 752 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 753 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 754 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 755 | list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 756 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 757 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 758 | l2cap_chan_lock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 759 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 760 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 761 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 762 | continue; |
| 763 | } |
| 764 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 765 | if (chan->state == BT_CONNECT) { |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 766 | struct l2cap_conn_req req; |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 767 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 768 | if (!l2cap_chan_check_security(chan) || |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 769 | !__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 770 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 771 | continue; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 772 | } |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 773 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 774 | if (!l2cap_mode_supported(chan->mode, conn->feat_mask) |
| 775 | && test_bit(CONF_STATE2_DEVICE, |
| 776 | &chan->conf_state)) { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 777 | l2cap_chan_close(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 778 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 779 | continue; |
| 780 | } |
| 781 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 782 | req.scid = cpu_to_le16(chan->scid); |
| 783 | req.psm = chan->psm; |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 784 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 785 | chan->ident = l2cap_get_ident(conn); |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 786 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 787 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 788 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, |
| 789 | sizeof(req), &req); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 790 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 791 | } else if (chan->state == BT_CONNECT2) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 792 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 793 | char buf[128]; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 794 | rsp.scid = cpu_to_le16(chan->dcid); |
| 795 | rsp.dcid = cpu_to_le16(chan->scid); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 796 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 797 | if (l2cap_chan_check_security(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 798 | lock_sock(sk); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 799 | if (bt_sk(sk)->defer_setup) { |
| 800 | struct sock *parent = bt_sk(sk)->parent; |
| 801 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 802 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); |
Ilia Kolomisnky | 05e9a2f | 2011-07-15 18:30:21 +0000 | [diff] [blame] | 803 | if (parent) |
| 804 | parent->sk_data_ready(parent, 0); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 805 | |
| 806 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 807 | __l2cap_state_change(chan, BT_CONFIG); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 808 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 809 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 810 | } |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 811 | release_sock(sk); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 812 | } else { |
| 813 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 814 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); |
| 815 | } |
| 816 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 817 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
| 818 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 819 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 820 | if (test_bit(CONF_REQ_SENT, &chan->conf_state) || |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 821 | rsp.result != L2CAP_CR_SUCCESS) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 822 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 823 | continue; |
| 824 | } |
| 825 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 826 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 827 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 828 | l2cap_build_conf_req(chan, buf), buf); |
| 829 | chan->num_conf_req++; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 830 | } |
| 831 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 832 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 833 | } |
| 834 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 835 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 836 | } |
| 837 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 838 | /* Find socket with cid and source bdaddr. |
| 839 | * Returns closest match, locked. |
| 840 | */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 841 | static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 842 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 843 | struct l2cap_chan *c, *c1 = NULL; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 844 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 845 | read_lock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 846 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 847 | list_for_each_entry(c, &chan_list, global_l) { |
| 848 | struct sock *sk = c->sk; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 849 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 850 | if (state && c->state != state) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 851 | continue; |
| 852 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 853 | if (c->scid == cid) { |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 854 | /* Exact match. */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 855 | if (!bacmp(&bt_sk(sk)->src, src)) { |
| 856 | read_unlock(&chan_list_lock); |
| 857 | return c; |
| 858 | } |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 859 | |
| 860 | /* Closest match */ |
| 861 | if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 862 | c1 = c; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 863 | } |
| 864 | } |
Gustavo F. Padovan | 280f294 | 2011-04-13 19:01:22 -0300 | [diff] [blame] | 865 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 866 | read_unlock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 867 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 868 | return c1; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | static void l2cap_le_conn_ready(struct l2cap_conn *conn) |
| 872 | { |
Gustavo F. Padovan | c916fbe | 2011-04-04 16:00:55 -0300 | [diff] [blame] | 873 | struct sock *parent, *sk; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 874 | struct l2cap_chan *chan, *pchan; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 875 | |
| 876 | BT_DBG(""); |
| 877 | |
| 878 | /* Check if we have socket listening on cid */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 879 | pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 880 | conn->src); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 881 | if (!pchan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 882 | return; |
| 883 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 884 | parent = pchan->sk; |
| 885 | |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 886 | lock_sock(parent); |
Gustavo F. Padovan | 62f3a2c | 2011-04-14 18:34:34 -0300 | [diff] [blame] | 887 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 888 | /* Check for backlog size */ |
| 889 | if (sk_acceptq_is_full(parent)) { |
| 890 | BT_DBG("backlog full %d", parent->sk_ack_backlog); |
| 891 | goto clean; |
| 892 | } |
| 893 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 894 | chan = pchan->ops->new_connection(pchan->data); |
| 895 | if (!chan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 896 | goto clean; |
| 897 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 898 | sk = chan->sk; |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 899 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 900 | hci_conn_hold(conn->hcon); |
| 901 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 902 | bacpy(&bt_sk(sk)->src, conn->src); |
| 903 | bacpy(&bt_sk(sk)->dst, conn->dst); |
| 904 | |
Gustavo F. Padovan | d101024 | 2011-03-25 00:39:48 -0300 | [diff] [blame] | 905 | bt_accept_enqueue(parent, sk); |
| 906 | |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 907 | l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 908 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 909 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 910 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 911 | __l2cap_state_change(chan, BT_CONNECTED); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 912 | parent->sk_data_ready(parent, 0); |
| 913 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 914 | clean: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 915 | release_sock(parent); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 916 | } |
| 917 | |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 918 | static void l2cap_chan_ready(struct l2cap_chan *chan) |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 919 | { |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 920 | struct sock *sk = chan->sk; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 921 | struct sock *parent; |
| 922 | |
| 923 | lock_sock(sk); |
| 924 | |
| 925 | parent = bt_sk(sk)->parent; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 926 | |
| 927 | BT_DBG("sk %p, parent %p", sk, parent); |
| 928 | |
| 929 | chan->conf_state = 0; |
| 930 | __clear_chan_timer(chan); |
| 931 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 932 | __l2cap_state_change(chan, BT_CONNECTED); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 933 | sk->sk_state_change(sk); |
| 934 | |
| 935 | if (parent) |
| 936 | parent->sk_data_ready(parent, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 937 | |
| 938 | release_sock(sk); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 939 | } |
| 940 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 941 | static void l2cap_conn_ready(struct l2cap_conn *conn) |
| 942 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 943 | struct l2cap_chan *chan; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 944 | |
| 945 | BT_DBG("conn %p", conn); |
| 946 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 947 | if (!conn->hcon->out && conn->hcon->type == LE_LINK) |
| 948 | l2cap_le_conn_ready(conn); |
| 949 | |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 950 | if (conn->hcon->out && conn->hcon->type == LE_LINK) |
| 951 | smp_conn_security(conn, conn->hcon->pending_sec_level); |
| 952 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 953 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 954 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 955 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 956 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 957 | l2cap_chan_lock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 958 | |
Vinicius Costa Gomes | 6312845 | 2011-06-17 22:46:26 -0300 | [diff] [blame] | 959 | if (conn->hcon->type == LE_LINK) { |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 960 | if (smp_conn_security(conn, chan->sec_level)) |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 961 | l2cap_chan_ready(chan); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 962 | |
Vinicius Costa Gomes | 6312845 | 2011-06-17 22:46:26 -0300 | [diff] [blame] | 963 | } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 964 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 965 | __clear_chan_timer(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 966 | lock_sock(sk); |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 967 | __l2cap_state_change(chan, BT_CONNECTED); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 968 | sk->sk_state_change(sk); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 969 | release_sock(sk); |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 970 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 971 | } else if (chan->state == BT_CONNECT) |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 972 | l2cap_do_start(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 973 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 974 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 975 | } |
| 976 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 977 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 978 | } |
| 979 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 980 | /* Notify sockets that we cannot guaranty reliability anymore */ |
| 981 | static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) |
| 982 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 983 | struct l2cap_chan *chan; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 984 | |
| 985 | BT_DBG("conn %p", conn); |
| 986 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 987 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 988 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 989 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | ecf61bd | 2011-10-11 14:04:32 +0300 | [diff] [blame] | 990 | if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 991 | __l2cap_chan_set_err(chan, err); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 992 | } |
| 993 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 994 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 995 | } |
| 996 | |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 997 | static void l2cap_info_timeout(struct work_struct *work) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 998 | { |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 999 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
Gustavo F. Padovan | 030013d | 2011-12-20 10:57:28 -0200 | [diff] [blame] | 1000 | info_timer.work); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1001 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1002 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 1003 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1004 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1005 | l2cap_conn_start(conn); |
| 1006 | } |
| 1007 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1008 | static void l2cap_conn_del(struct hci_conn *hcon, int err) |
| 1009 | { |
| 1010 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 1011 | struct l2cap_chan *chan, *l; |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1012 | |
| 1013 | if (!conn) |
| 1014 | return; |
| 1015 | |
| 1016 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 1017 | |
| 1018 | kfree_skb(conn->rx_skb); |
| 1019 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1020 | mutex_lock(&conn->chan_lock); |
| 1021 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1022 | /* Kill channels */ |
| 1023 | list_for_each_entry_safe(chan, l, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1024 | l2cap_chan_lock(chan); |
| 1025 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1026 | l2cap_chan_del(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1027 | |
| 1028 | l2cap_chan_unlock(chan); |
| 1029 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1030 | chan->ops->close(chan->data); |
| 1031 | } |
| 1032 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1033 | mutex_unlock(&conn->chan_lock); |
| 1034 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1035 | hci_chan_del(conn->hchan); |
| 1036 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1037 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1038 | cancel_delayed_work_sync(&conn->info_timer); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1039 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1040 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) { |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1041 | cancel_delayed_work_sync(&conn->security_timer); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1042 | smp_chan_destroy(conn); |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1043 | } |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1044 | |
| 1045 | hcon->l2cap_data = NULL; |
| 1046 | kfree(conn); |
| 1047 | } |
| 1048 | |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1049 | static void security_timeout(struct work_struct *work) |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1050 | { |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1051 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
| 1052 | security_timer.work); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1053 | |
| 1054 | l2cap_conn_del(conn->hcon, ETIMEDOUT); |
| 1055 | } |
| 1056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) |
| 1058 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1059 | struct l2cap_conn *conn = hcon->l2cap_data; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1060 | struct hci_chan *hchan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1062 | if (conn || status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | return conn; |
| 1064 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1065 | hchan = hci_chan_create(hcon); |
| 1066 | if (!hchan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1069 | conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); |
| 1070 | if (!conn) { |
| 1071 | hci_chan_del(hchan); |
| 1072 | return NULL; |
| 1073 | } |
| 1074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | hcon->l2cap_data = conn; |
| 1076 | conn->hcon = hcon; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1077 | conn->hchan = hchan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1079 | BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1080 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1081 | if (hcon->hdev->le_mtu && hcon->type == LE_LINK) |
| 1082 | conn->mtu = hcon->hdev->le_mtu; |
| 1083 | else |
| 1084 | conn->mtu = hcon->hdev->acl_mtu; |
| 1085 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | conn->src = &hcon->hdev->bdaddr; |
| 1087 | conn->dst = &hcon->dst; |
| 1088 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1089 | conn->feat_mask = 0; |
| 1090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | spin_lock_init(&conn->lock); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1092 | mutex_init(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1093 | |
| 1094 | INIT_LIST_HEAD(&conn->chan_l); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1096 | if (hcon->type == LE_LINK) |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1097 | INIT_DELAYED_WORK(&conn->security_timer, security_timeout); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1098 | else |
Gustavo F. Padovan | 030013d | 2011-12-20 10:57:28 -0200 | [diff] [blame] | 1099 | INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); |
Dave Young | 45054dc | 2009-10-18 20:28:30 +0000 | [diff] [blame] | 1100 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 1101 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 1102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | return conn; |
| 1104 | } |
| 1105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | /* ---- Socket interface ---- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | |
| 1108 | /* Find socket with psm and source bdaddr. |
| 1109 | * Returns closest match. |
| 1110 | */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1111 | static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm, bdaddr_t *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1113 | struct l2cap_chan *c, *c1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1115 | read_lock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1116 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1117 | list_for_each_entry(c, &chan_list, global_l) { |
| 1118 | struct sock *sk = c->sk; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1119 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1120 | if (state && c->state != state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | continue; |
| 1122 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1123 | if (c->psm == psm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | /* Exact match. */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1125 | if (!bacmp(&bt_sk(sk)->src, src)) { |
Johannes Berg | a7567b2 | 2011-06-01 08:29:54 +0200 | [diff] [blame] | 1126 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1127 | return c; |
| 1128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | |
| 1130 | /* Closest match */ |
| 1131 | if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1132 | c1 = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | } |
| 1134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1136 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1137 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1138 | return c1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Johan Hedberg | cbe8fed | 2012-01-08 22:51:16 +0200 | [diff] [blame] | 1141 | int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, bdaddr_t *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | { |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 1143 | struct sock *sk = chan->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | bdaddr_t *src = &bt_sk(sk)->src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | struct l2cap_conn *conn; |
| 1146 | struct hci_conn *hcon; |
| 1147 | struct hci_dev *hdev; |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1148 | __u8 auth_type; |
Marcel Holtmann | 44d0e48 | 2009-04-20 07:09:16 +0200 | [diff] [blame] | 1149 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | |
Marcel Holtmann | f29972d | 2009-02-12 05:07:45 +0100 | [diff] [blame] | 1151 | BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1152 | chan->psm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1154 | hdev = hci_get_route(dst, src); |
| 1155 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | return -EHOSTUNREACH; |
| 1157 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1158 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1160 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1161 | |
| 1162 | /* PSM must be odd and lsb of upper byte must be 0 */ |
| 1163 | if ((__le16_to_cpu(psm) & 0x0101) != 0x0001 && !cid && |
| 1164 | chan->chan_type != L2CAP_CHAN_RAW) { |
| 1165 | err = -EINVAL; |
| 1166 | goto done; |
| 1167 | } |
| 1168 | |
| 1169 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !(psm || cid)) { |
| 1170 | err = -EINVAL; |
| 1171 | goto done; |
| 1172 | } |
| 1173 | |
| 1174 | switch (chan->mode) { |
| 1175 | case L2CAP_MODE_BASIC: |
| 1176 | break; |
| 1177 | case L2CAP_MODE_ERTM: |
| 1178 | case L2CAP_MODE_STREAMING: |
| 1179 | if (!disable_ertm) |
| 1180 | break; |
| 1181 | /* fall through */ |
| 1182 | default: |
| 1183 | err = -ENOTSUPP; |
| 1184 | goto done; |
| 1185 | } |
| 1186 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1187 | lock_sock(sk); |
| 1188 | |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1189 | switch (sk->sk_state) { |
| 1190 | case BT_CONNECT: |
| 1191 | case BT_CONNECT2: |
| 1192 | case BT_CONFIG: |
| 1193 | /* Already connecting */ |
| 1194 | err = 0; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1195 | release_sock(sk); |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1196 | goto done; |
| 1197 | |
| 1198 | case BT_CONNECTED: |
| 1199 | /* Already connected */ |
| 1200 | err = -EISCONN; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1201 | release_sock(sk); |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1202 | goto done; |
| 1203 | |
| 1204 | case BT_OPEN: |
| 1205 | case BT_BOUND: |
| 1206 | /* Can connect */ |
| 1207 | break; |
| 1208 | |
| 1209 | default: |
| 1210 | err = -EBADFD; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1211 | release_sock(sk); |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1212 | goto done; |
| 1213 | } |
| 1214 | |
| 1215 | /* Set destination address and psm */ |
Gustavo F. Padovan | 9219b2a | 2012-01-02 20:08:04 -0200 | [diff] [blame] | 1216 | bacpy(&bt_sk(sk)->dst, dst); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1217 | |
| 1218 | release_sock(sk); |
| 1219 | |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1220 | chan->psm = psm; |
| 1221 | chan->dcid = cid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1223 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1224 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1225 | if (chan->dcid == L2CAP_CID_LE_DATA) |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1226 | hcon = hci_connect(hdev, LE_LINK, dst, |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1227 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1228 | else |
| 1229 | hcon = hci_connect(hdev, ACL_LINK, dst, |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1230 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1231 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1232 | if (IS_ERR(hcon)) { |
| 1233 | err = PTR_ERR(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | goto done; |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | |
| 1237 | conn = l2cap_conn_add(hcon, 0); |
| 1238 | if (!conn) { |
| 1239 | hci_conn_put(hcon); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1240 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | goto done; |
| 1242 | } |
| 1243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | /* Update source addr of the socket */ |
| 1245 | bacpy(src, conn->src); |
| 1246 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1247 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1248 | l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1249 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1250 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1251 | l2cap_state_change(chan, BT_CONNECT); |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1252 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | |
| 1254 | if (hcon->state == BT_CONNECTED) { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1255 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1256 | __clear_chan_timer(chan); |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1257 | if (l2cap_chan_check_security(chan)) |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1258 | l2cap_state_change(chan, BT_CONNECTED); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1259 | } else |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1260 | l2cap_do_start(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1263 | err = 0; |
| 1264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1266 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1267 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | hci_dev_put(hdev); |
| 1269 | return err; |
| 1270 | } |
| 1271 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1272 | int __l2cap_wait_ack(struct sock *sk) |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1273 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1274 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1275 | DECLARE_WAITQUEUE(wait, current); |
| 1276 | int err = 0; |
| 1277 | int timeo = HZ/5; |
| 1278 | |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1279 | add_wait_queue(sk_sleep(sk), &wait); |
Peter Hurley | a71a0cf | 2011-07-25 18:36:26 -0400 | [diff] [blame] | 1280 | set_current_state(TASK_INTERRUPTIBLE); |
| 1281 | while (chan->unacked_frames > 0 && chan->conn) { |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1282 | if (!timeo) |
| 1283 | timeo = HZ/5; |
| 1284 | |
| 1285 | if (signal_pending(current)) { |
| 1286 | err = sock_intr_errno(timeo); |
| 1287 | break; |
| 1288 | } |
| 1289 | |
| 1290 | release_sock(sk); |
| 1291 | timeo = schedule_timeout(timeo); |
| 1292 | lock_sock(sk); |
Peter Hurley | a71a0cf | 2011-07-25 18:36:26 -0400 | [diff] [blame] | 1293 | set_current_state(TASK_INTERRUPTIBLE); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1294 | |
| 1295 | err = sock_error(sk); |
| 1296 | if (err) |
| 1297 | break; |
| 1298 | } |
| 1299 | set_current_state(TASK_RUNNING); |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1300 | remove_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1301 | return err; |
| 1302 | } |
| 1303 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1304 | static void l2cap_monitor_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1305 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1306 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
| 1307 | monitor_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1308 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1309 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1310 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1311 | l2cap_chan_lock(chan); |
| 1312 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1313 | if (chan->retry_count >= chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1314 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1315 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1316 | return; |
| 1317 | } |
| 1318 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1319 | chan->retry_count++; |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1320 | __set_monitor_timer(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1321 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1322 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1323 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1324 | } |
| 1325 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1326 | static void l2cap_retrans_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1327 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1328 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
| 1329 | retrans_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1330 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1331 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1332 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1333 | l2cap_chan_lock(chan); |
| 1334 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1335 | chan->retry_count = 1; |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1336 | __set_monitor_timer(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1337 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1338 | set_bit(CONN_WAIT_F, &chan->conn_state); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1339 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1340 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1341 | |
| 1342 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1343 | } |
| 1344 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1345 | static void l2cap_drop_acked_frames(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1346 | { |
| 1347 | struct sk_buff *skb; |
| 1348 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1349 | while ((skb = skb_peek(&chan->tx_q)) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1350 | chan->unacked_frames) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1351 | if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1352 | break; |
| 1353 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1354 | skb = skb_dequeue(&chan->tx_q); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1355 | kfree_skb(skb); |
| 1356 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1357 | chan->unacked_frames--; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1358 | } |
| 1359 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1360 | if (!chan->unacked_frames) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1361 | __clear_retrans_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1362 | } |
| 1363 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1364 | static void l2cap_streaming_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1365 | { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1366 | struct sk_buff *skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1367 | u32 control; |
| 1368 | u16 fcs; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1369 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1370 | while ((skb = skb_dequeue(&chan->tx_q))) { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1371 | control = __get_control(chan, skb->data + L2CAP_HDR_SIZE); |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 1372 | control |= __set_txseq(chan, chan->next_tx_seq); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1373 | __put_control(chan, control, skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1374 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1375 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1376 | fcs = crc16(0, (u8 *)skb->data, |
| 1377 | skb->len - L2CAP_FCS_SIZE); |
| 1378 | put_unaligned_le16(fcs, |
| 1379 | skb->data + skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1380 | } |
| 1381 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1382 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1383 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1384 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1385 | } |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1386 | } |
| 1387 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 1388 | static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u16 tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1389 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1390 | struct sk_buff *skb, *tx_skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1391 | u16 fcs; |
| 1392 | u32 control; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1393 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1394 | skb = skb_peek(&chan->tx_q); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1395 | if (!skb) |
| 1396 | return; |
| 1397 | |
Szymon Janc | d1726b6 | 2011-11-16 09:32:20 +0100 | [diff] [blame] | 1398 | while (bt_cb(skb)->tx_seq != tx_seq) { |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1399 | if (skb_queue_is_last(&chan->tx_q, skb)) |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1400 | return; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1401 | |
Szymon Janc | d1726b6 | 2011-11-16 09:32:20 +0100 | [diff] [blame] | 1402 | skb = skb_queue_next(&chan->tx_q, skb); |
| 1403 | } |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1404 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1405 | if (chan->remote_max_tx && |
| 1406 | bt_cb(skb)->retries == chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1407 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1408 | return; |
| 1409 | } |
| 1410 | |
| 1411 | tx_skb = skb_clone(skb, GFP_ATOMIC); |
| 1412 | bt_cb(skb)->retries++; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1413 | |
| 1414 | control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1415 | control &= __get_sar_mask(chan); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 1416 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1417 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 1418 | control |= __set_ctrl_final(chan); |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1419 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 1420 | control |= __set_reqseq(chan, chan->buffer_seq); |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 1421 | control |= __set_txseq(chan, tx_seq); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 1422 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1423 | __put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1424 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1425 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1426 | fcs = crc16(0, (u8 *)tx_skb->data, |
| 1427 | tx_skb->len - L2CAP_FCS_SIZE); |
| 1428 | put_unaligned_le16(fcs, |
| 1429 | tx_skb->data + tx_skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1430 | } |
| 1431 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1432 | l2cap_do_send(chan, tx_skb); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1433 | } |
| 1434 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1435 | static int l2cap_ertm_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1436 | { |
| 1437 | struct sk_buff *skb, *tx_skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1438 | u16 fcs; |
| 1439 | u32 control; |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1440 | int nsent = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1441 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1442 | if (chan->state != BT_CONNECTED) |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1443 | return -ENOTCONN; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1444 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1445 | while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1446 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1447 | if (chan->remote_max_tx && |
| 1448 | bt_cb(skb)->retries == chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1449 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1450 | break; |
| 1451 | } |
| 1452 | |
Andrei Emeltchenko | e420aba | 2009-12-23 13:07:14 +0200 | [diff] [blame] | 1453 | tx_skb = skb_clone(skb, GFP_ATOMIC); |
| 1454 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1455 | bt_cb(skb)->retries++; |
| 1456 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1457 | control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1458 | control &= __get_sar_mask(chan); |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1459 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1460 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 1461 | control |= __set_ctrl_final(chan); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1462 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 1463 | control |= __set_reqseq(chan, chan->buffer_seq); |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 1464 | control |= __set_txseq(chan, chan->next_tx_seq); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1465 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1466 | __put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1467 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1468 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1469 | fcs = crc16(0, (u8 *)skb->data, |
| 1470 | tx_skb->len - L2CAP_FCS_SIZE); |
| 1471 | put_unaligned_le16(fcs, skb->data + |
| 1472 | tx_skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1473 | } |
| 1474 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1475 | l2cap_do_send(chan, tx_skb); |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1476 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1477 | __set_retrans_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1478 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1479 | bt_cb(skb)->tx_seq = chan->next_tx_seq; |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1480 | |
| 1481 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1482 | |
Szymon Janc | 8ed7a0a | 2012-02-07 15:43:01 +0100 | [diff] [blame] | 1483 | if (bt_cb(skb)->retries == 1) { |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1484 | chan->unacked_frames++; |
Szymon Janc | 930fa4a | 2012-02-07 15:43:02 +0100 | [diff] [blame] | 1485 | |
| 1486 | if (!nsent++) |
| 1487 | __clear_ack_timer(chan); |
Szymon Janc | 8ed7a0a | 2012-02-07 15:43:01 +0100 | [diff] [blame] | 1488 | } |
Suraj Sumangala | 23e9fde | 2011-03-09 14:44:05 +0530 | [diff] [blame] | 1489 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1490 | chan->frames_sent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1491 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1492 | if (skb_queue_is_last(&chan->tx_q, skb)) |
| 1493 | chan->tx_send_head = NULL; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1494 | else |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1495 | chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1496 | } |
| 1497 | |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1498 | return nsent; |
| 1499 | } |
| 1500 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1501 | static int l2cap_retransmit_frames(struct l2cap_chan *chan) |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1502 | { |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1503 | int ret; |
| 1504 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1505 | if (!skb_queue_empty(&chan->tx_q)) |
| 1506 | chan->tx_send_head = chan->tx_q.next; |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1507 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1508 | chan->next_tx_seq = chan->expected_ack_seq; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1509 | ret = l2cap_ertm_send(chan); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1510 | return ret; |
| 1511 | } |
| 1512 | |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 1513 | static void __l2cap_send_ack(struct l2cap_chan *chan) |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1514 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1515 | u32 control = 0; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1516 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 1517 | control |= __set_reqseq(chan, chan->buffer_seq); |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1518 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1519 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 1520 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1521 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1522 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1523 | return; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1524 | } |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1525 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1526 | if (l2cap_ertm_send(chan) > 0) |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1527 | return; |
| 1528 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 1529 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1530 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1531 | } |
| 1532 | |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 1533 | static void l2cap_send_ack(struct l2cap_chan *chan) |
| 1534 | { |
| 1535 | __clear_ack_timer(chan); |
| 1536 | __l2cap_send_ack(chan); |
| 1537 | } |
| 1538 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1539 | static void l2cap_send_srejtail(struct l2cap_chan *chan) |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1540 | { |
| 1541 | struct srej_list *tail; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1542 | u32 control; |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1543 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 1544 | control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 1545 | control |= __set_ctrl_final(chan); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1546 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 1547 | tail = list_entry((&chan->srej_l)->prev, struct srej_list, list); |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 1548 | control |= __set_reqseq(chan, tail->tx_seq); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1549 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1550 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1551 | } |
| 1552 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 1553 | static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, struct msghdr *msg, int len, int count, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | { |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 1555 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1556 | struct sk_buff **frag; |
| 1557 | int err, sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | |
Gustavo F. Padovan | 59203a2 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1559 | if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1560 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | |
| 1562 | sent += count; |
| 1563 | len -= count; |
| 1564 | |
| 1565 | /* Continuation fragments (no L2CAP header) */ |
| 1566 | frag = &skb_shinfo(skb)->frag_list; |
| 1567 | while (len) { |
| 1568 | count = min_t(unsigned int, conn->mtu, len); |
| 1569 | |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1570 | *frag = chan->ops->alloc_skb(chan, count, |
| 1571 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | if (!*frag) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1574 | return err; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1575 | if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) |
| 1576 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1578 | (*frag)->priority = skb->priority; |
| 1579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | sent += count; |
| 1581 | len -= count; |
| 1582 | |
| 1583 | frag = &(*frag)->next; |
| 1584 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | |
| 1586 | return sent; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1589 | static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, |
| 1590 | struct msghdr *msg, size_t len, |
| 1591 | u32 priority) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1592 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1593 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1594 | struct sk_buff *skb; |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1595 | int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1596 | struct l2cap_hdr *lh; |
| 1597 | |
Andrei Emeltchenko | 6d5922b | 2012-02-06 15:04:01 +0200 | [diff] [blame] | 1598 | BT_DBG("chan %p len %d priority %u", chan, (int)len, priority); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1599 | |
| 1600 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1601 | |
| 1602 | skb = chan->ops->alloc_skb(chan, count + hlen, |
| 1603 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1604 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1605 | if (!skb) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1606 | return ERR_PTR(err); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1607 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1608 | skb->priority = priority; |
| 1609 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1610 | /* Create L2CAP header */ |
| 1611 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1612 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1613 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1614 | put_unaligned_le16(chan->psm, skb_put(skb, 2)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1615 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 1616 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1617 | if (unlikely(err < 0)) { |
| 1618 | kfree_skb(skb); |
| 1619 | return ERR_PTR(err); |
| 1620 | } |
| 1621 | return skb; |
| 1622 | } |
| 1623 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1624 | static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, |
| 1625 | struct msghdr *msg, size_t len, |
| 1626 | u32 priority) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1627 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1628 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1629 | struct sk_buff *skb; |
| 1630 | int err, count, hlen = L2CAP_HDR_SIZE; |
| 1631 | struct l2cap_hdr *lh; |
| 1632 | |
Andrei Emeltchenko | 6d5922b | 2012-02-06 15:04:01 +0200 | [diff] [blame] | 1633 | BT_DBG("chan %p len %d", chan, (int)len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1634 | |
| 1635 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1636 | |
| 1637 | skb = chan->ops->alloc_skb(chan, count + hlen, |
| 1638 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1639 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1640 | if (!skb) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1641 | return ERR_PTR(err); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1642 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1643 | skb->priority = priority; |
| 1644 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1645 | /* Create L2CAP header */ |
| 1646 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1647 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1648 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
| 1649 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 1650 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1651 | if (unlikely(err < 0)) { |
| 1652 | kfree_skb(skb); |
| 1653 | return ERR_PTR(err); |
| 1654 | } |
| 1655 | return skb; |
| 1656 | } |
| 1657 | |
Luiz Augusto von Dentz | ab0ff76 | 2011-09-12 20:00:50 +0300 | [diff] [blame] | 1658 | static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, |
| 1659 | struct msghdr *msg, size_t len, |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1660 | u32 control, u16 sdulen) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1661 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1662 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1663 | struct sk_buff *skb; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 1664 | int err, count, hlen; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1665 | struct l2cap_hdr *lh; |
| 1666 | |
Andrei Emeltchenko | 6d5922b | 2012-02-06 15:04:01 +0200 | [diff] [blame] | 1667 | BT_DBG("chan %p len %d", chan, (int)len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1668 | |
Gustavo F. Padovan | 0ee0d20 | 2010-05-01 16:15:41 -0300 | [diff] [blame] | 1669 | if (!conn) |
| 1670 | return ERR_PTR(-ENOTCONN); |
| 1671 | |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 1672 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 1673 | hlen = L2CAP_EXT_HDR_SIZE; |
| 1674 | else |
| 1675 | hlen = L2CAP_ENH_HDR_SIZE; |
| 1676 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1677 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1678 | hlen += L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1679 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1680 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1681 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1682 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1683 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1684 | |
| 1685 | skb = chan->ops->alloc_skb(chan, count + hlen, |
| 1686 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1687 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1688 | if (!skb) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1689 | return ERR_PTR(err); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1690 | |
| 1691 | /* Create L2CAP header */ |
| 1692 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1693 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1694 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1695 | |
| 1696 | __put_control(chan, control, skb_put(skb, __ctrl_size(chan))); |
| 1697 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1698 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1699 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1700 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 1701 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1702 | if (unlikely(err < 0)) { |
| 1703 | kfree_skb(skb); |
| 1704 | return ERR_PTR(err); |
| 1705 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1706 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1707 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1708 | put_unaligned_le16(0, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1709 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1710 | bt_cb(skb)->retries = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1711 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | } |
| 1713 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1714 | static int l2cap_sar_segment_sdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1715 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1716 | struct sk_buff *skb; |
| 1717 | struct sk_buff_head sar_queue; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1718 | u32 control; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1719 | size_t size = 0; |
| 1720 | |
Gustavo F. Padovan | ff12fd6 | 2010-05-05 22:09:15 -0300 | [diff] [blame] | 1721 | skb_queue_head_init(&sar_queue); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1722 | control = __set_ctrl_sar(chan, L2CAP_SAR_START); |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1723 | skb = l2cap_create_iframe_pdu(chan, msg, chan->remote_mps, control, len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1724 | if (IS_ERR(skb)) |
| 1725 | return PTR_ERR(skb); |
| 1726 | |
| 1727 | __skb_queue_tail(&sar_queue, skb); |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1728 | len -= chan->remote_mps; |
| 1729 | size += chan->remote_mps; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1730 | |
| 1731 | while (len > 0) { |
| 1732 | size_t buflen; |
| 1733 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1734 | if (len > chan->remote_mps) { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1735 | control = __set_ctrl_sar(chan, L2CAP_SAR_CONTINUE); |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1736 | buflen = chan->remote_mps; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1737 | } else { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1738 | control = __set_ctrl_sar(chan, L2CAP_SAR_END); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1739 | buflen = len; |
| 1740 | } |
| 1741 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1742 | skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1743 | if (IS_ERR(skb)) { |
| 1744 | skb_queue_purge(&sar_queue); |
| 1745 | return PTR_ERR(skb); |
| 1746 | } |
| 1747 | |
| 1748 | __skb_queue_tail(&sar_queue, skb); |
| 1749 | len -= buflen; |
| 1750 | size += buflen; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1751 | } |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1752 | skb_queue_splice_tail(&sar_queue, &chan->tx_q); |
| 1753 | if (chan->tx_send_head == NULL) |
| 1754 | chan->tx_send_head = sar_queue.next; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1755 | |
| 1756 | return size; |
| 1757 | } |
| 1758 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1759 | int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, |
| 1760 | u32 priority) |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1761 | { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1762 | struct sk_buff *skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1763 | u32 control; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1764 | int err; |
| 1765 | |
| 1766 | /* Connectionless channel */ |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1767 | if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1768 | skb = l2cap_create_connless_pdu(chan, msg, len, priority); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1769 | if (IS_ERR(skb)) |
| 1770 | return PTR_ERR(skb); |
| 1771 | |
| 1772 | l2cap_do_send(chan, skb); |
| 1773 | return len; |
| 1774 | } |
| 1775 | |
| 1776 | switch (chan->mode) { |
| 1777 | case L2CAP_MODE_BASIC: |
| 1778 | /* Check outgoing MTU */ |
| 1779 | if (len > chan->omtu) |
| 1780 | return -EMSGSIZE; |
| 1781 | |
| 1782 | /* Create a basic PDU */ |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1783 | skb = l2cap_create_basic_pdu(chan, msg, len, priority); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1784 | if (IS_ERR(skb)) |
| 1785 | return PTR_ERR(skb); |
| 1786 | |
| 1787 | l2cap_do_send(chan, skb); |
| 1788 | err = len; |
| 1789 | break; |
| 1790 | |
| 1791 | case L2CAP_MODE_ERTM: |
| 1792 | case L2CAP_MODE_STREAMING: |
| 1793 | /* Entire SDU fits into one PDU */ |
| 1794 | if (len <= chan->remote_mps) { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1795 | control = __set_ctrl_sar(chan, L2CAP_SAR_UNSEGMENTED); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1796 | skb = l2cap_create_iframe_pdu(chan, msg, len, control, |
| 1797 | 0); |
| 1798 | if (IS_ERR(skb)) |
| 1799 | return PTR_ERR(skb); |
| 1800 | |
| 1801 | __skb_queue_tail(&chan->tx_q, skb); |
| 1802 | |
| 1803 | if (chan->tx_send_head == NULL) |
| 1804 | chan->tx_send_head = skb; |
| 1805 | |
| 1806 | } else { |
| 1807 | /* Segment SDU into multiples PDUs */ |
| 1808 | err = l2cap_sar_segment_sdu(chan, msg, len); |
| 1809 | if (err < 0) |
| 1810 | return err; |
| 1811 | } |
| 1812 | |
| 1813 | if (chan->mode == L2CAP_MODE_STREAMING) { |
| 1814 | l2cap_streaming_send(chan); |
| 1815 | err = len; |
| 1816 | break; |
| 1817 | } |
| 1818 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1819 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
| 1820 | test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1821 | err = len; |
| 1822 | break; |
| 1823 | } |
| 1824 | |
| 1825 | err = l2cap_ertm_send(chan); |
| 1826 | if (err >= 0) |
| 1827 | err = len; |
| 1828 | |
| 1829 | break; |
| 1830 | |
| 1831 | default: |
| 1832 | BT_DBG("bad state %1.1x", chan->mode); |
| 1833 | err = -EBADFD; |
| 1834 | } |
| 1835 | |
| 1836 | return err; |
| 1837 | } |
| 1838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | /* Copy frame to all raw sockets on that connection */ |
| 1840 | static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1841 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | struct sk_buff *nskb; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1843 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | |
| 1845 | BT_DBG("conn %p", conn); |
| 1846 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1847 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 1848 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1849 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1850 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1851 | if (chan->chan_type != L2CAP_CHAN_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | continue; |
| 1853 | |
| 1854 | /* Don't send frame to the socket it came from */ |
| 1855 | if (skb->sk == sk) |
| 1856 | continue; |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1857 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 1858 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | continue; |
| 1860 | |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 1861 | if (chan->ops->recv(chan->data, nskb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | kfree_skb(nskb); |
| 1863 | } |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 1864 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1865 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | /* ---- L2CAP signalling commands ---- */ |
| 1869 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, |
| 1870 | u8 code, u8 ident, u16 dlen, void *data) |
| 1871 | { |
| 1872 | struct sk_buff *skb, **frag; |
| 1873 | struct l2cap_cmd_hdr *cmd; |
| 1874 | struct l2cap_hdr *lh; |
| 1875 | int len, count; |
| 1876 | |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1877 | BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", |
| 1878 | conn, code, ident, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | |
| 1880 | len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; |
| 1881 | count = min_t(unsigned int, conn->mtu, len); |
| 1882 | |
| 1883 | skb = bt_skb_alloc(count, GFP_ATOMIC); |
| 1884 | if (!skb) |
| 1885 | return NULL; |
| 1886 | |
| 1887 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1888 | lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 1889 | |
| 1890 | if (conn->hcon->type == LE_LINK) |
| 1891 | lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); |
| 1892 | else |
| 1893 | lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | |
| 1895 | cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); |
| 1896 | cmd->code = code; |
| 1897 | cmd->ident = ident; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1898 | cmd->len = cpu_to_le16(dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | |
| 1900 | if (dlen) { |
| 1901 | count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; |
| 1902 | memcpy(skb_put(skb, count), data, count); |
| 1903 | data += count; |
| 1904 | } |
| 1905 | |
| 1906 | len -= skb->len; |
| 1907 | |
| 1908 | /* Continuation fragments (no L2CAP header) */ |
| 1909 | frag = &skb_shinfo(skb)->frag_list; |
| 1910 | while (len) { |
| 1911 | count = min_t(unsigned int, conn->mtu, len); |
| 1912 | |
| 1913 | *frag = bt_skb_alloc(count, GFP_ATOMIC); |
| 1914 | if (!*frag) |
| 1915 | goto fail; |
| 1916 | |
| 1917 | memcpy(skb_put(*frag, count), data, count); |
| 1918 | |
| 1919 | len -= count; |
| 1920 | data += count; |
| 1921 | |
| 1922 | frag = &(*frag)->next; |
| 1923 | } |
| 1924 | |
| 1925 | return skb; |
| 1926 | |
| 1927 | fail: |
| 1928 | kfree_skb(skb); |
| 1929 | return NULL; |
| 1930 | } |
| 1931 | |
| 1932 | static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) |
| 1933 | { |
| 1934 | struct l2cap_conf_opt *opt = *ptr; |
| 1935 | int len; |
| 1936 | |
| 1937 | len = L2CAP_CONF_OPT_SIZE + opt->len; |
| 1938 | *ptr += len; |
| 1939 | |
| 1940 | *type = opt->type; |
| 1941 | *olen = opt->len; |
| 1942 | |
| 1943 | switch (opt->len) { |
| 1944 | case 1: |
| 1945 | *val = *((u8 *) opt->val); |
| 1946 | break; |
| 1947 | |
| 1948 | case 2: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 1949 | *val = get_unaligned_le16(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | break; |
| 1951 | |
| 1952 | case 4: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 1953 | *val = get_unaligned_le32(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | break; |
| 1955 | |
| 1956 | default: |
| 1957 | *val = (unsigned long) opt->val; |
| 1958 | break; |
| 1959 | } |
| 1960 | |
| 1961 | BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); |
| 1962 | return len; |
| 1963 | } |
| 1964 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) |
| 1966 | { |
| 1967 | struct l2cap_conf_opt *opt = *ptr; |
| 1968 | |
| 1969 | BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); |
| 1970 | |
| 1971 | opt->type = type; |
| 1972 | opt->len = len; |
| 1973 | |
| 1974 | switch (len) { |
| 1975 | case 1: |
| 1976 | *((u8 *) opt->val) = val; |
| 1977 | break; |
| 1978 | |
| 1979 | case 2: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 1980 | put_unaligned_le16(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | break; |
| 1982 | |
| 1983 | case 4: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 1984 | put_unaligned_le32(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | break; |
| 1986 | |
| 1987 | default: |
| 1988 | memcpy(opt->val, (void *) val, len); |
| 1989 | break; |
| 1990 | } |
| 1991 | |
| 1992 | *ptr += L2CAP_CONF_OPT_SIZE + len; |
| 1993 | } |
| 1994 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 1995 | static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) |
| 1996 | { |
| 1997 | struct l2cap_conf_efs efs; |
| 1998 | |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 1999 | switch (chan->mode) { |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2000 | case L2CAP_MODE_ERTM: |
| 2001 | efs.id = chan->local_id; |
| 2002 | efs.stype = chan->local_stype; |
| 2003 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 2004 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
| 2005 | efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); |
| 2006 | efs.flush_to = cpu_to_le32(L2CAP_DEFAULT_FLUSH_TO); |
| 2007 | break; |
| 2008 | |
| 2009 | case L2CAP_MODE_STREAMING: |
| 2010 | efs.id = 1; |
| 2011 | efs.stype = L2CAP_SERV_BESTEFFORT; |
| 2012 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 2013 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
| 2014 | efs.acc_lat = 0; |
| 2015 | efs.flush_to = 0; |
| 2016 | break; |
| 2017 | |
| 2018 | default: |
| 2019 | return; |
| 2020 | } |
| 2021 | |
| 2022 | l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), |
| 2023 | (unsigned long) &efs); |
| 2024 | } |
| 2025 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 2026 | static void l2cap_ack_timeout(struct work_struct *work) |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2027 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 2028 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
| 2029 | ack_timer.work); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2030 | |
Gustavo F. Padovan | 2fb9b3d | 2011-12-22 16:56:05 -0200 | [diff] [blame] | 2031 | BT_DBG("chan %p", chan); |
| 2032 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2033 | l2cap_chan_lock(chan); |
| 2034 | |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2035 | __l2cap_send_ack(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2036 | |
| 2037 | l2cap_chan_unlock(chan); |
Szymon Janc | 09bfb2e | 2012-01-11 10:59:49 +0100 | [diff] [blame] | 2038 | |
| 2039 | l2cap_chan_put(chan); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2040 | } |
| 2041 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2042 | static inline void l2cap_ertm_init(struct l2cap_chan *chan) |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2043 | { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2044 | chan->expected_ack_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 2045 | chan->unacked_frames = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2046 | chan->buffer_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 2047 | chan->num_acked = 0; |
| 2048 | chan->frames_sent = 0; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2049 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 2050 | INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); |
| 2051 | INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); |
| 2052 | INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2053 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 2054 | skb_queue_head_init(&chan->srej_q); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2055 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 2056 | INIT_LIST_HEAD(&chan->srej_l); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2057 | } |
| 2058 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2059 | static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) |
| 2060 | { |
| 2061 | switch (mode) { |
| 2062 | case L2CAP_MODE_STREAMING: |
| 2063 | case L2CAP_MODE_ERTM: |
| 2064 | if (l2cap_mode_supported(mode, remote_feat_mask)) |
| 2065 | return mode; |
| 2066 | /* fall through */ |
| 2067 | default: |
| 2068 | return L2CAP_MODE_BASIC; |
| 2069 | } |
| 2070 | } |
| 2071 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2072 | static inline bool __l2cap_ews_supported(struct l2cap_chan *chan) |
| 2073 | { |
| 2074 | return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; |
| 2075 | } |
| 2076 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2077 | static inline bool __l2cap_efs_supported(struct l2cap_chan *chan) |
| 2078 | { |
| 2079 | return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW; |
| 2080 | } |
| 2081 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2082 | static inline void l2cap_txwin_setup(struct l2cap_chan *chan) |
| 2083 | { |
| 2084 | if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2085 | __l2cap_ews_supported(chan)) { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2086 | /* use extended control field */ |
| 2087 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2088 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
| 2089 | } else { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2090 | chan->tx_win = min_t(u16, chan->tx_win, |
| 2091 | L2CAP_DEFAULT_TX_WINDOW); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2092 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
| 2093 | } |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2094 | } |
| 2095 | |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2096 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | struct l2cap_conf_req *req = data; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2099 | struct l2cap_conf_rfc rfc = { .mode = chan->mode }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | void *ptr = req->data; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2101 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 2103 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2105 | if (chan->num_conf_req || chan->num_conf_rsp) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2106 | goto done; |
| 2107 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2108 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2109 | case L2CAP_MODE_STREAMING: |
| 2110 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2111 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 2112 | break; |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 2113 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2114 | if (__l2cap_efs_supported(chan)) |
| 2115 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 2116 | |
Gustavo F. Padovan | 2ba13ed | 2010-06-09 16:39:05 -0300 | [diff] [blame] | 2117 | /* fall through */ |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2118 | default: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2119 | chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2120 | break; |
| 2121 | } |
| 2122 | |
| 2123 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2124 | if (chan->imtu != L2CAP_DEFAULT_MTU) |
| 2125 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | 7990681c | 2011-01-24 16:01:43 -0200 | [diff] [blame] | 2126 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2127 | switch (chan->mode) { |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 2128 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2129 | if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && |
| 2130 | !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 2131 | break; |
| 2132 | |
Gustavo F. Padovan | 6254775 | 2010-06-08 20:05:31 -0300 | [diff] [blame] | 2133 | rfc.mode = L2CAP_MODE_BASIC; |
| 2134 | rfc.txwin_size = 0; |
| 2135 | rfc.max_transmit = 0; |
| 2136 | rfc.retrans_timeout = 0; |
| 2137 | rfc.monitor_timeout = 0; |
| 2138 | rfc.max_pdu_size = 0; |
| 2139 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 2140 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 2141 | (unsigned long) &rfc); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 2142 | break; |
| 2143 | |
| 2144 | case L2CAP_MODE_ERTM: |
| 2145 | rfc.mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2146 | rfc.max_transmit = chan->max_tx; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2147 | rfc.retrans_timeout = 0; |
| 2148 | rfc.monitor_timeout = 0; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2149 | |
| 2150 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
| 2151 | L2CAP_EXT_HDR_SIZE - |
| 2152 | L2CAP_SDULEN_SIZE - |
| 2153 | L2CAP_FCS_SIZE); |
| 2154 | rfc.max_pdu_size = cpu_to_le16(size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2155 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2156 | l2cap_txwin_setup(chan); |
| 2157 | |
| 2158 | rfc.txwin_size = min_t(u16, chan->tx_win, |
| 2159 | L2CAP_DEFAULT_TX_WINDOW); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2160 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 2161 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 2162 | (unsigned long) &rfc); |
| 2163 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2164 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 2165 | l2cap_add_opt_efs(&ptr, chan); |
| 2166 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2167 | if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2168 | break; |
| 2169 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2170 | if (chan->fcs == L2CAP_FCS_NONE || |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2171 | test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2172 | chan->fcs = L2CAP_FCS_NONE; |
| 2173 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2174 | } |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2175 | |
| 2176 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 2177 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
| 2178 | chan->tx_win); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2179 | break; |
| 2180 | |
| 2181 | case L2CAP_MODE_STREAMING: |
| 2182 | rfc.mode = L2CAP_MODE_STREAMING; |
| 2183 | rfc.txwin_size = 0; |
| 2184 | rfc.max_transmit = 0; |
| 2185 | rfc.retrans_timeout = 0; |
| 2186 | rfc.monitor_timeout = 0; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2187 | |
| 2188 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
| 2189 | L2CAP_EXT_HDR_SIZE - |
| 2190 | L2CAP_SDULEN_SIZE - |
| 2191 | L2CAP_FCS_SIZE); |
| 2192 | rfc.max_pdu_size = cpu_to_le16(size); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 2193 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 2194 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 2195 | (unsigned long) &rfc); |
| 2196 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2197 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 2198 | l2cap_add_opt_efs(&ptr, chan); |
| 2199 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2200 | if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2201 | break; |
| 2202 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2203 | if (chan->fcs == L2CAP_FCS_NONE || |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2204 | test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2205 | chan->fcs = L2CAP_FCS_NONE; |
| 2206 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2207 | } |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 2208 | break; |
| 2209 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2211 | req->dcid = cpu_to_le16(chan->dcid); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2212 | req->flags = cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | |
| 2214 | return ptr - data; |
| 2215 | } |
| 2216 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2217 | static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2219 | struct l2cap_conf_rsp *rsp = data; |
| 2220 | void *ptr = rsp->data; |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2221 | void *req = chan->conf_req; |
| 2222 | int len = chan->conf_len; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2223 | int type, hint, olen; |
| 2224 | unsigned long val; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 2225 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2226 | struct l2cap_conf_efs efs; |
| 2227 | u8 remote_efs = 0; |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 2228 | u16 mtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2229 | u16 result = L2CAP_CONF_SUCCESS; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2230 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2232 | BT_DBG("chan %p", chan); |
Marcel Holtmann | 820ae1b | 2006-11-18 22:15:00 +0100 | [diff] [blame] | 2233 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2234 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 2235 | len -= l2cap_get_conf_opt(&req, &type, &olen, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | |
Gustavo F. Padovan | 589d274 | 2009-04-20 01:31:07 -0300 | [diff] [blame] | 2237 | hint = type & L2CAP_CONF_HINT; |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 2238 | type &= L2CAP_CONF_MASK; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2239 | |
| 2240 | switch (type) { |
| 2241 | case L2CAP_CONF_MTU: |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 2242 | mtu = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2243 | break; |
| 2244 | |
| 2245 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2246 | chan->flush_to = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2247 | break; |
| 2248 | |
| 2249 | case L2CAP_CONF_QOS: |
| 2250 | break; |
| 2251 | |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 2252 | case L2CAP_CONF_RFC: |
| 2253 | if (olen == sizeof(rfc)) |
| 2254 | memcpy(&rfc, (void *) val, olen); |
| 2255 | break; |
| 2256 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2257 | case L2CAP_CONF_FCS: |
| 2258 | if (val == L2CAP_FCS_NONE) |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2259 | set_bit(CONF_NO_FCS_RECV, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2260 | break; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2261 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2262 | case L2CAP_CONF_EFS: |
| 2263 | remote_efs = 1; |
| 2264 | if (olen == sizeof(efs)) |
| 2265 | memcpy(&efs, (void *) val, olen); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2266 | break; |
| 2267 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2268 | case L2CAP_CONF_EWS: |
| 2269 | if (!enable_hs) |
| 2270 | return -ECONNREFUSED; |
| 2271 | |
| 2272 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
| 2273 | set_bit(CONF_EWS_RECV, &chan->conf_state); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2274 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2275 | chan->remote_tx_win = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2276 | break; |
| 2277 | |
| 2278 | default: |
| 2279 | if (hint) |
| 2280 | break; |
| 2281 | |
| 2282 | result = L2CAP_CONF_UNKNOWN; |
| 2283 | *((u8 *) ptr++) = type; |
| 2284 | break; |
| 2285 | } |
| 2286 | } |
| 2287 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2288 | if (chan->num_conf_rsp || chan->num_conf_req > 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2289 | goto done; |
| 2290 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2291 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2292 | case L2CAP_MODE_STREAMING: |
| 2293 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2294 | if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2295 | chan->mode = l2cap_select_mode(rfc.mode, |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2296 | chan->conn->feat_mask); |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 2297 | break; |
| 2298 | } |
| 2299 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2300 | if (remote_efs) { |
| 2301 | if (__l2cap_efs_supported(chan)) |
| 2302 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 2303 | else |
| 2304 | return -ECONNREFUSED; |
| 2305 | } |
| 2306 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2307 | if (chan->mode != rfc.mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2308 | return -ECONNREFUSED; |
Gustavo F. Padovan | 742e519 | 2010-06-08 19:09:48 -0300 | [diff] [blame] | 2309 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2310 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2314 | if (chan->mode != rfc.mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2315 | result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2316 | rfc.mode = chan->mode; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2317 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2318 | if (chan->num_conf_rsp == 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2319 | return -ECONNREFUSED; |
| 2320 | |
| 2321 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2322 | sizeof(rfc), (unsigned long) &rfc); |
| 2323 | } |
| 2324 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2325 | if (result == L2CAP_CONF_SUCCESS) { |
| 2326 | /* Configure output options and let the other side know |
| 2327 | * which ones we don't like. */ |
| 2328 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2329 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
| 2330 | result = L2CAP_CONF_UNACCEPT; |
| 2331 | else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2332 | chan->omtu = mtu; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2333 | set_bit(CONF_MTU_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2334 | } |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2335 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2336 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2337 | if (remote_efs) { |
| 2338 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
| 2339 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 2340 | efs.stype != chan->local_stype) { |
| 2341 | |
| 2342 | result = L2CAP_CONF_UNACCEPT; |
| 2343 | |
| 2344 | if (chan->num_conf_req >= 1) |
| 2345 | return -ECONNREFUSED; |
| 2346 | |
| 2347 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 2348 | sizeof(efs), |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2349 | (unsigned long) &efs); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2350 | } else { |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 2351 | /* Send PENDING Conf Rsp */ |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2352 | result = L2CAP_CONF_PENDING; |
| 2353 | set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2354 | } |
| 2355 | } |
| 2356 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2357 | switch (rfc.mode) { |
| 2358 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2359 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2360 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2361 | break; |
| 2362 | |
| 2363 | case L2CAP_MODE_ERTM: |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2364 | if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) |
| 2365 | chan->remote_tx_win = rfc.txwin_size; |
| 2366 | else |
| 2367 | rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; |
| 2368 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 2369 | chan->remote_max_tx = rfc.max_transmit; |
Mat Martineau | 86b1b26 | 2010-08-05 15:54:22 -0700 | [diff] [blame] | 2370 | |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2371 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
| 2372 | chan->conn->mtu - |
| 2373 | L2CAP_EXT_HDR_SIZE - |
| 2374 | L2CAP_SDULEN_SIZE - |
| 2375 | L2CAP_FCS_SIZE); |
| 2376 | rfc.max_pdu_size = cpu_to_le16(size); |
| 2377 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2378 | |
Gustavo F. Padovan | 10467e9 | 2010-05-01 16:15:40 -0300 | [diff] [blame] | 2379 | rfc.retrans_timeout = |
| 2380 | le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO); |
| 2381 | rfc.monitor_timeout = |
| 2382 | le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2383 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2384 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 2385 | |
| 2386 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2387 | sizeof(rfc), (unsigned long) &rfc); |
| 2388 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2389 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 2390 | chan->remote_id = efs.id; |
| 2391 | chan->remote_stype = efs.stype; |
| 2392 | chan->remote_msdu = le16_to_cpu(efs.msdu); |
| 2393 | chan->remote_flush_to = |
| 2394 | le32_to_cpu(efs.flush_to); |
| 2395 | chan->remote_acc_lat = |
| 2396 | le32_to_cpu(efs.acc_lat); |
| 2397 | chan->remote_sdu_itime = |
| 2398 | le32_to_cpu(efs.sdu_itime); |
| 2399 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
| 2400 | sizeof(efs), (unsigned long) &efs); |
| 2401 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2402 | break; |
| 2403 | |
| 2404 | case L2CAP_MODE_STREAMING: |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2405 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
| 2406 | chan->conn->mtu - |
| 2407 | L2CAP_EXT_HDR_SIZE - |
| 2408 | L2CAP_SDULEN_SIZE - |
| 2409 | L2CAP_FCS_SIZE); |
| 2410 | rfc.max_pdu_size = cpu_to_le16(size); |
| 2411 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2412 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2413 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 2414 | |
| 2415 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2416 | sizeof(rfc), (unsigned long) &rfc); |
| 2417 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2418 | break; |
| 2419 | |
| 2420 | default: |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 2421 | result = L2CAP_CONF_UNACCEPT; |
| 2422 | |
| 2423 | memset(&rfc, 0, sizeof(rfc)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2424 | rfc.mode = chan->mode; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 2425 | } |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2426 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2427 | if (result == L2CAP_CONF_SUCCESS) |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2428 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2429 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2430 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2431 | rsp->result = cpu_to_le16(result); |
| 2432 | rsp->flags = cpu_to_le16(0x0000); |
| 2433 | |
| 2434 | return ptr - data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | } |
| 2436 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2437 | static int l2cap_parse_conf_rsp(struct l2cap_chan *chan, void *rsp, int len, void *data, u16 *result) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2438 | { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2439 | struct l2cap_conf_req *req = data; |
| 2440 | void *ptr = req->data; |
| 2441 | int type, olen; |
| 2442 | unsigned long val; |
Mat Martineau | 36e999a | 2011-12-08 17:23:21 -0800 | [diff] [blame] | 2443 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 2444 | struct l2cap_conf_efs efs; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2445 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2446 | 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] | 2447 | |
| 2448 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 2449 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 2450 | |
| 2451 | switch (type) { |
| 2452 | case L2CAP_CONF_MTU: |
| 2453 | if (val < L2CAP_DEFAULT_MIN_MTU) { |
| 2454 | *result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2455 | chan->imtu = L2CAP_DEFAULT_MIN_MTU; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2456 | } else |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2457 | chan->imtu = val; |
| 2458 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2459 | break; |
| 2460 | |
| 2461 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2462 | chan->flush_to = val; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2463 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2464 | 2, chan->flush_to); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2465 | break; |
| 2466 | |
| 2467 | case L2CAP_CONF_RFC: |
| 2468 | if (olen == sizeof(rfc)) |
| 2469 | memcpy(&rfc, (void *)val, olen); |
| 2470 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2471 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2472 | rfc.mode != chan->mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2473 | return -ECONNREFUSED; |
| 2474 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2475 | chan->fcs = 0; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2476 | |
| 2477 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2478 | sizeof(rfc), (unsigned long) &rfc); |
| 2479 | break; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2480 | |
| 2481 | case L2CAP_CONF_EWS: |
| 2482 | chan->tx_win = min_t(u16, val, |
| 2483 | L2CAP_DEFAULT_EXT_WINDOW); |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 2484 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
| 2485 | chan->tx_win); |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2486 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 2487 | |
| 2488 | case L2CAP_CONF_EFS: |
| 2489 | if (olen == sizeof(efs)) |
| 2490 | memcpy(&efs, (void *)val, olen); |
| 2491 | |
| 2492 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
| 2493 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 2494 | efs.stype != chan->local_stype) |
| 2495 | return -ECONNREFUSED; |
| 2496 | |
| 2497 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
| 2498 | sizeof(efs), (unsigned long) &efs); |
| 2499 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2500 | } |
| 2501 | } |
| 2502 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2503 | if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 2504 | return -ECONNREFUSED; |
| 2505 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2506 | chan->mode = rfc.mode; |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 2507 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2508 | if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2509 | switch (rfc.mode) { |
| 2510 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2511 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 2512 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 2513 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 2514 | |
| 2515 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 2516 | chan->local_msdu = le16_to_cpu(efs.msdu); |
| 2517 | chan->local_sdu_itime = |
| 2518 | le32_to_cpu(efs.sdu_itime); |
| 2519 | chan->local_acc_lat = le32_to_cpu(efs.acc_lat); |
| 2520 | chan->local_flush_to = |
| 2521 | le32_to_cpu(efs.flush_to); |
| 2522 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2523 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 2524 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2525 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2526 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2527 | } |
| 2528 | } |
| 2529 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2530 | req->dcid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2531 | req->flags = cpu_to_le16(0x0000); |
| 2532 | |
| 2533 | return ptr - data; |
| 2534 | } |
| 2535 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2536 | static int l2cap_build_conf_rsp(struct l2cap_chan *chan, void *data, u16 result, u16 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | { |
| 2538 | struct l2cap_conf_rsp *rsp = data; |
| 2539 | void *ptr = rsp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2541 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2543 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2544 | rsp->result = cpu_to_le16(result); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2545 | rsp->flags = cpu_to_le16(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | |
| 2547 | return ptr - data; |
| 2548 | } |
| 2549 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2550 | void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2551 | { |
| 2552 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2553 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2554 | u8 buf[128]; |
| 2555 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2556 | rsp.scid = cpu_to_le16(chan->dcid); |
| 2557 | rsp.dcid = cpu_to_le16(chan->scid); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2558 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 2559 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 2560 | l2cap_send_cmd(conn, chan->ident, |
| 2561 | L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
| 2562 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2563 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2564 | return; |
| 2565 | |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2566 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
| 2567 | l2cap_build_conf_req(chan, buf), buf); |
| 2568 | chan->num_conf_req++; |
| 2569 | } |
| 2570 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2571 | 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] | 2572 | { |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2573 | int type, olen; |
| 2574 | unsigned long val; |
| 2575 | struct l2cap_conf_rfc rfc; |
| 2576 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2577 | BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2578 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2579 | if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2580 | return; |
| 2581 | |
| 2582 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 2583 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 2584 | |
| 2585 | switch (type) { |
| 2586 | case L2CAP_CONF_RFC: |
| 2587 | if (olen == sizeof(rfc)) |
| 2588 | memcpy(&rfc, (void *)val, olen); |
| 2589 | goto done; |
| 2590 | } |
| 2591 | } |
| 2592 | |
Mat Martineau | 36e999a | 2011-12-08 17:23:21 -0800 | [diff] [blame] | 2593 | /* Use sane default values in case a misbehaving remote device |
| 2594 | * did not send an RFC option. |
| 2595 | */ |
| 2596 | rfc.mode = chan->mode; |
| 2597 | rfc.retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); |
| 2598 | rfc.monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); |
| 2599 | rfc.max_pdu_size = cpu_to_le16(chan->imtu); |
| 2600 | |
| 2601 | BT_ERR("Expected RFC option was not found, using defaults"); |
| 2602 | |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2603 | done: |
| 2604 | switch (rfc.mode) { |
| 2605 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2606 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 2607 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 2608 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2609 | break; |
| 2610 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2611 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2612 | } |
| 2613 | } |
| 2614 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2615 | static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2616 | { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 2617 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2618 | |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 2619 | if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2620 | return 0; |
| 2621 | |
| 2622 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && |
| 2623 | cmd->ident == conn->info_ident) { |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 2624 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2625 | |
| 2626 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 2627 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2628 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2629 | l2cap_conn_start(conn); |
| 2630 | } |
| 2631 | |
| 2632 | return 0; |
| 2633 | } |
| 2634 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2636 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; |
| 2638 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2639 | struct l2cap_chan *chan = NULL, *pchan; |
Nathan Holstein | d793fe8 | 2010-10-15 11:54:02 -0400 | [diff] [blame] | 2640 | struct sock *parent, *sk = NULL; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2641 | int result, status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | |
| 2643 | u16 dcid = 0, scid = __le16_to_cpu(req->scid); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2644 | __le16 psm = req->psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | |
| 2646 | BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid); |
| 2647 | |
| 2648 | /* Check if we have socket listening on psm */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2649 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src); |
| 2650 | if (!pchan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2651 | result = L2CAP_CR_BAD_PSM; |
| 2652 | goto sendresp; |
| 2653 | } |
| 2654 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2655 | parent = pchan->sk; |
| 2656 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2657 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 2658 | lock_sock(parent); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 2659 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2660 | /* Check if the ACL is secure enough (if not SDP) */ |
| 2661 | if (psm != cpu_to_le16(0x0001) && |
| 2662 | !hci_conn_check_link_mode(conn->hcon)) { |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 2663 | conn->disc_reason = HCI_ERROR_AUTH_FAILURE; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2664 | result = L2CAP_CR_SEC_BLOCK; |
| 2665 | goto response; |
| 2666 | } |
| 2667 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | result = L2CAP_CR_NO_MEM; |
| 2669 | |
| 2670 | /* Check for backlog size */ |
| 2671 | if (sk_acceptq_is_full(parent)) { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2672 | BT_DBG("backlog full %d", parent->sk_ack_backlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | goto response; |
| 2674 | } |
| 2675 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 2676 | chan = pchan->ops->new_connection(pchan->data); |
| 2677 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | goto response; |
| 2679 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 2680 | sk = chan->sk; |
| 2681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | /* Check if we already have channel with that dcid */ |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2683 | if (__l2cap_get_chan_by_dcid(conn, scid)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2684 | sock_set_flag(sk, SOCK_ZAPPED); |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 2685 | chan->ops->close(chan->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | goto response; |
| 2687 | } |
| 2688 | |
| 2689 | hci_conn_hold(conn->hcon); |
| 2690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | bacpy(&bt_sk(sk)->src, conn->src); |
| 2692 | bacpy(&bt_sk(sk)->dst, conn->dst); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2693 | chan->psm = psm; |
| 2694 | chan->dcid = scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | |
Gustavo F. Padovan | d101024 | 2011-03-25 00:39:48 -0300 | [diff] [blame] | 2696 | bt_accept_enqueue(parent, sk); |
| 2697 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2698 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2699 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2700 | dcid = chan->scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2701 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 2702 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2703 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 2704 | chan->ident = cmd->ident; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2706 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 2707 | if (l2cap_chan_check_security(chan)) { |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 2708 | if (bt_sk(sk)->defer_setup) { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 2709 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 2710 | result = L2CAP_CR_PEND; |
| 2711 | status = L2CAP_CS_AUTHOR_PEND; |
| 2712 | parent->sk_data_ready(parent, 0); |
| 2713 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 2714 | __l2cap_state_change(chan, BT_CONFIG); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 2715 | result = L2CAP_CR_SUCCESS; |
| 2716 | status = L2CAP_CS_NO_INFO; |
| 2717 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2718 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 2719 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2720 | result = L2CAP_CR_PEND; |
| 2721 | status = L2CAP_CS_AUTHEN_PEND; |
| 2722 | } |
| 2723 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 2724 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2725 | result = L2CAP_CR_PEND; |
| 2726 | status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | } |
| 2728 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | response: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 2730 | release_sock(parent); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2731 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | |
| 2733 | sendresp: |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2734 | rsp.scid = cpu_to_le16(scid); |
| 2735 | rsp.dcid = cpu_to_le16(dcid); |
| 2736 | rsp.result = cpu_to_le16(result); |
| 2737 | rsp.status = cpu_to_le16(status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2739 | |
| 2740 | if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { |
| 2741 | struct l2cap_info_req info; |
| 2742 | info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 2743 | |
| 2744 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 2745 | conn->info_ident = l2cap_get_ident(conn); |
| 2746 | |
Gustavo F. Padovan | 030013d | 2011-12-20 10:57:28 -0200 | [diff] [blame] | 2747 | schedule_delayed_work(&conn->info_timer, |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2748 | msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); |
| 2749 | |
| 2750 | l2cap_send_cmd(conn, conn->info_ident, |
| 2751 | L2CAP_INFO_REQ, sizeof(info), &info); |
| 2752 | } |
| 2753 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2754 | if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2755 | result == L2CAP_CR_SUCCESS) { |
| 2756 | u8 buf[128]; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2757 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2758 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2759 | l2cap_build_conf_req(chan, buf), buf); |
| 2760 | chan->num_conf_req++; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2761 | } |
| 2762 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | return 0; |
| 2764 | } |
| 2765 | |
| 2766 | static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2767 | { |
| 2768 | struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; |
| 2769 | u16 scid, dcid, result, status; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2770 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | u8 req[128]; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2772 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | |
| 2774 | scid = __le16_to_cpu(rsp->scid); |
| 2775 | dcid = __le16_to_cpu(rsp->dcid); |
| 2776 | result = __le16_to_cpu(rsp->result); |
| 2777 | status = __le16_to_cpu(rsp->status); |
| 2778 | |
Andrei Emeltchenko | 1b009c9 | 2012-02-21 12:54:54 +0200 | [diff] [blame] | 2779 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", |
| 2780 | dcid, scid, result, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2782 | mutex_lock(&conn->chan_lock); |
| 2783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | if (scid) { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2785 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 2786 | if (!chan) { |
| 2787 | err = -EFAULT; |
| 2788 | goto unlock; |
| 2789 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | } else { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2791 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 2792 | if (!chan) { |
| 2793 | err = -EFAULT; |
| 2794 | goto unlock; |
| 2795 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | } |
| 2797 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2798 | err = 0; |
| 2799 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2800 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2801 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | switch (result) { |
| 2803 | case L2CAP_CR_SUCCESS: |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 2804 | l2cap_state_change(chan, BT_CONFIG); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 2805 | chan->ident = 0; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2806 | chan->dcid = dcid; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2807 | clear_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 2808 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2809 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2810 | break; |
| 2811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2813 | l2cap_build_conf_req(chan, req), req); |
| 2814 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2815 | break; |
| 2816 | |
| 2817 | case L2CAP_CR_PEND: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2818 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | break; |
| 2820 | |
| 2821 | default: |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2822 | l2cap_chan_del(chan, ECONNREFUSED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | break; |
| 2824 | } |
| 2825 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2826 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2827 | |
| 2828 | unlock: |
| 2829 | mutex_unlock(&conn->chan_lock); |
| 2830 | |
| 2831 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | } |
| 2833 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2834 | static inline void set_default_fcs(struct l2cap_chan *chan) |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 2835 | { |
| 2836 | /* FCS is enabled only in ERTM or streaming mode, if one or both |
| 2837 | * sides request it. |
| 2838 | */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2839 | if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2840 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2841 | else if (!test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2842 | chan->fcs = L2CAP_FCS_CRC16; |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 2843 | } |
| 2844 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2845 | static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | { |
| 2847 | struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; |
| 2848 | u16 dcid, flags; |
| 2849 | u8 rsp[64]; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2850 | struct l2cap_chan *chan; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2851 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | |
| 2853 | dcid = __le16_to_cpu(req->dcid); |
| 2854 | flags = __le16_to_cpu(req->flags); |
| 2855 | |
| 2856 | BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); |
| 2857 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2858 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2859 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | return -ENOENT; |
| 2861 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2862 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2863 | |
David S. Miller | 033b114 | 2011-07-21 13:38:42 -0700 | [diff] [blame] | 2864 | if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 2865 | struct l2cap_cmd_rej_cid rej; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 2866 | |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 2867 | rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID); |
| 2868 | rej.scid = cpu_to_le16(chan->scid); |
| 2869 | rej.dcid = cpu_to_le16(chan->dcid); |
| 2870 | |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 2871 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
| 2872 | sizeof(rej), &rej); |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 2873 | goto unlock; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 2874 | } |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 2875 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2876 | /* Reject if config buffer is too small. */ |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2877 | len = cmd_len - sizeof(*req); |
Dan Rosenberg | 7ac2881 | 2011-06-24 08:38:05 -0400 | [diff] [blame] | 2878 | if (len < 0 || chan->conf_len + len > sizeof(chan->conf_req)) { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2879 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2880 | l2cap_build_conf_rsp(chan, rsp, |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2881 | L2CAP_CONF_REJECT, flags), rsp); |
| 2882 | goto unlock; |
| 2883 | } |
| 2884 | |
| 2885 | /* Store config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2886 | memcpy(chan->conf_req + chan->conf_len, req->data, len); |
| 2887 | chan->conf_len += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2888 | |
| 2889 | if (flags & 0x0001) { |
| 2890 | /* Incomplete config. Send empty response. */ |
| 2891 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2892 | l2cap_build_conf_rsp(chan, rsp, |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2893 | L2CAP_CONF_SUCCESS, 0x0001), rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | goto unlock; |
| 2895 | } |
| 2896 | |
| 2897 | /* Complete config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2898 | len = l2cap_parse_conf_req(chan, rsp); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2899 | if (len < 0) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 2900 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2901 | goto unlock; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2902 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2903 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2904 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2905 | chan->num_conf_rsp++; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2906 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2907 | /* Reset config buffer. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2908 | chan->conf_len = 0; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2909 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2910 | if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 2911 | goto unlock; |
| 2912 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2913 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2914 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2915 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 2916 | l2cap_state_change(chan, BT_CONNECTED); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2917 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2918 | chan->next_tx_seq = 0; |
| 2919 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2920 | skb_queue_head_init(&chan->tx_q); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2921 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2922 | l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2923 | |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 2924 | l2cap_chan_ready(chan); |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 2925 | goto unlock; |
| 2926 | } |
| 2927 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2928 | if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2929 | u8 buf[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2931 | l2cap_build_conf_req(chan, buf), buf); |
| 2932 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | } |
| 2934 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2935 | /* Got Conf Rsp PENDING from remote side and asume we sent |
| 2936 | Conf Rsp PENDING in the code above */ |
| 2937 | if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && |
| 2938 | test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
| 2939 | |
| 2940 | /* check compatibility */ |
| 2941 | |
| 2942 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
| 2943 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
| 2944 | |
| 2945 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 2946 | l2cap_build_conf_rsp(chan, rsp, |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2947 | L2CAP_CONF_SUCCESS, 0x0000), rsp); |
| 2948 | } |
| 2949 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | unlock: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2951 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | return 0; |
| 2953 | } |
| 2954 | |
| 2955 | static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2956 | { |
| 2957 | struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; |
| 2958 | u16 scid, flags, result; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2959 | struct l2cap_chan *chan; |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2960 | int len = cmd->len - sizeof(*rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2961 | |
| 2962 | scid = __le16_to_cpu(rsp->scid); |
| 2963 | flags = __le16_to_cpu(rsp->flags); |
| 2964 | result = __le16_to_cpu(rsp->result); |
| 2965 | |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 2966 | BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", |
| 2967 | scid, flags, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2969 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2970 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | return 0; |
| 2972 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2973 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | switch (result) { |
| 2976 | case L2CAP_CONF_SUCCESS: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2977 | l2cap_conf_rfc_get(chan, rsp->data, len); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2978 | clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | break; |
| 2980 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2981 | case L2CAP_CONF_PENDING: |
| 2982 | set_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
| 2983 | |
| 2984 | if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
| 2985 | char buf[64]; |
| 2986 | |
| 2987 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
| 2988 | buf, &result); |
| 2989 | if (len < 0) { |
| 2990 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
| 2991 | goto done; |
| 2992 | } |
| 2993 | |
| 2994 | /* check compatibility */ |
| 2995 | |
| 2996 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
| 2997 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
| 2998 | |
| 2999 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3000 | l2cap_build_conf_rsp(chan, buf, |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3001 | L2CAP_CONF_SUCCESS, 0x0000), buf); |
| 3002 | } |
| 3003 | goto done; |
| 3004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | case L2CAP_CONF_UNACCEPT: |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3006 | if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3007 | char req[64]; |
| 3008 | |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 3009 | if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3010 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 3011 | goto done; |
| 3012 | } |
| 3013 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3014 | /* throw out any old stored conf requests */ |
| 3015 | result = L2CAP_CONF_SUCCESS; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 3016 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
| 3017 | req, &result); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3018 | if (len < 0) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3019 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3020 | goto done; |
| 3021 | } |
| 3022 | |
| 3023 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 3024 | L2CAP_CONF_REQ, len, req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3025 | chan->num_conf_req++; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3026 | if (result != L2CAP_CONF_SUCCESS) |
| 3027 | goto done; |
| 3028 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3029 | } |
| 3030 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3031 | default: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3032 | l2cap_chan_set_err(chan, ECONNRESET); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 3033 | |
Andrzej Kaczmarek | b83ddfe | 2012-01-04 12:10:42 +0100 | [diff] [blame] | 3034 | __set_chan_timer(chan, |
| 3035 | msecs_to_jiffies(L2CAP_DISC_REJ_TIMEOUT)); |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3036 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | goto done; |
| 3038 | } |
| 3039 | |
| 3040 | if (flags & 0x01) |
| 3041 | goto done; |
| 3042 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3043 | set_bit(CONF_INPUT_DONE, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3045 | if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3046 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3047 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 3048 | l2cap_state_change(chan, BT_CONNECTED); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3049 | chan->next_tx_seq = 0; |
| 3050 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 3051 | skb_queue_head_init(&chan->tx_q); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3052 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3053 | l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3054 | |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 3055 | l2cap_chan_ready(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3059 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | return 0; |
| 3061 | } |
| 3062 | |
| 3063 | static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3064 | { |
| 3065 | struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; |
| 3066 | struct l2cap_disconn_rsp rsp; |
| 3067 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3068 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3069 | struct sock *sk; |
| 3070 | |
| 3071 | scid = __le16_to_cpu(req->scid); |
| 3072 | dcid = __le16_to_cpu(req->dcid); |
| 3073 | |
| 3074 | BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); |
| 3075 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3076 | mutex_lock(&conn->chan_lock); |
| 3077 | |
| 3078 | chan = __l2cap_get_chan_by_scid(conn, dcid); |
| 3079 | if (!chan) { |
| 3080 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3082 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3084 | l2cap_chan_lock(chan); |
| 3085 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3086 | sk = chan->sk; |
| 3087 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3088 | rsp.dcid = cpu_to_le16(chan->scid); |
| 3089 | rsp.scid = cpu_to_le16(chan->dcid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); |
| 3091 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3092 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3093 | sk->sk_shutdown = SHUTDOWN_MASK; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3094 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3096 | l2cap_chan_del(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3097 | |
| 3098 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3099 | |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 3100 | chan->ops->close(chan->data); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3101 | |
| 3102 | mutex_unlock(&conn->chan_lock); |
| 3103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | return 0; |
| 3105 | } |
| 3106 | |
| 3107 | static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3108 | { |
| 3109 | struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; |
| 3110 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3111 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | |
| 3113 | scid = __le16_to_cpu(rsp->scid); |
| 3114 | dcid = __le16_to_cpu(rsp->dcid); |
| 3115 | |
| 3116 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); |
| 3117 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3118 | mutex_lock(&conn->chan_lock); |
| 3119 | |
| 3120 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 3121 | if (!chan) { |
| 3122 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3124 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3126 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3127 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3128 | l2cap_chan_del(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3129 | |
| 3130 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 3132 | chan->ops->close(chan->data); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3133 | |
| 3134 | mutex_unlock(&conn->chan_lock); |
| 3135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3136 | return 0; |
| 3137 | } |
| 3138 | |
| 3139 | static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3140 | { |
| 3141 | struct l2cap_info_req *req = (struct l2cap_info_req *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3142 | u16 type; |
| 3143 | |
| 3144 | type = __le16_to_cpu(req->type); |
| 3145 | |
| 3146 | BT_DBG("type 0x%4.4x", type); |
| 3147 | |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 3148 | if (type == L2CAP_IT_FEAT_MASK) { |
| 3149 | u8 buf[8]; |
Marcel Holtmann | 44dd46d | 2009-05-02 19:09:01 -0700 | [diff] [blame] | 3150 | u32 feat_mask = l2cap_feat_mask; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 3151 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
| 3152 | rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 3153 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 3154 | if (!disable_ertm) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3155 | feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING |
| 3156 | | L2CAP_FEAT_FCS; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 3157 | if (enable_hs) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3158 | feat_mask |= L2CAP_FEAT_EXT_FLOW |
| 3159 | | L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 3160 | |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 3161 | put_unaligned_le32(feat_mask, rsp->data); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 3162 | l2cap_send_cmd(conn, cmd->ident, |
| 3163 | L2CAP_INFO_RSP, sizeof(buf), buf); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3164 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
| 3165 | u8 buf[12]; |
| 3166 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 3167 | |
| 3168 | if (enable_hs) |
| 3169 | l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; |
| 3170 | else |
| 3171 | l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; |
| 3172 | |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3173 | rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 3174 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Andrei Emeltchenko | c6337ea | 2011-10-20 17:02:44 +0300 | [diff] [blame] | 3175 | memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan)); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3176 | l2cap_send_cmd(conn, cmd->ident, |
| 3177 | L2CAP_INFO_RSP, sizeof(buf), buf); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 3178 | } else { |
| 3179 | struct l2cap_info_rsp rsp; |
| 3180 | rsp.type = cpu_to_le16(type); |
| 3181 | rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); |
| 3182 | l2cap_send_cmd(conn, cmd->ident, |
| 3183 | L2CAP_INFO_RSP, sizeof(rsp), &rsp); |
| 3184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | |
| 3186 | return 0; |
| 3187 | } |
| 3188 | |
| 3189 | static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3190 | { |
| 3191 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; |
| 3192 | u16 type, result; |
| 3193 | |
| 3194 | type = __le16_to_cpu(rsp->type); |
| 3195 | result = __le16_to_cpu(rsp->result); |
| 3196 | |
| 3197 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); |
| 3198 | |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 3199 | /* L2CAP Info req/rsp are unbound to channels, add extra checks */ |
| 3200 | if (cmd->ident != conn->info_ident || |
| 3201 | conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
| 3202 | return 0; |
| 3203 | |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 3204 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3205 | |
Ville Tervo | adb08ed | 2010-08-04 09:43:33 +0300 | [diff] [blame] | 3206 | if (result != L2CAP_IR_SUCCESS) { |
| 3207 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 3208 | conn->info_ident = 0; |
| 3209 | |
| 3210 | l2cap_conn_start(conn); |
| 3211 | |
| 3212 | return 0; |
| 3213 | } |
| 3214 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3215 | if (type == L2CAP_IT_FEAT_MASK) { |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 3216 | conn->feat_mask = get_unaligned_le32(rsp->data); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3217 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 3218 | if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3219 | struct l2cap_info_req req; |
| 3220 | req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 3221 | |
| 3222 | conn->info_ident = l2cap_get_ident(conn); |
| 3223 | |
| 3224 | l2cap_send_cmd(conn, conn->info_ident, |
| 3225 | L2CAP_INFO_REQ, sizeof(req), &req); |
| 3226 | } else { |
| 3227 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 3228 | conn->info_ident = 0; |
| 3229 | |
| 3230 | l2cap_conn_start(conn); |
| 3231 | } |
| 3232 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3233 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3234 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3235 | |
| 3236 | l2cap_conn_start(conn); |
| 3237 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3239 | return 0; |
| 3240 | } |
| 3241 | |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 3242 | static inline int l2cap_create_channel_req(struct l2cap_conn *conn, |
| 3243 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3244 | void *data) |
| 3245 | { |
| 3246 | struct l2cap_create_chan_req *req = data; |
| 3247 | struct l2cap_create_chan_rsp rsp; |
| 3248 | u16 psm, scid; |
| 3249 | |
| 3250 | if (cmd_len != sizeof(*req)) |
| 3251 | return -EPROTO; |
| 3252 | |
| 3253 | if (!enable_hs) |
| 3254 | return -EINVAL; |
| 3255 | |
| 3256 | psm = le16_to_cpu(req->psm); |
| 3257 | scid = le16_to_cpu(req->scid); |
| 3258 | |
| 3259 | BT_DBG("psm %d, scid %d, amp_id %d", psm, scid, req->amp_id); |
| 3260 | |
| 3261 | /* Placeholder: Always reject */ |
| 3262 | rsp.dcid = 0; |
| 3263 | rsp.scid = cpu_to_le16(scid); |
| 3264 | rsp.result = L2CAP_CR_NO_MEM; |
| 3265 | rsp.status = L2CAP_CS_NO_INFO; |
| 3266 | |
| 3267 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, |
| 3268 | sizeof(rsp), &rsp); |
| 3269 | |
| 3270 | return 0; |
| 3271 | } |
| 3272 | |
| 3273 | static inline int l2cap_create_channel_rsp(struct l2cap_conn *conn, |
| 3274 | struct l2cap_cmd_hdr *cmd, void *data) |
| 3275 | { |
| 3276 | BT_DBG("conn %p", conn); |
| 3277 | |
| 3278 | return l2cap_connect_rsp(conn, cmd, data); |
| 3279 | } |
| 3280 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 3281 | static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident, |
| 3282 | u16 icid, u16 result) |
| 3283 | { |
| 3284 | struct l2cap_move_chan_rsp rsp; |
| 3285 | |
| 3286 | BT_DBG("icid %d, result %d", icid, result); |
| 3287 | |
| 3288 | rsp.icid = cpu_to_le16(icid); |
| 3289 | rsp.result = cpu_to_le16(result); |
| 3290 | |
| 3291 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_RSP, sizeof(rsp), &rsp); |
| 3292 | } |
| 3293 | |
| 3294 | static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn, |
| 3295 | struct l2cap_chan *chan, u16 icid, u16 result) |
| 3296 | { |
| 3297 | struct l2cap_move_chan_cfm cfm; |
| 3298 | u8 ident; |
| 3299 | |
| 3300 | BT_DBG("icid %d, result %d", icid, result); |
| 3301 | |
| 3302 | ident = l2cap_get_ident(conn); |
| 3303 | if (chan) |
| 3304 | chan->ident = ident; |
| 3305 | |
| 3306 | cfm.icid = cpu_to_le16(icid); |
| 3307 | cfm.result = cpu_to_le16(result); |
| 3308 | |
| 3309 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM, sizeof(cfm), &cfm); |
| 3310 | } |
| 3311 | |
| 3312 | static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident, |
| 3313 | u16 icid) |
| 3314 | { |
| 3315 | struct l2cap_move_chan_cfm_rsp rsp; |
| 3316 | |
| 3317 | BT_DBG("icid %d", icid); |
| 3318 | |
| 3319 | rsp.icid = cpu_to_le16(icid); |
| 3320 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); |
| 3321 | } |
| 3322 | |
| 3323 | static inline int l2cap_move_channel_req(struct l2cap_conn *conn, |
| 3324 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) |
| 3325 | { |
| 3326 | struct l2cap_move_chan_req *req = data; |
| 3327 | u16 icid = 0; |
| 3328 | u16 result = L2CAP_MR_NOT_ALLOWED; |
| 3329 | |
| 3330 | if (cmd_len != sizeof(*req)) |
| 3331 | return -EPROTO; |
| 3332 | |
| 3333 | icid = le16_to_cpu(req->icid); |
| 3334 | |
| 3335 | BT_DBG("icid %d, dest_amp_id %d", icid, req->dest_amp_id); |
| 3336 | |
| 3337 | if (!enable_hs) |
| 3338 | return -EINVAL; |
| 3339 | |
| 3340 | /* Placeholder: Always refuse */ |
| 3341 | l2cap_send_move_chan_rsp(conn, cmd->ident, icid, result); |
| 3342 | |
| 3343 | return 0; |
| 3344 | } |
| 3345 | |
| 3346 | static inline int l2cap_move_channel_rsp(struct l2cap_conn *conn, |
| 3347 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) |
| 3348 | { |
| 3349 | struct l2cap_move_chan_rsp *rsp = data; |
| 3350 | u16 icid, result; |
| 3351 | |
| 3352 | if (cmd_len != sizeof(*rsp)) |
| 3353 | return -EPROTO; |
| 3354 | |
| 3355 | icid = le16_to_cpu(rsp->icid); |
| 3356 | result = le16_to_cpu(rsp->result); |
| 3357 | |
| 3358 | BT_DBG("icid %d, result %d", icid, result); |
| 3359 | |
| 3360 | /* Placeholder: Always unconfirmed */ |
| 3361 | l2cap_send_move_chan_cfm(conn, NULL, icid, L2CAP_MC_UNCONFIRMED); |
| 3362 | |
| 3363 | return 0; |
| 3364 | } |
| 3365 | |
| 3366 | static inline int l2cap_move_channel_confirm(struct l2cap_conn *conn, |
| 3367 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) |
| 3368 | { |
| 3369 | struct l2cap_move_chan_cfm *cfm = data; |
| 3370 | u16 icid, result; |
| 3371 | |
| 3372 | if (cmd_len != sizeof(*cfm)) |
| 3373 | return -EPROTO; |
| 3374 | |
| 3375 | icid = le16_to_cpu(cfm->icid); |
| 3376 | result = le16_to_cpu(cfm->result); |
| 3377 | |
| 3378 | BT_DBG("icid %d, result %d", icid, result); |
| 3379 | |
| 3380 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 3381 | |
| 3382 | return 0; |
| 3383 | } |
| 3384 | |
| 3385 | static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, |
| 3386 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) |
| 3387 | { |
| 3388 | struct l2cap_move_chan_cfm_rsp *rsp = data; |
| 3389 | u16 icid; |
| 3390 | |
| 3391 | if (cmd_len != sizeof(*rsp)) |
| 3392 | return -EPROTO; |
| 3393 | |
| 3394 | icid = le16_to_cpu(rsp->icid); |
| 3395 | |
| 3396 | BT_DBG("icid %d", icid); |
| 3397 | |
| 3398 | return 0; |
| 3399 | } |
| 3400 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 3401 | static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency, |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3402 | u16 to_multiplier) |
| 3403 | { |
| 3404 | u16 max_latency; |
| 3405 | |
| 3406 | if (min > max || min < 6 || max > 3200) |
| 3407 | return -EINVAL; |
| 3408 | |
| 3409 | if (to_multiplier < 10 || to_multiplier > 3200) |
| 3410 | return -EINVAL; |
| 3411 | |
| 3412 | if (max >= to_multiplier * 8) |
| 3413 | return -EINVAL; |
| 3414 | |
| 3415 | max_latency = (to_multiplier * 8 / max) - 1; |
| 3416 | if (latency > 499 || latency > max_latency) |
| 3417 | return -EINVAL; |
| 3418 | |
| 3419 | return 0; |
| 3420 | } |
| 3421 | |
| 3422 | static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, |
| 3423 | struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3424 | { |
| 3425 | struct hci_conn *hcon = conn->hcon; |
| 3426 | struct l2cap_conn_param_update_req *req; |
| 3427 | struct l2cap_conn_param_update_rsp rsp; |
| 3428 | u16 min, max, latency, to_multiplier, cmd_len; |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 3429 | int err; |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3430 | |
| 3431 | if (!(hcon->link_mode & HCI_LM_MASTER)) |
| 3432 | return -EINVAL; |
| 3433 | |
| 3434 | cmd_len = __le16_to_cpu(cmd->len); |
| 3435 | if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) |
| 3436 | return -EPROTO; |
| 3437 | |
| 3438 | req = (struct l2cap_conn_param_update_req *) data; |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 3439 | min = __le16_to_cpu(req->min); |
| 3440 | max = __le16_to_cpu(req->max); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3441 | latency = __le16_to_cpu(req->latency); |
| 3442 | to_multiplier = __le16_to_cpu(req->to_multiplier); |
| 3443 | |
| 3444 | BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", |
| 3445 | min, max, latency, to_multiplier); |
| 3446 | |
| 3447 | memset(&rsp, 0, sizeof(rsp)); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 3448 | |
| 3449 | err = l2cap_check_conn_param(min, max, latency, to_multiplier); |
| 3450 | if (err) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3451 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); |
| 3452 | else |
| 3453 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); |
| 3454 | |
| 3455 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, |
| 3456 | sizeof(rsp), &rsp); |
| 3457 | |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 3458 | if (!err) |
| 3459 | hci_le_conn_update(hcon, min, max, latency, to_multiplier); |
| 3460 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3461 | return 0; |
| 3462 | } |
| 3463 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3464 | static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, |
| 3465 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
| 3466 | { |
| 3467 | int err = 0; |
| 3468 | |
| 3469 | switch (cmd->code) { |
| 3470 | case L2CAP_COMMAND_REJ: |
| 3471 | l2cap_command_rej(conn, cmd, data); |
| 3472 | break; |
| 3473 | |
| 3474 | case L2CAP_CONN_REQ: |
| 3475 | err = l2cap_connect_req(conn, cmd, data); |
| 3476 | break; |
| 3477 | |
| 3478 | case L2CAP_CONN_RSP: |
| 3479 | err = l2cap_connect_rsp(conn, cmd, data); |
| 3480 | break; |
| 3481 | |
| 3482 | case L2CAP_CONF_REQ: |
| 3483 | err = l2cap_config_req(conn, cmd, cmd_len, data); |
| 3484 | break; |
| 3485 | |
| 3486 | case L2CAP_CONF_RSP: |
| 3487 | err = l2cap_config_rsp(conn, cmd, data); |
| 3488 | break; |
| 3489 | |
| 3490 | case L2CAP_DISCONN_REQ: |
| 3491 | err = l2cap_disconnect_req(conn, cmd, data); |
| 3492 | break; |
| 3493 | |
| 3494 | case L2CAP_DISCONN_RSP: |
| 3495 | err = l2cap_disconnect_rsp(conn, cmd, data); |
| 3496 | break; |
| 3497 | |
| 3498 | case L2CAP_ECHO_REQ: |
| 3499 | l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); |
| 3500 | break; |
| 3501 | |
| 3502 | case L2CAP_ECHO_RSP: |
| 3503 | break; |
| 3504 | |
| 3505 | case L2CAP_INFO_REQ: |
| 3506 | err = l2cap_information_req(conn, cmd, data); |
| 3507 | break; |
| 3508 | |
| 3509 | case L2CAP_INFO_RSP: |
| 3510 | err = l2cap_information_rsp(conn, cmd, data); |
| 3511 | break; |
| 3512 | |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 3513 | case L2CAP_CREATE_CHAN_REQ: |
| 3514 | err = l2cap_create_channel_req(conn, cmd, cmd_len, data); |
| 3515 | break; |
| 3516 | |
| 3517 | case L2CAP_CREATE_CHAN_RSP: |
| 3518 | err = l2cap_create_channel_rsp(conn, cmd, data); |
| 3519 | break; |
| 3520 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 3521 | case L2CAP_MOVE_CHAN_REQ: |
| 3522 | err = l2cap_move_channel_req(conn, cmd, cmd_len, data); |
| 3523 | break; |
| 3524 | |
| 3525 | case L2CAP_MOVE_CHAN_RSP: |
| 3526 | err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data); |
| 3527 | break; |
| 3528 | |
| 3529 | case L2CAP_MOVE_CHAN_CFM: |
| 3530 | err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); |
| 3531 | break; |
| 3532 | |
| 3533 | case L2CAP_MOVE_CHAN_CFM_RSP: |
| 3534 | err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); |
| 3535 | break; |
| 3536 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3537 | default: |
| 3538 | BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); |
| 3539 | err = -EINVAL; |
| 3540 | break; |
| 3541 | } |
| 3542 | |
| 3543 | return err; |
| 3544 | } |
| 3545 | |
| 3546 | static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, |
| 3547 | struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3548 | { |
| 3549 | switch (cmd->code) { |
| 3550 | case L2CAP_COMMAND_REJ: |
| 3551 | return 0; |
| 3552 | |
| 3553 | case L2CAP_CONN_PARAM_UPDATE_REQ: |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3554 | return l2cap_conn_param_update_req(conn, cmd, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3555 | |
| 3556 | case L2CAP_CONN_PARAM_UPDATE_RSP: |
| 3557 | return 0; |
| 3558 | |
| 3559 | default: |
| 3560 | BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); |
| 3561 | return -EINVAL; |
| 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | static inline void l2cap_sig_channel(struct l2cap_conn *conn, |
| 3566 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3567 | { |
| 3568 | u8 *data = skb->data; |
| 3569 | int len = skb->len; |
| 3570 | struct l2cap_cmd_hdr cmd; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3571 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3572 | |
| 3573 | l2cap_raw_recv(conn, skb); |
| 3574 | |
| 3575 | while (len >= L2CAP_CMD_HDR_SIZE) { |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3576 | u16 cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3577 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 3578 | data += L2CAP_CMD_HDR_SIZE; |
| 3579 | len -= L2CAP_CMD_HDR_SIZE; |
| 3580 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3581 | cmd_len = le16_to_cpu(cmd.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3583 | BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3585 | if (cmd_len > len || !cmd.ident) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3586 | BT_DBG("corrupted command"); |
| 3587 | break; |
| 3588 | } |
| 3589 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3590 | if (conn->hcon->type == LE_LINK) |
| 3591 | err = l2cap_le_sig_cmd(conn, &cmd, data); |
| 3592 | else |
| 3593 | err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | |
| 3595 | if (err) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3596 | struct l2cap_cmd_rej_unk rej; |
Gustavo F. Padovan | 2c6d1a2 | 2011-03-23 14:38:32 -0300 | [diff] [blame] | 3597 | |
| 3598 | BT_ERR("Wrong link type (%d)", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | |
| 3600 | /* FIXME: Map err to a valid reason */ |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3601 | rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3602 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); |
| 3603 | } |
| 3604 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3605 | data += cmd_len; |
| 3606 | len -= cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | kfree_skb(skb); |
| 3610 | } |
| 3611 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3612 | 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] | 3613 | { |
| 3614 | u16 our_fcs, rcv_fcs; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 3615 | int hdr_size; |
| 3616 | |
| 3617 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3618 | hdr_size = L2CAP_EXT_HDR_SIZE; |
| 3619 | else |
| 3620 | hdr_size = L2CAP_ENH_HDR_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3621 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3622 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 3623 | skb_trim(skb, skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3624 | rcv_fcs = get_unaligned_le16(skb->data + skb->len); |
| 3625 | our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); |
| 3626 | |
| 3627 | if (our_fcs != rcv_fcs) |
João Paulo Rechi Vita | 7a560e5 | 2010-06-22 13:56:27 -0300 | [diff] [blame] | 3628 | return -EBADMSG; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3629 | } |
| 3630 | return 0; |
| 3631 | } |
| 3632 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3633 | static inline 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] | 3634 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3635 | u32 control = 0; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3636 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3637 | chan->frames_sent = 0; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3638 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3639 | control |= __set_reqseq(chan, chan->buffer_seq); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3640 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3641 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3642 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3643 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3644 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3645 | } |
| 3646 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3647 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3648 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3649 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3650 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3651 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3652 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3653 | chan->frames_sent == 0) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3654 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3655 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3656 | } |
| 3657 | } |
| 3658 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 3659 | static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u16 tx_seq, u8 sar) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3660 | { |
| 3661 | struct sk_buff *next_skb; |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 3662 | int tx_seq_offset, next_tx_seq_offset; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3663 | |
| 3664 | bt_cb(skb)->tx_seq = tx_seq; |
| 3665 | bt_cb(skb)->sar = sar; |
| 3666 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3667 | next_skb = skb_peek(&chan->srej_q); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3668 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3669 | tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq); |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 3670 | |
Szymon Janc | 039d957 | 2011-11-16 09:32:19 +0100 | [diff] [blame] | 3671 | while (next_skb) { |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3672 | if (bt_cb(next_skb)->tx_seq == tx_seq) |
| 3673 | return -EINVAL; |
| 3674 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3675 | next_tx_seq_offset = __seq_offset(chan, |
| 3676 | bt_cb(next_skb)->tx_seq, chan->buffer_seq); |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 3677 | |
| 3678 | if (next_tx_seq_offset > tx_seq_offset) { |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3679 | __skb_queue_before(&chan->srej_q, next_skb, skb); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3680 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3681 | } |
| 3682 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3683 | if (skb_queue_is_last(&chan->srej_q, next_skb)) |
Szymon Janc | 039d957 | 2011-11-16 09:32:19 +0100 | [diff] [blame] | 3684 | next_skb = NULL; |
| 3685 | else |
| 3686 | next_skb = skb_queue_next(&chan->srej_q, next_skb); |
| 3687 | } |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3688 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3689 | __skb_queue_tail(&chan->srej_q, skb); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3690 | |
| 3691 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3692 | } |
| 3693 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3694 | static void append_skb_frag(struct sk_buff *skb, |
| 3695 | struct sk_buff *new_frag, struct sk_buff **last_frag) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3696 | { |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3697 | /* skb->len reflects data in skb as well as all fragments |
| 3698 | * skb->data_len reflects only data in fragments |
| 3699 | */ |
| 3700 | if (!skb_has_frag_list(skb)) |
| 3701 | skb_shinfo(skb)->frag_list = new_frag; |
| 3702 | |
| 3703 | new_frag->next = NULL; |
| 3704 | |
| 3705 | (*last_frag)->next = new_frag; |
| 3706 | *last_frag = new_frag; |
| 3707 | |
| 3708 | skb->len += new_frag->len; |
| 3709 | skb->data_len += new_frag->len; |
| 3710 | skb->truesize += new_frag->truesize; |
| 3711 | } |
| 3712 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3713 | static int l2cap_reassemble_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u32 control) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3714 | { |
| 3715 | int err = -EINVAL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3716 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3717 | switch (__get_ctrl_sar(chan, control)) { |
| 3718 | case L2CAP_SAR_UNSEGMENTED: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3719 | if (chan->sdu) |
| 3720 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3721 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3722 | err = chan->ops->recv(chan->data, skb); |
| 3723 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3724 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3725 | case L2CAP_SAR_START: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3726 | if (chan->sdu) |
| 3727 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3728 | |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3729 | chan->sdu_len = get_unaligned_le16(skb->data); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 3730 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3731 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3732 | if (chan->sdu_len > chan->imtu) { |
| 3733 | err = -EMSGSIZE; |
| 3734 | break; |
| 3735 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3736 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3737 | if (skb->len >= chan->sdu_len) |
| 3738 | break; |
| 3739 | |
| 3740 | chan->sdu = skb; |
| 3741 | chan->sdu_last_frag = skb; |
| 3742 | |
| 3743 | skb = NULL; |
| 3744 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3745 | break; |
| 3746 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3747 | case L2CAP_SAR_CONTINUE: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3748 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3749 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3750 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3751 | append_skb_frag(chan->sdu, skb, |
| 3752 | &chan->sdu_last_frag); |
| 3753 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3754 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3755 | if (chan->sdu->len >= chan->sdu_len) |
| 3756 | break; |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3757 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3758 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3759 | break; |
| 3760 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3761 | case L2CAP_SAR_END: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3762 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3763 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3764 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3765 | append_skb_frag(chan->sdu, skb, |
| 3766 | &chan->sdu_last_frag); |
| 3767 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3768 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3769 | if (chan->sdu->len != chan->sdu_len) |
| 3770 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3771 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3772 | err = chan->ops->recv(chan->data, chan->sdu); |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3773 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3774 | if (!err) { |
| 3775 | /* Reassembly complete */ |
| 3776 | chan->sdu = NULL; |
| 3777 | chan->sdu_last_frag = NULL; |
| 3778 | chan->sdu_len = 0; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3779 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3780 | break; |
| 3781 | } |
| 3782 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3783 | if (err) { |
| 3784 | kfree_skb(skb); |
| 3785 | kfree_skb(chan->sdu); |
| 3786 | chan->sdu = NULL; |
| 3787 | chan->sdu_last_frag = NULL; |
| 3788 | chan->sdu_len = 0; |
| 3789 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3790 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3791 | return err; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3792 | } |
| 3793 | |
Mat Martineau | 26f880d | 2011-07-07 09:39:01 -0700 | [diff] [blame] | 3794 | static void l2cap_ertm_enter_local_busy(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3795 | { |
Mat Martineau | 26f880d | 2011-07-07 09:39:01 -0700 | [diff] [blame] | 3796 | BT_DBG("chan %p, Enter local busy", chan); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3797 | |
Mat Martineau | 26f880d | 2011-07-07 09:39:01 -0700 | [diff] [blame] | 3798 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 3799 | |
Szymon Janc | 77f918b | 2012-01-11 10:59:48 +0100 | [diff] [blame] | 3800 | __set_ack_timer(chan); |
Mat Martineau | 26f880d | 2011-07-07 09:39:01 -0700 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | static void l2cap_ertm_exit_local_busy(struct l2cap_chan *chan) |
| 3804 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3805 | u32 control; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3806 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3807 | if (!test_bit(CONN_RNR_SENT, &chan->conn_state)) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3808 | goto done; |
| 3809 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3810 | control = __set_reqseq(chan, chan->buffer_seq); |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 3811 | control |= __set_ctrl_poll(chan); |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3812 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3813 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3814 | chan->retry_count = 1; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3815 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 3816 | __clear_retrans_timer(chan); |
| 3817 | __set_monitor_timer(chan); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3818 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3819 | set_bit(CONN_WAIT_F, &chan->conn_state); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3820 | |
| 3821 | done: |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3822 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 3823 | clear_bit(CONN_RNR_SENT, &chan->conn_state); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3824 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3825 | BT_DBG("chan %p, Exit local busy", chan); |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3826 | } |
| 3827 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 3828 | void l2cap_chan_busy(struct l2cap_chan *chan, int busy) |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3829 | { |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 3830 | if (chan->mode == L2CAP_MODE_ERTM) { |
| 3831 | if (busy) |
| 3832 | l2cap_ertm_enter_local_busy(chan); |
| 3833 | else |
| 3834 | l2cap_ertm_exit_local_busy(chan); |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3835 | } |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3836 | } |
| 3837 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 3838 | static void l2cap_check_srej_gap(struct l2cap_chan *chan, u16 tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3839 | { |
| 3840 | struct sk_buff *skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3841 | u32 control; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3842 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 3843 | while ((skb = skb_peek(&chan->srej_q)) && |
| 3844 | !test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 3845 | int err; |
| 3846 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3847 | if (bt_cb(skb)->tx_seq != tx_seq) |
| 3848 | break; |
| 3849 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3850 | skb = skb_dequeue(&chan->srej_q); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3851 | control = __set_ctrl_sar(chan, bt_cb(skb)->sar); |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3852 | err = l2cap_reassemble_sdu(chan, skb, control); |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 3853 | |
| 3854 | if (err < 0) { |
| 3855 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
| 3856 | break; |
| 3857 | } |
| 3858 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3859 | chan->buffer_seq_srej = __next_seq(chan, chan->buffer_seq_srej); |
| 3860 | tx_seq = __next_seq(chan, tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3861 | } |
| 3862 | } |
| 3863 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 3864 | static void l2cap_resend_srejframe(struct l2cap_chan *chan, u16 tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3865 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3866 | struct srej_list *l, *tmp; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3867 | u32 control; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3868 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3869 | list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3870 | if (l->tx_seq == tx_seq) { |
| 3871 | list_del(&l->list); |
| 3872 | kfree(l); |
| 3873 | return; |
| 3874 | } |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3875 | control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3876 | control |= __set_reqseq(chan, l->tx_seq); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3877 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3878 | list_del(&l->list); |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3879 | list_add_tail(&l->list, &chan->srej_l); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3880 | } |
| 3881 | } |
| 3882 | |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 3883 | static int l2cap_send_srejframe(struct l2cap_chan *chan, u16 tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3884 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3885 | struct srej_list *new; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3886 | u32 control; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3887 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3888 | while (tx_seq != chan->expected_tx_seq) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3889 | control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3890 | control |= __set_reqseq(chan, chan->expected_tx_seq); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3891 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3892 | |
| 3893 | new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 3894 | if (!new) |
| 3895 | return -ENOMEM; |
| 3896 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3897 | new->tx_seq = chan->expected_tx_seq; |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3898 | |
| 3899 | chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); |
| 3900 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3901 | list_add_tail(&new->list, &chan->srej_l); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3902 | } |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3903 | |
| 3904 | chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 3905 | |
| 3906 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3907 | } |
| 3908 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3909 | static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3910 | { |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 3911 | u16 tx_seq = __get_txseq(chan, rx_control); |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3912 | u16 req_seq = __get_reqseq(chan, rx_control); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3913 | u8 sar = __get_ctrl_sar(chan, rx_control); |
Gustavo F. Padovan | f6337c7 | 2010-05-10 18:32:04 -0300 | [diff] [blame] | 3914 | int tx_seq_offset, expected_tx_seq_offset; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3915 | int num_to_ack = (chan->tx_win/6) + 1; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3916 | int err = 0; |
| 3917 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3918 | BT_DBG("chan %p len %d tx_seq %d rx_control 0x%8.8x", chan, skb->len, |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3919 | tx_seq, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3920 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 3921 | if (__is_ctrl_final(chan, rx_control) && |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3922 | test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 3923 | __clear_monitor_timer(chan); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3924 | if (chan->unacked_frames > 0) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 3925 | __set_retrans_timer(chan); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3926 | clear_bit(CONN_WAIT_F, &chan->conn_state); |
Gustavo F. Padovan | 1d8f5d1 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3927 | } |
| 3928 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3929 | chan->expected_ack_seq = req_seq; |
| 3930 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 9f121a5 | 2009-10-03 02:34:38 -0300 | [diff] [blame] | 3931 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3932 | tx_seq_offset = __seq_offset(chan, tx_seq, chan->buffer_seq); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3933 | |
| 3934 | /* invalid tx_seq */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3935 | if (tx_seq_offset >= chan->tx_win) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3936 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3937 | goto drop; |
| 3938 | } |
| 3939 | |
Szymon Janc | 77f918b | 2012-01-11 10:59:48 +0100 | [diff] [blame] | 3940 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 3941 | if (!test_bit(CONN_RNR_SENT, &chan->conn_state)) |
| 3942 | l2cap_send_ack(chan); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3943 | goto drop; |
Szymon Janc | 77f918b | 2012-01-11 10:59:48 +0100 | [diff] [blame] | 3944 | } |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3945 | |
Mat Martineau | 02f1b64 | 2011-06-29 14:35:19 -0700 | [diff] [blame] | 3946 | if (tx_seq == chan->expected_tx_seq) |
| 3947 | goto expected; |
| 3948 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3949 | if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3950 | struct srej_list *first; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3951 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3952 | first = list_first_entry(&chan->srej_l, |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3953 | struct srej_list, list); |
| 3954 | if (tx_seq == first->tx_seq) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3955 | l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3956 | l2cap_check_srej_gap(chan, tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3957 | |
| 3958 | list_del(&first->list); |
| 3959 | kfree(first); |
| 3960 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3961 | if (list_empty(&chan->srej_l)) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3962 | chan->buffer_seq = chan->buffer_seq_srej; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3963 | clear_bit(CONN_SREJ_SENT, &chan->conn_state); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3964 | l2cap_send_ack(chan); |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3965 | BT_DBG("chan %p, Exit SREJ_SENT", chan); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3966 | } |
| 3967 | } else { |
| 3968 | struct srej_list *l; |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3969 | |
| 3970 | /* duplicated tx_seq */ |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3971 | if (l2cap_add_to_srej_queue(chan, skb, tx_seq, sar) < 0) |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3972 | goto drop; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3973 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3974 | list_for_each_entry(l, &chan->srej_l, list) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3975 | if (l->tx_seq == tx_seq) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3976 | l2cap_resend_srejframe(chan, tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3977 | return 0; |
| 3978 | } |
| 3979 | } |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 3980 | |
| 3981 | err = l2cap_send_srejframe(chan, tx_seq); |
| 3982 | if (err < 0) { |
| 3983 | l2cap_send_disconn_req(chan->conn, chan, -err); |
| 3984 | return err; |
| 3985 | } |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 3986 | } |
| 3987 | } else { |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3988 | expected_tx_seq_offset = __seq_offset(chan, |
| 3989 | chan->expected_tx_seq, chan->buffer_seq); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3990 | |
| 3991 | /* duplicated tx_seq */ |
| 3992 | if (tx_seq_offset < expected_tx_seq_offset) |
| 3993 | goto drop; |
| 3994 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3995 | set_bit(CONN_SREJ_SENT, &chan->conn_state); |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 3996 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3997 | BT_DBG("chan %p, Enter SREJ", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3998 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3999 | INIT_LIST_HEAD(&chan->srej_l); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 4000 | chan->buffer_seq_srej = chan->buffer_seq; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4001 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 4002 | __skb_queue_head_init(&chan->srej_q); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 4003 | l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4004 | |
Szymon Janc | 0ef3ef0 | 2012-01-11 10:59:46 +0100 | [diff] [blame] | 4005 | /* Set P-bit only if there are some I-frames to ack. */ |
| 4006 | if (__clear_ack_timer(chan)) |
| 4007 | set_bit(CONN_SEND_PBIT, &chan->conn_state); |
Gustavo F. Padovan | ef54fd9 | 2009-08-20 22:26:04 -0300 | [diff] [blame] | 4008 | |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 4009 | err = l2cap_send_srejframe(chan, tx_seq); |
| 4010 | if (err < 0) { |
| 4011 | l2cap_send_disconn_req(chan->conn, chan, -err); |
| 4012 | return err; |
| 4013 | } |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4014 | } |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 4015 | return 0; |
| 4016 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4017 | expected: |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 4018 | chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4019 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4020 | if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { |
Gustavo F. Padovan | 3b1a9f3 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 4021 | bt_cb(skb)->tx_seq = tx_seq; |
| 4022 | bt_cb(skb)->sar = sar; |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 4023 | __skb_queue_tail(&chan->srej_q, skb); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4024 | return 0; |
| 4025 | } |
| 4026 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 4027 | err = l2cap_reassemble_sdu(chan, skb, rx_control); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 4028 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 4029 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 4030 | if (err < 0) { |
| 4031 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
| 4032 | return err; |
| 4033 | } |
Gustavo F. Padovan | 2ece368 | 2010-06-16 17:21:44 -0300 | [diff] [blame] | 4034 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 4035 | if (__is_ctrl_final(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4036 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4037 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | 4ec10d9 | 2009-10-03 02:34:39 -0300 | [diff] [blame] | 4038 | } |
| 4039 | |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 4040 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4041 | chan->num_acked = (chan->num_acked + 1) % num_to_ack; |
| 4042 | if (chan->num_acked == num_to_ack - 1) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4043 | l2cap_send_ack(chan); |
Gustavo F. Padovan | 4d611e4 | 2011-06-23 19:30:48 -0300 | [diff] [blame] | 4044 | else |
| 4045 | __set_ack_timer(chan); |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 4046 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4047 | return 0; |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 4048 | |
| 4049 | drop: |
| 4050 | kfree_skb(skb); |
| 4051 | return 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4052 | } |
| 4053 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4054 | static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u32 rx_control) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4055 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4056 | BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4057 | __get_reqseq(chan, rx_control), rx_control); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 4058 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4059 | chan->expected_ack_seq = __get_reqseq(chan, rx_control); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 4060 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4061 | |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 4062 | if (__is_ctrl_poll(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4063 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 4064 | if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { |
| 4065 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4066 | (chan->unacked_frames > 0)) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 4067 | __set_retrans_timer(chan); |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 4068 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4069 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4070 | l2cap_send_srejtail(chan); |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 4071 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4072 | l2cap_send_i_or_rr_or_rnr(chan); |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 4073 | } |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4074 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 4075 | } else if (__is_ctrl_final(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4076 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4077 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4078 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4079 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4080 | |
| 4081 | } else { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4082 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4083 | (chan->unacked_frames > 0)) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 4084 | __set_retrans_timer(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4085 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4086 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 4087 | if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4088 | l2cap_send_ack(chan); |
Andrei Emeltchenko | 894718a | 2010-12-01 16:58:24 +0200 | [diff] [blame] | 4089 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4090 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4091 | } |
| 4092 | } |
| 4093 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4094 | static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u32 rx_control) |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4095 | { |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4096 | u16 tx_seq = __get_reqseq(chan, rx_control); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4097 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4098 | BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 4099 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4100 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4101 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 4102 | chan->expected_ack_seq = tx_seq; |
| 4103 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4104 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 4105 | if (__is_ctrl_final(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4106 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4107 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4108 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4109 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4110 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4111 | if (test_bit(CONN_WAIT_F, &chan->conn_state)) |
| 4112 | set_bit(CONN_REJ_ACT, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4113 | } |
| 4114 | } |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4115 | static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u32 rx_control) |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4116 | { |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4117 | u16 tx_seq = __get_reqseq(chan, rx_control); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4118 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4119 | BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 4120 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4121 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4122 | |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 4123 | if (__is_ctrl_poll(chan, rx_control)) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 4124 | chan->expected_ack_seq = tx_seq; |
| 4125 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 4126 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4127 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4128 | l2cap_retransmit_one_frame(chan, tx_seq); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 4129 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4130 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 4131 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4132 | if (test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4133 | chan->srej_save_reqseq = tx_seq; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4134 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4135 | } |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 4136 | } else if (__is_ctrl_final(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4137 | if (test_bit(CONN_SREJ_ACT, &chan->conn_state) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4138 | chan->srej_save_reqseq == tx_seq) |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4139 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4140 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4141 | l2cap_retransmit_one_frame(chan, tx_seq); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4142 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4143 | l2cap_retransmit_one_frame(chan, tx_seq); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4144 | if (test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4145 | chan->srej_save_reqseq = tx_seq; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4146 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4147 | } |
| 4148 | } |
| 4149 | } |
| 4150 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4151 | static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u32 rx_control) |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4152 | { |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4153 | u16 tx_seq = __get_reqseq(chan, rx_control); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4154 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4155 | BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, tx_seq, rx_control); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 4156 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4157 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 4158 | chan->expected_ack_seq = tx_seq; |
| 4159 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4160 | |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 4161 | if (__is_ctrl_poll(chan, rx_control)) |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4162 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 4163 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4164 | if (!test_bit(CONN_SREJ_SENT, &chan->conn_state)) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 4165 | __clear_retrans_timer(chan); |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 4166 | if (__is_ctrl_poll(chan, rx_control)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4167 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4168 | return; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4169 | } |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4170 | |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 4171 | if (__is_ctrl_poll(chan, rx_control)) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4172 | l2cap_send_srejtail(chan); |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4173 | } else { |
| 4174 | rx_control = __set_ctrl_super(chan, L2CAP_SUPER_RR); |
| 4175 | l2cap_send_sframe(chan, rx_control); |
| 4176 | } |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4177 | } |
| 4178 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4179 | static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u32 rx_control, struct sk_buff *skb) |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4180 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4181 | BT_DBG("chan %p rx_control 0x%8.8x len %d", chan, rx_control, skb->len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4182 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 4183 | if (__is_ctrl_final(chan, rx_control) && |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4184 | test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 4185 | __clear_monitor_timer(chan); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4186 | if (chan->unacked_frames > 0) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 4187 | __set_retrans_timer(chan); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4188 | clear_bit(CONN_WAIT_F, &chan->conn_state); |
Gustavo F. Padovan | 1d8f5d1 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 4189 | } |
| 4190 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4191 | switch (__get_ctrl_super(chan, rx_control)) { |
| 4192 | case L2CAP_SUPER_RR: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4193 | l2cap_data_channel_rrframe(chan, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4194 | break; |
| 4195 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4196 | case L2CAP_SUPER_REJ: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4197 | l2cap_data_channel_rejframe(chan, rx_control); |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 4198 | break; |
| 4199 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4200 | case L2CAP_SUPER_SREJ: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4201 | l2cap_data_channel_srejframe(chan, rx_control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4202 | break; |
| 4203 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4204 | case L2CAP_SUPER_RNR: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4205 | l2cap_data_channel_rnrframe(chan, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4206 | break; |
| 4207 | } |
| 4208 | |
Gustavo F. Padovan | faaebd1 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 4209 | kfree_skb(skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4210 | return 0; |
| 4211 | } |
| 4212 | |
Szymon Janc | cad8f1d0 | 2012-01-23 10:06:05 +0100 | [diff] [blame] | 4213 | static int l2cap_ertm_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4214 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4215 | u32 control; |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4216 | u16 req_seq; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4217 | int len, next_tx_seq_offset, req_seq_offset; |
| 4218 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4219 | control = __get_control(chan, skb->data); |
| 4220 | skb_pull(skb, __ctrl_size(chan)); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4221 | len = skb->len; |
| 4222 | |
| 4223 | /* |
| 4224 | * We can just drop the corrupted I-frame here. |
| 4225 | * Receiver will miss it and start proper recovery |
| 4226 | * procedures and ask retransmission. |
| 4227 | */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4228 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4229 | goto drop; |
| 4230 | |
Andrei Emeltchenko | 793c2f1 | 2011-10-11 13:37:48 +0300 | [diff] [blame] | 4231 | if (__is_sar_start(chan, control) && !__is_sframe(chan, control)) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 4232 | len -= L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4233 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4234 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 4235 | len -= L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4236 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4237 | if (len > chan->mps) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 4238 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4239 | goto drop; |
| 4240 | } |
| 4241 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4242 | req_seq = __get_reqseq(chan, control); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4243 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 4244 | req_seq_offset = __seq_offset(chan, req_seq, chan->expected_ack_seq); |
| 4245 | |
| 4246 | next_tx_seq_offset = __seq_offset(chan, chan->next_tx_seq, |
| 4247 | chan->expected_ack_seq); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4248 | |
| 4249 | /* check for invalid req-seq */ |
| 4250 | if (req_seq_offset > next_tx_seq_offset) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 4251 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4252 | goto drop; |
| 4253 | } |
| 4254 | |
Andrei Emeltchenko | 793c2f1 | 2011-10-11 13:37:48 +0300 | [diff] [blame] | 4255 | if (!__is_sframe(chan, control)) { |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4256 | if (len < 0) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 4257 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4258 | goto drop; |
| 4259 | } |
| 4260 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4261 | l2cap_data_channel_iframe(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4262 | } else { |
| 4263 | if (len != 0) { |
| 4264 | BT_ERR("%d", len); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 4265 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4266 | goto drop; |
| 4267 | } |
| 4268 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4269 | l2cap_data_channel_sframe(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4270 | } |
| 4271 | |
| 4272 | return 0; |
| 4273 | |
| 4274 | drop: |
| 4275 | kfree_skb(skb); |
| 4276 | return 0; |
| 4277 | } |
| 4278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4279 | static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) |
| 4280 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4281 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4282 | u32 control; |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 4283 | u16 tx_seq; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4284 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4285 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4286 | chan = l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4287 | if (!chan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4288 | BT_DBG("unknown cid 0x%4.4x", cid); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4289 | /* Drop packet and return */ |
| 4290 | kfree(skb); |
| 4291 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | } |
| 4293 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4294 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4295 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 4296 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4298 | if (chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4299 | goto drop; |
| 4300 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4301 | switch (chan->mode) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4302 | case L2CAP_MODE_BASIC: |
| 4303 | /* If socket recv buffers overflows we drop data here |
| 4304 | * which is *bad* because L2CAP has to be reliable. |
| 4305 | * But we don't have any other choice. L2CAP doesn't |
| 4306 | * provide flow control mechanism. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4308 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4309 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 4311 | if (!chan->ops->recv(chan->data, skb)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4312 | goto done; |
| 4313 | break; |
| 4314 | |
| 4315 | case L2CAP_MODE_ERTM: |
Andrei Emeltchenko | 5ef8cb9 | 2012-01-13 17:21:42 +0200 | [diff] [blame] | 4316 | l2cap_ertm_data_rcv(chan, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4317 | |
Andrei Emeltchenko | fcafde2 | 2009-12-22 15:58:08 +0200 | [diff] [blame] | 4318 | goto done; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4319 | |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4320 | case L2CAP_MODE_STREAMING: |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4321 | control = __get_control(chan, skb->data); |
| 4322 | skb_pull(skb, __ctrl_size(chan)); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4323 | len = skb->len; |
| 4324 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4325 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 2600008 | 2010-05-11 22:02:00 -0300 | [diff] [blame] | 4326 | goto drop; |
| 4327 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 4328 | if (__is_sar_start(chan, control)) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 4329 | len -= L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4330 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4331 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 4332 | len -= L2CAP_FCS_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4333 | |
Andrei Emeltchenko | 793c2f1 | 2011-10-11 13:37:48 +0300 | [diff] [blame] | 4334 | if (len > chan->mps || len < 0 || __is_sframe(chan, control)) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4335 | goto drop; |
| 4336 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 4337 | tx_seq = __get_txseq(chan, control); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4338 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 4339 | if (chan->expected_tx_seq != tx_seq) { |
| 4340 | /* Frame(s) missing - must discard partial SDU */ |
| 4341 | kfree_skb(chan->sdu); |
| 4342 | chan->sdu = NULL; |
| 4343 | chan->sdu_last_frag = NULL; |
| 4344 | chan->sdu_len = 0; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4345 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 4346 | /* TODO: Notify userland of missing data */ |
| 4347 | } |
| 4348 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 4349 | chan->expected_tx_seq = __next_seq(chan, tx_seq); |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 4350 | |
| 4351 | if (l2cap_reassemble_sdu(chan, skb, control) == -EMSGSIZE) |
| 4352 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4353 | |
| 4354 | goto done; |
| 4355 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4356 | default: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4357 | 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] | 4358 | break; |
| 4359 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4360 | |
| 4361 | drop: |
| 4362 | kfree_skb(skb); |
| 4363 | |
| 4364 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4365 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4367 | return 0; |
| 4368 | } |
| 4369 | |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 4370 | static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4371 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4372 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4373 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4374 | chan = l2cap_global_chan_by_psm(0, psm, conn->src); |
| 4375 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4376 | goto drop; |
| 4377 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame^] | 4378 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4379 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4380 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4381 | goto drop; |
| 4382 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 4383 | if (chan->imtu < skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | goto drop; |
| 4385 | |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 4386 | if (!chan->ops->recv(chan->data, skb)) |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame^] | 4387 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4388 | |
| 4389 | drop: |
| 4390 | kfree_skb(skb); |
| 4391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | return 0; |
| 4393 | } |
| 4394 | |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4395 | static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb) |
| 4396 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4397 | struct l2cap_chan *chan; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4398 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4399 | chan = l2cap_global_chan_by_scid(0, cid, conn->src); |
| 4400 | if (!chan) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4401 | goto drop; |
| 4402 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame^] | 4403 | BT_DBG("chan %p, len %d", chan, skb->len); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4404 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4405 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4406 | goto drop; |
| 4407 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 4408 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4409 | goto drop; |
| 4410 | |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 4411 | if (!chan->ops->recv(chan->data, skb)) |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame^] | 4412 | return 0; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4413 | |
| 4414 | drop: |
| 4415 | kfree_skb(skb); |
| 4416 | |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4417 | return 0; |
| 4418 | } |
| 4419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) |
| 4421 | { |
| 4422 | struct l2cap_hdr *lh = (void *) skb->data; |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 4423 | u16 cid, len; |
| 4424 | __le16 psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | |
| 4426 | skb_pull(skb, L2CAP_HDR_SIZE); |
| 4427 | cid = __le16_to_cpu(lh->cid); |
| 4428 | len = __le16_to_cpu(lh->len); |
| 4429 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4430 | if (len != skb->len) { |
| 4431 | kfree_skb(skb); |
| 4432 | return; |
| 4433 | } |
| 4434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | BT_DBG("len %d, cid 0x%4.4x", len, cid); |
| 4436 | |
| 4437 | switch (cid) { |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 4438 | case L2CAP_CID_LE_SIGNALING: |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 4439 | case L2CAP_CID_SIGNALING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4440 | l2cap_sig_channel(conn, skb); |
| 4441 | break; |
| 4442 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 4443 | case L2CAP_CID_CONN_LESS: |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 4444 | psm = get_unaligned_le16(skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4445 | skb_pull(skb, 2); |
| 4446 | l2cap_conless_channel(conn, psm, skb); |
| 4447 | break; |
| 4448 | |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4449 | case L2CAP_CID_LE_DATA: |
| 4450 | l2cap_att_channel(conn, cid, skb); |
| 4451 | break; |
| 4452 | |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 4453 | case L2CAP_CID_SMP: |
| 4454 | if (smp_sig_channel(conn, skb)) |
| 4455 | l2cap_conn_del(conn->hcon, EACCES); |
| 4456 | break; |
| 4457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4458 | default: |
| 4459 | l2cap_data_channel(conn, cid, skb); |
| 4460 | break; |
| 4461 | } |
| 4462 | } |
| 4463 | |
| 4464 | /* ---- L2CAP interface with lower layer (HCI) ---- */ |
| 4465 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4466 | int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4467 | { |
| 4468 | int exact = 0, lm1 = 0, lm2 = 0; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4469 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4471 | BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); |
| 4472 | |
| 4473 | /* Find listening sockets and check their link_mode */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4474 | read_lock(&chan_list_lock); |
| 4475 | list_for_each_entry(c, &chan_list, global_l) { |
| 4476 | struct sock *sk = c->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4477 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4478 | if (c->state != BT_LISTEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4479 | continue; |
| 4480 | |
| 4481 | if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4482 | lm1 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 4483 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4484 | lm1 |= HCI_LM_MASTER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | exact++; |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4486 | } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { |
| 4487 | lm2 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 4488 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4489 | lm2 |= HCI_LM_MASTER; |
| 4490 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | } |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4492 | read_unlock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | |
| 4494 | return exact ? lm1 : lm2; |
| 4495 | } |
| 4496 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4497 | int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4499 | struct l2cap_conn *conn; |
| 4500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); |
| 4502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4503 | if (!status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4504 | conn = l2cap_conn_add(hcon, status); |
| 4505 | if (conn) |
| 4506 | l2cap_conn_ready(conn); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4507 | } else |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 4508 | l2cap_conn_del(hcon, bt_to_errno(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | |
| 4510 | return 0; |
| 4511 | } |
| 4512 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4513 | int l2cap_disconn_ind(struct hci_conn *hcon) |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 4514 | { |
| 4515 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 4516 | |
| 4517 | BT_DBG("hcon %p", hcon); |
| 4518 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4519 | if (!conn) |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 4520 | return HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 4521 | return conn->disc_reason; |
| 4522 | } |
| 4523 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4524 | int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4525 | { |
| 4526 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 4527 | |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 4528 | l2cap_conn_del(hcon, bt_to_errno(reason)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4529 | return 0; |
| 4530 | } |
| 4531 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4532 | static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4533 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 4534 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
Marcel Holtmann | 255c760 | 2009-02-04 21:07:19 +0100 | [diff] [blame] | 4535 | return; |
| 4536 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4537 | if (encrypt == 0x00) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4538 | if (chan->sec_level == BT_SECURITY_MEDIUM) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 4539 | __clear_chan_timer(chan); |
Andrzej Kaczmarek | b83ddfe | 2012-01-04 12:10:42 +0100 | [diff] [blame] | 4540 | __set_chan_timer(chan, |
| 4541 | msecs_to_jiffies(L2CAP_ENC_TIMEOUT)); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4542 | } else if (chan->sec_level == BT_SECURITY_HIGH) |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 4543 | l2cap_chan_close(chan, ECONNREFUSED); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4544 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4545 | if (chan->sec_level == BT_SECURITY_MEDIUM) |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 4546 | __clear_chan_timer(chan); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4547 | } |
| 4548 | } |
| 4549 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4550 | int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4551 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 4552 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4553 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4554 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4555 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4556 | return 0; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4558 | BT_DBG("conn %p", conn); |
| 4559 | |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 4560 | if (hcon->type == LE_LINK) { |
| 4561 | smp_distribute_keys(conn, 0); |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 4562 | cancel_delayed_work(&conn->security_timer); |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 4563 | } |
| 4564 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4565 | mutex_lock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4567 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4568 | l2cap_chan_lock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 4570 | BT_DBG("chan->scid %d", chan->scid); |
| 4571 | |
| 4572 | if (chan->scid == L2CAP_CID_LE_DATA) { |
| 4573 | if (!status && encrypt) { |
| 4574 | chan->sec_level = hcon->sec_level; |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 4575 | l2cap_chan_ready(chan); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 4576 | } |
| 4577 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4578 | l2cap_chan_unlock(chan); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 4579 | continue; |
| 4580 | } |
| 4581 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4582 | if (test_bit(CONF_CONNECT_PEND, &chan->conf_state)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4583 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 4584 | continue; |
| 4585 | } |
| 4586 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4587 | if (!status && (chan->state == BT_CONNECTED || |
| 4588 | chan->state == BT_CONFIG)) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4589 | l2cap_check_encryption(chan, encrypt); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4590 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 9719f8a | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 4591 | continue; |
| 4592 | } |
| 4593 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4594 | if (chan->state == BT_CONNECT) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4595 | if (!status) { |
| 4596 | struct l2cap_conn_req req; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4597 | req.scid = cpu_to_le16(chan->scid); |
| 4598 | req.psm = chan->psm; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4599 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 4600 | chan->ident = l2cap_get_ident(conn); |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4601 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4602 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 4603 | l2cap_send_cmd(conn, chan->ident, |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4604 | L2CAP_CONN_REQ, sizeof(req), &req); |
| 4605 | } else { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 4606 | __clear_chan_timer(chan); |
Andrzej Kaczmarek | b83ddfe | 2012-01-04 12:10:42 +0100 | [diff] [blame] | 4607 | __set_chan_timer(chan, |
| 4608 | msecs_to_jiffies(L2CAP_DISC_TIMEOUT)); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4609 | } |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4610 | } else if (chan->state == BT_CONNECT2) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4611 | struct sock *sk = chan->sk; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4612 | struct l2cap_conn_rsp rsp; |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4613 | __u16 res, stat; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4614 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4615 | lock_sock(sk); |
| 4616 | |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4617 | if (!status) { |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4618 | if (bt_sk(sk)->defer_setup) { |
| 4619 | struct sock *parent = bt_sk(sk)->parent; |
| 4620 | res = L2CAP_CR_PEND; |
| 4621 | stat = L2CAP_CS_AUTHOR_PEND; |
Ilia Kolomisnky | 05e9a2f | 2011-07-15 18:30:21 +0000 | [diff] [blame] | 4622 | if (parent) |
| 4623 | parent->sk_data_ready(parent, 0); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4624 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 4625 | __l2cap_state_change(chan, BT_CONFIG); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4626 | res = L2CAP_CR_SUCCESS; |
| 4627 | stat = L2CAP_CS_NO_INFO; |
| 4628 | } |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4629 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 4630 | __l2cap_state_change(chan, BT_DISCONN); |
Andrzej Kaczmarek | b83ddfe | 2012-01-04 12:10:42 +0100 | [diff] [blame] | 4631 | __set_chan_timer(chan, |
| 4632 | msecs_to_jiffies(L2CAP_DISC_TIMEOUT)); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4633 | res = L2CAP_CR_SEC_BLOCK; |
| 4634 | stat = L2CAP_CS_NO_INFO; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4635 | } |
| 4636 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4637 | release_sock(sk); |
| 4638 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4639 | rsp.scid = cpu_to_le16(chan->dcid); |
| 4640 | rsp.dcid = cpu_to_le16(chan->scid); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4641 | rsp.result = cpu_to_le16(res); |
| 4642 | rsp.status = cpu_to_le16(stat); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 4643 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
| 4644 | sizeof(rsp), &rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | } |
| 4646 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4647 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4648 | } |
| 4649 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4650 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4651 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4652 | return 0; |
| 4653 | } |
| 4654 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4655 | 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] | 4656 | { |
| 4657 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 4658 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 4659 | if (!conn) |
| 4660 | conn = l2cap_conn_add(hcon, 0); |
| 4661 | |
| 4662 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4663 | goto drop; |
| 4664 | |
| 4665 | BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); |
| 4666 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 4667 | if (!(flags & ACL_CONT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4668 | struct l2cap_hdr *hdr; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4669 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4670 | u16 cid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | int len; |
| 4672 | |
| 4673 | if (conn->rx_len) { |
| 4674 | BT_ERR("Unexpected start frame (len %d)", skb->len); |
| 4675 | kfree_skb(conn->rx_skb); |
| 4676 | conn->rx_skb = NULL; |
| 4677 | conn->rx_len = 0; |
| 4678 | l2cap_conn_unreliable(conn, ECOMM); |
| 4679 | } |
| 4680 | |
Andrei Emeltchenko | aae7fe2 | 2010-09-15 14:28:43 +0300 | [diff] [blame] | 4681 | /* Start fragment always begin with Basic L2CAP header */ |
| 4682 | if (skb->len < L2CAP_HDR_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | BT_ERR("Frame is too short (len %d)", skb->len); |
| 4684 | l2cap_conn_unreliable(conn, ECOMM); |
| 4685 | goto drop; |
| 4686 | } |
| 4687 | |
| 4688 | hdr = (struct l2cap_hdr *) skb->data; |
| 4689 | len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4690 | cid = __le16_to_cpu(hdr->cid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4691 | |
| 4692 | if (len == skb->len) { |
| 4693 | /* Complete frame received */ |
| 4694 | l2cap_recv_frame(conn, skb); |
| 4695 | return 0; |
| 4696 | } |
| 4697 | |
| 4698 | BT_DBG("Start: total len %d, frag len %d", len, skb->len); |
| 4699 | |
| 4700 | if (skb->len > len) { |
| 4701 | BT_ERR("Frame is too long (len %d, expected len %d)", |
| 4702 | skb->len, len); |
| 4703 | l2cap_conn_unreliable(conn, ECOMM); |
| 4704 | goto drop; |
| 4705 | } |
| 4706 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4707 | chan = l2cap_get_chan_by_scid(conn, cid); |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4708 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4709 | if (chan && chan->sk) { |
| 4710 | struct sock *sk = chan->sk; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4711 | lock_sock(sk); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4712 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4713 | if (chan->imtu < len - L2CAP_HDR_SIZE) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4714 | BT_ERR("Frame exceeding recv MTU (len %d, " |
| 4715 | "MTU %d)", len, |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4716 | chan->imtu); |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 4717 | release_sock(sk); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4718 | l2cap_conn_unreliable(conn, ECOMM); |
| 4719 | goto drop; |
| 4720 | } |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 4721 | release_sock(sk); |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4722 | } |
| 4723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | /* Allocate skb for the complete frame (with header) */ |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 4725 | conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); |
| 4726 | if (!conn->rx_skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | goto drop; |
| 4728 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 4729 | skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4730 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4731 | conn->rx_len = len - skb->len; |
| 4732 | } else { |
| 4733 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
| 4734 | |
| 4735 | if (!conn->rx_len) { |
| 4736 | BT_ERR("Unexpected continuation frame (len %d)", skb->len); |
| 4737 | l2cap_conn_unreliable(conn, ECOMM); |
| 4738 | goto drop; |
| 4739 | } |
| 4740 | |
| 4741 | if (skb->len > conn->rx_len) { |
| 4742 | BT_ERR("Fragment is too long (len %d, expected %d)", |
| 4743 | skb->len, conn->rx_len); |
| 4744 | kfree_skb(conn->rx_skb); |
| 4745 | conn->rx_skb = NULL; |
| 4746 | conn->rx_len = 0; |
| 4747 | l2cap_conn_unreliable(conn, ECOMM); |
| 4748 | goto drop; |
| 4749 | } |
| 4750 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 4751 | skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len), |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 4752 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | conn->rx_len -= skb->len; |
| 4754 | |
| 4755 | if (!conn->rx_len) { |
| 4756 | /* Complete frame received */ |
| 4757 | l2cap_recv_frame(conn, conn->rx_skb); |
| 4758 | conn->rx_skb = NULL; |
| 4759 | } |
| 4760 | } |
| 4761 | |
| 4762 | drop: |
| 4763 | kfree_skb(skb); |
| 4764 | return 0; |
| 4765 | } |
| 4766 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4767 | static int l2cap_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4768 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4769 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 4771 | read_lock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4772 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4773 | list_for_each_entry(c, &chan_list, global_l) { |
| 4774 | struct sock *sk = c->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4775 | |
Gustavo F. Padovan | 903d343 | 2011-02-10 14:16:06 -0200 | [diff] [blame] | 4776 | seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4777 | batostr(&bt_sk(sk)->src), |
| 4778 | batostr(&bt_sk(sk)->dst), |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4779 | c->state, __le16_to_cpu(c->psm), |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4780 | c->scid, c->dcid, c->imtu, c->omtu, |
| 4781 | c->sec_level, c->mode); |
Andrei Emeltchenko | 61e1b4b | 2012-01-19 11:19:50 +0200 | [diff] [blame] | 4782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 4784 | read_unlock(&chan_list_lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 4785 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4786 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4787 | } |
| 4788 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4789 | static int l2cap_debugfs_open(struct inode *inode, struct file *file) |
| 4790 | { |
| 4791 | return single_open(file, l2cap_debugfs_show, inode->i_private); |
| 4792 | } |
| 4793 | |
| 4794 | static const struct file_operations l2cap_debugfs_fops = { |
| 4795 | .open = l2cap_debugfs_open, |
| 4796 | .read = seq_read, |
| 4797 | .llseek = seq_lseek, |
| 4798 | .release = single_release, |
| 4799 | }; |
| 4800 | |
| 4801 | static struct dentry *l2cap_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4802 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 4803 | int __init l2cap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | { |
| 4805 | int err; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 4806 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4807 | err = l2cap_init_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4808 | if (err < 0) |
| 4809 | return err; |
| 4810 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4811 | if (bt_debugfs) { |
| 4812 | l2cap_debugfs = debugfs_create_file("l2cap", 0444, |
| 4813 | bt_debugfs, NULL, &l2cap_debugfs_fops); |
| 4814 | if (!l2cap_debugfs) |
| 4815 | BT_ERR("Failed to create L2CAP debug file"); |
| 4816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4818 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | } |
| 4820 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 4821 | void l2cap_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4823 | debugfs_remove(l2cap_debugfs); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4824 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | } |
| 4826 | |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 4827 | module_param(disable_ertm, bool, 0644); |
| 4828 | MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); |